Btrfs: Fix race against disk_i_size updates
[pandora-kernel.git] / fs / btrfs / inode.c
1 /*
2  * Copyright (C) 2007 Oracle.  All rights reserved.
3  *
4  * This program is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU General Public
6  * License v2 as published by the Free Software Foundation.
7  *
8  * This program is distributed in the hope that it will be useful,
9  * but WITHOUT ANY WARRANTY; without even the implied warranty of
10  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11  * General Public License for more details.
12  *
13  * You should have received a copy of the GNU General Public
14  * License along with this program; if not, write to the
15  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
16  * Boston, MA 021110-1307, USA.
17  */
18
19 #include <linux/kernel.h>
20 #include <linux/bio.h>
21 #include <linux/buffer_head.h>
22 #include <linux/file.h>
23 #include <linux/fs.h>
24 #include <linux/pagemap.h>
25 #include <linux/highmem.h>
26 #include <linux/time.h>
27 #include <linux/init.h>
28 #include <linux/string.h>
29 #include <linux/smp_lock.h>
30 #include <linux/backing-dev.h>
31 #include <linux/mpage.h>
32 #include <linux/swap.h>
33 #include <linux/writeback.h>
34 #include <linux/statfs.h>
35 #include <linux/compat.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/version.h>
38 #include <linux/xattr.h>
39 #include <linux/posix_acl.h>
40 #include "ctree.h"
41 #include "disk-io.h"
42 #include "transaction.h"
43 #include "btrfs_inode.h"
44 #include "ioctl.h"
45 #include "print-tree.h"
46 #include "volumes.h"
47 #include "ordered-data.h"
48 #include "xattr.h"
49 #include "compat.h"
50 #include "tree-log.h"
51
52 struct btrfs_iget_args {
53         u64 ino;
54         struct btrfs_root *root;
55 };
56
57 static struct inode_operations btrfs_dir_inode_operations;
58 static struct inode_operations btrfs_symlink_inode_operations;
59 static struct inode_operations btrfs_dir_ro_inode_operations;
60 static struct inode_operations btrfs_special_inode_operations;
61 static struct inode_operations btrfs_file_inode_operations;
62 static struct address_space_operations btrfs_aops;
63 static struct address_space_operations btrfs_symlink_aops;
64 static struct file_operations btrfs_dir_file_operations;
65 static struct extent_io_ops btrfs_extent_io_ops;
66
67 static struct kmem_cache *btrfs_inode_cachep;
68 struct kmem_cache *btrfs_trans_handle_cachep;
69 struct kmem_cache *btrfs_transaction_cachep;
70 struct kmem_cache *btrfs_bit_radix_cachep;
71 struct kmem_cache *btrfs_path_cachep;
72
73 #define S_SHIFT 12
74 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75         [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
76         [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
77         [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
78         [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
79         [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
80         [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
81         [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
82 };
83
84 static void btrfs_truncate(struct inode *inode);
85
86 int btrfs_check_free_space(struct btrfs_root *root, u64 num_required,
87                            int for_del)
88 {
89         u64 total;
90         u64 used;
91         u64 thresh;
92         unsigned long flags;
93         int ret = 0;
94
95         spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
96         total = btrfs_super_total_bytes(&root->fs_info->super_copy);
97         used = btrfs_super_bytes_used(&root->fs_info->super_copy);
98         if (for_del)
99                 thresh = total * 90;
100         else
101                 thresh = total * 85;
102
103         do_div(thresh, 100);
104
105         if (used + root->fs_info->delalloc_bytes + num_required > thresh)
106                 ret = -ENOSPC;
107         spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
108         return ret;
109 }
110
111 static int cow_file_range(struct inode *inode, u64 start, u64 end)
112 {
113         struct btrfs_root *root = BTRFS_I(inode)->root;
114         struct btrfs_trans_handle *trans;
115         u64 alloc_hint = 0;
116         u64 num_bytes;
117         u64 cur_alloc_size;
118         u64 blocksize = root->sectorsize;
119         u64 orig_num_bytes;
120         struct btrfs_key ins;
121         struct extent_map *em;
122         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
123         int ret = 0;
124
125         trans = btrfs_join_transaction(root, 1);
126         BUG_ON(!trans);
127         btrfs_set_trans_block_group(trans, inode);
128
129         num_bytes = (end - start + blocksize) & ~(blocksize - 1);
130         num_bytes = max(blocksize,  num_bytes);
131         orig_num_bytes = num_bytes;
132
133         if (alloc_hint == EXTENT_MAP_INLINE)
134                 goto out;
135
136         BUG_ON(num_bytes > btrfs_super_total_bytes(&root->fs_info->super_copy));
137         mutex_lock(&BTRFS_I(inode)->extent_mutex);
138         btrfs_drop_extent_cache(inode, start, start + num_bytes - 1);
139         mutex_unlock(&BTRFS_I(inode)->extent_mutex);
140
141         while(num_bytes > 0) {
142                 cur_alloc_size = min(num_bytes, root->fs_info->max_extent);
143                 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
144                                            root->sectorsize, 0, alloc_hint,
145                                            (u64)-1, &ins, 1);
146                 if (ret) {
147                         WARN_ON(1);
148                         goto out;
149                 }
150                 em = alloc_extent_map(GFP_NOFS);
151                 em->start = start;
152                 em->len = ins.offset;
153                 em->block_start = ins.objectid;
154                 em->bdev = root->fs_info->fs_devices->latest_bdev;
155                 mutex_lock(&BTRFS_I(inode)->extent_mutex);
156                 set_bit(EXTENT_FLAG_PINNED, &em->flags);
157                 while(1) {
158                         spin_lock(&em_tree->lock);
159                         ret = add_extent_mapping(em_tree, em);
160                         spin_unlock(&em_tree->lock);
161                         if (ret != -EEXIST) {
162                                 free_extent_map(em);
163                                 break;
164                         }
165                         btrfs_drop_extent_cache(inode, start,
166                                                 start + ins.offset - 1);
167                 }
168                 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
169
170                 cur_alloc_size = ins.offset;
171                 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
172                                                ins.offset, 0);
173                 BUG_ON(ret);
174                 if (num_bytes < cur_alloc_size) {
175                         printk("num_bytes %Lu cur_alloc %Lu\n", num_bytes,
176                                cur_alloc_size);
177                         break;
178                 }
179                 num_bytes -= cur_alloc_size;
180                 alloc_hint = ins.objectid + ins.offset;
181                 start += cur_alloc_size;
182         }
183 out:
184         btrfs_end_transaction(trans, root);
185         return ret;
186 }
187
188 static int run_delalloc_nocow(struct inode *inode, u64 start, u64 end)
189 {
190         u64 extent_start;
191         u64 extent_end;
192         u64 bytenr;
193         u64 loops = 0;
194         u64 total_fs_bytes;
195         struct btrfs_root *root = BTRFS_I(inode)->root;
196         struct btrfs_block_group_cache *block_group;
197         struct btrfs_trans_handle *trans;
198         struct extent_buffer *leaf;
199         int found_type;
200         struct btrfs_path *path;
201         struct btrfs_file_extent_item *item;
202         int ret;
203         int err = 0;
204         struct btrfs_key found_key;
205
206         total_fs_bytes = btrfs_super_total_bytes(&root->fs_info->super_copy);
207         path = btrfs_alloc_path();
208         BUG_ON(!path);
209         trans = btrfs_join_transaction(root, 1);
210         BUG_ON(!trans);
211 again:
212         ret = btrfs_lookup_file_extent(NULL, root, path,
213                                        inode->i_ino, start, 0);
214         if (ret < 0) {
215                 err = ret;
216                 goto out;
217         }
218
219         if (ret != 0) {
220                 if (path->slots[0] == 0)
221                         goto not_found;
222                 path->slots[0]--;
223         }
224
225         leaf = path->nodes[0];
226         item = btrfs_item_ptr(leaf, path->slots[0],
227                               struct btrfs_file_extent_item);
228
229         /* are we inside the extent that was found? */
230         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
231         found_type = btrfs_key_type(&found_key);
232         if (found_key.objectid != inode->i_ino ||
233             found_type != BTRFS_EXTENT_DATA_KEY)
234                 goto not_found;
235
236         found_type = btrfs_file_extent_type(leaf, item);
237         extent_start = found_key.offset;
238         if (found_type == BTRFS_FILE_EXTENT_REG) {
239                 u64 extent_num_bytes;
240
241                 extent_num_bytes = btrfs_file_extent_num_bytes(leaf, item);
242                 extent_end = extent_start + extent_num_bytes;
243                 err = 0;
244
245                 if (loops && start != extent_start)
246                         goto not_found;
247
248                 if (start < extent_start || start >= extent_end)
249                         goto not_found;
250
251                 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
252                 if (bytenr == 0)
253                         goto not_found;
254
255                 if (btrfs_cross_ref_exists(trans, root, &found_key, bytenr))
256                         goto not_found;
257                 /*
258                  * we may be called by the resizer, make sure we're inside
259                  * the limits of the FS
260                  */
261                 block_group = btrfs_lookup_block_group(root->fs_info,
262                                                        bytenr);
263                 if (!block_group || block_group->ro)
264                         goto not_found;
265
266                 bytenr += btrfs_file_extent_offset(leaf, item);
267                 extent_num_bytes = min(end + 1, extent_end) - start;
268                 ret = btrfs_add_ordered_extent(inode, start, bytenr,
269                                                 extent_num_bytes, 1);
270                 if (ret) {
271                         err = ret;
272                         goto out;
273                 }
274
275                 btrfs_release_path(root, path);
276                 start = extent_end;
277                 if (start <= end) {
278                         loops++;
279                         goto again;
280                 }
281         } else {
282 not_found:
283                 btrfs_end_transaction(trans, root);
284                 btrfs_free_path(path);
285                 return cow_file_range(inode, start, end);
286         }
287 out:
288         WARN_ON(err);
289         btrfs_end_transaction(trans, root);
290         btrfs_free_path(path);
291         return err;
292 }
293
294 static int run_delalloc_range(struct inode *inode, u64 start, u64 end)
295 {
296         struct btrfs_root *root = BTRFS_I(inode)->root;
297         int ret;
298
299         if (btrfs_test_opt(root, NODATACOW) ||
300             btrfs_test_flag(inode, NODATACOW))
301                 ret = run_delalloc_nocow(inode, start, end);
302         else
303                 ret = cow_file_range(inode, start, end);
304
305         return ret;
306 }
307
308 int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
309                        unsigned long old, unsigned long bits)
310 {
311         unsigned long flags;
312         if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
313                 struct btrfs_root *root = BTRFS_I(inode)->root;
314                 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
315                 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
316                 root->fs_info->delalloc_bytes += end - start + 1;
317                 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
318                         list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
319                                       &root->fs_info->delalloc_inodes);
320                 }
321                 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
322         }
323         return 0;
324 }
325
326 int btrfs_clear_bit_hook(struct inode *inode, u64 start, u64 end,
327                          unsigned long old, unsigned long bits)
328 {
329         if ((old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
330                 struct btrfs_root *root = BTRFS_I(inode)->root;
331                 unsigned long flags;
332
333                 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
334                 if (end - start + 1 > root->fs_info->delalloc_bytes) {
335                         printk("warning: delalloc account %Lu %Lu\n",
336                                end - start + 1, root->fs_info->delalloc_bytes);
337                         root->fs_info->delalloc_bytes = 0;
338                         BTRFS_I(inode)->delalloc_bytes = 0;
339                 } else {
340                         root->fs_info->delalloc_bytes -= end - start + 1;
341                         BTRFS_I(inode)->delalloc_bytes -= end - start + 1;
342                 }
343                 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
344                     !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
345                         list_del_init(&BTRFS_I(inode)->delalloc_inodes);
346                 }
347                 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
348         }
349         return 0;
350 }
351
352 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
353                          size_t size, struct bio *bio)
354 {
355         struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
356         struct btrfs_mapping_tree *map_tree;
357         u64 logical = bio->bi_sector << 9;
358         u64 length = 0;
359         u64 map_length;
360         int ret;
361
362         length = bio->bi_size;
363         map_tree = &root->fs_info->mapping_tree;
364         map_length = length;
365         ret = btrfs_map_block(map_tree, READ, logical,
366                               &map_length, NULL, 0);
367
368         if (map_length < length + size) {
369                 return 1;
370         }
371         return 0;
372 }
373
374 int __btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
375                           int mirror_num)
376 {
377         struct btrfs_root *root = BTRFS_I(inode)->root;
378         int ret = 0;
379
380         ret = btrfs_csum_one_bio(root, inode, bio);
381         BUG_ON(ret);
382
383         return btrfs_map_bio(root, rw, bio, mirror_num, 1);
384 }
385
386 int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
387                           int mirror_num)
388 {
389         struct btrfs_root *root = BTRFS_I(inode)->root;
390         int ret = 0;
391
392         ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
393         BUG_ON(ret);
394
395         if (btrfs_test_opt(root, NODATASUM) ||
396             btrfs_test_flag(inode, NODATASUM)) {
397                 goto mapit;
398         }
399
400         if (!(rw & (1 << BIO_RW))) {
401                 btrfs_lookup_bio_sums(root, inode, bio);
402                 goto mapit;
403         }
404         return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
405                                    inode, rw, bio, mirror_num,
406                                    __btrfs_submit_bio_hook);
407 mapit:
408         return btrfs_map_bio(root, rw, bio, mirror_num, 0);
409 }
410
411 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
412                              struct inode *inode, u64 file_offset,
413                              struct list_head *list)
414 {
415         struct list_head *cur;
416         struct btrfs_ordered_sum *sum;
417
418         btrfs_set_trans_block_group(trans, inode);
419         list_for_each(cur, list) {
420                 sum = list_entry(cur, struct btrfs_ordered_sum, list);
421                 btrfs_csum_file_blocks(trans, BTRFS_I(inode)->root,
422                                        inode, sum);
423         }
424         return 0;
425 }
426
427 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
428 {
429         return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
430                                    GFP_NOFS);
431 }
432
433 struct btrfs_writepage_fixup {
434         struct page *page;
435         struct btrfs_work work;
436 };
437
438 /* see btrfs_writepage_start_hook for details on why this is required */
439 void btrfs_writepage_fixup_worker(struct btrfs_work *work)
440 {
441         struct btrfs_writepage_fixup *fixup;
442         struct btrfs_ordered_extent *ordered;
443         struct page *page;
444         struct inode *inode;
445         u64 page_start;
446         u64 page_end;
447
448         fixup = container_of(work, struct btrfs_writepage_fixup, work);
449         page = fixup->page;
450 again:
451         lock_page(page);
452         if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
453                 ClearPageChecked(page);
454                 goto out_page;
455         }
456
457         inode = page->mapping->host;
458         page_start = page_offset(page);
459         page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
460
461         lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
462
463         /* already ordered? We're done */
464         if (test_range_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
465                              EXTENT_ORDERED, 0)) {
466                 goto out;
467         }
468
469         ordered = btrfs_lookup_ordered_extent(inode, page_start);
470         if (ordered) {
471                 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
472                               page_end, GFP_NOFS);
473                 unlock_page(page);
474                 btrfs_start_ordered_extent(inode, ordered, 1);
475                 goto again;
476         }
477
478         btrfs_set_extent_delalloc(inode, page_start, page_end);
479         ClearPageChecked(page);
480 out:
481         unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
482 out_page:
483         unlock_page(page);
484         page_cache_release(page);
485 }
486
487 /*
488  * There are a few paths in the higher layers of the kernel that directly
489  * set the page dirty bit without asking the filesystem if it is a
490  * good idea.  This causes problems because we want to make sure COW
491  * properly happens and the data=ordered rules are followed.
492  *
493  * In our case any range that doesn't have the EXTENT_ORDERED bit set
494  * hasn't been properly setup for IO.  We kick off an async process
495  * to fix it up.  The async helper will wait for ordered extents, set
496  * the delalloc bit and make it safe to write the page.
497  */
498 int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
499 {
500         struct inode *inode = page->mapping->host;
501         struct btrfs_writepage_fixup *fixup;
502         struct btrfs_root *root = BTRFS_I(inode)->root;
503         int ret;
504
505         ret = test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
506                              EXTENT_ORDERED, 0);
507         if (ret)
508                 return 0;
509
510         if (PageChecked(page))
511                 return -EAGAIN;
512
513         fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
514         if (!fixup)
515                 return -EAGAIN;
516
517         SetPageChecked(page);
518         page_cache_get(page);
519         fixup->work.func = btrfs_writepage_fixup_worker;
520         fixup->page = page;
521         btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
522         return -EAGAIN;
523 }
524
525 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
526 {
527         struct btrfs_root *root = BTRFS_I(inode)->root;
528         struct btrfs_trans_handle *trans;
529         struct btrfs_ordered_extent *ordered_extent;
530         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
531         struct btrfs_file_extent_item *extent_item;
532         struct btrfs_path *path = NULL;
533         struct extent_buffer *leaf;
534         u64 alloc_hint = 0;
535         struct list_head list;
536         struct btrfs_key ins;
537         int ret;
538
539         ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
540         if (!ret)
541                 return 0;
542
543         trans = btrfs_join_transaction(root, 1);
544
545         ordered_extent = btrfs_lookup_ordered_extent(inode, start);
546         BUG_ON(!ordered_extent);
547         if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags))
548                 goto nocow;
549
550         path = btrfs_alloc_path();
551         BUG_ON(!path);
552
553         lock_extent(io_tree, ordered_extent->file_offset,
554                     ordered_extent->file_offset + ordered_extent->len - 1,
555                     GFP_NOFS);
556
557         INIT_LIST_HEAD(&list);
558
559         mutex_lock(&BTRFS_I(inode)->extent_mutex);
560
561         ret = btrfs_drop_extents(trans, root, inode,
562                                  ordered_extent->file_offset,
563                                  ordered_extent->file_offset +
564                                  ordered_extent->len,
565                                  ordered_extent->file_offset, &alloc_hint);
566         BUG_ON(ret);
567
568         ins.objectid = inode->i_ino;
569         ins.offset = ordered_extent->file_offset;
570         ins.type = BTRFS_EXTENT_DATA_KEY;
571         ret = btrfs_insert_empty_item(trans, root, path, &ins,
572                                       sizeof(*extent_item));
573         BUG_ON(ret);
574         leaf = path->nodes[0];
575         extent_item = btrfs_item_ptr(leaf, path->slots[0],
576                                      struct btrfs_file_extent_item);
577         btrfs_set_file_extent_generation(leaf, extent_item, trans->transid);
578         btrfs_set_file_extent_type(leaf, extent_item, BTRFS_FILE_EXTENT_REG);
579         btrfs_set_file_extent_disk_bytenr(leaf, extent_item,
580                                           ordered_extent->start);
581         btrfs_set_file_extent_disk_num_bytes(leaf, extent_item,
582                                              ordered_extent->len);
583         btrfs_set_file_extent_offset(leaf, extent_item, 0);
584         btrfs_set_file_extent_num_bytes(leaf, extent_item,
585                                         ordered_extent->len);
586         btrfs_mark_buffer_dirty(leaf);
587
588         btrfs_drop_extent_cache(inode, ordered_extent->file_offset,
589                                 ordered_extent->file_offset +
590                                 ordered_extent->len - 1);
591         mutex_unlock(&BTRFS_I(inode)->extent_mutex);
592
593         ins.objectid = ordered_extent->start;
594         ins.offset = ordered_extent->len;
595         ins.type = BTRFS_EXTENT_ITEM_KEY;
596         ret = btrfs_alloc_reserved_extent(trans, root, leaf->start,
597                                           root->root_key.objectid,
598                                           trans->transid, inode->i_ino,
599                                           ordered_extent->file_offset, &ins);
600         BUG_ON(ret);
601         btrfs_release_path(root, path);
602
603         inode->i_blocks += ordered_extent->len >> 9;
604         unlock_extent(io_tree, ordered_extent->file_offset,
605                     ordered_extent->file_offset + ordered_extent->len - 1,
606                     GFP_NOFS);
607 nocow:
608         add_pending_csums(trans, inode, ordered_extent->file_offset,
609                           &ordered_extent->list);
610
611         mutex_lock(&BTRFS_I(inode)->extent_mutex);
612         btrfs_ordered_update_i_size(inode, ordered_extent);
613         btrfs_update_inode(trans, root, inode);
614         btrfs_remove_ordered_extent(inode, ordered_extent);
615         mutex_unlock(&BTRFS_I(inode)->extent_mutex);
616
617         /* once for us */
618         btrfs_put_ordered_extent(ordered_extent);
619         /* once for the tree */
620         btrfs_put_ordered_extent(ordered_extent);
621
622         btrfs_end_transaction(trans, root);
623         if (path)
624                 btrfs_free_path(path);
625         return 0;
626 }
627
628 int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
629                                 struct extent_state *state, int uptodate)
630 {
631         return btrfs_finish_ordered_io(page->mapping->host, start, end);
632 }
633
634 struct io_failure_record {
635         struct page *page;
636         u64 start;
637         u64 len;
638         u64 logical;
639         int last_mirror;
640 };
641
642 int btrfs_io_failed_hook(struct bio *failed_bio,
643                          struct page *page, u64 start, u64 end,
644                          struct extent_state *state)
645 {
646         struct io_failure_record *failrec = NULL;
647         u64 private;
648         struct extent_map *em;
649         struct inode *inode = page->mapping->host;
650         struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
651         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
652         struct bio *bio;
653         int num_copies;
654         int ret;
655         int rw;
656         u64 logical;
657
658         ret = get_state_private(failure_tree, start, &private);
659         if (ret) {
660                 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
661                 if (!failrec)
662                         return -ENOMEM;
663                 failrec->start = start;
664                 failrec->len = end - start + 1;
665                 failrec->last_mirror = 0;
666
667                 spin_lock(&em_tree->lock);
668                 em = lookup_extent_mapping(em_tree, start, failrec->len);
669                 if (em->start > start || em->start + em->len < start) {
670                         free_extent_map(em);
671                         em = NULL;
672                 }
673                 spin_unlock(&em_tree->lock);
674
675                 if (!em || IS_ERR(em)) {
676                         kfree(failrec);
677                         return -EIO;
678                 }
679                 logical = start - em->start;
680                 logical = em->block_start + logical;
681                 failrec->logical = logical;
682                 free_extent_map(em);
683                 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
684                                 EXTENT_DIRTY, GFP_NOFS);
685                 set_state_private(failure_tree, start,
686                                  (u64)(unsigned long)failrec);
687         } else {
688                 failrec = (struct io_failure_record *)(unsigned long)private;
689         }
690         num_copies = btrfs_num_copies(
691                               &BTRFS_I(inode)->root->fs_info->mapping_tree,
692                               failrec->logical, failrec->len);
693         failrec->last_mirror++;
694         if (!state) {
695                 spin_lock_irq(&BTRFS_I(inode)->io_tree.lock);
696                 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
697                                                     failrec->start,
698                                                     EXTENT_LOCKED);
699                 if (state && state->start != failrec->start)
700                         state = NULL;
701                 spin_unlock_irq(&BTRFS_I(inode)->io_tree.lock);
702         }
703         if (!state || failrec->last_mirror > num_copies) {
704                 set_state_private(failure_tree, failrec->start, 0);
705                 clear_extent_bits(failure_tree, failrec->start,
706                                   failrec->start + failrec->len - 1,
707                                   EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
708                 kfree(failrec);
709                 return -EIO;
710         }
711         bio = bio_alloc(GFP_NOFS, 1);
712         bio->bi_private = state;
713         bio->bi_end_io = failed_bio->bi_end_io;
714         bio->bi_sector = failrec->logical >> 9;
715         bio->bi_bdev = failed_bio->bi_bdev;
716         bio->bi_size = 0;
717         bio_add_page(bio, page, failrec->len, start - page_offset(page));
718         if (failed_bio->bi_rw & (1 << BIO_RW))
719                 rw = WRITE;
720         else
721                 rw = READ;
722
723         BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
724                                                       failrec->last_mirror);
725         return 0;
726 }
727
728 int btrfs_clean_io_failures(struct inode *inode, u64 start)
729 {
730         u64 private;
731         u64 private_failure;
732         struct io_failure_record *failure;
733         int ret;
734
735         private = 0;
736         if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
737                              (u64)-1, 1, EXTENT_DIRTY)) {
738                 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
739                                         start, &private_failure);
740                 if (ret == 0) {
741                         failure = (struct io_failure_record *)(unsigned long)
742                                    private_failure;
743                         set_state_private(&BTRFS_I(inode)->io_failure_tree,
744                                           failure->start, 0);
745                         clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
746                                           failure->start,
747                                           failure->start + failure->len - 1,
748                                           EXTENT_DIRTY | EXTENT_LOCKED,
749                                           GFP_NOFS);
750                         kfree(failure);
751                 }
752         }
753         return 0;
754 }
755
756 int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
757                                struct extent_state *state)
758 {
759         size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
760         struct inode *inode = page->mapping->host;
761         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
762         char *kaddr;
763         u64 private = ~(u32)0;
764         int ret;
765         struct btrfs_root *root = BTRFS_I(inode)->root;
766         u32 csum = ~(u32)0;
767         unsigned long flags;
768
769         if (btrfs_test_opt(root, NODATASUM) ||
770             btrfs_test_flag(inode, NODATASUM))
771                 return 0;
772         if (state && state->start == start) {
773                 private = state->private;
774                 ret = 0;
775         } else {
776                 ret = get_state_private(io_tree, start, &private);
777         }
778         local_irq_save(flags);
779         kaddr = kmap_atomic(page, KM_IRQ0);
780         if (ret) {
781                 goto zeroit;
782         }
783         csum = btrfs_csum_data(root, kaddr + offset, csum,  end - start + 1);
784         btrfs_csum_final(csum, (char *)&csum);
785         if (csum != private) {
786                 goto zeroit;
787         }
788         kunmap_atomic(kaddr, KM_IRQ0);
789         local_irq_restore(flags);
790
791         /* if the io failure tree for this inode is non-empty,
792          * check to see if we've recovered from a failed IO
793          */
794         btrfs_clean_io_failures(inode, start);
795         return 0;
796
797 zeroit:
798         printk("btrfs csum failed ino %lu off %llu csum %u private %Lu\n",
799                page->mapping->host->i_ino, (unsigned long long)start, csum,
800                private);
801         memset(kaddr + offset, 1, end - start + 1);
802         flush_dcache_page(page);
803         kunmap_atomic(kaddr, KM_IRQ0);
804         local_irq_restore(flags);
805         if (private == 0)
806                 return 0;
807         return -EIO;
808 }
809
810 /*
811  * This creates an orphan entry for the given inode in case something goes
812  * wrong in the middle of an unlink/truncate.
813  */
814 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
815 {
816         struct btrfs_root *root = BTRFS_I(inode)->root;
817         int ret = 0;
818
819         spin_lock(&root->list_lock);
820
821         /* already on the orphan list, we're good */
822         if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
823                 spin_unlock(&root->list_lock);
824                 return 0;
825         }
826
827         list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
828
829         spin_unlock(&root->list_lock);
830
831         /*
832          * insert an orphan item to track this unlinked/truncated file
833          */
834         ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
835
836         return ret;
837 }
838
839 /*
840  * We have done the truncate/delete so we can go ahead and remove the orphan
841  * item for this particular inode.
842  */
843 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
844 {
845         struct btrfs_root *root = BTRFS_I(inode)->root;
846         int ret = 0;
847
848         spin_lock(&root->list_lock);
849
850         if (list_empty(&BTRFS_I(inode)->i_orphan)) {
851                 spin_unlock(&root->list_lock);
852                 return 0;
853         }
854
855         list_del_init(&BTRFS_I(inode)->i_orphan);
856         if (!trans) {
857                 spin_unlock(&root->list_lock);
858                 return 0;
859         }
860
861         spin_unlock(&root->list_lock);
862
863         ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
864
865         return ret;
866 }
867
868 /*
869  * this cleans up any orphans that may be left on the list from the last use
870  * of this root.
871  */
872 void btrfs_orphan_cleanup(struct btrfs_root *root)
873 {
874         struct btrfs_path *path;
875         struct extent_buffer *leaf;
876         struct btrfs_item *item;
877         struct btrfs_key key, found_key;
878         struct btrfs_trans_handle *trans;
879         struct inode *inode;
880         int ret = 0, nr_unlink = 0, nr_truncate = 0;
881
882         /* don't do orphan cleanup if the fs is readonly. */
883         if (root->inode->i_sb->s_flags & MS_RDONLY)
884                 return;
885
886         path = btrfs_alloc_path();
887         if (!path)
888                 return;
889         path->reada = -1;
890
891         key.objectid = BTRFS_ORPHAN_OBJECTID;
892         btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
893         key.offset = (u64)-1;
894
895         trans = btrfs_start_transaction(root, 1);
896         btrfs_set_trans_block_group(trans, root->inode);
897
898         while (1) {
899                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
900                 if (ret < 0) {
901                         printk(KERN_ERR "Error searching slot for orphan: %d"
902                                "\n", ret);
903                         break;
904                 }
905
906                 /*
907                  * if ret == 0 means we found what we were searching for, which
908                  * is weird, but possible, so only screw with path if we didnt
909                  * find the key and see if we have stuff that matches
910                  */
911                 if (ret > 0) {
912                         if (path->slots[0] == 0)
913                                 break;
914                         path->slots[0]--;
915                 }
916
917                 /* pull out the item */
918                 leaf = path->nodes[0];
919                 item = btrfs_item_nr(leaf, path->slots[0]);
920                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
921
922                 /* make sure the item matches what we want */
923                 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
924                         break;
925                 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
926                         break;
927
928                 /* release the path since we're done with it */
929                 btrfs_release_path(root, path);
930
931                 /*
932                  * this is where we are basically btrfs_lookup, without the
933                  * crossing root thing.  we store the inode number in the
934                  * offset of the orphan item.
935                  */
936                 inode = btrfs_iget_locked(root->inode->i_sb,
937                                           found_key.offset, root);
938                 if (!inode)
939                         break;
940
941                 if (inode->i_state & I_NEW) {
942                         BTRFS_I(inode)->root = root;
943
944                         /* have to set the location manually */
945                         BTRFS_I(inode)->location.objectid = inode->i_ino;
946                         BTRFS_I(inode)->location.type = BTRFS_INODE_ITEM_KEY;
947                         BTRFS_I(inode)->location.offset = 0;
948
949                         btrfs_read_locked_inode(inode);
950                         unlock_new_inode(inode);
951                 }
952
953                 /*
954                  * add this inode to the orphan list so btrfs_orphan_del does
955                  * the proper thing when we hit it
956                  */
957                 spin_lock(&root->list_lock);
958                 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
959                 spin_unlock(&root->list_lock);
960
961                 /*
962                  * if this is a bad inode, means we actually succeeded in
963                  * removing the inode, but not the orphan record, which means
964                  * we need to manually delete the orphan since iput will just
965                  * do a destroy_inode
966                  */
967                 if (is_bad_inode(inode)) {
968                         btrfs_orphan_del(trans, inode);
969                         iput(inode);
970                         continue;
971                 }
972
973                 /* if we have links, this was a truncate, lets do that */
974                 if (inode->i_nlink) {
975                         nr_truncate++;
976                         btrfs_truncate(inode);
977                 } else {
978                         nr_unlink++;
979                 }
980
981                 /* this will do delete_inode and everything for us */
982                 iput(inode);
983         }
984
985         if (nr_unlink)
986                 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
987         if (nr_truncate)
988                 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
989
990         btrfs_free_path(path);
991         btrfs_end_transaction(trans, root);
992 }
993
994 void btrfs_read_locked_inode(struct inode *inode)
995 {
996         struct btrfs_path *path;
997         struct extent_buffer *leaf;
998         struct btrfs_inode_item *inode_item;
999         struct btrfs_timespec *tspec;
1000         struct btrfs_root *root = BTRFS_I(inode)->root;
1001         struct btrfs_key location;
1002         u64 alloc_group_block;
1003         u32 rdev;
1004         int ret;
1005
1006         path = btrfs_alloc_path();
1007         BUG_ON(!path);
1008         memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
1009
1010         ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
1011         if (ret)
1012                 goto make_bad;
1013
1014         leaf = path->nodes[0];
1015         inode_item = btrfs_item_ptr(leaf, path->slots[0],
1016                                     struct btrfs_inode_item);
1017
1018         inode->i_mode = btrfs_inode_mode(leaf, inode_item);
1019         inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
1020         inode->i_uid = btrfs_inode_uid(leaf, inode_item);
1021         inode->i_gid = btrfs_inode_gid(leaf, inode_item);
1022         btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
1023
1024         tspec = btrfs_inode_atime(inode_item);
1025         inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1026         inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1027
1028         tspec = btrfs_inode_mtime(inode_item);
1029         inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1030         inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1031
1032         tspec = btrfs_inode_ctime(inode_item);
1033         inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
1034         inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
1035
1036         inode->i_blocks = btrfs_inode_nblocks(leaf, inode_item);
1037         BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
1038         inode->i_generation = BTRFS_I(inode)->generation;
1039         inode->i_rdev = 0;
1040         rdev = btrfs_inode_rdev(leaf, inode_item);
1041
1042         BTRFS_I(inode)->index_cnt = (u64)-1;
1043
1044         alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
1045         BTRFS_I(inode)->block_group = btrfs_lookup_block_group(root->fs_info,
1046                                                        alloc_group_block);
1047         BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
1048         if (!BTRFS_I(inode)->block_group) {
1049                 BTRFS_I(inode)->block_group = btrfs_find_block_group(root,
1050                                                  NULL, 0,
1051                                                  BTRFS_BLOCK_GROUP_METADATA, 0);
1052         }
1053         btrfs_free_path(path);
1054         inode_item = NULL;
1055
1056         switch (inode->i_mode & S_IFMT) {
1057         case S_IFREG:
1058                 inode->i_mapping->a_ops = &btrfs_aops;
1059                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1060                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
1061                 inode->i_fop = &btrfs_file_operations;
1062                 inode->i_op = &btrfs_file_inode_operations;
1063                 break;
1064         case S_IFDIR:
1065                 inode->i_fop = &btrfs_dir_file_operations;
1066                 if (root == root->fs_info->tree_root)
1067                         inode->i_op = &btrfs_dir_ro_inode_operations;
1068                 else
1069                         inode->i_op = &btrfs_dir_inode_operations;
1070                 break;
1071         case S_IFLNK:
1072                 inode->i_op = &btrfs_symlink_inode_operations;
1073                 inode->i_mapping->a_ops = &btrfs_symlink_aops;
1074                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
1075                 break;
1076         default:
1077                 init_special_inode(inode, inode->i_mode, rdev);
1078                 break;
1079         }
1080         return;
1081
1082 make_bad:
1083         btrfs_free_path(path);
1084         make_bad_inode(inode);
1085 }
1086
1087 static void fill_inode_item(struct btrfs_trans_handle *trans,
1088                             struct extent_buffer *leaf,
1089                             struct btrfs_inode_item *item,
1090                             struct inode *inode)
1091 {
1092         btrfs_set_inode_uid(leaf, item, inode->i_uid);
1093         btrfs_set_inode_gid(leaf, item, inode->i_gid);
1094         btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
1095         btrfs_set_inode_mode(leaf, item, inode->i_mode);
1096         btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
1097
1098         btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
1099                                inode->i_atime.tv_sec);
1100         btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
1101                                 inode->i_atime.tv_nsec);
1102
1103         btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
1104                                inode->i_mtime.tv_sec);
1105         btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
1106                                 inode->i_mtime.tv_nsec);
1107
1108         btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
1109                                inode->i_ctime.tv_sec);
1110         btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
1111                                 inode->i_ctime.tv_nsec);
1112
1113         btrfs_set_inode_nblocks(leaf, item, inode->i_blocks);
1114         btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
1115         btrfs_set_inode_transid(leaf, item, trans->transid);
1116         btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
1117         btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
1118         btrfs_set_inode_block_group(leaf, item,
1119                                     BTRFS_I(inode)->block_group->key.objectid);
1120 }
1121
1122 int noinline btrfs_update_inode(struct btrfs_trans_handle *trans,
1123                               struct btrfs_root *root,
1124                               struct inode *inode)
1125 {
1126         struct btrfs_inode_item *inode_item;
1127         struct btrfs_path *path;
1128         struct extent_buffer *leaf;
1129         int ret;
1130
1131         path = btrfs_alloc_path();
1132         BUG_ON(!path);
1133         ret = btrfs_lookup_inode(trans, root, path,
1134                                  &BTRFS_I(inode)->location, 1);
1135         if (ret) {
1136                 if (ret > 0)
1137                         ret = -ENOENT;
1138                 goto failed;
1139         }
1140
1141         leaf = path->nodes[0];
1142         inode_item = btrfs_item_ptr(leaf, path->slots[0],
1143                                   struct btrfs_inode_item);
1144
1145         fill_inode_item(trans, leaf, inode_item, inode);
1146         btrfs_mark_buffer_dirty(leaf);
1147         btrfs_set_inode_last_trans(trans, inode);
1148         ret = 0;
1149 failed:
1150         btrfs_free_path(path);
1151         return ret;
1152 }
1153
1154
1155 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
1156                        struct btrfs_root *root,
1157                        struct inode *dir, struct inode *inode,
1158                        const char *name, int name_len)
1159 {
1160         struct btrfs_path *path;
1161         int ret = 0;
1162         struct extent_buffer *leaf;
1163         struct btrfs_dir_item *di;
1164         struct btrfs_key key;
1165         u64 index;
1166
1167         path = btrfs_alloc_path();
1168         if (!path) {
1169                 ret = -ENOMEM;
1170                 goto err;
1171         }
1172
1173         di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
1174                                     name, name_len, -1);
1175         if (IS_ERR(di)) {
1176                 ret = PTR_ERR(di);
1177                 goto err;
1178         }
1179         if (!di) {
1180                 ret = -ENOENT;
1181                 goto err;
1182         }
1183         leaf = path->nodes[0];
1184         btrfs_dir_item_key_to_cpu(leaf, di, &key);
1185         ret = btrfs_delete_one_dir_name(trans, root, path, di);
1186         if (ret)
1187                 goto err;
1188         btrfs_release_path(root, path);
1189
1190         ret = btrfs_del_inode_ref(trans, root, name, name_len,
1191                                   inode->i_ino,
1192                                   dir->i_ino, &index);
1193         if (ret) {
1194                 printk("failed to delete reference to %.*s, "
1195                        "inode %lu parent %lu\n", name_len, name,
1196                        inode->i_ino, dir->i_ino);
1197                 goto err;
1198         }
1199
1200         di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
1201                                          index, name, name_len, -1);
1202         if (IS_ERR(di)) {
1203                 ret = PTR_ERR(di);
1204                 goto err;
1205         }
1206         if (!di) {
1207                 ret = -ENOENT;
1208                 goto err;
1209         }
1210         ret = btrfs_delete_one_dir_name(trans, root, path, di);
1211         btrfs_release_path(root, path);
1212
1213         ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
1214                                          inode, dir->i_ino);
1215         BUG_ON(ret != 0 && ret != -ENOENT);
1216         if (ret != -ENOENT)
1217                 BTRFS_I(dir)->log_dirty_trans = trans->transid;
1218
1219         ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
1220                                            dir, index);
1221         BUG_ON(ret);
1222 err:
1223         btrfs_free_path(path);
1224         if (ret)
1225                 goto out;
1226
1227         btrfs_i_size_write(dir, dir->i_size - name_len * 2);
1228         inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
1229         btrfs_update_inode(trans, root, dir);
1230         btrfs_drop_nlink(inode);
1231         ret = btrfs_update_inode(trans, root, inode);
1232         dir->i_sb->s_dirt = 1;
1233 out:
1234         return ret;
1235 }
1236
1237 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
1238 {
1239         struct btrfs_root *root;
1240         struct btrfs_trans_handle *trans;
1241         struct inode *inode = dentry->d_inode;
1242         int ret;
1243         unsigned long nr = 0;
1244
1245         root = BTRFS_I(dir)->root;
1246
1247         ret = btrfs_check_free_space(root, 1, 1);
1248         if (ret)
1249                 goto fail;
1250
1251         trans = btrfs_start_transaction(root, 1);
1252
1253         btrfs_set_trans_block_group(trans, dir);
1254         ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1255                                  dentry->d_name.name, dentry->d_name.len);
1256
1257         if (inode->i_nlink == 0)
1258                 ret = btrfs_orphan_add(trans, inode);
1259
1260         nr = trans->blocks_used;
1261
1262         btrfs_end_transaction_throttle(trans, root);
1263 fail:
1264         btrfs_btree_balance_dirty(root, nr);
1265         return ret;
1266 }
1267
1268 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
1269 {
1270         struct inode *inode = dentry->d_inode;
1271         int err = 0;
1272         int ret;
1273         struct btrfs_root *root = BTRFS_I(dir)->root;
1274         struct btrfs_trans_handle *trans;
1275         unsigned long nr = 0;
1276
1277         if (inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
1278                 return -ENOTEMPTY;
1279         }
1280
1281         ret = btrfs_check_free_space(root, 1, 1);
1282         if (ret)
1283                 goto fail;
1284
1285         trans = btrfs_start_transaction(root, 1);
1286         btrfs_set_trans_block_group(trans, dir);
1287
1288         err = btrfs_orphan_add(trans, inode);
1289         if (err)
1290                 goto fail_trans;
1291
1292         /* now the directory is empty */
1293         err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
1294                                  dentry->d_name.name, dentry->d_name.len);
1295         if (!err) {
1296                 btrfs_i_size_write(inode, 0);
1297         }
1298
1299 fail_trans:
1300         nr = trans->blocks_used;
1301         ret = btrfs_end_transaction_throttle(trans, root);
1302 fail:
1303         btrfs_btree_balance_dirty(root, nr);
1304
1305         if (ret && !err)
1306                 err = ret;
1307         return err;
1308 }
1309
1310 /*
1311  * this can truncate away extent items, csum items and directory items.
1312  * It starts at a high offset and removes keys until it can't find
1313  * any higher than i_size.
1314  *
1315  * csum items that cross the new i_size are truncated to the new size
1316  * as well.
1317  *
1318  * min_type is the minimum key type to truncate down to.  If set to 0, this
1319  * will kill all the items on this inode, including the INODE_ITEM_KEY.
1320  */
1321 noinline int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
1322                                         struct btrfs_root *root,
1323                                         struct inode *inode,
1324                                         u64 new_size, u32 min_type)
1325 {
1326         int ret;
1327         struct btrfs_path *path;
1328         struct btrfs_key key;
1329         struct btrfs_key found_key;
1330         u32 found_type;
1331         struct extent_buffer *leaf;
1332         struct btrfs_file_extent_item *fi;
1333         u64 extent_start = 0;
1334         u64 extent_num_bytes = 0;
1335         u64 item_end = 0;
1336         u64 root_gen = 0;
1337         u64 root_owner = 0;
1338         int found_extent;
1339         int del_item;
1340         int pending_del_nr = 0;
1341         int pending_del_slot = 0;
1342         int extent_type = -1;
1343         u64 mask = root->sectorsize - 1;
1344
1345         if (root->ref_cows)
1346                 btrfs_drop_extent_cache(inode,
1347                                         new_size & (~mask), (u64)-1);
1348         path = btrfs_alloc_path();
1349         path->reada = -1;
1350         BUG_ON(!path);
1351
1352         /* FIXME, add redo link to tree so we don't leak on crash */
1353         key.objectid = inode->i_ino;
1354         key.offset = (u64)-1;
1355         key.type = (u8)-1;
1356
1357         btrfs_init_path(path);
1358 search_again:
1359         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
1360         if (ret < 0) {
1361                 goto error;
1362         }
1363         if (ret > 0) {
1364                 /* there are no items in the tree for us to truncate, we're
1365                  * done
1366                  */
1367                 if (path->slots[0] == 0) {
1368                         ret = 0;
1369                         goto error;
1370                 }
1371                 path->slots[0]--;
1372         }
1373
1374         while(1) {
1375                 fi = NULL;
1376                 leaf = path->nodes[0];
1377                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1378                 found_type = btrfs_key_type(&found_key);
1379
1380                 if (found_key.objectid != inode->i_ino)
1381                         break;
1382
1383                 if (found_type < min_type)
1384                         break;
1385
1386                 item_end = found_key.offset;
1387                 if (found_type == BTRFS_EXTENT_DATA_KEY) {
1388                         fi = btrfs_item_ptr(leaf, path->slots[0],
1389                                             struct btrfs_file_extent_item);
1390                         extent_type = btrfs_file_extent_type(leaf, fi);
1391                         if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1392                                 item_end +=
1393                                     btrfs_file_extent_num_bytes(leaf, fi);
1394                         } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1395                                 struct btrfs_item *item = btrfs_item_nr(leaf,
1396                                                                 path->slots[0]);
1397                                 item_end += btrfs_file_extent_inline_len(leaf,
1398                                                                          item);
1399                         }
1400                         item_end--;
1401                 }
1402                 if (found_type == BTRFS_CSUM_ITEM_KEY) {
1403                         ret = btrfs_csum_truncate(trans, root, path,
1404                                                   new_size);
1405                         BUG_ON(ret);
1406                 }
1407                 if (item_end < new_size) {
1408                         if (found_type == BTRFS_DIR_ITEM_KEY) {
1409                                 found_type = BTRFS_INODE_ITEM_KEY;
1410                         } else if (found_type == BTRFS_EXTENT_ITEM_KEY) {
1411                                 found_type = BTRFS_CSUM_ITEM_KEY;
1412                         } else if (found_type == BTRFS_EXTENT_DATA_KEY) {
1413                                 found_type = BTRFS_XATTR_ITEM_KEY;
1414                         } else if (found_type == BTRFS_XATTR_ITEM_KEY) {
1415                                 found_type = BTRFS_INODE_REF_KEY;
1416                         } else if (found_type) {
1417                                 found_type--;
1418                         } else {
1419                                 break;
1420                         }
1421                         btrfs_set_key_type(&key, found_type);
1422                         goto next;
1423                 }
1424                 if (found_key.offset >= new_size)
1425                         del_item = 1;
1426                 else
1427                         del_item = 0;
1428                 found_extent = 0;
1429
1430                 /* FIXME, shrink the extent if the ref count is only 1 */
1431                 if (found_type != BTRFS_EXTENT_DATA_KEY)
1432                         goto delete;
1433
1434                 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
1435                         u64 num_dec;
1436                         extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
1437                         if (!del_item) {
1438                                 u64 orig_num_bytes =
1439                                         btrfs_file_extent_num_bytes(leaf, fi);
1440                                 extent_num_bytes = new_size -
1441                                         found_key.offset + root->sectorsize - 1;
1442                                 extent_num_bytes = extent_num_bytes &
1443                                         ~((u64)root->sectorsize - 1);
1444                                 btrfs_set_file_extent_num_bytes(leaf, fi,
1445                                                          extent_num_bytes);
1446                                 num_dec = (orig_num_bytes -
1447                                            extent_num_bytes);
1448                                 if (root->ref_cows && extent_start != 0)
1449                                         dec_i_blocks(inode, num_dec);
1450                                 btrfs_mark_buffer_dirty(leaf);
1451                         } else {
1452                                 extent_num_bytes =
1453                                         btrfs_file_extent_disk_num_bytes(leaf,
1454                                                                          fi);
1455                                 /* FIXME blocksize != 4096 */
1456                                 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
1457                                 if (extent_start != 0) {
1458                                         found_extent = 1;
1459                                         if (root->ref_cows)
1460                                                 dec_i_blocks(inode, num_dec);
1461                                 }
1462                                 root_gen = btrfs_header_generation(leaf);
1463                                 root_owner = btrfs_header_owner(leaf);
1464                         }
1465                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1466                         if (!del_item) {
1467                                 u32 size = new_size - found_key.offset;
1468
1469                                 if (root->ref_cows) {
1470                                         dec_i_blocks(inode, item_end + 1 -
1471                                                     found_key.offset - size);
1472                                 }
1473                                 size =
1474                                     btrfs_file_extent_calc_inline_size(size);
1475                                 ret = btrfs_truncate_item(trans, root, path,
1476                                                           size, 1);
1477                                 BUG_ON(ret);
1478                         } else if (root->ref_cows) {
1479                                 dec_i_blocks(inode, item_end + 1 -
1480                                              found_key.offset);
1481                         }
1482                 }
1483 delete:
1484                 if (del_item) {
1485                         if (!pending_del_nr) {
1486                                 /* no pending yet, add ourselves */
1487                                 pending_del_slot = path->slots[0];
1488                                 pending_del_nr = 1;
1489                         } else if (pending_del_nr &&
1490                                    path->slots[0] + 1 == pending_del_slot) {
1491                                 /* hop on the pending chunk */
1492                                 pending_del_nr++;
1493                                 pending_del_slot = path->slots[0];
1494                         } else {
1495                                 printk("bad pending slot %d pending_del_nr %d pending_del_slot %d\n", path->slots[0], pending_del_nr, pending_del_slot);
1496                         }
1497                 } else {
1498                         break;
1499                 }
1500                 if (found_extent) {
1501                         ret = btrfs_free_extent(trans, root, extent_start,
1502                                                 extent_num_bytes,
1503                                                 leaf->start, root_owner,
1504                                                 root_gen, inode->i_ino,
1505                                                 found_key.offset, 0);
1506                         BUG_ON(ret);
1507                 }
1508 next:
1509                 if (path->slots[0] == 0) {
1510                         if (pending_del_nr)
1511                                 goto del_pending;
1512                         btrfs_release_path(root, path);
1513                         goto search_again;
1514                 }
1515
1516                 path->slots[0]--;
1517                 if (pending_del_nr &&
1518                     path->slots[0] + 1 != pending_del_slot) {
1519                         struct btrfs_key debug;
1520 del_pending:
1521                         btrfs_item_key_to_cpu(path->nodes[0], &debug,
1522                                               pending_del_slot);
1523                         ret = btrfs_del_items(trans, root, path,
1524                                               pending_del_slot,
1525                                               pending_del_nr);
1526                         BUG_ON(ret);
1527                         pending_del_nr = 0;
1528                         btrfs_release_path(root, path);
1529                         goto search_again;
1530                 }
1531         }
1532         ret = 0;
1533 error:
1534         if (pending_del_nr) {
1535                 ret = btrfs_del_items(trans, root, path, pending_del_slot,
1536                                       pending_del_nr);
1537         }
1538         btrfs_free_path(path);
1539         inode->i_sb->s_dirt = 1;
1540         return ret;
1541 }
1542
1543 /*
1544  * taken from block_truncate_page, but does cow as it zeros out
1545  * any bytes left in the last page in the file.
1546  */
1547 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
1548 {
1549         struct inode *inode = mapping->host;
1550         struct btrfs_root *root = BTRFS_I(inode)->root;
1551         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1552         struct btrfs_ordered_extent *ordered;
1553         char *kaddr;
1554         u32 blocksize = root->sectorsize;
1555         pgoff_t index = from >> PAGE_CACHE_SHIFT;
1556         unsigned offset = from & (PAGE_CACHE_SIZE-1);
1557         struct page *page;
1558         int ret = 0;
1559         u64 page_start;
1560         u64 page_end;
1561
1562         if ((offset & (blocksize - 1)) == 0)
1563                 goto out;
1564
1565         ret = -ENOMEM;
1566 again:
1567         page = grab_cache_page(mapping, index);
1568         if (!page)
1569                 goto out;
1570
1571         page_start = page_offset(page);
1572         page_end = page_start + PAGE_CACHE_SIZE - 1;
1573
1574         if (!PageUptodate(page)) {
1575                 ret = btrfs_readpage(NULL, page);
1576                 lock_page(page);
1577                 if (page->mapping != mapping) {
1578                         unlock_page(page);
1579                         page_cache_release(page);
1580                         goto again;
1581                 }
1582                 if (!PageUptodate(page)) {
1583                         ret = -EIO;
1584                         goto out_unlock;
1585                 }
1586         }
1587         wait_on_page_writeback(page);
1588
1589         lock_extent(io_tree, page_start, page_end, GFP_NOFS);
1590         set_page_extent_mapped(page);
1591
1592         ordered = btrfs_lookup_ordered_extent(inode, page_start);
1593         if (ordered) {
1594                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1595                 unlock_page(page);
1596                 page_cache_release(page);
1597                 btrfs_start_ordered_extent(inode, ordered, 1);
1598                 btrfs_put_ordered_extent(ordered);
1599                 goto again;
1600         }
1601
1602         btrfs_set_extent_delalloc(inode, page_start, page_end);
1603         ret = 0;
1604         if (offset != PAGE_CACHE_SIZE) {
1605                 kaddr = kmap(page);
1606                 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
1607                 flush_dcache_page(page);
1608                 kunmap(page);
1609         }
1610         ClearPageChecked(page);
1611         set_page_dirty(page);
1612         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
1613
1614 out_unlock:
1615         unlock_page(page);
1616         page_cache_release(page);
1617 out:
1618         return ret;
1619 }
1620
1621 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
1622 {
1623         struct inode *inode = dentry->d_inode;
1624         int err;
1625
1626         err = inode_change_ok(inode, attr);
1627         if (err)
1628                 return err;
1629
1630         if (S_ISREG(inode->i_mode) &&
1631             attr->ia_valid & ATTR_SIZE && attr->ia_size > inode->i_size) {
1632                 struct btrfs_trans_handle *trans;
1633                 struct btrfs_root *root = BTRFS_I(inode)->root;
1634                 struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1635
1636                 u64 mask = root->sectorsize - 1;
1637                 u64 hole_start = (inode->i_size + mask) & ~mask;
1638                 u64 block_end = (attr->ia_size + mask) & ~mask;
1639                 u64 hole_size;
1640                 u64 alloc_hint = 0;
1641
1642                 if (attr->ia_size <= hole_start)
1643                         goto out;
1644
1645                 err = btrfs_check_free_space(root, 1, 0);
1646                 if (err)
1647                         goto fail;
1648
1649                 btrfs_truncate_page(inode->i_mapping, inode->i_size);
1650
1651                 hole_size = block_end - hole_start;
1652                 while(1) {
1653                         struct btrfs_ordered_extent *ordered;
1654                         btrfs_wait_ordered_range(inode, hole_start, hole_size);
1655
1656                         lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1657                         ordered = btrfs_lookup_ordered_extent(inode, hole_start);
1658                         if (ordered) {
1659                                 unlock_extent(io_tree, hole_start,
1660                                               block_end - 1, GFP_NOFS);
1661                                 btrfs_put_ordered_extent(ordered);
1662                         } else {
1663                                 break;
1664                         }
1665                 }
1666
1667                 trans = btrfs_start_transaction(root, 1);
1668                 btrfs_set_trans_block_group(trans, inode);
1669                 mutex_lock(&BTRFS_I(inode)->extent_mutex);
1670                 err = btrfs_drop_extents(trans, root, inode,
1671                                          hole_start, block_end, hole_start,
1672                                          &alloc_hint);
1673
1674                 if (alloc_hint != EXTENT_MAP_INLINE) {
1675                         err = btrfs_insert_file_extent(trans, root,
1676                                                        inode->i_ino,
1677                                                        hole_start, 0, 0,
1678                                                        hole_size, 0);
1679                         btrfs_drop_extent_cache(inode, hole_start,
1680                                                 (u64)-1);
1681                         btrfs_check_file(root, inode);
1682                 }
1683                 mutex_unlock(&BTRFS_I(inode)->extent_mutex);
1684                 btrfs_end_transaction(trans, root);
1685                 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
1686                 if (err)
1687                         return err;
1688         }
1689 out:
1690         err = inode_setattr(inode, attr);
1691
1692         if (!err && ((attr->ia_valid & ATTR_MODE)))
1693                 err = btrfs_acl_chmod(inode);
1694 fail:
1695         return err;
1696 }
1697
1698 void btrfs_delete_inode(struct inode *inode)
1699 {
1700         struct btrfs_trans_handle *trans;
1701         struct btrfs_root *root = BTRFS_I(inode)->root;
1702         unsigned long nr;
1703         int ret;
1704
1705         truncate_inode_pages(&inode->i_data, 0);
1706         if (is_bad_inode(inode)) {
1707                 btrfs_orphan_del(NULL, inode);
1708                 goto no_delete;
1709         }
1710         btrfs_wait_ordered_range(inode, 0, (u64)-1);
1711
1712         btrfs_i_size_write(inode, 0);
1713         trans = btrfs_start_transaction(root, 1);
1714
1715         btrfs_set_trans_block_group(trans, inode);
1716         ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size, 0);
1717         if (ret) {
1718                 btrfs_orphan_del(NULL, inode);
1719                 goto no_delete_lock;
1720         }
1721
1722         btrfs_orphan_del(trans, inode);
1723
1724         nr = trans->blocks_used;
1725         clear_inode(inode);
1726
1727         btrfs_end_transaction(trans, root);
1728         btrfs_btree_balance_dirty(root, nr);
1729         return;
1730
1731 no_delete_lock:
1732         nr = trans->blocks_used;
1733         btrfs_end_transaction(trans, root);
1734         btrfs_btree_balance_dirty(root, nr);
1735 no_delete:
1736         clear_inode(inode);
1737 }
1738
1739 /*
1740  * this returns the key found in the dir entry in the location pointer.
1741  * If no dir entries were found, location->objectid is 0.
1742  */
1743 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
1744                                struct btrfs_key *location)
1745 {
1746         const char *name = dentry->d_name.name;
1747         int namelen = dentry->d_name.len;
1748         struct btrfs_dir_item *di;
1749         struct btrfs_path *path;
1750         struct btrfs_root *root = BTRFS_I(dir)->root;
1751         int ret = 0;
1752
1753         path = btrfs_alloc_path();
1754         BUG_ON(!path);
1755
1756         di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
1757                                     namelen, 0);
1758         if (IS_ERR(di))
1759                 ret = PTR_ERR(di);
1760         if (!di || IS_ERR(di)) {
1761                 goto out_err;
1762         }
1763         btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
1764 out:
1765         btrfs_free_path(path);
1766         return ret;
1767 out_err:
1768         location->objectid = 0;
1769         goto out;
1770 }
1771
1772 /*
1773  * when we hit a tree root in a directory, the btrfs part of the inode
1774  * needs to be changed to reflect the root directory of the tree root.  This
1775  * is kind of like crossing a mount point.
1776  */
1777 static int fixup_tree_root_location(struct btrfs_root *root,
1778                              struct btrfs_key *location,
1779                              struct btrfs_root **sub_root,
1780                              struct dentry *dentry)
1781 {
1782         struct btrfs_root_item *ri;
1783
1784         if (btrfs_key_type(location) != BTRFS_ROOT_ITEM_KEY)
1785                 return 0;
1786         if (location->objectid == BTRFS_ROOT_TREE_OBJECTID)
1787                 return 0;
1788
1789         *sub_root = btrfs_read_fs_root(root->fs_info, location,
1790                                         dentry->d_name.name,
1791                                         dentry->d_name.len);
1792         if (IS_ERR(*sub_root))
1793                 return PTR_ERR(*sub_root);
1794
1795         ri = &(*sub_root)->root_item;
1796         location->objectid = btrfs_root_dirid(ri);
1797         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
1798         location->offset = 0;
1799
1800         return 0;
1801 }
1802
1803 static noinline void init_btrfs_i(struct inode *inode)
1804 {
1805         struct btrfs_inode *bi = BTRFS_I(inode);
1806
1807         bi->i_acl = NULL;
1808         bi->i_default_acl = NULL;
1809
1810         bi->generation = 0;
1811         bi->last_trans = 0;
1812         bi->logged_trans = 0;
1813         bi->delalloc_bytes = 0;
1814         bi->disk_i_size = 0;
1815         bi->flags = 0;
1816         bi->index_cnt = (u64)-1;
1817         bi->log_dirty_trans = 0;
1818         extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
1819         extent_io_tree_init(&BTRFS_I(inode)->io_tree,
1820                              inode->i_mapping, GFP_NOFS);
1821         extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
1822                              inode->i_mapping, GFP_NOFS);
1823         INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
1824         btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
1825         mutex_init(&BTRFS_I(inode)->csum_mutex);
1826         mutex_init(&BTRFS_I(inode)->extent_mutex);
1827         mutex_init(&BTRFS_I(inode)->log_mutex);
1828 }
1829
1830 static int btrfs_init_locked_inode(struct inode *inode, void *p)
1831 {
1832         struct btrfs_iget_args *args = p;
1833         inode->i_ino = args->ino;
1834         init_btrfs_i(inode);
1835         BTRFS_I(inode)->root = args->root;
1836         return 0;
1837 }
1838
1839 static int btrfs_find_actor(struct inode *inode, void *opaque)
1840 {
1841         struct btrfs_iget_args *args = opaque;
1842         return (args->ino == inode->i_ino &&
1843                 args->root == BTRFS_I(inode)->root);
1844 }
1845
1846 struct inode *btrfs_iget_locked(struct super_block *s, u64 objectid,
1847                                 struct btrfs_root *root)
1848 {
1849         struct inode *inode;
1850         struct btrfs_iget_args args;
1851         args.ino = objectid;
1852         args.root = root;
1853
1854         inode = iget5_locked(s, objectid, btrfs_find_actor,
1855                              btrfs_init_locked_inode,
1856                              (void *)&args);
1857         return inode;
1858 }
1859
1860 /* Get an inode object given its location and corresponding root.
1861  * Returns in *is_new if the inode was read from disk
1862  */
1863 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
1864                          struct btrfs_root *root, int *is_new)
1865 {
1866         struct inode *inode;
1867
1868         inode = btrfs_iget_locked(s, location->objectid, root);
1869         if (!inode)
1870                 return ERR_PTR(-EACCES);
1871
1872         if (inode->i_state & I_NEW) {
1873                 BTRFS_I(inode)->root = root;
1874                 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
1875                 btrfs_read_locked_inode(inode);
1876                 unlock_new_inode(inode);
1877                 if (is_new)
1878                         *is_new = 1;
1879         } else {
1880                 if (is_new)
1881                         *is_new = 0;
1882         }
1883
1884         return inode;
1885 }
1886
1887 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
1888                                    struct nameidata *nd)
1889 {
1890         struct inode * inode;
1891         struct btrfs_inode *bi = BTRFS_I(dir);
1892         struct btrfs_root *root = bi->root;
1893         struct btrfs_root *sub_root = root;
1894         struct btrfs_key location;
1895         int ret, new, do_orphan = 0;
1896
1897         if (dentry->d_name.len > BTRFS_NAME_LEN)
1898                 return ERR_PTR(-ENAMETOOLONG);
1899
1900         ret = btrfs_inode_by_name(dir, dentry, &location);
1901
1902         if (ret < 0)
1903                 return ERR_PTR(ret);
1904
1905         inode = NULL;
1906         if (location.objectid) {
1907                 ret = fixup_tree_root_location(root, &location, &sub_root,
1908                                                 dentry);
1909                 if (ret < 0)
1910                         return ERR_PTR(ret);
1911                 if (ret > 0)
1912                         return ERR_PTR(-ENOENT);
1913                 inode = btrfs_iget(dir->i_sb, &location, sub_root, &new);
1914                 if (IS_ERR(inode))
1915                         return ERR_CAST(inode);
1916
1917                 /* the inode and parent dir are two different roots */
1918                 if (new && root != sub_root) {
1919                         igrab(inode);
1920                         sub_root->inode = inode;
1921                         do_orphan = 1;
1922                 }
1923         }
1924
1925         if (unlikely(do_orphan))
1926                 btrfs_orphan_cleanup(sub_root);
1927
1928         return d_splice_alias(inode, dentry);
1929 }
1930
1931 static unsigned char btrfs_filetype_table[] = {
1932         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
1933 };
1934
1935 static int btrfs_real_readdir(struct file *filp, void *dirent,
1936                               filldir_t filldir)
1937 {
1938         struct inode *inode = filp->f_dentry->d_inode;
1939         struct btrfs_root *root = BTRFS_I(inode)->root;
1940         struct btrfs_item *item;
1941         struct btrfs_dir_item *di;
1942         struct btrfs_key key;
1943         struct btrfs_key found_key;
1944         struct btrfs_path *path;
1945         int ret;
1946         u32 nritems;
1947         struct extent_buffer *leaf;
1948         int slot;
1949         int advance;
1950         unsigned char d_type;
1951         int over = 0;
1952         u32 di_cur;
1953         u32 di_total;
1954         u32 di_len;
1955         int key_type = BTRFS_DIR_INDEX_KEY;
1956         char tmp_name[32];
1957         char *name_ptr;
1958         int name_len;
1959
1960         /* FIXME, use a real flag for deciding about the key type */
1961         if (root->fs_info->tree_root == root)
1962                 key_type = BTRFS_DIR_ITEM_KEY;
1963
1964         /* special case for "." */
1965         if (filp->f_pos == 0) {
1966                 over = filldir(dirent, ".", 1,
1967                                1, inode->i_ino,
1968                                DT_DIR);
1969                 if (over)
1970                         return 0;
1971                 filp->f_pos = 1;
1972         }
1973         /* special case for .., just use the back ref */
1974         if (filp->f_pos == 1) {
1975                 u64 pino = parent_ino(filp->f_path.dentry);
1976                 over = filldir(dirent, "..", 2,
1977                                2, pino, DT_DIR);
1978                 if (over)
1979                         return 0;
1980                 filp->f_pos = 2;
1981         }
1982
1983         path = btrfs_alloc_path();
1984         path->reada = 2;
1985
1986         btrfs_set_key_type(&key, key_type);
1987         key.offset = filp->f_pos;
1988         key.objectid = inode->i_ino;
1989
1990         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
1991         if (ret < 0)
1992                 goto err;
1993         advance = 0;
1994
1995         while (1) {
1996                 leaf = path->nodes[0];
1997                 nritems = btrfs_header_nritems(leaf);
1998                 slot = path->slots[0];
1999                 if (advance || slot >= nritems) {
2000                         if (slot >= nritems - 1) {
2001                                 ret = btrfs_next_leaf(root, path);
2002                                 if (ret)
2003                                         break;
2004                                 leaf = path->nodes[0];
2005                                 nritems = btrfs_header_nritems(leaf);
2006                                 slot = path->slots[0];
2007                         } else {
2008                                 slot++;
2009                                 path->slots[0]++;
2010                         }
2011                 }
2012                 advance = 1;
2013                 item = btrfs_item_nr(leaf, slot);
2014                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2015
2016                 if (found_key.objectid != key.objectid)
2017                         break;
2018                 if (btrfs_key_type(&found_key) != key_type)
2019                         break;
2020                 if (found_key.offset < filp->f_pos)
2021                         continue;
2022
2023                 filp->f_pos = found_key.offset;
2024
2025                 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
2026                 di_cur = 0;
2027                 di_total = btrfs_item_size(leaf, item);
2028
2029                 while (di_cur < di_total) {
2030                         struct btrfs_key location;
2031
2032                         name_len = btrfs_dir_name_len(leaf, di);
2033                         if (name_len <= sizeof(tmp_name)) {
2034                                 name_ptr = tmp_name;
2035                         } else {
2036                                 name_ptr = kmalloc(name_len, GFP_NOFS);
2037                                 if (!name_ptr) {
2038                                         ret = -ENOMEM;
2039                                         goto err;
2040                                 }
2041                         }
2042                         read_extent_buffer(leaf, name_ptr,
2043                                            (unsigned long)(di + 1), name_len);
2044
2045                         d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
2046                         btrfs_dir_item_key_to_cpu(leaf, di, &location);
2047                         over = filldir(dirent, name_ptr, name_len,
2048                                        found_key.offset, location.objectid,
2049                                        d_type);
2050
2051                         if (name_ptr != tmp_name)
2052                                 kfree(name_ptr);
2053
2054                         if (over)
2055                                 goto nopos;
2056
2057                         di_len = btrfs_dir_name_len(leaf, di) +
2058                                  btrfs_dir_data_len(leaf, di) + sizeof(*di);
2059                         di_cur += di_len;
2060                         di = (struct btrfs_dir_item *)((char *)di + di_len);
2061                 }
2062         }
2063
2064         /* Reached end of directory/root. Bump pos past the last item. */
2065         if (key_type == BTRFS_DIR_INDEX_KEY)
2066                 filp->f_pos = INT_LIMIT(typeof(filp->f_pos));
2067         else
2068                 filp->f_pos++;
2069 nopos:
2070         ret = 0;
2071 err:
2072         btrfs_free_path(path);
2073         return ret;
2074 }
2075
2076 /* Kernels earlier than 2.6.28 still have the NFS deadlock where nfsd
2077    will call the file system's ->lookup() method from within its
2078    filldir callback, which in turn was called from the file system's
2079    ->readdir() method. And will deadlock for many file systems. */
2080 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
2081
2082 struct nfshack_dirent {
2083         u64             ino;
2084         loff_t          offset;
2085         int             namlen;
2086         unsigned int    d_type;
2087         char            name[];
2088 };
2089
2090 struct nfshack_readdir {
2091         char            *dirent;
2092         size_t          used;
2093         int             full;
2094 };
2095
2096
2097
2098 static int btrfs_nfshack_filldir(void *__buf, const char *name, int namlen,
2099                               loff_t offset, u64 ino, unsigned int d_type)
2100 {
2101         struct nfshack_readdir *buf = __buf;
2102         struct nfshack_dirent *de = (void *)(buf->dirent + buf->used);
2103         unsigned int reclen;
2104
2105         reclen = ALIGN(sizeof(struct nfshack_dirent) + namlen, sizeof(u64));
2106         if (buf->used + reclen > PAGE_SIZE) {
2107                 buf->full = 1;
2108                 return -EINVAL;
2109         }
2110
2111         de->namlen = namlen;
2112         de->offset = offset;
2113         de->ino = ino;
2114         de->d_type = d_type;
2115         memcpy(de->name, name, namlen);
2116         buf->used += reclen;
2117
2118         return 0;
2119 }
2120
2121 static int btrfs_nfshack_readdir(struct file *file, void *dirent,
2122                                  filldir_t filldir)
2123 {
2124         struct nfshack_readdir buf;
2125         struct nfshack_dirent *de;
2126         int err;
2127         int size;
2128         loff_t offset;
2129
2130         buf.dirent = (void *)__get_free_page(GFP_KERNEL);
2131         if (!buf.dirent)
2132                 return -ENOMEM;
2133
2134         offset = file->f_pos;
2135
2136         do {
2137                 unsigned int reclen;
2138
2139                 buf.used = 0;
2140                 buf.full = 0;
2141                 err = btrfs_real_readdir(file, &buf, btrfs_nfshack_filldir);
2142                 if (err)
2143                         break;
2144
2145                 size = buf.used;
2146
2147                 if (!size)
2148                         break;
2149
2150                 de = (struct nfshack_dirent *)buf.dirent;
2151                 while (size > 0) {
2152                         offset = de->offset;
2153
2154                         if (filldir(dirent, de->name, de->namlen, de->offset,
2155                                     de->ino, de->d_type))
2156                                 goto done;
2157                         offset = file->f_pos;
2158
2159                         reclen = ALIGN(sizeof(*de) + de->namlen,
2160                                        sizeof(u64));
2161                         size -= reclen;
2162                         de = (struct nfshack_dirent *)((char *)de + reclen);
2163                 }
2164         } while (buf.full);
2165
2166  done:
2167         free_page((unsigned long)buf.dirent);
2168         file->f_pos = offset;
2169
2170         return err;
2171 }
2172 #endif
2173
2174 int btrfs_write_inode(struct inode *inode, int wait)
2175 {
2176         struct btrfs_root *root = BTRFS_I(inode)->root;
2177         struct btrfs_trans_handle *trans;
2178         int ret = 0;
2179
2180         if (root->fs_info->closing > 1)
2181                 return 0;
2182
2183         if (wait) {
2184                 trans = btrfs_join_transaction(root, 1);
2185                 btrfs_set_trans_block_group(trans, inode);
2186                 ret = btrfs_commit_transaction(trans, root);
2187         }
2188         return ret;
2189 }
2190
2191 /*
2192  * This is somewhat expensive, updating the tree every time the
2193  * inode changes.  But, it is most likely to find the inode in cache.
2194  * FIXME, needs more benchmarking...there are no reasons other than performance
2195  * to keep or drop this code.
2196  */
2197 void btrfs_dirty_inode(struct inode *inode)
2198 {
2199         struct btrfs_root *root = BTRFS_I(inode)->root;
2200         struct btrfs_trans_handle *trans;
2201
2202         trans = btrfs_join_transaction(root, 1);
2203         btrfs_set_trans_block_group(trans, inode);
2204         btrfs_update_inode(trans, root, inode);
2205         btrfs_end_transaction(trans, root);
2206 }
2207
2208 static int btrfs_set_inode_index_count(struct inode *inode)
2209 {
2210         struct btrfs_root *root = BTRFS_I(inode)->root;
2211         struct btrfs_key key, found_key;
2212         struct btrfs_path *path;
2213         struct extent_buffer *leaf;
2214         int ret;
2215
2216         key.objectid = inode->i_ino;
2217         btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
2218         key.offset = (u64)-1;
2219
2220         path = btrfs_alloc_path();
2221         if (!path)
2222                 return -ENOMEM;
2223
2224         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2225         if (ret < 0)
2226                 goto out;
2227         /* FIXME: we should be able to handle this */
2228         if (ret == 0)
2229                 goto out;
2230         ret = 0;
2231
2232         /*
2233          * MAGIC NUMBER EXPLANATION:
2234          * since we search a directory based on f_pos we have to start at 2
2235          * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
2236          * else has to start at 2
2237          */
2238         if (path->slots[0] == 0) {
2239                 BTRFS_I(inode)->index_cnt = 2;
2240                 goto out;
2241         }
2242
2243         path->slots[0]--;
2244
2245         leaf = path->nodes[0];
2246         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2247
2248         if (found_key.objectid != inode->i_ino ||
2249             btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
2250                 BTRFS_I(inode)->index_cnt = 2;
2251                 goto out;
2252         }
2253
2254         BTRFS_I(inode)->index_cnt = found_key.offset + 1;
2255 out:
2256         btrfs_free_path(path);
2257         return ret;
2258 }
2259
2260 static int btrfs_set_inode_index(struct inode *dir, struct inode *inode,
2261                                  u64 *index)
2262 {
2263         int ret = 0;
2264
2265         if (BTRFS_I(dir)->index_cnt == (u64)-1) {
2266                 ret = btrfs_set_inode_index_count(dir);
2267                 if (ret) {
2268                         return ret;
2269                 }
2270         }
2271
2272         *index = BTRFS_I(dir)->index_cnt;
2273         BTRFS_I(dir)->index_cnt++;
2274
2275         return ret;
2276 }
2277
2278 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
2279                                      struct btrfs_root *root,
2280                                      struct inode *dir,
2281                                      const char *name, int name_len,
2282                                      u64 ref_objectid,
2283                                      u64 objectid,
2284                                      struct btrfs_block_group_cache *group,
2285                                      int mode, u64 *index)
2286 {
2287         struct inode *inode;
2288         struct btrfs_inode_item *inode_item;
2289         struct btrfs_block_group_cache *new_inode_group;
2290         struct btrfs_key *location;
2291         struct btrfs_path *path;
2292         struct btrfs_inode_ref *ref;
2293         struct btrfs_key key[2];
2294         u32 sizes[2];
2295         unsigned long ptr;
2296         int ret;
2297         int owner;
2298
2299         path = btrfs_alloc_path();
2300         BUG_ON(!path);
2301
2302         inode = new_inode(root->fs_info->sb);
2303         if (!inode)
2304                 return ERR_PTR(-ENOMEM);
2305
2306         if (dir) {
2307                 ret = btrfs_set_inode_index(dir, inode, index);
2308                 if (ret)
2309                         return ERR_PTR(ret);
2310         }
2311         /*
2312          * index_cnt is ignored for everything but a dir,
2313          * btrfs_get_inode_index_count has an explanation for the magic
2314          * number
2315          */
2316         init_btrfs_i(inode);
2317         BTRFS_I(inode)->index_cnt = 2;
2318         BTRFS_I(inode)->root = root;
2319         BTRFS_I(inode)->generation = trans->transid;
2320
2321         if (mode & S_IFDIR)
2322                 owner = 0;
2323         else
2324                 owner = 1;
2325         new_inode_group = btrfs_find_block_group(root, group, 0,
2326                                        BTRFS_BLOCK_GROUP_METADATA, owner);
2327         if (!new_inode_group) {
2328                 printk("find_block group failed\n");
2329                 new_inode_group = group;
2330         }
2331         BTRFS_I(inode)->block_group = new_inode_group;
2332
2333         key[0].objectid = objectid;
2334         btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
2335         key[0].offset = 0;
2336
2337         key[1].objectid = objectid;
2338         btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
2339         key[1].offset = ref_objectid;
2340
2341         sizes[0] = sizeof(struct btrfs_inode_item);
2342         sizes[1] = name_len + sizeof(*ref);
2343
2344         ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
2345         if (ret != 0)
2346                 goto fail;
2347
2348         if (objectid > root->highest_inode)
2349                 root->highest_inode = objectid;
2350
2351         inode->i_uid = current->fsuid;
2352         inode->i_gid = current->fsgid;
2353         inode->i_mode = mode;
2354         inode->i_ino = objectid;
2355         inode->i_blocks = 0;
2356         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
2357         inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
2358                                   struct btrfs_inode_item);
2359         fill_inode_item(trans, path->nodes[0], inode_item, inode);
2360
2361         ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
2362                              struct btrfs_inode_ref);
2363         btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
2364         btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
2365         ptr = (unsigned long)(ref + 1);
2366         write_extent_buffer(path->nodes[0], name, ptr, name_len);
2367
2368         btrfs_mark_buffer_dirty(path->nodes[0]);
2369         btrfs_free_path(path);
2370
2371         location = &BTRFS_I(inode)->location;
2372         location->objectid = objectid;
2373         location->offset = 0;
2374         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
2375
2376         insert_inode_hash(inode);
2377         return inode;
2378 fail:
2379         if (dir)
2380                 BTRFS_I(dir)->index_cnt--;
2381         btrfs_free_path(path);
2382         return ERR_PTR(ret);
2383 }
2384
2385 static inline u8 btrfs_inode_type(struct inode *inode)
2386 {
2387         return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
2388 }
2389
2390 int btrfs_add_link(struct btrfs_trans_handle *trans,
2391                    struct inode *parent_inode, struct inode *inode,
2392                    const char *name, int name_len, int add_backref, u64 index)
2393 {
2394         int ret;
2395         struct btrfs_key key;
2396         struct btrfs_root *root = BTRFS_I(parent_inode)->root;
2397
2398         key.objectid = inode->i_ino;
2399         btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
2400         key.offset = 0;
2401
2402         ret = btrfs_insert_dir_item(trans, root, name, name_len,
2403                                     parent_inode->i_ino,
2404                                     &key, btrfs_inode_type(inode),
2405                                     index);
2406         if (ret == 0) {
2407                 if (add_backref) {
2408                         ret = btrfs_insert_inode_ref(trans, root,
2409                                                      name, name_len,
2410                                                      inode->i_ino,
2411                                                      parent_inode->i_ino,
2412                                                      index);
2413                 }
2414                 btrfs_i_size_write(parent_inode, parent_inode->i_size +
2415                                    name_len * 2);
2416                 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
2417                 ret = btrfs_update_inode(trans, root, parent_inode);
2418         }
2419         return ret;
2420 }
2421
2422 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
2423                             struct dentry *dentry, struct inode *inode,
2424                             int backref, u64 index)
2425 {
2426         int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2427                                  inode, dentry->d_name.name,
2428                                  dentry->d_name.len, backref, index);
2429         if (!err) {
2430                 d_instantiate(dentry, inode);
2431                 return 0;
2432         }
2433         if (err > 0)
2434                 err = -EEXIST;
2435         return err;
2436 }
2437
2438 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
2439                         int mode, dev_t rdev)
2440 {
2441         struct btrfs_trans_handle *trans;
2442         struct btrfs_root *root = BTRFS_I(dir)->root;
2443         struct inode *inode = NULL;
2444         int err;
2445         int drop_inode = 0;
2446         u64 objectid;
2447         unsigned long nr = 0;
2448         u64 index = 0;
2449
2450         if (!new_valid_dev(rdev))
2451                 return -EINVAL;
2452
2453         err = btrfs_check_free_space(root, 1, 0);
2454         if (err)
2455                 goto fail;
2456
2457         trans = btrfs_start_transaction(root, 1);
2458         btrfs_set_trans_block_group(trans, dir);
2459
2460         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2461         if (err) {
2462                 err = -ENOSPC;
2463                 goto out_unlock;
2464         }
2465
2466         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2467                                 dentry->d_name.len,
2468                                 dentry->d_parent->d_inode->i_ino, objectid,
2469                                 BTRFS_I(dir)->block_group, mode, &index);
2470         err = PTR_ERR(inode);
2471         if (IS_ERR(inode))
2472                 goto out_unlock;
2473
2474         err = btrfs_init_acl(inode, dir);
2475         if (err) {
2476                 drop_inode = 1;
2477                 goto out_unlock;
2478         }
2479
2480         btrfs_set_trans_block_group(trans, inode);
2481         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2482         if (err)
2483                 drop_inode = 1;
2484         else {
2485                 inode->i_op = &btrfs_special_inode_operations;
2486                 init_special_inode(inode, inode->i_mode, rdev);
2487                 btrfs_update_inode(trans, root, inode);
2488         }
2489         dir->i_sb->s_dirt = 1;
2490         btrfs_update_inode_block_group(trans, inode);
2491         btrfs_update_inode_block_group(trans, dir);
2492 out_unlock:
2493         nr = trans->blocks_used;
2494         btrfs_end_transaction_throttle(trans, root);
2495 fail:
2496         if (drop_inode) {
2497                 inode_dec_link_count(inode);
2498                 iput(inode);
2499         }
2500         btrfs_btree_balance_dirty(root, nr);
2501         return err;
2502 }
2503
2504 static int btrfs_create(struct inode *dir, struct dentry *dentry,
2505                         int mode, struct nameidata *nd)
2506 {
2507         struct btrfs_trans_handle *trans;
2508         struct btrfs_root *root = BTRFS_I(dir)->root;
2509         struct inode *inode = NULL;
2510         int err;
2511         int drop_inode = 0;
2512         unsigned long nr = 0;
2513         u64 objectid;
2514         u64 index = 0;
2515
2516         err = btrfs_check_free_space(root, 1, 0);
2517         if (err)
2518                 goto fail;
2519         trans = btrfs_start_transaction(root, 1);
2520         btrfs_set_trans_block_group(trans, dir);
2521
2522         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2523         if (err) {
2524                 err = -ENOSPC;
2525                 goto out_unlock;
2526         }
2527
2528         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2529                                 dentry->d_name.len,
2530                                 dentry->d_parent->d_inode->i_ino,
2531                                 objectid, BTRFS_I(dir)->block_group, mode,
2532                                 &index);
2533         err = PTR_ERR(inode);
2534         if (IS_ERR(inode))
2535                 goto out_unlock;
2536
2537         err = btrfs_init_acl(inode, dir);
2538         if (err) {
2539                 drop_inode = 1;
2540                 goto out_unlock;
2541         }
2542
2543         btrfs_set_trans_block_group(trans, inode);
2544         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
2545         if (err)
2546                 drop_inode = 1;
2547         else {
2548                 inode->i_mapping->a_ops = &btrfs_aops;
2549                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2550                 inode->i_fop = &btrfs_file_operations;
2551                 inode->i_op = &btrfs_file_inode_operations;
2552                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2553         }
2554         dir->i_sb->s_dirt = 1;
2555         btrfs_update_inode_block_group(trans, inode);
2556         btrfs_update_inode_block_group(trans, dir);
2557 out_unlock:
2558         nr = trans->blocks_used;
2559         btrfs_end_transaction_throttle(trans, root);
2560 fail:
2561         if (drop_inode) {
2562                 inode_dec_link_count(inode);
2563                 iput(inode);
2564         }
2565         btrfs_btree_balance_dirty(root, nr);
2566         return err;
2567 }
2568
2569 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
2570                       struct dentry *dentry)
2571 {
2572         struct btrfs_trans_handle *trans;
2573         struct btrfs_root *root = BTRFS_I(dir)->root;
2574         struct inode *inode = old_dentry->d_inode;
2575         u64 index;
2576         unsigned long nr = 0;
2577         int err;
2578         int drop_inode = 0;
2579
2580         if (inode->i_nlink == 0)
2581                 return -ENOENT;
2582
2583         btrfs_inc_nlink(inode);
2584         err = btrfs_check_free_space(root, 1, 0);
2585         if (err)
2586                 goto fail;
2587         err = btrfs_set_inode_index(dir, inode, &index);
2588         if (err)
2589                 goto fail;
2590
2591         trans = btrfs_start_transaction(root, 1);
2592
2593         btrfs_set_trans_block_group(trans, dir);
2594         atomic_inc(&inode->i_count);
2595
2596         err = btrfs_add_nondir(trans, dentry, inode, 1, index);
2597
2598         if (err)
2599                 drop_inode = 1;
2600
2601         dir->i_sb->s_dirt = 1;
2602         btrfs_update_inode_block_group(trans, dir);
2603         err = btrfs_update_inode(trans, root, inode);
2604
2605         if (err)
2606                 drop_inode = 1;
2607
2608         nr = trans->blocks_used;
2609         btrfs_end_transaction_throttle(trans, root);
2610 fail:
2611         if (drop_inode) {
2612                 inode_dec_link_count(inode);
2613                 iput(inode);
2614         }
2615         btrfs_btree_balance_dirty(root, nr);
2616         return err;
2617 }
2618
2619 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
2620 {
2621         struct inode *inode = NULL;
2622         struct btrfs_trans_handle *trans;
2623         struct btrfs_root *root = BTRFS_I(dir)->root;
2624         int err = 0;
2625         int drop_on_err = 0;
2626         u64 objectid = 0;
2627         u64 index = 0;
2628         unsigned long nr = 1;
2629
2630         err = btrfs_check_free_space(root, 1, 0);
2631         if (err)
2632                 goto out_unlock;
2633
2634         trans = btrfs_start_transaction(root, 1);
2635         btrfs_set_trans_block_group(trans, dir);
2636
2637         if (IS_ERR(trans)) {
2638                 err = PTR_ERR(trans);
2639                 goto out_unlock;
2640         }
2641
2642         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
2643         if (err) {
2644                 err = -ENOSPC;
2645                 goto out_unlock;
2646         }
2647
2648         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
2649                                 dentry->d_name.len,
2650                                 dentry->d_parent->d_inode->i_ino, objectid,
2651                                 BTRFS_I(dir)->block_group, S_IFDIR | mode,
2652                                 &index);
2653         if (IS_ERR(inode)) {
2654                 err = PTR_ERR(inode);
2655                 goto out_fail;
2656         }
2657
2658         drop_on_err = 1;
2659
2660         err = btrfs_init_acl(inode, dir);
2661         if (err)
2662                 goto out_fail;
2663
2664         inode->i_op = &btrfs_dir_inode_operations;
2665         inode->i_fop = &btrfs_dir_file_operations;
2666         btrfs_set_trans_block_group(trans, inode);
2667
2668         btrfs_i_size_write(inode, 0);
2669         err = btrfs_update_inode(trans, root, inode);
2670         if (err)
2671                 goto out_fail;
2672
2673         err = btrfs_add_link(trans, dentry->d_parent->d_inode,
2674                                  inode, dentry->d_name.name,
2675                                  dentry->d_name.len, 0, index);
2676         if (err)
2677                 goto out_fail;
2678
2679         d_instantiate(dentry, inode);
2680         drop_on_err = 0;
2681         dir->i_sb->s_dirt = 1;
2682         btrfs_update_inode_block_group(trans, inode);
2683         btrfs_update_inode_block_group(trans, dir);
2684
2685 out_fail:
2686         nr = trans->blocks_used;
2687         btrfs_end_transaction_throttle(trans, root);
2688
2689 out_unlock:
2690         if (drop_on_err)
2691                 iput(inode);
2692         btrfs_btree_balance_dirty(root, nr);
2693         return err;
2694 }
2695
2696 static int merge_extent_mapping(struct extent_map_tree *em_tree,
2697                                 struct extent_map *existing,
2698                                 struct extent_map *em,
2699                                 u64 map_start, u64 map_len)
2700 {
2701         u64 start_diff;
2702
2703         BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
2704         start_diff = map_start - em->start;
2705         em->start = map_start;
2706         em->len = map_len;
2707         if (em->block_start < EXTENT_MAP_LAST_BYTE)
2708                 em->block_start += start_diff;
2709         return add_extent_mapping(em_tree, em);
2710 }
2711
2712 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
2713                                     size_t pg_offset, u64 start, u64 len,
2714                                     int create)
2715 {
2716         int ret;
2717         int err = 0;
2718         u64 bytenr;
2719         u64 extent_start = 0;
2720         u64 extent_end = 0;
2721         u64 objectid = inode->i_ino;
2722         u32 found_type;
2723         struct btrfs_path *path = NULL;
2724         struct btrfs_root *root = BTRFS_I(inode)->root;
2725         struct btrfs_file_extent_item *item;
2726         struct extent_buffer *leaf;
2727         struct btrfs_key found_key;
2728         struct extent_map *em = NULL;
2729         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
2730         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2731         struct btrfs_trans_handle *trans = NULL;
2732
2733 again:
2734         spin_lock(&em_tree->lock);
2735         em = lookup_extent_mapping(em_tree, start, len);
2736         if (em)
2737                 em->bdev = root->fs_info->fs_devices->latest_bdev;
2738         spin_unlock(&em_tree->lock);
2739
2740         if (em) {
2741                 if (em->start > start || em->start + em->len <= start)
2742                         free_extent_map(em);
2743                 else if (em->block_start == EXTENT_MAP_INLINE && page)
2744                         free_extent_map(em);
2745                 else
2746                         goto out;
2747         }
2748         em = alloc_extent_map(GFP_NOFS);
2749         if (!em) {
2750                 err = -ENOMEM;
2751                 goto out;
2752         }
2753         em->bdev = root->fs_info->fs_devices->latest_bdev;
2754         em->start = EXTENT_MAP_HOLE;
2755         em->len = (u64)-1;
2756
2757         if (!path) {
2758                 path = btrfs_alloc_path();
2759                 BUG_ON(!path);
2760         }
2761
2762         ret = btrfs_lookup_file_extent(trans, root, path,
2763                                        objectid, start, trans != NULL);
2764         if (ret < 0) {
2765                 err = ret;
2766                 goto out;
2767         }
2768
2769         if (ret != 0) {
2770                 if (path->slots[0] == 0)
2771                         goto not_found;
2772                 path->slots[0]--;
2773         }
2774
2775         leaf = path->nodes[0];
2776         item = btrfs_item_ptr(leaf, path->slots[0],
2777                               struct btrfs_file_extent_item);
2778         /* are we inside the extent that was found? */
2779         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2780         found_type = btrfs_key_type(&found_key);
2781         if (found_key.objectid != objectid ||
2782             found_type != BTRFS_EXTENT_DATA_KEY) {
2783                 goto not_found;
2784         }
2785
2786         found_type = btrfs_file_extent_type(leaf, item);
2787         extent_start = found_key.offset;
2788         if (found_type == BTRFS_FILE_EXTENT_REG) {
2789                 extent_end = extent_start +
2790                        btrfs_file_extent_num_bytes(leaf, item);
2791                 err = 0;
2792                 if (start < extent_start || start >= extent_end) {
2793                         em->start = start;
2794                         if (start < extent_start) {
2795                                 if (start + len <= extent_start)
2796                                         goto not_found;
2797                                 em->len = extent_end - extent_start;
2798                         } else {
2799                                 em->len = len;
2800                         }
2801                         goto not_found_em;
2802                 }
2803                 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
2804                 if (bytenr == 0) {
2805                         em->start = extent_start;
2806                         em->len = extent_end - extent_start;
2807                         em->block_start = EXTENT_MAP_HOLE;
2808                         goto insert;
2809                 }
2810                 bytenr += btrfs_file_extent_offset(leaf, item);
2811                 em->block_start = bytenr;
2812                 em->start = extent_start;
2813                 em->len = extent_end - extent_start;
2814                 goto insert;
2815         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
2816                 u64 page_start;
2817                 unsigned long ptr;
2818                 char *map;
2819                 size_t size;
2820                 size_t extent_offset;
2821                 size_t copy_size;
2822
2823                 size = btrfs_file_extent_inline_len(leaf, btrfs_item_nr(leaf,
2824                                                     path->slots[0]));
2825                 extent_end = (extent_start + size + root->sectorsize - 1) &
2826                         ~((u64)root->sectorsize - 1);
2827                 if (start < extent_start || start >= extent_end) {
2828                         em->start = start;
2829                         if (start < extent_start) {
2830                                 if (start + len <= extent_start)
2831                                         goto not_found;
2832                                 em->len = extent_end - extent_start;
2833                         } else {
2834                                 em->len = len;
2835                         }
2836                         goto not_found_em;
2837                 }
2838                 em->block_start = EXTENT_MAP_INLINE;
2839
2840                 if (!page) {
2841                         em->start = extent_start;
2842                         em->len = size;
2843                         goto out;
2844                 }
2845
2846                 page_start = page_offset(page) + pg_offset;
2847                 extent_offset = page_start - extent_start;
2848                 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
2849                                 size - extent_offset);
2850                 em->start = extent_start + extent_offset;
2851                 em->len = (copy_size + root->sectorsize - 1) &
2852                         ~((u64)root->sectorsize - 1);
2853                 map = kmap(page);
2854                 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
2855                 if (create == 0 && !PageUptodate(page)) {
2856                         read_extent_buffer(leaf, map + pg_offset, ptr,
2857                                            copy_size);
2858                         flush_dcache_page(page);
2859                 } else if (create && PageUptodate(page)) {
2860                         if (!trans) {
2861                                 kunmap(page);
2862                                 free_extent_map(em);
2863                                 em = NULL;
2864                                 btrfs_release_path(root, path);
2865                                 trans = btrfs_join_transaction(root, 1);
2866                                 goto again;
2867                         }
2868                         write_extent_buffer(leaf, map + pg_offset, ptr,
2869                                             copy_size);
2870                         btrfs_mark_buffer_dirty(leaf);
2871                 }
2872                 kunmap(page);
2873                 set_extent_uptodate(io_tree, em->start,
2874                                     extent_map_end(em) - 1, GFP_NOFS);
2875                 goto insert;
2876         } else {
2877                 printk("unkknown found_type %d\n", found_type);
2878                 WARN_ON(1);
2879         }
2880 not_found:
2881         em->start = start;
2882         em->len = len;
2883 not_found_em:
2884         em->block_start = EXTENT_MAP_HOLE;
2885 insert:
2886         btrfs_release_path(root, path);
2887         if (em->start > start || extent_map_end(em) <= start) {
2888                 printk("bad extent! em: [%Lu %Lu] passed [%Lu %Lu]\n", em->start, em->len, start, len);
2889                 err = -EIO;
2890                 goto out;
2891         }
2892
2893         err = 0;
2894         spin_lock(&em_tree->lock);
2895         ret = add_extent_mapping(em_tree, em);
2896         /* it is possible that someone inserted the extent into the tree
2897          * while we had the lock dropped.  It is also possible that
2898          * an overlapping map exists in the tree
2899          */
2900         if (ret == -EEXIST) {
2901                 struct extent_map *existing;
2902
2903                 ret = 0;
2904
2905                 existing = lookup_extent_mapping(em_tree, start, len);
2906                 if (existing && (existing->start > start ||
2907                     existing->start + existing->len <= start)) {
2908                         free_extent_map(existing);
2909                         existing = NULL;
2910                 }
2911                 if (!existing) {
2912                         existing = lookup_extent_mapping(em_tree, em->start,
2913                                                          em->len);
2914                         if (existing) {
2915                                 err = merge_extent_mapping(em_tree, existing,
2916                                                            em, start,
2917                                                            root->sectorsize);
2918                                 free_extent_map(existing);
2919                                 if (err) {
2920                                         free_extent_map(em);
2921                                         em = NULL;
2922                                 }
2923                         } else {
2924                                 err = -EIO;
2925                                 printk("failing to insert %Lu %Lu\n",
2926                                        start, len);
2927                                 free_extent_map(em);
2928                                 em = NULL;
2929                         }
2930                 } else {
2931                         free_extent_map(em);
2932                         em = existing;
2933                         err = 0;
2934                 }
2935         }
2936         spin_unlock(&em_tree->lock);
2937 out:
2938         if (path)
2939                 btrfs_free_path(path);
2940         if (trans) {
2941                 ret = btrfs_end_transaction(trans, root);
2942                 if (!err) {
2943                         err = ret;
2944                 }
2945         }
2946         if (err) {
2947                 free_extent_map(em);
2948                 WARN_ON(1);
2949                 return ERR_PTR(err);
2950         }
2951         return em;
2952 }
2953
2954 #if 0 /* waiting for O_DIRECT reads */
2955 static int btrfs_get_block(struct inode *inode, sector_t iblock,
2956                         struct buffer_head *bh_result, int create)
2957 {
2958         struct extent_map *em;
2959         u64 start = (u64)iblock << inode->i_blkbits;
2960         struct btrfs_multi_bio *multi = NULL;
2961         struct btrfs_root *root = BTRFS_I(inode)->root;
2962         u64 len;
2963         u64 logical;
2964         u64 map_length;
2965         int ret = 0;
2966
2967         em = btrfs_get_extent(inode, NULL, 0, start, bh_result->b_size, 0);
2968
2969         if (!em || IS_ERR(em))
2970                 goto out;
2971
2972         if (em->start > start || em->start + em->len <= start) {
2973             goto out;
2974         }
2975
2976         if (em->block_start == EXTENT_MAP_INLINE) {
2977                 ret = -EINVAL;
2978                 goto out;
2979         }
2980
2981         len = em->start + em->len - start;
2982         len = min_t(u64, len, INT_LIMIT(typeof(bh_result->b_size)));
2983
2984         if (em->block_start == EXTENT_MAP_HOLE ||
2985             em->block_start == EXTENT_MAP_DELALLOC) {
2986                 bh_result->b_size = len;
2987                 goto out;
2988         }
2989
2990         logical = start - em->start;
2991         logical = em->block_start + logical;
2992
2993         map_length = len;
2994         ret = btrfs_map_block(&root->fs_info->mapping_tree, READ,
2995                               logical, &map_length, &multi, 0);
2996         BUG_ON(ret);
2997         bh_result->b_blocknr = multi->stripes[0].physical >> inode->i_blkbits;
2998         bh_result->b_size = min(map_length, len);
2999
3000         bh_result->b_bdev = multi->stripes[0].dev->bdev;
3001         set_buffer_mapped(bh_result);
3002         kfree(multi);
3003 out:
3004         free_extent_map(em);
3005         return ret;
3006 }
3007 #endif
3008
3009 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
3010                         const struct iovec *iov, loff_t offset,
3011                         unsigned long nr_segs)
3012 {
3013         return -EINVAL;
3014 #if 0
3015         struct file *file = iocb->ki_filp;
3016         struct inode *inode = file->f_mapping->host;
3017
3018         if (rw == WRITE)
3019                 return -EINVAL;
3020
3021         return blockdev_direct_IO(rw, iocb, inode, inode->i_sb->s_bdev, iov,
3022                                   offset, nr_segs, btrfs_get_block, NULL);
3023 #endif
3024 }
3025
3026 static sector_t btrfs_bmap(struct address_space *mapping, sector_t iblock)
3027 {
3028         return extent_bmap(mapping, iblock, btrfs_get_extent);
3029 }
3030
3031 int btrfs_readpage(struct file *file, struct page *page)
3032 {
3033         struct extent_io_tree *tree;
3034         tree = &BTRFS_I(page->mapping->host)->io_tree;
3035         return extent_read_full_page(tree, page, btrfs_get_extent);
3036 }
3037
3038 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
3039 {
3040         struct extent_io_tree *tree;
3041
3042
3043         if (current->flags & PF_MEMALLOC) {
3044                 redirty_page_for_writepage(wbc, page);
3045                 unlock_page(page);
3046                 return 0;
3047         }
3048         tree = &BTRFS_I(page->mapping->host)->io_tree;
3049         return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
3050 }
3051
3052 int btrfs_writepages(struct address_space *mapping,
3053                      struct writeback_control *wbc)
3054 {
3055         struct extent_io_tree *tree;
3056         tree = &BTRFS_I(mapping->host)->io_tree;
3057         return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
3058 }
3059
3060 static int
3061 btrfs_readpages(struct file *file, struct address_space *mapping,
3062                 struct list_head *pages, unsigned nr_pages)
3063 {
3064         struct extent_io_tree *tree;
3065         tree = &BTRFS_I(mapping->host)->io_tree;
3066         return extent_readpages(tree, mapping, pages, nr_pages,
3067                                 btrfs_get_extent);
3068 }
3069 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3070 {
3071         struct extent_io_tree *tree;
3072         struct extent_map_tree *map;
3073         int ret;
3074
3075         tree = &BTRFS_I(page->mapping->host)->io_tree;
3076         map = &BTRFS_I(page->mapping->host)->extent_tree;
3077         ret = try_release_extent_mapping(map, tree, page, gfp_flags);
3078         if (ret == 1) {
3079                 ClearPagePrivate(page);
3080                 set_page_private(page, 0);
3081                 page_cache_release(page);
3082         }
3083         return ret;
3084 }
3085
3086 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
3087 {
3088         if (PageWriteback(page) || PageDirty(page))
3089                 return 0;
3090         return __btrfs_releasepage(page, gfp_flags);
3091 }
3092
3093 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
3094 {
3095         struct extent_io_tree *tree;
3096         struct btrfs_ordered_extent *ordered;
3097         u64 page_start = page_offset(page);
3098         u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
3099
3100         wait_on_page_writeback(page);
3101         tree = &BTRFS_I(page->mapping->host)->io_tree;
3102         if (offset) {
3103                 btrfs_releasepage(page, GFP_NOFS);
3104                 return;
3105         }
3106
3107         lock_extent(tree, page_start, page_end, GFP_NOFS);
3108         ordered = btrfs_lookup_ordered_extent(page->mapping->host,
3109                                            page_offset(page));
3110         if (ordered) {
3111                 /*
3112                  * IO on this page will never be started, so we need
3113                  * to account for any ordered extents now
3114                  */
3115                 clear_extent_bit(tree, page_start, page_end,
3116                                  EXTENT_DIRTY | EXTENT_DELALLOC |
3117                                  EXTENT_LOCKED, 1, 0, GFP_NOFS);
3118                 btrfs_finish_ordered_io(page->mapping->host,
3119                                         page_start, page_end);
3120                 btrfs_put_ordered_extent(ordered);
3121                 lock_extent(tree, page_start, page_end, GFP_NOFS);
3122         }
3123         clear_extent_bit(tree, page_start, page_end,
3124                  EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
3125                  EXTENT_ORDERED,
3126                  1, 1, GFP_NOFS);
3127         __btrfs_releasepage(page, GFP_NOFS);
3128
3129         ClearPageChecked(page);
3130         if (PagePrivate(page)) {
3131                 ClearPagePrivate(page);
3132                 set_page_private(page, 0);
3133                 page_cache_release(page);
3134         }
3135 }
3136
3137 /*
3138  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
3139  * called from a page fault handler when a page is first dirtied. Hence we must
3140  * be careful to check for EOF conditions here. We set the page up correctly
3141  * for a written page which means we get ENOSPC checking when writing into
3142  * holes and correct delalloc and unwritten extent mapping on filesystems that
3143  * support these features.
3144  *
3145  * We are not allowed to take the i_mutex here so we have to play games to
3146  * protect against truncate races as the page could now be beyond EOF.  Because
3147  * vmtruncate() writes the inode size before removing pages, once we have the
3148  * page lock we can determine safely if the page is beyond EOF. If it is not
3149  * beyond EOF, then the page is guaranteed safe against truncation until we
3150  * unlock the page.
3151  */
3152 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct page *page)
3153 {
3154         struct inode *inode = fdentry(vma->vm_file)->d_inode;
3155         struct btrfs_root *root = BTRFS_I(inode)->root;
3156         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3157         struct btrfs_ordered_extent *ordered;
3158         char *kaddr;
3159         unsigned long zero_start;
3160         loff_t size;
3161         int ret;
3162         u64 page_start;
3163         u64 page_end;
3164
3165         ret = btrfs_check_free_space(root, PAGE_CACHE_SIZE, 0);
3166         if (ret)
3167                 goto out;
3168
3169         ret = -EINVAL;
3170 again:
3171         lock_page(page);
3172         size = i_size_read(inode);
3173         page_start = page_offset(page);
3174         page_end = page_start + PAGE_CACHE_SIZE - 1;
3175
3176         if ((page->mapping != inode->i_mapping) ||
3177             (page_start >= size)) {
3178                 /* page got truncated out from underneath us */
3179                 goto out_unlock;
3180         }
3181         wait_on_page_writeback(page);
3182
3183         lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3184         set_page_extent_mapped(page);
3185
3186         /*
3187          * we can't set the delalloc bits if there are pending ordered
3188          * extents.  Drop our locks and wait for them to finish
3189          */
3190         ordered = btrfs_lookup_ordered_extent(inode, page_start);
3191         if (ordered) {
3192                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3193                 unlock_page(page);
3194                 btrfs_start_ordered_extent(inode, ordered, 1);
3195                 btrfs_put_ordered_extent(ordered);
3196                 goto again;
3197         }
3198
3199         btrfs_set_extent_delalloc(inode, page_start, page_end);
3200         ret = 0;
3201
3202         /* page is wholly or partially inside EOF */
3203         if (page_start + PAGE_CACHE_SIZE > size)
3204                 zero_start = size & ~PAGE_CACHE_MASK;
3205         else
3206                 zero_start = PAGE_CACHE_SIZE;
3207
3208         if (zero_start != PAGE_CACHE_SIZE) {
3209                 kaddr = kmap(page);
3210                 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
3211                 flush_dcache_page(page);
3212                 kunmap(page);
3213         }
3214         ClearPageChecked(page);
3215         set_page_dirty(page);
3216         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3217
3218 out_unlock:
3219         unlock_page(page);
3220 out:
3221         return ret;
3222 }
3223
3224 static void btrfs_truncate(struct inode *inode)
3225 {
3226         struct btrfs_root *root = BTRFS_I(inode)->root;
3227         int ret;
3228         struct btrfs_trans_handle *trans;
3229         unsigned long nr;
3230         u64 mask = root->sectorsize - 1;
3231
3232         if (!S_ISREG(inode->i_mode))
3233                 return;
3234         if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
3235                 return;
3236
3237         btrfs_truncate_page(inode->i_mapping, inode->i_size);
3238         btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
3239
3240         trans = btrfs_start_transaction(root, 1);
3241         btrfs_set_trans_block_group(trans, inode);
3242         btrfs_i_size_write(inode, inode->i_size);
3243
3244         ret = btrfs_orphan_add(trans, inode);
3245         if (ret)
3246                 goto out;
3247         /* FIXME, add redo link to tree so we don't leak on crash */
3248         ret = btrfs_truncate_inode_items(trans, root, inode, inode->i_size,
3249                                       BTRFS_EXTENT_DATA_KEY);
3250         btrfs_update_inode(trans, root, inode);
3251
3252         ret = btrfs_orphan_del(trans, inode);
3253         BUG_ON(ret);
3254
3255 out:
3256         nr = trans->blocks_used;
3257         ret = btrfs_end_transaction_throttle(trans, root);
3258         BUG_ON(ret);
3259         btrfs_btree_balance_dirty(root, nr);
3260 }
3261
3262 /*
3263  * Invalidate a single dcache entry at the root of the filesystem.
3264  * Needed after creation of snapshot or subvolume.
3265  */
3266 void btrfs_invalidate_dcache_root(struct btrfs_root *root, char *name,
3267                                   int namelen)
3268 {
3269         struct dentry *alias, *entry;
3270         struct qstr qstr;
3271
3272         alias = d_find_alias(root->fs_info->sb->s_root->d_inode);
3273         if (alias) {
3274                 qstr.name = name;
3275                 qstr.len = namelen;
3276                 /* change me if btrfs ever gets a d_hash operation */
3277                 qstr.hash = full_name_hash(qstr.name, qstr.len);
3278                 entry = d_lookup(alias, &qstr);
3279                 dput(alias);
3280                 if (entry) {
3281                         d_invalidate(entry);
3282                         dput(entry);
3283                 }
3284         }
3285 }
3286
3287 int btrfs_create_subvol_root(struct btrfs_root *new_root,
3288                 struct btrfs_trans_handle *trans, u64 new_dirid,
3289                 struct btrfs_block_group_cache *block_group)
3290 {
3291         struct inode *inode;
3292         u64 index = 0;
3293
3294         inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
3295                                 new_dirid, block_group, S_IFDIR | 0700, &index);
3296         if (IS_ERR(inode))
3297                 return PTR_ERR(inode);
3298         inode->i_op = &btrfs_dir_inode_operations;
3299         inode->i_fop = &btrfs_dir_file_operations;
3300         new_root->inode = inode;
3301
3302         inode->i_nlink = 1;
3303         btrfs_i_size_write(inode, 0);
3304
3305         return btrfs_update_inode(trans, new_root, inode);
3306 }
3307
3308 unsigned long btrfs_force_ra(struct address_space *mapping,
3309                               struct file_ra_state *ra, struct file *file,
3310                               pgoff_t offset, pgoff_t last_index)
3311 {
3312         pgoff_t req_size = last_index - offset + 1;
3313
3314 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3315         offset = page_cache_readahead(mapping, ra, file, offset, req_size);
3316         return offset;
3317 #else
3318         page_cache_sync_readahead(mapping, ra, file, offset, req_size);
3319         return offset + req_size;
3320 #endif
3321 }
3322
3323 struct inode *btrfs_alloc_inode(struct super_block *sb)
3324 {
3325         struct btrfs_inode *ei;
3326
3327         ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
3328         if (!ei)
3329                 return NULL;
3330         ei->last_trans = 0;
3331         ei->logged_trans = 0;
3332         btrfs_ordered_inode_tree_init(&ei->ordered_tree);
3333         ei->i_acl = BTRFS_ACL_NOT_CACHED;
3334         ei->i_default_acl = BTRFS_ACL_NOT_CACHED;
3335         INIT_LIST_HEAD(&ei->i_orphan);
3336         return &ei->vfs_inode;
3337 }
3338
3339 void btrfs_destroy_inode(struct inode *inode)
3340 {
3341         struct btrfs_ordered_extent *ordered;
3342         WARN_ON(!list_empty(&inode->i_dentry));
3343         WARN_ON(inode->i_data.nrpages);
3344
3345         if (BTRFS_I(inode)->i_acl &&
3346             BTRFS_I(inode)->i_acl != BTRFS_ACL_NOT_CACHED)
3347                 posix_acl_release(BTRFS_I(inode)->i_acl);
3348         if (BTRFS_I(inode)->i_default_acl &&
3349             BTRFS_I(inode)->i_default_acl != BTRFS_ACL_NOT_CACHED)
3350                 posix_acl_release(BTRFS_I(inode)->i_default_acl);
3351
3352         spin_lock(&BTRFS_I(inode)->root->list_lock);
3353         if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
3354                 printk(KERN_ERR "BTRFS: inode %lu: inode still on the orphan"
3355                        " list\n", inode->i_ino);
3356                 dump_stack();
3357         }
3358         spin_unlock(&BTRFS_I(inode)->root->list_lock);
3359
3360         while(1) {
3361                 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
3362                 if (!ordered)
3363                         break;
3364                 else {
3365                         printk("found ordered extent %Lu %Lu\n",
3366                                ordered->file_offset, ordered->len);
3367                         btrfs_remove_ordered_extent(inode, ordered);
3368                         btrfs_put_ordered_extent(ordered);
3369                         btrfs_put_ordered_extent(ordered);
3370                 }
3371         }
3372         btrfs_drop_extent_cache(inode, 0, (u64)-1);
3373         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
3374 }
3375
3376 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3377 static void init_once(void *foo)
3378 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3379 static void init_once(struct kmem_cache * cachep, void *foo)
3380 #else
3381 static void init_once(void * foo, struct kmem_cache * cachep,
3382                       unsigned long flags)
3383 #endif
3384 {
3385         struct btrfs_inode *ei = (struct btrfs_inode *) foo;
3386
3387         inode_init_once(&ei->vfs_inode);
3388 }
3389
3390 void btrfs_destroy_cachep(void)
3391 {
3392         if (btrfs_inode_cachep)
3393                 kmem_cache_destroy(btrfs_inode_cachep);
3394         if (btrfs_trans_handle_cachep)
3395                 kmem_cache_destroy(btrfs_trans_handle_cachep);
3396         if (btrfs_transaction_cachep)
3397                 kmem_cache_destroy(btrfs_transaction_cachep);
3398         if (btrfs_bit_radix_cachep)
3399                 kmem_cache_destroy(btrfs_bit_radix_cachep);
3400         if (btrfs_path_cachep)
3401                 kmem_cache_destroy(btrfs_path_cachep);
3402 }
3403
3404 struct kmem_cache *btrfs_cache_create(const char *name, size_t size,
3405                                        unsigned long extra_flags,
3406 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3407                                        void (*ctor)(void *)
3408 #elif LINUX_VERSION_CODE > KERNEL_VERSION(2,6,23)
3409                                        void (*ctor)(struct kmem_cache *, void *)
3410 #else
3411                                        void (*ctor)(void *, struct kmem_cache *,
3412                                                     unsigned long)
3413 #endif
3414                                      )
3415 {
3416         return kmem_cache_create(name, size, 0, (SLAB_RECLAIM_ACCOUNT |
3417                                  SLAB_MEM_SPREAD | extra_flags), ctor
3418 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23)
3419                                  ,NULL
3420 #endif
3421                                 );
3422 }
3423
3424 int btrfs_init_cachep(void)
3425 {
3426         btrfs_inode_cachep = btrfs_cache_create("btrfs_inode_cache",
3427                                           sizeof(struct btrfs_inode),
3428                                           0, init_once);
3429         if (!btrfs_inode_cachep)
3430                 goto fail;
3431         btrfs_trans_handle_cachep =
3432                         btrfs_cache_create("btrfs_trans_handle_cache",
3433                                            sizeof(struct btrfs_trans_handle),
3434                                            0, NULL);
3435         if (!btrfs_trans_handle_cachep)
3436                 goto fail;
3437         btrfs_transaction_cachep = btrfs_cache_create("btrfs_transaction_cache",
3438                                              sizeof(struct btrfs_transaction),
3439                                              0, NULL);
3440         if (!btrfs_transaction_cachep)
3441                 goto fail;
3442         btrfs_path_cachep = btrfs_cache_create("btrfs_path_cache",
3443                                          sizeof(struct btrfs_path),
3444                                          0, NULL);
3445         if (!btrfs_path_cachep)
3446                 goto fail;
3447         btrfs_bit_radix_cachep = btrfs_cache_create("btrfs_radix", 256,
3448                                               SLAB_DESTROY_BY_RCU, NULL);
3449         if (!btrfs_bit_radix_cachep)
3450                 goto fail;
3451         return 0;
3452 fail:
3453         btrfs_destroy_cachep();
3454         return -ENOMEM;
3455 }
3456
3457 static int btrfs_getattr(struct vfsmount *mnt,
3458                          struct dentry *dentry, struct kstat *stat)
3459 {
3460         struct inode *inode = dentry->d_inode;
3461         generic_fillattr(inode, stat);
3462         stat->blksize = PAGE_CACHE_SIZE;
3463         stat->blocks = inode->i_blocks + (BTRFS_I(inode)->delalloc_bytes >> 9);
3464         return 0;
3465 }
3466
3467 static int btrfs_rename(struct inode * old_dir, struct dentry *old_dentry,
3468                            struct inode * new_dir,struct dentry *new_dentry)
3469 {
3470         struct btrfs_trans_handle *trans;
3471         struct btrfs_root *root = BTRFS_I(old_dir)->root;
3472         struct inode *new_inode = new_dentry->d_inode;
3473         struct inode *old_inode = old_dentry->d_inode;
3474         struct timespec ctime = CURRENT_TIME;
3475         u64 index = 0;
3476         int ret;
3477
3478         if (S_ISDIR(old_inode->i_mode) && new_inode &&
3479             new_inode->i_size > BTRFS_EMPTY_DIR_SIZE) {
3480                 return -ENOTEMPTY;
3481         }
3482
3483         ret = btrfs_check_free_space(root, 1, 0);
3484         if (ret)
3485                 goto out_unlock;
3486
3487         trans = btrfs_start_transaction(root, 1);
3488
3489         btrfs_set_trans_block_group(trans, new_dir);
3490
3491         btrfs_inc_nlink(old_dentry->d_inode);
3492         old_dir->i_ctime = old_dir->i_mtime = ctime;
3493         new_dir->i_ctime = new_dir->i_mtime = ctime;
3494         old_inode->i_ctime = ctime;
3495
3496         ret = btrfs_unlink_inode(trans, root, old_dir, old_dentry->d_inode,
3497                                  old_dentry->d_name.name,
3498                                  old_dentry->d_name.len);
3499         if (ret)
3500                 goto out_fail;
3501
3502         if (new_inode) {
3503                 new_inode->i_ctime = CURRENT_TIME;
3504                 ret = btrfs_unlink_inode(trans, root, new_dir,
3505                                          new_dentry->d_inode,
3506                                          new_dentry->d_name.name,
3507                                          new_dentry->d_name.len);
3508                 if (ret)
3509                         goto out_fail;
3510                 if (new_inode->i_nlink == 0) {
3511                         ret = btrfs_orphan_add(trans, new_dentry->d_inode);
3512                         if (ret)
3513                                 goto out_fail;
3514                 }
3515
3516         }
3517         ret = btrfs_set_inode_index(new_dir, old_inode, &index);
3518         if (ret)
3519                 goto out_fail;
3520
3521         ret = btrfs_add_link(trans, new_dentry->d_parent->d_inode,
3522                              old_inode, new_dentry->d_name.name,
3523                              new_dentry->d_name.len, 1, index);
3524         if (ret)
3525                 goto out_fail;
3526
3527 out_fail:
3528         btrfs_end_transaction_throttle(trans, root);
3529 out_unlock:
3530         return ret;
3531 }
3532
3533 int btrfs_start_delalloc_inodes(struct btrfs_root *root)
3534 {
3535         struct list_head *head = &root->fs_info->delalloc_inodes;
3536         struct btrfs_inode *binode;
3537         unsigned long flags;
3538
3539         spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3540         while(!list_empty(head)) {
3541                 binode = list_entry(head->next, struct btrfs_inode,
3542                                     delalloc_inodes);
3543                 atomic_inc(&binode->vfs_inode.i_count);
3544                 spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3545                 filemap_write_and_wait(binode->vfs_inode.i_mapping);
3546                 iput(&binode->vfs_inode);
3547                 spin_lock_irqsave(&root->fs_info->delalloc_lock, flags);
3548         }
3549         spin_unlock_irqrestore(&root->fs_info->delalloc_lock, flags);
3550         return 0;
3551 }
3552
3553 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
3554                          const char *symname)
3555 {
3556         struct btrfs_trans_handle *trans;
3557         struct btrfs_root *root = BTRFS_I(dir)->root;
3558         struct btrfs_path *path;
3559         struct btrfs_key key;
3560         struct inode *inode = NULL;
3561         int err;
3562         int drop_inode = 0;
3563         u64 objectid;
3564         u64 index = 0 ;
3565         int name_len;
3566         int datasize;
3567         unsigned long ptr;
3568         struct btrfs_file_extent_item *ei;
3569         struct extent_buffer *leaf;
3570         unsigned long nr = 0;
3571
3572         name_len = strlen(symname) + 1;
3573         if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
3574                 return -ENAMETOOLONG;
3575
3576         err = btrfs_check_free_space(root, 1, 0);
3577         if (err)
3578                 goto out_fail;
3579
3580         trans = btrfs_start_transaction(root, 1);
3581         btrfs_set_trans_block_group(trans, dir);
3582
3583         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
3584         if (err) {
3585                 err = -ENOSPC;
3586                 goto out_unlock;
3587         }
3588
3589         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
3590                                 dentry->d_name.len,
3591                                 dentry->d_parent->d_inode->i_ino, objectid,
3592                                 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
3593                                 &index);
3594         err = PTR_ERR(inode);
3595         if (IS_ERR(inode))
3596                 goto out_unlock;
3597
3598         err = btrfs_init_acl(inode, dir);
3599         if (err) {
3600                 drop_inode = 1;
3601                 goto out_unlock;
3602         }
3603
3604         btrfs_set_trans_block_group(trans, inode);
3605         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
3606         if (err)
3607                 drop_inode = 1;
3608         else {
3609                 inode->i_mapping->a_ops = &btrfs_aops;
3610                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3611                 inode->i_fop = &btrfs_file_operations;
3612                 inode->i_op = &btrfs_file_inode_operations;
3613                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3614         }
3615         dir->i_sb->s_dirt = 1;
3616         btrfs_update_inode_block_group(trans, inode);
3617         btrfs_update_inode_block_group(trans, dir);
3618         if (drop_inode)
3619                 goto out_unlock;
3620
3621         path = btrfs_alloc_path();
3622         BUG_ON(!path);
3623         key.objectid = inode->i_ino;
3624         key.offset = 0;
3625         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
3626         datasize = btrfs_file_extent_calc_inline_size(name_len);
3627         err = btrfs_insert_empty_item(trans, root, path, &key,
3628                                       datasize);
3629         if (err) {
3630                 drop_inode = 1;
3631                 goto out_unlock;
3632         }
3633         leaf = path->nodes[0];
3634         ei = btrfs_item_ptr(leaf, path->slots[0],
3635                             struct btrfs_file_extent_item);
3636         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
3637         btrfs_set_file_extent_type(leaf, ei,
3638                                    BTRFS_FILE_EXTENT_INLINE);
3639         ptr = btrfs_file_extent_inline_start(ei);
3640         write_extent_buffer(leaf, symname, ptr, name_len);
3641         btrfs_mark_buffer_dirty(leaf);
3642         btrfs_free_path(path);
3643
3644         inode->i_op = &btrfs_symlink_inode_operations;
3645         inode->i_mapping->a_ops = &btrfs_symlink_aops;
3646         inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
3647         btrfs_i_size_write(inode, name_len - 1);
3648         err = btrfs_update_inode(trans, root, inode);
3649         if (err)
3650                 drop_inode = 1;
3651
3652 out_unlock:
3653         nr = trans->blocks_used;
3654         btrfs_end_transaction_throttle(trans, root);
3655 out_fail:
3656         if (drop_inode) {
3657                 inode_dec_link_count(inode);
3658                 iput(inode);
3659         }
3660         btrfs_btree_balance_dirty(root, nr);
3661         return err;
3662 }
3663
3664 static int btrfs_set_page_dirty(struct page *page)
3665 {
3666         return __set_page_dirty_nobuffers(page);
3667 }
3668
3669 #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,26)
3670 static int btrfs_permission(struct inode *inode, int mask)
3671 #else
3672 static int btrfs_permission(struct inode *inode, int mask,
3673                             struct nameidata *nd)
3674 #endif
3675 {
3676         if (btrfs_test_flag(inode, READONLY) && (mask & MAY_WRITE))
3677                 return -EACCES;
3678         return generic_permission(inode, mask, btrfs_check_acl);
3679 }
3680
3681 static struct inode_operations btrfs_dir_inode_operations = {
3682         .lookup         = btrfs_lookup,
3683         .create         = btrfs_create,
3684         .unlink         = btrfs_unlink,
3685         .link           = btrfs_link,
3686         .mkdir          = btrfs_mkdir,
3687         .rmdir          = btrfs_rmdir,
3688         .rename         = btrfs_rename,
3689         .symlink        = btrfs_symlink,
3690         .setattr        = btrfs_setattr,
3691         .mknod          = btrfs_mknod,
3692         .setxattr       = btrfs_setxattr,
3693         .getxattr       = btrfs_getxattr,
3694         .listxattr      = btrfs_listxattr,
3695         .removexattr    = btrfs_removexattr,
3696         .permission     = btrfs_permission,
3697 };
3698 static struct inode_operations btrfs_dir_ro_inode_operations = {
3699         .lookup         = btrfs_lookup,
3700         .permission     = btrfs_permission,
3701 };
3702 static struct file_operations btrfs_dir_file_operations = {
3703         .llseek         = generic_file_llseek,
3704         .read           = generic_read_dir,
3705 #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,28)
3706         .readdir        = btrfs_nfshack_readdir,
3707 #else /* NFSd readdir/lookup deadlock is fixed */
3708         .readdir        = btrfs_real_readdir,
3709 #endif
3710         .unlocked_ioctl = btrfs_ioctl,
3711 #ifdef CONFIG_COMPAT
3712         .compat_ioctl   = btrfs_ioctl,
3713 #endif
3714         .release        = btrfs_release_file,
3715         .fsync          = btrfs_sync_file,
3716 };
3717
3718 static struct extent_io_ops btrfs_extent_io_ops = {
3719         .fill_delalloc = run_delalloc_range,
3720         .submit_bio_hook = btrfs_submit_bio_hook,
3721         .merge_bio_hook = btrfs_merge_bio_hook,
3722         .readpage_end_io_hook = btrfs_readpage_end_io_hook,
3723         .writepage_end_io_hook = btrfs_writepage_end_io_hook,
3724         .writepage_start_hook = btrfs_writepage_start_hook,
3725         .readpage_io_failed_hook = btrfs_io_failed_hook,
3726         .set_bit_hook = btrfs_set_bit_hook,
3727         .clear_bit_hook = btrfs_clear_bit_hook,
3728 };
3729
3730 static struct address_space_operations btrfs_aops = {
3731         .readpage       = btrfs_readpage,
3732         .writepage      = btrfs_writepage,
3733         .writepages     = btrfs_writepages,
3734         .readpages      = btrfs_readpages,
3735         .sync_page      = block_sync_page,
3736         .bmap           = btrfs_bmap,
3737         .direct_IO      = btrfs_direct_IO,
3738         .invalidatepage = btrfs_invalidatepage,
3739         .releasepage    = btrfs_releasepage,
3740         .set_page_dirty = btrfs_set_page_dirty,
3741 };
3742
3743 static struct address_space_operations btrfs_symlink_aops = {
3744         .readpage       = btrfs_readpage,
3745         .writepage      = btrfs_writepage,
3746         .invalidatepage = btrfs_invalidatepage,
3747         .releasepage    = btrfs_releasepage,
3748 };
3749
3750 static struct inode_operations btrfs_file_inode_operations = {
3751         .truncate       = btrfs_truncate,
3752         .getattr        = btrfs_getattr,
3753         .setattr        = btrfs_setattr,
3754         .setxattr       = btrfs_setxattr,
3755         .getxattr       = btrfs_getxattr,
3756         .listxattr      = btrfs_listxattr,
3757         .removexattr    = btrfs_removexattr,
3758         .permission     = btrfs_permission,
3759 };
3760 static struct inode_operations btrfs_special_inode_operations = {
3761         .getattr        = btrfs_getattr,
3762         .setattr        = btrfs_setattr,
3763         .permission     = btrfs_permission,
3764         .setxattr       = btrfs_setxattr,
3765         .getxattr       = btrfs_getxattr,
3766         .listxattr      = btrfs_listxattr,
3767         .removexattr    = btrfs_removexattr,
3768 };
3769 static struct inode_operations btrfs_symlink_inode_operations = {
3770         .readlink       = generic_readlink,
3771         .follow_link    = page_follow_link_light,
3772         .put_link       = page_put_link,
3773         .permission     = btrfs_permission,
3774 };