xref: /openbmc/linux/fs/btrfs/inode.c (revision 174cd4b1)
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/compat.h>
34 #include <linux/bit_spinlock.h>
35 #include <linux/xattr.h>
36 #include <linux/posix_acl.h>
37 #include <linux/falloc.h>
38 #include <linux/slab.h>
39 #include <linux/ratelimit.h>
40 #include <linux/mount.h>
41 #include <linux/btrfs.h>
42 #include <linux/blkdev.h>
43 #include <linux/posix_acl_xattr.h>
44 #include <linux/uio.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 #include "hash.h"
60 #include "props.h"
61 #include "qgroup.h"
62 #include "dedupe.h"
63 
64 struct btrfs_iget_args {
65 	struct btrfs_key *location;
66 	struct btrfs_root *root;
67 };
68 
69 struct btrfs_dio_data {
70 	u64 outstanding_extents;
71 	u64 reserve;
72 	u64 unsubmitted_oe_range_start;
73 	u64 unsubmitted_oe_range_end;
74 	int overwrite;
75 };
76 
77 static const struct inode_operations btrfs_dir_inode_operations;
78 static const struct inode_operations btrfs_symlink_inode_operations;
79 static const struct inode_operations btrfs_dir_ro_inode_operations;
80 static const struct inode_operations btrfs_special_inode_operations;
81 static const struct inode_operations btrfs_file_inode_operations;
82 static const struct address_space_operations btrfs_aops;
83 static const struct address_space_operations btrfs_symlink_aops;
84 static const struct file_operations btrfs_dir_file_operations;
85 static const struct extent_io_ops btrfs_extent_io_ops;
86 
87 static struct kmem_cache *btrfs_inode_cachep;
88 struct kmem_cache *btrfs_trans_handle_cachep;
89 struct kmem_cache *btrfs_transaction_cachep;
90 struct kmem_cache *btrfs_path_cachep;
91 struct kmem_cache *btrfs_free_space_cachep;
92 
93 #define S_SHIFT 12
94 static const unsigned char btrfs_type_by_mode[S_IFMT >> S_SHIFT] = {
95 	[S_IFREG >> S_SHIFT]	= BTRFS_FT_REG_FILE,
96 	[S_IFDIR >> S_SHIFT]	= BTRFS_FT_DIR,
97 	[S_IFCHR >> S_SHIFT]	= BTRFS_FT_CHRDEV,
98 	[S_IFBLK >> S_SHIFT]	= BTRFS_FT_BLKDEV,
99 	[S_IFIFO >> S_SHIFT]	= BTRFS_FT_FIFO,
100 	[S_IFSOCK >> S_SHIFT]	= BTRFS_FT_SOCK,
101 	[S_IFLNK >> S_SHIFT]	= BTRFS_FT_SYMLINK,
102 };
103 
104 static int btrfs_setsize(struct inode *inode, struct iattr *attr);
105 static int btrfs_truncate(struct inode *inode);
106 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent);
107 static noinline int cow_file_range(struct inode *inode,
108 				   struct page *locked_page,
109 				   u64 start, u64 end, u64 delalloc_end,
110 				   int *page_started, unsigned long *nr_written,
111 				   int unlock, struct btrfs_dedupe_hash *hash);
112 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
113 				       u64 orig_start, u64 block_start,
114 				       u64 block_len, u64 orig_block_len,
115 				       u64 ram_bytes, int compress_type,
116 				       int type);
117 
118 static int btrfs_dirty_inode(struct inode *inode);
119 
120 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
121 void btrfs_test_inode_set_ops(struct inode *inode)
122 {
123 	BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
124 }
125 #endif
126 
127 static int btrfs_init_inode_security(struct btrfs_trans_handle *trans,
128 				     struct inode *inode,  struct inode *dir,
129 				     const struct qstr *qstr)
130 {
131 	int err;
132 
133 	err = btrfs_init_acl(trans, inode, dir);
134 	if (!err)
135 		err = btrfs_xattr_security_init(trans, inode, dir, qstr);
136 	return err;
137 }
138 
139 /*
140  * this does all the hard work for inserting an inline extent into
141  * the btree.  The caller should have done a btrfs_drop_extents so that
142  * no overlapping inline items exist in the btree
143  */
144 static int insert_inline_extent(struct btrfs_trans_handle *trans,
145 				struct btrfs_path *path, int extent_inserted,
146 				struct btrfs_root *root, struct inode *inode,
147 				u64 start, size_t size, size_t compressed_size,
148 				int compress_type,
149 				struct page **compressed_pages)
150 {
151 	struct extent_buffer *leaf;
152 	struct page *page = NULL;
153 	char *kaddr;
154 	unsigned long ptr;
155 	struct btrfs_file_extent_item *ei;
156 	int err = 0;
157 	int ret;
158 	size_t cur_size = size;
159 	unsigned long offset;
160 
161 	if (compressed_size && compressed_pages)
162 		cur_size = compressed_size;
163 
164 	inode_add_bytes(inode, size);
165 
166 	if (!extent_inserted) {
167 		struct btrfs_key key;
168 		size_t datasize;
169 
170 		key.objectid = btrfs_ino(BTRFS_I(inode));
171 		key.offset = start;
172 		key.type = BTRFS_EXTENT_DATA_KEY;
173 
174 		datasize = btrfs_file_extent_calc_inline_size(cur_size);
175 		path->leave_spinning = 1;
176 		ret = btrfs_insert_empty_item(trans, root, path, &key,
177 					      datasize);
178 		if (ret) {
179 			err = ret;
180 			goto fail;
181 		}
182 	}
183 	leaf = path->nodes[0];
184 	ei = btrfs_item_ptr(leaf, path->slots[0],
185 			    struct btrfs_file_extent_item);
186 	btrfs_set_file_extent_generation(leaf, ei, trans->transid);
187 	btrfs_set_file_extent_type(leaf, ei, BTRFS_FILE_EXTENT_INLINE);
188 	btrfs_set_file_extent_encryption(leaf, ei, 0);
189 	btrfs_set_file_extent_other_encoding(leaf, ei, 0);
190 	btrfs_set_file_extent_ram_bytes(leaf, ei, size);
191 	ptr = btrfs_file_extent_inline_start(ei);
192 
193 	if (compress_type != BTRFS_COMPRESS_NONE) {
194 		struct page *cpage;
195 		int i = 0;
196 		while (compressed_size > 0) {
197 			cpage = compressed_pages[i];
198 			cur_size = min_t(unsigned long, compressed_size,
199 				       PAGE_SIZE);
200 
201 			kaddr = kmap_atomic(cpage);
202 			write_extent_buffer(leaf, kaddr, ptr, cur_size);
203 			kunmap_atomic(kaddr);
204 
205 			i++;
206 			ptr += cur_size;
207 			compressed_size -= cur_size;
208 		}
209 		btrfs_set_file_extent_compression(leaf, ei,
210 						  compress_type);
211 	} else {
212 		page = find_get_page(inode->i_mapping,
213 				     start >> PAGE_SHIFT);
214 		btrfs_set_file_extent_compression(leaf, ei, 0);
215 		kaddr = kmap_atomic(page);
216 		offset = start & (PAGE_SIZE - 1);
217 		write_extent_buffer(leaf, kaddr + offset, ptr, size);
218 		kunmap_atomic(kaddr);
219 		put_page(page);
220 	}
221 	btrfs_mark_buffer_dirty(leaf);
222 	btrfs_release_path(path);
223 
224 	/*
225 	 * we're an inline extent, so nobody can
226 	 * extend the file past i_size without locking
227 	 * a page we already have locked.
228 	 *
229 	 * We must do any isize and inode updates
230 	 * before we unlock the pages.  Otherwise we
231 	 * could end up racing with unlink.
232 	 */
233 	BTRFS_I(inode)->disk_i_size = inode->i_size;
234 	ret = btrfs_update_inode(trans, root, inode);
235 
236 	return ret;
237 fail:
238 	return err;
239 }
240 
241 
242 /*
243  * conditionally insert an inline extent into the file.  This
244  * does the checks required to make sure the data is small enough
245  * to fit as an inline extent.
246  */
247 static noinline int cow_file_range_inline(struct btrfs_root *root,
248 					  struct inode *inode, u64 start,
249 					  u64 end, size_t compressed_size,
250 					  int compress_type,
251 					  struct page **compressed_pages)
252 {
253 	struct btrfs_fs_info *fs_info = root->fs_info;
254 	struct btrfs_trans_handle *trans;
255 	u64 isize = i_size_read(inode);
256 	u64 actual_end = min(end + 1, isize);
257 	u64 inline_len = actual_end - start;
258 	u64 aligned_end = ALIGN(end, fs_info->sectorsize);
259 	u64 data_len = inline_len;
260 	int ret;
261 	struct btrfs_path *path;
262 	int extent_inserted = 0;
263 	u32 extent_item_size;
264 
265 	if (compressed_size)
266 		data_len = compressed_size;
267 
268 	if (start > 0 ||
269 	    actual_end > fs_info->sectorsize ||
270 	    data_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info) ||
271 	    (!compressed_size &&
272 	    (actual_end & (fs_info->sectorsize - 1)) == 0) ||
273 	    end + 1 < isize ||
274 	    data_len > fs_info->max_inline) {
275 		return 1;
276 	}
277 
278 	path = btrfs_alloc_path();
279 	if (!path)
280 		return -ENOMEM;
281 
282 	trans = btrfs_join_transaction(root);
283 	if (IS_ERR(trans)) {
284 		btrfs_free_path(path);
285 		return PTR_ERR(trans);
286 	}
287 	trans->block_rsv = &fs_info->delalloc_block_rsv;
288 
289 	if (compressed_size && compressed_pages)
290 		extent_item_size = btrfs_file_extent_calc_inline_size(
291 		   compressed_size);
292 	else
293 		extent_item_size = btrfs_file_extent_calc_inline_size(
294 		    inline_len);
295 
296 	ret = __btrfs_drop_extents(trans, root, inode, path,
297 				   start, aligned_end, NULL,
298 				   1, 1, extent_item_size, &extent_inserted);
299 	if (ret) {
300 		btrfs_abort_transaction(trans, ret);
301 		goto out;
302 	}
303 
304 	if (isize > actual_end)
305 		inline_len = min_t(u64, isize, actual_end);
306 	ret = insert_inline_extent(trans, path, extent_inserted,
307 				   root, inode, start,
308 				   inline_len, compressed_size,
309 				   compress_type, compressed_pages);
310 	if (ret && ret != -ENOSPC) {
311 		btrfs_abort_transaction(trans, ret);
312 		goto out;
313 	} else if (ret == -ENOSPC) {
314 		ret = 1;
315 		goto out;
316 	}
317 
318 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
319 	btrfs_delalloc_release_metadata(inode, end + 1 - start);
320 	btrfs_drop_extent_cache(inode, start, aligned_end - 1, 0);
321 out:
322 	/*
323 	 * Don't forget to free the reserved space, as for inlined extent
324 	 * it won't count as data extent, free them directly here.
325 	 * And at reserve time, it's always aligned to page size, so
326 	 * just free one page here.
327 	 */
328 	btrfs_qgroup_free_data(inode, 0, PAGE_SIZE);
329 	btrfs_free_path(path);
330 	btrfs_end_transaction(trans);
331 	return ret;
332 }
333 
334 struct async_extent {
335 	u64 start;
336 	u64 ram_size;
337 	u64 compressed_size;
338 	struct page **pages;
339 	unsigned long nr_pages;
340 	int compress_type;
341 	struct list_head list;
342 };
343 
344 struct async_cow {
345 	struct inode *inode;
346 	struct btrfs_root *root;
347 	struct page *locked_page;
348 	u64 start;
349 	u64 end;
350 	struct list_head extents;
351 	struct btrfs_work work;
352 };
353 
354 static noinline int add_async_extent(struct async_cow *cow,
355 				     u64 start, u64 ram_size,
356 				     u64 compressed_size,
357 				     struct page **pages,
358 				     unsigned long nr_pages,
359 				     int compress_type)
360 {
361 	struct async_extent *async_extent;
362 
363 	async_extent = kmalloc(sizeof(*async_extent), GFP_NOFS);
364 	BUG_ON(!async_extent); /* -ENOMEM */
365 	async_extent->start = start;
366 	async_extent->ram_size = ram_size;
367 	async_extent->compressed_size = compressed_size;
368 	async_extent->pages = pages;
369 	async_extent->nr_pages = nr_pages;
370 	async_extent->compress_type = compress_type;
371 	list_add_tail(&async_extent->list, &cow->extents);
372 	return 0;
373 }
374 
375 static inline int inode_need_compress(struct inode *inode)
376 {
377 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
378 
379 	/* force compress */
380 	if (btrfs_test_opt(fs_info, FORCE_COMPRESS))
381 		return 1;
382 	/* bad compression ratios */
383 	if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS)
384 		return 0;
385 	if (btrfs_test_opt(fs_info, COMPRESS) ||
386 	    BTRFS_I(inode)->flags & BTRFS_INODE_COMPRESS ||
387 	    BTRFS_I(inode)->force_compress)
388 		return 1;
389 	return 0;
390 }
391 
392 static inline void inode_should_defrag(struct inode *inode,
393 		u64 start, u64 end, u64 num_bytes, u64 small_write)
394 {
395 	/* If this is a small write inside eof, kick off a defrag */
396 	if (num_bytes < small_write &&
397 	    (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
398 		btrfs_add_inode_defrag(NULL, inode);
399 }
400 
401 /*
402  * we create compressed extents in two phases.  The first
403  * phase compresses a range of pages that have already been
404  * locked (both pages and state bits are locked).
405  *
406  * This is done inside an ordered work queue, and the compression
407  * is spread across many cpus.  The actual IO submission is step
408  * two, and the ordered work queue takes care of making sure that
409  * happens in the same order things were put onto the queue by
410  * writepages and friends.
411  *
412  * If this code finds it can't get good compression, it puts an
413  * entry onto the work queue to write the uncompressed bytes.  This
414  * makes sure that both compressed inodes and uncompressed inodes
415  * are written in the same order that the flusher thread sent them
416  * down.
417  */
418 static noinline void compress_file_range(struct inode *inode,
419 					struct page *locked_page,
420 					u64 start, u64 end,
421 					struct async_cow *async_cow,
422 					int *num_added)
423 {
424 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
425 	struct btrfs_root *root = BTRFS_I(inode)->root;
426 	u64 num_bytes;
427 	u64 blocksize = fs_info->sectorsize;
428 	u64 actual_end;
429 	u64 isize = i_size_read(inode);
430 	int ret = 0;
431 	struct page **pages = NULL;
432 	unsigned long nr_pages;
433 	unsigned long nr_pages_ret = 0;
434 	unsigned long total_compressed = 0;
435 	unsigned long total_in = 0;
436 	unsigned long max_compressed = SZ_128K;
437 	unsigned long max_uncompressed = SZ_128K;
438 	int i;
439 	int will_compress;
440 	int compress_type = fs_info->compress_type;
441 	int redirty = 0;
442 
443 	inode_should_defrag(inode, start, end, end - start + 1, SZ_16K);
444 
445 	actual_end = min_t(u64, isize, end + 1);
446 again:
447 	will_compress = 0;
448 	nr_pages = (end >> PAGE_SHIFT) - (start >> PAGE_SHIFT) + 1;
449 	nr_pages = min_t(unsigned long, nr_pages, SZ_128K / PAGE_SIZE);
450 
451 	/*
452 	 * we don't want to send crud past the end of i_size through
453 	 * compression, that's just a waste of CPU time.  So, if the
454 	 * end of the file is before the start of our current
455 	 * requested range of bytes, we bail out to the uncompressed
456 	 * cleanup code that can deal with all of this.
457 	 *
458 	 * It isn't really the fastest way to fix things, but this is a
459 	 * very uncommon corner.
460 	 */
461 	if (actual_end <= start)
462 		goto cleanup_and_bail_uncompressed;
463 
464 	total_compressed = actual_end - start;
465 
466 	/*
467 	 * skip compression for a small file range(<=blocksize) that
468 	 * isn't an inline extent, since it doesn't save disk space at all.
469 	 */
470 	if (total_compressed <= blocksize &&
471 	   (start > 0 || end + 1 < BTRFS_I(inode)->disk_i_size))
472 		goto cleanup_and_bail_uncompressed;
473 
474 	/* we want to make sure that amount of ram required to uncompress
475 	 * an extent is reasonable, so we limit the total size in ram
476 	 * of a compressed extent to 128k.  This is a crucial number
477 	 * because it also controls how easily we can spread reads across
478 	 * cpus for decompression.
479 	 *
480 	 * We also want to make sure the amount of IO required to do
481 	 * a random read is reasonably small, so we limit the size of
482 	 * a compressed extent to 128k.
483 	 */
484 	total_compressed = min(total_compressed, max_uncompressed);
485 	num_bytes = ALIGN(end - start + 1, blocksize);
486 	num_bytes = max(blocksize,  num_bytes);
487 	total_in = 0;
488 	ret = 0;
489 
490 	/*
491 	 * we do compression for mount -o compress and when the
492 	 * inode has not been flagged as nocompress.  This flag can
493 	 * change at any time if we discover bad compression ratios.
494 	 */
495 	if (inode_need_compress(inode)) {
496 		WARN_ON(pages);
497 		pages = kcalloc(nr_pages, sizeof(struct page *), GFP_NOFS);
498 		if (!pages) {
499 			/* just bail out to the uncompressed code */
500 			goto cont;
501 		}
502 
503 		if (BTRFS_I(inode)->force_compress)
504 			compress_type = BTRFS_I(inode)->force_compress;
505 
506 		/*
507 		 * we need to call clear_page_dirty_for_io on each
508 		 * page in the range.  Otherwise applications with the file
509 		 * mmap'd can wander in and change the page contents while
510 		 * we are compressing them.
511 		 *
512 		 * If the compression fails for any reason, we set the pages
513 		 * dirty again later on.
514 		 */
515 		extent_range_clear_dirty_for_io(inode, start, end);
516 		redirty = 1;
517 		ret = btrfs_compress_pages(compress_type,
518 					   inode->i_mapping, start,
519 					   total_compressed, pages,
520 					   nr_pages, &nr_pages_ret,
521 					   &total_in,
522 					   &total_compressed,
523 					   max_compressed);
524 
525 		if (!ret) {
526 			unsigned long offset = total_compressed &
527 				(PAGE_SIZE - 1);
528 			struct page *page = pages[nr_pages_ret - 1];
529 			char *kaddr;
530 
531 			/* zero the tail end of the last page, we might be
532 			 * sending it down to disk
533 			 */
534 			if (offset) {
535 				kaddr = kmap_atomic(page);
536 				memset(kaddr + offset, 0,
537 				       PAGE_SIZE - offset);
538 				kunmap_atomic(kaddr);
539 			}
540 			will_compress = 1;
541 		}
542 	}
543 cont:
544 	if (start == 0) {
545 		/* lets try to make an inline extent */
546 		if (ret || total_in < (actual_end - start)) {
547 			/* we didn't compress the entire range, try
548 			 * to make an uncompressed inline extent.
549 			 */
550 			ret = cow_file_range_inline(root, inode, start, end,
551 					    0, BTRFS_COMPRESS_NONE, NULL);
552 		} else {
553 			/* try making a compressed inline extent */
554 			ret = cow_file_range_inline(root, inode, start, end,
555 						    total_compressed,
556 						    compress_type, pages);
557 		}
558 		if (ret <= 0) {
559 			unsigned long clear_flags = EXTENT_DELALLOC |
560 				EXTENT_DEFRAG;
561 			unsigned long page_error_op;
562 
563 			clear_flags |= (ret < 0) ? EXTENT_DO_ACCOUNTING : 0;
564 			page_error_op = ret < 0 ? PAGE_SET_ERROR : 0;
565 
566 			/*
567 			 * inline extent creation worked or returned error,
568 			 * we don't need to create any more async work items.
569 			 * Unlock and free up our temp pages.
570 			 */
571 			extent_clear_unlock_delalloc(inode, start, end, end,
572 						     NULL, clear_flags,
573 						     PAGE_UNLOCK |
574 						     PAGE_CLEAR_DIRTY |
575 						     PAGE_SET_WRITEBACK |
576 						     page_error_op |
577 						     PAGE_END_WRITEBACK);
578 			btrfs_free_reserved_data_space_noquota(inode, start,
579 						end - start + 1);
580 			goto free_pages_out;
581 		}
582 	}
583 
584 	if (will_compress) {
585 		/*
586 		 * we aren't doing an inline extent round the compressed size
587 		 * up to a block size boundary so the allocator does sane
588 		 * things
589 		 */
590 		total_compressed = ALIGN(total_compressed, blocksize);
591 
592 		/*
593 		 * one last check to make sure the compression is really a
594 		 * win, compare the page count read with the blocks on disk
595 		 */
596 		total_in = ALIGN(total_in, PAGE_SIZE);
597 		if (total_compressed >= total_in) {
598 			will_compress = 0;
599 		} else {
600 			num_bytes = total_in;
601 			*num_added += 1;
602 
603 			/*
604 			 * The async work queues will take care of doing actual
605 			 * allocation on disk for these compressed pages, and
606 			 * will submit them to the elevator.
607 			 */
608 			add_async_extent(async_cow, start, num_bytes,
609 					total_compressed, pages, nr_pages_ret,
610 					compress_type);
611 
612 			if (start + num_bytes < end) {
613 				start += num_bytes;
614 				pages = NULL;
615 				cond_resched();
616 				goto again;
617 			}
618 			return;
619 		}
620 	}
621 	if (pages) {
622 		/*
623 		 * the compression code ran but failed to make things smaller,
624 		 * free any pages it allocated and our page pointer array
625 		 */
626 		for (i = 0; i < nr_pages_ret; i++) {
627 			WARN_ON(pages[i]->mapping);
628 			put_page(pages[i]);
629 		}
630 		kfree(pages);
631 		pages = NULL;
632 		total_compressed = 0;
633 		nr_pages_ret = 0;
634 
635 		/* flag the file so we don't compress in the future */
636 		if (!btrfs_test_opt(fs_info, FORCE_COMPRESS) &&
637 		    !(BTRFS_I(inode)->force_compress)) {
638 			BTRFS_I(inode)->flags |= BTRFS_INODE_NOCOMPRESS;
639 		}
640 	}
641 cleanup_and_bail_uncompressed:
642 	/*
643 	 * No compression, but we still need to write the pages in the file
644 	 * we've been given so far.  redirty the locked page if it corresponds
645 	 * to our extent and set things up for the async work queue to run
646 	 * cow_file_range to do the normal delalloc dance.
647 	 */
648 	if (page_offset(locked_page) >= start &&
649 	    page_offset(locked_page) <= end)
650 		__set_page_dirty_nobuffers(locked_page);
651 		/* unlocked later on in the async handlers */
652 
653 	if (redirty)
654 		extent_range_redirty_for_io(inode, start, end);
655 	add_async_extent(async_cow, start, end - start + 1, 0, NULL, 0,
656 			 BTRFS_COMPRESS_NONE);
657 	*num_added += 1;
658 
659 	return;
660 
661 free_pages_out:
662 	for (i = 0; i < nr_pages_ret; i++) {
663 		WARN_ON(pages[i]->mapping);
664 		put_page(pages[i]);
665 	}
666 	kfree(pages);
667 }
668 
669 static void free_async_extent_pages(struct async_extent *async_extent)
670 {
671 	int i;
672 
673 	if (!async_extent->pages)
674 		return;
675 
676 	for (i = 0; i < async_extent->nr_pages; i++) {
677 		WARN_ON(async_extent->pages[i]->mapping);
678 		put_page(async_extent->pages[i]);
679 	}
680 	kfree(async_extent->pages);
681 	async_extent->nr_pages = 0;
682 	async_extent->pages = NULL;
683 }
684 
685 /*
686  * phase two of compressed writeback.  This is the ordered portion
687  * of the code, which only gets called in the order the work was
688  * queued.  We walk all the async extents created by compress_file_range
689  * and send them down to the disk.
690  */
691 static noinline void submit_compressed_extents(struct inode *inode,
692 					      struct async_cow *async_cow)
693 {
694 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
695 	struct async_extent *async_extent;
696 	u64 alloc_hint = 0;
697 	struct btrfs_key ins;
698 	struct extent_map *em;
699 	struct btrfs_root *root = BTRFS_I(inode)->root;
700 	struct extent_io_tree *io_tree;
701 	int ret = 0;
702 
703 again:
704 	while (!list_empty(&async_cow->extents)) {
705 		async_extent = list_entry(async_cow->extents.next,
706 					  struct async_extent, list);
707 		list_del(&async_extent->list);
708 
709 		io_tree = &BTRFS_I(inode)->io_tree;
710 
711 retry:
712 		/* did the compression code fall back to uncompressed IO? */
713 		if (!async_extent->pages) {
714 			int page_started = 0;
715 			unsigned long nr_written = 0;
716 
717 			lock_extent(io_tree, async_extent->start,
718 					 async_extent->start +
719 					 async_extent->ram_size - 1);
720 
721 			/* allocate blocks */
722 			ret = cow_file_range(inode, async_cow->locked_page,
723 					     async_extent->start,
724 					     async_extent->start +
725 					     async_extent->ram_size - 1,
726 					     async_extent->start +
727 					     async_extent->ram_size - 1,
728 					     &page_started, &nr_written, 0,
729 					     NULL);
730 
731 			/* JDM XXX */
732 
733 			/*
734 			 * if page_started, cow_file_range inserted an
735 			 * inline extent and took care of all the unlocking
736 			 * and IO for us.  Otherwise, we need to submit
737 			 * all those pages down to the drive.
738 			 */
739 			if (!page_started && !ret)
740 				extent_write_locked_range(io_tree,
741 						  inode, async_extent->start,
742 						  async_extent->start +
743 						  async_extent->ram_size - 1,
744 						  btrfs_get_extent,
745 						  WB_SYNC_ALL);
746 			else if (ret)
747 				unlock_page(async_cow->locked_page);
748 			kfree(async_extent);
749 			cond_resched();
750 			continue;
751 		}
752 
753 		lock_extent(io_tree, async_extent->start,
754 			    async_extent->start + async_extent->ram_size - 1);
755 
756 		ret = btrfs_reserve_extent(root, async_extent->ram_size,
757 					   async_extent->compressed_size,
758 					   async_extent->compressed_size,
759 					   0, alloc_hint, &ins, 1, 1);
760 		if (ret) {
761 			free_async_extent_pages(async_extent);
762 
763 			if (ret == -ENOSPC) {
764 				unlock_extent(io_tree, async_extent->start,
765 					      async_extent->start +
766 					      async_extent->ram_size - 1);
767 
768 				/*
769 				 * we need to redirty the pages if we decide to
770 				 * fallback to uncompressed IO, otherwise we
771 				 * will not submit these pages down to lower
772 				 * layers.
773 				 */
774 				extent_range_redirty_for_io(inode,
775 						async_extent->start,
776 						async_extent->start +
777 						async_extent->ram_size - 1);
778 
779 				goto retry;
780 			}
781 			goto out_free;
782 		}
783 		/*
784 		 * here we're doing allocation and writeback of the
785 		 * compressed pages
786 		 */
787 		em = create_io_em(inode, async_extent->start,
788 				  async_extent->ram_size, /* len */
789 				  async_extent->start, /* orig_start */
790 				  ins.objectid, /* block_start */
791 				  ins.offset, /* block_len */
792 				  ins.offset, /* orig_block_len */
793 				  async_extent->ram_size, /* ram_bytes */
794 				  async_extent->compress_type,
795 				  BTRFS_ORDERED_COMPRESSED);
796 		if (IS_ERR(em))
797 			/* ret value is not necessary due to void function */
798 			goto out_free_reserve;
799 		free_extent_map(em);
800 
801 		ret = btrfs_add_ordered_extent_compress(inode,
802 						async_extent->start,
803 						ins.objectid,
804 						async_extent->ram_size,
805 						ins.offset,
806 						BTRFS_ORDERED_COMPRESSED,
807 						async_extent->compress_type);
808 		if (ret) {
809 			btrfs_drop_extent_cache(inode, async_extent->start,
810 						async_extent->start +
811 						async_extent->ram_size - 1, 0);
812 			goto out_free_reserve;
813 		}
814 		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
815 
816 		/*
817 		 * clear dirty, set writeback and unlock the pages.
818 		 */
819 		extent_clear_unlock_delalloc(inode, async_extent->start,
820 				async_extent->start +
821 				async_extent->ram_size - 1,
822 				async_extent->start +
823 				async_extent->ram_size - 1,
824 				NULL, EXTENT_LOCKED | EXTENT_DELALLOC,
825 				PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
826 				PAGE_SET_WRITEBACK);
827 		ret = btrfs_submit_compressed_write(inode,
828 				    async_extent->start,
829 				    async_extent->ram_size,
830 				    ins.objectid,
831 				    ins.offset, async_extent->pages,
832 				    async_extent->nr_pages);
833 		if (ret) {
834 			struct extent_io_tree *tree = &BTRFS_I(inode)->io_tree;
835 			struct page *p = async_extent->pages[0];
836 			const u64 start = async_extent->start;
837 			const u64 end = start + async_extent->ram_size - 1;
838 
839 			p->mapping = inode->i_mapping;
840 			tree->ops->writepage_end_io_hook(p, start, end,
841 							 NULL, 0);
842 			p->mapping = NULL;
843 			extent_clear_unlock_delalloc(inode, start, end, end,
844 						     NULL, 0,
845 						     PAGE_END_WRITEBACK |
846 						     PAGE_SET_ERROR);
847 			free_async_extent_pages(async_extent);
848 		}
849 		alloc_hint = ins.objectid + ins.offset;
850 		kfree(async_extent);
851 		cond_resched();
852 	}
853 	return;
854 out_free_reserve:
855 	btrfs_dec_block_group_reservations(fs_info, ins.objectid);
856 	btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
857 out_free:
858 	extent_clear_unlock_delalloc(inode, async_extent->start,
859 				     async_extent->start +
860 				     async_extent->ram_size - 1,
861 				     async_extent->start +
862 				     async_extent->ram_size - 1,
863 				     NULL, EXTENT_LOCKED | EXTENT_DELALLOC |
864 				     EXTENT_DEFRAG | EXTENT_DO_ACCOUNTING,
865 				     PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
866 				     PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK |
867 				     PAGE_SET_ERROR);
868 	free_async_extent_pages(async_extent);
869 	kfree(async_extent);
870 	goto again;
871 }
872 
873 static u64 get_extent_allocation_hint(struct inode *inode, u64 start,
874 				      u64 num_bytes)
875 {
876 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
877 	struct extent_map *em;
878 	u64 alloc_hint = 0;
879 
880 	read_lock(&em_tree->lock);
881 	em = search_extent_mapping(em_tree, start, num_bytes);
882 	if (em) {
883 		/*
884 		 * if block start isn't an actual block number then find the
885 		 * first block in this inode and use that as a hint.  If that
886 		 * block is also bogus then just don't worry about it.
887 		 */
888 		if (em->block_start >= EXTENT_MAP_LAST_BYTE) {
889 			free_extent_map(em);
890 			em = search_extent_mapping(em_tree, 0, 0);
891 			if (em && em->block_start < EXTENT_MAP_LAST_BYTE)
892 				alloc_hint = em->block_start;
893 			if (em)
894 				free_extent_map(em);
895 		} else {
896 			alloc_hint = em->block_start;
897 			free_extent_map(em);
898 		}
899 	}
900 	read_unlock(&em_tree->lock);
901 
902 	return alloc_hint;
903 }
904 
905 /*
906  * when extent_io.c finds a delayed allocation range in the file,
907  * the call backs end up in this code.  The basic idea is to
908  * allocate extents on disk for the range, and create ordered data structs
909  * in ram to track those extents.
910  *
911  * locked_page is the page that writepage had locked already.  We use
912  * it to make sure we don't do extra locks or unlocks.
913  *
914  * *page_started is set to one if we unlock locked_page and do everything
915  * required to start IO on it.  It may be clean and already done with
916  * IO when we return.
917  */
918 static noinline int cow_file_range(struct inode *inode,
919 				   struct page *locked_page,
920 				   u64 start, u64 end, u64 delalloc_end,
921 				   int *page_started, unsigned long *nr_written,
922 				   int unlock, struct btrfs_dedupe_hash *hash)
923 {
924 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
925 	struct btrfs_root *root = BTRFS_I(inode)->root;
926 	u64 alloc_hint = 0;
927 	u64 num_bytes;
928 	unsigned long ram_size;
929 	u64 disk_num_bytes;
930 	u64 cur_alloc_size;
931 	u64 blocksize = fs_info->sectorsize;
932 	struct btrfs_key ins;
933 	struct extent_map *em;
934 	int ret = 0;
935 
936 	if (btrfs_is_free_space_inode(inode)) {
937 		WARN_ON_ONCE(1);
938 		ret = -EINVAL;
939 		goto out_unlock;
940 	}
941 
942 	num_bytes = ALIGN(end - start + 1, blocksize);
943 	num_bytes = max(blocksize,  num_bytes);
944 	disk_num_bytes = num_bytes;
945 
946 	inode_should_defrag(inode, start, end, num_bytes, SZ_64K);
947 
948 	if (start == 0) {
949 		/* lets try to make an inline extent */
950 		ret = cow_file_range_inline(root, inode, start, end, 0,
951 					BTRFS_COMPRESS_NONE, NULL);
952 		if (ret == 0) {
953 			extent_clear_unlock_delalloc(inode, start, end,
954 				     delalloc_end, NULL,
955 				     EXTENT_LOCKED | EXTENT_DELALLOC |
956 				     EXTENT_DEFRAG, PAGE_UNLOCK |
957 				     PAGE_CLEAR_DIRTY | PAGE_SET_WRITEBACK |
958 				     PAGE_END_WRITEBACK);
959 			btrfs_free_reserved_data_space_noquota(inode, start,
960 						end - start + 1);
961 			*nr_written = *nr_written +
962 			     (end - start + PAGE_SIZE) / PAGE_SIZE;
963 			*page_started = 1;
964 			goto out;
965 		} else if (ret < 0) {
966 			goto out_unlock;
967 		}
968 	}
969 
970 	BUG_ON(disk_num_bytes >
971 	       btrfs_super_total_bytes(fs_info->super_copy));
972 
973 	alloc_hint = get_extent_allocation_hint(inode, start, num_bytes);
974 	btrfs_drop_extent_cache(inode, start, start + num_bytes - 1, 0);
975 
976 	while (disk_num_bytes > 0) {
977 		unsigned long op;
978 
979 		cur_alloc_size = disk_num_bytes;
980 		ret = btrfs_reserve_extent(root, cur_alloc_size, cur_alloc_size,
981 					   fs_info->sectorsize, 0, alloc_hint,
982 					   &ins, 1, 1);
983 		if (ret < 0)
984 			goto out_unlock;
985 
986 		ram_size = ins.offset;
987 		em = create_io_em(inode, start, ins.offset, /* len */
988 				  start, /* orig_start */
989 				  ins.objectid, /* block_start */
990 				  ins.offset, /* block_len */
991 				  ins.offset, /* orig_block_len */
992 				  ram_size, /* ram_bytes */
993 				  BTRFS_COMPRESS_NONE, /* compress_type */
994 				  BTRFS_ORDERED_REGULAR /* type */);
995 		if (IS_ERR(em))
996 			goto out_reserve;
997 		free_extent_map(em);
998 
999 		cur_alloc_size = ins.offset;
1000 		ret = btrfs_add_ordered_extent(inode, start, ins.objectid,
1001 					       ram_size, cur_alloc_size, 0);
1002 		if (ret)
1003 			goto out_drop_extent_cache;
1004 
1005 		if (root->root_key.objectid ==
1006 		    BTRFS_DATA_RELOC_TREE_OBJECTID) {
1007 			ret = btrfs_reloc_clone_csums(inode, start,
1008 						      cur_alloc_size);
1009 			if (ret)
1010 				goto out_drop_extent_cache;
1011 		}
1012 
1013 		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1014 
1015 		if (disk_num_bytes < cur_alloc_size)
1016 			break;
1017 
1018 		/* we're not doing compressed IO, don't unlock the first
1019 		 * page (which the caller expects to stay locked), don't
1020 		 * clear any dirty bits and don't set any writeback bits
1021 		 *
1022 		 * Do set the Private2 bit so we know this page was properly
1023 		 * setup for writepage
1024 		 */
1025 		op = unlock ? PAGE_UNLOCK : 0;
1026 		op |= PAGE_SET_PRIVATE2;
1027 
1028 		extent_clear_unlock_delalloc(inode, start,
1029 					     start + ram_size - 1,
1030 					     delalloc_end, locked_page,
1031 					     EXTENT_LOCKED | EXTENT_DELALLOC,
1032 					     op);
1033 		disk_num_bytes -= cur_alloc_size;
1034 		num_bytes -= cur_alloc_size;
1035 		alloc_hint = ins.objectid + ins.offset;
1036 		start += cur_alloc_size;
1037 	}
1038 out:
1039 	return ret;
1040 
1041 out_drop_extent_cache:
1042 	btrfs_drop_extent_cache(inode, start, start + ram_size - 1, 0);
1043 out_reserve:
1044 	btrfs_dec_block_group_reservations(fs_info, ins.objectid);
1045 	btrfs_free_reserved_extent(fs_info, ins.objectid, ins.offset, 1);
1046 out_unlock:
1047 	extent_clear_unlock_delalloc(inode, start, end, delalloc_end,
1048 				     locked_page,
1049 				     EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
1050 				     EXTENT_DELALLOC | EXTENT_DEFRAG,
1051 				     PAGE_UNLOCK | PAGE_CLEAR_DIRTY |
1052 				     PAGE_SET_WRITEBACK | PAGE_END_WRITEBACK);
1053 	goto out;
1054 }
1055 
1056 /*
1057  * work queue call back to started compression on a file and pages
1058  */
1059 static noinline void async_cow_start(struct btrfs_work *work)
1060 {
1061 	struct async_cow *async_cow;
1062 	int num_added = 0;
1063 	async_cow = container_of(work, struct async_cow, work);
1064 
1065 	compress_file_range(async_cow->inode, async_cow->locked_page,
1066 			    async_cow->start, async_cow->end, async_cow,
1067 			    &num_added);
1068 	if (num_added == 0) {
1069 		btrfs_add_delayed_iput(async_cow->inode);
1070 		async_cow->inode = NULL;
1071 	}
1072 }
1073 
1074 /*
1075  * work queue call back to submit previously compressed pages
1076  */
1077 static noinline void async_cow_submit(struct btrfs_work *work)
1078 {
1079 	struct btrfs_fs_info *fs_info;
1080 	struct async_cow *async_cow;
1081 	struct btrfs_root *root;
1082 	unsigned long nr_pages;
1083 
1084 	async_cow = container_of(work, struct async_cow, work);
1085 
1086 	root = async_cow->root;
1087 	fs_info = root->fs_info;
1088 	nr_pages = (async_cow->end - async_cow->start + PAGE_SIZE) >>
1089 		PAGE_SHIFT;
1090 
1091 	/*
1092 	 * atomic_sub_return implies a barrier for waitqueue_active
1093 	 */
1094 	if (atomic_sub_return(nr_pages, &fs_info->async_delalloc_pages) <
1095 	    5 * SZ_1M &&
1096 	    waitqueue_active(&fs_info->async_submit_wait))
1097 		wake_up(&fs_info->async_submit_wait);
1098 
1099 	if (async_cow->inode)
1100 		submit_compressed_extents(async_cow->inode, async_cow);
1101 }
1102 
1103 static noinline void async_cow_free(struct btrfs_work *work)
1104 {
1105 	struct async_cow *async_cow;
1106 	async_cow = container_of(work, struct async_cow, work);
1107 	if (async_cow->inode)
1108 		btrfs_add_delayed_iput(async_cow->inode);
1109 	kfree(async_cow);
1110 }
1111 
1112 static int cow_file_range_async(struct inode *inode, struct page *locked_page,
1113 				u64 start, u64 end, int *page_started,
1114 				unsigned long *nr_written)
1115 {
1116 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1117 	struct async_cow *async_cow;
1118 	struct btrfs_root *root = BTRFS_I(inode)->root;
1119 	unsigned long nr_pages;
1120 	u64 cur_end;
1121 
1122 	clear_extent_bit(&BTRFS_I(inode)->io_tree, start, end, EXTENT_LOCKED,
1123 			 1, 0, NULL, GFP_NOFS);
1124 	while (start < end) {
1125 		async_cow = kmalloc(sizeof(*async_cow), GFP_NOFS);
1126 		BUG_ON(!async_cow); /* -ENOMEM */
1127 		async_cow->inode = igrab(inode);
1128 		async_cow->root = root;
1129 		async_cow->locked_page = locked_page;
1130 		async_cow->start = start;
1131 
1132 		if (BTRFS_I(inode)->flags & BTRFS_INODE_NOCOMPRESS &&
1133 		    !btrfs_test_opt(fs_info, FORCE_COMPRESS))
1134 			cur_end = end;
1135 		else
1136 			cur_end = min(end, start + SZ_512K - 1);
1137 
1138 		async_cow->end = cur_end;
1139 		INIT_LIST_HEAD(&async_cow->extents);
1140 
1141 		btrfs_init_work(&async_cow->work,
1142 				btrfs_delalloc_helper,
1143 				async_cow_start, async_cow_submit,
1144 				async_cow_free);
1145 
1146 		nr_pages = (cur_end - start + PAGE_SIZE) >>
1147 			PAGE_SHIFT;
1148 		atomic_add(nr_pages, &fs_info->async_delalloc_pages);
1149 
1150 		btrfs_queue_work(fs_info->delalloc_workers, &async_cow->work);
1151 
1152 		while (atomic_read(&fs_info->async_submit_draining) &&
1153 		       atomic_read(&fs_info->async_delalloc_pages)) {
1154 			wait_event(fs_info->async_submit_wait,
1155 				   (atomic_read(&fs_info->async_delalloc_pages) ==
1156 				    0));
1157 		}
1158 
1159 		*nr_written += nr_pages;
1160 		start = cur_end + 1;
1161 	}
1162 	*page_started = 1;
1163 	return 0;
1164 }
1165 
1166 static noinline int csum_exist_in_range(struct btrfs_fs_info *fs_info,
1167 					u64 bytenr, u64 num_bytes)
1168 {
1169 	int ret;
1170 	struct btrfs_ordered_sum *sums;
1171 	LIST_HEAD(list);
1172 
1173 	ret = btrfs_lookup_csums_range(fs_info->csum_root, bytenr,
1174 				       bytenr + num_bytes - 1, &list, 0);
1175 	if (ret == 0 && list_empty(&list))
1176 		return 0;
1177 
1178 	while (!list_empty(&list)) {
1179 		sums = list_entry(list.next, struct btrfs_ordered_sum, list);
1180 		list_del(&sums->list);
1181 		kfree(sums);
1182 	}
1183 	return 1;
1184 }
1185 
1186 /*
1187  * when nowcow writeback call back.  This checks for snapshots or COW copies
1188  * of the extents that exist in the file, and COWs the file as required.
1189  *
1190  * If no cow copies or snapshots exist, we write directly to the existing
1191  * blocks on disk
1192  */
1193 static noinline int run_delalloc_nocow(struct inode *inode,
1194 				       struct page *locked_page,
1195 			      u64 start, u64 end, int *page_started, int force,
1196 			      unsigned long *nr_written)
1197 {
1198 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1199 	struct btrfs_root *root = BTRFS_I(inode)->root;
1200 	struct extent_buffer *leaf;
1201 	struct btrfs_path *path;
1202 	struct btrfs_file_extent_item *fi;
1203 	struct btrfs_key found_key;
1204 	struct extent_map *em;
1205 	u64 cow_start;
1206 	u64 cur_offset;
1207 	u64 extent_end;
1208 	u64 extent_offset;
1209 	u64 disk_bytenr;
1210 	u64 num_bytes;
1211 	u64 disk_num_bytes;
1212 	u64 ram_bytes;
1213 	int extent_type;
1214 	int ret, err;
1215 	int type;
1216 	int nocow;
1217 	int check_prev = 1;
1218 	bool nolock;
1219 	u64 ino = btrfs_ino(BTRFS_I(inode));
1220 
1221 	path = btrfs_alloc_path();
1222 	if (!path) {
1223 		extent_clear_unlock_delalloc(inode, start, end, end,
1224 					     locked_page,
1225 					     EXTENT_LOCKED | EXTENT_DELALLOC |
1226 					     EXTENT_DO_ACCOUNTING |
1227 					     EXTENT_DEFRAG, PAGE_UNLOCK |
1228 					     PAGE_CLEAR_DIRTY |
1229 					     PAGE_SET_WRITEBACK |
1230 					     PAGE_END_WRITEBACK);
1231 		return -ENOMEM;
1232 	}
1233 
1234 	nolock = btrfs_is_free_space_inode(inode);
1235 
1236 	cow_start = (u64)-1;
1237 	cur_offset = start;
1238 	while (1) {
1239 		ret = btrfs_lookup_file_extent(NULL, root, path, ino,
1240 					       cur_offset, 0);
1241 		if (ret < 0)
1242 			goto error;
1243 		if (ret > 0 && path->slots[0] > 0 && check_prev) {
1244 			leaf = path->nodes[0];
1245 			btrfs_item_key_to_cpu(leaf, &found_key,
1246 					      path->slots[0] - 1);
1247 			if (found_key.objectid == ino &&
1248 			    found_key.type == BTRFS_EXTENT_DATA_KEY)
1249 				path->slots[0]--;
1250 		}
1251 		check_prev = 0;
1252 next_slot:
1253 		leaf = path->nodes[0];
1254 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
1255 			ret = btrfs_next_leaf(root, path);
1256 			if (ret < 0)
1257 				goto error;
1258 			if (ret > 0)
1259 				break;
1260 			leaf = path->nodes[0];
1261 		}
1262 
1263 		nocow = 0;
1264 		disk_bytenr = 0;
1265 		num_bytes = 0;
1266 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
1267 
1268 		if (found_key.objectid > ino)
1269 			break;
1270 		if (WARN_ON_ONCE(found_key.objectid < ino) ||
1271 		    found_key.type < BTRFS_EXTENT_DATA_KEY) {
1272 			path->slots[0]++;
1273 			goto next_slot;
1274 		}
1275 		if (found_key.type > BTRFS_EXTENT_DATA_KEY ||
1276 		    found_key.offset > end)
1277 			break;
1278 
1279 		if (found_key.offset > cur_offset) {
1280 			extent_end = found_key.offset;
1281 			extent_type = 0;
1282 			goto out_check;
1283 		}
1284 
1285 		fi = btrfs_item_ptr(leaf, path->slots[0],
1286 				    struct btrfs_file_extent_item);
1287 		extent_type = btrfs_file_extent_type(leaf, fi);
1288 
1289 		ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
1290 		if (extent_type == BTRFS_FILE_EXTENT_REG ||
1291 		    extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1292 			disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
1293 			extent_offset = btrfs_file_extent_offset(leaf, fi);
1294 			extent_end = found_key.offset +
1295 				btrfs_file_extent_num_bytes(leaf, fi);
1296 			disk_num_bytes =
1297 				btrfs_file_extent_disk_num_bytes(leaf, fi);
1298 			if (extent_end <= start) {
1299 				path->slots[0]++;
1300 				goto next_slot;
1301 			}
1302 			if (disk_bytenr == 0)
1303 				goto out_check;
1304 			if (btrfs_file_extent_compression(leaf, fi) ||
1305 			    btrfs_file_extent_encryption(leaf, fi) ||
1306 			    btrfs_file_extent_other_encoding(leaf, fi))
1307 				goto out_check;
1308 			if (extent_type == BTRFS_FILE_EXTENT_REG && !force)
1309 				goto out_check;
1310 			if (btrfs_extent_readonly(fs_info, disk_bytenr))
1311 				goto out_check;
1312 			if (btrfs_cross_ref_exist(root, ino,
1313 						  found_key.offset -
1314 						  extent_offset, disk_bytenr))
1315 				goto out_check;
1316 			disk_bytenr += extent_offset;
1317 			disk_bytenr += cur_offset - found_key.offset;
1318 			num_bytes = min(end + 1, extent_end) - cur_offset;
1319 			/*
1320 			 * if there are pending snapshots for this root,
1321 			 * we fall into common COW way.
1322 			 */
1323 			if (!nolock) {
1324 				err = btrfs_start_write_no_snapshoting(root);
1325 				if (!err)
1326 					goto out_check;
1327 			}
1328 			/*
1329 			 * force cow if csum exists in the range.
1330 			 * this ensure that csum for a given extent are
1331 			 * either valid or do not exist.
1332 			 */
1333 			if (csum_exist_in_range(fs_info, disk_bytenr,
1334 						num_bytes))
1335 				goto out_check;
1336 			if (!btrfs_inc_nocow_writers(fs_info, disk_bytenr))
1337 				goto out_check;
1338 			nocow = 1;
1339 		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
1340 			extent_end = found_key.offset +
1341 				btrfs_file_extent_inline_len(leaf,
1342 						     path->slots[0], fi);
1343 			extent_end = ALIGN(extent_end,
1344 					   fs_info->sectorsize);
1345 		} else {
1346 			BUG_ON(1);
1347 		}
1348 out_check:
1349 		if (extent_end <= start) {
1350 			path->slots[0]++;
1351 			if (!nolock && nocow)
1352 				btrfs_end_write_no_snapshoting(root);
1353 			if (nocow)
1354 				btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1355 			goto next_slot;
1356 		}
1357 		if (!nocow) {
1358 			if (cow_start == (u64)-1)
1359 				cow_start = cur_offset;
1360 			cur_offset = extent_end;
1361 			if (cur_offset > end)
1362 				break;
1363 			path->slots[0]++;
1364 			goto next_slot;
1365 		}
1366 
1367 		btrfs_release_path(path);
1368 		if (cow_start != (u64)-1) {
1369 			ret = cow_file_range(inode, locked_page,
1370 					     cow_start, found_key.offset - 1,
1371 					     end, page_started, nr_written, 1,
1372 					     NULL);
1373 			if (ret) {
1374 				if (!nolock && nocow)
1375 					btrfs_end_write_no_snapshoting(root);
1376 				if (nocow)
1377 					btrfs_dec_nocow_writers(fs_info,
1378 								disk_bytenr);
1379 				goto error;
1380 			}
1381 			cow_start = (u64)-1;
1382 		}
1383 
1384 		if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1385 			u64 orig_start = found_key.offset - extent_offset;
1386 
1387 			em = create_io_em(inode, cur_offset, num_bytes,
1388 					  orig_start,
1389 					  disk_bytenr, /* block_start */
1390 					  num_bytes, /* block_len */
1391 					  disk_num_bytes, /* orig_block_len */
1392 					  ram_bytes, BTRFS_COMPRESS_NONE,
1393 					  BTRFS_ORDERED_PREALLOC);
1394 			if (IS_ERR(em)) {
1395 				if (!nolock && nocow)
1396 					btrfs_end_write_no_snapshoting(root);
1397 				if (nocow)
1398 					btrfs_dec_nocow_writers(fs_info,
1399 								disk_bytenr);
1400 				ret = PTR_ERR(em);
1401 				goto error;
1402 			}
1403 			free_extent_map(em);
1404 		}
1405 
1406 		if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
1407 			type = BTRFS_ORDERED_PREALLOC;
1408 		} else {
1409 			type = BTRFS_ORDERED_NOCOW;
1410 		}
1411 
1412 		ret = btrfs_add_ordered_extent(inode, cur_offset, disk_bytenr,
1413 					       num_bytes, num_bytes, type);
1414 		if (nocow)
1415 			btrfs_dec_nocow_writers(fs_info, disk_bytenr);
1416 		BUG_ON(ret); /* -ENOMEM */
1417 
1418 		if (root->root_key.objectid ==
1419 		    BTRFS_DATA_RELOC_TREE_OBJECTID) {
1420 			ret = btrfs_reloc_clone_csums(inode, cur_offset,
1421 						      num_bytes);
1422 			if (ret) {
1423 				if (!nolock && nocow)
1424 					btrfs_end_write_no_snapshoting(root);
1425 				goto error;
1426 			}
1427 		}
1428 
1429 		extent_clear_unlock_delalloc(inode, cur_offset,
1430 					     cur_offset + num_bytes - 1, end,
1431 					     locked_page, EXTENT_LOCKED |
1432 					     EXTENT_DELALLOC |
1433 					     EXTENT_CLEAR_DATA_RESV,
1434 					     PAGE_UNLOCK | PAGE_SET_PRIVATE2);
1435 
1436 		if (!nolock && nocow)
1437 			btrfs_end_write_no_snapshoting(root);
1438 		cur_offset = extent_end;
1439 		if (cur_offset > end)
1440 			break;
1441 	}
1442 	btrfs_release_path(path);
1443 
1444 	if (cur_offset <= end && cow_start == (u64)-1) {
1445 		cow_start = cur_offset;
1446 		cur_offset = end;
1447 	}
1448 
1449 	if (cow_start != (u64)-1) {
1450 		ret = cow_file_range(inode, locked_page, cow_start, end, end,
1451 				     page_started, nr_written, 1, NULL);
1452 		if (ret)
1453 			goto error;
1454 	}
1455 
1456 error:
1457 	if (ret && cur_offset < end)
1458 		extent_clear_unlock_delalloc(inode, cur_offset, end, end,
1459 					     locked_page, EXTENT_LOCKED |
1460 					     EXTENT_DELALLOC | EXTENT_DEFRAG |
1461 					     EXTENT_DO_ACCOUNTING, PAGE_UNLOCK |
1462 					     PAGE_CLEAR_DIRTY |
1463 					     PAGE_SET_WRITEBACK |
1464 					     PAGE_END_WRITEBACK);
1465 	btrfs_free_path(path);
1466 	return ret;
1467 }
1468 
1469 static inline int need_force_cow(struct inode *inode, u64 start, u64 end)
1470 {
1471 
1472 	if (!(BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
1473 	    !(BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC))
1474 		return 0;
1475 
1476 	/*
1477 	 * @defrag_bytes is a hint value, no spinlock held here,
1478 	 * if is not zero, it means the file is defragging.
1479 	 * Force cow if given extent needs to be defragged.
1480 	 */
1481 	if (BTRFS_I(inode)->defrag_bytes &&
1482 	    test_range_bit(&BTRFS_I(inode)->io_tree, start, end,
1483 			   EXTENT_DEFRAG, 0, NULL))
1484 		return 1;
1485 
1486 	return 0;
1487 }
1488 
1489 /*
1490  * extent_io.c call back to do delayed allocation processing
1491  */
1492 static int run_delalloc_range(struct inode *inode, struct page *locked_page,
1493 			      u64 start, u64 end, int *page_started,
1494 			      unsigned long *nr_written)
1495 {
1496 	int ret;
1497 	int force_cow = need_force_cow(inode, start, end);
1498 
1499 	if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW && !force_cow) {
1500 		ret = run_delalloc_nocow(inode, locked_page, start, end,
1501 					 page_started, 1, nr_written);
1502 	} else if (BTRFS_I(inode)->flags & BTRFS_INODE_PREALLOC && !force_cow) {
1503 		ret = run_delalloc_nocow(inode, locked_page, start, end,
1504 					 page_started, 0, nr_written);
1505 	} else if (!inode_need_compress(inode)) {
1506 		ret = cow_file_range(inode, locked_page, start, end, end,
1507 				      page_started, nr_written, 1, NULL);
1508 	} else {
1509 		set_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
1510 			&BTRFS_I(inode)->runtime_flags);
1511 		ret = cow_file_range_async(inode, locked_page, start, end,
1512 					   page_started, nr_written);
1513 	}
1514 	return ret;
1515 }
1516 
1517 static void btrfs_split_extent_hook(struct inode *inode,
1518 				    struct extent_state *orig, u64 split)
1519 {
1520 	u64 size;
1521 
1522 	/* not delalloc, ignore it */
1523 	if (!(orig->state & EXTENT_DELALLOC))
1524 		return;
1525 
1526 	size = orig->end - orig->start + 1;
1527 	if (size > BTRFS_MAX_EXTENT_SIZE) {
1528 		u32 num_extents;
1529 		u64 new_size;
1530 
1531 		/*
1532 		 * See the explanation in btrfs_merge_extent_hook, the same
1533 		 * applies here, just in reverse.
1534 		 */
1535 		new_size = orig->end - split + 1;
1536 		num_extents = count_max_extents(new_size);
1537 		new_size = split - orig->start;
1538 		num_extents += count_max_extents(new_size);
1539 		if (count_max_extents(size) >= num_extents)
1540 			return;
1541 	}
1542 
1543 	spin_lock(&BTRFS_I(inode)->lock);
1544 	BTRFS_I(inode)->outstanding_extents++;
1545 	spin_unlock(&BTRFS_I(inode)->lock);
1546 }
1547 
1548 /*
1549  * extent_io.c merge_extent_hook, used to track merged delayed allocation
1550  * extents so we can keep track of new extents that are just merged onto old
1551  * extents, such as when we are doing sequential writes, so we can properly
1552  * account for the metadata space we'll need.
1553  */
1554 static void btrfs_merge_extent_hook(struct inode *inode,
1555 				    struct extent_state *new,
1556 				    struct extent_state *other)
1557 {
1558 	u64 new_size, old_size;
1559 	u32 num_extents;
1560 
1561 	/* not delalloc, ignore it */
1562 	if (!(other->state & EXTENT_DELALLOC))
1563 		return;
1564 
1565 	if (new->start > other->start)
1566 		new_size = new->end - other->start + 1;
1567 	else
1568 		new_size = other->end - new->start + 1;
1569 
1570 	/* we're not bigger than the max, unreserve the space and go */
1571 	if (new_size <= BTRFS_MAX_EXTENT_SIZE) {
1572 		spin_lock(&BTRFS_I(inode)->lock);
1573 		BTRFS_I(inode)->outstanding_extents--;
1574 		spin_unlock(&BTRFS_I(inode)->lock);
1575 		return;
1576 	}
1577 
1578 	/*
1579 	 * We have to add up either side to figure out how many extents were
1580 	 * accounted for before we merged into one big extent.  If the number of
1581 	 * extents we accounted for is <= the amount we need for the new range
1582 	 * then we can return, otherwise drop.  Think of it like this
1583 	 *
1584 	 * [ 4k][MAX_SIZE]
1585 	 *
1586 	 * So we've grown the extent by a MAX_SIZE extent, this would mean we
1587 	 * need 2 outstanding extents, on one side we have 1 and the other side
1588 	 * we have 1 so they are == and we can return.  But in this case
1589 	 *
1590 	 * [MAX_SIZE+4k][MAX_SIZE+4k]
1591 	 *
1592 	 * Each range on their own accounts for 2 extents, but merged together
1593 	 * they are only 3 extents worth of accounting, so we need to drop in
1594 	 * this case.
1595 	 */
1596 	old_size = other->end - other->start + 1;
1597 	num_extents = count_max_extents(old_size);
1598 	old_size = new->end - new->start + 1;
1599 	num_extents += count_max_extents(old_size);
1600 	if (count_max_extents(new_size) >= num_extents)
1601 		return;
1602 
1603 	spin_lock(&BTRFS_I(inode)->lock);
1604 	BTRFS_I(inode)->outstanding_extents--;
1605 	spin_unlock(&BTRFS_I(inode)->lock);
1606 }
1607 
1608 static void btrfs_add_delalloc_inodes(struct btrfs_root *root,
1609 				      struct inode *inode)
1610 {
1611 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1612 
1613 	spin_lock(&root->delalloc_lock);
1614 	if (list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1615 		list_add_tail(&BTRFS_I(inode)->delalloc_inodes,
1616 			      &root->delalloc_inodes);
1617 		set_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1618 			&BTRFS_I(inode)->runtime_flags);
1619 		root->nr_delalloc_inodes++;
1620 		if (root->nr_delalloc_inodes == 1) {
1621 			spin_lock(&fs_info->delalloc_root_lock);
1622 			BUG_ON(!list_empty(&root->delalloc_root));
1623 			list_add_tail(&root->delalloc_root,
1624 				      &fs_info->delalloc_roots);
1625 			spin_unlock(&fs_info->delalloc_root_lock);
1626 		}
1627 	}
1628 	spin_unlock(&root->delalloc_lock);
1629 }
1630 
1631 static void btrfs_del_delalloc_inode(struct btrfs_root *root,
1632 				     struct inode *inode)
1633 {
1634 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1635 
1636 	spin_lock(&root->delalloc_lock);
1637 	if (!list_empty(&BTRFS_I(inode)->delalloc_inodes)) {
1638 		list_del_init(&BTRFS_I(inode)->delalloc_inodes);
1639 		clear_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1640 			  &BTRFS_I(inode)->runtime_flags);
1641 		root->nr_delalloc_inodes--;
1642 		if (!root->nr_delalloc_inodes) {
1643 			spin_lock(&fs_info->delalloc_root_lock);
1644 			BUG_ON(list_empty(&root->delalloc_root));
1645 			list_del_init(&root->delalloc_root);
1646 			spin_unlock(&fs_info->delalloc_root_lock);
1647 		}
1648 	}
1649 	spin_unlock(&root->delalloc_lock);
1650 }
1651 
1652 /*
1653  * extent_io.c set_bit_hook, used to track delayed allocation
1654  * bytes in this file, and to maintain the list of inodes that
1655  * have pending delalloc work to be done.
1656  */
1657 static void btrfs_set_bit_hook(struct inode *inode,
1658 			       struct extent_state *state, unsigned *bits)
1659 {
1660 
1661 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1662 
1663 	if ((*bits & EXTENT_DEFRAG) && !(*bits & EXTENT_DELALLOC))
1664 		WARN_ON(1);
1665 	/*
1666 	 * set_bit and clear bit hooks normally require _irqsave/restore
1667 	 * but in this case, we are only testing for the DELALLOC
1668 	 * bit, which is only set or cleared with irqs on
1669 	 */
1670 	if (!(state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1671 		struct btrfs_root *root = BTRFS_I(inode)->root;
1672 		u64 len = state->end + 1 - state->start;
1673 		bool do_list = !btrfs_is_free_space_inode(inode);
1674 
1675 		if (*bits & EXTENT_FIRST_DELALLOC) {
1676 			*bits &= ~EXTENT_FIRST_DELALLOC;
1677 		} else {
1678 			spin_lock(&BTRFS_I(inode)->lock);
1679 			BTRFS_I(inode)->outstanding_extents++;
1680 			spin_unlock(&BTRFS_I(inode)->lock);
1681 		}
1682 
1683 		/* For sanity tests */
1684 		if (btrfs_is_testing(fs_info))
1685 			return;
1686 
1687 		__percpu_counter_add(&fs_info->delalloc_bytes, len,
1688 				     fs_info->delalloc_batch);
1689 		spin_lock(&BTRFS_I(inode)->lock);
1690 		BTRFS_I(inode)->delalloc_bytes += len;
1691 		if (*bits & EXTENT_DEFRAG)
1692 			BTRFS_I(inode)->defrag_bytes += len;
1693 		if (do_list && !test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1694 					 &BTRFS_I(inode)->runtime_flags))
1695 			btrfs_add_delalloc_inodes(root, inode);
1696 		spin_unlock(&BTRFS_I(inode)->lock);
1697 	}
1698 }
1699 
1700 /*
1701  * extent_io.c clear_bit_hook, see set_bit_hook for why
1702  */
1703 static void btrfs_clear_bit_hook(struct inode *inode,
1704 				 struct extent_state *state,
1705 				 unsigned *bits)
1706 {
1707 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1708 	u64 len = state->end + 1 - state->start;
1709 	u32 num_extents = count_max_extents(len);
1710 
1711 	spin_lock(&BTRFS_I(inode)->lock);
1712 	if ((state->state & EXTENT_DEFRAG) && (*bits & EXTENT_DEFRAG))
1713 		BTRFS_I(inode)->defrag_bytes -= len;
1714 	spin_unlock(&BTRFS_I(inode)->lock);
1715 
1716 	/*
1717 	 * set_bit and clear bit hooks normally require _irqsave/restore
1718 	 * but in this case, we are only testing for the DELALLOC
1719 	 * bit, which is only set or cleared with irqs on
1720 	 */
1721 	if ((state->state & EXTENT_DELALLOC) && (*bits & EXTENT_DELALLOC)) {
1722 		struct btrfs_root *root = BTRFS_I(inode)->root;
1723 		bool do_list = !btrfs_is_free_space_inode(inode);
1724 
1725 		if (*bits & EXTENT_FIRST_DELALLOC) {
1726 			*bits &= ~EXTENT_FIRST_DELALLOC;
1727 		} else if (!(*bits & EXTENT_DO_ACCOUNTING)) {
1728 			spin_lock(&BTRFS_I(inode)->lock);
1729 			BTRFS_I(inode)->outstanding_extents -= num_extents;
1730 			spin_unlock(&BTRFS_I(inode)->lock);
1731 		}
1732 
1733 		/*
1734 		 * We don't reserve metadata space for space cache inodes so we
1735 		 * don't need to call dellalloc_release_metadata if there is an
1736 		 * error.
1737 		 */
1738 		if (*bits & EXTENT_DO_ACCOUNTING &&
1739 		    root != fs_info->tree_root)
1740 			btrfs_delalloc_release_metadata(inode, len);
1741 
1742 		/* For sanity tests. */
1743 		if (btrfs_is_testing(fs_info))
1744 			return;
1745 
1746 		if (root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
1747 		    && do_list && !(state->state & EXTENT_NORESERVE)
1748 		    && (*bits & (EXTENT_DO_ACCOUNTING |
1749 		    EXTENT_CLEAR_DATA_RESV)))
1750 			btrfs_free_reserved_data_space_noquota(inode,
1751 					state->start, len);
1752 
1753 		__percpu_counter_add(&fs_info->delalloc_bytes, -len,
1754 				     fs_info->delalloc_batch);
1755 		spin_lock(&BTRFS_I(inode)->lock);
1756 		BTRFS_I(inode)->delalloc_bytes -= len;
1757 		if (do_list && BTRFS_I(inode)->delalloc_bytes == 0 &&
1758 		    test_bit(BTRFS_INODE_IN_DELALLOC_LIST,
1759 			     &BTRFS_I(inode)->runtime_flags))
1760 			btrfs_del_delalloc_inode(root, inode);
1761 		spin_unlock(&BTRFS_I(inode)->lock);
1762 	}
1763 }
1764 
1765 /*
1766  * extent_io.c merge_bio_hook, this must check the chunk tree to make sure
1767  * we don't create bios that span stripes or chunks
1768  *
1769  * return 1 if page cannot be merged to bio
1770  * return 0 if page can be merged to bio
1771  * return error otherwise
1772  */
1773 int btrfs_merge_bio_hook(struct page *page, unsigned long offset,
1774 			 size_t size, struct bio *bio,
1775 			 unsigned long bio_flags)
1776 {
1777 	struct inode *inode = page->mapping->host;
1778 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1779 	u64 logical = (u64)bio->bi_iter.bi_sector << 9;
1780 	u64 length = 0;
1781 	u64 map_length;
1782 	int ret;
1783 
1784 	if (bio_flags & EXTENT_BIO_COMPRESSED)
1785 		return 0;
1786 
1787 	length = bio->bi_iter.bi_size;
1788 	map_length = length;
1789 	ret = btrfs_map_block(fs_info, btrfs_op(bio), logical, &map_length,
1790 			      NULL, 0);
1791 	if (ret < 0)
1792 		return ret;
1793 	if (map_length < length + size)
1794 		return 1;
1795 	return 0;
1796 }
1797 
1798 /*
1799  * in order to insert checksums into the metadata in large chunks,
1800  * we wait until bio submission time.   All the pages in the bio are
1801  * checksummed and sums are attached onto the ordered extent record.
1802  *
1803  * At IO completion time the cums attached on the ordered extent record
1804  * are inserted into the btree
1805  */
1806 static int __btrfs_submit_bio_start(struct inode *inode, struct bio *bio,
1807 				    int mirror_num, unsigned long bio_flags,
1808 				    u64 bio_offset)
1809 {
1810 	int ret = 0;
1811 
1812 	ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1813 	BUG_ON(ret); /* -ENOMEM */
1814 	return 0;
1815 }
1816 
1817 /*
1818  * in order to insert checksums into the metadata in large chunks,
1819  * we wait until bio submission time.   All the pages in the bio are
1820  * checksummed and sums are attached onto the ordered extent record.
1821  *
1822  * At IO completion time the cums attached on the ordered extent record
1823  * are inserted into the btree
1824  */
1825 static int __btrfs_submit_bio_done(struct inode *inode, struct bio *bio,
1826 			  int mirror_num, unsigned long bio_flags,
1827 			  u64 bio_offset)
1828 {
1829 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1830 	int ret;
1831 
1832 	ret = btrfs_map_bio(fs_info, bio, mirror_num, 1);
1833 	if (ret) {
1834 		bio->bi_error = ret;
1835 		bio_endio(bio);
1836 	}
1837 	return ret;
1838 }
1839 
1840 /*
1841  * extent_io.c submission hook. This does the right thing for csum calculation
1842  * on write, or reading the csums from the tree before a read
1843  */
1844 static int btrfs_submit_bio_hook(struct inode *inode, struct bio *bio,
1845 			  int mirror_num, unsigned long bio_flags,
1846 			  u64 bio_offset)
1847 {
1848 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
1849 	struct btrfs_root *root = BTRFS_I(inode)->root;
1850 	enum btrfs_wq_endio_type metadata = BTRFS_WQ_ENDIO_DATA;
1851 	int ret = 0;
1852 	int skip_sum;
1853 	int async = !atomic_read(&BTRFS_I(inode)->sync_writers);
1854 
1855 	skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
1856 
1857 	if (btrfs_is_free_space_inode(inode))
1858 		metadata = BTRFS_WQ_ENDIO_FREE_SPACE;
1859 
1860 	if (bio_op(bio) != REQ_OP_WRITE) {
1861 		ret = btrfs_bio_wq_end_io(fs_info, bio, metadata);
1862 		if (ret)
1863 			goto out;
1864 
1865 		if (bio_flags & EXTENT_BIO_COMPRESSED) {
1866 			ret = btrfs_submit_compressed_read(inode, bio,
1867 							   mirror_num,
1868 							   bio_flags);
1869 			goto out;
1870 		} else if (!skip_sum) {
1871 			ret = btrfs_lookup_bio_sums(inode, bio, NULL);
1872 			if (ret)
1873 				goto out;
1874 		}
1875 		goto mapit;
1876 	} else if (async && !skip_sum) {
1877 		/* csum items have already been cloned */
1878 		if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID)
1879 			goto mapit;
1880 		/* we're doing a write, do the async checksumming */
1881 		ret = btrfs_wq_submit_bio(fs_info, inode, bio, mirror_num,
1882 					  bio_flags, bio_offset,
1883 					  __btrfs_submit_bio_start,
1884 					  __btrfs_submit_bio_done);
1885 		goto out;
1886 	} else if (!skip_sum) {
1887 		ret = btrfs_csum_one_bio(inode, bio, 0, 0);
1888 		if (ret)
1889 			goto out;
1890 	}
1891 
1892 mapit:
1893 	ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
1894 
1895 out:
1896 	if (ret < 0) {
1897 		bio->bi_error = ret;
1898 		bio_endio(bio);
1899 	}
1900 	return ret;
1901 }
1902 
1903 /*
1904  * given a list of ordered sums record them in the inode.  This happens
1905  * at IO completion time based on sums calculated at bio submission time.
1906  */
1907 static noinline int add_pending_csums(struct btrfs_trans_handle *trans,
1908 			     struct inode *inode, struct list_head *list)
1909 {
1910 	struct btrfs_ordered_sum *sum;
1911 
1912 	list_for_each_entry(sum, list, list) {
1913 		trans->adding_csums = 1;
1914 		btrfs_csum_file_blocks(trans,
1915 		       BTRFS_I(inode)->root->fs_info->csum_root, sum);
1916 		trans->adding_csums = 0;
1917 	}
1918 	return 0;
1919 }
1920 
1921 int btrfs_set_extent_delalloc(struct inode *inode, u64 start, u64 end,
1922 			      struct extent_state **cached_state, int dedupe)
1923 {
1924 	WARN_ON((end & (PAGE_SIZE - 1)) == 0);
1925 	return set_extent_delalloc(&BTRFS_I(inode)->io_tree, start, end,
1926 				   cached_state);
1927 }
1928 
1929 /* see btrfs_writepage_start_hook for details on why this is required */
1930 struct btrfs_writepage_fixup {
1931 	struct page *page;
1932 	struct btrfs_work work;
1933 };
1934 
1935 static void btrfs_writepage_fixup_worker(struct btrfs_work *work)
1936 {
1937 	struct btrfs_writepage_fixup *fixup;
1938 	struct btrfs_ordered_extent *ordered;
1939 	struct extent_state *cached_state = NULL;
1940 	struct page *page;
1941 	struct inode *inode;
1942 	u64 page_start;
1943 	u64 page_end;
1944 	int ret;
1945 
1946 	fixup = container_of(work, struct btrfs_writepage_fixup, work);
1947 	page = fixup->page;
1948 again:
1949 	lock_page(page);
1950 	if (!page->mapping || !PageDirty(page) || !PageChecked(page)) {
1951 		ClearPageChecked(page);
1952 		goto out_page;
1953 	}
1954 
1955 	inode = page->mapping->host;
1956 	page_start = page_offset(page);
1957 	page_end = page_offset(page) + PAGE_SIZE - 1;
1958 
1959 	lock_extent_bits(&BTRFS_I(inode)->io_tree, page_start, page_end,
1960 			 &cached_state);
1961 
1962 	/* already ordered? We're done */
1963 	if (PagePrivate2(page))
1964 		goto out;
1965 
1966 	ordered = btrfs_lookup_ordered_range(inode, page_start,
1967 					PAGE_SIZE);
1968 	if (ordered) {
1969 		unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start,
1970 				     page_end, &cached_state, GFP_NOFS);
1971 		unlock_page(page);
1972 		btrfs_start_ordered_extent(inode, ordered, 1);
1973 		btrfs_put_ordered_extent(ordered);
1974 		goto again;
1975 	}
1976 
1977 	ret = btrfs_delalloc_reserve_space(inode, page_start,
1978 					   PAGE_SIZE);
1979 	if (ret) {
1980 		mapping_set_error(page->mapping, ret);
1981 		end_extent_writepage(page, ret, page_start, page_end);
1982 		ClearPageChecked(page);
1983 		goto out;
1984 	 }
1985 
1986 	btrfs_set_extent_delalloc(inode, page_start, page_end, &cached_state,
1987 				  0);
1988 	ClearPageChecked(page);
1989 	set_page_dirty(page);
1990 out:
1991 	unlock_extent_cached(&BTRFS_I(inode)->io_tree, page_start, page_end,
1992 			     &cached_state, GFP_NOFS);
1993 out_page:
1994 	unlock_page(page);
1995 	put_page(page);
1996 	kfree(fixup);
1997 }
1998 
1999 /*
2000  * There are a few paths in the higher layers of the kernel that directly
2001  * set the page dirty bit without asking the filesystem if it is a
2002  * good idea.  This causes problems because we want to make sure COW
2003  * properly happens and the data=ordered rules are followed.
2004  *
2005  * In our case any range that doesn't have the ORDERED bit set
2006  * hasn't been properly setup for IO.  We kick off an async process
2007  * to fix it up.  The async helper will wait for ordered extents, set
2008  * the delalloc bit and make it safe to write the page.
2009  */
2010 static int btrfs_writepage_start_hook(struct page *page, u64 start, u64 end)
2011 {
2012 	struct inode *inode = page->mapping->host;
2013 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2014 	struct btrfs_writepage_fixup *fixup;
2015 
2016 	/* this page is properly in the ordered list */
2017 	if (TestClearPagePrivate2(page))
2018 		return 0;
2019 
2020 	if (PageChecked(page))
2021 		return -EAGAIN;
2022 
2023 	fixup = kzalloc(sizeof(*fixup), GFP_NOFS);
2024 	if (!fixup)
2025 		return -EAGAIN;
2026 
2027 	SetPageChecked(page);
2028 	get_page(page);
2029 	btrfs_init_work(&fixup->work, btrfs_fixup_helper,
2030 			btrfs_writepage_fixup_worker, NULL, NULL);
2031 	fixup->page = page;
2032 	btrfs_queue_work(fs_info->fixup_workers, &fixup->work);
2033 	return -EBUSY;
2034 }
2035 
2036 static int insert_reserved_file_extent(struct btrfs_trans_handle *trans,
2037 				       struct inode *inode, u64 file_pos,
2038 				       u64 disk_bytenr, u64 disk_num_bytes,
2039 				       u64 num_bytes, u64 ram_bytes,
2040 				       u8 compression, u8 encryption,
2041 				       u16 other_encoding, int extent_type)
2042 {
2043 	struct btrfs_root *root = BTRFS_I(inode)->root;
2044 	struct btrfs_file_extent_item *fi;
2045 	struct btrfs_path *path;
2046 	struct extent_buffer *leaf;
2047 	struct btrfs_key ins;
2048 	int extent_inserted = 0;
2049 	int ret;
2050 
2051 	path = btrfs_alloc_path();
2052 	if (!path)
2053 		return -ENOMEM;
2054 
2055 	/*
2056 	 * we may be replacing one extent in the tree with another.
2057 	 * The new extent is pinned in the extent map, and we don't want
2058 	 * to drop it from the cache until it is completely in the btree.
2059 	 *
2060 	 * So, tell btrfs_drop_extents to leave this extent in the cache.
2061 	 * the caller is expected to unpin it and allow it to be merged
2062 	 * with the others.
2063 	 */
2064 	ret = __btrfs_drop_extents(trans, root, inode, path, file_pos,
2065 				   file_pos + num_bytes, NULL, 0,
2066 				   1, sizeof(*fi), &extent_inserted);
2067 	if (ret)
2068 		goto out;
2069 
2070 	if (!extent_inserted) {
2071 		ins.objectid = btrfs_ino(BTRFS_I(inode));
2072 		ins.offset = file_pos;
2073 		ins.type = BTRFS_EXTENT_DATA_KEY;
2074 
2075 		path->leave_spinning = 1;
2076 		ret = btrfs_insert_empty_item(trans, root, path, &ins,
2077 					      sizeof(*fi));
2078 		if (ret)
2079 			goto out;
2080 	}
2081 	leaf = path->nodes[0];
2082 	fi = btrfs_item_ptr(leaf, path->slots[0],
2083 			    struct btrfs_file_extent_item);
2084 	btrfs_set_file_extent_generation(leaf, fi, trans->transid);
2085 	btrfs_set_file_extent_type(leaf, fi, extent_type);
2086 	btrfs_set_file_extent_disk_bytenr(leaf, fi, disk_bytenr);
2087 	btrfs_set_file_extent_disk_num_bytes(leaf, fi, disk_num_bytes);
2088 	btrfs_set_file_extent_offset(leaf, fi, 0);
2089 	btrfs_set_file_extent_num_bytes(leaf, fi, num_bytes);
2090 	btrfs_set_file_extent_ram_bytes(leaf, fi, ram_bytes);
2091 	btrfs_set_file_extent_compression(leaf, fi, compression);
2092 	btrfs_set_file_extent_encryption(leaf, fi, encryption);
2093 	btrfs_set_file_extent_other_encoding(leaf, fi, other_encoding);
2094 
2095 	btrfs_mark_buffer_dirty(leaf);
2096 	btrfs_release_path(path);
2097 
2098 	inode_add_bytes(inode, num_bytes);
2099 
2100 	ins.objectid = disk_bytenr;
2101 	ins.offset = disk_num_bytes;
2102 	ins.type = BTRFS_EXTENT_ITEM_KEY;
2103 	ret = btrfs_alloc_reserved_file_extent(trans, root->root_key.objectid,
2104 			btrfs_ino(BTRFS_I(inode)), file_pos, ram_bytes, &ins);
2105 	/*
2106 	 * Release the reserved range from inode dirty range map, as it is
2107 	 * already moved into delayed_ref_head
2108 	 */
2109 	btrfs_qgroup_release_data(inode, file_pos, ram_bytes);
2110 out:
2111 	btrfs_free_path(path);
2112 
2113 	return ret;
2114 }
2115 
2116 /* snapshot-aware defrag */
2117 struct sa_defrag_extent_backref {
2118 	struct rb_node node;
2119 	struct old_sa_defrag_extent *old;
2120 	u64 root_id;
2121 	u64 inum;
2122 	u64 file_pos;
2123 	u64 extent_offset;
2124 	u64 num_bytes;
2125 	u64 generation;
2126 };
2127 
2128 struct old_sa_defrag_extent {
2129 	struct list_head list;
2130 	struct new_sa_defrag_extent *new;
2131 
2132 	u64 extent_offset;
2133 	u64 bytenr;
2134 	u64 offset;
2135 	u64 len;
2136 	int count;
2137 };
2138 
2139 struct new_sa_defrag_extent {
2140 	struct rb_root root;
2141 	struct list_head head;
2142 	struct btrfs_path *path;
2143 	struct inode *inode;
2144 	u64 file_pos;
2145 	u64 len;
2146 	u64 bytenr;
2147 	u64 disk_len;
2148 	u8 compress_type;
2149 };
2150 
2151 static int backref_comp(struct sa_defrag_extent_backref *b1,
2152 			struct sa_defrag_extent_backref *b2)
2153 {
2154 	if (b1->root_id < b2->root_id)
2155 		return -1;
2156 	else if (b1->root_id > b2->root_id)
2157 		return 1;
2158 
2159 	if (b1->inum < b2->inum)
2160 		return -1;
2161 	else if (b1->inum > b2->inum)
2162 		return 1;
2163 
2164 	if (b1->file_pos < b2->file_pos)
2165 		return -1;
2166 	else if (b1->file_pos > b2->file_pos)
2167 		return 1;
2168 
2169 	/*
2170 	 * [------------------------------] ===> (a range of space)
2171 	 *     |<--->|   |<---->| =============> (fs/file tree A)
2172 	 * |<---------------------------->| ===> (fs/file tree B)
2173 	 *
2174 	 * A range of space can refer to two file extents in one tree while
2175 	 * refer to only one file extent in another tree.
2176 	 *
2177 	 * So we may process a disk offset more than one time(two extents in A)
2178 	 * and locate at the same extent(one extent in B), then insert two same
2179 	 * backrefs(both refer to the extent in B).
2180 	 */
2181 	return 0;
2182 }
2183 
2184 static void backref_insert(struct rb_root *root,
2185 			   struct sa_defrag_extent_backref *backref)
2186 {
2187 	struct rb_node **p = &root->rb_node;
2188 	struct rb_node *parent = NULL;
2189 	struct sa_defrag_extent_backref *entry;
2190 	int ret;
2191 
2192 	while (*p) {
2193 		parent = *p;
2194 		entry = rb_entry(parent, struct sa_defrag_extent_backref, node);
2195 
2196 		ret = backref_comp(backref, entry);
2197 		if (ret < 0)
2198 			p = &(*p)->rb_left;
2199 		else
2200 			p = &(*p)->rb_right;
2201 	}
2202 
2203 	rb_link_node(&backref->node, parent, p);
2204 	rb_insert_color(&backref->node, root);
2205 }
2206 
2207 /*
2208  * Note the backref might has changed, and in this case we just return 0.
2209  */
2210 static noinline int record_one_backref(u64 inum, u64 offset, u64 root_id,
2211 				       void *ctx)
2212 {
2213 	struct btrfs_file_extent_item *extent;
2214 	struct old_sa_defrag_extent *old = ctx;
2215 	struct new_sa_defrag_extent *new = old->new;
2216 	struct btrfs_path *path = new->path;
2217 	struct btrfs_key key;
2218 	struct btrfs_root *root;
2219 	struct sa_defrag_extent_backref *backref;
2220 	struct extent_buffer *leaf;
2221 	struct inode *inode = new->inode;
2222 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2223 	int slot;
2224 	int ret;
2225 	u64 extent_offset;
2226 	u64 num_bytes;
2227 
2228 	if (BTRFS_I(inode)->root->root_key.objectid == root_id &&
2229 	    inum == btrfs_ino(BTRFS_I(inode)))
2230 		return 0;
2231 
2232 	key.objectid = root_id;
2233 	key.type = BTRFS_ROOT_ITEM_KEY;
2234 	key.offset = (u64)-1;
2235 
2236 	root = btrfs_read_fs_root_no_name(fs_info, &key);
2237 	if (IS_ERR(root)) {
2238 		if (PTR_ERR(root) == -ENOENT)
2239 			return 0;
2240 		WARN_ON(1);
2241 		btrfs_debug(fs_info, "inum=%llu, offset=%llu, root_id=%llu",
2242 			 inum, offset, root_id);
2243 		return PTR_ERR(root);
2244 	}
2245 
2246 	key.objectid = inum;
2247 	key.type = BTRFS_EXTENT_DATA_KEY;
2248 	if (offset > (u64)-1 << 32)
2249 		key.offset = 0;
2250 	else
2251 		key.offset = offset;
2252 
2253 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2254 	if (WARN_ON(ret < 0))
2255 		return ret;
2256 	ret = 0;
2257 
2258 	while (1) {
2259 		cond_resched();
2260 
2261 		leaf = path->nodes[0];
2262 		slot = path->slots[0];
2263 
2264 		if (slot >= btrfs_header_nritems(leaf)) {
2265 			ret = btrfs_next_leaf(root, path);
2266 			if (ret < 0) {
2267 				goto out;
2268 			} else if (ret > 0) {
2269 				ret = 0;
2270 				goto out;
2271 			}
2272 			continue;
2273 		}
2274 
2275 		path->slots[0]++;
2276 
2277 		btrfs_item_key_to_cpu(leaf, &key, slot);
2278 
2279 		if (key.objectid > inum)
2280 			goto out;
2281 
2282 		if (key.objectid < inum || key.type != BTRFS_EXTENT_DATA_KEY)
2283 			continue;
2284 
2285 		extent = btrfs_item_ptr(leaf, slot,
2286 					struct btrfs_file_extent_item);
2287 
2288 		if (btrfs_file_extent_disk_bytenr(leaf, extent) != old->bytenr)
2289 			continue;
2290 
2291 		/*
2292 		 * 'offset' refers to the exact key.offset,
2293 		 * NOT the 'offset' field in btrfs_extent_data_ref, ie.
2294 		 * (key.offset - extent_offset).
2295 		 */
2296 		if (key.offset != offset)
2297 			continue;
2298 
2299 		extent_offset = btrfs_file_extent_offset(leaf, extent);
2300 		num_bytes = btrfs_file_extent_num_bytes(leaf, extent);
2301 
2302 		if (extent_offset >= old->extent_offset + old->offset +
2303 		    old->len || extent_offset + num_bytes <=
2304 		    old->extent_offset + old->offset)
2305 			continue;
2306 		break;
2307 	}
2308 
2309 	backref = kmalloc(sizeof(*backref), GFP_NOFS);
2310 	if (!backref) {
2311 		ret = -ENOENT;
2312 		goto out;
2313 	}
2314 
2315 	backref->root_id = root_id;
2316 	backref->inum = inum;
2317 	backref->file_pos = offset;
2318 	backref->num_bytes = num_bytes;
2319 	backref->extent_offset = extent_offset;
2320 	backref->generation = btrfs_file_extent_generation(leaf, extent);
2321 	backref->old = old;
2322 	backref_insert(&new->root, backref);
2323 	old->count++;
2324 out:
2325 	btrfs_release_path(path);
2326 	WARN_ON(ret);
2327 	return ret;
2328 }
2329 
2330 static noinline bool record_extent_backrefs(struct btrfs_path *path,
2331 				   struct new_sa_defrag_extent *new)
2332 {
2333 	struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2334 	struct old_sa_defrag_extent *old, *tmp;
2335 	int ret;
2336 
2337 	new->path = path;
2338 
2339 	list_for_each_entry_safe(old, tmp, &new->head, list) {
2340 		ret = iterate_inodes_from_logical(old->bytenr +
2341 						  old->extent_offset, fs_info,
2342 						  path, record_one_backref,
2343 						  old);
2344 		if (ret < 0 && ret != -ENOENT)
2345 			return false;
2346 
2347 		/* no backref to be processed for this extent */
2348 		if (!old->count) {
2349 			list_del(&old->list);
2350 			kfree(old);
2351 		}
2352 	}
2353 
2354 	if (list_empty(&new->head))
2355 		return false;
2356 
2357 	return true;
2358 }
2359 
2360 static int relink_is_mergable(struct extent_buffer *leaf,
2361 			      struct btrfs_file_extent_item *fi,
2362 			      struct new_sa_defrag_extent *new)
2363 {
2364 	if (btrfs_file_extent_disk_bytenr(leaf, fi) != new->bytenr)
2365 		return 0;
2366 
2367 	if (btrfs_file_extent_type(leaf, fi) != BTRFS_FILE_EXTENT_REG)
2368 		return 0;
2369 
2370 	if (btrfs_file_extent_compression(leaf, fi) != new->compress_type)
2371 		return 0;
2372 
2373 	if (btrfs_file_extent_encryption(leaf, fi) ||
2374 	    btrfs_file_extent_other_encoding(leaf, fi))
2375 		return 0;
2376 
2377 	return 1;
2378 }
2379 
2380 /*
2381  * Note the backref might has changed, and in this case we just return 0.
2382  */
2383 static noinline int relink_extent_backref(struct btrfs_path *path,
2384 				 struct sa_defrag_extent_backref *prev,
2385 				 struct sa_defrag_extent_backref *backref)
2386 {
2387 	struct btrfs_file_extent_item *extent;
2388 	struct btrfs_file_extent_item *item;
2389 	struct btrfs_ordered_extent *ordered;
2390 	struct btrfs_trans_handle *trans;
2391 	struct btrfs_root *root;
2392 	struct btrfs_key key;
2393 	struct extent_buffer *leaf;
2394 	struct old_sa_defrag_extent *old = backref->old;
2395 	struct new_sa_defrag_extent *new = old->new;
2396 	struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2397 	struct inode *inode;
2398 	struct extent_state *cached = NULL;
2399 	int ret = 0;
2400 	u64 start;
2401 	u64 len;
2402 	u64 lock_start;
2403 	u64 lock_end;
2404 	bool merge = false;
2405 	int index;
2406 
2407 	if (prev && prev->root_id == backref->root_id &&
2408 	    prev->inum == backref->inum &&
2409 	    prev->file_pos + prev->num_bytes == backref->file_pos)
2410 		merge = true;
2411 
2412 	/* step 1: get root */
2413 	key.objectid = backref->root_id;
2414 	key.type = BTRFS_ROOT_ITEM_KEY;
2415 	key.offset = (u64)-1;
2416 
2417 	index = srcu_read_lock(&fs_info->subvol_srcu);
2418 
2419 	root = btrfs_read_fs_root_no_name(fs_info, &key);
2420 	if (IS_ERR(root)) {
2421 		srcu_read_unlock(&fs_info->subvol_srcu, index);
2422 		if (PTR_ERR(root) == -ENOENT)
2423 			return 0;
2424 		return PTR_ERR(root);
2425 	}
2426 
2427 	if (btrfs_root_readonly(root)) {
2428 		srcu_read_unlock(&fs_info->subvol_srcu, index);
2429 		return 0;
2430 	}
2431 
2432 	/* step 2: get inode */
2433 	key.objectid = backref->inum;
2434 	key.type = BTRFS_INODE_ITEM_KEY;
2435 	key.offset = 0;
2436 
2437 	inode = btrfs_iget(fs_info->sb, &key, root, NULL);
2438 	if (IS_ERR(inode)) {
2439 		srcu_read_unlock(&fs_info->subvol_srcu, index);
2440 		return 0;
2441 	}
2442 
2443 	srcu_read_unlock(&fs_info->subvol_srcu, index);
2444 
2445 	/* step 3: relink backref */
2446 	lock_start = backref->file_pos;
2447 	lock_end = backref->file_pos + backref->num_bytes - 1;
2448 	lock_extent_bits(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2449 			 &cached);
2450 
2451 	ordered = btrfs_lookup_first_ordered_extent(inode, lock_end);
2452 	if (ordered) {
2453 		btrfs_put_ordered_extent(ordered);
2454 		goto out_unlock;
2455 	}
2456 
2457 	trans = btrfs_join_transaction(root);
2458 	if (IS_ERR(trans)) {
2459 		ret = PTR_ERR(trans);
2460 		goto out_unlock;
2461 	}
2462 
2463 	key.objectid = backref->inum;
2464 	key.type = BTRFS_EXTENT_DATA_KEY;
2465 	key.offset = backref->file_pos;
2466 
2467 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2468 	if (ret < 0) {
2469 		goto out_free_path;
2470 	} else if (ret > 0) {
2471 		ret = 0;
2472 		goto out_free_path;
2473 	}
2474 
2475 	extent = btrfs_item_ptr(path->nodes[0], path->slots[0],
2476 				struct btrfs_file_extent_item);
2477 
2478 	if (btrfs_file_extent_generation(path->nodes[0], extent) !=
2479 	    backref->generation)
2480 		goto out_free_path;
2481 
2482 	btrfs_release_path(path);
2483 
2484 	start = backref->file_pos;
2485 	if (backref->extent_offset < old->extent_offset + old->offset)
2486 		start += old->extent_offset + old->offset -
2487 			 backref->extent_offset;
2488 
2489 	len = min(backref->extent_offset + backref->num_bytes,
2490 		  old->extent_offset + old->offset + old->len);
2491 	len -= max(backref->extent_offset, old->extent_offset + old->offset);
2492 
2493 	ret = btrfs_drop_extents(trans, root, inode, start,
2494 				 start + len, 1);
2495 	if (ret)
2496 		goto out_free_path;
2497 again:
2498 	key.objectid = btrfs_ino(BTRFS_I(inode));
2499 	key.type = BTRFS_EXTENT_DATA_KEY;
2500 	key.offset = start;
2501 
2502 	path->leave_spinning = 1;
2503 	if (merge) {
2504 		struct btrfs_file_extent_item *fi;
2505 		u64 extent_len;
2506 		struct btrfs_key found_key;
2507 
2508 		ret = btrfs_search_slot(trans, root, &key, path, 0, 1);
2509 		if (ret < 0)
2510 			goto out_free_path;
2511 
2512 		path->slots[0]--;
2513 		leaf = path->nodes[0];
2514 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
2515 
2516 		fi = btrfs_item_ptr(leaf, path->slots[0],
2517 				    struct btrfs_file_extent_item);
2518 		extent_len = btrfs_file_extent_num_bytes(leaf, fi);
2519 
2520 		if (extent_len + found_key.offset == start &&
2521 		    relink_is_mergable(leaf, fi, new)) {
2522 			btrfs_set_file_extent_num_bytes(leaf, fi,
2523 							extent_len + len);
2524 			btrfs_mark_buffer_dirty(leaf);
2525 			inode_add_bytes(inode, len);
2526 
2527 			ret = 1;
2528 			goto out_free_path;
2529 		} else {
2530 			merge = false;
2531 			btrfs_release_path(path);
2532 			goto again;
2533 		}
2534 	}
2535 
2536 	ret = btrfs_insert_empty_item(trans, root, path, &key,
2537 					sizeof(*extent));
2538 	if (ret) {
2539 		btrfs_abort_transaction(trans, ret);
2540 		goto out_free_path;
2541 	}
2542 
2543 	leaf = path->nodes[0];
2544 	item = btrfs_item_ptr(leaf, path->slots[0],
2545 				struct btrfs_file_extent_item);
2546 	btrfs_set_file_extent_disk_bytenr(leaf, item, new->bytenr);
2547 	btrfs_set_file_extent_disk_num_bytes(leaf, item, new->disk_len);
2548 	btrfs_set_file_extent_offset(leaf, item, start - new->file_pos);
2549 	btrfs_set_file_extent_num_bytes(leaf, item, len);
2550 	btrfs_set_file_extent_ram_bytes(leaf, item, new->len);
2551 	btrfs_set_file_extent_generation(leaf, item, trans->transid);
2552 	btrfs_set_file_extent_type(leaf, item, BTRFS_FILE_EXTENT_REG);
2553 	btrfs_set_file_extent_compression(leaf, item, new->compress_type);
2554 	btrfs_set_file_extent_encryption(leaf, item, 0);
2555 	btrfs_set_file_extent_other_encoding(leaf, item, 0);
2556 
2557 	btrfs_mark_buffer_dirty(leaf);
2558 	inode_add_bytes(inode, len);
2559 	btrfs_release_path(path);
2560 
2561 	ret = btrfs_inc_extent_ref(trans, fs_info, new->bytenr,
2562 			new->disk_len, 0,
2563 			backref->root_id, backref->inum,
2564 			new->file_pos);	/* start - extent_offset */
2565 	if (ret) {
2566 		btrfs_abort_transaction(trans, ret);
2567 		goto out_free_path;
2568 	}
2569 
2570 	ret = 1;
2571 out_free_path:
2572 	btrfs_release_path(path);
2573 	path->leave_spinning = 0;
2574 	btrfs_end_transaction(trans);
2575 out_unlock:
2576 	unlock_extent_cached(&BTRFS_I(inode)->io_tree, lock_start, lock_end,
2577 			     &cached, GFP_NOFS);
2578 	iput(inode);
2579 	return ret;
2580 }
2581 
2582 static void free_sa_defrag_extent(struct new_sa_defrag_extent *new)
2583 {
2584 	struct old_sa_defrag_extent *old, *tmp;
2585 
2586 	if (!new)
2587 		return;
2588 
2589 	list_for_each_entry_safe(old, tmp, &new->head, list) {
2590 		kfree(old);
2591 	}
2592 	kfree(new);
2593 }
2594 
2595 static void relink_file_extents(struct new_sa_defrag_extent *new)
2596 {
2597 	struct btrfs_fs_info *fs_info = btrfs_sb(new->inode->i_sb);
2598 	struct btrfs_path *path;
2599 	struct sa_defrag_extent_backref *backref;
2600 	struct sa_defrag_extent_backref *prev = NULL;
2601 	struct inode *inode;
2602 	struct btrfs_root *root;
2603 	struct rb_node *node;
2604 	int ret;
2605 
2606 	inode = new->inode;
2607 	root = BTRFS_I(inode)->root;
2608 
2609 	path = btrfs_alloc_path();
2610 	if (!path)
2611 		return;
2612 
2613 	if (!record_extent_backrefs(path, new)) {
2614 		btrfs_free_path(path);
2615 		goto out;
2616 	}
2617 	btrfs_release_path(path);
2618 
2619 	while (1) {
2620 		node = rb_first(&new->root);
2621 		if (!node)
2622 			break;
2623 		rb_erase(node, &new->root);
2624 
2625 		backref = rb_entry(node, struct sa_defrag_extent_backref, node);
2626 
2627 		ret = relink_extent_backref(path, prev, backref);
2628 		WARN_ON(ret < 0);
2629 
2630 		kfree(prev);
2631 
2632 		if (ret == 1)
2633 			prev = backref;
2634 		else
2635 			prev = NULL;
2636 		cond_resched();
2637 	}
2638 	kfree(prev);
2639 
2640 	btrfs_free_path(path);
2641 out:
2642 	free_sa_defrag_extent(new);
2643 
2644 	atomic_dec(&fs_info->defrag_running);
2645 	wake_up(&fs_info->transaction_wait);
2646 }
2647 
2648 static struct new_sa_defrag_extent *
2649 record_old_file_extents(struct inode *inode,
2650 			struct btrfs_ordered_extent *ordered)
2651 {
2652 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2653 	struct btrfs_root *root = BTRFS_I(inode)->root;
2654 	struct btrfs_path *path;
2655 	struct btrfs_key key;
2656 	struct old_sa_defrag_extent *old;
2657 	struct new_sa_defrag_extent *new;
2658 	int ret;
2659 
2660 	new = kmalloc(sizeof(*new), GFP_NOFS);
2661 	if (!new)
2662 		return NULL;
2663 
2664 	new->inode = inode;
2665 	new->file_pos = ordered->file_offset;
2666 	new->len = ordered->len;
2667 	new->bytenr = ordered->start;
2668 	new->disk_len = ordered->disk_len;
2669 	new->compress_type = ordered->compress_type;
2670 	new->root = RB_ROOT;
2671 	INIT_LIST_HEAD(&new->head);
2672 
2673 	path = btrfs_alloc_path();
2674 	if (!path)
2675 		goto out_kfree;
2676 
2677 	key.objectid = btrfs_ino(BTRFS_I(inode));
2678 	key.type = BTRFS_EXTENT_DATA_KEY;
2679 	key.offset = new->file_pos;
2680 
2681 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
2682 	if (ret < 0)
2683 		goto out_free_path;
2684 	if (ret > 0 && path->slots[0] > 0)
2685 		path->slots[0]--;
2686 
2687 	/* find out all the old extents for the file range */
2688 	while (1) {
2689 		struct btrfs_file_extent_item *extent;
2690 		struct extent_buffer *l;
2691 		int slot;
2692 		u64 num_bytes;
2693 		u64 offset;
2694 		u64 end;
2695 		u64 disk_bytenr;
2696 		u64 extent_offset;
2697 
2698 		l = path->nodes[0];
2699 		slot = path->slots[0];
2700 
2701 		if (slot >= btrfs_header_nritems(l)) {
2702 			ret = btrfs_next_leaf(root, path);
2703 			if (ret < 0)
2704 				goto out_free_path;
2705 			else if (ret > 0)
2706 				break;
2707 			continue;
2708 		}
2709 
2710 		btrfs_item_key_to_cpu(l, &key, slot);
2711 
2712 		if (key.objectid != btrfs_ino(BTRFS_I(inode)))
2713 			break;
2714 		if (key.type != BTRFS_EXTENT_DATA_KEY)
2715 			break;
2716 		if (key.offset >= new->file_pos + new->len)
2717 			break;
2718 
2719 		extent = btrfs_item_ptr(l, slot, struct btrfs_file_extent_item);
2720 
2721 		num_bytes = btrfs_file_extent_num_bytes(l, extent);
2722 		if (key.offset + num_bytes < new->file_pos)
2723 			goto next;
2724 
2725 		disk_bytenr = btrfs_file_extent_disk_bytenr(l, extent);
2726 		if (!disk_bytenr)
2727 			goto next;
2728 
2729 		extent_offset = btrfs_file_extent_offset(l, extent);
2730 
2731 		old = kmalloc(sizeof(*old), GFP_NOFS);
2732 		if (!old)
2733 			goto out_free_path;
2734 
2735 		offset = max(new->file_pos, key.offset);
2736 		end = min(new->file_pos + new->len, key.offset + num_bytes);
2737 
2738 		old->bytenr = disk_bytenr;
2739 		old->extent_offset = extent_offset;
2740 		old->offset = offset - key.offset;
2741 		old->len = end - offset;
2742 		old->new = new;
2743 		old->count = 0;
2744 		list_add_tail(&old->list, &new->head);
2745 next:
2746 		path->slots[0]++;
2747 		cond_resched();
2748 	}
2749 
2750 	btrfs_free_path(path);
2751 	atomic_inc(&fs_info->defrag_running);
2752 
2753 	return new;
2754 
2755 out_free_path:
2756 	btrfs_free_path(path);
2757 out_kfree:
2758 	free_sa_defrag_extent(new);
2759 	return NULL;
2760 }
2761 
2762 static void btrfs_release_delalloc_bytes(struct btrfs_fs_info *fs_info,
2763 					 u64 start, u64 len)
2764 {
2765 	struct btrfs_block_group_cache *cache;
2766 
2767 	cache = btrfs_lookup_block_group(fs_info, start);
2768 	ASSERT(cache);
2769 
2770 	spin_lock(&cache->lock);
2771 	cache->delalloc_bytes -= len;
2772 	spin_unlock(&cache->lock);
2773 
2774 	btrfs_put_block_group(cache);
2775 }
2776 
2777 /* as ordered data IO finishes, this gets called so we can finish
2778  * an ordered extent if the range of bytes in the file it covers are
2779  * fully written.
2780  */
2781 static int btrfs_finish_ordered_io(struct btrfs_ordered_extent *ordered_extent)
2782 {
2783 	struct inode *inode = ordered_extent->inode;
2784 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2785 	struct btrfs_root *root = BTRFS_I(inode)->root;
2786 	struct btrfs_trans_handle *trans = NULL;
2787 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
2788 	struct extent_state *cached_state = NULL;
2789 	struct new_sa_defrag_extent *new = NULL;
2790 	int compress_type = 0;
2791 	int ret = 0;
2792 	u64 logical_len = ordered_extent->len;
2793 	bool nolock;
2794 	bool truncated = false;
2795 
2796 	nolock = btrfs_is_free_space_inode(inode);
2797 
2798 	if (test_bit(BTRFS_ORDERED_IOERR, &ordered_extent->flags)) {
2799 		ret = -EIO;
2800 		goto out;
2801 	}
2802 
2803 	btrfs_free_io_failure_record(inode, ordered_extent->file_offset,
2804 				     ordered_extent->file_offset +
2805 				     ordered_extent->len - 1);
2806 
2807 	if (test_bit(BTRFS_ORDERED_TRUNCATED, &ordered_extent->flags)) {
2808 		truncated = true;
2809 		logical_len = ordered_extent->truncated_len;
2810 		/* Truncated the entire extent, don't bother adding */
2811 		if (!logical_len)
2812 			goto out;
2813 	}
2814 
2815 	if (test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags)) {
2816 		BUG_ON(!list_empty(&ordered_extent->list)); /* Logic error */
2817 
2818 		/*
2819 		 * For mwrite(mmap + memset to write) case, we still reserve
2820 		 * space for NOCOW range.
2821 		 * As NOCOW won't cause a new delayed ref, just free the space
2822 		 */
2823 		btrfs_qgroup_free_data(inode, ordered_extent->file_offset,
2824 				       ordered_extent->len);
2825 		btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2826 		if (nolock)
2827 			trans = btrfs_join_transaction_nolock(root);
2828 		else
2829 			trans = btrfs_join_transaction(root);
2830 		if (IS_ERR(trans)) {
2831 			ret = PTR_ERR(trans);
2832 			trans = NULL;
2833 			goto out;
2834 		}
2835 		trans->block_rsv = &fs_info->delalloc_block_rsv;
2836 		ret = btrfs_update_inode_fallback(trans, root, inode);
2837 		if (ret) /* -ENOMEM or corruption */
2838 			btrfs_abort_transaction(trans, ret);
2839 		goto out;
2840 	}
2841 
2842 	lock_extent_bits(io_tree, ordered_extent->file_offset,
2843 			 ordered_extent->file_offset + ordered_extent->len - 1,
2844 			 &cached_state);
2845 
2846 	ret = test_range_bit(io_tree, ordered_extent->file_offset,
2847 			ordered_extent->file_offset + ordered_extent->len - 1,
2848 			EXTENT_DEFRAG, 1, cached_state);
2849 	if (ret) {
2850 		u64 last_snapshot = btrfs_root_last_snapshot(&root->root_item);
2851 		if (0 && last_snapshot >= BTRFS_I(inode)->generation)
2852 			/* the inode is shared */
2853 			new = record_old_file_extents(inode, ordered_extent);
2854 
2855 		clear_extent_bit(io_tree, ordered_extent->file_offset,
2856 			ordered_extent->file_offset + ordered_extent->len - 1,
2857 			EXTENT_DEFRAG, 0, 0, &cached_state, GFP_NOFS);
2858 	}
2859 
2860 	if (nolock)
2861 		trans = btrfs_join_transaction_nolock(root);
2862 	else
2863 		trans = btrfs_join_transaction(root);
2864 	if (IS_ERR(trans)) {
2865 		ret = PTR_ERR(trans);
2866 		trans = NULL;
2867 		goto out_unlock;
2868 	}
2869 
2870 	trans->block_rsv = &fs_info->delalloc_block_rsv;
2871 
2872 	if (test_bit(BTRFS_ORDERED_COMPRESSED, &ordered_extent->flags))
2873 		compress_type = ordered_extent->compress_type;
2874 	if (test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags)) {
2875 		BUG_ON(compress_type);
2876 		ret = btrfs_mark_extent_written(trans, inode,
2877 						ordered_extent->file_offset,
2878 						ordered_extent->file_offset +
2879 						logical_len);
2880 	} else {
2881 		BUG_ON(root == fs_info->tree_root);
2882 		ret = insert_reserved_file_extent(trans, inode,
2883 						ordered_extent->file_offset,
2884 						ordered_extent->start,
2885 						ordered_extent->disk_len,
2886 						logical_len, logical_len,
2887 						compress_type, 0, 0,
2888 						BTRFS_FILE_EXTENT_REG);
2889 		if (!ret)
2890 			btrfs_release_delalloc_bytes(fs_info,
2891 						     ordered_extent->start,
2892 						     ordered_extent->disk_len);
2893 	}
2894 	unpin_extent_cache(&BTRFS_I(inode)->extent_tree,
2895 			   ordered_extent->file_offset, ordered_extent->len,
2896 			   trans->transid);
2897 	if (ret < 0) {
2898 		btrfs_abort_transaction(trans, ret);
2899 		goto out_unlock;
2900 	}
2901 
2902 	add_pending_csums(trans, inode, &ordered_extent->list);
2903 
2904 	btrfs_ordered_update_i_size(inode, 0, ordered_extent);
2905 	ret = btrfs_update_inode_fallback(trans, root, inode);
2906 	if (ret) { /* -ENOMEM or corruption */
2907 		btrfs_abort_transaction(trans, ret);
2908 		goto out_unlock;
2909 	}
2910 	ret = 0;
2911 out_unlock:
2912 	unlock_extent_cached(io_tree, ordered_extent->file_offset,
2913 			     ordered_extent->file_offset +
2914 			     ordered_extent->len - 1, &cached_state, GFP_NOFS);
2915 out:
2916 	if (root != fs_info->tree_root)
2917 		btrfs_delalloc_release_metadata(inode, ordered_extent->len);
2918 	if (trans)
2919 		btrfs_end_transaction(trans);
2920 
2921 	if (ret || truncated) {
2922 		u64 start, end;
2923 
2924 		if (truncated)
2925 			start = ordered_extent->file_offset + logical_len;
2926 		else
2927 			start = ordered_extent->file_offset;
2928 		end = ordered_extent->file_offset + ordered_extent->len - 1;
2929 		clear_extent_uptodate(io_tree, start, end, NULL, GFP_NOFS);
2930 
2931 		/* Drop the cache for the part of the extent we didn't write. */
2932 		btrfs_drop_extent_cache(inode, start, end, 0);
2933 
2934 		/*
2935 		 * If the ordered extent had an IOERR or something else went
2936 		 * wrong we need to return the space for this ordered extent
2937 		 * back to the allocator.  We only free the extent in the
2938 		 * truncated case if we didn't write out the extent at all.
2939 		 */
2940 		if ((ret || !logical_len) &&
2941 		    !test_bit(BTRFS_ORDERED_NOCOW, &ordered_extent->flags) &&
2942 		    !test_bit(BTRFS_ORDERED_PREALLOC, &ordered_extent->flags))
2943 			btrfs_free_reserved_extent(fs_info,
2944 						   ordered_extent->start,
2945 						   ordered_extent->disk_len, 1);
2946 	}
2947 
2948 
2949 	/*
2950 	 * This needs to be done to make sure anybody waiting knows we are done
2951 	 * updating everything for this ordered extent.
2952 	 */
2953 	btrfs_remove_ordered_extent(inode, ordered_extent);
2954 
2955 	/* for snapshot-aware defrag */
2956 	if (new) {
2957 		if (ret) {
2958 			free_sa_defrag_extent(new);
2959 			atomic_dec(&fs_info->defrag_running);
2960 		} else {
2961 			relink_file_extents(new);
2962 		}
2963 	}
2964 
2965 	/* once for us */
2966 	btrfs_put_ordered_extent(ordered_extent);
2967 	/* once for the tree */
2968 	btrfs_put_ordered_extent(ordered_extent);
2969 
2970 	return ret;
2971 }
2972 
2973 static void finish_ordered_fn(struct btrfs_work *work)
2974 {
2975 	struct btrfs_ordered_extent *ordered_extent;
2976 	ordered_extent = container_of(work, struct btrfs_ordered_extent, work);
2977 	btrfs_finish_ordered_io(ordered_extent);
2978 }
2979 
2980 static int btrfs_writepage_end_io_hook(struct page *page, u64 start, u64 end,
2981 				struct extent_state *state, int uptodate)
2982 {
2983 	struct inode *inode = page->mapping->host;
2984 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
2985 	struct btrfs_ordered_extent *ordered_extent = NULL;
2986 	struct btrfs_workqueue *wq;
2987 	btrfs_work_func_t func;
2988 
2989 	trace_btrfs_writepage_end_io_hook(page, start, end, uptodate);
2990 
2991 	ClearPagePrivate2(page);
2992 	if (!btrfs_dec_test_ordered_pending(inode, &ordered_extent, start,
2993 					    end - start + 1, uptodate))
2994 		return 0;
2995 
2996 	if (btrfs_is_free_space_inode(inode)) {
2997 		wq = fs_info->endio_freespace_worker;
2998 		func = btrfs_freespace_write_helper;
2999 	} else {
3000 		wq = fs_info->endio_write_workers;
3001 		func = btrfs_endio_write_helper;
3002 	}
3003 
3004 	btrfs_init_work(&ordered_extent->work, func, finish_ordered_fn, NULL,
3005 			NULL);
3006 	btrfs_queue_work(wq, &ordered_extent->work);
3007 
3008 	return 0;
3009 }
3010 
3011 static int __readpage_endio_check(struct inode *inode,
3012 				  struct btrfs_io_bio *io_bio,
3013 				  int icsum, struct page *page,
3014 				  int pgoff, u64 start, size_t len)
3015 {
3016 	char *kaddr;
3017 	u32 csum_expected;
3018 	u32 csum = ~(u32)0;
3019 
3020 	csum_expected = *(((u32 *)io_bio->csum) + icsum);
3021 
3022 	kaddr = kmap_atomic(page);
3023 	csum = btrfs_csum_data(kaddr + pgoff, csum,  len);
3024 	btrfs_csum_final(csum, (u8 *)&csum);
3025 	if (csum != csum_expected)
3026 		goto zeroit;
3027 
3028 	kunmap_atomic(kaddr);
3029 	return 0;
3030 zeroit:
3031 	btrfs_print_data_csum_error(inode, start, csum, csum_expected,
3032 				    io_bio->mirror_num);
3033 	memset(kaddr + pgoff, 1, len);
3034 	flush_dcache_page(page);
3035 	kunmap_atomic(kaddr);
3036 	if (csum_expected == 0)
3037 		return 0;
3038 	return -EIO;
3039 }
3040 
3041 /*
3042  * when reads are done, we need to check csums to verify the data is correct
3043  * if there's a match, we allow the bio to finish.  If not, the code in
3044  * extent_io.c will try to find good copies for us.
3045  */
3046 static int btrfs_readpage_end_io_hook(struct btrfs_io_bio *io_bio,
3047 				      u64 phy_offset, struct page *page,
3048 				      u64 start, u64 end, int mirror)
3049 {
3050 	size_t offset = start - page_offset(page);
3051 	struct inode *inode = page->mapping->host;
3052 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
3053 	struct btrfs_root *root = BTRFS_I(inode)->root;
3054 
3055 	if (PageChecked(page)) {
3056 		ClearPageChecked(page);
3057 		return 0;
3058 	}
3059 
3060 	if (BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM)
3061 		return 0;
3062 
3063 	if (root->root_key.objectid == BTRFS_DATA_RELOC_TREE_OBJECTID &&
3064 	    test_range_bit(io_tree, start, end, EXTENT_NODATASUM, 1, NULL)) {
3065 		clear_extent_bits(io_tree, start, end, EXTENT_NODATASUM);
3066 		return 0;
3067 	}
3068 
3069 	phy_offset >>= inode->i_sb->s_blocksize_bits;
3070 	return __readpage_endio_check(inode, io_bio, phy_offset, page, offset,
3071 				      start, (size_t)(end - start + 1));
3072 }
3073 
3074 void btrfs_add_delayed_iput(struct inode *inode)
3075 {
3076 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3077 	struct btrfs_inode *binode = BTRFS_I(inode);
3078 
3079 	if (atomic_add_unless(&inode->i_count, -1, 1))
3080 		return;
3081 
3082 	spin_lock(&fs_info->delayed_iput_lock);
3083 	if (binode->delayed_iput_count == 0) {
3084 		ASSERT(list_empty(&binode->delayed_iput));
3085 		list_add_tail(&binode->delayed_iput, &fs_info->delayed_iputs);
3086 	} else {
3087 		binode->delayed_iput_count++;
3088 	}
3089 	spin_unlock(&fs_info->delayed_iput_lock);
3090 }
3091 
3092 void btrfs_run_delayed_iputs(struct btrfs_fs_info *fs_info)
3093 {
3094 
3095 	spin_lock(&fs_info->delayed_iput_lock);
3096 	while (!list_empty(&fs_info->delayed_iputs)) {
3097 		struct btrfs_inode *inode;
3098 
3099 		inode = list_first_entry(&fs_info->delayed_iputs,
3100 				struct btrfs_inode, delayed_iput);
3101 		if (inode->delayed_iput_count) {
3102 			inode->delayed_iput_count--;
3103 			list_move_tail(&inode->delayed_iput,
3104 					&fs_info->delayed_iputs);
3105 		} else {
3106 			list_del_init(&inode->delayed_iput);
3107 		}
3108 		spin_unlock(&fs_info->delayed_iput_lock);
3109 		iput(&inode->vfs_inode);
3110 		spin_lock(&fs_info->delayed_iput_lock);
3111 	}
3112 	spin_unlock(&fs_info->delayed_iput_lock);
3113 }
3114 
3115 /*
3116  * This is called in transaction commit time. If there are no orphan
3117  * files in the subvolume, it removes orphan item and frees block_rsv
3118  * structure.
3119  */
3120 void btrfs_orphan_commit_root(struct btrfs_trans_handle *trans,
3121 			      struct btrfs_root *root)
3122 {
3123 	struct btrfs_fs_info *fs_info = root->fs_info;
3124 	struct btrfs_block_rsv *block_rsv;
3125 	int ret;
3126 
3127 	if (atomic_read(&root->orphan_inodes) ||
3128 	    root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE)
3129 		return;
3130 
3131 	spin_lock(&root->orphan_lock);
3132 	if (atomic_read(&root->orphan_inodes)) {
3133 		spin_unlock(&root->orphan_lock);
3134 		return;
3135 	}
3136 
3137 	if (root->orphan_cleanup_state != ORPHAN_CLEANUP_DONE) {
3138 		spin_unlock(&root->orphan_lock);
3139 		return;
3140 	}
3141 
3142 	block_rsv = root->orphan_block_rsv;
3143 	root->orphan_block_rsv = NULL;
3144 	spin_unlock(&root->orphan_lock);
3145 
3146 	if (test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state) &&
3147 	    btrfs_root_refs(&root->root_item) > 0) {
3148 		ret = btrfs_del_orphan_item(trans, fs_info->tree_root,
3149 					    root->root_key.objectid);
3150 		if (ret)
3151 			btrfs_abort_transaction(trans, ret);
3152 		else
3153 			clear_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED,
3154 				  &root->state);
3155 	}
3156 
3157 	if (block_rsv) {
3158 		WARN_ON(block_rsv->size > 0);
3159 		btrfs_free_block_rsv(fs_info, block_rsv);
3160 	}
3161 }
3162 
3163 /*
3164  * This creates an orphan entry for the given inode in case something goes
3165  * wrong in the middle of an unlink/truncate.
3166  *
3167  * NOTE: caller of this function should reserve 5 units of metadata for
3168  *	 this function.
3169  */
3170 int btrfs_orphan_add(struct btrfs_trans_handle *trans, struct inode *inode)
3171 {
3172 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3173 	struct btrfs_root *root = BTRFS_I(inode)->root;
3174 	struct btrfs_block_rsv *block_rsv = NULL;
3175 	int reserve = 0;
3176 	int insert = 0;
3177 	int ret;
3178 
3179 	if (!root->orphan_block_rsv) {
3180 		block_rsv = btrfs_alloc_block_rsv(fs_info,
3181 						  BTRFS_BLOCK_RSV_TEMP);
3182 		if (!block_rsv)
3183 			return -ENOMEM;
3184 	}
3185 
3186 	spin_lock(&root->orphan_lock);
3187 	if (!root->orphan_block_rsv) {
3188 		root->orphan_block_rsv = block_rsv;
3189 	} else if (block_rsv) {
3190 		btrfs_free_block_rsv(fs_info, block_rsv);
3191 		block_rsv = NULL;
3192 	}
3193 
3194 	if (!test_and_set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3195 			      &BTRFS_I(inode)->runtime_flags)) {
3196 #if 0
3197 		/*
3198 		 * For proper ENOSPC handling, we should do orphan
3199 		 * cleanup when mounting. But this introduces backward
3200 		 * compatibility issue.
3201 		 */
3202 		if (!xchg(&root->orphan_item_inserted, 1))
3203 			insert = 2;
3204 		else
3205 			insert = 1;
3206 #endif
3207 		insert = 1;
3208 		atomic_inc(&root->orphan_inodes);
3209 	}
3210 
3211 	if (!test_and_set_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3212 			      &BTRFS_I(inode)->runtime_flags))
3213 		reserve = 1;
3214 	spin_unlock(&root->orphan_lock);
3215 
3216 	/* grab metadata reservation from transaction handle */
3217 	if (reserve) {
3218 		ret = btrfs_orphan_reserve_metadata(trans, inode);
3219 		ASSERT(!ret);
3220 		if (ret) {
3221 			atomic_dec(&root->orphan_inodes);
3222 			clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3223 				  &BTRFS_I(inode)->runtime_flags);
3224 			if (insert)
3225 				clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3226 					  &BTRFS_I(inode)->runtime_flags);
3227 			return ret;
3228 		}
3229 	}
3230 
3231 	/* insert an orphan item to track this unlinked/truncated file */
3232 	if (insert >= 1) {
3233 		ret = btrfs_insert_orphan_item(trans, root,
3234 				btrfs_ino(BTRFS_I(inode)));
3235 		if (ret) {
3236 			atomic_dec(&root->orphan_inodes);
3237 			if (reserve) {
3238 				clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3239 					  &BTRFS_I(inode)->runtime_flags);
3240 				btrfs_orphan_release_metadata(inode);
3241 			}
3242 			if (ret != -EEXIST) {
3243 				clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3244 					  &BTRFS_I(inode)->runtime_flags);
3245 				btrfs_abort_transaction(trans, ret);
3246 				return ret;
3247 			}
3248 		}
3249 		ret = 0;
3250 	}
3251 
3252 	/* insert an orphan item to track subvolume contains orphan files */
3253 	if (insert >= 2) {
3254 		ret = btrfs_insert_orphan_item(trans, fs_info->tree_root,
3255 					       root->root_key.objectid);
3256 		if (ret && ret != -EEXIST) {
3257 			btrfs_abort_transaction(trans, ret);
3258 			return ret;
3259 		}
3260 	}
3261 	return 0;
3262 }
3263 
3264 /*
3265  * We have done the truncate/delete so we can go ahead and remove the orphan
3266  * item for this particular inode.
3267  */
3268 static int btrfs_orphan_del(struct btrfs_trans_handle *trans,
3269 			    struct inode *inode)
3270 {
3271 	struct btrfs_root *root = BTRFS_I(inode)->root;
3272 	int delete_item = 0;
3273 	int release_rsv = 0;
3274 	int ret = 0;
3275 
3276 	spin_lock(&root->orphan_lock);
3277 	if (test_and_clear_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3278 			       &BTRFS_I(inode)->runtime_flags))
3279 		delete_item = 1;
3280 
3281 	if (test_and_clear_bit(BTRFS_INODE_ORPHAN_META_RESERVED,
3282 			       &BTRFS_I(inode)->runtime_flags))
3283 		release_rsv = 1;
3284 	spin_unlock(&root->orphan_lock);
3285 
3286 	if (delete_item) {
3287 		atomic_dec(&root->orphan_inodes);
3288 		if (trans)
3289 			ret = btrfs_del_orphan_item(trans, root,
3290 						    btrfs_ino(BTRFS_I(inode)));
3291 	}
3292 
3293 	if (release_rsv)
3294 		btrfs_orphan_release_metadata(inode);
3295 
3296 	return ret;
3297 }
3298 
3299 /*
3300  * this cleans up any orphans that may be left on the list from the last use
3301  * of this root.
3302  */
3303 int btrfs_orphan_cleanup(struct btrfs_root *root)
3304 {
3305 	struct btrfs_fs_info *fs_info = root->fs_info;
3306 	struct btrfs_path *path;
3307 	struct extent_buffer *leaf;
3308 	struct btrfs_key key, found_key;
3309 	struct btrfs_trans_handle *trans;
3310 	struct inode *inode;
3311 	u64 last_objectid = 0;
3312 	int ret = 0, nr_unlink = 0, nr_truncate = 0;
3313 
3314 	if (cmpxchg(&root->orphan_cleanup_state, 0, ORPHAN_CLEANUP_STARTED))
3315 		return 0;
3316 
3317 	path = btrfs_alloc_path();
3318 	if (!path) {
3319 		ret = -ENOMEM;
3320 		goto out;
3321 	}
3322 	path->reada = READA_BACK;
3323 
3324 	key.objectid = BTRFS_ORPHAN_OBJECTID;
3325 	key.type = BTRFS_ORPHAN_ITEM_KEY;
3326 	key.offset = (u64)-1;
3327 
3328 	while (1) {
3329 		ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
3330 		if (ret < 0)
3331 			goto out;
3332 
3333 		/*
3334 		 * if ret == 0 means we found what we were searching for, which
3335 		 * is weird, but possible, so only screw with path if we didn't
3336 		 * find the key and see if we have stuff that matches
3337 		 */
3338 		if (ret > 0) {
3339 			ret = 0;
3340 			if (path->slots[0] == 0)
3341 				break;
3342 			path->slots[0]--;
3343 		}
3344 
3345 		/* pull out the item */
3346 		leaf = path->nodes[0];
3347 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
3348 
3349 		/* make sure the item matches what we want */
3350 		if (found_key.objectid != BTRFS_ORPHAN_OBJECTID)
3351 			break;
3352 		if (found_key.type != BTRFS_ORPHAN_ITEM_KEY)
3353 			break;
3354 
3355 		/* release the path since we're done with it */
3356 		btrfs_release_path(path);
3357 
3358 		/*
3359 		 * this is where we are basically btrfs_lookup, without the
3360 		 * crossing root thing.  we store the inode number in the
3361 		 * offset of the orphan item.
3362 		 */
3363 
3364 		if (found_key.offset == last_objectid) {
3365 			btrfs_err(fs_info,
3366 				  "Error removing orphan entry, stopping orphan cleanup");
3367 			ret = -EINVAL;
3368 			goto out;
3369 		}
3370 
3371 		last_objectid = found_key.offset;
3372 
3373 		found_key.objectid = found_key.offset;
3374 		found_key.type = BTRFS_INODE_ITEM_KEY;
3375 		found_key.offset = 0;
3376 		inode = btrfs_iget(fs_info->sb, &found_key, root, NULL);
3377 		ret = PTR_ERR_OR_ZERO(inode);
3378 		if (ret && ret != -ENOENT)
3379 			goto out;
3380 
3381 		if (ret == -ENOENT && root == fs_info->tree_root) {
3382 			struct btrfs_root *dead_root;
3383 			struct btrfs_fs_info *fs_info = root->fs_info;
3384 			int is_dead_root = 0;
3385 
3386 			/*
3387 			 * this is an orphan in the tree root. Currently these
3388 			 * could come from 2 sources:
3389 			 *  a) a snapshot deletion in progress
3390 			 *  b) a free space cache inode
3391 			 * We need to distinguish those two, as the snapshot
3392 			 * orphan must not get deleted.
3393 			 * find_dead_roots already ran before us, so if this
3394 			 * is a snapshot deletion, we should find the root
3395 			 * in the dead_roots list
3396 			 */
3397 			spin_lock(&fs_info->trans_lock);
3398 			list_for_each_entry(dead_root, &fs_info->dead_roots,
3399 					    root_list) {
3400 				if (dead_root->root_key.objectid ==
3401 				    found_key.objectid) {
3402 					is_dead_root = 1;
3403 					break;
3404 				}
3405 			}
3406 			spin_unlock(&fs_info->trans_lock);
3407 			if (is_dead_root) {
3408 				/* prevent this orphan from being found again */
3409 				key.offset = found_key.objectid - 1;
3410 				continue;
3411 			}
3412 		}
3413 		/*
3414 		 * Inode is already gone but the orphan item is still there,
3415 		 * kill the orphan item.
3416 		 */
3417 		if (ret == -ENOENT) {
3418 			trans = btrfs_start_transaction(root, 1);
3419 			if (IS_ERR(trans)) {
3420 				ret = PTR_ERR(trans);
3421 				goto out;
3422 			}
3423 			btrfs_debug(fs_info, "auto deleting %Lu",
3424 				    found_key.objectid);
3425 			ret = btrfs_del_orphan_item(trans, root,
3426 						    found_key.objectid);
3427 			btrfs_end_transaction(trans);
3428 			if (ret)
3429 				goto out;
3430 			continue;
3431 		}
3432 
3433 		/*
3434 		 * add this inode to the orphan list so btrfs_orphan_del does
3435 		 * the proper thing when we hit it
3436 		 */
3437 		set_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
3438 			&BTRFS_I(inode)->runtime_flags);
3439 		atomic_inc(&root->orphan_inodes);
3440 
3441 		/* if we have links, this was a truncate, lets do that */
3442 		if (inode->i_nlink) {
3443 			if (WARN_ON(!S_ISREG(inode->i_mode))) {
3444 				iput(inode);
3445 				continue;
3446 			}
3447 			nr_truncate++;
3448 
3449 			/* 1 for the orphan item deletion. */
3450 			trans = btrfs_start_transaction(root, 1);
3451 			if (IS_ERR(trans)) {
3452 				iput(inode);
3453 				ret = PTR_ERR(trans);
3454 				goto out;
3455 			}
3456 			ret = btrfs_orphan_add(trans, inode);
3457 			btrfs_end_transaction(trans);
3458 			if (ret) {
3459 				iput(inode);
3460 				goto out;
3461 			}
3462 
3463 			ret = btrfs_truncate(inode);
3464 			if (ret)
3465 				btrfs_orphan_del(NULL, inode);
3466 		} else {
3467 			nr_unlink++;
3468 		}
3469 
3470 		/* this will do delete_inode and everything for us */
3471 		iput(inode);
3472 		if (ret)
3473 			goto out;
3474 	}
3475 	/* release the path since we're done with it */
3476 	btrfs_release_path(path);
3477 
3478 	root->orphan_cleanup_state = ORPHAN_CLEANUP_DONE;
3479 
3480 	if (root->orphan_block_rsv)
3481 		btrfs_block_rsv_release(fs_info, root->orphan_block_rsv,
3482 					(u64)-1);
3483 
3484 	if (root->orphan_block_rsv ||
3485 	    test_bit(BTRFS_ROOT_ORPHAN_ITEM_INSERTED, &root->state)) {
3486 		trans = btrfs_join_transaction(root);
3487 		if (!IS_ERR(trans))
3488 			btrfs_end_transaction(trans);
3489 	}
3490 
3491 	if (nr_unlink)
3492 		btrfs_debug(fs_info, "unlinked %d orphans", nr_unlink);
3493 	if (nr_truncate)
3494 		btrfs_debug(fs_info, "truncated %d orphans", nr_truncate);
3495 
3496 out:
3497 	if (ret)
3498 		btrfs_err(fs_info, "could not do orphan cleanup %d", ret);
3499 	btrfs_free_path(path);
3500 	return ret;
3501 }
3502 
3503 /*
3504  * very simple check to peek ahead in the leaf looking for xattrs.  If we
3505  * don't find any xattrs, we know there can't be any acls.
3506  *
3507  * slot is the slot the inode is in, objectid is the objectid of the inode
3508  */
3509 static noinline int acls_after_inode_item(struct extent_buffer *leaf,
3510 					  int slot, u64 objectid,
3511 					  int *first_xattr_slot)
3512 {
3513 	u32 nritems = btrfs_header_nritems(leaf);
3514 	struct btrfs_key found_key;
3515 	static u64 xattr_access = 0;
3516 	static u64 xattr_default = 0;
3517 	int scanned = 0;
3518 
3519 	if (!xattr_access) {
3520 		xattr_access = btrfs_name_hash(XATTR_NAME_POSIX_ACL_ACCESS,
3521 					strlen(XATTR_NAME_POSIX_ACL_ACCESS));
3522 		xattr_default = btrfs_name_hash(XATTR_NAME_POSIX_ACL_DEFAULT,
3523 					strlen(XATTR_NAME_POSIX_ACL_DEFAULT));
3524 	}
3525 
3526 	slot++;
3527 	*first_xattr_slot = -1;
3528 	while (slot < nritems) {
3529 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
3530 
3531 		/* we found a different objectid, there must not be acls */
3532 		if (found_key.objectid != objectid)
3533 			return 0;
3534 
3535 		/* we found an xattr, assume we've got an acl */
3536 		if (found_key.type == BTRFS_XATTR_ITEM_KEY) {
3537 			if (*first_xattr_slot == -1)
3538 				*first_xattr_slot = slot;
3539 			if (found_key.offset == xattr_access ||
3540 			    found_key.offset == xattr_default)
3541 				return 1;
3542 		}
3543 
3544 		/*
3545 		 * we found a key greater than an xattr key, there can't
3546 		 * be any acls later on
3547 		 */
3548 		if (found_key.type > BTRFS_XATTR_ITEM_KEY)
3549 			return 0;
3550 
3551 		slot++;
3552 		scanned++;
3553 
3554 		/*
3555 		 * it goes inode, inode backrefs, xattrs, extents,
3556 		 * so if there are a ton of hard links to an inode there can
3557 		 * be a lot of backrefs.  Don't waste time searching too hard,
3558 		 * this is just an optimization
3559 		 */
3560 		if (scanned >= 8)
3561 			break;
3562 	}
3563 	/* we hit the end of the leaf before we found an xattr or
3564 	 * something larger than an xattr.  We have to assume the inode
3565 	 * has acls
3566 	 */
3567 	if (*first_xattr_slot == -1)
3568 		*first_xattr_slot = slot;
3569 	return 1;
3570 }
3571 
3572 /*
3573  * read an inode from the btree into the in-memory inode
3574  */
3575 static int btrfs_read_locked_inode(struct inode *inode)
3576 {
3577 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
3578 	struct btrfs_path *path;
3579 	struct extent_buffer *leaf;
3580 	struct btrfs_inode_item *inode_item;
3581 	struct btrfs_root *root = BTRFS_I(inode)->root;
3582 	struct btrfs_key location;
3583 	unsigned long ptr;
3584 	int maybe_acls;
3585 	u32 rdev;
3586 	int ret;
3587 	bool filled = false;
3588 	int first_xattr_slot;
3589 
3590 	ret = btrfs_fill_inode(inode, &rdev);
3591 	if (!ret)
3592 		filled = true;
3593 
3594 	path = btrfs_alloc_path();
3595 	if (!path) {
3596 		ret = -ENOMEM;
3597 		goto make_bad;
3598 	}
3599 
3600 	memcpy(&location, &BTRFS_I(inode)->location, sizeof(location));
3601 
3602 	ret = btrfs_lookup_inode(NULL, root, path, &location, 0);
3603 	if (ret) {
3604 		if (ret > 0)
3605 			ret = -ENOENT;
3606 		goto make_bad;
3607 	}
3608 
3609 	leaf = path->nodes[0];
3610 
3611 	if (filled)
3612 		goto cache_index;
3613 
3614 	inode_item = btrfs_item_ptr(leaf, path->slots[0],
3615 				    struct btrfs_inode_item);
3616 	inode->i_mode = btrfs_inode_mode(leaf, inode_item);
3617 	set_nlink(inode, btrfs_inode_nlink(leaf, inode_item));
3618 	i_uid_write(inode, btrfs_inode_uid(leaf, inode_item));
3619 	i_gid_write(inode, btrfs_inode_gid(leaf, inode_item));
3620 	btrfs_i_size_write(inode, btrfs_inode_size(leaf, inode_item));
3621 
3622 	inode->i_atime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->atime);
3623 	inode->i_atime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->atime);
3624 
3625 	inode->i_mtime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->mtime);
3626 	inode->i_mtime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->mtime);
3627 
3628 	inode->i_ctime.tv_sec = btrfs_timespec_sec(leaf, &inode_item->ctime);
3629 	inode->i_ctime.tv_nsec = btrfs_timespec_nsec(leaf, &inode_item->ctime);
3630 
3631 	BTRFS_I(inode)->i_otime.tv_sec =
3632 		btrfs_timespec_sec(leaf, &inode_item->otime);
3633 	BTRFS_I(inode)->i_otime.tv_nsec =
3634 		btrfs_timespec_nsec(leaf, &inode_item->otime);
3635 
3636 	inode_set_bytes(inode, btrfs_inode_nbytes(leaf, inode_item));
3637 	BTRFS_I(inode)->generation = btrfs_inode_generation(leaf, inode_item);
3638 	BTRFS_I(inode)->last_trans = btrfs_inode_transid(leaf, inode_item);
3639 
3640 	inode->i_version = btrfs_inode_sequence(leaf, inode_item);
3641 	inode->i_generation = BTRFS_I(inode)->generation;
3642 	inode->i_rdev = 0;
3643 	rdev = btrfs_inode_rdev(leaf, inode_item);
3644 
3645 	BTRFS_I(inode)->index_cnt = (u64)-1;
3646 	BTRFS_I(inode)->flags = btrfs_inode_flags(leaf, inode_item);
3647 
3648 cache_index:
3649 	/*
3650 	 * If we were modified in the current generation and evicted from memory
3651 	 * and then re-read we need to do a full sync since we don't have any
3652 	 * idea about which extents were modified before we were evicted from
3653 	 * cache.
3654 	 *
3655 	 * This is required for both inode re-read from disk and delayed inode
3656 	 * in delayed_nodes_tree.
3657 	 */
3658 	if (BTRFS_I(inode)->last_trans == fs_info->generation)
3659 		set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
3660 			&BTRFS_I(inode)->runtime_flags);
3661 
3662 	/*
3663 	 * We don't persist the id of the transaction where an unlink operation
3664 	 * against the inode was last made. So here we assume the inode might
3665 	 * have been evicted, and therefore the exact value of last_unlink_trans
3666 	 * lost, and set it to last_trans to avoid metadata inconsistencies
3667 	 * between the inode and its parent if the inode is fsync'ed and the log
3668 	 * replayed. For example, in the scenario:
3669 	 *
3670 	 * touch mydir/foo
3671 	 * ln mydir/foo mydir/bar
3672 	 * sync
3673 	 * unlink mydir/bar
3674 	 * echo 2 > /proc/sys/vm/drop_caches   # evicts inode
3675 	 * xfs_io -c fsync mydir/foo
3676 	 * <power failure>
3677 	 * mount fs, triggers fsync log replay
3678 	 *
3679 	 * We must make sure that when we fsync our inode foo we also log its
3680 	 * parent inode, otherwise after log replay the parent still has the
3681 	 * dentry with the "bar" name but our inode foo has a link count of 1
3682 	 * and doesn't have an inode ref with the name "bar" anymore.
3683 	 *
3684 	 * Setting last_unlink_trans to last_trans is a pessimistic approach,
3685 	 * but it guarantees correctness at the expense of occasional full
3686 	 * transaction commits on fsync if our inode is a directory, or if our
3687 	 * inode is not a directory, logging its parent unnecessarily.
3688 	 */
3689 	BTRFS_I(inode)->last_unlink_trans = BTRFS_I(inode)->last_trans;
3690 
3691 	path->slots[0]++;
3692 	if (inode->i_nlink != 1 ||
3693 	    path->slots[0] >= btrfs_header_nritems(leaf))
3694 		goto cache_acl;
3695 
3696 	btrfs_item_key_to_cpu(leaf, &location, path->slots[0]);
3697 	if (location.objectid != btrfs_ino(BTRFS_I(inode)))
3698 		goto cache_acl;
3699 
3700 	ptr = btrfs_item_ptr_offset(leaf, path->slots[0]);
3701 	if (location.type == BTRFS_INODE_REF_KEY) {
3702 		struct btrfs_inode_ref *ref;
3703 
3704 		ref = (struct btrfs_inode_ref *)ptr;
3705 		BTRFS_I(inode)->dir_index = btrfs_inode_ref_index(leaf, ref);
3706 	} else if (location.type == BTRFS_INODE_EXTREF_KEY) {
3707 		struct btrfs_inode_extref *extref;
3708 
3709 		extref = (struct btrfs_inode_extref *)ptr;
3710 		BTRFS_I(inode)->dir_index = btrfs_inode_extref_index(leaf,
3711 								     extref);
3712 	}
3713 cache_acl:
3714 	/*
3715 	 * try to precache a NULL acl entry for files that don't have
3716 	 * any xattrs or acls
3717 	 */
3718 	maybe_acls = acls_after_inode_item(leaf, path->slots[0],
3719 			btrfs_ino(BTRFS_I(inode)), &first_xattr_slot);
3720 	if (first_xattr_slot != -1) {
3721 		path->slots[0] = first_xattr_slot;
3722 		ret = btrfs_load_inode_props(inode, path);
3723 		if (ret)
3724 			btrfs_err(fs_info,
3725 				  "error loading props for ino %llu (root %llu): %d",
3726 				  btrfs_ino(BTRFS_I(inode)),
3727 				  root->root_key.objectid, ret);
3728 	}
3729 	btrfs_free_path(path);
3730 
3731 	if (!maybe_acls)
3732 		cache_no_acl(inode);
3733 
3734 	switch (inode->i_mode & S_IFMT) {
3735 	case S_IFREG:
3736 		inode->i_mapping->a_ops = &btrfs_aops;
3737 		BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
3738 		inode->i_fop = &btrfs_file_operations;
3739 		inode->i_op = &btrfs_file_inode_operations;
3740 		break;
3741 	case S_IFDIR:
3742 		inode->i_fop = &btrfs_dir_file_operations;
3743 		inode->i_op = &btrfs_dir_inode_operations;
3744 		break;
3745 	case S_IFLNK:
3746 		inode->i_op = &btrfs_symlink_inode_operations;
3747 		inode_nohighmem(inode);
3748 		inode->i_mapping->a_ops = &btrfs_symlink_aops;
3749 		break;
3750 	default:
3751 		inode->i_op = &btrfs_special_inode_operations;
3752 		init_special_inode(inode, inode->i_mode, rdev);
3753 		break;
3754 	}
3755 
3756 	btrfs_update_iflags(inode);
3757 	return 0;
3758 
3759 make_bad:
3760 	btrfs_free_path(path);
3761 	make_bad_inode(inode);
3762 	return ret;
3763 }
3764 
3765 /*
3766  * given a leaf and an inode, copy the inode fields into the leaf
3767  */
3768 static void fill_inode_item(struct btrfs_trans_handle *trans,
3769 			    struct extent_buffer *leaf,
3770 			    struct btrfs_inode_item *item,
3771 			    struct inode *inode)
3772 {
3773 	struct btrfs_map_token token;
3774 
3775 	btrfs_init_map_token(&token);
3776 
3777 	btrfs_set_token_inode_uid(leaf, item, i_uid_read(inode), &token);
3778 	btrfs_set_token_inode_gid(leaf, item, i_gid_read(inode), &token);
3779 	btrfs_set_token_inode_size(leaf, item, BTRFS_I(inode)->disk_i_size,
3780 				   &token);
3781 	btrfs_set_token_inode_mode(leaf, item, inode->i_mode, &token);
3782 	btrfs_set_token_inode_nlink(leaf, item, inode->i_nlink, &token);
3783 
3784 	btrfs_set_token_timespec_sec(leaf, &item->atime,
3785 				     inode->i_atime.tv_sec, &token);
3786 	btrfs_set_token_timespec_nsec(leaf, &item->atime,
3787 				      inode->i_atime.tv_nsec, &token);
3788 
3789 	btrfs_set_token_timespec_sec(leaf, &item->mtime,
3790 				     inode->i_mtime.tv_sec, &token);
3791 	btrfs_set_token_timespec_nsec(leaf, &item->mtime,
3792 				      inode->i_mtime.tv_nsec, &token);
3793 
3794 	btrfs_set_token_timespec_sec(leaf, &item->ctime,
3795 				     inode->i_ctime.tv_sec, &token);
3796 	btrfs_set_token_timespec_nsec(leaf, &item->ctime,
3797 				      inode->i_ctime.tv_nsec, &token);
3798 
3799 	btrfs_set_token_timespec_sec(leaf, &item->otime,
3800 				     BTRFS_I(inode)->i_otime.tv_sec, &token);
3801 	btrfs_set_token_timespec_nsec(leaf, &item->otime,
3802 				      BTRFS_I(inode)->i_otime.tv_nsec, &token);
3803 
3804 	btrfs_set_token_inode_nbytes(leaf, item, inode_get_bytes(inode),
3805 				     &token);
3806 	btrfs_set_token_inode_generation(leaf, item, BTRFS_I(inode)->generation,
3807 					 &token);
3808 	btrfs_set_token_inode_sequence(leaf, item, inode->i_version, &token);
3809 	btrfs_set_token_inode_transid(leaf, item, trans->transid, &token);
3810 	btrfs_set_token_inode_rdev(leaf, item, inode->i_rdev, &token);
3811 	btrfs_set_token_inode_flags(leaf, item, BTRFS_I(inode)->flags, &token);
3812 	btrfs_set_token_inode_block_group(leaf, item, 0, &token);
3813 }
3814 
3815 /*
3816  * copy everything in the in-memory inode into the btree.
3817  */
3818 static noinline int btrfs_update_inode_item(struct btrfs_trans_handle *trans,
3819 				struct btrfs_root *root, struct inode *inode)
3820 {
3821 	struct btrfs_inode_item *inode_item;
3822 	struct btrfs_path *path;
3823 	struct extent_buffer *leaf;
3824 	int ret;
3825 
3826 	path = btrfs_alloc_path();
3827 	if (!path)
3828 		return -ENOMEM;
3829 
3830 	path->leave_spinning = 1;
3831 	ret = btrfs_lookup_inode(trans, root, path, &BTRFS_I(inode)->location,
3832 				 1);
3833 	if (ret) {
3834 		if (ret > 0)
3835 			ret = -ENOENT;
3836 		goto failed;
3837 	}
3838 
3839 	leaf = path->nodes[0];
3840 	inode_item = btrfs_item_ptr(leaf, path->slots[0],
3841 				    struct btrfs_inode_item);
3842 
3843 	fill_inode_item(trans, leaf, inode_item, inode);
3844 	btrfs_mark_buffer_dirty(leaf);
3845 	btrfs_set_inode_last_trans(trans, inode);
3846 	ret = 0;
3847 failed:
3848 	btrfs_free_path(path);
3849 	return ret;
3850 }
3851 
3852 /*
3853  * copy everything in the in-memory inode into the btree.
3854  */
3855 noinline int btrfs_update_inode(struct btrfs_trans_handle *trans,
3856 				struct btrfs_root *root, struct inode *inode)
3857 {
3858 	struct btrfs_fs_info *fs_info = root->fs_info;
3859 	int ret;
3860 
3861 	/*
3862 	 * If the inode is a free space inode, we can deadlock during commit
3863 	 * if we put it into the delayed code.
3864 	 *
3865 	 * The data relocation inode should also be directly updated
3866 	 * without delay
3867 	 */
3868 	if (!btrfs_is_free_space_inode(inode)
3869 	    && root->root_key.objectid != BTRFS_DATA_RELOC_TREE_OBJECTID
3870 	    && !test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
3871 		btrfs_update_root_times(trans, root);
3872 
3873 		ret = btrfs_delayed_update_inode(trans, root, inode);
3874 		if (!ret)
3875 			btrfs_set_inode_last_trans(trans, inode);
3876 		return ret;
3877 	}
3878 
3879 	return btrfs_update_inode_item(trans, root, inode);
3880 }
3881 
3882 noinline int btrfs_update_inode_fallback(struct btrfs_trans_handle *trans,
3883 					 struct btrfs_root *root,
3884 					 struct inode *inode)
3885 {
3886 	int ret;
3887 
3888 	ret = btrfs_update_inode(trans, root, inode);
3889 	if (ret == -ENOSPC)
3890 		return btrfs_update_inode_item(trans, root, inode);
3891 	return ret;
3892 }
3893 
3894 /*
3895  * unlink helper that gets used here in inode.c and in the tree logging
3896  * recovery code.  It remove a link in a directory with a given name, and
3897  * also drops the back refs in the inode to the directory
3898  */
3899 static int __btrfs_unlink_inode(struct btrfs_trans_handle *trans,
3900 				struct btrfs_root *root,
3901 				struct btrfs_inode *dir,
3902 				struct btrfs_inode *inode,
3903 				const char *name, int name_len)
3904 {
3905 	struct btrfs_fs_info *fs_info = root->fs_info;
3906 	struct btrfs_path *path;
3907 	int ret = 0;
3908 	struct extent_buffer *leaf;
3909 	struct btrfs_dir_item *di;
3910 	struct btrfs_key key;
3911 	u64 index;
3912 	u64 ino = btrfs_ino(inode);
3913 	u64 dir_ino = btrfs_ino(dir);
3914 
3915 	path = btrfs_alloc_path();
3916 	if (!path) {
3917 		ret = -ENOMEM;
3918 		goto out;
3919 	}
3920 
3921 	path->leave_spinning = 1;
3922 	di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
3923 				    name, name_len, -1);
3924 	if (IS_ERR(di)) {
3925 		ret = PTR_ERR(di);
3926 		goto err;
3927 	}
3928 	if (!di) {
3929 		ret = -ENOENT;
3930 		goto err;
3931 	}
3932 	leaf = path->nodes[0];
3933 	btrfs_dir_item_key_to_cpu(leaf, di, &key);
3934 	ret = btrfs_delete_one_dir_name(trans, root, path, di);
3935 	if (ret)
3936 		goto err;
3937 	btrfs_release_path(path);
3938 
3939 	/*
3940 	 * If we don't have dir index, we have to get it by looking up
3941 	 * the inode ref, since we get the inode ref, remove it directly,
3942 	 * it is unnecessary to do delayed deletion.
3943 	 *
3944 	 * But if we have dir index, needn't search inode ref to get it.
3945 	 * Since the inode ref is close to the inode item, it is better
3946 	 * that we delay to delete it, and just do this deletion when
3947 	 * we update the inode item.
3948 	 */
3949 	if (inode->dir_index) {
3950 		ret = btrfs_delayed_delete_inode_ref(inode);
3951 		if (!ret) {
3952 			index = inode->dir_index;
3953 			goto skip_backref;
3954 		}
3955 	}
3956 
3957 	ret = btrfs_del_inode_ref(trans, root, name, name_len, ino,
3958 				  dir_ino, &index);
3959 	if (ret) {
3960 		btrfs_info(fs_info,
3961 			"failed to delete reference to %.*s, inode %llu parent %llu",
3962 			name_len, name, ino, dir_ino);
3963 		btrfs_abort_transaction(trans, ret);
3964 		goto err;
3965 	}
3966 skip_backref:
3967 	ret = btrfs_delete_delayed_dir_index(trans, fs_info, dir, index);
3968 	if (ret) {
3969 		btrfs_abort_transaction(trans, ret);
3970 		goto err;
3971 	}
3972 
3973 	ret = btrfs_del_inode_ref_in_log(trans, root, name, name_len, inode,
3974 			dir_ino);
3975 	if (ret != 0 && ret != -ENOENT) {
3976 		btrfs_abort_transaction(trans, ret);
3977 		goto err;
3978 	}
3979 
3980 	ret = btrfs_del_dir_entries_in_log(trans, root, name, name_len, dir,
3981 			index);
3982 	if (ret == -ENOENT)
3983 		ret = 0;
3984 	else if (ret)
3985 		btrfs_abort_transaction(trans, ret);
3986 err:
3987 	btrfs_free_path(path);
3988 	if (ret)
3989 		goto out;
3990 
3991 	btrfs_i_size_write(&dir->vfs_inode,
3992 			dir->vfs_inode.i_size - name_len * 2);
3993 	inode_inc_iversion(&inode->vfs_inode);
3994 	inode_inc_iversion(&dir->vfs_inode);
3995 	inode->vfs_inode.i_ctime = dir->vfs_inode.i_mtime =
3996 		dir->vfs_inode.i_ctime = current_time(&inode->vfs_inode);
3997 	ret = btrfs_update_inode(trans, root, &dir->vfs_inode);
3998 out:
3999 	return ret;
4000 }
4001 
4002 int btrfs_unlink_inode(struct btrfs_trans_handle *trans,
4003 		       struct btrfs_root *root,
4004 		       struct btrfs_inode *dir, struct btrfs_inode *inode,
4005 		       const char *name, int name_len)
4006 {
4007 	int ret;
4008 	ret = __btrfs_unlink_inode(trans, root, dir, inode, name, name_len);
4009 	if (!ret) {
4010 		drop_nlink(&inode->vfs_inode);
4011 		ret = btrfs_update_inode(trans, root, &inode->vfs_inode);
4012 	}
4013 	return ret;
4014 }
4015 
4016 /*
4017  * helper to start transaction for unlink and rmdir.
4018  *
4019  * unlink and rmdir are special in btrfs, they do not always free space, so
4020  * if we cannot make our reservations the normal way try and see if there is
4021  * plenty of slack room in the global reserve to migrate, otherwise we cannot
4022  * allow the unlink to occur.
4023  */
4024 static struct btrfs_trans_handle *__unlink_start_trans(struct inode *dir)
4025 {
4026 	struct btrfs_root *root = BTRFS_I(dir)->root;
4027 
4028 	/*
4029 	 * 1 for the possible orphan item
4030 	 * 1 for the dir item
4031 	 * 1 for the dir index
4032 	 * 1 for the inode ref
4033 	 * 1 for the inode
4034 	 */
4035 	return btrfs_start_transaction_fallback_global_rsv(root, 5, 5);
4036 }
4037 
4038 static int btrfs_unlink(struct inode *dir, struct dentry *dentry)
4039 {
4040 	struct btrfs_root *root = BTRFS_I(dir)->root;
4041 	struct btrfs_trans_handle *trans;
4042 	struct inode *inode = d_inode(dentry);
4043 	int ret;
4044 
4045 	trans = __unlink_start_trans(dir);
4046 	if (IS_ERR(trans))
4047 		return PTR_ERR(trans);
4048 
4049 	btrfs_record_unlink_dir(trans, BTRFS_I(dir), BTRFS_I(d_inode(dentry)),
4050 			0);
4051 
4052 	ret = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4053 			BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4054 			dentry->d_name.len);
4055 	if (ret)
4056 		goto out;
4057 
4058 	if (inode->i_nlink == 0) {
4059 		ret = btrfs_orphan_add(trans, inode);
4060 		if (ret)
4061 			goto out;
4062 	}
4063 
4064 out:
4065 	btrfs_end_transaction(trans);
4066 	btrfs_btree_balance_dirty(root->fs_info);
4067 	return ret;
4068 }
4069 
4070 int btrfs_unlink_subvol(struct btrfs_trans_handle *trans,
4071 			struct btrfs_root *root,
4072 			struct inode *dir, u64 objectid,
4073 			const char *name, int name_len)
4074 {
4075 	struct btrfs_fs_info *fs_info = root->fs_info;
4076 	struct btrfs_path *path;
4077 	struct extent_buffer *leaf;
4078 	struct btrfs_dir_item *di;
4079 	struct btrfs_key key;
4080 	u64 index;
4081 	int ret;
4082 	u64 dir_ino = btrfs_ino(BTRFS_I(dir));
4083 
4084 	path = btrfs_alloc_path();
4085 	if (!path)
4086 		return -ENOMEM;
4087 
4088 	di = btrfs_lookup_dir_item(trans, root, path, dir_ino,
4089 				   name, name_len, -1);
4090 	if (IS_ERR_OR_NULL(di)) {
4091 		if (!di)
4092 			ret = -ENOENT;
4093 		else
4094 			ret = PTR_ERR(di);
4095 		goto out;
4096 	}
4097 
4098 	leaf = path->nodes[0];
4099 	btrfs_dir_item_key_to_cpu(leaf, di, &key);
4100 	WARN_ON(key.type != BTRFS_ROOT_ITEM_KEY || key.objectid != objectid);
4101 	ret = btrfs_delete_one_dir_name(trans, root, path, di);
4102 	if (ret) {
4103 		btrfs_abort_transaction(trans, ret);
4104 		goto out;
4105 	}
4106 	btrfs_release_path(path);
4107 
4108 	ret = btrfs_del_root_ref(trans, fs_info, objectid,
4109 				 root->root_key.objectid, dir_ino,
4110 				 &index, name, name_len);
4111 	if (ret < 0) {
4112 		if (ret != -ENOENT) {
4113 			btrfs_abort_transaction(trans, ret);
4114 			goto out;
4115 		}
4116 		di = btrfs_search_dir_index_item(root, path, dir_ino,
4117 						 name, name_len);
4118 		if (IS_ERR_OR_NULL(di)) {
4119 			if (!di)
4120 				ret = -ENOENT;
4121 			else
4122 				ret = PTR_ERR(di);
4123 			btrfs_abort_transaction(trans, ret);
4124 			goto out;
4125 		}
4126 
4127 		leaf = path->nodes[0];
4128 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
4129 		btrfs_release_path(path);
4130 		index = key.offset;
4131 	}
4132 	btrfs_release_path(path);
4133 
4134 	ret = btrfs_delete_delayed_dir_index(trans, fs_info, BTRFS_I(dir), index);
4135 	if (ret) {
4136 		btrfs_abort_transaction(trans, ret);
4137 		goto out;
4138 	}
4139 
4140 	btrfs_i_size_write(dir, dir->i_size - name_len * 2);
4141 	inode_inc_iversion(dir);
4142 	dir->i_mtime = dir->i_ctime = current_time(dir);
4143 	ret = btrfs_update_inode_fallback(trans, root, dir);
4144 	if (ret)
4145 		btrfs_abort_transaction(trans, ret);
4146 out:
4147 	btrfs_free_path(path);
4148 	return ret;
4149 }
4150 
4151 static int btrfs_rmdir(struct inode *dir, struct dentry *dentry)
4152 {
4153 	struct inode *inode = d_inode(dentry);
4154 	int err = 0;
4155 	struct btrfs_root *root = BTRFS_I(dir)->root;
4156 	struct btrfs_trans_handle *trans;
4157 	u64 last_unlink_trans;
4158 
4159 	if (inode->i_size > BTRFS_EMPTY_DIR_SIZE)
4160 		return -ENOTEMPTY;
4161 	if (btrfs_ino(BTRFS_I(inode)) == BTRFS_FIRST_FREE_OBJECTID)
4162 		return -EPERM;
4163 
4164 	trans = __unlink_start_trans(dir);
4165 	if (IS_ERR(trans))
4166 		return PTR_ERR(trans);
4167 
4168 	if (unlikely(btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
4169 		err = btrfs_unlink_subvol(trans, root, dir,
4170 					  BTRFS_I(inode)->location.objectid,
4171 					  dentry->d_name.name,
4172 					  dentry->d_name.len);
4173 		goto out;
4174 	}
4175 
4176 	err = btrfs_orphan_add(trans, inode);
4177 	if (err)
4178 		goto out;
4179 
4180 	last_unlink_trans = BTRFS_I(inode)->last_unlink_trans;
4181 
4182 	/* now the directory is empty */
4183 	err = btrfs_unlink_inode(trans, root, BTRFS_I(dir),
4184 			BTRFS_I(d_inode(dentry)), dentry->d_name.name,
4185 			dentry->d_name.len);
4186 	if (!err) {
4187 		btrfs_i_size_write(inode, 0);
4188 		/*
4189 		 * Propagate the last_unlink_trans value of the deleted dir to
4190 		 * its parent directory. This is to prevent an unrecoverable
4191 		 * log tree in the case we do something like this:
4192 		 * 1) create dir foo
4193 		 * 2) create snapshot under dir foo
4194 		 * 3) delete the snapshot
4195 		 * 4) rmdir foo
4196 		 * 5) mkdir foo
4197 		 * 6) fsync foo or some file inside foo
4198 		 */
4199 		if (last_unlink_trans >= trans->transid)
4200 			BTRFS_I(dir)->last_unlink_trans = last_unlink_trans;
4201 	}
4202 out:
4203 	btrfs_end_transaction(trans);
4204 	btrfs_btree_balance_dirty(root->fs_info);
4205 
4206 	return err;
4207 }
4208 
4209 static int truncate_space_check(struct btrfs_trans_handle *trans,
4210 				struct btrfs_root *root,
4211 				u64 bytes_deleted)
4212 {
4213 	struct btrfs_fs_info *fs_info = root->fs_info;
4214 	int ret;
4215 
4216 	/*
4217 	 * This is only used to apply pressure to the enospc system, we don't
4218 	 * intend to use this reservation at all.
4219 	 */
4220 	bytes_deleted = btrfs_csum_bytes_to_leaves(fs_info, bytes_deleted);
4221 	bytes_deleted *= fs_info->nodesize;
4222 	ret = btrfs_block_rsv_add(root, &fs_info->trans_block_rsv,
4223 				  bytes_deleted, BTRFS_RESERVE_NO_FLUSH);
4224 	if (!ret) {
4225 		trace_btrfs_space_reservation(fs_info, "transaction",
4226 					      trans->transid,
4227 					      bytes_deleted, 1);
4228 		trans->bytes_reserved += bytes_deleted;
4229 	}
4230 	return ret;
4231 
4232 }
4233 
4234 static int truncate_inline_extent(struct inode *inode,
4235 				  struct btrfs_path *path,
4236 				  struct btrfs_key *found_key,
4237 				  const u64 item_end,
4238 				  const u64 new_size)
4239 {
4240 	struct extent_buffer *leaf = path->nodes[0];
4241 	int slot = path->slots[0];
4242 	struct btrfs_file_extent_item *fi;
4243 	u32 size = (u32)(new_size - found_key->offset);
4244 	struct btrfs_root *root = BTRFS_I(inode)->root;
4245 
4246 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
4247 
4248 	if (btrfs_file_extent_compression(leaf, fi) != BTRFS_COMPRESS_NONE) {
4249 		loff_t offset = new_size;
4250 		loff_t page_end = ALIGN(offset, PAGE_SIZE);
4251 
4252 		/*
4253 		 * Zero out the remaining of the last page of our inline extent,
4254 		 * instead of directly truncating our inline extent here - that
4255 		 * would be much more complex (decompressing all the data, then
4256 		 * compressing the truncated data, which might be bigger than
4257 		 * the size of the inline extent, resize the extent, etc).
4258 		 * We release the path because to get the page we might need to
4259 		 * read the extent item from disk (data not in the page cache).
4260 		 */
4261 		btrfs_release_path(path);
4262 		return btrfs_truncate_block(inode, offset, page_end - offset,
4263 					0);
4264 	}
4265 
4266 	btrfs_set_file_extent_ram_bytes(leaf, fi, size);
4267 	size = btrfs_file_extent_calc_inline_size(size);
4268 	btrfs_truncate_item(root->fs_info, path, size, 1);
4269 
4270 	if (test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4271 		inode_sub_bytes(inode, item_end + 1 - new_size);
4272 
4273 	return 0;
4274 }
4275 
4276 /*
4277  * this can truncate away extent items, csum items and directory items.
4278  * It starts at a high offset and removes keys until it can't find
4279  * any higher than new_size
4280  *
4281  * csum items that cross the new i_size are truncated to the new size
4282  * as well.
4283  *
4284  * min_type is the minimum key type to truncate down to.  If set to 0, this
4285  * will kill all the items on this inode, including the INODE_ITEM_KEY.
4286  */
4287 int btrfs_truncate_inode_items(struct btrfs_trans_handle *trans,
4288 			       struct btrfs_root *root,
4289 			       struct inode *inode,
4290 			       u64 new_size, u32 min_type)
4291 {
4292 	struct btrfs_fs_info *fs_info = root->fs_info;
4293 	struct btrfs_path *path;
4294 	struct extent_buffer *leaf;
4295 	struct btrfs_file_extent_item *fi;
4296 	struct btrfs_key key;
4297 	struct btrfs_key found_key;
4298 	u64 extent_start = 0;
4299 	u64 extent_num_bytes = 0;
4300 	u64 extent_offset = 0;
4301 	u64 item_end = 0;
4302 	u64 last_size = new_size;
4303 	u32 found_type = (u8)-1;
4304 	int found_extent;
4305 	int del_item;
4306 	int pending_del_nr = 0;
4307 	int pending_del_slot = 0;
4308 	int extent_type = -1;
4309 	int ret;
4310 	int err = 0;
4311 	u64 ino = btrfs_ino(BTRFS_I(inode));
4312 	u64 bytes_deleted = 0;
4313 	bool be_nice = 0;
4314 	bool should_throttle = 0;
4315 	bool should_end = 0;
4316 
4317 	BUG_ON(new_size > 0 && min_type != BTRFS_EXTENT_DATA_KEY);
4318 
4319 	/*
4320 	 * for non-free space inodes and ref cows, we want to back off from
4321 	 * time to time
4322 	 */
4323 	if (!btrfs_is_free_space_inode(inode) &&
4324 	    test_bit(BTRFS_ROOT_REF_COWS, &root->state))
4325 		be_nice = 1;
4326 
4327 	path = btrfs_alloc_path();
4328 	if (!path)
4329 		return -ENOMEM;
4330 	path->reada = READA_BACK;
4331 
4332 	/*
4333 	 * We want to drop from the next block forward in case this new size is
4334 	 * not block aligned since we will be keeping the last block of the
4335 	 * extent just the way it is.
4336 	 */
4337 	if (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4338 	    root == fs_info->tree_root)
4339 		btrfs_drop_extent_cache(inode, ALIGN(new_size,
4340 					fs_info->sectorsize),
4341 					(u64)-1, 0);
4342 
4343 	/*
4344 	 * This function is also used to drop the items in the log tree before
4345 	 * we relog the inode, so if root != BTRFS_I(inode)->root, it means
4346 	 * it is used to drop the loged items. So we shouldn't kill the delayed
4347 	 * items.
4348 	 */
4349 	if (min_type == 0 && root == BTRFS_I(inode)->root)
4350 		btrfs_kill_delayed_inode_items(BTRFS_I(inode));
4351 
4352 	key.objectid = ino;
4353 	key.offset = (u64)-1;
4354 	key.type = (u8)-1;
4355 
4356 search_again:
4357 	/*
4358 	 * with a 16K leaf size and 128MB extents, you can actually queue
4359 	 * up a huge file in a single leaf.  Most of the time that
4360 	 * bytes_deleted is > 0, it will be huge by the time we get here
4361 	 */
4362 	if (be_nice && bytes_deleted > SZ_32M) {
4363 		if (btrfs_should_end_transaction(trans)) {
4364 			err = -EAGAIN;
4365 			goto error;
4366 		}
4367 	}
4368 
4369 
4370 	path->leave_spinning = 1;
4371 	ret = btrfs_search_slot(trans, root, &key, path, -1, 1);
4372 	if (ret < 0) {
4373 		err = ret;
4374 		goto out;
4375 	}
4376 
4377 	if (ret > 0) {
4378 		/* there are no items in the tree for us to truncate, we're
4379 		 * done
4380 		 */
4381 		if (path->slots[0] == 0)
4382 			goto out;
4383 		path->slots[0]--;
4384 	}
4385 
4386 	while (1) {
4387 		fi = NULL;
4388 		leaf = path->nodes[0];
4389 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
4390 		found_type = found_key.type;
4391 
4392 		if (found_key.objectid != ino)
4393 			break;
4394 
4395 		if (found_type < min_type)
4396 			break;
4397 
4398 		item_end = found_key.offset;
4399 		if (found_type == BTRFS_EXTENT_DATA_KEY) {
4400 			fi = btrfs_item_ptr(leaf, path->slots[0],
4401 					    struct btrfs_file_extent_item);
4402 			extent_type = btrfs_file_extent_type(leaf, fi);
4403 			if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4404 				item_end +=
4405 				    btrfs_file_extent_num_bytes(leaf, fi);
4406 			} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4407 				item_end += btrfs_file_extent_inline_len(leaf,
4408 							 path->slots[0], fi);
4409 			}
4410 			item_end--;
4411 		}
4412 		if (found_type > min_type) {
4413 			del_item = 1;
4414 		} else {
4415 			if (item_end < new_size) {
4416 				/*
4417 				 * With NO_HOLES mode, for the following mapping
4418 				 *
4419 				 * [0-4k][hole][8k-12k]
4420 				 *
4421 				 * if truncating isize down to 6k, it ends up
4422 				 * isize being 8k.
4423 				 */
4424 				if (btrfs_fs_incompat(root->fs_info, NO_HOLES))
4425 					last_size = new_size;
4426 				break;
4427 			}
4428 			if (found_key.offset >= new_size)
4429 				del_item = 1;
4430 			else
4431 				del_item = 0;
4432 		}
4433 		found_extent = 0;
4434 		/* FIXME, shrink the extent if the ref count is only 1 */
4435 		if (found_type != BTRFS_EXTENT_DATA_KEY)
4436 			goto delete;
4437 
4438 		if (del_item)
4439 			last_size = found_key.offset;
4440 		else
4441 			last_size = new_size;
4442 
4443 		if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
4444 			u64 num_dec;
4445 			extent_start = btrfs_file_extent_disk_bytenr(leaf, fi);
4446 			if (!del_item) {
4447 				u64 orig_num_bytes =
4448 					btrfs_file_extent_num_bytes(leaf, fi);
4449 				extent_num_bytes = ALIGN(new_size -
4450 						found_key.offset,
4451 						fs_info->sectorsize);
4452 				btrfs_set_file_extent_num_bytes(leaf, fi,
4453 							 extent_num_bytes);
4454 				num_dec = (orig_num_bytes -
4455 					   extent_num_bytes);
4456 				if (test_bit(BTRFS_ROOT_REF_COWS,
4457 					     &root->state) &&
4458 				    extent_start != 0)
4459 					inode_sub_bytes(inode, num_dec);
4460 				btrfs_mark_buffer_dirty(leaf);
4461 			} else {
4462 				extent_num_bytes =
4463 					btrfs_file_extent_disk_num_bytes(leaf,
4464 									 fi);
4465 				extent_offset = found_key.offset -
4466 					btrfs_file_extent_offset(leaf, fi);
4467 
4468 				/* FIXME blocksize != 4096 */
4469 				num_dec = btrfs_file_extent_num_bytes(leaf, fi);
4470 				if (extent_start != 0) {
4471 					found_extent = 1;
4472 					if (test_bit(BTRFS_ROOT_REF_COWS,
4473 						     &root->state))
4474 						inode_sub_bytes(inode, num_dec);
4475 				}
4476 			}
4477 		} else if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
4478 			/*
4479 			 * we can't truncate inline items that have had
4480 			 * special encodings
4481 			 */
4482 			if (!del_item &&
4483 			    btrfs_file_extent_encryption(leaf, fi) == 0 &&
4484 			    btrfs_file_extent_other_encoding(leaf, fi) == 0) {
4485 
4486 				/*
4487 				 * Need to release path in order to truncate a
4488 				 * compressed extent. So delete any accumulated
4489 				 * extent items so far.
4490 				 */
4491 				if (btrfs_file_extent_compression(leaf, fi) !=
4492 				    BTRFS_COMPRESS_NONE && pending_del_nr) {
4493 					err = btrfs_del_items(trans, root, path,
4494 							      pending_del_slot,
4495 							      pending_del_nr);
4496 					if (err) {
4497 						btrfs_abort_transaction(trans,
4498 									err);
4499 						goto error;
4500 					}
4501 					pending_del_nr = 0;
4502 				}
4503 
4504 				err = truncate_inline_extent(inode, path,
4505 							     &found_key,
4506 							     item_end,
4507 							     new_size);
4508 				if (err) {
4509 					btrfs_abort_transaction(trans, err);
4510 					goto error;
4511 				}
4512 			} else if (test_bit(BTRFS_ROOT_REF_COWS,
4513 					    &root->state)) {
4514 				inode_sub_bytes(inode, item_end + 1 - new_size);
4515 			}
4516 		}
4517 delete:
4518 		if (del_item) {
4519 			if (!pending_del_nr) {
4520 				/* no pending yet, add ourselves */
4521 				pending_del_slot = path->slots[0];
4522 				pending_del_nr = 1;
4523 			} else if (pending_del_nr &&
4524 				   path->slots[0] + 1 == pending_del_slot) {
4525 				/* hop on the pending chunk */
4526 				pending_del_nr++;
4527 				pending_del_slot = path->slots[0];
4528 			} else {
4529 				BUG();
4530 			}
4531 		} else {
4532 			break;
4533 		}
4534 		should_throttle = 0;
4535 
4536 		if (found_extent &&
4537 		    (test_bit(BTRFS_ROOT_REF_COWS, &root->state) ||
4538 		     root == fs_info->tree_root)) {
4539 			btrfs_set_path_blocking(path);
4540 			bytes_deleted += extent_num_bytes;
4541 			ret = btrfs_free_extent(trans, fs_info, extent_start,
4542 						extent_num_bytes, 0,
4543 						btrfs_header_owner(leaf),
4544 						ino, extent_offset);
4545 			BUG_ON(ret);
4546 			if (btrfs_should_throttle_delayed_refs(trans, fs_info))
4547 				btrfs_async_run_delayed_refs(fs_info,
4548 					trans->delayed_ref_updates * 2,
4549 					trans->transid, 0);
4550 			if (be_nice) {
4551 				if (truncate_space_check(trans, root,
4552 							 extent_num_bytes)) {
4553 					should_end = 1;
4554 				}
4555 				if (btrfs_should_throttle_delayed_refs(trans,
4556 								       fs_info))
4557 					should_throttle = 1;
4558 			}
4559 		}
4560 
4561 		if (found_type == BTRFS_INODE_ITEM_KEY)
4562 			break;
4563 
4564 		if (path->slots[0] == 0 ||
4565 		    path->slots[0] != pending_del_slot ||
4566 		    should_throttle || should_end) {
4567 			if (pending_del_nr) {
4568 				ret = btrfs_del_items(trans, root, path,
4569 						pending_del_slot,
4570 						pending_del_nr);
4571 				if (ret) {
4572 					btrfs_abort_transaction(trans, ret);
4573 					goto error;
4574 				}
4575 				pending_del_nr = 0;
4576 			}
4577 			btrfs_release_path(path);
4578 			if (should_throttle) {
4579 				unsigned long updates = trans->delayed_ref_updates;
4580 				if (updates) {
4581 					trans->delayed_ref_updates = 0;
4582 					ret = btrfs_run_delayed_refs(trans,
4583 								   fs_info,
4584 								   updates * 2);
4585 					if (ret && !err)
4586 						err = ret;
4587 				}
4588 			}
4589 			/*
4590 			 * if we failed to refill our space rsv, bail out
4591 			 * and let the transaction restart
4592 			 */
4593 			if (should_end) {
4594 				err = -EAGAIN;
4595 				goto error;
4596 			}
4597 			goto search_again;
4598 		} else {
4599 			path->slots[0]--;
4600 		}
4601 	}
4602 out:
4603 	if (pending_del_nr) {
4604 		ret = btrfs_del_items(trans, root, path, pending_del_slot,
4605 				      pending_del_nr);
4606 		if (ret)
4607 			btrfs_abort_transaction(trans, ret);
4608 	}
4609 error:
4610 	if (root->root_key.objectid != BTRFS_TREE_LOG_OBJECTID)
4611 		btrfs_ordered_update_i_size(inode, last_size, NULL);
4612 
4613 	btrfs_free_path(path);
4614 
4615 	if (err == 0) {
4616 		/* only inline file may have last_size != new_size */
4617 		if (new_size >= fs_info->sectorsize ||
4618 		    new_size > fs_info->max_inline)
4619 			ASSERT(last_size == new_size);
4620 	}
4621 
4622 	if (be_nice && bytes_deleted > SZ_32M) {
4623 		unsigned long updates = trans->delayed_ref_updates;
4624 		if (updates) {
4625 			trans->delayed_ref_updates = 0;
4626 			ret = btrfs_run_delayed_refs(trans, fs_info,
4627 						     updates * 2);
4628 			if (ret && !err)
4629 				err = ret;
4630 		}
4631 	}
4632 	return err;
4633 }
4634 
4635 /*
4636  * btrfs_truncate_block - read, zero a chunk and write a block
4637  * @inode - inode that we're zeroing
4638  * @from - the offset to start zeroing
4639  * @len - the length to zero, 0 to zero the entire range respective to the
4640  *	offset
4641  * @front - zero up to the offset instead of from the offset on
4642  *
4643  * This will find the block for the "from" offset and cow the block and zero the
4644  * part we want to zero.  This is used with truncate and hole punching.
4645  */
4646 int btrfs_truncate_block(struct inode *inode, loff_t from, loff_t len,
4647 			int front)
4648 {
4649 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4650 	struct address_space *mapping = inode->i_mapping;
4651 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4652 	struct btrfs_ordered_extent *ordered;
4653 	struct extent_state *cached_state = NULL;
4654 	char *kaddr;
4655 	u32 blocksize = fs_info->sectorsize;
4656 	pgoff_t index = from >> PAGE_SHIFT;
4657 	unsigned offset = from & (blocksize - 1);
4658 	struct page *page;
4659 	gfp_t mask = btrfs_alloc_write_mask(mapping);
4660 	int ret = 0;
4661 	u64 block_start;
4662 	u64 block_end;
4663 
4664 	if ((offset & (blocksize - 1)) == 0 &&
4665 	    (!len || ((len & (blocksize - 1)) == 0)))
4666 		goto out;
4667 
4668 	ret = btrfs_delalloc_reserve_space(inode,
4669 			round_down(from, blocksize), blocksize);
4670 	if (ret)
4671 		goto out;
4672 
4673 again:
4674 	page = find_or_create_page(mapping, index, mask);
4675 	if (!page) {
4676 		btrfs_delalloc_release_space(inode,
4677 				round_down(from, blocksize),
4678 				blocksize);
4679 		ret = -ENOMEM;
4680 		goto out;
4681 	}
4682 
4683 	block_start = round_down(from, blocksize);
4684 	block_end = block_start + blocksize - 1;
4685 
4686 	if (!PageUptodate(page)) {
4687 		ret = btrfs_readpage(NULL, page);
4688 		lock_page(page);
4689 		if (page->mapping != mapping) {
4690 			unlock_page(page);
4691 			put_page(page);
4692 			goto again;
4693 		}
4694 		if (!PageUptodate(page)) {
4695 			ret = -EIO;
4696 			goto out_unlock;
4697 		}
4698 	}
4699 	wait_on_page_writeback(page);
4700 
4701 	lock_extent_bits(io_tree, block_start, block_end, &cached_state);
4702 	set_page_extent_mapped(page);
4703 
4704 	ordered = btrfs_lookup_ordered_extent(inode, block_start);
4705 	if (ordered) {
4706 		unlock_extent_cached(io_tree, block_start, block_end,
4707 				     &cached_state, GFP_NOFS);
4708 		unlock_page(page);
4709 		put_page(page);
4710 		btrfs_start_ordered_extent(inode, ordered, 1);
4711 		btrfs_put_ordered_extent(ordered);
4712 		goto again;
4713 	}
4714 
4715 	clear_extent_bit(&BTRFS_I(inode)->io_tree, block_start, block_end,
4716 			  EXTENT_DIRTY | EXTENT_DELALLOC |
4717 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
4718 			  0, 0, &cached_state, GFP_NOFS);
4719 
4720 	ret = btrfs_set_extent_delalloc(inode, block_start, block_end,
4721 					&cached_state, 0);
4722 	if (ret) {
4723 		unlock_extent_cached(io_tree, block_start, block_end,
4724 				     &cached_state, GFP_NOFS);
4725 		goto out_unlock;
4726 	}
4727 
4728 	if (offset != blocksize) {
4729 		if (!len)
4730 			len = blocksize - offset;
4731 		kaddr = kmap(page);
4732 		if (front)
4733 			memset(kaddr + (block_start - page_offset(page)),
4734 				0, offset);
4735 		else
4736 			memset(kaddr + (block_start - page_offset(page)) +  offset,
4737 				0, len);
4738 		flush_dcache_page(page);
4739 		kunmap(page);
4740 	}
4741 	ClearPageChecked(page);
4742 	set_page_dirty(page);
4743 	unlock_extent_cached(io_tree, block_start, block_end, &cached_state,
4744 			     GFP_NOFS);
4745 
4746 out_unlock:
4747 	if (ret)
4748 		btrfs_delalloc_release_space(inode, block_start,
4749 					     blocksize);
4750 	unlock_page(page);
4751 	put_page(page);
4752 out:
4753 	return ret;
4754 }
4755 
4756 static int maybe_insert_hole(struct btrfs_root *root, struct inode *inode,
4757 			     u64 offset, u64 len)
4758 {
4759 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4760 	struct btrfs_trans_handle *trans;
4761 	int ret;
4762 
4763 	/*
4764 	 * Still need to make sure the inode looks like it's been updated so
4765 	 * that any holes get logged if we fsync.
4766 	 */
4767 	if (btrfs_fs_incompat(fs_info, NO_HOLES)) {
4768 		BTRFS_I(inode)->last_trans = fs_info->generation;
4769 		BTRFS_I(inode)->last_sub_trans = root->log_transid;
4770 		BTRFS_I(inode)->last_log_commit = root->last_log_commit;
4771 		return 0;
4772 	}
4773 
4774 	/*
4775 	 * 1 - for the one we're dropping
4776 	 * 1 - for the one we're adding
4777 	 * 1 - for updating the inode.
4778 	 */
4779 	trans = btrfs_start_transaction(root, 3);
4780 	if (IS_ERR(trans))
4781 		return PTR_ERR(trans);
4782 
4783 	ret = btrfs_drop_extents(trans, root, inode, offset, offset + len, 1);
4784 	if (ret) {
4785 		btrfs_abort_transaction(trans, ret);
4786 		btrfs_end_transaction(trans);
4787 		return ret;
4788 	}
4789 
4790 	ret = btrfs_insert_file_extent(trans, root, btrfs_ino(BTRFS_I(inode)),
4791 			offset, 0, 0, len, 0, len, 0, 0, 0);
4792 	if (ret)
4793 		btrfs_abort_transaction(trans, ret);
4794 	else
4795 		btrfs_update_inode(trans, root, inode);
4796 	btrfs_end_transaction(trans);
4797 	return ret;
4798 }
4799 
4800 /*
4801  * This function puts in dummy file extents for the area we're creating a hole
4802  * for.  So if we are truncating this file to a larger size we need to insert
4803  * these file extents so that btrfs_get_extent will return a EXTENT_MAP_HOLE for
4804  * the range between oldsize and size
4805  */
4806 int btrfs_cont_expand(struct inode *inode, loff_t oldsize, loff_t size)
4807 {
4808 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
4809 	struct btrfs_root *root = BTRFS_I(inode)->root;
4810 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
4811 	struct extent_map *em = NULL;
4812 	struct extent_state *cached_state = NULL;
4813 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
4814 	u64 hole_start = ALIGN(oldsize, fs_info->sectorsize);
4815 	u64 block_end = ALIGN(size, fs_info->sectorsize);
4816 	u64 last_byte;
4817 	u64 cur_offset;
4818 	u64 hole_size;
4819 	int err = 0;
4820 
4821 	/*
4822 	 * If our size started in the middle of a block we need to zero out the
4823 	 * rest of the block before we expand the i_size, otherwise we could
4824 	 * expose stale data.
4825 	 */
4826 	err = btrfs_truncate_block(inode, oldsize, 0, 0);
4827 	if (err)
4828 		return err;
4829 
4830 	if (size <= hole_start)
4831 		return 0;
4832 
4833 	while (1) {
4834 		struct btrfs_ordered_extent *ordered;
4835 
4836 		lock_extent_bits(io_tree, hole_start, block_end - 1,
4837 				 &cached_state);
4838 		ordered = btrfs_lookup_ordered_range(inode, hole_start,
4839 						     block_end - hole_start);
4840 		if (!ordered)
4841 			break;
4842 		unlock_extent_cached(io_tree, hole_start, block_end - 1,
4843 				     &cached_state, GFP_NOFS);
4844 		btrfs_start_ordered_extent(inode, ordered, 1);
4845 		btrfs_put_ordered_extent(ordered);
4846 	}
4847 
4848 	cur_offset = hole_start;
4849 	while (1) {
4850 		em = btrfs_get_extent(inode, NULL, 0, cur_offset,
4851 				block_end - cur_offset, 0);
4852 		if (IS_ERR(em)) {
4853 			err = PTR_ERR(em);
4854 			em = NULL;
4855 			break;
4856 		}
4857 		last_byte = min(extent_map_end(em), block_end);
4858 		last_byte = ALIGN(last_byte, fs_info->sectorsize);
4859 		if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags)) {
4860 			struct extent_map *hole_em;
4861 			hole_size = last_byte - cur_offset;
4862 
4863 			err = maybe_insert_hole(root, inode, cur_offset,
4864 						hole_size);
4865 			if (err)
4866 				break;
4867 			btrfs_drop_extent_cache(inode, cur_offset,
4868 						cur_offset + hole_size - 1, 0);
4869 			hole_em = alloc_extent_map();
4870 			if (!hole_em) {
4871 				set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
4872 					&BTRFS_I(inode)->runtime_flags);
4873 				goto next;
4874 			}
4875 			hole_em->start = cur_offset;
4876 			hole_em->len = hole_size;
4877 			hole_em->orig_start = cur_offset;
4878 
4879 			hole_em->block_start = EXTENT_MAP_HOLE;
4880 			hole_em->block_len = 0;
4881 			hole_em->orig_block_len = 0;
4882 			hole_em->ram_bytes = hole_size;
4883 			hole_em->bdev = fs_info->fs_devices->latest_bdev;
4884 			hole_em->compress_type = BTRFS_COMPRESS_NONE;
4885 			hole_em->generation = fs_info->generation;
4886 
4887 			while (1) {
4888 				write_lock(&em_tree->lock);
4889 				err = add_extent_mapping(em_tree, hole_em, 1);
4890 				write_unlock(&em_tree->lock);
4891 				if (err != -EEXIST)
4892 					break;
4893 				btrfs_drop_extent_cache(inode, cur_offset,
4894 							cur_offset +
4895 							hole_size - 1, 0);
4896 			}
4897 			free_extent_map(hole_em);
4898 		}
4899 next:
4900 		free_extent_map(em);
4901 		em = NULL;
4902 		cur_offset = last_byte;
4903 		if (cur_offset >= block_end)
4904 			break;
4905 	}
4906 	free_extent_map(em);
4907 	unlock_extent_cached(io_tree, hole_start, block_end - 1, &cached_state,
4908 			     GFP_NOFS);
4909 	return err;
4910 }
4911 
4912 static int btrfs_setsize(struct inode *inode, struct iattr *attr)
4913 {
4914 	struct btrfs_root *root = BTRFS_I(inode)->root;
4915 	struct btrfs_trans_handle *trans;
4916 	loff_t oldsize = i_size_read(inode);
4917 	loff_t newsize = attr->ia_size;
4918 	int mask = attr->ia_valid;
4919 	int ret;
4920 
4921 	/*
4922 	 * The regular truncate() case without ATTR_CTIME and ATTR_MTIME is a
4923 	 * special case where we need to update the times despite not having
4924 	 * these flags set.  For all other operations the VFS set these flags
4925 	 * explicitly if it wants a timestamp update.
4926 	 */
4927 	if (newsize != oldsize) {
4928 		inode_inc_iversion(inode);
4929 		if (!(mask & (ATTR_CTIME | ATTR_MTIME)))
4930 			inode->i_ctime = inode->i_mtime =
4931 				current_time(inode);
4932 	}
4933 
4934 	if (newsize > oldsize) {
4935 		/*
4936 		 * Don't do an expanding truncate while snapshoting is ongoing.
4937 		 * This is to ensure the snapshot captures a fully consistent
4938 		 * state of this file - if the snapshot captures this expanding
4939 		 * truncation, it must capture all writes that happened before
4940 		 * this truncation.
4941 		 */
4942 		btrfs_wait_for_snapshot_creation(root);
4943 		ret = btrfs_cont_expand(inode, oldsize, newsize);
4944 		if (ret) {
4945 			btrfs_end_write_no_snapshoting(root);
4946 			return ret;
4947 		}
4948 
4949 		trans = btrfs_start_transaction(root, 1);
4950 		if (IS_ERR(trans)) {
4951 			btrfs_end_write_no_snapshoting(root);
4952 			return PTR_ERR(trans);
4953 		}
4954 
4955 		i_size_write(inode, newsize);
4956 		btrfs_ordered_update_i_size(inode, i_size_read(inode), NULL);
4957 		pagecache_isize_extended(inode, oldsize, newsize);
4958 		ret = btrfs_update_inode(trans, root, inode);
4959 		btrfs_end_write_no_snapshoting(root);
4960 		btrfs_end_transaction(trans);
4961 	} else {
4962 
4963 		/*
4964 		 * We're truncating a file that used to have good data down to
4965 		 * zero. Make sure it gets into the ordered flush list so that
4966 		 * any new writes get down to disk quickly.
4967 		 */
4968 		if (newsize == 0)
4969 			set_bit(BTRFS_INODE_ORDERED_DATA_CLOSE,
4970 				&BTRFS_I(inode)->runtime_flags);
4971 
4972 		/*
4973 		 * 1 for the orphan item we're going to add
4974 		 * 1 for the orphan item deletion.
4975 		 */
4976 		trans = btrfs_start_transaction(root, 2);
4977 		if (IS_ERR(trans))
4978 			return PTR_ERR(trans);
4979 
4980 		/*
4981 		 * We need to do this in case we fail at _any_ point during the
4982 		 * actual truncate.  Once we do the truncate_setsize we could
4983 		 * invalidate pages which forces any outstanding ordered io to
4984 		 * be instantly completed which will give us extents that need
4985 		 * to be truncated.  If we fail to get an orphan inode down we
4986 		 * could have left over extents that were never meant to live,
4987 		 * so we need to guarantee from this point on that everything
4988 		 * will be consistent.
4989 		 */
4990 		ret = btrfs_orphan_add(trans, inode);
4991 		btrfs_end_transaction(trans);
4992 		if (ret)
4993 			return ret;
4994 
4995 		/* we don't support swapfiles, so vmtruncate shouldn't fail */
4996 		truncate_setsize(inode, newsize);
4997 
4998 		/* Disable nonlocked read DIO to avoid the end less truncate */
4999 		btrfs_inode_block_unlocked_dio(inode);
5000 		inode_dio_wait(inode);
5001 		btrfs_inode_resume_unlocked_dio(inode);
5002 
5003 		ret = btrfs_truncate(inode);
5004 		if (ret && inode->i_nlink) {
5005 			int err;
5006 
5007 			/* To get a stable disk_i_size */
5008 			err = btrfs_wait_ordered_range(inode, 0, (u64)-1);
5009 			if (err) {
5010 				btrfs_orphan_del(NULL, inode);
5011 				return err;
5012 			}
5013 
5014 			/*
5015 			 * failed to truncate, disk_i_size is only adjusted down
5016 			 * as we remove extents, so it should represent the true
5017 			 * size of the inode, so reset the in memory size and
5018 			 * delete our orphan entry.
5019 			 */
5020 			trans = btrfs_join_transaction(root);
5021 			if (IS_ERR(trans)) {
5022 				btrfs_orphan_del(NULL, inode);
5023 				return ret;
5024 			}
5025 			i_size_write(inode, BTRFS_I(inode)->disk_i_size);
5026 			err = btrfs_orphan_del(trans, inode);
5027 			if (err)
5028 				btrfs_abort_transaction(trans, err);
5029 			btrfs_end_transaction(trans);
5030 		}
5031 	}
5032 
5033 	return ret;
5034 }
5035 
5036 static int btrfs_setattr(struct dentry *dentry, struct iattr *attr)
5037 {
5038 	struct inode *inode = d_inode(dentry);
5039 	struct btrfs_root *root = BTRFS_I(inode)->root;
5040 	int err;
5041 
5042 	if (btrfs_root_readonly(root))
5043 		return -EROFS;
5044 
5045 	err = setattr_prepare(dentry, attr);
5046 	if (err)
5047 		return err;
5048 
5049 	if (S_ISREG(inode->i_mode) && (attr->ia_valid & ATTR_SIZE)) {
5050 		err = btrfs_setsize(inode, attr);
5051 		if (err)
5052 			return err;
5053 	}
5054 
5055 	if (attr->ia_valid) {
5056 		setattr_copy(inode, attr);
5057 		inode_inc_iversion(inode);
5058 		err = btrfs_dirty_inode(inode);
5059 
5060 		if (!err && attr->ia_valid & ATTR_MODE)
5061 			err = posix_acl_chmod(inode, inode->i_mode);
5062 	}
5063 
5064 	return err;
5065 }
5066 
5067 /*
5068  * While truncating the inode pages during eviction, we get the VFS calling
5069  * btrfs_invalidatepage() against each page of the inode. This is slow because
5070  * the calls to btrfs_invalidatepage() result in a huge amount of calls to
5071  * lock_extent_bits() and clear_extent_bit(), which keep merging and splitting
5072  * extent_state structures over and over, wasting lots of time.
5073  *
5074  * Therefore if the inode is being evicted, let btrfs_invalidatepage() skip all
5075  * those expensive operations on a per page basis and do only the ordered io
5076  * finishing, while we release here the extent_map and extent_state structures,
5077  * without the excessive merging and splitting.
5078  */
5079 static void evict_inode_truncate_pages(struct inode *inode)
5080 {
5081 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
5082 	struct extent_map_tree *map_tree = &BTRFS_I(inode)->extent_tree;
5083 	struct rb_node *node;
5084 
5085 	ASSERT(inode->i_state & I_FREEING);
5086 	truncate_inode_pages_final(&inode->i_data);
5087 
5088 	write_lock(&map_tree->lock);
5089 	while (!RB_EMPTY_ROOT(&map_tree->map)) {
5090 		struct extent_map *em;
5091 
5092 		node = rb_first(&map_tree->map);
5093 		em = rb_entry(node, struct extent_map, rb_node);
5094 		clear_bit(EXTENT_FLAG_PINNED, &em->flags);
5095 		clear_bit(EXTENT_FLAG_LOGGING, &em->flags);
5096 		remove_extent_mapping(map_tree, em);
5097 		free_extent_map(em);
5098 		if (need_resched()) {
5099 			write_unlock(&map_tree->lock);
5100 			cond_resched();
5101 			write_lock(&map_tree->lock);
5102 		}
5103 	}
5104 	write_unlock(&map_tree->lock);
5105 
5106 	/*
5107 	 * Keep looping until we have no more ranges in the io tree.
5108 	 * We can have ongoing bios started by readpages (called from readahead)
5109 	 * that have their endio callback (extent_io.c:end_bio_extent_readpage)
5110 	 * still in progress (unlocked the pages in the bio but did not yet
5111 	 * unlocked the ranges in the io tree). Therefore this means some
5112 	 * ranges can still be locked and eviction started because before
5113 	 * submitting those bios, which are executed by a separate task (work
5114 	 * queue kthread), inode references (inode->i_count) were not taken
5115 	 * (which would be dropped in the end io callback of each bio).
5116 	 * Therefore here we effectively end up waiting for those bios and
5117 	 * anyone else holding locked ranges without having bumped the inode's
5118 	 * reference count - if we don't do it, when they access the inode's
5119 	 * io_tree to unlock a range it may be too late, leading to an
5120 	 * use-after-free issue.
5121 	 */
5122 	spin_lock(&io_tree->lock);
5123 	while (!RB_EMPTY_ROOT(&io_tree->state)) {
5124 		struct extent_state *state;
5125 		struct extent_state *cached_state = NULL;
5126 		u64 start;
5127 		u64 end;
5128 
5129 		node = rb_first(&io_tree->state);
5130 		state = rb_entry(node, struct extent_state, rb_node);
5131 		start = state->start;
5132 		end = state->end;
5133 		spin_unlock(&io_tree->lock);
5134 
5135 		lock_extent_bits(io_tree, start, end, &cached_state);
5136 
5137 		/*
5138 		 * If still has DELALLOC flag, the extent didn't reach disk,
5139 		 * and its reserved space won't be freed by delayed_ref.
5140 		 * So we need to free its reserved space here.
5141 		 * (Refer to comment in btrfs_invalidatepage, case 2)
5142 		 *
5143 		 * Note, end is the bytenr of last byte, so we need + 1 here.
5144 		 */
5145 		if (state->state & EXTENT_DELALLOC)
5146 			btrfs_qgroup_free_data(inode, start, end - start + 1);
5147 
5148 		clear_extent_bit(io_tree, start, end,
5149 				 EXTENT_LOCKED | EXTENT_DIRTY |
5150 				 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
5151 				 EXTENT_DEFRAG, 1, 1,
5152 				 &cached_state, GFP_NOFS);
5153 
5154 		cond_resched();
5155 		spin_lock(&io_tree->lock);
5156 	}
5157 	spin_unlock(&io_tree->lock);
5158 }
5159 
5160 void btrfs_evict_inode(struct inode *inode)
5161 {
5162 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5163 	struct btrfs_trans_handle *trans;
5164 	struct btrfs_root *root = BTRFS_I(inode)->root;
5165 	struct btrfs_block_rsv *rsv, *global_rsv;
5166 	int steal_from_global = 0;
5167 	u64 min_size;
5168 	int ret;
5169 
5170 	trace_btrfs_inode_evict(inode);
5171 
5172 	if (!root) {
5173 		kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
5174 		return;
5175 	}
5176 
5177 	min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
5178 
5179 	evict_inode_truncate_pages(inode);
5180 
5181 	if (inode->i_nlink &&
5182 	    ((btrfs_root_refs(&root->root_item) != 0 &&
5183 	      root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID) ||
5184 	     btrfs_is_free_space_inode(inode)))
5185 		goto no_delete;
5186 
5187 	if (is_bad_inode(inode)) {
5188 		btrfs_orphan_del(NULL, inode);
5189 		goto no_delete;
5190 	}
5191 	/* do we really want it for ->i_nlink > 0 and zero btrfs_root_refs? */
5192 	if (!special_file(inode->i_mode))
5193 		btrfs_wait_ordered_range(inode, 0, (u64)-1);
5194 
5195 	btrfs_free_io_failure_record(inode, 0, (u64)-1);
5196 
5197 	if (test_bit(BTRFS_FS_LOG_RECOVERING, &fs_info->flags)) {
5198 		BUG_ON(test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
5199 				 &BTRFS_I(inode)->runtime_flags));
5200 		goto no_delete;
5201 	}
5202 
5203 	if (inode->i_nlink > 0) {
5204 		BUG_ON(btrfs_root_refs(&root->root_item) != 0 &&
5205 		       root->root_key.objectid != BTRFS_ROOT_TREE_OBJECTID);
5206 		goto no_delete;
5207 	}
5208 
5209 	ret = btrfs_commit_inode_delayed_inode(BTRFS_I(inode));
5210 	if (ret) {
5211 		btrfs_orphan_del(NULL, inode);
5212 		goto no_delete;
5213 	}
5214 
5215 	rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
5216 	if (!rsv) {
5217 		btrfs_orphan_del(NULL, inode);
5218 		goto no_delete;
5219 	}
5220 	rsv->size = min_size;
5221 	rsv->failfast = 1;
5222 	global_rsv = &fs_info->global_block_rsv;
5223 
5224 	btrfs_i_size_write(inode, 0);
5225 
5226 	/*
5227 	 * This is a bit simpler than btrfs_truncate since we've already
5228 	 * reserved our space for our orphan item in the unlink, so we just
5229 	 * need to reserve some slack space in case we add bytes and update
5230 	 * inode item when doing the truncate.
5231 	 */
5232 	while (1) {
5233 		ret = btrfs_block_rsv_refill(root, rsv, min_size,
5234 					     BTRFS_RESERVE_FLUSH_LIMIT);
5235 
5236 		/*
5237 		 * Try and steal from the global reserve since we will
5238 		 * likely not use this space anyway, we want to try as
5239 		 * hard as possible to get this to work.
5240 		 */
5241 		if (ret)
5242 			steal_from_global++;
5243 		else
5244 			steal_from_global = 0;
5245 		ret = 0;
5246 
5247 		/*
5248 		 * steal_from_global == 0: we reserved stuff, hooray!
5249 		 * steal_from_global == 1: we didn't reserve stuff, boo!
5250 		 * steal_from_global == 2: we've committed, still not a lot of
5251 		 * room but maybe we'll have room in the global reserve this
5252 		 * time.
5253 		 * steal_from_global == 3: abandon all hope!
5254 		 */
5255 		if (steal_from_global > 2) {
5256 			btrfs_warn(fs_info,
5257 				   "Could not get space for a delete, will truncate on mount %d",
5258 				   ret);
5259 			btrfs_orphan_del(NULL, inode);
5260 			btrfs_free_block_rsv(fs_info, rsv);
5261 			goto no_delete;
5262 		}
5263 
5264 		trans = btrfs_join_transaction(root);
5265 		if (IS_ERR(trans)) {
5266 			btrfs_orphan_del(NULL, inode);
5267 			btrfs_free_block_rsv(fs_info, rsv);
5268 			goto no_delete;
5269 		}
5270 
5271 		/*
5272 		 * We can't just steal from the global reserve, we need to make
5273 		 * sure there is room to do it, if not we need to commit and try
5274 		 * again.
5275 		 */
5276 		if (steal_from_global) {
5277 			if (!btrfs_check_space_for_delayed_refs(trans, fs_info))
5278 				ret = btrfs_block_rsv_migrate(global_rsv, rsv,
5279 							      min_size, 0);
5280 			else
5281 				ret = -ENOSPC;
5282 		}
5283 
5284 		/*
5285 		 * Couldn't steal from the global reserve, we have too much
5286 		 * pending stuff built up, commit the transaction and try it
5287 		 * again.
5288 		 */
5289 		if (ret) {
5290 			ret = btrfs_commit_transaction(trans);
5291 			if (ret) {
5292 				btrfs_orphan_del(NULL, inode);
5293 				btrfs_free_block_rsv(fs_info, rsv);
5294 				goto no_delete;
5295 			}
5296 			continue;
5297 		} else {
5298 			steal_from_global = 0;
5299 		}
5300 
5301 		trans->block_rsv = rsv;
5302 
5303 		ret = btrfs_truncate_inode_items(trans, root, inode, 0, 0);
5304 		if (ret != -ENOSPC && ret != -EAGAIN)
5305 			break;
5306 
5307 		trans->block_rsv = &fs_info->trans_block_rsv;
5308 		btrfs_end_transaction(trans);
5309 		trans = NULL;
5310 		btrfs_btree_balance_dirty(fs_info);
5311 	}
5312 
5313 	btrfs_free_block_rsv(fs_info, rsv);
5314 
5315 	/*
5316 	 * Errors here aren't a big deal, it just means we leave orphan items
5317 	 * in the tree.  They will be cleaned up on the next mount.
5318 	 */
5319 	if (ret == 0) {
5320 		trans->block_rsv = root->orphan_block_rsv;
5321 		btrfs_orphan_del(trans, inode);
5322 	} else {
5323 		btrfs_orphan_del(NULL, inode);
5324 	}
5325 
5326 	trans->block_rsv = &fs_info->trans_block_rsv;
5327 	if (!(root == fs_info->tree_root ||
5328 	      root->root_key.objectid == BTRFS_TREE_RELOC_OBJECTID))
5329 		btrfs_return_ino(root, btrfs_ino(BTRFS_I(inode)));
5330 
5331 	btrfs_end_transaction(trans);
5332 	btrfs_btree_balance_dirty(fs_info);
5333 no_delete:
5334 	btrfs_remove_delayed_node(BTRFS_I(inode));
5335 	clear_inode(inode);
5336 }
5337 
5338 /*
5339  * this returns the key found in the dir entry in the location pointer.
5340  * If no dir entries were found, location->objectid is 0.
5341  */
5342 static int btrfs_inode_by_name(struct inode *dir, struct dentry *dentry,
5343 			       struct btrfs_key *location)
5344 {
5345 	const char *name = dentry->d_name.name;
5346 	int namelen = dentry->d_name.len;
5347 	struct btrfs_dir_item *di;
5348 	struct btrfs_path *path;
5349 	struct btrfs_root *root = BTRFS_I(dir)->root;
5350 	int ret = 0;
5351 
5352 	path = btrfs_alloc_path();
5353 	if (!path)
5354 		return -ENOMEM;
5355 
5356 	di = btrfs_lookup_dir_item(NULL, root, path, btrfs_ino(BTRFS_I(dir)),
5357 			name, namelen, 0);
5358 	if (IS_ERR(di))
5359 		ret = PTR_ERR(di);
5360 
5361 	if (IS_ERR_OR_NULL(di))
5362 		goto out_err;
5363 
5364 	btrfs_dir_item_key_to_cpu(path->nodes[0], di, location);
5365 out:
5366 	btrfs_free_path(path);
5367 	return ret;
5368 out_err:
5369 	location->objectid = 0;
5370 	goto out;
5371 }
5372 
5373 /*
5374  * when we hit a tree root in a directory, the btrfs part of the inode
5375  * needs to be changed to reflect the root directory of the tree root.  This
5376  * is kind of like crossing a mount point.
5377  */
5378 static int fixup_tree_root_location(struct btrfs_fs_info *fs_info,
5379 				    struct inode *dir,
5380 				    struct dentry *dentry,
5381 				    struct btrfs_key *location,
5382 				    struct btrfs_root **sub_root)
5383 {
5384 	struct btrfs_path *path;
5385 	struct btrfs_root *new_root;
5386 	struct btrfs_root_ref *ref;
5387 	struct extent_buffer *leaf;
5388 	struct btrfs_key key;
5389 	int ret;
5390 	int err = 0;
5391 
5392 	path = btrfs_alloc_path();
5393 	if (!path) {
5394 		err = -ENOMEM;
5395 		goto out;
5396 	}
5397 
5398 	err = -ENOENT;
5399 	key.objectid = BTRFS_I(dir)->root->root_key.objectid;
5400 	key.type = BTRFS_ROOT_REF_KEY;
5401 	key.offset = location->objectid;
5402 
5403 	ret = btrfs_search_slot(NULL, fs_info->tree_root, &key, path, 0, 0);
5404 	if (ret) {
5405 		if (ret < 0)
5406 			err = ret;
5407 		goto out;
5408 	}
5409 
5410 	leaf = path->nodes[0];
5411 	ref = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_root_ref);
5412 	if (btrfs_root_ref_dirid(leaf, ref) != btrfs_ino(BTRFS_I(dir)) ||
5413 	    btrfs_root_ref_name_len(leaf, ref) != dentry->d_name.len)
5414 		goto out;
5415 
5416 	ret = memcmp_extent_buffer(leaf, dentry->d_name.name,
5417 				   (unsigned long)(ref + 1),
5418 				   dentry->d_name.len);
5419 	if (ret)
5420 		goto out;
5421 
5422 	btrfs_release_path(path);
5423 
5424 	new_root = btrfs_read_fs_root_no_name(fs_info, location);
5425 	if (IS_ERR(new_root)) {
5426 		err = PTR_ERR(new_root);
5427 		goto out;
5428 	}
5429 
5430 	*sub_root = new_root;
5431 	location->objectid = btrfs_root_dirid(&new_root->root_item);
5432 	location->type = BTRFS_INODE_ITEM_KEY;
5433 	location->offset = 0;
5434 	err = 0;
5435 out:
5436 	btrfs_free_path(path);
5437 	return err;
5438 }
5439 
5440 static void inode_tree_add(struct inode *inode)
5441 {
5442 	struct btrfs_root *root = BTRFS_I(inode)->root;
5443 	struct btrfs_inode *entry;
5444 	struct rb_node **p;
5445 	struct rb_node *parent;
5446 	struct rb_node *new = &BTRFS_I(inode)->rb_node;
5447 	u64 ino = btrfs_ino(BTRFS_I(inode));
5448 
5449 	if (inode_unhashed(inode))
5450 		return;
5451 	parent = NULL;
5452 	spin_lock(&root->inode_lock);
5453 	p = &root->inode_tree.rb_node;
5454 	while (*p) {
5455 		parent = *p;
5456 		entry = rb_entry(parent, struct btrfs_inode, rb_node);
5457 
5458 		if (ino < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
5459 			p = &parent->rb_left;
5460 		else if (ino > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
5461 			p = &parent->rb_right;
5462 		else {
5463 			WARN_ON(!(entry->vfs_inode.i_state &
5464 				  (I_WILL_FREE | I_FREEING)));
5465 			rb_replace_node(parent, new, &root->inode_tree);
5466 			RB_CLEAR_NODE(parent);
5467 			spin_unlock(&root->inode_lock);
5468 			return;
5469 		}
5470 	}
5471 	rb_link_node(new, parent, p);
5472 	rb_insert_color(new, &root->inode_tree);
5473 	spin_unlock(&root->inode_lock);
5474 }
5475 
5476 static void inode_tree_del(struct inode *inode)
5477 {
5478 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5479 	struct btrfs_root *root = BTRFS_I(inode)->root;
5480 	int empty = 0;
5481 
5482 	spin_lock(&root->inode_lock);
5483 	if (!RB_EMPTY_NODE(&BTRFS_I(inode)->rb_node)) {
5484 		rb_erase(&BTRFS_I(inode)->rb_node, &root->inode_tree);
5485 		RB_CLEAR_NODE(&BTRFS_I(inode)->rb_node);
5486 		empty = RB_EMPTY_ROOT(&root->inode_tree);
5487 	}
5488 	spin_unlock(&root->inode_lock);
5489 
5490 	if (empty && btrfs_root_refs(&root->root_item) == 0) {
5491 		synchronize_srcu(&fs_info->subvol_srcu);
5492 		spin_lock(&root->inode_lock);
5493 		empty = RB_EMPTY_ROOT(&root->inode_tree);
5494 		spin_unlock(&root->inode_lock);
5495 		if (empty)
5496 			btrfs_add_dead_root(root);
5497 	}
5498 }
5499 
5500 void btrfs_invalidate_inodes(struct btrfs_root *root)
5501 {
5502 	struct btrfs_fs_info *fs_info = root->fs_info;
5503 	struct rb_node *node;
5504 	struct rb_node *prev;
5505 	struct btrfs_inode *entry;
5506 	struct inode *inode;
5507 	u64 objectid = 0;
5508 
5509 	if (!test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
5510 		WARN_ON(btrfs_root_refs(&root->root_item) != 0);
5511 
5512 	spin_lock(&root->inode_lock);
5513 again:
5514 	node = root->inode_tree.rb_node;
5515 	prev = NULL;
5516 	while (node) {
5517 		prev = node;
5518 		entry = rb_entry(node, struct btrfs_inode, rb_node);
5519 
5520 		if (objectid < btrfs_ino(BTRFS_I(&entry->vfs_inode)))
5521 			node = node->rb_left;
5522 		else if (objectid > btrfs_ino(BTRFS_I(&entry->vfs_inode)))
5523 			node = node->rb_right;
5524 		else
5525 			break;
5526 	}
5527 	if (!node) {
5528 		while (prev) {
5529 			entry = rb_entry(prev, struct btrfs_inode, rb_node);
5530 			if (objectid <= btrfs_ino(BTRFS_I(&entry->vfs_inode))) {
5531 				node = prev;
5532 				break;
5533 			}
5534 			prev = rb_next(prev);
5535 		}
5536 	}
5537 	while (node) {
5538 		entry = rb_entry(node, struct btrfs_inode, rb_node);
5539 		objectid = btrfs_ino(BTRFS_I(&entry->vfs_inode)) + 1;
5540 		inode = igrab(&entry->vfs_inode);
5541 		if (inode) {
5542 			spin_unlock(&root->inode_lock);
5543 			if (atomic_read(&inode->i_count) > 1)
5544 				d_prune_aliases(inode);
5545 			/*
5546 			 * btrfs_drop_inode will have it removed from
5547 			 * the inode cache when its usage count
5548 			 * hits zero.
5549 			 */
5550 			iput(inode);
5551 			cond_resched();
5552 			spin_lock(&root->inode_lock);
5553 			goto again;
5554 		}
5555 
5556 		if (cond_resched_lock(&root->inode_lock))
5557 			goto again;
5558 
5559 		node = rb_next(node);
5560 	}
5561 	spin_unlock(&root->inode_lock);
5562 }
5563 
5564 static int btrfs_init_locked_inode(struct inode *inode, void *p)
5565 {
5566 	struct btrfs_iget_args *args = p;
5567 	inode->i_ino = args->location->objectid;
5568 	memcpy(&BTRFS_I(inode)->location, args->location,
5569 	       sizeof(*args->location));
5570 	BTRFS_I(inode)->root = args->root;
5571 	return 0;
5572 }
5573 
5574 static int btrfs_find_actor(struct inode *inode, void *opaque)
5575 {
5576 	struct btrfs_iget_args *args = opaque;
5577 	return args->location->objectid == BTRFS_I(inode)->location.objectid &&
5578 		args->root == BTRFS_I(inode)->root;
5579 }
5580 
5581 static struct inode *btrfs_iget_locked(struct super_block *s,
5582 				       struct btrfs_key *location,
5583 				       struct btrfs_root *root)
5584 {
5585 	struct inode *inode;
5586 	struct btrfs_iget_args args;
5587 	unsigned long hashval = btrfs_inode_hash(location->objectid, root);
5588 
5589 	args.location = location;
5590 	args.root = root;
5591 
5592 	inode = iget5_locked(s, hashval, btrfs_find_actor,
5593 			     btrfs_init_locked_inode,
5594 			     (void *)&args);
5595 	return inode;
5596 }
5597 
5598 /* Get an inode object given its location and corresponding root.
5599  * Returns in *is_new if the inode was read from disk
5600  */
5601 struct inode *btrfs_iget(struct super_block *s, struct btrfs_key *location,
5602 			 struct btrfs_root *root, int *new)
5603 {
5604 	struct inode *inode;
5605 
5606 	inode = btrfs_iget_locked(s, location, root);
5607 	if (!inode)
5608 		return ERR_PTR(-ENOMEM);
5609 
5610 	if (inode->i_state & I_NEW) {
5611 		int ret;
5612 
5613 		ret = btrfs_read_locked_inode(inode);
5614 		if (!is_bad_inode(inode)) {
5615 			inode_tree_add(inode);
5616 			unlock_new_inode(inode);
5617 			if (new)
5618 				*new = 1;
5619 		} else {
5620 			unlock_new_inode(inode);
5621 			iput(inode);
5622 			ASSERT(ret < 0);
5623 			inode = ERR_PTR(ret < 0 ? ret : -ESTALE);
5624 		}
5625 	}
5626 
5627 	return inode;
5628 }
5629 
5630 static struct inode *new_simple_dir(struct super_block *s,
5631 				    struct btrfs_key *key,
5632 				    struct btrfs_root *root)
5633 {
5634 	struct inode *inode = new_inode(s);
5635 
5636 	if (!inode)
5637 		return ERR_PTR(-ENOMEM);
5638 
5639 	BTRFS_I(inode)->root = root;
5640 	memcpy(&BTRFS_I(inode)->location, key, sizeof(*key));
5641 	set_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags);
5642 
5643 	inode->i_ino = BTRFS_EMPTY_SUBVOL_DIR_OBJECTID;
5644 	inode->i_op = &btrfs_dir_ro_inode_operations;
5645 	inode->i_opflags &= ~IOP_XATTR;
5646 	inode->i_fop = &simple_dir_operations;
5647 	inode->i_mode = S_IFDIR | S_IRUGO | S_IWUSR | S_IXUGO;
5648 	inode->i_mtime = current_time(inode);
5649 	inode->i_atime = inode->i_mtime;
5650 	inode->i_ctime = inode->i_mtime;
5651 	BTRFS_I(inode)->i_otime = inode->i_mtime;
5652 
5653 	return inode;
5654 }
5655 
5656 struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)
5657 {
5658 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
5659 	struct inode *inode;
5660 	struct btrfs_root *root = BTRFS_I(dir)->root;
5661 	struct btrfs_root *sub_root = root;
5662 	struct btrfs_key location;
5663 	int index;
5664 	int ret = 0;
5665 
5666 	if (dentry->d_name.len > BTRFS_NAME_LEN)
5667 		return ERR_PTR(-ENAMETOOLONG);
5668 
5669 	ret = btrfs_inode_by_name(dir, dentry, &location);
5670 	if (ret < 0)
5671 		return ERR_PTR(ret);
5672 
5673 	if (location.objectid == 0)
5674 		return ERR_PTR(-ENOENT);
5675 
5676 	if (location.type == BTRFS_INODE_ITEM_KEY) {
5677 		inode = btrfs_iget(dir->i_sb, &location, root, NULL);
5678 		return inode;
5679 	}
5680 
5681 	BUG_ON(location.type != BTRFS_ROOT_ITEM_KEY);
5682 
5683 	index = srcu_read_lock(&fs_info->subvol_srcu);
5684 	ret = fixup_tree_root_location(fs_info, dir, dentry,
5685 				       &location, &sub_root);
5686 	if (ret < 0) {
5687 		if (ret != -ENOENT)
5688 			inode = ERR_PTR(ret);
5689 		else
5690 			inode = new_simple_dir(dir->i_sb, &location, sub_root);
5691 	} else {
5692 		inode = btrfs_iget(dir->i_sb, &location, sub_root, NULL);
5693 	}
5694 	srcu_read_unlock(&fs_info->subvol_srcu, index);
5695 
5696 	if (!IS_ERR(inode) && root != sub_root) {
5697 		down_read(&fs_info->cleanup_work_sem);
5698 		if (!(inode->i_sb->s_flags & MS_RDONLY))
5699 			ret = btrfs_orphan_cleanup(sub_root);
5700 		up_read(&fs_info->cleanup_work_sem);
5701 		if (ret) {
5702 			iput(inode);
5703 			inode = ERR_PTR(ret);
5704 		}
5705 	}
5706 
5707 	return inode;
5708 }
5709 
5710 static int btrfs_dentry_delete(const struct dentry *dentry)
5711 {
5712 	struct btrfs_root *root;
5713 	struct inode *inode = d_inode(dentry);
5714 
5715 	if (!inode && !IS_ROOT(dentry))
5716 		inode = d_inode(dentry->d_parent);
5717 
5718 	if (inode) {
5719 		root = BTRFS_I(inode)->root;
5720 		if (btrfs_root_refs(&root->root_item) == 0)
5721 			return 1;
5722 
5723 		if (btrfs_ino(BTRFS_I(inode)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
5724 			return 1;
5725 	}
5726 	return 0;
5727 }
5728 
5729 static void btrfs_dentry_release(struct dentry *dentry)
5730 {
5731 	kfree(dentry->d_fsdata);
5732 }
5733 
5734 static struct dentry *btrfs_lookup(struct inode *dir, struct dentry *dentry,
5735 				   unsigned int flags)
5736 {
5737 	struct inode *inode;
5738 
5739 	inode = btrfs_lookup_dentry(dir, dentry);
5740 	if (IS_ERR(inode)) {
5741 		if (PTR_ERR(inode) == -ENOENT)
5742 			inode = NULL;
5743 		else
5744 			return ERR_CAST(inode);
5745 	}
5746 
5747 	return d_splice_alias(inode, dentry);
5748 }
5749 
5750 unsigned char btrfs_filetype_table[] = {
5751 	DT_UNKNOWN, DT_REG, DT_DIR, DT_CHR, DT_BLK, DT_FIFO, DT_SOCK, DT_LNK
5752 };
5753 
5754 static int btrfs_real_readdir(struct file *file, struct dir_context *ctx)
5755 {
5756 	struct inode *inode = file_inode(file);
5757 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5758 	struct btrfs_root *root = BTRFS_I(inode)->root;
5759 	struct btrfs_item *item;
5760 	struct btrfs_dir_item *di;
5761 	struct btrfs_key key;
5762 	struct btrfs_key found_key;
5763 	struct btrfs_path *path;
5764 	struct list_head ins_list;
5765 	struct list_head del_list;
5766 	int ret;
5767 	struct extent_buffer *leaf;
5768 	int slot;
5769 	unsigned char d_type;
5770 	int over = 0;
5771 	char tmp_name[32];
5772 	char *name_ptr;
5773 	int name_len;
5774 	bool put = false;
5775 	struct btrfs_key location;
5776 
5777 	if (!dir_emit_dots(file, ctx))
5778 		return 0;
5779 
5780 	path = btrfs_alloc_path();
5781 	if (!path)
5782 		return -ENOMEM;
5783 
5784 	path->reada = READA_FORWARD;
5785 
5786 	INIT_LIST_HEAD(&ins_list);
5787 	INIT_LIST_HEAD(&del_list);
5788 	put = btrfs_readdir_get_delayed_items(inode, &ins_list, &del_list);
5789 
5790 	key.type = BTRFS_DIR_INDEX_KEY;
5791 	key.offset = ctx->pos;
5792 	key.objectid = btrfs_ino(BTRFS_I(inode));
5793 
5794 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5795 	if (ret < 0)
5796 		goto err;
5797 
5798 	while (1) {
5799 		leaf = path->nodes[0];
5800 		slot = path->slots[0];
5801 		if (slot >= btrfs_header_nritems(leaf)) {
5802 			ret = btrfs_next_leaf(root, path);
5803 			if (ret < 0)
5804 				goto err;
5805 			else if (ret > 0)
5806 				break;
5807 			continue;
5808 		}
5809 
5810 		item = btrfs_item_nr(slot);
5811 		btrfs_item_key_to_cpu(leaf, &found_key, slot);
5812 
5813 		if (found_key.objectid != key.objectid)
5814 			break;
5815 		if (found_key.type != BTRFS_DIR_INDEX_KEY)
5816 			break;
5817 		if (found_key.offset < ctx->pos)
5818 			goto next;
5819 		if (btrfs_should_delete_dir_index(&del_list, found_key.offset))
5820 			goto next;
5821 
5822 		ctx->pos = found_key.offset;
5823 
5824 		di = btrfs_item_ptr(leaf, slot, struct btrfs_dir_item);
5825 		if (verify_dir_item(fs_info, leaf, di))
5826 			goto next;
5827 
5828 		name_len = btrfs_dir_name_len(leaf, di);
5829 		if (name_len <= sizeof(tmp_name)) {
5830 			name_ptr = tmp_name;
5831 		} else {
5832 			name_ptr = kmalloc(name_len, GFP_KERNEL);
5833 			if (!name_ptr) {
5834 				ret = -ENOMEM;
5835 				goto err;
5836 			}
5837 		}
5838 		read_extent_buffer(leaf, name_ptr, (unsigned long)(di + 1),
5839 				   name_len);
5840 
5841 		d_type = btrfs_filetype_table[btrfs_dir_type(leaf, di)];
5842 		btrfs_dir_item_key_to_cpu(leaf, di, &location);
5843 
5844 		over = !dir_emit(ctx, name_ptr, name_len, location.objectid,
5845 				 d_type);
5846 
5847 		if (name_ptr != tmp_name)
5848 			kfree(name_ptr);
5849 
5850 		if (over)
5851 			goto nopos;
5852 		ctx->pos++;
5853 next:
5854 		path->slots[0]++;
5855 	}
5856 
5857 	ret = btrfs_readdir_delayed_dir_index(ctx, &ins_list);
5858 	if (ret)
5859 		goto nopos;
5860 
5861 	/*
5862 	 * Stop new entries from being returned after we return the last
5863 	 * entry.
5864 	 *
5865 	 * New directory entries are assigned a strictly increasing
5866 	 * offset.  This means that new entries created during readdir
5867 	 * are *guaranteed* to be seen in the future by that readdir.
5868 	 * This has broken buggy programs which operate on names as
5869 	 * they're returned by readdir.  Until we re-use freed offsets
5870 	 * we have this hack to stop new entries from being returned
5871 	 * under the assumption that they'll never reach this huge
5872 	 * offset.
5873 	 *
5874 	 * This is being careful not to overflow 32bit loff_t unless the
5875 	 * last entry requires it because doing so has broken 32bit apps
5876 	 * in the past.
5877 	 */
5878 	if (ctx->pos >= INT_MAX)
5879 		ctx->pos = LLONG_MAX;
5880 	else
5881 		ctx->pos = INT_MAX;
5882 nopos:
5883 	ret = 0;
5884 err:
5885 	if (put)
5886 		btrfs_readdir_put_delayed_items(inode, &ins_list, &del_list);
5887 	btrfs_free_path(path);
5888 	return ret;
5889 }
5890 
5891 int btrfs_write_inode(struct inode *inode, struct writeback_control *wbc)
5892 {
5893 	struct btrfs_root *root = BTRFS_I(inode)->root;
5894 	struct btrfs_trans_handle *trans;
5895 	int ret = 0;
5896 	bool nolock = false;
5897 
5898 	if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5899 		return 0;
5900 
5901 	if (btrfs_fs_closing(root->fs_info) && btrfs_is_free_space_inode(inode))
5902 		nolock = true;
5903 
5904 	if (wbc->sync_mode == WB_SYNC_ALL) {
5905 		if (nolock)
5906 			trans = btrfs_join_transaction_nolock(root);
5907 		else
5908 			trans = btrfs_join_transaction(root);
5909 		if (IS_ERR(trans))
5910 			return PTR_ERR(trans);
5911 		ret = btrfs_commit_transaction(trans);
5912 	}
5913 	return ret;
5914 }
5915 
5916 /*
5917  * This is somewhat expensive, updating the tree every time the
5918  * inode changes.  But, it is most likely to find the inode in cache.
5919  * FIXME, needs more benchmarking...there are no reasons other than performance
5920  * to keep or drop this code.
5921  */
5922 static int btrfs_dirty_inode(struct inode *inode)
5923 {
5924 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
5925 	struct btrfs_root *root = BTRFS_I(inode)->root;
5926 	struct btrfs_trans_handle *trans;
5927 	int ret;
5928 
5929 	if (test_bit(BTRFS_INODE_DUMMY, &BTRFS_I(inode)->runtime_flags))
5930 		return 0;
5931 
5932 	trans = btrfs_join_transaction(root);
5933 	if (IS_ERR(trans))
5934 		return PTR_ERR(trans);
5935 
5936 	ret = btrfs_update_inode(trans, root, inode);
5937 	if (ret && ret == -ENOSPC) {
5938 		/* whoops, lets try again with the full transaction */
5939 		btrfs_end_transaction(trans);
5940 		trans = btrfs_start_transaction(root, 1);
5941 		if (IS_ERR(trans))
5942 			return PTR_ERR(trans);
5943 
5944 		ret = btrfs_update_inode(trans, root, inode);
5945 	}
5946 	btrfs_end_transaction(trans);
5947 	if (BTRFS_I(inode)->delayed_node)
5948 		btrfs_balance_delayed_items(fs_info);
5949 
5950 	return ret;
5951 }
5952 
5953 /*
5954  * This is a copy of file_update_time.  We need this so we can return error on
5955  * ENOSPC for updating the inode in the case of file write and mmap writes.
5956  */
5957 static int btrfs_update_time(struct inode *inode, struct timespec *now,
5958 			     int flags)
5959 {
5960 	struct btrfs_root *root = BTRFS_I(inode)->root;
5961 
5962 	if (btrfs_root_readonly(root))
5963 		return -EROFS;
5964 
5965 	if (flags & S_VERSION)
5966 		inode_inc_iversion(inode);
5967 	if (flags & S_CTIME)
5968 		inode->i_ctime = *now;
5969 	if (flags & S_MTIME)
5970 		inode->i_mtime = *now;
5971 	if (flags & S_ATIME)
5972 		inode->i_atime = *now;
5973 	return btrfs_dirty_inode(inode);
5974 }
5975 
5976 /*
5977  * find the highest existing sequence number in a directory
5978  * and then set the in-memory index_cnt variable to reflect
5979  * free sequence numbers
5980  */
5981 static int btrfs_set_inode_index_count(struct inode *inode)
5982 {
5983 	struct btrfs_root *root = BTRFS_I(inode)->root;
5984 	struct btrfs_key key, found_key;
5985 	struct btrfs_path *path;
5986 	struct extent_buffer *leaf;
5987 	int ret;
5988 
5989 	key.objectid = btrfs_ino(BTRFS_I(inode));
5990 	key.type = BTRFS_DIR_INDEX_KEY;
5991 	key.offset = (u64)-1;
5992 
5993 	path = btrfs_alloc_path();
5994 	if (!path)
5995 		return -ENOMEM;
5996 
5997 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
5998 	if (ret < 0)
5999 		goto out;
6000 	/* FIXME: we should be able to handle this */
6001 	if (ret == 0)
6002 		goto out;
6003 	ret = 0;
6004 
6005 	/*
6006 	 * MAGIC NUMBER EXPLANATION:
6007 	 * since we search a directory based on f_pos we have to start at 2
6008 	 * since '.' and '..' have f_pos of 0 and 1 respectively, so everybody
6009 	 * else has to start at 2
6010 	 */
6011 	if (path->slots[0] == 0) {
6012 		BTRFS_I(inode)->index_cnt = 2;
6013 		goto out;
6014 	}
6015 
6016 	path->slots[0]--;
6017 
6018 	leaf = path->nodes[0];
6019 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6020 
6021 	if (found_key.objectid != btrfs_ino(BTRFS_I(inode)) ||
6022 	    found_key.type != BTRFS_DIR_INDEX_KEY) {
6023 		BTRFS_I(inode)->index_cnt = 2;
6024 		goto out;
6025 	}
6026 
6027 	BTRFS_I(inode)->index_cnt = found_key.offset + 1;
6028 out:
6029 	btrfs_free_path(path);
6030 	return ret;
6031 }
6032 
6033 /*
6034  * helper to find a free sequence number in a given directory.  This current
6035  * code is very simple, later versions will do smarter things in the btree
6036  */
6037 int btrfs_set_inode_index(struct inode *dir, u64 *index)
6038 {
6039 	int ret = 0;
6040 
6041 	if (BTRFS_I(dir)->index_cnt == (u64)-1) {
6042 		ret = btrfs_inode_delayed_dir_index_count(BTRFS_I(dir));
6043 		if (ret) {
6044 			ret = btrfs_set_inode_index_count(dir);
6045 			if (ret)
6046 				return ret;
6047 		}
6048 	}
6049 
6050 	*index = BTRFS_I(dir)->index_cnt;
6051 	BTRFS_I(dir)->index_cnt++;
6052 
6053 	return ret;
6054 }
6055 
6056 static int btrfs_insert_inode_locked(struct inode *inode)
6057 {
6058 	struct btrfs_iget_args args;
6059 	args.location = &BTRFS_I(inode)->location;
6060 	args.root = BTRFS_I(inode)->root;
6061 
6062 	return insert_inode_locked4(inode,
6063 		   btrfs_inode_hash(inode->i_ino, BTRFS_I(inode)->root),
6064 		   btrfs_find_actor, &args);
6065 }
6066 
6067 static struct inode *btrfs_new_inode(struct btrfs_trans_handle *trans,
6068 				     struct btrfs_root *root,
6069 				     struct inode *dir,
6070 				     const char *name, int name_len,
6071 				     u64 ref_objectid, u64 objectid,
6072 				     umode_t mode, u64 *index)
6073 {
6074 	struct btrfs_fs_info *fs_info = root->fs_info;
6075 	struct inode *inode;
6076 	struct btrfs_inode_item *inode_item;
6077 	struct btrfs_key *location;
6078 	struct btrfs_path *path;
6079 	struct btrfs_inode_ref *ref;
6080 	struct btrfs_key key[2];
6081 	u32 sizes[2];
6082 	int nitems = name ? 2 : 1;
6083 	unsigned long ptr;
6084 	int ret;
6085 
6086 	path = btrfs_alloc_path();
6087 	if (!path)
6088 		return ERR_PTR(-ENOMEM);
6089 
6090 	inode = new_inode(fs_info->sb);
6091 	if (!inode) {
6092 		btrfs_free_path(path);
6093 		return ERR_PTR(-ENOMEM);
6094 	}
6095 
6096 	/*
6097 	 * O_TMPFILE, set link count to 0, so that after this point,
6098 	 * we fill in an inode item with the correct link count.
6099 	 */
6100 	if (!name)
6101 		set_nlink(inode, 0);
6102 
6103 	/*
6104 	 * we have to initialize this early, so we can reclaim the inode
6105 	 * number if we fail afterwards in this function.
6106 	 */
6107 	inode->i_ino = objectid;
6108 
6109 	if (dir && name) {
6110 		trace_btrfs_inode_request(dir);
6111 
6112 		ret = btrfs_set_inode_index(dir, index);
6113 		if (ret) {
6114 			btrfs_free_path(path);
6115 			iput(inode);
6116 			return ERR_PTR(ret);
6117 		}
6118 	} else if (dir) {
6119 		*index = 0;
6120 	}
6121 	/*
6122 	 * index_cnt is ignored for everything but a dir,
6123 	 * btrfs_get_inode_index_count has an explanation for the magic
6124 	 * number
6125 	 */
6126 	BTRFS_I(inode)->index_cnt = 2;
6127 	BTRFS_I(inode)->dir_index = *index;
6128 	BTRFS_I(inode)->root = root;
6129 	BTRFS_I(inode)->generation = trans->transid;
6130 	inode->i_generation = BTRFS_I(inode)->generation;
6131 
6132 	/*
6133 	 * We could have gotten an inode number from somebody who was fsynced
6134 	 * and then removed in this same transaction, so let's just set full
6135 	 * sync since it will be a full sync anyway and this will blow away the
6136 	 * old info in the log.
6137 	 */
6138 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
6139 
6140 	key[0].objectid = objectid;
6141 	key[0].type = BTRFS_INODE_ITEM_KEY;
6142 	key[0].offset = 0;
6143 
6144 	sizes[0] = sizeof(struct btrfs_inode_item);
6145 
6146 	if (name) {
6147 		/*
6148 		 * Start new inodes with an inode_ref. This is slightly more
6149 		 * efficient for small numbers of hard links since they will
6150 		 * be packed into one item. Extended refs will kick in if we
6151 		 * add more hard links than can fit in the ref item.
6152 		 */
6153 		key[1].objectid = objectid;
6154 		key[1].type = BTRFS_INODE_REF_KEY;
6155 		key[1].offset = ref_objectid;
6156 
6157 		sizes[1] = name_len + sizeof(*ref);
6158 	}
6159 
6160 	location = &BTRFS_I(inode)->location;
6161 	location->objectid = objectid;
6162 	location->offset = 0;
6163 	location->type = BTRFS_INODE_ITEM_KEY;
6164 
6165 	ret = btrfs_insert_inode_locked(inode);
6166 	if (ret < 0)
6167 		goto fail;
6168 
6169 	path->leave_spinning = 1;
6170 	ret = btrfs_insert_empty_items(trans, root, path, key, sizes, nitems);
6171 	if (ret != 0)
6172 		goto fail_unlock;
6173 
6174 	inode_init_owner(inode, dir, mode);
6175 	inode_set_bytes(inode, 0);
6176 
6177 	inode->i_mtime = current_time(inode);
6178 	inode->i_atime = inode->i_mtime;
6179 	inode->i_ctime = inode->i_mtime;
6180 	BTRFS_I(inode)->i_otime = inode->i_mtime;
6181 
6182 	inode_item = btrfs_item_ptr(path->nodes[0], path->slots[0],
6183 				  struct btrfs_inode_item);
6184 	memzero_extent_buffer(path->nodes[0], (unsigned long)inode_item,
6185 			     sizeof(*inode_item));
6186 	fill_inode_item(trans, path->nodes[0], inode_item, inode);
6187 
6188 	if (name) {
6189 		ref = btrfs_item_ptr(path->nodes[0], path->slots[0] + 1,
6190 				     struct btrfs_inode_ref);
6191 		btrfs_set_inode_ref_name_len(path->nodes[0], ref, name_len);
6192 		btrfs_set_inode_ref_index(path->nodes[0], ref, *index);
6193 		ptr = (unsigned long)(ref + 1);
6194 		write_extent_buffer(path->nodes[0], name, ptr, name_len);
6195 	}
6196 
6197 	btrfs_mark_buffer_dirty(path->nodes[0]);
6198 	btrfs_free_path(path);
6199 
6200 	btrfs_inherit_iflags(inode, dir);
6201 
6202 	if (S_ISREG(mode)) {
6203 		if (btrfs_test_opt(fs_info, NODATASUM))
6204 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATASUM;
6205 		if (btrfs_test_opt(fs_info, NODATACOW))
6206 			BTRFS_I(inode)->flags |= BTRFS_INODE_NODATACOW |
6207 				BTRFS_INODE_NODATASUM;
6208 	}
6209 
6210 	inode_tree_add(inode);
6211 
6212 	trace_btrfs_inode_new(inode);
6213 	btrfs_set_inode_last_trans(trans, inode);
6214 
6215 	btrfs_update_root_times(trans, root);
6216 
6217 	ret = btrfs_inode_inherit_props(trans, inode, dir);
6218 	if (ret)
6219 		btrfs_err(fs_info,
6220 			  "error inheriting props for ino %llu (root %llu): %d",
6221 			btrfs_ino(BTRFS_I(inode)), root->root_key.objectid, ret);
6222 
6223 	return inode;
6224 
6225 fail_unlock:
6226 	unlock_new_inode(inode);
6227 fail:
6228 	if (dir && name)
6229 		BTRFS_I(dir)->index_cnt--;
6230 	btrfs_free_path(path);
6231 	iput(inode);
6232 	return ERR_PTR(ret);
6233 }
6234 
6235 static inline u8 btrfs_inode_type(struct inode *inode)
6236 {
6237 	return btrfs_type_by_mode[(inode->i_mode & S_IFMT) >> S_SHIFT];
6238 }
6239 
6240 /*
6241  * utility function to add 'inode' into 'parent_inode' with
6242  * a give name and a given sequence number.
6243  * if 'add_backref' is true, also insert a backref from the
6244  * inode to the parent directory.
6245  */
6246 int btrfs_add_link(struct btrfs_trans_handle *trans,
6247 		   struct inode *parent_inode, struct inode *inode,
6248 		   const char *name, int name_len, int add_backref, u64 index)
6249 {
6250 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6251 	int ret = 0;
6252 	struct btrfs_key key;
6253 	struct btrfs_root *root = BTRFS_I(parent_inode)->root;
6254 	u64 ino = btrfs_ino(BTRFS_I(inode));
6255 	u64 parent_ino = btrfs_ino(BTRFS_I(parent_inode));
6256 
6257 	if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6258 		memcpy(&key, &BTRFS_I(inode)->root->root_key, sizeof(key));
6259 	} else {
6260 		key.objectid = ino;
6261 		key.type = BTRFS_INODE_ITEM_KEY;
6262 		key.offset = 0;
6263 	}
6264 
6265 	if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6266 		ret = btrfs_add_root_ref(trans, fs_info, key.objectid,
6267 					 root->root_key.objectid, parent_ino,
6268 					 index, name, name_len);
6269 	} else if (add_backref) {
6270 		ret = btrfs_insert_inode_ref(trans, root, name, name_len, ino,
6271 					     parent_ino, index);
6272 	}
6273 
6274 	/* Nothing to clean up yet */
6275 	if (ret)
6276 		return ret;
6277 
6278 	ret = btrfs_insert_dir_item(trans, root, name, name_len,
6279 				    parent_inode, &key,
6280 				    btrfs_inode_type(inode), index);
6281 	if (ret == -EEXIST || ret == -EOVERFLOW)
6282 		goto fail_dir_item;
6283 	else if (ret) {
6284 		btrfs_abort_transaction(trans, ret);
6285 		return ret;
6286 	}
6287 
6288 	btrfs_i_size_write(parent_inode, parent_inode->i_size +
6289 			   name_len * 2);
6290 	inode_inc_iversion(parent_inode);
6291 	parent_inode->i_mtime = parent_inode->i_ctime =
6292 		current_time(parent_inode);
6293 	ret = btrfs_update_inode(trans, root, parent_inode);
6294 	if (ret)
6295 		btrfs_abort_transaction(trans, ret);
6296 	return ret;
6297 
6298 fail_dir_item:
6299 	if (unlikely(ino == BTRFS_FIRST_FREE_OBJECTID)) {
6300 		u64 local_index;
6301 		int err;
6302 		err = btrfs_del_root_ref(trans, fs_info, key.objectid,
6303 					 root->root_key.objectid, parent_ino,
6304 					 &local_index, name, name_len);
6305 
6306 	} else if (add_backref) {
6307 		u64 local_index;
6308 		int err;
6309 
6310 		err = btrfs_del_inode_ref(trans, root, name, name_len,
6311 					  ino, parent_ino, &local_index);
6312 	}
6313 	return ret;
6314 }
6315 
6316 static int btrfs_add_nondir(struct btrfs_trans_handle *trans,
6317 			    struct inode *dir, struct dentry *dentry,
6318 			    struct inode *inode, int backref, u64 index)
6319 {
6320 	int err = btrfs_add_link(trans, dir, inode,
6321 				 dentry->d_name.name, dentry->d_name.len,
6322 				 backref, index);
6323 	if (err > 0)
6324 		err = -EEXIST;
6325 	return err;
6326 }
6327 
6328 static int btrfs_mknod(struct inode *dir, struct dentry *dentry,
6329 			umode_t mode, dev_t rdev)
6330 {
6331 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6332 	struct btrfs_trans_handle *trans;
6333 	struct btrfs_root *root = BTRFS_I(dir)->root;
6334 	struct inode *inode = NULL;
6335 	int err;
6336 	int drop_inode = 0;
6337 	u64 objectid;
6338 	u64 index = 0;
6339 
6340 	/*
6341 	 * 2 for inode item and ref
6342 	 * 2 for dir items
6343 	 * 1 for xattr if selinux is on
6344 	 */
6345 	trans = btrfs_start_transaction(root, 5);
6346 	if (IS_ERR(trans))
6347 		return PTR_ERR(trans);
6348 
6349 	err = btrfs_find_free_ino(root, &objectid);
6350 	if (err)
6351 		goto out_unlock;
6352 
6353 	inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6354 			dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6355 			mode, &index);
6356 	if (IS_ERR(inode)) {
6357 		err = PTR_ERR(inode);
6358 		goto out_unlock;
6359 	}
6360 
6361 	/*
6362 	* If the active LSM wants to access the inode during
6363 	* d_instantiate it needs these. Smack checks to see
6364 	* if the filesystem supports xattrs by looking at the
6365 	* ops vector.
6366 	*/
6367 	inode->i_op = &btrfs_special_inode_operations;
6368 	init_special_inode(inode, inode->i_mode, rdev);
6369 
6370 	err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6371 	if (err)
6372 		goto out_unlock_inode;
6373 
6374 	err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6375 	if (err) {
6376 		goto out_unlock_inode;
6377 	} else {
6378 		btrfs_update_inode(trans, root, inode);
6379 		unlock_new_inode(inode);
6380 		d_instantiate(dentry, inode);
6381 	}
6382 
6383 out_unlock:
6384 	btrfs_end_transaction(trans);
6385 	btrfs_balance_delayed_items(fs_info);
6386 	btrfs_btree_balance_dirty(fs_info);
6387 	if (drop_inode) {
6388 		inode_dec_link_count(inode);
6389 		iput(inode);
6390 	}
6391 	return err;
6392 
6393 out_unlock_inode:
6394 	drop_inode = 1;
6395 	unlock_new_inode(inode);
6396 	goto out_unlock;
6397 
6398 }
6399 
6400 static int btrfs_create(struct inode *dir, struct dentry *dentry,
6401 			umode_t mode, bool excl)
6402 {
6403 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6404 	struct btrfs_trans_handle *trans;
6405 	struct btrfs_root *root = BTRFS_I(dir)->root;
6406 	struct inode *inode = NULL;
6407 	int drop_inode_on_err = 0;
6408 	int err;
6409 	u64 objectid;
6410 	u64 index = 0;
6411 
6412 	/*
6413 	 * 2 for inode item and ref
6414 	 * 2 for dir items
6415 	 * 1 for xattr if selinux is on
6416 	 */
6417 	trans = btrfs_start_transaction(root, 5);
6418 	if (IS_ERR(trans))
6419 		return PTR_ERR(trans);
6420 
6421 	err = btrfs_find_free_ino(root, &objectid);
6422 	if (err)
6423 		goto out_unlock;
6424 
6425 	inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6426 			dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6427 			mode, &index);
6428 	if (IS_ERR(inode)) {
6429 		err = PTR_ERR(inode);
6430 		goto out_unlock;
6431 	}
6432 	drop_inode_on_err = 1;
6433 	/*
6434 	* If the active LSM wants to access the inode during
6435 	* d_instantiate it needs these. Smack checks to see
6436 	* if the filesystem supports xattrs by looking at the
6437 	* ops vector.
6438 	*/
6439 	inode->i_fop = &btrfs_file_operations;
6440 	inode->i_op = &btrfs_file_inode_operations;
6441 	inode->i_mapping->a_ops = &btrfs_aops;
6442 
6443 	err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6444 	if (err)
6445 		goto out_unlock_inode;
6446 
6447 	err = btrfs_update_inode(trans, root, inode);
6448 	if (err)
6449 		goto out_unlock_inode;
6450 
6451 	err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
6452 	if (err)
6453 		goto out_unlock_inode;
6454 
6455 	BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
6456 	unlock_new_inode(inode);
6457 	d_instantiate(dentry, inode);
6458 
6459 out_unlock:
6460 	btrfs_end_transaction(trans);
6461 	if (err && drop_inode_on_err) {
6462 		inode_dec_link_count(inode);
6463 		iput(inode);
6464 	}
6465 	btrfs_balance_delayed_items(fs_info);
6466 	btrfs_btree_balance_dirty(fs_info);
6467 	return err;
6468 
6469 out_unlock_inode:
6470 	unlock_new_inode(inode);
6471 	goto out_unlock;
6472 
6473 }
6474 
6475 static int btrfs_link(struct dentry *old_dentry, struct inode *dir,
6476 		      struct dentry *dentry)
6477 {
6478 	struct btrfs_trans_handle *trans = NULL;
6479 	struct btrfs_root *root = BTRFS_I(dir)->root;
6480 	struct inode *inode = d_inode(old_dentry);
6481 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6482 	u64 index;
6483 	int err;
6484 	int drop_inode = 0;
6485 
6486 	/* do not allow sys_link's with other subvols of the same device */
6487 	if (root->objectid != BTRFS_I(inode)->root->objectid)
6488 		return -EXDEV;
6489 
6490 	if (inode->i_nlink >= BTRFS_LINK_MAX)
6491 		return -EMLINK;
6492 
6493 	err = btrfs_set_inode_index(dir, &index);
6494 	if (err)
6495 		goto fail;
6496 
6497 	/*
6498 	 * 2 items for inode and inode ref
6499 	 * 2 items for dir items
6500 	 * 1 item for parent inode
6501 	 */
6502 	trans = btrfs_start_transaction(root, 5);
6503 	if (IS_ERR(trans)) {
6504 		err = PTR_ERR(trans);
6505 		trans = NULL;
6506 		goto fail;
6507 	}
6508 
6509 	/* There are several dir indexes for this inode, clear the cache. */
6510 	BTRFS_I(inode)->dir_index = 0ULL;
6511 	inc_nlink(inode);
6512 	inode_inc_iversion(inode);
6513 	inode->i_ctime = current_time(inode);
6514 	ihold(inode);
6515 	set_bit(BTRFS_INODE_COPY_EVERYTHING, &BTRFS_I(inode)->runtime_flags);
6516 
6517 	err = btrfs_add_nondir(trans, dir, dentry, inode, 1, index);
6518 
6519 	if (err) {
6520 		drop_inode = 1;
6521 	} else {
6522 		struct dentry *parent = dentry->d_parent;
6523 		err = btrfs_update_inode(trans, root, inode);
6524 		if (err)
6525 			goto fail;
6526 		if (inode->i_nlink == 1) {
6527 			/*
6528 			 * If new hard link count is 1, it's a file created
6529 			 * with open(2) O_TMPFILE flag.
6530 			 */
6531 			err = btrfs_orphan_del(trans, inode);
6532 			if (err)
6533 				goto fail;
6534 		}
6535 		d_instantiate(dentry, inode);
6536 		btrfs_log_new_name(trans, BTRFS_I(inode), NULL, parent);
6537 	}
6538 
6539 	btrfs_balance_delayed_items(fs_info);
6540 fail:
6541 	if (trans)
6542 		btrfs_end_transaction(trans);
6543 	if (drop_inode) {
6544 		inode_dec_link_count(inode);
6545 		iput(inode);
6546 	}
6547 	btrfs_btree_balance_dirty(fs_info);
6548 	return err;
6549 }
6550 
6551 static int btrfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
6552 {
6553 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
6554 	struct inode *inode = NULL;
6555 	struct btrfs_trans_handle *trans;
6556 	struct btrfs_root *root = BTRFS_I(dir)->root;
6557 	int err = 0;
6558 	int drop_on_err = 0;
6559 	u64 objectid = 0;
6560 	u64 index = 0;
6561 
6562 	/*
6563 	 * 2 items for inode and ref
6564 	 * 2 items for dir items
6565 	 * 1 for xattr if selinux is on
6566 	 */
6567 	trans = btrfs_start_transaction(root, 5);
6568 	if (IS_ERR(trans))
6569 		return PTR_ERR(trans);
6570 
6571 	err = btrfs_find_free_ino(root, &objectid);
6572 	if (err)
6573 		goto out_fail;
6574 
6575 	inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
6576 			dentry->d_name.len, btrfs_ino(BTRFS_I(dir)), objectid,
6577 			S_IFDIR | mode, &index);
6578 	if (IS_ERR(inode)) {
6579 		err = PTR_ERR(inode);
6580 		goto out_fail;
6581 	}
6582 
6583 	drop_on_err = 1;
6584 	/* these must be set before we unlock the inode */
6585 	inode->i_op = &btrfs_dir_inode_operations;
6586 	inode->i_fop = &btrfs_dir_file_operations;
6587 
6588 	err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
6589 	if (err)
6590 		goto out_fail_inode;
6591 
6592 	btrfs_i_size_write(inode, 0);
6593 	err = btrfs_update_inode(trans, root, inode);
6594 	if (err)
6595 		goto out_fail_inode;
6596 
6597 	err = btrfs_add_link(trans, dir, inode, dentry->d_name.name,
6598 			     dentry->d_name.len, 0, index);
6599 	if (err)
6600 		goto out_fail_inode;
6601 
6602 	d_instantiate(dentry, inode);
6603 	/*
6604 	 * mkdir is special.  We're unlocking after we call d_instantiate
6605 	 * to avoid a race with nfsd calling d_instantiate.
6606 	 */
6607 	unlock_new_inode(inode);
6608 	drop_on_err = 0;
6609 
6610 out_fail:
6611 	btrfs_end_transaction(trans);
6612 	if (drop_on_err) {
6613 		inode_dec_link_count(inode);
6614 		iput(inode);
6615 	}
6616 	btrfs_balance_delayed_items(fs_info);
6617 	btrfs_btree_balance_dirty(fs_info);
6618 	return err;
6619 
6620 out_fail_inode:
6621 	unlock_new_inode(inode);
6622 	goto out_fail;
6623 }
6624 
6625 /* Find next extent map of a given extent map, caller needs to ensure locks */
6626 static struct extent_map *next_extent_map(struct extent_map *em)
6627 {
6628 	struct rb_node *next;
6629 
6630 	next = rb_next(&em->rb_node);
6631 	if (!next)
6632 		return NULL;
6633 	return container_of(next, struct extent_map, rb_node);
6634 }
6635 
6636 static struct extent_map *prev_extent_map(struct extent_map *em)
6637 {
6638 	struct rb_node *prev;
6639 
6640 	prev = rb_prev(&em->rb_node);
6641 	if (!prev)
6642 		return NULL;
6643 	return container_of(prev, struct extent_map, rb_node);
6644 }
6645 
6646 /* helper for btfs_get_extent.  Given an existing extent in the tree,
6647  * the existing extent is the nearest extent to map_start,
6648  * and an extent that you want to insert, deal with overlap and insert
6649  * the best fitted new extent into the tree.
6650  */
6651 static int merge_extent_mapping(struct extent_map_tree *em_tree,
6652 				struct extent_map *existing,
6653 				struct extent_map *em,
6654 				u64 map_start)
6655 {
6656 	struct extent_map *prev;
6657 	struct extent_map *next;
6658 	u64 start;
6659 	u64 end;
6660 	u64 start_diff;
6661 
6662 	BUG_ON(map_start < em->start || map_start >= extent_map_end(em));
6663 
6664 	if (existing->start > map_start) {
6665 		next = existing;
6666 		prev = prev_extent_map(next);
6667 	} else {
6668 		prev = existing;
6669 		next = next_extent_map(prev);
6670 	}
6671 
6672 	start = prev ? extent_map_end(prev) : em->start;
6673 	start = max_t(u64, start, em->start);
6674 	end = next ? next->start : extent_map_end(em);
6675 	end = min_t(u64, end, extent_map_end(em));
6676 	start_diff = start - em->start;
6677 	em->start = start;
6678 	em->len = end - start;
6679 	if (em->block_start < EXTENT_MAP_LAST_BYTE &&
6680 	    !test_bit(EXTENT_FLAG_COMPRESSED, &em->flags)) {
6681 		em->block_start += start_diff;
6682 		em->block_len -= start_diff;
6683 	}
6684 	return add_extent_mapping(em_tree, em, 0);
6685 }
6686 
6687 static noinline int uncompress_inline(struct btrfs_path *path,
6688 				      struct page *page,
6689 				      size_t pg_offset, u64 extent_offset,
6690 				      struct btrfs_file_extent_item *item)
6691 {
6692 	int ret;
6693 	struct extent_buffer *leaf = path->nodes[0];
6694 	char *tmp;
6695 	size_t max_size;
6696 	unsigned long inline_size;
6697 	unsigned long ptr;
6698 	int compress_type;
6699 
6700 	WARN_ON(pg_offset != 0);
6701 	compress_type = btrfs_file_extent_compression(leaf, item);
6702 	max_size = btrfs_file_extent_ram_bytes(leaf, item);
6703 	inline_size = btrfs_file_extent_inline_item_len(leaf,
6704 					btrfs_item_nr(path->slots[0]));
6705 	tmp = kmalloc(inline_size, GFP_NOFS);
6706 	if (!tmp)
6707 		return -ENOMEM;
6708 	ptr = btrfs_file_extent_inline_start(item);
6709 
6710 	read_extent_buffer(leaf, tmp, ptr, inline_size);
6711 
6712 	max_size = min_t(unsigned long, PAGE_SIZE, max_size);
6713 	ret = btrfs_decompress(compress_type, tmp, page,
6714 			       extent_offset, inline_size, max_size);
6715 	kfree(tmp);
6716 	return ret;
6717 }
6718 
6719 /*
6720  * a bit scary, this does extent mapping from logical file offset to the disk.
6721  * the ugly parts come from merging extents from the disk with the in-ram
6722  * representation.  This gets more complex because of the data=ordered code,
6723  * where the in-ram extents might be locked pending data=ordered completion.
6724  *
6725  * This also copies inline extents directly into the page.
6726  */
6727 
6728 struct extent_map *btrfs_get_extent(struct inode *inode, struct page *page,
6729 				    size_t pg_offset, u64 start, u64 len,
6730 				    int create)
6731 {
6732 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
6733 	int ret;
6734 	int err = 0;
6735 	u64 extent_start = 0;
6736 	u64 extent_end = 0;
6737 	u64 objectid = btrfs_ino(BTRFS_I(inode));
6738 	u32 found_type;
6739 	struct btrfs_path *path = NULL;
6740 	struct btrfs_root *root = BTRFS_I(inode)->root;
6741 	struct btrfs_file_extent_item *item;
6742 	struct extent_buffer *leaf;
6743 	struct btrfs_key found_key;
6744 	struct extent_map *em = NULL;
6745 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
6746 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
6747 	struct btrfs_trans_handle *trans = NULL;
6748 	const bool new_inline = !page || create;
6749 
6750 again:
6751 	read_lock(&em_tree->lock);
6752 	em = lookup_extent_mapping(em_tree, start, len);
6753 	if (em)
6754 		em->bdev = fs_info->fs_devices->latest_bdev;
6755 	read_unlock(&em_tree->lock);
6756 
6757 	if (em) {
6758 		if (em->start > start || em->start + em->len <= start)
6759 			free_extent_map(em);
6760 		else if (em->block_start == EXTENT_MAP_INLINE && page)
6761 			free_extent_map(em);
6762 		else
6763 			goto out;
6764 	}
6765 	em = alloc_extent_map();
6766 	if (!em) {
6767 		err = -ENOMEM;
6768 		goto out;
6769 	}
6770 	em->bdev = fs_info->fs_devices->latest_bdev;
6771 	em->start = EXTENT_MAP_HOLE;
6772 	em->orig_start = EXTENT_MAP_HOLE;
6773 	em->len = (u64)-1;
6774 	em->block_len = (u64)-1;
6775 
6776 	if (!path) {
6777 		path = btrfs_alloc_path();
6778 		if (!path) {
6779 			err = -ENOMEM;
6780 			goto out;
6781 		}
6782 		/*
6783 		 * Chances are we'll be called again, so go ahead and do
6784 		 * readahead
6785 		 */
6786 		path->reada = READA_FORWARD;
6787 	}
6788 
6789 	ret = btrfs_lookup_file_extent(trans, root, path,
6790 				       objectid, start, trans != NULL);
6791 	if (ret < 0) {
6792 		err = ret;
6793 		goto out;
6794 	}
6795 
6796 	if (ret != 0) {
6797 		if (path->slots[0] == 0)
6798 			goto not_found;
6799 		path->slots[0]--;
6800 	}
6801 
6802 	leaf = path->nodes[0];
6803 	item = btrfs_item_ptr(leaf, path->slots[0],
6804 			      struct btrfs_file_extent_item);
6805 	/* are we inside the extent that was found? */
6806 	btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6807 	found_type = found_key.type;
6808 	if (found_key.objectid != objectid ||
6809 	    found_type != BTRFS_EXTENT_DATA_KEY) {
6810 		/*
6811 		 * If we backup past the first extent we want to move forward
6812 		 * and see if there is an extent in front of us, otherwise we'll
6813 		 * say there is a hole for our whole search range which can
6814 		 * cause problems.
6815 		 */
6816 		extent_end = start;
6817 		goto next;
6818 	}
6819 
6820 	found_type = btrfs_file_extent_type(leaf, item);
6821 	extent_start = found_key.offset;
6822 	if (found_type == BTRFS_FILE_EXTENT_REG ||
6823 	    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6824 		extent_end = extent_start +
6825 		       btrfs_file_extent_num_bytes(leaf, item);
6826 	} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6827 		size_t size;
6828 		size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6829 		extent_end = ALIGN(extent_start + size,
6830 				   fs_info->sectorsize);
6831 	}
6832 next:
6833 	if (start >= extent_end) {
6834 		path->slots[0]++;
6835 		if (path->slots[0] >= btrfs_header_nritems(leaf)) {
6836 			ret = btrfs_next_leaf(root, path);
6837 			if (ret < 0) {
6838 				err = ret;
6839 				goto out;
6840 			}
6841 			if (ret > 0)
6842 				goto not_found;
6843 			leaf = path->nodes[0];
6844 		}
6845 		btrfs_item_key_to_cpu(leaf, &found_key, path->slots[0]);
6846 		if (found_key.objectid != objectid ||
6847 		    found_key.type != BTRFS_EXTENT_DATA_KEY)
6848 			goto not_found;
6849 		if (start + len <= found_key.offset)
6850 			goto not_found;
6851 		if (start > found_key.offset)
6852 			goto next;
6853 		em->start = start;
6854 		em->orig_start = start;
6855 		em->len = found_key.offset - start;
6856 		goto not_found_em;
6857 	}
6858 
6859 	btrfs_extent_item_to_extent_map(inode, path, item, new_inline, em);
6860 
6861 	if (found_type == BTRFS_FILE_EXTENT_REG ||
6862 	    found_type == BTRFS_FILE_EXTENT_PREALLOC) {
6863 		goto insert;
6864 	} else if (found_type == BTRFS_FILE_EXTENT_INLINE) {
6865 		unsigned long ptr;
6866 		char *map;
6867 		size_t size;
6868 		size_t extent_offset;
6869 		size_t copy_size;
6870 
6871 		if (new_inline)
6872 			goto out;
6873 
6874 		size = btrfs_file_extent_inline_len(leaf, path->slots[0], item);
6875 		extent_offset = page_offset(page) + pg_offset - extent_start;
6876 		copy_size = min_t(u64, PAGE_SIZE - pg_offset,
6877 				  size - extent_offset);
6878 		em->start = extent_start + extent_offset;
6879 		em->len = ALIGN(copy_size, fs_info->sectorsize);
6880 		em->orig_block_len = em->len;
6881 		em->orig_start = em->start;
6882 		ptr = btrfs_file_extent_inline_start(item) + extent_offset;
6883 		if (create == 0 && !PageUptodate(page)) {
6884 			if (btrfs_file_extent_compression(leaf, item) !=
6885 			    BTRFS_COMPRESS_NONE) {
6886 				ret = uncompress_inline(path, page, pg_offset,
6887 							extent_offset, item);
6888 				if (ret) {
6889 					err = ret;
6890 					goto out;
6891 				}
6892 			} else {
6893 				map = kmap(page);
6894 				read_extent_buffer(leaf, map + pg_offset, ptr,
6895 						   copy_size);
6896 				if (pg_offset + copy_size < PAGE_SIZE) {
6897 					memset(map + pg_offset + copy_size, 0,
6898 					       PAGE_SIZE - pg_offset -
6899 					       copy_size);
6900 				}
6901 				kunmap(page);
6902 			}
6903 			flush_dcache_page(page);
6904 		} else if (create && PageUptodate(page)) {
6905 			BUG();
6906 			if (!trans) {
6907 				kunmap(page);
6908 				free_extent_map(em);
6909 				em = NULL;
6910 
6911 				btrfs_release_path(path);
6912 				trans = btrfs_join_transaction(root);
6913 
6914 				if (IS_ERR(trans))
6915 					return ERR_CAST(trans);
6916 				goto again;
6917 			}
6918 			map = kmap(page);
6919 			write_extent_buffer(leaf, map + pg_offset, ptr,
6920 					    copy_size);
6921 			kunmap(page);
6922 			btrfs_mark_buffer_dirty(leaf);
6923 		}
6924 		set_extent_uptodate(io_tree, em->start,
6925 				    extent_map_end(em) - 1, NULL, GFP_NOFS);
6926 		goto insert;
6927 	}
6928 not_found:
6929 	em->start = start;
6930 	em->orig_start = start;
6931 	em->len = len;
6932 not_found_em:
6933 	em->block_start = EXTENT_MAP_HOLE;
6934 	set_bit(EXTENT_FLAG_VACANCY, &em->flags);
6935 insert:
6936 	btrfs_release_path(path);
6937 	if (em->start > start || extent_map_end(em) <= start) {
6938 		btrfs_err(fs_info,
6939 			  "bad extent! em: [%llu %llu] passed [%llu %llu]",
6940 			  em->start, em->len, start, len);
6941 		err = -EIO;
6942 		goto out;
6943 	}
6944 
6945 	err = 0;
6946 	write_lock(&em_tree->lock);
6947 	ret = add_extent_mapping(em_tree, em, 0);
6948 	/* it is possible that someone inserted the extent into the tree
6949 	 * while we had the lock dropped.  It is also possible that
6950 	 * an overlapping map exists in the tree
6951 	 */
6952 	if (ret == -EEXIST) {
6953 		struct extent_map *existing;
6954 
6955 		ret = 0;
6956 
6957 		existing = search_extent_mapping(em_tree, start, len);
6958 		/*
6959 		 * existing will always be non-NULL, since there must be
6960 		 * extent causing the -EEXIST.
6961 		 */
6962 		if (existing->start == em->start &&
6963 		    extent_map_end(existing) >= extent_map_end(em) &&
6964 		    em->block_start == existing->block_start) {
6965 			/*
6966 			 * The existing extent map already encompasses the
6967 			 * entire extent map we tried to add.
6968 			 */
6969 			free_extent_map(em);
6970 			em = existing;
6971 			err = 0;
6972 
6973 		} else if (start >= extent_map_end(existing) ||
6974 		    start <= existing->start) {
6975 			/*
6976 			 * The existing extent map is the one nearest to
6977 			 * the [start, start + len) range which overlaps
6978 			 */
6979 			err = merge_extent_mapping(em_tree, existing,
6980 						   em, start);
6981 			free_extent_map(existing);
6982 			if (err) {
6983 				free_extent_map(em);
6984 				em = NULL;
6985 			}
6986 		} else {
6987 			free_extent_map(em);
6988 			em = existing;
6989 			err = 0;
6990 		}
6991 	}
6992 	write_unlock(&em_tree->lock);
6993 out:
6994 
6995 	trace_btrfs_get_extent(root, BTRFS_I(inode), em);
6996 
6997 	btrfs_free_path(path);
6998 	if (trans) {
6999 		ret = btrfs_end_transaction(trans);
7000 		if (!err)
7001 			err = ret;
7002 	}
7003 	if (err) {
7004 		free_extent_map(em);
7005 		return ERR_PTR(err);
7006 	}
7007 	BUG_ON(!em); /* Error is always set */
7008 	return em;
7009 }
7010 
7011 struct extent_map *btrfs_get_extent_fiemap(struct inode *inode, struct page *page,
7012 					   size_t pg_offset, u64 start, u64 len,
7013 					   int create)
7014 {
7015 	struct extent_map *em;
7016 	struct extent_map *hole_em = NULL;
7017 	u64 range_start = start;
7018 	u64 end;
7019 	u64 found;
7020 	u64 found_end;
7021 	int err = 0;
7022 
7023 	em = btrfs_get_extent(inode, page, pg_offset, start, len, create);
7024 	if (IS_ERR(em))
7025 		return em;
7026 	if (em) {
7027 		/*
7028 		 * if our em maps to
7029 		 * -  a hole or
7030 		 * -  a pre-alloc extent,
7031 		 * there might actually be delalloc bytes behind it.
7032 		 */
7033 		if (em->block_start != EXTENT_MAP_HOLE &&
7034 		    !test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7035 			return em;
7036 		else
7037 			hole_em = em;
7038 	}
7039 
7040 	/* check to see if we've wrapped (len == -1 or similar) */
7041 	end = start + len;
7042 	if (end < start)
7043 		end = (u64)-1;
7044 	else
7045 		end -= 1;
7046 
7047 	em = NULL;
7048 
7049 	/* ok, we didn't find anything, lets look for delalloc */
7050 	found = count_range_bits(&BTRFS_I(inode)->io_tree, &range_start,
7051 				 end, len, EXTENT_DELALLOC, 1);
7052 	found_end = range_start + found;
7053 	if (found_end < range_start)
7054 		found_end = (u64)-1;
7055 
7056 	/*
7057 	 * we didn't find anything useful, return
7058 	 * the original results from get_extent()
7059 	 */
7060 	if (range_start > end || found_end <= start) {
7061 		em = hole_em;
7062 		hole_em = NULL;
7063 		goto out;
7064 	}
7065 
7066 	/* adjust the range_start to make sure it doesn't
7067 	 * go backwards from the start they passed in
7068 	 */
7069 	range_start = max(start, range_start);
7070 	found = found_end - range_start;
7071 
7072 	if (found > 0) {
7073 		u64 hole_start = start;
7074 		u64 hole_len = len;
7075 
7076 		em = alloc_extent_map();
7077 		if (!em) {
7078 			err = -ENOMEM;
7079 			goto out;
7080 		}
7081 		/*
7082 		 * when btrfs_get_extent can't find anything it
7083 		 * returns one huge hole
7084 		 *
7085 		 * make sure what it found really fits our range, and
7086 		 * adjust to make sure it is based on the start from
7087 		 * the caller
7088 		 */
7089 		if (hole_em) {
7090 			u64 calc_end = extent_map_end(hole_em);
7091 
7092 			if (calc_end <= start || (hole_em->start > end)) {
7093 				free_extent_map(hole_em);
7094 				hole_em = NULL;
7095 			} else {
7096 				hole_start = max(hole_em->start, start);
7097 				hole_len = calc_end - hole_start;
7098 			}
7099 		}
7100 		em->bdev = NULL;
7101 		if (hole_em && range_start > hole_start) {
7102 			/* our hole starts before our delalloc, so we
7103 			 * have to return just the parts of the hole
7104 			 * that go until  the delalloc starts
7105 			 */
7106 			em->len = min(hole_len,
7107 				      range_start - hole_start);
7108 			em->start = hole_start;
7109 			em->orig_start = hole_start;
7110 			/*
7111 			 * don't adjust block start at all,
7112 			 * it is fixed at EXTENT_MAP_HOLE
7113 			 */
7114 			em->block_start = hole_em->block_start;
7115 			em->block_len = hole_len;
7116 			if (test_bit(EXTENT_FLAG_PREALLOC, &hole_em->flags))
7117 				set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
7118 		} else {
7119 			em->start = range_start;
7120 			em->len = found;
7121 			em->orig_start = range_start;
7122 			em->block_start = EXTENT_MAP_DELALLOC;
7123 			em->block_len = found;
7124 		}
7125 	} else if (hole_em) {
7126 		return hole_em;
7127 	}
7128 out:
7129 
7130 	free_extent_map(hole_em);
7131 	if (err) {
7132 		free_extent_map(em);
7133 		return ERR_PTR(err);
7134 	}
7135 	return em;
7136 }
7137 
7138 static struct extent_map *btrfs_create_dio_extent(struct inode *inode,
7139 						  const u64 start,
7140 						  const u64 len,
7141 						  const u64 orig_start,
7142 						  const u64 block_start,
7143 						  const u64 block_len,
7144 						  const u64 orig_block_len,
7145 						  const u64 ram_bytes,
7146 						  const int type)
7147 {
7148 	struct extent_map *em = NULL;
7149 	int ret;
7150 
7151 	if (type != BTRFS_ORDERED_NOCOW) {
7152 		em = create_io_em(inode, start, len, orig_start,
7153 				  block_start, block_len, orig_block_len,
7154 				  ram_bytes,
7155 				  BTRFS_COMPRESS_NONE, /* compress_type */
7156 				  type);
7157 		if (IS_ERR(em))
7158 			goto out;
7159 	}
7160 	ret = btrfs_add_ordered_extent_dio(inode, start, block_start,
7161 					   len, block_len, type);
7162 	if (ret) {
7163 		if (em) {
7164 			free_extent_map(em);
7165 			btrfs_drop_extent_cache(inode, start,
7166 						start + len - 1, 0);
7167 		}
7168 		em = ERR_PTR(ret);
7169 	}
7170  out:
7171 
7172 	return em;
7173 }
7174 
7175 static struct extent_map *btrfs_new_extent_direct(struct inode *inode,
7176 						  u64 start, u64 len)
7177 {
7178 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7179 	struct btrfs_root *root = BTRFS_I(inode)->root;
7180 	struct extent_map *em;
7181 	struct btrfs_key ins;
7182 	u64 alloc_hint;
7183 	int ret;
7184 
7185 	alloc_hint = get_extent_allocation_hint(inode, start, len);
7186 	ret = btrfs_reserve_extent(root, len, len, fs_info->sectorsize,
7187 				   0, alloc_hint, &ins, 1, 1);
7188 	if (ret)
7189 		return ERR_PTR(ret);
7190 
7191 	em = btrfs_create_dio_extent(inode, start, ins.offset, start,
7192 				     ins.objectid, ins.offset, ins.offset,
7193 				     ins.offset, BTRFS_ORDERED_REGULAR);
7194 	btrfs_dec_block_group_reservations(fs_info, ins.objectid);
7195 	if (IS_ERR(em))
7196 		btrfs_free_reserved_extent(fs_info, ins.objectid,
7197 					   ins.offset, 1);
7198 
7199 	return em;
7200 }
7201 
7202 /*
7203  * returns 1 when the nocow is safe, < 1 on error, 0 if the
7204  * block must be cow'd
7205  */
7206 noinline int can_nocow_extent(struct inode *inode, u64 offset, u64 *len,
7207 			      u64 *orig_start, u64 *orig_block_len,
7208 			      u64 *ram_bytes)
7209 {
7210 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7211 	struct btrfs_path *path;
7212 	int ret;
7213 	struct extent_buffer *leaf;
7214 	struct btrfs_root *root = BTRFS_I(inode)->root;
7215 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
7216 	struct btrfs_file_extent_item *fi;
7217 	struct btrfs_key key;
7218 	u64 disk_bytenr;
7219 	u64 backref_offset;
7220 	u64 extent_end;
7221 	u64 num_bytes;
7222 	int slot;
7223 	int found_type;
7224 	bool nocow = (BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW);
7225 
7226 	path = btrfs_alloc_path();
7227 	if (!path)
7228 		return -ENOMEM;
7229 
7230 	ret = btrfs_lookup_file_extent(NULL, root, path,
7231 			btrfs_ino(BTRFS_I(inode)), offset, 0);
7232 	if (ret < 0)
7233 		goto out;
7234 
7235 	slot = path->slots[0];
7236 	if (ret == 1) {
7237 		if (slot == 0) {
7238 			/* can't find the item, must cow */
7239 			ret = 0;
7240 			goto out;
7241 		}
7242 		slot--;
7243 	}
7244 	ret = 0;
7245 	leaf = path->nodes[0];
7246 	btrfs_item_key_to_cpu(leaf, &key, slot);
7247 	if (key.objectid != btrfs_ino(BTRFS_I(inode)) ||
7248 	    key.type != BTRFS_EXTENT_DATA_KEY) {
7249 		/* not our file or wrong item type, must cow */
7250 		goto out;
7251 	}
7252 
7253 	if (key.offset > offset) {
7254 		/* Wrong offset, must cow */
7255 		goto out;
7256 	}
7257 
7258 	fi = btrfs_item_ptr(leaf, slot, struct btrfs_file_extent_item);
7259 	found_type = btrfs_file_extent_type(leaf, fi);
7260 	if (found_type != BTRFS_FILE_EXTENT_REG &&
7261 	    found_type != BTRFS_FILE_EXTENT_PREALLOC) {
7262 		/* not a regular extent, must cow */
7263 		goto out;
7264 	}
7265 
7266 	if (!nocow && found_type == BTRFS_FILE_EXTENT_REG)
7267 		goto out;
7268 
7269 	extent_end = key.offset + btrfs_file_extent_num_bytes(leaf, fi);
7270 	if (extent_end <= offset)
7271 		goto out;
7272 
7273 	disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, fi);
7274 	if (disk_bytenr == 0)
7275 		goto out;
7276 
7277 	if (btrfs_file_extent_compression(leaf, fi) ||
7278 	    btrfs_file_extent_encryption(leaf, fi) ||
7279 	    btrfs_file_extent_other_encoding(leaf, fi))
7280 		goto out;
7281 
7282 	backref_offset = btrfs_file_extent_offset(leaf, fi);
7283 
7284 	if (orig_start) {
7285 		*orig_start = key.offset - backref_offset;
7286 		*orig_block_len = btrfs_file_extent_disk_num_bytes(leaf, fi);
7287 		*ram_bytes = btrfs_file_extent_ram_bytes(leaf, fi);
7288 	}
7289 
7290 	if (btrfs_extent_readonly(fs_info, disk_bytenr))
7291 		goto out;
7292 
7293 	num_bytes = min(offset + *len, extent_end) - offset;
7294 	if (!nocow && found_type == BTRFS_FILE_EXTENT_PREALLOC) {
7295 		u64 range_end;
7296 
7297 		range_end = round_up(offset + num_bytes,
7298 				     root->fs_info->sectorsize) - 1;
7299 		ret = test_range_bit(io_tree, offset, range_end,
7300 				     EXTENT_DELALLOC, 0, NULL);
7301 		if (ret) {
7302 			ret = -EAGAIN;
7303 			goto out;
7304 		}
7305 	}
7306 
7307 	btrfs_release_path(path);
7308 
7309 	/*
7310 	 * look for other files referencing this extent, if we
7311 	 * find any we must cow
7312 	 */
7313 
7314 	ret = btrfs_cross_ref_exist(root, btrfs_ino(BTRFS_I(inode)),
7315 				    key.offset - backref_offset, disk_bytenr);
7316 	if (ret) {
7317 		ret = 0;
7318 		goto out;
7319 	}
7320 
7321 	/*
7322 	 * adjust disk_bytenr and num_bytes to cover just the bytes
7323 	 * in this extent we are about to write.  If there
7324 	 * are any csums in that range we have to cow in order
7325 	 * to keep the csums correct
7326 	 */
7327 	disk_bytenr += backref_offset;
7328 	disk_bytenr += offset - key.offset;
7329 	if (csum_exist_in_range(fs_info, disk_bytenr, num_bytes))
7330 		goto out;
7331 	/*
7332 	 * all of the above have passed, it is safe to overwrite this extent
7333 	 * without cow
7334 	 */
7335 	*len = num_bytes;
7336 	ret = 1;
7337 out:
7338 	btrfs_free_path(path);
7339 	return ret;
7340 }
7341 
7342 bool btrfs_page_exists_in_range(struct inode *inode, loff_t start, loff_t end)
7343 {
7344 	struct radix_tree_root *root = &inode->i_mapping->page_tree;
7345 	int found = false;
7346 	void **pagep = NULL;
7347 	struct page *page = NULL;
7348 	int start_idx;
7349 	int end_idx;
7350 
7351 	start_idx = start >> PAGE_SHIFT;
7352 
7353 	/*
7354 	 * end is the last byte in the last page.  end == start is legal
7355 	 */
7356 	end_idx = end >> PAGE_SHIFT;
7357 
7358 	rcu_read_lock();
7359 
7360 	/* Most of the code in this while loop is lifted from
7361 	 * find_get_page.  It's been modified to begin searching from a
7362 	 * page and return just the first page found in that range.  If the
7363 	 * found idx is less than or equal to the end idx then we know that
7364 	 * a page exists.  If no pages are found or if those pages are
7365 	 * outside of the range then we're fine (yay!) */
7366 	while (page == NULL &&
7367 	       radix_tree_gang_lookup_slot(root, &pagep, NULL, start_idx, 1)) {
7368 		page = radix_tree_deref_slot(pagep);
7369 		if (unlikely(!page))
7370 			break;
7371 
7372 		if (radix_tree_exception(page)) {
7373 			if (radix_tree_deref_retry(page)) {
7374 				page = NULL;
7375 				continue;
7376 			}
7377 			/*
7378 			 * Otherwise, shmem/tmpfs must be storing a swap entry
7379 			 * here as an exceptional entry: so return it without
7380 			 * attempting to raise page count.
7381 			 */
7382 			page = NULL;
7383 			break; /* TODO: Is this relevant for this use case? */
7384 		}
7385 
7386 		if (!page_cache_get_speculative(page)) {
7387 			page = NULL;
7388 			continue;
7389 		}
7390 
7391 		/*
7392 		 * Has the page moved?
7393 		 * This is part of the lockless pagecache protocol. See
7394 		 * include/linux/pagemap.h for details.
7395 		 */
7396 		if (unlikely(page != *pagep)) {
7397 			put_page(page);
7398 			page = NULL;
7399 		}
7400 	}
7401 
7402 	if (page) {
7403 		if (page->index <= end_idx)
7404 			found = true;
7405 		put_page(page);
7406 	}
7407 
7408 	rcu_read_unlock();
7409 	return found;
7410 }
7411 
7412 static int lock_extent_direct(struct inode *inode, u64 lockstart, u64 lockend,
7413 			      struct extent_state **cached_state, int writing)
7414 {
7415 	struct btrfs_ordered_extent *ordered;
7416 	int ret = 0;
7417 
7418 	while (1) {
7419 		lock_extent_bits(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7420 				 cached_state);
7421 		/*
7422 		 * We're concerned with the entire range that we're going to be
7423 		 * doing DIO to, so we need to make sure there's no ordered
7424 		 * extents in this range.
7425 		 */
7426 		ordered = btrfs_lookup_ordered_range(inode, lockstart,
7427 						     lockend - lockstart + 1);
7428 
7429 		/*
7430 		 * We need to make sure there are no buffered pages in this
7431 		 * range either, we could have raced between the invalidate in
7432 		 * generic_file_direct_write and locking the extent.  The
7433 		 * invalidate needs to happen so that reads after a write do not
7434 		 * get stale data.
7435 		 */
7436 		if (!ordered &&
7437 		    (!writing ||
7438 		     !btrfs_page_exists_in_range(inode, lockstart, lockend)))
7439 			break;
7440 
7441 		unlock_extent_cached(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7442 				     cached_state, GFP_NOFS);
7443 
7444 		if (ordered) {
7445 			/*
7446 			 * If we are doing a DIO read and the ordered extent we
7447 			 * found is for a buffered write, we can not wait for it
7448 			 * to complete and retry, because if we do so we can
7449 			 * deadlock with concurrent buffered writes on page
7450 			 * locks. This happens only if our DIO read covers more
7451 			 * than one extent map, if at this point has already
7452 			 * created an ordered extent for a previous extent map
7453 			 * and locked its range in the inode's io tree, and a
7454 			 * concurrent write against that previous extent map's
7455 			 * range and this range started (we unlock the ranges
7456 			 * in the io tree only when the bios complete and
7457 			 * buffered writes always lock pages before attempting
7458 			 * to lock range in the io tree).
7459 			 */
7460 			if (writing ||
7461 			    test_bit(BTRFS_ORDERED_DIRECT, &ordered->flags))
7462 				btrfs_start_ordered_extent(inode, ordered, 1);
7463 			else
7464 				ret = -ENOTBLK;
7465 			btrfs_put_ordered_extent(ordered);
7466 		} else {
7467 			/*
7468 			 * We could trigger writeback for this range (and wait
7469 			 * for it to complete) and then invalidate the pages for
7470 			 * this range (through invalidate_inode_pages2_range()),
7471 			 * but that can lead us to a deadlock with a concurrent
7472 			 * call to readpages() (a buffered read or a defrag call
7473 			 * triggered a readahead) on a page lock due to an
7474 			 * ordered dio extent we created before but did not have
7475 			 * yet a corresponding bio submitted (whence it can not
7476 			 * complete), which makes readpages() wait for that
7477 			 * ordered extent to complete while holding a lock on
7478 			 * that page.
7479 			 */
7480 			ret = -ENOTBLK;
7481 		}
7482 
7483 		if (ret)
7484 			break;
7485 
7486 		cond_resched();
7487 	}
7488 
7489 	return ret;
7490 }
7491 
7492 /* The callers of this must take lock_extent() */
7493 static struct extent_map *create_io_em(struct inode *inode, u64 start, u64 len,
7494 				       u64 orig_start, u64 block_start,
7495 				       u64 block_len, u64 orig_block_len,
7496 				       u64 ram_bytes, int compress_type,
7497 				       int type)
7498 {
7499 	struct extent_map_tree *em_tree;
7500 	struct extent_map *em;
7501 	struct btrfs_root *root = BTRFS_I(inode)->root;
7502 	int ret;
7503 
7504 	ASSERT(type == BTRFS_ORDERED_PREALLOC ||
7505 	       type == BTRFS_ORDERED_COMPRESSED ||
7506 	       type == BTRFS_ORDERED_NOCOW ||
7507 	       type == BTRFS_ORDERED_REGULAR);
7508 
7509 	em_tree = &BTRFS_I(inode)->extent_tree;
7510 	em = alloc_extent_map();
7511 	if (!em)
7512 		return ERR_PTR(-ENOMEM);
7513 
7514 	em->start = start;
7515 	em->orig_start = orig_start;
7516 	em->len = len;
7517 	em->block_len = block_len;
7518 	em->block_start = block_start;
7519 	em->bdev = root->fs_info->fs_devices->latest_bdev;
7520 	em->orig_block_len = orig_block_len;
7521 	em->ram_bytes = ram_bytes;
7522 	em->generation = -1;
7523 	set_bit(EXTENT_FLAG_PINNED, &em->flags);
7524 	if (type == BTRFS_ORDERED_PREALLOC) {
7525 		set_bit(EXTENT_FLAG_FILLING, &em->flags);
7526 	} else if (type == BTRFS_ORDERED_COMPRESSED) {
7527 		set_bit(EXTENT_FLAG_COMPRESSED, &em->flags);
7528 		em->compress_type = compress_type;
7529 	}
7530 
7531 	do {
7532 		btrfs_drop_extent_cache(inode, em->start,
7533 				em->start + em->len - 1, 0);
7534 		write_lock(&em_tree->lock);
7535 		ret = add_extent_mapping(em_tree, em, 1);
7536 		write_unlock(&em_tree->lock);
7537 		/*
7538 		 * The caller has taken lock_extent(), who could race with us
7539 		 * to add em?
7540 		 */
7541 	} while (ret == -EEXIST);
7542 
7543 	if (ret) {
7544 		free_extent_map(em);
7545 		return ERR_PTR(ret);
7546 	}
7547 
7548 	/* em got 2 refs now, callers needs to do free_extent_map once. */
7549 	return em;
7550 }
7551 
7552 static void adjust_dio_outstanding_extents(struct inode *inode,
7553 					   struct btrfs_dio_data *dio_data,
7554 					   const u64 len)
7555 {
7556 	unsigned num_extents = count_max_extents(len);
7557 
7558 	/*
7559 	 * If we have an outstanding_extents count still set then we're
7560 	 * within our reservation, otherwise we need to adjust our inode
7561 	 * counter appropriately.
7562 	 */
7563 	if (dio_data->outstanding_extents >= num_extents) {
7564 		dio_data->outstanding_extents -= num_extents;
7565 	} else {
7566 		/*
7567 		 * If dio write length has been split due to no large enough
7568 		 * contiguous space, we need to compensate our inode counter
7569 		 * appropriately.
7570 		 */
7571 		u64 num_needed = num_extents - dio_data->outstanding_extents;
7572 
7573 		spin_lock(&BTRFS_I(inode)->lock);
7574 		BTRFS_I(inode)->outstanding_extents += num_needed;
7575 		spin_unlock(&BTRFS_I(inode)->lock);
7576 	}
7577 }
7578 
7579 static int btrfs_get_blocks_direct(struct inode *inode, sector_t iblock,
7580 				   struct buffer_head *bh_result, int create)
7581 {
7582 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7583 	struct extent_map *em;
7584 	struct extent_state *cached_state = NULL;
7585 	struct btrfs_dio_data *dio_data = NULL;
7586 	u64 start = iblock << inode->i_blkbits;
7587 	u64 lockstart, lockend;
7588 	u64 len = bh_result->b_size;
7589 	int unlock_bits = EXTENT_LOCKED;
7590 	int ret = 0;
7591 
7592 	if (create)
7593 		unlock_bits |= EXTENT_DIRTY;
7594 	else
7595 		len = min_t(u64, len, fs_info->sectorsize);
7596 
7597 	lockstart = start;
7598 	lockend = start + len - 1;
7599 
7600 	if (current->journal_info) {
7601 		/*
7602 		 * Need to pull our outstanding extents and set journal_info to NULL so
7603 		 * that anything that needs to check if there's a transaction doesn't get
7604 		 * confused.
7605 		 */
7606 		dio_data = current->journal_info;
7607 		current->journal_info = NULL;
7608 	}
7609 
7610 	/*
7611 	 * If this errors out it's because we couldn't invalidate pagecache for
7612 	 * this range and we need to fallback to buffered.
7613 	 */
7614 	if (lock_extent_direct(inode, lockstart, lockend, &cached_state,
7615 			       create)) {
7616 		ret = -ENOTBLK;
7617 		goto err;
7618 	}
7619 
7620 	em = btrfs_get_extent(inode, NULL, 0, start, len, 0);
7621 	if (IS_ERR(em)) {
7622 		ret = PTR_ERR(em);
7623 		goto unlock_err;
7624 	}
7625 
7626 	/*
7627 	 * Ok for INLINE and COMPRESSED extents we need to fallback on buffered
7628 	 * io.  INLINE is special, and we could probably kludge it in here, but
7629 	 * it's still buffered so for safety lets just fall back to the generic
7630 	 * buffered path.
7631 	 *
7632 	 * For COMPRESSED we _have_ to read the entire extent in so we can
7633 	 * decompress it, so there will be buffering required no matter what we
7634 	 * do, so go ahead and fallback to buffered.
7635 	 *
7636 	 * We return -ENOTBLK because that's what makes DIO go ahead and go back
7637 	 * to buffered IO.  Don't blame me, this is the price we pay for using
7638 	 * the generic code.
7639 	 */
7640 	if (test_bit(EXTENT_FLAG_COMPRESSED, &em->flags) ||
7641 	    em->block_start == EXTENT_MAP_INLINE) {
7642 		free_extent_map(em);
7643 		ret = -ENOTBLK;
7644 		goto unlock_err;
7645 	}
7646 
7647 	/* Just a good old fashioned hole, return */
7648 	if (!create && (em->block_start == EXTENT_MAP_HOLE ||
7649 			test_bit(EXTENT_FLAG_PREALLOC, &em->flags))) {
7650 		free_extent_map(em);
7651 		goto unlock_err;
7652 	}
7653 
7654 	/*
7655 	 * We don't allocate a new extent in the following cases
7656 	 *
7657 	 * 1) The inode is marked as NODATACOW.  In this case we'll just use the
7658 	 * existing extent.
7659 	 * 2) The extent is marked as PREALLOC.  We're good to go here and can
7660 	 * just use the extent.
7661 	 *
7662 	 */
7663 	if (!create) {
7664 		len = min(len, em->len - (start - em->start));
7665 		lockstart = start + len;
7666 		goto unlock;
7667 	}
7668 
7669 	if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags) ||
7670 	    ((BTRFS_I(inode)->flags & BTRFS_INODE_NODATACOW) &&
7671 	     em->block_start != EXTENT_MAP_HOLE)) {
7672 		int type;
7673 		u64 block_start, orig_start, orig_block_len, ram_bytes;
7674 
7675 		if (test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7676 			type = BTRFS_ORDERED_PREALLOC;
7677 		else
7678 			type = BTRFS_ORDERED_NOCOW;
7679 		len = min(len, em->len - (start - em->start));
7680 		block_start = em->block_start + (start - em->start);
7681 
7682 		if (can_nocow_extent(inode, start, &len, &orig_start,
7683 				     &orig_block_len, &ram_bytes) == 1 &&
7684 		    btrfs_inc_nocow_writers(fs_info, block_start)) {
7685 			struct extent_map *em2;
7686 
7687 			em2 = btrfs_create_dio_extent(inode, start, len,
7688 						      orig_start, block_start,
7689 						      len, orig_block_len,
7690 						      ram_bytes, type);
7691 			btrfs_dec_nocow_writers(fs_info, block_start);
7692 			if (type == BTRFS_ORDERED_PREALLOC) {
7693 				free_extent_map(em);
7694 				em = em2;
7695 			}
7696 			if (em2 && IS_ERR(em2)) {
7697 				ret = PTR_ERR(em2);
7698 				goto unlock_err;
7699 			}
7700 			/*
7701 			 * For inode marked NODATACOW or extent marked PREALLOC,
7702 			 * use the existing or preallocated extent, so does not
7703 			 * need to adjust btrfs_space_info's bytes_may_use.
7704 			 */
7705 			btrfs_free_reserved_data_space_noquota(inode,
7706 					start, len);
7707 			goto unlock;
7708 		}
7709 	}
7710 
7711 	/*
7712 	 * this will cow the extent, reset the len in case we changed
7713 	 * it above
7714 	 */
7715 	len = bh_result->b_size;
7716 	free_extent_map(em);
7717 	em = btrfs_new_extent_direct(inode, start, len);
7718 	if (IS_ERR(em)) {
7719 		ret = PTR_ERR(em);
7720 		goto unlock_err;
7721 	}
7722 	len = min(len, em->len - (start - em->start));
7723 unlock:
7724 	bh_result->b_blocknr = (em->block_start + (start - em->start)) >>
7725 		inode->i_blkbits;
7726 	bh_result->b_size = len;
7727 	bh_result->b_bdev = em->bdev;
7728 	set_buffer_mapped(bh_result);
7729 	if (create) {
7730 		if (!test_bit(EXTENT_FLAG_PREALLOC, &em->flags))
7731 			set_buffer_new(bh_result);
7732 
7733 		/*
7734 		 * Need to update the i_size under the extent lock so buffered
7735 		 * readers will get the updated i_size when we unlock.
7736 		 */
7737 		if (!dio_data->overwrite && start + len > i_size_read(inode))
7738 			i_size_write(inode, start + len);
7739 
7740 		adjust_dio_outstanding_extents(inode, dio_data, len);
7741 		WARN_ON(dio_data->reserve < len);
7742 		dio_data->reserve -= len;
7743 		dio_data->unsubmitted_oe_range_end = start + len;
7744 		current->journal_info = dio_data;
7745 	}
7746 
7747 	/*
7748 	 * In the case of write we need to clear and unlock the entire range,
7749 	 * in the case of read we need to unlock only the end area that we
7750 	 * aren't using if there is any left over space.
7751 	 */
7752 	if (lockstart < lockend) {
7753 		clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart,
7754 				 lockend, unlock_bits, 1, 0,
7755 				 &cached_state, GFP_NOFS);
7756 	} else {
7757 		free_extent_state(cached_state);
7758 	}
7759 
7760 	free_extent_map(em);
7761 
7762 	return 0;
7763 
7764 unlock_err:
7765 	clear_extent_bit(&BTRFS_I(inode)->io_tree, lockstart, lockend,
7766 			 unlock_bits, 1, 0, &cached_state, GFP_NOFS);
7767 err:
7768 	if (dio_data)
7769 		current->journal_info = dio_data;
7770 	/*
7771 	 * Compensate the delalloc release we do in btrfs_direct_IO() when we
7772 	 * write less data then expected, so that we don't underflow our inode's
7773 	 * outstanding extents counter.
7774 	 */
7775 	if (create && dio_data)
7776 		adjust_dio_outstanding_extents(inode, dio_data, len);
7777 
7778 	return ret;
7779 }
7780 
7781 static inline int submit_dio_repair_bio(struct inode *inode, struct bio *bio,
7782 					int mirror_num)
7783 {
7784 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7785 	int ret;
7786 
7787 	BUG_ON(bio_op(bio) == REQ_OP_WRITE);
7788 
7789 	bio_get(bio);
7790 
7791 	ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DIO_REPAIR);
7792 	if (ret)
7793 		goto err;
7794 
7795 	ret = btrfs_map_bio(fs_info, bio, mirror_num, 0);
7796 err:
7797 	bio_put(bio);
7798 	return ret;
7799 }
7800 
7801 static int btrfs_check_dio_repairable(struct inode *inode,
7802 				      struct bio *failed_bio,
7803 				      struct io_failure_record *failrec,
7804 				      int failed_mirror)
7805 {
7806 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
7807 	int num_copies;
7808 
7809 	num_copies = btrfs_num_copies(fs_info, failrec->logical, failrec->len);
7810 	if (num_copies == 1) {
7811 		/*
7812 		 * we only have a single copy of the data, so don't bother with
7813 		 * all the retry and error correction code that follows. no
7814 		 * matter what the error is, it is very likely to persist.
7815 		 */
7816 		btrfs_debug(fs_info,
7817 			"Check DIO Repairable: cannot repair, num_copies=%d, next_mirror %d, failed_mirror %d",
7818 			num_copies, failrec->this_mirror, failed_mirror);
7819 		return 0;
7820 	}
7821 
7822 	failrec->failed_mirror = failed_mirror;
7823 	failrec->this_mirror++;
7824 	if (failrec->this_mirror == failed_mirror)
7825 		failrec->this_mirror++;
7826 
7827 	if (failrec->this_mirror > num_copies) {
7828 		btrfs_debug(fs_info,
7829 			"Check DIO Repairable: (fail) num_copies=%d, next_mirror %d, failed_mirror %d",
7830 			num_copies, failrec->this_mirror, failed_mirror);
7831 		return 0;
7832 	}
7833 
7834 	return 1;
7835 }
7836 
7837 static int dio_read_error(struct inode *inode, struct bio *failed_bio,
7838 			struct page *page, unsigned int pgoff,
7839 			u64 start, u64 end, int failed_mirror,
7840 			bio_end_io_t *repair_endio, void *repair_arg)
7841 {
7842 	struct io_failure_record *failrec;
7843 	struct bio *bio;
7844 	int isector;
7845 	int read_mode = 0;
7846 	int ret;
7847 
7848 	BUG_ON(bio_op(failed_bio) == REQ_OP_WRITE);
7849 
7850 	ret = btrfs_get_io_failure_record(inode, start, end, &failrec);
7851 	if (ret)
7852 		return ret;
7853 
7854 	ret = btrfs_check_dio_repairable(inode, failed_bio, failrec,
7855 					 failed_mirror);
7856 	if (!ret) {
7857 		free_io_failure(inode, failrec);
7858 		return -EIO;
7859 	}
7860 
7861 	if ((failed_bio->bi_vcnt > 1)
7862 		|| (failed_bio->bi_io_vec->bv_len
7863 			> btrfs_inode_sectorsize(inode)))
7864 		read_mode |= REQ_FAILFAST_DEV;
7865 
7866 	isector = start - btrfs_io_bio(failed_bio)->logical;
7867 	isector >>= inode->i_sb->s_blocksize_bits;
7868 	bio = btrfs_create_repair_bio(inode, failed_bio, failrec, page,
7869 				pgoff, isector, repair_endio, repair_arg);
7870 	if (!bio) {
7871 		free_io_failure(inode, failrec);
7872 		return -EIO;
7873 	}
7874 	bio_set_op_attrs(bio, REQ_OP_READ, read_mode);
7875 
7876 	btrfs_debug(BTRFS_I(inode)->root->fs_info,
7877 		    "Repair DIO Read Error: submitting new dio read[%#x] to this_mirror=%d, in_validation=%d\n",
7878 		    read_mode, failrec->this_mirror, failrec->in_validation);
7879 
7880 	ret = submit_dio_repair_bio(inode, bio, failrec->this_mirror);
7881 	if (ret) {
7882 		free_io_failure(inode, failrec);
7883 		bio_put(bio);
7884 	}
7885 
7886 	return ret;
7887 }
7888 
7889 struct btrfs_retry_complete {
7890 	struct completion done;
7891 	struct inode *inode;
7892 	u64 start;
7893 	int uptodate;
7894 };
7895 
7896 static void btrfs_retry_endio_nocsum(struct bio *bio)
7897 {
7898 	struct btrfs_retry_complete *done = bio->bi_private;
7899 	struct inode *inode;
7900 	struct bio_vec *bvec;
7901 	int i;
7902 
7903 	if (bio->bi_error)
7904 		goto end;
7905 
7906 	ASSERT(bio->bi_vcnt == 1);
7907 	inode = bio->bi_io_vec->bv_page->mapping->host;
7908 	ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
7909 
7910 	done->uptodate = 1;
7911 	bio_for_each_segment_all(bvec, bio, i)
7912 		clean_io_failure(done->inode, done->start, bvec->bv_page, 0);
7913 end:
7914 	complete(&done->done);
7915 	bio_put(bio);
7916 }
7917 
7918 static int __btrfs_correct_data_nocsum(struct inode *inode,
7919 				       struct btrfs_io_bio *io_bio)
7920 {
7921 	struct btrfs_fs_info *fs_info;
7922 	struct bio_vec *bvec;
7923 	struct btrfs_retry_complete done;
7924 	u64 start;
7925 	unsigned int pgoff;
7926 	u32 sectorsize;
7927 	int nr_sectors;
7928 	int i;
7929 	int ret;
7930 
7931 	fs_info = BTRFS_I(inode)->root->fs_info;
7932 	sectorsize = fs_info->sectorsize;
7933 
7934 	start = io_bio->logical;
7935 	done.inode = inode;
7936 
7937 	bio_for_each_segment_all(bvec, &io_bio->bio, i) {
7938 		nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
7939 		pgoff = bvec->bv_offset;
7940 
7941 next_block_or_try_again:
7942 		done.uptodate = 0;
7943 		done.start = start;
7944 		init_completion(&done.done);
7945 
7946 		ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
7947 				pgoff, start, start + sectorsize - 1,
7948 				io_bio->mirror_num,
7949 				btrfs_retry_endio_nocsum, &done);
7950 		if (ret)
7951 			return ret;
7952 
7953 		wait_for_completion(&done.done);
7954 
7955 		if (!done.uptodate) {
7956 			/* We might have another mirror, so try again */
7957 			goto next_block_or_try_again;
7958 		}
7959 
7960 		start += sectorsize;
7961 
7962 		if (nr_sectors--) {
7963 			pgoff += sectorsize;
7964 			goto next_block_or_try_again;
7965 		}
7966 	}
7967 
7968 	return 0;
7969 }
7970 
7971 static void btrfs_retry_endio(struct bio *bio)
7972 {
7973 	struct btrfs_retry_complete *done = bio->bi_private;
7974 	struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
7975 	struct inode *inode;
7976 	struct bio_vec *bvec;
7977 	u64 start;
7978 	int uptodate;
7979 	int ret;
7980 	int i;
7981 
7982 	if (bio->bi_error)
7983 		goto end;
7984 
7985 	uptodate = 1;
7986 
7987 	start = done->start;
7988 
7989 	ASSERT(bio->bi_vcnt == 1);
7990 	inode = bio->bi_io_vec->bv_page->mapping->host;
7991 	ASSERT(bio->bi_io_vec->bv_len == btrfs_inode_sectorsize(inode));
7992 
7993 	bio_for_each_segment_all(bvec, bio, i) {
7994 		ret = __readpage_endio_check(done->inode, io_bio, i,
7995 					bvec->bv_page, bvec->bv_offset,
7996 					done->start, bvec->bv_len);
7997 		if (!ret)
7998 			clean_io_failure(done->inode, done->start,
7999 					bvec->bv_page, bvec->bv_offset);
8000 		else
8001 			uptodate = 0;
8002 	}
8003 
8004 	done->uptodate = uptodate;
8005 end:
8006 	complete(&done->done);
8007 	bio_put(bio);
8008 }
8009 
8010 static int __btrfs_subio_endio_read(struct inode *inode,
8011 				    struct btrfs_io_bio *io_bio, int err)
8012 {
8013 	struct btrfs_fs_info *fs_info;
8014 	struct bio_vec *bvec;
8015 	struct btrfs_retry_complete done;
8016 	u64 start;
8017 	u64 offset = 0;
8018 	u32 sectorsize;
8019 	int nr_sectors;
8020 	unsigned int pgoff;
8021 	int csum_pos;
8022 	int i;
8023 	int ret;
8024 
8025 	fs_info = BTRFS_I(inode)->root->fs_info;
8026 	sectorsize = fs_info->sectorsize;
8027 
8028 	err = 0;
8029 	start = io_bio->logical;
8030 	done.inode = inode;
8031 
8032 	bio_for_each_segment_all(bvec, &io_bio->bio, i) {
8033 		nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8034 
8035 		pgoff = bvec->bv_offset;
8036 next_block:
8037 		csum_pos = BTRFS_BYTES_TO_BLKS(fs_info, offset);
8038 		ret = __readpage_endio_check(inode, io_bio, csum_pos,
8039 					bvec->bv_page, pgoff, start,
8040 					sectorsize);
8041 		if (likely(!ret))
8042 			goto next;
8043 try_again:
8044 		done.uptodate = 0;
8045 		done.start = start;
8046 		init_completion(&done.done);
8047 
8048 		ret = dio_read_error(inode, &io_bio->bio, bvec->bv_page,
8049 				pgoff, start, start + sectorsize - 1,
8050 				io_bio->mirror_num,
8051 				btrfs_retry_endio, &done);
8052 		if (ret) {
8053 			err = ret;
8054 			goto next;
8055 		}
8056 
8057 		wait_for_completion(&done.done);
8058 
8059 		if (!done.uptodate) {
8060 			/* We might have another mirror, so try again */
8061 			goto try_again;
8062 		}
8063 next:
8064 		offset += sectorsize;
8065 		start += sectorsize;
8066 
8067 		ASSERT(nr_sectors);
8068 
8069 		if (--nr_sectors) {
8070 			pgoff += sectorsize;
8071 			goto next_block;
8072 		}
8073 	}
8074 
8075 	return err;
8076 }
8077 
8078 static int btrfs_subio_endio_read(struct inode *inode,
8079 				  struct btrfs_io_bio *io_bio, int err)
8080 {
8081 	bool skip_csum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8082 
8083 	if (skip_csum) {
8084 		if (unlikely(err))
8085 			return __btrfs_correct_data_nocsum(inode, io_bio);
8086 		else
8087 			return 0;
8088 	} else {
8089 		return __btrfs_subio_endio_read(inode, io_bio, err);
8090 	}
8091 }
8092 
8093 static void btrfs_endio_direct_read(struct bio *bio)
8094 {
8095 	struct btrfs_dio_private *dip = bio->bi_private;
8096 	struct inode *inode = dip->inode;
8097 	struct bio *dio_bio;
8098 	struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8099 	int err = bio->bi_error;
8100 
8101 	if (dip->flags & BTRFS_DIO_ORIG_BIO_SUBMITTED)
8102 		err = btrfs_subio_endio_read(inode, io_bio, err);
8103 
8104 	unlock_extent(&BTRFS_I(inode)->io_tree, dip->logical_offset,
8105 		      dip->logical_offset + dip->bytes - 1);
8106 	dio_bio = dip->dio_bio;
8107 
8108 	kfree(dip);
8109 
8110 	dio_bio->bi_error = bio->bi_error;
8111 	dio_end_io(dio_bio, bio->bi_error);
8112 
8113 	if (io_bio->end_io)
8114 		io_bio->end_io(io_bio, err);
8115 	bio_put(bio);
8116 }
8117 
8118 static void btrfs_endio_direct_write_update_ordered(struct inode *inode,
8119 						    const u64 offset,
8120 						    const u64 bytes,
8121 						    const int uptodate)
8122 {
8123 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8124 	struct btrfs_ordered_extent *ordered = NULL;
8125 	u64 ordered_offset = offset;
8126 	u64 ordered_bytes = bytes;
8127 	int ret;
8128 
8129 again:
8130 	ret = btrfs_dec_test_first_ordered_pending(inode, &ordered,
8131 						   &ordered_offset,
8132 						   ordered_bytes,
8133 						   uptodate);
8134 	if (!ret)
8135 		goto out_test;
8136 
8137 	btrfs_init_work(&ordered->work, btrfs_endio_write_helper,
8138 			finish_ordered_fn, NULL, NULL);
8139 	btrfs_queue_work(fs_info->endio_write_workers, &ordered->work);
8140 out_test:
8141 	/*
8142 	 * our bio might span multiple ordered extents.  If we haven't
8143 	 * completed the accounting for the whole dio, go back and try again
8144 	 */
8145 	if (ordered_offset < offset + bytes) {
8146 		ordered_bytes = offset + bytes - ordered_offset;
8147 		ordered = NULL;
8148 		goto again;
8149 	}
8150 }
8151 
8152 static void btrfs_endio_direct_write(struct bio *bio)
8153 {
8154 	struct btrfs_dio_private *dip = bio->bi_private;
8155 	struct bio *dio_bio = dip->dio_bio;
8156 
8157 	btrfs_endio_direct_write_update_ordered(dip->inode,
8158 						dip->logical_offset,
8159 						dip->bytes,
8160 						!bio->bi_error);
8161 
8162 	kfree(dip);
8163 
8164 	dio_bio->bi_error = bio->bi_error;
8165 	dio_end_io(dio_bio, bio->bi_error);
8166 	bio_put(bio);
8167 }
8168 
8169 static int __btrfs_submit_bio_start_direct_io(struct inode *inode,
8170 				    struct bio *bio, int mirror_num,
8171 				    unsigned long bio_flags, u64 offset)
8172 {
8173 	int ret;
8174 	ret = btrfs_csum_one_bio(inode, bio, offset, 1);
8175 	BUG_ON(ret); /* -ENOMEM */
8176 	return 0;
8177 }
8178 
8179 static void btrfs_end_dio_bio(struct bio *bio)
8180 {
8181 	struct btrfs_dio_private *dip = bio->bi_private;
8182 	int err = bio->bi_error;
8183 
8184 	if (err)
8185 		btrfs_warn(BTRFS_I(dip->inode)->root->fs_info,
8186 			   "direct IO failed ino %llu rw %d,%u sector %#Lx len %u err no %d",
8187 			   btrfs_ino(BTRFS_I(dip->inode)), bio_op(bio),
8188 			   bio->bi_opf,
8189 			   (unsigned long long)bio->bi_iter.bi_sector,
8190 			   bio->bi_iter.bi_size, err);
8191 
8192 	if (dip->subio_endio)
8193 		err = dip->subio_endio(dip->inode, btrfs_io_bio(bio), err);
8194 
8195 	if (err) {
8196 		dip->errors = 1;
8197 
8198 		/*
8199 		 * before atomic variable goto zero, we must make sure
8200 		 * dip->errors is perceived to be set.
8201 		 */
8202 		smp_mb__before_atomic();
8203 	}
8204 
8205 	/* if there are more bios still pending for this dio, just exit */
8206 	if (!atomic_dec_and_test(&dip->pending_bios))
8207 		goto out;
8208 
8209 	if (dip->errors) {
8210 		bio_io_error(dip->orig_bio);
8211 	} else {
8212 		dip->dio_bio->bi_error = 0;
8213 		bio_endio(dip->orig_bio);
8214 	}
8215 out:
8216 	bio_put(bio);
8217 }
8218 
8219 static struct bio *btrfs_dio_bio_alloc(struct block_device *bdev,
8220 				       u64 first_sector, gfp_t gfp_flags)
8221 {
8222 	struct bio *bio;
8223 	bio = btrfs_bio_alloc(bdev, first_sector, BIO_MAX_PAGES, gfp_flags);
8224 	if (bio)
8225 		bio_associate_current(bio);
8226 	return bio;
8227 }
8228 
8229 static inline int btrfs_lookup_and_bind_dio_csum(struct inode *inode,
8230 						 struct btrfs_dio_private *dip,
8231 						 struct bio *bio,
8232 						 u64 file_offset)
8233 {
8234 	struct btrfs_io_bio *io_bio = btrfs_io_bio(bio);
8235 	struct btrfs_io_bio *orig_io_bio = btrfs_io_bio(dip->orig_bio);
8236 	int ret;
8237 
8238 	/*
8239 	 * We load all the csum data we need when we submit
8240 	 * the first bio to reduce the csum tree search and
8241 	 * contention.
8242 	 */
8243 	if (dip->logical_offset == file_offset) {
8244 		ret = btrfs_lookup_bio_sums_dio(inode, dip->orig_bio,
8245 						file_offset);
8246 		if (ret)
8247 			return ret;
8248 	}
8249 
8250 	if (bio == dip->orig_bio)
8251 		return 0;
8252 
8253 	file_offset -= dip->logical_offset;
8254 	file_offset >>= inode->i_sb->s_blocksize_bits;
8255 	io_bio->csum = (u8 *)(((u32 *)orig_io_bio->csum) + file_offset);
8256 
8257 	return 0;
8258 }
8259 
8260 static inline int __btrfs_submit_dio_bio(struct bio *bio, struct inode *inode,
8261 					 u64 file_offset, int skip_sum,
8262 					 int async_submit)
8263 {
8264 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8265 	struct btrfs_dio_private *dip = bio->bi_private;
8266 	bool write = bio_op(bio) == REQ_OP_WRITE;
8267 	int ret;
8268 
8269 	if (async_submit)
8270 		async_submit = !atomic_read(&BTRFS_I(inode)->sync_writers);
8271 
8272 	bio_get(bio);
8273 
8274 	if (!write) {
8275 		ret = btrfs_bio_wq_end_io(fs_info, bio, BTRFS_WQ_ENDIO_DATA);
8276 		if (ret)
8277 			goto err;
8278 	}
8279 
8280 	if (skip_sum)
8281 		goto map;
8282 
8283 	if (write && async_submit) {
8284 		ret = btrfs_wq_submit_bio(fs_info, inode, bio, 0, 0,
8285 					  file_offset,
8286 					  __btrfs_submit_bio_start_direct_io,
8287 					  __btrfs_submit_bio_done);
8288 		goto err;
8289 	} else if (write) {
8290 		/*
8291 		 * If we aren't doing async submit, calculate the csum of the
8292 		 * bio now.
8293 		 */
8294 		ret = btrfs_csum_one_bio(inode, bio, file_offset, 1);
8295 		if (ret)
8296 			goto err;
8297 	} else {
8298 		ret = btrfs_lookup_and_bind_dio_csum(inode, dip, bio,
8299 						     file_offset);
8300 		if (ret)
8301 			goto err;
8302 	}
8303 map:
8304 	ret = btrfs_map_bio(fs_info, bio, 0, async_submit);
8305 err:
8306 	bio_put(bio);
8307 	return ret;
8308 }
8309 
8310 static int btrfs_submit_direct_hook(struct btrfs_dio_private *dip,
8311 				    int skip_sum)
8312 {
8313 	struct inode *inode = dip->inode;
8314 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8315 	struct btrfs_root *root = BTRFS_I(inode)->root;
8316 	struct bio *bio;
8317 	struct bio *orig_bio = dip->orig_bio;
8318 	struct bio_vec *bvec;
8319 	u64 start_sector = orig_bio->bi_iter.bi_sector;
8320 	u64 file_offset = dip->logical_offset;
8321 	u64 submit_len = 0;
8322 	u64 map_length;
8323 	u32 blocksize = fs_info->sectorsize;
8324 	int async_submit = 0;
8325 	int nr_sectors;
8326 	int ret;
8327 	int i, j;
8328 
8329 	map_length = orig_bio->bi_iter.bi_size;
8330 	ret = btrfs_map_block(fs_info, btrfs_op(orig_bio), start_sector << 9,
8331 			      &map_length, NULL, 0);
8332 	if (ret)
8333 		return -EIO;
8334 
8335 	if (map_length >= orig_bio->bi_iter.bi_size) {
8336 		bio = orig_bio;
8337 		dip->flags |= BTRFS_DIO_ORIG_BIO_SUBMITTED;
8338 		goto submit;
8339 	}
8340 
8341 	/* async crcs make it difficult to collect full stripe writes. */
8342 	if (btrfs_get_alloc_profile(root, 1) & BTRFS_BLOCK_GROUP_RAID56_MASK)
8343 		async_submit = 0;
8344 	else
8345 		async_submit = 1;
8346 
8347 	bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev, start_sector, GFP_NOFS);
8348 	if (!bio)
8349 		return -ENOMEM;
8350 
8351 	bio->bi_opf = orig_bio->bi_opf;
8352 	bio->bi_private = dip;
8353 	bio->bi_end_io = btrfs_end_dio_bio;
8354 	btrfs_io_bio(bio)->logical = file_offset;
8355 	atomic_inc(&dip->pending_bios);
8356 
8357 	bio_for_each_segment_all(bvec, orig_bio, j) {
8358 		nr_sectors = BTRFS_BYTES_TO_BLKS(fs_info, bvec->bv_len);
8359 		i = 0;
8360 next_block:
8361 		if (unlikely(map_length < submit_len + blocksize ||
8362 		    bio_add_page(bio, bvec->bv_page, blocksize,
8363 			    bvec->bv_offset + (i * blocksize)) < blocksize)) {
8364 			/*
8365 			 * inc the count before we submit the bio so
8366 			 * we know the end IO handler won't happen before
8367 			 * we inc the count. Otherwise, the dip might get freed
8368 			 * before we're done setting it up
8369 			 */
8370 			atomic_inc(&dip->pending_bios);
8371 			ret = __btrfs_submit_dio_bio(bio, inode,
8372 						     file_offset, skip_sum,
8373 						     async_submit);
8374 			if (ret) {
8375 				bio_put(bio);
8376 				atomic_dec(&dip->pending_bios);
8377 				goto out_err;
8378 			}
8379 
8380 			start_sector += submit_len >> 9;
8381 			file_offset += submit_len;
8382 
8383 			submit_len = 0;
8384 
8385 			bio = btrfs_dio_bio_alloc(orig_bio->bi_bdev,
8386 						  start_sector, GFP_NOFS);
8387 			if (!bio)
8388 				goto out_err;
8389 			bio->bi_opf = orig_bio->bi_opf;
8390 			bio->bi_private = dip;
8391 			bio->bi_end_io = btrfs_end_dio_bio;
8392 			btrfs_io_bio(bio)->logical = file_offset;
8393 
8394 			map_length = orig_bio->bi_iter.bi_size;
8395 			ret = btrfs_map_block(fs_info, btrfs_op(orig_bio),
8396 					      start_sector << 9,
8397 					      &map_length, NULL, 0);
8398 			if (ret) {
8399 				bio_put(bio);
8400 				goto out_err;
8401 			}
8402 
8403 			goto next_block;
8404 		} else {
8405 			submit_len += blocksize;
8406 			if (--nr_sectors) {
8407 				i++;
8408 				goto next_block;
8409 			}
8410 		}
8411 	}
8412 
8413 submit:
8414 	ret = __btrfs_submit_dio_bio(bio, inode, file_offset, skip_sum,
8415 				     async_submit);
8416 	if (!ret)
8417 		return 0;
8418 
8419 	bio_put(bio);
8420 out_err:
8421 	dip->errors = 1;
8422 	/*
8423 	 * before atomic variable goto zero, we must
8424 	 * make sure dip->errors is perceived to be set.
8425 	 */
8426 	smp_mb__before_atomic();
8427 	if (atomic_dec_and_test(&dip->pending_bios))
8428 		bio_io_error(dip->orig_bio);
8429 
8430 	/* bio_end_io() will handle error, so we needn't return it */
8431 	return 0;
8432 }
8433 
8434 static void btrfs_submit_direct(struct bio *dio_bio, struct inode *inode,
8435 				loff_t file_offset)
8436 {
8437 	struct btrfs_dio_private *dip = NULL;
8438 	struct bio *io_bio = NULL;
8439 	struct btrfs_io_bio *btrfs_bio;
8440 	int skip_sum;
8441 	bool write = (bio_op(dio_bio) == REQ_OP_WRITE);
8442 	int ret = 0;
8443 
8444 	skip_sum = BTRFS_I(inode)->flags & BTRFS_INODE_NODATASUM;
8445 
8446 	io_bio = btrfs_bio_clone(dio_bio, GFP_NOFS);
8447 	if (!io_bio) {
8448 		ret = -ENOMEM;
8449 		goto free_ordered;
8450 	}
8451 
8452 	dip = kzalloc(sizeof(*dip), GFP_NOFS);
8453 	if (!dip) {
8454 		ret = -ENOMEM;
8455 		goto free_ordered;
8456 	}
8457 
8458 	dip->private = dio_bio->bi_private;
8459 	dip->inode = inode;
8460 	dip->logical_offset = file_offset;
8461 	dip->bytes = dio_bio->bi_iter.bi_size;
8462 	dip->disk_bytenr = (u64)dio_bio->bi_iter.bi_sector << 9;
8463 	io_bio->bi_private = dip;
8464 	dip->orig_bio = io_bio;
8465 	dip->dio_bio = dio_bio;
8466 	atomic_set(&dip->pending_bios, 0);
8467 	btrfs_bio = btrfs_io_bio(io_bio);
8468 	btrfs_bio->logical = file_offset;
8469 
8470 	if (write) {
8471 		io_bio->bi_end_io = btrfs_endio_direct_write;
8472 	} else {
8473 		io_bio->bi_end_io = btrfs_endio_direct_read;
8474 		dip->subio_endio = btrfs_subio_endio_read;
8475 	}
8476 
8477 	/*
8478 	 * Reset the range for unsubmitted ordered extents (to a 0 length range)
8479 	 * even if we fail to submit a bio, because in such case we do the
8480 	 * corresponding error handling below and it must not be done a second
8481 	 * time by btrfs_direct_IO().
8482 	 */
8483 	if (write) {
8484 		struct btrfs_dio_data *dio_data = current->journal_info;
8485 
8486 		dio_data->unsubmitted_oe_range_end = dip->logical_offset +
8487 			dip->bytes;
8488 		dio_data->unsubmitted_oe_range_start =
8489 			dio_data->unsubmitted_oe_range_end;
8490 	}
8491 
8492 	ret = btrfs_submit_direct_hook(dip, skip_sum);
8493 	if (!ret)
8494 		return;
8495 
8496 	if (btrfs_bio->end_io)
8497 		btrfs_bio->end_io(btrfs_bio, ret);
8498 
8499 free_ordered:
8500 	/*
8501 	 * If we arrived here it means either we failed to submit the dip
8502 	 * or we either failed to clone the dio_bio or failed to allocate the
8503 	 * dip. If we cloned the dio_bio and allocated the dip, we can just
8504 	 * call bio_endio against our io_bio so that we get proper resource
8505 	 * cleanup if we fail to submit the dip, otherwise, we must do the
8506 	 * same as btrfs_endio_direct_[write|read] because we can't call these
8507 	 * callbacks - they require an allocated dip and a clone of dio_bio.
8508 	 */
8509 	if (io_bio && dip) {
8510 		io_bio->bi_error = -EIO;
8511 		bio_endio(io_bio);
8512 		/*
8513 		 * The end io callbacks free our dip, do the final put on io_bio
8514 		 * and all the cleanup and final put for dio_bio (through
8515 		 * dio_end_io()).
8516 		 */
8517 		dip = NULL;
8518 		io_bio = NULL;
8519 	} else {
8520 		if (write)
8521 			btrfs_endio_direct_write_update_ordered(inode,
8522 						file_offset,
8523 						dio_bio->bi_iter.bi_size,
8524 						0);
8525 		else
8526 			unlock_extent(&BTRFS_I(inode)->io_tree, file_offset,
8527 			      file_offset + dio_bio->bi_iter.bi_size - 1);
8528 
8529 		dio_bio->bi_error = -EIO;
8530 		/*
8531 		 * Releases and cleans up our dio_bio, no need to bio_put()
8532 		 * nor bio_endio()/bio_io_error() against dio_bio.
8533 		 */
8534 		dio_end_io(dio_bio, ret);
8535 	}
8536 	if (io_bio)
8537 		bio_put(io_bio);
8538 	kfree(dip);
8539 }
8540 
8541 static ssize_t check_direct_IO(struct btrfs_fs_info *fs_info,
8542 			       struct kiocb *iocb,
8543 			       const struct iov_iter *iter, loff_t offset)
8544 {
8545 	int seg;
8546 	int i;
8547 	unsigned int blocksize_mask = fs_info->sectorsize - 1;
8548 	ssize_t retval = -EINVAL;
8549 
8550 	if (offset & blocksize_mask)
8551 		goto out;
8552 
8553 	if (iov_iter_alignment(iter) & blocksize_mask)
8554 		goto out;
8555 
8556 	/* If this is a write we don't need to check anymore */
8557 	if (iov_iter_rw(iter) != READ || !iter_is_iovec(iter))
8558 		return 0;
8559 	/*
8560 	 * Check to make sure we don't have duplicate iov_base's in this
8561 	 * iovec, if so return EINVAL, otherwise we'll get csum errors
8562 	 * when reading back.
8563 	 */
8564 	for (seg = 0; seg < iter->nr_segs; seg++) {
8565 		for (i = seg + 1; i < iter->nr_segs; i++) {
8566 			if (iter->iov[seg].iov_base == iter->iov[i].iov_base)
8567 				goto out;
8568 		}
8569 	}
8570 	retval = 0;
8571 out:
8572 	return retval;
8573 }
8574 
8575 static ssize_t btrfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)
8576 {
8577 	struct file *file = iocb->ki_filp;
8578 	struct inode *inode = file->f_mapping->host;
8579 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8580 	struct btrfs_dio_data dio_data = { 0 };
8581 	loff_t offset = iocb->ki_pos;
8582 	size_t count = 0;
8583 	int flags = 0;
8584 	bool wakeup = true;
8585 	bool relock = false;
8586 	ssize_t ret;
8587 
8588 	if (check_direct_IO(fs_info, iocb, iter, offset))
8589 		return 0;
8590 
8591 	inode_dio_begin(inode);
8592 	smp_mb__after_atomic();
8593 
8594 	/*
8595 	 * The generic stuff only does filemap_write_and_wait_range, which
8596 	 * isn't enough if we've written compressed pages to this area, so
8597 	 * we need to flush the dirty pages again to make absolutely sure
8598 	 * that any outstanding dirty pages are on disk.
8599 	 */
8600 	count = iov_iter_count(iter);
8601 	if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
8602 		     &BTRFS_I(inode)->runtime_flags))
8603 		filemap_fdatawrite_range(inode->i_mapping, offset,
8604 					 offset + count - 1);
8605 
8606 	if (iov_iter_rw(iter) == WRITE) {
8607 		/*
8608 		 * If the write DIO is beyond the EOF, we need update
8609 		 * the isize, but it is protected by i_mutex. So we can
8610 		 * not unlock the i_mutex at this case.
8611 		 */
8612 		if (offset + count <= inode->i_size) {
8613 			dio_data.overwrite = 1;
8614 			inode_unlock(inode);
8615 			relock = true;
8616 		}
8617 		ret = btrfs_delalloc_reserve_space(inode, offset, count);
8618 		if (ret)
8619 			goto out;
8620 		dio_data.outstanding_extents = count_max_extents(count);
8621 
8622 		/*
8623 		 * We need to know how many extents we reserved so that we can
8624 		 * do the accounting properly if we go over the number we
8625 		 * originally calculated.  Abuse current->journal_info for this.
8626 		 */
8627 		dio_data.reserve = round_up(count,
8628 					    fs_info->sectorsize);
8629 		dio_data.unsubmitted_oe_range_start = (u64)offset;
8630 		dio_data.unsubmitted_oe_range_end = (u64)offset;
8631 		current->journal_info = &dio_data;
8632 		down_read(&BTRFS_I(inode)->dio_sem);
8633 	} else if (test_bit(BTRFS_INODE_READDIO_NEED_LOCK,
8634 				     &BTRFS_I(inode)->runtime_flags)) {
8635 		inode_dio_end(inode);
8636 		flags = DIO_LOCKING | DIO_SKIP_HOLES;
8637 		wakeup = false;
8638 	}
8639 
8640 	ret = __blockdev_direct_IO(iocb, inode,
8641 				   fs_info->fs_devices->latest_bdev,
8642 				   iter, btrfs_get_blocks_direct, NULL,
8643 				   btrfs_submit_direct, flags);
8644 	if (iov_iter_rw(iter) == WRITE) {
8645 		up_read(&BTRFS_I(inode)->dio_sem);
8646 		current->journal_info = NULL;
8647 		if (ret < 0 && ret != -EIOCBQUEUED) {
8648 			if (dio_data.reserve)
8649 				btrfs_delalloc_release_space(inode, offset,
8650 							     dio_data.reserve);
8651 			/*
8652 			 * On error we might have left some ordered extents
8653 			 * without submitting corresponding bios for them, so
8654 			 * cleanup them up to avoid other tasks getting them
8655 			 * and waiting for them to complete forever.
8656 			 */
8657 			if (dio_data.unsubmitted_oe_range_start <
8658 			    dio_data.unsubmitted_oe_range_end)
8659 				btrfs_endio_direct_write_update_ordered(inode,
8660 					dio_data.unsubmitted_oe_range_start,
8661 					dio_data.unsubmitted_oe_range_end -
8662 					dio_data.unsubmitted_oe_range_start,
8663 					0);
8664 		} else if (ret >= 0 && (size_t)ret < count)
8665 			btrfs_delalloc_release_space(inode, offset,
8666 						     count - (size_t)ret);
8667 	}
8668 out:
8669 	if (wakeup)
8670 		inode_dio_end(inode);
8671 	if (relock)
8672 		inode_lock(inode);
8673 
8674 	return ret;
8675 }
8676 
8677 #define BTRFS_FIEMAP_FLAGS	(FIEMAP_FLAG_SYNC)
8678 
8679 static int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
8680 		__u64 start, __u64 len)
8681 {
8682 	int	ret;
8683 
8684 	ret = fiemap_check_flags(fieinfo, BTRFS_FIEMAP_FLAGS);
8685 	if (ret)
8686 		return ret;
8687 
8688 	return extent_fiemap(inode, fieinfo, start, len, btrfs_get_extent_fiemap);
8689 }
8690 
8691 int btrfs_readpage(struct file *file, struct page *page)
8692 {
8693 	struct extent_io_tree *tree;
8694 	tree = &BTRFS_I(page->mapping->host)->io_tree;
8695 	return extent_read_full_page(tree, page, btrfs_get_extent, 0);
8696 }
8697 
8698 static int btrfs_writepage(struct page *page, struct writeback_control *wbc)
8699 {
8700 	struct extent_io_tree *tree;
8701 	struct inode *inode = page->mapping->host;
8702 	int ret;
8703 
8704 	if (current->flags & PF_MEMALLOC) {
8705 		redirty_page_for_writepage(wbc, page);
8706 		unlock_page(page);
8707 		return 0;
8708 	}
8709 
8710 	/*
8711 	 * If we are under memory pressure we will call this directly from the
8712 	 * VM, we need to make sure we have the inode referenced for the ordered
8713 	 * extent.  If not just return like we didn't do anything.
8714 	 */
8715 	if (!igrab(inode)) {
8716 		redirty_page_for_writepage(wbc, page);
8717 		return AOP_WRITEPAGE_ACTIVATE;
8718 	}
8719 	tree = &BTRFS_I(page->mapping->host)->io_tree;
8720 	ret = extent_write_full_page(tree, page, btrfs_get_extent, wbc);
8721 	btrfs_add_delayed_iput(inode);
8722 	return ret;
8723 }
8724 
8725 static int btrfs_writepages(struct address_space *mapping,
8726 			    struct writeback_control *wbc)
8727 {
8728 	struct extent_io_tree *tree;
8729 
8730 	tree = &BTRFS_I(mapping->host)->io_tree;
8731 	return extent_writepages(tree, mapping, btrfs_get_extent, wbc);
8732 }
8733 
8734 static int
8735 btrfs_readpages(struct file *file, struct address_space *mapping,
8736 		struct list_head *pages, unsigned nr_pages)
8737 {
8738 	struct extent_io_tree *tree;
8739 	tree = &BTRFS_I(mapping->host)->io_tree;
8740 	return extent_readpages(tree, mapping, pages, nr_pages,
8741 				btrfs_get_extent);
8742 }
8743 static int __btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8744 {
8745 	struct extent_io_tree *tree;
8746 	struct extent_map_tree *map;
8747 	int ret;
8748 
8749 	tree = &BTRFS_I(page->mapping->host)->io_tree;
8750 	map = &BTRFS_I(page->mapping->host)->extent_tree;
8751 	ret = try_release_extent_mapping(map, tree, page, gfp_flags);
8752 	if (ret == 1) {
8753 		ClearPagePrivate(page);
8754 		set_page_private(page, 0);
8755 		put_page(page);
8756 	}
8757 	return ret;
8758 }
8759 
8760 static int btrfs_releasepage(struct page *page, gfp_t gfp_flags)
8761 {
8762 	if (PageWriteback(page) || PageDirty(page))
8763 		return 0;
8764 	return __btrfs_releasepage(page, gfp_flags);
8765 }
8766 
8767 static void btrfs_invalidatepage(struct page *page, unsigned int offset,
8768 				 unsigned int length)
8769 {
8770 	struct inode *inode = page->mapping->host;
8771 	struct extent_io_tree *tree;
8772 	struct btrfs_ordered_extent *ordered;
8773 	struct extent_state *cached_state = NULL;
8774 	u64 page_start = page_offset(page);
8775 	u64 page_end = page_start + PAGE_SIZE - 1;
8776 	u64 start;
8777 	u64 end;
8778 	int inode_evicting = inode->i_state & I_FREEING;
8779 
8780 	/*
8781 	 * we have the page locked, so new writeback can't start,
8782 	 * and the dirty bit won't be cleared while we are here.
8783 	 *
8784 	 * Wait for IO on this page so that we can safely clear
8785 	 * the PagePrivate2 bit and do ordered accounting
8786 	 */
8787 	wait_on_page_writeback(page);
8788 
8789 	tree = &BTRFS_I(inode)->io_tree;
8790 	if (offset) {
8791 		btrfs_releasepage(page, GFP_NOFS);
8792 		return;
8793 	}
8794 
8795 	if (!inode_evicting)
8796 		lock_extent_bits(tree, page_start, page_end, &cached_state);
8797 again:
8798 	start = page_start;
8799 	ordered = btrfs_lookup_ordered_range(inode, start,
8800 					page_end - start + 1);
8801 	if (ordered) {
8802 		end = min(page_end, ordered->file_offset + ordered->len - 1);
8803 		/*
8804 		 * IO on this page will never be started, so we need
8805 		 * to account for any ordered extents now
8806 		 */
8807 		if (!inode_evicting)
8808 			clear_extent_bit(tree, start, end,
8809 					 EXTENT_DIRTY | EXTENT_DELALLOC |
8810 					 EXTENT_LOCKED | EXTENT_DO_ACCOUNTING |
8811 					 EXTENT_DEFRAG, 1, 0, &cached_state,
8812 					 GFP_NOFS);
8813 		/*
8814 		 * whoever cleared the private bit is responsible
8815 		 * for the finish_ordered_io
8816 		 */
8817 		if (TestClearPagePrivate2(page)) {
8818 			struct btrfs_ordered_inode_tree *tree;
8819 			u64 new_len;
8820 
8821 			tree = &BTRFS_I(inode)->ordered_tree;
8822 
8823 			spin_lock_irq(&tree->lock);
8824 			set_bit(BTRFS_ORDERED_TRUNCATED, &ordered->flags);
8825 			new_len = start - ordered->file_offset;
8826 			if (new_len < ordered->truncated_len)
8827 				ordered->truncated_len = new_len;
8828 			spin_unlock_irq(&tree->lock);
8829 
8830 			if (btrfs_dec_test_ordered_pending(inode, &ordered,
8831 							   start,
8832 							   end - start + 1, 1))
8833 				btrfs_finish_ordered_io(ordered);
8834 		}
8835 		btrfs_put_ordered_extent(ordered);
8836 		if (!inode_evicting) {
8837 			cached_state = NULL;
8838 			lock_extent_bits(tree, start, end,
8839 					 &cached_state);
8840 		}
8841 
8842 		start = end + 1;
8843 		if (start < page_end)
8844 			goto again;
8845 	}
8846 
8847 	/*
8848 	 * Qgroup reserved space handler
8849 	 * Page here will be either
8850 	 * 1) Already written to disk
8851 	 *    In this case, its reserved space is released from data rsv map
8852 	 *    and will be freed by delayed_ref handler finally.
8853 	 *    So even we call qgroup_free_data(), it won't decrease reserved
8854 	 *    space.
8855 	 * 2) Not written to disk
8856 	 *    This means the reserved space should be freed here. However,
8857 	 *    if a truncate invalidates the page (by clearing PageDirty)
8858 	 *    and the page is accounted for while allocating extent
8859 	 *    in btrfs_check_data_free_space() we let delayed_ref to
8860 	 *    free the entire extent.
8861 	 */
8862 	if (PageDirty(page))
8863 		btrfs_qgroup_free_data(inode, page_start, PAGE_SIZE);
8864 	if (!inode_evicting) {
8865 		clear_extent_bit(tree, page_start, page_end,
8866 				 EXTENT_LOCKED | EXTENT_DIRTY |
8867 				 EXTENT_DELALLOC | EXTENT_DO_ACCOUNTING |
8868 				 EXTENT_DEFRAG, 1, 1,
8869 				 &cached_state, GFP_NOFS);
8870 
8871 		__btrfs_releasepage(page, GFP_NOFS);
8872 	}
8873 
8874 	ClearPageChecked(page);
8875 	if (PagePrivate(page)) {
8876 		ClearPagePrivate(page);
8877 		set_page_private(page, 0);
8878 		put_page(page);
8879 	}
8880 }
8881 
8882 /*
8883  * btrfs_page_mkwrite() is not allowed to change the file size as it gets
8884  * called from a page fault handler when a page is first dirtied. Hence we must
8885  * be careful to check for EOF conditions here. We set the page up correctly
8886  * for a written page which means we get ENOSPC checking when writing into
8887  * holes and correct delalloc and unwritten extent mapping on filesystems that
8888  * support these features.
8889  *
8890  * We are not allowed to take the i_mutex here so we have to play games to
8891  * protect against truncate races as the page could now be beyond EOF.  Because
8892  * vmtruncate() writes the inode size before removing pages, once we have the
8893  * page lock we can determine safely if the page is beyond EOF. If it is not
8894  * beyond EOF, then the page is guaranteed safe against truncation until we
8895  * unlock the page.
8896  */
8897 int btrfs_page_mkwrite(struct vm_fault *vmf)
8898 {
8899 	struct page *page = vmf->page;
8900 	struct inode *inode = file_inode(vmf->vma->vm_file);
8901 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
8902 	struct extent_io_tree *io_tree = &BTRFS_I(inode)->io_tree;
8903 	struct btrfs_ordered_extent *ordered;
8904 	struct extent_state *cached_state = NULL;
8905 	char *kaddr;
8906 	unsigned long zero_start;
8907 	loff_t size;
8908 	int ret;
8909 	int reserved = 0;
8910 	u64 reserved_space;
8911 	u64 page_start;
8912 	u64 page_end;
8913 	u64 end;
8914 
8915 	reserved_space = PAGE_SIZE;
8916 
8917 	sb_start_pagefault(inode->i_sb);
8918 	page_start = page_offset(page);
8919 	page_end = page_start + PAGE_SIZE - 1;
8920 	end = page_end;
8921 
8922 	/*
8923 	 * Reserving delalloc space after obtaining the page lock can lead to
8924 	 * deadlock. For example, if a dirty page is locked by this function
8925 	 * and the call to btrfs_delalloc_reserve_space() ends up triggering
8926 	 * dirty page write out, then the btrfs_writepage() function could
8927 	 * end up waiting indefinitely to get a lock on the page currently
8928 	 * being processed by btrfs_page_mkwrite() function.
8929 	 */
8930 	ret = btrfs_delalloc_reserve_space(inode, page_start,
8931 					   reserved_space);
8932 	if (!ret) {
8933 		ret = file_update_time(vmf->vma->vm_file);
8934 		reserved = 1;
8935 	}
8936 	if (ret) {
8937 		if (ret == -ENOMEM)
8938 			ret = VM_FAULT_OOM;
8939 		else /* -ENOSPC, -EIO, etc */
8940 			ret = VM_FAULT_SIGBUS;
8941 		if (reserved)
8942 			goto out;
8943 		goto out_noreserve;
8944 	}
8945 
8946 	ret = VM_FAULT_NOPAGE; /* make the VM retry the fault */
8947 again:
8948 	lock_page(page);
8949 	size = i_size_read(inode);
8950 
8951 	if ((page->mapping != inode->i_mapping) ||
8952 	    (page_start >= size)) {
8953 		/* page got truncated out from underneath us */
8954 		goto out_unlock;
8955 	}
8956 	wait_on_page_writeback(page);
8957 
8958 	lock_extent_bits(io_tree, page_start, page_end, &cached_state);
8959 	set_page_extent_mapped(page);
8960 
8961 	/*
8962 	 * we can't set the delalloc bits if there are pending ordered
8963 	 * extents.  Drop our locks and wait for them to finish
8964 	 */
8965 	ordered = btrfs_lookup_ordered_range(inode, page_start, PAGE_SIZE);
8966 	if (ordered) {
8967 		unlock_extent_cached(io_tree, page_start, page_end,
8968 				     &cached_state, GFP_NOFS);
8969 		unlock_page(page);
8970 		btrfs_start_ordered_extent(inode, ordered, 1);
8971 		btrfs_put_ordered_extent(ordered);
8972 		goto again;
8973 	}
8974 
8975 	if (page->index == ((size - 1) >> PAGE_SHIFT)) {
8976 		reserved_space = round_up(size - page_start,
8977 					  fs_info->sectorsize);
8978 		if (reserved_space < PAGE_SIZE) {
8979 			end = page_start + reserved_space - 1;
8980 			spin_lock(&BTRFS_I(inode)->lock);
8981 			BTRFS_I(inode)->outstanding_extents++;
8982 			spin_unlock(&BTRFS_I(inode)->lock);
8983 			btrfs_delalloc_release_space(inode, page_start,
8984 						PAGE_SIZE - reserved_space);
8985 		}
8986 	}
8987 
8988 	/*
8989 	 * page_mkwrite gets called when the page is firstly dirtied after it's
8990 	 * faulted in, but write(2) could also dirty a page and set delalloc
8991 	 * bits, thus in this case for space account reason, we still need to
8992 	 * clear any delalloc bits within this page range since we have to
8993 	 * reserve data&meta space before lock_page() (see above comments).
8994 	 */
8995 	clear_extent_bit(&BTRFS_I(inode)->io_tree, page_start, end,
8996 			  EXTENT_DIRTY | EXTENT_DELALLOC |
8997 			  EXTENT_DO_ACCOUNTING | EXTENT_DEFRAG,
8998 			  0, 0, &cached_state, GFP_NOFS);
8999 
9000 	ret = btrfs_set_extent_delalloc(inode, page_start, end,
9001 					&cached_state, 0);
9002 	if (ret) {
9003 		unlock_extent_cached(io_tree, page_start, page_end,
9004 				     &cached_state, GFP_NOFS);
9005 		ret = VM_FAULT_SIGBUS;
9006 		goto out_unlock;
9007 	}
9008 	ret = 0;
9009 
9010 	/* page is wholly or partially inside EOF */
9011 	if (page_start + PAGE_SIZE > size)
9012 		zero_start = size & ~PAGE_MASK;
9013 	else
9014 		zero_start = PAGE_SIZE;
9015 
9016 	if (zero_start != PAGE_SIZE) {
9017 		kaddr = kmap(page);
9018 		memset(kaddr + zero_start, 0, PAGE_SIZE - zero_start);
9019 		flush_dcache_page(page);
9020 		kunmap(page);
9021 	}
9022 	ClearPageChecked(page);
9023 	set_page_dirty(page);
9024 	SetPageUptodate(page);
9025 
9026 	BTRFS_I(inode)->last_trans = fs_info->generation;
9027 	BTRFS_I(inode)->last_sub_trans = BTRFS_I(inode)->root->log_transid;
9028 	BTRFS_I(inode)->last_log_commit = BTRFS_I(inode)->root->last_log_commit;
9029 
9030 	unlock_extent_cached(io_tree, page_start, page_end, &cached_state, GFP_NOFS);
9031 
9032 out_unlock:
9033 	if (!ret) {
9034 		sb_end_pagefault(inode->i_sb);
9035 		return VM_FAULT_LOCKED;
9036 	}
9037 	unlock_page(page);
9038 out:
9039 	btrfs_delalloc_release_space(inode, page_start, reserved_space);
9040 out_noreserve:
9041 	sb_end_pagefault(inode->i_sb);
9042 	return ret;
9043 }
9044 
9045 static int btrfs_truncate(struct inode *inode)
9046 {
9047 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9048 	struct btrfs_root *root = BTRFS_I(inode)->root;
9049 	struct btrfs_block_rsv *rsv;
9050 	int ret = 0;
9051 	int err = 0;
9052 	struct btrfs_trans_handle *trans;
9053 	u64 mask = fs_info->sectorsize - 1;
9054 	u64 min_size = btrfs_calc_trunc_metadata_size(fs_info, 1);
9055 
9056 	ret = btrfs_wait_ordered_range(inode, inode->i_size & (~mask),
9057 				       (u64)-1);
9058 	if (ret)
9059 		return ret;
9060 
9061 	/*
9062 	 * Yes ladies and gentlemen, this is indeed ugly.  The fact is we have
9063 	 * 3 things going on here
9064 	 *
9065 	 * 1) We need to reserve space for our orphan item and the space to
9066 	 * delete our orphan item.  Lord knows we don't want to have a dangling
9067 	 * orphan item because we didn't reserve space to remove it.
9068 	 *
9069 	 * 2) We need to reserve space to update our inode.
9070 	 *
9071 	 * 3) We need to have something to cache all the space that is going to
9072 	 * be free'd up by the truncate operation, but also have some slack
9073 	 * space reserved in case it uses space during the truncate (thank you
9074 	 * very much snapshotting).
9075 	 *
9076 	 * And we need these to all be separate.  The fact is we can use a lot of
9077 	 * space doing the truncate, and we have no earthly idea how much space
9078 	 * we will use, so we need the truncate reservation to be separate so it
9079 	 * doesn't end up using space reserved for updating the inode or
9080 	 * removing the orphan item.  We also need to be able to stop the
9081 	 * transaction and start a new one, which means we need to be able to
9082 	 * update the inode several times, and we have no idea of knowing how
9083 	 * many times that will be, so we can't just reserve 1 item for the
9084 	 * entirety of the operation, so that has to be done separately as well.
9085 	 * Then there is the orphan item, which does indeed need to be held on
9086 	 * to for the whole operation, and we need nobody to touch this reserved
9087 	 * space except the orphan code.
9088 	 *
9089 	 * So that leaves us with
9090 	 *
9091 	 * 1) root->orphan_block_rsv - for the orphan deletion.
9092 	 * 2) rsv - for the truncate reservation, which we will steal from the
9093 	 * transaction reservation.
9094 	 * 3) fs_info->trans_block_rsv - this will have 1 items worth left for
9095 	 * updating the inode.
9096 	 */
9097 	rsv = btrfs_alloc_block_rsv(fs_info, BTRFS_BLOCK_RSV_TEMP);
9098 	if (!rsv)
9099 		return -ENOMEM;
9100 	rsv->size = min_size;
9101 	rsv->failfast = 1;
9102 
9103 	/*
9104 	 * 1 for the truncate slack space
9105 	 * 1 for updating the inode.
9106 	 */
9107 	trans = btrfs_start_transaction(root, 2);
9108 	if (IS_ERR(trans)) {
9109 		err = PTR_ERR(trans);
9110 		goto out;
9111 	}
9112 
9113 	/* Migrate the slack space for the truncate to our reserve */
9114 	ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv, rsv,
9115 				      min_size, 0);
9116 	BUG_ON(ret);
9117 
9118 	/*
9119 	 * So if we truncate and then write and fsync we normally would just
9120 	 * write the extents that changed, which is a problem if we need to
9121 	 * first truncate that entire inode.  So set this flag so we write out
9122 	 * all of the extents in the inode to the sync log so we're completely
9123 	 * safe.
9124 	 */
9125 	set_bit(BTRFS_INODE_NEEDS_FULL_SYNC, &BTRFS_I(inode)->runtime_flags);
9126 	trans->block_rsv = rsv;
9127 
9128 	while (1) {
9129 		ret = btrfs_truncate_inode_items(trans, root, inode,
9130 						 inode->i_size,
9131 						 BTRFS_EXTENT_DATA_KEY);
9132 		if (ret != -ENOSPC && ret != -EAGAIN) {
9133 			err = ret;
9134 			break;
9135 		}
9136 
9137 		trans->block_rsv = &fs_info->trans_block_rsv;
9138 		ret = btrfs_update_inode(trans, root, inode);
9139 		if (ret) {
9140 			err = ret;
9141 			break;
9142 		}
9143 
9144 		btrfs_end_transaction(trans);
9145 		btrfs_btree_balance_dirty(fs_info);
9146 
9147 		trans = btrfs_start_transaction(root, 2);
9148 		if (IS_ERR(trans)) {
9149 			ret = err = PTR_ERR(trans);
9150 			trans = NULL;
9151 			break;
9152 		}
9153 
9154 		btrfs_block_rsv_release(fs_info, rsv, -1);
9155 		ret = btrfs_block_rsv_migrate(&fs_info->trans_block_rsv,
9156 					      rsv, min_size, 0);
9157 		BUG_ON(ret);	/* shouldn't happen */
9158 		trans->block_rsv = rsv;
9159 	}
9160 
9161 	if (ret == 0 && inode->i_nlink > 0) {
9162 		trans->block_rsv = root->orphan_block_rsv;
9163 		ret = btrfs_orphan_del(trans, inode);
9164 		if (ret)
9165 			err = ret;
9166 	}
9167 
9168 	if (trans) {
9169 		trans->block_rsv = &fs_info->trans_block_rsv;
9170 		ret = btrfs_update_inode(trans, root, inode);
9171 		if (ret && !err)
9172 			err = ret;
9173 
9174 		ret = btrfs_end_transaction(trans);
9175 		btrfs_btree_balance_dirty(fs_info);
9176 	}
9177 out:
9178 	btrfs_free_block_rsv(fs_info, rsv);
9179 
9180 	if (ret && !err)
9181 		err = ret;
9182 
9183 	return err;
9184 }
9185 
9186 /*
9187  * create a new subvolume directory/inode (helper for the ioctl).
9188  */
9189 int btrfs_create_subvol_root(struct btrfs_trans_handle *trans,
9190 			     struct btrfs_root *new_root,
9191 			     struct btrfs_root *parent_root,
9192 			     u64 new_dirid)
9193 {
9194 	struct inode *inode;
9195 	int err;
9196 	u64 index = 0;
9197 
9198 	inode = btrfs_new_inode(trans, new_root, NULL, "..", 2,
9199 				new_dirid, new_dirid,
9200 				S_IFDIR | (~current_umask() & S_IRWXUGO),
9201 				&index);
9202 	if (IS_ERR(inode))
9203 		return PTR_ERR(inode);
9204 	inode->i_op = &btrfs_dir_inode_operations;
9205 	inode->i_fop = &btrfs_dir_file_operations;
9206 
9207 	set_nlink(inode, 1);
9208 	btrfs_i_size_write(inode, 0);
9209 	unlock_new_inode(inode);
9210 
9211 	err = btrfs_subvol_inherit_props(trans, new_root, parent_root);
9212 	if (err)
9213 		btrfs_err(new_root->fs_info,
9214 			  "error inheriting subvolume %llu properties: %d",
9215 			  new_root->root_key.objectid, err);
9216 
9217 	err = btrfs_update_inode(trans, new_root, inode);
9218 
9219 	iput(inode);
9220 	return err;
9221 }
9222 
9223 struct inode *btrfs_alloc_inode(struct super_block *sb)
9224 {
9225 	struct btrfs_inode *ei;
9226 	struct inode *inode;
9227 
9228 	ei = kmem_cache_alloc(btrfs_inode_cachep, GFP_NOFS);
9229 	if (!ei)
9230 		return NULL;
9231 
9232 	ei->root = NULL;
9233 	ei->generation = 0;
9234 	ei->last_trans = 0;
9235 	ei->last_sub_trans = 0;
9236 	ei->logged_trans = 0;
9237 	ei->delalloc_bytes = 0;
9238 	ei->defrag_bytes = 0;
9239 	ei->disk_i_size = 0;
9240 	ei->flags = 0;
9241 	ei->csum_bytes = 0;
9242 	ei->index_cnt = (u64)-1;
9243 	ei->dir_index = 0;
9244 	ei->last_unlink_trans = 0;
9245 	ei->last_log_commit = 0;
9246 	ei->delayed_iput_count = 0;
9247 
9248 	spin_lock_init(&ei->lock);
9249 	ei->outstanding_extents = 0;
9250 	ei->reserved_extents = 0;
9251 
9252 	ei->runtime_flags = 0;
9253 	ei->force_compress = BTRFS_COMPRESS_NONE;
9254 
9255 	ei->delayed_node = NULL;
9256 
9257 	ei->i_otime.tv_sec = 0;
9258 	ei->i_otime.tv_nsec = 0;
9259 
9260 	inode = &ei->vfs_inode;
9261 	extent_map_tree_init(&ei->extent_tree);
9262 	extent_io_tree_init(&ei->io_tree, &inode->i_data);
9263 	extent_io_tree_init(&ei->io_failure_tree, &inode->i_data);
9264 	ei->io_tree.track_uptodate = 1;
9265 	ei->io_failure_tree.track_uptodate = 1;
9266 	atomic_set(&ei->sync_writers, 0);
9267 	mutex_init(&ei->log_mutex);
9268 	mutex_init(&ei->delalloc_mutex);
9269 	btrfs_ordered_inode_tree_init(&ei->ordered_tree);
9270 	INIT_LIST_HEAD(&ei->delalloc_inodes);
9271 	INIT_LIST_HEAD(&ei->delayed_iput);
9272 	RB_CLEAR_NODE(&ei->rb_node);
9273 	init_rwsem(&ei->dio_sem);
9274 
9275 	return inode;
9276 }
9277 
9278 #ifdef CONFIG_BTRFS_FS_RUN_SANITY_TESTS
9279 void btrfs_test_destroy_inode(struct inode *inode)
9280 {
9281 	btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9282 	kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9283 }
9284 #endif
9285 
9286 static void btrfs_i_callback(struct rcu_head *head)
9287 {
9288 	struct inode *inode = container_of(head, struct inode, i_rcu);
9289 	kmem_cache_free(btrfs_inode_cachep, BTRFS_I(inode));
9290 }
9291 
9292 void btrfs_destroy_inode(struct inode *inode)
9293 {
9294 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
9295 	struct btrfs_ordered_extent *ordered;
9296 	struct btrfs_root *root = BTRFS_I(inode)->root;
9297 
9298 	WARN_ON(!hlist_empty(&inode->i_dentry));
9299 	WARN_ON(inode->i_data.nrpages);
9300 	WARN_ON(BTRFS_I(inode)->outstanding_extents);
9301 	WARN_ON(BTRFS_I(inode)->reserved_extents);
9302 	WARN_ON(BTRFS_I(inode)->delalloc_bytes);
9303 	WARN_ON(BTRFS_I(inode)->csum_bytes);
9304 	WARN_ON(BTRFS_I(inode)->defrag_bytes);
9305 
9306 	/*
9307 	 * This can happen where we create an inode, but somebody else also
9308 	 * created the same inode and we need to destroy the one we already
9309 	 * created.
9310 	 */
9311 	if (!root)
9312 		goto free;
9313 
9314 	if (test_bit(BTRFS_INODE_HAS_ORPHAN_ITEM,
9315 		     &BTRFS_I(inode)->runtime_flags)) {
9316 		btrfs_info(fs_info, "inode %llu still on the orphan list",
9317 			   btrfs_ino(BTRFS_I(inode)));
9318 		atomic_dec(&root->orphan_inodes);
9319 	}
9320 
9321 	while (1) {
9322 		ordered = btrfs_lookup_first_ordered_extent(inode, (u64)-1);
9323 		if (!ordered)
9324 			break;
9325 		else {
9326 			btrfs_err(fs_info,
9327 				  "found ordered extent %llu %llu on inode cleanup",
9328 				  ordered->file_offset, ordered->len);
9329 			btrfs_remove_ordered_extent(inode, ordered);
9330 			btrfs_put_ordered_extent(ordered);
9331 			btrfs_put_ordered_extent(ordered);
9332 		}
9333 	}
9334 	btrfs_qgroup_check_reserved_leak(inode);
9335 	inode_tree_del(inode);
9336 	btrfs_drop_extent_cache(inode, 0, (u64)-1, 0);
9337 free:
9338 	call_rcu(&inode->i_rcu, btrfs_i_callback);
9339 }
9340 
9341 int btrfs_drop_inode(struct inode *inode)
9342 {
9343 	struct btrfs_root *root = BTRFS_I(inode)->root;
9344 
9345 	if (root == NULL)
9346 		return 1;
9347 
9348 	/* the snap/subvol tree is on deleting */
9349 	if (btrfs_root_refs(&root->root_item) == 0)
9350 		return 1;
9351 	else
9352 		return generic_drop_inode(inode);
9353 }
9354 
9355 static void init_once(void *foo)
9356 {
9357 	struct btrfs_inode *ei = (struct btrfs_inode *) foo;
9358 
9359 	inode_init_once(&ei->vfs_inode);
9360 }
9361 
9362 void btrfs_destroy_cachep(void)
9363 {
9364 	/*
9365 	 * Make sure all delayed rcu free inodes are flushed before we
9366 	 * destroy cache.
9367 	 */
9368 	rcu_barrier();
9369 	kmem_cache_destroy(btrfs_inode_cachep);
9370 	kmem_cache_destroy(btrfs_trans_handle_cachep);
9371 	kmem_cache_destroy(btrfs_transaction_cachep);
9372 	kmem_cache_destroy(btrfs_path_cachep);
9373 	kmem_cache_destroy(btrfs_free_space_cachep);
9374 }
9375 
9376 int btrfs_init_cachep(void)
9377 {
9378 	btrfs_inode_cachep = kmem_cache_create("btrfs_inode",
9379 			sizeof(struct btrfs_inode), 0,
9380 			SLAB_RECLAIM_ACCOUNT | SLAB_MEM_SPREAD | SLAB_ACCOUNT,
9381 			init_once);
9382 	if (!btrfs_inode_cachep)
9383 		goto fail;
9384 
9385 	btrfs_trans_handle_cachep = kmem_cache_create("btrfs_trans_handle",
9386 			sizeof(struct btrfs_trans_handle), 0,
9387 			SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
9388 	if (!btrfs_trans_handle_cachep)
9389 		goto fail;
9390 
9391 	btrfs_transaction_cachep = kmem_cache_create("btrfs_transaction",
9392 			sizeof(struct btrfs_transaction), 0,
9393 			SLAB_TEMPORARY | SLAB_MEM_SPREAD, NULL);
9394 	if (!btrfs_transaction_cachep)
9395 		goto fail;
9396 
9397 	btrfs_path_cachep = kmem_cache_create("btrfs_path",
9398 			sizeof(struct btrfs_path), 0,
9399 			SLAB_MEM_SPREAD, NULL);
9400 	if (!btrfs_path_cachep)
9401 		goto fail;
9402 
9403 	btrfs_free_space_cachep = kmem_cache_create("btrfs_free_space",
9404 			sizeof(struct btrfs_free_space), 0,
9405 			SLAB_MEM_SPREAD, NULL);
9406 	if (!btrfs_free_space_cachep)
9407 		goto fail;
9408 
9409 	return 0;
9410 fail:
9411 	btrfs_destroy_cachep();
9412 	return -ENOMEM;
9413 }
9414 
9415 static int btrfs_getattr(struct vfsmount *mnt,
9416 			 struct dentry *dentry, struct kstat *stat)
9417 {
9418 	u64 delalloc_bytes;
9419 	struct inode *inode = d_inode(dentry);
9420 	u32 blocksize = inode->i_sb->s_blocksize;
9421 
9422 	generic_fillattr(inode, stat);
9423 	stat->dev = BTRFS_I(inode)->root->anon_dev;
9424 
9425 	spin_lock(&BTRFS_I(inode)->lock);
9426 	delalloc_bytes = BTRFS_I(inode)->delalloc_bytes;
9427 	spin_unlock(&BTRFS_I(inode)->lock);
9428 	stat->blocks = (ALIGN(inode_get_bytes(inode), blocksize) +
9429 			ALIGN(delalloc_bytes, blocksize)) >> 9;
9430 	return 0;
9431 }
9432 
9433 static int btrfs_rename_exchange(struct inode *old_dir,
9434 			      struct dentry *old_dentry,
9435 			      struct inode *new_dir,
9436 			      struct dentry *new_dentry)
9437 {
9438 	struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9439 	struct btrfs_trans_handle *trans;
9440 	struct btrfs_root *root = BTRFS_I(old_dir)->root;
9441 	struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9442 	struct inode *new_inode = new_dentry->d_inode;
9443 	struct inode *old_inode = old_dentry->d_inode;
9444 	struct timespec ctime = current_time(old_inode);
9445 	struct dentry *parent;
9446 	u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9447 	u64 new_ino = btrfs_ino(BTRFS_I(new_inode));
9448 	u64 old_idx = 0;
9449 	u64 new_idx = 0;
9450 	u64 root_objectid;
9451 	int ret;
9452 	bool root_log_pinned = false;
9453 	bool dest_log_pinned = false;
9454 
9455 	/* we only allow rename subvolume link between subvolumes */
9456 	if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9457 		return -EXDEV;
9458 
9459 	/* close the race window with snapshot create/destroy ioctl */
9460 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9461 		down_read(&fs_info->subvol_sem);
9462 	if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9463 		down_read(&fs_info->subvol_sem);
9464 
9465 	/*
9466 	 * We want to reserve the absolute worst case amount of items.  So if
9467 	 * both inodes are subvols and we need to unlink them then that would
9468 	 * require 4 item modifications, but if they are both normal inodes it
9469 	 * would require 5 item modifications, so we'll assume their normal
9470 	 * inodes.  So 5 * 2 is 10, plus 2 for the new links, so 12 total items
9471 	 * should cover the worst case number of items we'll modify.
9472 	 */
9473 	trans = btrfs_start_transaction(root, 12);
9474 	if (IS_ERR(trans)) {
9475 		ret = PTR_ERR(trans);
9476 		goto out_notrans;
9477 	}
9478 
9479 	/*
9480 	 * We need to find a free sequence number both in the source and
9481 	 * in the destination directory for the exchange.
9482 	 */
9483 	ret = btrfs_set_inode_index(new_dir, &old_idx);
9484 	if (ret)
9485 		goto out_fail;
9486 	ret = btrfs_set_inode_index(old_dir, &new_idx);
9487 	if (ret)
9488 		goto out_fail;
9489 
9490 	BTRFS_I(old_inode)->dir_index = 0ULL;
9491 	BTRFS_I(new_inode)->dir_index = 0ULL;
9492 
9493 	/* Reference for the source. */
9494 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9495 		/* force full log commit if subvolume involved. */
9496 		btrfs_set_log_full_commit(fs_info, trans);
9497 	} else {
9498 		btrfs_pin_log_trans(root);
9499 		root_log_pinned = true;
9500 		ret = btrfs_insert_inode_ref(trans, dest,
9501 					     new_dentry->d_name.name,
9502 					     new_dentry->d_name.len,
9503 					     old_ino,
9504 					     btrfs_ino(BTRFS_I(new_dir)),
9505 					     old_idx);
9506 		if (ret)
9507 			goto out_fail;
9508 	}
9509 
9510 	/* And now for the dest. */
9511 	if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9512 		/* force full log commit if subvolume involved. */
9513 		btrfs_set_log_full_commit(fs_info, trans);
9514 	} else {
9515 		btrfs_pin_log_trans(dest);
9516 		dest_log_pinned = true;
9517 		ret = btrfs_insert_inode_ref(trans, root,
9518 					     old_dentry->d_name.name,
9519 					     old_dentry->d_name.len,
9520 					     new_ino,
9521 					     btrfs_ino(BTRFS_I(old_dir)),
9522 					     new_idx);
9523 		if (ret)
9524 			goto out_fail;
9525 	}
9526 
9527 	/* Update inode version and ctime/mtime. */
9528 	inode_inc_iversion(old_dir);
9529 	inode_inc_iversion(new_dir);
9530 	inode_inc_iversion(old_inode);
9531 	inode_inc_iversion(new_inode);
9532 	old_dir->i_ctime = old_dir->i_mtime = ctime;
9533 	new_dir->i_ctime = new_dir->i_mtime = ctime;
9534 	old_inode->i_ctime = ctime;
9535 	new_inode->i_ctime = ctime;
9536 
9537 	if (old_dentry->d_parent != new_dentry->d_parent) {
9538 		btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9539 				BTRFS_I(old_inode), 1);
9540 		btrfs_record_unlink_dir(trans, BTRFS_I(new_dir),
9541 				BTRFS_I(new_inode), 1);
9542 	}
9543 
9544 	/* src is a subvolume */
9545 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID) {
9546 		root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9547 		ret = btrfs_unlink_subvol(trans, root, old_dir,
9548 					  root_objectid,
9549 					  old_dentry->d_name.name,
9550 					  old_dentry->d_name.len);
9551 	} else { /* src is an inode */
9552 		ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9553 					   BTRFS_I(old_dentry->d_inode),
9554 					   old_dentry->d_name.name,
9555 					   old_dentry->d_name.len);
9556 		if (!ret)
9557 			ret = btrfs_update_inode(trans, root, old_inode);
9558 	}
9559 	if (ret) {
9560 		btrfs_abort_transaction(trans, ret);
9561 		goto out_fail;
9562 	}
9563 
9564 	/* dest is a subvolume */
9565 	if (new_ino == BTRFS_FIRST_FREE_OBJECTID) {
9566 		root_objectid = BTRFS_I(new_inode)->root->root_key.objectid;
9567 		ret = btrfs_unlink_subvol(trans, dest, new_dir,
9568 					  root_objectid,
9569 					  new_dentry->d_name.name,
9570 					  new_dentry->d_name.len);
9571 	} else { /* dest is an inode */
9572 		ret = __btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9573 					   BTRFS_I(new_dentry->d_inode),
9574 					   new_dentry->d_name.name,
9575 					   new_dentry->d_name.len);
9576 		if (!ret)
9577 			ret = btrfs_update_inode(trans, dest, new_inode);
9578 	}
9579 	if (ret) {
9580 		btrfs_abort_transaction(trans, ret);
9581 		goto out_fail;
9582 	}
9583 
9584 	ret = btrfs_add_link(trans, new_dir, old_inode,
9585 			     new_dentry->d_name.name,
9586 			     new_dentry->d_name.len, 0, old_idx);
9587 	if (ret) {
9588 		btrfs_abort_transaction(trans, ret);
9589 		goto out_fail;
9590 	}
9591 
9592 	ret = btrfs_add_link(trans, old_dir, new_inode,
9593 			     old_dentry->d_name.name,
9594 			     old_dentry->d_name.len, 0, new_idx);
9595 	if (ret) {
9596 		btrfs_abort_transaction(trans, ret);
9597 		goto out_fail;
9598 	}
9599 
9600 	if (old_inode->i_nlink == 1)
9601 		BTRFS_I(old_inode)->dir_index = old_idx;
9602 	if (new_inode->i_nlink == 1)
9603 		BTRFS_I(new_inode)->dir_index = new_idx;
9604 
9605 	if (root_log_pinned) {
9606 		parent = new_dentry->d_parent;
9607 		btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9608 				parent);
9609 		btrfs_end_log_trans(root);
9610 		root_log_pinned = false;
9611 	}
9612 	if (dest_log_pinned) {
9613 		parent = old_dentry->d_parent;
9614 		btrfs_log_new_name(trans, BTRFS_I(new_inode), BTRFS_I(new_dir),
9615 				parent);
9616 		btrfs_end_log_trans(dest);
9617 		dest_log_pinned = false;
9618 	}
9619 out_fail:
9620 	/*
9621 	 * If we have pinned a log and an error happened, we unpin tasks
9622 	 * trying to sync the log and force them to fallback to a transaction
9623 	 * commit if the log currently contains any of the inodes involved in
9624 	 * this rename operation (to ensure we do not persist a log with an
9625 	 * inconsistent state for any of these inodes or leading to any
9626 	 * inconsistencies when replayed). If the transaction was aborted, the
9627 	 * abortion reason is propagated to userspace when attempting to commit
9628 	 * the transaction. If the log does not contain any of these inodes, we
9629 	 * allow the tasks to sync it.
9630 	 */
9631 	if (ret && (root_log_pinned || dest_log_pinned)) {
9632 		if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9633 		    btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9634 		    btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
9635 		    (new_inode &&
9636 		     btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
9637 			btrfs_set_log_full_commit(fs_info, trans);
9638 
9639 		if (root_log_pinned) {
9640 			btrfs_end_log_trans(root);
9641 			root_log_pinned = false;
9642 		}
9643 		if (dest_log_pinned) {
9644 			btrfs_end_log_trans(dest);
9645 			dest_log_pinned = false;
9646 		}
9647 	}
9648 	ret = btrfs_end_transaction(trans);
9649 out_notrans:
9650 	if (new_ino == BTRFS_FIRST_FREE_OBJECTID)
9651 		up_read(&fs_info->subvol_sem);
9652 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9653 		up_read(&fs_info->subvol_sem);
9654 
9655 	return ret;
9656 }
9657 
9658 static int btrfs_whiteout_for_rename(struct btrfs_trans_handle *trans,
9659 				     struct btrfs_root *root,
9660 				     struct inode *dir,
9661 				     struct dentry *dentry)
9662 {
9663 	int ret;
9664 	struct inode *inode;
9665 	u64 objectid;
9666 	u64 index;
9667 
9668 	ret = btrfs_find_free_ino(root, &objectid);
9669 	if (ret)
9670 		return ret;
9671 
9672 	inode = btrfs_new_inode(trans, root, dir,
9673 				dentry->d_name.name,
9674 				dentry->d_name.len,
9675 				btrfs_ino(BTRFS_I(dir)),
9676 				objectid,
9677 				S_IFCHR | WHITEOUT_MODE,
9678 				&index);
9679 
9680 	if (IS_ERR(inode)) {
9681 		ret = PTR_ERR(inode);
9682 		return ret;
9683 	}
9684 
9685 	inode->i_op = &btrfs_special_inode_operations;
9686 	init_special_inode(inode, inode->i_mode,
9687 		WHITEOUT_DEV);
9688 
9689 	ret = btrfs_init_inode_security(trans, inode, dir,
9690 				&dentry->d_name);
9691 	if (ret)
9692 		goto out;
9693 
9694 	ret = btrfs_add_nondir(trans, dir, dentry,
9695 				inode, 0, index);
9696 	if (ret)
9697 		goto out;
9698 
9699 	ret = btrfs_update_inode(trans, root, inode);
9700 out:
9701 	unlock_new_inode(inode);
9702 	if (ret)
9703 		inode_dec_link_count(inode);
9704 	iput(inode);
9705 
9706 	return ret;
9707 }
9708 
9709 static int btrfs_rename(struct inode *old_dir, struct dentry *old_dentry,
9710 			   struct inode *new_dir, struct dentry *new_dentry,
9711 			   unsigned int flags)
9712 {
9713 	struct btrfs_fs_info *fs_info = btrfs_sb(old_dir->i_sb);
9714 	struct btrfs_trans_handle *trans;
9715 	unsigned int trans_num_items;
9716 	struct btrfs_root *root = BTRFS_I(old_dir)->root;
9717 	struct btrfs_root *dest = BTRFS_I(new_dir)->root;
9718 	struct inode *new_inode = d_inode(new_dentry);
9719 	struct inode *old_inode = d_inode(old_dentry);
9720 	u64 index = 0;
9721 	u64 root_objectid;
9722 	int ret;
9723 	u64 old_ino = btrfs_ino(BTRFS_I(old_inode));
9724 	bool log_pinned = false;
9725 
9726 	if (btrfs_ino(BTRFS_I(new_dir)) == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)
9727 		return -EPERM;
9728 
9729 	/* we only allow rename subvolume link between subvolumes */
9730 	if (old_ino != BTRFS_FIRST_FREE_OBJECTID && root != dest)
9731 		return -EXDEV;
9732 
9733 	if (old_ino == BTRFS_EMPTY_SUBVOL_DIR_OBJECTID ||
9734 	    (new_inode && btrfs_ino(BTRFS_I(new_inode)) == BTRFS_FIRST_FREE_OBJECTID))
9735 		return -ENOTEMPTY;
9736 
9737 	if (S_ISDIR(old_inode->i_mode) && new_inode &&
9738 	    new_inode->i_size > BTRFS_EMPTY_DIR_SIZE)
9739 		return -ENOTEMPTY;
9740 
9741 
9742 	/* check for collisions, even if the  name isn't there */
9743 	ret = btrfs_check_dir_item_collision(dest, new_dir->i_ino,
9744 			     new_dentry->d_name.name,
9745 			     new_dentry->d_name.len);
9746 
9747 	if (ret) {
9748 		if (ret == -EEXIST) {
9749 			/* we shouldn't get
9750 			 * eexist without a new_inode */
9751 			if (WARN_ON(!new_inode)) {
9752 				return ret;
9753 			}
9754 		} else {
9755 			/* maybe -EOVERFLOW */
9756 			return ret;
9757 		}
9758 	}
9759 	ret = 0;
9760 
9761 	/*
9762 	 * we're using rename to replace one file with another.  Start IO on it
9763 	 * now so  we don't add too much work to the end of the transaction
9764 	 */
9765 	if (new_inode && S_ISREG(old_inode->i_mode) && new_inode->i_size)
9766 		filemap_flush(old_inode->i_mapping);
9767 
9768 	/* close the racy window with snapshot create/destroy ioctl */
9769 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9770 		down_read(&fs_info->subvol_sem);
9771 	/*
9772 	 * We want to reserve the absolute worst case amount of items.  So if
9773 	 * both inodes are subvols and we need to unlink them then that would
9774 	 * require 4 item modifications, but if they are both normal inodes it
9775 	 * would require 5 item modifications, so we'll assume they are normal
9776 	 * inodes.  So 5 * 2 is 10, plus 1 for the new link, so 11 total items
9777 	 * should cover the worst case number of items we'll modify.
9778 	 * If our rename has the whiteout flag, we need more 5 units for the
9779 	 * new inode (1 inode item, 1 inode ref, 2 dir items and 1 xattr item
9780 	 * when selinux is enabled).
9781 	 */
9782 	trans_num_items = 11;
9783 	if (flags & RENAME_WHITEOUT)
9784 		trans_num_items += 5;
9785 	trans = btrfs_start_transaction(root, trans_num_items);
9786 	if (IS_ERR(trans)) {
9787 		ret = PTR_ERR(trans);
9788 		goto out_notrans;
9789 	}
9790 
9791 	if (dest != root)
9792 		btrfs_record_root_in_trans(trans, dest);
9793 
9794 	ret = btrfs_set_inode_index(new_dir, &index);
9795 	if (ret)
9796 		goto out_fail;
9797 
9798 	BTRFS_I(old_inode)->dir_index = 0ULL;
9799 	if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9800 		/* force full log commit if subvolume involved. */
9801 		btrfs_set_log_full_commit(fs_info, trans);
9802 	} else {
9803 		btrfs_pin_log_trans(root);
9804 		log_pinned = true;
9805 		ret = btrfs_insert_inode_ref(trans, dest,
9806 					     new_dentry->d_name.name,
9807 					     new_dentry->d_name.len,
9808 					     old_ino,
9809 					     btrfs_ino(BTRFS_I(new_dir)), index);
9810 		if (ret)
9811 			goto out_fail;
9812 	}
9813 
9814 	inode_inc_iversion(old_dir);
9815 	inode_inc_iversion(new_dir);
9816 	inode_inc_iversion(old_inode);
9817 	old_dir->i_ctime = old_dir->i_mtime =
9818 	new_dir->i_ctime = new_dir->i_mtime =
9819 	old_inode->i_ctime = current_time(old_dir);
9820 
9821 	if (old_dentry->d_parent != new_dentry->d_parent)
9822 		btrfs_record_unlink_dir(trans, BTRFS_I(old_dir),
9823 				BTRFS_I(old_inode), 1);
9824 
9825 	if (unlikely(old_ino == BTRFS_FIRST_FREE_OBJECTID)) {
9826 		root_objectid = BTRFS_I(old_inode)->root->root_key.objectid;
9827 		ret = btrfs_unlink_subvol(trans, root, old_dir, root_objectid,
9828 					old_dentry->d_name.name,
9829 					old_dentry->d_name.len);
9830 	} else {
9831 		ret = __btrfs_unlink_inode(trans, root, BTRFS_I(old_dir),
9832 					BTRFS_I(d_inode(old_dentry)),
9833 					old_dentry->d_name.name,
9834 					old_dentry->d_name.len);
9835 		if (!ret)
9836 			ret = btrfs_update_inode(trans, root, old_inode);
9837 	}
9838 	if (ret) {
9839 		btrfs_abort_transaction(trans, ret);
9840 		goto out_fail;
9841 	}
9842 
9843 	if (new_inode) {
9844 		inode_inc_iversion(new_inode);
9845 		new_inode->i_ctime = current_time(new_inode);
9846 		if (unlikely(btrfs_ino(BTRFS_I(new_inode)) ==
9847 			     BTRFS_EMPTY_SUBVOL_DIR_OBJECTID)) {
9848 			root_objectid = BTRFS_I(new_inode)->location.objectid;
9849 			ret = btrfs_unlink_subvol(trans, dest, new_dir,
9850 						root_objectid,
9851 						new_dentry->d_name.name,
9852 						new_dentry->d_name.len);
9853 			BUG_ON(new_inode->i_nlink == 0);
9854 		} else {
9855 			ret = btrfs_unlink_inode(trans, dest, BTRFS_I(new_dir),
9856 						 BTRFS_I(d_inode(new_dentry)),
9857 						 new_dentry->d_name.name,
9858 						 new_dentry->d_name.len);
9859 		}
9860 		if (!ret && new_inode->i_nlink == 0)
9861 			ret = btrfs_orphan_add(trans, d_inode(new_dentry));
9862 		if (ret) {
9863 			btrfs_abort_transaction(trans, ret);
9864 			goto out_fail;
9865 		}
9866 	}
9867 
9868 	ret = btrfs_add_link(trans, new_dir, old_inode,
9869 			     new_dentry->d_name.name,
9870 			     new_dentry->d_name.len, 0, index);
9871 	if (ret) {
9872 		btrfs_abort_transaction(trans, ret);
9873 		goto out_fail;
9874 	}
9875 
9876 	if (old_inode->i_nlink == 1)
9877 		BTRFS_I(old_inode)->dir_index = index;
9878 
9879 	if (log_pinned) {
9880 		struct dentry *parent = new_dentry->d_parent;
9881 
9882 		btrfs_log_new_name(trans, BTRFS_I(old_inode), BTRFS_I(old_dir),
9883 				parent);
9884 		btrfs_end_log_trans(root);
9885 		log_pinned = false;
9886 	}
9887 
9888 	if (flags & RENAME_WHITEOUT) {
9889 		ret = btrfs_whiteout_for_rename(trans, root, old_dir,
9890 						old_dentry);
9891 
9892 		if (ret) {
9893 			btrfs_abort_transaction(trans, ret);
9894 			goto out_fail;
9895 		}
9896 	}
9897 out_fail:
9898 	/*
9899 	 * If we have pinned the log and an error happened, we unpin tasks
9900 	 * trying to sync the log and force them to fallback to a transaction
9901 	 * commit if the log currently contains any of the inodes involved in
9902 	 * this rename operation (to ensure we do not persist a log with an
9903 	 * inconsistent state for any of these inodes or leading to any
9904 	 * inconsistencies when replayed). If the transaction was aborted, the
9905 	 * abortion reason is propagated to userspace when attempting to commit
9906 	 * the transaction. If the log does not contain any of these inodes, we
9907 	 * allow the tasks to sync it.
9908 	 */
9909 	if (ret && log_pinned) {
9910 		if (btrfs_inode_in_log(BTRFS_I(old_dir), fs_info->generation) ||
9911 		    btrfs_inode_in_log(BTRFS_I(new_dir), fs_info->generation) ||
9912 		    btrfs_inode_in_log(BTRFS_I(old_inode), fs_info->generation) ||
9913 		    (new_inode &&
9914 		     btrfs_inode_in_log(BTRFS_I(new_inode), fs_info->generation)))
9915 			btrfs_set_log_full_commit(fs_info, trans);
9916 
9917 		btrfs_end_log_trans(root);
9918 		log_pinned = false;
9919 	}
9920 	btrfs_end_transaction(trans);
9921 out_notrans:
9922 	if (old_ino == BTRFS_FIRST_FREE_OBJECTID)
9923 		up_read(&fs_info->subvol_sem);
9924 
9925 	return ret;
9926 }
9927 
9928 static int btrfs_rename2(struct inode *old_dir, struct dentry *old_dentry,
9929 			 struct inode *new_dir, struct dentry *new_dentry,
9930 			 unsigned int flags)
9931 {
9932 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
9933 		return -EINVAL;
9934 
9935 	if (flags & RENAME_EXCHANGE)
9936 		return btrfs_rename_exchange(old_dir, old_dentry, new_dir,
9937 					  new_dentry);
9938 
9939 	return btrfs_rename(old_dir, old_dentry, new_dir, new_dentry, flags);
9940 }
9941 
9942 static void btrfs_run_delalloc_work(struct btrfs_work *work)
9943 {
9944 	struct btrfs_delalloc_work *delalloc_work;
9945 	struct inode *inode;
9946 
9947 	delalloc_work = container_of(work, struct btrfs_delalloc_work,
9948 				     work);
9949 	inode = delalloc_work->inode;
9950 	filemap_flush(inode->i_mapping);
9951 	if (test_bit(BTRFS_INODE_HAS_ASYNC_EXTENT,
9952 				&BTRFS_I(inode)->runtime_flags))
9953 		filemap_flush(inode->i_mapping);
9954 
9955 	if (delalloc_work->delay_iput)
9956 		btrfs_add_delayed_iput(inode);
9957 	else
9958 		iput(inode);
9959 	complete(&delalloc_work->completion);
9960 }
9961 
9962 struct btrfs_delalloc_work *btrfs_alloc_delalloc_work(struct inode *inode,
9963 						    int delay_iput)
9964 {
9965 	struct btrfs_delalloc_work *work;
9966 
9967 	work = kmalloc(sizeof(*work), GFP_NOFS);
9968 	if (!work)
9969 		return NULL;
9970 
9971 	init_completion(&work->completion);
9972 	INIT_LIST_HEAD(&work->list);
9973 	work->inode = inode;
9974 	work->delay_iput = delay_iput;
9975 	WARN_ON_ONCE(!inode);
9976 	btrfs_init_work(&work->work, btrfs_flush_delalloc_helper,
9977 			btrfs_run_delalloc_work, NULL, NULL);
9978 
9979 	return work;
9980 }
9981 
9982 void btrfs_wait_and_free_delalloc_work(struct btrfs_delalloc_work *work)
9983 {
9984 	wait_for_completion(&work->completion);
9985 	kfree(work);
9986 }
9987 
9988 /*
9989  * some fairly slow code that needs optimization. This walks the list
9990  * of all the inodes with pending delalloc and forces them to disk.
9991  */
9992 static int __start_delalloc_inodes(struct btrfs_root *root, int delay_iput,
9993 				   int nr)
9994 {
9995 	struct btrfs_inode *binode;
9996 	struct inode *inode;
9997 	struct btrfs_delalloc_work *work, *next;
9998 	struct list_head works;
9999 	struct list_head splice;
10000 	int ret = 0;
10001 
10002 	INIT_LIST_HEAD(&works);
10003 	INIT_LIST_HEAD(&splice);
10004 
10005 	mutex_lock(&root->delalloc_mutex);
10006 	spin_lock(&root->delalloc_lock);
10007 	list_splice_init(&root->delalloc_inodes, &splice);
10008 	while (!list_empty(&splice)) {
10009 		binode = list_entry(splice.next, struct btrfs_inode,
10010 				    delalloc_inodes);
10011 
10012 		list_move_tail(&binode->delalloc_inodes,
10013 			       &root->delalloc_inodes);
10014 		inode = igrab(&binode->vfs_inode);
10015 		if (!inode) {
10016 			cond_resched_lock(&root->delalloc_lock);
10017 			continue;
10018 		}
10019 		spin_unlock(&root->delalloc_lock);
10020 
10021 		work = btrfs_alloc_delalloc_work(inode, delay_iput);
10022 		if (!work) {
10023 			if (delay_iput)
10024 				btrfs_add_delayed_iput(inode);
10025 			else
10026 				iput(inode);
10027 			ret = -ENOMEM;
10028 			goto out;
10029 		}
10030 		list_add_tail(&work->list, &works);
10031 		btrfs_queue_work(root->fs_info->flush_workers,
10032 				 &work->work);
10033 		ret++;
10034 		if (nr != -1 && ret >= nr)
10035 			goto out;
10036 		cond_resched();
10037 		spin_lock(&root->delalloc_lock);
10038 	}
10039 	spin_unlock(&root->delalloc_lock);
10040 
10041 out:
10042 	list_for_each_entry_safe(work, next, &works, list) {
10043 		list_del_init(&work->list);
10044 		btrfs_wait_and_free_delalloc_work(work);
10045 	}
10046 
10047 	if (!list_empty_careful(&splice)) {
10048 		spin_lock(&root->delalloc_lock);
10049 		list_splice_tail(&splice, &root->delalloc_inodes);
10050 		spin_unlock(&root->delalloc_lock);
10051 	}
10052 	mutex_unlock(&root->delalloc_mutex);
10053 	return ret;
10054 }
10055 
10056 int btrfs_start_delalloc_inodes(struct btrfs_root *root, int delay_iput)
10057 {
10058 	struct btrfs_fs_info *fs_info = root->fs_info;
10059 	int ret;
10060 
10061 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10062 		return -EROFS;
10063 
10064 	ret = __start_delalloc_inodes(root, delay_iput, -1);
10065 	if (ret > 0)
10066 		ret = 0;
10067 	/*
10068 	 * the filemap_flush will queue IO into the worker threads, but
10069 	 * we have to make sure the IO is actually started and that
10070 	 * ordered extents get created before we return
10071 	 */
10072 	atomic_inc(&fs_info->async_submit_draining);
10073 	while (atomic_read(&fs_info->nr_async_submits) ||
10074 	       atomic_read(&fs_info->async_delalloc_pages)) {
10075 		wait_event(fs_info->async_submit_wait,
10076 			   (atomic_read(&fs_info->nr_async_submits) == 0 &&
10077 			    atomic_read(&fs_info->async_delalloc_pages) == 0));
10078 	}
10079 	atomic_dec(&fs_info->async_submit_draining);
10080 	return ret;
10081 }
10082 
10083 int btrfs_start_delalloc_roots(struct btrfs_fs_info *fs_info, int delay_iput,
10084 			       int nr)
10085 {
10086 	struct btrfs_root *root;
10087 	struct list_head splice;
10088 	int ret;
10089 
10090 	if (test_bit(BTRFS_FS_STATE_ERROR, &fs_info->fs_state))
10091 		return -EROFS;
10092 
10093 	INIT_LIST_HEAD(&splice);
10094 
10095 	mutex_lock(&fs_info->delalloc_root_mutex);
10096 	spin_lock(&fs_info->delalloc_root_lock);
10097 	list_splice_init(&fs_info->delalloc_roots, &splice);
10098 	while (!list_empty(&splice) && nr) {
10099 		root = list_first_entry(&splice, struct btrfs_root,
10100 					delalloc_root);
10101 		root = btrfs_grab_fs_root(root);
10102 		BUG_ON(!root);
10103 		list_move_tail(&root->delalloc_root,
10104 			       &fs_info->delalloc_roots);
10105 		spin_unlock(&fs_info->delalloc_root_lock);
10106 
10107 		ret = __start_delalloc_inodes(root, delay_iput, nr);
10108 		btrfs_put_fs_root(root);
10109 		if (ret < 0)
10110 			goto out;
10111 
10112 		if (nr != -1) {
10113 			nr -= ret;
10114 			WARN_ON(nr < 0);
10115 		}
10116 		spin_lock(&fs_info->delalloc_root_lock);
10117 	}
10118 	spin_unlock(&fs_info->delalloc_root_lock);
10119 
10120 	ret = 0;
10121 	atomic_inc(&fs_info->async_submit_draining);
10122 	while (atomic_read(&fs_info->nr_async_submits) ||
10123 	      atomic_read(&fs_info->async_delalloc_pages)) {
10124 		wait_event(fs_info->async_submit_wait,
10125 		   (atomic_read(&fs_info->nr_async_submits) == 0 &&
10126 		    atomic_read(&fs_info->async_delalloc_pages) == 0));
10127 	}
10128 	atomic_dec(&fs_info->async_submit_draining);
10129 out:
10130 	if (!list_empty_careful(&splice)) {
10131 		spin_lock(&fs_info->delalloc_root_lock);
10132 		list_splice_tail(&splice, &fs_info->delalloc_roots);
10133 		spin_unlock(&fs_info->delalloc_root_lock);
10134 	}
10135 	mutex_unlock(&fs_info->delalloc_root_mutex);
10136 	return ret;
10137 }
10138 
10139 static int btrfs_symlink(struct inode *dir, struct dentry *dentry,
10140 			 const char *symname)
10141 {
10142 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10143 	struct btrfs_trans_handle *trans;
10144 	struct btrfs_root *root = BTRFS_I(dir)->root;
10145 	struct btrfs_path *path;
10146 	struct btrfs_key key;
10147 	struct inode *inode = NULL;
10148 	int err;
10149 	int drop_inode = 0;
10150 	u64 objectid;
10151 	u64 index = 0;
10152 	int name_len;
10153 	int datasize;
10154 	unsigned long ptr;
10155 	struct btrfs_file_extent_item *ei;
10156 	struct extent_buffer *leaf;
10157 
10158 	name_len = strlen(symname);
10159 	if (name_len > BTRFS_MAX_INLINE_DATA_SIZE(fs_info))
10160 		return -ENAMETOOLONG;
10161 
10162 	/*
10163 	 * 2 items for inode item and ref
10164 	 * 2 items for dir items
10165 	 * 1 item for updating parent inode item
10166 	 * 1 item for the inline extent item
10167 	 * 1 item for xattr if selinux is on
10168 	 */
10169 	trans = btrfs_start_transaction(root, 7);
10170 	if (IS_ERR(trans))
10171 		return PTR_ERR(trans);
10172 
10173 	err = btrfs_find_free_ino(root, &objectid);
10174 	if (err)
10175 		goto out_unlock;
10176 
10177 	inode = btrfs_new_inode(trans, root, dir, dentry->d_name.name,
10178 				dentry->d_name.len, btrfs_ino(BTRFS_I(dir)),
10179 				objectid, S_IFLNK|S_IRWXUGO, &index);
10180 	if (IS_ERR(inode)) {
10181 		err = PTR_ERR(inode);
10182 		goto out_unlock;
10183 	}
10184 
10185 	/*
10186 	* If the active LSM wants to access the inode during
10187 	* d_instantiate it needs these. Smack checks to see
10188 	* if the filesystem supports xattrs by looking at the
10189 	* ops vector.
10190 	*/
10191 	inode->i_fop = &btrfs_file_operations;
10192 	inode->i_op = &btrfs_file_inode_operations;
10193 	inode->i_mapping->a_ops = &btrfs_aops;
10194 	BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10195 
10196 	err = btrfs_init_inode_security(trans, inode, dir, &dentry->d_name);
10197 	if (err)
10198 		goto out_unlock_inode;
10199 
10200 	path = btrfs_alloc_path();
10201 	if (!path) {
10202 		err = -ENOMEM;
10203 		goto out_unlock_inode;
10204 	}
10205 	key.objectid = btrfs_ino(BTRFS_I(inode));
10206 	key.offset = 0;
10207 	key.type = BTRFS_EXTENT_DATA_KEY;
10208 	datasize = btrfs_file_extent_calc_inline_size(name_len);
10209 	err = btrfs_insert_empty_item(trans, root, path, &key,
10210 				      datasize);
10211 	if (err) {
10212 		btrfs_free_path(path);
10213 		goto out_unlock_inode;
10214 	}
10215 	leaf = path->nodes[0];
10216 	ei = btrfs_item_ptr(leaf, path->slots[0],
10217 			    struct btrfs_file_extent_item);
10218 	btrfs_set_file_extent_generation(leaf, ei, trans->transid);
10219 	btrfs_set_file_extent_type(leaf, ei,
10220 				   BTRFS_FILE_EXTENT_INLINE);
10221 	btrfs_set_file_extent_encryption(leaf, ei, 0);
10222 	btrfs_set_file_extent_compression(leaf, ei, 0);
10223 	btrfs_set_file_extent_other_encoding(leaf, ei, 0);
10224 	btrfs_set_file_extent_ram_bytes(leaf, ei, name_len);
10225 
10226 	ptr = btrfs_file_extent_inline_start(ei);
10227 	write_extent_buffer(leaf, symname, ptr, name_len);
10228 	btrfs_mark_buffer_dirty(leaf);
10229 	btrfs_free_path(path);
10230 
10231 	inode->i_op = &btrfs_symlink_inode_operations;
10232 	inode_nohighmem(inode);
10233 	inode->i_mapping->a_ops = &btrfs_symlink_aops;
10234 	inode_set_bytes(inode, name_len);
10235 	btrfs_i_size_write(inode, name_len);
10236 	err = btrfs_update_inode(trans, root, inode);
10237 	/*
10238 	 * Last step, add directory indexes for our symlink inode. This is the
10239 	 * last step to avoid extra cleanup of these indexes if an error happens
10240 	 * elsewhere above.
10241 	 */
10242 	if (!err)
10243 		err = btrfs_add_nondir(trans, dir, dentry, inode, 0, index);
10244 	if (err) {
10245 		drop_inode = 1;
10246 		goto out_unlock_inode;
10247 	}
10248 
10249 	unlock_new_inode(inode);
10250 	d_instantiate(dentry, inode);
10251 
10252 out_unlock:
10253 	btrfs_end_transaction(trans);
10254 	if (drop_inode) {
10255 		inode_dec_link_count(inode);
10256 		iput(inode);
10257 	}
10258 	btrfs_btree_balance_dirty(fs_info);
10259 	return err;
10260 
10261 out_unlock_inode:
10262 	drop_inode = 1;
10263 	unlock_new_inode(inode);
10264 	goto out_unlock;
10265 }
10266 
10267 static int __btrfs_prealloc_file_range(struct inode *inode, int mode,
10268 				       u64 start, u64 num_bytes, u64 min_size,
10269 				       loff_t actual_len, u64 *alloc_hint,
10270 				       struct btrfs_trans_handle *trans)
10271 {
10272 	struct btrfs_fs_info *fs_info = btrfs_sb(inode->i_sb);
10273 	struct extent_map_tree *em_tree = &BTRFS_I(inode)->extent_tree;
10274 	struct extent_map *em;
10275 	struct btrfs_root *root = BTRFS_I(inode)->root;
10276 	struct btrfs_key ins;
10277 	u64 cur_offset = start;
10278 	u64 i_size;
10279 	u64 cur_bytes;
10280 	u64 last_alloc = (u64)-1;
10281 	int ret = 0;
10282 	bool own_trans = true;
10283 	u64 end = start + num_bytes - 1;
10284 
10285 	if (trans)
10286 		own_trans = false;
10287 	while (num_bytes > 0) {
10288 		if (own_trans) {
10289 			trans = btrfs_start_transaction(root, 3);
10290 			if (IS_ERR(trans)) {
10291 				ret = PTR_ERR(trans);
10292 				break;
10293 			}
10294 		}
10295 
10296 		cur_bytes = min_t(u64, num_bytes, SZ_256M);
10297 		cur_bytes = max(cur_bytes, min_size);
10298 		/*
10299 		 * If we are severely fragmented we could end up with really
10300 		 * small allocations, so if the allocator is returning small
10301 		 * chunks lets make its job easier by only searching for those
10302 		 * sized chunks.
10303 		 */
10304 		cur_bytes = min(cur_bytes, last_alloc);
10305 		ret = btrfs_reserve_extent(root, cur_bytes, cur_bytes,
10306 				min_size, 0, *alloc_hint, &ins, 1, 0);
10307 		if (ret) {
10308 			if (own_trans)
10309 				btrfs_end_transaction(trans);
10310 			break;
10311 		}
10312 		btrfs_dec_block_group_reservations(fs_info, ins.objectid);
10313 
10314 		last_alloc = ins.offset;
10315 		ret = insert_reserved_file_extent(trans, inode,
10316 						  cur_offset, ins.objectid,
10317 						  ins.offset, ins.offset,
10318 						  ins.offset, 0, 0, 0,
10319 						  BTRFS_FILE_EXTENT_PREALLOC);
10320 		if (ret) {
10321 			btrfs_free_reserved_extent(fs_info, ins.objectid,
10322 						   ins.offset, 0);
10323 			btrfs_abort_transaction(trans, ret);
10324 			if (own_trans)
10325 				btrfs_end_transaction(trans);
10326 			break;
10327 		}
10328 
10329 		btrfs_drop_extent_cache(inode, cur_offset,
10330 					cur_offset + ins.offset -1, 0);
10331 
10332 		em = alloc_extent_map();
10333 		if (!em) {
10334 			set_bit(BTRFS_INODE_NEEDS_FULL_SYNC,
10335 				&BTRFS_I(inode)->runtime_flags);
10336 			goto next;
10337 		}
10338 
10339 		em->start = cur_offset;
10340 		em->orig_start = cur_offset;
10341 		em->len = ins.offset;
10342 		em->block_start = ins.objectid;
10343 		em->block_len = ins.offset;
10344 		em->orig_block_len = ins.offset;
10345 		em->ram_bytes = ins.offset;
10346 		em->bdev = fs_info->fs_devices->latest_bdev;
10347 		set_bit(EXTENT_FLAG_PREALLOC, &em->flags);
10348 		em->generation = trans->transid;
10349 
10350 		while (1) {
10351 			write_lock(&em_tree->lock);
10352 			ret = add_extent_mapping(em_tree, em, 1);
10353 			write_unlock(&em_tree->lock);
10354 			if (ret != -EEXIST)
10355 				break;
10356 			btrfs_drop_extent_cache(inode, cur_offset,
10357 						cur_offset + ins.offset - 1,
10358 						0);
10359 		}
10360 		free_extent_map(em);
10361 next:
10362 		num_bytes -= ins.offset;
10363 		cur_offset += ins.offset;
10364 		*alloc_hint = ins.objectid + ins.offset;
10365 
10366 		inode_inc_iversion(inode);
10367 		inode->i_ctime = current_time(inode);
10368 		BTRFS_I(inode)->flags |= BTRFS_INODE_PREALLOC;
10369 		if (!(mode & FALLOC_FL_KEEP_SIZE) &&
10370 		    (actual_len > inode->i_size) &&
10371 		    (cur_offset > inode->i_size)) {
10372 			if (cur_offset > actual_len)
10373 				i_size = actual_len;
10374 			else
10375 				i_size = cur_offset;
10376 			i_size_write(inode, i_size);
10377 			btrfs_ordered_update_i_size(inode, i_size, NULL);
10378 		}
10379 
10380 		ret = btrfs_update_inode(trans, root, inode);
10381 
10382 		if (ret) {
10383 			btrfs_abort_transaction(trans, ret);
10384 			if (own_trans)
10385 				btrfs_end_transaction(trans);
10386 			break;
10387 		}
10388 
10389 		if (own_trans)
10390 			btrfs_end_transaction(trans);
10391 	}
10392 	if (cur_offset < end)
10393 		btrfs_free_reserved_data_space(inode, cur_offset,
10394 			end - cur_offset + 1);
10395 	return ret;
10396 }
10397 
10398 int btrfs_prealloc_file_range(struct inode *inode, int mode,
10399 			      u64 start, u64 num_bytes, u64 min_size,
10400 			      loff_t actual_len, u64 *alloc_hint)
10401 {
10402 	return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10403 					   min_size, actual_len, alloc_hint,
10404 					   NULL);
10405 }
10406 
10407 int btrfs_prealloc_file_range_trans(struct inode *inode,
10408 				    struct btrfs_trans_handle *trans, int mode,
10409 				    u64 start, u64 num_bytes, u64 min_size,
10410 				    loff_t actual_len, u64 *alloc_hint)
10411 {
10412 	return __btrfs_prealloc_file_range(inode, mode, start, num_bytes,
10413 					   min_size, actual_len, alloc_hint, trans);
10414 }
10415 
10416 static int btrfs_set_page_dirty(struct page *page)
10417 {
10418 	return __set_page_dirty_nobuffers(page);
10419 }
10420 
10421 static int btrfs_permission(struct inode *inode, int mask)
10422 {
10423 	struct btrfs_root *root = BTRFS_I(inode)->root;
10424 	umode_t mode = inode->i_mode;
10425 
10426 	if (mask & MAY_WRITE &&
10427 	    (S_ISREG(mode) || S_ISDIR(mode) || S_ISLNK(mode))) {
10428 		if (btrfs_root_readonly(root))
10429 			return -EROFS;
10430 		if (BTRFS_I(inode)->flags & BTRFS_INODE_READONLY)
10431 			return -EACCES;
10432 	}
10433 	return generic_permission(inode, mask);
10434 }
10435 
10436 static int btrfs_tmpfile(struct inode *dir, struct dentry *dentry, umode_t mode)
10437 {
10438 	struct btrfs_fs_info *fs_info = btrfs_sb(dir->i_sb);
10439 	struct btrfs_trans_handle *trans;
10440 	struct btrfs_root *root = BTRFS_I(dir)->root;
10441 	struct inode *inode = NULL;
10442 	u64 objectid;
10443 	u64 index;
10444 	int ret = 0;
10445 
10446 	/*
10447 	 * 5 units required for adding orphan entry
10448 	 */
10449 	trans = btrfs_start_transaction(root, 5);
10450 	if (IS_ERR(trans))
10451 		return PTR_ERR(trans);
10452 
10453 	ret = btrfs_find_free_ino(root, &objectid);
10454 	if (ret)
10455 		goto out;
10456 
10457 	inode = btrfs_new_inode(trans, root, dir, NULL, 0,
10458 			btrfs_ino(BTRFS_I(dir)), objectid, mode, &index);
10459 	if (IS_ERR(inode)) {
10460 		ret = PTR_ERR(inode);
10461 		inode = NULL;
10462 		goto out;
10463 	}
10464 
10465 	inode->i_fop = &btrfs_file_operations;
10466 	inode->i_op = &btrfs_file_inode_operations;
10467 
10468 	inode->i_mapping->a_ops = &btrfs_aops;
10469 	BTRFS_I(inode)->io_tree.ops = &btrfs_extent_io_ops;
10470 
10471 	ret = btrfs_init_inode_security(trans, inode, dir, NULL);
10472 	if (ret)
10473 		goto out_inode;
10474 
10475 	ret = btrfs_update_inode(trans, root, inode);
10476 	if (ret)
10477 		goto out_inode;
10478 	ret = btrfs_orphan_add(trans, inode);
10479 	if (ret)
10480 		goto out_inode;
10481 
10482 	/*
10483 	 * We set number of links to 0 in btrfs_new_inode(), and here we set
10484 	 * it to 1 because d_tmpfile() will issue a warning if the count is 0,
10485 	 * through:
10486 	 *
10487 	 *    d_tmpfile() -> inode_dec_link_count() -> drop_nlink()
10488 	 */
10489 	set_nlink(inode, 1);
10490 	unlock_new_inode(inode);
10491 	d_tmpfile(dentry, inode);
10492 	mark_inode_dirty(inode);
10493 
10494 out:
10495 	btrfs_end_transaction(trans);
10496 	if (ret)
10497 		iput(inode);
10498 	btrfs_balance_delayed_items(fs_info);
10499 	btrfs_btree_balance_dirty(fs_info);
10500 	return ret;
10501 
10502 out_inode:
10503 	unlock_new_inode(inode);
10504 	goto out;
10505 
10506 }
10507 
10508 static const struct inode_operations btrfs_dir_inode_operations = {
10509 	.getattr	= btrfs_getattr,
10510 	.lookup		= btrfs_lookup,
10511 	.create		= btrfs_create,
10512 	.unlink		= btrfs_unlink,
10513 	.link		= btrfs_link,
10514 	.mkdir		= btrfs_mkdir,
10515 	.rmdir		= btrfs_rmdir,
10516 	.rename		= btrfs_rename2,
10517 	.symlink	= btrfs_symlink,
10518 	.setattr	= btrfs_setattr,
10519 	.mknod		= btrfs_mknod,
10520 	.listxattr	= btrfs_listxattr,
10521 	.permission	= btrfs_permission,
10522 	.get_acl	= btrfs_get_acl,
10523 	.set_acl	= btrfs_set_acl,
10524 	.update_time	= btrfs_update_time,
10525 	.tmpfile        = btrfs_tmpfile,
10526 };
10527 static const struct inode_operations btrfs_dir_ro_inode_operations = {
10528 	.lookup		= btrfs_lookup,
10529 	.permission	= btrfs_permission,
10530 	.update_time	= btrfs_update_time,
10531 };
10532 
10533 static const struct file_operations btrfs_dir_file_operations = {
10534 	.llseek		= generic_file_llseek,
10535 	.read		= generic_read_dir,
10536 	.iterate_shared	= btrfs_real_readdir,
10537 	.unlocked_ioctl	= btrfs_ioctl,
10538 #ifdef CONFIG_COMPAT
10539 	.compat_ioctl	= btrfs_compat_ioctl,
10540 #endif
10541 	.release        = btrfs_release_file,
10542 	.fsync		= btrfs_sync_file,
10543 };
10544 
10545 static const struct extent_io_ops btrfs_extent_io_ops = {
10546 	.fill_delalloc = run_delalloc_range,
10547 	.submit_bio_hook = btrfs_submit_bio_hook,
10548 	.merge_bio_hook = btrfs_merge_bio_hook,
10549 	.readpage_end_io_hook = btrfs_readpage_end_io_hook,
10550 	.writepage_end_io_hook = btrfs_writepage_end_io_hook,
10551 	.writepage_start_hook = btrfs_writepage_start_hook,
10552 	.set_bit_hook = btrfs_set_bit_hook,
10553 	.clear_bit_hook = btrfs_clear_bit_hook,
10554 	.merge_extent_hook = btrfs_merge_extent_hook,
10555 	.split_extent_hook = btrfs_split_extent_hook,
10556 };
10557 
10558 /*
10559  * btrfs doesn't support the bmap operation because swapfiles
10560  * use bmap to make a mapping of extents in the file.  They assume
10561  * these extents won't change over the life of the file and they
10562  * use the bmap result to do IO directly to the drive.
10563  *
10564  * the btrfs bmap call would return logical addresses that aren't
10565  * suitable for IO and they also will change frequently as COW
10566  * operations happen.  So, swapfile + btrfs == corruption.
10567  *
10568  * For now we're avoiding this by dropping bmap.
10569  */
10570 static const struct address_space_operations btrfs_aops = {
10571 	.readpage	= btrfs_readpage,
10572 	.writepage	= btrfs_writepage,
10573 	.writepages	= btrfs_writepages,
10574 	.readpages	= btrfs_readpages,
10575 	.direct_IO	= btrfs_direct_IO,
10576 	.invalidatepage = btrfs_invalidatepage,
10577 	.releasepage	= btrfs_releasepage,
10578 	.set_page_dirty	= btrfs_set_page_dirty,
10579 	.error_remove_page = generic_error_remove_page,
10580 };
10581 
10582 static const struct address_space_operations btrfs_symlink_aops = {
10583 	.readpage	= btrfs_readpage,
10584 	.writepage	= btrfs_writepage,
10585 	.invalidatepage = btrfs_invalidatepage,
10586 	.releasepage	= btrfs_releasepage,
10587 };
10588 
10589 static const struct inode_operations btrfs_file_inode_operations = {
10590 	.getattr	= btrfs_getattr,
10591 	.setattr	= btrfs_setattr,
10592 	.listxattr      = btrfs_listxattr,
10593 	.permission	= btrfs_permission,
10594 	.fiemap		= btrfs_fiemap,
10595 	.get_acl	= btrfs_get_acl,
10596 	.set_acl	= btrfs_set_acl,
10597 	.update_time	= btrfs_update_time,
10598 };
10599 static const struct inode_operations btrfs_special_inode_operations = {
10600 	.getattr	= btrfs_getattr,
10601 	.setattr	= btrfs_setattr,
10602 	.permission	= btrfs_permission,
10603 	.listxattr	= btrfs_listxattr,
10604 	.get_acl	= btrfs_get_acl,
10605 	.set_acl	= btrfs_set_acl,
10606 	.update_time	= btrfs_update_time,
10607 };
10608 static const struct inode_operations btrfs_symlink_inode_operations = {
10609 	.get_link	= page_get_link,
10610 	.getattr	= btrfs_getattr,
10611 	.setattr	= btrfs_setattr,
10612 	.permission	= btrfs_permission,
10613 	.listxattr	= btrfs_listxattr,
10614 	.update_time	= btrfs_update_time,
10615 };
10616 
10617 const struct dentry_operations btrfs_dentry_operations = {
10618 	.d_delete	= btrfs_dentry_delete,
10619 	.d_release	= btrfs_dentry_release,
10620 };
10621