xref: /linux/fs/xfs/xfs_fsmap.c (revision c148bc7535650fbfa95a1f571b9ffa2ab478ea33)
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_mount.h"
13 #include "xfs_inode.h"
14 #include "xfs_trans.h"
15 #include "xfs_btree.h"
16 #include "xfs_rmap_btree.h"
17 #include "xfs_trace.h"
18 #include "xfs_rmap.h"
19 #include "xfs_alloc.h"
20 #include "xfs_bit.h"
21 #include <linux/fsmap.h>
22 #include "xfs_fsmap.h"
23 #include "xfs_refcount.h"
24 #include "xfs_refcount_btree.h"
25 #include "xfs_alloc_btree.h"
26 #include "xfs_rtbitmap.h"
27 #include "xfs_ag.h"
28 #include "xfs_rtgroup.h"
29 #include "xfs_rtrmap_btree.h"
30 #include "xfs_rtrefcount_btree.h"
31 
32 /* Convert an xfs_fsmap to an fsmap. */
33 static void
xfs_fsmap_from_internal(struct fsmap * dest,struct xfs_fsmap * src)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 static void
xfs_fsmap_to_internal(struct xfs_fsmap * dest,struct fsmap * src)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
xfs_fsmap_owner_to_rmap(struct xfs_rmap_irec * dest,const struct xfs_fsmap * src)65 xfs_fsmap_owner_to_rmap(
66 	struct xfs_rmap_irec	*dest,
67 	const 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 = src->fmr_owner;
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
xfs_fsmap_owner_from_frec(struct xfs_fsmap * dest,const struct xfs_fsmap_irec * frec)116 xfs_fsmap_owner_from_frec(
117 	struct xfs_fsmap		*dest,
118 	const struct xfs_fsmap_irec	*frec)
119 {
120 	dest->fmr_flags = 0;
121 	if (!XFS_RMAP_NON_INODE_OWNER(frec->owner)) {
122 		dest->fmr_owner = frec->owner;
123 		return 0;
124 	}
125 	dest->fmr_flags |= FMR_OF_SPECIAL_OWNER;
126 
127 	switch (frec->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 		ASSERT(0);
154 		return -EFSCORRUPTED;
155 	}
156 	return 0;
157 }
158 
159 /* getfsmap query state */
160 struct xfs_getfsmap_info {
161 	struct xfs_fsmap_head	*head;
162 	struct fsmap		*fsmap_recs;	/* mapping records */
163 	struct xfs_buf		*agf_bp;	/* AGF, for refcount queries */
164 	struct xfs_group	*group;		/* group info, if applicable */
165 	xfs_daddr_t		next_daddr;	/* next daddr we expect */
166 	/* daddr of low fsmap key when we're using the rtbitmap */
167 	xfs_daddr_t		low_daddr;
168 	/* daddr of high fsmap key, or the last daddr on the device */
169 	xfs_daddr_t		end_daddr;
170 	u64			missing_owner;	/* owner of holes */
171 	u32			dev;		/* device id */
172 	/*
173 	 * Low rmap key for the query.  If low.rm_blockcount is nonzero, this
174 	 * is the second (or later) call to retrieve the recordset in pieces.
175 	 * xfs_getfsmap_rec_before_start will compare all records retrieved
176 	 * by the rmapbt query to filter out any records that start before
177 	 * the last record.
178 	 */
179 	struct xfs_rmap_irec	low;
180 	struct xfs_rmap_irec	high;		/* high rmap key */
181 	bool			last;		/* last extent? */
182 };
183 
184 /* Associate a device with a getfsmap handler. */
185 struct xfs_getfsmap_dev {
186 	u32			dev;
187 	int			(*fn)(struct xfs_trans *tp,
188 				      const struct xfs_fsmap *keys,
189 				      struct xfs_getfsmap_info *info);
190 	sector_t		nr_sectors;
191 };
192 
193 /* Compare two getfsmap device handlers. */
194 static int
xfs_getfsmap_dev_compare(const void * p1,const void * p2)195 xfs_getfsmap_dev_compare(
196 	const void			*p1,
197 	const void			*p2)
198 {
199 	const struct xfs_getfsmap_dev	*d1 = p1;
200 	const struct xfs_getfsmap_dev	*d2 = p2;
201 
202 	return d1->dev - d2->dev;
203 }
204 
205 /* Decide if this mapping is shared. */
206 STATIC int
xfs_getfsmap_is_shared(struct xfs_trans * tp,struct xfs_getfsmap_info * info,const struct xfs_fsmap_irec * frec,bool * stat)207 xfs_getfsmap_is_shared(
208 	struct xfs_trans		*tp,
209 	struct xfs_getfsmap_info	*info,
210 	const struct xfs_fsmap_irec	*frec,
211 	bool				*stat)
212 {
213 	struct xfs_mount		*mp = tp->t_mountp;
214 	struct xfs_btree_cur		*cur;
215 	xfs_agblock_t			fbno;
216 	xfs_extlen_t			flen = 0;
217 	int				error;
218 
219 	*stat = false;
220 	if (!xfs_has_reflink(mp) || !info->group)
221 		return 0;
222 
223 	if (info->group->xg_type == XG_TYPE_RTG)
224 		cur = xfs_rtrefcountbt_init_cursor(tp, to_rtg(info->group));
225 	else
226 		cur = xfs_refcountbt_init_cursor(mp, tp, info->agf_bp,
227 				to_perag(info->group));
228 
229 	/* Are there any shared blocks here? */
230 	error = xfs_refcount_find_shared(cur, frec->rec_key,
231 			XFS_BB_TO_FSBT(mp, frec->len_daddr), &fbno, &flen,
232 			false);
233 
234 	xfs_btree_del_cursor(cur, error);
235 	if (error)
236 		return error;
237 
238 	*stat = flen > 0;
239 	return 0;
240 }
241 
242 static inline void
xfs_getfsmap_format(struct xfs_mount * mp,struct xfs_fsmap * xfm,struct xfs_getfsmap_info * info)243 xfs_getfsmap_format(
244 	struct xfs_mount		*mp,
245 	struct xfs_fsmap		*xfm,
246 	struct xfs_getfsmap_info	*info)
247 {
248 	struct fsmap			*rec;
249 
250 	trace_xfs_getfsmap_mapping(mp, xfm);
251 
252 	rec = &info->fsmap_recs[info->head->fmh_entries++];
253 	xfs_fsmap_from_internal(rec, xfm);
254 }
255 
256 static inline bool
xfs_getfsmap_frec_before_start(struct xfs_getfsmap_info * info,const struct xfs_fsmap_irec * frec)257 xfs_getfsmap_frec_before_start(
258 	struct xfs_getfsmap_info	*info,
259 	const struct xfs_fsmap_irec	*frec)
260 {
261 	if (info->low_daddr != XFS_BUF_DADDR_NULL)
262 		return frec->start_daddr < info->low_daddr;
263 	if (info->low.rm_blockcount) {
264 		struct xfs_rmap_irec	rec = {
265 			.rm_startblock	= frec->rec_key,
266 			.rm_owner	= frec->owner,
267 			.rm_flags	= frec->rm_flags,
268 		};
269 
270 		return xfs_rmap_compare(&rec, &info->low) < 0;
271 	}
272 
273 	return false;
274 }
275 
276 /*
277  * Format a reverse mapping for getfsmap, having translated rm_startblock
278  * into the appropriate daddr units.  Pass in a nonzero @len_daddr if the
279  * length could be larger than rm_blockcount in struct xfs_rmap_irec.
280  */
281 STATIC int
xfs_getfsmap_helper(struct xfs_trans * tp,struct xfs_getfsmap_info * info,const struct xfs_fsmap_irec * frec)282 xfs_getfsmap_helper(
283 	struct xfs_trans		*tp,
284 	struct xfs_getfsmap_info	*info,
285 	const struct xfs_fsmap_irec	*frec)
286 {
287 	struct xfs_fsmap		fmr;
288 	struct xfs_mount		*mp = tp->t_mountp;
289 	bool				shared;
290 	int				error = 0;
291 
292 	if (fatal_signal_pending(current))
293 		return -EINTR;
294 
295 	/*
296 	 * Filter out records that start before our startpoint, if the
297 	 * caller requested that.
298 	 */
299 	if (xfs_getfsmap_frec_before_start(info, frec))
300 		goto out;
301 
302 	/* Are we just counting mappings? */
303 	if (info->head->fmh_count == 0) {
304 		if (info->head->fmh_entries == UINT_MAX)
305 			return -ECANCELED;
306 
307 		if (frec->start_daddr > info->next_daddr)
308 			info->head->fmh_entries++;
309 
310 		if (info->last)
311 			return 0;
312 
313 		info->head->fmh_entries++;
314 		goto out;
315 	}
316 
317 	/*
318 	 * If the record starts past the last physical block we saw,
319 	 * then we've found a gap.  Report the gap as being owned by
320 	 * whatever the caller specified is the missing owner.
321 	 */
322 	if (frec->start_daddr > info->next_daddr) {
323 		if (info->head->fmh_entries >= info->head->fmh_count)
324 			return -ECANCELED;
325 
326 		fmr.fmr_device = info->dev;
327 		fmr.fmr_physical = info->next_daddr;
328 		fmr.fmr_owner = info->missing_owner;
329 		fmr.fmr_offset = 0;
330 		fmr.fmr_length = frec->start_daddr - info->next_daddr;
331 		fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
332 		xfs_getfsmap_format(mp, &fmr, info);
333 	}
334 
335 	if (info->last)
336 		goto out;
337 
338 	/* Fill out the extent we found */
339 	if (info->head->fmh_entries >= info->head->fmh_count)
340 		return -ECANCELED;
341 
342 	trace_xfs_fsmap_mapping(mp, info->dev,
343 			info->group ? info->group->xg_gno : NULLAGNUMBER,
344 			frec);
345 
346 	fmr.fmr_device = info->dev;
347 	fmr.fmr_physical = frec->start_daddr;
348 	error = xfs_fsmap_owner_from_frec(&fmr, frec);
349 	if (error)
350 		return error;
351 	fmr.fmr_offset = XFS_FSB_TO_BB(mp, frec->offset);
352 	fmr.fmr_length = frec->len_daddr;
353 	if (frec->rm_flags & XFS_RMAP_UNWRITTEN)
354 		fmr.fmr_flags |= FMR_OF_PREALLOC;
355 	if (frec->rm_flags & XFS_RMAP_ATTR_FORK)
356 		fmr.fmr_flags |= FMR_OF_ATTR_FORK;
357 	if (frec->rm_flags & XFS_RMAP_BMBT_BLOCK)
358 		fmr.fmr_flags |= FMR_OF_EXTENT_MAP;
359 	if (fmr.fmr_flags == 0) {
360 		error = xfs_getfsmap_is_shared(tp, info, frec, &shared);
361 		if (error)
362 			return error;
363 		if (shared)
364 			fmr.fmr_flags |= FMR_OF_SHARED;
365 	}
366 
367 	xfs_getfsmap_format(mp, &fmr, info);
368 out:
369 	info->next_daddr = max(info->next_daddr,
370 			       frec->start_daddr + frec->len_daddr);
371 	return 0;
372 }
373 
374 static inline int
xfs_getfsmap_group_helper(struct xfs_getfsmap_info * info,struct xfs_trans * tp,struct xfs_group * xg,xfs_agblock_t startblock,xfs_extlen_t blockcount,struct xfs_fsmap_irec * frec)375 xfs_getfsmap_group_helper(
376 	struct xfs_getfsmap_info	*info,
377 	struct xfs_trans		*tp,
378 	struct xfs_group		*xg,
379 	xfs_agblock_t			startblock,
380 	xfs_extlen_t			blockcount,
381 	struct xfs_fsmap_irec		*frec)
382 {
383 	/*
384 	 * For an info->last query, we're looking for a gap between the last
385 	 * mapping emitted and the high key specified by userspace.  If the
386 	 * user's query spans less than 1 fsblock, then info->high and
387 	 * info->low will have the same rm_startblock, which causes rec_daddr
388 	 * and next_daddr to be the same.  Therefore, use the end_daddr that
389 	 * we calculated from userspace's high key to synthesize the record.
390 	 * Note that if the btree query found a mapping, there won't be a gap.
391 	 */
392 	if (info->last)
393 		frec->start_daddr = info->end_daddr + 1;
394 	else
395 		frec->start_daddr = xfs_gbno_to_daddr(xg, startblock);
396 
397 	frec->len_daddr = XFS_FSB_TO_BB(xg->xg_mount, blockcount);
398 	return xfs_getfsmap_helper(tp, info, frec);
399 }
400 
401 /* Transform a rmapbt irec into a fsmap */
402 STATIC int
xfs_getfsmap_rmapbt_helper(struct xfs_btree_cur * cur,const struct xfs_rmap_irec * rec,void * priv)403 xfs_getfsmap_rmapbt_helper(
404 	struct xfs_btree_cur		*cur,
405 	const struct xfs_rmap_irec	*rec,
406 	void				*priv)
407 {
408 	struct xfs_fsmap_irec		frec = {
409 		.owner			= rec->rm_owner,
410 		.offset			= rec->rm_offset,
411 		.rm_flags		= rec->rm_flags,
412 		.rec_key		= rec->rm_startblock,
413 	};
414 	struct xfs_getfsmap_info	*info = priv;
415 
416 	return xfs_getfsmap_group_helper(info, cur->bc_tp, cur->bc_group,
417 			rec->rm_startblock, rec->rm_blockcount, &frec);
418 }
419 
420 /* Transform a bnobt irec into a fsmap */
421 STATIC int
xfs_getfsmap_datadev_bnobt_helper(struct xfs_btree_cur * cur,const struct xfs_alloc_rec_incore * rec,void * priv)422 xfs_getfsmap_datadev_bnobt_helper(
423 	struct xfs_btree_cur		*cur,
424 	const struct xfs_alloc_rec_incore *rec,
425 	void				*priv)
426 {
427 	struct xfs_fsmap_irec		frec = {
428 		.owner			= XFS_RMAP_OWN_NULL, /* "free" */
429 		.rec_key		= rec->ar_startblock,
430 	};
431 	struct xfs_getfsmap_info	*info = priv;
432 
433 	return xfs_getfsmap_group_helper(info, cur->bc_tp, cur->bc_group,
434 			rec->ar_startblock, rec->ar_blockcount, &frec);
435 }
436 
437 /* Set rmap flags based on the getfsmap flags */
438 static void
xfs_getfsmap_set_irec_flags(struct xfs_rmap_irec * irec,const struct xfs_fsmap * fmr)439 xfs_getfsmap_set_irec_flags(
440 	struct xfs_rmap_irec	*irec,
441 	const struct xfs_fsmap	*fmr)
442 {
443 	irec->rm_flags = 0;
444 	if (fmr->fmr_flags & FMR_OF_ATTR_FORK)
445 		irec->rm_flags |= XFS_RMAP_ATTR_FORK;
446 	if (fmr->fmr_flags & FMR_OF_EXTENT_MAP)
447 		irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
448 	if (fmr->fmr_flags & FMR_OF_PREALLOC)
449 		irec->rm_flags |= XFS_RMAP_UNWRITTEN;
450 }
451 
452 static inline bool
rmap_not_shareable(struct xfs_mount * mp,const struct xfs_rmap_irec * r)453 rmap_not_shareable(struct xfs_mount *mp, const struct xfs_rmap_irec *r)
454 {
455 	if (!xfs_has_reflink(mp))
456 		return true;
457 	if (XFS_RMAP_NON_INODE_OWNER(r->rm_owner))
458 		return true;
459 	if (r->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK |
460 			   XFS_RMAP_UNWRITTEN))
461 		return true;
462 	return false;
463 }
464 
465 /* Execute a getfsmap query against the regular data device. */
466 STATIC int
__xfs_getfsmap_datadev(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info,int (* query_fn)(struct xfs_trans *,struct xfs_getfsmap_info *,struct xfs_btree_cur **,void *),void * priv)467 __xfs_getfsmap_datadev(
468 	struct xfs_trans		*tp,
469 	const struct xfs_fsmap		*keys,
470 	struct xfs_getfsmap_info	*info,
471 	int				(*query_fn)(struct xfs_trans *,
472 						    struct xfs_getfsmap_info *,
473 						    struct xfs_btree_cur **,
474 						    void *),
475 	void				*priv)
476 {
477 	struct xfs_mount		*mp = tp->t_mountp;
478 	struct xfs_perag		*pag = NULL;
479 	struct xfs_btree_cur		*bt_cur = NULL;
480 	xfs_fsblock_t			start_fsb;
481 	xfs_fsblock_t			end_fsb;
482 	xfs_agnumber_t			start_ag, end_ag;
483 	uint64_t			eofs;
484 	int				error = 0;
485 
486 	eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
487 	if (keys[0].fmr_physical >= eofs)
488 		return 0;
489 	start_fsb = XFS_DADDR_TO_FSB(mp, keys[0].fmr_physical);
490 	end_fsb = XFS_DADDR_TO_FSB(mp, min(eofs - 1, keys[1].fmr_physical));
491 
492 	/*
493 	 * Convert the fsmap low/high keys to AG based keys.  Initialize
494 	 * low to the fsmap low key and max out the high key to the end
495 	 * of the AG.
496 	 */
497 	info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
498 	error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
499 	if (error)
500 		return error;
501 	info->low.rm_blockcount = XFS_BB_TO_FSBT(mp, keys[0].fmr_length);
502 	xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
503 
504 	/* Adjust the low key if we are continuing from where we left off. */
505 	if (info->low.rm_blockcount == 0) {
506 		/* No previous record from which to continue */
507 	} else if (rmap_not_shareable(mp, &info->low)) {
508 		/* Last record seen was an unshareable extent */
509 		info->low.rm_owner = 0;
510 		info->low.rm_offset = 0;
511 
512 		start_fsb += info->low.rm_blockcount;
513 		if (XFS_FSB_TO_DADDR(mp, start_fsb) >= eofs)
514 			return 0;
515 	} else {
516 		/* Last record seen was a shareable file data extent */
517 		info->low.rm_offset += info->low.rm_blockcount;
518 	}
519 	info->low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb);
520 
521 	info->high.rm_startblock = -1U;
522 	info->high.rm_owner = ULLONG_MAX;
523 	info->high.rm_offset = ULLONG_MAX;
524 	info->high.rm_blockcount = 0;
525 	info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
526 
527 	start_ag = XFS_FSB_TO_AGNO(mp, start_fsb);
528 	end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);
529 
530 	while ((pag = xfs_perag_next_range(mp, pag, start_ag, end_ag))) {
531 		/*
532 		 * Set the AG high key from the fsmap high key if this
533 		 * is the last AG that we're querying.
534 		 */
535 		info->group = pag_group(pag);
536 		if (pag_agno(pag) == end_ag) {
537 			info->high.rm_startblock = XFS_FSB_TO_AGBNO(mp,
538 					end_fsb);
539 			info->high.rm_offset = XFS_BB_TO_FSBT(mp,
540 					keys[1].fmr_offset);
541 			error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
542 			if (error)
543 				break;
544 			xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
545 		}
546 
547 		if (bt_cur) {
548 			xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
549 			bt_cur = NULL;
550 			xfs_trans_brelse(tp, info->agf_bp);
551 			info->agf_bp = NULL;
552 		}
553 
554 		error = xfs_alloc_read_agf(pag, tp, 0, &info->agf_bp);
555 		if (error)
556 			break;
557 
558 		trace_xfs_fsmap_low_group_key(mp, info->dev, pag_agno(pag),
559 				&info->low);
560 		trace_xfs_fsmap_high_group_key(mp, info->dev, pag_agno(pag),
561 				&info->high);
562 
563 		error = query_fn(tp, info, &bt_cur, priv);
564 		if (error)
565 			break;
566 
567 		/*
568 		 * Set the AG low key to the start of the AG prior to
569 		 * moving on to the next AG.
570 		 */
571 		if (pag_agno(pag) == start_ag)
572 			memset(&info->low, 0, sizeof(info->low));
573 
574 		/*
575 		 * If this is the last AG, report any gap at the end of it
576 		 * before we drop the reference to the perag when the loop
577 		 * terminates.
578 		 */
579 		if (pag_agno(pag) == end_ag) {
580 			info->last = true;
581 			error = query_fn(tp, info, &bt_cur, priv);
582 			if (error)
583 				break;
584 		}
585 		info->group = NULL;
586 	}
587 
588 	if (bt_cur)
589 		xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
590 							 XFS_BTREE_NOERROR);
591 	if (info->agf_bp) {
592 		xfs_trans_brelse(tp, info->agf_bp);
593 		info->agf_bp = NULL;
594 	}
595 	if (info->group) {
596 		xfs_perag_rele(pag);
597 		info->group = NULL;
598 	} else if (pag) {
599 		/* loop termination case */
600 		xfs_perag_rele(pag);
601 	}
602 
603 	return error;
604 }
605 
606 /* Actually query the rmap btree. */
607 STATIC int
xfs_getfsmap_datadev_rmapbt_query(struct xfs_trans * tp,struct xfs_getfsmap_info * info,struct xfs_btree_cur ** curpp,void * priv)608 xfs_getfsmap_datadev_rmapbt_query(
609 	struct xfs_trans		*tp,
610 	struct xfs_getfsmap_info	*info,
611 	struct xfs_btree_cur		**curpp,
612 	void				*priv)
613 {
614 	/* Report any gap at the end of the last AG. */
615 	if (info->last)
616 		return xfs_getfsmap_rmapbt_helper(*curpp, &info->high, info);
617 
618 	/* Allocate cursor for this AG and query_range it. */
619 	*curpp = xfs_rmapbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
620 			to_perag(info->group));
621 	return xfs_rmap_query_range(*curpp, &info->low, &info->high,
622 			xfs_getfsmap_rmapbt_helper, info);
623 }
624 
625 /* Execute a getfsmap query against the regular data device rmapbt. */
626 STATIC int
xfs_getfsmap_datadev_rmapbt(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)627 xfs_getfsmap_datadev_rmapbt(
628 	struct xfs_trans		*tp,
629 	const struct xfs_fsmap		*keys,
630 	struct xfs_getfsmap_info	*info)
631 {
632 	info->missing_owner = XFS_FMR_OWN_FREE;
633 	return __xfs_getfsmap_datadev(tp, keys, info,
634 			xfs_getfsmap_datadev_rmapbt_query, NULL);
635 }
636 
637 /* Actually query the bno btree. */
638 STATIC int
xfs_getfsmap_datadev_bnobt_query(struct xfs_trans * tp,struct xfs_getfsmap_info * info,struct xfs_btree_cur ** curpp,void * priv)639 xfs_getfsmap_datadev_bnobt_query(
640 	struct xfs_trans		*tp,
641 	struct xfs_getfsmap_info	*info,
642 	struct xfs_btree_cur		**curpp,
643 	void				*priv)
644 {
645 	struct xfs_alloc_rec_incore	*key = priv;
646 
647 	/* Report any gap at the end of the last AG. */
648 	if (info->last)
649 		return xfs_getfsmap_datadev_bnobt_helper(*curpp, &key[1], info);
650 
651 	/* Allocate cursor for this AG and query_range it. */
652 	*curpp = xfs_bnobt_init_cursor(tp->t_mountp, tp, info->agf_bp,
653 			to_perag(info->group));
654 	key->ar_startblock = info->low.rm_startblock;
655 	key[1].ar_startblock = info->high.rm_startblock;
656 	return xfs_alloc_query_range(*curpp, key, &key[1],
657 			xfs_getfsmap_datadev_bnobt_helper, info);
658 }
659 
660 /* Execute a getfsmap query against the regular data device's bnobt. */
661 STATIC int
xfs_getfsmap_datadev_bnobt(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)662 xfs_getfsmap_datadev_bnobt(
663 	struct xfs_trans		*tp,
664 	const struct xfs_fsmap		*keys,
665 	struct xfs_getfsmap_info	*info)
666 {
667 	struct xfs_alloc_rec_incore	akeys[2];
668 
669 	memset(akeys, 0, sizeof(akeys));
670 	info->missing_owner = XFS_FMR_OWN_UNKNOWN;
671 	return __xfs_getfsmap_datadev(tp, keys, info,
672 			xfs_getfsmap_datadev_bnobt_query, &akeys[0]);
673 }
674 
675 /* Execute a getfsmap query against the log device. */
676 STATIC int
xfs_getfsmap_logdev(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)677 xfs_getfsmap_logdev(
678 	struct xfs_trans		*tp,
679 	const struct xfs_fsmap		*keys,
680 	struct xfs_getfsmap_info	*info)
681 {
682 	struct xfs_fsmap_irec		frec = {
683 		.start_daddr		= 0,
684 		.rec_key		= 0,
685 		.owner			= XFS_RMAP_OWN_LOG,
686 	};
687 	struct xfs_mount		*mp = tp->t_mountp;
688 	xfs_fsblock_t			start_fsb, end_fsb;
689 	uint64_t			eofs;
690 
691 	eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
692 	if (keys[0].fmr_physical >= eofs)
693 		return 0;
694 	start_fsb = XFS_BB_TO_FSBT(mp,
695 				keys[0].fmr_physical + keys[0].fmr_length);
696 	end_fsb = XFS_BB_TO_FSB(mp, min(eofs - 1, keys[1].fmr_physical));
697 
698 	/* Adjust the low key if we are continuing from where we left off. */
699 	if (keys[0].fmr_length > 0)
700 		info->low_daddr = XFS_FSB_TO_BB(mp, start_fsb);
701 
702 	trace_xfs_fsmap_low_linear_key(mp, info->dev, start_fsb);
703 	trace_xfs_fsmap_high_linear_key(mp, info->dev, end_fsb);
704 
705 	if (start_fsb > 0)
706 		return 0;
707 
708 	/* Fabricate an rmap entry for the external log device. */
709 	frec.len_daddr = XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
710 	return xfs_getfsmap_helper(tp, info, &frec);
711 }
712 
713 #ifdef CONFIG_XFS_RT
714 /* Transform a rtbitmap "record" into a fsmap */
715 STATIC int
xfs_getfsmap_rtdev_rtbitmap_helper(struct xfs_rtgroup * rtg,struct xfs_trans * tp,const struct xfs_rtalloc_rec * rec,void * priv)716 xfs_getfsmap_rtdev_rtbitmap_helper(
717 	struct xfs_rtgroup		*rtg,
718 	struct xfs_trans		*tp,
719 	const struct xfs_rtalloc_rec	*rec,
720 	void				*priv)
721 {
722 	struct xfs_fsmap_irec		frec = {
723 		.owner			= XFS_RMAP_OWN_NULL, /* "free" */
724 	};
725 	struct xfs_mount		*mp = rtg_mount(rtg);
726 	struct xfs_getfsmap_info	*info = priv;
727 	xfs_rtblock_t			start_rtb =
728 				xfs_rtx_to_rtb(rtg, rec->ar_startext);
729 	uint64_t			rtbcount =
730 				xfs_rtbxlen_to_blen(mp, rec->ar_extcount);
731 
732 	/*
733 	 * For an info->last query, we're looking for a gap between the last
734 	 * mapping emitted and the high key specified by userspace.  If the
735 	 * user's query spans less than 1 fsblock, then info->high and
736 	 * info->low will have the same rm_startblock, which causes rec_daddr
737 	 * and next_daddr to be the same.  Therefore, use the end_daddr that
738 	 * we calculated from userspace's high key to synthesize the record.
739 	 * Note that if the btree query found a mapping, there won't be a gap.
740 	 */
741 	if (info->last)
742 		frec.start_daddr = info->end_daddr + 1;
743 	else
744 		frec.start_daddr = xfs_rtb_to_daddr(mp, start_rtb);
745 
746 	frec.len_daddr = XFS_FSB_TO_BB(mp, rtbcount);
747 	return xfs_getfsmap_helper(tp, info, &frec);
748 }
749 
750 /* Execute a getfsmap query against the realtime device rtbitmap. */
751 STATIC int
xfs_getfsmap_rtdev_rtbitmap(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)752 xfs_getfsmap_rtdev_rtbitmap(
753 	struct xfs_trans		*tp,
754 	const struct xfs_fsmap		*keys,
755 	struct xfs_getfsmap_info	*info)
756 {
757 	struct xfs_mount		*mp = tp->t_mountp;
758 	xfs_rtblock_t			start_rtbno, end_rtbno;
759 	xfs_rtxnum_t			start_rtx, end_rtx;
760 	xfs_rgnumber_t			start_rgno, end_rgno;
761 	struct xfs_rtgroup		*rtg = NULL;
762 	uint64_t			eofs;
763 	int				error;
764 
765 	eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
766 	if (keys[0].fmr_physical >= eofs)
767 		return 0;
768 
769 	info->missing_owner = XFS_FMR_OWN_UNKNOWN;
770 
771 	/* Adjust the low key if we are continuing from where we left off. */
772 	start_rtbno = xfs_daddr_to_rtb(mp,
773 			keys[0].fmr_physical + keys[0].fmr_length);
774 	if (keys[0].fmr_length > 0) {
775 		info->low_daddr = xfs_rtb_to_daddr(mp, start_rtbno);
776 		if (info->low_daddr >= eofs)
777 			return 0;
778 	}
779 	start_rtx = xfs_rtb_to_rtx(mp, start_rtbno);
780 	start_rgno = xfs_rtb_to_rgno(mp, start_rtbno);
781 
782 	end_rtbno = xfs_daddr_to_rtb(mp, min(eofs - 1, keys[1].fmr_physical));
783 	end_rgno = xfs_rtb_to_rgno(mp, end_rtbno);
784 
785 	trace_xfs_fsmap_low_linear_key(mp, info->dev, start_rtbno);
786 	trace_xfs_fsmap_high_linear_key(mp, info->dev, end_rtbno);
787 
788 	end_rtx = -1ULL;
789 
790 	while ((rtg = xfs_rtgroup_next_range(mp, rtg, start_rgno, end_rgno))) {
791 		if (rtg_rgno(rtg) == end_rgno)
792 			end_rtx = xfs_rtb_to_rtx(mp,
793 					end_rtbno + mp->m_sb.sb_rextsize - 1);
794 
795 		info->group = rtg_group(rtg);
796 		xfs_rtgroup_lock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
797 		error = xfs_rtalloc_query_range(rtg, tp, start_rtx, end_rtx,
798 				xfs_getfsmap_rtdev_rtbitmap_helper, info);
799 		if (error)
800 			break;
801 
802 		/*
803 		 * Report any gaps at the end of the rtbitmap by simulating a
804 		 * zero-length free extent starting at the rtx after the end
805 		 * of the query range.
806 		 */
807 		if (rtg_rgno(rtg) == end_rgno) {
808 			struct xfs_rtalloc_rec	ahigh = {
809 				.ar_startext	= min(end_rtx + 1,
810 						      rtg->rtg_extents),
811 			};
812 
813 			info->last = true;
814 			error = xfs_getfsmap_rtdev_rtbitmap_helper(rtg, tp,
815 					&ahigh, info);
816 			if (error)
817 				break;
818 		}
819 
820 		xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
821 		info->group = NULL;
822 		start_rtx = 0;
823 	}
824 
825 	/* loop termination case */
826 	if (rtg) {
827 		if (info->group) {
828 			xfs_rtgroup_unlock(rtg, XFS_RTGLOCK_BITMAP_SHARED);
829 			info->group = NULL;
830 		}
831 		xfs_rtgroup_rele(rtg);
832 	}
833 
834 	return error;
835 }
836 
837 /* Transform a realtime rmapbt record into a fsmap */
838 STATIC int
xfs_getfsmap_rtdev_rmapbt_helper(struct xfs_btree_cur * cur,const struct xfs_rmap_irec * rec,void * priv)839 xfs_getfsmap_rtdev_rmapbt_helper(
840 	struct xfs_btree_cur		*cur,
841 	const struct xfs_rmap_irec	*rec,
842 	void				*priv)
843 {
844 	struct xfs_fsmap_irec		frec = {
845 		.owner			= rec->rm_owner,
846 		.offset			= rec->rm_offset,
847 		.rm_flags		= rec->rm_flags,
848 		.rec_key		= rec->rm_startblock,
849 	};
850 	struct xfs_getfsmap_info	*info = priv;
851 
852 	return xfs_getfsmap_group_helper(info, cur->bc_tp, cur->bc_group,
853 			rec->rm_startblock, rec->rm_blockcount, &frec);
854 }
855 
856 /* Actually query the rtrmap btree. */
857 STATIC int
xfs_getfsmap_rtdev_rmapbt_query(struct xfs_trans * tp,struct xfs_getfsmap_info * info,struct xfs_btree_cur ** curpp)858 xfs_getfsmap_rtdev_rmapbt_query(
859 	struct xfs_trans		*tp,
860 	struct xfs_getfsmap_info	*info,
861 	struct xfs_btree_cur		**curpp)
862 {
863 	struct xfs_rtgroup		*rtg = to_rtg(info->group);
864 
865 	/* Query the rtrmapbt */
866 	xfs_rtgroup_lock(rtg, XFS_RTGLOCK_RMAP | XFS_RTGLOCK_REFCOUNT);
867 	*curpp = xfs_rtrmapbt_init_cursor(tp, rtg);
868 	return xfs_rmap_query_range(*curpp, &info->low, &info->high,
869 			xfs_getfsmap_rtdev_rmapbt_helper, info);
870 }
871 
872 /* Execute a getfsmap query against the realtime device rmapbt. */
873 STATIC int
xfs_getfsmap_rtdev_rmapbt(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)874 xfs_getfsmap_rtdev_rmapbt(
875 	struct xfs_trans		*tp,
876 	const struct xfs_fsmap		*keys,
877 	struct xfs_getfsmap_info	*info)
878 {
879 	struct xfs_mount		*mp = tp->t_mountp;
880 	struct xfs_rtgroup		*rtg = NULL;
881 	struct xfs_btree_cur		*bt_cur = NULL;
882 	xfs_daddr_t			rtstart_daddr;
883 	xfs_rtblock_t			start_rtb;
884 	xfs_rtblock_t			end_rtb;
885 	xfs_rgnumber_t			start_rg, end_rg;
886 	uint64_t			eofs;
887 	int				error = 0;
888 
889 	eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rtstart + mp->m_sb.sb_rblocks);
890 	if (keys[0].fmr_physical >= eofs)
891 		return 0;
892 
893 	rtstart_daddr = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rtstart);
894 	if (keys[0].fmr_physical < rtstart_daddr) {
895 		struct xfs_fsmap_irec		frec = {
896 			.owner			= XFS_RMAP_OWN_FS,
897 			.len_daddr		= rtstart_daddr,
898 		};
899 
900 		/* Adjust the low key if we are continuing from where we left off. */
901 		if (keys[0].fmr_length > 0) {
902 			info->low_daddr = keys[0].fmr_physical + keys[0].fmr_length;
903 			return 0;
904 		}
905 
906 		/* Fabricate an rmap entry for space occupied by the data dev */
907 		error = xfs_getfsmap_helper(tp, info, &frec);
908 		if (error)
909 			return error;
910 	}
911 
912 	start_rtb = xfs_daddr_to_rtb(mp, rtstart_daddr + keys[0].fmr_physical);
913 	end_rtb = xfs_daddr_to_rtb(mp, rtstart_daddr +
914 			min(eofs - 1, keys[1].fmr_physical));
915 
916 	info->missing_owner = XFS_FMR_OWN_FREE;
917 
918 	/*
919 	 * Convert the fsmap low/high keys to rtgroup based keys.  Initialize
920 	 * low to the fsmap low key and max out the high key to the end
921 	 * of the rtgroup.
922 	 */
923 	info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
924 	error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
925 	if (error)
926 		return error;
927 	info->low.rm_blockcount = XFS_BB_TO_FSBT(mp, keys[0].fmr_length);
928 	xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
929 
930 	/* Adjust the low key if we are continuing from where we left off. */
931 	if (info->low.rm_blockcount == 0) {
932 		/* No previous record from which to continue */
933 	} else if (rmap_not_shareable(mp, &info->low)) {
934 		/* Last record seen was an unshareable extent */
935 		info->low.rm_owner = 0;
936 		info->low.rm_offset = 0;
937 
938 		start_rtb += info->low.rm_blockcount;
939 		if (xfs_rtb_to_daddr(mp, start_rtb) >= eofs)
940 			return 0;
941 	} else {
942 		/* Last record seen was a shareable file data extent */
943 		info->low.rm_offset += info->low.rm_blockcount;
944 	}
945 	info->low.rm_startblock = xfs_rtb_to_rgbno(mp, start_rtb);
946 
947 	info->high.rm_startblock = -1U;
948 	info->high.rm_owner = ULLONG_MAX;
949 	info->high.rm_offset = ULLONG_MAX;
950 	info->high.rm_blockcount = 0;
951 	info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
952 
953 	start_rg = xfs_rtb_to_rgno(mp, start_rtb);
954 	end_rg = xfs_rtb_to_rgno(mp, end_rtb);
955 
956 	while ((rtg = xfs_rtgroup_next_range(mp, rtg, start_rg, end_rg))) {
957 		/*
958 		 * Set the rtgroup high key from the fsmap high key if this
959 		 * is the last rtgroup that we're querying.
960 		 */
961 		info->group = rtg_group(rtg);
962 		if (rtg_rgno(rtg) == end_rg) {
963 			info->high.rm_startblock =
964 				xfs_rtb_to_rgbno(mp, end_rtb);
965 			info->high.rm_offset =
966 				XFS_BB_TO_FSBT(mp, keys[1].fmr_offset);
967 			error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
968 			if (error)
969 				break;
970 			xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
971 		}
972 
973 		if (bt_cur) {
974 			xfs_rtgroup_unlock(to_rtg(bt_cur->bc_group),
975 					XFS_RTGLOCK_RMAP |
976 					XFS_RTGLOCK_REFCOUNT);
977 			xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
978 			bt_cur = NULL;
979 		}
980 
981 		trace_xfs_fsmap_low_group_key(mp, info->dev, rtg_rgno(rtg),
982 				&info->low);
983 		trace_xfs_fsmap_high_group_key(mp, info->dev, rtg_rgno(rtg),
984 				&info->high);
985 
986 		error = xfs_getfsmap_rtdev_rmapbt_query(tp, info, &bt_cur);
987 		if (error)
988 			break;
989 
990 		/*
991 		 * Set the rtgroup low key to the start of the rtgroup prior to
992 		 * moving on to the next rtgroup.
993 		 */
994 		if (rtg_rgno(rtg) == start_rg)
995 			memset(&info->low, 0, sizeof(info->low));
996 
997 		/*
998 		 * If this is the last rtgroup, report any gap at the end of it
999 		 * before we drop the reference to the perag when the loop
1000 		 * terminates.
1001 		 */
1002 		if (rtg_rgno(rtg) == end_rg) {
1003 			info->last = true;
1004 			error = xfs_getfsmap_rtdev_rmapbt_helper(bt_cur,
1005 					&info->high, info);
1006 			if (error)
1007 				break;
1008 		}
1009 		info->group = NULL;
1010 	}
1011 
1012 	if (bt_cur) {
1013 		xfs_rtgroup_unlock(to_rtg(bt_cur->bc_group),
1014 				XFS_RTGLOCK_RMAP | XFS_RTGLOCK_REFCOUNT);
1015 		xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
1016 							 XFS_BTREE_NOERROR);
1017 	}
1018 
1019 	/* loop termination case */
1020 	if (rtg) {
1021 		info->group = NULL;
1022 		xfs_rtgroup_rele(rtg);
1023 	}
1024 
1025 	return error;
1026 }
1027 #endif /* CONFIG_XFS_RT */
1028 
1029 static uint32_t
xfs_getfsmap_device(struct xfs_mount * mp,enum xfs_device dev)1030 xfs_getfsmap_device(
1031 	struct xfs_mount	*mp,
1032 	enum xfs_device		dev)
1033 {
1034 	if (mp->m_sb.sb_rtstart)
1035 		return dev;
1036 
1037 	switch (dev) {
1038 	case XFS_DEV_DATA:
1039 		return new_encode_dev(mp->m_ddev_targp->bt_dev);
1040 	case XFS_DEV_LOG:
1041 		return new_encode_dev(mp->m_logdev_targp->bt_dev);
1042 	case XFS_DEV_RT:
1043 		if (!mp->m_rtdev_targp)
1044 			break;
1045 		return new_encode_dev(mp->m_rtdev_targp->bt_dev);
1046 	}
1047 
1048 	return -1;
1049 }
1050 
1051 /* Do we recognize the device? */
1052 STATIC bool
xfs_getfsmap_is_valid_device(struct xfs_mount * mp,struct xfs_fsmap * fm)1053 xfs_getfsmap_is_valid_device(
1054 	struct xfs_mount	*mp,
1055 	struct xfs_fsmap	*fm)
1056 {
1057 	return fm->fmr_device == 0 ||
1058 		fm->fmr_device == UINT_MAX ||
1059 		fm->fmr_device == xfs_getfsmap_device(mp, XFS_DEV_DATA) ||
1060 		fm->fmr_device == xfs_getfsmap_device(mp, XFS_DEV_LOG) ||
1061 		(mp->m_rtdev_targp &&
1062 		 fm->fmr_device == xfs_getfsmap_device(mp, XFS_DEV_RT));
1063 }
1064 
1065 /* Ensure that the low key is less than the high key. */
1066 STATIC bool
xfs_getfsmap_check_keys(struct xfs_fsmap * low_key,struct xfs_fsmap * high_key)1067 xfs_getfsmap_check_keys(
1068 	struct xfs_fsmap		*low_key,
1069 	struct xfs_fsmap		*high_key)
1070 {
1071 	if (low_key->fmr_flags & (FMR_OF_SPECIAL_OWNER | FMR_OF_EXTENT_MAP)) {
1072 		if (low_key->fmr_offset)
1073 			return false;
1074 	}
1075 	if (high_key->fmr_flags != -1U &&
1076 	    (high_key->fmr_flags & (FMR_OF_SPECIAL_OWNER |
1077 				    FMR_OF_EXTENT_MAP))) {
1078 		if (high_key->fmr_offset && high_key->fmr_offset != -1ULL)
1079 			return false;
1080 	}
1081 	if (high_key->fmr_length && high_key->fmr_length != -1ULL)
1082 		return false;
1083 
1084 	if (low_key->fmr_device > high_key->fmr_device)
1085 		return false;
1086 	if (low_key->fmr_device < high_key->fmr_device)
1087 		return true;
1088 
1089 	if (low_key->fmr_physical > high_key->fmr_physical)
1090 		return false;
1091 	if (low_key->fmr_physical < high_key->fmr_physical)
1092 		return true;
1093 
1094 	if (low_key->fmr_owner > high_key->fmr_owner)
1095 		return false;
1096 	if (low_key->fmr_owner < high_key->fmr_owner)
1097 		return true;
1098 
1099 	if (low_key->fmr_offset > high_key->fmr_offset)
1100 		return false;
1101 	if (low_key->fmr_offset < high_key->fmr_offset)
1102 		return true;
1103 
1104 	return false;
1105 }
1106 
1107 /*
1108  * There are only two devices if we didn't configure RT devices at build time.
1109  */
1110 #ifdef CONFIG_XFS_RT
1111 #define XFS_GETFSMAP_DEVS	3
1112 #else
1113 #define XFS_GETFSMAP_DEVS	2
1114 #endif /* CONFIG_XFS_RT */
1115 
1116 /*
1117  * Get filesystem's extents as described in head, and format for output. Fills
1118  * in the supplied records array until there are no more reverse mappings to
1119  * return or head.fmh_entries == head.fmh_count.  In the second case, this
1120  * function returns -ECANCELED to indicate that more records would have been
1121  * returned.
1122  *
1123  * Key to Confusion
1124  * ----------------
1125  * There are multiple levels of keys and counters at work here:
1126  * xfs_fsmap_head.fmh_keys	-- low and high fsmap keys passed in;
1127  *				   these reflect fs-wide sector addrs.
1128  * dkeys			-- fmh_keys used to query each device;
1129  *				   these are fmh_keys but w/ the low key
1130  *				   bumped up by fmr_length.
1131  * xfs_getfsmap_info.next_daddr	-- next disk addr we expect to see; this
1132  *				   is how we detect gaps in the fsmap
1133 				   records and report them.
1134  * xfs_getfsmap_info.low/high	-- per-AG low/high keys computed from
1135  *				   dkeys; used to query the metadata.
1136  */
1137 STATIC int
xfs_getfsmap(struct xfs_mount * mp,struct xfs_fsmap_head * head,struct fsmap * fsmap_recs)1138 xfs_getfsmap(
1139 	struct xfs_mount		*mp,
1140 	struct xfs_fsmap_head		*head,
1141 	struct fsmap			*fsmap_recs)
1142 {
1143 	struct xfs_trans		*tp = NULL;
1144 	struct xfs_fsmap		dkeys[2];	/* per-dev keys */
1145 	struct xfs_getfsmap_dev		handlers[XFS_GETFSMAP_DEVS];
1146 	struct xfs_getfsmap_info	info = {
1147 		.fsmap_recs		= fsmap_recs,
1148 		.head			= head,
1149 	};
1150 	bool				use_rmap;
1151 	int				i;
1152 	int				error = 0;
1153 
1154 	if (head->fmh_iflags & ~FMH_IF_VALID)
1155 		return -EINVAL;
1156 	if (!xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[0]) ||
1157 	    !xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[1]))
1158 		return -EINVAL;
1159 	if (!xfs_getfsmap_check_keys(&head->fmh_keys[0], &head->fmh_keys[1]))
1160 		return -EINVAL;
1161 
1162 	use_rmap = xfs_has_rmapbt(mp) &&
1163 		   has_capability_noaudit(current, CAP_SYS_ADMIN);
1164 	head->fmh_entries = 0;
1165 
1166 	/* Set up our device handlers. */
1167 	memset(handlers, 0, sizeof(handlers));
1168 	handlers[0].nr_sectors = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
1169 	handlers[0].dev = xfs_getfsmap_device(mp, XFS_DEV_DATA);
1170 	if (use_rmap)
1171 		handlers[0].fn = xfs_getfsmap_datadev_rmapbt;
1172 	else
1173 		handlers[0].fn = xfs_getfsmap_datadev_bnobt;
1174 	if (mp->m_logdev_targp != mp->m_ddev_targp) {
1175 		handlers[1].nr_sectors = XFS_FSB_TO_BB(mp,
1176 						       mp->m_sb.sb_logblocks);
1177 		handlers[1].dev = xfs_getfsmap_device(mp, XFS_DEV_LOG);
1178 		handlers[1].fn = xfs_getfsmap_logdev;
1179 	}
1180 #ifdef CONFIG_XFS_RT
1181 	/*
1182 	 * For zoned file systems there is no rtbitmap, so only support fsmap
1183 	 * if the callers is privileged enough to use the full rmap version.
1184 	 */
1185 	if (mp->m_rtdev_targp && (use_rmap || !xfs_has_zoned(mp))) {
1186 		handlers[2].nr_sectors = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
1187 		handlers[2].dev = xfs_getfsmap_device(mp, XFS_DEV_RT);
1188 		if (use_rmap)
1189 			handlers[2].fn = xfs_getfsmap_rtdev_rmapbt;
1190 		else
1191 			handlers[2].fn = xfs_getfsmap_rtdev_rtbitmap;
1192 	}
1193 #endif /* CONFIG_XFS_RT */
1194 
1195 	xfs_sort(handlers, XFS_GETFSMAP_DEVS, sizeof(struct xfs_getfsmap_dev),
1196 			xfs_getfsmap_dev_compare);
1197 
1198 	/*
1199 	 * To continue where we left off, we allow userspace to use the
1200 	 * last mapping from a previous call as the low key of the next.
1201 	 * This is identified by a non-zero length in the low key. We
1202 	 * have to increment the low key in this scenario to ensure we
1203 	 * don't return the same mapping again, and instead return the
1204 	 * very next mapping.
1205 	 *
1206 	 * If the low key mapping refers to file data, the same physical
1207 	 * blocks could be mapped to several other files/offsets.
1208 	 * According to rmapbt record ordering, the minimal next
1209 	 * possible record for the block range is the next starting
1210 	 * offset in the same inode. Therefore, each fsmap backend bumps
1211 	 * the file offset to continue the search appropriately.  For
1212 	 * all other low key mapping types (attr blocks, metadata), each
1213 	 * fsmap backend bumps the physical offset as there can be no
1214 	 * other mapping for the same physical block range.
1215 	 */
1216 	dkeys[0] = head->fmh_keys[0];
1217 	memset(&dkeys[1], 0xFF, sizeof(struct xfs_fsmap));
1218 
1219 	info.next_daddr = head->fmh_keys[0].fmr_physical +
1220 			  head->fmh_keys[0].fmr_length;
1221 
1222 	/* For each device we support... */
1223 	for (i = 0; i < XFS_GETFSMAP_DEVS; i++) {
1224 		/* Is this device within the range the user asked for? */
1225 		if (!handlers[i].fn)
1226 			continue;
1227 		if (head->fmh_keys[0].fmr_device > handlers[i].dev)
1228 			continue;
1229 		if (head->fmh_keys[1].fmr_device < handlers[i].dev)
1230 			break;
1231 
1232 		/*
1233 		 * If this device number matches the high key, we have to pass
1234 		 * the high key to the handler to limit the query results, and
1235 		 * set the end_daddr so that we can synthesize records at the
1236 		 * end of the query range or device.
1237 		 */
1238 		if (handlers[i].dev == head->fmh_keys[1].fmr_device) {
1239 			dkeys[1] = head->fmh_keys[1];
1240 			info.end_daddr = min(handlers[i].nr_sectors - 1,
1241 					     dkeys[1].fmr_physical);
1242 		} else {
1243 			info.end_daddr = handlers[i].nr_sectors - 1;
1244 		}
1245 
1246 		/*
1247 		 * If the device number exceeds the low key, zero out the low
1248 		 * key so that we get everything from the beginning.
1249 		 */
1250 		if (handlers[i].dev > head->fmh_keys[0].fmr_device)
1251 			memset(&dkeys[0], 0, sizeof(struct xfs_fsmap));
1252 
1253 		/*
1254 		 * Grab an empty transaction so that we can use its recursive
1255 		 * buffer locking abilities to detect cycles in the rmapbt
1256 		 * without deadlocking.
1257 		 */
1258 		error = xfs_trans_alloc_empty(mp, &tp);
1259 		if (error)
1260 			break;
1261 
1262 		info.dev = handlers[i].dev;
1263 		info.last = false;
1264 		info.group = NULL;
1265 		info.low_daddr = XFS_BUF_DADDR_NULL;
1266 		info.low.rm_blockcount = 0;
1267 		error = handlers[i].fn(tp, dkeys, &info);
1268 		if (error)
1269 			break;
1270 		xfs_trans_cancel(tp);
1271 		tp = NULL;
1272 		info.next_daddr = 0;
1273 	}
1274 
1275 	if (tp)
1276 		xfs_trans_cancel(tp);
1277 
1278 	/*
1279 	 * For internal RT device we need to report different synthetic devices
1280 	 * for a single physical device, and thus can't report the actual dev_t.
1281 	 */
1282 	if (!mp->m_sb.sb_rtstart)
1283 		head->fmh_oflags = FMH_OF_DEV_T;
1284 	return error;
1285 }
1286 
1287 int
xfs_ioc_getfsmap(struct xfs_inode * ip,struct fsmap_head __user * arg)1288 xfs_ioc_getfsmap(
1289 	struct xfs_inode	*ip,
1290 	struct fsmap_head	__user *arg)
1291 {
1292 	struct xfs_fsmap_head	xhead = {0};
1293 	struct fsmap_head	head;
1294 	struct fsmap		*recs;
1295 	unsigned int		count;
1296 	__u32			last_flags = 0;
1297 	bool			done = false;
1298 	int			error;
1299 
1300 	if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1301 		return -EFAULT;
1302 	if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1303 	    memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1304 		       sizeof(head.fmh_keys[0].fmr_reserved)) ||
1305 	    memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1306 		       sizeof(head.fmh_keys[1].fmr_reserved)))
1307 		return -EINVAL;
1308 
1309 	/*
1310 	 * Use an internal memory buffer so that we don't have to copy fsmap
1311 	 * data to userspace while holding locks.  Start by trying to allocate
1312 	 * up to 128k for the buffer, but fall back to a single page if needed.
1313 	 */
1314 	count = min_t(unsigned int, head.fmh_count,
1315 			131072 / sizeof(struct fsmap));
1316 	recs = kvcalloc(count, sizeof(struct fsmap), GFP_KERNEL);
1317 	if (!recs) {
1318 		count = min_t(unsigned int, head.fmh_count,
1319 				PAGE_SIZE / sizeof(struct fsmap));
1320 		recs = kvcalloc(count, sizeof(struct fsmap), GFP_KERNEL);
1321 		if (!recs)
1322 			return -ENOMEM;
1323 	}
1324 
1325 	xhead.fmh_iflags = head.fmh_iflags;
1326 	xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1327 	xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1328 
1329 	trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1330 	trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1331 
1332 	head.fmh_entries = 0;
1333 	do {
1334 		struct fsmap __user	*user_recs;
1335 		struct fsmap		*last_rec;
1336 
1337 		user_recs = &arg->fmh_recs[head.fmh_entries];
1338 		xhead.fmh_entries = 0;
1339 		xhead.fmh_count = min_t(unsigned int, count,
1340 					head.fmh_count - head.fmh_entries);
1341 
1342 		/* Run query, record how many entries we got. */
1343 		error = xfs_getfsmap(ip->i_mount, &xhead, recs);
1344 		switch (error) {
1345 		case 0:
1346 			/*
1347 			 * There are no more records in the result set.  Copy
1348 			 * whatever we got to userspace and break out.
1349 			 */
1350 			done = true;
1351 			break;
1352 		case -ECANCELED:
1353 			/*
1354 			 * The internal memory buffer is full.  Copy whatever
1355 			 * records we got to userspace and go again if we have
1356 			 * not yet filled the userspace buffer.
1357 			 */
1358 			error = 0;
1359 			break;
1360 		default:
1361 			goto out_free;
1362 		}
1363 		head.fmh_entries += xhead.fmh_entries;
1364 		head.fmh_oflags = xhead.fmh_oflags;
1365 
1366 		/*
1367 		 * If the caller wanted a record count or there aren't any
1368 		 * new records to return, we're done.
1369 		 */
1370 		if (head.fmh_count == 0 || xhead.fmh_entries == 0)
1371 			break;
1372 
1373 		/* Copy all the records we got out to userspace. */
1374 		if (copy_to_user(user_recs, recs,
1375 				 xhead.fmh_entries * sizeof(struct fsmap))) {
1376 			error = -EFAULT;
1377 			goto out_free;
1378 		}
1379 
1380 		/* Remember the last record flags we copied to userspace. */
1381 		last_rec = &recs[xhead.fmh_entries - 1];
1382 		last_flags = last_rec->fmr_flags;
1383 
1384 		/* Set up the low key for the next iteration. */
1385 		xfs_fsmap_to_internal(&xhead.fmh_keys[0], last_rec);
1386 		trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1387 	} while (!done && head.fmh_entries < head.fmh_count);
1388 
1389 	/*
1390 	 * If there are no more records in the query result set and we're not
1391 	 * in counting mode, mark the last record returned with the LAST flag.
1392 	 */
1393 	if (done && head.fmh_count > 0 && head.fmh_entries > 0) {
1394 		struct fsmap __user	*user_rec;
1395 
1396 		last_flags |= FMR_OF_LAST;
1397 		user_rec = &arg->fmh_recs[head.fmh_entries - 1];
1398 
1399 		if (copy_to_user(&user_rec->fmr_flags, &last_flags,
1400 					sizeof(last_flags))) {
1401 			error = -EFAULT;
1402 			goto out_free;
1403 		}
1404 	}
1405 
1406 	/* copy back header */
1407 	if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) {
1408 		error = -EFAULT;
1409 		goto out_free;
1410 	}
1411 
1412 out_free:
1413 	kvfree(recs);
1414 	return error;
1415 }
1416