~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/btrfs/inode.c

Version: ~ [ linux-5.5-rc1 ] ~ [ linux-5.4.2 ] ~ [ linux-5.3.15 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.88 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.158 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.206 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.206 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.19.8 ] ~ [ linux-3.18.140 ] ~ [ linux-3.17.8 ] ~ [ linux-3.16.78 ] ~ [ linux-3.15.10 ] ~ [ linux-3.14.79 ] ~ [ linux-3.13.11 ] ~ [ linux-3.12.74 ] ~ [ linux-3.11.10 ] ~ [ linux-3.10.108 ] ~ [ linux-3.9.11 ] ~ [ linux-3.8.13 ] ~ [ linux-3.7.10 ] ~ [ linux-3.6.11 ] ~ [ linux-3.5.7 ] ~ [ linux-3.4.113 ] ~ [ linux-3.3.8 ] ~ [ linux-3.2.102 ] ~ [ linux-3.1.10 ] ~ [ linux-3.0.101 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

  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(root, 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, eb->len,
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;