blob: 24b3e53379bf311e7324199830e869a117cb3d9a [file] [log] [blame]
Darrick J. Wonge89c0412017-03-28 14:56:37 -07001/*
2 * Copyright (C) 2017 Oracle. All Rights Reserved.
3 *
4 * Author: Darrick J. Wong <darrick.wong@oracle.com>
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
10 *
11 * This program is distributed in the hope that it would be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write the Free Software Foundation,
18 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA.
19 */
20#include "xfs.h"
21#include "xfs_fs.h"
22#include "xfs_shared.h"
23#include "xfs_format.h"
24#include "xfs_log_format.h"
25#include "xfs_trans_resv.h"
26#include "xfs_sb.h"
27#include "xfs_mount.h"
28#include "xfs_defer.h"
29#include "xfs_inode.h"
30#include "xfs_trans.h"
31#include "xfs_error.h"
32#include "xfs_btree.h"
33#include "xfs_rmap_btree.h"
34#include "xfs_trace.h"
35#include "xfs_log.h"
36#include "xfs_rmap.h"
37#include "xfs_alloc.h"
38#include "xfs_bit.h"
39#include <linux/fsmap.h>
40#include "xfs_fsmap.h"
41#include "xfs_refcount.h"
42#include "xfs_refcount_btree.h"
43
44/* Convert an xfs_fsmap to an fsmap. */
45void
46xfs_fsmap_from_internal(
47 struct fsmap *dest,
48 struct xfs_fsmap *src)
49{
50 dest->fmr_device = src->fmr_device;
51 dest->fmr_flags = src->fmr_flags;
52 dest->fmr_physical = BBTOB(src->fmr_physical);
53 dest->fmr_owner = src->fmr_owner;
54 dest->fmr_offset = BBTOB(src->fmr_offset);
55 dest->fmr_length = BBTOB(src->fmr_length);
56 dest->fmr_reserved[0] = 0;
57 dest->fmr_reserved[1] = 0;
58 dest->fmr_reserved[2] = 0;
59}
60
61/* Convert an fsmap to an xfs_fsmap. */
62void
63xfs_fsmap_to_internal(
64 struct xfs_fsmap *dest,
65 struct fsmap *src)
66{
67 dest->fmr_device = src->fmr_device;
68 dest->fmr_flags = src->fmr_flags;
69 dest->fmr_physical = BTOBBT(src->fmr_physical);
70 dest->fmr_owner = src->fmr_owner;
71 dest->fmr_offset = BTOBBT(src->fmr_offset);
72 dest->fmr_length = BTOBBT(src->fmr_length);
73}
74
75/* Convert an fsmap owner into an rmapbt owner. */
76static int
77xfs_fsmap_owner_to_rmap(
78 struct xfs_rmap_irec *dest,
79 struct xfs_fsmap *src)
80{
81 if (!(src->fmr_flags & FMR_OF_SPECIAL_OWNER)) {
82 dest->rm_owner = src->fmr_owner;
83 return 0;
84 }
85
86 switch (src->fmr_owner) {
87 case 0: /* "lowest owner id possible" */
88 case -1ULL: /* "highest owner id possible" */
89 dest->rm_owner = 0;
90 break;
91 case XFS_FMR_OWN_FREE:
92 dest->rm_owner = XFS_RMAP_OWN_NULL;
93 break;
94 case XFS_FMR_OWN_UNKNOWN:
95 dest->rm_owner = XFS_RMAP_OWN_UNKNOWN;
96 break;
97 case XFS_FMR_OWN_FS:
98 dest->rm_owner = XFS_RMAP_OWN_FS;
99 break;
100 case XFS_FMR_OWN_LOG:
101 dest->rm_owner = XFS_RMAP_OWN_LOG;
102 break;
103 case XFS_FMR_OWN_AG:
104 dest->rm_owner = XFS_RMAP_OWN_AG;
105 break;
106 case XFS_FMR_OWN_INOBT:
107 dest->rm_owner = XFS_RMAP_OWN_INOBT;
108 break;
109 case XFS_FMR_OWN_INODES:
110 dest->rm_owner = XFS_RMAP_OWN_INODES;
111 break;
112 case XFS_FMR_OWN_REFC:
113 dest->rm_owner = XFS_RMAP_OWN_REFC;
114 break;
115 case XFS_FMR_OWN_COW:
116 dest->rm_owner = XFS_RMAP_OWN_COW;
117 break;
118 case XFS_FMR_OWN_DEFECTIVE: /* not implemented */
119 /* fall through */
120 default:
121 return -EINVAL;
122 }
123 return 0;
124}
125
126/* Convert an rmapbt owner into an fsmap owner. */
127static int
128xfs_fsmap_owner_from_rmap(
129 struct xfs_fsmap *dest,
130 struct xfs_rmap_irec *src)
131{
132 dest->fmr_flags = 0;
133 if (!XFS_RMAP_NON_INODE_OWNER(src->rm_owner)) {
134 dest->fmr_owner = src->rm_owner;
135 return 0;
136 }
137 dest->fmr_flags |= FMR_OF_SPECIAL_OWNER;
138
139 switch (src->rm_owner) {
140 case XFS_RMAP_OWN_FS:
141 dest->fmr_owner = XFS_FMR_OWN_FS;
142 break;
143 case XFS_RMAP_OWN_LOG:
144 dest->fmr_owner = XFS_FMR_OWN_LOG;
145 break;
146 case XFS_RMAP_OWN_AG:
147 dest->fmr_owner = XFS_FMR_OWN_AG;
148 break;
149 case XFS_RMAP_OWN_INOBT:
150 dest->fmr_owner = XFS_FMR_OWN_INOBT;
151 break;
152 case XFS_RMAP_OWN_INODES:
153 dest->fmr_owner = XFS_FMR_OWN_INODES;
154 break;
155 case XFS_RMAP_OWN_REFC:
156 dest->fmr_owner = XFS_FMR_OWN_REFC;
157 break;
158 case XFS_RMAP_OWN_COW:
159 dest->fmr_owner = XFS_FMR_OWN_COW;
160 break;
161 default:
162 return -EFSCORRUPTED;
163 }
164 return 0;
165}
166
167/* getfsmap query state */
168struct xfs_getfsmap_info {
169 struct xfs_fsmap_head *head;
170 xfs_fsmap_format_t formatter; /* formatting fn */
171 void *format_arg; /* format buffer */
172 struct xfs_buf *agf_bp; /* AGF, for refcount queries */
173 xfs_daddr_t next_daddr; /* next daddr we expect */
174 u64 missing_owner; /* owner of holes */
175 u32 dev; /* device id */
176 xfs_agnumber_t agno; /* AG number, if applicable */
177 struct xfs_rmap_irec low; /* low rmap key */
178 struct xfs_rmap_irec high; /* high rmap key */
179 bool last; /* last extent? */
180};
181
182/* Associate a device with a getfsmap handler. */
183struct xfs_getfsmap_dev {
184 u32 dev;
185 int (*fn)(struct xfs_trans *tp,
186 struct xfs_fsmap *keys,
187 struct xfs_getfsmap_info *info);
188};
189
190/* Compare two getfsmap device handlers. */
191static int
192xfs_getfsmap_dev_compare(
193 const void *p1,
194 const void *p2)
195{
196 const struct xfs_getfsmap_dev *d1 = p1;
197 const struct xfs_getfsmap_dev *d2 = p2;
198
199 return d1->dev - d2->dev;
200}
201
202/* Decide if this mapping is shared. */
203STATIC int
204xfs_getfsmap_is_shared(
205 struct xfs_trans *tp,
206 struct xfs_getfsmap_info *info,
207 struct xfs_rmap_irec *rec,
208 bool *stat)
209{
210 struct xfs_mount *mp = tp->t_mountp;
211 struct xfs_btree_cur *cur;
212 xfs_agblock_t fbno;
213 xfs_extlen_t flen;
214 int error;
215
216 *stat = false;
217 if (!xfs_sb_version_hasreflink(&mp->m_sb))
218 return 0;
219 /* rt files will have agno set to NULLAGNUMBER */
220 if (info->agno == NULLAGNUMBER)
221 return 0;
222
223 /* Are there any shared blocks here? */
224 flen = 0;
225 cur = xfs_refcountbt_init_cursor(mp, tp, info->agf_bp,
226 info->agno, NULL);
227
228 error = xfs_refcount_find_shared(cur, rec->rm_startblock,
229 rec->rm_blockcount, &fbno, &flen, false);
230
231 xfs_btree_del_cursor(cur, error ? XFS_BTREE_ERROR : XFS_BTREE_NOERROR);
232 if (error)
233 return error;
234
235 *stat = flen > 0;
236 return 0;
237}
238
239/*
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;
266 return XFS_BTREE_QUERY_RANGE_CONTINUE;
267 }
268
269 /* Are we just counting mappings? */
270 if (info->head->fmh_count == 0) {
271 if (rec_daddr > info->next_daddr)
272 info->head->fmh_entries++;
273
274 if (info->last)
275 return XFS_BTREE_QUERY_RANGE_CONTINUE;
276
277 info->head->fmh_entries++;
278
279 rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
280 if (info->next_daddr < rec_daddr)
281 info->next_daddr = rec_daddr;
282 return XFS_BTREE_QUERY_RANGE_CONTINUE;
283 }
284
285 /*
286 * If the record starts past the last physical block we saw,
287 * then we've found a gap. Report the gap as being owned by
288 * whatever the caller specified is the missing owner.
289 */
290 if (rec_daddr > info->next_daddr) {
291 if (info->head->fmh_entries >= info->head->fmh_count)
292 return XFS_BTREE_QUERY_RANGE_ABORT;
293
294 fmr.fmr_device = info->dev;
295 fmr.fmr_physical = info->next_daddr;
296 fmr.fmr_owner = info->missing_owner;
297 fmr.fmr_offset = 0;
298 fmr.fmr_length = rec_daddr - info->next_daddr;
299 fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
300 error = info->formatter(&fmr, info->format_arg);
301 if (error)
302 return error;
303 info->head->fmh_entries++;
304 }
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)
311 return XFS_BTREE_QUERY_RANGE_ABORT;
312
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 }
335 error = info->formatter(&fmr, info->format_arg);
336 if (error)
337 return error;
338 info->head->fmh_entries++;
339
340out:
341 rec_daddr += XFS_FSB_TO_BB(mp, rec->rm_blockcount);
342 if (info->next_daddr < rec_daddr)
343 info->next_daddr = rec_daddr;
344 return XFS_BTREE_QUERY_RANGE_CONTINUE;
345}
346
347/* Transform a rmapbt irec into a fsmap */
348STATIC int
349xfs_getfsmap_datadev_helper(
350 struct xfs_btree_cur *cur,
351 struct xfs_rmap_irec *rec,
352 void *priv)
353{
354 struct xfs_mount *mp = cur->bc_mp;
355 struct xfs_getfsmap_info *info = priv;
356 xfs_fsblock_t fsb;
357 xfs_daddr_t rec_daddr;
358
359 fsb = XFS_AGB_TO_FSB(mp, cur->bc_private.a.agno, rec->rm_startblock);
360 rec_daddr = XFS_FSB_TO_DADDR(mp, fsb);
361
362 return xfs_getfsmap_helper(cur->bc_tp, info, rec, rec_daddr);
363}
364
365/* Set rmap flags based on the getfsmap flags */
366static void
367xfs_getfsmap_set_irec_flags(
368 struct xfs_rmap_irec *irec,
369 struct xfs_fsmap *fmr)
370{
371 irec->rm_flags = 0;
372 if (fmr->fmr_flags & FMR_OF_ATTR_FORK)
373 irec->rm_flags |= XFS_RMAP_ATTR_FORK;
374 if (fmr->fmr_flags & FMR_OF_EXTENT_MAP)
375 irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
376 if (fmr->fmr_flags & FMR_OF_PREALLOC)
377 irec->rm_flags |= XFS_RMAP_UNWRITTEN;
378}
379
380/* Execute a getfsmap query against the log device. */
381STATIC int
382xfs_getfsmap_logdev(
383 struct xfs_trans *tp,
384 struct xfs_fsmap *keys,
385 struct xfs_getfsmap_info *info)
386{
387 struct xfs_mount *mp = tp->t_mountp;
388 struct xfs_rmap_irec rmap;
389 int error;
390
391 /* Set up search keys */
392 info->low.rm_startblock = XFS_BB_TO_FSBT(mp, keys[0].fmr_physical);
393 info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
394 error = xfs_fsmap_owner_to_rmap(&info->low, keys);
395 if (error)
396 return error;
397 info->low.rm_blockcount = 0;
398 xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
399
400 error = xfs_fsmap_owner_to_rmap(&info->high, keys + 1);
401 if (error)
402 return error;
403 info->high.rm_startblock = -1U;
404 info->high.rm_owner = ULLONG_MAX;
405 info->high.rm_offset = ULLONG_MAX;
406 info->high.rm_blockcount = 0;
407 info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
408 info->missing_owner = XFS_FMR_OWN_FREE;
409
410 trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
411 trace_xfs_fsmap_high_key(mp, info->dev, info->agno, &info->high);
412
413 if (keys[0].fmr_physical > 0)
414 return 0;
415
416 /* Fabricate an rmap entry for the external log device. */
417 rmap.rm_startblock = 0;
418 rmap.rm_blockcount = mp->m_sb.sb_logblocks;
419 rmap.rm_owner = XFS_RMAP_OWN_LOG;
420 rmap.rm_offset = 0;
421 rmap.rm_flags = 0;
422
423 return xfs_getfsmap_helper(tp, info, &rmap, 0);
424}
425
426/* Execute a getfsmap query against the regular data device. */
427STATIC int
428__xfs_getfsmap_datadev(
429 struct xfs_trans *tp,
430 struct xfs_fsmap *keys,
431 struct xfs_getfsmap_info *info,
432 int (*query_fn)(struct xfs_trans *,
433 struct xfs_getfsmap_info *,
434 struct xfs_btree_cur **,
435 void *),
436 void *priv)
437{
438 struct xfs_mount *mp = tp->t_mountp;
439 struct xfs_btree_cur *bt_cur = NULL;
440 xfs_fsblock_t start_fsb;
441 xfs_fsblock_t end_fsb;
442 xfs_agnumber_t start_ag;
443 xfs_agnumber_t end_ag;
444 xfs_daddr_t eofs;
445 int error = 0;
446
447 eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
448 if (keys[0].fmr_physical >= eofs)
449 return 0;
450 if (keys[1].fmr_physical >= eofs)
451 keys[1].fmr_physical = eofs - 1;
452 start_fsb = XFS_DADDR_TO_FSB(mp, keys[0].fmr_physical);
453 end_fsb = XFS_DADDR_TO_FSB(mp, keys[1].fmr_physical);
454
455 /*
456 * Convert the fsmap low/high keys to AG based keys. Initialize
457 * low to the fsmap low key and max out the high key to the end
458 * of the AG.
459 */
460 info->low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb);
461 info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
462 error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
463 if (error)
464 return error;
465 info->low.rm_blockcount = 0;
466 xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
467
468 info->high.rm_startblock = -1U;
469 info->high.rm_owner = ULLONG_MAX;
470 info->high.rm_offset = ULLONG_MAX;
471 info->high.rm_blockcount = 0;
472 info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
473
474 start_ag = XFS_FSB_TO_AGNO(mp, start_fsb);
475 end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);
476
477 /* Query each AG */
478 for (info->agno = start_ag; info->agno <= end_ag; info->agno++) {
479 /*
480 * Set the AG high key from the fsmap high key if this
481 * is the last AG that we're querying.
482 */
483 if (info->agno == end_ag) {
484 info->high.rm_startblock = XFS_FSB_TO_AGBNO(mp,
485 end_fsb);
486 info->high.rm_offset = XFS_BB_TO_FSBT(mp,
487 keys[1].fmr_offset);
488 error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
489 if (error)
490 goto err;
491 xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
492 }
493
494 if (bt_cur) {
495 xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
496 bt_cur = NULL;
497 xfs_trans_brelse(tp, info->agf_bp);
498 info->agf_bp = NULL;
499 }
500
501 error = xfs_alloc_read_agf(mp, tp, info->agno, 0,
502 &info->agf_bp);
503 if (error)
504 goto err;
505
506 trace_xfs_fsmap_low_key(mp, info->dev, info->agno, &info->low);
507 trace_xfs_fsmap_high_key(mp, info->dev, info->agno,
508 &info->high);
509
510 error = query_fn(tp, info, &bt_cur, priv);
511 if (error)
512 goto err;
513
514 /*
515 * Set the AG low key to the start of the AG prior to
516 * moving on to the next AG.
517 */
518 if (info->agno == start_ag) {
519 info->low.rm_startblock = 0;
520 info->low.rm_owner = 0;
521 info->low.rm_offset = 0;
522 info->low.rm_flags = 0;
523 }
524 }
525
526 /* Report any gap at the end of the AG */
527 info->last = true;
528 error = query_fn(tp, info, &bt_cur, priv);
529 if (error)
530 goto err;
531
532err:
533 if (bt_cur)
534 xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
535 XFS_BTREE_NOERROR);
536 if (info->agf_bp) {
537 xfs_trans_brelse(tp, info->agf_bp);
538 info->agf_bp = NULL;
539 }
540
541 return error;
542}
543
544/* Actually query the rmap btree. */
545STATIC int
546xfs_getfsmap_datadev_rmapbt_query(
547 struct xfs_trans *tp,
548 struct xfs_getfsmap_info *info,
549 struct xfs_btree_cur **curpp,
550 void *priv)
551{
552 /* Report any gap at the end of the last AG. */
553 if (info->last)
554 return xfs_getfsmap_datadev_helper(*curpp, &info->high, info);
555
556 /* Allocate cursor for this AG and query_range it. */
557 *curpp = xfs_rmapbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
558 info->agno);
559 return xfs_rmap_query_range(*curpp, &info->low, &info->high,
560 xfs_getfsmap_datadev_helper, info);
561}
562
563/* Execute a getfsmap query against the regular data device rmapbt. */
564STATIC int
565xfs_getfsmap_datadev_rmapbt(
566 struct xfs_trans *tp,
567 struct xfs_fsmap *keys,
568 struct xfs_getfsmap_info *info)
569{
570 info->missing_owner = XFS_FMR_OWN_FREE;
571 return __xfs_getfsmap_datadev(tp, keys, info,
572 xfs_getfsmap_datadev_rmapbt_query, NULL);
573}
574
575/* Do we recognize the device? */
576STATIC bool
577xfs_getfsmap_is_valid_device(
578 struct xfs_mount *mp,
579 struct xfs_fsmap *fm)
580{
581 if (fm->fmr_device == 0 || fm->fmr_device == UINT_MAX ||
582 fm->fmr_device == new_encode_dev(mp->m_ddev_targp->bt_dev))
583 return true;
584 if (mp->m_logdev_targp &&
585 fm->fmr_device == new_encode_dev(mp->m_logdev_targp->bt_dev))
586 return true;
587 return false;
588}
589
590/* Ensure that the low key is less than the high key. */
591STATIC bool
592xfs_getfsmap_check_keys(
593 struct xfs_fsmap *low_key,
594 struct xfs_fsmap *high_key)
595{
596 if (low_key->fmr_device > high_key->fmr_device)
597 return false;
598 if (low_key->fmr_device < high_key->fmr_device)
599 return true;
600
601 if (low_key->fmr_physical > high_key->fmr_physical)
602 return false;
603 if (low_key->fmr_physical < high_key->fmr_physical)
604 return true;
605
606 if (low_key->fmr_owner > high_key->fmr_owner)
607 return false;
608 if (low_key->fmr_owner < high_key->fmr_owner)
609 return true;
610
611 if (low_key->fmr_offset > high_key->fmr_offset)
612 return false;
613 if (low_key->fmr_offset < high_key->fmr_offset)
614 return true;
615
616 return false;
617}
618
619#define XFS_GETFSMAP_DEVS 2
620/*
621 * Get filesystem's extents as described in head, and format for
622 * output. Calls formatter to fill the user's buffer until all
623 * extents are mapped, until the passed-in head->fmh_count slots have
624 * been filled, or until the formatter short-circuits the loop, if it
625 * is tracking filled-in extents on its own.
626 *
627 * Key to Confusion
628 * ----------------
629 * There are multiple levels of keys and counters at work here:
630 * xfs_fsmap_head.fmh_keys -- low and high fsmap keys passed in;
631 * these reflect fs-wide sector addrs.
632 * dkeys -- fmh_keys used to query each device;
633 * these are fmh_keys but w/ the low key
634 * bumped up by fmr_length.
635 * xfs_getfsmap_info.next_daddr -- next disk addr we expect to see; this
636 * is how we detect gaps in the fsmap
637 records and report them.
638 * xfs_getfsmap_info.low/high -- per-AG low/high keys computed from
639 * dkeys; used to query the metadata.
640 */
641int
642xfs_getfsmap(
643 struct xfs_mount *mp,
644 struct xfs_fsmap_head *head,
645 xfs_fsmap_format_t formatter,
646 void *arg)
647{
648 struct xfs_trans *tp = NULL;
649 struct xfs_fsmap dkeys[2]; /* per-dev keys */
650 struct xfs_getfsmap_dev handlers[XFS_GETFSMAP_DEVS];
651 struct xfs_getfsmap_info info = {0};
652 int i;
653 int error = 0;
654
655 if (!xfs_sb_version_hasrmapbt(&mp->m_sb))
656 return -EOPNOTSUPP;
657 if (head->fmh_iflags & ~FMH_IF_VALID)
658 return -EINVAL;
659 if (!xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[0]) ||
660 !xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[1]))
661 return -EINVAL;
662
663 head->fmh_entries = 0;
664
665 /* Set up our device handlers. */
666 memset(handlers, 0, sizeof(handlers));
667 handlers[0].dev = new_encode_dev(mp->m_ddev_targp->bt_dev);
668 handlers[0].fn = xfs_getfsmap_datadev_rmapbt;
669 if (mp->m_logdev_targp != mp->m_ddev_targp) {
670 handlers[1].dev = new_encode_dev(mp->m_logdev_targp->bt_dev);
671 handlers[1].fn = xfs_getfsmap_logdev;
672 }
673
674 xfs_sort(handlers, XFS_GETFSMAP_DEVS, sizeof(struct xfs_getfsmap_dev),
675 xfs_getfsmap_dev_compare);
676
677 /*
678 * To continue where we left off, we allow userspace to use the
679 * last mapping from a previous call as the low key of the next.
680 * This is identified by a non-zero length in the low key. We
681 * have to increment the low key in this scenario to ensure we
682 * don't return the same mapping again, and instead return the
683 * very next mapping.
684 *
685 * If the low key mapping refers to file data, the same physical
686 * blocks could be mapped to several other files/offsets.
687 * According to rmapbt record ordering, the minimal next
688 * possible record for the block range is the next starting
689 * offset in the same inode. Therefore, bump the file offset to
690 * continue the search appropriately. For all other low key
691 * mapping types (attr blocks, metadata), bump the physical
692 * offset as there can be no other mapping for the same physical
693 * block range.
694 */
695 dkeys[0] = head->fmh_keys[0];
696 if (dkeys[0].fmr_flags & (FMR_OF_SPECIAL_OWNER | FMR_OF_EXTENT_MAP)) {
697 dkeys[0].fmr_physical += dkeys[0].fmr_length;
698 dkeys[0].fmr_owner = 0;
699 if (dkeys[0].fmr_offset)
700 return -EINVAL;
701 } else
702 dkeys[0].fmr_offset += dkeys[0].fmr_length;
703 dkeys[0].fmr_length = 0;
704 memset(&dkeys[1], 0xFF, sizeof(struct xfs_fsmap));
705
706 if (!xfs_getfsmap_check_keys(dkeys, &head->fmh_keys[1]))
707 return -EINVAL;
708
709 info.next_daddr = head->fmh_keys[0].fmr_physical +
710 head->fmh_keys[0].fmr_length;
711 info.formatter = formatter;
712 info.format_arg = arg;
713 info.head = head;
714
715 /* For each device we support... */
716 for (i = 0; i < XFS_GETFSMAP_DEVS; i++) {
717 /* Is this device within the range the user asked for? */
718 if (!handlers[i].fn)
719 continue;
720 if (head->fmh_keys[0].fmr_device > handlers[i].dev)
721 continue;
722 if (head->fmh_keys[1].fmr_device < handlers[i].dev)
723 break;
724
725 /*
726 * If this device number matches the high key, we have
727 * to pass the high key to the handler to limit the
728 * query results. If the device number exceeds the
729 * low key, zero out the low key so that we get
730 * everything from the beginning.
731 */
732 if (handlers[i].dev == head->fmh_keys[1].fmr_device)
733 dkeys[1] = head->fmh_keys[1];
734 if (handlers[i].dev > head->fmh_keys[0].fmr_device)
735 memset(&dkeys[0], 0, sizeof(struct xfs_fsmap));
736
737 error = xfs_trans_alloc_empty(mp, &tp);
738 if (error)
739 break;
740
741 info.dev = handlers[i].dev;
742 info.last = false;
743 info.agno = NULLAGNUMBER;
744 error = handlers[i].fn(tp, dkeys, &info);
745 if (error)
746 break;
747 xfs_trans_cancel(tp);
748 tp = NULL;
749 info.next_daddr = 0;
750 }
751
752 if (tp)
753 xfs_trans_cancel(tp);
754 head->fmh_oflags = FMH_OF_DEV_T;
755 return error;
756}