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