NFSv4.1: Ensure state manager thread dies on last umount
[firefly-linux-kernel-4.4.55.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/backing-dev.h>
30 #include <linux/mpage.h>
31 #include <linux/swap.h>
32 #include <linux/writeback.h>
33 #include <linux/statfs.h>
34 #include <linux/compat.h>
35 #include <linux/bit_spinlock.h>
36 #include <linux/xattr.h>
37 #include <linux/posix_acl.h>
38 #include <linux/falloc.h>
39 #include "compat.h"
40 #include "ctree.h"
41 #include "disk-io.h"
42 #include "transaction.h"
43 #include "btrfs_inode.h"
44 #include "ioctl.h"
45 #include "print-tree.h"
46 #include "volumes.h"
47 #include "ordered-data.h"
48 #include "xattr.h"
49 #include "tree-log.h"
50 #include "compression.h"
51 #include "locking.h"
52
53 struct btrfs_iget_args {
54         u64 ino;
55         struct btrfs_root *root;
56 };
57
58 static const struct inode_operations btrfs_dir_inode_operations;
59 static const struct inode_operations btrfs_symlink_inode_operations;
60 static const struct inode_operations btrfs_dir_ro_inode_operations;
61 static const struct inode_operations btrfs_special_inode_operations;
62 static const struct inode_operations btrfs_file_inode_operations;
63 static const struct address_space_operations btrfs_aops;
64 static const struct address_space_operations btrfs_symlink_aops;
65 static const struct file_operations btrfs_dir_file_operations;
66 static struct extent_io_ops btrfs_extent_io_ops;
67
68 static struct kmem_cache *btrfs_inode_cachep;
69 struct kmem_cache *btrfs_trans_handle_cachep;
70 struct kmem_cache *btrfs_transaction_cachep;
71 struct kmem_cache *btrfs_path_cachep;
72
73 #define S_SHIFT 12
74 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
75         [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
76         [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
77         [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
78         [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
79         [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
80         [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
81         [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
82 };
83
84 static void btrfs_truncate(struct inode *inode);
85 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
86 static noinline int cow_file_range(struct inode *inode,
87                                    struct page *locked_page,
88                                    u64 start, u64 end, int *page_started,
89                                    unsigned long *nr_written, int unlock);
90
91 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
92                                      struct inode *inode,  struct inode *dir)
93 {
94         int err;
95
96         err = btrfs_init_acl(trans, inode, dir);
97         if (!err)
98                 err = btrfs_xattr_security_init(trans, inode, dir);
99         return err;
100 }
101
102 /*
103  * this does all the hard work for inserting an inline extent into
104  * the btree.  The caller should have done a btrfs_drop_extents so that
105  * no overlapping inline items exist in the btree
106  */
107 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
108                                 struct btrfs_root *root, struct inode *inode,
109                                 u64 start, size_t size, size_t compressed_size,
110                                 struct page **compressed_pages)
111 {
112         struct btrfs_key key;
113         struct btrfs_path *path;
114         struct extent_buffer *leaf;
115         struct page *page = NULL;
116         char *kaddr;
117         unsigned long ptr;
118         struct btrfs_file_extent_item *ei;
119         int err = 0;
120         int ret;
121         size_t cur_size = size;
122         size_t datasize;
123         unsigned long offset;
124         int use_compress = 0;
125
126         if (compressed_size && compressed_pages) {
127                 use_compress = 1;
128                 cur_size = compressed_size;
129         }
130
131         path = btrfs_alloc_path();
132         if (!path)
133                 return -ENOMEM;
134
135         path->leave_spinning = 1;
136         btrfs_set_trans_block_group(trans, inode);
137
138         key.objectid = inode->i_ino;
139         key.offset = start;
140         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
141         datasize = btrfs_file_extent_calc_inline_size(cur_size);
142
143         inode_add_bytes(inode, size);
144         ret = btrfs_insert_empty_item(trans, root, path, &key,
145                                       datasize);
146         BUG_ON(ret);
147         if (ret) {
148                 err = ret;
149                 goto fail;
150         }
151         leaf = path->nodes[0];
152         ei = btrfs_item_ptr(leaf, path->slots[0],
153                             struct btrfs_file_extent_item);
154         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
155         btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
156         btrfs_set_file_extent_encryption(leaf, ei, 0);
157         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
158         btrfs_set_file_extent_ram_bytes(leaf, ei, size);
159         ptr = btrfs_file_extent_inline_start(ei);
160
161         if (use_compress) {
162                 struct page *cpage;
163                 int i = 0;
164                 while (compressed_size > 0) {
165                         cpage = compressed_pages[i];
166                         cur_size = min_t(unsigned long, compressed_size,
167                                        PAGE_CACHE_SIZE);
168
169                         kaddr = kmap_atomic(cpage, KM_USER0);
170                         write_extent_buffer(leaf, kaddr, ptr, cur_size);
171                         kunmap_atomic(kaddr, KM_USER0);
172
173                         i++;
174                         ptr += cur_size;
175                         compressed_size -= cur_size;
176                 }
177                 btrfs_set_file_extent_compression(leaf, ei,
178                                                   BTRFS_COMPRESS_ZLIB);
179         } else {
180                 page = find_get_page(inode->i_mapping,
181                                      start >> PAGE_CACHE_SHIFT);
182                 btrfs_set_file_extent_compression(leaf, ei, 0);
183                 kaddr = kmap_atomic(page, KM_USER0);
184                 offset = start & (PAGE_CACHE_SIZE - 1);
185                 write_extent_buffer(leaf, kaddr + offset, ptr, size);
186                 kunmap_atomic(kaddr, KM_USER0);
187                 page_cache_release(page);
188         }
189         btrfs_mark_buffer_dirty(leaf);
190         btrfs_free_path(path);
191
192         /*
193          * we're an inline extent, so nobody can
194          * extend the file past i_size without locking
195          * a page we already have locked.
196          *
197          * We must do any isize and inode updates
198          * before we unlock the pages.  Otherwise we
199          * could end up racing with unlink.
200          */
201         BTRFS_I(inode)->disk_i_size = inode->i_size;
202         btrfs_update_inode(trans, root, inode);
203
204         return 0;
205 fail:
206         btrfs_free_path(path);
207         return err;
208 }
209
210
211 /*
212  * conditionally insert an inline extent into the file.  This
213  * does the checks required to make sure the data is small enough
214  * to fit as an inline extent.
215  */
216 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
217                                  struct btrfs_root *root,
218                                  struct inode *inode, u64 start, u64 end,
219                                  size_t compressed_size,
220                                  struct page **compressed_pages)
221 {
222         u64 isize = i_size_read(inode);
223         u64 actual_end = min(end + 1, isize);
224         u64 inline_len = actual_end - start;
225         u64 aligned_end = (end + root->sectorsize - 1) &
226                         ~((u64)root->sectorsize - 1);
227         u64 hint_byte;
228         u64 data_len = inline_len;
229         int ret;
230
231         if (compressed_size)
232                 data_len = compressed_size;
233
234         if (start > 0 ||
235             actual_end >= PAGE_CACHE_SIZE ||
236             data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
237             (!compressed_size &&
238             (actual_end & (root->sectorsize - 1)) == 0) ||
239             end + 1 < isize ||
240             data_len > root->fs_info->max_inline) {
241                 return 1;
242         }
243
244         ret = btrfs_drop_extents(trans, inode, start, aligned_end,
245                                  &hint_byte, 1);
246         BUG_ON(ret);
247
248         if (isize > actual_end)
249                 inline_len = min_t(u64, isize, actual_end);
250         ret = insert_inline_extent(trans, root, inode, start,
251                                    inline_len, compressed_size,
252                                    compressed_pages);
253         BUG_ON(ret);
254         btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
255         return 0;
256 }
257
258 struct async_extent {
259         u64 start;
260         u64 ram_size;
261         u64 compressed_size;
262         struct page **pages;
263         unsigned long nr_pages;
264         struct list_head list;
265 };
266
267 struct async_cow {
268         struct inode *inode;
269         struct btrfs_root *root;
270         struct page *locked_page;
271         u64 start;
272         u64 end;
273         struct list_head extents;
274         struct btrfs_work work;
275 };
276
277 static noinline int add_async_extent(struct async_cow *cow,
278                                      u64 start, u64 ram_size,
279                                      u64 compressed_size,
280                                      struct page **pages,
281                                      unsigned long nr_pages)
282 {
283         struct async_extent *async_extent;
284
285         async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
286         async_extent->start = start;
287         async_extent->ram_size = ram_size;
288         async_extent->compressed_size = compressed_size;
289         async_extent->pages = pages;
290         async_extent->nr_pages = nr_pages;
291         list_add_tail(&async_extent->list, &cow->extents);
292         return 0;
293 }
294
295 /*
296  * we create compressed extents in two phases.  The first
297  * phase compresses a range of pages that have already been
298  * locked (both pages and state bits are locked).
299  *
300  * This is done inside an ordered work queue, and the compression
301  * is spread across many cpus.  The actual IO submission is step
302  * two, and the ordered work queue takes care of making sure that
303  * happens in the same order things were put onto the queue by
304  * writepages and friends.
305  *
306  * If this code finds it can't get good compression, it puts an
307  * entry onto the work queue to write the uncompressed bytes.  This
308  * makes sure that both compressed inodes and uncompressed inodes
309  * are written in the same order that pdflush sent them down.
310  */
311 static noinline int compress_file_range(struct inode *inode,
312                                         struct page *locked_page,
313                                         u64 start, u64 end,
314                                         struct async_cow *async_cow,
315                                         int *num_added)
316 {
317         struct btrfs_root *root = BTRFS_I(inode)->root;
318         struct btrfs_trans_handle *trans;
319         u64 num_bytes;
320         u64 orig_start;
321         u64 disk_num_bytes;
322         u64 blocksize = root->sectorsize;
323         u64 actual_end;
324         u64 isize = i_size_read(inode);
325         int ret = 0;
326         struct page **pages = NULL;
327         unsigned long nr_pages;
328         unsigned long nr_pages_ret = 0;
329         unsigned long total_compressed = 0;
330         unsigned long total_in = 0;
331         unsigned long max_compressed = 128 * 1024;
332         unsigned long max_uncompressed = 128 * 1024;
333         int i;
334         int will_compress;
335
336         orig_start = start;
337
338         actual_end = min_t(u64, isize, end + 1);
339 again:
340         will_compress = 0;
341         nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
342         nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
343
344         /*
345          * we don't want to send crud past the end of i_size through
346          * compression, that's just a waste of CPU time.  So, if the
347          * end of the file is before the start of our current
348          * requested range of bytes, we bail out to the uncompressed
349          * cleanup code that can deal with all of this.
350          *
351          * It isn't really the fastest way to fix things, but this is a
352          * very uncommon corner.
353          */
354         if (actual_end <= start)
355                 goto cleanup_and_bail_uncompressed;
356
357         total_compressed = actual_end - start;
358
359         /* we want to make sure that amount of ram required to uncompress
360          * an extent is reasonable, so we limit the total size in ram
361          * of a compressed extent to 128k.  This is a crucial number
362          * because it also controls how easily we can spread reads across
363          * cpus for decompression.
364          *
365          * We also want to make sure the amount of IO required to do
366          * a random read is reasonably small, so we limit the size of
367          * a compressed extent to 128k.
368          */
369         total_compressed = min(total_compressed, max_uncompressed);
370         num_bytes = (end - start + blocksize) & ~(blocksize - 1);
371         num_bytes = max(blocksize,  num_bytes);
372         disk_num_bytes = num_bytes;
373         total_in = 0;
374         ret = 0;
375
376         /*
377          * we do compression for mount -o compress and when the
378          * inode has not been flagged as nocompress.  This flag can
379          * change at any time if we discover bad compression ratios.
380          */
381         if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
382             btrfs_test_opt(root, COMPRESS)) {
383                 WARN_ON(pages);
384                 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
385
386                 ret = btrfs_zlib_compress_pages(inode->i_mapping, start,
387                                                 total_compressed, pages,
388                                                 nr_pages, &nr_pages_ret,
389                                                 &total_in,
390                                                 &total_compressed,
391                                                 max_compressed);
392
393                 if (!ret) {
394                         unsigned long offset = total_compressed &
395                                 (PAGE_CACHE_SIZE - 1);
396                         struct page *page = pages[nr_pages_ret - 1];
397                         char *kaddr;
398
399                         /* zero the tail end of the last page, we might be
400                          * sending it down to disk
401                          */
402                         if (offset) {
403                                 kaddr = kmap_atomic(page, KM_USER0);
404                                 memset(kaddr + offset, 0,
405                                        PAGE_CACHE_SIZE - offset);
406                                 kunmap_atomic(kaddr, KM_USER0);
407                         }
408                         will_compress = 1;
409                 }
410         }
411         if (start == 0) {
412                 trans = btrfs_join_transaction(root, 1);
413                 BUG_ON(!trans);
414                 btrfs_set_trans_block_group(trans, inode);
415
416                 /* lets try to make an inline extent */
417                 if (ret || total_in < (actual_end - start)) {
418                         /* we didn't compress the entire range, try
419                          * to make an uncompressed inline extent.
420                          */
421                         ret = cow_file_range_inline(trans, root, inode,
422                                                     start, end, 0, NULL);
423                 } else {
424                         /* try making a compressed inline extent */
425                         ret = cow_file_range_inline(trans, root, inode,
426                                                     start, end,
427                                                     total_compressed, pages);
428                 }
429                 if (ret == 0) {
430                         /*
431                          * inline extent creation worked, we don't need
432                          * to create any more async work items.  Unlock
433                          * and free up our temp pages.
434                          */
435                         extent_clear_unlock_delalloc(inode,
436                              &BTRFS_I(inode)->io_tree,
437                              start, end, NULL,
438                              EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
439                              EXTENT_CLEAR_DELALLOC |
440                              EXTENT_CLEAR_ACCOUNTING |
441                              EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
442
443                         btrfs_end_transaction(trans, root);
444                         goto free_pages_out;
445                 }
446                 btrfs_end_transaction(trans, root);
447         }
448
449         if (will_compress) {
450                 /*
451                  * we aren't doing an inline extent round the compressed size
452                  * up to a block size boundary so the allocator does sane
453                  * things
454                  */
455                 total_compressed = (total_compressed + blocksize - 1) &
456                         ~(blocksize - 1);
457
458                 /*
459                  * one last check to make sure the compression is really a
460                  * win, compare the page count read with the blocks on disk
461                  */
462                 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
463                         ~(PAGE_CACHE_SIZE - 1);
464                 if (total_compressed >= total_in) {
465                         will_compress = 0;
466                 } else {
467                         disk_num_bytes = total_compressed;
468                         num_bytes = total_in;
469                 }
470         }
471         if (!will_compress && pages) {
472                 /*
473                  * the compression code ran but failed to make things smaller,
474                  * free any pages it allocated and our page pointer array
475                  */
476                 for (i = 0; i < nr_pages_ret; i++) {
477                         WARN_ON(pages[i]->mapping);
478                         page_cache_release(pages[i]);
479                 }
480                 kfree(pages);
481                 pages = NULL;
482                 total_compressed = 0;
483                 nr_pages_ret = 0;
484
485                 /* flag the file so we don't compress in the future */
486                 BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
487         }
488         if (will_compress) {
489                 *num_added += 1;
490
491                 /* the async work queues will take care of doing actual
492                  * allocation on disk for these compressed pages,
493                  * and will submit them to the elevator.
494                  */
495                 add_async_extent(async_cow, start, num_bytes,
496                                  total_compressed, pages, nr_pages_ret);
497
498                 if (start + num_bytes < end && start + num_bytes < actual_end) {
499                         start += num_bytes;
500                         pages = NULL;
501                         cond_resched();
502                         goto again;
503                 }
504         } else {
505 cleanup_and_bail_uncompressed:
506                 /*
507                  * No compression, but we still need to write the pages in
508                  * the file we've been given so far.  redirty the locked
509                  * page if it corresponds to our extent and set things up
510                  * for the async work queue to run cow_file_range to do
511                  * the normal delalloc dance
512                  */
513                 if (page_offset(locked_page) >= start &&
514                     page_offset(locked_page) <= end) {
515                         __set_page_dirty_nobuffers(locked_page);
516                         /* unlocked later on in the async handlers */
517                 }
518                 add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0);
519                 *num_added += 1;
520         }
521
522 out:
523         return 0;
524
525 free_pages_out:
526         for (i = 0; i < nr_pages_ret; i++) {
527                 WARN_ON(pages[i]->mapping);
528                 page_cache_release(pages[i]);
529         }
530         kfree(pages);
531
532         goto out;
533 }
534
535 /*
536  * phase two of compressed writeback.  This is the ordered portion
537  * of the code, which only gets called in the order the work was
538  * queued.  We walk all the async extents created by compress_file_range
539  * and send them down to the disk.
540  */
541 static noinline int submit_compressed_extents(struct inode *inode,
542                                               struct async_cow *async_cow)
543 {
544         struct async_extent *async_extent;
545         u64 alloc_hint = 0;
546         struct btrfs_trans_handle *trans;
547         struct btrfs_key ins;
548         struct extent_map *em;
549         struct btrfs_root *root = BTRFS_I(inode)->root;
550         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
551         struct extent_io_tree *io_tree;
552         int ret = 0;
553
554         if (list_empty(&async_cow->extents))
555                 return 0;
556
557
558         while (!list_empty(&async_cow->extents)) {
559                 async_extent = list_entry(async_cow->extents.next,
560                                           struct async_extent, list);
561                 list_del(&async_extent->list);
562
563                 io_tree = &BTRFS_I(inode)->io_tree;
564
565 retry:
566                 /* did the compression code fall back to uncompressed IO? */
567                 if (!async_extent->pages) {
568                         int page_started = 0;
569                         unsigned long nr_written = 0;
570
571                         lock_extent(io_tree, async_extent->start,
572                                     async_extent->start +
573                                     async_extent->ram_size - 1, GFP_NOFS);
574
575                         /* allocate blocks */
576                         ret = cow_file_range(inode, async_cow->locked_page,
577                                              async_extent->start,
578                                              async_extent->start +
579                                              async_extent->ram_size - 1,
580                                              &page_started, &nr_written, 0);
581
582                         /*
583                          * if page_started, cow_file_range inserted an
584                          * inline extent and took care of all the unlocking
585                          * and IO for us.  Otherwise, we need to submit
586                          * all those pages down to the drive.
587                          */
588                         if (!page_started && !ret)
589                                 extent_write_locked_range(io_tree,
590                                                   inode, async_extent->start,
591                                                   async_extent->start +
592                                                   async_extent->ram_size - 1,
593                                                   btrfs_get_extent,
594                                                   WB_SYNC_ALL);
595                         kfree(async_extent);
596                         cond_resched();
597                         continue;
598                 }
599
600                 lock_extent(io_tree, async_extent->start,
601                             async_extent->start + async_extent->ram_size - 1,
602                             GFP_NOFS);
603
604                 trans = btrfs_join_transaction(root, 1);
605                 ret = btrfs_reserve_extent(trans, root,
606                                            async_extent->compressed_size,
607                                            async_extent->compressed_size,
608                                            0, alloc_hint,
609                                            (u64)-1, &ins, 1);
610                 btrfs_end_transaction(trans, root);
611
612                 if (ret) {
613                         int i;
614                         for (i = 0; i < async_extent->nr_pages; i++) {
615                                 WARN_ON(async_extent->pages[i]->mapping);
616                                 page_cache_release(async_extent->pages[i]);
617                         }
618                         kfree(async_extent->pages);
619                         async_extent->nr_pages = 0;
620                         async_extent->pages = NULL;
621                         unlock_extent(io_tree, async_extent->start,
622                                       async_extent->start +
623                                       async_extent->ram_size - 1, GFP_NOFS);
624                         goto retry;
625                 }
626
627                 /*
628                  * here we're doing allocation and writeback of the
629                  * compressed pages
630                  */
631                 btrfs_drop_extent_cache(inode, async_extent->start,
632                                         async_extent->start +
633                                         async_extent->ram_size - 1, 0);
634
635                 em = alloc_extent_map(GFP_NOFS);
636                 em->start = async_extent->start;
637                 em->len = async_extent->ram_size;
638                 em->orig_start = em->start;
639
640                 em->block_start = ins.objectid;
641                 em->block_len = ins.offset;
642                 em->bdev = root->fs_info->fs_devices->latest_bdev;
643                 set_bit(EXTENT_FLAG_PINNED, &em->flags);
644                 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
645
646                 while (1) {
647                         write_lock(&em_tree->lock);
648                         ret = add_extent_mapping(em_tree, em);
649                         write_unlock(&em_tree->lock);
650                         if (ret != -EEXIST) {
651                                 free_extent_map(em);
652                                 break;
653                         }
654                         btrfs_drop_extent_cache(inode, async_extent->start,
655                                                 async_extent->start +
656                                                 async_extent->ram_size - 1, 0);
657                 }
658
659                 ret = btrfs_add_ordered_extent(inode, async_extent->start,
660                                                ins.objectid,
661                                                async_extent->ram_size,
662                                                ins.offset,
663                                                BTRFS_ORDERED_COMPRESSED);
664                 BUG_ON(ret);
665
666                 /*
667                  * clear dirty, set writeback and unlock the pages.
668                  */
669                 extent_clear_unlock_delalloc(inode,
670                                 &BTRFS_I(inode)->io_tree,
671                                 async_extent->start,
672                                 async_extent->start +
673                                 async_extent->ram_size - 1,
674                                 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
675                                 EXTENT_CLEAR_UNLOCK |
676                                 EXTENT_CLEAR_DELALLOC |
677                                 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
678
679                 ret = btrfs_submit_compressed_write(inode,
680                                     async_extent->start,
681                                     async_extent->ram_size,
682                                     ins.objectid,
683                                     ins.offset, async_extent->pages,
684                                     async_extent->nr_pages);
685
686                 BUG_ON(ret);
687                 alloc_hint = ins.objectid + ins.offset;
688                 kfree(async_extent);
689                 cond_resched();
690         }
691
692         return 0;
693 }
694
695 /*
696  * when extent_io.c finds a delayed allocation range in the file,
697  * the call backs end up in this code.  The basic idea is to
698  * allocate extents on disk for the range, and create ordered data structs
699  * in ram to track those extents.
700  *
701  * locked_page is the page that writepage had locked already.  We use
702  * it to make sure we don't do extra locks or unlocks.
703  *
704  * *page_started is set to one if we unlock locked_page and do everything
705  * required to start IO on it.  It may be clean and already done with
706  * IO when we return.
707  */
708 static noinline int cow_file_range(struct inode *inode,
709                                    struct page *locked_page,
710                                    u64 start, u64 end, int *page_started,
711                                    unsigned long *nr_written,
712                                    int unlock)
713 {
714         struct btrfs_root *root = BTRFS_I(inode)->root;
715         struct btrfs_trans_handle *trans;
716         u64 alloc_hint = 0;
717         u64 num_bytes;
718         unsigned long ram_size;
719         u64 disk_num_bytes;
720         u64 cur_alloc_size;
721         u64 blocksize = root->sectorsize;
722         u64 actual_end;
723         u64 isize = i_size_read(inode);
724         struct btrfs_key ins;
725         struct extent_map *em;
726         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
727         int ret = 0;
728
729         trans = btrfs_join_transaction(root, 1);
730         BUG_ON(!trans);
731         btrfs_set_trans_block_group(trans, inode);
732
733         actual_end = min_t(u64, isize, end + 1);
734
735         num_bytes = (end - start + blocksize) & ~(blocksize - 1);
736         num_bytes = max(blocksize,  num_bytes);
737         disk_num_bytes = num_bytes;
738         ret = 0;
739
740         if (start == 0) {
741                 /* lets try to make an inline extent */
742                 ret = cow_file_range_inline(trans, root, inode,
743                                             start, end, 0, NULL);
744                 if (ret == 0) {
745                         extent_clear_unlock_delalloc(inode,
746                                      &BTRFS_I(inode)->io_tree,
747                                      start, end, NULL,
748                                      EXTENT_CLEAR_UNLOCK_PAGE |
749                                      EXTENT_CLEAR_UNLOCK |
750                                      EXTENT_CLEAR_DELALLOC |
751                                      EXTENT_CLEAR_ACCOUNTING |
752                                      EXTENT_CLEAR_DIRTY |
753                                      EXTENT_SET_WRITEBACK |
754                                      EXTENT_END_WRITEBACK);
755
756                         *nr_written = *nr_written +
757                              (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
758                         *page_started = 1;
759                         ret = 0;
760                         goto out;
761                 }
762         }
763
764         BUG_ON(disk_num_bytes >
765                btrfs_super_total_bytes(&root->fs_info->super_copy));
766
767
768         read_lock(&BTRFS_I(inode)->extent_tree.lock);
769         em = search_extent_mapping(&BTRFS_I(inode)->extent_tree,
770                                    start, num_bytes);
771         if (em) {
772                 /*
773                  * if block start isn't an actual block number then find the
774                  * first block in this inode and use that as a hint.  If that
775                  * block is also bogus then just don't worry about it.
776                  */
777                 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
778                         free_extent_map(em);
779                         em = search_extent_mapping(em_tree, 0, 0);
780                         if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
781                                 alloc_hint = em->block_start;
782                         if (em)
783                                 free_extent_map(em);
784                 } else {
785                         alloc_hint = em->block_start;
786                         free_extent_map(em);
787                 }
788         }
789         read_unlock(&BTRFS_I(inode)->extent_tree.lock);
790         btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
791
792         while (disk_num_bytes > 0) {
793                 unsigned long op;
794
795                 cur_alloc_size = min(disk_num_bytes, root->fs_info->max_extent);
796                 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
797                                            root->sectorsize, 0, alloc_hint,
798                                            (u64)-1, &ins, 1);
799                 BUG_ON(ret);
800
801                 em = alloc_extent_map(GFP_NOFS);
802                 em->start = start;
803                 em->orig_start = em->start;
804                 ram_size = ins.offset;
805                 em->len = ins.offset;
806
807                 em->block_start = ins.objectid;
808                 em->block_len = ins.offset;
809                 em->bdev = root->fs_info->fs_devices->latest_bdev;
810                 set_bit(EXTENT_FLAG_PINNED, &em->flags);
811
812                 while (1) {
813                         write_lock(&em_tree->lock);
814                         ret = add_extent_mapping(em_tree, em);
815                         write_unlock(&em_tree->lock);
816                         if (ret != -EEXIST) {
817                                 free_extent_map(em);
818                                 break;
819                         }
820                         btrfs_drop_extent_cache(inode, start,
821                                                 start + ram_size - 1, 0);
822                 }
823
824                 cur_alloc_size = ins.offset;
825                 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
826                                                ram_size, cur_alloc_size, 0);
827                 BUG_ON(ret);
828
829                 if (root->root_key.objectid ==
830                     BTRFS_DATA_RELOC_TREE_OBJECTID) {
831                         ret = btrfs_reloc_clone_csums(inode, start,
832                                                       cur_alloc_size);
833                         BUG_ON(ret);
834                 }
835
836                 if (disk_num_bytes < cur_alloc_size)
837                         break;
838
839                 /* we're not doing compressed IO, don't unlock the first
840                  * page (which the caller expects to stay locked), don't
841                  * clear any dirty bits and don't set any writeback bits
842                  *
843                  * Do set the Private2 bit so we know this page was properly
844                  * setup for writepage
845                  */
846                 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
847                 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
848                         EXTENT_SET_PRIVATE2;
849
850                 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
851                                              start, start + ram_size - 1,
852                                              locked_page, op);
853                 disk_num_bytes -= cur_alloc_size;
854                 num_bytes -= cur_alloc_size;
855                 alloc_hint = ins.objectid + ins.offset;
856                 start += cur_alloc_size;
857         }
858 out:
859         ret = 0;
860         btrfs_end_transaction(trans, root);
861
862         return ret;
863 }
864
865 /*
866  * work queue call back to started compression on a file and pages
867  */
868 static noinline void async_cow_start(struct btrfs_work *work)
869 {
870         struct async_cow *async_cow;
871         int num_added = 0;
872         async_cow = container_of(work, struct async_cow, work);
873
874         compress_file_range(async_cow->inode, async_cow->locked_page,
875                             async_cow->start, async_cow->end, async_cow,
876                             &num_added);
877         if (num_added == 0)
878                 async_cow->inode = NULL;
879 }
880
881 /*
882  * work queue call back to submit previously compressed pages
883  */
884 static noinline void async_cow_submit(struct btrfs_work *work)
885 {
886         struct async_cow *async_cow;
887         struct btrfs_root *root;
888         unsigned long nr_pages;
889
890         async_cow = container_of(work, struct async_cow, work);
891
892         root = async_cow->root;
893         nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
894                 PAGE_CACHE_SHIFT;
895
896         atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
897
898         if (atomic_read(&root->fs_info->async_delalloc_pages) <
899             5 * 1042 * 1024 &&
900             waitqueue_active(&root->fs_info->async_submit_wait))
901                 wake_up(&root->fs_info->async_submit_wait);
902
903         if (async_cow->inode)
904                 submit_compressed_extents(async_cow->inode, async_cow);
905 }
906
907 static noinline void async_cow_free(struct btrfs_work *work)
908 {
909         struct async_cow *async_cow;
910         async_cow = container_of(work, struct async_cow, work);
911         kfree(async_cow);
912 }
913
914 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
915                                 u64 start, u64 end, int *page_started,
916                                 unsigned long *nr_written)
917 {
918         struct async_cow *async_cow;
919         struct btrfs_root *root = BTRFS_I(inode)->root;
920         unsigned long nr_pages;
921         u64 cur_end;
922         int limit = 10 * 1024 * 1042;
923
924         clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
925                          1, 0, NULL, GFP_NOFS);
926         while (start < end) {
927                 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
928                 async_cow->inode = inode;
929                 async_cow->root = root;
930                 async_cow->locked_page = locked_page;
931                 async_cow->start = start;
932
933                 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
934                         cur_end = end;
935                 else
936                         cur_end = min(end, start + 512 * 1024 - 1);
937
938                 async_cow->end = cur_end;
939                 INIT_LIST_HEAD(&async_cow->extents);
940
941                 async_cow->work.func = async_cow_start;
942                 async_cow->work.ordered_func = async_cow_submit;
943                 async_cow->work.ordered_free = async_cow_free;
944                 async_cow->work.flags = 0;
945
946                 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
947                         PAGE_CACHE_SHIFT;
948                 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
949
950                 btrfs_queue_worker(&root->fs_info->delalloc_workers,
951                                    &async_cow->work);
952
953                 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
954                         wait_event(root->fs_info->async_submit_wait,
955                            (atomic_read(&root->fs_info->async_delalloc_pages) <
956                             limit));
957                 }
958
959                 while (atomic_read(&root->fs_info->async_submit_draining) &&
960                       atomic_read(&root->fs_info->async_delalloc_pages)) {
961                         wait_event(root->fs_info->async_submit_wait,
962                           (atomic_read(&root->fs_info->async_delalloc_pages) ==
963                            0));
964                 }
965
966                 *nr_written += nr_pages;
967                 start = cur_end + 1;
968         }
969         *page_started = 1;
970         return 0;
971 }
972
973 static noinline int csum_exist_in_range(struct btrfs_root *root,
974                                         u64 bytenr, u64 num_bytes)
975 {
976         int ret;
977         struct btrfs_ordered_sum *sums;
978         LIST_HEAD(list);
979
980         ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
981                                        bytenr + num_bytes - 1, &list);
982         if (ret == 0 && list_empty(&list))
983                 return 0;
984
985         while (!list_empty(&list)) {
986                 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
987                 list_del(&sums->list);
988                 kfree(sums);
989         }
990         return 1;
991 }
992
993 /*
994  * when nowcow writeback call back.  This checks for snapshots or COW copies
995  * of the extents that exist in the file, and COWs the file as required.
996  *
997  * If no cow copies or snapshots exist, we write directly to the existing
998  * blocks on disk
999  */
1000 static noinline int run_delalloc_nocow(struct inode *inode,
1001                                        struct page *locked_page,
1002                               u64 start, u64 end, int *page_started, int force,
1003                               unsigned long *nr_written)
1004 {
1005         struct btrfs_root *root = BTRFS_I(inode)->root;
1006         struct btrfs_trans_handle *trans;
1007         struct extent_buffer *leaf;
1008         struct btrfs_path *path;
1009         struct btrfs_file_extent_item *fi;
1010         struct btrfs_key found_key;
1011         u64 cow_start;
1012         u64 cur_offset;
1013         u64 extent_end;
1014         u64 extent_offset;
1015         u64 disk_bytenr;
1016         u64 num_bytes;
1017         int extent_type;
1018         int ret;
1019         int type;
1020         int nocow;
1021         int check_prev = 1;
1022
1023         path = btrfs_alloc_path();
1024         BUG_ON(!path);
1025         trans = btrfs_join_transaction(root, 1);
1026         BUG_ON(!trans);
1027
1028         cow_start = (u64)-1;
1029         cur_offset = start;
1030         while (1) {
1031                 ret = btrfs_lookup_file_extent(trans, root, path, inode->i_ino,
1032                                                cur_offset, 0);
1033                 BUG_ON(ret < 0);
1034                 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1035                         leaf = path->nodes[0];
1036                         btrfs_item_key_to_cpu(leaf, &found_key,
1037                                               path->slots[0] - 1);
1038                         if (found_key.objectid == inode->i_ino &&
1039                             found_key.type == BTRFS_EXTENT_DATA_KEY)
1040                                 path->slots[0]--;
1041                 }
1042                 check_prev = 0;
1043 next_slot:
1044                 leaf = path->nodes[0];
1045                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1046                         ret = btrfs_next_leaf(root, path);
1047                         if (ret < 0)
1048                                 BUG_ON(1);
1049                         if (ret > 0)
1050                                 break;
1051                         leaf = path->nodes[0];
1052                 }
1053
1054                 nocow = 0;
1055                 disk_bytenr = 0;
1056                 num_bytes = 0;
1057                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1058
1059                 if (found_key.objectid > inode->i_ino ||
1060                     found_key.type > BTRFS_EXTENT_DATA_KEY ||
1061                     found_key.offset > end)
1062                         break;
1063
1064                 if (found_key.offset > cur_offset) {
1065                         extent_end = found_key.offset;
1066                         extent_type = 0;
1067                         goto out_check;
1068                 }
1069
1070                 fi = btrfs_item_ptr(leaf, path->slots[0],
1071                                     struct btrfs_file_extent_item);
1072                 extent_type = btrfs_file_extent_type(leaf, fi);
1073
1074                 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1075                     extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1076                         disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1077                         extent_offset = btrfs_file_extent_offset(leaf, fi);
1078                         extent_end = found_key.offset +
1079                                 btrfs_file_extent_num_bytes(leaf, fi);
1080                         if (extent_end <= start) {
1081                                 path->slots[0]++;
1082                                 goto next_slot;
1083                         }
1084                         if (disk_bytenr == 0)
1085                                 goto out_check;
1086                         if (btrfs_file_extent_compression(leaf, fi) ||
1087                             btrfs_file_extent_encryption(leaf, fi) ||
1088                             btrfs_file_extent_other_encoding(leaf, fi))
1089                                 goto out_check;
1090                         if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1091                                 goto out_check;
1092                         if (btrfs_extent_readonly(root, disk_bytenr))
1093                                 goto out_check;
1094                         if (btrfs_cross_ref_exist(trans, root, inode->i_ino,
1095                                                   found_key.offset -
1096                                                   extent_offset, disk_bytenr))
1097                                 goto out_check;
1098                         disk_bytenr += extent_offset;
1099                         disk_bytenr += cur_offset - found_key.offset;
1100                         num_bytes = min(end + 1, extent_end) - cur_offset;
1101                         /*
1102                          * force cow if csum exists in the range.
1103                          * this ensure that csum for a given extent are
1104                          * either valid or do not exist.
1105                          */
1106                         if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1107                                 goto out_check;
1108                         nocow = 1;
1109                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1110                         extent_end = found_key.offset +
1111                                 btrfs_file_extent_inline_len(leaf, fi);
1112                         extent_end = ALIGN(extent_end, root->sectorsize);
1113                 } else {
1114                         BUG_ON(1);
1115                 }
1116 out_check:
1117                 if (extent_end <= start) {
1118                         path->slots[0]++;
1119                         goto next_slot;
1120                 }
1121                 if (!nocow) {
1122                         if (cow_start == (u64)-1)
1123                                 cow_start = cur_offset;
1124                         cur_offset = extent_end;
1125                         if (cur_offset > end)
1126                                 break;
1127                         path->slots[0]++;
1128                         goto next_slot;
1129                 }
1130
1131                 btrfs_release_path(root, path);
1132                 if (cow_start != (u64)-1) {
1133                         ret = cow_file_range(inode, locked_page, cow_start,
1134                                         found_key.offset - 1, page_started,
1135                                         nr_written, 1);
1136                         BUG_ON(ret);
1137                         cow_start = (u64)-1;
1138                 }
1139
1140                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1141                         struct extent_map *em;
1142                         struct extent_map_tree *em_tree;
1143                         em_tree = &BTRFS_I(inode)->extent_tree;
1144                         em = alloc_extent_map(GFP_NOFS);
1145                         em->start = cur_offset;
1146                         em->orig_start = em->start;
1147                         em->len = num_bytes;
1148                         em->block_len = num_bytes;
1149                         em->block_start = disk_bytenr;
1150                         em->bdev = root->fs_info->fs_devices->latest_bdev;
1151                         set_bit(EXTENT_FLAG_PINNED, &em->flags);
1152                         while (1) {
1153                                 write_lock(&em_tree->lock);
1154                                 ret = add_extent_mapping(em_tree, em);
1155                                 write_unlock(&em_tree->lock);
1156                                 if (ret != -EEXIST) {
1157                                         free_extent_map(em);
1158                                         break;
1159                                 }
1160                                 btrfs_drop_extent_cache(inode, em->start,
1161                                                 em->start + em->len - 1, 0);
1162                         }
1163                         type = BTRFS_ORDERED_PREALLOC;
1164                 } else {
1165                         type = BTRFS_ORDERED_NOCOW;
1166                 }
1167
1168                 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1169                                                num_bytes, num_bytes, type);
1170                 BUG_ON(ret);
1171
1172                 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1173                                 cur_offset, cur_offset + num_bytes - 1,
1174                                 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1175                                 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1176                                 EXTENT_SET_PRIVATE2);
1177                 cur_offset = extent_end;
1178                 if (cur_offset > end)
1179                         break;
1180         }
1181         btrfs_release_path(root, path);
1182
1183         if (cur_offset <= end && cow_start == (u64)-1)
1184                 cow_start = cur_offset;
1185         if (cow_start != (u64)-1) {
1186                 ret = cow_file_range(inode, locked_page, cow_start, end,
1187                                      page_started, nr_written, 1);
1188                 BUG_ON(ret);
1189         }
1190
1191         ret = btrfs_end_transaction(trans, root);
1192         BUG_ON(ret);
1193         btrfs_free_path(path);
1194         return 0;
1195 }
1196
1197 /*
1198  * extent_io.c call back to do delayed allocation processing
1199  */
1200 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1201                               u64 start, u64 end, int *page_started,
1202                               unsigned long *nr_written)
1203 {
1204         int ret;
1205         struct btrfs_root *root = BTRFS_I(inode)->root;
1206
1207         if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1208                 ret = run_delalloc_nocow(inode, locked_page, start, end,
1209                                          page_started, 1, nr_written);
1210         else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1211                 ret = run_delalloc_nocow(inode, locked_page, start, end,
1212                                          page_started, 0, nr_written);
1213         else if (!btrfs_test_opt(root, COMPRESS))
1214                 ret = cow_file_range(inode, locked_page, start, end,
1215                                       page_started, nr_written, 1);
1216         else
1217                 ret = cow_file_range_async(inode, locked_page, start, end,
1218                                            page_started, nr_written);
1219         return ret;
1220 }
1221
1222 static int btrfs_split_extent_hook(struct inode *inode,
1223                                     struct extent_state *orig, u64 split)
1224 {
1225         struct btrfs_root *root = BTRFS_I(inode)->root;
1226         u64 size;
1227
1228         if (!(orig->state & EXTENT_DELALLOC))
1229                 return 0;
1230
1231         size = orig->end - orig->start + 1;
1232         if (size > root->fs_info->max_extent) {
1233                 u64 num_extents;
1234                 u64 new_size;
1235
1236                 new_size = orig->end - split + 1;
1237                 num_extents = div64_u64(size + root->fs_info->max_extent - 1,
1238                                         root->fs_info->max_extent);
1239
1240                 /*
1241                  * if we break a large extent up then leave oustanding_extents
1242                  * be, since we've already accounted for the large extent.
1243                  */
1244                 if (div64_u64(new_size + root->fs_info->max_extent - 1,
1245                               root->fs_info->max_extent) < num_extents)
1246                         return 0;
1247         }
1248
1249         spin_lock(&BTRFS_I(inode)->accounting_lock);
1250         BTRFS_I(inode)->outstanding_extents++;
1251         spin_unlock(&BTRFS_I(inode)->accounting_lock);
1252
1253         return 0;
1254 }
1255
1256 /*
1257  * extent_io.c merge_extent_hook, used to track merged delayed allocation
1258  * extents so we can keep track of new extents that are just merged onto old
1259  * extents, such as when we are doing sequential writes, so we can properly
1260  * account for the metadata space we'll need.
1261  */
1262 static int btrfs_merge_extent_hook(struct inode *inode,
1263                                    struct extent_state *new,
1264                                    struct extent_state *other)
1265 {
1266         struct btrfs_root *root = BTRFS_I(inode)->root;
1267         u64 new_size, old_size;
1268         u64 num_extents;
1269
1270         /* not delalloc, ignore it */
1271         if (!(other->state & EXTENT_DELALLOC))
1272                 return 0;
1273
1274         old_size = other->end - other->start + 1;
1275         if (new->start < other->start)
1276                 new_size = other->end - new->start + 1;
1277         else
1278                 new_size = new->end - other->start + 1;
1279
1280         /* we're not bigger than the max, unreserve the space and go */
1281         if (new_size <= root->fs_info->max_extent) {
1282                 spin_lock(&BTRFS_I(inode)->accounting_lock);
1283                 BTRFS_I(inode)->outstanding_extents--;
1284                 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1285                 return 0;
1286         }
1287
1288         /*
1289          * If we grew by another max_extent, just return, we want to keep that
1290          * reserved amount.
1291          */
1292         num_extents = div64_u64(old_size + root->fs_info->max_extent - 1,
1293                                 root->fs_info->max_extent);
1294         if (div64_u64(new_size + root->fs_info->max_extent - 1,
1295                       root->fs_info->max_extent) > num_extents)
1296                 return 0;
1297
1298         spin_lock(&BTRFS_I(inode)->accounting_lock);
1299         BTRFS_I(inode)->outstanding_extents--;
1300         spin_unlock(&BTRFS_I(inode)->accounting_lock);
1301
1302         return 0;
1303 }
1304
1305 /*
1306  * extent_io.c set_bit_hook, used to track delayed allocation
1307  * bytes in this file, and to maintain the list of inodes that
1308  * have pending delalloc work to be done.
1309  */
1310 static int btrfs_set_bit_hook(struct inode *inode, u64 start, u64 end,
1311                        unsigned long old, unsigned long bits)
1312 {
1313
1314         /*
1315          * set_bit and clear bit hooks normally require _irqsave/restore
1316          * but in this case, we are only testeing for the DELALLOC
1317          * bit, which is only set or cleared with irqs on
1318          */
1319         if (!(old & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1320                 struct btrfs_root *root = BTRFS_I(inode)->root;
1321
1322                 spin_lock(&BTRFS_I(inode)->accounting_lock);
1323                 BTRFS_I(inode)->outstanding_extents++;
1324                 spin_unlock(&BTRFS_I(inode)->accounting_lock);
1325                 btrfs_delalloc_reserve_space(root, inode, end - start + 1);
1326                 spin_lock(&root->fs_info->delalloc_lock);
1327                 BTRFS_I(inode)->delalloc_bytes += end - start + 1;
1328                 root->fs_info->delalloc_bytes += end - start + 1;
1329                 if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1330                         list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1331                                       &root->fs_info->delalloc_inodes);
1332                 }
1333                 spin_unlock(&root->fs_info->delalloc_lock);
1334         }
1335         return 0;
1336 }
1337
1338 /*
1339  * extent_io.c clear_bit_hook, see set_bit_hook for why
1340  */
1341 static int btrfs_clear_bit_hook(struct inode *inode,
1342                                 struct extent_state *state, unsigned long bits)
1343 {
1344         /*
1345          * set_bit and clear bit hooks normally require _irqsave/restore
1346          * but in this case, we are only testeing for the DELALLOC
1347          * bit, which is only set or cleared with irqs on
1348          */
1349         if ((state->state & EXTENT_DELALLOC) && (bits & EXTENT_DELALLOC)) {
1350                 struct btrfs_root *root = BTRFS_I(inode)->root;
1351
1352                 if (bits & EXTENT_DO_ACCOUNTING) {
1353                         spin_lock(&BTRFS_I(inode)->accounting_lock);
1354                         BTRFS_I(inode)->outstanding_extents--;
1355                         spin_unlock(&BTRFS_I(inode)->accounting_lock);
1356                         btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
1357                 }
1358
1359                 spin_lock(&root->fs_info->delalloc_lock);
1360                 if (state->end - state->start + 1 >
1361                     root->fs_info->delalloc_bytes) {
1362                         printk(KERN_INFO "btrfs warning: delalloc account "
1363                                "%llu %llu\n",
1364                                (unsigned long long)
1365                                state->end - state->start + 1,
1366                                (unsigned long long)
1367                                root->fs_info->delalloc_bytes);
1368                         btrfs_delalloc_free_space(root, inode, (u64)-1);
1369                         root->fs_info->delalloc_bytes = 0;
1370                         BTRFS_I(inode)->delalloc_bytes = 0;
1371                 } else {
1372                         btrfs_delalloc_free_space(root, inode,
1373                                                   state->end -
1374                                                   state->start + 1);
1375                         root->fs_info->delalloc_bytes -= state->end -
1376                                 state->start + 1;
1377                         BTRFS_I(inode)->delalloc_bytes -= state->end -
1378                                 state->start + 1;
1379                 }
1380                 if (BTRFS_I(inode)->delalloc_bytes == 0 &&
1381                     !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1382                         list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1383                 }
1384                 spin_unlock(&root->fs_info->delalloc_lock);
1385         }
1386         return 0;
1387 }
1388
1389 /*
1390  * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1391  * we don't create bios that span stripes or chunks
1392  */
1393 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1394                          size_t size, struct bio *bio,
1395                          unsigned long bio_flags)
1396 {
1397         struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1398         struct btrfs_mapping_tree *map_tree;
1399         u64 logical = (u64)bio->bi_sector << 9;
1400         u64 length = 0;
1401         u64 map_length;
1402         int ret;
1403
1404         if (bio_flags & EXTENT_BIO_COMPRESSED)
1405                 return 0;
1406
1407         length = bio->bi_size;
1408         map_tree = &root->fs_info->mapping_tree;
1409         map_length = length;
1410         ret = btrfs_map_block(map_tree, READ, logical,
1411                               &map_length, NULL, 0);
1412
1413         if (map_length < length + size)
1414                 return 1;
1415         return 0;
1416 }
1417
1418 /*
1419  * in order to insert checksums into the metadata in large chunks,
1420  * we wait until bio submission time.   All the pages in the bio are
1421  * checksummed and sums are attached onto the ordered extent record.
1422  *
1423  * At IO completion time the cums attached on the ordered extent record
1424  * are inserted into the btree
1425  */
1426 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1427                                     struct bio *bio, int mirror_num,
1428                                     unsigned long bio_flags)
1429 {
1430         struct btrfs_root *root = BTRFS_I(inode)->root;
1431         int ret = 0;
1432
1433         ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1434         BUG_ON(ret);
1435         return 0;
1436 }
1437
1438 /*
1439  * in order to insert checksums into the metadata in large chunks,
1440  * we wait until bio submission time.   All the pages in the bio are
1441  * checksummed and sums are attached onto the ordered extent record.
1442  *
1443  * At IO completion time the cums attached on the ordered extent record
1444  * are inserted into the btree
1445  */
1446 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1447                           int mirror_num, unsigned long bio_flags)
1448 {
1449         struct btrfs_root *root = BTRFS_I(inode)->root;
1450         return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1451 }
1452
1453 /*
1454  * extent_io.c submission hook. This does the right thing for csum calculation
1455  * on write, or reading the csums from the tree before a read
1456  */
1457 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1458                           int mirror_num, unsigned long bio_flags)
1459 {
1460         struct btrfs_root *root = BTRFS_I(inode)->root;
1461         int ret = 0;
1462         int skip_sum;
1463
1464         skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1465
1466         ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1467         BUG_ON(ret);
1468
1469         if (!(rw & (1 << BIO_RW))) {
1470                 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1471                         return btrfs_submit_compressed_read(inode, bio,
1472                                                     mirror_num, bio_flags);
1473                 } else if (!skip_sum)
1474                         btrfs_lookup_bio_sums(root, inode, bio, NULL);
1475                 goto mapit;
1476         } else if (!skip_sum) {
1477                 /* csum items have already been cloned */
1478                 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1479                         goto mapit;
1480                 /* we're doing a write, do the async checksumming */
1481                 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1482                                    inode, rw, bio, mirror_num,
1483                                    bio_flags, __btrfs_submit_bio_start,
1484                                    __btrfs_submit_bio_done);
1485         }
1486
1487 mapit:
1488         return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1489 }
1490
1491 /*
1492  * given a list of ordered sums record them in the inode.  This happens
1493  * at IO completion time based on sums calculated at bio submission time.
1494  */
1495 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1496                              struct inode *inode, u64 file_offset,
1497                              struct list_head *list)
1498 {
1499         struct btrfs_ordered_sum *sum;
1500
1501         btrfs_set_trans_block_group(trans, inode);
1502
1503         list_for_each_entry(sum, list, list) {
1504                 btrfs_csum_file_blocks(trans,
1505                        BTRFS_I(inode)->root->fs_info->csum_root, sum);
1506         }
1507         return 0;
1508 }
1509
1510 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end)
1511 {
1512         if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1513                 WARN_ON(1);
1514         return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1515                                    GFP_NOFS);
1516 }
1517
1518 /* see btrfs_writepage_start_hook for details on why this is required */
1519 struct btrfs_writepage_fixup {
1520         struct page *page;
1521         struct btrfs_work work;
1522 };
1523
1524 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1525 {
1526         struct btrfs_writepage_fixup *fixup;
1527         struct btrfs_ordered_extent *ordered;
1528         struct page *page;
1529         struct inode *inode;
1530         u64 page_start;
1531         u64 page_end;
1532
1533         fixup = container_of(work, struct btrfs_writepage_fixup, work);
1534         page = fixup->page;
1535 again:
1536         lock_page(page);
1537         if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1538                 ClearPageChecked(page);
1539                 goto out_page;
1540         }
1541
1542         inode = page->mapping->host;
1543         page_start = page_offset(page);
1544         page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1545
1546         lock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1547
1548         /* already ordered? We're done */
1549         if (PagePrivate2(page))
1550                 goto out;
1551
1552         ordered = btrfs_lookup_ordered_extent(inode, page_start);
1553         if (ordered) {
1554                 unlock_extent(&BTRFS_I(inode)->io_tree, page_start,
1555                               page_end, GFP_NOFS);
1556                 unlock_page(page);
1557                 btrfs_start_ordered_extent(inode, ordered, 1);
1558                 goto again;
1559         }
1560
1561         btrfs_set_extent_delalloc(inode, page_start, page_end);
1562         ClearPageChecked(page);
1563 out:
1564         unlock_extent(&BTRFS_I(inode)->io_tree, page_start, page_end, GFP_NOFS);
1565 out_page:
1566         unlock_page(page);
1567         page_cache_release(page);
1568 }
1569
1570 /*
1571  * There are a few paths in the higher layers of the kernel that directly
1572  * set the page dirty bit without asking the filesystem if it is a
1573  * good idea.  This causes problems because we want to make sure COW
1574  * properly happens and the data=ordered rules are followed.
1575  *
1576  * In our case any range that doesn't have the ORDERED bit set
1577  * hasn't been properly setup for IO.  We kick off an async process
1578  * to fix it up.  The async helper will wait for ordered extents, set
1579  * the delalloc bit and make it safe to write the page.
1580  */
1581 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1582 {
1583         struct inode *inode = page->mapping->host;
1584         struct btrfs_writepage_fixup *fixup;
1585         struct btrfs_root *root = BTRFS_I(inode)->root;
1586
1587         /* this page is properly in the ordered list */
1588         if (TestClearPagePrivate2(page))
1589                 return 0;
1590
1591         if (PageChecked(page))
1592                 return -EAGAIN;
1593
1594         fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1595         if (!fixup)
1596                 return -EAGAIN;
1597
1598         SetPageChecked(page);
1599         page_cache_get(page);
1600         fixup->work.func = btrfs_writepage_fixup_worker;
1601         fixup->page = page;
1602         btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1603         return -EAGAIN;
1604 }
1605
1606 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1607                                        struct inode *inode, u64 file_pos,
1608                                        u64 disk_bytenr, u64 disk_num_bytes,
1609                                        u64 num_bytes, u64 ram_bytes,
1610                                        u8 compression, u8 encryption,
1611                                        u16 other_encoding, int extent_type)
1612 {
1613         struct btrfs_root *root = BTRFS_I(inode)->root;
1614         struct btrfs_file_extent_item *fi;
1615         struct btrfs_path *path;
1616         struct extent_buffer *leaf;
1617         struct btrfs_key ins;
1618         u64 hint;
1619         int ret;
1620
1621         path = btrfs_alloc_path();
1622         BUG_ON(!path);
1623
1624         path->leave_spinning = 1;
1625
1626         /*
1627          * we may be replacing one extent in the tree with another.
1628          * The new extent is pinned in the extent map, and we don't want
1629          * to drop it from the cache until it is completely in the btree.
1630          *
1631          * So, tell btrfs_drop_extents to leave this extent in the cache.
1632          * the caller is expected to unpin it and allow it to be merged
1633          * with the others.
1634          */
1635         ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1636                                  &hint, 0);
1637         BUG_ON(ret);
1638
1639         ins.objectid = inode->i_ino;
1640         ins.offset = file_pos;
1641         ins.type = BTRFS_EXTENT_DATA_KEY;
1642         ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1643         BUG_ON(ret);
1644         leaf = path->nodes[0];
1645         fi = btrfs_item_ptr(leaf, path->slots[0],
1646                             struct btrfs_file_extent_item);
1647         btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1648         btrfs_set_file_extent_type(leaf, fi, extent_type);
1649         btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1650         btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1651         btrfs_set_file_extent_offset(leaf, fi, 0);
1652         btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1653         btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1654         btrfs_set_file_extent_compression(leaf, fi, compression);
1655         btrfs_set_file_extent_encryption(leaf, fi, encryption);
1656         btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1657
1658         btrfs_unlock_up_safe(path, 1);
1659         btrfs_set_lock_blocking(leaf);
1660
1661         btrfs_mark_buffer_dirty(leaf);
1662
1663         inode_add_bytes(inode, num_bytes);
1664
1665         ins.objectid = disk_bytenr;
1666         ins.offset = disk_num_bytes;
1667         ins.type = BTRFS_EXTENT_ITEM_KEY;
1668         ret = btrfs_alloc_reserved_file_extent(trans, root,
1669                                         root->root_key.objectid,
1670                                         inode->i_ino, file_pos, &ins);
1671         BUG_ON(ret);
1672         btrfs_free_path(path);
1673
1674         return 0;
1675 }
1676
1677 /*
1678  * helper function for btrfs_finish_ordered_io, this
1679  * just reads in some of the csum leaves to prime them into ram
1680  * before we start the transaction.  It limits the amount of btree
1681  * reads required while inside the transaction.
1682  */
1683 /* as ordered data IO finishes, this gets called so we can finish
1684  * an ordered extent if the range of bytes in the file it covers are
1685  * fully written.
1686  */
1687 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1688 {
1689         struct btrfs_root *root = BTRFS_I(inode)->root;
1690         struct btrfs_trans_handle *trans;
1691         struct btrfs_ordered_extent *ordered_extent = NULL;
1692         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1693         int compressed = 0;
1694         int ret;
1695
1696         ret = btrfs_dec_test_ordered_pending(inode, start, end - start + 1);
1697         if (!ret)
1698                 return 0;
1699
1700         ordered_extent = btrfs_lookup_ordered_extent(inode, start);
1701         BUG_ON(!ordered_extent);
1702
1703         if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1704                 BUG_ON(!list_empty(&ordered_extent->list));
1705                 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1706                 if (!ret) {
1707                         trans = btrfs_join_transaction(root, 1);
1708                         ret = btrfs_update_inode(trans, root, inode);
1709                         BUG_ON(ret);
1710                         btrfs_end_transaction(trans, root);
1711                 }
1712                 goto out;
1713         }
1714
1715         lock_extent(io_tree, ordered_extent->file_offset,
1716                     ordered_extent->file_offset + ordered_extent->len - 1,
1717                     GFP_NOFS);
1718
1719         trans = btrfs_join_transaction(root, 1);
1720
1721         if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1722                 compressed = 1;
1723         if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1724                 BUG_ON(compressed);
1725                 ret = btrfs_mark_extent_written(trans, inode,
1726                                                 ordered_extent->file_offset,
1727                                                 ordered_extent->file_offset +
1728                                                 ordered_extent->len);
1729                 BUG_ON(ret);
1730         } else {
1731                 ret = insert_reserved_file_extent(trans, inode,
1732                                                 ordered_extent->file_offset,
1733                                                 ordered_extent->start,
1734                                                 ordered_extent->disk_len,
1735                                                 ordered_extent->len,
1736                                                 ordered_extent->len,
1737                                                 compressed, 0, 0,
1738                                                 BTRFS_FILE_EXTENT_REG);
1739                 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1740                                    ordered_extent->file_offset,
1741                                    ordered_extent->len);
1742                 BUG_ON(ret);
1743         }
1744         unlock_extent(io_tree, ordered_extent->file_offset,
1745                     ordered_extent->file_offset + ordered_extent->len - 1,
1746                     GFP_NOFS);
1747         add_pending_csums(trans, inode, ordered_extent->file_offset,
1748                           &ordered_extent->list);
1749
1750         /* this also removes the ordered extent from the tree */
1751         btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1752         ret = btrfs_update_inode(trans, root, inode);
1753         BUG_ON(ret);
1754         btrfs_end_transaction(trans, root);
1755 out:
1756         /* once for us */
1757         btrfs_put_ordered_extent(ordered_extent);
1758         /* once for the tree */
1759         btrfs_put_ordered_extent(ordered_extent);
1760
1761         return 0;
1762 }
1763
1764 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1765                                 struct extent_state *state, int uptodate)
1766 {
1767         ClearPagePrivate2(page);
1768         return btrfs_finish_ordered_io(page->mapping->host, start, end);
1769 }
1770
1771 /*
1772  * When IO fails, either with EIO or csum verification fails, we
1773  * try other mirrors that might have a good copy of the data.  This
1774  * io_failure_record is used to record state as we go through all the
1775  * mirrors.  If another mirror has good data, the page is set up to date
1776  * and things continue.  If a good mirror can't be found, the original
1777  * bio end_io callback is called to indicate things have failed.
1778  */
1779 struct io_failure_record {
1780         struct page *page;
1781         u64 start;
1782         u64 len;
1783         u64 logical;
1784         unsigned long bio_flags;
1785         int last_mirror;
1786 };
1787
1788 static int btrfs_io_failed_hook(struct bio *failed_bio,
1789                          struct page *page, u64 start, u64 end,
1790                          struct extent_state *state)
1791 {
1792         struct io_failure_record *failrec = NULL;
1793         u64 private;
1794         struct extent_map *em;
1795         struct inode *inode = page->mapping->host;
1796         struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1797         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1798         struct bio *bio;
1799         int num_copies;
1800         int ret;
1801         int rw;
1802         u64 logical;
1803
1804         ret = get_state_private(failure_tree, start, &private);
1805         if (ret) {
1806                 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1807                 if (!failrec)
1808                         return -ENOMEM;
1809                 failrec->start = start;
1810                 failrec->len = end - start + 1;
1811                 failrec->last_mirror = 0;
1812                 failrec->bio_flags = 0;
1813
1814                 read_lock(&em_tree->lock);
1815                 em = lookup_extent_mapping(em_tree, start, failrec->len);
1816                 if (em->start > start || em->start + em->len < start) {
1817                         free_extent_map(em);
1818                         em = NULL;
1819                 }
1820                 read_unlock(&em_tree->lock);
1821
1822                 if (!em || IS_ERR(em)) {
1823                         kfree(failrec);
1824                         return -EIO;
1825                 }
1826                 logical = start - em->start;
1827                 logical = em->block_start + logical;
1828                 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1829                         logical = em->block_start;
1830                         failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1831                 }
1832                 failrec->logical = logical;
1833                 free_extent_map(em);
1834                 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1835                                 EXTENT_DIRTY, GFP_NOFS);
1836                 set_state_private(failure_tree, start,
1837                                  (u64)(unsigned long)failrec);
1838         } else {
1839                 failrec = (struct io_failure_record *)(unsigned long)private;
1840         }
1841         num_copies = btrfs_num_copies(
1842                               &BTRFS_I(inode)->root->fs_info->mapping_tree,
1843                               failrec->logical, failrec->len);
1844         failrec->last_mirror++;
1845         if (!state) {
1846                 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1847                 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1848                                                     failrec->start,
1849                                                     EXTENT_LOCKED);
1850                 if (state && state->start != failrec->start)
1851                         state = NULL;
1852                 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1853         }
1854         if (!state || failrec->last_mirror > num_copies) {
1855                 set_state_private(failure_tree, failrec->start, 0);
1856                 clear_extent_bits(failure_tree, failrec->start,
1857                                   failrec->start + failrec->len - 1,
1858                                   EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1859                 kfree(failrec);
1860                 return -EIO;
1861         }
1862         bio = bio_alloc(GFP_NOFS, 1);
1863         bio->bi_private = state;
1864         bio->bi_end_io = failed_bio->bi_end_io;
1865         bio->bi_sector = failrec->logical >> 9;
1866         bio->bi_bdev = failed_bio->bi_bdev;
1867         bio->bi_size = 0;
1868
1869         bio_add_page(bio, page, failrec->len, start - page_offset(page));
1870         if (failed_bio->bi_rw & (1 << BIO_RW))
1871                 rw = WRITE;
1872         else
1873                 rw = READ;
1874
1875         BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1876                                                       failrec->last_mirror,
1877                                                       failrec->bio_flags);
1878         return 0;
1879 }
1880
1881 /*
1882  * each time an IO finishes, we do a fast check in the IO failure tree
1883  * to see if we need to process or clean up an io_failure_record
1884  */
1885 static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1886 {
1887         u64 private;
1888         u64 private_failure;
1889         struct io_failure_record *failure;
1890         int ret;
1891
1892         private = 0;
1893         if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1894                              (u64)-1, 1, EXTENT_DIRTY)) {
1895                 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1896                                         start, &private_failure);
1897                 if (ret == 0) {
1898                         failure = (struct io_failure_record *)(unsigned long)
1899                                    private_failure;
1900                         set_state_private(&BTRFS_I(inode)->io_failure_tree,
1901                                           failure->start, 0);
1902                         clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1903                                           failure->start,
1904                                           failure->start + failure->len - 1,
1905                                           EXTENT_DIRTY | EXTENT_LOCKED,
1906                                           GFP_NOFS);
1907                         kfree(failure);
1908                 }
1909         }
1910         return 0;
1911 }
1912
1913 /*
1914  * when reads are done, we need to check csums to verify the data is correct
1915  * if there's a match, we allow the bio to finish.  If not, we go through
1916  * the io_failure_record routines to find good copies
1917  */
1918 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1919                                struct extent_state *state)
1920 {
1921         size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1922         struct inode *inode = page->mapping->host;
1923         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1924         char *kaddr;
1925         u64 private = ~(u32)0;
1926         int ret;
1927         struct btrfs_root *root = BTRFS_I(inode)->root;
1928         u32 csum = ~(u32)0;
1929
1930         if (PageChecked(page)) {
1931                 ClearPageChecked(page);
1932                 goto good;
1933         }
1934
1935         if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1936                 return 0;
1937
1938         if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1939             test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1940                 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1941                                   GFP_NOFS);
1942                 return 0;
1943         }
1944
1945         if (state && state->start == start) {
1946                 private = state->private;
1947                 ret = 0;
1948         } else {
1949                 ret = get_state_private(io_tree, start, &private);
1950         }
1951         kaddr = kmap_atomic(page, KM_USER0);
1952         if (ret)
1953                 goto zeroit;
1954
1955         csum = btrfs_csum_data(root, kaddr + offset, csum,  end - start + 1);
1956         btrfs_csum_final(csum, (char *)&csum);
1957         if (csum != private)
1958                 goto zeroit;
1959
1960         kunmap_atomic(kaddr, KM_USER0);
1961 good:
1962         /* if the io failure tree for this inode is non-empty,
1963          * check to see if we've recovered from a failed IO
1964          */
1965         btrfs_clean_io_failures(inode, start);
1966         return 0;
1967
1968 zeroit:
1969         if (printk_ratelimit()) {
1970                 printk(KERN_INFO "btrfs csum failed ino %lu off %llu csum %u "
1971                        "private %llu\n", page->mapping->host->i_ino,
1972                        (unsigned long long)start, csum,
1973                        (unsigned long long)private);
1974         }
1975         memset(kaddr + offset, 1, end - start + 1);
1976         flush_dcache_page(page);
1977         kunmap_atomic(kaddr, KM_USER0);
1978         if (private == 0)
1979                 return 0;
1980         return -EIO;
1981 }
1982
1983 struct delayed_iput {
1984         struct list_head list;
1985         struct inode *inode;
1986 };
1987
1988 void btrfs_add_delayed_iput(struct inode *inode)
1989 {
1990         struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
1991         struct delayed_iput *delayed;
1992
1993         if (atomic_add_unless(&inode->i_count, -1, 1))
1994                 return;
1995
1996         delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
1997         delayed->inode = inode;
1998
1999         spin_lock(&fs_info->delayed_iput_lock);
2000         list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2001         spin_unlock(&fs_info->delayed_iput_lock);
2002 }
2003
2004 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2005 {
2006         LIST_HEAD(list);
2007         struct btrfs_fs_info *fs_info = root->fs_info;
2008         struct delayed_iput *delayed;
2009         int empty;
2010
2011         spin_lock(&fs_info->delayed_iput_lock);
2012         empty = list_empty(&fs_info->delayed_iputs);
2013         spin_unlock(&fs_info->delayed_iput_lock);
2014         if (empty)
2015                 return;
2016
2017         down_read(&root->fs_info->cleanup_work_sem);
2018         spin_lock(&fs_info->delayed_iput_lock);
2019         list_splice_init(&fs_info->delayed_iputs, &list);
2020         spin_unlock(&fs_info->delayed_iput_lock);
2021
2022         while (!list_empty(&list)) {
2023                 delayed = list_entry(list.next, struct delayed_iput, list);
2024                 list_del(&delayed->list);
2025                 iput(delayed->inode);
2026                 kfree(delayed);
2027         }
2028         up_read(&root->fs_info->cleanup_work_sem);
2029 }
2030
2031 /*
2032  * This creates an orphan entry for the given inode in case something goes
2033  * wrong in the middle of an unlink/truncate.
2034  */
2035 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2036 {
2037         struct btrfs_root *root = BTRFS_I(inode)->root;
2038         int ret = 0;
2039
2040         spin_lock(&root->list_lock);
2041
2042         /* already on the orphan list, we're good */
2043         if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2044                 spin_unlock(&root->list_lock);
2045                 return 0;
2046         }
2047
2048         list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2049
2050         spin_unlock(&root->list_lock);
2051
2052         /*
2053          * insert an orphan item to track this unlinked/truncated file
2054          */
2055         ret = btrfs_insert_orphan_item(trans, root, inode->i_ino);
2056
2057         return ret;
2058 }
2059
2060 /*
2061  * We have done the truncate/delete so we can go ahead and remove the orphan
2062  * item for this particular inode.
2063  */
2064 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2065 {
2066         struct btrfs_root *root = BTRFS_I(inode)->root;
2067         int ret = 0;
2068
2069         spin_lock(&root->list_lock);
2070
2071         if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2072                 spin_unlock(&root->list_lock);
2073                 return 0;
2074         }
2075
2076         list_del_init(&BTRFS_I(inode)->i_orphan);
2077         if (!trans) {
2078                 spin_unlock(&root->list_lock);
2079                 return 0;
2080         }
2081
2082         spin_unlock(&root->list_lock);
2083
2084         ret = btrfs_del_orphan_item(trans, root, inode->i_ino);
2085
2086         return ret;
2087 }
2088
2089 /*
2090  * this cleans up any orphans that may be left on the list from the last use
2091  * of this root.
2092  */
2093 void btrfs_orphan_cleanup(struct btrfs_root *root)
2094 {
2095         struct btrfs_path *path;
2096         struct extent_buffer *leaf;
2097         struct btrfs_item *item;
2098         struct btrfs_key key, found_key;
2099         struct btrfs_trans_handle *trans;
2100         struct inode *inode;
2101         int ret = 0, nr_unlink = 0, nr_truncate = 0;
2102
2103         if (!xchg(&root->clean_orphans, 0))
2104                 return;
2105
2106         path = btrfs_alloc_path();
2107         BUG_ON(!path);
2108         path->reada = -1;
2109
2110         key.objectid = BTRFS_ORPHAN_OBJECTID;
2111         btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2112         key.offset = (u64)-1;
2113
2114         while (1) {
2115                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2116                 if (ret < 0) {
2117                         printk(KERN_ERR "Error searching slot for orphan: %d"
2118                                "\n", ret);
2119                         break;
2120                 }
2121
2122                 /*
2123                  * if ret == 0 means we found what we were searching for, which
2124                  * is weird, but possible, so only screw with path if we didnt
2125                  * find the key and see if we have stuff that matches
2126                  */
2127                 if (ret > 0) {
2128                         if (path->slots[0] == 0)
2129                                 break;
2130                         path->slots[0]--;
2131                 }
2132
2133                 /* pull out the item */
2134                 leaf = path->nodes[0];
2135                 item = btrfs_item_nr(leaf, path->slots[0]);
2136                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2137
2138                 /* make sure the item matches what we want */
2139                 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2140                         break;
2141                 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2142                         break;
2143
2144                 /* release the path since we're done with it */
2145                 btrfs_release_path(root, path);
2146
2147                 /*
2148                  * this is where we are basically btrfs_lookup, without the
2149                  * crossing root thing.  we store the inode number in the
2150                  * offset of the orphan item.
2151                  */
2152                 found_key.objectid = found_key.offset;
2153                 found_key.type = BTRFS_INODE_ITEM_KEY;
2154                 found_key.offset = 0;
2155                 inode = btrfs_iget(root->fs_info->sb, &found_key, root);
2156                 if (IS_ERR(inode))
2157                         break;
2158
2159                 /*
2160                  * add this inode to the orphan list so btrfs_orphan_del does
2161                  * the proper thing when we hit it
2162                  */
2163                 spin_lock(&root->list_lock);
2164                 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2165                 spin_unlock(&root->list_lock);
2166
2167                 /*
2168                  * if this is a bad inode, means we actually succeeded in
2169                  * removing the inode, but not the orphan record, which means
2170                  * we need to manually delete the orphan since iput will just
2171                  * do a destroy_inode
2172                  */
2173                 if (is_bad_inode(inode)) {
2174                         trans = btrfs_start_transaction(root, 1);
2175                         btrfs_orphan_del(trans, inode);
2176                         btrfs_end_transaction(trans, root);
2177                         iput(inode);
2178                         continue;
2179                 }
2180
2181                 /* if we have links, this was a truncate, lets do that */
2182                 if (inode->i_nlink) {
2183                         nr_truncate++;
2184                         btrfs_truncate(inode);
2185                 } else {
2186                         nr_unlink++;
2187                 }
2188
2189                 /* this will do delete_inode and everything for us */
2190                 iput(inode);
2191         }
2192
2193         if (nr_unlink)
2194                 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2195         if (nr_truncate)
2196                 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2197
2198         btrfs_free_path(path);
2199 }
2200
2201 /*
2202  * very simple check to peek ahead in the leaf looking for xattrs.  If we
2203  * don't find any xattrs, we know there can't be any acls.
2204  *
2205  * slot is the slot the inode is in, objectid is the objectid of the inode
2206  */
2207 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2208                                           int slot, u64 objectid)
2209 {
2210         u32 nritems = btrfs_header_nritems(leaf);
2211         struct btrfs_key found_key;
2212         int scanned = 0;
2213
2214         slot++;
2215         while (slot < nritems) {
2216                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2217
2218                 /* we found a different objectid, there must not be acls */
2219                 if (found_key.objectid != objectid)
2220                         return 0;
2221
2222                 /* we found an xattr, assume we've got an acl */
2223                 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2224                         return 1;
2225
2226                 /*
2227                  * we found a key greater than an xattr key, there can't
2228                  * be any acls later on
2229                  */
2230                 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2231                         return 0;
2232
2233                 slot++;
2234                 scanned++;
2235
2236                 /*
2237                  * it goes inode, inode backrefs, xattrs, extents,
2238                  * so if there are a ton of hard links to an inode there can
2239                  * be a lot of backrefs.  Don't waste time searching too hard,
2240                  * this is just an optimization
2241                  */
2242                 if (scanned >= 8)
2243                         break;
2244         }
2245         /* we hit the end of the leaf before we found an xattr or
2246          * something larger than an xattr.  We have to assume the inode
2247          * has acls
2248          */
2249         return 1;
2250 }
2251
2252 /*
2253  * read an inode from the btree into the in-memory inode
2254  */
2255 static void btrfs_read_locked_inode(struct inode *inode)
2256 {
2257         struct btrfs_path *path;
2258         struct extent_buffer *leaf;
2259         struct btrfs_inode_item *inode_item;
2260         struct btrfs_timespec *tspec;
2261         struct btrfs_root *root = BTRFS_I(inode)->root;
2262         struct btrfs_key location;
2263         int maybe_acls;
2264         u64 alloc_group_block;
2265         u32 rdev;
2266         int ret;
2267
2268         path = btrfs_alloc_path();
2269         BUG_ON(!path);
2270         memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2271
2272         ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2273         if (ret)
2274                 goto make_bad;
2275
2276         leaf = path->nodes[0];
2277         inode_item = btrfs_item_ptr(leaf, path->slots[0],
2278                                     struct btrfs_inode_item);
2279
2280         inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2281         inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2282         inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2283         inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2284         btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2285
2286         tspec = btrfs_inode_atime(inode_item);
2287         inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2288         inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2289
2290         tspec = btrfs_inode_mtime(inode_item);
2291         inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2292         inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2293
2294         tspec = btrfs_inode_ctime(inode_item);
2295         inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2296         inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2297
2298         inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2299         BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2300         BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2301         inode->i_generation = BTRFS_I(inode)->generation;
2302         inode->i_rdev = 0;
2303         rdev = btrfs_inode_rdev(leaf, inode_item);
2304
2305         BTRFS_I(inode)->index_cnt = (u64)-1;
2306         BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2307
2308         alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2309
2310         /*
2311          * try to precache a NULL acl entry for files that don't have
2312          * any xattrs or acls
2313          */
2314         maybe_acls = acls_after_inode_item(leaf, path->slots[0], inode->i_ino);
2315         if (!maybe_acls)
2316                 cache_no_acl(inode);
2317
2318         BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2319                                                 alloc_group_block, 0);
2320         btrfs_free_path(path);
2321         inode_item = NULL;
2322
2323         switch (inode->i_mode & S_IFMT) {
2324         case S_IFREG:
2325                 inode->i_mapping->a_ops = &btrfs_aops;
2326                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2327                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2328                 inode->i_fop = &btrfs_file_operations;
2329                 inode->i_op = &btrfs_file_inode_operations;
2330                 break;
2331         case S_IFDIR:
2332                 inode->i_fop = &btrfs_dir_file_operations;
2333                 if (root == root->fs_info->tree_root)
2334                         inode->i_op = &btrfs_dir_ro_inode_operations;
2335                 else
2336                         inode->i_op = &btrfs_dir_inode_operations;
2337                 break;
2338         case S_IFLNK:
2339                 inode->i_op = &btrfs_symlink_inode_operations;
2340                 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2341                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2342                 break;
2343         default:
2344                 inode->i_op = &btrfs_special_inode_operations;
2345                 init_special_inode(inode, inode->i_mode, rdev);
2346                 break;
2347         }
2348
2349         btrfs_update_iflags(inode);
2350         return;
2351
2352 make_bad:
2353         btrfs_free_path(path);
2354         make_bad_inode(inode);
2355 }
2356
2357 /*
2358  * given a leaf and an inode, copy the inode fields into the leaf
2359  */
2360 static void fill_inode_item(struct btrfs_trans_handle *trans,
2361                             struct extent_buffer *leaf,
2362                             struct btrfs_inode_item *item,
2363                             struct inode *inode)
2364 {
2365         btrfs_set_inode_uid(leaf, item, inode->i_uid);
2366         btrfs_set_inode_gid(leaf, item, inode->i_gid);
2367         btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2368         btrfs_set_inode_mode(leaf, item, inode->i_mode);
2369         btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2370
2371         btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2372                                inode->i_atime.tv_sec);
2373         btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2374                                 inode->i_atime.tv_nsec);
2375
2376         btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2377                                inode->i_mtime.tv_sec);
2378         btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2379                                 inode->i_mtime.tv_nsec);
2380
2381         btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2382                                inode->i_ctime.tv_sec);
2383         btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2384                                 inode->i_ctime.tv_nsec);
2385
2386         btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2387         btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2388         btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2389         btrfs_set_inode_transid(leaf, item, trans->transid);
2390         btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2391         btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2392         btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
2393 }
2394
2395 /*
2396  * copy everything in the in-memory inode into the btree.
2397  */
2398 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2399                                 struct btrfs_root *root, struct inode *inode)
2400 {
2401         struct btrfs_inode_item *inode_item;
2402         struct btrfs_path *path;
2403         struct extent_buffer *leaf;
2404         int ret;
2405
2406         path = btrfs_alloc_path();
2407         BUG_ON(!path);
2408         path->leave_spinning = 1;
2409         ret = btrfs_lookup_inode(trans, root, path,
2410                                  &BTRFS_I(inode)->location, 1);
2411         if (ret) {
2412                 if (ret > 0)
2413                         ret = -ENOENT;
2414                 goto failed;
2415         }
2416
2417         btrfs_unlock_up_safe(path, 1);
2418         leaf = path->nodes[0];
2419         inode_item = btrfs_item_ptr(leaf, path->slots[0],
2420                                   struct btrfs_inode_item);
2421
2422         fill_inode_item(trans, leaf, inode_item, inode);
2423         btrfs_mark_buffer_dirty(leaf);
2424         btrfs_set_inode_last_trans(trans, inode);
2425         ret = 0;
2426 failed:
2427         btrfs_free_path(path);
2428         return ret;
2429 }
2430
2431
2432 /*
2433  * unlink helper that gets used here in inode.c and in the tree logging
2434  * recovery code.  It remove a link in a directory with a given name, and
2435  * also drops the back refs in the inode to the directory
2436  */
2437 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2438                        struct btrfs_root *root,
2439                        struct inode *dir, struct inode *inode,
2440                        const char *name, int name_len)
2441 {
2442         struct btrfs_path *path;
2443         int ret = 0;
2444         struct extent_buffer *leaf;
2445         struct btrfs_dir_item *di;
2446         struct btrfs_key key;
2447         u64 index;
2448
2449         path = btrfs_alloc_path();
2450         if (!path) {
2451                 ret = -ENOMEM;
2452                 goto err;
2453         }
2454
2455         path->leave_spinning = 1;
2456         di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2457                                     name, name_len, -1);
2458         if (IS_ERR(di)) {
2459                 ret = PTR_ERR(di);
2460                 goto err;
2461         }
2462         if (!di) {
2463                 ret = -ENOENT;
2464                 goto err;
2465         }
2466         leaf = path->nodes[0];
2467         btrfs_dir_item_key_to_cpu(leaf, di, &key);
2468         ret = btrfs_delete_one_dir_name(trans, root, path, di);
2469         if (ret)
2470                 goto err;
2471         btrfs_release_path(root, path);
2472
2473         ret = btrfs_del_inode_ref(trans, root, name, name_len,
2474                                   inode->i_ino,
2475                                   dir->i_ino, &index);
2476         if (ret) {
2477                 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2478                        "inode %lu parent %lu\n", name_len, name,
2479                        inode->i_ino, dir->i_ino);
2480                 goto err;
2481         }
2482
2483         di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2484                                          index, name, name_len, -1);
2485         if (IS_ERR(di)) {
2486                 ret = PTR_ERR(di);
2487                 goto err;
2488         }
2489         if (!di) {
2490                 ret = -ENOENT;
2491                 goto err;
2492         }
2493         ret = btrfs_delete_one_dir_name(trans, root, path, di);
2494         btrfs_release_path(root, path);
2495
2496         ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2497                                          inode, dir->i_ino);
2498         BUG_ON(ret != 0 && ret != -ENOENT);
2499
2500         ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2501                                            dir, index);
2502         BUG_ON(ret);
2503 err:
2504         btrfs_free_path(path);
2505         if (ret)
2506                 goto out;
2507
2508         btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2509         inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2510         btrfs_update_inode(trans, root, dir);
2511         btrfs_drop_nlink(inode);
2512         ret = btrfs_update_inode(trans, root, inode);
2513 out:
2514         return ret;
2515 }
2516
2517 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2518 {
2519         struct btrfs_root *root;
2520         struct btrfs_trans_handle *trans;
2521         struct inode *inode = dentry->d_inode;
2522         int ret;
2523         unsigned long nr = 0;
2524
2525         root = BTRFS_I(dir)->root;
2526
2527         /*
2528          * 5 items for unlink inode
2529          * 1 for orphan
2530          */
2531         ret = btrfs_reserve_metadata_space(root, 6);
2532         if (ret)
2533                 return ret;
2534
2535         trans = btrfs_start_transaction(root, 1);
2536         if (IS_ERR(trans)) {
2537                 btrfs_unreserve_metadata_space(root, 6);
2538                 return PTR_ERR(trans);
2539         }
2540
2541         btrfs_set_trans_block_group(trans, dir);
2542
2543         btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
2544
2545         ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2546                                  dentry->d_name.name, dentry->d_name.len);
2547
2548         if (inode->i_nlink == 0)
2549                 ret = btrfs_orphan_add(trans, inode);
2550
2551         nr = trans->blocks_used;
2552
2553         btrfs_end_transaction_throttle(trans, root);
2554         btrfs_unreserve_metadata_space(root, 6);
2555         btrfs_btree_balance_dirty(root, nr);
2556         return ret;
2557 }
2558
2559 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
2560                         struct btrfs_root *root,
2561                         struct inode *dir, u64 objectid,
2562                         const char *name, int name_len)
2563 {
2564         struct btrfs_path *path;
2565         struct extent_buffer *leaf;
2566         struct btrfs_dir_item *di;
2567         struct btrfs_key key;
2568         u64 index;
2569         int ret;
2570
2571         path = btrfs_alloc_path();
2572         if (!path)
2573                 return -ENOMEM;
2574
2575         di = btrfs_lookup_dir_item(trans, root, path, dir->i_ino,
2576                                    name, name_len, -1);
2577         BUG_ON(!di || IS_ERR(di));
2578
2579         leaf = path->nodes[0];
2580         btrfs_dir_item_key_to_cpu(leaf, di, &key);
2581         WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2582         ret = btrfs_delete_one_dir_name(trans, root, path, di);
2583         BUG_ON(ret);
2584         btrfs_release_path(root, path);
2585
2586         ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
2587                                  objectid, root->root_key.objectid,
2588                                  dir->i_ino, &index, name, name_len);
2589         if (ret < 0) {
2590                 BUG_ON(ret != -ENOENT);
2591                 di = btrfs_search_dir_index_item(root, path, dir->i_ino,
2592                                                  name, name_len);
2593                 BUG_ON(!di || IS_ERR(di));
2594
2595                 leaf = path->nodes[0];
2596                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
2597                 btrfs_release_path(root, path);
2598                 index = key.offset;
2599         }
2600
2601         di = btrfs_lookup_dir_index_item(trans, root, path, dir->i_ino,
2602                                          index, name, name_len, -1);
2603         BUG_ON(!di || IS_ERR(di));
2604
2605         leaf = path->nodes[0];
2606         btrfs_dir_item_key_to_cpu(leaf, di, &key);
2607         WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
2608         ret = btrfs_delete_one_dir_name(trans, root, path, di);
2609         BUG_ON(ret);
2610         btrfs_release_path(root, path);
2611
2612         btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2613         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2614         ret = btrfs_update_inode(trans, root, dir);
2615         BUG_ON(ret);
2616         dir->i_sb->s_dirt = 1;
2617
2618         btrfs_free_path(path);
2619         return 0;
2620 }
2621
2622 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
2623 {
2624         struct inode *inode = dentry->d_inode;
2625         int err = 0;
2626         int ret;
2627         struct btrfs_root *root = BTRFS_I(dir)->root;
2628         struct btrfs_trans_handle *trans;
2629         unsigned long nr = 0;
2630
2631         if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
2632             inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
2633                 return -ENOTEMPTY;
2634
2635         ret = btrfs_reserve_metadata_space(root, 5);
2636         if (ret)
2637                 return ret;
2638
2639         trans = btrfs_start_transaction(root, 1);
2640         if (IS_ERR(trans)) {
2641                 btrfs_unreserve_metadata_space(root, 5);
2642                 return PTR_ERR(trans);
2643         }
2644
2645         btrfs_set_trans_block_group(trans, dir);
2646
2647         if (unlikely(inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
2648                 err = btrfs_unlink_subvol(trans, root, dir,
2649                                           BTRFS_I(inode)->location.objectid,
2650                                           dentry->d_name.name,
2651                                           dentry->d_name.len);
2652                 goto out;
2653         }
2654
2655         err = btrfs_orphan_add(trans, inode);
2656         if (err)
2657                 goto out;
2658
2659         /* now the directory is empty */
2660         err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
2661                                  dentry->d_name.name, dentry->d_name.len);
2662         if (!err)
2663                 btrfs_i_size_write(inode, 0);
2664 out:
2665         nr = trans->blocks_used;
2666         ret = btrfs_end_transaction_throttle(trans, root);
2667         btrfs_unreserve_metadata_space(root, 5);
2668         btrfs_btree_balance_dirty(root, nr);
2669
2670         if (ret && !err)
2671                 err = ret;
2672         return err;
2673 }
2674
2675 #if 0
2676 /*
2677  * when truncating bytes in a file, it is possible to avoid reading
2678  * the leaves that contain only checksum items.  This can be the
2679  * majority of the IO required to delete a large file, but it must
2680  * be done carefully.
2681  *
2682  * The keys in the level just above the leaves are checked to make sure
2683  * the lowest key in a given leaf is a csum key, and starts at an offset
2684  * after the new  size.
2685  *
2686  * Then the key for the next leaf is checked to make sure it also has
2687  * a checksum item for the same file.  If it does, we know our target leaf
2688  * contains only checksum items, and it can be safely freed without reading
2689  * it.
2690  *
2691  * This is just an optimization targeted at large files.  It may do
2692  * nothing.  It will return 0 unless things went badly.
2693  */
2694 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
2695                                      struct btrfs_root *root,
2696                                      struct btrfs_path *path,
2697                                      struct inode *inode, u64 new_size)
2698 {
2699         struct btrfs_key key;
2700         int ret;
2701         int nritems;
2702         struct btrfs_key found_key;
2703         struct btrfs_key other_key;
2704         struct btrfs_leaf_ref *ref;
2705         u64 leaf_gen;
2706         u64 leaf_start;
2707
2708         path->lowest_level = 1;
2709         key.objectid = inode->i_ino;
2710         key.type = BTRFS_CSUM_ITEM_KEY;
2711         key.offset = new_size;
2712 again:
2713         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2714         if (ret < 0)
2715                 goto out;
2716
2717         if (path->nodes[1] == NULL) {
2718                 ret = 0;
2719                 goto out;
2720         }
2721         ret = 0;
2722         btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
2723         nritems = btrfs_header_nritems(path->nodes[1]);
2724
2725         if (!nritems)
2726                 goto out;
2727
2728         if (path->slots[1] >= nritems)
2729                 goto next_node;
2730
2731         /* did we find a key greater than anything we want to delete? */
2732         if (found_key.objectid > inode->i_ino ||
2733            (found_key.objectid == inode->i_ino && found_key.type > key.type))
2734                 goto out;
2735
2736         /* we check the next key in the node to make sure the leave contains
2737          * only checksum items.  This comparison doesn't work if our
2738          * leaf is the last one in the node
2739          */
2740         if (path->slots[1] + 1 >= nritems) {
2741 next_node:
2742                 /* search forward from the last key in the node, this
2743                  * will bring us into the next node in the tree
2744                  */
2745                 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
2746
2747                 /* unlikely, but we inc below, so check to be safe */
2748                 if (found_key.offset == (u64)-1)
2749                         goto out;
2750
2751                 /* search_forward needs a path with locks held, do the
2752                  * search again for the original key.  It is possible
2753                  * this will race with a balance and return a path that
2754                  * we could modify, but this drop is just an optimization
2755                  * and is allowed to miss some leaves.
2756                  */
2757                 btrfs_release_path(root, path);
2758                 found_key.offset++;
2759
2760                 /* setup a max key for search_forward */
2761                 other_key.offset = (u64)-1;
2762                 other_key.type = key.type;
2763                 other_key.objectid = key.objectid;
2764
2765                 path->keep_locks = 1;
2766                 ret = btrfs_search_forward(root, &found_key, &other_key,
2767                                            path, 0, 0);
2768                 path->keep_locks = 0;
2769                 if (ret || found_key.objectid != key.objectid ||
2770                     found_key.type != key.type) {
2771                         ret = 0;
2772                         goto out;
2773                 }
2774
2775                 key.offset = found_key.offset;
2776                 btrfs_release_path(root, path);
2777                 cond_resched();
2778                 goto again;
2779         }
2780
2781         /* we know there's one more slot after us in the tree,
2782          * read that key so we can verify it is also a checksum item
2783          */
2784         btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
2785
2786         if (found_key.objectid < inode->i_ino)
2787                 goto next_key;
2788
2789         if (found_key.type != key.type || found_key.offset < new_size)
2790                 goto next_key;
2791
2792         /*
2793          * if the key for the next leaf isn't a csum key from this objectid,
2794          * we can't be sure there aren't good items inside this leaf.
2795          * Bail out
2796          */
2797         if (other_key.objectid != inode->i_ino || other_key.type != key.type)
2798                 goto out;
2799
2800         leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
2801         leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
2802         /*
2803          * it is safe to delete this leaf, it contains only
2804          * csum items from this inode at an offset >= new_size
2805          */
2806         ret = btrfs_del_leaf(trans, root, path, leaf_start);
2807         BUG_ON(ret);
2808
2809         if (root->ref_cows && leaf_gen < trans->transid) {
2810                 ref = btrfs_alloc_leaf_ref(root, 0);
2811                 if (ref) {
2812                         ref->root_gen = root->root_key.offset;
2813                         ref->bytenr = leaf_start;
2814                         ref->owner = 0;
2815                         ref->generation = leaf_gen;
2816                         ref->nritems = 0;
2817
2818                         btrfs_sort_leaf_ref(ref);
2819
2820                         ret = btrfs_add_leaf_ref(root, ref, 0);
2821                         WARN_ON(ret);
2822                         btrfs_free_leaf_ref(root, ref);
2823                 } else {
2824                         WARN_ON(1);
2825                 }
2826         }
2827 next_key:
2828         btrfs_release_path(root, path);
2829
2830         if (other_key.objectid == inode->i_ino &&
2831             other_key.type == key.type && other_key.offset > key.offset) {
2832                 key.offset = other_key.offset;
2833                 cond_resched();
2834                 goto again;
2835         }
2836         ret = 0;
2837 out:
2838         /* fixup any changes we've made to the path */
2839         path->lowest_level = 0;
2840         path->keep_locks = 0;
2841         btrfs_release_path(root, path);
2842         return ret;
2843 }
2844
2845 #endif
2846
2847 /*
2848  * this can truncate away extent items, csum items and directory items.
2849  * It starts at a high offset and removes keys until it can't find
2850  * any higher than new_size
2851  *
2852  * csum items that cross the new i_size are truncated to the new size
2853  * as well.
2854  *
2855  * min_type is the minimum key type to truncate down to.  If set to 0, this
2856  * will kill all the items on this inode, including the INODE_ITEM_KEY.
2857  */
2858 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
2859                                struct btrfs_root *root,
2860                                struct inode *inode,
2861                                u64 new_size, u32 min_type)
2862 {
2863         struct btrfs_path *path;
2864         struct extent_buffer *leaf;
2865         struct btrfs_file_extent_item *fi;
2866         struct btrfs_key key;
2867         struct btrfs_key found_key;
2868         u64 extent_start = 0;
2869         u64 extent_num_bytes = 0;
2870         u64 extent_offset = 0;
2871         u64 item_end = 0;
2872         u64 mask = root->sectorsize - 1;
2873         u32 found_type = (u8)-1;
2874         int found_extent;
2875         int del_item;
2876         int pending_del_nr = 0;
2877         int pending_del_slot = 0;
2878         int extent_type = -1;
2879         int encoding;
2880         int ret;
2881         int err = 0;
2882
2883         BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
2884
2885         if (root->ref_cows)
2886                 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
2887
2888         path = btrfs_alloc_path();
2889         BUG_ON(!path);
2890         path->reada = -1;
2891
2892         key.objectid = inode->i_ino;
2893         key.offset = (u64)-1;
2894         key.type = (u8)-1;
2895
2896 search_again:
2897         path->leave_spinning = 1;
2898         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
2899         if (ret < 0) {
2900                 err = ret;
2901                 goto out;
2902         }
2903
2904         if (ret > 0) {
2905                 /* there are no items in the tree for us to truncate, we're
2906                  * done
2907                  */
2908                 if (path->slots[0] == 0)
2909                         goto out;
2910                 path->slots[0]--;
2911         }
2912
2913         while (1) {
2914                 fi = NULL;
2915                 leaf = path->nodes[0];
2916                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2917                 found_type = btrfs_key_type(&found_key);
2918                 encoding = 0;
2919
2920                 if (found_key.objectid != inode->i_ino)
2921                         break;
2922
2923                 if (found_type < min_type)
2924                         break;
2925
2926                 item_end = found_key.offset;
2927                 if (found_type == BTRFS_EXTENT_DATA_KEY) {
2928                         fi = btrfs_item_ptr(leaf, path->slots[0],
2929                                             struct btrfs_file_extent_item);
2930                         extent_type = btrfs_file_extent_type(leaf, fi);
2931                         encoding = btrfs_file_extent_compression(leaf, fi);
2932                         encoding |= btrfs_file_extent_encryption(leaf, fi);
2933                         encoding |= btrfs_file_extent_other_encoding(leaf, fi);
2934
2935                         if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2936                                 item_end +=
2937                                     btrfs_file_extent_num_bytes(leaf, fi);
2938                         } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2939                                 item_end += btrfs_file_extent_inline_len(leaf,
2940                                                                          fi);
2941                         }
2942                         item_end--;
2943                 }
2944                 if (found_type > min_type) {
2945                         del_item = 1;
2946                 } else {
2947                         if (item_end < new_size)
2948                                 break;
2949                         if (found_key.offset >= new_size)
2950                                 del_item = 1;
2951                         else
2952                                 del_item = 0;
2953                 }
2954                 found_extent = 0;
2955                 /* FIXME, shrink the extent if the ref count is only 1 */
2956                 if (found_type != BTRFS_EXTENT_DATA_KEY)
2957                         goto delete;
2958
2959                 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
2960                         u64 num_dec;
2961                         extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
2962                         if (!del_item && !encoding) {
2963                                 u64 orig_num_bytes =
2964                                         btrfs_file_extent_num_bytes(leaf, fi);
2965                                 extent_num_bytes = new_size -
2966                                         found_key.offset + root->sectorsize - 1;
2967                                 extent_num_bytes = extent_num_bytes &
2968                                         ~((u64)root->sectorsize - 1);
2969                                 btrfs_set_file_extent_num_bytes(leaf, fi,
2970                                                          extent_num_bytes);
2971                                 num_dec = (orig_num_bytes -
2972                                            extent_num_bytes);
2973                                 if (root->ref_cows && extent_start != 0)
2974                                         inode_sub_bytes(inode, num_dec);
2975                                 btrfs_mark_buffer_dirty(leaf);
2976                         } else {
2977                                 extent_num_bytes =
2978                                         btrfs_file_extent_disk_num_bytes(leaf,
2979                                                                          fi);
2980                                 extent_offset = found_key.offset -
2981                                         btrfs_file_extent_offset(leaf, fi);
2982
2983                                 /* FIXME blocksize != 4096 */
2984                                 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
2985                                 if (extent_start != 0) {
2986                                         found_extent = 1;
2987                                         if (root->ref_cows)
2988                                                 inode_sub_bytes(inode, num_dec);
2989                                 }
2990                         }
2991                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
2992                         /*
2993                          * we can't truncate inline items that have had
2994                          * special encodings
2995                          */
2996                         if (!del_item &&
2997                             btrfs_file_extent_compression(leaf, fi) == 0 &&
2998                             btrfs_file_extent_encryption(leaf, fi) == 0 &&
2999                             btrfs_file_extent_other_encoding(leaf, fi) == 0) {
3000                                 u32 size = new_size - found_key.offset;
3001
3002                                 if (root->ref_cows) {
3003                                         inode_sub_bytes(inode, item_end + 1 -
3004                                                         new_size);
3005                                 }
3006                                 size =
3007                                     btrfs_file_extent_calc_inline_size(size);
3008                                 ret = btrfs_truncate_item(trans, root, path,
3009                                                           size, 1);
3010                                 BUG_ON(ret);
3011                         } else if (root->ref_cows) {
3012                                 inode_sub_bytes(inode, item_end + 1 -
3013                                                 found_key.offset);
3014                         }
3015                 }
3016 delete:
3017                 if (del_item) {
3018                         if (!pending_del_nr) {
3019                                 /* no pending yet, add ourselves */
3020                                 pending_del_slot = path->slots[0];
3021                                 pending_del_nr = 1;
3022                         } else if (pending_del_nr &&
3023                                    path->slots[0] + 1 == pending_del_slot) {
3024                                 /* hop on the pending chunk */
3025                                 pending_del_nr++;
3026                                 pending_del_slot = path->slots[0];
3027                         } else {
3028                                 BUG();
3029                         }
3030                 } else {
3031                         break;
3032                 }
3033                 if (found_extent && root->ref_cows) {
3034                         btrfs_set_path_blocking(path);
3035                         ret = btrfs_free_extent(trans, root, extent_start,
3036                                                 extent_num_bytes, 0,
3037                                                 btrfs_header_owner(leaf),
3038                                                 inode->i_ino, extent_offset);
3039                         BUG_ON(ret);
3040                 }
3041
3042                 if (found_type == BTRFS_INODE_ITEM_KEY)
3043                         break;
3044
3045                 if (path->slots[0] == 0 ||
3046                     path->slots[0] != pending_del_slot) {
3047                         if (root->ref_cows) {
3048                                 err = -EAGAIN;
3049                                 goto out;
3050                         }
3051                         if (pending_del_nr) {
3052                                 ret = btrfs_del_items(trans, root, path,
3053                                                 pending_del_slot,
3054                                                 pending_del_nr);
3055                                 BUG_ON(ret);
3056                                 pending_del_nr = 0;
3057                         }
3058                         btrfs_release_path(root, path);
3059                         goto search_again;
3060                 } else {
3061                         path->slots[0]--;
3062                 }
3063         }
3064 out:
3065         if (pending_del_nr) {
3066                 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3067                                       pending_del_nr);
3068         }
3069         btrfs_free_path(path);
3070         return err;
3071 }
3072
3073 /*
3074  * taken from block_truncate_page, but does cow as it zeros out
3075  * any bytes left in the last page in the file.
3076  */
3077 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3078 {
3079         struct inode *inode = mapping->host;
3080         struct btrfs_root *root = BTRFS_I(inode)->root;
3081         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3082         struct btrfs_ordered_extent *ordered;
3083         char *kaddr;
3084         u32 blocksize = root->sectorsize;
3085         pgoff_t index = from >> PAGE_CACHE_SHIFT;
3086         unsigned offset = from & (PAGE_CACHE_SIZE-1);
3087         struct page *page;
3088         int ret = 0;
3089         u64 page_start;
3090         u64 page_end;
3091
3092         if ((offset & (blocksize - 1)) == 0)
3093                 goto out;
3094         ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
3095         if (ret)
3096                 goto out;
3097
3098         ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
3099         if (ret)
3100                 goto out;
3101
3102         ret = -ENOMEM;
3103 again:
3104         page = grab_cache_page(mapping, index);
3105         if (!page) {
3106                 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3107                 btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
3108                 goto out;
3109         }
3110
3111         page_start = page_offset(page);
3112         page_end = page_start + PAGE_CACHE_SIZE - 1;
3113
3114         if (!PageUptodate(page)) {
3115                 ret = btrfs_readpage(NULL, page);
3116                 lock_page(page);
3117                 if (page->mapping != mapping) {
3118                         unlock_page(page);
3119                         page_cache_release(page);
3120                         goto again;
3121                 }
3122                 if (!PageUptodate(page)) {
3123                         ret = -EIO;
3124                         goto out_unlock;
3125                 }
3126         }
3127         wait_on_page_writeback(page);
3128
3129         lock_extent(io_tree, page_start, page_end, GFP_NOFS);
3130         set_page_extent_mapped(page);
3131
3132         ordered = btrfs_lookup_ordered_extent(inode, page_start);
3133         if (ordered) {
3134                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3135                 unlock_page(page);
3136                 page_cache_release(page);
3137                 btrfs_start_ordered_extent(inode, ordered, 1);
3138                 btrfs_put_ordered_extent(ordered);
3139                 goto again;
3140         }
3141
3142         clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
3143                           EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3144                           GFP_NOFS);
3145
3146         ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
3147         if (ret) {
3148                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3149                 goto out_unlock;
3150         }
3151
3152         ret = 0;
3153         if (offset != PAGE_CACHE_SIZE) {
3154                 kaddr = kmap(page);
3155                 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3156                 flush_dcache_page(page);
3157                 kunmap(page);
3158         }
3159         ClearPageChecked(page);
3160         set_page_dirty(page);
3161         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
3162
3163 out_unlock:
3164         if (ret)
3165                 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
3166         btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
3167         unlock_page(page);
3168         page_cache_release(page);
3169 out:
3170         return ret;
3171 }
3172
3173 int btrfs_cont_expand(struct inode *inode, loff_t size)
3174 {
3175         struct btrfs_trans_handle *trans;
3176         struct btrfs_root *root = BTRFS_I(inode)->root;
3177         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3178         struct extent_map *em;
3179         u64 mask = root->sectorsize - 1;
3180         u64 hole_start = (inode->i_size + mask) & ~mask;
3181         u64 block_end = (size + mask) & ~mask;
3182         u64 last_byte;
3183         u64 cur_offset;
3184         u64 hole_size;
3185         int err = 0;
3186
3187         if (size <= hole_start)
3188                 return 0;
3189
3190         while (1) {
3191                 struct btrfs_ordered_extent *ordered;
3192                 btrfs_wait_ordered_range(inode, hole_start,
3193                                          block_end - hole_start);
3194                 lock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3195                 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3196                 if (!ordered)
3197                         break;
3198                 unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3199                 btrfs_put_ordered_extent(ordered);
3200         }
3201
3202         cur_offset = hole_start;
3203         while (1) {
3204                 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3205                                 block_end - cur_offset, 0);
3206                 BUG_ON(IS_ERR(em) || !em);
3207                 last_byte = min(extent_map_end(em), block_end);
3208                 last_byte = (last_byte + mask) & ~mask;
3209                 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3210                         u64 hint_byte = 0;
3211                         hole_size = last_byte - cur_offset;
3212
3213                         err = btrfs_reserve_metadata_space(root, 2);
3214                         if (err)
3215                                 break;
3216
3217                         trans = btrfs_start_transaction(root, 1);
3218                         btrfs_set_trans_block_group(trans, inode);
3219
3220                         err = btrfs_drop_extents(trans, inode, cur_offset,
3221                                                  cur_offset + hole_size,
3222                                                  &hint_byte, 1);
3223                         BUG_ON(err);
3224
3225                         err = btrfs_insert_file_extent(trans, root,
3226                                         inode->i_ino, cur_offset, 0,
3227                                         0, hole_size, 0, hole_size,
3228                                         0, 0, 0);
3229                         BUG_ON(err);
3230
3231                         btrfs_drop_extent_cache(inode, hole_start,
3232                                         last_byte - 1, 0);
3233
3234                         btrfs_end_transaction(trans, root);
3235                         btrfs_unreserve_metadata_space(root, 2);
3236                 }
3237                 free_extent_map(em);
3238                 cur_offset = last_byte;
3239                 if (cur_offset >= block_end)
3240                         break;
3241         }
3242
3243         unlock_extent(io_tree, hole_start, block_end - 1, GFP_NOFS);
3244         return err;
3245 }
3246
3247 static int btrfs_setattr_size(struct inode *inode, struct iattr *attr)
3248 {
3249         struct btrfs_root *root = BTRFS_I(inode)->root;
3250         struct btrfs_trans_handle *trans;
3251         unsigned long nr;
3252         int ret;
3253
3254         if (attr->ia_size == inode->i_size)
3255                 return 0;
3256
3257         if (attr->ia_size > inode->i_size) {
3258                 unsigned long limit;
3259                 limit = current->signal->rlim[RLIMIT_FSIZE].rlim_cur;
3260                 if (attr->ia_size > inode->i_sb->s_maxbytes)
3261                         return -EFBIG;
3262                 if (limit != RLIM_INFINITY && attr->ia_size > limit) {
3263                         send_sig(SIGXFSZ, current, 0);
3264                         return -EFBIG;
3265                 }
3266         }
3267
3268         ret = btrfs_reserve_metadata_space(root, 1);
3269         if (ret)
3270                 return ret;
3271
3272         trans = btrfs_start_transaction(root, 1);
3273         btrfs_set_trans_block_group(trans, inode);
3274
3275         ret = btrfs_orphan_add(trans, inode);
3276         BUG_ON(ret);
3277
3278         nr = trans->blocks_used;
3279         btrfs_end_transaction(trans, root);
3280         btrfs_unreserve_metadata_space(root, 1);
3281         btrfs_btree_balance_dirty(root, nr);
3282
3283         if (attr->ia_size > inode->i_size) {
3284                 ret = btrfs_cont_expand(inode, attr->ia_size);
3285                 if (ret) {
3286                         btrfs_truncate(inode);
3287                         return ret;
3288                 }
3289
3290                 i_size_write(inode, attr->ia_size);
3291                 btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
3292
3293                 trans = btrfs_start_transaction(root, 1);
3294                 btrfs_set_trans_block_group(trans, inode);
3295
3296                 ret = btrfs_update_inode(trans, root, inode);
3297                 BUG_ON(ret);
3298                 if (inode->i_nlink > 0) {
3299                         ret = btrfs_orphan_del(trans, inode);
3300                         BUG_ON(ret);
3301                 }
3302                 nr = trans->blocks_used;
3303                 btrfs_end_transaction(trans, root);
3304                 btrfs_btree_balance_dirty(root, nr);
3305                 return 0;
3306         }
3307
3308         /*
3309          * We're truncating a file that used to have good data down to
3310          * zero. Make sure it gets into the ordered flush list so that
3311          * any new writes get down to disk quickly.
3312          */
3313         if (attr->ia_size == 0)
3314                 BTRFS_I(inode)->ordered_data_close = 1;
3315
3316         /* we don't support swapfiles, so vmtruncate shouldn't fail */
3317         ret = vmtruncate(inode, attr->ia_size);
3318         BUG_ON(ret);
3319
3320         return 0;
3321 }
3322
3323 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3324 {
3325         struct inode *inode = dentry->d_inode;
3326         int err;
3327
3328         err = inode_change_ok(inode, attr);
3329         if (err)
3330                 return err;
3331
3332         if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3333                 err = btrfs_setattr_size(inode, attr);
3334                 if (err)
3335                         return err;
3336         }
3337         attr->ia_valid &= ~ATTR_SIZE;
3338
3339         if (attr->ia_valid)
3340                 err = inode_setattr(inode, attr);
3341
3342         if (!err && ((attr->ia_valid & ATTR_MODE)))
3343                 err = btrfs_acl_chmod(inode);
3344         return err;
3345 }
3346
3347 void btrfs_delete_inode(struct inode *inode)
3348 {
3349         struct btrfs_trans_handle *trans;
3350         struct btrfs_root *root = BTRFS_I(inode)->root;
3351         unsigned long nr;
3352         int ret;
3353
3354         truncate_inode_pages(&inode->i_data, 0);
3355         if (is_bad_inode(inode)) {
3356                 btrfs_orphan_del(NULL, inode);
3357                 goto no_delete;
3358         }
3359         btrfs_wait_ordered_range(inode, 0, (u64)-1);
3360
3361         if (root->fs_info->log_root_recovering) {
3362                 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3363                 goto no_delete;
3364         }
3365
3366         if (inode->i_nlink > 0) {
3367                 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3368                 goto no_delete;
3369         }
3370
3371         btrfs_i_size_write(inode, 0);
3372
3373         while (1) {
3374                 trans = btrfs_start_transaction(root, 1);
3375                 btrfs_set_trans_block_group(trans, inode);
3376                 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3377
3378                 if (ret != -EAGAIN)
3379                         break;
3380
3381                 nr = trans->blocks_used;
3382                 btrfs_end_transaction(trans, root);
3383                 trans = NULL;
3384                 btrfs_btree_balance_dirty(root, nr);
3385         }
3386
3387         if (ret == 0) {
3388                 ret = btrfs_orphan_del(trans, inode);
3389                 BUG_ON(ret);
3390         }
3391
3392         nr = trans->blocks_used;
3393         btrfs_end_transaction(trans, root);
3394         btrfs_btree_balance_dirty(root, nr);
3395 no_delete:
3396         clear_inode(inode);
3397         return;
3398 }
3399
3400 /*
3401  * this returns the key found in the dir entry in the location pointer.
3402  * If no dir entries were found, location->objectid is 0.
3403  */
3404 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3405                                struct btrfs_key *location)
3406 {
3407         const char *name = dentry->d_name.name;
3408         int namelen = dentry->d_name.len;
3409         struct btrfs_dir_item *di;
3410         struct btrfs_path *path;
3411         struct btrfs_root *root = BTRFS_I(dir)->root;
3412         int ret = 0;
3413
3414         path = btrfs_alloc_path();
3415         BUG_ON(!path);
3416
3417         di = btrfs_lookup_dir_item(NULL, root, path, dir->i_ino, name,
3418                                     namelen, 0);
3419         if (IS_ERR(di))
3420                 ret = PTR_ERR(di);
3421
3422         if (!di || IS_ERR(di))
3423                 goto out_err;
3424
3425         btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3426 out:
3427         btrfs_free_path(path);
3428         return ret;
3429 out_err:
3430         location->objectid = 0;
3431         goto out;
3432 }
3433
3434 /*
3435  * when we hit a tree root in a directory, the btrfs part of the inode
3436  * needs to be changed to reflect the root directory of the tree root.  This
3437  * is kind of like crossing a mount point.
3438  */
3439 static int fixup_tree_root_location(struct btrfs_root *root,
3440                                     struct inode *dir,
3441                                     struct dentry *dentry,
3442                                     struct btrfs_key *location,
3443                                     struct btrfs_root **sub_root)
3444 {
3445         struct btrfs_path *path;
3446         struct btrfs_root *new_root;
3447         struct btrfs_root_ref *ref;
3448         struct extent_buffer *leaf;
3449         int ret;
3450         int err = 0;
3451
3452         path = btrfs_alloc_path();
3453         if (!path) {
3454                 err = -ENOMEM;
3455                 goto out;
3456         }
3457
3458         err = -ENOENT;
3459         ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3460                                   BTRFS_I(dir)->root->root_key.objectid,
3461                                   location->objectid);
3462         if (ret) {
3463                 if (ret < 0)
3464                         err = ret;
3465                 goto out;
3466         }
3467
3468         leaf = path->nodes[0];
3469         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3470         if (btrfs_root_ref_dirid(leaf, ref) != dir->i_ino ||
3471             btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3472                 goto out;
3473
3474         ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3475                                    (unsigned long)(ref + 1),
3476                                    dentry->d_name.len);
3477         if (ret)
3478                 goto out;
3479
3480         btrfs_release_path(root->fs_info->tree_root, path);
3481
3482         new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3483         if (IS_ERR(new_root)) {
3484                 err = PTR_ERR(new_root);
3485                 goto out;
3486         }
3487
3488         if (btrfs_root_refs(&new_root->root_item) == 0) {
3489                 err = -ENOENT;
3490                 goto out;
3491         }
3492
3493         *sub_root = new_root;
3494         location->objectid = btrfs_root_dirid(&new_root->root_item);
3495         location->type = BTRFS_INODE_ITEM_KEY;
3496         location->offset = 0;
3497         err = 0;
3498 out:
3499         btrfs_free_path(path);
3500         return err;
3501 }
3502
3503 static void inode_tree_add(struct inode *inode)
3504 {
3505         struct btrfs_root *root = BTRFS_I(inode)->root;
3506         struct btrfs_inode *entry;
3507         struct rb_node **p;
3508         struct rb_node *parent;
3509 again:
3510         p = &root->inode_tree.rb_node;
3511         parent = NULL;
3512
3513         if (hlist_unhashed(&inode->i_hash))
3514                 return;
3515
3516         spin_lock(&root->inode_lock);
3517         while (*p) {
3518                 parent = *p;
3519                 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3520
3521                 if (inode->i_ino < entry->vfs_inode.i_ino)
3522                         p = &parent->rb_left;
3523                 else if (inode->i_ino > entry->vfs_inode.i_ino)
3524                         p = &parent->rb_right;
3525                 else {
3526                         WARN_ON(!(entry->vfs_inode.i_state &
3527                                   (I_WILL_FREE | I_FREEING | I_CLEAR)));
3528                         rb_erase(parent, &root->inode_tree);
3529                         RB_CLEAR_NODE(parent);
3530                         spin_unlock(&root->inode_lock);
3531                         goto again;
3532                 }
3533         }
3534         rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3535         rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3536         spin_unlock(&root->inode_lock);
3537 }
3538
3539 static void inode_tree_del(struct inode *inode)
3540 {
3541         struct btrfs_root *root = BTRFS_I(inode)->root;
3542         int empty = 0;
3543
3544         spin_lock(&root->inode_lock);
3545         if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
3546                 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3547                 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3548                 empty = RB_EMPTY_ROOT(&root->inode_tree);
3549         }
3550         spin_unlock(&root->inode_lock);
3551
3552         if (empty && btrfs_root_refs(&root->root_item) == 0) {
3553                 synchronize_srcu(&root->fs_info->subvol_srcu);
3554                 spin_lock(&root->inode_lock);
3555                 empty = RB_EMPTY_ROOT(&root->inode_tree);
3556                 spin_unlock(&root->inode_lock);
3557                 if (empty)
3558                         btrfs_add_dead_root(root);
3559         }
3560 }
3561
3562 int btrfs_invalidate_inodes(struct btrfs_root *root)
3563 {
3564         struct rb_node *node;
3565         struct rb_node *prev;
3566         struct btrfs_inode *entry;
3567         struct inode *inode;
3568         u64 objectid = 0;
3569
3570         WARN_ON(btrfs_root_refs(&root->root_item) != 0);
3571
3572         spin_lock(&root->inode_lock);
3573 again:
3574         node = root->inode_tree.rb_node;
3575         prev = NULL;
3576         while (node) {
3577                 prev = node;
3578                 entry = rb_entry(node, struct btrfs_inode, rb_node);
3579
3580                 if (objectid < entry->vfs_inode.i_ino)
3581                         node = node->rb_left;
3582                 else if (objectid > entry->vfs_inode.i_ino)
3583                         node = node->rb_right;
3584                 else
3585                         break;
3586         }
3587         if (!node) {
3588                 while (prev) {
3589                         entry = rb_entry(prev, struct btrfs_inode, rb_node);
3590                         if (objectid <= entry->vfs_inode.i_ino) {
3591                                 node = prev;
3592                                 break;
3593                         }
3594                         prev = rb_next(prev);
3595                 }
3596         }
3597         while (node) {
3598                 entry = rb_entry(node, struct btrfs_inode, rb_node);
3599                 objectid = entry->vfs_inode.i_ino + 1;
3600                 inode = igrab(&entry->vfs_inode);
3601                 if (inode) {
3602                         spin_unlock(&root->inode_lock);
3603                         if (atomic_read(&inode->i_count) > 1)
3604                                 d_prune_aliases(inode);
3605                         /*
3606                          * btrfs_drop_inode will remove it from
3607                          * the inode cache when its usage count
3608                          * hits zero.
3609                          */
3610                         iput(inode);
3611                         cond_resched();
3612                         spin_lock(&root->inode_lock);
3613                         goto again;
3614                 }
3615
3616                 if (cond_resched_lock(&root->inode_lock))
3617                         goto again;
3618
3619                 node = rb_next(node);
3620         }
3621         spin_unlock(&root->inode_lock);
3622         return 0;
3623 }
3624
3625 static noinline void init_btrfs_i(struct inode *inode)
3626 {
3627         struct btrfs_inode *bi = BTRFS_I(inode);
3628
3629         bi->generation = 0;
3630         bi->sequence = 0;
3631         bi->last_trans = 0;
3632         bi->last_sub_trans = 0;
3633         bi->logged_trans = 0;
3634         bi->delalloc_bytes = 0;
3635         bi->reserved_bytes = 0;
3636         bi->disk_i_size = 0;
3637         bi->flags = 0;
3638         bi->index_cnt = (u64)-1;
3639         bi->last_unlink_trans = 0;
3640         bi->ordered_data_close = 0;
3641         extent_map_tree_init(&BTRFS_I(inode)->extent_tree, GFP_NOFS);
3642         extent_io_tree_init(&BTRFS_I(inode)->io_tree,
3643                              inode->i_mapping, GFP_NOFS);
3644         extent_io_tree_init(&BTRFS_I(inode)->io_failure_tree,
3645                              inode->i_mapping, GFP_NOFS);
3646         INIT_LIST_HEAD(&BTRFS_I(inode)->delalloc_inodes);
3647         INIT_LIST_HEAD(&BTRFS_I(inode)->ordered_operations);
3648         RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
3649         btrfs_ordered_inode_tree_init(&BTRFS_I(inode)->ordered_tree);
3650         mutex_init(&BTRFS_I(inode)->log_mutex);
3651 }
3652
3653 static int btrfs_init_locked_inode(struct inode *inode, void *p)
3654 {
3655         struct btrfs_iget_args *args = p;
3656         inode->i_ino = args->ino;
3657         init_btrfs_i(inode);
3658         BTRFS_I(inode)->root = args->root;
3659         btrfs_set_inode_space_info(args->root, inode);
3660         return 0;
3661 }
3662
3663 static int btrfs_find_actor(struct inode *inode, void *opaque)
3664 {
3665         struct btrfs_iget_args *args = opaque;
3666         return args->ino == inode->i_ino &&
3667                 args->root == BTRFS_I(inode)->root;
3668 }
3669
3670 static struct inode *btrfs_iget_locked(struct super_block *s,
3671                                        u64 objectid,
3672                                        struct btrfs_root *root)
3673 {
3674         struct inode *inode;
3675         struct btrfs_iget_args args;
3676         args.ino = objectid;
3677         args.root = root;
3678
3679         inode = iget5_locked(s, objectid, btrfs_find_actor,
3680                              btrfs_init_locked_inode,
3681                              (void *)&args);
3682         return inode;
3683 }
3684
3685 /* Get an inode object given its location and corresponding root.
3686  * Returns in *is_new if the inode was read from disk
3687  */
3688 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
3689                          struct btrfs_root *root)
3690 {
3691         struct inode *inode;
3692
3693         inode = btrfs_iget_locked(s, location->objectid, root);
3694         if (!inode)
3695                 return ERR_PTR(-ENOMEM);
3696
3697         if (inode->i_state & I_NEW) {
3698                 BTRFS_I(inode)->root = root;
3699                 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
3700                 btrfs_read_locked_inode(inode);
3701
3702                 inode_tree_add(inode);
3703                 unlock_new_inode(inode);
3704         }
3705
3706         return inode;
3707 }
3708
3709 static struct inode *new_simple_dir(struct super_block *s,
3710                                     struct btrfs_key *key,
3711                                     struct btrfs_root *root)
3712 {
3713         struct inode *inode = new_inode(s);
3714
3715         if (!inode)
3716                 return ERR_PTR(-ENOMEM);
3717
3718         init_btrfs_i(inode);
3719
3720         BTRFS_I(inode)->root = root;
3721         memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
3722         BTRFS_I(inode)->dummy_inode = 1;
3723
3724         inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
3725         inode->i_op = &simple_dir_inode_operations;
3726         inode->i_fop = &simple_dir_operations;
3727         inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
3728         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
3729
3730         return inode;
3731 }
3732
3733 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
3734 {
3735         struct inode *inode;
3736         struct btrfs_root *root = BTRFS_I(dir)->root;
3737         struct btrfs_root *sub_root = root;
3738         struct btrfs_key location;
3739         int index;
3740         int ret;
3741
3742         dentry->d_op = &btrfs_dentry_operations;
3743
3744         if (dentry->d_name.len > BTRFS_NAME_LEN)
3745                 return ERR_PTR(-ENAMETOOLONG);
3746
3747         ret = btrfs_inode_by_name(dir, dentry, &location);
3748
3749         if (ret < 0)
3750                 return ERR_PTR(ret);
3751
3752         if (location.objectid == 0)
3753                 return NULL;
3754
3755         if (location.type == BTRFS_INODE_ITEM_KEY) {
3756                 inode = btrfs_iget(dir->i_sb, &location, root);
3757                 return inode;
3758         }
3759
3760         BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
3761
3762         index = srcu_read_lock(&root->fs_info->subvol_srcu);
3763         ret = fixup_tree_root_location(root, dir, dentry,
3764                                        &location, &sub_root);
3765         if (ret < 0) {
3766                 if (ret != -ENOENT)
3767                         inode = ERR_PTR(ret);
3768                 else
3769                         inode = new_simple_dir(dir->i_sb, &location, sub_root);
3770         } else {
3771                 inode = btrfs_iget(dir->i_sb, &location, sub_root);
3772         }
3773         srcu_read_unlock(&root->fs_info->subvol_srcu, index);
3774
3775         if (root != sub_root) {
3776                 down_read(&root->fs_info->cleanup_work_sem);
3777                 if (!(inode->i_sb->s_flags & MS_RDONLY))
3778                         btrfs_orphan_cleanup(sub_root);
3779                 up_read(&root->fs_info->cleanup_work_sem);
3780         }
3781
3782         return inode;
3783 }
3784
3785 static int btrfs_dentry_delete(struct dentry *dentry)
3786 {
3787         struct btrfs_root *root;
3788
3789         if (!dentry->d_inode && !IS_ROOT(dentry))
3790                 dentry = dentry->d_parent;
3791
3792         if (dentry->d_inode) {
3793                 root = BTRFS_I(dentry->d_inode)->root;
3794                 if (btrfs_root_refs(&root->root_item) == 0)
3795                         return 1;
3796         }
3797         return 0;
3798 }
3799
3800 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
3801                                    struct nameidata *nd)
3802 {
3803         struct inode *inode;
3804
3805         inode = btrfs_lookup_dentry(dir, dentry);
3806         if (IS_ERR(inode))
3807                 return ERR_CAST(inode);
3808
3809         return d_splice_alias(inode, dentry);
3810 }
3811
3812 static unsigned char btrfs_filetype_table[] = {
3813         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
3814 };
3815
3816 static int btrfs_real_readdir(struct file *filp, void *dirent,
3817                               filldir_t filldir)
3818 {
3819         struct inode *inode = filp->f_dentry->d_inode;
3820         struct btrfs_root *root = BTRFS_I(inode)->root;
3821         struct btrfs_item *item;
3822         struct btrfs_dir_item *di;
3823         struct btrfs_key key;
3824         struct btrfs_key found_key;
3825         struct btrfs_path *path;
3826         int ret;
3827         u32 nritems;
3828         struct extent_buffer *leaf;
3829         int slot;
3830         int advance;
3831         unsigned char d_type;
3832         int over = 0;
3833         u32 di_cur;
3834         u32 di_total;
3835         u32 di_len;
3836         int key_type = BTRFS_DIR_INDEX_KEY;
3837         char tmp_name[32];
3838         char *name_ptr;
3839         int name_len;
3840
3841         /* FIXME, use a real flag for deciding about the key type */
3842         if (root->fs_info->tree_root == root)
3843                 key_type = BTRFS_DIR_ITEM_KEY;
3844
3845         /* special case for "." */
3846         if (filp->f_pos == 0) {
3847                 over = filldir(dirent, ".", 1,
3848                                1, inode->i_ino,
3849                                DT_DIR);
3850                 if (over)
3851                         return 0;
3852                 filp->f_pos = 1;
3853         }
3854         /* special case for .., just use the back ref */
3855         if (filp->f_pos == 1) {
3856                 u64 pino = parent_ino(filp->f_path.dentry);
3857                 over = filldir(dirent, "..", 2,
3858                                2, pino, DT_DIR);
3859                 if (over)
3860                         return 0;
3861                 filp->f_pos = 2;
3862         }
3863         path = btrfs_alloc_path();
3864         path->reada = 2;
3865
3866         btrfs_set_key_type(&key, key_type);
3867         key.offset = filp->f_pos;
3868         key.objectid = inode->i_ino;
3869
3870         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3871         if (ret < 0)
3872                 goto err;
3873         advance = 0;
3874
3875         while (1) {
3876                 leaf = path->nodes[0];
3877                 nritems = btrfs_header_nritems(leaf);
3878                 slot = path->slots[0];
3879                 if (advance || slot >= nritems) {
3880                         if (slot >= nritems - 1) {
3881                                 ret = btrfs_next_leaf(root, path);
3882                                 if (ret)
3883                                         break;
3884                                 leaf = path->nodes[0];
3885                                 nritems = btrfs_header_nritems(leaf);
3886                                 slot = path->slots[0];
3887                         } else {
3888                                 slot++;
3889                                 path->slots[0]++;
3890                         }
3891                 }
3892
3893                 advance = 1;
3894                 item = btrfs_item_nr(leaf, slot);
3895                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
3896
3897                 if (found_key.objectid != key.objectid)
3898                         break;
3899                 if (btrfs_key_type(&found_key) != key_type)
3900                         break;
3901                 if (found_key.offset < filp->f_pos)
3902                         continue;
3903
3904                 filp->f_pos = found_key.offset;
3905
3906                 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
3907                 di_cur = 0;
3908                 di_total = btrfs_item_size(leaf, item);
3909
3910                 while (di_cur < di_total) {
3911                         struct btrfs_key location;
3912
3913                         name_len = btrfs_dir_name_len(leaf, di);
3914                         if (name_len <= sizeof(tmp_name)) {
3915                                 name_ptr = tmp_name;
3916                         } else {
3917                                 name_ptr = kmalloc(name_len, GFP_NOFS);
3918                                 if (!name_ptr) {
3919                                         ret = -ENOMEM;
3920                                         goto err;
3921                                 }
3922                         }
3923                         read_extent_buffer(leaf, name_ptr,
3924                                            (unsigned long)(di + 1), name_len);
3925
3926                         d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
3927                         btrfs_dir_item_key_to_cpu(leaf, di, &location);
3928
3929                         /* is this a reference to our own snapshot? If so
3930                          * skip it
3931                          */
3932                         if (location.type == BTRFS_ROOT_ITEM_KEY &&
3933                             location.objectid == root->root_key.objectid) {
3934                                 over = 0;
3935                                 goto skip;
3936                         }
3937                         over = filldir(dirent, name_ptr, name_len,
3938                                        found_key.offset, location.objectid,
3939                                        d_type);
3940
3941 skip:
3942                         if (name_ptr != tmp_name)
3943                                 kfree(name_ptr);
3944
3945                         if (over)
3946                                 goto nopos;
3947                         di_len = btrfs_dir_name_len(leaf, di) +
3948                                  btrfs_dir_data_len(leaf, di) + sizeof(*di);
3949                         di_cur += di_len;
3950                         di = (struct btrfs_dir_item *)((char *)di + di_len);
3951                 }
3952         }
3953
3954         /* Reached end of directory/root. Bump pos past the last item. */
3955         if (key_type == BTRFS_DIR_INDEX_KEY)
3956                 /*
3957                  * 32-bit glibc will use getdents64, but then strtol -
3958                  * so the last number we can serve is this.
3959                  */
3960                 filp->f_pos = 0x7fffffff;
3961         else
3962                 filp->f_pos++;
3963 nopos:
3964         ret = 0;
3965 err:
3966         btrfs_free_path(path);
3967         return ret;
3968 }
3969
3970 int btrfs_write_inode(struct inode *inode, int wait)
3971 {
3972         struct btrfs_root *root = BTRFS_I(inode)->root;
3973         struct btrfs_trans_handle *trans;
3974         int ret = 0;
3975
3976         if (root->fs_info->btree_inode == inode)
3977                 return 0;
3978
3979         if (wait) {
3980                 trans = btrfs_join_transaction(root, 1);
3981                 btrfs_set_trans_block_group(trans, inode);
3982                 ret = btrfs_commit_transaction(trans, root);
3983         }
3984         return ret;
3985 }
3986
3987 /*
3988  * This is somewhat expensive, updating the tree every time the
3989  * inode changes.  But, it is most likely to find the inode in cache.
3990  * FIXME, needs more benchmarking...there are no reasons other than performance
3991  * to keep or drop this code.
3992  */
3993 void btrfs_dirty_inode(struct inode *inode)
3994 {
3995         struct btrfs_root *root = BTRFS_I(inode)->root;
3996         struct btrfs_trans_handle *trans;
3997
3998         trans = btrfs_join_transaction(root, 1);
3999         btrfs_set_trans_block_group(trans, inode);
4000         btrfs_update_inode(trans, root, inode);
4001         btrfs_end_transaction(trans, root);
4002 }
4003
4004 /*
4005  * find the highest existing sequence number in a directory
4006  * and then set the in-memory index_cnt variable to reflect
4007  * free sequence numbers
4008  */
4009 static int btrfs_set_inode_index_count(struct inode *inode)
4010 {
4011         struct btrfs_root *root = BTRFS_I(inode)->root;
4012         struct btrfs_key key, found_key;
4013         struct btrfs_path *path;
4014         struct extent_buffer *leaf;
4015         int ret;
4016
4017         key.objectid = inode->i_ino;
4018         btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4019         key.offset = (u64)-1;
4020
4021         path = btrfs_alloc_path();
4022         if (!path)
4023                 return -ENOMEM;
4024
4025         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4026         if (ret < 0)
4027                 goto out;
4028         /* FIXME: we should be able to handle this */
4029         if (ret == 0)
4030                 goto out;
4031         ret = 0;
4032
4033         /*
4034          * MAGIC NUMBER EXPLANATION:
4035          * since we search a directory based on f_pos we have to start at 2
4036          * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4037          * else has to start at 2
4038          */
4039         if (path->slots[0] == 0) {
4040                 BTRFS_I(inode)->index_cnt = 2;
4041                 goto out;
4042         }
4043
4044         path->slots[0]--;
4045
4046         leaf = path->nodes[0];
4047         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4048
4049         if (found_key.objectid != inode->i_ino ||
4050             btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4051                 BTRFS_I(inode)->index_cnt = 2;
4052                 goto out;
4053         }
4054
4055         BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4056 out:
4057         btrfs_free_path(path);
4058         return ret;
4059 }
4060
4061 /*
4062  * helper to find a free sequence number in a given directory.  This current
4063  * code is very simple, later versions will do smarter things in the btree
4064  */
4065 int btrfs_set_inode_index(struct inode *dir, u64 *index)
4066 {
4067         int ret = 0;
4068
4069         if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4070                 ret = btrfs_set_inode_index_count(dir);
4071                 if (ret)
4072                         return ret;
4073         }
4074
4075         *index = BTRFS_I(dir)->index_cnt;
4076         BTRFS_I(dir)->index_cnt++;
4077
4078         return ret;
4079 }
4080
4081 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4082                                      struct btrfs_root *root,
4083                                      struct inode *dir,
4084                                      const char *name, int name_len,
4085                                      u64 ref_objectid, u64 objectid,
4086                                      u64 alloc_hint, int mode, u64 *index)
4087 {
4088         struct inode *inode;
4089         struct btrfs_inode_item *inode_item;
4090         struct btrfs_key *location;
4091         struct btrfs_path *path;
4092         struct btrfs_inode_ref *ref;
4093         struct btrfs_key key[2];
4094         u32 sizes[2];
4095         unsigned long ptr;
4096         int ret;
4097         int owner;
4098
4099         path = btrfs_alloc_path();
4100         BUG_ON(!path);
4101
4102         inode = new_inode(root->fs_info->sb);
4103         if (!inode)
4104                 return ERR_PTR(-ENOMEM);
4105
4106         if (dir) {
4107                 ret = btrfs_set_inode_index(dir, index);
4108                 if (ret) {
4109                         iput(inode);
4110                         return ERR_PTR(ret);
4111                 }
4112         }
4113         /*
4114          * index_cnt is ignored for everything but a dir,
4115          * btrfs_get_inode_index_count has an explanation for the magic
4116          * number
4117          */
4118         init_btrfs_i(inode);
4119         BTRFS_I(inode)->index_cnt = 2;
4120         BTRFS_I(inode)->root = root;
4121         BTRFS_I(inode)->generation = trans->transid;
4122         btrfs_set_inode_space_info(root, inode);
4123
4124         if (mode & S_IFDIR)
4125                 owner = 0;
4126         else
4127                 owner = 1;
4128         BTRFS_I(inode)->block_group =
4129                         btrfs_find_block_group(root, 0, alloc_hint, owner);
4130
4131         key[0].objectid = objectid;
4132         btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4133         key[0].offset = 0;
4134
4135         key[1].objectid = objectid;
4136         btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4137         key[1].offset = ref_objectid;
4138
4139         sizes[0] = sizeof(struct btrfs_inode_item);
4140         sizes[1] = name_len + sizeof(*ref);
4141
4142         path->leave_spinning = 1;
4143         ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4144         if (ret != 0)
4145                 goto fail;
4146
4147         inode->i_uid = current_fsuid();
4148
4149         if (dir && (dir->i_mode & S_ISGID)) {
4150                 inode->i_gid = dir->i_gid;
4151                 if (S_ISDIR(mode))
4152                         mode |= S_ISGID;
4153         } else
4154                 inode->i_gid = current_fsgid();
4155
4156         inode->i_mode = mode;
4157         inode->i_ino = objectid;
4158         inode_set_bytes(inode, 0);
4159         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4160         inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4161                                   struct btrfs_inode_item);
4162         fill_inode_item(trans, path->nodes[0], inode_item, inode);
4163
4164         ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4165                              struct btrfs_inode_ref);
4166         btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
4167         btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
4168         ptr = (unsigned long)(ref + 1);
4169         write_extent_buffer(path->nodes[0], name, ptr, name_len);
4170
4171         btrfs_mark_buffer_dirty(path->nodes[0]);
4172         btrfs_free_path(path);
4173
4174         location = &BTRFS_I(inode)->location;
4175         location->objectid = objectid;
4176         location->offset = 0;
4177         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4178
4179         btrfs_inherit_iflags(inode, dir);
4180
4181         if ((mode & S_IFREG)) {
4182                 if (btrfs_test_opt(root, NODATASUM))
4183                         BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4184                 if (btrfs_test_opt(root, NODATACOW))
4185                         BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4186         }
4187
4188         insert_inode_hash(inode);
4189         inode_tree_add(inode);
4190         return inode;
4191 fail:
4192         if (dir)
4193                 BTRFS_I(dir)->index_cnt--;
4194         btrfs_free_path(path);
4195         iput(inode);
4196         return ERR_PTR(ret);
4197 }
4198
4199 static inline u8 btrfs_inode_type(struct inode *inode)
4200 {
4201         return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4202 }
4203
4204 /*
4205  * utility function to add 'inode' into 'parent_inode' with
4206  * a give name and a given sequence number.
4207  * if 'add_backref' is true, also insert a backref from the
4208  * inode to the parent directory.
4209  */
4210 int btrfs_add_link(struct btrfs_trans_handle *trans,
4211                    struct inode *parent_inode, struct inode *inode,
4212                    const char *name, int name_len, int add_backref, u64 index)
4213 {
4214         int ret = 0;
4215         struct btrfs_key key;
4216         struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4217
4218         if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4219                 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4220         } else {
4221                 key.objectid = inode->i_ino;
4222                 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4223                 key.offset = 0;
4224         }
4225
4226         if (unlikely(inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
4227                 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4228                                          key.objectid, root->root_key.objectid,
4229                                          parent_inode->i_ino,
4230                                          index, name, name_len);
4231         } else if (add_backref) {
4232                 ret = btrfs_insert_inode_ref(trans, root,
4233                                              name, name_len, inode->i_ino,
4234                                              parent_inode->i_ino, index);
4235         }
4236
4237         if (ret == 0) {
4238                 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4239                                             parent_inode->i_ino, &key,
4240                                             btrfs_inode_type(inode), index);
4241                 BUG_ON(ret);
4242
4243                 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4244                                    name_len * 2);
4245                 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4246                 ret = btrfs_update_inode(trans, root, parent_inode);
4247         }
4248         return ret;
4249 }
4250
4251 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
4252                             struct dentry *dentry, struct inode *inode,
4253                             int backref, u64 index)
4254 {
4255         int err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4256                                  inode, dentry->d_name.name,
4257                                  dentry->d_name.len, backref, index);
4258         if (!err) {
4259                 d_instantiate(dentry, inode);
4260                 return 0;
4261         }
4262         if (err > 0)
4263                 err = -EEXIST;
4264         return err;
4265 }
4266
4267 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4268                         int mode, dev_t rdev)
4269 {
4270         struct btrfs_trans_handle *trans;
4271         struct btrfs_root *root = BTRFS_I(dir)->root;
4272         struct inode *inode = NULL;
4273         int err;
4274         int drop_inode = 0;
4275         u64 objectid;
4276         unsigned long nr = 0;
4277         u64 index = 0;
4278
4279         if (!new_valid_dev(rdev))
4280                 return -EINVAL;
4281
4282         /*
4283          * 2 for inode item and ref
4284          * 2 for dir items
4285          * 1 for xattr if selinux is on
4286          */
4287         err = btrfs_reserve_metadata_space(root, 5);
4288         if (err)
4289                 return err;
4290
4291         trans = btrfs_start_transaction(root, 1);
4292         if (!trans)
4293                 goto fail;
4294         btrfs_set_trans_block_group(trans, dir);
4295
4296         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4297         if (err) {
4298                 err = -ENOSPC;
4299                 goto out_unlock;
4300         }
4301
4302         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4303                                 dentry->d_name.len,
4304                                 dentry->d_parent->d_inode->i_ino, objectid,
4305                                 BTRFS_I(dir)->block_group, mode, &index);
4306         err = PTR_ERR(inode);
4307         if (IS_ERR(inode))
4308                 goto out_unlock;
4309
4310         err = btrfs_init_inode_security(trans, inode, dir);
4311         if (err) {
4312                 drop_inode = 1;
4313                 goto out_unlock;
4314         }
4315
4316         btrfs_set_trans_block_group(trans, inode);
4317         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
4318         if (err)
4319                 drop_inode = 1;
4320         else {
4321                 inode->i_op = &btrfs_special_inode_operations;
4322                 init_special_inode(inode, inode->i_mode, rdev);
4323                 btrfs_update_inode(trans, root, inode);
4324         }
4325         btrfs_update_inode_block_group(trans, inode);
4326         btrfs_update_inode_block_group(trans, dir);
4327 out_unlock:
4328         nr = trans->blocks_used;
4329         btrfs_end_transaction_throttle(trans, root);
4330 fail:
4331         btrfs_unreserve_metadata_space(root, 5);
4332         if (drop_inode) {
4333                 inode_dec_link_count(inode);
4334                 iput(inode);
4335         }
4336         btrfs_btree_balance_dirty(root, nr);
4337         return err;
4338 }
4339
4340 static int btrfs_create(struct inode *dir, struct dentry *dentry,
4341                         int mode, struct nameidata *nd)
4342 {
4343         struct btrfs_trans_handle *trans;
4344         struct btrfs_root *root = BTRFS_I(dir)->root;
4345         struct inode *inode = NULL;
4346         int err;
4347         int drop_inode = 0;
4348         unsigned long nr = 0;
4349         u64 objectid;
4350         u64 index = 0;
4351
4352         /*
4353          * 2 for inode item and ref
4354          * 2 for dir items
4355          * 1 for xattr if selinux is on
4356          */
4357         err = btrfs_reserve_metadata_space(root, 5);
4358         if (err)
4359                 return err;
4360
4361         trans = btrfs_start_transaction(root, 1);
4362         if (!trans)
4363                 goto fail;
4364         btrfs_set_trans_block_group(trans, dir);
4365
4366         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4367         if (err) {
4368                 err = -ENOSPC;
4369                 goto out_unlock;
4370         }
4371
4372         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4373                                 dentry->d_name.len,
4374                                 dentry->d_parent->d_inode->i_ino,
4375                                 objectid, BTRFS_I(dir)->block_group, mode,
4376                                 &index);
4377         err = PTR_ERR(inode);
4378         if (IS_ERR(inode))
4379                 goto out_unlock;
4380
4381         err = btrfs_init_inode_security(trans, inode, dir);
4382         if (err) {
4383                 drop_inode = 1;
4384                 goto out_unlock;
4385         }
4386
4387         btrfs_set_trans_block_group(trans, inode);
4388         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
4389         if (err)
4390                 drop_inode = 1;
4391         else {
4392                 inode->i_mapping->a_ops = &btrfs_aops;
4393                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4394                 inode->i_fop = &btrfs_file_operations;
4395                 inode->i_op = &btrfs_file_inode_operations;
4396                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4397         }
4398         btrfs_update_inode_block_group(trans, inode);
4399         btrfs_update_inode_block_group(trans, dir);
4400 out_unlock:
4401         nr = trans->blocks_used;
4402         btrfs_end_transaction_throttle(trans, root);
4403 fail:
4404         btrfs_unreserve_metadata_space(root, 5);
4405         if (drop_inode) {
4406                 inode_dec_link_count(inode);
4407                 iput(inode);
4408         }
4409         btrfs_btree_balance_dirty(root, nr);
4410         return err;
4411 }
4412
4413 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4414                       struct dentry *dentry)
4415 {
4416         struct btrfs_trans_handle *trans;
4417         struct btrfs_root *root = BTRFS_I(dir)->root;
4418         struct inode *inode = old_dentry->d_inode;
4419         u64 index;
4420         unsigned long nr = 0;
4421         int err;
4422         int drop_inode = 0;
4423
4424         if (inode->i_nlink == 0)
4425                 return -ENOENT;
4426
4427         /* do not allow sys_link's with other subvols of the same device */
4428         if (root->objectid != BTRFS_I(inode)->root->objectid)
4429                 return -EPERM;
4430
4431         /*
4432          * 1 item for inode ref
4433          * 2 items for dir items
4434          */
4435         err = btrfs_reserve_metadata_space(root, 3);
4436         if (err)
4437                 return err;
4438
4439         btrfs_inc_nlink(inode);
4440
4441         err = btrfs_set_inode_index(dir, &index);
4442         if (err)
4443                 goto fail;
4444
4445         trans = btrfs_start_transaction(root, 1);
4446
4447         btrfs_set_trans_block_group(trans, dir);
4448         atomic_inc(&inode->i_count);
4449
4450         err = btrfs_add_nondir(trans, dentry, inode, 1, index);
4451
4452         if (err) {
4453                 drop_inode = 1;
4454         } else {
4455                 btrfs_update_inode_block_group(trans, dir);
4456                 err = btrfs_update_inode(trans, root, inode);
4457                 BUG_ON(err);
4458                 btrfs_log_new_name(trans, inode, NULL, dentry->d_parent);
4459         }
4460
4461         nr = trans->blocks_used;
4462         btrfs_end_transaction_throttle(trans, root);
4463 fail:
4464         btrfs_unreserve_metadata_space(root, 3);
4465         if (drop_inode) {
4466                 inode_dec_link_count(inode);
4467                 iput(inode);
4468         }
4469         btrfs_btree_balance_dirty(root, nr);
4470         return err;
4471 }
4472
4473 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4474 {
4475         struct inode *inode = NULL;
4476         struct btrfs_trans_handle *trans;
4477         struct btrfs_root *root = BTRFS_I(dir)->root;
4478         int err = 0;
4479         int drop_on_err = 0;
4480         u64 objectid = 0;
4481         u64 index = 0;
4482         unsigned long nr = 1;
4483
4484         /*
4485          * 2 items for inode and ref
4486          * 2 items for dir items
4487          * 1 for xattr if selinux is on
4488          */
4489         err = btrfs_reserve_metadata_space(root, 5);
4490         if (err)
4491                 return err;
4492
4493         trans = btrfs_start_transaction(root, 1);
4494         if (!trans) {
4495                 err = -ENOMEM;
4496                 goto out_unlock;
4497         }
4498         btrfs_set_trans_block_group(trans, dir);
4499
4500         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
4501         if (err) {
4502                 err = -ENOSPC;
4503                 goto out_unlock;
4504         }
4505
4506         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4507                                 dentry->d_name.len,
4508                                 dentry->d_parent->d_inode->i_ino, objectid,
4509                                 BTRFS_I(dir)->block_group, S_IFDIR | mode,
4510                                 &index);
4511         if (IS_ERR(inode)) {
4512                 err = PTR_ERR(inode);
4513                 goto out_fail;
4514         }
4515
4516         drop_on_err = 1;
4517
4518         err = btrfs_init_inode_security(trans, inode, dir);
4519         if (err)
4520                 goto out_fail;
4521
4522         inode->i_op = &btrfs_dir_inode_operations;
4523         inode->i_fop = &btrfs_dir_file_operations;
4524         btrfs_set_trans_block_group(trans, inode);
4525
4526         btrfs_i_size_write(inode, 0);
4527         err = btrfs_update_inode(trans, root, inode);
4528         if (err)
4529                 goto out_fail;
4530
4531         err = btrfs_add_link(trans, dentry->d_parent->d_inode,
4532                                  inode, dentry->d_name.name,
4533                                  dentry->d_name.len, 0, index);
4534         if (err)
4535                 goto out_fail;
4536
4537         d_instantiate(dentry, inode);
4538         drop_on_err = 0;
4539         btrfs_update_inode_block_group(trans, inode);
4540         btrfs_update_inode_block_group(trans, dir);
4541
4542 out_fail:
4543         nr = trans->blocks_used;
4544         btrfs_end_transaction_throttle(trans, root);
4545
4546 out_unlock:
4547         btrfs_unreserve_metadata_space(root, 5);
4548         if (drop_on_err)
4549                 iput(inode);
4550         btrfs_btree_balance_dirty(root, nr);
4551         return err;
4552 }
4553
4554 /* helper for btfs_get_extent.  Given an existing extent in the tree,
4555  * and an extent that you want to insert, deal with overlap and insert
4556  * the new extent into the tree.
4557  */
4558 static int merge_extent_mapping(struct extent_map_tree *em_tree,
4559                                 struct extent_map *existing,
4560                                 struct extent_map *em,
4561                                 u64 map_start, u64 map_len)
4562 {
4563         u64 start_diff;
4564
4565         BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
4566         start_diff = map_start - em->start;
4567         em->start = map_start;
4568         em->len = map_len;
4569         if (em->block_start < EXTENT_MAP_LAST_BYTE &&
4570             !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
4571                 em->block_start += start_diff;
4572                 em->block_len -= start_diff;
4573         }
4574         return add_extent_mapping(em_tree, em);
4575 }
4576
4577 static noinline int uncompress_inline(struct btrfs_path *path,
4578                                       struct inode *inode, struct page *page,
4579                                       size_t pg_offset, u64 extent_offset,
4580                                       struct btrfs_file_extent_item *item)
4581 {
4582         int ret;
4583         struct extent_buffer *leaf = path->nodes[0];
4584         char *tmp;
4585         size_t max_size;
4586         unsigned long inline_size;
4587         unsigned long ptr;
4588
4589         WARN_ON(pg_offset != 0);
4590         max_size = btrfs_file_extent_ram_bytes(leaf, item);
4591         inline_size = btrfs_file_extent_inline_item_len(leaf,
4592                                         btrfs_item_nr(leaf, path->slots[0]));
4593         tmp = kmalloc(inline_size, GFP_NOFS);
4594         ptr = btrfs_file_extent_inline_start(item);
4595
4596         read_extent_buffer(leaf, tmp, ptr, inline_size);
4597
4598         max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
4599         ret = btrfs_zlib_decompress(tmp, page, extent_offset,
4600                                     inline_size, max_size);
4601         if (ret) {
4602                 char *kaddr = kmap_atomic(page, KM_USER0);
4603                 unsigned long copy_size = min_t(u64,
4604                                   PAGE_CACHE_SIZE - pg_offset,
4605                                   max_size - extent_offset);
4606                 memset(kaddr + pg_offset, 0, copy_size);
4607                 kunmap_atomic(kaddr, KM_USER0);
4608         }
4609         kfree(tmp);
4610         return 0;
4611 }
4612
4613 /*
4614  * a bit scary, this does extent mapping from logical file offset to the disk.
4615  * the ugly parts come from merging extents from the disk with the in-ram
4616  * representation.  This gets more complex because of the data=ordered code,
4617  * where the in-ram extents might be locked pending data=ordered completion.
4618  *
4619  * This also copies inline extents directly into the page.
4620  */
4621
4622 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
4623                                     size_t pg_offset, u64 start, u64 len,
4624                                     int create)
4625 {
4626         int ret;
4627         int err = 0;
4628         u64 bytenr;
4629         u64 extent_start = 0;
4630         u64 extent_end = 0;
4631         u64 objectid = inode->i_ino;
4632         u32 found_type;
4633         struct btrfs_path *path = NULL;
4634         struct btrfs_root *root = BTRFS_I(inode)->root;
4635         struct btrfs_file_extent_item *item;
4636         struct extent_buffer *leaf;
4637         struct btrfs_key found_key;
4638         struct extent_map *em = NULL;
4639         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4640         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4641         struct btrfs_trans_handle *trans = NULL;
4642         int compressed;
4643
4644 again:
4645         read_lock(&em_tree->lock);
4646         em = lookup_extent_mapping(em_tree, start, len);
4647         if (em)
4648                 em->bdev = root->fs_info->fs_devices->latest_bdev;
4649         read_unlock(&em_tree->lock);
4650
4651         if (em) {
4652                 if (em->start > start || em->start + em->len <= start)
4653                         free_extent_map(em);
4654                 else if (em->block_start == EXTENT_MAP_INLINE && page)
4655                         free_extent_map(em);
4656                 else
4657                         goto out;
4658         }
4659         em = alloc_extent_map(GFP_NOFS);
4660         if (!em) {
4661                 err = -ENOMEM;
4662                 goto out;
4663         }
4664         em->bdev = root->fs_info->fs_devices->latest_bdev;
4665         em->start = EXTENT_MAP_HOLE;
4666         em->orig_start = EXTENT_MAP_HOLE;
4667         em->len = (u64)-1;
4668         em->block_len = (u64)-1;
4669
4670         if (!path) {
4671                 path = btrfs_alloc_path();
4672                 BUG_ON(!path);
4673         }
4674
4675         ret = btrfs_lookup_file_extent(trans, root, path,
4676                                        objectid, start, trans != NULL);
4677         if (ret < 0) {
4678                 err = ret;
4679                 goto out;
4680         }
4681
4682         if (ret != 0) {
4683                 if (path->slots[0] == 0)
4684                         goto not_found;
4685                 path->slots[0]--;
4686         }
4687
4688         leaf = path->nodes[0];
4689         item = btrfs_item_ptr(leaf, path->slots[0],
4690                               struct btrfs_file_extent_item);
4691         /* are we inside the extent that was found? */
4692         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4693         found_type = btrfs_key_type(&found_key);
4694         if (found_key.objectid != objectid ||
4695             found_type != BTRFS_EXTENT_DATA_KEY) {
4696                 goto not_found;
4697         }
4698
4699         found_type = btrfs_file_extent_type(leaf, item);
4700         extent_start = found_key.offset;
4701         compressed = btrfs_file_extent_compression(leaf, item);
4702         if (found_type == BTRFS_FILE_EXTENT_REG ||
4703             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
4704                 extent_end = extent_start +
4705                        btrfs_file_extent_num_bytes(leaf, item);
4706         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4707                 size_t size;
4708                 size = btrfs_file_extent_inline_len(leaf, item);
4709                 extent_end = (extent_start + size + root->sectorsize - 1) &
4710                         ~((u64)root->sectorsize - 1);
4711         }
4712
4713         if (start >= extent_end) {
4714                 path->slots[0]++;
4715                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
4716                         ret = btrfs_next_leaf(root, path);
4717                         if (ret < 0) {
4718                                 err = ret;
4719                                 goto out;
4720                         }
4721                         if (ret > 0)
4722                                 goto not_found;
4723                         leaf = path->nodes[0];
4724                 }
4725                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4726                 if (found_key.objectid != objectid ||
4727                     found_key.type != BTRFS_EXTENT_DATA_KEY)
4728                         goto not_found;
4729                 if (start + len <= found_key.offset)
4730                         goto not_found;
4731                 em->start = start;
4732                 em->len = found_key.offset - start;
4733                 goto not_found_em;
4734         }
4735
4736         if (found_type == BTRFS_FILE_EXTENT_REG ||
4737             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
4738                 em->start = extent_start;
4739                 em->len = extent_end - extent_start;
4740                 em->orig_start = extent_start -
4741                                  btrfs_file_extent_offset(leaf, item);
4742                 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
4743                 if (bytenr == 0) {
4744                         em->block_start = EXTENT_MAP_HOLE;
4745                         goto insert;
4746                 }
4747                 if (compressed) {
4748                         set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4749                         em->block_start = bytenr;
4750                         em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
4751                                                                          item);
4752                 } else {
4753                         bytenr += btrfs_file_extent_offset(leaf, item);
4754                         em->block_start = bytenr;
4755                         em->block_len = em->len;
4756                         if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
4757                                 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
4758                 }
4759                 goto insert;
4760         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
4761                 unsigned long ptr;
4762                 char *map;
4763                 size_t size;
4764                 size_t extent_offset;
4765                 size_t copy_size;
4766
4767                 em->block_start = EXTENT_MAP_INLINE;
4768                 if (!page || create) {
4769                         em->start = extent_start;
4770                         em->len = extent_end - extent_start;
4771                         goto out;
4772                 }
4773
4774                 size = btrfs_file_extent_inline_len(leaf, item);
4775                 extent_offset = page_offset(page) + pg_offset - extent_start;
4776                 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
4777                                 size - extent_offset);
4778                 em->start = extent_start + extent_offset;
4779                 em->len = (copy_size + root->sectorsize - 1) &
4780                         ~((u64)root->sectorsize - 1);
4781                 em->orig_start = EXTENT_MAP_INLINE;
4782                 if (compressed)
4783                         set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
4784                 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
4785                 if (create == 0 && !PageUptodate(page)) {
4786                         if (btrfs_file_extent_compression(leaf, item) ==
4787                             BTRFS_COMPRESS_ZLIB) {
4788                                 ret = uncompress_inline(path, inode, page,
4789                                                         pg_offset,
4790                                                         extent_offset, item);
4791                                 BUG_ON(ret);
4792                         } else {
4793                                 map = kmap(page);
4794                                 read_extent_buffer(leaf, map + pg_offset, ptr,
4795                                                    copy_size);
4796                                 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
4797                                         memset(map + pg_offset + copy_size, 0,
4798                                                PAGE_CACHE_SIZE - pg_offset -
4799                                                copy_size);
4800                                 }
4801                                 kunmap(page);
4802                         }
4803                         flush_dcache_page(page);
4804                 } else if (create && PageUptodate(page)) {
4805                         if (!trans) {
4806                                 kunmap(page);
4807                                 free_extent_map(em);
4808                                 em = NULL;
4809                                 btrfs_release_path(root, path);
4810                                 trans = btrfs_join_transaction(root, 1);
4811                                 goto again;
4812                         }
4813                         map = kmap(page);
4814                         write_extent_buffer(leaf, map + pg_offset, ptr,
4815                                             copy_size);
4816                         kunmap(page);
4817                         btrfs_mark_buffer_dirty(leaf);
4818                 }
4819                 set_extent_uptodate(io_tree, em->start,
4820                                     extent_map_end(em) - 1, GFP_NOFS);
4821                 goto insert;
4822         } else {
4823                 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
4824                 WARN_ON(1);
4825         }
4826 not_found:
4827         em->start = start;
4828         em->len = len;
4829 not_found_em:
4830         em->block_start = EXTENT_MAP_HOLE;
4831         set_bit(EXTENT_FLAG_VACANCY, &em->flags);
4832 insert:
4833         btrfs_release_path(root, path);
4834         if (em->start > start || extent_map_end(em) <= start) {
4835                 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
4836                        "[%llu %llu]\n", (unsigned long long)em->start,
4837                        (unsigned long long)em->len,
4838                        (unsigned long long)start,
4839                        (unsigned long long)len);
4840                 err = -EIO;
4841                 goto out;
4842         }
4843
4844         err = 0;
4845         write_lock(&em_tree->lock);
4846         ret = add_extent_mapping(em_tree, em);
4847         /* it is possible that someone inserted the extent into the tree
4848          * while we had the lock dropped.  It is also possible that
4849          * an overlapping map exists in the tree
4850          */
4851         if (ret == -EEXIST) {
4852                 struct extent_map *existing;
4853
4854                 ret = 0;
4855
4856                 existing = lookup_extent_mapping(em_tree, start, len);
4857                 if (existing && (existing->start > start ||
4858                     existing->start + existing->len <= start)) {
4859                         free_extent_map(existing);
4860                         existing = NULL;
4861                 }
4862                 if (!existing) {
4863                         existing = lookup_extent_mapping(em_tree, em->start,
4864                                                          em->len);
4865                         if (existing) {
4866                                 err = merge_extent_mapping(em_tree, existing,
4867                                                            em, start,
4868                                                            root->sectorsize);
4869                                 free_extent_map(existing);
4870                                 if (err) {
4871                                         free_extent_map(em);
4872                                         em = NULL;
4873                                 }
4874                         } else {
4875                                 err = -EIO;
4876                                 free_extent_map(em);
4877                                 em = NULL;
4878                         }
4879                 } else {
4880                         free_extent_map(em);
4881                         em = existing;
4882                         err = 0;
4883                 }
4884         }
4885         write_unlock(&em_tree->lock);
4886 out:
4887         if (path)
4888                 btrfs_free_path(path);
4889         if (trans) {
4890                 ret = btrfs_end_transaction(trans, root);
4891                 if (!err)
4892                         err = ret;
4893         }
4894         if (err) {
4895                 free_extent_map(em);
4896                 return ERR_PTR(err);
4897         }
4898         return em;
4899 }
4900
4901 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
4902                         const struct iovec *iov, loff_t offset,
4903                         unsigned long nr_segs)
4904 {
4905         return -EINVAL;
4906 }
4907
4908 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
4909                 __u64 start, __u64 len)
4910 {
4911         return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent);
4912 }
4913
4914 int btrfs_readpage(struct file *file, struct page *page)
4915 {
4916         struct extent_io_tree *tree;
4917         tree = &BTRFS_I(page->mapping->host)->io_tree;
4918         return extent_read_full_page(tree, page, btrfs_get_extent);
4919 }
4920
4921 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
4922 {
4923         struct extent_io_tree *tree;
4924
4925
4926         if (current->flags & PF_MEMALLOC) {
4927                 redirty_page_for_writepage(wbc, page);
4928                 unlock_page(page);
4929                 return 0;
4930         }
4931         tree = &BTRFS_I(page->mapping->host)->io_tree;
4932         return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
4933 }
4934
4935 int btrfs_writepages(struct address_space *mapping,
4936                      struct writeback_control *wbc)
4937 {
4938         struct extent_io_tree *tree;
4939
4940         tree = &BTRFS_I(mapping->host)->io_tree;
4941         return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
4942 }
4943
4944 static int
4945 btrfs_readpages(struct file *file, struct address_space *mapping,
4946                 struct list_head *pages, unsigned nr_pages)
4947 {
4948         struct extent_io_tree *tree;
4949         tree = &BTRFS_I(mapping->host)->io_tree;
4950         return extent_readpages(tree, mapping, pages, nr_pages,
4951                                 btrfs_get_extent);
4952 }
4953 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4954 {
4955         struct extent_io_tree *tree;
4956         struct extent_map_tree *map;
4957         int ret;
4958
4959         tree = &BTRFS_I(page->mapping->host)->io_tree;
4960         map = &BTRFS_I(page->mapping->host)->extent_tree;
4961         ret = try_release_extent_mapping(map, tree, page, gfp_flags);
4962         if (ret == 1) {
4963                 ClearPagePrivate(page);
4964                 set_page_private(page, 0);
4965                 page_cache_release(page);
4966         }
4967         return ret;
4968 }
4969
4970 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
4971 {
4972         if (PageWriteback(page) || PageDirty(page))
4973                 return 0;
4974         return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
4975 }
4976
4977 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
4978 {
4979         struct extent_io_tree *tree;
4980         struct btrfs_ordered_extent *ordered;
4981         u64 page_start = page_offset(page);
4982         u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
4983
4984
4985         /*
4986          * we have the page locked, so new writeback can't start,
4987          * and the dirty bit won't be cleared while we are here.
4988          *
4989          * Wait for IO on this page so that we can safely clear
4990          * the PagePrivate2 bit and do ordered accounting
4991          */
4992         wait_on_page_writeback(page);
4993
4994         tree = &BTRFS_I(page->mapping->host)->io_tree;
4995         if (offset) {
4996                 btrfs_releasepage(page, GFP_NOFS);
4997                 return;
4998         }
4999         lock_extent(tree, page_start, page_end, GFP_NOFS);
5000         ordered = btrfs_lookup_ordered_extent(page->mapping->host,
5001                                            page_offset(page));
5002         if (ordered) {
5003                 /*
5004                  * IO on this page will never be started, so we need
5005                  * to account for any ordered extents now
5006                  */
5007                 clear_extent_bit(tree, page_start, page_end,
5008                                  EXTENT_DIRTY | EXTENT_DELALLOC |
5009                                  EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
5010                                  NULL, GFP_NOFS);
5011                 /*
5012                  * whoever cleared the private bit is responsible
5013                  * for the finish_ordered_io
5014                  */
5015                 if (TestClearPagePrivate2(page)) {
5016                         btrfs_finish_ordered_io(page->mapping->host,
5017                                                 page_start, page_end);
5018                 }
5019                 btrfs_put_ordered_extent(ordered);
5020                 lock_extent(tree, page_start, page_end, GFP_NOFS);
5021         }
5022         clear_extent_bit(tree, page_start, page_end,
5023                  EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
5024                  EXTENT_DO_ACCOUNTING, 1, 1, NULL, GFP_NOFS);
5025         __btrfs_releasepage(page, GFP_NOFS);
5026
5027         ClearPageChecked(page);
5028         if (PagePrivate(page)) {
5029                 ClearPagePrivate(page);
5030                 set_page_private(page, 0);
5031                 page_cache_release(page);
5032         }
5033 }
5034
5035 /*
5036  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
5037  * called from a page fault handler when a page is first dirtied. Hence we must
5038  * be careful to check for EOF conditions here. We set the page up correctly
5039  * for a written page which means we get ENOSPC checking when writing into
5040  * holes and correct delalloc and unwritten extent mapping on filesystems that
5041  * support these features.
5042  *
5043  * We are not allowed to take the i_mutex here so we have to play games to
5044  * protect against truncate races as the page could now be beyond EOF.  Because
5045  * vmtruncate() writes the inode size before removing pages, once we have the
5046  * page lock we can determine safely if the page is beyond EOF. If it is not
5047  * beyond EOF, then the page is guaranteed safe against truncation until we
5048  * unlock the page.
5049  */
5050 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
5051 {
5052         struct page *page = vmf->page;
5053         struct inode *inode = fdentry(vma->vm_file)->d_inode;
5054         struct btrfs_root *root = BTRFS_I(inode)->root;
5055         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5056         struct btrfs_ordered_extent *ordered;
5057         char *kaddr;
5058         unsigned long zero_start;
5059         loff_t size;
5060         int ret;
5061         u64 page_start;
5062         u64 page_end;
5063
5064         ret = btrfs_check_data_free_space(root, inode, PAGE_CACHE_SIZE);
5065         if (ret) {
5066                 if (ret == -ENOMEM)
5067                         ret = VM_FAULT_OOM;
5068                 else /* -ENOSPC, -EIO, etc */
5069                         ret = VM_FAULT_SIGBUS;
5070                 goto out;
5071         }
5072
5073         ret = btrfs_reserve_metadata_for_delalloc(root, inode, 1);
5074         if (ret) {
5075                 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5076                 ret = VM_FAULT_SIGBUS;
5077                 goto out;
5078         }
5079
5080         ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
5081 again:
5082         lock_page(page);
5083         size = i_size_read(inode);
5084         page_start = page_offset(page);
5085         page_end = page_start + PAGE_CACHE_SIZE - 1;
5086
5087         if ((page->mapping != inode->i_mapping) ||
5088             (page_start >= size)) {
5089                 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5090                 /* page got truncated out from underneath us */
5091                 goto out_unlock;
5092         }
5093         wait_on_page_writeback(page);
5094
5095         lock_extent(io_tree, page_start, page_end, GFP_NOFS);
5096         set_page_extent_mapped(page);
5097
5098         /*
5099          * we can't set the delalloc bits if there are pending ordered
5100          * extents.  Drop our locks and wait for them to finish
5101          */
5102         ordered = btrfs_lookup_ordered_extent(inode, page_start);
5103         if (ordered) {
5104                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5105                 unlock_page(page);
5106                 btrfs_start_ordered_extent(inode, ordered, 1);
5107                 btrfs_put_ordered_extent(ordered);
5108                 goto again;
5109         }
5110
5111         /*
5112          * XXX - page_mkwrite gets called every time the page is dirtied, even
5113          * if it was already dirty, so for space accounting reasons we need to
5114          * clear any delalloc bits for the range we are fixing to save.  There
5115          * is probably a better way to do this, but for now keep consistent with
5116          * prepare_pages in the normal write path.
5117          */
5118         clear_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
5119                           EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
5120                           GFP_NOFS);
5121
5122         ret = btrfs_set_extent_delalloc(inode, page_start, page_end);
5123         if (ret) {
5124                 unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5125                 ret = VM_FAULT_SIGBUS;
5126                 btrfs_free_reserved_data_space(root, inode, PAGE_CACHE_SIZE);
5127                 goto out_unlock;
5128         }
5129         ret = 0;
5130
5131         /* page is wholly or partially inside EOF */
5132         if (page_start + PAGE_CACHE_SIZE > size)
5133                 zero_start = size & ~PAGE_CACHE_MASK;
5134         else
5135                 zero_start = PAGE_CACHE_SIZE;
5136
5137         if (zero_start != PAGE_CACHE_SIZE) {
5138                 kaddr = kmap(page);
5139                 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
5140                 flush_dcache_page(page);
5141                 kunmap(page);
5142         }
5143         ClearPageChecked(page);
5144         set_page_dirty(page);
5145         SetPageUptodate(page);
5146
5147         BTRFS_I(inode)->last_trans = root->fs_info->generation;
5148         BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
5149
5150         unlock_extent(io_tree, page_start, page_end, GFP_NOFS);
5151
5152 out_unlock:
5153         btrfs_unreserve_metadata_for_delalloc(root, inode, 1);
5154         if (!ret)
5155                 return VM_FAULT_LOCKED;
5156         unlock_page(page);
5157 out:
5158         return ret;
5159 }
5160
5161 static void btrfs_truncate(struct inode *inode)
5162 {
5163         struct btrfs_root *root = BTRFS_I(inode)->root;
5164         int ret;
5165         struct btrfs_trans_handle *trans;
5166         unsigned long nr;
5167         u64 mask = root->sectorsize - 1;
5168
5169         if (!S_ISREG(inode->i_mode)) {
5170                 WARN_ON(1);
5171                 return;
5172         }
5173
5174         ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
5175         if (ret)
5176                 return;
5177
5178         btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
5179         btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
5180
5181         trans = btrfs_start_transaction(root, 1);
5182         btrfs_set_trans_block_group(trans, inode);
5183
5184         /*
5185          * setattr is responsible for setting the ordered_data_close flag,
5186          * but that is only tested during the last file release.  That
5187          * could happen well after the next commit, leaving a great big
5188          * window where new writes may get lost if someone chooses to write
5189          * to this file after truncating to zero
5190          *
5191          * The inode doesn't have any dirty data here, and so if we commit
5192          * this is a noop.  If someone immediately starts writing to the inode
5193          * it is very likely we'll catch some of their writes in this
5194          * transaction, and the commit will find this file on the ordered
5195          * data list with good things to send down.
5196          *
5197          * This is a best effort solution, there is still a window where
5198          * using truncate to replace the contents of the file will
5199          * end up with a zero length file after a crash.
5200          */
5201         if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
5202                 btrfs_add_ordered_operation(trans, root, inode);
5203
5204         while (1) {
5205                 ret = btrfs_truncate_inode_items(trans, root, inode,
5206                                                  inode->i_size,
5207                                                  BTRFS_EXTENT_DATA_KEY);
5208                 if (ret != -EAGAIN)
5209                         break;
5210
5211                 ret = btrfs_update_inode(trans, root, inode);
5212                 BUG_ON(ret);
5213
5214                 nr = trans->blocks_used;
5215                 btrfs_end_transaction(trans, root);
5216                 btrfs_btree_balance_dirty(root, nr);
5217
5218                 trans = btrfs_start_transaction(root, 1);
5219                 btrfs_set_trans_block_group(trans, inode);
5220         }
5221
5222         if (ret == 0 && inode->i_nlink > 0) {
5223                 ret = btrfs_orphan_del(trans, inode);
5224                 BUG_ON(ret);
5225         }
5226
5227         ret = btrfs_update_inode(trans, root, inode);
5228         BUG_ON(ret);
5229
5230         nr = trans->blocks_used;
5231         ret = btrfs_end_transaction_throttle(trans, root);
5232         BUG_ON(ret);
5233         btrfs_btree_balance_dirty(root, nr);
5234 }
5235
5236 /*
5237  * create a new subvolume directory/inode (helper for the ioctl).
5238  */
5239 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
5240                              struct btrfs_root *new_root,
5241                              u64 new_dirid, u64 alloc_hint)
5242 {
5243         struct inode *inode;
5244         int err;
5245         u64 index = 0;
5246
5247         inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
5248                                 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
5249         if (IS_ERR(inode))
5250                 return PTR_ERR(inode);
5251         inode->i_op = &btrfs_dir_inode_operations;
5252         inode->i_fop = &btrfs_dir_file_operations;
5253
5254         inode->i_nlink = 1;
5255         btrfs_i_size_write(inode, 0);
5256
5257         err = btrfs_update_inode(trans, new_root, inode);
5258         BUG_ON(err);
5259
5260         iput(inode);
5261         return 0;
5262 }
5263
5264 /* helper function for file defrag and space balancing.  This
5265  * forces readahead on a given range of bytes in an inode
5266  */
5267 unsigned long btrfs_force_ra(struct address_space *mapping,
5268                               struct file_ra_state *ra, struct file *file,
5269                               pgoff_t offset, pgoff_t last_index)
5270 {
5271         pgoff_t req_size = last_index - offset + 1;
5272
5273         page_cache_sync_readahead(mapping, ra, file, offset, req_size);
5274         return offset + req_size;
5275 }
5276
5277 struct inode *btrfs_alloc_inode(struct super_block *sb)
5278 {
5279         struct btrfs_inode *ei;
5280
5281         ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
5282         if (!ei)
5283                 return NULL;
5284         ei->last_trans = 0;
5285         ei->last_sub_trans = 0;
5286         ei->logged_trans = 0;
5287         ei->outstanding_extents = 0;
5288         ei->reserved_extents = 0;
5289         ei->root = NULL;
5290         spin_lock_init(&ei->accounting_lock);
5291         btrfs_ordered_inode_tree_init(&ei->ordered_tree);
5292         INIT_LIST_HEAD(&ei->i_orphan);
5293         INIT_LIST_HEAD(&ei->ordered_operations);
5294         return &ei->vfs_inode;
5295 }
5296
5297 void btrfs_destroy_inode(struct inode *inode)
5298 {
5299         struct btrfs_ordered_extent *ordered;
5300         struct btrfs_root *root = BTRFS_I(inode)->root;
5301
5302         WARN_ON(!list_empty(&inode->i_dentry));
5303         WARN_ON(inode->i_data.nrpages);
5304
5305         /*
5306          * This can happen where we create an inode, but somebody else also
5307          * created the same inode and we need to destroy the one we already
5308          * created.
5309          */
5310         if (!root)
5311                 goto free;
5312
5313         /*
5314          * Make sure we're properly removed from the ordered operation
5315          * lists.
5316          */
5317         smp_mb();
5318         if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
5319                 spin_lock(&root->fs_info->ordered_extent_lock);
5320                 list_del_init(&BTRFS_I(inode)->ordered_operations);
5321                 spin_unlock(&root->fs_info->ordered_extent_lock);
5322         }
5323
5324         spin_lock(&root->list_lock);
5325         if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
5326                 printk(KERN_INFO "BTRFS: inode %lu still on the orphan list\n",
5327                        inode->i_ino);
5328                 list_del_init(&BTRFS_I(inode)->i_orphan);
5329         }
5330         spin_unlock(&root->list_lock);
5331
5332         while (1) {
5333                 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
5334                 if (!ordered)
5335                         break;
5336                 else {
5337                         printk(KERN_ERR "btrfs found ordered "
5338                                "extent %llu %llu on inode cleanup\n",
5339                                (unsigned long long)ordered->file_offset,
5340                                (unsigned long long)ordered->len);
5341                         btrfs_remove_ordered_extent(inode, ordered);
5342                         btrfs_put_ordered_extent(ordered);
5343                         btrfs_put_ordered_extent(ordered);
5344                 }
5345         }
5346         inode_tree_del(inode);
5347         btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
5348 free:
5349         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5350 }
5351
5352 void btrfs_drop_inode(struct inode *inode)
5353 {
5354         struct btrfs_root *root = BTRFS_I(inode)->root;
5355
5356         if (inode->i_nlink > 0 && btrfs_root_refs(&root->root_item) == 0)
5357                 generic_delete_inode(inode);
5358         else
5359                 generic_drop_inode(inode);
5360 }
5361
5362 static void init_once(void *foo)
5363 {
5364         struct btrfs_inode *ei = (struct btrfs_inode *) foo;
5365
5366         inode_init_once(&ei->vfs_inode);
5367 }
5368
5369 void btrfs_destroy_cachep(void)
5370 {
5371         if (btrfs_inode_cachep)
5372                 kmem_cache_destroy(btrfs_inode_cachep);
5373         if (btrfs_trans_handle_cachep)
5374                 kmem_cache_destroy(btrfs_trans_handle_cachep);
5375         if (btrfs_transaction_cachep)
5376                 kmem_cache_destroy(btrfs_transaction_cachep);
5377         if (btrfs_path_cachep)
5378                 kmem_cache_destroy(btrfs_path_cachep);
5379 }
5380
5381 int btrfs_init_cachep(void)
5382 {
5383         btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
5384                         sizeof(struct btrfs_inode), 0,
5385                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
5386         if (!btrfs_inode_cachep)
5387                 goto fail;
5388
5389         btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
5390                         sizeof(struct btrfs_trans_handle), 0,
5391                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
5392         if (!btrfs_trans_handle_cachep)
5393                 goto fail;
5394
5395         btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
5396                         sizeof(struct btrfs_transaction), 0,
5397                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
5398         if (!btrfs_transaction_cachep)
5399                 goto fail;
5400
5401         btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
5402                         sizeof(struct btrfs_path), 0,
5403                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
5404         if (!btrfs_path_cachep)
5405                 goto fail;
5406
5407         return 0;
5408 fail:
5409         btrfs_destroy_cachep();
5410         return -ENOMEM;
5411 }
5412
5413 static int btrfs_getattr(struct vfsmount *mnt,
5414                          struct dentry *dentry, struct kstat *stat)
5415 {
5416         struct inode *inode = dentry->d_inode;
5417         generic_fillattr(inode, stat);
5418         stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
5419         stat->blksize = PAGE_CACHE_SIZE;
5420         stat->blocks = (inode_get_bytes(inode) +
5421                         BTRFS_I(inode)->delalloc_bytes) >> 9;
5422         return 0;
5423 }
5424
5425 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
5426                            struct inode *new_dir, struct dentry *new_dentry)
5427 {
5428         struct btrfs_trans_handle *trans;
5429         struct btrfs_root *root = BTRFS_I(old_dir)->root;
5430         struct btrfs_root *dest = BTRFS_I(new_dir)->root;
5431         struct inode *new_inode = new_dentry->d_inode;
5432         struct inode *old_inode = old_dentry->d_inode;
5433         struct timespec ctime = CURRENT_TIME;
5434         u64 index = 0;
5435         u64 root_objectid;
5436         int ret;
5437
5438         if (new_dir->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5439                 return -EPERM;
5440
5441         /* we only allow rename subvolume link between subvolumes */
5442         if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
5443                 return -EXDEV;
5444
5445         if (old_inode->i_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
5446             (new_inode && new_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID))
5447                 return -ENOTEMPTY;
5448
5449         if (S_ISDIR(old_inode->i_mode) && new_inode &&
5450             new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
5451                 return -ENOTEMPTY;
5452
5453         /*
5454          * We want to reserve the absolute worst case amount of items.  So if
5455          * both inodes are subvols and we need to unlink them then that would
5456          * require 4 item modifications, but if they are both normal inodes it
5457          * would require 5 item modifications, so we'll assume their normal
5458          * inodes.  So 5 * 2 is 10, plus 1 for the new link, so 11 total items
5459          * should cover the worst case number of items we'll modify.
5460          */
5461         ret = btrfs_reserve_metadata_space(root, 11);
5462         if (ret)
5463                 return ret;
5464
5465         /*
5466          * we're using rename to replace one file with another.
5467          * and the replacement file is large.  Start IO on it now so
5468          * we don't add too much work to the end of the transaction
5469          */
5470         if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
5471             old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
5472                 filemap_flush(old_inode->i_mapping);
5473
5474         /* close the racy window with snapshot create/destroy ioctl */
5475         if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5476                 down_read(&root->fs_info->subvol_sem);
5477
5478         trans = btrfs_start_transaction(root, 1);
5479         btrfs_set_trans_block_group(trans, new_dir);
5480
5481         if (dest != root)
5482                 btrfs_record_root_in_trans(trans, dest);
5483
5484         ret = btrfs_set_inode_index(new_dir, &index);
5485         if (ret)
5486                 goto out_fail;
5487
5488         if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5489                 /* force full log commit if subvolume involved. */
5490                 root->fs_info->last_trans_log_full_commit = trans->transid;
5491         } else {
5492                 ret = btrfs_insert_inode_ref(trans, dest,
5493                                              new_dentry->d_name.name,
5494                                              new_dentry->d_name.len,
5495                                              old_inode->i_ino,
5496                                              new_dir->i_ino, index);
5497                 if (ret)
5498                         goto out_fail;
5499                 /*
5500                  * this is an ugly little race, but the rename is required
5501                  * to make sure that if we crash, the inode is either at the
5502                  * old name or the new one.  pinning the log transaction lets
5503                  * us make sure we don't allow a log commit to come in after
5504                  * we unlink the name but before we add the new name back in.
5505                  */
5506                 btrfs_pin_log_trans(root);
5507         }
5508         /*
5509          * make sure the inode gets flushed if it is replacing
5510          * something.
5511          */
5512         if (new_inode && new_inode->i_size &&
5513             old_inode && S_ISREG(old_inode->i_mode)) {
5514                 btrfs_add_ordered_operation(trans, root, old_inode);
5515         }
5516
5517         old_dir->i_ctime = old_dir->i_mtime = ctime;
5518         new_dir->i_ctime = new_dir->i_mtime = ctime;
5519         old_inode->i_ctime = ctime;
5520
5521         if (old_dentry->d_parent != new_dentry->d_parent)
5522                 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
5523
5524         if (unlikely(old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)) {
5525                 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
5526                 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
5527                                         old_dentry->d_name.name,
5528                                         old_dentry->d_name.len);
5529         } else {
5530                 btrfs_inc_nlink(old_dentry->d_inode);
5531                 ret = btrfs_unlink_inode(trans, root, old_dir,
5532                                          old_dentry->d_inode,
5533                                          old_dentry->d_name.name,
5534                                          old_dentry->d_name.len);
5535         }
5536         BUG_ON(ret);
5537
5538         if (new_inode) {
5539                 new_inode->i_ctime = CURRENT_TIME;
5540                 if (unlikely(new_inode->i_ino ==
5541                              BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
5542                         root_objectid = BTRFS_I(new_inode)->location.objectid;
5543                         ret = btrfs_unlink_subvol(trans, dest, new_dir,
5544                                                 root_objectid,
5545                                                 new_dentry->d_name.name,
5546                                                 new_dentry->d_name.len);
5547                         BUG_ON(new_inode->i_nlink == 0);
5548                 } else {
5549                         ret = btrfs_unlink_inode(trans, dest, new_dir,
5550                                                  new_dentry->d_inode,
5551                                                  new_dentry->d_name.name,
5552                                                  new_dentry->d_name.len);
5553                 }
5554                 BUG_ON(ret);
5555                 if (new_inode->i_nlink == 0) {
5556                         ret = btrfs_orphan_add(trans, new_dentry->d_inode);
5557                         BUG_ON(ret);
5558                 }
5559         }
5560
5561         ret = btrfs_add_link(trans, new_dir, old_inode,
5562                              new_dentry->d_name.name,
5563                              new_dentry->d_name.len, 0, index);
5564         BUG_ON(ret);
5565
5566         if (old_inode->i_ino != BTRFS_FIRST_FREE_OBJECTID) {
5567                 btrfs_log_new_name(trans, old_inode, old_dir,
5568                                    new_dentry->d_parent);
5569                 btrfs_end_log_trans(root);
5570         }
5571 out_fail:
5572         btrfs_end_transaction_throttle(trans, root);
5573
5574         if (old_inode->i_ino == BTRFS_FIRST_FREE_OBJECTID)
5575                 up_read(&root->fs_info->subvol_sem);
5576
5577         btrfs_unreserve_metadata_space(root, 11);
5578         return ret;
5579 }
5580
5581 /*
5582  * some fairly slow code that needs optimization. This walks the list
5583  * of all the inodes with pending delalloc and forces them to disk.
5584  */
5585 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
5586 {
5587         struct list_head *head = &root->fs_info->delalloc_inodes;
5588         struct btrfs_inode *binode;
5589         struct inode *inode;
5590
5591         if (root->fs_info->sb->s_flags & MS_RDONLY)
5592                 return -EROFS;
5593
5594         spin_lock(&root->fs_info->delalloc_lock);
5595         while (!list_empty(head)) {
5596                 binode = list_entry(head->next, struct btrfs_inode,
5597                                     delalloc_inodes);
5598                 inode = igrab(&binode->vfs_inode);
5599                 if (!inode)
5600                         list_del_init(&binode->delalloc_inodes);
5601                 spin_unlock(&root->fs_info->delalloc_lock);
5602                 if (inode) {
5603                         filemap_flush(inode->i_mapping);
5604                         if (delay_iput)
5605                                 btrfs_add_delayed_iput(inode);
5606                         else
5607                                 iput(inode);
5608                 }
5609                 cond_resched();
5610                 spin_lock(&root->fs_info->delalloc_lock);
5611         }
5612         spin_unlock(&root->fs_info->delalloc_lock);
5613
5614         /* the filemap_flush will queue IO into the worker threads, but
5615          * we have to make sure the IO is actually started and that
5616          * ordered extents get created before we return
5617          */
5618         atomic_inc(&root->fs_info->async_submit_draining);
5619         while (atomic_read(&root->fs_info->nr_async_submits) ||
5620               atomic_read(&root->fs_info->async_delalloc_pages)) {
5621                 wait_event(root->fs_info->async_submit_wait,
5622                    (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
5623                     atomic_read(&root->fs_info->async_delalloc_pages) == 0));
5624         }
5625         atomic_dec(&root->fs_info->async_submit_draining);
5626         return 0;
5627 }
5628
5629 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
5630                          const char *symname)
5631 {
5632         struct btrfs_trans_handle *trans;
5633         struct btrfs_root *root = BTRFS_I(dir)->root;
5634         struct btrfs_path *path;
5635         struct btrfs_key key;
5636         struct inode *inode = NULL;
5637         int err;
5638         int drop_inode = 0;
5639         u64 objectid;
5640         u64 index = 0 ;
5641         int name_len;
5642         int datasize;
5643         unsigned long ptr;
5644         struct btrfs_file_extent_item *ei;
5645         struct extent_buffer *leaf;
5646         unsigned long nr = 0;
5647
5648         name_len = strlen(symname) + 1;
5649         if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
5650                 return -ENAMETOOLONG;
5651
5652         /*
5653          * 2 items for inode item and ref
5654          * 2 items for dir items
5655          * 1 item for xattr if selinux is on
5656          */
5657         err = btrfs_reserve_metadata_space(root, 5);
5658         if (err)
5659                 return err;
5660
5661         trans = btrfs_start_transaction(root, 1);
5662         if (!trans)
5663                 goto out_fail;
5664         btrfs_set_trans_block_group(trans, dir);
5665
5666         err = btrfs_find_free_objectid(trans, root, dir->i_ino, &objectid);
5667         if (err) {
5668                 err = -ENOSPC;
5669                 goto out_unlock;
5670         }
5671
5672         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
5673                                 dentry->d_name.len,
5674                                 dentry->d_parent->d_inode->i_ino, objectid,
5675                                 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
5676                                 &index);
5677         err = PTR_ERR(inode);
5678         if (IS_ERR(inode))
5679                 goto out_unlock;
5680
5681         err = btrfs_init_inode_security(trans, inode, dir);
5682         if (err) {
5683                 drop_inode = 1;
5684                 goto out_unlock;
5685         }
5686
5687         btrfs_set_trans_block_group(trans, inode);
5688         err = btrfs_add_nondir(trans, dentry, inode, 0, index);
5689         if (err)
5690                 drop_inode = 1;
5691         else {
5692                 inode->i_mapping->a_ops = &btrfs_aops;
5693                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5694                 inode->i_fop = &btrfs_file_operations;
5695                 inode->i_op = &btrfs_file_inode_operations;
5696                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
5697         }
5698         btrfs_update_inode_block_group(trans, inode);
5699         btrfs_update_inode_block_group(trans, dir);
5700         if (drop_inode)
5701                 goto out_unlock;
5702
5703         path = btrfs_alloc_path();
5704         BUG_ON(!path);
5705         key.objectid = inode->i_ino;
5706         key.offset = 0;
5707         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
5708         datasize = btrfs_file_extent_calc_inline_size(name_len);
5709         err = btrfs_insert_empty_item(trans, root, path, &key,
5710                                       datasize);
5711         if (err) {
5712                 drop_inode = 1;
5713                 goto out_unlock;
5714         }
5715         leaf = path->nodes[0];
5716         ei = btrfs_item_ptr(leaf, path->slots[0],
5717                             struct btrfs_file_extent_item);
5718         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
5719         btrfs_set_file_extent_type(leaf, ei,
5720                                    BTRFS_FILE_EXTENT_INLINE);
5721         btrfs_set_file_extent_encryption(leaf, ei, 0);
5722         btrfs_set_file_extent_compression(leaf, ei, 0);
5723         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
5724         btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
5725
5726         ptr = btrfs_file_extent_inline_start(ei);
5727         write_extent_buffer(leaf, symname, ptr, name_len);
5728         btrfs_mark_buffer_dirty(leaf);
5729         btrfs_free_path(path);
5730
5731         inode->i_op = &btrfs_symlink_inode_operations;
5732         inode->i_mapping->a_ops = &btrfs_symlink_aops;
5733         inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
5734         inode_set_bytes(inode, name_len);
5735         btrfs_i_size_write(inode, name_len - 1);
5736         err = btrfs_update_inode(trans, root, inode);
5737         if (err)
5738                 drop_inode = 1;
5739
5740 out_unlock:
5741         nr = trans->blocks_used;
5742         btrfs_end_transaction_throttle(trans, root);
5743 out_fail:
5744         btrfs_unreserve_metadata_space(root, 5);
5745         if (drop_inode) {
5746                 inode_dec_link_count(inode);
5747                 iput(inode);
5748         }
5749         btrfs_btree_balance_dirty(root, nr);
5750         return err;
5751 }
5752
5753 static int prealloc_file_range(struct inode *inode, u64 start, u64 end,
5754                         u64 alloc_hint, int mode, loff_t actual_len)
5755 {
5756         struct btrfs_trans_handle *trans;
5757         struct btrfs_root *root = BTRFS_I(inode)->root;
5758         struct btrfs_key ins;
5759         u64 alloc_size;
5760         u64 cur_offset = start;
5761         u64 num_bytes = end - start;
5762         int ret = 0;
5763         u64 i_size;
5764
5765         while (num_bytes > 0) {
5766                 alloc_size = min(num_bytes, root->fs_info->max_extent);
5767
5768                 trans = btrfs_start_transaction(root, 1);
5769
5770                 ret = btrfs_reserve_extent(trans, root, alloc_size,
5771                                            root->sectorsize, 0, alloc_hint,
5772                                            (u64)-1, &ins, 1);
5773                 if (ret) {
5774                         WARN_ON(1);
5775                         goto stop_trans;
5776                 }
5777
5778                 ret = btrfs_reserve_metadata_space(root, 3);
5779                 if (ret) {
5780                         btrfs_free_reserved_extent(root, ins.objectid,
5781                                                    ins.offset);
5782                         goto stop_trans;
5783                 }
5784
5785                 ret = insert_reserved_file_extent(trans, inode,
5786                                                   cur_offset, ins.objectid,
5787                                                   ins.offset, ins.offset,
5788                                                   ins.offset, 0, 0, 0,
5789                                                   BTRFS_FILE_EXTENT_PREALLOC);
5790                 BUG_ON(ret);
5791                 btrfs_drop_extent_cache(inode, cur_offset,
5792                                         cur_offset + ins.offset -1, 0);
5793
5794                 num_bytes -= ins.offset;
5795                 cur_offset += ins.offset;
5796                 alloc_hint = ins.objectid + ins.offset;
5797
5798                 inode->i_ctime = CURRENT_TIME;
5799                 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
5800                 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
5801                         (actual_len > inode->i_size) &&
5802                         (cur_offset > inode->i_size)) {
5803
5804                         if (cur_offset > actual_len)
5805                                 i_size  = actual_len;
5806                         else
5807                                 i_size = cur_offset;
5808                         i_size_write(inode, i_size);
5809                         btrfs_ordered_update_i_size(inode, i_size, NULL);
5810                 }
5811
5812                 ret = btrfs_update_inode(trans, root, inode);
5813                 BUG_ON(ret);
5814
5815                 btrfs_end_transaction(trans, root);
5816                 btrfs_unreserve_metadata_space(root, 3);
5817         }
5818         return ret;
5819
5820 stop_trans:
5821         btrfs_end_transaction(trans, root);
5822         return ret;
5823
5824 }
5825
5826 static long btrfs_fallocate(struct inode *inode, int mode,
5827                             loff_t offset, loff_t len)
5828 {
5829         u64 cur_offset;
5830         u64 last_byte;
5831         u64 alloc_start;
5832         u64 alloc_end;
5833         u64 alloc_hint = 0;
5834         u64 locked_end;
5835         u64 mask = BTRFS_I(inode)->root->sectorsize - 1;
5836         struct extent_map *em;
5837         int ret;
5838
5839         alloc_start = offset & ~mask;
5840         alloc_end =  (offset + len + mask) & ~mask;
5841
5842         /*
5843          * wait for ordered IO before we have any locks.  We'll loop again
5844          * below with the locks held.
5845          */
5846         btrfs_wait_ordered_range(inode, alloc_start, alloc_end - alloc_start);
5847
5848         mutex_lock(&inode->i_mutex);
5849         if (alloc_start > inode->i_size) {
5850                 ret = btrfs_cont_expand(inode, alloc_start);
5851                 if (ret)
5852                         goto out;
5853         }
5854
5855         ret = btrfs_check_data_free_space(BTRFS_I(inode)->root, inode,
5856                                           alloc_end - alloc_start);
5857         if (ret)
5858                 goto out;
5859
5860         locked_end = alloc_end - 1;
5861         while (1) {
5862                 struct btrfs_ordered_extent *ordered;
5863
5864                 /* the extent lock is ordered inside the running
5865                  * transaction
5866                  */
5867                 lock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5868                             GFP_NOFS);
5869                 ordered = btrfs_lookup_first_ordered_extent(inode,
5870                                                             alloc_end - 1);
5871                 if (ordered &&
5872                     ordered->file_offset + ordered->len > alloc_start &&
5873                     ordered->file_offset < alloc_end) {
5874                         btrfs_put_ordered_extent(ordered);
5875                         unlock_extent(&BTRFS_I(inode)->io_tree,
5876                                       alloc_start, locked_end, GFP_NOFS);
5877                         /*
5878                          * we can't wait on the range with the transaction
5879                          * running or with the extent lock held
5880                          */
5881                         btrfs_wait_ordered_range(inode, alloc_start,
5882                                                  alloc_end - alloc_start);
5883                 } else {
5884                         if (ordered)
5885                                 btrfs_put_ordered_extent(ordered);
5886                         break;
5887                 }
5888         }
5889
5890         cur_offset = alloc_start;
5891         while (1) {
5892                 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
5893                                       alloc_end - cur_offset, 0);
5894                 BUG_ON(IS_ERR(em) || !em);
5895                 last_byte = min(extent_map_end(em), alloc_end);
5896                 last_byte = (last_byte + mask) & ~mask;
5897                 if (em->block_start == EXTENT_MAP_HOLE ||
5898                     (cur_offset >= inode->i_size &&
5899                      !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5900                         ret = prealloc_file_range(inode,
5901                                                   cur_offset, last_byte,
5902                                                 alloc_hint, mode, offset+len);
5903                         if (ret < 0) {
5904                                 free_extent_map(em);
5905                                 break;
5906                         }
5907                 }
5908                 if (em->block_start <= EXTENT_MAP_LAST_BYTE)
5909                         alloc_hint = em->block_start;
5910                 free_extent_map(em);
5911
5912                 cur_offset = last_byte;
5913                 if (cur_offset >= alloc_end) {
5914                         ret = 0;
5915                         break;
5916                 }
5917         }
5918         unlock_extent(&BTRFS_I(inode)->io_tree, alloc_start, locked_end,
5919                       GFP_NOFS);
5920
5921         btrfs_free_reserved_data_space(BTRFS_I(inode)->root, inode,
5922                                        alloc_end - alloc_start);
5923 out:
5924         mutex_unlock(&inode->i_mutex);
5925         return ret;
5926 }
5927
5928 static int btrfs_set_page_dirty(struct page *page)
5929 {
5930         return __set_page_dirty_nobuffers(page);
5931 }
5932
5933 static int btrfs_permission(struct inode *inode, int mask)
5934 {
5935         if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
5936                 return -EACCES;
5937         return generic_permission(inode, mask, btrfs_check_acl);
5938 }
5939
5940 static const struct inode_operations btrfs_dir_inode_operations = {
5941         .getattr        = btrfs_getattr,
5942         .lookup         = btrfs_lookup,
5943         .create         = btrfs_create,
5944         .unlink         = btrfs_unlink,
5945         .link           = btrfs_link,
5946         .mkdir          = btrfs_mkdir,
5947         .rmdir          = btrfs_rmdir,
5948         .rename         = btrfs_rename,
5949         .symlink        = btrfs_symlink,
5950         .setattr        = btrfs_setattr,
5951         .mknod          = btrfs_mknod,
5952         .setxattr       = btrfs_setxattr,
5953         .getxattr       = btrfs_getxattr,
5954         .listxattr      = btrfs_listxattr,
5955         .removexattr    = btrfs_removexattr,
5956         .permission     = btrfs_permission,
5957 };
5958 static const struct inode_operations btrfs_dir_ro_inode_operations = {
5959         .lookup         = btrfs_lookup,
5960         .permission     = btrfs_permission,
5961 };
5962
5963 static const struct file_operations btrfs_dir_file_operations = {
5964         .llseek         = generic_file_llseek,
5965         .read           = generic_read_dir,
5966         .readdir        = btrfs_real_readdir,
5967         .unlocked_ioctl = btrfs_ioctl,
5968 #ifdef CONFIG_COMPAT
5969         .compat_ioctl   = btrfs_ioctl,
5970 #endif
5971         .release        = btrfs_release_file,
5972         .fsync          = btrfs_sync_file,
5973 };
5974
5975 static struct extent_io_ops btrfs_extent_io_ops = {
5976         .fill_delalloc = run_delalloc_range,
5977         .submit_bio_hook = btrfs_submit_bio_hook,
5978         .merge_bio_hook = btrfs_merge_bio_hook,
5979         .readpage_end_io_hook = btrfs_readpage_end_io_hook,
5980         .writepage_end_io_hook = btrfs_writepage_end_io_hook,
5981         .writepage_start_hook = btrfs_writepage_start_hook,
5982         .readpage_io_failed_hook = btrfs_io_failed_hook,
5983         .set_bit_hook = btrfs_set_bit_hook,
5984         .clear_bit_hook = btrfs_clear_bit_hook,
5985         .merge_extent_hook = btrfs_merge_extent_hook,
5986         .split_extent_hook = btrfs_split_extent_hook,
5987 };
5988
5989 /*
5990  * btrfs doesn't support the bmap operation because swapfiles
5991  * use bmap to make a mapping of extents in the file.  They assume
5992  * these extents won't change over the life of the file and they
5993  * use the bmap result to do IO directly to the drive.
5994  *
5995  * the btrfs bmap call would return logical addresses that aren't
5996  * suitable for IO and they also will change frequently as COW
5997  * operations happen.  So, swapfile + btrfs == corruption.
5998  *
5999  * For now we're avoiding this by dropping bmap.
6000  */
6001 static const struct address_space_operations btrfs_aops = {
6002         .readpage       = btrfs_readpage,
6003         .writepage      = btrfs_writepage,
6004         .writepages     = btrfs_writepages,
6005         .readpages      = btrfs_readpages,
6006         .sync_page      = block_sync_page,
6007         .direct_IO      = btrfs_direct_IO,
6008         .invalidatepage = btrfs_invalidatepage,
6009         .releasepage    = btrfs_releasepage,
6010         .set_page_dirty = btrfs_set_page_dirty,
6011         .error_remove_page = generic_error_remove_page,
6012 };
6013
6014 static const struct address_space_operations btrfs_symlink_aops = {
6015         .readpage       = btrfs_readpage,
6016         .writepage      = btrfs_writepage,
6017         .invalidatepage = btrfs_invalidatepage,
6018         .releasepage    = btrfs_releasepage,
6019 };
6020
6021 static const struct inode_operations btrfs_file_inode_operations = {
6022         .truncate       = btrfs_truncate,
6023         .getattr        = btrfs_getattr,
6024         .setattr        = btrfs_setattr,
6025         .setxattr       = btrfs_setxattr,
6026         .getxattr       = btrfs_getxattr,
6027         .listxattr      = btrfs_listxattr,
6028         .removexattr    = btrfs_removexattr,
6029         .permission     = btrfs_permission,
6030         .fallocate      = btrfs_fallocate,
6031         .fiemap         = btrfs_fiemap,
6032 };
6033 static const struct inode_operations btrfs_special_inode_operations = {
6034         .getattr        = btrfs_getattr,
6035         .setattr        = btrfs_setattr,
6036         .permission     = btrfs_permission,
6037         .setxattr       = btrfs_setxattr,
6038         .getxattr       = btrfs_getxattr,
6039         .listxattr      = btrfs_listxattr,
6040         .removexattr    = btrfs_removexattr,
6041 };
6042 static const struct inode_operations btrfs_symlink_inode_operations = {
6043         .readlink       = generic_readlink,
6044         .follow_link    = page_follow_link_light,
6045         .put_link       = page_put_link,
6046         .permission     = btrfs_permission,
6047         .setxattr       = btrfs_setxattr,
6048         .getxattr       = btrfs_getxattr,
6049         .listxattr      = btrfs_listxattr,
6050         .removexattr    = btrfs_removexattr,
6051 };
6052
6053 const struct dentry_operations btrfs_dentry_operations = {
6054         .d_delete       = btrfs_dentry_delete,
6055 };