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