]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/btrfs/inode.c
Btrfs: checksum file data at bio submission time instead of during writepage
[karo-tx-linux.git] / fs / btrfs / inode.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/bio.h>
20 #include <linux/buffer_head.h>
21 #include <linux/fs.h>
22 #include <linux/pagemap.h>
23 #include <linux/highmem.h>
24 #include <linux/time.h>
25 #include <linux/init.h>
26 #include <linux/string.h>
27 #include <linux/smp_lock.h>
28 #include <linux/backing-dev.h>
29 #include <linux/mpage.h>
30 #include <linux/swap.h>
31 #include <linux/writeback.h>
32 #include <linux/statfs.h>
33 #include <linux/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/version.h>
36 #include <linux/xattr.h>
37 #include "ctree.h"
38 #include "disk-io.h"
39 #include "transaction.h"
40 #include "btrfs_inode.h"
41 #include "ioctl.h"
42 #include "print-tree.h"
43
44 struct btrfs_iget_args {
45         u64 ino;
46         struct btrfs_root *root;
47 };
48
49 static struct inode_operations btrfs_dir_inode_operations;
50 static struct inode_operations btrfs_symlink_inode_operations;
51 static struct inode_operations btrfs_dir_ro_inode_operations;
52 static struct inode_operations btrfs_special_inode_operations;
53 static struct inode_operations btrfs_file_inode_operations;
54 static struct address_space_operations btrfs_aops;
55 static struct address_space_operations btrfs_symlink_aops;
56 static struct file_operations btrfs_dir_file_operations;
57 static struct extent_io_ops btrfs_extent_io_ops;
58
59 static struct kmem_cache *btrfs_inode_cachep;
60 struct kmem_cache *btrfs_trans_handle_cachep;
61 struct kmem_cache *btrfs_transaction_cachep;
62 struct kmem_cache *btrfs_bit_radix_cachep;
63 struct kmem_cache *btrfs_path_cachep;
64
65 #define S_SHIFT 12
66 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
67         [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
68         [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
69         [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
70         [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
71         [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
72         [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
73         [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
74 };
75
76 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
77                            int for_del)
78 {
79         u64 total = btrfs_super_total_bytes(&root->fs_info->super_copy);
80         u64 used = btrfs_super_bytes_used(&root->fs_info->super_copy);
81         u64 thresh;
82         int ret = 0;
83
84         if (for_del)
85                 thresh = total * 90;
86         else
87                 thresh = total * 85;
88
89         do_div(thresh, 100);
90
91         spin_lock(&root->fs_info->delalloc_lock);
92         if (used + root->fs_info->delalloc_bytes + num_required > thresh)
93                 ret = -ENOSPC;
94         spin_unlock(&root->fs_info->delalloc_lock);
95         return ret;
96 }
97
98 static int cow_file_range(struct inode *inode, u64 start, u64 end)
99 {
100         struct btrfs_root *root = BTRFS_I(inode)->root;
101         struct btrfs_trans_handle *trans;
102         u64 alloc_hint = 0;
103         u64 num_bytes;
104         u64 cur_alloc_size;
105         u64 blocksize = root->sectorsize;
106         u64 orig_start = start;
107         u64 orig_num_bytes;
108         struct btrfs_key ins;
109         int ret;
110
111         trans = btrfs_start_transaction(root, 1);
112         BUG_ON(!trans);
113         btrfs_set_trans_block_group(trans, inode);
114
115         num_bytes = (end - start + blocksize) & ~(blocksize - 1);
116         num_bytes = max(blocksize,  num_bytes);
117         ret = btrfs_drop_extents(trans, root, inode,
118                                  start, start + num_bytes, start, &alloc_hint);
119         orig_num_bytes = num_bytes;
120
121         if (alloc_hint == EXTENT_MAP_INLINE)
122                 goto out;
123
124         while(num_bytes > 0) {
125                 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
126                 ret = btrfs_alloc_extent(trans, root, cur_alloc_size,
127                                          root->root_key.objectid,
128                                          trans->transid,
129                                          inode->i_ino, start, 0,
130                                          alloc_hint, (u64)-1, &ins, 1);
131                 if (ret) {
132                         WARN_ON(1);
133                         goto out;
134                 }
135                 ret = btrfs_insert_file_extent(trans, root, inode->i_ino,
136                                                start, ins.objectid, ins.offset,
137                                                ins.offset);
138                 inode->i_blocks += ins.offset >> 9;
139                 btrfs_check_file(root, inode);
140                 num_bytes -= cur_alloc_size;
141                 alloc_hint = ins.objectid + ins.offset;
142                 start += cur_alloc_size;
143         }
144         btrfs_drop_extent_cache(inode, orig_start,
145                                 orig_start + orig_num_bytes - 1);
146         btrfs_add_ordered_inode(inode);
147         btrfs_update_inode(trans, root, inode);
148 out:
149         btrfs_end_transaction(trans, root);
150         return ret;
151 }
152
153 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
154 {
155         u64 extent_start;
156         u64 extent_end;
157         u64 bytenr;
158         u64 cow_end;
159         u64 loops = 0;
160         u64 total_fs_bytes;
161         struct btrfs_root *root = BTRFS_I(inode)->root;
162         struct extent_buffer *leaf;
163         int found_type;
164         struct btrfs_path *path;
165         struct btrfs_file_extent_item *item;
166         int ret;
167         int err;
168         struct btrfs_key found_key;
169
170         total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
171         path = btrfs_alloc_path();
172         BUG_ON(!path);
173 again:
174         ret = btrfs_lookup_file_extent(NULL, root, path,
175                                        inode->i_ino, start, 0);
176         if (ret < 0) {
177                 btrfs_free_path(path);
178                 return ret;
179         }
180
181         cow_end = end;
182         if (ret != 0) {
183                 if (path->slots[0] == 0)
184                         goto not_found;
185                 path->slots[0]--;
186         }
187
188         leaf = path->nodes[0];
189         item = btrfs_item_ptr(leaf, path->slots[0],
190                               struct btrfs_file_extent_item);
191
192         /* are we inside the extent that was found? */
193         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
194         found_type = btrfs_key_type(&found_key);
195         if (found_key.objectid != inode->i_ino ||
196             found_type != BTRFS_EXTENT_DATA_KEY) {
197                 goto not_found;
198         }
199
200         found_type = btrfs_file_extent_type(leaf, item);
201         extent_start = found_key.offset;
202         if (found_type == BTRFS_FILE_EXTENT_REG) {
203                 u64 extent_num_bytes;
204
205                 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
206                 extent_end = extent_start + extent_num_bytes;
207                 err = 0;
208
209                 if (loops && start != extent_start)
210                         goto not_found;
211
212                 if (start < extent_start || start >= extent_end)
213                         goto not_found;
214
215                 cow_end = min(end, extent_end - 1);
216                 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
217                 if (bytenr == 0)
218                         goto not_found;
219
220                 /*
221                  * we may be called by the resizer, make sure we're inside
222                  * the limits of the FS
223                  */
224                 if (bytenr + extent_num_bytes > total_fs_bytes)
225                         goto not_found;
226
227                 if (btrfs_count_snapshots_in_path(root, path, bytenr) != 1) {
228                         goto not_found;
229                 }
230
231                 start = extent_end;
232         } else {
233                 goto not_found;
234         }
235 loop:
236         if (start > end) {
237                 btrfs_free_path(path);
238                 return 0;
239         }
240         btrfs_release_path(root, path);
241         loops++;
242         goto again;
243
244 not_found:
245         cow_file_range(inode, start, cow_end);
246         start = cow_end + 1;
247         goto loop;
248 }
249
250 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
251 {
252         struct btrfs_root *root = BTRFS_I(inode)->root;
253         int ret;
254         mutex_lock(&root->fs_info->fs_mutex);
255         if (btrfs_test_opt(root, NODATACOW) ||
256             btrfs_test_flag(inode, NODATACOW))
257                 ret = run_delalloc_nocow(inode, start, end);
258         else
259                 ret = cow_file_range(inode, start, end);
260
261         mutex_unlock(&root->fs_info->fs_mutex);
262         return ret;
263 }
264
265 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
266                        unsigned long old, unsigned long bits)
267 {
268         if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
269                 struct btrfs_root *root = BTRFS_I(inode)->root;
270                 spin_lock(&root->fs_info->delalloc_lock);
271                 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
272                 root->fs_info->delalloc_bytes += end - start + 1;
273                 spin_unlock(&root->fs_info->delalloc_lock);
274         }
275         return 0;
276 }
277
278 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
279                          unsigned long old, unsigned long bits)
280 {
281         if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
282                 struct btrfs_root *root = BTRFS_I(inode)->root;
283                 spin_lock(&root->fs_info->delalloc_lock);
284                 if (end - start + 1 > root->fs_info->delalloc_bytes) {
285                         printk("warning: delalloc account %Lu %Lu\n",
286                                end - start + 1, root->fs_info->delalloc_bytes);
287                         root->fs_info->delalloc_bytes = 0;
288                         BTRFS_I(inode)->delalloc_bytes = 0;
289                 } else {
290                         root->fs_info->delalloc_bytes -= end - start + 1;
291                         BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
292                 }
293                 spin_unlock(&root->fs_info->delalloc_lock);
294         }
295         return 0;
296 }
297
298 int btrfs_submit_bio_hook(int rw, struct bio *bio)
299 {
300         // struct bio_vec *bvec = bio->bi_io_vec + bio->bi_vcnt - 1;
301         struct bio_vec *bvec = bio->bi_io_vec;
302         struct inode *inode = bvec->bv_page->mapping->host;
303         struct btrfs_root *root = BTRFS_I(inode)->root;
304         struct btrfs_trans_handle *trans;
305         int ret = 0;
306
307         if (rw != WRITE)
308                 return 0;
309
310         if (btrfs_test_opt(root, NODATASUM) ||
311             btrfs_test_flag(inode, NODATASUM))
312                 return 0;
313
314         mutex_lock(&root->fs_info->fs_mutex);
315         trans = btrfs_start_transaction(root, 1);
316         btrfs_set_trans_block_group(trans, inode);
317         btrfs_csum_file_blocks(trans, root, inode, bio);
318         ret = btrfs_end_transaction(trans, root);
319         BUG_ON(ret);
320         mutex_unlock(&root->fs_info->fs_mutex);
321         return ret;
322 }
323 #if 0
324 int btrfs_writepage_io_hook(struct page *page, u64 start, u64 end)
325 {
326         struct inode *inode = page->mapping->host;
327         struct btrfs_root *root = BTRFS_I(inode)->root;
328         struct btrfs_trans_handle *trans;
329         char *kaddr;
330         int ret = 0;
331         u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
332         size_t offset = start - page_start;
333         if (btrfs_test_opt(root, NODATASUM) ||
334             btrfs_test_flag(inode, NODATASUM))
335                 return 0;
336         mutex_lock(&root->fs_info->fs_mutex);
337         trans = btrfs_start_transaction(root, 1);
338         btrfs_set_trans_block_group(trans, inode);
339         kaddr = kmap(page);
340         btrfs_csum_file_block(trans, root, inode, inode->i_ino,
341                               start, kaddr + offset, end - start + 1);
342         kunmap(page);
343         ret = btrfs_end_transaction(trans, root);
344         BUG_ON(ret);
345         mutex_unlock(&root->fs_info->fs_mutex);
346         return ret;
347 }
348 #endif
349 int btrfs_readpage_io_hook(struct page *page, u64 start, u64 end)
350 {
351         int ret = 0;
352         struct inode *inode = page->mapping->host;
353         struct btrfs_root *root = BTRFS_I(inode)->root;
354         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
355         struct btrfs_csum_item *item;
356         struct btrfs_path *path = NULL;
357         u32 csum;
358         if (btrfs_test_opt(root, NODATASUM) ||
359             btrfs_test_flag(inode, NODATASUM))
360                 return 0;
361         mutex_lock(&root->fs_info->fs_mutex);
362         path = btrfs_alloc_path();
363         item = btrfs_lookup_csum(NULL, root, path, inode->i_ino, start, 0);
364         if (IS_ERR(item)) {
365                 ret = PTR_ERR(item);
366                 /* a csum that isn't present is a preallocated region. */
367                 if (ret == -ENOENT || ret == -EFBIG)
368                         ret = 0;
369                 csum = 0;
370                 printk("no csum found for inode %lu start %Lu\n", inode->i_ino, start);
371                 goto out;
372         }
373         read_extent_buffer(path->nodes[0], &csum, (unsigned long)item,
374                            BTRFS_CRC32_SIZE);
375         set_state_private(io_tree, start, csum);
376 out:
377         if (path)
378                 btrfs_free_path(path);
379         mutex_unlock(&root->fs_info->fs_mutex);
380         return ret;
381 }
382
383 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
384                                struct extent_state *state)
385 {
386         size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
387         struct inode *inode = page->mapping->host;
388         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
389         char *kaddr;
390         u64 private = ~(u32)0;
391         int ret;
392         struct btrfs_root *root = BTRFS_I(inode)->root;
393         u32 csum = ~(u32)0;
394         unsigned long flags;
395
396         if (btrfs_test_opt(root, NODATASUM) ||
397             btrfs_test_flag(inode, NODATASUM))
398                 return 0;
399         if (state && state->start == start) {
400                 private = state->private;
401                 ret = 0;
402         } else {
403                 ret = get_state_private(io_tree, start, &private);
404         }
405         local_irq_save(flags);
406         kaddr = kmap_atomic(page, KM_IRQ0);
407         if (ret) {
408                 goto zeroit;
409         }
410         csum = btrfs_csum_data(root, kaddr + offset, csum,  end - start + 1);
411         btrfs_csum_final(csum, (char *)&csum);
412         if (csum != private) {
413                 goto zeroit;
414         }
415         kunmap_atomic(kaddr, KM_IRQ0);
416         local_irq_restore(flags);
417         return 0;
418
419 zeroit:
420         printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
421                page->mapping->host->i_ino, (unsigned long long)start, csum,
422                private);
423         memset(kaddr + offset, 1, end - start + 1);
424         flush_dcache_page(page);
425         kunmap_atomic(kaddr, KM_IRQ0);
426         local_irq_restore(flags);
427         return 0;
428 }
429
430 void btrfs_read_locked_inode(struct inode *inode)
431 {
432         struct btrfs_path *path;
433         struct extent_buffer *leaf;
434         struct btrfs_inode_item *inode_item;
435         struct btrfs_inode_timespec *tspec;
436         struct btrfs_root *root = BTRFS_I(inode)->root;
437         struct btrfs_key location;
438         u64 alloc_group_block;
439         u32 rdev;
440         int ret;
441
442         path = btrfs_alloc_path();
443         BUG_ON(!path);
444         mutex_lock(&root->fs_info->fs_mutex);
445         memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
446
447         ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
448         if (ret)
449                 goto make_bad;
450
451         leaf = path->nodes[0];
452         inode_item = btrfs_item_ptr(leaf, path->slots[0],
453                                     struct btrfs_inode_item);
454
455         inode->i_mode = btrfs_inode_mode(leaf, inode_item);
456         inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
457         inode->i_uid = btrfs_inode_uid(leaf, inode_item);
458         inode->i_gid = btrfs_inode_gid(leaf, inode_item);
459         inode->i_size = btrfs_inode_size(leaf, inode_item);
460
461         tspec = btrfs_inode_atime(inode_item);
462         inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
463         inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
464
465         tspec = btrfs_inode_mtime(inode_item);
466         inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
467         inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
468
469         tspec = btrfs_inode_ctime(inode_item);
470         inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
471         inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
472
473         inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
474         inode->i_generation = btrfs_inode_generation(leaf, inode_item);
475         inode->i_rdev = 0;
476         rdev = btrfs_inode_rdev(leaf, inode_item);
477
478         alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
479         BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
480                                                        alloc_group_block);
481         BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
482         if (!BTRFS_I(inode)->block_group) {
483                 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
484                                                          NULL, 0, 0, 0);
485         }
486         btrfs_free_path(path);
487         inode_item = NULL;
488
489         mutex_unlock(&root->fs_info->fs_mutex);
490
491         switch (inode->i_mode & S_IFMT) {
492         case S_IFREG:
493                 inode->i_mapping->a_ops = &btrfs_aops;
494                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
495                 inode->i_fop = &btrfs_file_operations;
496                 inode->i_op = &btrfs_file_inode_operations;
497                 break;
498         case S_IFDIR:
499                 inode->i_fop = &btrfs_dir_file_operations;
500                 if (root == root->fs_info->tree_root)
501                         inode->i_op = &btrfs_dir_ro_inode_operations;
502                 else
503                         inode->i_op = &btrfs_dir_inode_operations;
504                 break;
505         case S_IFLNK:
506                 inode->i_op = &btrfs_symlink_inode_operations;
507                 inode->i_mapping->a_ops = &btrfs_symlink_aops;
508                 break;
509         default:
510                 init_special_inode(inode, inode->i_mode, rdev);
511                 break;
512         }
513         return;
514
515 make_bad:
516         btrfs_release_path(root, path);
517         btrfs_free_path(path);
518         mutex_unlock(&root->fs_info->fs_mutex);
519         make_bad_inode(inode);
520 }
521
522 static void fill_inode_item(struct extent_buffer *leaf,
523                             struct btrfs_inode_item *item,
524                             struct inode *inode)
525 {
526         btrfs_set_inode_uid(leaf, item, inode->i_uid);
527         btrfs_set_inode_gid(leaf, item, inode->i_gid);
528         btrfs_set_inode_size(leaf, item, inode->i_size);
529         btrfs_set_inode_mode(leaf, item, inode->i_mode);
530         btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
531
532         btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
533                                inode->i_atime.tv_sec);
534         btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
535                                 inode->i_atime.tv_nsec);
536
537         btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
538                                inode->i_mtime.tv_sec);
539         btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
540                                 inode->i_mtime.tv_nsec);
541
542         btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
543                                inode->i_ctime.tv_sec);
544         btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
545                                 inode->i_ctime.tv_nsec);
546
547         btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
548         btrfs_set_inode_generation(leaf, item, inode->i_generation);
549         btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
550         btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
551         btrfs_set_inode_block_group(leaf, item,
552                                     BTRFS_I(inode)->block_group->key.objectid);
553 }
554
555 int btrfs_update_inode(struct btrfs_trans_handle *trans,
556                               struct btrfs_root *root,
557                               struct inode *inode)
558 {
559         struct btrfs_inode_item *inode_item;
560         struct btrfs_path *path;
561         struct extent_buffer *leaf;
562         int ret;
563
564         path = btrfs_alloc_path();
565         BUG_ON(!path);
566         ret = btrfs_lookup_inode(trans, root, path,
567                                  &BTRFS_I(inode)->location, 1);
568         if (ret) {
569                 if (ret > 0)
570                         ret = -ENOENT;
571                 goto failed;
572         }
573
574         leaf = path->nodes[0];
575         inode_item = btrfs_item_ptr(leaf, path->slots[0],
576                                   struct btrfs_inode_item);
577
578         fill_inode_item(leaf, inode_item, inode);
579         btrfs_mark_buffer_dirty(leaf);
580         btrfs_set_inode_last_trans(trans, inode);
581         ret = 0;
582 failed:
583         btrfs_release_path(root, path);
584         btrfs_free_path(path);
585         return ret;
586 }
587
588
589 static int btrfs_unlink_trans(struct btrfs_trans_handle *trans,
590                               struct btrfs_root *root,
591                               struct inode *dir,
592                               struct dentry *dentry)
593 {
594         struct btrfs_path *path;
595         const char *name = dentry->d_name.name;
596         int name_len = dentry->d_name.len;
597         int ret = 0;
598         struct extent_buffer *leaf;
599         struct btrfs_dir_item *di;
600         struct btrfs_key key;
601
602         path = btrfs_alloc_path();
603         if (!path) {
604                 ret = -ENOMEM;
605                 goto err;
606         }
607
608         di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
609                                     name, name_len, -1);
610         if (IS_ERR(di)) {
611                 ret = PTR_ERR(di);
612                 goto err;
613         }
614         if (!di) {
615                 ret = -ENOENT;
616                 goto err;
617         }
618         leaf = path->nodes[0];
619         btrfs_dir_item_key_to_cpu(leaf, di, &key);
620         ret = btrfs_delete_one_dir_name(trans, root, path, di);
621         if (ret)
622                 goto err;
623         btrfs_release_path(root, path);
624
625         di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
626                                          key.objectid, name, name_len, -1);
627         if (IS_ERR(di)) {
628                 ret = PTR_ERR(di);
629                 goto err;
630         }
631         if (!di) {
632                 ret = -ENOENT;
633                 goto err;
634         }
635         ret = btrfs_delete_one_dir_name(trans, root, path, di);
636
637         dentry->d_inode->i_ctime = dir->i_ctime;
638         ret = btrfs_del_inode_ref(trans, root, name, name_len,
639                                   dentry->d_inode->i_ino,
640                                   dentry->d_parent->d_inode->i_ino);
641         if (ret) {
642                 printk("failed to delete reference to %.*s, "
643                        "inode %lu parent %lu\n", name_len, name,
644                        dentry->d_inode->i_ino,
645                        dentry->d_parent->d_inode->i_ino);
646         }
647 err:
648         btrfs_free_path(path);
649         if (!ret) {
650                 dir->i_size -= name_len * 2;
651                 dir->i_mtime = dir->i_ctime = CURRENT_TIME;
652                 btrfs_update_inode(trans, root, dir);
653 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
654                 dentry->d_inode->i_nlink--;
655 #else
656                 drop_nlink(dentry->d_inode);
657 #endif
658                 ret = btrfs_update_inode(trans, root, dentry->d_inode);
659                 dir->i_sb->s_dirt = 1;
660         }
661         return ret;
662 }
663
664 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
665 {
666         struct btrfs_root *root;
667         struct btrfs_trans_handle *trans;
668         struct inode *inode = dentry->d_inode;
669         int ret;
670         unsigned long nr = 0;
671
672         root = BTRFS_I(dir)->root;
673         mutex_lock(&root->fs_info->fs_mutex);
674
675         ret = btrfs_check_free_space(root, 1, 1);
676         if (ret)
677                 goto fail;
678
679         trans = btrfs_start_transaction(root, 1);
680
681         btrfs_set_trans_block_group(trans, dir);
682         ret = btrfs_unlink_trans(trans, root, dir, dentry);
683         nr = trans->blocks_used;
684
685         if (inode->i_nlink == 0) {
686                 int found;
687                 /* if the inode isn't linked anywhere,
688                  * we don't need to worry about
689                  * data=ordered
690                  */
691                 found = btrfs_del_ordered_inode(inode);
692                 if (found == 1) {
693                         atomic_dec(&inode->i_count);
694                 }
695         }
696
697         btrfs_end_transaction(trans, root);
698 fail:
699         mutex_unlock(&root->fs_info->fs_mutex);
700         btrfs_btree_balance_dirty(root, nr);
701         btrfs_throttle(root);
702         return ret;
703 }
704
705 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
706 {
707         struct inode *inode = dentry->d_inode;
708         int err = 0;
709         int ret;
710         struct btrfs_root *root = BTRFS_I(dir)->root;
711         struct btrfs_trans_handle *trans;
712         unsigned long nr = 0;
713
714         if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
715                 return -ENOTEMPTY;
716
717         mutex_lock(&root->fs_info->fs_mutex);
718         ret = btrfs_check_free_space(root, 1, 1);
719         if (ret)
720                 goto fail;
721
722         trans = btrfs_start_transaction(root, 1);
723         btrfs_set_trans_block_group(trans, dir);
724
725         /* now the directory is empty */
726         err = btrfs_unlink_trans(trans, root, dir, dentry);
727         if (!err) {
728                 inode->i_size = 0;
729         }
730
731         nr = trans->blocks_used;
732         ret = btrfs_end_transaction(trans, root);
733 fail:
734         mutex_unlock(&root->fs_info->fs_mutex);
735         btrfs_btree_balance_dirty(root, nr);
736         btrfs_throttle(root);
737
738         if (ret && !err)
739                 err = ret;
740         return err;
741 }
742
743 /*
744  * this can truncate away extent items, csum items and directory items.
745  * It starts at a high offset and removes keys until it can't find
746  * any higher than i_size.
747  *
748  * csum items that cross the new i_size are truncated to the new size
749  * as well.
750  */
751 static int btrfs_truncate_in_trans(struct btrfs_trans_handle *trans,
752                                    struct btrfs_root *root,
753                                    struct inode *inode,
754                                    u32 min_type)
755 {
756         int ret;
757         struct btrfs_path *path;
758         struct btrfs_key key;
759         struct btrfs_key found_key;
760         u32 found_type;
761         struct extent_buffer *leaf;
762         struct btrfs_file_extent_item *fi;
763         u64 extent_start = 0;
764         u64 extent_num_bytes = 0;
765         u64 item_end = 0;
766         u64 root_gen = 0;
767         u64 root_owner = 0;
768         int found_extent;
769         int del_item;
770         int pending_del_nr = 0;
771         int pending_del_slot = 0;
772         int extent_type = -1;
773
774         btrfs_drop_extent_cache(inode, inode->i_size, (u64)-1);
775         path = btrfs_alloc_path();
776         path->reada = -1;
777         BUG_ON(!path);
778
779         /* FIXME, add redo link to tree so we don't leak on crash */
780         key.objectid = inode->i_ino;
781         key.offset = (u64)-1;
782         key.type = (u8)-1;
783
784         btrfs_init_path(path);
785 search_again:
786         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
787         if (ret < 0) {
788                 goto error;
789         }
790         if (ret > 0) {
791                 BUG_ON(path->slots[0] == 0);
792                 path->slots[0]--;
793         }
794
795         while(1) {
796                 fi = NULL;
797                 leaf = path->nodes[0];
798                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
799                 found_type = btrfs_key_type(&found_key);
800
801                 if (found_key.objectid != inode->i_ino)
802                         break;
803
804                 if (found_type < min_type)
805                         break;
806
807                 item_end = found_key.offset;
808                 if (found_type == BTRFS_EXTENT_DATA_KEY) {
809                         fi = btrfs_item_ptr(leaf, path->slots[0],
810                                             struct btrfs_file_extent_item);
811                         extent_type = btrfs_file_extent_type(leaf, fi);
812                         if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
813                                 item_end +=
814                                     btrfs_file_extent_num_bytes(leaf, fi);
815                         } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
816                                 struct btrfs_item *item = btrfs_item_nr(leaf,
817                                                                 path->slots[0]);
818                                 item_end += btrfs_file_extent_inline_len(leaf,
819                                                                          item);
820                         }
821                         item_end--;
822                 }
823                 if (found_type == BTRFS_CSUM_ITEM_KEY) {
824                         ret = btrfs_csum_truncate(trans, root, path,
825                                                   inode->i_size);
826                         BUG_ON(ret);
827                 }
828                 if (item_end < inode->i_size) {
829                         if (found_type == BTRFS_DIR_ITEM_KEY) {
830                                 found_type = BTRFS_INODE_ITEM_KEY;
831                         } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
832                                 found_type = BTRFS_CSUM_ITEM_KEY;
833                         } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
834                                 found_type = BTRFS_XATTR_ITEM_KEY;
835                         } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
836                                 found_type = BTRFS_INODE_REF_KEY;
837                         } else if (found_type) {
838                                 found_type--;
839                         } else {
840                                 break;
841                         }
842                         btrfs_set_key_type(&key, found_type);
843                         goto next;
844                 }
845                 if (found_key.offset >= inode->i_size)
846                         del_item = 1;
847                 else
848                         del_item = 0;
849                 found_extent = 0;
850
851                 /* FIXME, shrink the extent if the ref count is only 1 */
852                 if (found_type != BTRFS_EXTENT_DATA_KEY)
853                         goto delete;
854
855                 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
856                         u64 num_dec;
857                         extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
858                         if (!del_item) {
859                                 u64 orig_num_bytes =
860                                         btrfs_file_extent_num_bytes(leaf, fi);
861                                 extent_num_bytes = inode->i_size -
862                                         found_key.offset + root->sectorsize - 1;
863                                 extent_num_bytes = extent_num_bytes &
864                                         ~((u64)root->sectorsize - 1);
865                                 btrfs_set_file_extent_num_bytes(leaf, fi,
866                                                          extent_num_bytes);
867                                 num_dec = (orig_num_bytes -
868                                            extent_num_bytes);
869                                 if (extent_start != 0)
870                                         dec_i_blocks(inode, num_dec);
871                                 btrfs_mark_buffer_dirty(leaf);
872                         } else {
873                                 extent_num_bytes =
874                                         btrfs_file_extent_disk_num_bytes(leaf,
875                                                                          fi);
876                                 /* FIXME blocksize != 4096 */
877                                 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
878                                 if (extent_start != 0) {
879                                         found_extent = 1;
880                                         dec_i_blocks(inode, num_dec);
881                                 }
882                                 root_gen = btrfs_header_generation(leaf);
883                                 root_owner = btrfs_header_owner(leaf);
884                         }
885                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
886                         if (!del_item) {
887                                 u32 newsize = inode->i_size - found_key.offset;
888                                 dec_i_blocks(inode, item_end + 1 -
889                                             found_key.offset - newsize);
890                                 newsize =
891                                     btrfs_file_extent_calc_inline_size(newsize);
892                                 ret = btrfs_truncate_item(trans, root, path,
893                                                           newsize, 1);
894                                 BUG_ON(ret);
895                         } else {
896                                 dec_i_blocks(inode, item_end + 1 -
897                                              found_key.offset);
898                         }
899                 }
900 delete:
901                 if (del_item) {
902                         if (!pending_del_nr) {
903                                 /* no pending yet, add ourselves */
904                                 pending_del_slot = path->slots[0];
905                                 pending_del_nr = 1;
906                         } else if (pending_del_nr &&
907                                    path->slots[0] + 1 == pending_del_slot) {
908                                 /* hop on the pending chunk */
909                                 pending_del_nr++;
910                                 pending_del_slot = path->slots[0];
911                         } else {
912                                 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
913                         }
914                 } else {
915                         break;
916                 }
917                 if (found_extent) {
918                         ret = btrfs_free_extent(trans, root, extent_start,
919                                                 extent_num_bytes,
920                                                 root_owner,
921                                                 root_gen, inode->i_ino,
922                                                 found_key.offset, 0);
923                         BUG_ON(ret);
924                 }
925 next:
926                 if (path->slots[0] == 0) {
927                         if (pending_del_nr)
928                                 goto del_pending;
929                         btrfs_release_path(root, path);
930                         goto search_again;
931                 }
932
933                 path->slots[0]--;
934                 if (pending_del_nr &&
935                     path->slots[0] + 1 != pending_del_slot) {
936                         struct btrfs_key debug;
937 del_pending:
938                         btrfs_item_key_to_cpu(path->nodes[0], &debug,
939                                               pending_del_slot);
940                         ret = btrfs_del_items(trans, root, path,
941                                               pending_del_slot,
942                                               pending_del_nr);
943                         BUG_ON(ret);
944                         pending_del_nr = 0;
945                         btrfs_release_path(root, path);
946                         goto search_again;
947                 }
948         }
949         ret = 0;
950 error:
951         if (pending_del_nr) {
952                 ret = btrfs_del_items(trans, root, path, pending_del_slot,
953                                       pending_del_nr);
954         }
955         btrfs_release_path(root, path);
956         btrfs_free_path(path);
957         inode->i_sb->s_dirt = 1;
958         return ret;
959 }
960
961 static int btrfs_cow_one_page(struct inode *inode, struct page *page,
962                               size_t zero_start)
963 {
964         char *kaddr;
965         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
966         u64 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
967         u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
968         int ret = 0;
969
970         WARN_ON(!PageLocked(page));
971         set_page_extent_mapped(page);
972
973         lock_extent(io_tree, page_start, page_end, GFP_NOFS);
974         set_extent_delalloc(&BTRFS_I(inode)->io_tree, page_start,
975                             page_end, GFP_NOFS);
976
977         if (zero_start != PAGE_CACHE_SIZE) {
978                 kaddr = kmap(page);
979                 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
980                 flush_dcache_page(page);
981                 kunmap(page);
982         }
983         set_page_dirty(page);
984         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
985
986         return ret;
987 }
988
989 /*
990  * taken from block_truncate_page, but does cow as it zeros out
991  * any bytes left in the last page in the file.
992  */
993 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
994 {
995         struct inode *inode = mapping->host;
996         struct btrfs_root *root = BTRFS_I(inode)->root;
997         u32 blocksize = root->sectorsize;
998         pgoff_t index = from >> PAGE_CACHE_SHIFT;
999         unsigned offset = from & (PAGE_CACHE_SIZE-1);
1000         struct page *page;
1001         int ret = 0;
1002         u64 page_start;
1003
1004         if ((offset & (blocksize - 1)) == 0)
1005                 goto out;
1006
1007         ret = -ENOMEM;
1008         page = grab_cache_page(mapping, index);
1009         if (!page)
1010                 goto out;
1011         if (!PageUptodate(page)) {
1012                 ret = btrfs_readpage(NULL, page);
1013                 lock_page(page);
1014                 if (!PageUptodate(page)) {
1015                         ret = -EIO;
1016                         goto out;
1017                 }
1018         }
1019         page_start = (u64)page->index << PAGE_CACHE_SHIFT;
1020
1021         ret = btrfs_cow_one_page(inode, page, offset);
1022
1023         unlock_page(page);
1024         page_cache_release(page);
1025 out:
1026         return ret;
1027 }
1028
1029 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1030 {
1031         struct inode *inode = dentry->d_inode;
1032         int err;
1033
1034         err = inode_change_ok(inode, attr);
1035         if (err)
1036                 return err;
1037
1038         if (S_ISREG(inode->i_mode) &&
1039             attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1040                 struct btrfs_trans_handle *trans;
1041                 struct btrfs_root *root = BTRFS_I(inode)->root;
1042                 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1043
1044                 u64 mask = root->sectorsize - 1;
1045                 u64 hole_start = (inode->i_size + mask) & ~mask;
1046                 u64 block_end = (attr->ia_size + mask) & ~mask;
1047                 u64 hole_size;
1048                 u64 alloc_hint = 0;
1049
1050                 if (attr->ia_size <= hole_start)
1051                         goto out;
1052
1053                 mutex_lock(&root->fs_info->fs_mutex);
1054                 err = btrfs_check_free_space(root, 1, 0);
1055                 mutex_unlock(&root->fs_info->fs_mutex);
1056                 if (err)
1057                         goto fail;
1058
1059                 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1060
1061                 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1062                 hole_size = block_end - hole_start;
1063
1064                 mutex_lock(&root->fs_info->fs_mutex);
1065                 trans = btrfs_start_transaction(root, 1);
1066                 btrfs_set_trans_block_group(trans, inode);
1067                 err = btrfs_drop_extents(trans, root, inode,
1068                                          hole_start, block_end, hole_start,
1069                                          &alloc_hint);
1070
1071                 if (alloc_hint != EXTENT_MAP_INLINE) {
1072                         err = btrfs_insert_file_extent(trans, root,
1073                                                        inode->i_ino,
1074                                                        hole_start, 0, 0,
1075                                                        hole_size);
1076                         btrfs_drop_extent_cache(inode, hole_start,
1077                                                 hole_size - 1);
1078                         btrfs_check_file(root, inode);
1079                 }
1080                 btrfs_end_transaction(trans, root);
1081                 mutex_unlock(&root->fs_info->fs_mutex);
1082                 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1083                 if (err)
1084                         return err;
1085         }
1086 out:
1087         err = inode_setattr(inode, attr);
1088 fail:
1089         return err;
1090 }
1091
1092 void btrfs_put_inode(struct inode *inode)
1093 {
1094         int ret;
1095
1096         if (!BTRFS_I(inode)->ordered_trans) {
1097                 return;
1098         }
1099
1100         if (mapping_tagged(inode->i_mapping, PAGECACHE_TAG_DIRTY) ||
1101             mapping_tagged(inode->i_mapping, PAGECACHE_TAG_WRITEBACK))
1102                 return;
1103
1104         ret = btrfs_del_ordered_inode(inode);
1105         if (ret == 1) {
1106                 atomic_dec(&inode->i_count);
1107         }
1108 }
1109
1110 void btrfs_delete_inode(struct inode *inode)
1111 {
1112         struct btrfs_trans_handle *trans;
1113         struct btrfs_root *root = BTRFS_I(inode)->root;
1114         unsigned long nr;
1115         int ret;
1116
1117         truncate_inode_pages(&inode->i_data, 0);
1118         if (is_bad_inode(inode)) {
1119                 goto no_delete;
1120         }
1121
1122         inode->i_size = 0;
1123         mutex_lock(&root->fs_info->fs_mutex);
1124         trans = btrfs_start_transaction(root, 1);
1125
1126         btrfs_set_trans_block_group(trans, inode);
1127         ret = btrfs_truncate_in_trans(trans, root, inode, 0);
1128         if (ret)
1129                 goto no_delete_lock;
1130
1131         nr = trans->blocks_used;
1132         clear_inode(inode);
1133
1134         btrfs_end_transaction(trans, root);
1135         mutex_unlock(&root->fs_info->fs_mutex);
1136         btrfs_btree_balance_dirty(root, nr);
1137         btrfs_throttle(root);
1138         return;
1139
1140 no_delete_lock:
1141         nr = trans->blocks_used;
1142         btrfs_end_transaction(trans, root);
1143         mutex_unlock(&root->fs_info->fs_mutex);
1144         btrfs_btree_balance_dirty(root, nr);
1145         btrfs_throttle(root);
1146 no_delete:
1147         clear_inode(inode);
1148 }
1149
1150 /*
1151  * this returns the key found in the dir entry in the location pointer.
1152  * If no dir entries were found, location->objectid is 0.
1153  */
1154 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1155                                struct btrfs_key *location)
1156 {
1157         const char *name = dentry->d_name.name;
1158         int namelen = dentry->d_name.len;
1159         struct btrfs_dir_item *di;
1160         struct btrfs_path *path;
1161         struct btrfs_root *root = BTRFS_I(dir)->root;
1162         int ret = 0;
1163
1164         if (namelen == 1 && strcmp(name, ".") == 0) {
1165                 location->objectid = dir->i_ino;
1166                 location->type = BTRFS_INODE_ITEM_KEY;
1167                 location->offset = 0;
1168                 return 0;
1169         }
1170         path = btrfs_alloc_path();
1171         BUG_ON(!path);
1172
1173         if (namelen == 2 && strcmp(name, "..") == 0) {
1174                 struct btrfs_key key;
1175                 struct extent_buffer *leaf;
1176                 u32 nritems;
1177                 int slot;
1178
1179                 key.objectid = dir->i_ino;
1180                 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1181                 key.offset = 0;
1182                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1183                 BUG_ON(ret == 0);
1184                 ret = 0;
1185
1186                 leaf = path->nodes[0];
1187                 slot = path->slots[0];
1188                 nritems = btrfs_header_nritems(leaf);
1189                 if (slot >= nritems)
1190                         goto out_err;
1191
1192                 btrfs_item_key_to_cpu(leaf, &key, slot);
1193                 if (key.objectid != dir->i_ino ||
1194                     key.type != BTRFS_INODE_REF_KEY) {
1195                         goto out_err;
1196                 }
1197                 location->objectid = key.offset;
1198                 location->type = BTRFS_INODE_ITEM_KEY;
1199                 location->offset = 0;
1200                 goto out;
1201         }
1202
1203         di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1204                                     namelen, 0);
1205         if (IS_ERR(di))
1206                 ret = PTR_ERR(di);
1207         if (!di || IS_ERR(di)) {
1208                 goto out_err;
1209         }
1210         btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1211 out:
1212         btrfs_free_path(path);
1213         return ret;
1214 out_err:
1215         location->objectid = 0;
1216         goto out;
1217 }
1218
1219 /*
1220  * when we hit a tree root in a directory, the btrfs part of the inode
1221  * needs to be changed to reflect the root directory of the tree root.  This
1222  * is kind of like crossing a mount point.
1223  */
1224 static int fixup_tree_root_location(struct btrfs_root *root,
1225                              struct btrfs_key *location,
1226                              struct btrfs_root **sub_root,
1227                              struct dentry *dentry)
1228 {
1229         struct btrfs_path *path;
1230         struct btrfs_root_item *ri;
1231
1232         if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1233                 return 0;
1234         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1235                 return 0;
1236
1237         path = btrfs_alloc_path();
1238         BUG_ON(!path);
1239         mutex_lock(&root->fs_info->fs_mutex);
1240
1241         *sub_root = btrfs_read_fs_root(root->fs_info, location,
1242                                         dentry->d_name.name,
1243                                         dentry->d_name.len);
1244         if (IS_ERR(*sub_root))
1245                 return PTR_ERR(*sub_root);
1246
1247         ri = &(*sub_root)->root_item;
1248         location->objectid = btrfs_root_dirid(ri);
1249         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1250         location->offset = 0;
1251
1252         btrfs_free_path(path);
1253         mutex_unlock(&root->fs_info->fs_mutex);
1254         return 0;
1255 }
1256
1257 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1258 {
1259         struct btrfs_iget_args *args = p;
1260         inode->i_ino = args->ino;
1261         BTRFS_I(inode)->root = args->root;
1262         BTRFS_I(inode)->delalloc_bytes = 0;
1263         extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1264         extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1265                              inode->i_mapping, GFP_NOFS);
1266         return 0;
1267 }
1268
1269 static int btrfs_find_actor(struct inode *inode, void *opaque)
1270 {
1271         struct btrfs_iget_args *args = opaque;
1272         return (args->ino == inode->i_ino &&
1273                 args->root == BTRFS_I(inode)->root);
1274 }
1275
1276 struct inode *btrfs_ilookup(struct super_block *s, u64 objectid,
1277                             u64 root_objectid)
1278 {
1279         struct btrfs_iget_args args;
1280         args.ino = objectid;
1281         args.root = btrfs_lookup_fs_root(btrfs_sb(s)->fs_info, root_objectid);
1282
1283         if (!args.root)
1284                 return NULL;
1285
1286         return ilookup5(s, objectid, btrfs_find_actor, (void *)&args);
1287 }
1288
1289 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1290                                 struct btrfs_root *root)
1291 {
1292         struct inode *inode;
1293         struct btrfs_iget_args args;
1294         args.ino = objectid;
1295         args.root = root;
1296
1297         inode = iget5_locked(s, objectid, btrfs_find_actor,
1298                              btrfs_init_locked_inode,
1299                              (void *)&args);
1300         return inode;
1301 }
1302
1303 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1304                                    struct nameidata *nd)
1305 {
1306         struct inode * inode;
1307         struct btrfs_inode *bi = BTRFS_I(dir);
1308         struct btrfs_root *root = bi->root;
1309         struct btrfs_root *sub_root = root;
1310         struct btrfs_key location;
1311         int ret;
1312
1313         if (dentry->d_name.len > BTRFS_NAME_LEN)
1314                 return ERR_PTR(-ENAMETOOLONG);
1315
1316         mutex_lock(&root->fs_info->fs_mutex);
1317         ret = btrfs_inode_by_name(dir, dentry, &location);
1318         mutex_unlock(&root->fs_info->fs_mutex);
1319
1320         if (ret < 0)
1321                 return ERR_PTR(ret);
1322
1323         inode = NULL;
1324         if (location.objectid) {
1325                 ret = fixup_tree_root_location(root, &location, &sub_root,
1326                                                 dentry);
1327                 if (ret < 0)
1328                         return ERR_PTR(ret);
1329                 if (ret > 0)
1330                         return ERR_PTR(-ENOENT);
1331                 inode = btrfs_iget_locked(dir->i_sb, location.objectid,
1332                                           sub_root);
1333                 if (!inode)
1334                         return ERR_PTR(-EACCES);
1335                 if (inode->i_state & I_NEW) {
1336                         /* the inode and parent dir are two different roots */
1337                         if (sub_root != root) {
1338                                 igrab(inode);
1339                                 sub_root->inode = inode;
1340                         }
1341                         BTRFS_I(inode)->root = sub_root;
1342                         memcpy(&BTRFS_I(inode)->location, &location,
1343                                sizeof(location));
1344                         btrfs_read_locked_inode(inode);
1345                         unlock_new_inode(inode);
1346                 }
1347         }
1348         return d_splice_alias(inode, dentry);
1349 }
1350
1351 static unsigned char btrfs_filetype_table[] = {
1352         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1353 };
1354
1355 static int btrfs_readdir(struct file *filp, void *dirent, filldir_t filldir)
1356 {
1357         struct inode *inode = filp->f_dentry->d_inode;
1358         struct btrfs_root *root = BTRFS_I(inode)->root;
1359         struct btrfs_item *item;
1360         struct btrfs_dir_item *di;
1361         struct btrfs_key key;
1362         struct btrfs_key found_key;
1363         struct btrfs_path *path;
1364         int ret;
1365         u32 nritems;
1366         struct extent_buffer *leaf;
1367         int slot;
1368         int advance;
1369         unsigned char d_type;
1370         int over = 0;
1371         u32 di_cur;
1372         u32 di_total;
1373         u32 di_len;
1374         int key_type = BTRFS_DIR_INDEX_KEY;
1375         char tmp_name[32];
1376         char *name_ptr;
1377         int name_len;
1378
1379         /* FIXME, use a real flag for deciding about the key type */
1380         if (root->fs_info->tree_root == root)
1381                 key_type = BTRFS_DIR_ITEM_KEY;
1382
1383         /* special case for "." */
1384         if (filp->f_pos == 0) {
1385                 over = filldir(dirent, ".", 1,
1386                                1, inode->i_ino,
1387                                DT_DIR);
1388                 if (over)
1389                         return 0;
1390                 filp->f_pos = 1;
1391         }
1392
1393         mutex_lock(&root->fs_info->fs_mutex);
1394         key.objectid = inode->i_ino;
1395         path = btrfs_alloc_path();
1396         path->reada = 2;
1397
1398         /* special case for .., just use the back ref */
1399         if (filp->f_pos == 1) {
1400                 btrfs_set_key_type(&key, BTRFS_INODE_REF_KEY);
1401                 key.offset = 0;
1402                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1403                 BUG_ON(ret == 0);
1404                 leaf = path->nodes[0];
1405                 slot = path->slots[0];
1406                 nritems = btrfs_header_nritems(leaf);
1407                 if (slot >= nritems) {
1408                         btrfs_release_path(root, path);
1409                         goto read_dir_items;
1410                 }
1411                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1412                 btrfs_release_path(root, path);
1413                 if (found_key.objectid != key.objectid ||
1414                     found_key.type != BTRFS_INODE_REF_KEY)
1415                         goto read_dir_items;
1416                 over = filldir(dirent, "..", 2,
1417                                2, found_key.offset, DT_DIR);
1418                 if (over)
1419                         goto nopos;
1420                 filp->f_pos = 2;
1421         }
1422
1423 read_dir_items:
1424         btrfs_set_key_type(&key, key_type);
1425         key.offset = filp->f_pos;
1426
1427         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1428         if (ret < 0)
1429                 goto err;
1430         advance = 0;
1431         while(1) {
1432                 leaf = path->nodes[0];
1433                 nritems = btrfs_header_nritems(leaf);
1434                 slot = path->slots[0];
1435                 if (advance || slot >= nritems) {
1436                         if (slot >= nritems -1) {
1437                                 ret = btrfs_next_leaf(root, path);
1438                                 if (ret)
1439                                         break;
1440                                 leaf = path->nodes[0];
1441                                 nritems = btrfs_header_nritems(leaf);
1442                                 slot = path->slots[0];
1443                         } else {
1444                                 slot++;
1445                                 path->slots[0]++;
1446                         }
1447                 }
1448                 advance = 1;
1449                 item = btrfs_item_nr(leaf, slot);
1450                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
1451
1452                 if (found_key.objectid != key.objectid)
1453                         break;
1454                 if (btrfs_key_type(&found_key) != key_type)
1455                         break;
1456                 if (found_key.offset < filp->f_pos)
1457                         continue;
1458
1459                 filp->f_pos = found_key.offset;
1460                 advance = 1;
1461                 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
1462                 di_cur = 0;
1463                 di_total = btrfs_item_size(leaf, item);
1464                 while(di_cur < di_total) {
1465                         struct btrfs_key location;
1466
1467                         name_len = btrfs_dir_name_len(leaf, di);
1468                         if (name_len < 32) {
1469                                 name_ptr = tmp_name;
1470                         } else {
1471                                 name_ptr = kmalloc(name_len, GFP_NOFS);
1472                                 BUG_ON(!name_ptr);
1473                         }
1474                         read_extent_buffer(leaf, name_ptr,
1475                                            (unsigned long)(di + 1), name_len);
1476
1477                         d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
1478                         btrfs_dir_item_key_to_cpu(leaf, di, &location);
1479                         over = filldir(dirent, name_ptr, name_len,
1480                                        found_key.offset,
1481                                        location.objectid,
1482                                        d_type);
1483
1484                         if (name_ptr != tmp_name)
1485                                 kfree(name_ptr);
1486
1487                         if (over)
1488                                 goto nopos;
1489                         di_len = btrfs_dir_name_len(leaf, di) +
1490                                 btrfs_dir_data_len(leaf, di) +sizeof(*di);
1491                         di_cur += di_len;
1492                         di = (struct btrfs_dir_item *)((char *)di + di_len);
1493                 }
1494         }
1495         if (key_type == BTRFS_DIR_INDEX_KEY)
1496                 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
1497         else
1498                 filp->f_pos++;
1499 nopos:
1500         ret = 0;
1501 err:
1502         btrfs_release_path(root, path);
1503         btrfs_free_path(path);
1504         mutex_unlock(&root->fs_info->fs_mutex);
1505         return ret;
1506 }
1507
1508 int btrfs_write_inode(struct inode *inode, int wait)
1509 {
1510         struct btrfs_root *root = BTRFS_I(inode)->root;
1511         struct btrfs_trans_handle *trans;
1512         int ret = 0;
1513
1514         if (wait) {
1515                 mutex_lock(&root->fs_info->fs_mutex);
1516                 trans = btrfs_start_transaction(root, 1);
1517                 btrfs_set_trans_block_group(trans, inode);
1518                 ret = btrfs_commit_transaction(trans, root);
1519                 mutex_unlock(&root->fs_info->fs_mutex);
1520         }
1521         return ret;
1522 }
1523
1524 /*
1525  * This is somewhat expensive, updating the tree every time the
1526  * inode changes.  But, it is most likely to find the inode in cache.
1527  * FIXME, needs more benchmarking...there are no reasons other than performance
1528  * to keep or drop this code.
1529  */
1530 void btrfs_dirty_inode(struct inode *inode)
1531 {
1532         struct btrfs_root *root = BTRFS_I(inode)->root;
1533         struct btrfs_trans_handle *trans;
1534
1535         mutex_lock(&root->fs_info->fs_mutex);
1536         trans = btrfs_start_transaction(root, 1);
1537         btrfs_set_trans_block_group(trans, inode);
1538         btrfs_update_inode(trans, root, inode);
1539         btrfs_end_transaction(trans, root);
1540         mutex_unlock(&root->fs_info->fs_mutex);
1541 }
1542
1543 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
1544                                      struct btrfs_root *root,
1545                                      const char *name, int name_len,
1546                                      u64 ref_objectid,
1547                                      u64 objectid,
1548                                      struct btrfs_block_group_cache *group,
1549                                      int mode)
1550 {
1551         struct inode *inode;
1552         struct btrfs_inode_item *inode_item;
1553         struct btrfs_key *location;
1554         struct btrfs_path *path;
1555         struct btrfs_inode_ref *ref;
1556         struct btrfs_key key[2];
1557         u32 sizes[2];
1558         unsigned long ptr;
1559         int ret;
1560         int owner;
1561
1562         path = btrfs_alloc_path();
1563         BUG_ON(!path);
1564
1565         inode = new_inode(root->fs_info->sb);
1566         if (!inode)
1567                 return ERR_PTR(-ENOMEM);
1568
1569         extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1570         extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1571                              inode->i_mapping, GFP_NOFS);
1572         BTRFS_I(inode)->delalloc_bytes = 0;
1573         BTRFS_I(inode)->root = root;
1574
1575         if (mode & S_IFDIR)
1576                 owner = 0;
1577         else
1578                 owner = 1;
1579         group = btrfs_find_block_group(root, group, 0, 0, owner);
1580         BTRFS_I(inode)->block_group = group;
1581         BTRFS_I(inode)->flags = 0;
1582
1583         key[0].objectid = objectid;
1584         btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
1585         key[0].offset = 0;
1586
1587         key[1].objectid = objectid;
1588         btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
1589         key[1].offset = ref_objectid;
1590
1591         sizes[0] = sizeof(struct btrfs_inode_item);
1592         sizes[1] = name_len + sizeof(*ref);
1593
1594         ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
1595         if (ret != 0)
1596                 goto fail;
1597
1598         if (objectid > root->highest_inode)
1599                 root->highest_inode = objectid;
1600
1601         inode->i_uid = current->fsuid;
1602         inode->i_gid = current->fsgid;
1603         inode->i_mode = mode;
1604         inode->i_ino = objectid;
1605         inode->i_blocks = 0;
1606         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
1607         inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
1608                                   struct btrfs_inode_item);
1609         fill_inode_item(path->nodes[0], inode_item, inode);
1610
1611         ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
1612                              struct btrfs_inode_ref);
1613         btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
1614         ptr = (unsigned long)(ref + 1);
1615         write_extent_buffer(path->nodes[0], name, ptr, name_len);
1616
1617         btrfs_mark_buffer_dirty(path->nodes[0]);
1618         btrfs_free_path(path);
1619
1620         location = &BTRFS_I(inode)->location;
1621         location->objectid = objectid;
1622         location->offset = 0;
1623         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1624
1625         insert_inode_hash(inode);
1626         return inode;
1627 fail:
1628         btrfs_free_path(path);
1629         return ERR_PTR(ret);
1630 }
1631
1632 static inline u8 btrfs_inode_type(struct inode *inode)
1633 {
1634         return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
1635 }
1636
1637 static int btrfs_add_link(struct btrfs_trans_handle *trans,
1638                             struct dentry *dentry, struct inode *inode,
1639                             int add_backref)
1640 {
1641         int ret;
1642         struct btrfs_key key;
1643         struct btrfs_root *root = BTRFS_I(dentry->d_parent->d_inode)->root;
1644         struct inode *parent_inode;
1645
1646         key.objectid = inode->i_ino;
1647         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
1648         key.offset = 0;
1649
1650         ret = btrfs_insert_dir_item(trans, root,
1651                                     dentry->d_name.name, dentry->d_name.len,
1652                                     dentry->d_parent->d_inode->i_ino,
1653                                     &key, btrfs_inode_type(inode));
1654         if (ret == 0) {
1655                 if (add_backref) {
1656                         ret = btrfs_insert_inode_ref(trans, root,
1657                                              dentry->d_name.name,
1658                                              dentry->d_name.len,
1659                                              inode->i_ino,
1660                                              dentry->d_parent->d_inode->i_ino);
1661                 }
1662                 parent_inode = dentry->d_parent->d_inode;
1663                 parent_inode->i_size += dentry->d_name.len * 2;
1664                 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
1665                 ret = btrfs_update_inode(trans, root,
1666                                          dentry->d_parent->d_inode);
1667         }
1668         return ret;
1669 }
1670
1671 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
1672                             struct dentry *dentry, struct inode *inode,
1673                             int backref)
1674 {
1675         int err = btrfs_add_link(trans, dentry, inode, backref);
1676         if (!err) {
1677                 d_instantiate(dentry, inode);
1678                 return 0;
1679         }
1680         if (err > 0)
1681                 err = -EEXIST;
1682         return err;
1683 }
1684
1685 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
1686                         int mode, dev_t rdev)
1687 {
1688         struct btrfs_trans_handle *trans;
1689         struct btrfs_root *root = BTRFS_I(dir)->root;
1690         struct inode *inode = NULL;
1691         int err;
1692         int drop_inode = 0;
1693         u64 objectid;
1694         unsigned long nr = 0;
1695
1696         if (!new_valid_dev(rdev))
1697                 return -EINVAL;
1698
1699         mutex_lock(&root->fs_info->fs_mutex);
1700         err = btrfs_check_free_space(root, 1, 0);
1701         if (err)
1702                 goto fail;
1703
1704         trans = btrfs_start_transaction(root, 1);
1705         btrfs_set_trans_block_group(trans, dir);
1706
1707         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1708         if (err) {
1709                 err = -ENOSPC;
1710                 goto out_unlock;
1711         }
1712
1713         inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1714                                 dentry->d_name.len,
1715                                 dentry->d_parent->d_inode->i_ino, objectid,
1716                                 BTRFS_I(dir)->block_group, mode);
1717         err = PTR_ERR(inode);
1718         if (IS_ERR(inode))
1719                 goto out_unlock;
1720
1721         btrfs_set_trans_block_group(trans, inode);
1722         err = btrfs_add_nondir(trans, dentry, inode, 0);
1723         if (err)
1724                 drop_inode = 1;
1725         else {
1726                 inode->i_op = &btrfs_special_inode_operations;
1727                 init_special_inode(inode, inode->i_mode, rdev);
1728                 btrfs_update_inode(trans, root, inode);
1729         }
1730         dir->i_sb->s_dirt = 1;
1731         btrfs_update_inode_block_group(trans, inode);
1732         btrfs_update_inode_block_group(trans, dir);
1733 out_unlock:
1734         nr = trans->blocks_used;
1735         btrfs_end_transaction(trans, root);
1736 fail:
1737         mutex_unlock(&root->fs_info->fs_mutex);
1738
1739         if (drop_inode) {
1740                 inode_dec_link_count(inode);
1741                 iput(inode);
1742         }
1743         btrfs_btree_balance_dirty(root, nr);
1744         btrfs_throttle(root);
1745         return err;
1746 }
1747
1748 static int btrfs_create(struct inode *dir, struct dentry *dentry,
1749                         int mode, struct nameidata *nd)
1750 {
1751         struct btrfs_trans_handle *trans;
1752         struct btrfs_root *root = BTRFS_I(dir)->root;
1753         struct inode *inode = NULL;
1754         int err;
1755         int drop_inode = 0;
1756         unsigned long nr = 0;
1757         u64 objectid;
1758
1759         mutex_lock(&root->fs_info->fs_mutex);
1760         err = btrfs_check_free_space(root, 1, 0);
1761         if (err)
1762                 goto fail;
1763         trans = btrfs_start_transaction(root, 1);
1764         btrfs_set_trans_block_group(trans, dir);
1765
1766         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1767         if (err) {
1768                 err = -ENOSPC;
1769                 goto out_unlock;
1770         }
1771
1772         inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1773                                 dentry->d_name.len,
1774                                 dentry->d_parent->d_inode->i_ino,
1775                                 objectid, BTRFS_I(dir)->block_group, mode);
1776         err = PTR_ERR(inode);
1777         if (IS_ERR(inode))
1778                 goto out_unlock;
1779
1780         btrfs_set_trans_block_group(trans, inode);
1781         err = btrfs_add_nondir(trans, dentry, inode, 0);
1782         if (err)
1783                 drop_inode = 1;
1784         else {
1785                 inode->i_mapping->a_ops = &btrfs_aops;
1786                 inode->i_fop = &btrfs_file_operations;
1787                 inode->i_op = &btrfs_file_inode_operations;
1788                 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1789                 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1790                                      inode->i_mapping, GFP_NOFS);
1791                 BTRFS_I(inode)->delalloc_bytes = 0;
1792                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1793         }
1794         dir->i_sb->s_dirt = 1;
1795         btrfs_update_inode_block_group(trans, inode);
1796         btrfs_update_inode_block_group(trans, dir);
1797 out_unlock:
1798         nr = trans->blocks_used;
1799         btrfs_end_transaction(trans, root);
1800 fail:
1801         mutex_unlock(&root->fs_info->fs_mutex);
1802
1803         if (drop_inode) {
1804                 inode_dec_link_count(inode);
1805                 iput(inode);
1806         }
1807         btrfs_btree_balance_dirty(root, nr);
1808         btrfs_throttle(root);
1809         return err;
1810 }
1811
1812 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
1813                       struct dentry *dentry)
1814 {
1815         struct btrfs_trans_handle *trans;
1816         struct btrfs_root *root = BTRFS_I(dir)->root;
1817         struct inode *inode = old_dentry->d_inode;
1818         unsigned long nr = 0;
1819         int err;
1820         int drop_inode = 0;
1821
1822         if (inode->i_nlink == 0)
1823                 return -ENOENT;
1824
1825 #if LINUX_VERSION_CODE <= KERNEL_VERSION(2,6,18)
1826         inode->i_nlink++;
1827 #else
1828         inc_nlink(inode);
1829 #endif
1830         mutex_lock(&root->fs_info->fs_mutex);
1831         err = btrfs_check_free_space(root, 1, 0);
1832         if (err)
1833                 goto fail;
1834         trans = btrfs_start_transaction(root, 1);
1835
1836         btrfs_set_trans_block_group(trans, dir);
1837         atomic_inc(&inode->i_count);
1838         err = btrfs_add_nondir(trans, dentry, inode, 1);
1839
1840         if (err)
1841                 drop_inode = 1;
1842
1843         dir->i_sb->s_dirt = 1;
1844         btrfs_update_inode_block_group(trans, dir);
1845         err = btrfs_update_inode(trans, root, inode);
1846
1847         if (err)
1848                 drop_inode = 1;
1849
1850         nr = trans->blocks_used;
1851         btrfs_end_transaction(trans, root);
1852 fail:
1853         mutex_unlock(&root->fs_info->fs_mutex);
1854
1855         if (drop_inode) {
1856                 inode_dec_link_count(inode);
1857                 iput(inode);
1858         }
1859         btrfs_btree_balance_dirty(root, nr);
1860         btrfs_throttle(root);
1861         return err;
1862 }
1863
1864 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
1865 {
1866         struct inode *inode;
1867         struct btrfs_trans_handle *trans;
1868         struct btrfs_root *root = BTRFS_I(dir)->root;
1869         int err = 0;
1870         int drop_on_err = 0;
1871         u64 objectid;
1872         unsigned long nr = 1;
1873
1874         mutex_lock(&root->fs_info->fs_mutex);
1875         err = btrfs_check_free_space(root, 1, 0);
1876         if (err)
1877                 goto out_unlock;
1878
1879         trans = btrfs_start_transaction(root, 1);
1880         btrfs_set_trans_block_group(trans, dir);
1881
1882         if (IS_ERR(trans)) {
1883                 err = PTR_ERR(trans);
1884                 goto out_unlock;
1885         }
1886
1887         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
1888         if (err) {
1889                 err = -ENOSPC;
1890                 goto out_unlock;
1891         }
1892
1893         inode = btrfs_new_inode(trans, root, dentry->d_name.name,
1894                                 dentry->d_name.len,
1895                                 dentry->d_parent->d_inode->i_ino, objectid,
1896                                 BTRFS_I(dir)->block_group, S_IFDIR | mode);
1897         if (IS_ERR(inode)) {
1898                 err = PTR_ERR(inode);
1899                 goto out_fail;
1900         }
1901
1902         drop_on_err = 1;
1903         inode->i_op = &btrfs_dir_inode_operations;
1904         inode->i_fop = &btrfs_dir_file_operations;
1905         btrfs_set_trans_block_group(trans, inode);
1906
1907         inode->i_size = 0;
1908         err = btrfs_update_inode(trans, root, inode);
1909         if (err)
1910                 goto out_fail;
1911
1912         err = btrfs_add_link(trans, dentry, inode, 0);
1913         if (err)
1914                 goto out_fail;
1915
1916         d_instantiate(dentry, inode);
1917         drop_on_err = 0;
1918         dir->i_sb->s_dirt = 1;
1919         btrfs_update_inode_block_group(trans, inode);
1920         btrfs_update_inode_block_group(trans, dir);
1921
1922 out_fail:
1923         nr = trans->blocks_used;
1924         btrfs_end_transaction(trans, root);
1925
1926 out_unlock:
1927         mutex_unlock(&root->fs_info->fs_mutex);
1928         if (drop_on_err)
1929                 iput(inode);
1930         btrfs_btree_balance_dirty(root, nr);
1931         btrfs_throttle(root);
1932         return err;
1933 }
1934
1935 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
1936                                     size_t pg_offset, u64 start, u64 len,
1937                                     int create)
1938 {
1939         int ret;
1940         int err = 0;
1941         u64 bytenr;
1942         u64 extent_start = 0;
1943         u64 extent_end = 0;
1944         u64 objectid = inode->i_ino;
1945         u32 found_type;
1946         struct btrfs_path *path;
1947         struct btrfs_root *root = BTRFS_I(inode)->root;
1948         struct btrfs_file_extent_item *item;
1949         struct extent_buffer *leaf;
1950         struct btrfs_key found_key;
1951         struct extent_map *em = NULL;
1952         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1953         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1954         struct btrfs_trans_handle *trans = NULL;
1955
1956         path = btrfs_alloc_path();
1957         BUG_ON(!path);
1958         mutex_lock(&root->fs_info->fs_mutex);
1959
1960 again:
1961         spin_lock(&em_tree->lock);
1962         em = lookup_extent_mapping(em_tree, start, len);
1963         spin_unlock(&em_tree->lock);
1964
1965         if (em) {
1966                 if (em->start > start) {
1967                         printk("get_extent lookup [%Lu %Lu] em [%Lu %Lu]\n",
1968                                start, len, em->start, em->len);
1969                         WARN_ON(1);
1970                 }
1971                 if (em->block_start == EXTENT_MAP_INLINE && page)
1972                         free_extent_map(em);
1973                 else
1974                         goto out;
1975         }
1976         em = alloc_extent_map(GFP_NOFS);
1977         if (!em) {
1978                 err = -ENOMEM;
1979                 goto out;
1980         }
1981
1982         em->start = EXTENT_MAP_HOLE;
1983         em->len = (u64)-1;
1984         em->bdev = inode->i_sb->s_bdev;
1985         ret = btrfs_lookup_file_extent(trans, root, path,
1986                                        objectid, start, trans != NULL);
1987         if (ret < 0) {
1988                 err = ret;
1989                 goto out;
1990         }
1991
1992         if (ret != 0) {
1993                 if (path->slots[0] == 0)
1994                         goto not_found;
1995                 path->slots[0]--;
1996         }
1997
1998         leaf = path->nodes[0];
1999         item = btrfs_item_ptr(leaf, path->slots[0],
2000                               struct btrfs_file_extent_item);
2001         /* are we inside the extent that was found? */
2002         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2003         found_type = btrfs_key_type(&found_key);
2004         if (found_key.objectid != objectid ||
2005             found_type != BTRFS_EXTENT_DATA_KEY) {
2006                 goto not_found;
2007         }
2008
2009         found_type = btrfs_file_extent_type(leaf, item);
2010         extent_start = found_key.offset;
2011         if (found_type == BTRFS_FILE_EXTENT_REG) {
2012                 extent_end = extent_start +
2013                        btrfs_file_extent_num_bytes(leaf, item);
2014                 err = 0;
2015                 if (start < extent_start || start >= extent_end) {
2016                         em->start = start;
2017                         if (start < extent_start) {
2018                                 if (start + len <= extent_start)
2019                                         goto not_found;
2020                                 em->len = extent_end - extent_start;
2021                         } else {
2022                                 em->len = len;
2023                         }
2024                         goto not_found_em;
2025                 }
2026                 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2027                 if (bytenr == 0) {
2028                         em->start = extent_start;
2029                         em->len = extent_end - extent_start;
2030                         em->block_start = EXTENT_MAP_HOLE;
2031                         goto insert;
2032                 }
2033                 bytenr += btrfs_file_extent_offset(leaf, item);
2034                 em->block_start = bytenr;
2035                 em->start = extent_start;
2036                 em->len = extent_end - extent_start;
2037                 goto insert;
2038         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2039                 u64 page_start;
2040                 unsigned long ptr;
2041                 char *map;
2042                 size_t size;
2043                 size_t extent_offset;
2044                 size_t copy_size;
2045
2046                 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2047                                                     path->slots[0]));
2048                 extent_end = (extent_start + size + root->sectorsize - 1) &
2049                         ~((u64)root->sectorsize - 1);
2050                 if (start < extent_start || start >= extent_end) {
2051                         em->start = start;
2052                         if (start < extent_start) {
2053                                 if (start + len <= extent_start)
2054                                         goto not_found;
2055                                 em->len = extent_end - extent_start;
2056                         } else {
2057                                 em->len = len;
2058                         }
2059                         goto not_found_em;
2060                 }
2061                 em->block_start = EXTENT_MAP_INLINE;
2062
2063                 if (!page) {
2064                         em->start = extent_start;
2065                         em->len = size;
2066                         goto out;
2067                 }
2068
2069                 page_start = page_offset(page) + pg_offset;
2070                 extent_offset = page_start - extent_start;
2071                 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2072                                 size - extent_offset);
2073                 em->start = extent_start + extent_offset;
2074                 em->len = (copy_size + root->sectorsize - 1) &
2075                         ~((u64)root->sectorsize - 1);
2076                 map = kmap(page);
2077                 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2078                 if (create == 0 && !PageUptodate(page)) {
2079                         read_extent_buffer(leaf, map + pg_offset, ptr,
2080                                            copy_size);
2081                         flush_dcache_page(page);
2082                 } else if (create && PageUptodate(page)) {
2083                         if (!trans) {
2084                                 kunmap(page);
2085                                 free_extent_map(em);
2086                                 em = NULL;
2087                                 btrfs_release_path(root, path);
2088                                 trans = btrfs_start_transaction(root, 1);
2089                                 goto again;
2090                         }
2091                         write_extent_buffer(leaf, map + pg_offset, ptr,
2092                                             copy_size);
2093                         btrfs_mark_buffer_dirty(leaf);
2094                 }
2095                 kunmap(page);
2096                 set_extent_uptodate(io_tree, em->start,
2097                                     extent_map_end(em) - 1, GFP_NOFS);
2098                 goto insert;
2099         } else {
2100                 printk("unkknown found_type %d\n", found_type);
2101                 WARN_ON(1);
2102         }
2103 not_found:
2104         em->start = start;
2105         em->len = len;
2106 not_found_em:
2107         em->block_start = EXTENT_MAP_HOLE;
2108 insert:
2109         btrfs_release_path(root, path);
2110         if (em->start > start || extent_map_end(em) <= start) {
2111                 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2112                 err = -EIO;
2113                 goto out;
2114         }
2115
2116         err = 0;
2117         spin_lock(&em_tree->lock);
2118         ret = add_extent_mapping(em_tree, em);
2119         if (ret == -EEXIST) {
2120                 free_extent_map(em);
2121                 em = lookup_extent_mapping(em_tree, start, len);
2122                 if (!em) {
2123                         err = -EIO;
2124                         printk("failing to insert %Lu %Lu\n", start, len);
2125                 }
2126         }
2127         spin_unlock(&em_tree->lock);
2128 out:
2129         btrfs_free_path(path);
2130         if (trans) {
2131                 ret = btrfs_end_transaction(trans, root);
2132                 if (!err)
2133                         err = ret;
2134         }
2135         mutex_unlock(&root->fs_info->fs_mutex);
2136         if (err) {
2137                 free_extent_map(em);
2138                 WARN_ON(1);
2139                 return ERR_PTR(err);
2140         }
2141         return em;
2142 }
2143
2144 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
2145 {
2146         return extent_bmap(mapping, iblock, btrfs_get_extent);
2147 }
2148
2149 int btrfs_readpage(struct file *file, struct page *page)
2150 {
2151         struct extent_io_tree *tree;
2152         tree = &BTRFS_I(page->mapping->host)->io_tree;
2153         return extent_read_full_page(tree, page, btrfs_get_extent);
2154 }
2155
2156 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
2157 {
2158         struct extent_io_tree *tree;
2159
2160
2161         if (current->flags & PF_MEMALLOC) {
2162                 redirty_page_for_writepage(wbc, page);
2163                 unlock_page(page);
2164                 return 0;
2165         }
2166         tree = &BTRFS_I(page->mapping->host)->io_tree;
2167         return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
2168 }
2169
2170 static int btrfs_writepages(struct address_space *mapping,
2171                             struct writeback_control *wbc)
2172 {
2173         struct extent_io_tree *tree;
2174         tree = &BTRFS_I(mapping->host)->io_tree;
2175         return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
2176 }
2177
2178 static int
2179 btrfs_readpages(struct file *file, struct address_space *mapping,
2180                 struct list_head *pages, unsigned nr_pages)
2181 {
2182         struct extent_io_tree *tree;
2183         tree = &BTRFS_I(mapping->host)->io_tree;
2184         return extent_readpages(tree, mapping, pages, nr_pages,
2185                                 btrfs_get_extent);
2186 }
2187
2188 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
2189 {
2190         struct extent_io_tree *tree;
2191         struct extent_map_tree *map;
2192         int ret;
2193
2194         tree = &BTRFS_I(page->mapping->host)->io_tree;
2195         map = &BTRFS_I(page->mapping->host)->extent_tree;
2196         ret = try_release_extent_mapping(map, tree, page, gfp_flags);
2197         if (ret == 1) {
2198                 ClearPagePrivate(page);
2199                 set_page_private(page, 0);
2200                 page_cache_release(page);
2201         }
2202         return ret;
2203 }
2204
2205 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
2206 {
2207         struct extent_io_tree *tree;
2208
2209         tree = &BTRFS_I(page->mapping->host)->io_tree;
2210         extent_invalidatepage(tree, page, offset);
2211         btrfs_releasepage(page, GFP_NOFS);
2212 }
2213
2214 /*
2215  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
2216  * called from a page fault handler when a page is first dirtied. Hence we must
2217  * be careful to check for EOF conditions here. We set the page up correctly
2218  * for a written page which means we get ENOSPC checking when writing into
2219  * holes and correct delalloc and unwritten extent mapping on filesystems that
2220  * support these features.
2221  *
2222  * We are not allowed to take the i_mutex here so we have to play games to
2223  * protect against truncate races as the page could now be beyond EOF.  Because
2224  * vmtruncate() writes the inode size before removing pages, once we have the
2225  * page lock we can determine safely if the page is beyond EOF. If it is not
2226  * beyond EOF, then the page is guaranteed safe against truncation until we
2227  * unlock the page.
2228  */
2229 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
2230 {
2231         struct inode *inode = fdentry(vma->vm_file)->d_inode;
2232         struct btrfs_root *root = BTRFS_I(inode)->root;
2233         unsigned long end;
2234         loff_t size;
2235         int ret;
2236         u64 page_start;
2237
2238         mutex_lock(&root->fs_info->fs_mutex);
2239         ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
2240         mutex_unlock(&root->fs_info->fs_mutex);
2241         if (ret)
2242                 goto out;
2243
2244         ret = -EINVAL;
2245
2246         lock_page(page);
2247         wait_on_page_writeback(page);
2248         size = i_size_read(inode);
2249         page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2250
2251         if ((page->mapping != inode->i_mapping) ||
2252             (page_start > size)) {
2253                 /* page got truncated out from underneath us */
2254                 goto out_unlock;
2255         }
2256
2257         /* page is wholly or partially inside EOF */
2258         if (page_start + PAGE_CACHE_SIZE > size)
2259                 end = size & ~PAGE_CACHE_MASK;
2260         else
2261                 end = PAGE_CACHE_SIZE;
2262
2263         ret = btrfs_cow_one_page(inode, page, end);
2264
2265 out_unlock:
2266         unlock_page(page);
2267 out:
2268         return ret;
2269 }
2270
2271 static void btrfs_truncate(struct inode *inode)
2272 {
2273         struct btrfs_root *root = BTRFS_I(inode)->root;
2274         int ret;
2275         struct btrfs_trans_handle *trans;
2276         unsigned long nr;
2277
2278         if (!S_ISREG(inode->i_mode))
2279                 return;
2280         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
2281                 return;
2282
2283         btrfs_truncate_page(inode->i_mapping, inode->i_size);
2284
2285         mutex_lock(&root->fs_info->fs_mutex);
2286         trans = btrfs_start_transaction(root, 1);
2287         btrfs_set_trans_block_group(trans, inode);
2288
2289         /* FIXME, add redo link to tree so we don't leak on crash */
2290         ret = btrfs_truncate_in_trans(trans, root, inode,
2291                                       BTRFS_EXTENT_DATA_KEY);
2292         btrfs_update_inode(trans, root, inode);
2293         nr = trans->blocks_used;
2294
2295         ret = btrfs_end_transaction(trans, root);
2296         BUG_ON(ret);
2297         mutex_unlock(&root->fs_info->fs_mutex);
2298         btrfs_btree_balance_dirty(root, nr);
2299         btrfs_throttle(root);
2300 }
2301
2302 static int noinline create_subvol(struct btrfs_root *root, char *name,
2303                                   int namelen)
2304 {
2305         struct btrfs_trans_handle *trans;
2306         struct btrfs_key key;
2307         struct btrfs_root_item root_item;
2308         struct btrfs_inode_item *inode_item;
2309         struct extent_buffer *leaf;
2310         struct btrfs_root *new_root = root;
2311         struct inode *inode;
2312         struct inode *dir;
2313         int ret;
2314         int err;
2315         u64 objectid;
2316         u64 new_dirid = BTRFS_FIRST_FREE_OBJECTID;
2317         unsigned long nr = 1;
2318
2319         mutex_lock(&root->fs_info->fs_mutex);
2320         ret = btrfs_check_free_space(root, 1, 0);
2321         if (ret)
2322                 goto fail_commit;
2323
2324         trans = btrfs_start_transaction(root, 1);
2325         BUG_ON(!trans);
2326
2327         ret = btrfs_find_free_objectid(trans, root->fs_info->tree_root,
2328                                        0, &objectid);
2329         if (ret)
2330                 goto fail;
2331
2332         leaf = __btrfs_alloc_free_block(trans, root, root->leafsize,
2333                                         objectid, trans->transid, 0, 0,
2334                                         0, 0);
2335         if (IS_ERR(leaf))
2336                 return PTR_ERR(leaf);
2337
2338         btrfs_set_header_nritems(leaf, 0);
2339         btrfs_set_header_level(leaf, 0);
2340         btrfs_set_header_bytenr(leaf, leaf->start);
2341         btrfs_set_header_generation(leaf, trans->transid);
2342         btrfs_set_header_owner(leaf, objectid);
2343
2344         write_extent_buffer(leaf, root->fs_info->fsid,
2345                             (unsigned long)btrfs_header_fsid(leaf),
2346                             BTRFS_FSID_SIZE);
2347         btrfs_mark_buffer_dirty(leaf);
2348
2349         inode_item = &root_item.inode;
2350         memset(inode_item, 0, sizeof(*inode_item));
2351         inode_item->generation = cpu_to_le64(1);
2352         inode_item->size = cpu_to_le64(3);
2353         inode_item->nlink = cpu_to_le32(1);
2354         inode_item->nblocks = cpu_to_le64(1);
2355         inode_item->mode = cpu_to_le32(S_IFDIR | 0755);
2356
2357         btrfs_set_root_bytenr(&root_item, leaf->start);
2358         btrfs_set_root_level(&root_item, 0);
2359         btrfs_set_root_refs(&root_item, 1);
2360         btrfs_set_root_used(&root_item, 0);
2361
2362         memset(&root_item.drop_progress, 0, sizeof(root_item.drop_progress));
2363         root_item.drop_level = 0;
2364
2365         free_extent_buffer(leaf);
2366         leaf = NULL;
2367
2368         btrfs_set_root_dirid(&root_item, new_dirid);
2369
2370         key.objectid = objectid;
2371         key.offset = 1;
2372         btrfs_set_key_type(&key, BTRFS_ROOT_ITEM_KEY);
2373         ret = btrfs_insert_root(trans, root->fs_info->tree_root, &key,
2374                                 &root_item);
2375         if (ret)
2376                 goto fail;
2377
2378         /*
2379          * insert the directory item
2380          */
2381         key.offset = (u64)-1;
2382         dir = root->fs_info->sb->s_root->d_inode;
2383         ret = btrfs_insert_dir_item(trans, root->fs_info->tree_root,
2384                                     name, namelen, dir->i_ino, &key,
2385                                     BTRFS_FT_DIR);
2386         if (ret)
2387                 goto fail;
2388
2389         ret = btrfs_insert_inode_ref(trans, root->fs_info->tree_root,
2390                              name, namelen, objectid,
2391                              root->fs_info->sb->s_root->d_inode->i_ino);
2392         if (ret)
2393                 goto fail;
2394
2395         ret = btrfs_commit_transaction(trans, root);
2396         if (ret)
2397                 goto fail_commit;
2398
2399         new_root = btrfs_read_fs_root(root->fs_info, &key, name, namelen);
2400         BUG_ON(!new_root);
2401
2402         trans = btrfs_start_transaction(new_root, 1);
2403         BUG_ON(!trans);
2404
2405         inode = btrfs_new_inode(trans, new_root, "..", 2, new_dirid,
2406                                 new_dirid,
2407                                 BTRFS_I(dir)->block_group, S_IFDIR | 0700);
2408         if (IS_ERR(inode))
2409                 goto fail;
2410         inode->i_op = &btrfs_dir_inode_operations;
2411         inode->i_fop = &btrfs_dir_file_operations;
2412         new_root->inode = inode;
2413
2414         ret = btrfs_insert_inode_ref(trans, new_root, "..", 2, new_dirid,
2415                                      new_dirid);
2416         inode->i_nlink = 1;
2417         inode->i_size = 0;
2418         ret = btrfs_update_inode(trans, new_root, inode);
2419         if (ret)
2420                 goto fail;
2421 fail:
2422         nr = trans->blocks_used;
2423         err = btrfs_commit_transaction(trans, new_root);
2424         if (err && !ret)
2425                 ret = err;
2426 fail_commit:
2427         mutex_unlock(&root->fs_info->fs_mutex);
2428         btrfs_btree_balance_dirty(root, nr);
2429         btrfs_throttle(root);
2430         return ret;
2431 }
2432
2433 static int create_snapshot(struct btrfs_root *root, char *name, int namelen)
2434 {
2435         struct btrfs_pending_snapshot *pending_snapshot;
2436         struct btrfs_trans_handle *trans;
2437         int ret;
2438         int err;
2439         unsigned long nr = 0;
2440
2441         if (!root->ref_cows)
2442                 return -EINVAL;
2443
2444         mutex_lock(&root->fs_info->fs_mutex);
2445         ret = btrfs_check_free_space(root, 1, 0);
2446         if (ret)
2447                 goto fail_unlock;
2448
2449         pending_snapshot = kmalloc(sizeof(*pending_snapshot), GFP_NOFS);
2450         if (!pending_snapshot) {
2451                 ret = -ENOMEM;
2452                 goto fail_unlock;
2453         }
2454         pending_snapshot->name = kmalloc(namelen + 1, GFP_NOFS);
2455         if (!pending_snapshot->name) {
2456                 ret = -ENOMEM;
2457                 kfree(pending_snapshot);
2458                 goto fail_unlock;
2459         }
2460         memcpy(pending_snapshot->name, name, namelen);
2461         pending_snapshot->name[namelen] = '\0';
2462         trans = btrfs_start_transaction(root, 1);
2463         BUG_ON(!trans);
2464         pending_snapshot->root = root;
2465         list_add(&pending_snapshot->list,
2466                  &trans->transaction->pending_snapshots);
2467         ret = btrfs_update_inode(trans, root, root->inode);
2468         err = btrfs_commit_transaction(trans, root);
2469
2470 fail_unlock:
2471         mutex_unlock(&root->fs_info->fs_mutex);
2472         btrfs_btree_balance_dirty(root, nr);
2473         btrfs_throttle(root);
2474         return ret;
2475 }
2476
2477 unsigned long btrfs_force_ra(struct address_space *mapping,
2478                               struct file_ra_state *ra, struct file *file,
2479                               pgoff_t offset, pgoff_t last_index)
2480 {
2481         pgoff_t req_size;
2482
2483 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2484         req_size = last_index - offset + 1;
2485         offset = page_cache_readahead(mapping, ra, file, offset, req_size);
2486         return offset;
2487 #else
2488         req_size = min(last_index - offset + 1, (pgoff_t)128);
2489         page_cache_sync_readahead(mapping, ra, file, offset, req_size);
2490         return offset + req_size;
2491 #endif
2492 }
2493
2494 int btrfs_defrag_file(struct file *file) {
2495         struct inode *inode = fdentry(file)->d_inode;
2496         struct btrfs_root *root = BTRFS_I(inode)->root;
2497         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2498         struct page *page;
2499         unsigned long last_index;
2500         unsigned long ra_index = 0;
2501         u64 page_start;
2502         u64 page_end;
2503         unsigned long i;
2504         int ret;
2505
2506         mutex_lock(&root->fs_info->fs_mutex);
2507         ret = btrfs_check_free_space(root, inode->i_size, 0);
2508         mutex_unlock(&root->fs_info->fs_mutex);
2509         if (ret)
2510                 return -ENOSPC;
2511
2512         mutex_lock(&inode->i_mutex);
2513         last_index = inode->i_size >> PAGE_CACHE_SHIFT;
2514         for (i = 0; i <= last_index; i++) {
2515                 if (i == ra_index) {
2516                         ra_index = btrfs_force_ra(inode->i_mapping,
2517                                                   &file->f_ra,
2518                                                   file, ra_index, last_index);
2519                 }
2520                 page = grab_cache_page(inode->i_mapping, i);
2521                 if (!page)
2522                         goto out_unlock;
2523                 if (!PageUptodate(page)) {
2524                         btrfs_readpage(NULL, page);
2525                         lock_page(page);
2526                         if (!PageUptodate(page)) {
2527                                 unlock_page(page);
2528                                 page_cache_release(page);
2529                                 goto out_unlock;
2530                         }
2531                 }
2532                 page_start = (u64)page->index << PAGE_CACHE_SHIFT;
2533                 page_end = page_start + PAGE_CACHE_SIZE - 1;
2534
2535                 lock_extent(io_tree, page_start, page_end, GFP_NOFS);
2536                 set_extent_delalloc(io_tree, page_start,
2537                                     page_end, GFP_NOFS);
2538
2539                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
2540                 set_page_dirty(page);
2541                 unlock_page(page);
2542                 page_cache_release(page);
2543                 balance_dirty_pages_ratelimited_nr(inode->i_mapping, 1);
2544         }
2545
2546 out_unlock:
2547         mutex_unlock(&inode->i_mutex);
2548         return 0;
2549 }
2550
2551 static int btrfs_ioctl_resize(struct btrfs_root *root, void __user *arg)
2552 {
2553         u64 new_size;
2554         u64 old_size;
2555         struct btrfs_ioctl_vol_args *vol_args;
2556         struct btrfs_trans_handle *trans;
2557         char *sizestr;
2558         int ret = 0;
2559         int namelen;
2560         int mod = 0;
2561
2562         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2563
2564         if (!vol_args)
2565                 return -ENOMEM;
2566
2567         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2568                 ret = -EFAULT;
2569                 goto out;
2570         }
2571         namelen = strlen(vol_args->name);
2572         if (namelen > BTRFS_VOL_NAME_MAX) {
2573                 ret = -EINVAL;
2574                 goto out;
2575         }
2576
2577         sizestr = vol_args->name;
2578         if (!strcmp(sizestr, "max"))
2579                 new_size = root->fs_info->sb->s_bdev->bd_inode->i_size;
2580         else {
2581                 if (sizestr[0] == '-') {
2582                         mod = -1;
2583                         sizestr++;
2584                 } else if (sizestr[0] == '+') {
2585                         mod = 1;
2586                         sizestr++;
2587                 }
2588                 new_size = btrfs_parse_size(sizestr);
2589                 if (new_size == 0) {
2590                         ret = -EINVAL;
2591                         goto out;
2592                 }
2593         }
2594
2595         mutex_lock(&root->fs_info->fs_mutex);
2596         old_size = btrfs_super_total_bytes(&root->fs_info->super_copy);
2597
2598         if (mod < 0) {
2599                 if (new_size > old_size) {
2600                         ret = -EINVAL;
2601                         goto out_unlock;
2602                 }
2603                 new_size = old_size - new_size;
2604         } else if (mod > 0) {
2605                 new_size = old_size + new_size;
2606         }
2607
2608         if (new_size < 256 * 1024 * 1024) {
2609                 ret = -EINVAL;
2610                 goto out_unlock;
2611         }
2612         if (new_size > root->fs_info->sb->s_bdev->bd_inode->i_size) {
2613                 ret = -EFBIG;
2614                 goto out_unlock;
2615         }
2616
2617         do_div(new_size, root->sectorsize);
2618         new_size *= root->sectorsize;
2619
2620 printk("new size is %Lu\n", new_size);
2621         if (new_size > old_size) {
2622                 trans = btrfs_start_transaction(root, 1);
2623                 ret = btrfs_grow_extent_tree(trans, root, new_size);
2624                 btrfs_commit_transaction(trans, root);
2625         } else {
2626                 ret = btrfs_shrink_extent_tree(root, new_size);
2627         }
2628
2629 out_unlock:
2630         mutex_unlock(&root->fs_info->fs_mutex);
2631 out:
2632         kfree(vol_args);
2633         return ret;
2634 }
2635
2636 static int noinline btrfs_ioctl_snap_create(struct btrfs_root *root,
2637                                             void __user *arg)
2638 {
2639         struct btrfs_ioctl_vol_args *vol_args;
2640         struct btrfs_dir_item *di;
2641         struct btrfs_path *path;
2642         u64 root_dirid;
2643         int namelen;
2644         int ret;
2645
2646         vol_args = kmalloc(sizeof(*vol_args), GFP_NOFS);
2647
2648         if (!vol_args)
2649                 return -ENOMEM;
2650
2651         if (copy_from_user(vol_args, arg, sizeof(*vol_args))) {
2652                 ret = -EFAULT;
2653                 goto out;
2654         }
2655
2656         namelen = strlen(vol_args->name);
2657         if (namelen > BTRFS_VOL_NAME_MAX) {
2658                 ret = -EINVAL;
2659                 goto out;
2660         }
2661         if (strchr(vol_args->name, '/')) {
2662                 ret = -EINVAL;
2663                 goto out;
2664         }
2665
2666         path = btrfs_alloc_path();
2667         if (!path) {
2668                 ret = -ENOMEM;
2669                 goto out;
2670         }
2671
2672         root_dirid = root->fs_info->sb->s_root->d_inode->i_ino,
2673         mutex_lock(&root->fs_info->fs_mutex);
2674         di = btrfs_lookup_dir_item(NULL, root->fs_info->tree_root,
2675                             path, root_dirid,
2676                             vol_args->name, namelen, 0);
2677         mutex_unlock(&root->fs_info->fs_mutex);
2678         btrfs_free_path(path);
2679
2680         if (di && !IS_ERR(di)) {
2681                 ret = -EEXIST;
2682                 goto out;
2683         }
2684
2685         if (IS_ERR(di)) {
2686                 ret = PTR_ERR(di);
2687                 goto out;
2688         }
2689
2690         if (root == root->fs_info->tree_root)
2691                 ret = create_subvol(root, vol_args->name, namelen);
2692         else
2693                 ret = create_snapshot(root, vol_args->name, namelen);
2694 out:
2695         kfree(vol_args);
2696         return ret;
2697 }
2698
2699 static int btrfs_ioctl_defrag(struct file *file)
2700 {
2701         struct inode *inode = fdentry(file)->d_inode;
2702         struct btrfs_root *root = BTRFS_I(inode)->root;
2703
2704         switch (inode->i_mode & S_IFMT) {
2705         case S_IFDIR:
2706                 mutex_lock(&root->fs_info->fs_mutex);
2707                 btrfs_defrag_root(root, 0);
2708                 btrfs_defrag_root(root->fs_info->extent_root, 0);
2709                 mutex_unlock(&root->fs_info->fs_mutex);
2710                 break;
2711         case S_IFREG:
2712                 btrfs_defrag_file(file);
2713                 break;
2714         }
2715
2716         return 0;
2717 }
2718
2719 long btrfs_ioctl(struct file *file, unsigned int
2720                 cmd, unsigned long arg)
2721 {
2722         struct btrfs_root *root = BTRFS_I(fdentry(file)->d_inode)->root;
2723
2724         switch (cmd) {
2725         case BTRFS_IOC_SNAP_CREATE:
2726                 return btrfs_ioctl_snap_create(root, (void __user *)arg);
2727         case BTRFS_IOC_DEFRAG:
2728                 return btrfs_ioctl_defrag(file);
2729         case BTRFS_IOC_RESIZE:
2730                 return btrfs_ioctl_resize(root, (void __user *)arg);
2731         }
2732
2733         return -ENOTTY;
2734 }
2735
2736 /*
2737  * Called inside transaction, so use GFP_NOFS
2738  */
2739 struct inode *btrfs_alloc_inode(struct super_block *sb)
2740 {
2741         struct btrfs_inode *ei;
2742
2743         ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
2744         if (!ei)
2745                 return NULL;
2746         ei->last_trans = 0;
2747         ei->ordered_trans = 0;
2748         return &ei->vfs_inode;
2749 }
2750
2751 void btrfs_destroy_inode(struct inode *inode)
2752 {
2753         WARN_ON(!list_empty(&inode->i_dentry));
2754         WARN_ON(inode->i_data.nrpages);
2755
2756         btrfs_drop_extent_cache(inode, 0, (u64)-1);
2757         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
2758 }
2759
2760 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2761 static void init_once(struct kmem_cache * cachep, void *foo)
2762 #else
2763 static void init_once(void * foo, struct kmem_cache * cachep,
2764                       unsigned long flags)
2765 #endif
2766 {
2767         struct btrfs_inode *ei = (struct btrfs_inode *) foo;
2768
2769         inode_init_once(&ei->vfs_inode);
2770 }
2771
2772 void btrfs_destroy_cachep(void)
2773 {
2774         if (btrfs_inode_cachep)
2775                 kmem_cache_destroy(btrfs_inode_cachep);
2776         if (btrfs_trans_handle_cachep)
2777                 kmem_cache_destroy(btrfs_trans_handle_cachep);
2778         if (btrfs_transaction_cachep)
2779                 kmem_cache_destroy(btrfs_transaction_cachep);
2780         if (btrfs_bit_radix_cachep)
2781                 kmem_cache_destroy(btrfs_bit_radix_cachep);
2782         if (btrfs_path_cachep)
2783                 kmem_cache_destroy(btrfs_path_cachep);
2784 }
2785
2786 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
2787                                        unsigned long extra_flags,
2788 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
2789                                        void (*ctor)(struct kmem_cache *, void *)
2790 #else
2791                                        void (*ctor)(void *, struct kmem_cache *,
2792                                                     unsigned long)
2793 #endif
2794                                      )
2795 {
2796         return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
2797                                  SLAB_MEM_SPREAD | extra_flags), ctor
2798 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
2799                                  ,NULL
2800 #endif
2801                                 );
2802 }
2803
2804 int btrfs_init_cachep(void)
2805 {
2806         btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
2807                                           sizeof(struct btrfs_inode),
2808                                           0, init_once);
2809         if (!btrfs_inode_cachep)
2810                 goto fail;
2811         btrfs_trans_handle_cachep =
2812                         btrfs_cache_create("btrfs_trans_handle_cache",
2813                                            sizeof(struct btrfs_trans_handle),
2814                                            0, NULL);
2815         if (!btrfs_trans_handle_cachep)
2816                 goto fail;
2817         btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
2818                                              sizeof(struct btrfs_transaction),
2819                                              0, NULL);
2820         if (!btrfs_transaction_cachep)
2821                 goto fail;
2822         btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
2823                                          sizeof(struct btrfs_path),
2824                                          0, NULL);
2825         if (!btrfs_path_cachep)
2826                 goto fail;
2827         btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
2828                                               SLAB_DESTROY_BY_RCU, NULL);
2829         if (!btrfs_bit_radix_cachep)
2830                 goto fail;
2831         return 0;
2832 fail:
2833         btrfs_destroy_cachep();
2834         return -ENOMEM;
2835 }
2836
2837 static int btrfs_getattr(struct vfsmount *mnt,
2838                          struct dentry *dentry, struct kstat *stat)
2839 {
2840         struct inode *inode = dentry->d_inode;
2841         generic_fillattr(inode, stat);
2842         stat->blksize = PAGE_CACHE_SIZE;
2843         stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
2844         return 0;
2845 }
2846
2847 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
2848                            struct inode * new_dir,struct dentry *new_dentry)
2849 {
2850         struct btrfs_trans_handle *trans;
2851         struct btrfs_root *root = BTRFS_I(old_dir)->root;
2852         struct inode *new_inode = new_dentry->d_inode;
2853         struct inode *old_inode = old_dentry->d_inode;
2854         struct timespec ctime = CURRENT_TIME;
2855         struct btrfs_path *path;
2856         int ret;
2857
2858         if (S_ISDIR(old_inode->i_mode) && new_inode &&
2859             new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
2860                 return -ENOTEMPTY;
2861         }
2862
2863         mutex_lock(&root->fs_info->fs_mutex);
2864         ret = btrfs_check_free_space(root, 1, 0);
2865         if (ret)
2866                 goto out_unlock;
2867
2868         trans = btrfs_start_transaction(root, 1);
2869
2870         btrfs_set_trans_block_group(trans, new_dir);
2871         path = btrfs_alloc_path();
2872         if (!path) {
2873                 ret = -ENOMEM;
2874                 goto out_fail;
2875         }
2876
2877         old_dentry->d_inode->i_nlink++;
2878         old_dir->i_ctime = old_dir->i_mtime = ctime;
2879         new_dir->i_ctime = new_dir->i_mtime = ctime;
2880         old_inode->i_ctime = ctime;
2881
2882         ret = btrfs_unlink_trans(trans, root, old_dir, old_dentry);
2883         if (ret)
2884                 goto out_fail;
2885
2886         if (new_inode) {
2887                 new_inode->i_ctime = CURRENT_TIME;
2888                 ret = btrfs_unlink_trans(trans, root, new_dir, new_dentry);
2889                 if (ret)
2890                         goto out_fail;
2891         }
2892         ret = btrfs_add_link(trans, new_dentry, old_inode, 1);
2893         if (ret)
2894                 goto out_fail;
2895
2896 out_fail:
2897         btrfs_free_path(path);
2898         btrfs_end_transaction(trans, root);
2899 out_unlock:
2900         mutex_unlock(&root->fs_info->fs_mutex);
2901         return ret;
2902 }
2903
2904 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
2905                          const char *symname)
2906 {
2907         struct btrfs_trans_handle *trans;
2908         struct btrfs_root *root = BTRFS_I(dir)->root;
2909         struct btrfs_path *path;
2910         struct btrfs_key key;
2911         struct inode *inode = NULL;
2912         int err;
2913         int drop_inode = 0;
2914         u64 objectid;
2915         int name_len;
2916         int datasize;
2917         unsigned long ptr;
2918         struct btrfs_file_extent_item *ei;
2919         struct extent_buffer *leaf;
2920         unsigned long nr = 0;
2921
2922         name_len = strlen(symname) + 1;
2923         if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
2924                 return -ENAMETOOLONG;
2925
2926         mutex_lock(&root->fs_info->fs_mutex);
2927         err = btrfs_check_free_space(root, 1, 0);
2928         if (err)
2929                 goto out_fail;
2930
2931         trans = btrfs_start_transaction(root, 1);
2932         btrfs_set_trans_block_group(trans, dir);
2933
2934         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2935         if (err) {
2936                 err = -ENOSPC;
2937                 goto out_unlock;
2938         }
2939
2940         inode = btrfs_new_inode(trans, root, dentry->d_name.name,
2941                                 dentry->d_name.len,
2942                                 dentry->d_parent->d_inode->i_ino, objectid,
2943                                 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO);
2944         err = PTR_ERR(inode);
2945         if (IS_ERR(inode))
2946                 goto out_unlock;
2947
2948         btrfs_set_trans_block_group(trans, inode);
2949         err = btrfs_add_nondir(trans, dentry, inode, 0);
2950         if (err)
2951                 drop_inode = 1;
2952         else {
2953                 inode->i_mapping->a_ops = &btrfs_aops;
2954                 inode->i_fop = &btrfs_file_operations;
2955                 inode->i_op = &btrfs_file_inode_operations;
2956                 extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
2957                 extent_io_tree_init(&BTRFS_I(inode)->io_tree,
2958                                      inode->i_mapping, GFP_NOFS);
2959                 BTRFS_I(inode)->delalloc_bytes = 0;
2960                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2961         }
2962         dir->i_sb->s_dirt = 1;
2963         btrfs_update_inode_block_group(trans, inode);
2964         btrfs_update_inode_block_group(trans, dir);
2965         if (drop_inode)
2966                 goto out_unlock;
2967
2968         path = btrfs_alloc_path();
2969         BUG_ON(!path);
2970         key.objectid = inode->i_ino;
2971         key.offset = 0;
2972         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
2973         datasize = btrfs_file_extent_calc_inline_size(name_len);
2974         err = btrfs_insert_empty_item(trans, root, path, &key,
2975                                       datasize);
2976         if (err) {
2977                 drop_inode = 1;
2978                 goto out_unlock;
2979         }
2980         leaf = path->nodes[0];
2981         ei = btrfs_item_ptr(leaf, path->slots[0],
2982                             struct btrfs_file_extent_item);
2983         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
2984         btrfs_set_file_extent_type(leaf, ei,
2985                                    BTRFS_FILE_EXTENT_INLINE);
2986         ptr = btrfs_file_extent_inline_start(ei);
2987         write_extent_buffer(leaf, symname, ptr, name_len);
2988         btrfs_mark_buffer_dirty(leaf);
2989         btrfs_free_path(path);
2990
2991         inode->i_op = &btrfs_symlink_inode_operations;
2992         inode->i_mapping->a_ops = &btrfs_symlink_aops;
2993         inode->i_size = name_len - 1;
2994         err = btrfs_update_inode(trans, root, inode);
2995         if (err)
2996                 drop_inode = 1;
2997
2998 out_unlock:
2999         nr = trans->blocks_used;
3000         btrfs_end_transaction(trans, root);
3001 out_fail:
3002         mutex_unlock(&root->fs_info->fs_mutex);
3003         if (drop_inode) {
3004                 inode_dec_link_count(inode);
3005                 iput(inode);
3006         }
3007         btrfs_btree_balance_dirty(root, nr);
3008         btrfs_throttle(root);
3009         return err;
3010 }
3011 static int btrfs_permission(struct inode *inode, int mask,
3012                             struct nameidata *nd)
3013 {
3014         if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3015                 return -EACCES;
3016         return generic_permission(inode, mask, NULL);
3017 }
3018
3019 static struct inode_operations btrfs_dir_inode_operations = {
3020         .lookup         = btrfs_lookup,
3021         .create         = btrfs_create,
3022         .unlink         = btrfs_unlink,
3023         .link           = btrfs_link,
3024         .mkdir          = btrfs_mkdir,
3025         .rmdir          = btrfs_rmdir,
3026         .rename         = btrfs_rename,
3027         .symlink        = btrfs_symlink,
3028         .setattr        = btrfs_setattr,
3029         .mknod          = btrfs_mknod,
3030         .setxattr       = generic_setxattr,
3031         .getxattr       = generic_getxattr,
3032         .listxattr      = btrfs_listxattr,
3033         .removexattr    = generic_removexattr,
3034         .permission     = btrfs_permission,
3035 };
3036 static struct inode_operations btrfs_dir_ro_inode_operations = {
3037         .lookup         = btrfs_lookup,
3038         .permission     = btrfs_permission,
3039 };
3040 static struct file_operations btrfs_dir_file_operations = {
3041         .llseek         = generic_file_llseek,
3042         .read           = generic_read_dir,
3043         .readdir        = btrfs_readdir,
3044         .unlocked_ioctl = btrfs_ioctl,
3045 #ifdef CONFIG_COMPAT
3046         .compat_ioctl   = btrfs_ioctl,
3047 #endif
3048 };
3049
3050 static struct extent_io_ops btrfs_extent_io_ops = {
3051         .fill_delalloc = run_delalloc_range,
3052         // .writepage_io_hook = btrfs_writepage_io_hook,
3053         .submit_bio_hook = btrfs_submit_bio_hook,
3054         .readpage_io_hook = btrfs_readpage_io_hook,
3055         .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3056         .set_bit_hook = btrfs_set_bit_hook,
3057         .clear_bit_hook = btrfs_clear_bit_hook,
3058 };
3059
3060 static struct address_space_operations btrfs_aops = {
3061         .readpage       = btrfs_readpage,
3062         .writepage      = btrfs_writepage,
3063         .writepages     = btrfs_writepages,
3064         .readpages      = btrfs_readpages,
3065         .sync_page      = block_sync_page,
3066         .bmap           = btrfs_bmap,
3067         .invalidatepage = btrfs_invalidatepage,
3068         .releasepage    = btrfs_releasepage,
3069         .set_page_dirty = __set_page_dirty_nobuffers,
3070 };
3071
3072 static struct address_space_operations btrfs_symlink_aops = {
3073         .readpage       = btrfs_readpage,
3074         .writepage      = btrfs_writepage,
3075         .invalidatepage = btrfs_invalidatepage,
3076         .releasepage    = btrfs_releasepage,
3077 };
3078
3079 static struct inode_operations btrfs_file_inode_operations = {
3080         .truncate       = btrfs_truncate,
3081         .getattr        = btrfs_getattr,
3082         .setattr        = btrfs_setattr,
3083         .setxattr       = generic_setxattr,
3084         .getxattr       = generic_getxattr,
3085         .listxattr      = btrfs_listxattr,
3086         .removexattr    = generic_removexattr,
3087         .permission     = btrfs_permission,
3088 };
3089 static struct inode_operations btrfs_special_inode_operations = {
3090         .getattr        = btrfs_getattr,
3091         .setattr        = btrfs_setattr,
3092         .permission     = btrfs_permission,
3093 };
3094 static struct inode_operations btrfs_symlink_inode_operations = {
3095         .readlink       = generic_readlink,
3096         .follow_link    = page_follow_link_light,
3097         .put_link       = page_put_link,
3098         .permission     = btrfs_permission,
3099 };