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