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