xref: /openbmc/linux/fs/xfs/xfs_fsmap.c (revision d0e22329)
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Copyright (C) 2017 Oracle.  All Rights Reserved.
4  * Author: Darrick J. Wong <darrick.wong@oracle.com>
5  */
6 #include "xfs.h"
7 #include "xfs_fs.h"
8 #include "xfs_shared.h"
9 #include "xfs_format.h"
10 #include "xfs_log_format.h"
11 #include "xfs_trans_resv.h"
12 #include "xfs_sb.h"
13 #include "xfs_mount.h"
14 #include "xfs_defer.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_error.h"
18 #include "xfs_btree.h"
19 #include "xfs_rmap_btree.h"
20 #include "xfs_trace.h"
21 #include "xfs_log.h"
22 #include "xfs_rmap.h"
23 #include "xfs_alloc.h"
24 #include "xfs_bit.h"
25 #include <linux/fsmap.h>
26 #include "xfs_fsmap.h"
27 #include "xfs_refcount.h"
28 #include "xfs_refcount_btree.h"
29 #include "xfs_alloc_btree.h"
30 #include "xfs_rtalloc.h"
31 
32 /* Convert an xfs_fsmap to an fsmap. */
33 void
34 xfs_fsmap_from_internal(
35 	struct fsmap		*dest,
36 	struct xfs_fsmap	*src)
37 {
38 	dest->fmr_device = src->fmr_device;
39 	dest->fmr_flags = src->fmr_flags;
40 	dest->fmr_physical = BBTOB(src->fmr_physical);
41 	dest->fmr_owner = src->fmr_owner;
42 	dest->fmr_offset = BBTOB(src->fmr_offset);
43 	dest->fmr_length = BBTOB(src->fmr_length);
44 	dest->fmr_reserved[0] = 0;
45 	dest->fmr_reserved[1] = 0;
46 	dest->fmr_reserved[2] = 0;
47 }
48 
49 /* Convert an fsmap to an xfs_fsmap. */
50 void
51 xfs_fsmap_to_internal(
52 	struct xfs_fsmap	*dest,
53 	struct fsmap		*src)
54 {
55 	dest->fmr_device = src->fmr_device;
56 	dest->fmr_flags = src->fmr_flags;
57 	dest->fmr_physical = BTOBBT(src->fmr_physical);
58 	dest->fmr_owner = src->fmr_owner;
59 	dest->fmr_offset = BTOBBT(src->fmr_offset);
60 	dest->fmr_length = BTOBBT(src->fmr_length);
61 }
62 
63 /* Convert an fsmap owner into an rmapbt owner. */
64 static int
65 xfs_fsmap_owner_to_rmap(
66 	struct xfs_rmap_irec	*dest,
67 	struct xfs_fsmap	*src)
68 {
69 	if (!(src->fmr_flags & FMR_OF_SPECIAL_OWNER)) {
70 		dest->rm_owner = src->fmr_owner;
71 		return 0;
72 	}
73 
74 	switch (src->fmr_owner) {
75 	case 0:			/* "lowest owner id possible" */
76 	case -1ULL:		/* "highest owner id possible" */
77 		dest->rm_owner = 0;
78 		break;
79 	case XFS_FMR_OWN_FREE:
80 		dest->rm_owner = XFS_RMAP_OWN_NULL;
81 		break;
82 	case XFS_FMR_OWN_UNKNOWN:
83 		dest->rm_owner = XFS_RMAP_OWN_UNKNOWN;
84 		break;
85 	case XFS_FMR_OWN_FS:
86 		dest->rm_owner = XFS_RMAP_OWN_FS;
87 		break;
88 	case XFS_FMR_OWN_LOG:
89 		dest->rm_owner = XFS_RMAP_OWN_LOG;
90 		break;
91 	case XFS_FMR_OWN_AG:
92 		dest->rm_owner = XFS_RMAP_OWN_AG;
93 		break;
94 	case XFS_FMR_OWN_INOBT:
95 		dest->rm_owner = XFS_RMAP_OWN_INOBT;
96 		break;
97 	case XFS_FMR_OWN_INODES:
98 		dest->rm_owner = XFS_RMAP_OWN_INODES;
99 		break;
100 	case XFS_FMR_OWN_REFC:
101 		dest->rm_owner = XFS_RMAP_OWN_REFC;
102 		break;
103 	case XFS_FMR_OWN_COW:
104 		dest->rm_owner = XFS_RMAP_OWN_COW;
105 		break;
106 	case XFS_FMR_OWN_DEFECTIVE:	/* not implemented */
107 		/* fall through */
108 	default:
109 		return -EINVAL;
110 	}
111 	return 0;
112 }
113 
114 /* Convert an rmapbt owner into an fsmap owner. */
115 static int
116 xfs_fsmap_owner_from_rmap(
117 	struct xfs_fsmap	*dest,
118 	struct xfs_rmap_irec	*src)
119 {
120 	dest->fmr_flags = 0;
121 	if (!XFS_RMAP_NON_INODE_OWNER(src->rm_owner)) {
122 		dest->fmr_owner = src->rm_owner;
123 		return 0;
124 	}
125 	dest->fmr_flags |= FMR_OF_SPECIAL_OWNER;
126 
127 	switch (src->rm_owner) {
128 	case XFS_RMAP_OWN_FS:
129 		dest->fmr_owner = XFS_FMR_OWN_FS;
130 		break;
131 	case XFS_RMAP_OWN_LOG:
132 		dest->fmr_owner = XFS_FMR_OWN_LOG;
133 		break;
134 	case XFS_RMAP_OWN_AG:
135 		dest->fmr_owner = XFS_FMR_OWN_AG;
136 		break;
137 	case XFS_RMAP_OWN_INOBT:
138 		dest->fmr_owner = XFS_FMR_OWN_INOBT;
139 		break;
140 	case XFS_RMAP_OWN_INODES:
141 		dest->fmr_owner = XFS_FMR_OWN_INODES;
142 		break;
143 	case XFS_RMAP_OWN_REFC:
144 		dest->fmr_owner = XFS_FMR_OWN_REFC;
145 		break;
146 	case XFS_RMAP_OWN_COW:
147 		dest->fmr_owner = XFS_FMR_OWN_COW;
148 		break;
149 	case XFS_RMAP_OWN_NULL:	/* "free" */
150 		dest->fmr_owner = XFS_FMR_OWN_FREE;
151 		break;
152 	default:
153 		return -EFSCORRUPTED;
154 	}
155 	return 0;
156 }
157 
158 /* getfsmap query state */
159 struct xfs_getfsmap_info {
160 	struct xfs_fsmap_head	*head;
161 	xfs_fsmap_format_t	formatter;	/* formatting fn */
162 	void			*format_arg;	/* format buffer */
163 	struct xfs_buf		*agf_bp;	/* AGF, for refcount queries */
164 	xfs_daddr_t		next_daddr;	/* next daddr we expect */
165 	u64			missing_owner;	/* owner of holes */
166 	u32			dev;		/* device id */
167 	xfs_agnumber_t		agno;		/* AG number, if applicable */
168 	struct xfs_rmap_irec	low;		/* low rmap key */
169 	struct xfs_rmap_irec	high;		/* high rmap key */
170 	bool			last;		/* last extent? */
171 };
172 
173 /* Associate a device with a getfsmap handler. */
174 struct xfs_getfsmap_dev {
175 	u32			dev;
176 	int			(*fn)(struct xfs_trans *tp,
177 				      struct xfs_fsmap *keys,
178 				      struct xfs_getfsmap_info *info);
179 };
180 
181 /* Compare two getfsmap device handlers. */
182 static int
183 xfs_getfsmap_dev_compare(
184 	const void			*p1,
185 	const void			*p2)
186 {
187 	const struct xfs_getfsmap_dev	*d1 = p1;
188 	const struct xfs_getfsmap_dev	*d2 = p2;
189 
190 	return d1->dev - d2->dev;
191 }
192 
193 /* Decide if this mapping is shared. */
194 STATIC int
195 xfs_getfsmap_is_shared(
196 	struct xfs_trans		*tp,
197 	struct xfs_getfsmap_info	*info,
198 	struct xfs_rmap_irec		*rec,
199 	bool				*stat)
200 {
201 	struct xfs_mount		*mp = tp->t_mountp;
202 	struct xfs_btree_cur		*cur;
203 	xfs_agblock_t			fbno;
204 	xfs_extlen_t			flen;
205 	int				error;
206 
207 	*stat = false;
208 	if (!xfs_sb_version_hasreflink(&mp->m_sb))
209 		return 0;
210 	/* rt files will have agno set to NULLAGNUMBER */
211 	if (info->agno == NULLAGNUMBER)
212 		return 0;
213 
214 	/* Are there any shared blocks here? */
215 	flen = 0;
216 	cur = xfs_refcountbt_init_cursor(mp, tp, info->agf_bp,
217 			info->agno);
218 
219 	error = xfs_refcount_find_shared(cur, rec->rm_startblock,
220 			rec->rm_blockcount, &fbno, &flen, false);
221 
222 	xfs_btree_del_cursor(cur, error);
223 	if (error)
224 		return error;
225 
226 	*stat = flen > 0;
227 	return 0;
228 }
229 
230 /*
231  * Format a reverse mapping for getfsmap, having translated rm_startblock
232  * into the appropriate daddr units.
233  */
234 STATIC int
235 xfs_getfsmap_helper(
236 	struct xfs_trans		*tp,
237 	struct xfs_getfsmap_info	*info,
238 	struct xfs_rmap_irec		*rec,
239 	xfs_daddr_t			rec_daddr)
240 {
241 	struct xfs_fsmap		fmr;
242 	struct xfs_mount		*mp = tp->t_mountp;
243 	bool				shared;
244 	int				error;
245 
246 	if (fatal_signal_pending(current))
247 		return -EINTR;
248 
249 	/*
250 	 * Filter out records that start before our startpoint, if the
251 	 * caller requested that.
252 	 */
253 	if (xfs_rmap_compare(rec, &info->low) < 0) {
254 		rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
255 		if (info->next_daddr < rec_daddr)
256 			info->next_daddr = rec_daddr;
257 		return XFS_BTREE_QUERY_RANGE_CONTINUE;
258 	}
259 
260 	/* Are we just counting mappings? */
261 	if (info->head->fmh_count == 0) {
262 		if (rec_daddr > info->next_daddr)
263 			info->head->fmh_entries++;
264 
265 		if (info->last)
266 			return XFS_BTREE_QUERY_RANGE_CONTINUE;
267 
268 		info->head->fmh_entries++;
269 
270 		rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
271 		if (info->next_daddr < rec_daddr)
272 			info->next_daddr = rec_daddr;
273 		return XFS_BTREE_QUERY_RANGE_CONTINUE;
274 	}
275 
276 	/*
277 	 * If the record starts past the last physical block we saw,
278 	 * then we've found a gap.  Report the gap as being owned by
279 	 * whatever the caller specified is the missing owner.
280 	 */
281 	if (rec_daddr > info->next_daddr) {
282 		if (info->head->fmh_entries >= info->head->fmh_count)
283 			return XFS_BTREE_QUERY_RANGE_ABORT;
284 
285 		fmr.fmr_device = info->dev;
286 		fmr.fmr_physical = info->next_daddr;
287 		fmr.fmr_owner = info->missing_owner;
288 		fmr.fmr_offset = 0;
289 		fmr.fmr_length = rec_daddr - info->next_daddr;
290 		fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
291 		error = info->formatter(&fmr, info->format_arg);
292 		if (error)
293 			return error;
294 		info->head->fmh_entries++;
295 	}
296 
297 	if (info->last)
298 		goto out;
299 
300 	/* Fill out the extent we found */
301 	if (info->head->fmh_entries >= info->head->fmh_count)
302 		return XFS_BTREE_QUERY_RANGE_ABORT;
303 
304 	trace_xfs_fsmap_mapping(mp, info->dev, info->agno, rec);
305 
306 	fmr.fmr_device = info->dev;
307 	fmr.fmr_physical = rec_daddr;
308 	error = xfs_fsmap_owner_from_rmap(&fmr, rec);
309 	if (error)
310 		return error;
311 	fmr.fmr_offset = XFS_FSB_TO_BB(mp, rec->rm_offset);
312 	fmr.fmr_length = XFS_FSB_TO_BB(mp, rec->rm_blockcount);
313 	if (rec->rm_flags & XFS_RMAP_UNWRITTEN)
314 		fmr.fmr_flags |= FMR_OF_PREALLOC;
315 	if (rec->rm_flags & XFS_RMAP_ATTR_FORK)
316 		fmr.fmr_flags |= FMR_OF_ATTR_FORK;
317 	if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
318 		fmr.fmr_flags |= FMR_OF_EXTENT_MAP;
319 	if (fmr.fmr_flags == 0) {
320 		error = xfs_getfsmap_is_shared(tp, info, rec, &shared);
321 		if (error)
322 			return error;
323 		if (shared)
324 			fmr.fmr_flags |= FMR_OF_SHARED;
325 	}
326 	error = info->formatter(&fmr, info->format_arg);
327 	if (error)
328 		return error;
329 	info->head->fmh_entries++;
330 
331 out:
332 	rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
333 	if (info->next_daddr < rec_daddr)
334 		info->next_daddr = rec_daddr;
335 	return XFS_BTREE_QUERY_RANGE_CONTINUE;
336 }
337 
338 /* Transform a rmapbt irec into a fsmap */
339 STATIC int
340 xfs_getfsmap_datadev_helper(
341 	struct xfs_btree_cur		*cur,
342 	struct xfs_rmap_irec		*rec,
343 	void				*priv)
344 {
345 	struct xfs_mount		*mp = cur->bc_mp;
346 	struct xfs_getfsmap_info	*info = priv;
347 	xfs_fsblock_t			fsb;
348 	xfs_daddr_t			rec_daddr;
349 
350 	fsb = XFS_AGB_TO_FSB(mp, cur->bc_private.a.agno, rec->rm_startblock);
351 	rec_daddr = XFS_FSB_TO_DADDR(mp, fsb);
352 
353 	return xfs_getfsmap_helper(cur->bc_tp, info, rec, rec_daddr);
354 }
355 
356 /* Transform a bnobt irec into a fsmap */
357 STATIC int
358 xfs_getfsmap_datadev_bnobt_helper(
359 	struct xfs_btree_cur		*cur,
360 	struct xfs_alloc_rec_incore	*rec,
361 	void				*priv)
362 {
363 	struct xfs_mount		*mp = cur->bc_mp;
364 	struct xfs_getfsmap_info	*info = priv;
365 	struct xfs_rmap_irec		irec;
366 	xfs_daddr_t			rec_daddr;
367 
368 	rec_daddr = XFS_AGB_TO_DADDR(mp, cur->bc_private.a.agno,
369 			rec->ar_startblock);
370 
371 	irec.rm_startblock = rec->ar_startblock;
372 	irec.rm_blockcount = rec->ar_blockcount;
373 	irec.rm_owner = XFS_RMAP_OWN_NULL;	/* "free" */
374 	irec.rm_offset = 0;
375 	irec.rm_flags = 0;
376 
377 	return xfs_getfsmap_helper(cur->bc_tp, info, &irec, rec_daddr);
378 }
379 
380 /* Set rmap flags based on the getfsmap flags */
381 static void
382 xfs_getfsmap_set_irec_flags(
383 	struct xfs_rmap_irec	*irec,
384 	struct xfs_fsmap	*fmr)
385 {
386 	irec->rm_flags = 0;
387 	if (fmr->fmr_flags & FMR_OF_ATTR_FORK)
388 		irec->rm_flags |= XFS_RMAP_ATTR_FORK;
389 	if (fmr->fmr_flags & FMR_OF_EXTENT_MAP)
390 		irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
391 	if (fmr->fmr_flags & FMR_OF_PREALLOC)
392 		irec->rm_flags |= XFS_RMAP_UNWRITTEN;
393 }
394 
395 /* Execute a getfsmap query against the log device. */
396 STATIC int
397 xfs_getfsmap_logdev(
398 	struct xfs_trans		*tp,
399 	struct xfs_fsmap		*keys,
400 	struct xfs_getfsmap_info	*info)
401 {
402 	struct xfs_mount		*mp = tp->t_mountp;
403 	struct xfs_rmap_irec		rmap;
404 	int				error;
405 
406 	/* Set up search keys */
407 	info->low.rm_startblock = XFS_BB_TO_FSBT(mp, keys[0].fmr_physical);
408 	info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
409 	error = xfs_fsmap_owner_to_rmap(&info->low, keys);
410 	if (error)
411 		return error;
412 	info->low.rm_blockcount = 0;
413 	xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
414 
415 	error = xfs_fsmap_owner_to_rmap(&info->high, keys + 1);
416 	if (error)
417 		return error;
418 	info->high.rm_startblock = -1U;
419 	info->high.rm_owner = ULLONG_MAX;
420 	info->high.rm_offset = ULLONG_MAX;
421 	info->high.rm_blockcount = 0;
422 	info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
423 	info->missing_owner = XFS_FMR_OWN_FREE;
424 
425 	trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
426 	trace_xfs_fsmap_high_key(mp, info->dev, info->agno, &info->high);
427 
428 	if (keys[0].fmr_physical > 0)
429 		return 0;
430 
431 	/* Fabricate an rmap entry for the external log device. */
432 	rmap.rm_startblock = 0;
433 	rmap.rm_blockcount = mp->m_sb.sb_logblocks;
434 	rmap.rm_owner = XFS_RMAP_OWN_LOG;
435 	rmap.rm_offset = 0;
436 	rmap.rm_flags = 0;
437 
438 	return xfs_getfsmap_helper(tp, info, &rmap, 0);
439 }
440 
441 #ifdef CONFIG_XFS_RT
442 /* Transform a rtbitmap "record" into a fsmap */
443 STATIC int
444 xfs_getfsmap_rtdev_rtbitmap_helper(
445 	struct xfs_trans		*tp,
446 	struct xfs_rtalloc_rec		*rec,
447 	void				*priv)
448 {
449 	struct xfs_mount		*mp = tp->t_mountp;
450 	struct xfs_getfsmap_info	*info = priv;
451 	struct xfs_rmap_irec		irec;
452 	xfs_daddr_t			rec_daddr;
453 
454 	irec.rm_startblock = rec->ar_startext * mp->m_sb.sb_rextsize;
455 	rec_daddr = XFS_FSB_TO_BB(mp, irec.rm_startblock);
456 	irec.rm_blockcount = rec->ar_extcount * mp->m_sb.sb_rextsize;
457 	irec.rm_owner = XFS_RMAP_OWN_NULL;	/* "free" */
458 	irec.rm_offset = 0;
459 	irec.rm_flags = 0;
460 
461 	return xfs_getfsmap_helper(tp, info, &irec, rec_daddr);
462 }
463 
464 /* Execute a getfsmap query against the realtime device. */
465 STATIC int
466 __xfs_getfsmap_rtdev(
467 	struct xfs_trans		*tp,
468 	struct xfs_fsmap		*keys,
469 	int				(*query_fn)(struct xfs_trans *,
470 						    struct xfs_getfsmap_info *),
471 	struct xfs_getfsmap_info	*info)
472 {
473 	struct xfs_mount		*mp = tp->t_mountp;
474 	xfs_fsblock_t			start_fsb;
475 	xfs_fsblock_t			end_fsb;
476 	xfs_daddr_t			eofs;
477 	int				error = 0;
478 
479 	eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
480 	if (keys[0].fmr_physical >= eofs)
481 		return 0;
482 	if (keys[1].fmr_physical >= eofs)
483 		keys[1].fmr_physical = eofs - 1;
484 	start_fsb = XFS_BB_TO_FSBT(mp, keys[0].fmr_physical);
485 	end_fsb = XFS_BB_TO_FSB(mp, keys[1].fmr_physical);
486 
487 	/* Set up search keys */
488 	info->low.rm_startblock = start_fsb;
489 	error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
490 	if (error)
491 		return error;
492 	info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
493 	info->low.rm_blockcount = 0;
494 	xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
495 
496 	info->high.rm_startblock = end_fsb;
497 	error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
498 	if (error)
499 		return error;
500 	info->high.rm_offset = XFS_BB_TO_FSBT(mp, keys[1].fmr_offset);
501 	info->high.rm_blockcount = 0;
502 	xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
503 
504 	trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
505 	trace_xfs_fsmap_high_key(mp, info->dev, info->agno, &info->high);
506 
507 	return query_fn(tp, info);
508 }
509 
510 /* Actually query the realtime bitmap. */
511 STATIC int
512 xfs_getfsmap_rtdev_rtbitmap_query(
513 	struct xfs_trans		*tp,
514 	struct xfs_getfsmap_info	*info)
515 {
516 	struct xfs_rtalloc_rec		alow = { 0 };
517 	struct xfs_rtalloc_rec		ahigh = { 0 };
518 	int				error;
519 
520 	xfs_ilock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
521 
522 	alow.ar_startext = info->low.rm_startblock;
523 	ahigh.ar_startext = info->high.rm_startblock;
524 	do_div(alow.ar_startext, tp->t_mountp->m_sb.sb_rextsize);
525 	if (do_div(ahigh.ar_startext, tp->t_mountp->m_sb.sb_rextsize))
526 		ahigh.ar_startext++;
527 	error = xfs_rtalloc_query_range(tp, &alow, &ahigh,
528 			xfs_getfsmap_rtdev_rtbitmap_helper, info);
529 	if (error)
530 		goto err;
531 
532 	/* Report any gaps at the end of the rtbitmap */
533 	info->last = true;
534 	error = xfs_getfsmap_rtdev_rtbitmap_helper(tp, &ahigh, info);
535 	if (error)
536 		goto err;
537 err:
538 	xfs_iunlock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
539 	return error;
540 }
541 
542 /* Execute a getfsmap query against the realtime device rtbitmap. */
543 STATIC int
544 xfs_getfsmap_rtdev_rtbitmap(
545 	struct xfs_trans		*tp,
546 	struct xfs_fsmap		*keys,
547 	struct xfs_getfsmap_info	*info)
548 {
549 	info->missing_owner = XFS_FMR_OWN_UNKNOWN;
550 	return __xfs_getfsmap_rtdev(tp, keys, xfs_getfsmap_rtdev_rtbitmap_query,
551 			info);
552 }
553 #endif /* CONFIG_XFS_RT */
554 
555 /* Execute a getfsmap query against the regular data device. */
556 STATIC int
557 __xfs_getfsmap_datadev(
558 	struct xfs_trans		*tp,
559 	struct xfs_fsmap		*keys,
560 	struct xfs_getfsmap_info	*info,
561 	int				(*query_fn)(struct xfs_trans *,
562 						    struct xfs_getfsmap_info *,
563 						    struct xfs_btree_cur **,
564 						    void *),
565 	void				*priv)
566 {
567 	struct xfs_mount		*mp = tp->t_mountp;
568 	struct xfs_btree_cur		*bt_cur = NULL;
569 	xfs_fsblock_t			start_fsb;
570 	xfs_fsblock_t			end_fsb;
571 	xfs_agnumber_t			start_ag;
572 	xfs_agnumber_t			end_ag;
573 	xfs_daddr_t			eofs;
574 	int				error = 0;
575 
576 	eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
577 	if (keys[0].fmr_physical >= eofs)
578 		return 0;
579 	if (keys[1].fmr_physical >= eofs)
580 		keys[1].fmr_physical = eofs - 1;
581 	start_fsb = XFS_DADDR_TO_FSB(mp, keys[0].fmr_physical);
582 	end_fsb = XFS_DADDR_TO_FSB(mp, keys[1].fmr_physical);
583 
584 	/*
585 	 * Convert the fsmap low/high keys to AG based keys.  Initialize
586 	 * low to the fsmap low key and max out the high key to the end
587 	 * of the AG.
588 	 */
589 	info->low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb);
590 	info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
591 	error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
592 	if (error)
593 		return error;
594 	info->low.rm_blockcount = 0;
595 	xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
596 
597 	info->high.rm_startblock = -1U;
598 	info->high.rm_owner = ULLONG_MAX;
599 	info->high.rm_offset = ULLONG_MAX;
600 	info->high.rm_blockcount = 0;
601 	info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
602 
603 	start_ag = XFS_FSB_TO_AGNO(mp, start_fsb);
604 	end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);
605 
606 	/* Query each AG */
607 	for (info->agno = start_ag; info->agno <= end_ag; info->agno++) {
608 		/*
609 		 * Set the AG high key from the fsmap high key if this
610 		 * is the last AG that we're querying.
611 		 */
612 		if (info->agno == end_ag) {
613 			info->high.rm_startblock = XFS_FSB_TO_AGBNO(mp,
614 					end_fsb);
615 			info->high.rm_offset = XFS_BB_TO_FSBT(mp,
616 					keys[1].fmr_offset);
617 			error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
618 			if (error)
619 				goto err;
620 			xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
621 		}
622 
623 		if (bt_cur) {
624 			xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
625 			bt_cur = NULL;
626 			xfs_trans_brelse(tp, info->agf_bp);
627 			info->agf_bp = NULL;
628 		}
629 
630 		error = xfs_alloc_read_agf(mp, tp, info->agno, 0,
631 				&info->agf_bp);
632 		if (error)
633 			goto err;
634 
635 		trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
636 		trace_xfs_fsmap_high_key(mp, info->dev, info->agno,
637 				&info->high);
638 
639 		error = query_fn(tp, info, &bt_cur, priv);
640 		if (error)
641 			goto err;
642 
643 		/*
644 		 * Set the AG low key to the start of the AG prior to
645 		 * moving on to the next AG.
646 		 */
647 		if (info->agno == start_ag) {
648 			info->low.rm_startblock = 0;
649 			info->low.rm_owner = 0;
650 			info->low.rm_offset = 0;
651 			info->low.rm_flags = 0;
652 		}
653 	}
654 
655 	/* Report any gap at the end of the AG */
656 	info->last = true;
657 	error = query_fn(tp, info, &bt_cur, priv);
658 	if (error)
659 		goto err;
660 
661 err:
662 	if (bt_cur)
663 		xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
664 							 XFS_BTREE_NOERROR);
665 	if (info->agf_bp) {
666 		xfs_trans_brelse(tp, info->agf_bp);
667 		info->agf_bp = NULL;
668 	}
669 
670 	return error;
671 }
672 
673 /* Actually query the rmap btree. */
674 STATIC int
675 xfs_getfsmap_datadev_rmapbt_query(
676 	struct xfs_trans		*tp,
677 	struct xfs_getfsmap_info	*info,
678 	struct xfs_btree_cur		**curpp,
679 	void				*priv)
680 {
681 	/* Report any gap at the end of the last AG. */
682 	if (info->last)
683 		return xfs_getfsmap_datadev_helper(*curpp, &info->high, info);
684 
685 	/* Allocate cursor for this AG and query_range it. */
686 	*curpp = xfs_rmapbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
687 			info->agno);
688 	return xfs_rmap_query_range(*curpp, &info->low, &info->high,
689 			xfs_getfsmap_datadev_helper, info);
690 }
691 
692 /* Execute a getfsmap query against the regular data device rmapbt. */
693 STATIC int
694 xfs_getfsmap_datadev_rmapbt(
695 	struct xfs_trans		*tp,
696 	struct xfs_fsmap		*keys,
697 	struct xfs_getfsmap_info	*info)
698 {
699 	info->missing_owner = XFS_FMR_OWN_FREE;
700 	return __xfs_getfsmap_datadev(tp, keys, info,
701 			xfs_getfsmap_datadev_rmapbt_query, NULL);
702 }
703 
704 /* Actually query the bno btree. */
705 STATIC int
706 xfs_getfsmap_datadev_bnobt_query(
707 	struct xfs_trans		*tp,
708 	struct xfs_getfsmap_info	*info,
709 	struct xfs_btree_cur		**curpp,
710 	void				*priv)
711 {
712 	struct xfs_alloc_rec_incore	*key = priv;
713 
714 	/* Report any gap at the end of the last AG. */
715 	if (info->last)
716 		return xfs_getfsmap_datadev_bnobt_helper(*curpp, &key[1], info);
717 
718 	/* Allocate cursor for this AG and query_range it. */
719 	*curpp = xfs_allocbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
720 			info->agno, XFS_BTNUM_BNO);
721 	key->ar_startblock = info->low.rm_startblock;
722 	key[1].ar_startblock = info->high.rm_startblock;
723 	return xfs_alloc_query_range(*curpp, key, &key[1],
724 			xfs_getfsmap_datadev_bnobt_helper, info);
725 }
726 
727 /* Execute a getfsmap query against the regular data device's bnobt. */
728 STATIC int
729 xfs_getfsmap_datadev_bnobt(
730 	struct xfs_trans		*tp,
731 	struct xfs_fsmap		*keys,
732 	struct xfs_getfsmap_info	*info)
733 {
734 	struct xfs_alloc_rec_incore	akeys[2];
735 
736 	info->missing_owner = XFS_FMR_OWN_UNKNOWN;
737 	return __xfs_getfsmap_datadev(tp, keys, info,
738 			xfs_getfsmap_datadev_bnobt_query, &akeys[0]);
739 }
740 
741 /* Do we recognize the device? */
742 STATIC bool
743 xfs_getfsmap_is_valid_device(
744 	struct xfs_mount	*mp,
745 	struct xfs_fsmap	*fm)
746 {
747 	if (fm->fmr_device == 0 || fm->fmr_device == UINT_MAX ||
748 	    fm->fmr_device == new_encode_dev(mp->m_ddev_targp->bt_dev))
749 		return true;
750 	if (mp->m_logdev_targp &&
751 	    fm->fmr_device == new_encode_dev(mp->m_logdev_targp->bt_dev))
752 		return true;
753 	if (mp->m_rtdev_targp &&
754 	    fm->fmr_device == new_encode_dev(mp->m_rtdev_targp->bt_dev))
755 		return true;
756 	return false;
757 }
758 
759 /* Ensure that the low key is less than the high key. */
760 STATIC bool
761 xfs_getfsmap_check_keys(
762 	struct xfs_fsmap		*low_key,
763 	struct xfs_fsmap		*high_key)
764 {
765 	if (low_key->fmr_device > high_key->fmr_device)
766 		return false;
767 	if (low_key->fmr_device < high_key->fmr_device)
768 		return true;
769 
770 	if (low_key->fmr_physical > high_key->fmr_physical)
771 		return false;
772 	if (low_key->fmr_physical < high_key->fmr_physical)
773 		return true;
774 
775 	if (low_key->fmr_owner > high_key->fmr_owner)
776 		return false;
777 	if (low_key->fmr_owner < high_key->fmr_owner)
778 		return true;
779 
780 	if (low_key->fmr_offset > high_key->fmr_offset)
781 		return false;
782 	if (low_key->fmr_offset < high_key->fmr_offset)
783 		return true;
784 
785 	return false;
786 }
787 
788 /*
789  * There are only two devices if we didn't configure RT devices at build time.
790  */
791 #ifdef CONFIG_XFS_RT
792 #define XFS_GETFSMAP_DEVS	3
793 #else
794 #define XFS_GETFSMAP_DEVS	2
795 #endif /* CONFIG_XFS_RT */
796 
797 /*
798  * Get filesystem's extents as described in head, and format for
799  * output.  Calls formatter to fill the user's buffer until all
800  * extents are mapped, until the passed-in head->fmh_count slots have
801  * been filled, or until the formatter short-circuits the loop, if it
802  * is tracking filled-in extents on its own.
803  *
804  * Key to Confusion
805  * ----------------
806  * There are multiple levels of keys and counters at work here:
807  * xfs_fsmap_head.fmh_keys	-- low and high fsmap keys passed in;
808  * 				   these reflect fs-wide sector addrs.
809  * dkeys			-- fmh_keys used to query each device;
810  * 				   these are fmh_keys but w/ the low key
811  * 				   bumped up by fmr_length.
812  * xfs_getfsmap_info.next_daddr	-- next disk addr we expect to see; this
813  *				   is how we detect gaps in the fsmap
814 				   records and report them.
815  * xfs_getfsmap_info.low/high	-- per-AG low/high keys computed from
816  * 				   dkeys; used to query the metadata.
817  */
818 int
819 xfs_getfsmap(
820 	struct xfs_mount		*mp,
821 	struct xfs_fsmap_head		*head,
822 	xfs_fsmap_format_t		formatter,
823 	void				*arg)
824 {
825 	struct xfs_trans		*tp = NULL;
826 	struct xfs_fsmap		dkeys[2];	/* per-dev keys */
827 	struct xfs_getfsmap_dev		handlers[XFS_GETFSMAP_DEVS];
828 	struct xfs_getfsmap_info	info = { NULL };
829 	bool				use_rmap;
830 	int				i;
831 	int				error = 0;
832 
833 	if (head->fmh_iflags & ~FMH_IF_VALID)
834 		return -EINVAL;
835 	if (!xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[0]) ||
836 	    !xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[1]))
837 		return -EINVAL;
838 
839 	use_rmap = capable(CAP_SYS_ADMIN) &&
840 		   xfs_sb_version_hasrmapbt(&mp->m_sb);
841 	head->fmh_entries = 0;
842 
843 	/* Set up our device handlers. */
844 	memset(handlers, 0, sizeof(handlers));
845 	handlers[0].dev = new_encode_dev(mp->m_ddev_targp->bt_dev);
846 	if (use_rmap)
847 		handlers[0].fn = xfs_getfsmap_datadev_rmapbt;
848 	else
849 		handlers[0].fn = xfs_getfsmap_datadev_bnobt;
850 	if (mp->m_logdev_targp != mp->m_ddev_targp) {
851 		handlers[1].dev = new_encode_dev(mp->m_logdev_targp->bt_dev);
852 		handlers[1].fn = xfs_getfsmap_logdev;
853 	}
854 #ifdef CONFIG_XFS_RT
855 	if (mp->m_rtdev_targp) {
856 		handlers[2].dev = new_encode_dev(mp->m_rtdev_targp->bt_dev);
857 		handlers[2].fn = xfs_getfsmap_rtdev_rtbitmap;
858 	}
859 #endif /* CONFIG_XFS_RT */
860 
861 	xfs_sort(handlers, XFS_GETFSMAP_DEVS, sizeof(struct xfs_getfsmap_dev),
862 			xfs_getfsmap_dev_compare);
863 
864 	/*
865 	 * To continue where we left off, we allow userspace to use the
866 	 * last mapping from a previous call as the low key of the next.
867 	 * This is identified by a non-zero length in the low key. We
868 	 * have to increment the low key in this scenario to ensure we
869 	 * don't return the same mapping again, and instead return the
870 	 * very next mapping.
871 	 *
872 	 * If the low key mapping refers to file data, the same physical
873 	 * blocks could be mapped to several other files/offsets.
874 	 * According to rmapbt record ordering, the minimal next
875 	 * possible record for the block range is the next starting
876 	 * offset in the same inode. Therefore, bump the file offset to
877 	 * continue the search appropriately.  For all other low key
878 	 * mapping types (attr blocks, metadata), bump the physical
879 	 * offset as there can be no other mapping for the same physical
880 	 * block range.
881 	 */
882 	dkeys[0] = head->fmh_keys[0];
883 	if (dkeys[0].fmr_flags & (FMR_OF_SPECIAL_OWNER | FMR_OF_EXTENT_MAP)) {
884 		dkeys[0].fmr_physical += dkeys[0].fmr_length;
885 		dkeys[0].fmr_owner = 0;
886 		if (dkeys[0].fmr_offset)
887 			return -EINVAL;
888 	} else
889 		dkeys[0].fmr_offset += dkeys[0].fmr_length;
890 	dkeys[0].fmr_length = 0;
891 	memset(&dkeys[1], 0xFF, sizeof(struct xfs_fsmap));
892 
893 	if (!xfs_getfsmap_check_keys(dkeys, &head->fmh_keys[1]))
894 		return -EINVAL;
895 
896 	info.next_daddr = head->fmh_keys[0].fmr_physical +
897 			  head->fmh_keys[0].fmr_length;
898 	info.formatter = formatter;
899 	info.format_arg = arg;
900 	info.head = head;
901 
902 	/* For each device we support... */
903 	for (i = 0; i < XFS_GETFSMAP_DEVS; i++) {
904 		/* Is this device within the range the user asked for? */
905 		if (!handlers[i].fn)
906 			continue;
907 		if (head->fmh_keys[0].fmr_device > handlers[i].dev)
908 			continue;
909 		if (head->fmh_keys[1].fmr_device < handlers[i].dev)
910 			break;
911 
912 		/*
913 		 * If this device number matches the high key, we have
914 		 * to pass the high key to the handler to limit the
915 		 * query results.  If the device number exceeds the
916 		 * low key, zero out the low key so that we get
917 		 * everything from the beginning.
918 		 */
919 		if (handlers[i].dev == head->fmh_keys[1].fmr_device)
920 			dkeys[1] = head->fmh_keys[1];
921 		if (handlers[i].dev > head->fmh_keys[0].fmr_device)
922 			memset(&dkeys[0], 0, sizeof(struct xfs_fsmap));
923 
924 		error = xfs_trans_alloc_empty(mp, &tp);
925 		if (error)
926 			break;
927 
928 		info.dev = handlers[i].dev;
929 		info.last = false;
930 		info.agno = NULLAGNUMBER;
931 		error = handlers[i].fn(tp, dkeys, &info);
932 		if (error)
933 			break;
934 		xfs_trans_cancel(tp);
935 		tp = NULL;
936 		info.next_daddr = 0;
937 	}
938 
939 	if (tp)
940 		xfs_trans_cancel(tp);
941 	head->fmh_oflags = FMH_OF_DEV_T;
942 	return error;
943 }
944