mm: fix exec activate_mm vs TLB shootdown and lazy tlb switching race
[linux/fpc-iii.git] / fs / xfs / xfs_fsmap.c
blobe7622e0841868daba1a5cef66e4def18c0e7b539
1 /*
2 * Copyright (C) 2017 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
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.
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.
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.
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"
46 /* Convert an xfs_fsmap to an fsmap. */
47 void
48 xfs_fsmap_from_internal(
49 struct fsmap *dest,
50 struct xfs_fsmap *src)
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;
63 /* Convert an fsmap to an xfs_fsmap. */
64 void
65 xfs_fsmap_to_internal(
66 struct xfs_fsmap *dest,
67 struct fsmap *src)
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);
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)
83 if (!(src->fmr_flags & FMR_OF_SPECIAL_OWNER)) {
84 dest->rm_owner = src->fmr_owner;
85 return 0;
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;
125 return 0;
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)
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;
139 dest->fmr_flags |= FMR_OF_SPECIAL_OWNER;
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;
169 return 0;
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? */
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);
195 /* Compare two getfsmap device handlers. */
196 static int
197 xfs_getfsmap_dev_compare(
198 const void *p1,
199 const void *p2)
201 const struct xfs_getfsmap_dev *d1 = p1;
202 const struct xfs_getfsmap_dev *d2 = p2;
204 return d1->dev - d2->dev;
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)
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;
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;
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);
233 error = xfs_refcount_find_shared(cur, rec->rm_startblock,
234 rec->rm_blockcount, &fbno, &flen, false);
236 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
237 if (error)
238 return error;
240 *stat = flen > 0;
241 return 0;
245 * Format a reverse mapping for getfsmap, having translated rm_startblock
246 * into the appropriate daddr units.
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)
255 struct xfs_fsmap fmr;
256 struct xfs_mount *mp = tp->t_mountp;
257 bool shared;
258 int error;
260 if (fatal_signal_pending(current))
261 return -EINTR;
264 * Filter out records that start before our startpoint, if the
265 * caller requested that.
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;
274 /* Are we just counting mappings? */
275 if (info->head->fmh_count == 0) {
276 if (info->head->fmh_entries == UINT_MAX)
277 return -ECANCELED;
279 if (rec_daddr > info->next_daddr)
280 info->head->fmh_entries++;
282 if (info->last)
283 return XFS_BTREE_QUERY_RANGE_CONTINUE;
285 info->head->fmh_entries++;
287 rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
288 if (info->next_daddr < rec_daddr)
289 info->next_daddr = rec_daddr;
290 return XFS_BTREE_QUERY_RANGE_CONTINUE;
294 * If the record starts past the last physical block we saw,
295 * then we've found a gap. Report the gap as being owned by
296 * whatever the caller specified is the missing owner.
298 if (rec_daddr > info->next_daddr) {
299 if (info->head->fmh_entries >= info->head->fmh_count)
300 return XFS_BTREE_QUERY_RANGE_ABORT;
302 fmr.fmr_device = info->dev;
303 fmr.fmr_physical = info->next_daddr;
304 fmr.fmr_owner = info->missing_owner;
305 fmr.fmr_offset = 0;
306 fmr.fmr_length = rec_daddr - info->next_daddr;
307 fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
308 error = info->formatter(&fmr, info->format_arg);
309 if (error)
310 return error;
311 info->head->fmh_entries++;
314 if (info->last)
315 goto out;
317 /* Fill out the extent we found */
318 if (info->head->fmh_entries >= info->head->fmh_count)
319 return XFS_BTREE_QUERY_RANGE_ABORT;
321 trace_xfs_fsmap_mapping(mp, info->dev, info->agno, rec);
323 fmr.fmr_device = info->dev;
324 fmr.fmr_physical = rec_daddr;
325 error = xfs_fsmap_owner_from_rmap(&fmr, rec);
326 if (error)
327 return error;
328 fmr.fmr_offset = XFS_FSB_TO_BB(mp, rec->rm_offset);
329 fmr.fmr_length = XFS_FSB_TO_BB(mp, rec->rm_blockcount);
330 if (rec->rm_flags & XFS_RMAP_UNWRITTEN)
331 fmr.fmr_flags |= FMR_OF_PREALLOC;
332 if (rec->rm_flags & XFS_RMAP_ATTR_FORK)
333 fmr.fmr_flags |= FMR_OF_ATTR_FORK;
334 if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
335 fmr.fmr_flags |= FMR_OF_EXTENT_MAP;
336 if (fmr.fmr_flags == 0) {
337 error = xfs_getfsmap_is_shared(tp, info, rec, &shared);
338 if (error)
339 return error;
340 if (shared)
341 fmr.fmr_flags |= FMR_OF_SHARED;
343 error = info->formatter(&fmr, info->format_arg);
344 if (error)
345 return error;
346 info->head->fmh_entries++;
348 out:
349 rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
350 if (info->next_daddr < rec_daddr)
351 info->next_daddr = rec_daddr;
352 return XFS_BTREE_QUERY_RANGE_CONTINUE;
355 /* Transform a rmapbt irec into a fsmap */
356 STATIC int
357 xfs_getfsmap_datadev_helper(
358 struct xfs_btree_cur *cur,
359 struct xfs_rmap_irec *rec,
360 void *priv)
362 struct xfs_mount *mp = cur->bc_mp;
363 struct xfs_getfsmap_info *info = priv;
364 xfs_fsblock_t fsb;
365 xfs_daddr_t rec_daddr;
367 fsb = XFS_AGB_TO_FSB(mp, cur->bc_private.a.agno, rec->rm_startblock);
368 rec_daddr = XFS_FSB_TO_DADDR(mp, fsb);
370 return xfs_getfsmap_helper(cur->bc_tp, info, rec, rec_daddr);
373 /* Transform a bnobt irec into a fsmap */
374 STATIC int
375 xfs_getfsmap_datadev_bnobt_helper(
376 struct xfs_btree_cur *cur,
377 struct xfs_alloc_rec_incore *rec,
378 void *priv)
380 struct xfs_mount *mp = cur->bc_mp;
381 struct xfs_getfsmap_info *info = priv;
382 struct xfs_rmap_irec irec;
383 xfs_daddr_t rec_daddr;
385 rec_daddr = XFS_AGB_TO_DADDR(mp, cur->bc_private.a.agno,
386 rec->ar_startblock);
388 irec.rm_startblock = rec->ar_startblock;
389 irec.rm_blockcount = rec->ar_blockcount;
390 irec.rm_owner = XFS_RMAP_OWN_NULL; /* "free" */
391 irec.rm_offset = 0;
392 irec.rm_flags = 0;
394 return xfs_getfsmap_helper(cur->bc_tp, info, &irec, rec_daddr);
397 /* Set rmap flags based on the getfsmap flags */
398 static void
399 xfs_getfsmap_set_irec_flags(
400 struct xfs_rmap_irec *irec,
401 struct xfs_fsmap *fmr)
403 irec->rm_flags = 0;
404 if (fmr->fmr_flags & FMR_OF_ATTR_FORK)
405 irec->rm_flags |= XFS_RMAP_ATTR_FORK;
406 if (fmr->fmr_flags & FMR_OF_EXTENT_MAP)
407 irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
408 if (fmr->fmr_flags & FMR_OF_PREALLOC)
409 irec->rm_flags |= XFS_RMAP_UNWRITTEN;
412 /* Execute a getfsmap query against the log device. */
413 STATIC int
414 xfs_getfsmap_logdev(
415 struct xfs_trans *tp,
416 struct xfs_fsmap *keys,
417 struct xfs_getfsmap_info *info)
419 struct xfs_mount *mp = tp->t_mountp;
420 struct xfs_rmap_irec rmap;
421 int error;
423 /* Set up search keys */
424 info->low.rm_startblock = XFS_BB_TO_FSBT(mp, keys[0].fmr_physical);
425 info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
426 error = xfs_fsmap_owner_to_rmap(&info->low, keys);
427 if (error)
428 return error;
429 info->low.rm_blockcount = 0;
430 xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
432 error = xfs_fsmap_owner_to_rmap(&info->high, keys + 1);
433 if (error)
434 return error;
435 info->high.rm_startblock = -1U;
436 info->high.rm_owner = ULLONG_MAX;
437 info->high.rm_offset = ULLONG_MAX;
438 info->high.rm_blockcount = 0;
439 info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
440 info->missing_owner = XFS_FMR_OWN_FREE;
442 trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
443 trace_xfs_fsmap_high_key(mp, info->dev, info->agno, &info->high);
445 if (keys[0].fmr_physical > 0)
446 return 0;
448 /* Fabricate an rmap entry for the external log device. */
449 rmap.rm_startblock = 0;
450 rmap.rm_blockcount = mp->m_sb.sb_logblocks;
451 rmap.rm_owner = XFS_RMAP_OWN_LOG;
452 rmap.rm_offset = 0;
453 rmap.rm_flags = 0;
455 return xfs_getfsmap_helper(tp, info, &rmap, 0);
458 #ifdef CONFIG_XFS_RT
459 /* Transform a rtbitmap "record" into a fsmap */
460 STATIC int
461 xfs_getfsmap_rtdev_rtbitmap_helper(
462 struct xfs_trans *tp,
463 struct xfs_rtalloc_rec *rec,
464 void *priv)
466 struct xfs_mount *mp = tp->t_mountp;
467 struct xfs_getfsmap_info *info = priv;
468 struct xfs_rmap_irec irec;
469 xfs_daddr_t rec_daddr;
471 rec_daddr = XFS_FSB_TO_BB(mp, rec->ar_startblock);
473 irec.rm_startblock = rec->ar_startblock;
474 irec.rm_blockcount = rec->ar_blockcount;
475 irec.rm_owner = XFS_RMAP_OWN_NULL; /* "free" */
476 irec.rm_offset = 0;
477 irec.rm_flags = 0;
479 return xfs_getfsmap_helper(tp, info, &irec, rec_daddr);
482 /* Execute a getfsmap query against the realtime device. */
483 STATIC int
484 __xfs_getfsmap_rtdev(
485 struct xfs_trans *tp,
486 struct xfs_fsmap *keys,
487 int (*query_fn)(struct xfs_trans *,
488 struct xfs_getfsmap_info *),
489 struct xfs_getfsmap_info *info)
491 struct xfs_mount *mp = tp->t_mountp;
492 xfs_fsblock_t start_fsb;
493 xfs_fsblock_t end_fsb;
494 xfs_daddr_t eofs;
495 int error = 0;
497 eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
498 if (keys[0].fmr_physical >= eofs)
499 return 0;
500 if (keys[1].fmr_physical >= eofs)
501 keys[1].fmr_physical = eofs - 1;
502 start_fsb = XFS_BB_TO_FSBT(mp, keys[0].fmr_physical);
503 end_fsb = XFS_BB_TO_FSB(mp, keys[1].fmr_physical);
505 /* Set up search keys */
506 info->low.rm_startblock = start_fsb;
507 error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
508 if (error)
509 return error;
510 info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
511 info->low.rm_blockcount = 0;
512 xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
514 info->high.rm_startblock = end_fsb;
515 error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
516 if (error)
517 return error;
518 info->high.rm_offset = XFS_BB_TO_FSBT(mp, keys[1].fmr_offset);
519 info->high.rm_blockcount = 0;
520 xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
522 trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
523 trace_xfs_fsmap_high_key(mp, info->dev, info->agno, &info->high);
525 return query_fn(tp, info);
528 /* Actually query the realtime bitmap. */
529 STATIC int
530 xfs_getfsmap_rtdev_rtbitmap_query(
531 struct xfs_trans *tp,
532 struct xfs_getfsmap_info *info)
534 struct xfs_rtalloc_rec alow;
535 struct xfs_rtalloc_rec ahigh;
536 int error;
538 xfs_ilock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
540 alow.ar_startblock = info->low.rm_startblock;
541 ahigh.ar_startblock = info->high.rm_startblock;
542 error = xfs_rtalloc_query_range(tp, &alow, &ahigh,
543 xfs_getfsmap_rtdev_rtbitmap_helper, info);
544 if (error)
545 goto err;
547 /* Report any gaps at the end of the rtbitmap */
548 info->last = true;
549 error = xfs_getfsmap_rtdev_rtbitmap_helper(tp, &ahigh, info);
550 if (error)
551 goto err;
552 err:
553 xfs_iunlock(tp->t_mountp->m_rbmip, XFS_ILOCK_SHARED);
554 return error;
557 /* Execute a getfsmap query against the realtime device rtbitmap. */
558 STATIC int
559 xfs_getfsmap_rtdev_rtbitmap(
560 struct xfs_trans *tp,
561 struct xfs_fsmap *keys,
562 struct xfs_getfsmap_info *info)
564 info->missing_owner = XFS_FMR_OWN_UNKNOWN;
565 return __xfs_getfsmap_rtdev(tp, keys, xfs_getfsmap_rtdev_rtbitmap_query,
566 info);
568 #endif /* CONFIG_XFS_RT */
570 /* Execute a getfsmap query against the regular data device. */
571 STATIC int
572 __xfs_getfsmap_datadev(
573 struct xfs_trans *tp,
574 struct xfs_fsmap *keys,
575 struct xfs_getfsmap_info *info,
576 int (*query_fn)(struct xfs_trans *,
577 struct xfs_getfsmap_info *,
578 struct xfs_btree_cur **,
579 void *),
580 void *priv)
582 struct xfs_mount *mp = tp->t_mountp;
583 struct xfs_btree_cur *bt_cur = NULL;
584 xfs_fsblock_t start_fsb;
585 xfs_fsblock_t end_fsb;
586 xfs_agnumber_t start_ag;
587 xfs_agnumber_t end_ag;
588 xfs_daddr_t eofs;
589 int error = 0;
591 eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
592 if (keys[0].fmr_physical >= eofs)
593 return 0;
594 if (keys[1].fmr_physical >= eofs)
595 keys[1].fmr_physical = eofs - 1;
596 start_fsb = XFS_DADDR_TO_FSB(mp, keys[0].fmr_physical);
597 end_fsb = XFS_DADDR_TO_FSB(mp, keys[1].fmr_physical);
600 * Convert the fsmap low/high keys to AG based keys. Initialize
601 * low to the fsmap low key and max out the high key to the end
602 * of the AG.
604 info->low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb);
605 info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
606 error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
607 if (error)
608 return error;
609 info->low.rm_blockcount = 0;
610 xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
612 info->high.rm_startblock = -1U;
613 info->high.rm_owner = ULLONG_MAX;
614 info->high.rm_offset = ULLONG_MAX;
615 info->high.rm_blockcount = 0;
616 info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
618 start_ag = XFS_FSB_TO_AGNO(mp, start_fsb);
619 end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);
621 /* Query each AG */
622 for (info->agno = start_ag; info->agno <= end_ag; info->agno++) {
624 * Set the AG high key from the fsmap high key if this
625 * is the last AG that we're querying.
627 if (info->agno == end_ag) {
628 info->high.rm_startblock = XFS_FSB_TO_AGBNO(mp,
629 end_fsb);
630 info->high.rm_offset = XFS_BB_TO_FSBT(mp,
631 keys[1].fmr_offset);
632 error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
633 if (error)
634 goto err;
635 xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
638 if (bt_cur) {
639 xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
640 bt_cur = NULL;
641 xfs_trans_brelse(tp, info->agf_bp);
642 info->agf_bp = NULL;
645 error = xfs_alloc_read_agf(mp, tp, info->agno, 0,
646 &info->agf_bp);
647 if (error)
648 goto err;
650 trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
651 trace_xfs_fsmap_high_key(mp, info->dev, info->agno,
652 &info->high);
654 error = query_fn(tp, info, &bt_cur, priv);
655 if (error)
656 goto err;
659 * Set the AG low key to the start of the AG prior to
660 * moving on to the next AG.
662 if (info->agno == start_ag) {
663 info->low.rm_startblock = 0;
664 info->low.rm_owner = 0;
665 info->low.rm_offset = 0;
666 info->low.rm_flags = 0;
670 /* Report any gap at the end of the AG */
671 info->last = true;
672 error = query_fn(tp, info, &bt_cur, priv);
673 if (error)
674 goto err;
676 err:
677 if (bt_cur)
678 xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
679 XFS_BTREE_NOERROR);
680 if (info->agf_bp) {
681 xfs_trans_brelse(tp, info->agf_bp);
682 info->agf_bp = NULL;
685 return error;
688 /* Actually query the rmap btree. */
689 STATIC int
690 xfs_getfsmap_datadev_rmapbt_query(
691 struct xfs_trans *tp,
692 struct xfs_getfsmap_info *info,
693 struct xfs_btree_cur **curpp,
694 void *priv)
696 /* Report any gap at the end of the last AG. */
697 if (info->last)
698 return xfs_getfsmap_datadev_helper(*curpp, &info->high, info);
700 /* Allocate cursor for this AG and query_range it. */
701 *curpp = xfs_rmapbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
702 info->agno);
703 return xfs_rmap_query_range(*curpp, &info->low, &info->high,
704 xfs_getfsmap_datadev_helper, info);
707 /* Execute a getfsmap query against the regular data device rmapbt. */
708 STATIC int
709 xfs_getfsmap_datadev_rmapbt(
710 struct xfs_trans *tp,
711 struct xfs_fsmap *keys,
712 struct xfs_getfsmap_info *info)
714 info->missing_owner = XFS_FMR_OWN_FREE;
715 return __xfs_getfsmap_datadev(tp, keys, info,
716 xfs_getfsmap_datadev_rmapbt_query, NULL);
719 /* Actually query the bno btree. */
720 STATIC int
721 xfs_getfsmap_datadev_bnobt_query(
722 struct xfs_trans *tp,
723 struct xfs_getfsmap_info *info,
724 struct xfs_btree_cur **curpp,
725 void *priv)
727 struct xfs_alloc_rec_incore *key = priv;
729 /* Report any gap at the end of the last AG. */
730 if (info->last)
731 return xfs_getfsmap_datadev_bnobt_helper(*curpp, &key[1], info);
733 /* Allocate cursor for this AG and query_range it. */
734 *curpp = xfs_allocbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
735 info->agno, XFS_BTNUM_BNO);
736 key->ar_startblock = info->low.rm_startblock;
737 key[1].ar_startblock = info->high.rm_startblock;
738 return xfs_alloc_query_range(*curpp, key, &key[1],
739 xfs_getfsmap_datadev_bnobt_helper, info);
742 /* Execute a getfsmap query against the regular data device's bnobt. */
743 STATIC int
744 xfs_getfsmap_datadev_bnobt(
745 struct xfs_trans *tp,
746 struct xfs_fsmap *keys,
747 struct xfs_getfsmap_info *info)
749 struct xfs_alloc_rec_incore akeys[2];
751 info->missing_owner = XFS_FMR_OWN_UNKNOWN;
752 return __xfs_getfsmap_datadev(tp, keys, info,
753 xfs_getfsmap_datadev_bnobt_query, &akeys[0]);
756 /* Do we recognize the device? */
757 STATIC bool
758 xfs_getfsmap_is_valid_device(
759 struct xfs_mount *mp,
760 struct xfs_fsmap *fm)
762 if (fm->fmr_device == 0 || fm->fmr_device == UINT_MAX ||
763 fm->fmr_device == new_encode_dev(mp->m_ddev_targp->bt_dev))
764 return true;
765 if (mp->m_logdev_targp &&
766 fm->fmr_device == new_encode_dev(mp->m_logdev_targp->bt_dev))
767 return true;
768 if (mp->m_rtdev_targp &&
769 fm->fmr_device == new_encode_dev(mp->m_rtdev_targp->bt_dev))
770 return true;
771 return false;
774 /* Ensure that the low key is less than the high key. */
775 STATIC bool
776 xfs_getfsmap_check_keys(
777 struct xfs_fsmap *low_key,
778 struct xfs_fsmap *high_key)
780 if (low_key->fmr_device > high_key->fmr_device)
781 return false;
782 if (low_key->fmr_device < high_key->fmr_device)
783 return true;
785 if (low_key->fmr_physical > high_key->fmr_physical)
786 return false;
787 if (low_key->fmr_physical < high_key->fmr_physical)
788 return true;
790 if (low_key->fmr_owner > high_key->fmr_owner)
791 return false;
792 if (low_key->fmr_owner < high_key->fmr_owner)
793 return true;
795 if (low_key->fmr_offset > high_key->fmr_offset)
796 return false;
797 if (low_key->fmr_offset < high_key->fmr_offset)
798 return true;
800 return false;
804 * There are only two devices if we didn't configure RT devices at build time.
806 #ifdef CONFIG_XFS_RT
807 #define XFS_GETFSMAP_DEVS 3
808 #else
809 #define XFS_GETFSMAP_DEVS 2
810 #endif /* CONFIG_XFS_RT */
813 * Get filesystem's extents as described in head, and format for
814 * output. Calls formatter to fill the user's buffer until all
815 * extents are mapped, until the passed-in head->fmh_count slots have
816 * been filled, or until the formatter short-circuits the loop, if it
817 * is tracking filled-in extents on its own.
819 * Key to Confusion
820 * ----------------
821 * There are multiple levels of keys and counters at work here:
822 * xfs_fsmap_head.fmh_keys -- low and high fsmap keys passed in;
823 * these reflect fs-wide sector addrs.
824 * dkeys -- fmh_keys used to query each device;
825 * these are fmh_keys but w/ the low key
826 * bumped up by fmr_length.
827 * xfs_getfsmap_info.next_daddr -- next disk addr we expect to see; this
828 * is how we detect gaps in the fsmap
829 records and report them.
830 * xfs_getfsmap_info.low/high -- per-AG low/high keys computed from
831 * dkeys; used to query the metadata.
834 xfs_getfsmap(
835 struct xfs_mount *mp,
836 struct xfs_fsmap_head *head,
837 xfs_fsmap_format_t formatter,
838 void *arg)
840 struct xfs_trans *tp = NULL;
841 struct xfs_fsmap dkeys[2]; /* per-dev keys */
842 struct xfs_getfsmap_dev handlers[XFS_GETFSMAP_DEVS];
843 struct xfs_getfsmap_info info = { NULL };
844 bool use_rmap;
845 int i;
846 int error = 0;
848 if (head->fmh_iflags & ~FMH_IF_VALID)
849 return -EINVAL;
850 if (!xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[0]) ||
851 !xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[1]))
852 return -EINVAL;
854 use_rmap = capable(CAP_SYS_ADMIN) &&
855 xfs_sb_version_hasrmapbt(&mp->m_sb);
856 head->fmh_entries = 0;
858 /* Set up our device handlers. */
859 memset(handlers, 0, sizeof(handlers));
860 handlers[0].dev = new_encode_dev(mp->m_ddev_targp->bt_dev);
861 if (use_rmap)
862 handlers[0].fn = xfs_getfsmap_datadev_rmapbt;
863 else
864 handlers[0].fn = xfs_getfsmap_datadev_bnobt;
865 if (mp->m_logdev_targp != mp->m_ddev_targp) {
866 handlers[1].dev = new_encode_dev(mp->m_logdev_targp->bt_dev);
867 handlers[1].fn = xfs_getfsmap_logdev;
869 #ifdef CONFIG_XFS_RT
870 if (mp->m_rtdev_targp) {
871 handlers[2].dev = new_encode_dev(mp->m_rtdev_targp->bt_dev);
872 handlers[2].fn = xfs_getfsmap_rtdev_rtbitmap;
874 #endif /* CONFIG_XFS_RT */
876 xfs_sort(handlers, XFS_GETFSMAP_DEVS, sizeof(struct xfs_getfsmap_dev),
877 xfs_getfsmap_dev_compare);
880 * To continue where we left off, we allow userspace to use the
881 * last mapping from a previous call as the low key of the next.
882 * This is identified by a non-zero length in the low key. We
883 * have to increment the low key in this scenario to ensure we
884 * don't return the same mapping again, and instead return the
885 * very next mapping.
887 * If the low key mapping refers to file data, the same physical
888 * blocks could be mapped to several other files/offsets.
889 * According to rmapbt record ordering, the minimal next
890 * possible record for the block range is the next starting
891 * offset in the same inode. Therefore, bump the file offset to
892 * continue the search appropriately. For all other low key
893 * mapping types (attr blocks, metadata), bump the physical
894 * offset as there can be no other mapping for the same physical
895 * block range.
897 dkeys[0] = head->fmh_keys[0];
898 if (dkeys[0].fmr_flags & (FMR_OF_SPECIAL_OWNER | FMR_OF_EXTENT_MAP)) {
899 dkeys[0].fmr_physical += dkeys[0].fmr_length;
900 dkeys[0].fmr_owner = 0;
901 if (dkeys[0].fmr_offset)
902 return -EINVAL;
903 } else
904 dkeys[0].fmr_offset += dkeys[0].fmr_length;
905 dkeys[0].fmr_length = 0;
906 memset(&dkeys[1], 0xFF, sizeof(struct xfs_fsmap));
908 if (!xfs_getfsmap_check_keys(dkeys, &head->fmh_keys[1]))
909 return -EINVAL;
911 info.next_daddr = head->fmh_keys[0].fmr_physical +
912 head->fmh_keys[0].fmr_length;
913 info.formatter = formatter;
914 info.format_arg = arg;
915 info.head = head;
917 /* For each device we support... */
918 for (i = 0; i < XFS_GETFSMAP_DEVS; i++) {
919 /* Is this device within the range the user asked for? */
920 if (!handlers[i].fn)
921 continue;
922 if (head->fmh_keys[0].fmr_device > handlers[i].dev)
923 continue;
924 if (head->fmh_keys[1].fmr_device < handlers[i].dev)
925 break;
928 * If this device number matches the high key, we have
929 * to pass the high key to the handler to limit the
930 * query results. If the device number exceeds the
931 * low key, zero out the low key so that we get
932 * everything from the beginning.
934 if (handlers[i].dev == head->fmh_keys[1].fmr_device)
935 dkeys[1] = head->fmh_keys[1];
936 if (handlers[i].dev > head->fmh_keys[0].fmr_device)
937 memset(&dkeys[0], 0, sizeof(struct xfs_fsmap));
939 error = xfs_trans_alloc_empty(mp, &tp);
940 if (error)
941 break;
943 info.dev = handlers[i].dev;
944 info.last = false;
945 info.agno = NULLAGNUMBER;
946 error = handlers[i].fn(tp, dkeys, &info);
947 if (error)
948 break;
949 xfs_trans_cancel(tp);
950 tp = NULL;
951 info.next_daddr = 0;
954 if (tp)
955 xfs_trans_cancel(tp);
956 head->fmh_oflags = FMH_OF_DEV_T;
957 return error;