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