]> git.karo-electronics.de Git - karo-tx-linux.git/blob - fs/btrfs/inode.c
Merge branch 'delayed_inode' into inode_numbers
[karo-tx-linux.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 <linux/slab.h>
40 #include "compat.h"
41 #include "ctree.h"
42 #include "disk-io.h"
43 #include "transaction.h"
44 #include "btrfs_inode.h"
45 #include "ioctl.h"
46 #include "print-tree.h"
47 #include "volumes.h"
48 #include "ordered-data.h"
49 #include "xattr.h"
50 #include "tree-log.h"
51 #include "compression.h"
52 #include "locking.h"
53 #include "free-space-cache.h"
54 #include "inode-map.h"
55
56 struct btrfs_iget_args {
57         u64 ino;
58         struct btrfs_root *root;
59 };
60
61 static const struct inode_operations btrfs_dir_inode_operations;
62 static const struct inode_operations btrfs_symlink_inode_operations;
63 static const struct inode_operations btrfs_dir_ro_inode_operations;
64 static const struct inode_operations btrfs_special_inode_operations;
65 static const struct inode_operations btrfs_file_inode_operations;
66 static const struct address_space_operations btrfs_aops;
67 static const struct address_space_operations btrfs_symlink_aops;
68 static const struct file_operations btrfs_dir_file_operations;
69 static struct extent_io_ops btrfs_extent_io_ops;
70
71 static struct kmem_cache *btrfs_inode_cachep;
72 struct kmem_cache *btrfs_trans_handle_cachep;
73 struct kmem_cache *btrfs_transaction_cachep;
74 struct kmem_cache *btrfs_path_cachep;
75 struct kmem_cache *btrfs_free_space_cachep;
76
77 #define S_SHIFT 12
78 static unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
79         [S_IFREG >> S_SHIFT]    = BTRFS_FT_REG_FILE,
80         [S_IFDIR >> S_SHIFT]    = BTRFS_FT_DIR,
81         [S_IFCHR >> S_SHIFT]    = BTRFS_FT_CHRDEV,
82         [S_IFBLK >> S_SHIFT]    = BTRFS_FT_BLKDEV,
83         [S_IFIFO >> S_SHIFT]    = BTRFS_FT_FIFO,
84         [S_IFSOCK >> S_SHIFT]   = BTRFS_FT_SOCK,
85         [S_IFLNK >> S_SHIFT]    = BTRFS_FT_SYMLINK,
86 };
87
88 static int btrfs_setsize(struct inode *inode, loff_t newsize);
89 static int btrfs_truncate(struct inode *inode);
90 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end);
91 static noinline int cow_file_range(struct inode *inode,
92                                    struct page *locked_page,
93                                    u64 start, u64 end, int *page_started,
94                                    unsigned long *nr_written, int unlock);
95
96 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
97                                      struct inode *inode,  struct inode *dir,
98                                      const struct qstr *qstr)
99 {
100         int err;
101
102         err = btrfs_init_acl(trans, inode, dir);
103         if (!err)
104                 err = btrfs_xattr_security_init(trans, inode, dir, qstr);
105         return err;
106 }
107
108 /*
109  * this does all the hard work for inserting an inline extent into
110  * the btree.  The caller should have done a btrfs_drop_extents so that
111  * no overlapping inline items exist in the btree
112  */
113 static noinline int insert_inline_extent(struct btrfs_trans_handle *trans,
114                                 struct btrfs_root *root, struct inode *inode,
115                                 u64 start, size_t size, size_t compressed_size,
116                                 int compress_type,
117                                 struct page **compressed_pages)
118 {
119         struct btrfs_key key;
120         struct btrfs_path *path;
121         struct extent_buffer *leaf;
122         struct page *page = NULL;
123         char *kaddr;
124         unsigned long ptr;
125         struct btrfs_file_extent_item *ei;
126         int err = 0;
127         int ret;
128         size_t cur_size = size;
129         size_t datasize;
130         unsigned long offset;
131
132         if (compressed_size && compressed_pages)
133                 cur_size = compressed_size;
134
135         path = btrfs_alloc_path();
136         if (!path)
137                 return -ENOMEM;
138
139         path->leave_spinning = 1;
140         btrfs_set_trans_block_group(trans, inode);
141
142         key.objectid = btrfs_ino(inode);
143         key.offset = start;
144         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
145         datasize = btrfs_file_extent_calc_inline_size(cur_size);
146
147         inode_add_bytes(inode, size);
148         ret = btrfs_insert_empty_item(trans, root, path, &key,
149                                       datasize);
150         BUG_ON(ret);
151         if (ret) {
152                 err = ret;
153                 goto fail;
154         }
155         leaf = path->nodes[0];
156         ei = btrfs_item_ptr(leaf, path->slots[0],
157                             struct btrfs_file_extent_item);
158         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
159         btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
160         btrfs_set_file_extent_encryption(leaf, ei, 0);
161         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
162         btrfs_set_file_extent_ram_bytes(leaf, ei, size);
163         ptr = btrfs_file_extent_inline_start(ei);
164
165         if (compress_type != BTRFS_COMPRESS_NONE) {
166                 struct page *cpage;
167                 int i = 0;
168                 while (compressed_size > 0) {
169                         cpage = compressed_pages[i];
170                         cur_size = min_t(unsigned long, compressed_size,
171                                        PAGE_CACHE_SIZE);
172
173                         kaddr = kmap_atomic(cpage, KM_USER0);
174                         write_extent_buffer(leaf, kaddr, ptr, cur_size);
175                         kunmap_atomic(kaddr, KM_USER0);
176
177                         i++;
178                         ptr += cur_size;
179                         compressed_size -= cur_size;
180                 }
181                 btrfs_set_file_extent_compression(leaf, ei,
182                                                   compress_type);
183         } else {
184                 page = find_get_page(inode->i_mapping,
185                                      start >> PAGE_CACHE_SHIFT);
186                 btrfs_set_file_extent_compression(leaf, ei, 0);
187                 kaddr = kmap_atomic(page, KM_USER0);
188                 offset = start & (PAGE_CACHE_SIZE - 1);
189                 write_extent_buffer(leaf, kaddr + offset, ptr, size);
190                 kunmap_atomic(kaddr, KM_USER0);
191                 page_cache_release(page);
192         }
193         btrfs_mark_buffer_dirty(leaf);
194         btrfs_free_path(path);
195
196         /*
197          * we're an inline extent, so nobody can
198          * extend the file past i_size without locking
199          * a page we already have locked.
200          *
201          * We must do any isize and inode updates
202          * before we unlock the pages.  Otherwise we
203          * could end up racing with unlink.
204          */
205         BTRFS_I(inode)->disk_i_size = inode->i_size;
206         btrfs_update_inode(trans, root, inode);
207
208         return 0;
209 fail:
210         btrfs_free_path(path);
211         return err;
212 }
213
214
215 /*
216  * conditionally insert an inline extent into the file.  This
217  * does the checks required to make sure the data is small enough
218  * to fit as an inline extent.
219  */
220 static noinline int cow_file_range_inline(struct btrfs_trans_handle *trans,
221                                  struct btrfs_root *root,
222                                  struct inode *inode, u64 start, u64 end,
223                                  size_t compressed_size, int compress_type,
224                                  struct page **compressed_pages)
225 {
226         u64 isize = i_size_read(inode);
227         u64 actual_end = min(end + 1, isize);
228         u64 inline_len = actual_end - start;
229         u64 aligned_end = (end + root->sectorsize - 1) &
230                         ~((u64)root->sectorsize - 1);
231         u64 hint_byte;
232         u64 data_len = inline_len;
233         int ret;
234
235         if (compressed_size)
236                 data_len = compressed_size;
237
238         if (start > 0 ||
239             actual_end >= PAGE_CACHE_SIZE ||
240             data_len >= BTRFS_MAX_INLINE_DATA_SIZE(root) ||
241             (!compressed_size &&
242             (actual_end & (root->sectorsize - 1)) == 0) ||
243             end + 1 < isize ||
244             data_len > root->fs_info->max_inline) {
245                 return 1;
246         }
247
248         ret = btrfs_drop_extents(trans, inode, start, aligned_end,
249                                  &hint_byte, 1);
250         BUG_ON(ret);
251
252         if (isize > actual_end)
253                 inline_len = min_t(u64, isize, actual_end);
254         ret = insert_inline_extent(trans, root, inode, start,
255                                    inline_len, compressed_size,
256                                    compress_type, compressed_pages);
257         BUG_ON(ret);
258         btrfs_delalloc_release_metadata(inode, end + 1 - start);
259         btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
260         return 0;
261 }
262
263 struct async_extent {
264         u64 start;
265         u64 ram_size;
266         u64 compressed_size;
267         struct page **pages;
268         unsigned long nr_pages;
269         int compress_type;
270         struct list_head list;
271 };
272
273 struct async_cow {
274         struct inode *inode;
275         struct btrfs_root *root;
276         struct page *locked_page;
277         u64 start;
278         u64 end;
279         struct list_head extents;
280         struct btrfs_work work;
281 };
282
283 static noinline int add_async_extent(struct async_cow *cow,
284                                      u64 start, u64 ram_size,
285                                      u64 compressed_size,
286                                      struct page **pages,
287                                      unsigned long nr_pages,
288                                      int compress_type)
289 {
290         struct async_extent *async_extent;
291
292         async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
293         BUG_ON(!async_extent);
294         async_extent->start = start;
295         async_extent->ram_size = ram_size;
296         async_extent->compressed_size = compressed_size;
297         async_extent->pages = pages;
298         async_extent->nr_pages = nr_pages;
299         async_extent->compress_type = compress_type;
300         list_add_tail(&async_extent->list, &cow->extents);
301         return 0;
302 }
303
304 /*
305  * we create compressed extents in two phases.  The first
306  * phase compresses a range of pages that have already been
307  * locked (both pages and state bits are locked).
308  *
309  * This is done inside an ordered work queue, and the compression
310  * is spread across many cpus.  The actual IO submission is step
311  * two, and the ordered work queue takes care of making sure that
312  * happens in the same order things were put onto the queue by
313  * writepages and friends.
314  *
315  * If this code finds it can't get good compression, it puts an
316  * entry onto the work queue to write the uncompressed bytes.  This
317  * makes sure that both compressed inodes and uncompressed inodes
318  * are written in the same order that pdflush sent them down.
319  */
320 static noinline int compress_file_range(struct inode *inode,
321                                         struct page *locked_page,
322                                         u64 start, u64 end,
323                                         struct async_cow *async_cow,
324                                         int *num_added)
325 {
326         struct btrfs_root *root = BTRFS_I(inode)->root;
327         struct btrfs_trans_handle *trans;
328         u64 num_bytes;
329         u64 blocksize = root->sectorsize;
330         u64 actual_end;
331         u64 isize = i_size_read(inode);
332         int ret = 0;
333         struct page **pages = NULL;
334         unsigned long nr_pages;
335         unsigned long nr_pages_ret = 0;
336         unsigned long total_compressed = 0;
337         unsigned long total_in = 0;
338         unsigned long max_compressed = 128 * 1024;
339         unsigned long max_uncompressed = 128 * 1024;
340         int i;
341         int will_compress;
342         int compress_type = root->fs_info->compress_type;
343
344         actual_end = min_t(u64, isize, end + 1);
345 again:
346         will_compress = 0;
347         nr_pages = (end >> PAGE_CACHE_SHIFT) - (start >> PAGE_CACHE_SHIFT) + 1;
348         nr_pages = min(nr_pages, (128 * 1024UL) / PAGE_CACHE_SIZE);
349
350         /*
351          * we don't want to send crud past the end of i_size through
352          * compression, that's just a waste of CPU time.  So, if the
353          * end of the file is before the start of our current
354          * requested range of bytes, we bail out to the uncompressed
355          * cleanup code that can deal with all of this.
356          *
357          * It isn't really the fastest way to fix things, but this is a
358          * very uncommon corner.
359          */
360         if (actual_end <= start)
361                 goto cleanup_and_bail_uncompressed;
362
363         total_compressed = actual_end - start;
364
365         /* we want to make sure that amount of ram required to uncompress
366          * an extent is reasonable, so we limit the total size in ram
367          * of a compressed extent to 128k.  This is a crucial number
368          * because it also controls how easily we can spread reads across
369          * cpus for decompression.
370          *
371          * We also want to make sure the amount of IO required to do
372          * a random read is reasonably small, so we limit the size of
373          * a compressed extent to 128k.
374          */
375         total_compressed = min(total_compressed, max_uncompressed);
376         num_bytes = (end - start + blocksize) & ~(blocksize - 1);
377         num_bytes = max(blocksize,  num_bytes);
378         total_in = 0;
379         ret = 0;
380
381         /*
382          * we do compression for mount -o compress and when the
383          * inode has not been flagged as nocompress.  This flag can
384          * change at any time if we discover bad compression ratios.
385          */
386         if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS) &&
387             (btrfs_test_opt(root, COMPRESS) ||
388              (BTRFS_I(inode)->force_compress) ||
389              (BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))) {
390                 WARN_ON(pages);
391                 pages = kzalloc(sizeof(struct page *) * nr_pages, GFP_NOFS);
392                 BUG_ON(!pages);
393
394                 if (BTRFS_I(inode)->force_compress)
395                         compress_type = BTRFS_I(inode)->force_compress;
396
397                 ret = btrfs_compress_pages(compress_type,
398                                            inode->i_mapping, start,
399                                            total_compressed, pages,
400                                            nr_pages, &nr_pages_ret,
401                                            &total_in,
402                                            &total_compressed,
403                                            max_compressed);
404
405                 if (!ret) {
406                         unsigned long offset = total_compressed &
407                                 (PAGE_CACHE_SIZE - 1);
408                         struct page *page = pages[nr_pages_ret - 1];
409                         char *kaddr;
410
411                         /* zero the tail end of the last page, we might be
412                          * sending it down to disk
413                          */
414                         if (offset) {
415                                 kaddr = kmap_atomic(page, KM_USER0);
416                                 memset(kaddr + offset, 0,
417                                        PAGE_CACHE_SIZE - offset);
418                                 kunmap_atomic(kaddr, KM_USER0);
419                         }
420                         will_compress = 1;
421                 }
422         }
423         if (start == 0) {
424                 trans = btrfs_join_transaction(root, 1);
425                 BUG_ON(IS_ERR(trans));
426                 btrfs_set_trans_block_group(trans, inode);
427                 trans->block_rsv = &root->fs_info->delalloc_block_rsv;
428
429                 /* lets try to make an inline extent */
430                 if (ret || total_in < (actual_end - start)) {
431                         /* we didn't compress the entire range, try
432                          * to make an uncompressed inline extent.
433                          */
434                         ret = cow_file_range_inline(trans, root, inode,
435                                                     start, end, 0, 0, NULL);
436                 } else {
437                         /* try making a compressed inline extent */
438                         ret = cow_file_range_inline(trans, root, inode,
439                                                     start, end,
440                                                     total_compressed,
441                                                     compress_type, pages);
442                 }
443                 if (ret == 0) {
444                         /*
445                          * inline extent creation worked, we don't need
446                          * to create any more async work items.  Unlock
447                          * and free up our temp pages.
448                          */
449                         extent_clear_unlock_delalloc(inode,
450                              &BTRFS_I(inode)->io_tree,
451                              start, end, NULL,
452                              EXTENT_CLEAR_UNLOCK_PAGE | EXTENT_CLEAR_DIRTY |
453                              EXTENT_CLEAR_DELALLOC |
454                              EXTENT_SET_WRITEBACK | EXTENT_END_WRITEBACK);
455
456                         btrfs_end_transaction(trans, root);
457                         goto free_pages_out;
458                 }
459                 btrfs_end_transaction(trans, root);
460         }
461
462         if (will_compress) {
463                 /*
464                  * we aren't doing an inline extent round the compressed size
465                  * up to a block size boundary so the allocator does sane
466                  * things
467                  */
468                 total_compressed = (total_compressed + blocksize - 1) &
469                         ~(blocksize - 1);
470
471                 /*
472                  * one last check to make sure the compression is really a
473                  * win, compare the page count read with the blocks on disk
474                  */
475                 total_in = (total_in + PAGE_CACHE_SIZE - 1) &
476                         ~(PAGE_CACHE_SIZE - 1);
477                 if (total_compressed >= total_in) {
478                         will_compress = 0;
479                 } else {
480                         num_bytes = total_in;
481                 }
482         }
483         if (!will_compress && pages) {
484                 /*
485                  * the compression code ran but failed to make things smaller,
486                  * free any pages it allocated and our page pointer array
487                  */
488                 for (i = 0; i < nr_pages_ret; i++) {
489                         WARN_ON(pages[i]->mapping);
490                         page_cache_release(pages[i]);
491                 }
492                 kfree(pages);
493                 pages = NULL;
494                 total_compressed = 0;
495                 nr_pages_ret = 0;
496
497                 /* flag the file so we don't compress in the future */
498                 if (!btrfs_test_opt(root, FORCE_COMPRESS) &&
499                     !(BTRFS_I(inode)->force_compress)) {
500                         BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
501                 }
502         }
503         if (will_compress) {
504                 *num_added += 1;
505
506                 /* the async work queues will take care of doing actual
507                  * allocation on disk for these compressed pages,
508                  * and will submit them to the elevator.
509                  */
510                 add_async_extent(async_cow, start, num_bytes,
511                                  total_compressed, pages, nr_pages_ret,
512                                  compress_type);
513
514                 if (start + num_bytes < end) {
515                         start += num_bytes;
516                         pages = NULL;
517                         cond_resched();
518                         goto again;
519                 }
520         } else {
521 cleanup_and_bail_uncompressed:
522                 /*
523                  * No compression, but we still need to write the pages in
524                  * the file we've been given so far.  redirty the locked
525                  * page if it corresponds to our extent and set things up
526                  * for the async work queue to run cow_file_range to do
527                  * the normal delalloc dance
528                  */
529                 if (page_offset(locked_page) >= start &&
530                     page_offset(locked_page) <= end) {
531                         __set_page_dirty_nobuffers(locked_page);
532                         /* unlocked later on in the async handlers */
533                 }
534                 add_async_extent(async_cow, start, end - start + 1,
535                                  0, NULL, 0, BTRFS_COMPRESS_NONE);
536                 *num_added += 1;
537         }
538
539 out:
540         return 0;
541
542 free_pages_out:
543         for (i = 0; i < nr_pages_ret; i++) {
544                 WARN_ON(pages[i]->mapping);
545                 page_cache_release(pages[i]);
546         }
547         kfree(pages);
548
549         goto out;
550 }
551
552 /*
553  * phase two of compressed writeback.  This is the ordered portion
554  * of the code, which only gets called in the order the work was
555  * queued.  We walk all the async extents created by compress_file_range
556  * and send them down to the disk.
557  */
558 static noinline int submit_compressed_extents(struct inode *inode,
559                                               struct async_cow *async_cow)
560 {
561         struct async_extent *async_extent;
562         u64 alloc_hint = 0;
563         struct btrfs_trans_handle *trans;
564         struct btrfs_key ins;
565         struct extent_map *em;
566         struct btrfs_root *root = BTRFS_I(inode)->root;
567         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
568         struct extent_io_tree *io_tree;
569         int ret = 0;
570
571         if (list_empty(&async_cow->extents))
572                 return 0;
573
574
575         while (!list_empty(&async_cow->extents)) {
576                 async_extent = list_entry(async_cow->extents.next,
577                                           struct async_extent, list);
578                 list_del(&async_extent->list);
579
580                 io_tree = &BTRFS_I(inode)->io_tree;
581
582 retry:
583                 /* did the compression code fall back to uncompressed IO? */
584                 if (!async_extent->pages) {
585                         int page_started = 0;
586                         unsigned long nr_written = 0;
587
588                         lock_extent(io_tree, async_extent->start,
589                                          async_extent->start +
590                                          async_extent->ram_size - 1, GFP_NOFS);
591
592                         /* allocate blocks */
593                         ret = cow_file_range(inode, async_cow->locked_page,
594                                              async_extent->start,
595                                              async_extent->start +
596                                              async_extent->ram_size - 1,
597                                              &page_started, &nr_written, 0);
598
599                         /*
600                          * if page_started, cow_file_range inserted an
601                          * inline extent and took care of all the unlocking
602                          * and IO for us.  Otherwise, we need to submit
603                          * all those pages down to the drive.
604                          */
605                         if (!page_started && !ret)
606                                 extent_write_locked_range(io_tree,
607                                                   inode, async_extent->start,
608                                                   async_extent->start +
609                                                   async_extent->ram_size - 1,
610                                                   btrfs_get_extent,
611                                                   WB_SYNC_ALL);
612                         kfree(async_extent);
613                         cond_resched();
614                         continue;
615                 }
616
617                 lock_extent(io_tree, async_extent->start,
618                             async_extent->start + async_extent->ram_size - 1,
619                             GFP_NOFS);
620
621                 trans = btrfs_join_transaction(root, 1);
622                 BUG_ON(IS_ERR(trans));
623                 ret = btrfs_reserve_extent(trans, root,
624                                            async_extent->compressed_size,
625                                            async_extent->compressed_size,
626                                            0, alloc_hint,
627                                            (u64)-1, &ins, 1);
628                 btrfs_end_transaction(trans, root);
629
630                 if (ret) {
631                         int i;
632                         for (i = 0; i < async_extent->nr_pages; i++) {
633                                 WARN_ON(async_extent->pages[i]->mapping);
634                                 page_cache_release(async_extent->pages[i]);
635                         }
636                         kfree(async_extent->pages);
637                         async_extent->nr_pages = 0;
638                         async_extent->pages = NULL;
639                         unlock_extent(io_tree, async_extent->start,
640                                       async_extent->start +
641                                       async_extent->ram_size - 1, GFP_NOFS);
642                         goto retry;
643                 }
644
645                 /*
646                  * here we're doing allocation and writeback of the
647                  * compressed pages
648                  */
649                 btrfs_drop_extent_cache(inode, async_extent->start,
650                                         async_extent->start +
651                                         async_extent->ram_size - 1, 0);
652
653                 em = alloc_extent_map(GFP_NOFS);
654                 BUG_ON(!em);
655                 em->start = async_extent->start;
656                 em->len = async_extent->ram_size;
657                 em->orig_start = em->start;
658
659                 em->block_start = ins.objectid;
660                 em->block_len = ins.offset;
661                 em->bdev = root->fs_info->fs_devices->latest_bdev;
662                 em->compress_type = async_extent->compress_type;
663                 set_bit(EXTENT_FLAG_PINNED, &em->flags);
664                 set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
665
666                 while (1) {
667                         write_lock(&em_tree->lock);
668                         ret = add_extent_mapping(em_tree, em);
669                         write_unlock(&em_tree->lock);
670                         if (ret != -EEXIST) {
671                                 free_extent_map(em);
672                                 break;
673                         }
674                         btrfs_drop_extent_cache(inode, async_extent->start,
675                                                 async_extent->start +
676                                                 async_extent->ram_size - 1, 0);
677                 }
678
679                 ret = btrfs_add_ordered_extent_compress(inode,
680                                                 async_extent->start,
681                                                 ins.objectid,
682                                                 async_extent->ram_size,
683                                                 ins.offset,
684                                                 BTRFS_ORDERED_COMPRESSED,
685                                                 async_extent->compress_type);
686                 BUG_ON(ret);
687
688                 /*
689                  * clear dirty, set writeback and unlock the pages.
690                  */
691                 extent_clear_unlock_delalloc(inode,
692                                 &BTRFS_I(inode)->io_tree,
693                                 async_extent->start,
694                                 async_extent->start +
695                                 async_extent->ram_size - 1,
696                                 NULL, EXTENT_CLEAR_UNLOCK_PAGE |
697                                 EXTENT_CLEAR_UNLOCK |
698                                 EXTENT_CLEAR_DELALLOC |
699                                 EXTENT_CLEAR_DIRTY | EXTENT_SET_WRITEBACK);
700
701                 ret = btrfs_submit_compressed_write(inode,
702                                     async_extent->start,
703                                     async_extent->ram_size,
704                                     ins.objectid,
705                                     ins.offset, async_extent->pages,
706                                     async_extent->nr_pages);
707
708                 BUG_ON(ret);
709                 alloc_hint = ins.objectid + ins.offset;
710                 kfree(async_extent);
711                 cond_resched();
712         }
713
714         return 0;
715 }
716
717 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
718                                       u64 num_bytes)
719 {
720         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
721         struct extent_map *em;
722         u64 alloc_hint = 0;
723
724         read_lock(&em_tree->lock);
725         em = search_extent_mapping(em_tree, start, num_bytes);
726         if (em) {
727                 /*
728                  * if block start isn't an actual block number then find the
729                  * first block in this inode and use that as a hint.  If that
730                  * block is also bogus then just don't worry about it.
731                  */
732                 if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
733                         free_extent_map(em);
734                         em = search_extent_mapping(em_tree, 0, 0);
735                         if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
736                                 alloc_hint = em->block_start;
737                         if (em)
738                                 free_extent_map(em);
739                 } else {
740                         alloc_hint = em->block_start;
741                         free_extent_map(em);
742                 }
743         }
744         read_unlock(&em_tree->lock);
745
746         return alloc_hint;
747 }
748
749 static inline bool is_free_space_inode(struct btrfs_root *root,
750                                        struct inode *inode)
751 {
752         if (root == root->fs_info->tree_root ||
753             BTRFS_I(inode)->location.objectid == BTRFS_FREE_INO_OBJECTID)
754                 return true;
755         return false;
756 }
757
758 /*
759  * when extent_io.c finds a delayed allocation range in the file,
760  * the call backs end up in this code.  The basic idea is to
761  * allocate extents on disk for the range, and create ordered data structs
762  * in ram to track those extents.
763  *
764  * locked_page is the page that writepage had locked already.  We use
765  * it to make sure we don't do extra locks or unlocks.
766  *
767  * *page_started is set to one if we unlock locked_page and do everything
768  * required to start IO on it.  It may be clean and already done with
769  * IO when we return.
770  */
771 static noinline int cow_file_range(struct inode *inode,
772                                    struct page *locked_page,
773                                    u64 start, u64 end, int *page_started,
774                                    unsigned long *nr_written,
775                                    int unlock)
776 {
777         struct btrfs_root *root = BTRFS_I(inode)->root;
778         struct btrfs_trans_handle *trans;
779         u64 alloc_hint = 0;
780         u64 num_bytes;
781         unsigned long ram_size;
782         u64 disk_num_bytes;
783         u64 cur_alloc_size;
784         u64 blocksize = root->sectorsize;
785         struct btrfs_key ins;
786         struct extent_map *em;
787         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
788         int ret = 0;
789
790         BUG_ON(is_free_space_inode(root, inode));
791         trans = btrfs_join_transaction(root, 1);
792         BUG_ON(IS_ERR(trans));
793         btrfs_set_trans_block_group(trans, inode);
794         trans->block_rsv = &root->fs_info->delalloc_block_rsv;
795
796         num_bytes = (end - start + blocksize) & ~(blocksize - 1);
797         num_bytes = max(blocksize,  num_bytes);
798         disk_num_bytes = num_bytes;
799         ret = 0;
800
801         if (start == 0) {
802                 /* lets try to make an inline extent */
803                 ret = cow_file_range_inline(trans, root, inode,
804                                             start, end, 0, 0, NULL);
805                 if (ret == 0) {
806                         extent_clear_unlock_delalloc(inode,
807                                      &BTRFS_I(inode)->io_tree,
808                                      start, end, NULL,
809                                      EXTENT_CLEAR_UNLOCK_PAGE |
810                                      EXTENT_CLEAR_UNLOCK |
811                                      EXTENT_CLEAR_DELALLOC |
812                                      EXTENT_CLEAR_DIRTY |
813                                      EXTENT_SET_WRITEBACK |
814                                      EXTENT_END_WRITEBACK);
815
816                         *nr_written = *nr_written +
817                              (end - start + PAGE_CACHE_SIZE) / PAGE_CACHE_SIZE;
818                         *page_started = 1;
819                         ret = 0;
820                         goto out;
821                 }
822         }
823
824         BUG_ON(disk_num_bytes >
825                btrfs_super_total_bytes(&root->fs_info->super_copy));
826
827         alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
828         btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
829
830         while (disk_num_bytes > 0) {
831                 unsigned long op;
832
833                 cur_alloc_size = disk_num_bytes;
834                 ret = btrfs_reserve_extent(trans, root, cur_alloc_size,
835                                            root->sectorsize, 0, alloc_hint,
836                                            (u64)-1, &ins, 1);
837                 BUG_ON(ret);
838
839                 em = alloc_extent_map(GFP_NOFS);
840                 BUG_ON(!em);
841                 em->start = start;
842                 em->orig_start = em->start;
843                 ram_size = ins.offset;
844                 em->len = ins.offset;
845
846                 em->block_start = ins.objectid;
847                 em->block_len = ins.offset;
848                 em->bdev = root->fs_info->fs_devices->latest_bdev;
849                 set_bit(EXTENT_FLAG_PINNED, &em->flags);
850
851                 while (1) {
852                         write_lock(&em_tree->lock);
853                         ret = add_extent_mapping(em_tree, em);
854                         write_unlock(&em_tree->lock);
855                         if (ret != -EEXIST) {
856                                 free_extent_map(em);
857                                 break;
858                         }
859                         btrfs_drop_extent_cache(inode, start,
860                                                 start + ram_size - 1, 0);
861                 }
862
863                 cur_alloc_size = ins.offset;
864                 ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
865                                                ram_size, cur_alloc_size, 0);
866                 BUG_ON(ret);
867
868                 if (root->root_key.objectid ==
869                     BTRFS_DATA_RELOC_TREE_OBJECTID) {
870                         ret = btrfs_reloc_clone_csums(inode, start,
871                                                       cur_alloc_size);
872                         BUG_ON(ret);
873                 }
874
875                 if (disk_num_bytes < cur_alloc_size)
876                         break;
877
878                 /* we're not doing compressed IO, don't unlock the first
879                  * page (which the caller expects to stay locked), don't
880                  * clear any dirty bits and don't set any writeback bits
881                  *
882                  * Do set the Private2 bit so we know this page was properly
883                  * setup for writepage
884                  */
885                 op = unlock ? EXTENT_CLEAR_UNLOCK_PAGE : 0;
886                 op |= EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
887                         EXTENT_SET_PRIVATE2;
888
889                 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
890                                              start, start + ram_size - 1,
891                                              locked_page, op);
892                 disk_num_bytes -= cur_alloc_size;
893                 num_bytes -= cur_alloc_size;
894                 alloc_hint = ins.objectid + ins.offset;
895                 start += cur_alloc_size;
896         }
897 out:
898         ret = 0;
899         btrfs_end_transaction(trans, root);
900
901         return ret;
902 }
903
904 /*
905  * work queue call back to started compression on a file and pages
906  */
907 static noinline void async_cow_start(struct btrfs_work *work)
908 {
909         struct async_cow *async_cow;
910         int num_added = 0;
911         async_cow = container_of(work, struct async_cow, work);
912
913         compress_file_range(async_cow->inode, async_cow->locked_page,
914                             async_cow->start, async_cow->end, async_cow,
915                             &num_added);
916         if (num_added == 0)
917                 async_cow->inode = NULL;
918 }
919
920 /*
921  * work queue call back to submit previously compressed pages
922  */
923 static noinline void async_cow_submit(struct btrfs_work *work)
924 {
925         struct async_cow *async_cow;
926         struct btrfs_root *root;
927         unsigned long nr_pages;
928
929         async_cow = container_of(work, struct async_cow, work);
930
931         root = async_cow->root;
932         nr_pages = (async_cow->end - async_cow->start + PAGE_CACHE_SIZE) >>
933                 PAGE_CACHE_SHIFT;
934
935         atomic_sub(nr_pages, &root->fs_info->async_delalloc_pages);
936
937         if (atomic_read(&root->fs_info->async_delalloc_pages) <
938             5 * 1042 * 1024 &&
939             waitqueue_active(&root->fs_info->async_submit_wait))
940                 wake_up(&root->fs_info->async_submit_wait);
941
942         if (async_cow->inode)
943                 submit_compressed_extents(async_cow->inode, async_cow);
944 }
945
946 static noinline void async_cow_free(struct btrfs_work *work)
947 {
948         struct async_cow *async_cow;
949         async_cow = container_of(work, struct async_cow, work);
950         kfree(async_cow);
951 }
952
953 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
954                                 u64 start, u64 end, int *page_started,
955                                 unsigned long *nr_written)
956 {
957         struct async_cow *async_cow;
958         struct btrfs_root *root = BTRFS_I(inode)->root;
959         unsigned long nr_pages;
960         u64 cur_end;
961         int limit = 10 * 1024 * 1042;
962
963         clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
964                          1, 0, NULL, GFP_NOFS);
965         while (start < end) {
966                 async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
967                 BUG_ON(!async_cow);
968                 async_cow->inode = inode;
969                 async_cow->root = root;
970                 async_cow->locked_page = locked_page;
971                 async_cow->start = start;
972
973                 if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
974                         cur_end = end;
975                 else
976                         cur_end = min(end, start + 512 * 1024 - 1);
977
978                 async_cow->end = cur_end;
979                 INIT_LIST_HEAD(&async_cow->extents);
980
981                 async_cow->work.func = async_cow_start;
982                 async_cow->work.ordered_func = async_cow_submit;
983                 async_cow->work.ordered_free = async_cow_free;
984                 async_cow->work.flags = 0;
985
986                 nr_pages = (cur_end - start + PAGE_CACHE_SIZE) >>
987                         PAGE_CACHE_SHIFT;
988                 atomic_add(nr_pages, &root->fs_info->async_delalloc_pages);
989
990                 btrfs_queue_worker(&root->fs_info->delalloc_workers,
991                                    &async_cow->work);
992
993                 if (atomic_read(&root->fs_info->async_delalloc_pages) > limit) {
994                         wait_event(root->fs_info->async_submit_wait,
995                            (atomic_read(&root->fs_info->async_delalloc_pages) <
996                             limit));
997                 }
998
999                 while (atomic_read(&root->fs_info->async_submit_draining) &&
1000                       atomic_read(&root->fs_info->async_delalloc_pages)) {
1001                         wait_event(root->fs_info->async_submit_wait,
1002                           (atomic_read(&root->fs_info->async_delalloc_pages) ==
1003                            0));
1004                 }
1005
1006                 *nr_written += nr_pages;
1007                 start = cur_end + 1;
1008         }
1009         *page_started = 1;
1010         return 0;
1011 }
1012
1013 static noinline int csum_exist_in_range(struct btrfs_root *root,
1014                                         u64 bytenr, u64 num_bytes)
1015 {
1016         int ret;
1017         struct btrfs_ordered_sum *sums;
1018         LIST_HEAD(list);
1019
1020         ret = btrfs_lookup_csums_range(root->fs_info->csum_root, bytenr,
1021                                        bytenr + num_bytes - 1, &list);
1022         if (ret == 0 && list_empty(&list))
1023                 return 0;
1024
1025         while (!list_empty(&list)) {
1026                 sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1027                 list_del(&sums->list);
1028                 kfree(sums);
1029         }
1030         return 1;
1031 }
1032
1033 /*
1034  * when nowcow writeback call back.  This checks for snapshots or COW copies
1035  * of the extents that exist in the file, and COWs the file as required.
1036  *
1037  * If no cow copies or snapshots exist, we write directly to the existing
1038  * blocks on disk
1039  */
1040 static noinline int run_delalloc_nocow(struct inode *inode,
1041                                        struct page *locked_page,
1042                               u64 start, u64 end, int *page_started, int force,
1043                               unsigned long *nr_written)
1044 {
1045         struct btrfs_root *root = BTRFS_I(inode)->root;
1046         struct btrfs_trans_handle *trans;
1047         struct extent_buffer *leaf;
1048         struct btrfs_path *path;
1049         struct btrfs_file_extent_item *fi;
1050         struct btrfs_key found_key;
1051         u64 cow_start;
1052         u64 cur_offset;
1053         u64 extent_end;
1054         u64 extent_offset;
1055         u64 disk_bytenr;
1056         u64 num_bytes;
1057         int extent_type;
1058         int ret;
1059         int type;
1060         int nocow;
1061         int check_prev = 1;
1062         bool nolock;
1063         u64 ino = btrfs_ino(inode);
1064
1065         path = btrfs_alloc_path();
1066         BUG_ON(!path);
1067
1068         nolock = is_free_space_inode(root, inode);
1069
1070         if (nolock)
1071                 trans = btrfs_join_transaction_nolock(root, 1);
1072         else
1073                 trans = btrfs_join_transaction(root, 1);
1074         BUG_ON(IS_ERR(trans));
1075
1076         cow_start = (u64)-1;
1077         cur_offset = start;
1078         while (1) {
1079                 ret = btrfs_lookup_file_extent(trans, root, path, ino,
1080                                                cur_offset, 0);
1081                 BUG_ON(ret < 0);
1082                 if (ret > 0 && path->slots[0] > 0 && check_prev) {
1083                         leaf = path->nodes[0];
1084                         btrfs_item_key_to_cpu(leaf, &found_key,
1085                                               path->slots[0] - 1);
1086                         if (found_key.objectid == ino &&
1087                             found_key.type == BTRFS_EXTENT_DATA_KEY)
1088                                 path->slots[0]--;
1089                 }
1090                 check_prev = 0;
1091 next_slot:
1092                 leaf = path->nodes[0];
1093                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1094                         ret = btrfs_next_leaf(root, path);
1095                         if (ret < 0)
1096                                 BUG_ON(1);
1097                         if (ret > 0)
1098                                 break;
1099                         leaf = path->nodes[0];
1100                 }
1101
1102                 nocow = 0;
1103                 disk_bytenr = 0;
1104                 num_bytes = 0;
1105                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1106
1107                 if (found_key.objectid > ino ||
1108                     found_key.type > BTRFS_EXTENT_DATA_KEY ||
1109                     found_key.offset > end)
1110                         break;
1111
1112                 if (found_key.offset > cur_offset) {
1113                         extent_end = found_key.offset;
1114                         extent_type = 0;
1115                         goto out_check;
1116                 }
1117
1118                 fi = btrfs_item_ptr(leaf, path->slots[0],
1119                                     struct btrfs_file_extent_item);
1120                 extent_type = btrfs_file_extent_type(leaf, fi);
1121
1122                 if (extent_type == BTRFS_FILE_EXTENT_REG ||
1123                     extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1124                         disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1125                         extent_offset = btrfs_file_extent_offset(leaf, fi);
1126                         extent_end = found_key.offset +
1127                                 btrfs_file_extent_num_bytes(leaf, fi);
1128                         if (extent_end <= start) {
1129                                 path->slots[0]++;
1130                                 goto next_slot;
1131                         }
1132                         if (disk_bytenr == 0)
1133                                 goto out_check;
1134                         if (btrfs_file_extent_compression(leaf, fi) ||
1135                             btrfs_file_extent_encryption(leaf, fi) ||
1136                             btrfs_file_extent_other_encoding(leaf, fi))
1137                                 goto out_check;
1138                         if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1139                                 goto out_check;
1140                         if (btrfs_extent_readonly(root, disk_bytenr))
1141                                 goto out_check;
1142                         if (btrfs_cross_ref_exist(trans, root, ino,
1143                                                   found_key.offset -
1144                                                   extent_offset, disk_bytenr))
1145                                 goto out_check;
1146                         disk_bytenr += extent_offset;
1147                         disk_bytenr += cur_offset - found_key.offset;
1148                         num_bytes = min(end + 1, extent_end) - cur_offset;
1149                         /*
1150                          * force cow if csum exists in the range.
1151                          * this ensure that csum for a given extent are
1152                          * either valid or do not exist.
1153                          */
1154                         if (csum_exist_in_range(root, disk_bytenr, num_bytes))
1155                                 goto out_check;
1156                         nocow = 1;
1157                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1158                         extent_end = found_key.offset +
1159                                 btrfs_file_extent_inline_len(leaf, fi);
1160                         extent_end = ALIGN(extent_end, root->sectorsize);
1161                 } else {
1162                         BUG_ON(1);
1163                 }
1164 out_check:
1165                 if (extent_end <= start) {
1166                         path->slots[0]++;
1167                         goto next_slot;
1168                 }
1169                 if (!nocow) {
1170                         if (cow_start == (u64)-1)
1171                                 cow_start = cur_offset;
1172                         cur_offset = extent_end;
1173                         if (cur_offset > end)
1174                                 break;
1175                         path->slots[0]++;
1176                         goto next_slot;
1177                 }
1178
1179                 btrfs_release_path(root, path);
1180                 if (cow_start != (u64)-1) {
1181                         ret = cow_file_range(inode, locked_page, cow_start,
1182                                         found_key.offset - 1, page_started,
1183                                         nr_written, 1);
1184                         BUG_ON(ret);
1185                         cow_start = (u64)-1;
1186                 }
1187
1188                 if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1189                         struct extent_map *em;
1190                         struct extent_map_tree *em_tree;
1191                         em_tree = &BTRFS_I(inode)->extent_tree;
1192                         em = alloc_extent_map(GFP_NOFS);
1193                         BUG_ON(!em);
1194                         em->start = cur_offset;
1195                         em->orig_start = em->start;
1196                         em->len = num_bytes;
1197                         em->block_len = num_bytes;
1198                         em->block_start = disk_bytenr;
1199                         em->bdev = root->fs_info->fs_devices->latest_bdev;
1200                         set_bit(EXTENT_FLAG_PINNED, &em->flags);
1201                         while (1) {
1202                                 write_lock(&em_tree->lock);
1203                                 ret = add_extent_mapping(em_tree, em);
1204                                 write_unlock(&em_tree->lock);
1205                                 if (ret != -EEXIST) {
1206                                         free_extent_map(em);
1207                                         break;
1208                                 }
1209                                 btrfs_drop_extent_cache(inode, em->start,
1210                                                 em->start + em->len - 1, 0);
1211                         }
1212                         type = BTRFS_ORDERED_PREALLOC;
1213                 } else {
1214                         type = BTRFS_ORDERED_NOCOW;
1215                 }
1216
1217                 ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1218                                                num_bytes, num_bytes, type);
1219                 BUG_ON(ret);
1220
1221                 if (root->root_key.objectid ==
1222                     BTRFS_DATA_RELOC_TREE_OBJECTID) {
1223                         ret = btrfs_reloc_clone_csums(inode, cur_offset,
1224                                                       num_bytes);
1225                         BUG_ON(ret);
1226                 }
1227
1228                 extent_clear_unlock_delalloc(inode, &BTRFS_I(inode)->io_tree,
1229                                 cur_offset, cur_offset + num_bytes - 1,
1230                                 locked_page, EXTENT_CLEAR_UNLOCK_PAGE |
1231                                 EXTENT_CLEAR_UNLOCK | EXTENT_CLEAR_DELALLOC |
1232                                 EXTENT_SET_PRIVATE2);
1233                 cur_offset = extent_end;
1234                 if (cur_offset > end)
1235                         break;
1236         }
1237         btrfs_release_path(root, path);
1238
1239         if (cur_offset <= end && cow_start == (u64)-1)
1240                 cow_start = cur_offset;
1241         if (cow_start != (u64)-1) {
1242                 ret = cow_file_range(inode, locked_page, cow_start, end,
1243                                      page_started, nr_written, 1);
1244                 BUG_ON(ret);
1245         }
1246
1247         if (nolock) {
1248                 ret = btrfs_end_transaction_nolock(trans, root);
1249                 BUG_ON(ret);
1250         } else {
1251                 ret = btrfs_end_transaction(trans, root);
1252                 BUG_ON(ret);
1253         }
1254         btrfs_free_path(path);
1255         return 0;
1256 }
1257
1258 /*
1259  * extent_io.c call back to do delayed allocation processing
1260  */
1261 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1262                               u64 start, u64 end, int *page_started,
1263                               unsigned long *nr_written)
1264 {
1265         int ret;
1266         struct btrfs_root *root = BTRFS_I(inode)->root;
1267
1268         if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW)
1269                 ret = run_delalloc_nocow(inode, locked_page, start, end,
1270                                          page_started, 1, nr_written);
1271         else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC)
1272                 ret = run_delalloc_nocow(inode, locked_page, start, end,
1273                                          page_started, 0, nr_written);
1274         else if (!btrfs_test_opt(root, COMPRESS) &&
1275                  !(BTRFS_I(inode)->force_compress) &&
1276                  !(BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS))
1277                 ret = cow_file_range(inode, locked_page, start, end,
1278                                       page_started, nr_written, 1);
1279         else
1280                 ret = cow_file_range_async(inode, locked_page, start, end,
1281                                            page_started, nr_written);
1282         return ret;
1283 }
1284
1285 static int btrfs_split_extent_hook(struct inode *inode,
1286                                    struct extent_state *orig, u64 split)
1287 {
1288         /* not delalloc, ignore it */
1289         if (!(orig->state & EXTENT_DELALLOC))
1290                 return 0;
1291
1292         atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1293         return 0;
1294 }
1295
1296 /*
1297  * extent_io.c merge_extent_hook, used to track merged delayed allocation
1298  * extents so we can keep track of new extents that are just merged onto old
1299  * extents, such as when we are doing sequential writes, so we can properly
1300  * account for the metadata space we'll need.
1301  */
1302 static int btrfs_merge_extent_hook(struct inode *inode,
1303                                    struct extent_state *new,
1304                                    struct extent_state *other)
1305 {
1306         /* not delalloc, ignore it */
1307         if (!(other->state & EXTENT_DELALLOC))
1308                 return 0;
1309
1310         atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1311         return 0;
1312 }
1313
1314 /*
1315  * extent_io.c set_bit_hook, used to track delayed allocation
1316  * bytes in this file, and to maintain the list of inodes that
1317  * have pending delalloc work to be done.
1318  */
1319 static int btrfs_set_bit_hook(struct inode *inode,
1320                               struct extent_state *state, int *bits)
1321 {
1322
1323         /*
1324          * set_bit and clear bit hooks normally require _irqsave/restore
1325          * but in this case, we are only testeing for the DELALLOC
1326          * bit, which is only set or cleared with irqs on
1327          */
1328         if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1329                 struct btrfs_root *root = BTRFS_I(inode)->root;
1330                 u64 len = state->end + 1 - state->start;
1331                 bool do_list = !is_free_space_inode(root, inode);
1332
1333                 if (*bits & EXTENT_FIRST_DELALLOC)
1334                         *bits &= ~EXTENT_FIRST_DELALLOC;
1335                 else
1336                         atomic_inc(&BTRFS_I(inode)->outstanding_extents);
1337
1338                 spin_lock(&root->fs_info->delalloc_lock);
1339                 BTRFS_I(inode)->delalloc_bytes += len;
1340                 root->fs_info->delalloc_bytes += len;
1341                 if (do_list && list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1342                         list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1343                                       &root->fs_info->delalloc_inodes);
1344                 }
1345                 spin_unlock(&root->fs_info->delalloc_lock);
1346         }
1347         return 0;
1348 }
1349
1350 /*
1351  * extent_io.c clear_bit_hook, see set_bit_hook for why
1352  */
1353 static int btrfs_clear_bit_hook(struct inode *inode,
1354                                 struct extent_state *state, int *bits)
1355 {
1356         /*
1357          * set_bit and clear bit hooks normally require _irqsave/restore
1358          * but in this case, we are only testeing for the DELALLOC
1359          * bit, which is only set or cleared with irqs on
1360          */
1361         if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1362                 struct btrfs_root *root = BTRFS_I(inode)->root;
1363                 u64 len = state->end + 1 - state->start;
1364                 bool do_list = !is_free_space_inode(root, inode);
1365
1366                 if (*bits & EXTENT_FIRST_DELALLOC)
1367                         *bits &= ~EXTENT_FIRST_DELALLOC;
1368                 else if (!(*bits & EXTENT_DO_ACCOUNTING))
1369                         atomic_dec(&BTRFS_I(inode)->outstanding_extents);
1370
1371                 if (*bits & EXTENT_DO_ACCOUNTING)
1372                         btrfs_delalloc_release_metadata(inode, len);
1373
1374                 if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1375                     && do_list)
1376                         btrfs_free_reserved_data_space(inode, len);
1377
1378                 spin_lock(&root->fs_info->delalloc_lock);
1379                 root->fs_info->delalloc_bytes -= len;
1380                 BTRFS_I(inode)->delalloc_bytes -= len;
1381
1382                 if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1383                     !list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1384                         list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1385                 }
1386                 spin_unlock(&root->fs_info->delalloc_lock);
1387         }
1388         return 0;
1389 }
1390
1391 /*
1392  * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1393  * we don't create bios that span stripes or chunks
1394  */
1395 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1396                          size_t size, struct bio *bio,
1397                          unsigned long bio_flags)
1398 {
1399         struct btrfs_root *root = BTRFS_I(page->mapping->host)->root;
1400         struct btrfs_mapping_tree *map_tree;
1401         u64 logical = (u64)bio->bi_sector << 9;
1402         u64 length = 0;
1403         u64 map_length;
1404         int ret;
1405
1406         if (bio_flags & EXTENT_BIO_COMPRESSED)
1407                 return 0;
1408
1409         length = bio->bi_size;
1410         map_tree = &root->fs_info->mapping_tree;
1411         map_length = length;
1412         ret = btrfs_map_block(map_tree, READ, logical,
1413                               &map_length, NULL, 0);
1414
1415         if (map_length < length + size)
1416                 return 1;
1417         return ret;
1418 }
1419
1420 /*
1421  * in order to insert checksums into the metadata in large chunks,
1422  * we wait until bio submission time.   All the pages in the bio are
1423  * checksummed and sums are attached onto the ordered extent record.
1424  *
1425  * At IO completion time the cums attached on the ordered extent record
1426  * are inserted into the btree
1427  */
1428 static int __btrfs_submit_bio_start(struct inode *inode, int rw,
1429                                     struct bio *bio, int mirror_num,
1430                                     unsigned long bio_flags,
1431                                     u64 bio_offset)
1432 {
1433         struct btrfs_root *root = BTRFS_I(inode)->root;
1434         int ret = 0;
1435
1436         ret = btrfs_csum_one_bio(root, inode, bio, 0, 0);
1437         BUG_ON(ret);
1438         return 0;
1439 }
1440
1441 /*
1442  * in order to insert checksums into the metadata in large chunks,
1443  * we wait until bio submission time.   All the pages in the bio are
1444  * checksummed and sums are attached onto the ordered extent record.
1445  *
1446  * At IO completion time the cums attached on the ordered extent record
1447  * are inserted into the btree
1448  */
1449 static int __btrfs_submit_bio_done(struct inode *inode, int rw, struct bio *bio,
1450                           int mirror_num, unsigned long bio_flags,
1451                           u64 bio_offset)
1452 {
1453         struct btrfs_root *root = BTRFS_I(inode)->root;
1454         return btrfs_map_bio(root, rw, bio, mirror_num, 1);
1455 }
1456
1457 /*
1458  * extent_io.c submission hook. This does the right thing for csum calculation
1459  * on write, or reading the csums from the tree before a read
1460  */
1461 static int btrfs_submit_bio_hook(struct inode *inode, int rw, struct bio *bio,
1462                           int mirror_num, unsigned long bio_flags,
1463                           u64 bio_offset)
1464 {
1465         struct btrfs_root *root = BTRFS_I(inode)->root;
1466         int ret = 0;
1467         int skip_sum;
1468
1469         skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1470
1471         if (is_free_space_inode(root, inode))
1472                 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 2);
1473         else
1474                 ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
1475         BUG_ON(ret);
1476
1477         if (!(rw & REQ_WRITE)) {
1478                 if (bio_flags & EXTENT_BIO_COMPRESSED) {
1479                         return btrfs_submit_compressed_read(inode, bio,
1480                                                     mirror_num, bio_flags);
1481                 } else if (!skip_sum) {
1482                         ret = btrfs_lookup_bio_sums(root, inode, bio, NULL);
1483                         if (ret)
1484                                 return ret;
1485                 }
1486                 goto mapit;
1487         } else if (!skip_sum) {
1488                 /* csum items have already been cloned */
1489                 if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1490                         goto mapit;
1491                 /* we're doing a write, do the async checksumming */
1492                 return btrfs_wq_submit_bio(BTRFS_I(inode)->root->fs_info,
1493                                    inode, rw, bio, mirror_num,
1494                                    bio_flags, bio_offset,
1495                                    __btrfs_submit_bio_start,
1496                                    __btrfs_submit_bio_done);
1497         }
1498
1499 mapit:
1500         return btrfs_map_bio(root, rw, bio, mirror_num, 0);
1501 }
1502
1503 /*
1504  * given a list of ordered sums record them in the inode.  This happens
1505  * at IO completion time based on sums calculated at bio submission time.
1506  */
1507 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1508                              struct inode *inode, u64 file_offset,
1509                              struct list_head *list)
1510 {
1511         struct btrfs_ordered_sum *sum;
1512
1513         btrfs_set_trans_block_group(trans, inode);
1514
1515         list_for_each_entry(sum, list, list) {
1516                 btrfs_csum_file_blocks(trans,
1517                        BTRFS_I(inode)->root->fs_info->csum_root, sum);
1518         }
1519         return 0;
1520 }
1521
1522 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1523                               struct extent_state **cached_state)
1524 {
1525         if ((end & (PAGE_CACHE_SIZE - 1)) == 0)
1526                 WARN_ON(1);
1527         return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1528                                    cached_state, GFP_NOFS);
1529 }
1530
1531 /* see btrfs_writepage_start_hook for details on why this is required */
1532 struct btrfs_writepage_fixup {
1533         struct page *page;
1534         struct btrfs_work work;
1535 };
1536
1537 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1538 {
1539         struct btrfs_writepage_fixup *fixup;
1540         struct btrfs_ordered_extent *ordered;
1541         struct extent_state *cached_state = NULL;
1542         struct page *page;
1543         struct inode *inode;
1544         u64 page_start;
1545         u64 page_end;
1546
1547         fixup = container_of(work, struct btrfs_writepage_fixup, work);
1548         page = fixup->page;
1549 again:
1550         lock_page(page);
1551         if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1552                 ClearPageChecked(page);
1553                 goto out_page;
1554         }
1555
1556         inode = page->mapping->host;
1557         page_start = page_offset(page);
1558         page_end = page_offset(page) + PAGE_CACHE_SIZE - 1;
1559
1560         lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end, 0,
1561                          &cached_state, GFP_NOFS);
1562
1563         /* already ordered? We're done */
1564         if (PagePrivate2(page))
1565                 goto out;
1566
1567         ordered = btrfs_lookup_ordered_extent(inode, page_start);
1568         if (ordered) {
1569                 unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1570                                      page_end, &cached_state, GFP_NOFS);
1571                 unlock_page(page);
1572                 btrfs_start_ordered_extent(inode, ordered, 1);
1573                 goto again;
1574         }
1575
1576         BUG();
1577         btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state);
1578         ClearPageChecked(page);
1579 out:
1580         unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1581                              &cached_state, GFP_NOFS);
1582 out_page:
1583         unlock_page(page);
1584         page_cache_release(page);
1585         kfree(fixup);
1586 }
1587
1588 /*
1589  * There are a few paths in the higher layers of the kernel that directly
1590  * set the page dirty bit without asking the filesystem if it is a
1591  * good idea.  This causes problems because we want to make sure COW
1592  * properly happens and the data=ordered rules are followed.
1593  *
1594  * In our case any range that doesn't have the ORDERED bit set
1595  * hasn't been properly setup for IO.  We kick off an async process
1596  * to fix it up.  The async helper will wait for ordered extents, set
1597  * the delalloc bit and make it safe to write the page.
1598  */
1599 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
1600 {
1601         struct inode *inode = page->mapping->host;
1602         struct btrfs_writepage_fixup *fixup;
1603         struct btrfs_root *root = BTRFS_I(inode)->root;
1604
1605         /* this page is properly in the ordered list */
1606         if (TestClearPagePrivate2(page))
1607                 return 0;
1608
1609         if (PageChecked(page))
1610                 return -EAGAIN;
1611
1612         fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
1613         if (!fixup)
1614                 return -EAGAIN;
1615
1616         SetPageChecked(page);
1617         page_cache_get(page);
1618         fixup->work.func = btrfs_writepage_fixup_worker;
1619         fixup->page = page;
1620         btrfs_queue_worker(&root->fs_info->fixup_workers, &fixup->work);
1621         return -EAGAIN;
1622 }
1623
1624 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
1625                                        struct inode *inode, u64 file_pos,
1626                                        u64 disk_bytenr, u64 disk_num_bytes,
1627                                        u64 num_bytes, u64 ram_bytes,
1628                                        u8 compression, u8 encryption,
1629                                        u16 other_encoding, int extent_type)
1630 {
1631         struct btrfs_root *root = BTRFS_I(inode)->root;
1632         struct btrfs_file_extent_item *fi;
1633         struct btrfs_path *path;
1634         struct extent_buffer *leaf;
1635         struct btrfs_key ins;
1636         u64 hint;
1637         int ret;
1638
1639         path = btrfs_alloc_path();
1640         BUG_ON(!path);
1641
1642         path->leave_spinning = 1;
1643
1644         /*
1645          * we may be replacing one extent in the tree with another.
1646          * The new extent is pinned in the extent map, and we don't want
1647          * to drop it from the cache until it is completely in the btree.
1648          *
1649          * So, tell btrfs_drop_extents to leave this extent in the cache.
1650          * the caller is expected to unpin it and allow it to be merged
1651          * with the others.
1652          */
1653         ret = btrfs_drop_extents(trans, inode, file_pos, file_pos + num_bytes,
1654                                  &hint, 0);
1655         BUG_ON(ret);
1656
1657         ins.objectid = btrfs_ino(inode);
1658         ins.offset = file_pos;
1659         ins.type = BTRFS_EXTENT_DATA_KEY;
1660         ret = btrfs_insert_empty_item(trans, root, path, &ins, sizeof(*fi));
1661         BUG_ON(ret);
1662         leaf = path->nodes[0];
1663         fi = btrfs_item_ptr(leaf, path->slots[0],
1664                             struct btrfs_file_extent_item);
1665         btrfs_set_file_extent_generation(leaf, fi, trans->transid);
1666         btrfs_set_file_extent_type(leaf, fi, extent_type);
1667         btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
1668         btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
1669         btrfs_set_file_extent_offset(leaf, fi, 0);
1670         btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
1671         btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
1672         btrfs_set_file_extent_compression(leaf, fi, compression);
1673         btrfs_set_file_extent_encryption(leaf, fi, encryption);
1674         btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
1675
1676         btrfs_unlock_up_safe(path, 1);
1677         btrfs_set_lock_blocking(leaf);
1678
1679         btrfs_mark_buffer_dirty(leaf);
1680
1681         inode_add_bytes(inode, num_bytes);
1682
1683         ins.objectid = disk_bytenr;
1684         ins.offset = disk_num_bytes;
1685         ins.type = BTRFS_EXTENT_ITEM_KEY;
1686         ret = btrfs_alloc_reserved_file_extent(trans, root,
1687                                         root->root_key.objectid,
1688                                         btrfs_ino(inode), file_pos, &ins);
1689         BUG_ON(ret);
1690         btrfs_free_path(path);
1691
1692         return 0;
1693 }
1694
1695 /*
1696  * helper function for btrfs_finish_ordered_io, this
1697  * just reads in some of the csum leaves to prime them into ram
1698  * before we start the transaction.  It limits the amount of btree
1699  * reads required while inside the transaction.
1700  */
1701 /* as ordered data IO finishes, this gets called so we can finish
1702  * an ordered extent if the range of bytes in the file it covers are
1703  * fully written.
1704  */
1705 static int btrfs_finish_ordered_io(struct inode *inode, u64 start, u64 end)
1706 {
1707         struct btrfs_root *root = BTRFS_I(inode)->root;
1708         struct btrfs_trans_handle *trans = NULL;
1709         struct btrfs_ordered_extent *ordered_extent = NULL;
1710         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1711         struct extent_state *cached_state = NULL;
1712         int compress_type = 0;
1713         int ret;
1714         bool nolock;
1715
1716         ret = btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
1717                                              end - start + 1);
1718         if (!ret)
1719                 return 0;
1720         BUG_ON(!ordered_extent);
1721
1722         nolock = is_free_space_inode(root, inode);
1723
1724         if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
1725                 BUG_ON(!list_empty(&ordered_extent->list));
1726                 ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1727                 if (!ret) {
1728                         if (nolock)
1729                                 trans = btrfs_join_transaction_nolock(root, 1);
1730                         else
1731                                 trans = btrfs_join_transaction(root, 1);
1732                         BUG_ON(IS_ERR(trans));
1733                         btrfs_set_trans_block_group(trans, inode);
1734                         trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1735                         ret = btrfs_update_inode(trans, root, inode);
1736                         BUG_ON(ret);
1737                 }
1738                 goto out;
1739         }
1740
1741         lock_extent_bits(io_tree, ordered_extent->file_offset,
1742                          ordered_extent->file_offset + ordered_extent->len - 1,
1743                          0, &cached_state, GFP_NOFS);
1744
1745         if (nolock)
1746                 trans = btrfs_join_transaction_nolock(root, 1);
1747         else
1748                 trans = btrfs_join_transaction(root, 1);
1749         BUG_ON(IS_ERR(trans));
1750         btrfs_set_trans_block_group(trans, inode);
1751         trans->block_rsv = &root->fs_info->delalloc_block_rsv;
1752
1753         if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
1754                 compress_type = ordered_extent->compress_type;
1755         if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
1756                 BUG_ON(compress_type);
1757                 ret = btrfs_mark_extent_written(trans, inode,
1758                                                 ordered_extent->file_offset,
1759                                                 ordered_extent->file_offset +
1760                                                 ordered_extent->len);
1761                 BUG_ON(ret);
1762         } else {
1763                 BUG_ON(root == root->fs_info->tree_root);
1764                 ret = insert_reserved_file_extent(trans, inode,
1765                                                 ordered_extent->file_offset,
1766                                                 ordered_extent->start,
1767                                                 ordered_extent->disk_len,
1768                                                 ordered_extent->len,
1769                                                 ordered_extent->len,
1770                                                 compress_type, 0, 0,
1771                                                 BTRFS_FILE_EXTENT_REG);
1772                 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
1773                                    ordered_extent->file_offset,
1774                                    ordered_extent->len);
1775                 BUG_ON(ret);
1776         }
1777         unlock_extent_cached(io_tree, ordered_extent->file_offset,
1778                              ordered_extent->file_offset +
1779                              ordered_extent->len - 1, &cached_state, GFP_NOFS);
1780
1781         add_pending_csums(trans, inode, ordered_extent->file_offset,
1782                           &ordered_extent->list);
1783
1784         ret = btrfs_ordered_update_i_size(inode, 0, ordered_extent);
1785         if (!ret) {
1786                 ret = btrfs_update_inode(trans, root, inode);
1787                 BUG_ON(ret);
1788         }
1789         ret = 0;
1790 out:
1791         if (nolock) {
1792                 if (trans)
1793                         btrfs_end_transaction_nolock(trans, root);
1794         } else {
1795                 btrfs_delalloc_release_metadata(inode, ordered_extent->len);
1796                 if (trans)
1797                         btrfs_end_transaction(trans, root);
1798         }
1799
1800         /* once for us */
1801         btrfs_put_ordered_extent(ordered_extent);
1802         /* once for the tree */
1803         btrfs_put_ordered_extent(ordered_extent);
1804
1805         return 0;
1806 }
1807
1808 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
1809                                 struct extent_state *state, int uptodate)
1810 {
1811         trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
1812
1813         ClearPagePrivate2(page);
1814         return btrfs_finish_ordered_io(page->mapping->host, start, end);
1815 }
1816
1817 /*
1818  * When IO fails, either with EIO or csum verification fails, we
1819  * try other mirrors that might have a good copy of the data.  This
1820  * io_failure_record is used to record state as we go through all the
1821  * mirrors.  If another mirror has good data, the page is set up to date
1822  * and things continue.  If a good mirror can't be found, the original
1823  * bio end_io callback is called to indicate things have failed.
1824  */
1825 struct io_failure_record {
1826         struct page *page;
1827         u64 start;
1828         u64 len;
1829         u64 logical;
1830         unsigned long bio_flags;
1831         int last_mirror;
1832 };
1833
1834 static int btrfs_io_failed_hook(struct bio *failed_bio,
1835                          struct page *page, u64 start, u64 end,
1836                          struct extent_state *state)
1837 {
1838         struct io_failure_record *failrec = NULL;
1839         u64 private;
1840         struct extent_map *em;
1841         struct inode *inode = page->mapping->host;
1842         struct extent_io_tree *failure_tree = &BTRFS_I(inode)->io_failure_tree;
1843         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
1844         struct bio *bio;
1845         int num_copies;
1846         int ret;
1847         int rw;
1848         u64 logical;
1849
1850         ret = get_state_private(failure_tree, start, &private);
1851         if (ret) {
1852                 failrec = kmalloc(sizeof(*failrec), GFP_NOFS);
1853                 if (!failrec)
1854                         return -ENOMEM;
1855                 failrec->start = start;
1856                 failrec->len = end - start + 1;
1857                 failrec->last_mirror = 0;
1858                 failrec->bio_flags = 0;
1859
1860                 read_lock(&em_tree->lock);
1861                 em = lookup_extent_mapping(em_tree, start, failrec->len);
1862                 if (em->start > start || em->start + em->len < start) {
1863                         free_extent_map(em);
1864                         em = NULL;
1865                 }
1866                 read_unlock(&em_tree->lock);
1867
1868                 if (!em || IS_ERR(em)) {
1869                         kfree(failrec);
1870                         return -EIO;
1871                 }
1872                 logical = start - em->start;
1873                 logical = em->block_start + logical;
1874                 if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
1875                         logical = em->block_start;
1876                         failrec->bio_flags = EXTENT_BIO_COMPRESSED;
1877                         extent_set_compress_type(&failrec->bio_flags,
1878                                                  em->compress_type);
1879                 }
1880                 failrec->logical = logical;
1881                 free_extent_map(em);
1882                 set_extent_bits(failure_tree, start, end, EXTENT_LOCKED |
1883                                 EXTENT_DIRTY, GFP_NOFS);
1884                 set_state_private(failure_tree, start,
1885                                  (u64)(unsigned long)failrec);
1886         } else {
1887                 failrec = (struct io_failure_record *)(unsigned long)private;
1888         }
1889         num_copies = btrfs_num_copies(
1890                               &BTRFS_I(inode)->root->fs_info->mapping_tree,
1891                               failrec->logical, failrec->len);
1892         failrec->last_mirror++;
1893         if (!state) {
1894                 spin_lock(&BTRFS_I(inode)->io_tree.lock);
1895                 state = find_first_extent_bit_state(&BTRFS_I(inode)->io_tree,
1896                                                     failrec->start,
1897                                                     EXTENT_LOCKED);
1898                 if (state && state->start != failrec->start)
1899                         state = NULL;
1900                 spin_unlock(&BTRFS_I(inode)->io_tree.lock);
1901         }
1902         if (!state || failrec->last_mirror > num_copies) {
1903                 set_state_private(failure_tree, failrec->start, 0);
1904                 clear_extent_bits(failure_tree, failrec->start,
1905                                   failrec->start + failrec->len - 1,
1906                                   EXTENT_LOCKED | EXTENT_DIRTY, GFP_NOFS);
1907                 kfree(failrec);
1908                 return -EIO;
1909         }
1910         bio = bio_alloc(GFP_NOFS, 1);
1911         bio->bi_private = state;
1912         bio->bi_end_io = failed_bio->bi_end_io;
1913         bio->bi_sector = failrec->logical >> 9;
1914         bio->bi_bdev = failed_bio->bi_bdev;
1915         bio->bi_size = 0;
1916
1917         bio_add_page(bio, page, failrec->len, start - page_offset(page));
1918         if (failed_bio->bi_rw & REQ_WRITE)
1919                 rw = WRITE;
1920         else
1921                 rw = READ;
1922
1923         ret = BTRFS_I(inode)->io_tree.ops->submit_bio_hook(inode, rw, bio,
1924                                                       failrec->last_mirror,
1925                                                       failrec->bio_flags, 0);
1926         return ret;
1927 }
1928
1929 /*
1930  * each time an IO finishes, we do a fast check in the IO failure tree
1931  * to see if we need to process or clean up an io_failure_record
1932  */
1933 static int btrfs_clean_io_failures(struct inode *inode, u64 start)
1934 {
1935         u64 private;
1936         u64 private_failure;
1937         struct io_failure_record *failure;
1938         int ret;
1939
1940         private = 0;
1941         if (count_range_bits(&BTRFS_I(inode)->io_failure_tree, &private,
1942                              (u64)-1, 1, EXTENT_DIRTY, 0)) {
1943                 ret = get_state_private(&BTRFS_I(inode)->io_failure_tree,
1944                                         start, &private_failure);
1945                 if (ret == 0) {
1946                         failure = (struct io_failure_record *)(unsigned long)
1947                                    private_failure;
1948                         set_state_private(&BTRFS_I(inode)->io_failure_tree,
1949                                           failure->start, 0);
1950                         clear_extent_bits(&BTRFS_I(inode)->io_failure_tree,
1951                                           failure->start,
1952                                           failure->start + failure->len - 1,
1953                                           EXTENT_DIRTY | EXTENT_LOCKED,
1954                                           GFP_NOFS);
1955                         kfree(failure);
1956                 }
1957         }
1958         return 0;
1959 }
1960
1961 /*
1962  * when reads are done, we need to check csums to verify the data is correct
1963  * if there's a match, we allow the bio to finish.  If not, we go through
1964  * the io_failure_record routines to find good copies
1965  */
1966 static int btrfs_readpage_end_io_hook(struct page *page, u64 start, u64 end,
1967                                struct extent_state *state)
1968 {
1969         size_t offset = start - ((u64)page->index << PAGE_CACHE_SHIFT);
1970         struct inode *inode = page->mapping->host;
1971         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
1972         char *kaddr;
1973         u64 private = ~(u32)0;
1974         int ret;
1975         struct btrfs_root *root = BTRFS_I(inode)->root;
1976         u32 csum = ~(u32)0;
1977
1978         if (PageChecked(page)) {
1979                 ClearPageChecked(page);
1980                 goto good;
1981         }
1982
1983         if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
1984                 return 0;
1985
1986         if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
1987             test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
1988                 clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM,
1989                                   GFP_NOFS);
1990                 return 0;
1991         }
1992
1993         if (state && state->start == start) {
1994                 private = state->private;
1995                 ret = 0;
1996         } else {
1997                 ret = get_state_private(io_tree, start, &private);
1998         }
1999         kaddr = kmap_atomic(page, KM_USER0);
2000         if (ret)
2001                 goto zeroit;
2002
2003         csum = btrfs_csum_data(root, kaddr + offset, csum,  end - start + 1);
2004         btrfs_csum_final(csum, (char *)&csum);
2005         if (csum != private)
2006                 goto zeroit;
2007
2008         kunmap_atomic(kaddr, KM_USER0);
2009 good:
2010         /* if the io failure tree for this inode is non-empty,
2011          * check to see if we've recovered from a failed IO
2012          */
2013         btrfs_clean_io_failures(inode, start);
2014         return 0;
2015
2016 zeroit:
2017         if (printk_ratelimit()) {
2018                 printk(KERN_INFO "btrfs csum failed ino %llu off %llu csum %u "
2019                        "private %llu\n",
2020                        (unsigned long long)btrfs_ino(page->mapping->host),
2021                        (unsigned long long)start, csum,
2022                        (unsigned long long)private);
2023         }
2024         memset(kaddr + offset, 1, end - start + 1);
2025         flush_dcache_page(page);
2026         kunmap_atomic(kaddr, KM_USER0);
2027         if (private == 0)
2028                 return 0;
2029         return -EIO;
2030 }
2031
2032 struct delayed_iput {
2033         struct list_head list;
2034         struct inode *inode;
2035 };
2036
2037 void btrfs_add_delayed_iput(struct inode *inode)
2038 {
2039         struct btrfs_fs_info *fs_info = BTRFS_I(inode)->root->fs_info;
2040         struct delayed_iput *delayed;
2041
2042         if (atomic_add_unless(&inode->i_count, -1, 1))
2043                 return;
2044
2045         delayed = kmalloc(sizeof(*delayed), GFP_NOFS | __GFP_NOFAIL);
2046         delayed->inode = inode;
2047
2048         spin_lock(&fs_info->delayed_iput_lock);
2049         list_add_tail(&delayed->list, &fs_info->delayed_iputs);
2050         spin_unlock(&fs_info->delayed_iput_lock);
2051 }
2052
2053 void btrfs_run_delayed_iputs(struct btrfs_root *root)
2054 {
2055         LIST_HEAD(list);
2056         struct btrfs_fs_info *fs_info = root->fs_info;
2057         struct delayed_iput *delayed;
2058         int empty;
2059
2060         spin_lock(&fs_info->delayed_iput_lock);
2061         empty = list_empty(&fs_info->delayed_iputs);
2062         spin_unlock(&fs_info->delayed_iput_lock);
2063         if (empty)
2064                 return;
2065
2066         down_read(&root->fs_info->cleanup_work_sem);
2067         spin_lock(&fs_info->delayed_iput_lock);
2068         list_splice_init(&fs_info->delayed_iputs, &list);
2069         spin_unlock(&fs_info->delayed_iput_lock);
2070
2071         while (!list_empty(&list)) {
2072                 delayed = list_entry(list.next, struct delayed_iput, list);
2073                 list_del(&delayed->list);
2074                 iput(delayed->inode);
2075                 kfree(delayed);
2076         }
2077         up_read(&root->fs_info->cleanup_work_sem);
2078 }
2079
2080 /*
2081  * calculate extra metadata reservation when snapshotting a subvolume
2082  * contains orphan files.
2083  */
2084 void btrfs_orphan_pre_snapshot(struct btrfs_trans_handle *trans,
2085                                 struct btrfs_pending_snapshot *pending,
2086                                 u64 *bytes_to_reserve)
2087 {
2088         struct btrfs_root *root;
2089         struct btrfs_block_rsv *block_rsv;
2090         u64 num_bytes;
2091         int index;
2092
2093         root = pending->root;
2094         if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2095                 return;
2096
2097         block_rsv = root->orphan_block_rsv;
2098
2099         /* orphan block reservation for the snapshot */
2100         num_bytes = block_rsv->size;
2101
2102         /*
2103          * after the snapshot is created, COWing tree blocks may use more
2104          * space than it frees. So we should make sure there is enough
2105          * reserved space.
2106          */
2107         index = trans->transid & 0x1;
2108         if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2109                 num_bytes += block_rsv->size -
2110                              (block_rsv->reserved + block_rsv->freed[index]);
2111         }
2112
2113         *bytes_to_reserve += num_bytes;
2114 }
2115
2116 void btrfs_orphan_post_snapshot(struct btrfs_trans_handle *trans,
2117                                 struct btrfs_pending_snapshot *pending)
2118 {
2119         struct btrfs_root *root = pending->root;
2120         struct btrfs_root *snap = pending->snap;
2121         struct btrfs_block_rsv *block_rsv;
2122         u64 num_bytes;
2123         int index;
2124         int ret;
2125
2126         if (!root->orphan_block_rsv || list_empty(&root->orphan_list))
2127                 return;
2128
2129         /* refill source subvolume's orphan block reservation */
2130         block_rsv = root->orphan_block_rsv;
2131         index = trans->transid & 0x1;
2132         if (block_rsv->reserved + block_rsv->freed[index] < block_rsv->size) {
2133                 num_bytes = block_rsv->size -
2134                             (block_rsv->reserved + block_rsv->freed[index]);
2135                 ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2136                                               root->orphan_block_rsv,
2137                                               num_bytes);
2138                 BUG_ON(ret);
2139         }
2140
2141         /* setup orphan block reservation for the snapshot */
2142         block_rsv = btrfs_alloc_block_rsv(snap);
2143         BUG_ON(!block_rsv);
2144
2145         btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2146         snap->orphan_block_rsv = block_rsv;
2147
2148         num_bytes = root->orphan_block_rsv->size;
2149         ret = btrfs_block_rsv_migrate(&pending->block_rsv,
2150                                       block_rsv, num_bytes);
2151         BUG_ON(ret);
2152
2153 #if 0
2154         /* insert orphan item for the snapshot */
2155         WARN_ON(!root->orphan_item_inserted);
2156         ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2157                                        snap->root_key.objectid);
2158         BUG_ON(ret);
2159         snap->orphan_item_inserted = 1;
2160 #endif
2161 }
2162
2163 enum btrfs_orphan_cleanup_state {
2164         ORPHAN_CLEANUP_STARTED  = 1,
2165         ORPHAN_CLEANUP_DONE     = 2,
2166 };
2167
2168 /*
2169  * This is called in transaction commmit time. If there are no orphan
2170  * files in the subvolume, it removes orphan item and frees block_rsv
2171  * structure.
2172  */
2173 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
2174                               struct btrfs_root *root)
2175 {
2176         int ret;
2177
2178         if (!list_empty(&root->orphan_list) ||
2179             root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
2180                 return;
2181
2182         if (root->orphan_item_inserted &&
2183             btrfs_root_refs(&root->root_item) > 0) {
2184                 ret = btrfs_del_orphan_item(trans, root->fs_info->tree_root,
2185                                             root->root_key.objectid);
2186                 BUG_ON(ret);
2187                 root->orphan_item_inserted = 0;
2188         }
2189
2190         if (root->orphan_block_rsv) {
2191                 WARN_ON(root->orphan_block_rsv->size > 0);
2192                 btrfs_free_block_rsv(root, root->orphan_block_rsv);
2193                 root->orphan_block_rsv = NULL;
2194         }
2195 }
2196
2197 /*
2198  * This creates an orphan entry for the given inode in case something goes
2199  * wrong in the middle of an unlink/truncate.
2200  *
2201  * NOTE: caller of this function should reserve 5 units of metadata for
2202  *       this function.
2203  */
2204 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
2205 {
2206         struct btrfs_root *root = BTRFS_I(inode)->root;
2207         struct btrfs_block_rsv *block_rsv = NULL;
2208         int reserve = 0;
2209         int insert = 0;
2210         int ret;
2211
2212         if (!root->orphan_block_rsv) {
2213                 block_rsv = btrfs_alloc_block_rsv(root);
2214                 BUG_ON(!block_rsv);
2215         }
2216
2217         spin_lock(&root->orphan_lock);
2218         if (!root->orphan_block_rsv) {
2219                 root->orphan_block_rsv = block_rsv;
2220         } else if (block_rsv) {
2221                 btrfs_free_block_rsv(root, block_rsv);
2222                 block_rsv = NULL;
2223         }
2224
2225         if (list_empty(&BTRFS_I(inode)->i_orphan)) {
2226                 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2227 #if 0
2228                 /*
2229                  * For proper ENOSPC handling, we should do orphan
2230                  * cleanup when mounting. But this introduces backward
2231                  * compatibility issue.
2232                  */
2233                 if (!xchg(&root->orphan_item_inserted, 1))
2234                         insert = 2;
2235                 else
2236                         insert = 1;
2237 #endif
2238                 insert = 1;
2239         }
2240
2241         if (!BTRFS_I(inode)->orphan_meta_reserved) {
2242                 BTRFS_I(inode)->orphan_meta_reserved = 1;
2243                 reserve = 1;
2244         }
2245         spin_unlock(&root->orphan_lock);
2246
2247         if (block_rsv)
2248                 btrfs_add_durable_block_rsv(root->fs_info, block_rsv);
2249
2250         /* grab metadata reservation from transaction handle */
2251         if (reserve) {
2252                 ret = btrfs_orphan_reserve_metadata(trans, inode);
2253                 BUG_ON(ret);
2254         }
2255
2256         /* insert an orphan item to track this unlinked/truncated file */
2257         if (insert >= 1) {
2258                 ret = btrfs_insert_orphan_item(trans, root, btrfs_ino(inode));
2259                 BUG_ON(ret);
2260         }
2261
2262         /* insert an orphan item to track subvolume contains orphan files */
2263         if (insert >= 2) {
2264                 ret = btrfs_insert_orphan_item(trans, root->fs_info->tree_root,
2265                                                root->root_key.objectid);
2266                 BUG_ON(ret);
2267         }
2268         return 0;
2269 }
2270
2271 /*
2272  * We have done the truncate/delete so we can go ahead and remove the orphan
2273  * item for this particular inode.
2274  */
2275 int btrfs_orphan_del(struct btrfs_trans_handle *trans, struct inode *inode)
2276 {
2277         struct btrfs_root *root = BTRFS_I(inode)->root;
2278         int delete_item = 0;
2279         int release_rsv = 0;
2280         int ret = 0;
2281
2282         spin_lock(&root->orphan_lock);
2283         if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
2284                 list_del_init(&BTRFS_I(inode)->i_orphan);
2285                 delete_item = 1;
2286         }
2287
2288         if (BTRFS_I(inode)->orphan_meta_reserved) {
2289                 BTRFS_I(inode)->orphan_meta_reserved = 0;
2290                 release_rsv = 1;
2291         }
2292         spin_unlock(&root->orphan_lock);
2293
2294         if (trans && delete_item) {
2295                 ret = btrfs_del_orphan_item(trans, root, btrfs_ino(inode));
2296                 BUG_ON(ret);
2297         }
2298
2299         if (release_rsv)
2300                 btrfs_orphan_release_metadata(inode);
2301
2302         return 0;
2303 }
2304
2305 /*
2306  * this cleans up any orphans that may be left on the list from the last use
2307  * of this root.
2308  */
2309 int btrfs_orphan_cleanup(struct btrfs_root *root)
2310 {
2311         struct btrfs_path *path;
2312         struct extent_buffer *leaf;
2313         struct btrfs_key key, found_key;
2314         struct btrfs_trans_handle *trans;
2315         struct inode *inode;
2316         int ret = 0, nr_unlink = 0, nr_truncate = 0;
2317
2318         if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
2319                 return 0;
2320
2321         path = btrfs_alloc_path();
2322         if (!path) {
2323                 ret = -ENOMEM;
2324                 goto out;
2325         }
2326         path->reada = -1;
2327
2328         key.objectid = BTRFS_ORPHAN_OBJECTID;
2329         btrfs_set_key_type(&key, BTRFS_ORPHAN_ITEM_KEY);
2330         key.offset = (u64)-1;
2331
2332         while (1) {
2333                 ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2334                 if (ret < 0)
2335                         goto out;
2336
2337                 /*
2338                  * if ret == 0 means we found what we were searching for, which
2339                  * is weird, but possible, so only screw with path if we didn't
2340                  * find the key and see if we have stuff that matches
2341                  */
2342                 if (ret > 0) {
2343                         ret = 0;
2344                         if (path->slots[0] == 0)
2345                                 break;
2346                         path->slots[0]--;
2347                 }
2348
2349                 /* pull out the item */
2350                 leaf = path->nodes[0];
2351                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2352
2353                 /* make sure the item matches what we want */
2354                 if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
2355                         break;
2356                 if (btrfs_key_type(&found_key) != BTRFS_ORPHAN_ITEM_KEY)
2357                         break;
2358
2359                 /* release the path since we're done with it */
2360                 btrfs_release_path(root, path);
2361
2362                 /*
2363                  * this is where we are basically btrfs_lookup, without the
2364                  * crossing root thing.  we store the inode number in the
2365                  * offset of the orphan item.
2366                  */
2367                 found_key.objectid = found_key.offset;
2368                 found_key.type = BTRFS_INODE_ITEM_KEY;
2369                 found_key.offset = 0;
2370                 inode = btrfs_iget(root->fs_info->sb, &found_key, root, NULL);
2371                 if (IS_ERR(inode)) {
2372                         ret = PTR_ERR(inode);
2373                         goto out;
2374                 }
2375
2376                 /*
2377                  * add this inode to the orphan list so btrfs_orphan_del does
2378                  * the proper thing when we hit it
2379                  */
2380                 spin_lock(&root->orphan_lock);
2381                 list_add(&BTRFS_I(inode)->i_orphan, &root->orphan_list);
2382                 spin_unlock(&root->orphan_lock);
2383
2384                 /*
2385                  * if this is a bad inode, means we actually succeeded in
2386                  * removing the inode, but not the orphan record, which means
2387                  * we need to manually delete the orphan since iput will just
2388                  * do a destroy_inode
2389                  */
2390                 if (is_bad_inode(inode)) {
2391                         trans = btrfs_start_transaction(root, 0);
2392                         if (IS_ERR(trans)) {
2393                                 ret = PTR_ERR(trans);
2394                                 goto out;
2395                         }
2396                         btrfs_orphan_del(trans, inode);
2397                         btrfs_end_transaction(trans, root);
2398                         iput(inode);
2399                         continue;
2400                 }
2401
2402                 /* if we have links, this was a truncate, lets do that */
2403                 if (inode->i_nlink) {
2404                         if (!S_ISREG(inode->i_mode)) {
2405                                 WARN_ON(1);
2406                                 iput(inode);
2407                                 continue;
2408                         }
2409                         nr_truncate++;
2410                         ret = btrfs_truncate(inode);
2411                 } else {
2412                         nr_unlink++;
2413                 }
2414
2415                 /* this will do delete_inode and everything for us */
2416                 iput(inode);
2417                 if (ret)
2418                         goto out;
2419         }
2420         root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
2421
2422         if (root->orphan_block_rsv)
2423                 btrfs_block_rsv_release(root, root->orphan_block_rsv,
2424                                         (u64)-1);
2425
2426         if (root->orphan_block_rsv || root->orphan_item_inserted) {
2427                 trans = btrfs_join_transaction(root, 1);
2428                 if (!IS_ERR(trans))
2429                         btrfs_end_transaction(trans, root);
2430         }
2431
2432         if (nr_unlink)
2433                 printk(KERN_INFO "btrfs: unlinked %d orphans\n", nr_unlink);
2434         if (nr_truncate)
2435                 printk(KERN_INFO "btrfs: truncated %d orphans\n", nr_truncate);
2436
2437 out:
2438         if (ret)
2439                 printk(KERN_CRIT "btrfs: could not do orphan cleanup %d\n", ret);
2440         btrfs_free_path(path);
2441         return ret;
2442 }
2443
2444 /*
2445  * very simple check to peek ahead in the leaf looking for xattrs.  If we
2446  * don't find any xattrs, we know there can't be any acls.
2447  *
2448  * slot is the slot the inode is in, objectid is the objectid of the inode
2449  */
2450 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
2451                                           int slot, u64 objectid)
2452 {
2453         u32 nritems = btrfs_header_nritems(leaf);
2454         struct btrfs_key found_key;
2455         int scanned = 0;
2456
2457         slot++;
2458         while (slot < nritems) {
2459                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
2460
2461                 /* we found a different objectid, there must not be acls */
2462                 if (found_key.objectid != objectid)
2463                         return 0;
2464
2465                 /* we found an xattr, assume we've got an acl */
2466                 if (found_key.type == BTRFS_XATTR_ITEM_KEY)
2467                         return 1;
2468
2469                 /*
2470                  * we found a key greater than an xattr key, there can't
2471                  * be any acls later on
2472                  */
2473                 if (found_key.type > BTRFS_XATTR_ITEM_KEY)
2474                         return 0;
2475
2476                 slot++;
2477                 scanned++;
2478
2479                 /*
2480                  * it goes inode, inode backrefs, xattrs, extents,
2481                  * so if there are a ton of hard links to an inode there can
2482                  * be a lot of backrefs.  Don't waste time searching too hard,
2483                  * this is just an optimization
2484                  */
2485                 if (scanned >= 8)
2486                         break;
2487         }
2488         /* we hit the end of the leaf before we found an xattr or
2489          * something larger than an xattr.  We have to assume the inode
2490          * has acls
2491          */
2492         return 1;
2493 }
2494
2495 /*
2496  * read an inode from the btree into the in-memory inode
2497  */
2498 static void btrfs_read_locked_inode(struct inode *inode)
2499 {
2500         struct btrfs_path *path;
2501         struct extent_buffer *leaf;
2502         struct btrfs_inode_item *inode_item;
2503         struct btrfs_timespec *tspec;
2504         struct btrfs_root *root = BTRFS_I(inode)->root;
2505         struct btrfs_key location;
2506         int maybe_acls;
2507         u64 alloc_group_block;
2508         u32 rdev;
2509         int ret;
2510
2511         path = btrfs_alloc_path();
2512         BUG_ON(!path);
2513         memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
2514
2515         ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
2516         if (ret)
2517                 goto make_bad;
2518
2519         leaf = path->nodes[0];
2520         inode_item = btrfs_item_ptr(leaf, path->slots[0],
2521                                     struct btrfs_inode_item);
2522
2523         inode->i_mode = btrfs_inode_mode(leaf, inode_item);
2524         inode->i_nlink = btrfs_inode_nlink(leaf, inode_item);
2525         inode->i_uid = btrfs_inode_uid(leaf, inode_item);
2526         inode->i_gid = btrfs_inode_gid(leaf, inode_item);
2527         btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
2528
2529         tspec = btrfs_inode_atime(inode_item);
2530         inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2531         inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2532
2533         tspec = btrfs_inode_mtime(inode_item);
2534         inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2535         inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2536
2537         tspec = btrfs_inode_ctime(inode_item);
2538         inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, tspec);
2539         inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, tspec);
2540
2541         inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
2542         BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
2543         BTRFS_I(inode)->sequence = btrfs_inode_sequence(leaf, inode_item);
2544         inode->i_generation = BTRFS_I(inode)->generation;
2545         inode->i_rdev = 0;
2546         rdev = btrfs_inode_rdev(leaf, inode_item);
2547
2548         BTRFS_I(inode)->index_cnt = (u64)-1;
2549         BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
2550
2551         alloc_group_block = btrfs_inode_block_group(leaf, inode_item);
2552
2553         /*
2554          * try to precache a NULL acl entry for files that don't have
2555          * any xattrs or acls
2556          */
2557         maybe_acls = acls_after_inode_item(leaf, path->slots[0],
2558                                            btrfs_ino(inode));
2559         if (!maybe_acls)
2560                 cache_no_acl(inode);
2561
2562         BTRFS_I(inode)->block_group = btrfs_find_block_group(root, 0,
2563                                                 alloc_group_block, 0);
2564         btrfs_free_path(path);
2565         inode_item = NULL;
2566
2567         switch (inode->i_mode & S_IFMT) {
2568         case S_IFREG:
2569                 inode->i_mapping->a_ops = &btrfs_aops;
2570                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2571                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
2572                 inode->i_fop = &btrfs_file_operations;
2573                 inode->i_op = &btrfs_file_inode_operations;
2574                 break;
2575         case S_IFDIR:
2576                 inode->i_fop = &btrfs_dir_file_operations;
2577                 if (root == root->fs_info->tree_root)
2578                         inode->i_op = &btrfs_dir_ro_inode_operations;
2579                 else
2580                         inode->i_op = &btrfs_dir_inode_operations;
2581                 break;
2582         case S_IFLNK:
2583                 inode->i_op = &btrfs_symlink_inode_operations;
2584                 inode->i_mapping->a_ops = &btrfs_symlink_aops;
2585                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
2586                 break;
2587         default:
2588                 inode->i_op = &btrfs_special_inode_operations;
2589                 init_special_inode(inode, inode->i_mode, rdev);
2590                 break;
2591         }
2592
2593         btrfs_update_iflags(inode);
2594         return;
2595
2596 make_bad:
2597         btrfs_free_path(path);
2598         make_bad_inode(inode);
2599 }
2600
2601 /*
2602  * given a leaf and an inode, copy the inode fields into the leaf
2603  */
2604 static void fill_inode_item(struct btrfs_trans_handle *trans,
2605                             struct extent_buffer *leaf,
2606                             struct btrfs_inode_item *item,
2607                             struct inode *inode)
2608 {
2609         if (!leaf->map_token)
2610                 map_private_extent_buffer(leaf, (unsigned long)item,
2611                                           sizeof(struct btrfs_inode_item),
2612                                           &leaf->map_token, &leaf->kaddr,
2613                                           &leaf->map_start, &leaf->map_len,
2614                                           KM_USER1);
2615
2616         btrfs_set_inode_uid(leaf, item, inode->i_uid);
2617         btrfs_set_inode_gid(leaf, item, inode->i_gid);
2618         btrfs_set_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size);
2619         btrfs_set_inode_mode(leaf, item, inode->i_mode);
2620         btrfs_set_inode_nlink(leaf, item, inode->i_nlink);
2621
2622         btrfs_set_timespec_sec(leaf, btrfs_inode_atime(item),
2623                                inode->i_atime.tv_sec);
2624         btrfs_set_timespec_nsec(leaf, btrfs_inode_atime(item),
2625                                 inode->i_atime.tv_nsec);
2626
2627         btrfs_set_timespec_sec(leaf, btrfs_inode_mtime(item),
2628                                inode->i_mtime.tv_sec);
2629         btrfs_set_timespec_nsec(leaf, btrfs_inode_mtime(item),
2630                                 inode->i_mtime.tv_nsec);
2631
2632         btrfs_set_timespec_sec(leaf, btrfs_inode_ctime(item),
2633                                inode->i_ctime.tv_sec);
2634         btrfs_set_timespec_nsec(leaf, btrfs_inode_ctime(item),
2635                                 inode->i_ctime.tv_nsec);
2636
2637         btrfs_set_inode_nbytes(leaf, item, inode_get_bytes(inode));
2638         btrfs_set_inode_generation(leaf, item, BTRFS_I(inode)->generation);
2639         btrfs_set_inode_sequence(leaf, item, BTRFS_I(inode)->sequence);
2640         btrfs_set_inode_transid(leaf, item, trans->transid);
2641         btrfs_set_inode_rdev(leaf, item, inode->i_rdev);
2642         btrfs_set_inode_flags(leaf, item, BTRFS_I(inode)->flags);
2643         btrfs_set_inode_block_group(leaf, item, BTRFS_I(inode)->block_group);
2644
2645         if (leaf->map_token) {
2646                 unmap_extent_buffer(leaf, leaf->map_token, KM_USER1);
2647                 leaf->map_token = NULL;
2648         }
2649 }
2650
2651 /*
2652  * copy everything in the in-memory inode into the btree.
2653  */
2654 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
2655                                 struct btrfs_root *root, struct inode *inode)
2656 {
2657         struct btrfs_inode_item *inode_item;
2658         struct btrfs_path *path;
2659         struct extent_buffer *leaf;
2660         int ret;
2661
2662         /*
2663          * If root is tree root, it means this inode is used to
2664          * store free space information. And these inodes are updated
2665          * when committing the transaction, so they needn't delaye to
2666          * be updated, or deadlock will occured.
2667          */
2668         if (!is_free_space_inode(root, inode)) {
2669                 ret = btrfs_delayed_update_inode(trans, root, inode);
2670                 if (!ret)
2671                         btrfs_set_inode_last_trans(trans, inode);
2672                 return ret;
2673         }
2674
2675         path = btrfs_alloc_path();
2676         if (!path)
2677                 return -ENOMEM;
2678
2679         path->leave_spinning = 1;
2680         ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
2681                                  1);
2682         if (ret) {
2683                 if (ret > 0)
2684                         ret = -ENOENT;
2685                 goto failed;
2686         }
2687
2688         btrfs_unlock_up_safe(path, 1);
2689         leaf = path->nodes[0];
2690         inode_item = btrfs_item_ptr(leaf, path->slots[0],
2691                                     struct btrfs_inode_item);
2692
2693         fill_inode_item(trans, leaf, inode_item, inode);
2694         btrfs_mark_buffer_dirty(leaf);
2695         btrfs_set_inode_last_trans(trans, inode);
2696         ret = 0;
2697 failed:
2698         btrfs_free_path(path);
2699         return ret;
2700 }
2701
2702 /*
2703  * unlink helper that gets used here in inode.c and in the tree logging
2704  * recovery code.  It remove a link in a directory with a given name, and
2705  * also drops the back refs in the inode to the directory
2706  */
2707 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2708                                 struct btrfs_root *root,
2709                                 struct inode *dir, struct inode *inode,
2710                                 const char *name, int name_len)
2711 {
2712         struct btrfs_path *path;
2713         int ret = 0;
2714         struct extent_buffer *leaf;
2715         struct btrfs_dir_item *di;
2716         struct btrfs_key key;
2717         u64 index;
2718         u64 ino = btrfs_ino(inode);
2719         u64 dir_ino = btrfs_ino(dir);
2720
2721         path = btrfs_alloc_path();
2722         if (!path) {
2723                 ret = -ENOMEM;
2724                 goto out;
2725         }
2726
2727         path->leave_spinning = 1;
2728         di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2729                                     name, name_len, -1);
2730         if (IS_ERR(di)) {
2731                 ret = PTR_ERR(di);
2732                 goto err;
2733         }
2734         if (!di) {
2735                 ret = -ENOENT;
2736                 goto err;
2737         }
2738         leaf = path->nodes[0];
2739         btrfs_dir_item_key_to_cpu(leaf, di, &key);
2740         ret = btrfs_delete_one_dir_name(trans, root, path, di);
2741         if (ret)
2742                 goto err;
2743         btrfs_release_path(root, path);
2744
2745         ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
2746                                   dir_ino, &index);
2747         if (ret) {
2748                 printk(KERN_INFO "btrfs failed to delete reference to %.*s, "
2749                        "inode %llu parent %llu\n", name_len, name,
2750                        (unsigned long long)ino, (unsigned long long)dir_ino);
2751                 goto err;
2752         }
2753
2754         ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
2755         if (ret)
2756                 goto err;
2757
2758         ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len,
2759                                          inode, dir_ino);
2760         BUG_ON(ret != 0 && ret != -ENOENT);
2761
2762         ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len,
2763                                            dir, index);
2764         if (ret == -ENOENT)
2765                 ret = 0;
2766 err:
2767         btrfs_free_path(path);
2768         if (ret)
2769                 goto out;
2770
2771         btrfs_i_size_write(dir, dir->i_size - name_len * 2);
2772         inode->i_ctime = dir->i_mtime = dir->i_ctime = CURRENT_TIME;
2773         btrfs_update_inode(trans, root, dir);
2774 out:
2775         return ret;
2776 }
2777
2778 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
2779                        struct btrfs_root *root,
2780                        struct inode *dir, struct inode *inode,
2781                        const char *name, int name_len)
2782 {
2783         int ret;
2784         ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
2785         if (!ret) {
2786                 btrfs_drop_nlink(inode);
2787                 ret = btrfs_update_inode(trans, root, inode);
2788         }
2789         return ret;
2790 }
2791                 
2792
2793 /* helper to check if there is any shared block in the path */
2794 static int check_path_shared(struct btrfs_root *root,
2795                              struct btrfs_path *path)
2796 {
2797         struct extent_buffer *eb;
2798         int level;
2799         u64 refs = 1;
2800
2801         for (level = 0; level < BTRFS_MAX_LEVEL; level++) {
2802                 int ret;
2803
2804                 if (!path->nodes[level])
2805                         break;
2806                 eb = path->nodes[level];
2807                 if (!btrfs_block_can_be_shared(root, eb))
2808                         continue;
2809                 ret = btrfs_lookup_extent_info(NULL, root, eb->start, eb->len,
2810                                                &refs, NULL);
2811                 if (refs > 1)
2812                         return 1;
2813         }
2814         return 0;
2815 }
2816
2817 /*
2818  * helper to start transaction for unlink and rmdir.
2819  *
2820  * unlink and rmdir are special in btrfs, they do not always free space.
2821  * so in enospc case, we should make sure they will free space before
2822  * allowing them to use the global metadata reservation.
2823  */
2824 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir,
2825                                                        struct dentry *dentry)
2826 {
2827         struct btrfs_trans_handle *trans;
2828         struct btrfs_root *root = BTRFS_I(dir)->root;
2829         struct btrfs_path *path;
2830         struct btrfs_inode_ref *ref;
2831         struct btrfs_dir_item *di;
2832         struct inode *inode = dentry->d_inode;
2833         u64 index;
2834         int check_link = 1;
2835         int err = -ENOSPC;
2836         int ret;
2837         u64 ino = btrfs_ino(inode);
2838         u64 dir_ino = btrfs_ino(dir);
2839
2840         trans = btrfs_start_transaction(root, 10);
2841         if (!IS_ERR(trans) || PTR_ERR(trans) != -ENOSPC)
2842                 return trans;
2843
2844         if (ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
2845                 return ERR_PTR(-ENOSPC);
2846
2847         /* check if there is someone else holds reference */
2848         if (S_ISDIR(inode->i_mode) && atomic_read(&inode->i_count) > 1)
2849                 return ERR_PTR(-ENOSPC);
2850
2851         if (atomic_read(&inode->i_count) > 2)
2852                 return ERR_PTR(-ENOSPC);
2853
2854         if (xchg(&root->fs_info->enospc_unlink, 1))
2855                 return ERR_PTR(-ENOSPC);
2856
2857         path = btrfs_alloc_path();
2858         if (!path) {
2859                 root->fs_info->enospc_unlink = 0;
2860                 return ERR_PTR(-ENOMEM);
2861         }
2862
2863         trans = btrfs_start_transaction(root, 0);
2864         if (IS_ERR(trans)) {
2865                 btrfs_free_path(path);
2866                 root->fs_info->enospc_unlink = 0;
2867                 return trans;
2868         }
2869
2870         path->skip_locking = 1;
2871         path->search_commit_root = 1;
2872
2873         ret = btrfs_lookup_inode(trans, root, path,
2874                                 &BTRFS_I(dir)->location, 0);
2875         if (ret < 0) {
2876                 err = ret;
2877                 goto out;
2878         }
2879         if (ret == 0) {
2880                 if (check_path_shared(root, path))
2881                         goto out;
2882         } else {
2883                 check_link = 0;
2884         }
2885         btrfs_release_path(root, path);
2886
2887         ret = btrfs_lookup_inode(trans, root, path,
2888                                 &BTRFS_I(inode)->location, 0);
2889         if (ret < 0) {
2890                 err = ret;
2891                 goto out;
2892         }
2893         if (ret == 0) {
2894                 if (check_path_shared(root, path))
2895                         goto out;
2896         } else {
2897                 check_link = 0;
2898         }
2899         btrfs_release_path(root, path);
2900
2901         if (ret == 0 && S_ISREG(inode->i_mode)) {
2902                 ret = btrfs_lookup_file_extent(trans, root, path,
2903                                                ino, (u64)-1, 0);
2904                 if (ret < 0) {
2905                         err = ret;
2906                         goto out;
2907                 }
2908                 BUG_ON(ret == 0);
2909                 if (check_path_shared(root, path))
2910                         goto out;
2911                 btrfs_release_path(root, path);
2912         }
2913
2914         if (!check_link) {
2915                 err = 0;
2916                 goto out;
2917         }
2918
2919         di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
2920                                 dentry->d_name.name, dentry->d_name.len, 0);
2921         if (IS_ERR(di)) {
2922                 err = PTR_ERR(di);
2923                 goto out;
2924         }
2925         if (di) {
2926                 if (check_path_shared(root, path))
2927                         goto out;
2928         } else {
2929                 err = 0;
2930                 goto out;
2931         }
2932         btrfs_release_path(root, path);
2933
2934         ref = btrfs_lookup_inode_ref(trans, root, path,
2935                                 dentry->d_name.name, dentry->d_name.len,
2936                                 ino, dir_ino, 0);
2937         if (IS_ERR(ref)) {
2938                 err = PTR_ERR(ref);
2939                 goto out;
2940         }
2941         BUG_ON(!ref);
2942         if (check_path_shared(root, path))
2943                 goto out;
2944         index = btrfs_inode_ref_index(path->nodes[0], ref);
2945         btrfs_release_path(root, path);
2946
2947         /*
2948          * This is a commit root search, if we can lookup inode item and other
2949          * relative items in the commit root, it means the transaction of
2950          * dir/file creation has been committed, and the dir index item that we
2951          * delay to insert has also been inserted into the commit root. So
2952          * we needn't worry about the delayed insertion of the dir index item
2953          * here.
2954          */
2955         di = btrfs_lookup_dir_index_item(trans, root, path, dir_ino, index,
2956                                 dentry->d_name.name, dentry->d_name.len, 0);
2957         if (IS_ERR(di)) {
2958                 err = PTR_ERR(di);
2959                 goto out;
2960         }
2961         BUG_ON(ret == -ENOENT);
2962         if (check_path_shared(root, path))
2963                 goto out;
2964
2965         err = 0;
2966 out:
2967         btrfs_free_path(path);
2968         if (err) {
2969                 btrfs_end_transaction(trans, root);
2970                 root->fs_info->enospc_unlink = 0;
2971                 return ERR_PTR(err);
2972         }
2973
2974         trans->block_rsv = &root->fs_info->global_block_rsv;
2975         return trans;
2976 }
2977
2978 static void __unlink_end_trans(struct btrfs_trans_handle *trans,
2979                                struct btrfs_root *root)
2980 {
2981         if (trans->block_rsv == &root->fs_info->global_block_rsv) {
2982                 BUG_ON(!root->fs_info->enospc_unlink);
2983                 root->fs_info->enospc_unlink = 0;
2984         }
2985         btrfs_end_transaction_throttle(trans, root);
2986 }
2987
2988 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
2989 {
2990         struct btrfs_root *root = BTRFS_I(dir)->root;
2991         struct btrfs_trans_handle *trans;
2992         struct inode *inode = dentry->d_inode;
2993         int ret;
2994         unsigned long nr = 0;
2995
2996         trans = __unlink_start_trans(dir, dentry);
2997         if (IS_ERR(trans))
2998                 return PTR_ERR(trans);
2999
3000         btrfs_set_trans_block_group(trans, dir);
3001
3002         btrfs_record_unlink_dir(trans, dir, dentry->d_inode, 0);
3003
3004         ret = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3005                                  dentry->d_name.name, dentry->d_name.len);
3006         BUG_ON(ret);
3007
3008         if (inode->i_nlink == 0) {
3009                 ret = btrfs_orphan_add(trans, inode);
3010                 BUG_ON(ret);
3011         }
3012
3013         nr = trans->blocks_used;
3014         __unlink_end_trans(trans, root);
3015         btrfs_btree_balance_dirty(root, nr);
3016         return ret;
3017 }
3018
3019 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
3020                         struct btrfs_root *root,
3021                         struct inode *dir, u64 objectid,
3022                         const char *name, int name_len)
3023 {
3024         struct btrfs_path *path;
3025         struct extent_buffer *leaf;
3026         struct btrfs_dir_item *di;
3027         struct btrfs_key key;
3028         u64 index;
3029         int ret;
3030         u64 dir_ino = btrfs_ino(dir);
3031
3032         path = btrfs_alloc_path();
3033         if (!path)
3034                 return -ENOMEM;
3035
3036         di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3037                                    name, name_len, -1);
3038         BUG_ON(!di || IS_ERR(di));
3039
3040         leaf = path->nodes[0];
3041         btrfs_dir_item_key_to_cpu(leaf, di, &key);
3042         WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
3043         ret = btrfs_delete_one_dir_name(trans, root, path, di);
3044         BUG_ON(ret);
3045         btrfs_release_path(root, path);
3046
3047         ret = btrfs_del_root_ref(trans, root->fs_info->tree_root,
3048                                  objectid, root->root_key.objectid,
3049                                  dir_ino, &index, name, name_len);
3050         if (ret < 0) {
3051                 BUG_ON(ret != -ENOENT);
3052                 di = btrfs_search_dir_index_item(root, path, dir_ino,
3053                                                  name, name_len);
3054                 BUG_ON(!di || IS_ERR(di));
3055
3056                 leaf = path->nodes[0];
3057                 btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
3058                 btrfs_release_path(root, path);
3059                 index = key.offset;
3060         }
3061         btrfs_release_path(root, path);
3062
3063         ret = btrfs_delete_delayed_dir_index(trans, root, dir, index);
3064         BUG_ON(ret);
3065
3066         btrfs_i_size_write(dir, dir->i_size - name_len * 2);
3067         dir->i_mtime = dir->i_ctime = CURRENT_TIME;
3068         ret = btrfs_update_inode(trans, root, dir);
3069         BUG_ON(ret);
3070
3071         return 0;
3072 }
3073
3074 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
3075 {
3076         struct inode *inode = dentry->d_inode;
3077         int err = 0;
3078         struct btrfs_root *root = BTRFS_I(dir)->root;
3079         struct btrfs_trans_handle *trans;
3080         unsigned long nr = 0;
3081
3082         if (inode->i_size > BTRFS_EMPTY_DIR_SIZE ||
3083             btrfs_ino(inode) == BTRFS_FIRST_FREE_OBJECTID)
3084                 return -ENOTEMPTY;
3085
3086         trans = __unlink_start_trans(dir, dentry);
3087         if (IS_ERR(trans))
3088                 return PTR_ERR(trans);
3089
3090         btrfs_set_trans_block_group(trans, dir);
3091
3092         if (unlikely(btrfs_ino(inode) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
3093                 err = btrfs_unlink_subvol(trans, root, dir,
3094                                           BTRFS_I(inode)->location.objectid,
3095                                           dentry->d_name.name,
3096                                           dentry->d_name.len);
3097                 goto out;
3098         }
3099
3100         err = btrfs_orphan_add(trans, inode);
3101         if (err)
3102                 goto out;
3103
3104         /* now the directory is empty */
3105         err = btrfs_unlink_inode(trans, root, dir, dentry->d_inode,
3106                                  dentry->d_name.name, dentry->d_name.len);
3107         if (!err)
3108                 btrfs_i_size_write(inode, 0);
3109 out:
3110         nr = trans->blocks_used;
3111         __unlink_end_trans(trans, root);
3112         btrfs_btree_balance_dirty(root, nr);
3113
3114         return err;
3115 }
3116
3117 #if 0
3118 /*
3119  * when truncating bytes in a file, it is possible to avoid reading
3120  * the leaves that contain only checksum items.  This can be the
3121  * majority of the IO required to delete a large file, but it must
3122  * be done carefully.
3123  *
3124  * The keys in the level just above the leaves are checked to make sure
3125  * the lowest key in a given leaf is a csum key, and starts at an offset
3126  * after the new  size.
3127  *
3128  * Then the key for the next leaf is checked to make sure it also has
3129  * a checksum item for the same file.  If it does, we know our target leaf
3130  * contains only checksum items, and it can be safely freed without reading
3131  * it.
3132  *
3133  * This is just an optimization targeted at large files.  It may do
3134  * nothing.  It will return 0 unless things went badly.
3135  */
3136 static noinline int drop_csum_leaves(struct btrfs_trans_handle *trans,
3137                                      struct btrfs_root *root,
3138                                      struct btrfs_path *path,
3139                                      struct inode *inode, u64 new_size)
3140 {
3141         struct btrfs_key key;
3142         int ret;
3143         int nritems;
3144         struct btrfs_key found_key;
3145         struct btrfs_key other_key;
3146         struct btrfs_leaf_ref *ref;
3147         u64 leaf_gen;
3148         u64 leaf_start;
3149
3150         path->lowest_level = 1;
3151         key.objectid = inode->i_ino;
3152         key.type = BTRFS_CSUM_ITEM_KEY;
3153         key.offset = new_size;
3154 again:
3155         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3156         if (ret < 0)
3157                 goto out;
3158
3159         if (path->nodes[1] == NULL) {
3160                 ret = 0;
3161                 goto out;
3162         }
3163         ret = 0;
3164         btrfs_node_key_to_cpu(path->nodes[1], &found_key, path->slots[1]);
3165         nritems = btrfs_header_nritems(path->nodes[1]);
3166
3167         if (!nritems)
3168                 goto out;
3169
3170         if (path->slots[1] >= nritems)
3171                 goto next_node;
3172
3173         /* did we find a key greater than anything we want to delete? */
3174         if (found_key.objectid > inode->i_ino ||
3175            (found_key.objectid == inode->i_ino && found_key.type > key.type))
3176                 goto out;
3177
3178         /* we check the next key in the node to make sure the leave contains
3179          * only checksum items.  This comparison doesn't work if our
3180          * leaf is the last one in the node
3181          */
3182         if (path->slots[1] + 1 >= nritems) {
3183 next_node:
3184                 /* search forward from the last key in the node, this
3185                  * will bring us into the next node in the tree
3186                  */
3187                 btrfs_node_key_to_cpu(path->nodes[1], &found_key, nritems - 1);
3188
3189                 /* unlikely, but we inc below, so check to be safe */
3190                 if (found_key.offset == (u64)-1)
3191                         goto out;
3192
3193                 /* search_forward needs a path with locks held, do the
3194                  * search again for the original key.  It is possible
3195                  * this will race with a balance and return a path that
3196                  * we could modify, but this drop is just an optimization
3197                  * and is allowed to miss some leaves.
3198                  */
3199                 btrfs_release_path(root, path);
3200                 found_key.offset++;
3201
3202                 /* setup a max key for search_forward */
3203                 other_key.offset = (u64)-1;
3204                 other_key.type = key.type;
3205                 other_key.objectid = key.objectid;
3206
3207                 path->keep_locks = 1;
3208                 ret = btrfs_search_forward(root, &found_key, &other_key,
3209                                            path, 0, 0);
3210                 path->keep_locks = 0;
3211                 if (ret || found_key.objectid != key.objectid ||
3212                     found_key.type != key.type) {
3213                         ret = 0;
3214                         goto out;
3215                 }
3216
3217                 key.offset = found_key.offset;
3218                 btrfs_release_path(root, path);
3219                 cond_resched();
3220                 goto again;
3221         }
3222
3223         /* we know there's one more slot after us in the tree,
3224          * read that key so we can verify it is also a checksum item
3225          */
3226         btrfs_node_key_to_cpu(path->nodes[1], &other_key, path->slots[1] + 1);
3227
3228         if (found_key.objectid < inode->i_ino)
3229                 goto next_key;
3230
3231         if (found_key.type != key.type || found_key.offset < new_size)
3232                 goto next_key;
3233
3234         /*
3235          * if the key for the next leaf isn't a csum key from this objectid,
3236          * we can't be sure there aren't good items inside this leaf.
3237          * Bail out
3238          */
3239         if (other_key.objectid != inode->i_ino || other_key.type != key.type)
3240                 goto out;
3241
3242         leaf_start = btrfs_node_blockptr(path->nodes[1], path->slots[1]);
3243         leaf_gen = btrfs_node_ptr_generation(path->nodes[1], path->slots[1]);
3244         /*
3245          * it is safe to delete this leaf, it contains only
3246          * csum items from this inode at an offset >= new_size
3247          */
3248         ret = btrfs_del_leaf(trans, root, path, leaf_start);
3249         BUG_ON(ret);
3250
3251         if (root->ref_cows && leaf_gen < trans->transid) {
3252                 ref = btrfs_alloc_leaf_ref(root, 0);
3253                 if (ref) {
3254                         ref->root_gen = root->root_key.offset;
3255                         ref->bytenr = leaf_start;
3256                         ref->owner = 0;
3257                         ref->generation = leaf_gen;
3258                         ref->nritems = 0;
3259
3260                         btrfs_sort_leaf_ref(ref);
3261
3262                         ret = btrfs_add_leaf_ref(root, ref, 0);
3263                         WARN_ON(ret);
3264                         btrfs_free_leaf_ref(root, ref);
3265                 } else {
3266                         WARN_ON(1);
3267                 }
3268         }
3269 next_key:
3270         btrfs_release_path(root, path);
3271
3272         if (other_key.objectid == inode->i_ino &&
3273             other_key.type == key.type && other_key.offset > key.offset) {
3274                 key.offset = other_key.offset;
3275                 cond_resched();
3276                 goto again;
3277         }
3278         ret = 0;
3279 out:
3280         /* fixup any changes we've made to the path */
3281         path->lowest_level = 0;
3282         path->keep_locks = 0;
3283         btrfs_release_path(root, path);
3284         return ret;
3285 }
3286
3287 #endif
3288
3289 /*
3290  * this can truncate away extent items, csum items and directory items.
3291  * It starts at a high offset and removes keys until it can't find
3292  * any higher than new_size
3293  *
3294  * csum items that cross the new i_size are truncated to the new size
3295  * as well.
3296  *
3297  * min_type is the minimum key type to truncate down to.  If set to 0, this
3298  * will kill all the items on this inode, including the INODE_ITEM_KEY.
3299  */
3300 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
3301                                struct btrfs_root *root,
3302                                struct inode *inode,
3303                                u64 new_size, u32 min_type)
3304 {
3305         struct btrfs_path *path;
3306         struct extent_buffer *leaf;
3307         struct btrfs_file_extent_item *fi;
3308         struct btrfs_key key;
3309         struct btrfs_key found_key;
3310         u64 extent_start = 0;
3311         u64 extent_num_bytes = 0;
3312         u64 extent_offset = 0;
3313         u64 item_end = 0;
3314         u64 mask = root->sectorsize - 1;
3315         u32 found_type = (u8)-1;
3316         int found_extent;
3317         int del_item;
3318         int pending_del_nr = 0;
3319         int pending_del_slot = 0;
3320         int extent_type = -1;
3321         int encoding;
3322         int ret;
3323         int err = 0;
3324         u64 ino = btrfs_ino(inode);
3325
3326         BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
3327
3328         if (root->ref_cows || root == root->fs_info->tree_root)
3329                 btrfs_drop_extent_cache(inode, new_size & (~mask), (u64)-1, 0);
3330
3331         /*
3332          * This function is also used to drop the items in the log tree before
3333          * we relog the inode, so if root != BTRFS_I(inode)->root, it means
3334          * it is used to drop the loged items. So we shouldn't kill the delayed
3335          * items.
3336          */
3337         if (min_type == 0 && root == BTRFS_I(inode)->root)
3338                 btrfs_kill_delayed_inode_items(inode);
3339
3340         path = btrfs_alloc_path();
3341         BUG_ON(!path);
3342         path->reada = -1;
3343
3344         key.objectid = ino;
3345         key.offset = (u64)-1;
3346         key.type = (u8)-1;
3347
3348 search_again:
3349         path->leave_spinning = 1;
3350         ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
3351         if (ret < 0) {
3352                 err = ret;
3353                 goto out;
3354         }
3355
3356         if (ret > 0) {
3357                 /* there are no items in the tree for us to truncate, we're
3358                  * done
3359                  */
3360                 if (path->slots[0] == 0)
3361                         goto out;
3362                 path->slots[0]--;
3363         }
3364
3365         while (1) {
3366                 fi = NULL;
3367                 leaf = path->nodes[0];
3368                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3369                 found_type = btrfs_key_type(&found_key);
3370                 encoding = 0;
3371
3372                 if (found_key.objectid != ino)
3373                         break;
3374
3375                 if (found_type < min_type)
3376                         break;
3377
3378                 item_end = found_key.offset;
3379                 if (found_type == BTRFS_EXTENT_DATA_KEY) {
3380                         fi = btrfs_item_ptr(leaf, path->slots[0],
3381                                             struct btrfs_file_extent_item);
3382                         extent_type = btrfs_file_extent_type(leaf, fi);
3383                         encoding = btrfs_file_extent_compression(leaf, fi);
3384                         encoding |= btrfs_file_extent_encryption(leaf, fi);
3385                         encoding |= btrfs_file_extent_other_encoding(leaf, fi);
3386
3387                         if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3388                                 item_end +=
3389                                     btrfs_file_extent_num_bytes(leaf, fi);
3390                         } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3391                                 item_end += btrfs_file_extent_inline_len(leaf,
3392                                                                          fi);
3393                         }
3394                         item_end--;
3395                 }
3396                 if (found_type > min_type) {
3397                         del_item = 1;
3398                 } else {
3399                         if (item_end < new_size)
3400                                 break;
3401                         if (found_key.offset >= new_size)
3402                                 del_item = 1;
3403                         else
3404                                 del_item = 0;
3405                 }
3406                 found_extent = 0;
3407                 /* FIXME, shrink the extent if the ref count is only 1 */
3408                 if (found_type != BTRFS_EXTENT_DATA_KEY)
3409                         goto delete;
3410
3411                 if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
3412                         u64 num_dec;
3413                         extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
3414                         if (!del_item && !encoding) {
3415                                 u64 orig_num_bytes =
3416                                         btrfs_file_extent_num_bytes(leaf, fi);
3417                                 extent_num_bytes = new_size -
3418                                         found_key.offset + root->sectorsize - 1;
3419                                 extent_num_bytes = extent_num_bytes &
3420                                         ~((u64)root->sectorsize - 1);
3421                                 btrfs_set_file_extent_num_bytes(leaf, fi,
3422                                                          extent_num_bytes);
3423                                 num_dec = (orig_num_bytes -
3424                                            extent_num_bytes);
3425                                 if (root->ref_cows && extent_start != 0)
3426                                         inode_sub_bytes(inode, num_dec);
3427                                 btrfs_mark_buffer_dirty(leaf);
3428                         } else {
3429                                 extent_num_bytes =
3430                                         btrfs_file_extent_disk_num_bytes(leaf,
3431                                                                          fi);
3432                                 extent_offset = found_key.offset -
3433                                         btrfs_file_extent_offset(leaf, fi);
3434
3435                                 /* FIXME blocksize != 4096 */
3436                                 num_dec = btrfs_file_extent_num_bytes(leaf, fi);
3437                                 if (extent_start != 0) {
3438                                         found_extent = 1;
3439                                         if (root->ref_cows)
3440                                                 inode_sub_bytes(inode, num_dec);
3441                                 }
3442                         }
3443                 } else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
3444                         /*
3445                          * we can't truncate inline items that have had
3446                          * special encodings
3447                          */
3448                         if (!del_item &&
3449                             btrfs_file_extent_compression(leaf, fi) == 0 &&
3450                             btrfs_file_extent_encryption(leaf, fi) == 0 &&
3451                             btrfs_file_extent_other_encoding(leaf, fi) == 0) {
3452                                 u32 size = new_size - found_key.offset;
3453
3454                                 if (root->ref_cows) {
3455                                         inode_sub_bytes(inode, item_end + 1 -
3456                                                         new_size);
3457                                 }
3458                                 size =
3459                                     btrfs_file_extent_calc_inline_size(size);
3460                                 ret = btrfs_truncate_item(trans, root, path,
3461                                                           size, 1);
3462                                 BUG_ON(ret);
3463                         } else if (root->ref_cows) {
3464                                 inode_sub_bytes(inode, item_end + 1 -
3465                                                 found_key.offset);
3466                         }
3467                 }
3468 delete:
3469                 if (del_item) {
3470                         if (!pending_del_nr) {
3471                                 /* no pending yet, add ourselves */
3472                                 pending_del_slot = path->slots[0];
3473                                 pending_del_nr = 1;
3474                         } else if (pending_del_nr &&
3475                                    path->slots[0] + 1 == pending_del_slot) {
3476                                 /* hop on the pending chunk */
3477                                 pending_del_nr++;
3478                                 pending_del_slot = path->slots[0];
3479                         } else {
3480                                 BUG();
3481                         }
3482                 } else {
3483                         break;
3484                 }
3485                 if (found_extent && (root->ref_cows ||
3486                                      root == root->fs_info->tree_root)) {
3487                         btrfs_set_path_blocking(path);
3488                         ret = btrfs_free_extent(trans, root, extent_start,
3489                                                 extent_num_bytes, 0,
3490                                                 btrfs_header_owner(leaf),
3491                                                 ino, extent_offset);
3492                         BUG_ON(ret);
3493                 }
3494
3495                 if (found_type == BTRFS_INODE_ITEM_KEY)
3496                         break;
3497
3498                 if (path->slots[0] == 0 ||
3499                     path->slots[0] != pending_del_slot) {
3500                         if (root->ref_cows &&
3501                             BTRFS_I(inode)->location.objectid !=
3502                                                 BTRFS_FREE_INO_OBJECTID) {
3503                                 err = -EAGAIN;
3504                                 goto out;
3505                         }
3506                         if (pending_del_nr) {
3507                                 ret = btrfs_del_items(trans, root, path,
3508                                                 pending_del_slot,
3509                                                 pending_del_nr);
3510                                 BUG_ON(ret);
3511                                 pending_del_nr = 0;
3512                         }
3513                         btrfs_release_path(root, path);
3514                         goto search_again;
3515                 } else {
3516                         path->slots[0]--;
3517                 }
3518         }
3519 out:
3520         if (pending_del_nr) {
3521                 ret = btrfs_del_items(trans, root, path, pending_del_slot,
3522                                       pending_del_nr);
3523                 BUG_ON(ret);
3524         }
3525         btrfs_free_path(path);
3526         return err;
3527 }
3528
3529 /*
3530  * taken from block_truncate_page, but does cow as it zeros out
3531  * any bytes left in the last page in the file.
3532  */
3533 static int btrfs_truncate_page(struct address_space *mapping, loff_t from)
3534 {
3535         struct inode *inode = mapping->host;
3536         struct btrfs_root *root = BTRFS_I(inode)->root;
3537         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3538         struct btrfs_ordered_extent *ordered;
3539         struct extent_state *cached_state = NULL;
3540         char *kaddr;
3541         u32 blocksize = root->sectorsize;
3542         pgoff_t index = from >> PAGE_CACHE_SHIFT;
3543         unsigned offset = from & (PAGE_CACHE_SIZE-1);
3544         struct page *page;
3545         int ret = 0;
3546         u64 page_start;
3547         u64 page_end;
3548
3549         if ((offset & (blocksize - 1)) == 0)
3550                 goto out;
3551         ret = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
3552         if (ret)
3553                 goto out;
3554
3555         ret = -ENOMEM;
3556 again:
3557         page = grab_cache_page(mapping, index);
3558         if (!page) {
3559                 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3560                 goto out;
3561         }
3562
3563         page_start = page_offset(page);
3564         page_end = page_start + PAGE_CACHE_SIZE - 1;
3565
3566         if (!PageUptodate(page)) {
3567                 ret = btrfs_readpage(NULL, page);
3568                 lock_page(page);
3569                 if (page->mapping != mapping) {
3570                         unlock_page(page);
3571                         page_cache_release(page);
3572                         goto again;
3573                 }
3574                 if (!PageUptodate(page)) {
3575                         ret = -EIO;
3576                         goto out_unlock;
3577                 }
3578         }
3579         wait_on_page_writeback(page);
3580
3581         lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
3582                          GFP_NOFS);
3583         set_page_extent_mapped(page);
3584
3585         ordered = btrfs_lookup_ordered_extent(inode, page_start);
3586         if (ordered) {
3587                 unlock_extent_cached(io_tree, page_start, page_end,
3588                                      &cached_state, GFP_NOFS);
3589                 unlock_page(page);
3590                 page_cache_release(page);
3591                 btrfs_start_ordered_extent(inode, ordered, 1);
3592                 btrfs_put_ordered_extent(ordered);
3593                 goto again;
3594         }
3595
3596         clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
3597                           EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
3598                           0, 0, &cached_state, GFP_NOFS);
3599
3600         ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
3601                                         &cached_state);
3602         if (ret) {
3603                 unlock_extent_cached(io_tree, page_start, page_end,
3604                                      &cached_state, GFP_NOFS);
3605                 goto out_unlock;
3606         }
3607
3608         ret = 0;
3609         if (offset != PAGE_CACHE_SIZE) {
3610                 kaddr = kmap(page);
3611                 memset(kaddr + offset, 0, PAGE_CACHE_SIZE - offset);
3612                 flush_dcache_page(page);
3613                 kunmap(page);
3614         }
3615         ClearPageChecked(page);
3616         set_page_dirty(page);
3617         unlock_extent_cached(io_tree, page_start, page_end, &cached_state,
3618                              GFP_NOFS);
3619
3620 out_unlock:
3621         if (ret)
3622                 btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
3623         unlock_page(page);
3624         page_cache_release(page);
3625 out:
3626         return ret;
3627 }
3628
3629 /*
3630  * This function puts in dummy file extents for the area we're creating a hole
3631  * for.  So if we are truncating this file to a larger size we need to insert
3632  * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
3633  * the range between oldsize and size
3634  */
3635 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
3636 {
3637         struct btrfs_trans_handle *trans;
3638         struct btrfs_root *root = BTRFS_I(inode)->root;
3639         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3640         struct extent_map *em = NULL;
3641         struct extent_state *cached_state = NULL;
3642         u64 mask = root->sectorsize - 1;
3643         u64 hole_start = (oldsize + mask) & ~mask;
3644         u64 block_end = (size + mask) & ~mask;
3645         u64 last_byte;
3646         u64 cur_offset;
3647         u64 hole_size;
3648         int err = 0;
3649
3650         if (size <= hole_start)
3651                 return 0;
3652
3653         while (1) {
3654                 struct btrfs_ordered_extent *ordered;
3655                 btrfs_wait_ordered_range(inode, hole_start,
3656                                          block_end - hole_start);
3657                 lock_extent_bits(io_tree, hole_start, block_end - 1, 0,
3658                                  &cached_state, GFP_NOFS);
3659                 ordered = btrfs_lookup_ordered_extent(inode, hole_start);
3660                 if (!ordered)
3661                         break;
3662                 unlock_extent_cached(io_tree, hole_start, block_end - 1,
3663                                      &cached_state, GFP_NOFS);
3664                 btrfs_put_ordered_extent(ordered);
3665         }
3666
3667         cur_offset = hole_start;
3668         while (1) {
3669                 em = btrfs_get_extent(inode, NULL, 0, cur_offset,
3670                                 block_end - cur_offset, 0);
3671                 BUG_ON(IS_ERR(em) || !em);
3672                 last_byte = min(extent_map_end(em), block_end);
3673                 last_byte = (last_byte + mask) & ~mask;
3674                 if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
3675                         u64 hint_byte = 0;
3676                         hole_size = last_byte - cur_offset;
3677
3678                         trans = btrfs_start_transaction(root, 2);
3679                         if (IS_ERR(trans)) {
3680                                 err = PTR_ERR(trans);
3681                                 break;
3682                         }
3683                         btrfs_set_trans_block_group(trans, inode);
3684
3685                         err = btrfs_drop_extents(trans, inode, cur_offset,
3686                                                  cur_offset + hole_size,
3687                                                  &hint_byte, 1);
3688                         if (err)
3689                                 break;
3690
3691                         err = btrfs_insert_file_extent(trans, root,
3692                                         btrfs_ino(inode), cur_offset, 0,
3693                                         0, hole_size, 0, hole_size,
3694                                         0, 0, 0);
3695                         if (err)
3696                                 break;
3697
3698                         btrfs_drop_extent_cache(inode, hole_start,
3699                                         last_byte - 1, 0);
3700
3701                         btrfs_end_transaction(trans, root);
3702                 }
3703                 free_extent_map(em);
3704                 em = NULL;
3705                 cur_offset = last_byte;
3706                 if (cur_offset >= block_end)
3707                         break;
3708         }
3709
3710         free_extent_map(em);
3711         unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
3712                              GFP_NOFS);
3713         return err;
3714 }
3715
3716 static int btrfs_setsize(struct inode *inode, loff_t newsize)
3717 {
3718         loff_t oldsize = i_size_read(inode);
3719         int ret;
3720
3721         if (newsize == oldsize)
3722                 return 0;
3723
3724         if (newsize > oldsize) {
3725                 i_size_write(inode, newsize);
3726                 btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
3727                 truncate_pagecache(inode, oldsize, newsize);
3728                 ret = btrfs_cont_expand(inode, oldsize, newsize);
3729                 if (ret) {
3730                         btrfs_setsize(inode, oldsize);
3731                         return ret;
3732                 }
3733
3734                 mark_inode_dirty(inode);
3735         } else {
3736
3737                 /*
3738                  * We're truncating a file that used to have good data down to
3739                  * zero. Make sure it gets into the ordered flush list so that
3740                  * any new writes get down to disk quickly.
3741                  */
3742                 if (newsize == 0)
3743                         BTRFS_I(inode)->ordered_data_close = 1;
3744
3745                 /* we don't support swapfiles, so vmtruncate shouldn't fail */
3746                 truncate_setsize(inode, newsize);
3747                 ret = btrfs_truncate(inode);
3748         }
3749
3750         return ret;
3751 }
3752
3753 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
3754 {
3755         struct inode *inode = dentry->d_inode;
3756         struct btrfs_root *root = BTRFS_I(inode)->root;
3757         int err;
3758
3759         if (btrfs_root_readonly(root))
3760                 return -EROFS;
3761
3762         err = inode_change_ok(inode, attr);
3763         if (err)
3764                 return err;
3765
3766         if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
3767                 err = btrfs_setsize(inode, attr->ia_size);
3768                 if (err)
3769                         return err;
3770         }
3771
3772         if (attr->ia_valid) {
3773                 setattr_copy(inode, attr);
3774                 mark_inode_dirty(inode);
3775
3776                 if (attr->ia_valid & ATTR_MODE)
3777                         err = btrfs_acl_chmod(inode);
3778         }
3779
3780         return err;
3781 }
3782
3783 void btrfs_evict_inode(struct inode *inode)
3784 {
3785         struct btrfs_trans_handle *trans;
3786         struct btrfs_root *root = BTRFS_I(inode)->root;
3787         unsigned long nr;
3788         int ret;
3789
3790         trace_btrfs_inode_evict(inode);
3791
3792         truncate_inode_pages(&inode->i_data, 0);
3793         if (inode->i_nlink && (btrfs_root_refs(&root->root_item) != 0 ||
3794                                is_free_space_inode(root, inode)))
3795                 goto no_delete;
3796
3797         if (is_bad_inode(inode)) {
3798                 btrfs_orphan_del(NULL, inode);
3799                 goto no_delete;
3800         }
3801         /* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
3802         btrfs_wait_ordered_range(inode, 0, (u64)-1);
3803
3804         if (root->fs_info->log_root_recovering) {
3805                 BUG_ON(!list_empty(&BTRFS_I(inode)->i_orphan));
3806                 goto no_delete;
3807         }
3808
3809         if (inode->i_nlink > 0) {
3810                 BUG_ON(btrfs_root_refs(&root->root_item) != 0);
3811                 goto no_delete;
3812         }
3813
3814         btrfs_i_size_write(inode, 0);
3815
3816         while (1) {
3817                 trans = btrfs_start_transaction(root, 0);
3818                 BUG_ON(IS_ERR(trans));
3819                 btrfs_set_trans_block_group(trans, inode);
3820                 trans->block_rsv = root->orphan_block_rsv;
3821
3822                 ret = btrfs_block_rsv_check(trans, root,
3823                                             root->orphan_block_rsv, 0, 5);
3824                 if (ret) {
3825                         BUG_ON(ret != -EAGAIN);
3826                         ret = btrfs_commit_transaction(trans, root);
3827                         BUG_ON(ret);
3828                         continue;
3829                 }
3830
3831                 ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
3832                 if (ret != -EAGAIN)
3833                         break;
3834
3835                 nr = trans->blocks_used;
3836                 btrfs_end_transaction(trans, root);
3837                 trans = NULL;
3838                 btrfs_btree_balance_dirty(root, nr);
3839
3840         }
3841
3842         if (ret == 0) {
3843                 ret = btrfs_orphan_del(trans, inode);
3844                 BUG_ON(ret);
3845         }
3846
3847         if (!(root == root->fs_info->tree_root ||
3848               root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
3849                 btrfs_return_ino(root, btrfs_ino(inode));
3850
3851         nr = trans->blocks_used;
3852         btrfs_end_transaction(trans, root);
3853         btrfs_btree_balance_dirty(root, nr);
3854 no_delete:
3855         end_writeback(inode);
3856         return;
3857 }
3858
3859 /*
3860  * this returns the key found in the dir entry in the location pointer.
3861  * If no dir entries were found, location->objectid is 0.
3862  */
3863 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
3864                                struct btrfs_key *location)
3865 {
3866         const char *name = dentry->d_name.name;
3867         int namelen = dentry->d_name.len;
3868         struct btrfs_dir_item *di;
3869         struct btrfs_path *path;
3870         struct btrfs_root *root = BTRFS_I(dir)->root;
3871         int ret = 0;
3872
3873         path = btrfs_alloc_path();
3874         BUG_ON(!path);
3875
3876         di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(dir), name,
3877                                     namelen, 0);
3878         if (IS_ERR(di))
3879                 ret = PTR_ERR(di);
3880
3881         if (!di || IS_ERR(di))
3882                 goto out_err;
3883
3884         btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
3885 out:
3886         btrfs_free_path(path);
3887         return ret;
3888 out_err:
3889         location->objectid = 0;
3890         goto out;
3891 }
3892
3893 /*
3894  * when we hit a tree root in a directory, the btrfs part of the inode
3895  * needs to be changed to reflect the root directory of the tree root.  This
3896  * is kind of like crossing a mount point.
3897  */
3898 static int fixup_tree_root_location(struct btrfs_root *root,
3899                                     struct inode *dir,
3900                                     struct dentry *dentry,
3901                                     struct btrfs_key *location,
3902                                     struct btrfs_root **sub_root)
3903 {
3904         struct btrfs_path *path;
3905         struct btrfs_root *new_root;
3906         struct btrfs_root_ref *ref;
3907         struct extent_buffer *leaf;
3908         int ret;
3909         int err = 0;
3910
3911         path = btrfs_alloc_path();
3912         if (!path) {
3913                 err = -ENOMEM;
3914                 goto out;
3915         }
3916
3917         err = -ENOENT;
3918         ret = btrfs_find_root_ref(root->fs_info->tree_root, path,
3919                                   BTRFS_I(dir)->root->root_key.objectid,
3920                                   location->objectid);
3921         if (ret) {
3922                 if (ret < 0)
3923                         err = ret;
3924                 goto out;
3925         }
3926
3927         leaf = path->nodes[0];
3928         ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
3929         if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(dir) ||
3930             btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
3931                 goto out;
3932
3933         ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
3934                                    (unsigned long)(ref + 1),
3935                                    dentry->d_name.len);
3936         if (ret)
3937                 goto out;
3938
3939         btrfs_release_path(root->fs_info->tree_root, path);
3940
3941         new_root = btrfs_read_fs_root_no_name(root->fs_info, location);
3942         if (IS_ERR(new_root)) {
3943                 err = PTR_ERR(new_root);
3944                 goto out;
3945         }
3946
3947         if (btrfs_root_refs(&new_root->root_item) == 0) {
3948                 err = -ENOENT;
3949                 goto out;
3950         }
3951
3952         *sub_root = new_root;
3953         location->objectid = btrfs_root_dirid(&new_root->root_item);
3954         location->type = BTRFS_INODE_ITEM_KEY;
3955         location->offset = 0;
3956         err = 0;
3957 out:
3958         btrfs_free_path(path);
3959         return err;
3960 }
3961
3962 static void inode_tree_add(struct inode *inode)
3963 {
3964         struct btrfs_root *root = BTRFS_I(inode)->root;
3965         struct btrfs_inode *entry;
3966         struct rb_node **p;
3967         struct rb_node *parent;
3968         u64 ino = btrfs_ino(inode);
3969 again:
3970         p = &root->inode_tree.rb_node;
3971         parent = NULL;
3972
3973         if (inode_unhashed(inode))
3974                 return;
3975
3976         spin_lock(&root->inode_lock);
3977         while (*p) {
3978                 parent = *p;
3979                 entry = rb_entry(parent, struct btrfs_inode, rb_node);
3980
3981                 if (ino < btrfs_ino(&entry->vfs_inode))
3982                         p = &parent->rb_left;
3983                 else if (ino > btrfs_ino(&entry->vfs_inode))
3984                         p = &parent->rb_right;
3985                 else {
3986                         WARN_ON(!(entry->vfs_inode.i_state &
3987                                   (I_WILL_FREE | I_FREEING)));
3988                         rb_erase(parent, &root->inode_tree);
3989                         RB_CLEAR_NODE(parent);
3990                         spin_unlock(&root->inode_lock);
3991                         goto again;
3992                 }
3993         }
3994         rb_link_node(&BTRFS_I(inode)->rb_node, parent, p);
3995         rb_insert_color(&BTRFS_I(inode)->rb_node, &root->inode_tree);
3996         spin_unlock(&root->inode_lock);
3997 }
3998
3999 static void inode_tree_del(struct inode *inode)
4000 {
4001         struct btrfs_root *root = BTRFS_I(inode)->root;
4002         int empty = 0;
4003
4004         spin_lock(&root->inode_lock);
4005         if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
4006                 rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
4007                 RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
4008                 empty = RB_EMPTY_ROOT(&root->inode_tree);
4009         }
4010         spin_unlock(&root->inode_lock);
4011
4012         /*
4013          * Free space cache has inodes in the tree root, but the tree root has a
4014          * root_refs of 0, so this could end up dropping the tree root as a
4015          * snapshot, so we need the extra !root->fs_info->tree_root check to
4016          * make sure we don't drop it.
4017          */
4018         if (empty && btrfs_root_refs(&root->root_item) == 0 &&
4019             root != root->fs_info->tree_root) {
4020                 synchronize_srcu(&root->fs_info->subvol_srcu);
4021                 spin_lock(&root->inode_lock);
4022                 empty = RB_EMPTY_ROOT(&root->inode_tree);
4023                 spin_unlock(&root->inode_lock);
4024                 if (empty)
4025                         btrfs_add_dead_root(root);
4026         }
4027 }
4028
4029 int btrfs_invalidate_inodes(struct btrfs_root *root)
4030 {
4031         struct rb_node *node;
4032         struct rb_node *prev;
4033         struct btrfs_inode *entry;
4034         struct inode *inode;
4035         u64 objectid = 0;
4036
4037         WARN_ON(btrfs_root_refs(&root->root_item) != 0);
4038
4039         spin_lock(&root->inode_lock);
4040 again:
4041         node = root->inode_tree.rb_node;
4042         prev = NULL;
4043         while (node) {
4044                 prev = node;
4045                 entry = rb_entry(node, struct btrfs_inode, rb_node);
4046
4047                 if (objectid < btrfs_ino(&entry->vfs_inode))
4048                         node = node->rb_left;
4049                 else if (objectid > btrfs_ino(&entry->vfs_inode))
4050                         node = node->rb_right;
4051                 else
4052                         break;
4053         }
4054         if (!node) {
4055                 while (prev) {
4056                         entry = rb_entry(prev, struct btrfs_inode, rb_node);
4057                         if (objectid <= btrfs_ino(&entry->vfs_inode)) {
4058                                 node = prev;
4059                                 break;
4060                         }
4061                         prev = rb_next(prev);
4062                 }
4063         }
4064         while (node) {
4065                 entry = rb_entry(node, struct btrfs_inode, rb_node);
4066                 objectid = btrfs_ino(&entry->vfs_inode) + 1;
4067                 inode = igrab(&entry->vfs_inode);
4068                 if (inode) {
4069                         spin_unlock(&root->inode_lock);
4070                         if (atomic_read(&inode->i_count) > 1)
4071                                 d_prune_aliases(inode);
4072                         /*
4073                          * btrfs_drop_inode will have it removed from
4074                          * the inode cache when its usage count
4075                          * hits zero.
4076                          */
4077                         iput(inode);
4078                         cond_resched();
4079                         spin_lock(&root->inode_lock);
4080                         goto again;
4081                 }
4082
4083                 if (cond_resched_lock(&root->inode_lock))
4084                         goto again;
4085
4086                 node = rb_next(node);
4087         }
4088         spin_unlock(&root->inode_lock);
4089         return 0;
4090 }
4091
4092 static int btrfs_init_locked_inode(struct inode *inode, void *p)
4093 {
4094         struct btrfs_iget_args *args = p;
4095         inode->i_ino = args->ino;
4096         BTRFS_I(inode)->root = args->root;
4097         btrfs_set_inode_space_info(args->root, inode);
4098         return 0;
4099 }
4100
4101 static int btrfs_find_actor(struct inode *inode, void *opaque)
4102 {
4103         struct btrfs_iget_args *args = opaque;
4104         return args->ino == btrfs_ino(inode) &&
4105                 args->root == BTRFS_I(inode)->root;
4106 }
4107
4108 static struct inode *btrfs_iget_locked(struct super_block *s,
4109                                        u64 objectid,
4110                                        struct btrfs_root *root)
4111 {
4112         struct inode *inode;
4113         struct btrfs_iget_args args;
4114         args.ino = objectid;
4115         args.root = root;
4116
4117         inode = iget5_locked(s, objectid, btrfs_find_actor,
4118                              btrfs_init_locked_inode,
4119                              (void *)&args);
4120         return inode;
4121 }
4122
4123 /* Get an inode object given its location and corresponding root.
4124  * Returns in *is_new if the inode was read from disk
4125  */
4126 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
4127                          struct btrfs_root *root, int *new)
4128 {
4129         struct inode *inode;
4130
4131         inode = btrfs_iget_locked(s, location->objectid, root);
4132         if (!inode)
4133                 return ERR_PTR(-ENOMEM);
4134
4135         if (inode->i_state & I_NEW) {
4136                 BTRFS_I(inode)->root = root;
4137                 memcpy(&BTRFS_I(inode)->location, location, sizeof(*location));
4138                 btrfs_read_locked_inode(inode);
4139                 inode_tree_add(inode);
4140                 unlock_new_inode(inode);
4141                 if (new)
4142                         *new = 1;
4143         }
4144
4145         return inode;
4146 }
4147
4148 static struct inode *new_simple_dir(struct super_block *s,
4149                                     struct btrfs_key *key,
4150                                     struct btrfs_root *root)
4151 {
4152         struct inode *inode = new_inode(s);
4153
4154         if (!inode)
4155                 return ERR_PTR(-ENOMEM);
4156
4157         BTRFS_I(inode)->root = root;
4158         memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
4159         BTRFS_I(inode)->dummy_inode = 1;
4160
4161         inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
4162         inode->i_op = &simple_dir_inode_operations;
4163         inode->i_fop = &simple_dir_operations;
4164         inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
4165         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4166
4167         return inode;
4168 }
4169
4170 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
4171 {
4172         struct inode *inode;
4173         struct btrfs_root *root = BTRFS_I(dir)->root;
4174         struct btrfs_root *sub_root = root;
4175         struct btrfs_key location;
4176         int index;
4177         int ret;
4178
4179         if (dentry->d_name.len > BTRFS_NAME_LEN)
4180                 return ERR_PTR(-ENAMETOOLONG);
4181
4182         ret = btrfs_inode_by_name(dir, dentry, &location);
4183
4184         if (ret < 0)
4185                 return ERR_PTR(ret);
4186
4187         if (location.objectid == 0)
4188                 return NULL;
4189
4190         if (location.type == BTRFS_INODE_ITEM_KEY) {
4191                 inode = btrfs_iget(dir->i_sb, &location, root, NULL);
4192                 return inode;
4193         }
4194
4195         BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
4196
4197         index = srcu_read_lock(&root->fs_info->subvol_srcu);
4198         ret = fixup_tree_root_location(root, dir, dentry,
4199                                        &location, &sub_root);
4200         if (ret < 0) {
4201                 if (ret != -ENOENT)
4202                         inode = ERR_PTR(ret);
4203                 else
4204                         inode = new_simple_dir(dir->i_sb, &location, sub_root);
4205         } else {
4206                 inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
4207         }
4208         srcu_read_unlock(&root->fs_info->subvol_srcu, index);
4209
4210         if (!IS_ERR(inode) && root != sub_root) {
4211                 down_read(&root->fs_info->cleanup_work_sem);
4212                 if (!(inode->i_sb->s_flags & MS_RDONLY))
4213                         ret = btrfs_orphan_cleanup(sub_root);
4214                 up_read(&root->fs_info->cleanup_work_sem);
4215                 if (ret)
4216                         inode = ERR_PTR(ret);
4217         }
4218
4219         return inode;
4220 }
4221
4222 static int btrfs_dentry_delete(const struct dentry *dentry)
4223 {
4224         struct btrfs_root *root;
4225
4226         if (!dentry->d_inode && !IS_ROOT(dentry))
4227                 dentry = dentry->d_parent;
4228
4229         if (dentry->d_inode) {
4230                 root = BTRFS_I(dentry->d_inode)->root;
4231                 if (btrfs_root_refs(&root->root_item) == 0)
4232                         return 1;
4233         }
4234         return 0;
4235 }
4236
4237 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
4238                                    struct nameidata *nd)
4239 {
4240         struct inode *inode;
4241
4242         inode = btrfs_lookup_dentry(dir, dentry);
4243         if (IS_ERR(inode))
4244                 return ERR_CAST(inode);
4245
4246         return d_splice_alias(inode, dentry);
4247 }
4248
4249 unsigned char btrfs_filetype_table[] = {
4250         DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
4251 };
4252
4253 static int btrfs_real_readdir(struct file *filp, void *dirent,
4254                               filldir_t filldir)
4255 {
4256         struct inode *inode = filp->f_dentry->d_inode;
4257         struct btrfs_root *root = BTRFS_I(inode)->root;
4258         struct btrfs_item *item;
4259         struct btrfs_dir_item *di;
4260         struct btrfs_key key;
4261         struct btrfs_key found_key;
4262         struct btrfs_path *path;
4263         struct list_head ins_list;
4264         struct list_head del_list;
4265         int ret;
4266         struct extent_buffer *leaf;
4267         int slot;
4268         unsigned char d_type;
4269         int over = 0;
4270         u32 di_cur;
4271         u32 di_total;
4272         u32 di_len;
4273         int key_type = BTRFS_DIR_INDEX_KEY;
4274         char tmp_name[32];
4275         char *name_ptr;
4276         int name_len;
4277         int is_curr = 0;        /* filp->f_pos points to the current index? */
4278
4279         /* FIXME, use a real flag for deciding about the key type */
4280         if (root->fs_info->tree_root == root)
4281                 key_type = BTRFS_DIR_ITEM_KEY;
4282
4283         /* special case for "." */
4284         if (filp->f_pos == 0) {
4285                 over = filldir(dirent, ".", 1, 1, btrfs_ino(inode), DT_DIR);
4286                 if (over)
4287                         return 0;
4288                 filp->f_pos = 1;
4289         }
4290         /* special case for .., just use the back ref */
4291         if (filp->f_pos == 1) {
4292                 u64 pino = parent_ino(filp->f_path.dentry);
4293                 over = filldir(dirent, "..", 2,
4294                                2, pino, DT_DIR);
4295                 if (over)
4296                         return 0;
4297                 filp->f_pos = 2;
4298         }
4299         path = btrfs_alloc_path();
4300         if (!path)
4301                 return -ENOMEM;
4302         path->reada = 2;
4303
4304         if (key_type == BTRFS_DIR_INDEX_KEY) {
4305                 INIT_LIST_HEAD(&ins_list);
4306                 INIT_LIST_HEAD(&del_list);
4307                 btrfs_get_delayed_items(inode, &ins_list, &del_list);
4308         }
4309
4310         btrfs_set_key_type(&key, key_type);
4311         key.offset = filp->f_pos;
4312         key.objectid = btrfs_ino(inode);
4313
4314         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4315         if (ret < 0)
4316                 goto err;
4317
4318         while (1) {
4319                 leaf = path->nodes[0];
4320                 slot = path->slots[0];
4321                 if (slot >= btrfs_header_nritems(leaf)) {
4322                         ret = btrfs_next_leaf(root, path);
4323                         if (ret < 0)
4324                                 goto err;
4325                         else if (ret > 0)
4326                                 break;
4327                         continue;
4328                 }
4329
4330                 item = btrfs_item_nr(leaf, slot);
4331                 btrfs_item_key_to_cpu(leaf, &found_key, slot);
4332
4333                 if (found_key.objectid != key.objectid)
4334                         break;
4335                 if (btrfs_key_type(&found_key) != key_type)
4336                         break;
4337                 if (found_key.offset < filp->f_pos)
4338                         goto next;
4339                 if (key_type == BTRFS_DIR_INDEX_KEY &&
4340                     btrfs_should_delete_dir_index(&del_list,
4341                                                   found_key.offset))
4342                         goto next;
4343
4344                 filp->f_pos = found_key.offset;
4345                 is_curr = 1;
4346
4347                 di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
4348                 di_cur = 0;
4349                 di_total = btrfs_item_size(leaf, item);
4350
4351                 while (di_cur < di_total) {
4352                         struct btrfs_key location;
4353
4354                         if (verify_dir_item(root, leaf, di))
4355                                 break;
4356
4357                         name_len = btrfs_dir_name_len(leaf, di);
4358                         if (name_len <= sizeof(tmp_name)) {
4359                                 name_ptr = tmp_name;
4360                         } else {
4361                                 name_ptr = kmalloc(name_len, GFP_NOFS);
4362                                 if (!name_ptr) {
4363                                         ret = -ENOMEM;
4364                                         goto err;
4365                                 }
4366                         }
4367                         read_extent_buffer(leaf, name_ptr,
4368                                            (unsigned long)(di + 1), name_len);
4369
4370                         d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
4371                         btrfs_dir_item_key_to_cpu(leaf, di, &location);
4372
4373                         /* is this a reference to our own snapshot? If so
4374                          * skip it
4375                          */
4376                         if (location.type == BTRFS_ROOT_ITEM_KEY &&
4377                             location.objectid == root->root_key.objectid) {
4378                                 over = 0;
4379                                 goto skip;
4380                         }
4381                         over = filldir(dirent, name_ptr, name_len,
4382                                        found_key.offset, location.objectid,
4383                                        d_type);
4384
4385 skip:
4386                         if (name_ptr != tmp_name)
4387                                 kfree(name_ptr);
4388
4389                         if (over)
4390                                 goto nopos;
4391                         di_len = btrfs_dir_name_len(leaf, di) +
4392                                  btrfs_dir_data_len(leaf, di) + sizeof(*di);
4393                         di_cur += di_len;
4394                         di = (struct btrfs_dir_item *)((char *)di + di_len);
4395                 }
4396 next:
4397                 path->slots[0]++;
4398         }
4399
4400         if (key_type == BTRFS_DIR_INDEX_KEY) {
4401                 if (is_curr)
4402                         filp->f_pos++;
4403                 ret = btrfs_readdir_delayed_dir_index(filp, dirent, filldir,
4404                                                       &ins_list);
4405                 if (ret)
4406                         goto nopos;
4407         }
4408
4409         /* Reached end of directory/root. Bump pos past the last item. */
4410         if (key_type == BTRFS_DIR_INDEX_KEY)
4411                 /*
4412                  * 32-bit glibc will use getdents64, but then strtol -
4413                  * so the last number we can serve is this.
4414                  */
4415                 filp->f_pos = 0x7fffffff;
4416         else
4417                 filp->f_pos++;
4418 nopos:
4419         ret = 0;
4420 err:
4421         if (key_type == BTRFS_DIR_INDEX_KEY)
4422                 btrfs_put_delayed_items(&ins_list, &del_list);
4423         btrfs_free_path(path);
4424         return ret;
4425 }
4426
4427 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
4428 {
4429         struct btrfs_root *root = BTRFS_I(inode)->root;
4430         struct btrfs_trans_handle *trans;
4431         int ret = 0;
4432         bool nolock = false;
4433
4434         if (BTRFS_I(inode)->dummy_inode)
4435                 return 0;
4436
4437         smp_mb();
4438         if (root->fs_info->closing && is_free_space_inode(root, inode))
4439                 nolock = true;
4440
4441         if (wbc->sync_mode == WB_SYNC_ALL) {
4442                 if (nolock)
4443                         trans = btrfs_join_transaction_nolock(root, 1);
4444                 else
4445                         trans = btrfs_join_transaction(root, 1);
4446                 if (IS_ERR(trans))
4447                         return PTR_ERR(trans);
4448                 btrfs_set_trans_block_group(trans, inode);
4449                 if (nolock)
4450                         ret = btrfs_end_transaction_nolock(trans, root);
4451                 else
4452                         ret = btrfs_commit_transaction(trans, root);
4453         }
4454         return ret;
4455 }
4456
4457 /*
4458  * This is somewhat expensive, updating the tree every time the
4459  * inode changes.  But, it is most likely to find the inode in cache.
4460  * FIXME, needs more benchmarking...there are no reasons other than performance
4461  * to keep or drop this code.
4462  */
4463 void btrfs_dirty_inode(struct inode *inode)
4464 {
4465         struct btrfs_root *root = BTRFS_I(inode)->root;
4466         struct btrfs_trans_handle *trans;
4467         int ret;
4468
4469         if (BTRFS_I(inode)->dummy_inode)
4470                 return;
4471
4472         trans = btrfs_join_transaction(root, 1);
4473         BUG_ON(IS_ERR(trans));
4474         btrfs_set_trans_block_group(trans, inode);
4475
4476         ret = btrfs_update_inode(trans, root, inode);
4477         if (ret && ret == -ENOSPC) {
4478                 /* whoops, lets try again with the full transaction */
4479                 btrfs_end_transaction(trans, root);
4480                 trans = btrfs_start_transaction(root, 1);
4481                 if (IS_ERR(trans)) {
4482                         if (printk_ratelimit()) {
4483                                 printk(KERN_ERR "btrfs: fail to "
4484                                        "dirty  inode %llu error %ld\n",
4485                                        (unsigned long long)btrfs_ino(inode),
4486                                        PTR_ERR(trans));
4487                         }
4488                         return;
4489                 }
4490                 btrfs_set_trans_block_group(trans, inode);
4491
4492                 ret = btrfs_update_inode(trans, root, inode);
4493                 if (ret) {
4494                         if (printk_ratelimit()) {
4495                                 printk(KERN_ERR "btrfs: fail to "
4496                                        "dirty  inode %llu error %d\n",
4497                                        (unsigned long long)btrfs_ino(inode),
4498                                        ret);
4499                         }
4500                 }
4501         }
4502         btrfs_end_transaction(trans, root);
4503         if (BTRFS_I(inode)->delayed_node)
4504                 btrfs_balance_delayed_items(root);
4505 }
4506
4507 /*
4508  * find the highest existing sequence number in a directory
4509  * and then set the in-memory index_cnt variable to reflect
4510  * free sequence numbers
4511  */
4512 static int btrfs_set_inode_index_count(struct inode *inode)
4513 {
4514         struct btrfs_root *root = BTRFS_I(inode)->root;
4515         struct btrfs_key key, found_key;
4516         struct btrfs_path *path;
4517         struct extent_buffer *leaf;
4518         int ret;
4519
4520         key.objectid = btrfs_ino(inode);
4521         btrfs_set_key_type(&key, BTRFS_DIR_INDEX_KEY);
4522         key.offset = (u64)-1;
4523
4524         path = btrfs_alloc_path();
4525         if (!path)
4526                 return -ENOMEM;
4527
4528         ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
4529         if (ret < 0)
4530                 goto out;
4531         /* FIXME: we should be able to handle this */
4532         if (ret == 0)
4533                 goto out;
4534         ret = 0;
4535
4536         /*
4537          * MAGIC NUMBER EXPLANATION:
4538          * since we search a directory based on f_pos we have to start at 2
4539          * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
4540          * else has to start at 2
4541          */
4542         if (path->slots[0] == 0) {
4543                 BTRFS_I(inode)->index_cnt = 2;
4544                 goto out;
4545         }
4546
4547         path->slots[0]--;
4548
4549         leaf = path->nodes[0];
4550         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4551
4552         if (found_key.objectid != btrfs_ino(inode) ||
4553             btrfs_key_type(&found_key) != BTRFS_DIR_INDEX_KEY) {
4554                 BTRFS_I(inode)->index_cnt = 2;
4555                 goto out;
4556         }
4557
4558         BTRFS_I(inode)->index_cnt = found_key.offset + 1;
4559 out:
4560         btrfs_free_path(path);
4561         return ret;
4562 }
4563
4564 /*
4565  * helper to find a free sequence number in a given directory.  This current
4566  * code is very simple, later versions will do smarter things in the btree
4567  */
4568 int btrfs_set_inode_index(struct inode *dir, u64 *index)
4569 {
4570         int ret = 0;
4571
4572         if (BTRFS_I(dir)->index_cnt == (u64)-1) {
4573                 ret = btrfs_inode_delayed_dir_index_count(dir);
4574                 if (ret) {
4575                         ret = btrfs_set_inode_index_count(dir);
4576                         if (ret)
4577                                 return ret;
4578                 }
4579         }
4580
4581         *index = BTRFS_I(dir)->index_cnt;
4582         BTRFS_I(dir)->index_cnt++;
4583
4584         return ret;
4585 }
4586
4587 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
4588                                      struct btrfs_root *root,
4589                                      struct inode *dir,
4590                                      const char *name, int name_len,
4591                                      u64 ref_objectid, u64 objectid,
4592                                      u64 alloc_hint, int mode, u64 *index)
4593 {
4594         struct inode *inode;
4595         struct btrfs_inode_item *inode_item;
4596         struct btrfs_key *location;
4597         struct btrfs_path *path;
4598         struct btrfs_inode_ref *ref;
4599         struct btrfs_key key[2];
4600         u32 sizes[2];
4601         unsigned long ptr;
4602         int ret;
4603         int owner;
4604
4605         path = btrfs_alloc_path();
4606         BUG_ON(!path);
4607
4608         inode = new_inode(root->fs_info->sb);
4609         if (!inode) {
4610                 btrfs_free_path(path);
4611                 return ERR_PTR(-ENOMEM);
4612         }
4613
4614         /*
4615          * we have to initialize this early, so we can reclaim the inode
4616          * number if we fail afterwards in this function.
4617          */
4618         inode->i_ino = objectid;
4619
4620         if (dir) {
4621                 trace_btrfs_inode_request(dir);
4622
4623                 ret = btrfs_set_inode_index(dir, index);
4624                 if (ret) {
4625                         btrfs_free_path(path);
4626                         iput(inode);
4627                         return ERR_PTR(ret);
4628                 }
4629         }
4630         /*
4631          * index_cnt is ignored for everything but a dir,
4632          * btrfs_get_inode_index_count has an explanation for the magic
4633          * number
4634          */
4635         BTRFS_I(inode)->index_cnt = 2;
4636         BTRFS_I(inode)->root = root;
4637         BTRFS_I(inode)->generation = trans->transid;
4638         inode->i_generation = BTRFS_I(inode)->generation;
4639         btrfs_set_inode_space_info(root, inode);
4640
4641         if (mode & S_IFDIR)
4642                 owner = 0;
4643         else
4644                 owner = 1;
4645         BTRFS_I(inode)->block_group =
4646                         btrfs_find_block_group(root, 0, alloc_hint, owner);
4647
4648         key[0].objectid = objectid;
4649         btrfs_set_key_type(&key[0], BTRFS_INODE_ITEM_KEY);
4650         key[0].offset = 0;
4651
4652         key[1].objectid = objectid;
4653         btrfs_set_key_type(&key[1], BTRFS_INODE_REF_KEY);
4654         key[1].offset = ref_objectid;
4655
4656         sizes[0] = sizeof(struct btrfs_inode_item);
4657         sizes[1] = name_len + sizeof(*ref);
4658
4659         path->leave_spinning = 1;
4660         ret = btrfs_insert_empty_items(trans, root, path, key, sizes, 2);
4661         if (ret != 0)
4662                 goto fail;
4663
4664         inode_init_owner(inode, dir, mode);
4665         inode_set_bytes(inode, 0);
4666         inode->i_mtime = inode->i_atime = inode->i_ctime = CURRENT_TIME;
4667         inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
4668                                   struct btrfs_inode_item);
4669         fill_inode_item(trans, path->nodes[0], inode_item, inode);
4670
4671         ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
4672                              struct btrfs_inode_ref);
4673         btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
4674         btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
4675         ptr = (unsigned long)(ref + 1);
4676         write_extent_buffer(path->nodes[0], name, ptr, name_len);
4677
4678         btrfs_mark_buffer_dirty(path->nodes[0]);
4679         btrfs_free_path(path);
4680
4681         location = &BTRFS_I(inode)->location;
4682         location->objectid = objectid;
4683         location->offset = 0;
4684         btrfs_set_key_type(location, BTRFS_INODE_ITEM_KEY);
4685
4686         btrfs_inherit_iflags(inode, dir);
4687
4688         if ((mode & S_IFREG)) {
4689                 if (btrfs_test_opt(root, NODATASUM))
4690                         BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
4691                 if (btrfs_test_opt(root, NODATACOW) ||
4692                     (BTRFS_I(dir)->flags & BTRFS_INODE_NODATACOW))
4693                         BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW;
4694         }
4695
4696         insert_inode_hash(inode);
4697         inode_tree_add(inode);
4698
4699         trace_btrfs_inode_new(inode);
4700
4701         return inode;
4702 fail:
4703         if (dir)
4704                 BTRFS_I(dir)->index_cnt--;
4705         btrfs_free_path(path);
4706         iput(inode);
4707         return ERR_PTR(ret);
4708 }
4709
4710 static inline u8 btrfs_inode_type(struct inode *inode)
4711 {
4712         return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
4713 }
4714
4715 /*
4716  * utility function to add 'inode' into 'parent_inode' with
4717  * a give name and a given sequence number.
4718  * if 'add_backref' is true, also insert a backref from the
4719  * inode to the parent directory.
4720  */
4721 int btrfs_add_link(struct btrfs_trans_handle *trans,
4722                    struct inode *parent_inode, struct inode *inode,
4723                    const char *name, int name_len, int add_backref, u64 index)
4724 {
4725         int ret = 0;
4726         struct btrfs_key key;
4727         struct btrfs_root *root = BTRFS_I(parent_inode)->root;
4728         u64 ino = btrfs_ino(inode);
4729         u64 parent_ino = btrfs_ino(parent_inode);
4730
4731         if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4732                 memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
4733         } else {
4734                 key.objectid = ino;
4735                 btrfs_set_key_type(&key, BTRFS_INODE_ITEM_KEY);
4736                 key.offset = 0;
4737         }
4738
4739         if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
4740                 ret = btrfs_add_root_ref(trans, root->fs_info->tree_root,
4741                                          key.objectid, root->root_key.objectid,
4742                                          parent_ino, index, name, name_len);
4743         } else if (add_backref) {
4744                 ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
4745                                              parent_ino, index);
4746         }
4747
4748         if (ret == 0) {
4749                 ret = btrfs_insert_dir_item(trans, root, name, name_len,
4750                                             parent_inode, &key,
4751                                             btrfs_inode_type(inode), index);
4752                 BUG_ON(ret);
4753
4754                 btrfs_i_size_write(parent_inode, parent_inode->i_size +
4755                                    name_len * 2);
4756                 parent_inode->i_mtime = parent_inode->i_ctime = CURRENT_TIME;
4757                 ret = btrfs_update_inode(trans, root, parent_inode);
4758         }
4759         return ret;
4760 }
4761
4762 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
4763                             struct inode *dir, struct dentry *dentry,
4764                             struct inode *inode, int backref, u64 index)
4765 {
4766         int err = btrfs_add_link(trans, dir, inode,
4767                                  dentry->d_name.name, dentry->d_name.len,
4768                                  backref, index);
4769         if (!err) {
4770                 d_instantiate(dentry, inode);
4771                 return 0;
4772         }
4773         if (err > 0)
4774                 err = -EEXIST;
4775         return err;
4776 }
4777
4778 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
4779                         int mode, dev_t rdev)
4780 {
4781         struct btrfs_trans_handle *trans;
4782         struct btrfs_root *root = BTRFS_I(dir)->root;
4783         struct inode *inode = NULL;
4784         int err;
4785         int drop_inode = 0;
4786         u64 objectid;
4787         unsigned long nr = 0;
4788         u64 index = 0;
4789
4790         if (!new_valid_dev(rdev))
4791                 return -EINVAL;
4792
4793         /*
4794          * 2 for inode item and ref
4795          * 2 for dir items
4796          * 1 for xattr if selinux is on
4797          */
4798         trans = btrfs_start_transaction(root, 5);
4799         if (IS_ERR(trans))
4800                 return PTR_ERR(trans);
4801
4802         btrfs_set_trans_block_group(trans, dir);
4803
4804         err = btrfs_find_free_ino(root, &objectid);
4805         if (err)
4806                 goto out_unlock;
4807
4808         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4809                                 dentry->d_name.len, btrfs_ino(dir), objectid,
4810                                 BTRFS_I(dir)->block_group, mode, &index);
4811         if (IS_ERR(inode)) {
4812                 err = PTR_ERR(inode);
4813                 goto out_unlock;
4814         }
4815
4816         err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4817         if (err) {
4818                 drop_inode = 1;
4819                 goto out_unlock;
4820         }
4821
4822         btrfs_set_trans_block_group(trans, inode);
4823         err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4824         if (err)
4825                 drop_inode = 1;
4826         else {
4827                 inode->i_op = &btrfs_special_inode_operations;
4828                 init_special_inode(inode, inode->i_mode, rdev);
4829                 btrfs_update_inode(trans, root, inode);
4830         }
4831         btrfs_update_inode_block_group(trans, inode);
4832         btrfs_update_inode_block_group(trans, dir);
4833 out_unlock:
4834         nr = trans->blocks_used;
4835         btrfs_end_transaction_throttle(trans, root);
4836         btrfs_btree_balance_dirty(root, nr);
4837         if (drop_inode) {
4838                 inode_dec_link_count(inode);
4839                 iput(inode);
4840         }
4841         return err;
4842 }
4843
4844 static int btrfs_create(struct inode *dir, struct dentry *dentry,
4845                         int mode, struct nameidata *nd)
4846 {
4847         struct btrfs_trans_handle *trans;
4848         struct btrfs_root *root = BTRFS_I(dir)->root;
4849         struct inode *inode = NULL;
4850         int drop_inode = 0;
4851         int err;
4852         unsigned long nr = 0;
4853         u64 objectid;
4854         u64 index = 0;
4855
4856         /*
4857          * 2 for inode item and ref
4858          * 2 for dir items
4859          * 1 for xattr if selinux is on
4860          */
4861         trans = btrfs_start_transaction(root, 5);
4862         if (IS_ERR(trans))
4863                 return PTR_ERR(trans);
4864
4865         btrfs_set_trans_block_group(trans, dir);
4866
4867         err = btrfs_find_free_ino(root, &objectid);
4868         if (err)
4869                 goto out_unlock;
4870
4871         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4872                                 dentry->d_name.len, btrfs_ino(dir), objectid,
4873                                 BTRFS_I(dir)->block_group, mode, &index);
4874         if (IS_ERR(inode)) {
4875                 err = PTR_ERR(inode);
4876                 goto out_unlock;
4877         }
4878
4879         err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
4880         if (err) {
4881                 drop_inode = 1;
4882                 goto out_unlock;
4883         }
4884
4885         btrfs_set_trans_block_group(trans, inode);
4886         err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
4887         if (err)
4888                 drop_inode = 1;
4889         else {
4890                 inode->i_mapping->a_ops = &btrfs_aops;
4891                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
4892                 inode->i_fop = &btrfs_file_operations;
4893                 inode->i_op = &btrfs_file_inode_operations;
4894                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
4895         }
4896         btrfs_update_inode_block_group(trans, inode);
4897         btrfs_update_inode_block_group(trans, dir);
4898 out_unlock:
4899         nr = trans->blocks_used;
4900         btrfs_end_transaction_throttle(trans, root);
4901         if (drop_inode) {
4902                 inode_dec_link_count(inode);
4903                 iput(inode);
4904         }
4905         btrfs_btree_balance_dirty(root, nr);
4906         return err;
4907 }
4908
4909 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
4910                       struct dentry *dentry)
4911 {
4912         struct btrfs_trans_handle *trans;
4913         struct btrfs_root *root = BTRFS_I(dir)->root;
4914         struct inode *inode = old_dentry->d_inode;
4915         u64 index;
4916         unsigned long nr = 0;
4917         int err;
4918         int drop_inode = 0;
4919
4920         /* do not allow sys_link's with other subvols of the same device */
4921         if (root->objectid != BTRFS_I(inode)->root->objectid)
4922                 return -EXDEV;
4923
4924         if (inode->i_nlink == ~0U)
4925                 return -EMLINK;
4926
4927         err = btrfs_set_inode_index(dir, &index);
4928         if (err)
4929                 goto fail;
4930
4931         /*
4932          * 2 items for inode and inode ref
4933          * 2 items for dir items
4934          * 1 item for parent inode
4935          */
4936         trans = btrfs_start_transaction(root, 5);
4937         if (IS_ERR(trans)) {
4938                 err = PTR_ERR(trans);
4939                 goto fail;
4940         }
4941
4942         btrfs_inc_nlink(inode);
4943         inode->i_ctime = CURRENT_TIME;
4944
4945         btrfs_set_trans_block_group(trans, dir);
4946         ihold(inode);
4947
4948         err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
4949
4950         if (err) {
4951                 drop_inode = 1;
4952         } else {
4953                 struct dentry *parent = dget_parent(dentry);
4954                 btrfs_update_inode_block_group(trans, dir);
4955                 err = btrfs_update_inode(trans, root, inode);
4956                 BUG_ON(err);
4957                 btrfs_log_new_name(trans, inode, NULL, parent);
4958                 dput(parent);
4959         }
4960
4961         nr = trans->blocks_used;
4962         btrfs_end_transaction_throttle(trans, root);
4963 fail:
4964         if (drop_inode) {
4965                 inode_dec_link_count(inode);
4966                 iput(inode);
4967         }
4968         btrfs_btree_balance_dirty(root, nr);
4969         return err;
4970 }
4971
4972 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, int mode)
4973 {
4974         struct inode *inode = NULL;
4975         struct btrfs_trans_handle *trans;
4976         struct btrfs_root *root = BTRFS_I(dir)->root;
4977         int err = 0;
4978         int drop_on_err = 0;
4979         u64 objectid = 0;
4980         u64 index = 0;
4981         unsigned long nr = 1;
4982
4983         /*
4984          * 2 items for inode and ref
4985          * 2 items for dir items
4986          * 1 for xattr if selinux is on
4987          */
4988         trans = btrfs_start_transaction(root, 5);
4989         if (IS_ERR(trans))
4990                 return PTR_ERR(trans);
4991         btrfs_set_trans_block_group(trans, dir);
4992
4993         err = btrfs_find_free_ino(root, &objectid);
4994         if (err)
4995                 goto out_fail;
4996
4997         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
4998                                 dentry->d_name.len, btrfs_ino(dir), objectid,
4999                                 BTRFS_I(dir)->block_group, S_IFDIR | mode,
5000                                 &index);
5001         if (IS_ERR(inode)) {
5002                 err = PTR_ERR(inode);
5003                 goto out_fail;
5004         }
5005
5006         drop_on_err = 1;
5007
5008         err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
5009         if (err)
5010                 goto out_fail;
5011
5012         inode->i_op = &btrfs_dir_inode_operations;
5013         inode->i_fop = &btrfs_dir_file_operations;
5014         btrfs_set_trans_block_group(trans, inode);
5015
5016         btrfs_i_size_write(inode, 0);
5017         err = btrfs_update_inode(trans, root, inode);
5018         if (err)
5019                 goto out_fail;
5020
5021         err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
5022                              dentry->d_name.len, 0, index);
5023         if (err)
5024                 goto out_fail;
5025
5026         d_instantiate(dentry, inode);
5027         drop_on_err = 0;
5028         btrfs_update_inode_block_group(trans, inode);
5029         btrfs_update_inode_block_group(trans, dir);
5030
5031 out_fail:
5032         nr = trans->blocks_used;
5033         btrfs_end_transaction_throttle(trans, root);
5034         if (drop_on_err)
5035                 iput(inode);
5036         btrfs_btree_balance_dirty(root, nr);
5037         return err;
5038 }
5039
5040 /* helper for btfs_get_extent.  Given an existing extent in the tree,
5041  * and an extent that you want to insert, deal with overlap and insert
5042  * the new extent into the tree.
5043  */
5044 static int merge_extent_mapping(struct extent_map_tree *em_tree,
5045                                 struct extent_map *existing,
5046                                 struct extent_map *em,
5047                                 u64 map_start, u64 map_len)
5048 {
5049         u64 start_diff;
5050
5051         BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
5052         start_diff = map_start - em->start;
5053         em->start = map_start;
5054         em->len = map_len;
5055         if (em->block_start < EXTENT_MAP_LAST_BYTE &&
5056             !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
5057                 em->block_start += start_diff;
5058                 em->block_len -= start_diff;
5059         }
5060         return add_extent_mapping(em_tree, em);
5061 }
5062
5063 static noinline int uncompress_inline(struct btrfs_path *path,
5064                                       struct inode *inode, struct page *page,
5065                                       size_t pg_offset, u64 extent_offset,
5066                                       struct btrfs_file_extent_item *item)
5067 {
5068         int ret;
5069         struct extent_buffer *leaf = path->nodes[0];
5070         char *tmp;
5071         size_t max_size;
5072         unsigned long inline_size;
5073         unsigned long ptr;
5074         int compress_type;
5075
5076         WARN_ON(pg_offset != 0);
5077         compress_type = btrfs_file_extent_compression(leaf, item);
5078         max_size = btrfs_file_extent_ram_bytes(leaf, item);
5079         inline_size = btrfs_file_extent_inline_item_len(leaf,
5080                                         btrfs_item_nr(leaf, path->slots[0]));
5081         tmp = kmalloc(inline_size, GFP_NOFS);
5082         if (!tmp)
5083                 return -ENOMEM;
5084         ptr = btrfs_file_extent_inline_start(item);
5085
5086         read_extent_buffer(leaf, tmp, ptr, inline_size);
5087
5088         max_size = min_t(unsigned long, PAGE_CACHE_SIZE, max_size);
5089         ret = btrfs_decompress(compress_type, tmp, page,
5090                                extent_offset, inline_size, max_size);
5091         if (ret) {
5092                 char *kaddr = kmap_atomic(page, KM_USER0);
5093                 unsigned long copy_size = min_t(u64,
5094                                   PAGE_CACHE_SIZE - pg_offset,
5095                                   max_size - extent_offset);
5096                 memset(kaddr + pg_offset, 0, copy_size);
5097                 kunmap_atomic(kaddr, KM_USER0);
5098         }
5099         kfree(tmp);
5100         return 0;
5101 }
5102
5103 /*
5104  * a bit scary, this does extent mapping from logical file offset to the disk.
5105  * the ugly parts come from merging extents from the disk with the in-ram
5106  * representation.  This gets more complex because of the data=ordered code,
5107  * where the in-ram extents might be locked pending data=ordered completion.
5108  *
5109  * This also copies inline extents directly into the page.
5110  */
5111
5112 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
5113                                     size_t pg_offset, u64 start, u64 len,
5114                                     int create)
5115 {
5116         int ret;
5117         int err = 0;
5118         u64 bytenr;
5119         u64 extent_start = 0;
5120         u64 extent_end = 0;
5121         u64 objectid = btrfs_ino(inode);
5122         u32 found_type;
5123         struct btrfs_path *path = NULL;
5124         struct btrfs_root *root = BTRFS_I(inode)->root;
5125         struct btrfs_file_extent_item *item;
5126         struct extent_buffer *leaf;
5127         struct btrfs_key found_key;
5128         struct extent_map *em = NULL;
5129         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5130         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5131         struct btrfs_trans_handle *trans = NULL;
5132         int compress_type;
5133
5134 again:
5135         read_lock(&em_tree->lock);
5136         em = lookup_extent_mapping(em_tree, start, len);
5137         if (em)
5138                 em->bdev = root->fs_info->fs_devices->latest_bdev;
5139         read_unlock(&em_tree->lock);
5140
5141         if (em) {
5142                 if (em->start > start || em->start + em->len <= start)
5143                         free_extent_map(em);
5144                 else if (em->block_start == EXTENT_MAP_INLINE && page)
5145                         free_extent_map(em);
5146                 else
5147                         goto out;
5148         }
5149         em = alloc_extent_map(GFP_NOFS);
5150         if (!em) {
5151                 err = -ENOMEM;
5152                 goto out;
5153         }
5154         em->bdev = root->fs_info->fs_devices->latest_bdev;
5155         em->start = EXTENT_MAP_HOLE;
5156         em->orig_start = EXTENT_MAP_HOLE;
5157         em->len = (u64)-1;
5158         em->block_len = (u64)-1;
5159
5160         if (!path) {
5161                 path = btrfs_alloc_path();
5162                 BUG_ON(!path);
5163         }
5164
5165         ret = btrfs_lookup_file_extent(trans, root, path,
5166                                        objectid, start, trans != NULL);
5167         if (ret < 0) {
5168                 err = ret;
5169                 goto out;
5170         }
5171
5172         if (ret != 0) {
5173                 if (path->slots[0] == 0)
5174                         goto not_found;
5175                 path->slots[0]--;
5176         }
5177
5178         leaf = path->nodes[0];
5179         item = btrfs_item_ptr(leaf, path->slots[0],
5180                               struct btrfs_file_extent_item);
5181         /* are we inside the extent that was found? */
5182         btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5183         found_type = btrfs_key_type(&found_key);
5184         if (found_key.objectid != objectid ||
5185             found_type != BTRFS_EXTENT_DATA_KEY) {
5186                 goto not_found;
5187         }
5188
5189         found_type = btrfs_file_extent_type(leaf, item);
5190         extent_start = found_key.offset;
5191         compress_type = btrfs_file_extent_compression(leaf, item);
5192         if (found_type == BTRFS_FILE_EXTENT_REG ||
5193             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5194                 extent_end = extent_start +
5195                        btrfs_file_extent_num_bytes(leaf, item);
5196         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5197                 size_t size;
5198                 size = btrfs_file_extent_inline_len(leaf, item);
5199                 extent_end = (extent_start + size + root->sectorsize - 1) &
5200                         ~((u64)root->sectorsize - 1);
5201         }
5202
5203         if (start >= extent_end) {
5204                 path->slots[0]++;
5205                 if (path->slots[0] >= btrfs_header_nritems(leaf)) {
5206                         ret = btrfs_next_leaf(root, path);
5207                         if (ret < 0) {
5208                                 err = ret;
5209                                 goto out;
5210                         }
5211                         if (ret > 0)
5212                                 goto not_found;
5213                         leaf = path->nodes[0];
5214                 }
5215                 btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
5216                 if (found_key.objectid != objectid ||
5217                     found_key.type != BTRFS_EXTENT_DATA_KEY)
5218                         goto not_found;
5219                 if (start + len <= found_key.offset)
5220                         goto not_found;
5221                 em->start = start;
5222                 em->len = found_key.offset - start;
5223                 goto not_found_em;
5224         }
5225
5226         if (found_type == BTRFS_FILE_EXTENT_REG ||
5227             found_type == BTRFS_FILE_EXTENT_PREALLOC) {
5228                 em->start = extent_start;
5229                 em->len = extent_end - extent_start;
5230                 em->orig_start = extent_start -
5231                                  btrfs_file_extent_offset(leaf, item);
5232                 bytenr = btrfs_file_extent_disk_bytenr(leaf, item);
5233                 if (bytenr == 0) {
5234                         em->block_start = EXTENT_MAP_HOLE;
5235                         goto insert;
5236                 }
5237                 if (compress_type != BTRFS_COMPRESS_NONE) {
5238                         set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5239                         em->compress_type = compress_type;
5240                         em->block_start = bytenr;
5241                         em->block_len = btrfs_file_extent_disk_num_bytes(leaf,
5242                                                                          item);
5243                 } else {
5244                         bytenr += btrfs_file_extent_offset(leaf, item);
5245                         em->block_start = bytenr;
5246                         em->block_len = em->len;
5247                         if (found_type == BTRFS_FILE_EXTENT_PREALLOC)
5248                                 set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
5249                 }
5250                 goto insert;
5251         } else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
5252                 unsigned long ptr;
5253                 char *map;
5254                 size_t size;
5255                 size_t extent_offset;
5256                 size_t copy_size;
5257
5258                 em->block_start = EXTENT_MAP_INLINE;
5259                 if (!page || create) {
5260                         em->start = extent_start;
5261                         em->len = extent_end - extent_start;
5262                         goto out;
5263                 }
5264
5265                 size = btrfs_file_extent_inline_len(leaf, item);
5266                 extent_offset = page_offset(page) + pg_offset - extent_start;
5267                 copy_size = min_t(u64, PAGE_CACHE_SIZE - pg_offset,
5268                                 size - extent_offset);
5269                 em->start = extent_start + extent_offset;
5270                 em->len = (copy_size + root->sectorsize - 1) &
5271                         ~((u64)root->sectorsize - 1);
5272                 em->orig_start = EXTENT_MAP_INLINE;
5273                 if (compress_type) {
5274                         set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
5275                         em->compress_type = compress_type;
5276                 }
5277                 ptr = btrfs_file_extent_inline_start(item) + extent_offset;
5278                 if (create == 0 && !PageUptodate(page)) {
5279                         if (btrfs_file_extent_compression(leaf, item) !=
5280                             BTRFS_COMPRESS_NONE) {
5281                                 ret = uncompress_inline(path, inode, page,
5282                                                         pg_offset,
5283                                                         extent_offset, item);
5284                                 BUG_ON(ret);
5285                         } else {
5286                                 map = kmap(page);
5287                                 read_extent_buffer(leaf, map + pg_offset, ptr,
5288                                                    copy_size);
5289                                 if (pg_offset + copy_size < PAGE_CACHE_SIZE) {
5290                                         memset(map + pg_offset + copy_size, 0,
5291                                                PAGE_CACHE_SIZE - pg_offset -
5292                                                copy_size);
5293                                 }
5294                                 kunmap(page);
5295                         }
5296                         flush_dcache_page(page);
5297                 } else if (create && PageUptodate(page)) {
5298                         WARN_ON(1);
5299                         if (!trans) {
5300                                 kunmap(page);
5301                                 free_extent_map(em);
5302                                 em = NULL;
5303                                 btrfs_release_path(root, path);
5304                                 trans = btrfs_join_transaction(root, 1);
5305                                 if (IS_ERR(trans))
5306                                         return ERR_CAST(trans);
5307                                 goto again;
5308                         }
5309                         map = kmap(page);
5310                         write_extent_buffer(leaf, map + pg_offset, ptr,
5311                                             copy_size);
5312                         kunmap(page);
5313                         btrfs_mark_buffer_dirty(leaf);
5314                 }
5315                 set_extent_uptodate(io_tree, em->start,
5316                                     extent_map_end(em) - 1, NULL, GFP_NOFS);
5317                 goto insert;
5318         } else {
5319                 printk(KERN_ERR "btrfs unknown found_type %d\n", found_type);
5320                 WARN_ON(1);
5321         }
5322 not_found:
5323         em->start = start;
5324         em->len = len;
5325 not_found_em:
5326         em->block_start = EXTENT_MAP_HOLE;
5327         set_bit(EXTENT_FLAG_VACANCY, &em->flags);
5328 insert:
5329         btrfs_release_path(root, path);
5330         if (em->start > start || extent_map_end(em) <= start) {
5331                 printk(KERN_ERR "Btrfs: bad extent! em: [%llu %llu] passed "
5332                        "[%llu %llu]\n", (unsigned long long)em->start,
5333                        (unsigned long long)em->len,
5334                        (unsigned long long)start,
5335                        (unsigned long long)len);
5336                 err = -EIO;
5337                 goto out;
5338         }
5339
5340         err = 0;
5341         write_lock(&em_tree->lock);
5342         ret = add_extent_mapping(em_tree, em);
5343         /* it is possible that someone inserted the extent into the tree
5344          * while we had the lock dropped.  It is also possible that
5345          * an overlapping map exists in the tree
5346          */
5347         if (ret == -EEXIST) {
5348                 struct extent_map *existing;
5349
5350                 ret = 0;
5351
5352                 existing = lookup_extent_mapping(em_tree, start, len);
5353                 if (existing && (existing->start > start ||
5354                     existing->start + existing->len <= start)) {
5355                         free_extent_map(existing);
5356                         existing = NULL;
5357                 }
5358                 if (!existing) {
5359                         existing = lookup_extent_mapping(em_tree, em->start,
5360                                                          em->len);
5361                         if (existing) {
5362                                 err = merge_extent_mapping(em_tree, existing,
5363                                                            em, start,
5364                                                            root->sectorsize);
5365                                 free_extent_map(existing);
5366                                 if (err) {
5367                                         free_extent_map(em);
5368                                         em = NULL;
5369                                 }
5370                         } else {
5371                                 err = -EIO;
5372                                 free_extent_map(em);
5373                                 em = NULL;
5374                         }
5375                 } else {
5376                         free_extent_map(em);
5377                         em = existing;
5378                         err = 0;
5379                 }
5380         }
5381         write_unlock(&em_tree->lock);
5382 out:
5383
5384         trace_btrfs_get_extent(root, em);
5385
5386         if (path)
5387                 btrfs_free_path(path);
5388         if (trans) {
5389                 ret = btrfs_end_transaction(trans, root);
5390                 if (!err)
5391                         err = ret;
5392         }
5393         if (err) {
5394                 free_extent_map(em);
5395                 return ERR_PTR(err);
5396         }
5397         return em;
5398 }
5399
5400 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
5401                                            size_t pg_offset, u64 start, u64 len,
5402                                            int create)
5403 {
5404         struct extent_map *em;
5405         struct extent_map *hole_em = NULL;
5406         u64 range_start = start;
5407         u64 end;
5408         u64 found;
5409         u64 found_end;
5410         int err = 0;
5411
5412         em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
5413         if (IS_ERR(em))
5414                 return em;
5415         if (em) {
5416                 /*
5417                  * if our em maps to a hole, there might
5418                  * actually be delalloc bytes behind it
5419                  */
5420                 if (em->block_start != EXTENT_MAP_HOLE)
5421                         return em;
5422                 else
5423                         hole_em = em;
5424         }
5425
5426         /* check to see if we've wrapped (len == -1 or similar) */
5427         end = start + len;
5428         if (end < start)
5429                 end = (u64)-1;
5430         else
5431                 end -= 1;
5432
5433         em = NULL;
5434
5435         /* ok, we didn't find anything, lets look for delalloc */
5436         found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
5437                                  end, len, EXTENT_DELALLOC, 1);
5438         found_end = range_start + found;
5439         if (found_end < range_start)
5440                 found_end = (u64)-1;
5441
5442         /*
5443          * we didn't find anything useful, return
5444          * the original results from get_extent()
5445          */
5446         if (range_start > end || found_end <= start) {
5447                 em = hole_em;
5448                 hole_em = NULL;
5449                 goto out;
5450         }
5451
5452         /* adjust the range_start to make sure it doesn't
5453          * go backwards from the start they passed in
5454          */
5455         range_start = max(start,range_start);
5456         found = found_end - range_start;
5457
5458         if (found > 0) {
5459                 u64 hole_start = start;
5460                 u64 hole_len = len;
5461
5462                 em = alloc_extent_map(GFP_NOFS);
5463                 if (!em) {
5464                         err = -ENOMEM;
5465                         goto out;
5466                 }
5467                 /*
5468                  * when btrfs_get_extent can't find anything it
5469                  * returns one huge hole
5470                  *
5471                  * make sure what it found really fits our range, and
5472                  * adjust to make sure it is based on the start from
5473                  * the caller
5474                  */
5475                 if (hole_em) {
5476                         u64 calc_end = extent_map_end(hole_em);
5477
5478                         if (calc_end <= start || (hole_em->start > end)) {
5479                                 free_extent_map(hole_em);
5480                                 hole_em = NULL;
5481                         } else {
5482                                 hole_start = max(hole_em->start, start);
5483                                 hole_len = calc_end - hole_start;
5484                         }
5485                 }
5486                 em->bdev = NULL;
5487                 if (hole_em && range_start > hole_start) {
5488                         /* our hole starts before our delalloc, so we
5489                          * have to return just the parts of the hole
5490                          * that go until  the delalloc starts
5491                          */
5492                         em->len = min(hole_len,
5493                                       range_start - hole_start);
5494                         em->start = hole_start;
5495                         em->orig_start = hole_start;
5496                         /*
5497                          * don't adjust block start at all,
5498                          * it is fixed at EXTENT_MAP_HOLE
5499                          */
5500                         em->block_start = hole_em->block_start;
5501                         em->block_len = hole_len;
5502                 } else {
5503                         em->start = range_start;
5504                         em->len = found;
5505                         em->orig_start = range_start;
5506                         em->block_start = EXTENT_MAP_DELALLOC;
5507                         em->block_len = found;
5508                 }
5509         } else if (hole_em) {
5510                 return hole_em;
5511         }
5512 out:
5513
5514         free_extent_map(hole_em);
5515         if (err) {
5516                 free_extent_map(em);
5517                 return ERR_PTR(err);
5518         }
5519         return em;
5520 }
5521
5522 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
5523                                                   struct extent_map *em,
5524                                                   u64 start, u64 len)
5525 {
5526         struct btrfs_root *root = BTRFS_I(inode)->root;
5527         struct btrfs_trans_handle *trans;
5528         struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
5529         struct btrfs_key ins;
5530         u64 alloc_hint;
5531         int ret;
5532         bool insert = false;
5533
5534         /*
5535          * Ok if the extent map we looked up is a hole and is for the exact
5536          * range we want, there is no reason to allocate a new one, however if
5537          * it is not right then we need to free this one and drop the cache for
5538          * our range.
5539          */
5540         if (em->block_start != EXTENT_MAP_HOLE || em->start != start ||
5541             em->len != len) {
5542                 free_extent_map(em);
5543                 em = NULL;
5544                 insert = true;
5545                 btrfs_drop_extent_cache(inode, start, start + len - 1, 0);
5546         }
5547
5548         trans = btrfs_join_transaction(root, 0);
5549         if (IS_ERR(trans))
5550                 return ERR_CAST(trans);
5551
5552         trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5553
5554         alloc_hint = get_extent_allocation_hint(inode, start, len);
5555         ret = btrfs_reserve_extent(trans, root, len, root->sectorsize, 0,
5556                                    alloc_hint, (u64)-1, &ins, 1);
5557         if (ret) {
5558                 em = ERR_PTR(ret);
5559                 goto out;
5560         }
5561
5562         if (!em) {
5563                 em = alloc_extent_map(GFP_NOFS);
5564                 if (!em) {
5565                         em = ERR_PTR(-ENOMEM);
5566                         goto out;
5567                 }
5568         }
5569
5570         em->start = start;
5571         em->orig_start = em->start;
5572         em->len = ins.offset;
5573
5574         em->block_start = ins.objectid;
5575         em->block_len = ins.offset;
5576         em->bdev = root->fs_info->fs_devices->latest_bdev;
5577
5578         /*
5579          * We need to do this because if we're using the original em we searched
5580          * for, we could have EXTENT_FLAG_VACANCY set, and we don't want that.
5581          */
5582         em->flags = 0;
5583         set_bit(EXTENT_FLAG_PINNED, &em->flags);
5584
5585         while (insert) {
5586                 write_lock(&em_tree->lock);
5587                 ret = add_extent_mapping(em_tree, em);
5588                 write_unlock(&em_tree->lock);
5589                 if (ret != -EEXIST)
5590                         break;
5591                 btrfs_drop_extent_cache(inode, start, start + em->len - 1, 0);
5592         }
5593
5594         ret = btrfs_add_ordered_extent_dio(inode, start, ins.objectid,
5595                                            ins.offset, ins.offset, 0);
5596         if (ret) {
5597                 btrfs_free_reserved_extent(root, ins.objectid, ins.offset);
5598                 em = ERR_PTR(ret);
5599         }
5600 out:
5601         btrfs_end_transaction(trans, root);
5602         return em;
5603 }
5604
5605 /*
5606  * returns 1 when the nocow is safe, < 1 on error, 0 if the
5607  * block must be cow'd
5608  */
5609 static noinline int can_nocow_odirect(struct btrfs_trans_handle *trans,
5610                                       struct inode *inode, u64 offset, u64 len)
5611 {
5612         struct btrfs_path *path;
5613         int ret;
5614         struct extent_buffer *leaf;
5615         struct btrfs_root *root = BTRFS_I(inode)->root;
5616         struct btrfs_file_extent_item *fi;
5617         struct btrfs_key key;
5618         u64 disk_bytenr;
5619         u64 backref_offset;
5620         u64 extent_end;
5621         u64 num_bytes;
5622         int slot;
5623         int found_type;
5624
5625         path = btrfs_alloc_path();
5626         if (!path)
5627                 return -ENOMEM;
5628
5629         ret = btrfs_lookup_file_extent(trans, root, path, btrfs_ino(inode),
5630                                        offset, 0);
5631         if (ret < 0)
5632                 goto out;
5633
5634         slot = path->slots[0];
5635         if (ret == 1) {
5636                 if (slot == 0) {
5637                         /* can't find the item, must cow */
5638                         ret = 0;
5639                         goto out;
5640                 }
5641                 slot--;
5642         }
5643         ret = 0;
5644         leaf = path->nodes[0];
5645         btrfs_item_key_to_cpu(leaf, &key, slot);
5646         if (key.objectid != btrfs_ino(inode) ||
5647             key.type != BTRFS_EXTENT_DATA_KEY) {
5648                 /* not our file or wrong item type, must cow */
5649                 goto out;
5650         }
5651
5652         if (key.offset > offset) {
5653                 /* Wrong offset, must cow */
5654                 goto out;
5655         }
5656
5657         fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
5658         found_type = btrfs_file_extent_type(leaf, fi);
5659         if (found_type != BTRFS_FILE_EXTENT_REG &&
5660             found_type != BTRFS_FILE_EXTENT_PREALLOC) {
5661                 /* not a regular extent, must cow */
5662                 goto out;
5663         }
5664         disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
5665         backref_offset = btrfs_file_extent_offset(leaf, fi);
5666
5667         extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
5668         if (extent_end < offset + len) {
5669                 /* extent doesn't include our full range, must cow */
5670                 goto out;
5671         }
5672
5673         if (btrfs_extent_readonly(root, disk_bytenr))
5674                 goto out;
5675
5676         /*
5677          * look for other files referencing this extent, if we
5678          * find any we must cow
5679          */
5680         if (btrfs_cross_ref_exist(trans, root, btrfs_ino(inode),
5681                                   key.offset - backref_offset, disk_bytenr))
5682                 goto out;
5683
5684         /*
5685          * adjust disk_bytenr and num_bytes to cover just the bytes
5686          * in this extent we are about to write.  If there
5687          * are any csums in that range we have to cow in order
5688          * to keep the csums correct
5689          */
5690         disk_bytenr += backref_offset;
5691         disk_bytenr += offset - key.offset;
5692         num_bytes = min(offset + len, extent_end) - offset;
5693         if (csum_exist_in_range(root, disk_bytenr, num_bytes))
5694                                 goto out;
5695         /*
5696          * all of the above have passed, it is safe to overwrite this extent
5697          * without cow
5698          */
5699         ret = 1;
5700 out:
5701         btrfs_free_path(path);
5702         return ret;
5703 }
5704
5705 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
5706                                    struct buffer_head *bh_result, int create)
5707 {
5708         struct extent_map *em;
5709         struct btrfs_root *root = BTRFS_I(inode)->root;
5710         u64 start = iblock << inode->i_blkbits;
5711         u64 len = bh_result->b_size;
5712         struct btrfs_trans_handle *trans;
5713
5714         em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
5715         if (IS_ERR(em))
5716                 return PTR_ERR(em);
5717
5718         /*
5719          * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
5720          * io.  INLINE is special, and we could probably kludge it in here, but
5721          * it's still buffered so for safety lets just fall back to the generic
5722          * buffered path.
5723          *
5724          * For COMPRESSED we _have_ to read the entire extent in so we can
5725          * decompress it, so there will be buffering required no matter what we
5726          * do, so go ahead and fallback to buffered.
5727          *
5728          * We return -ENOTBLK because thats what makes DIO go ahead and go back
5729          * to buffered IO.  Don't blame me, this is the price we pay for using
5730          * the generic code.
5731          */
5732         if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
5733             em->block_start == EXTENT_MAP_INLINE) {
5734                 free_extent_map(em);
5735                 return -ENOTBLK;
5736         }
5737
5738         /* Just a good old fashioned hole, return */
5739         if (!create && (em->block_start == EXTENT_MAP_HOLE ||
5740                         test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
5741                 free_extent_map(em);
5742                 /* DIO will do one hole at a time, so just unlock a sector */
5743                 unlock_extent(&BTRFS_I(inode)->io_tree, start,
5744                               start + root->sectorsize - 1, GFP_NOFS);
5745                 return 0;
5746         }
5747
5748         /*
5749          * We don't allocate a new extent in the following cases
5750          *
5751          * 1) The inode is marked as NODATACOW.  In this case we'll just use the
5752          * existing extent.
5753          * 2) The extent is marked as PREALLOC.  We're good to go here and can
5754          * just use the extent.
5755          *
5756          */
5757         if (!create) {
5758                 len = em->len - (start - em->start);
5759                 goto map;
5760         }
5761
5762         if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
5763             ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
5764              em->block_start != EXTENT_MAP_HOLE)) {
5765                 int type;
5766                 int ret;
5767                 u64 block_start;
5768
5769                 if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5770                         type = BTRFS_ORDERED_PREALLOC;
5771                 else
5772                         type = BTRFS_ORDERED_NOCOW;
5773                 len = min(len, em->len - (start - em->start));
5774                 block_start = em->block_start + (start - em->start);
5775
5776                 /*
5777                  * we're not going to log anything, but we do need
5778                  * to make sure the current transaction stays open
5779                  * while we look for nocow cross refs
5780                  */
5781                 trans = btrfs_join_transaction(root, 0);
5782                 if (IS_ERR(trans))
5783                         goto must_cow;
5784
5785                 if (can_nocow_odirect(trans, inode, start, len) == 1) {
5786                         ret = btrfs_add_ordered_extent_dio(inode, start,
5787                                            block_start, len, len, type);
5788                         btrfs_end_transaction(trans, root);
5789                         if (ret) {
5790                                 free_extent_map(em);
5791                                 return ret;
5792                         }
5793                         goto unlock;
5794                 }
5795                 btrfs_end_transaction(trans, root);
5796         }
5797 must_cow:
5798         /*
5799          * this will cow the extent, reset the len in case we changed
5800          * it above
5801          */
5802         len = bh_result->b_size;
5803         em = btrfs_new_extent_direct(inode, em, start, len);
5804         if (IS_ERR(em))
5805                 return PTR_ERR(em);
5806         len = min(len, em->len - (start - em->start));
5807 unlock:
5808         clear_extent_bit(&BTRFS_I(inode)->io_tree, start, start + len - 1,
5809                           EXTENT_LOCKED | EXTENT_DELALLOC | EXTENT_DIRTY, 1,
5810                           0, NULL, GFP_NOFS);
5811 map:
5812         bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
5813                 inode->i_blkbits;
5814         bh_result->b_size = len;
5815         bh_result->b_bdev = em->bdev;
5816         set_buffer_mapped(bh_result);
5817         if (create && !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
5818                 set_buffer_new(bh_result);
5819
5820         free_extent_map(em);
5821
5822         return 0;
5823 }
5824
5825 struct btrfs_dio_private {
5826         struct inode *inode;
5827         u64 logical_offset;
5828         u64 disk_bytenr;
5829         u64 bytes;
5830         u32 *csums;
5831         void *private;
5832
5833         /* number of bios pending for this dio */
5834         atomic_t pending_bios;
5835
5836         /* IO errors */
5837         int errors;
5838
5839         struct bio *orig_bio;
5840 };
5841
5842 static void btrfs_endio_direct_read(struct bio *bio, int err)
5843 {
5844         struct btrfs_dio_private *dip = bio->bi_private;
5845         struct bio_vec *bvec_end = bio->bi_io_vec + bio->bi_vcnt - 1;
5846         struct bio_vec *bvec = bio->bi_io_vec;
5847         struct inode *inode = dip->inode;
5848         struct btrfs_root *root = BTRFS_I(inode)->root;
5849         u64 start;
5850         u32 *private = dip->csums;
5851
5852         start = dip->logical_offset;
5853         do {
5854                 if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)) {
5855                         struct page *page = bvec->bv_page;
5856                         char *kaddr;
5857                         u32 csum = ~(u32)0;
5858                         unsigned long flags;
5859
5860                         local_irq_save(flags);
5861                         kaddr = kmap_atomic(page, KM_IRQ0);
5862                         csum = btrfs_csum_data(root, kaddr + bvec->bv_offset,
5863                                                csum, bvec->bv_len);
5864                         btrfs_csum_final(csum, (char *)&csum);
5865                         kunmap_atomic(kaddr, KM_IRQ0);
5866                         local_irq_restore(flags);
5867
5868                         flush_dcache_page(bvec->bv_page);
5869                         if (csum != *private) {
5870                                 printk(KERN_ERR "btrfs csum failed ino %llu off"
5871                                       " %llu csum %u private %u\n",
5872                                       (unsigned long long)btrfs_ino(inode),
5873                                       (unsigned long long)start,
5874                                       csum, *private);
5875                                 err = -EIO;
5876                         }
5877                 }
5878
5879                 start += bvec->bv_len;
5880                 private++;
5881                 bvec++;
5882         } while (bvec <= bvec_end);
5883
5884         unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
5885                       dip->logical_offset + dip->bytes - 1, GFP_NOFS);
5886         bio->bi_private = dip->private;
5887
5888         kfree(dip->csums);
5889         kfree(dip);
5890
5891         /* If we had a csum failure make sure to clear the uptodate flag */
5892         if (err)
5893                 clear_bit(BIO_UPTODATE, &bio->bi_flags);
5894         dio_end_io(bio, err);
5895 }
5896
5897 static void btrfs_endio_direct_write(struct bio *bio, int err)
5898 {
5899         struct btrfs_dio_private *dip = bio->bi_private;
5900         struct inode *inode = dip->inode;
5901         struct btrfs_root *root = BTRFS_I(inode)->root;
5902         struct btrfs_trans_handle *trans;
5903         struct btrfs_ordered_extent *ordered = NULL;
5904         struct extent_state *cached_state = NULL;
5905         u64 ordered_offset = dip->logical_offset;
5906         u64 ordered_bytes = dip->bytes;
5907         int ret;
5908
5909         if (err)
5910                 goto out_done;
5911 again:
5912         ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
5913                                                    &ordered_offset,
5914                                                    ordered_bytes);
5915         if (!ret)
5916                 goto out_test;
5917
5918         BUG_ON(!ordered);
5919
5920         trans = btrfs_join_transaction(root, 1);
5921         if (IS_ERR(trans)) {
5922                 err = -ENOMEM;
5923                 goto out;
5924         }
5925         trans->block_rsv = &root->fs_info->delalloc_block_rsv;
5926
5927         if (test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags)) {
5928                 ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5929                 if (!ret)
5930                         ret = btrfs_update_inode(trans, root, inode);
5931                 err = ret;
5932                 goto out;
5933         }
5934
5935         lock_extent_bits(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5936                          ordered->file_offset + ordered->len - 1, 0,
5937                          &cached_state, GFP_NOFS);
5938
5939         if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags)) {
5940                 ret = btrfs_mark_extent_written(trans, inode,
5941                                                 ordered->file_offset,
5942                                                 ordered->file_offset +
5943                                                 ordered->len);
5944                 if (ret) {
5945                         err = ret;
5946                         goto out_unlock;
5947                 }
5948         } else {
5949                 ret = insert_reserved_file_extent(trans, inode,
5950                                                   ordered->file_offset,
5951                                                   ordered->start,
5952                                                   ordered->disk_len,
5953                                                   ordered->len,
5954                                                   ordered->len,
5955                                                   0, 0, 0,
5956                                                   BTRFS_FILE_EXTENT_REG);
5957                 unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
5958                                    ordered->file_offset, ordered->len);
5959                 if (ret) {
5960                         err = ret;
5961                         WARN_ON(1);
5962                         goto out_unlock;
5963                 }
5964         }
5965
5966         add_pending_csums(trans, inode, ordered->file_offset, &ordered->list);
5967         ret = btrfs_ordered_update_i_size(inode, 0, ordered);
5968         if (!ret)
5969                 btrfs_update_inode(trans, root, inode);
5970         ret = 0;
5971 out_unlock:
5972         unlock_extent_cached(&BTRFS_I(inode)->io_tree, ordered->file_offset,
5973                              ordered->file_offset + ordered->len - 1,
5974                              &cached_state, GFP_NOFS);
5975 out:
5976         btrfs_delalloc_release_metadata(inode, ordered->len);
5977         btrfs_end_transaction(trans, root);
5978         ordered_offset = ordered->file_offset + ordered->len;
5979         btrfs_put_ordered_extent(ordered);
5980         btrfs_put_ordered_extent(ordered);
5981
5982 out_test:
5983         /*
5984          * our bio might span multiple ordered extents.  If we haven't
5985          * completed the accounting for the whole dio, go back and try again
5986          */
5987         if (ordered_offset < dip->logical_offset + dip->bytes) {
5988                 ordered_bytes = dip->logical_offset + dip->bytes -
5989                         ordered_offset;
5990                 goto again;
5991         }
5992 out_done:
5993         bio->bi_private = dip->private;
5994
5995         kfree(dip->csums);
5996         kfree(dip);
5997
5998         /* If we had an error make sure to clear the uptodate flag */
5999         if (err)
6000                 clear_bit(BIO_UPTODATE, &bio->bi_flags);
6001         dio_end_io(bio, err);
6002 }
6003
6004 static int __btrfs_submit_bio_start_direct_io(struct inode *inode, int rw,
6005                                     struct bio *bio, int mirror_num,
6006                                     unsigned long bio_flags, u64 offset)
6007 {
6008         int ret;
6009         struct btrfs_root *root = BTRFS_I(inode)->root;
6010         ret = btrfs_csum_one_bio(root, inode, bio, offset, 1);
6011         BUG_ON(ret);
6012         return 0;
6013 }
6014
6015 static void btrfs_end_dio_bio(struct bio *bio, int err)
6016 {
6017         struct btrfs_dio_private *dip = bio->bi_private;
6018
6019         if (err) {
6020                 printk(KERN_ERR "btrfs direct IO failed ino %llu rw %lu "
6021                       "sector %#Lx len %u err no %d\n",
6022                       (unsigned long long)btrfs_ino(dip->inode), bio->bi_rw,
6023                       (unsigned long long)bio->bi_sector, bio->bi_size, err);
6024                 dip->errors = 1;
6025
6026                 /*
6027                  * before atomic variable goto zero, we must make sure
6028                  * dip->errors is perceived to be set.
6029                  */
6030                 smp_mb__before_atomic_dec();
6031         }
6032
6033         /* if there are more bios still pending for this dio, just exit */
6034         if (!atomic_dec_and_test(&dip->pending_bios))
6035                 goto out;
6036
6037         if (dip->errors)
6038                 bio_io_error(dip->orig_bio);
6039         else {
6040                 set_bit(BIO_UPTODATE, &dip->orig_bio->bi_flags);
6041                 bio_endio(dip->orig_bio, 0);
6042         }
6043 out:
6044         bio_put(bio);
6045 }
6046
6047 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
6048                                        u64 first_sector, gfp_t gfp_flags)
6049 {
6050         int nr_vecs = bio_get_nr_vecs(bdev);
6051         return btrfs_bio_alloc(bdev, first_sector, nr_vecs, gfp_flags);
6052 }
6053
6054 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
6055                                          int rw, u64 file_offset, int skip_sum,
6056                                          u32 *csums, int async_submit)
6057 {
6058         int write = rw & REQ_WRITE;
6059         struct btrfs_root *root = BTRFS_I(inode)->root;
6060         int ret;
6061
6062         bio_get(bio);
6063         ret = btrfs_bio_wq_end_io(root->fs_info, bio, 0);
6064         if (ret)
6065                 goto err;
6066
6067         if (skip_sum)
6068                 goto map;
6069
6070         if (write && async_submit) {
6071                 ret = btrfs_wq_submit_bio(root->fs_info,
6072                                    inode, rw, bio, 0, 0,
6073                                    file_offset,
6074                                    __btrfs_submit_bio_start_direct_io,
6075                                    __btrfs_submit_bio_done);
6076                 goto err;
6077         } else if (write) {
6078                 /*
6079                  * If we aren't doing async submit, calculate the csum of the
6080                  * bio now.
6081                  */
6082                 ret = btrfs_csum_one_bio(root, inode, bio, file_offset, 1);
6083                 if (ret)
6084                         goto err;
6085         } else if (!skip_sum) {
6086                 ret = btrfs_lookup_bio_sums_dio(root, inode, bio,
6087                                           file_offset, csums);
6088                 if (ret)
6089                         goto err;
6090         }
6091
6092 map:
6093         ret = btrfs_map_bio(root, rw, bio, 0, async_submit);
6094 err:
6095         bio_put(bio);
6096         return ret;
6097 }
6098
6099 static int btrfs_submit_direct_hook(int rw, struct btrfs_dio_private *dip,
6100                                     int skip_sum)
6101 {
6102         struct inode *inode = dip->inode;
6103         struct btrfs_root *root = BTRFS_I(inode)->root;
6104         struct btrfs_mapping_tree *map_tree = &root->fs_info->mapping_tree;
6105         struct bio *bio;
6106         struct bio *orig_bio = dip->orig_bio;
6107         struct bio_vec *bvec = orig_bio->bi_io_vec;
6108         u64 start_sector = orig_bio->bi_sector;
6109         u64 file_offset = dip->logical_offset;
6110         u64 submit_len = 0;
6111         u64 map_length;
6112         int nr_pages = 0;
6113         u32 *csums = dip->csums;
6114         int ret = 0;
6115         int async_submit = 0;
6116         int write = rw & REQ_WRITE;
6117
6118         map_length = orig_bio->bi_size;
6119         ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6120                               &map_length, NULL, 0);
6121         if (ret) {
6122                 bio_put(orig_bio);
6123                 return -EIO;
6124         }
6125
6126         if (map_length >= orig_bio->bi_size) {
6127                 bio = orig_bio;
6128                 goto submit;
6129         }
6130
6131         async_submit = 1;
6132         bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
6133         if (!bio)
6134                 return -ENOMEM;
6135         bio->bi_private = dip;
6136         bio->bi_end_io = btrfs_end_dio_bio;
6137         atomic_inc(&dip->pending_bios);
6138
6139         while (bvec <= (orig_bio->bi_io_vec + orig_bio->bi_vcnt - 1)) {
6140                 if (unlikely(map_length < submit_len + bvec->bv_len ||
6141                     bio_add_page(bio, bvec->bv_page, bvec->bv_len,
6142                                  bvec->bv_offset) < bvec->bv_len)) {
6143                         /*
6144                          * inc the count before we submit the bio so
6145                          * we know the end IO handler won't happen before
6146                          * we inc the count. Otherwise, the dip might get freed
6147                          * before we're done setting it up
6148                          */
6149                         atomic_inc(&dip->pending_bios);
6150                         ret = __btrfs_submit_dio_bio(bio, inode, rw,
6151                                                      file_offset, skip_sum,
6152                                                      csums, async_submit);
6153                         if (ret) {
6154                                 bio_put(bio);
6155                                 atomic_dec(&dip->pending_bios);
6156                                 goto out_err;
6157                         }
6158
6159                         /* Write's use the ordered csums */
6160                         if (!write && !skip_sum)
6161                                 csums = csums + nr_pages;
6162                         start_sector += submit_len >> 9;
6163                         file_offset += submit_len;
6164
6165                         submit_len = 0;
6166                         nr_pages = 0;
6167
6168                         bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
6169                                                   start_sector, GFP_NOFS);
6170                         if (!bio)
6171                                 goto out_err;
6172                         bio->bi_private = dip;
6173                         bio->bi_end_io = btrfs_end_dio_bio;
6174
6175                         map_length = orig_bio->bi_size;
6176                         ret = btrfs_map_block(map_tree, READ, start_sector << 9,
6177                                               &map_length, NULL, 0);
6178                         if (ret) {
6179                                 bio_put(bio);
6180                                 goto out_err;
6181                         }
6182                 } else {
6183                         submit_len += bvec->bv_len;
6184                         nr_pages ++;
6185                         bvec++;
6186                 }
6187         }
6188
6189 submit:
6190         ret = __btrfs_submit_dio_bio(bio, inode, rw, file_offset, skip_sum,
6191                                      csums, async_submit);
6192         if (!ret)
6193                 return 0;
6194
6195         bio_put(bio);
6196 out_err:
6197         dip->errors = 1;
6198         /*
6199          * before atomic variable goto zero, we must
6200          * make sure dip->errors is perceived to be set.
6201          */
6202         smp_mb__before_atomic_dec();
6203         if (atomic_dec_and_test(&dip->pending_bios))
6204                 bio_io_error(dip->orig_bio);
6205
6206         /* bio_end_io() will handle error, so we needn't return it */
6207         return 0;
6208 }
6209
6210 static void btrfs_submit_direct(int rw, struct bio *bio, struct inode *inode,
6211                                 loff_t file_offset)
6212 {
6213         struct btrfs_root *root = BTRFS_I(inode)->root;
6214         struct btrfs_dio_private *dip;
6215         struct bio_vec *bvec = bio->bi_io_vec;
6216         int skip_sum;
6217         int write = rw & REQ_WRITE;
6218         int ret = 0;
6219
6220         skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
6221
6222         dip = kmalloc(sizeof(*dip), GFP_NOFS);
6223         if (!dip) {
6224                 ret = -ENOMEM;
6225                 goto free_ordered;
6226         }
6227         dip->csums = NULL;
6228
6229         /* Write's use the ordered csum stuff, so we don't need dip->csums */
6230         if (!write && !skip_sum) {
6231                 dip->csums = kmalloc(sizeof(u32) * bio->bi_vcnt, GFP_NOFS);
6232                 if (!dip->csums) {
6233                         kfree(dip);
6234                         ret = -ENOMEM;
6235                         goto free_ordered;
6236                 }
6237         }
6238
6239         dip->private = bio->bi_private;
6240         dip->inode = inode;
6241         dip->logical_offset = file_offset;
6242
6243         dip->bytes = 0;
6244         do {
6245                 dip->bytes += bvec->bv_len;
6246                 bvec++;
6247         } while (bvec <= (bio->bi_io_vec + bio->bi_vcnt - 1));
6248
6249         dip->disk_bytenr = (u64)bio->bi_sector << 9;
6250         bio->bi_private = dip;
6251         dip->errors = 0;
6252         dip->orig_bio = bio;
6253         atomic_set(&dip->pending_bios, 0);
6254
6255         if (write)
6256                 bio->bi_end_io = btrfs_endio_direct_write;
6257         else
6258                 bio->bi_end_io = btrfs_endio_direct_read;
6259
6260         ret = btrfs_submit_direct_hook(rw, dip, skip_sum);
6261         if (!ret)
6262                 return;
6263 free_ordered:
6264         /*
6265          * If this is a write, we need to clean up the reserved space and kill
6266          * the ordered extent.
6267          */
6268         if (write) {
6269                 struct btrfs_ordered_extent *ordered;
6270                 ordered = btrfs_lookup_ordered_extent(inode, file_offset);
6271                 if (!test_bit(BTRFS_ORDERED_PREALLOC, &ordered->flags) &&
6272                     !test_bit(BTRFS_ORDERED_NOCOW, &ordered->flags))
6273                         btrfs_free_reserved_extent(root, ordered->start,
6274                                                    ordered->disk_len);
6275                 btrfs_put_ordered_extent(ordered);
6276                 btrfs_put_ordered_extent(ordered);
6277         }
6278         bio_endio(bio, ret);
6279 }
6280
6281 static ssize_t check_direct_IO(struct btrfs_root *root, int rw, struct kiocb *iocb,
6282                         const struct iovec *iov, loff_t offset,
6283                         unsigned long nr_segs)
6284 {
6285         int seg;
6286         int i;
6287         size_t size;
6288         unsigned long addr;
6289         unsigned blocksize_mask = root->sectorsize - 1;
6290         ssize_t retval = -EINVAL;
6291         loff_t end = offset;
6292
6293         if (offset & blocksize_mask)
6294                 goto out;
6295
6296         /* Check the memory alignment.  Blocks cannot straddle pages */
6297         for (seg = 0; seg < nr_segs; seg++) {
6298                 addr = (unsigned long)iov[seg].iov_base;
6299                 size = iov[seg].iov_len;
6300                 end += size;
6301                 if ((addr & blocksize_mask) || (size & blocksize_mask))
6302                         goto out;
6303
6304                 /* If this is a write we don't need to check anymore */
6305                 if (rw & WRITE)
6306                         continue;
6307
6308                 /*
6309                  * Check to make sure we don't have duplicate iov_base's in this
6310                  * iovec, if so return EINVAL, otherwise we'll get csum errors
6311                  * when reading back.
6312                  */
6313                 for (i = seg + 1; i < nr_segs; i++) {
6314                         if (iov[seg].iov_base == iov[i].iov_base)
6315                                 goto out;
6316                 }
6317         }
6318         retval = 0;
6319 out:
6320         return retval;
6321 }
6322 static ssize_t btrfs_direct_IO(int rw, struct kiocb *iocb,
6323                         const struct iovec *iov, loff_t offset,
6324                         unsigned long nr_segs)
6325 {
6326         struct file *file = iocb->ki_filp;
6327         struct inode *inode = file->f_mapping->host;
6328         struct btrfs_ordered_extent *ordered;
6329         struct extent_state *cached_state = NULL;
6330         u64 lockstart, lockend;
6331         ssize_t ret;
6332         int writing = rw & WRITE;
6333         int write_bits = 0;
6334         size_t count = iov_length(iov, nr_segs);
6335
6336         if (check_direct_IO(BTRFS_I(inode)->root, rw, iocb, iov,
6337                             offset, nr_segs)) {
6338                 return 0;
6339         }
6340
6341         lockstart = offset;
6342         lockend = offset + count - 1;
6343
6344         if (writing) {
6345                 ret = btrfs_delalloc_reserve_space(inode, count);
6346                 if (ret)
6347                         goto out;
6348         }
6349
6350         while (1) {
6351                 lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6352                                  0, &cached_state, GFP_NOFS);
6353                 /*
6354                  * We're concerned with the entire range that we're going to be
6355                  * doing DIO to, so we need to make sure theres no ordered
6356                  * extents in this range.
6357                  */
6358                 ordered = btrfs_lookup_ordered_range(inode, lockstart,
6359                                                      lockend - lockstart + 1);
6360                 if (!ordered)
6361                         break;
6362                 unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6363                                      &cached_state, GFP_NOFS);
6364                 btrfs_start_ordered_extent(inode, ordered, 1);
6365                 btrfs_put_ordered_extent(ordered);
6366                 cond_resched();
6367         }
6368
6369         /*
6370          * we don't use btrfs_set_extent_delalloc because we don't want
6371          * the dirty or uptodate bits
6372          */
6373         if (writing) {
6374                 write_bits = EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING;
6375                 ret = set_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
6376                                      EXTENT_DELALLOC, 0, NULL, &cached_state,
6377                                      GFP_NOFS);
6378                 if (ret) {
6379                         clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
6380                                          lockend, EXTENT_LOCKED | write_bits,
6381                                          1, 0, &cached_state, GFP_NOFS);
6382                         goto out;
6383                 }
6384         }
6385
6386         free_extent_state(cached_state);
6387         cached_state = NULL;
6388
6389         ret = __blockdev_direct_IO(rw, iocb, inode,
6390                    BTRFS_I(inode)->root->fs_info->fs_devices->latest_bdev,
6391                    iov, offset, nr_segs, btrfs_get_blocks_direct, NULL,
6392                    btrfs_submit_direct, 0);
6393
6394         if (ret < 0 && ret != -EIOCBQUEUED) {
6395                 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset,
6396                               offset + iov_length(iov, nr_segs) - 1,
6397                               EXTENT_LOCKED | write_bits, 1, 0,
6398                               &cached_state, GFP_NOFS);
6399         } else if (ret >= 0 && ret < iov_length(iov, nr_segs)) {
6400                 /*
6401                  * We're falling back to buffered, unlock the section we didn't
6402                  * do IO on.
6403                  */
6404                 clear_extent_bit(&BTRFS_I(inode)->io_tree, offset + ret,
6405                               offset + iov_length(iov, nr_segs) - 1,
6406                               EXTENT_LOCKED | write_bits, 1, 0,
6407                               &cached_state, GFP_NOFS);
6408         }
6409 out:
6410         free_extent_state(cached_state);
6411         return ret;
6412 }
6413
6414 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
6415                 __u64 start, __u64 len)
6416 {
6417         return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
6418 }
6419
6420 int btrfs_readpage(struct file *file, struct page *page)
6421 {
6422         struct extent_io_tree *tree;
6423         tree = &BTRFS_I(page->mapping->host)->io_tree;
6424         return extent_read_full_page(tree, page, btrfs_get_extent);
6425 }
6426
6427 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
6428 {
6429         struct extent_io_tree *tree;
6430
6431
6432         if (current->flags & PF_MEMALLOC) {
6433                 redirty_page_for_writepage(wbc, page);
6434                 unlock_page(page);
6435                 return 0;
6436         }
6437         tree = &BTRFS_I(page->mapping->host)->io_tree;
6438         return extent_write_full_page(tree, page, btrfs_get_extent, wbc);
6439 }
6440
6441 int btrfs_writepages(struct address_space *mapping,
6442                      struct writeback_control *wbc)
6443 {
6444         struct extent_io_tree *tree;
6445
6446         tree = &BTRFS_I(mapping->host)->io_tree;
6447         return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
6448 }
6449
6450 static int
6451 btrfs_readpages(struct file *file, struct address_space *mapping,
6452                 struct list_head *pages, unsigned nr_pages)
6453 {
6454         struct extent_io_tree *tree;
6455         tree = &BTRFS_I(mapping->host)->io_tree;
6456         return extent_readpages(tree, mapping, pages, nr_pages,
6457                                 btrfs_get_extent);
6458 }
6459 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6460 {
6461         struct extent_io_tree *tree;
6462         struct extent_map_tree *map;
6463         int ret;
6464
6465         tree = &BTRFS_I(page->mapping->host)->io_tree;
6466         map = &BTRFS_I(page->mapping->host)->extent_tree;
6467         ret = try_release_extent_mapping(map, tree, page, gfp_flags);
6468         if (ret == 1) {
6469                 ClearPagePrivate(page);
6470                 set_page_private(page, 0);
6471                 page_cache_release(page);
6472         }
6473         return ret;
6474 }
6475
6476 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
6477 {
6478         if (PageWriteback(page) || PageDirty(page))
6479                 return 0;
6480         return __btrfs_releasepage(page, gfp_flags & GFP_NOFS);
6481 }
6482
6483 static void btrfs_invalidatepage(struct page *page, unsigned long offset)
6484 {
6485         struct extent_io_tree *tree;
6486         struct btrfs_ordered_extent *ordered;
6487         struct extent_state *cached_state = NULL;
6488         u64 page_start = page_offset(page);
6489         u64 page_end = page_start + PAGE_CACHE_SIZE - 1;
6490
6491
6492         /*
6493          * we have the page locked, so new writeback can't start,
6494          * and the dirty bit won't be cleared while we are here.
6495          *
6496          * Wait for IO on this page so that we can safely clear
6497          * the PagePrivate2 bit and do ordered accounting
6498          */
6499         wait_on_page_writeback(page);
6500
6501         tree = &BTRFS_I(page->mapping->host)->io_tree;
6502         if (offset) {
6503                 btrfs_releasepage(page, GFP_NOFS);
6504                 return;
6505         }
6506         lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6507                          GFP_NOFS);
6508         ordered = btrfs_lookup_ordered_extent(page->mapping->host,
6509                                            page_offset(page));
6510         if (ordered) {
6511                 /*
6512                  * IO on this page will never be started, so we need
6513                  * to account for any ordered extents now
6514                  */
6515                 clear_extent_bit(tree, page_start, page_end,
6516                                  EXTENT_DIRTY | EXTENT_DELALLOC |
6517                                  EXTENT_LOCKED | EXTENT_DO_ACCOUNTING, 1, 0,
6518                                  &cached_state, GFP_NOFS);
6519                 /*
6520                  * whoever cleared the private bit is responsible
6521                  * for the finish_ordered_io
6522                  */
6523                 if (TestClearPagePrivate2(page)) {
6524                         btrfs_finish_ordered_io(page->mapping->host,
6525                                                 page_start, page_end);
6526                 }
6527                 btrfs_put_ordered_extent(ordered);
6528                 cached_state = NULL;
6529                 lock_extent_bits(tree, page_start, page_end, 0, &cached_state,
6530                                  GFP_NOFS);
6531         }
6532         clear_extent_bit(tree, page_start, page_end,
6533                  EXTENT_LOCKED | EXTENT_DIRTY | EXTENT_DELALLOC |
6534                  EXTENT_DO_ACCOUNTING, 1, 1, &cached_state, GFP_NOFS);
6535         __btrfs_releasepage(page, GFP_NOFS);
6536
6537         ClearPageChecked(page);
6538         if (PagePrivate(page)) {
6539                 ClearPagePrivate(page);
6540                 set_page_private(page, 0);
6541                 page_cache_release(page);
6542         }
6543 }
6544
6545 /*
6546  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
6547  * called from a page fault handler when a page is first dirtied. Hence we must
6548  * be careful to check for EOF conditions here. We set the page up correctly
6549  * for a written page which means we get ENOSPC checking when writing into
6550  * holes and correct delalloc and unwritten extent mapping on filesystems that
6551  * support these features.
6552  *
6553  * We are not allowed to take the i_mutex here so we have to play games to
6554  * protect against truncate races as the page could now be beyond EOF.  Because
6555  * vmtruncate() writes the inode size before removing pages, once we have the
6556  * page lock we can determine safely if the page is beyond EOF. If it is not
6557  * beyond EOF, then the page is guaranteed safe against truncation until we
6558  * unlock the page.
6559  */
6560 int btrfs_page_mkwrite(struct vm_area_struct *vma, struct vm_fault *vmf)
6561 {
6562         struct page *page = vmf->page;
6563         struct inode *inode = fdentry(vma->vm_file)->d_inode;
6564         struct btrfs_root *root = BTRFS_I(inode)->root;
6565         struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6566         struct btrfs_ordered_extent *ordered;
6567         struct extent_state *cached_state = NULL;
6568         char *kaddr;
6569         unsigned long zero_start;
6570         loff_t size;
6571         int ret;
6572         u64 page_start;
6573         u64 page_end;
6574
6575         ret  = btrfs_delalloc_reserve_space(inode, PAGE_CACHE_SIZE);
6576         if (ret) {
6577                 if (ret == -ENOMEM)
6578                         ret = VM_FAULT_OOM;
6579                 else /* -ENOSPC, -EIO, etc */
6580                         ret = VM_FAULT_SIGBUS;
6581                 goto out;
6582         }
6583
6584         ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
6585 again:
6586         lock_page(page);
6587         size = i_size_read(inode);
6588         page_start = page_offset(page);
6589         page_end = page_start + PAGE_CACHE_SIZE - 1;
6590
6591         if ((page->mapping != inode->i_mapping) ||
6592             (page_start >= size)) {
6593                 /* page got truncated out from underneath us */
6594                 goto out_unlock;
6595         }
6596         wait_on_page_writeback(page);
6597
6598         lock_extent_bits(io_tree, page_start, page_end, 0, &cached_state,
6599                          GFP_NOFS);
6600         set_page_extent_mapped(page);
6601
6602         /*
6603          * we can't set the delalloc bits if there are pending ordered
6604          * extents.  Drop our locks and wait for them to finish
6605          */
6606         ordered = btrfs_lookup_ordered_extent(inode, page_start);
6607         if (ordered) {
6608                 unlock_extent_cached(io_tree, page_start, page_end,
6609                                      &cached_state, GFP_NOFS);
6610                 unlock_page(page);
6611                 btrfs_start_ordered_extent(inode, ordered, 1);
6612                 btrfs_put_ordered_extent(ordered);
6613                 goto again;
6614         }
6615
6616         /*
6617          * XXX - page_mkwrite gets called every time the page is dirtied, even
6618          * if it was already dirty, so for space accounting reasons we need to
6619          * clear any delalloc bits for the range we are fixing to save.  There
6620          * is probably a better way to do this, but for now keep consistent with
6621          * prepare_pages in the normal write path.
6622          */
6623         clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, page_end,
6624                           EXTENT_DIRTY | EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING,
6625                           0, 0, &cached_state, GFP_NOFS);
6626
6627         ret = btrfs_set_extent_delalloc(inode, page_start, page_end,
6628                                         &cached_state);
6629         if (ret) {
6630                 unlock_extent_cached(io_tree, page_start, page_end,
6631                                      &cached_state, GFP_NOFS);
6632                 ret = VM_FAULT_SIGBUS;
6633                 goto out_unlock;
6634         }
6635         ret = 0;
6636
6637         /* page is wholly or partially inside EOF */
6638         if (page_start + PAGE_CACHE_SIZE > size)
6639                 zero_start = size & ~PAGE_CACHE_MASK;
6640         else
6641                 zero_start = PAGE_CACHE_SIZE;
6642
6643         if (zero_start != PAGE_CACHE_SIZE) {
6644                 kaddr = kmap(page);
6645                 memset(kaddr + zero_start, 0, PAGE_CACHE_SIZE - zero_start);
6646                 flush_dcache_page(page);
6647                 kunmap(page);
6648         }
6649         ClearPageChecked(page);
6650         set_page_dirty(page);
6651         SetPageUptodate(page);
6652
6653         BTRFS_I(inode)->last_trans = root->fs_info->generation;
6654         BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
6655
6656         unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
6657
6658 out_unlock:
6659         if (!ret)
6660                 return VM_FAULT_LOCKED;
6661         unlock_page(page);
6662         btrfs_delalloc_release_space(inode, PAGE_CACHE_SIZE);
6663 out:
6664         return ret;
6665 }
6666
6667 static int btrfs_truncate(struct inode *inode)
6668 {
6669         struct btrfs_root *root = BTRFS_I(inode)->root;
6670         int ret;
6671         int err = 0;
6672         struct btrfs_trans_handle *trans;
6673         unsigned long nr;
6674         u64 mask = root->sectorsize - 1;
6675
6676         ret = btrfs_truncate_page(inode->i_mapping, inode->i_size);
6677         if (ret)
6678                 return ret;
6679
6680         btrfs_wait_ordered_range(inode, inode->i_size & (~mask), (u64)-1);
6681         btrfs_ordered_update_i_size(inode, inode->i_size, NULL);
6682
6683         trans = btrfs_start_transaction(root, 5);
6684         if (IS_ERR(trans))
6685                 return PTR_ERR(trans);
6686
6687         btrfs_set_trans_block_group(trans, inode);
6688
6689         ret = btrfs_orphan_add(trans, inode);
6690         if (ret) {
6691                 btrfs_end_transaction(trans, root);
6692                 return ret;
6693         }
6694
6695         nr = trans->blocks_used;
6696         btrfs_end_transaction(trans, root);
6697         btrfs_btree_balance_dirty(root, nr);
6698
6699         /* Now start a transaction for the truncate */
6700         trans = btrfs_start_transaction(root, 0);
6701         if (IS_ERR(trans))
6702                 return PTR_ERR(trans);
6703         btrfs_set_trans_block_group(trans, inode);
6704         trans->block_rsv = root->orphan_block_rsv;
6705
6706         /*
6707          * setattr is responsible for setting the ordered_data_close flag,
6708          * but that is only tested during the last file release.  That
6709          * could happen well after the next commit, leaving a great big
6710          * window where new writes may get lost if someone chooses to write
6711          * to this file after truncating to zero
6712          *
6713          * The inode doesn't have any dirty data here, and so if we commit
6714          * this is a noop.  If someone immediately starts writing to the inode
6715          * it is very likely we'll catch some of their writes in this
6716          * transaction, and the commit will find this file on the ordered
6717          * data list with good things to send down.
6718          *
6719          * This is a best effort solution, there is still a window where
6720          * using truncate to replace the contents of the file will
6721          * end up with a zero length file after a crash.
6722          */
6723         if (inode->i_size == 0 && BTRFS_I(inode)->ordered_data_close)
6724                 btrfs_add_ordered_operation(trans, root, inode);
6725
6726         while (1) {
6727                 if (!trans) {
6728                         trans = btrfs_start_transaction(root, 0);
6729                         if (IS_ERR(trans))
6730                                 return PTR_ERR(trans);
6731                         btrfs_set_trans_block_group(trans, inode);
6732                         trans->block_rsv = root->orphan_block_rsv;
6733                 }
6734
6735                 ret = btrfs_block_rsv_check(trans, root,
6736                                             root->orphan_block_rsv, 0, 5);
6737                 if (ret == -EAGAIN) {
6738                         ret = btrfs_commit_transaction(trans, root);
6739                         if (ret)
6740                                 return ret;
6741                         trans = NULL;
6742                         continue;
6743                 } else if (ret) {
6744                         err = ret;
6745                         break;
6746                 }
6747
6748                 ret = btrfs_truncate_inode_items(trans, root, inode,
6749                                                  inode->i_size,
6750                                                  BTRFS_EXTENT_DATA_KEY);
6751                 if (ret != -EAGAIN) {
6752                         err = ret;
6753                         break;
6754                 }
6755
6756                 ret = btrfs_update_inode(trans, root, inode);
6757                 if (ret) {
6758                         err = ret;
6759                         break;
6760                 }
6761
6762                 nr = trans->blocks_used;
6763                 btrfs_end_transaction(trans, root);
6764                 trans = NULL;
6765                 btrfs_btree_balance_dirty(root, nr);
6766         }
6767
6768         if (ret == 0 && inode->i_nlink > 0) {
6769                 ret = btrfs_orphan_del(trans, inode);
6770                 if (ret)
6771                         err = ret;
6772         } else if (ret && inode->i_nlink > 0) {
6773                 /*
6774                  * Failed to do the truncate, remove us from the in memory
6775                  * orphan list.
6776                  */
6777                 ret = btrfs_orphan_del(NULL, inode);
6778         }
6779
6780         ret = btrfs_update_inode(trans, root, inode);
6781         if (ret && !err)
6782                 err = ret;
6783
6784         nr = trans->blocks_used;
6785         ret = btrfs_end_transaction_throttle(trans, root);
6786         if (ret && !err)
6787                 err = ret;
6788         btrfs_btree_balance_dirty(root, nr);
6789
6790         return err;
6791 }
6792
6793 /*
6794  * create a new subvolume directory/inode (helper for the ioctl).
6795  */
6796 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
6797                              struct btrfs_root *new_root,
6798                              u64 new_dirid, u64 alloc_hint)
6799 {
6800         struct inode *inode;
6801         int err;
6802         u64 index = 0;
6803
6804         inode = btrfs_new_inode(trans, new_root, NULL, "..", 2, new_dirid,
6805                                 new_dirid, alloc_hint, S_IFDIR | 0700, &index);
6806         if (IS_ERR(inode))
6807                 return PTR_ERR(inode);
6808         inode->i_op = &btrfs_dir_inode_operations;
6809         inode->i_fop = &btrfs_dir_file_operations;
6810
6811         inode->i_nlink = 1;
6812         btrfs_i_size_write(inode, 0);
6813
6814         err = btrfs_update_inode(trans, new_root, inode);
6815         BUG_ON(err);
6816
6817         iput(inode);
6818         return 0;
6819 }
6820
6821 /* helper function for file defrag and space balancing.  This
6822  * forces readahead on a given range of bytes in an inode
6823  */
6824 unsigned long btrfs_force_ra(struct address_space *mapping,
6825                               struct file_ra_state *ra, struct file *file,
6826                               pgoff_t offset, pgoff_t last_index)
6827 {
6828         pgoff_t req_size = last_index - offset + 1;
6829
6830         page_cache_sync_readahead(mapping, ra, file, offset, req_size);
6831         return offset + req_size;
6832 }
6833
6834 struct inode *btrfs_alloc_inode(struct super_block *sb)
6835 {
6836         struct btrfs_inode *ei;
6837         struct inode *inode;
6838
6839         ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
6840         if (!ei)
6841                 return NULL;
6842
6843         ei->root = NULL;
6844         ei->space_info = NULL;
6845         ei->generation = 0;
6846         ei->sequence = 0;
6847         ei->last_trans = 0;
6848         ei->last_sub_trans = 0;
6849         ei->logged_trans = 0;
6850         ei->delalloc_bytes = 0;
6851         ei->reserved_bytes = 0;
6852         ei->disk_i_size = 0;
6853         ei->flags = 0;
6854         ei->index_cnt = (u64)-1;
6855         ei->last_unlink_trans = 0;
6856
6857         atomic_set(&ei->outstanding_extents, 0);
6858         atomic_set(&ei->reserved_extents, 0);
6859
6860         ei->ordered_data_close = 0;
6861         ei->orphan_meta_reserved = 0;
6862         ei->dummy_inode = 0;
6863         ei->force_compress = BTRFS_COMPRESS_NONE;
6864
6865         ei->delayed_node = NULL;
6866
6867         inode = &ei->vfs_inode;
6868         extent_map_tree_init(&ei->extent_tree, GFP_NOFS);
6869         extent_io_tree_init(&ei->io_tree, &inode->i_data, GFP_NOFS);
6870         extent_io_tree_init(&ei->io_failure_tree, &inode->i_data, GFP_NOFS);
6871         mutex_init(&ei->log_mutex);
6872         btrfs_ordered_inode_tree_init(&ei->ordered_tree);
6873         INIT_LIST_HEAD(&ei->i_orphan);
6874         INIT_LIST_HEAD(&ei->delalloc_inodes);
6875         INIT_LIST_HEAD(&ei->ordered_operations);
6876         RB_CLEAR_NODE(&ei->rb_node);
6877
6878         return inode;
6879 }
6880
6881 static void btrfs_i_callback(struct rcu_head *head)
6882 {
6883         struct inode *inode = container_of(head, struct inode, i_rcu);
6884         INIT_LIST_HEAD(&inode->i_dentry);
6885         kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
6886 }
6887
6888 void btrfs_destroy_inode(struct inode *inode)
6889 {
6890         struct btrfs_ordered_extent *ordered;
6891         struct btrfs_root *root = BTRFS_I(inode)->root;
6892
6893         WARN_ON(!list_empty(&inode->i_dentry));
6894         WARN_ON(inode->i_data.nrpages);
6895         WARN_ON(atomic_read(&BTRFS_I(inode)->outstanding_extents));
6896         WARN_ON(atomic_read(&BTRFS_I(inode)->reserved_extents));
6897
6898         /*
6899          * This can happen where we create an inode, but somebody else also
6900          * created the same inode and we need to destroy the one we already
6901          * created.
6902          */
6903         if (!root)
6904                 goto free;
6905
6906         /*
6907          * Make sure we're properly removed from the ordered operation
6908          * lists.
6909          */
6910         smp_mb();
6911         if (!list_empty(&BTRFS_I(inode)->ordered_operations)) {
6912                 spin_lock(&root->fs_info->ordered_extent_lock);
6913                 list_del_init(&BTRFS_I(inode)->ordered_operations);
6914                 spin_unlock(&root->fs_info->ordered_extent_lock);
6915         }
6916
6917         if (root == root->fs_info->tree_root) {
6918                 struct btrfs_block_group_cache *block_group;
6919
6920                 block_group = btrfs_lookup_block_group(root->fs_info,
6921                                                 BTRFS_I(inode)->block_group);
6922                 if (block_group && block_group->inode == inode) {
6923                         spin_lock(&block_group->lock);
6924                         block_group->inode = NULL;
6925                         spin_unlock(&block_group->lock);
6926                         btrfs_put_block_group(block_group);
6927                 } else if (block_group) {
6928                         btrfs_put_block_group(block_group);
6929                 }
6930         }
6931
6932         spin_lock(&root->orphan_lock);
6933         if (!list_empty(&BTRFS_I(inode)->i_orphan)) {
6934                 printk(KERN_INFO "BTRFS: inode %llu still on the orphan list\n",
6935                        (unsigned long long)btrfs_ino(inode));
6936                 list_del_init(&BTRFS_I(inode)->i_orphan);
6937         }
6938         spin_unlock(&root->orphan_lock);
6939
6940         while (1) {
6941                 ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
6942                 if (!ordered)
6943                         break;
6944                 else {
6945                         printk(KERN_ERR "btrfs found ordered "
6946                                "extent %llu %llu on inode cleanup\n",
6947                                (unsigned long long)ordered->file_offset,
6948                                (unsigned long long)ordered->len);
6949                         btrfs_remove_ordered_extent(inode, ordered);
6950                         btrfs_put_ordered_extent(ordered);
6951                         btrfs_put_ordered_extent(ordered);
6952                 }
6953         }
6954         inode_tree_del(inode);
6955         btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
6956 free:
6957         btrfs_remove_delayed_node(inode);
6958         call_rcu(&inode->i_rcu, btrfs_i_callback);
6959 }
6960
6961 int btrfs_drop_inode(struct inode *inode)
6962 {
6963         struct btrfs_root *root = BTRFS_I(inode)->root;
6964
6965         if (btrfs_root_refs(&root->root_item) == 0 &&
6966             !is_free_space_inode(root, inode))
6967                 return 1;
6968         else
6969                 return generic_drop_inode(inode);
6970 }
6971
6972 static void init_once(void *foo)
6973 {
6974         struct btrfs_inode *ei = (struct btrfs_inode *) foo;
6975
6976         inode_init_once(&ei->vfs_inode);
6977 }
6978
6979 void btrfs_destroy_cachep(void)
6980 {
6981         if (btrfs_inode_cachep)
6982                 kmem_cache_destroy(btrfs_inode_cachep);
6983         if (btrfs_trans_handle_cachep)
6984                 kmem_cache_destroy(btrfs_trans_handle_cachep);
6985         if (btrfs_transaction_cachep)
6986                 kmem_cache_destroy(btrfs_transaction_cachep);
6987         if (btrfs_path_cachep)
6988                 kmem_cache_destroy(btrfs_path_cachep);
6989         if (btrfs_free_space_cachep)
6990                 kmem_cache_destroy(btrfs_free_space_cachep);
6991 }
6992
6993 int btrfs_init_cachep(void)
6994 {
6995         btrfs_inode_cachep = kmem_cache_create("btrfs_inode_cache",
6996                         sizeof(struct btrfs_inode), 0,
6997                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, init_once);
6998         if (!btrfs_inode_cachep)
6999                 goto fail;
7000
7001         btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle_cache",
7002                         sizeof(struct btrfs_trans_handle), 0,
7003                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7004         if (!btrfs_trans_handle_cachep)
7005                 goto fail;
7006
7007         btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction_cache",
7008                         sizeof(struct btrfs_transaction), 0,
7009                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7010         if (!btrfs_transaction_cachep)
7011                 goto fail;
7012
7013         btrfs_path_cachep = kmem_cache_create("btrfs_path_cache",
7014                         sizeof(struct btrfs_path), 0,
7015                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7016         if (!btrfs_path_cachep)
7017                 goto fail;
7018
7019         btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space_cache",
7020                         sizeof(struct btrfs_free_space), 0,
7021                         SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD, NULL);
7022         if (!btrfs_free_space_cachep)
7023                 goto fail;
7024
7025         return 0;
7026 fail:
7027         btrfs_destroy_cachep();
7028         return -ENOMEM;
7029 }
7030
7031 static int btrfs_getattr(struct vfsmount *mnt,
7032                          struct dentry *dentry, struct kstat *stat)
7033 {
7034         struct inode *inode = dentry->d_inode;
7035         generic_fillattr(inode, stat);
7036         stat->dev = BTRFS_I(inode)->root->anon_super.s_dev;
7037         stat->blksize = PAGE_CACHE_SIZE;
7038         stat->blocks = (inode_get_bytes(inode) +
7039                         BTRFS_I(inode)->delalloc_bytes) >> 9;
7040         return 0;
7041 }
7042
7043 /*
7044  * If a file is moved, it will inherit the cow and compression flags of the new
7045  * directory.
7046  */
7047 static void fixup_inode_flags(struct inode *dir, struct inode *inode)
7048 {
7049         struct btrfs_inode *b_dir = BTRFS_I(dir);
7050         struct btrfs_inode *b_inode = BTRFS_I(inode);
7051
7052         if (b_dir->flags & BTRFS_INODE_NODATACOW)
7053                 b_inode->flags |= BTRFS_INODE_NODATACOW;
7054         else
7055                 b_inode->flags &= ~BTRFS_INODE_NODATACOW;
7056
7057         if (b_dir->flags & BTRFS_INODE_COMPRESS)
7058                 b_inode->flags |= BTRFS_INODE_COMPRESS;
7059         else
7060                 b_inode->flags &= ~BTRFS_INODE_COMPRESS;
7061 }
7062
7063 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
7064                            struct inode *new_dir, struct dentry *new_dentry)
7065 {
7066         struct btrfs_trans_handle *trans;
7067         struct btrfs_root *root = BTRFS_I(old_dir)->root;
7068         struct btrfs_root *dest = BTRFS_I(new_dir)->root;
7069         struct inode *new_inode = new_dentry->d_inode;
7070         struct inode *old_inode = old_dentry->d_inode;
7071         struct timespec ctime = CURRENT_TIME;
7072         u64 index = 0;
7073         u64 root_objectid;
7074         int ret;
7075         u64 old_ino = btrfs_ino(old_inode);
7076
7077         if (btrfs_ino(new_dir) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
7078                 return -EPERM;
7079
7080         /* we only allow rename subvolume link between subvolumes */
7081         if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
7082                 return -EXDEV;
7083
7084         if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
7085             (new_inode && btrfs_ino(new_inode) == BTRFS_FIRST_FREE_OBJECTID))
7086                 return -ENOTEMPTY;
7087
7088         if (S_ISDIR(old_inode->i_mode) && new_inode &&
7089             new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
7090                 return -ENOTEMPTY;
7091         /*
7092          * we're using rename to replace one file with another.
7093          * and the replacement file is large.  Start IO on it now so
7094          * we don't add too much work to the end of the transaction
7095          */
7096         if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size &&
7097             old_inode->i_size > BTRFS_ORDERED_OPERATIONS_FLUSH_LIMIT)
7098                 filemap_flush(old_inode->i_mapping);
7099
7100         /* close the racy window with snapshot create/destroy ioctl */
7101         if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7102                 down_read(&root->fs_info->subvol_sem);
7103         /*
7104          * We want to reserve the absolute worst case amount of items.  So if
7105          * both inodes are subvols and we need to unlink them then that would
7106          * require 4 item modifications, but if they are both normal inodes it
7107          * would require 5 item modifications, so we'll assume their normal
7108          * inodes.  So 5 * 2 is 10, plus 1 for the new link, so 11 total items
7109          * should cover the worst case number of items we'll modify.
7110          */
7111         trans = btrfs_start_transaction(root, 20);
7112         if (IS_ERR(trans)) {
7113                 ret = PTR_ERR(trans);
7114                 goto out_notrans;
7115         }
7116
7117         btrfs_set_trans_block_group(trans, new_dir);
7118
7119         if (dest != root)
7120                 btrfs_record_root_in_trans(trans, dest);
7121
7122         ret = btrfs_set_inode_index(new_dir, &index);
7123         if (ret)
7124                 goto out_fail;
7125
7126         if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7127                 /* force full log commit if subvolume involved. */
7128                 root->fs_info->last_trans_log_full_commit = trans->transid;
7129         } else {
7130                 ret = btrfs_insert_inode_ref(trans, dest,
7131                                              new_dentry->d_name.name,
7132                                              new_dentry->d_name.len,
7133                                              old_ino,
7134                                              btrfs_ino(new_dir), index);
7135                 if (ret)
7136                         goto out_fail;
7137                 /*
7138                  * this is an ugly little race, but the rename is required
7139                  * to make sure that if we crash, the inode is either at the
7140                  * old name or the new one.  pinning the log transaction lets
7141                  * us make sure we don't allow a log commit to come in after
7142                  * we unlink the name but before we add the new name back in.
7143                  */
7144                 btrfs_pin_log_trans(root);
7145         }
7146         /*
7147          * make sure the inode gets flushed if it is replacing
7148          * something.
7149          */
7150         if (new_inode && new_inode->i_size && S_ISREG(old_inode->i_mode))
7151                 btrfs_add_ordered_operation(trans, root, old_inode);
7152
7153         old_dir->i_ctime = old_dir->i_mtime = ctime;
7154         new_dir->i_ctime = new_dir->i_mtime = ctime;
7155         old_inode->i_ctime = ctime;
7156
7157         if (old_dentry->d_parent != new_dentry->d_parent)
7158                 btrfs_record_unlink_dir(trans, old_dir, old_inode, 1);
7159
7160         if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
7161                 root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
7162                 ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
7163                                         old_dentry->d_name.name,
7164                                         old_dentry->d_name.len);
7165         } else {
7166                 ret = __btrfs_unlink_inode(trans, root, old_dir,
7167                                         old_dentry->d_inode,
7168                                         old_dentry->d_name.name,
7169                                         old_dentry->d_name.len);
7170                 if (!ret)
7171                         ret = btrfs_update_inode(trans, root, old_inode);
7172         }
7173         BUG_ON(ret);
7174
7175         if (new_inode) {
7176                 new_inode->i_ctime = CURRENT_TIME;
7177                 if (unlikely(btrfs_ino(new_inode) ==
7178                              BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
7179                         root_objectid = BTRFS_I(new_inode)->location.objectid;
7180                         ret = btrfs_unlink_subvol(trans, dest, new_dir,
7181                                                 root_objectid,
7182                                                 new_dentry->d_name.name,
7183                                                 new_dentry->d_name.len);
7184                         BUG_ON(new_inode->i_nlink == 0);
7185                 } else {
7186                         ret = btrfs_unlink_inode(trans, dest, new_dir,
7187                                                  new_dentry->d_inode,
7188                                                  new_dentry->d_name.name,
7189                                                  new_dentry->d_name.len);
7190                 }
7191                 BUG_ON(ret);
7192                 if (new_inode->i_nlink == 0) {
7193                         ret = btrfs_orphan_add(trans, new_dentry->d_inode);
7194                         BUG_ON(ret);
7195                 }
7196         }
7197
7198         fixup_inode_flags(new_dir, old_inode);
7199
7200         ret = btrfs_add_link(trans, new_dir, old_inode,
7201                              new_dentry->d_name.name,
7202                              new_dentry->d_name.len, 0, index);
7203         BUG_ON(ret);
7204
7205         if (old_ino != BTRFS_FIRST_FREE_OBJECTID) {
7206                 struct dentry *parent = dget_parent(new_dentry);
7207                 btrfs_log_new_name(trans, old_inode, old_dir, parent);
7208                 dput(parent);
7209                 btrfs_end_log_trans(root);
7210         }
7211 out_fail:
7212         btrfs_end_transaction_throttle(trans, root);
7213 out_notrans:
7214         if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
7215                 up_read(&root->fs_info->subvol_sem);
7216
7217         return ret;
7218 }
7219
7220 /*
7221  * some fairly slow code that needs optimization. This walks the list
7222  * of all the inodes with pending delalloc and forces them to disk.
7223  */
7224 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
7225 {
7226         struct list_head *head = &root->fs_info->delalloc_inodes;
7227         struct btrfs_inode *binode;
7228         struct inode *inode;
7229
7230         if (root->fs_info->sb->s_flags & MS_RDONLY)
7231                 return -EROFS;
7232
7233         spin_lock(&root->fs_info->delalloc_lock);
7234         while (!list_empty(head)) {
7235                 binode = list_entry(head->next, struct btrfs_inode,
7236                                     delalloc_inodes);
7237                 inode = igrab(&binode->vfs_inode);
7238                 if (!inode)
7239                         list_del_init(&binode->delalloc_inodes);
7240                 spin_unlock(&root->fs_info->delalloc_lock);
7241                 if (inode) {
7242                         filemap_flush(inode->i_mapping);
7243                         if (delay_iput)
7244                                 btrfs_add_delayed_iput(inode);
7245                         else
7246                                 iput(inode);
7247                 }
7248                 cond_resched();
7249                 spin_lock(&root->fs_info->delalloc_lock);
7250         }
7251         spin_unlock(&root->fs_info->delalloc_lock);
7252
7253         /* the filemap_flush will queue IO into the worker threads, but
7254          * we have to make sure the IO is actually started and that
7255          * ordered extents get created before we return
7256          */
7257         atomic_inc(&root->fs_info->async_submit_draining);
7258         while (atomic_read(&root->fs_info->nr_async_submits) ||
7259               atomic_read(&root->fs_info->async_delalloc_pages)) {
7260                 wait_event(root->fs_info->async_submit_wait,
7261                    (atomic_read(&root->fs_info->nr_async_submits) == 0 &&
7262                     atomic_read(&root->fs_info->async_delalloc_pages) == 0));
7263         }
7264         atomic_dec(&root->fs_info->async_submit_draining);
7265         return 0;
7266 }
7267
7268 int btrfs_start_one_delalloc_inode(struct btrfs_root *root, int delay_iput,
7269                                    int sync)
7270 {
7271         struct btrfs_inode *binode;
7272         struct inode *inode = NULL;
7273
7274         spin_lock(&root->fs_info->delalloc_lock);
7275         while (!list_empty(&root->fs_info->delalloc_inodes)) {
7276                 binode = list_entry(root->fs_info->delalloc_inodes.next,
7277                                     struct btrfs_inode, delalloc_inodes);
7278                 inode = igrab(&binode->vfs_inode);
7279                 if (inode) {
7280                         list_move_tail(&binode->delalloc_inodes,
7281                                        &root->fs_info->delalloc_inodes);
7282                         break;
7283                 }
7284
7285                 list_del_init(&binode->delalloc_inodes);
7286                 cond_resched_lock(&root->fs_info->delalloc_lock);
7287         }
7288         spin_unlock(&root->fs_info->delalloc_lock);
7289
7290         if (inode) {
7291                 if (sync) {
7292                         filemap_write_and_wait(inode->i_mapping);
7293                         /*
7294                          * We have to do this because compression doesn't
7295                          * actually set PG_writeback until it submits the pages
7296                          * for IO, which happens in an async thread, so we could
7297                          * race and not actually wait for any writeback pages
7298                          * because they've not been submitted yet.  Technically
7299                          * this could still be the case for the ordered stuff
7300                          * since the async thread may not have started to do its
7301                          * work yet.  If this becomes the case then we need to
7302                          * figure out a way to make sure that in writepage we
7303                          * wait for any async pages to be submitted before
7304                          * returning so that fdatawait does what its supposed to
7305                          * do.
7306                          */
7307                         btrfs_wait_ordered_range(inode, 0, (u64)-1);
7308                 } else {
7309                         filemap_flush(inode->i_mapping);
7310                 }
7311                 if (delay_iput)
7312                         btrfs_add_delayed_iput(inode);
7313                 else
7314                         iput(inode);
7315                 return 1;
7316         }
7317         return 0;
7318 }
7319
7320 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
7321                          const char *symname)
7322 {
7323         struct btrfs_trans_handle *trans;
7324         struct btrfs_root *root = BTRFS_I(dir)->root;
7325         struct btrfs_path *path;
7326         struct btrfs_key key;
7327         struct inode *inode = NULL;
7328         int err;
7329         int drop_inode = 0;
7330         u64 objectid;
7331         u64 index = 0 ;
7332         int name_len;
7333         int datasize;
7334         unsigned long ptr;
7335         struct btrfs_file_extent_item *ei;
7336         struct extent_buffer *leaf;
7337         unsigned long nr = 0;
7338
7339         name_len = strlen(symname) + 1;
7340         if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(root))
7341                 return -ENAMETOOLONG;
7342
7343         /*
7344          * 2 items for inode item and ref
7345          * 2 items for dir items
7346          * 1 item for xattr if selinux is on
7347          */
7348         trans = btrfs_start_transaction(root, 5);
7349         if (IS_ERR(trans))
7350                 return PTR_ERR(trans);
7351
7352         btrfs_set_trans_block_group(trans, dir);
7353
7354         err = btrfs_find_free_ino(root, &objectid);
7355         if (err)
7356                 goto out_unlock;
7357
7358         inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
7359                                 dentry->d_name.len, btrfs_ino(dir), objectid,
7360                                 BTRFS_I(dir)->block_group, S_IFLNK|S_IRWXUGO,
7361                                 &index);
7362         if (IS_ERR(inode)) {
7363                 err = PTR_ERR(inode);
7364                 goto out_unlock;
7365         }
7366
7367         err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
7368         if (err) {
7369                 drop_inode = 1;
7370                 goto out_unlock;
7371         }
7372
7373         btrfs_set_trans_block_group(trans, inode);
7374         err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
7375         if (err)
7376                 drop_inode = 1;
7377         else {
7378                 inode->i_mapping->a_ops = &btrfs_aops;
7379                 inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7380                 inode->i_fop = &btrfs_file_operations;
7381                 inode->i_op = &btrfs_file_inode_operations;
7382                 BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
7383         }
7384         btrfs_update_inode_block_group(trans, inode);
7385         btrfs_update_inode_block_group(trans, dir);
7386         if (drop_inode)
7387                 goto out_unlock;
7388
7389         path = btrfs_alloc_path();
7390         BUG_ON(!path);
7391         key.objectid = btrfs_ino(inode);
7392         key.offset = 0;
7393         btrfs_set_key_type(&key, BTRFS_EXTENT_DATA_KEY);
7394         datasize = btrfs_file_extent_calc_inline_size(name_len);
7395         err = btrfs_insert_empty_item(trans, root, path, &key,
7396                                       datasize);
7397         if (err) {
7398                 drop_inode = 1;
7399                 goto out_unlock;
7400         }
7401         leaf = path->nodes[0];
7402         ei = btrfs_item_ptr(leaf, path->slots[0],
7403                             struct btrfs_file_extent_item);
7404         btrfs_set_file_extent_generation(leaf, ei, trans->transid);
7405         btrfs_set_file_extent_type(leaf, ei,
7406                                    BTRFS_FILE_EXTENT_INLINE);
7407         btrfs_set_file_extent_encryption(leaf, ei, 0);
7408         btrfs_set_file_extent_compression(leaf, ei, 0);
7409         btrfs_set_file_extent_other_encoding(leaf, ei, 0);
7410         btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
7411
7412         ptr = btrfs_file_extent_inline_start(ei);
7413         write_extent_buffer(leaf, symname, ptr, name_len);
7414         btrfs_mark_buffer_dirty(leaf);
7415         btrfs_free_path(path);
7416
7417         inode->i_op = &btrfs_symlink_inode_operations;
7418         inode->i_mapping->a_ops = &btrfs_symlink_aops;
7419         inode->i_mapping->backing_dev_info = &root->fs_info->bdi;
7420         inode_set_bytes(inode, name_len);
7421         btrfs_i_size_write(inode, name_len - 1);
7422         err = btrfs_update_inode(trans, root, inode);
7423         if (err)
7424                 drop_inode = 1;
7425
7426 out_unlock:
7427         nr = trans->blocks_used;
7428         btrfs_end_transaction_throttle(trans, root);
7429         if (drop_inode) {
7430                 inode_dec_link_count(inode);
7431                 iput(inode);
7432         }
7433         btrfs_btree_balance_dirty(root, nr);
7434         return err;
7435 }
7436
7437 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
7438                                        u64 start, u64 num_bytes, u64 min_size,
7439                                        loff_t actual_len, u64 *alloc_hint,
7440                                        struct btrfs_trans_handle *trans)
7441 {
7442         struct btrfs_root *root = BTRFS_I(inode)->root;
7443         struct btrfs_key ins;
7444         u64 cur_offset = start;
7445         u64 i_size;
7446         int ret = 0;
7447         bool own_trans = true;
7448
7449         if (trans)
7450                 own_trans = false;
7451         while (num_bytes > 0) {
7452                 if (own_trans) {
7453                         trans = btrfs_start_transaction(root, 3);
7454                         if (IS_ERR(trans)) {
7455                                 ret = PTR_ERR(trans);
7456                                 break;
7457                         }
7458                 }
7459
7460                 ret = btrfs_reserve_extent(trans, root, num_bytes, min_size,
7461                                            0, *alloc_hint, (u64)-1, &ins, 1);
7462                 if (ret) {
7463                         if (own_trans)
7464                                 btrfs_end_transaction(trans, root);
7465                         break;
7466                 }
7467
7468                 ret = insert_reserved_file_extent(trans, inode,
7469                                                   cur_offset, ins.objectid,
7470                                                   ins.offset, ins.offset,
7471                                                   ins.offset, 0, 0, 0,
7472                                                   BTRFS_FILE_EXTENT_PREALLOC);
7473                 BUG_ON(ret);
7474                 btrfs_drop_extent_cache(inode, cur_offset,
7475                                         cur_offset + ins.offset -1, 0);
7476
7477                 num_bytes -= ins.offset;
7478                 cur_offset += ins.offset;
7479                 *alloc_hint = ins.objectid + ins.offset;
7480
7481                 inode->i_ctime = CURRENT_TIME;
7482                 BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
7483                 if (!(mode & FALLOC_FL_KEEP_SIZE) &&
7484                     (actual_len > inode->i_size) &&
7485                     (cur_offset > inode->i_size)) {
7486                         if (cur_offset > actual_len)
7487                                 i_size = actual_len;
7488                         else
7489                                 i_size = cur_offset;
7490                         i_size_write(inode, i_size);
7491                         btrfs_ordered_update_i_size(inode, i_size, NULL);
7492                 }
7493
7494                 ret = btrfs_update_inode(trans, root, inode);
7495                 BUG_ON(ret);
7496
7497                 if (own_trans)
7498                         btrfs_end_transaction(trans, root);
7499         }
7500         return ret;
7501 }
7502
7503 int btrfs_prealloc_file_range(struct inode *inode, int mode,
7504                               u64 start, u64 num_bytes, u64 min_size,
7505                               loff_t actual_len, u64 *alloc_hint)
7506 {
7507         return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7508                                            min_size, actual_len, alloc_hint,
7509                                            NULL);
7510 }
7511
7512 int btrfs_prealloc_file_range_trans(struct inode *inode,
7513                                     struct btrfs_trans_handle *trans, int mode,
7514                                     u64 start, u64 num_bytes, u64 min_size,
7515                                     loff_t actual_len, u64 *alloc_hint)
7516 {
7517         return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
7518                                            min_size, actual_len, alloc_hint, trans);
7519 }
7520
7521 static int btrfs_set_page_dirty(struct page *page)
7522 {
7523         return __set_page_dirty_nobuffers(page);
7524 }
7525
7526 static int btrfs_permission(struct inode *inode, int mask, unsigned int flags)
7527 {
7528         struct btrfs_root *root = BTRFS_I(inode)->root;
7529
7530         if (btrfs_root_readonly(root) && (mask & MAY_WRITE))
7531                 return -EROFS;
7532         if ((BTRFS_I(inode)->flags & BTRFS_INODE_READONLY) && (mask & MAY_WRITE))
7533                 return -EACCES;
7534         return generic_permission(inode, mask, flags, btrfs_check_acl);
7535 }
7536
7537 static const struct inode_operations btrfs_dir_inode_operations = {
7538         .getattr        = btrfs_getattr,
7539         .lookup         = btrfs_lookup,
7540         .create         = btrfs_create,
7541         .unlink         = btrfs_unlink,
7542         .link           = btrfs_link,
7543         .mkdir          = btrfs_mkdir,
7544         .rmdir          = btrfs_rmdir,
7545         .rename         = btrfs_rename,
7546         .symlink        = btrfs_symlink,
7547         .setattr        = btrfs_setattr,
7548         .mknod          = btrfs_mknod,
7549         .setxattr       = btrfs_setxattr,
7550         .getxattr       = btrfs_getxattr,
7551         .listxattr      = btrfs_listxattr,
7552         .removexattr    = btrfs_removexattr,
7553         .permission     = btrfs_permission,
7554 };
7555 static const struct inode_operations btrfs_dir_ro_inode_operations = {
7556         .lookup         = btrfs_lookup,
7557         .permission     = btrfs_permission,
7558 };
7559
7560 static const struct file_operations btrfs_dir_file_operations = {
7561         .llseek         = generic_file_llseek,
7562         .read           = generic_read_dir,
7563         .readdir        = btrfs_real_readdir,
7564         .unlocked_ioctl = btrfs_ioctl,
7565 #ifdef CONFIG_COMPAT
7566         .compat_ioctl   = btrfs_ioctl,
7567 #endif
7568         .release        = btrfs_release_file,
7569         .fsync          = btrfs_sync_file,
7570 };
7571
7572 static struct extent_io_ops btrfs_extent_io_ops = {
7573         .fill_delalloc = run_delalloc_range,
7574         .submit_bio_hook = btrfs_submit_bio_hook,
7575         .merge_bio_hook = btrfs_merge_bio_hook,
7576         .readpage_end_io_hook = btrfs_readpage_end_io_hook,
7577         .writepage_end_io_hook = btrfs_writepage_end_io_hook,
7578         .writepage_start_hook = btrfs_writepage_start_hook,
7579         .readpage_io_failed_hook = btrfs_io_failed_hook,
7580         .set_bit_hook = btrfs_set_bit_hook,
7581         .clear_bit_hook = btrfs_clear_bit_hook,
7582         .merge_extent_hook = btrfs_merge_extent_hook,
7583         .split_extent_hook = btrfs_split_extent_hook,
7584 };
7585
7586 /*
7587  * btrfs doesn't support the bmap operation because swapfiles
7588  * use bmap to make a mapping of extents in the file.  They assume
7589  * these extents won't change over the life of the file and they
7590  * use the bmap result to do IO directly to the drive.
7591  *
7592  * the btrfs bmap call would return logical addresses that aren't
7593  * suitable for IO and they also will change frequently as COW
7594  * operations happen.  So, swapfile + btrfs == corruption.
7595  *
7596  * For now we're avoiding this by dropping bmap.
7597  */
7598 static const struct address_space_operations btrfs_aops = {
7599         .readpage       = btrfs_readpage,
7600         .writepage      = btrfs_writepage,
7601         .writepages     = btrfs_writepages,
7602         .readpages      = btrfs_readpages,
7603         .direct_IO      = btrfs_direct_IO,
7604         .invalidatepage = btrfs_invalidatepage,
7605         .releasepage    = btrfs_releasepage,
7606         .set_page_dirty = btrfs_set_page_dirty,
7607         .error_remove_page = generic_error_remove_page,
7608 };
7609
7610 static const struct address_space_operations btrfs_symlink_aops = {
7611         .readpage       = btrfs_readpage,
7612         .writepage      = btrfs_writepage,
7613         .invalidatepage = btrfs_invalidatepage,
7614         .releasepage    = btrfs_releasepage,
7615 };
7616
7617 static const struct inode_operations btrfs_file_inode_operations = {
7618         .getattr        = btrfs_getattr,
7619         .setattr        = btrfs_setattr,
7620         .setxattr       = btrfs_setxattr,
7621         .getxattr       = btrfs_getxattr,
7622         .listxattr      = btrfs_listxattr,
7623         .removexattr    = btrfs_removexattr,
7624         .permission     = btrfs_permission,
7625         .fiemap         = btrfs_fiemap,
7626 };
7627 static const struct inode_operations btrfs_special_inode_operations = {
7628         .getattr        = btrfs_getattr,
7629         .setattr        = btrfs_setattr,
7630         .permission     = btrfs_permission,
7631         .setxattr       = btrfs_setxattr,
7632         .getxattr       = btrfs_getxattr,
7633         .listxattr      = btrfs_listxattr,
7634         .removexattr    = btrfs_removexattr,
7635 };
7636 static const struct inode_operations btrfs_symlink_inode_operations = {
7637         .readlink       = generic_readlink,
7638         .follow_link    = page_follow_link_light,
7639         .put_link       = page_put_link,
7640         .getattr        = btrfs_getattr,
7641         .permission     = btrfs_permission,
7642         .setxattr       = btrfs_setxattr,
7643         .getxattr       = btrfs_getxattr,
7644         .listxattr      = btrfs_listxattr,
7645         .removexattr    = btrfs_removexattr,
7646 };
7647
7648 const struct dentry_operations btrfs_dentry_operations = {
7649         .d_delete       = btrfs_dentry_delete,
7650 };