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