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