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