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