xref: /linux/fs/btrfs/fiemap.c (revision f16d85cd3de9d0c761b74155b4cbe2f20d3dcf80)
1 // SPDX-License-Identifier: GPL-2.0
2 
3 #include "backref.h"
4 #include "btrfs_inode.h"
5 #include "fiemap.h"
6 #include "file.h"
7 #include "file-item.h"
8 
9 struct btrfs_fiemap_entry {
10 	u64 offset;
11 	u64 phys;
12 	u64 len;
13 	u32 flags;
14 };
15 
16 /*
17  * Indicate the caller of emit_fiemap_extent() that it needs to unlock the file
18  * range from the inode's io tree, unlock the subvolume tree search path, flush
19  * the fiemap cache and relock the file range and research the subvolume tree.
20  * The value here is something negative that can't be confused with a valid
21  * errno value and different from 1 because that's also a return value from
22  * fiemap_fill_next_extent() and also it's often used to mean some btree search
23  * did not find a key, so make it some distinct negative value.
24  */
25 #define BTRFS_FIEMAP_FLUSH_CACHE (-(MAX_ERRNO + 1))
26 
27 /*
28  * Used to:
29  *
30  * - Cache the next entry to be emitted to the fiemap buffer, so that we can
31  *   merge extents that are contiguous and can be grouped as a single one;
32  *
33  * - Store extents ready to be written to the fiemap buffer in an intermediary
34  *   buffer. This intermediary buffer is to ensure that in case the fiemap
35  *   buffer is memory mapped to the fiemap target file, we don't deadlock
36  *   during btrfs_page_mkwrite(). This is because during fiemap we are locking
37  *   an extent range in order to prevent races with delalloc flushing and
38  *   ordered extent completion, which is needed in order to reliably detect
39  *   delalloc in holes and prealloc extents. And this can lead to a deadlock
40  *   if the fiemap buffer is memory mapped to the file we are running fiemap
41  *   against (a silly, useless in practice scenario, but possible) because
42  *   btrfs_page_mkwrite() will try to lock the same extent range.
43  */
44 struct fiemap_cache {
45 	/* An array of ready fiemap entries. */
46 	struct btrfs_fiemap_entry *entries;
47 	/* Number of entries in the entries array. */
48 	int entries_size;
49 	/* Index of the next entry in the entries array to write to. */
50 	int entries_pos;
51 	/*
52 	 * Once the entries array is full, this indicates what's the offset for
53 	 * the next file extent item we must search for in the inode's subvolume
54 	 * tree after unlocking the extent range in the inode's io tree and
55 	 * releasing the search path.
56 	 */
57 	u64 next_search_offset;
58 	/*
59 	 * This matches struct fiemap_extent_info::fi_mapped_extents, we use it
60 	 * to count ourselves emitted extents and stop instead of relying on
61 	 * fiemap_fill_next_extent() because we buffer ready fiemap entries at
62 	 * the @entries array, and we want to stop as soon as we hit the max
63 	 * amount of extents to map, not just to save time but also to make the
64 	 * logic at extent_fiemap() simpler.
65 	 */
66 	unsigned int extents_mapped;
67 	/* Fields for the cached extent (unsubmitted, not ready, extent). */
68 	u64 offset;
69 	u64 phys;
70 	u64 len;
71 	u32 flags;
72 	bool cached;
73 };
74 
75 static int flush_fiemap_cache(struct fiemap_extent_info *fieinfo,
76 			      struct fiemap_cache *cache)
77 {
78 	for (int i = 0; i < cache->entries_pos; i++) {
79 		struct btrfs_fiemap_entry *entry = &cache->entries[i];
80 		int ret;
81 
82 		ret = fiemap_fill_next_extent(fieinfo, entry->offset,
83 					      entry->phys, entry->len,
84 					      entry->flags);
85 		/*
86 		 * Ignore 1 (reached max entries) because we keep track of that
87 		 * ourselves in emit_fiemap_extent().
88 		 */
89 		if (ret < 0)
90 			return ret;
91 	}
92 	cache->entries_pos = 0;
93 
94 	return 0;
95 }
96 
97 /*
98  * Helper to submit fiemap extent.
99  *
100  * Will try to merge current fiemap extent specified by @offset, @phys,
101  * @len and @flags with cached one.
102  * And only when we fails to merge, cached one will be submitted as
103  * fiemap extent.
104  *
105  * Return value is the same as fiemap_fill_next_extent().
106  */
107 static int emit_fiemap_extent(struct fiemap_extent_info *fieinfo,
108 				struct fiemap_cache *cache,
109 				u64 offset, u64 phys, u64 len, u32 flags)
110 {
111 	struct btrfs_fiemap_entry *entry;
112 	u64 cache_end;
113 
114 	/* Set at the end of extent_fiemap(). */
115 	ASSERT((flags & FIEMAP_EXTENT_LAST) == 0, "flags=0x%u", flags);
116 
117 	if (!cache->cached)
118 		goto assign;
119 
120 	/*
121 	 * When iterating the extents of the inode, at extent_fiemap(), we may
122 	 * find an extent that starts at an offset behind the end offset of the
123 	 * previous extent we processed. This happens if fiemap is called
124 	 * without FIEMAP_FLAG_SYNC and there are ordered extents completing
125 	 * after we had to unlock the file range, release the search path, emit
126 	 * the fiemap extents stored in the buffer (cache->entries array) and
127 	 * the lock the remainder of the range and re-search the btree.
128 	 *
129 	 * For example we are in leaf X processing its last item, which is the
130 	 * file extent item for file range [512K, 1M[, and after
131 	 * btrfs_next_leaf() releases the path, there's an ordered extent that
132 	 * completes for the file range [768K, 2M[, and that results in trimming
133 	 * the file extent item so that it now corresponds to the file range
134 	 * [512K, 768K[ and a new file extent item is inserted for the file
135 	 * range [768K, 2M[, which may end up as the last item of leaf X or as
136 	 * the first item of the next leaf - in either case btrfs_next_leaf()
137 	 * will leave us with a path pointing to the new extent item, for the
138 	 * file range [768K, 2M[, since that's the first key that follows the
139 	 * last one we processed. So in order not to report overlapping extents
140 	 * to user space, we trim the length of the previously cached extent and
141 	 * emit it.
142 	 *
143 	 * Upon calling btrfs_next_leaf() we may also find an extent with an
144 	 * offset smaller than or equals to cache->offset, and this happens
145 	 * when we had a hole or prealloc extent with several delalloc ranges in
146 	 * it, but after btrfs_next_leaf() released the path, delalloc was
147 	 * flushed and the resulting ordered extents were completed, so we can
148 	 * now have found a file extent item for an offset that is smaller than
149 	 * or equals to what we have in cache->offset. We deal with this as
150 	 * described below.
151 	 */
152 	cache_end = cache->offset + cache->len;
153 	if (cache_end > offset) {
154 		if (offset == cache->offset) {
155 			/*
156 			 * We cached a delalloc range (found in the io tree) for
157 			 * a hole or prealloc extent and we have now found a
158 			 * file extent item for the same offset. What we have
159 			 * now is more recent and up to date, so discard what
160 			 * we had in the cache and use what we have just found.
161 			 */
162 			goto assign;
163 		} else if (offset > cache->offset) {
164 			/*
165 			 * The extent range we previously found ends after the
166 			 * offset of the file extent item we found and that
167 			 * offset falls somewhere in the middle of that previous
168 			 * extent range. So adjust the range we previously found
169 			 * to end at the offset of the file extent item we have
170 			 * just found, since this extent is more up to date.
171 			 * Emit that adjusted range and cache the file extent
172 			 * item we have just found. This corresponds to the case
173 			 * where a previously found file extent item was split
174 			 * due to an ordered extent completing.
175 			 */
176 			cache->len = offset - cache->offset;
177 			goto emit;
178 		} else {
179 			const u64 range_end = offset + len;
180 
181 			/*
182 			 * The offset of the file extent item we have just found
183 			 * is behind the cached offset. This means we were
184 			 * processing a hole or prealloc extent for which we
185 			 * have found delalloc ranges (in the io tree), so what
186 			 * we have in the cache is the last delalloc range we
187 			 * found while the file extent item we found can be
188 			 * either for a whole delalloc range we previously
189 			 * emitted or only a part of that range.
190 			 *
191 			 * We have two cases here:
192 			 *
193 			 * 1) The file extent item's range ends at or behind the
194 			 *    cached extent's end. In this case just ignore the
195 			 *    current file extent item because we don't want to
196 			 *    overlap with previous ranges that may have been
197 			 *    emitted already;
198 			 *
199 			 * 2) The file extent item starts behind the currently
200 			 *    cached extent but its end offset goes beyond the
201 			 *    end offset of the cached extent. We don't want to
202 			 *    overlap with a previous range that may have been
203 			 *    emitted already, so we emit the currently cached
204 			 *    extent and then partially store the current file
205 			 *    extent item's range in the cache, for the subrange
206 			 *    going the cached extent's end to the end of the
207 			 *    file extent item.
208 			 */
209 			if (range_end <= cache_end)
210 				return 0;
211 
212 			if (!(flags & (FIEMAP_EXTENT_ENCODED | FIEMAP_EXTENT_DELALLOC)))
213 				phys += cache_end - offset;
214 
215 			offset = cache_end;
216 			len = range_end - cache_end;
217 			goto emit;
218 		}
219 	}
220 
221 	/*
222 	 * Only merges fiemap extents if
223 	 * 1) Their logical addresses are continuous
224 	 *
225 	 * 2) Their physical addresses are continuous
226 	 *    So truly compressed (physical size smaller than logical size)
227 	 *    extents won't get merged with each other
228 	 *
229 	 * 3) Share same flags
230 	 */
231 	if (!(flags & FIEMAP_EXTENT_ENCODED) &&
232 	    cache->offset + cache->len == offset &&
233 	    cache->phys + cache->len == phys  &&
234 	    cache->flags == flags) {
235 		cache->len += len;
236 		return 0;
237 	}
238 
239 emit:
240 	/* Not mergeable, need to submit cached one */
241 
242 	if (cache->entries_pos == cache->entries_size) {
243 		/*
244 		 * We will need to research for the end offset of the last
245 		 * stored extent and not from the current offset, because after
246 		 * unlocking the range and releasing the path, if there's a hole
247 		 * between that end offset and this current offset, a new extent
248 		 * may have been inserted due to a new write, so we don't want
249 		 * to miss it.
250 		 */
251 		entry = &cache->entries[cache->entries_size - 1];
252 		cache->next_search_offset = entry->offset + entry->len;
253 		cache->cached = false;
254 
255 		return BTRFS_FIEMAP_FLUSH_CACHE;
256 	}
257 
258 	entry = &cache->entries[cache->entries_pos];
259 	entry->offset = cache->offset;
260 	entry->phys = cache->phys;
261 	entry->len = cache->len;
262 	entry->flags = cache->flags;
263 	cache->entries_pos++;
264 	cache->extents_mapped++;
265 
266 	if (cache->extents_mapped == fieinfo->fi_extents_max) {
267 		cache->cached = false;
268 		return 1;
269 	}
270 assign:
271 	cache->cached = true;
272 	cache->offset = offset;
273 	cache->phys = phys;
274 	cache->len = len;
275 	cache->flags = flags;
276 
277 	return 0;
278 }
279 
280 /*
281  * Emit last fiemap cache
282  *
283  * The last fiemap cache may still be cached in the following case:
284  * 0		      4k		    8k
285  * |<- Fiemap range ->|
286  * |<------------  First extent ----------->|
287  *
288  * In this case, the first extent range will be cached but not emitted.
289  * So we must emit it before ending extent_fiemap().
290  */
291 static int emit_last_fiemap_cache(struct fiemap_extent_info *fieinfo,
292 				  struct fiemap_cache *cache)
293 {
294 	int ret;
295 
296 	if (!cache->cached)
297 		return 0;
298 
299 	ret = fiemap_fill_next_extent(fieinfo, cache->offset, cache->phys,
300 				      cache->len, cache->flags);
301 	cache->cached = false;
302 	if (ret > 0)
303 		ret = 0;
304 	return ret;
305 }
306 
307 static int fiemap_next_leaf_item(struct btrfs_inode *inode, struct btrfs_path *path)
308 {
309 	struct extent_buffer *clone = path->nodes[0];
310 	struct btrfs_key key;
311 	int slot;
312 	int ret;
313 
314 	path->slots[0]++;
315 	if (path->slots[0] < btrfs_header_nritems(path->nodes[0]))
316 		return 0;
317 
318 	/*
319 	 * Add a temporary extra ref to an already cloned extent buffer to
320 	 * prevent btrfs_next_leaf() freeing it, we want to reuse it to avoid
321 	 * the cost of allocating a new one.
322 	 */
323 	ASSERT(test_bit(EXTENT_BUFFER_UNMAPPED, &clone->bflags));
324 	refcount_inc(&clone->refs);
325 
326 	ret = btrfs_next_leaf(inode->root, path);
327 	if (ret != 0)
328 		goto out;
329 
330 	/*
331 	 * Don't bother with cloning if there are no more file extent items for
332 	 * our inode.
333 	 */
334 	btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
335 	if (key.objectid != btrfs_ino(inode) || key.type != BTRFS_EXTENT_DATA_KEY) {
336 		ret = 1;
337 		goto out;
338 	}
339 
340 	/*
341 	 * Important to preserve the start field, for the optimizations when
342 	 * checking if extents are shared (see extent_fiemap()).
343 	 *
344 	 * We must set ->start before calling copy_extent_buffer_full().  If we
345 	 * are on sub-pagesize blocksize, we use ->start to determine the offset
346 	 * into the folio where our eb exists, and if we update ->start after
347 	 * the fact then any subsequent reads of the eb may read from a
348 	 * different offset in the folio than where we originally copied into.
349 	 */
350 	clone->start = path->nodes[0]->start;
351 	/* See the comment at fiemap_search_slot() about why we clone. */
352 	copy_extent_buffer_full(clone, path->nodes[0]);
353 
354 	slot = path->slots[0];
355 	btrfs_release_path(path);
356 	path->nodes[0] = clone;
357 	path->slots[0] = slot;
358 out:
359 	if (ret)
360 		free_extent_buffer(clone);
361 
362 	return ret;
363 }
364 
365 /*
366  * Search for the first file extent item that starts at a given file offset or
367  * the one that starts immediately before that offset.
368  * Returns: 0 on success, < 0 on error, 1 if not found.
369  */
370 static int fiemap_search_slot(struct btrfs_inode *inode, struct btrfs_path *path,
371 			      u64 file_offset)
372 {
373 	const u64 ino = btrfs_ino(inode);
374 	struct btrfs_root *root = inode->root;
375 	struct extent_buffer *clone;
376 	struct btrfs_key key;
377 	int slot;
378 	int ret;
379 
380 	key.objectid = ino;
381 	key.type = BTRFS_EXTENT_DATA_KEY;
382 	key.offset = file_offset;
383 
384 	ret = btrfs_search_slot(NULL, root, &key, path, 0, 0);
385 	if (ret < 0)
386 		return ret;
387 
388 	if (ret > 0 && path->slots[0] > 0) {
389 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0] - 1);
390 		if (key.objectid == ino && key.type == BTRFS_EXTENT_DATA_KEY)
391 			path->slots[0]--;
392 	}
393 
394 	if (path->slots[0] >= btrfs_header_nritems(path->nodes[0])) {
395 		ret = btrfs_next_leaf(root, path);
396 		if (ret != 0)
397 			return ret;
398 
399 		btrfs_item_key_to_cpu(path->nodes[0], &key, path->slots[0]);
400 		if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
401 			return 1;
402 	}
403 
404 	/*
405 	 * We clone the leaf and use it during fiemap. This is because while
406 	 * using the leaf we do expensive things like checking if an extent is
407 	 * shared, which can take a long time. In order to prevent blocking
408 	 * other tasks for too long, we use a clone of the leaf. We have locked
409 	 * the file range in the inode's io tree, so we know none of our file
410 	 * extent items can change. This way we avoid blocking other tasks that
411 	 * want to insert items for other inodes in the same leaf or b+tree
412 	 * rebalance operations (triggered for example when someone is trying
413 	 * to push items into this leaf when trying to insert an item in a
414 	 * neighbour leaf).
415 	 * We also need the private clone because holding a read lock on an
416 	 * extent buffer of the subvolume's b+tree will make lockdep unhappy
417 	 * when we check if extents are shared, as backref walking may need to
418 	 * lock the same leaf we are processing.
419 	 */
420 	clone = btrfs_clone_extent_buffer(path->nodes[0]);
421 	if (!clone)
422 		return -ENOMEM;
423 
424 	slot = path->slots[0];
425 	btrfs_release_path(path);
426 	path->nodes[0] = clone;
427 	path->slots[0] = slot;
428 
429 	return 0;
430 }
431 
432 /*
433  * Process a range which is a hole or a prealloc extent in the inode's subvolume
434  * btree. If @disk_bytenr is 0, we are dealing with a hole, otherwise a prealloc
435  * extent. The end offset (@end) is inclusive.
436  */
437 static int fiemap_process_hole(struct btrfs_inode *inode,
438 			       struct fiemap_extent_info *fieinfo,
439 			       struct fiemap_cache *cache,
440 			       struct extent_state **delalloc_cached_state,
441 			       struct btrfs_backref_share_check_ctx *backref_ctx,
442 			       u64 disk_bytenr, u64 extent_offset,
443 			       u64 extent_gen,
444 			       u64 start, u64 end)
445 {
446 	const u64 i_size = i_size_read(&inode->vfs_inode);
447 	u64 cur_offset = start;
448 	u64 last_delalloc_end = 0;
449 	u32 prealloc_flags = FIEMAP_EXTENT_UNWRITTEN;
450 	bool checked_extent_shared = false;
451 	int ret;
452 
453 	/*
454 	 * There can be no delalloc past i_size, so don't waste time looking for
455 	 * it beyond i_size.
456 	 */
457 	while (cur_offset < end && cur_offset < i_size) {
458 		u64 delalloc_start;
459 		u64 delalloc_end;
460 		u64 prealloc_start;
461 		u64 prealloc_len = 0;
462 		bool delalloc;
463 
464 		delalloc = btrfs_find_delalloc_in_range(inode, cur_offset, end,
465 							delalloc_cached_state,
466 							&delalloc_start,
467 							&delalloc_end);
468 		if (!delalloc)
469 			break;
470 
471 		/*
472 		 * If this is a prealloc extent we have to report every section
473 		 * of it that has no delalloc.
474 		 */
475 		if (disk_bytenr != 0) {
476 			if (last_delalloc_end == 0) {
477 				prealloc_start = start;
478 				prealloc_len = delalloc_start - start;
479 			} else {
480 				prealloc_start = last_delalloc_end + 1;
481 				prealloc_len = delalloc_start - prealloc_start;
482 			}
483 		}
484 
485 		if (prealloc_len > 0) {
486 			if (!checked_extent_shared && fieinfo->fi_extents_max) {
487 				ret = btrfs_is_data_extent_shared(inode,
488 								  disk_bytenr,
489 								  extent_gen,
490 								  backref_ctx);
491 				if (ret < 0)
492 					return ret;
493 				else if (ret > 0)
494 					prealloc_flags |= FIEMAP_EXTENT_SHARED;
495 
496 				checked_extent_shared = true;
497 			}
498 			ret = emit_fiemap_extent(fieinfo, cache, prealloc_start,
499 						 disk_bytenr + extent_offset,
500 						 prealloc_len, prealloc_flags);
501 			if (ret)
502 				return ret;
503 			extent_offset += prealloc_len;
504 		}
505 
506 		ret = emit_fiemap_extent(fieinfo, cache, delalloc_start, 0,
507 					 delalloc_end + 1 - delalloc_start,
508 					 FIEMAP_EXTENT_DELALLOC |
509 					 FIEMAP_EXTENT_UNKNOWN);
510 		if (ret)
511 			return ret;
512 
513 		last_delalloc_end = delalloc_end;
514 		cur_offset = delalloc_end + 1;
515 		extent_offset += cur_offset - delalloc_start;
516 		cond_resched();
517 	}
518 
519 	/*
520 	 * Either we found no delalloc for the whole prealloc extent or we have
521 	 * a prealloc extent that spans i_size or starts at or after i_size.
522 	 */
523 	if (disk_bytenr != 0 && last_delalloc_end < end) {
524 		u64 prealloc_start;
525 		u64 prealloc_len;
526 
527 		if (last_delalloc_end == 0) {
528 			prealloc_start = start;
529 			prealloc_len = end + 1 - start;
530 		} else {
531 			prealloc_start = last_delalloc_end + 1;
532 			prealloc_len = end + 1 - prealloc_start;
533 		}
534 
535 		if (!checked_extent_shared && fieinfo->fi_extents_max) {
536 			ret = btrfs_is_data_extent_shared(inode,
537 							  disk_bytenr,
538 							  extent_gen,
539 							  backref_ctx);
540 			if (ret < 0)
541 				return ret;
542 			else if (ret > 0)
543 				prealloc_flags |= FIEMAP_EXTENT_SHARED;
544 		}
545 		ret = emit_fiemap_extent(fieinfo, cache, prealloc_start,
546 					 disk_bytenr + extent_offset,
547 					 prealloc_len, prealloc_flags);
548 		if (ret)
549 			return ret;
550 	}
551 
552 	return 0;
553 }
554 
555 static int fiemap_find_last_extent_offset(struct btrfs_inode *inode,
556 					  struct btrfs_path *path,
557 					  u64 *last_extent_end_ret)
558 {
559 	const u64 ino = btrfs_ino(inode);
560 	struct btrfs_root *root = inode->root;
561 	struct extent_buffer *leaf;
562 	struct btrfs_file_extent_item *ei;
563 	struct btrfs_key key;
564 	u64 disk_bytenr;
565 	int ret;
566 
567 	/*
568 	 * Lookup the last file extent. We're not using i_size here because
569 	 * there might be preallocation past i_size.
570 	 */
571 	ret = btrfs_lookup_file_extent(NULL, root, path, ino, (u64)-1, 0);
572 	/* There can't be a file extent item at offset (u64)-1 */
573 	ASSERT(ret != 0);
574 	if (ret < 0)
575 		return ret;
576 
577 	/*
578 	 * For a non-existing key, btrfs_search_slot() always leaves us at a
579 	 * slot > 0, except if the btree is empty, which is impossible because
580 	 * at least it has the inode item for this inode and all the items for
581 	 * the root inode 256.
582 	 */
583 	ASSERT(path->slots[0] > 0);
584 	path->slots[0]--;
585 	leaf = path->nodes[0];
586 	btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
587 	if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY) {
588 		/* No file extent items in the subvolume tree. */
589 		*last_extent_end_ret = 0;
590 		return 0;
591 	}
592 
593 	/*
594 	 * For an inline extent, the disk_bytenr is where inline data starts at,
595 	 * so first check if we have an inline extent item before checking if we
596 	 * have an implicit hole (disk_bytenr == 0).
597 	 */
598 	ei = btrfs_item_ptr(leaf, path->slots[0], struct btrfs_file_extent_item);
599 	if (btrfs_file_extent_type(leaf, ei) == BTRFS_FILE_EXTENT_INLINE) {
600 		*last_extent_end_ret = btrfs_file_extent_end(path);
601 		return 0;
602 	}
603 
604 	/*
605 	 * Find the last file extent item that is not a hole (when NO_HOLES is
606 	 * not enabled). This should take at most 2 iterations in the worst
607 	 * case: we have one hole file extent item at slot 0 of a leaf and
608 	 * another hole file extent item as the last item in the previous leaf.
609 	 * This is because we merge file extent items that represent holes.
610 	 */
611 	disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
612 	while (disk_bytenr == 0) {
613 		ret = btrfs_previous_item(root, path, ino, BTRFS_EXTENT_DATA_KEY);
614 		if (ret < 0) {
615 			return ret;
616 		} else if (ret > 0) {
617 			/* No file extent items that are not holes. */
618 			*last_extent_end_ret = 0;
619 			return 0;
620 		}
621 		leaf = path->nodes[0];
622 		ei = btrfs_item_ptr(leaf, path->slots[0],
623 				    struct btrfs_file_extent_item);
624 		disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
625 	}
626 
627 	*last_extent_end_ret = btrfs_file_extent_end(path);
628 	return 0;
629 }
630 
631 static int extent_fiemap(struct btrfs_inode *inode,
632 			 struct fiemap_extent_info *fieinfo,
633 			 u64 start, u64 len)
634 {
635 	const u64 ino = btrfs_ino(inode);
636 	struct extent_state *cached_state = NULL;
637 	struct extent_state *delalloc_cached_state = NULL;
638 	BTRFS_PATH_AUTO_FREE(path);
639 	struct fiemap_cache cache = { 0 };
640 	struct btrfs_backref_share_check_ctx *backref_ctx;
641 	u64 last_extent_end = 0;
642 	u64 prev_extent_end;
643 	u64 range_start;
644 	u64 range_end;
645 	const u32 sectorsize = inode->root->fs_info->sectorsize;
646 	bool stopped = false;
647 	int ret;
648 
649 	cache.entries_size = PAGE_SIZE / sizeof(struct btrfs_fiemap_entry);
650 	cache.entries = kmalloc_objs(struct btrfs_fiemap_entry,
651 				     cache.entries_size);
652 	backref_ctx = btrfs_alloc_backref_share_check_ctx();
653 	path = btrfs_alloc_path();
654 	if (!cache.entries || !backref_ctx || !path) {
655 		ret = -ENOMEM;
656 		goto out;
657 	}
658 
659 restart:
660 	range_start = round_down(start, sectorsize);
661 	range_end = round_up(start + len, sectorsize);
662 	prev_extent_end = range_start;
663 
664 	btrfs_lock_extent(&inode->io_tree, range_start, range_end - 1, &cached_state);
665 
666 	ret = fiemap_find_last_extent_offset(inode, path, &last_extent_end);
667 	if (ret < 0)
668 		goto out_unlock;
669 	btrfs_release_path(path);
670 
671 	path->reada = READA_FORWARD;
672 	ret = fiemap_search_slot(inode, path, range_start);
673 	if (ret < 0) {
674 		goto out_unlock;
675 	} else if (ret > 0) {
676 		/*
677 		 * No file extent item found, but we may have delalloc between
678 		 * the current offset and i_size. So check for that.
679 		 */
680 		ret = 0;
681 		goto check_eof_delalloc;
682 	}
683 
684 	while (prev_extent_end < range_end) {
685 		struct extent_buffer *leaf = path->nodes[0];
686 		struct btrfs_file_extent_item *ei;
687 		struct btrfs_key key;
688 		u64 extent_end;
689 		u64 extent_len;
690 		u64 extent_offset = 0;
691 		u64 extent_gen;
692 		u64 disk_bytenr = 0;
693 		u64 flags = 0;
694 		int extent_type;
695 		u8 compression;
696 
697 		btrfs_item_key_to_cpu(leaf, &key, path->slots[0]);
698 		if (key.objectid != ino || key.type != BTRFS_EXTENT_DATA_KEY)
699 			break;
700 
701 		extent_end = btrfs_file_extent_end(path);
702 
703 		/*
704 		 * The first iteration can leave us at an extent item that ends
705 		 * before our range's start. Move to the next item.
706 		 */
707 		if (extent_end <= range_start)
708 			goto next_item;
709 
710 		backref_ctx->curr_leaf_bytenr = leaf->start;
711 
712 		/* We have in implicit hole (NO_HOLES feature enabled). */
713 		if (prev_extent_end < key.offset) {
714 			const u64 hole_end = min(key.offset, range_end) - 1;
715 
716 			ret = fiemap_process_hole(inode, fieinfo, &cache,
717 						  &delalloc_cached_state,
718 						  backref_ctx, 0, 0, 0,
719 						  prev_extent_end, hole_end);
720 			if (ret < 0) {
721 				goto out_unlock;
722 			} else if (ret > 0) {
723 				/* fiemap_fill_next_extent() told us to stop. */
724 				stopped = true;
725 				break;
726 			}
727 
728 			/* We've reached the end of the fiemap range, stop. */
729 			if (key.offset >= range_end) {
730 				stopped = true;
731 				break;
732 			}
733 		}
734 
735 		extent_len = extent_end - key.offset;
736 		ei = btrfs_item_ptr(leaf, path->slots[0],
737 				    struct btrfs_file_extent_item);
738 		compression = btrfs_file_extent_compression(leaf, ei);
739 		extent_type = btrfs_file_extent_type(leaf, ei);
740 		extent_gen = btrfs_file_extent_generation(leaf, ei);
741 
742 		if (extent_type != BTRFS_FILE_EXTENT_INLINE) {
743 			disk_bytenr = btrfs_file_extent_disk_bytenr(leaf, ei);
744 			if (compression == BTRFS_COMPRESS_NONE)
745 				extent_offset = btrfs_file_extent_offset(leaf, ei);
746 		}
747 
748 		if (compression != BTRFS_COMPRESS_NONE)
749 			flags |= FIEMAP_EXTENT_ENCODED;
750 
751 		if (extent_type == BTRFS_FILE_EXTENT_INLINE) {
752 			flags |= FIEMAP_EXTENT_DATA_INLINE;
753 			flags |= FIEMAP_EXTENT_NOT_ALIGNED;
754 			ret = emit_fiemap_extent(fieinfo, &cache, key.offset, 0,
755 						 extent_len, flags);
756 		} else if (extent_type == BTRFS_FILE_EXTENT_PREALLOC) {
757 			ret = fiemap_process_hole(inode, fieinfo, &cache,
758 						  &delalloc_cached_state,
759 						  backref_ctx,
760 						  disk_bytenr, extent_offset,
761 						  extent_gen, key.offset,
762 						  extent_end - 1);
763 		} else if (disk_bytenr == 0) {
764 			/* We have an explicit hole. */
765 			ret = fiemap_process_hole(inode, fieinfo, &cache,
766 						  &delalloc_cached_state,
767 						  backref_ctx, 0, 0, 0,
768 						  key.offset, extent_end - 1);
769 		} else {
770 			/* We have a regular extent. */
771 			if (fieinfo->fi_extents_max) {
772 				ret = btrfs_is_data_extent_shared(inode,
773 								  disk_bytenr,
774 								  extent_gen,
775 								  backref_ctx);
776 				if (ret < 0)
777 					goto out_unlock;
778 				else if (ret > 0)
779 					flags |= FIEMAP_EXTENT_SHARED;
780 			}
781 
782 			ret = emit_fiemap_extent(fieinfo, &cache, key.offset,
783 						 disk_bytenr + extent_offset,
784 						 extent_len, flags);
785 		}
786 
787 		if (ret < 0) {
788 			goto out_unlock;
789 		} else if (ret > 0) {
790 			/* emit_fiemap_extent() told us to stop. */
791 			stopped = true;
792 			break;
793 		}
794 
795 		prev_extent_end = extent_end;
796 next_item:
797 		if (fatal_signal_pending(current)) {
798 			ret = -EINTR;
799 			goto out_unlock;
800 		}
801 
802 		ret = fiemap_next_leaf_item(inode, path);
803 		if (ret < 0) {
804 			goto out_unlock;
805 		} else if (ret > 0) {
806 			/* No more file extent items for this inode. */
807 			break;
808 		}
809 		cond_resched();
810 	}
811 
812 check_eof_delalloc:
813 	if (!stopped && prev_extent_end < range_end) {
814 		ret = fiemap_process_hole(inode, fieinfo, &cache,
815 					  &delalloc_cached_state, backref_ctx,
816 					  0, 0, 0, prev_extent_end, range_end - 1);
817 		if (ret < 0)
818 			goto out_unlock;
819 		prev_extent_end = range_end;
820 	}
821 
822 	if (cache.cached && cache.offset + cache.len >= last_extent_end) {
823 		const u64 i_size = i_size_read(&inode->vfs_inode);
824 
825 		if (prev_extent_end < i_size) {
826 			u64 delalloc_start;
827 			u64 delalloc_end;
828 			bool delalloc;
829 
830 			delalloc = btrfs_find_delalloc_in_range(inode,
831 								prev_extent_end,
832 								i_size - 1,
833 								&delalloc_cached_state,
834 								&delalloc_start,
835 								&delalloc_end);
836 			if (!delalloc)
837 				cache.flags |= FIEMAP_EXTENT_LAST;
838 		} else {
839 			cache.flags |= FIEMAP_EXTENT_LAST;
840 		}
841 	}
842 
843 out_unlock:
844 	btrfs_unlock_extent(&inode->io_tree, range_start, range_end - 1, &cached_state);
845 
846 	if (ret == BTRFS_FIEMAP_FLUSH_CACHE) {
847 		btrfs_release_path(path);
848 		ret = flush_fiemap_cache(fieinfo, &cache);
849 		if (ret)
850 			goto out;
851 		len -= cache.next_search_offset - start;
852 		start = cache.next_search_offset;
853 		goto restart;
854 	} else if (ret < 0) {
855 		goto out;
856 	}
857 
858 	/*
859 	 * Must free the path before emitting to the fiemap buffer because we
860 	 * may have a non-cloned leaf and if the fiemap buffer is memory mapped
861 	 * to a file, a write into it (through btrfs_page_mkwrite()) may trigger
862 	 * waiting for an ordered extent that in order to complete needs to
863 	 * modify that leaf, therefore leading to a deadlock.
864 	 */
865 	btrfs_free_path(path);
866 	path = NULL;
867 
868 	ret = flush_fiemap_cache(fieinfo, &cache);
869 	if (ret)
870 		goto out;
871 
872 	ret = emit_last_fiemap_cache(fieinfo, &cache);
873 out:
874 	btrfs_free_extent_state(delalloc_cached_state);
875 	kfree(cache.entries);
876 	btrfs_free_backref_share_ctx(backref_ctx);
877 	return ret;
878 }
879 
880 int btrfs_fiemap(struct inode *inode, struct fiemap_extent_info *fieinfo,
881 		 u64 start, u64 len)
882 {
883 	struct btrfs_inode *btrfs_inode = BTRFS_I(inode);
884 	int ret;
885 
886 	ret = fiemap_prep(inode, fieinfo, start, &len, 0);
887 	if (ret)
888 		return ret;
889 
890 	/*
891 	 * fiemap_prep() called filemap_write_and_wait() for the whole possible
892 	 * file range (0 to LLONG_MAX), but that is not enough if we have
893 	 * compression enabled. The first filemap_fdatawrite_range() only kicks
894 	 * in the compression of data (in an async thread) and will return
895 	 * before the compression is done and writeback is started. A second
896 	 * filemap_fdatawrite_range() is needed to wait for the compression to
897 	 * complete and writeback to start. We also need to wait for ordered
898 	 * extents to complete, because our fiemap implementation uses mainly
899 	 * file extent items to list the extents, searching for extent maps
900 	 * only for file ranges with holes or prealloc extents to figure out
901 	 * if we have delalloc in those ranges.
902 	 */
903 	if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) {
904 		ret = btrfs_wait_ordered_range(btrfs_inode, 0, LLONG_MAX);
905 		if (ret)
906 			return ret;
907 	}
908 
909 	btrfs_inode_lock(btrfs_inode, BTRFS_ILOCK_SHARED);
910 
911 	/*
912 	 * We did an initial flush to avoid holding the inode's lock while
913 	 * triggering writeback and waiting for the completion of IO and ordered
914 	 * extents. Now after we locked the inode we do it again, because it's
915 	 * possible a new write may have happened in between those two steps.
916 	 */
917 	if (fieinfo->fi_flags & FIEMAP_FLAG_SYNC) {
918 		ret = btrfs_wait_ordered_range(btrfs_inode, 0, LLONG_MAX);
919 		if (ret) {
920 			btrfs_inode_unlock(btrfs_inode, BTRFS_ILOCK_SHARED);
921 			return ret;
922 		}
923 	}
924 
925 	ret = extent_fiemap(btrfs_inode, fieinfo, start, len);
926 	btrfs_inode_unlock(btrfs_inode, BTRFS_ILOCK_SHARED);
927 
928 	return ret;
929 }
930