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