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