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