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