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