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