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_rtbitmap.h"
27 #include "xfs_ag.h"
28
29 /* Convert an xfs_fsmap to an fsmap. */
30 static void
xfs_fsmap_from_internal(struct fsmap * dest,struct xfs_fsmap * src)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 static void
xfs_fsmap_to_internal(struct xfs_fsmap * dest,struct fsmap * src)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
xfs_fsmap_owner_to_rmap(struct xfs_rmap_irec * dest,const struct xfs_fsmap * src)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 = src->fmr_owner;
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
xfs_fsmap_owner_from_rmap(struct xfs_fsmap * dest,const struct xfs_rmap_irec * src)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 /* daddr of high fsmap key, or the last daddr on the device */
166 xfs_daddr_t end_daddr;
167 u64 missing_owner; /* owner of holes */
168 u32 dev; /* device id */
169 /*
170 * Low rmap key for the query. If low.rm_blockcount is nonzero, this
171 * is the second (or later) call to retrieve the recordset in pieces.
172 * xfs_getfsmap_rec_before_start will compare all records retrieved
173 * by the rmapbt query to filter out any records that start before
174 * the last record.
175 */
176 struct xfs_rmap_irec low;
177 struct xfs_rmap_irec high; /* high rmap key */
178 bool last; /* last extent? */
179 };
180
181 /* Associate a device with a getfsmap handler. */
182 struct xfs_getfsmap_dev {
183 u32 dev;
184 int (*fn)(struct xfs_trans *tp,
185 const struct xfs_fsmap *keys,
186 struct xfs_getfsmap_info *info);
187 sector_t nr_sectors;
188 };
189
190 /* Compare two getfsmap device handlers. */
191 static int
xfs_getfsmap_dev_compare(const void * p1,const void * p2)192 xfs_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. */
203 STATIC int
xfs_getfsmap_is_shared(struct xfs_trans * tp,struct xfs_getfsmap_info * info,const struct xfs_rmap_irec * rec,bool * stat)204 xfs_getfsmap_is_shared(
205 struct xfs_trans *tp,
206 struct xfs_getfsmap_info *info,
207 const 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_has_reflink(mp))
218 return 0;
219 /* rt files will have no perag structure */
220 if (!info->pag)
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, info->pag);
226
227 error = xfs_refcount_find_shared(cur, rec->rm_startblock,
228 rec->rm_blockcount, &fbno, &flen, false);
229
230 xfs_btree_del_cursor(cur, error);
231 if (error)
232 return error;
233
234 *stat = flen > 0;
235 return 0;
236 }
237
238 static inline void
xfs_getfsmap_format(struct xfs_mount * mp,struct xfs_fsmap * xfm,struct xfs_getfsmap_info * info)239 xfs_getfsmap_format(
240 struct xfs_mount *mp,
241 struct xfs_fsmap *xfm,
242 struct xfs_getfsmap_info *info)
243 {
244 struct fsmap *rec;
245
246 trace_xfs_getfsmap_mapping(mp, xfm);
247
248 rec = &info->fsmap_recs[info->head->fmh_entries++];
249 xfs_fsmap_from_internal(rec, xfm);
250 }
251
252 static inline bool
xfs_getfsmap_rec_before_start(struct xfs_getfsmap_info * info,const struct xfs_rmap_irec * rec,xfs_daddr_t rec_daddr)253 xfs_getfsmap_rec_before_start(
254 struct xfs_getfsmap_info *info,
255 const struct xfs_rmap_irec *rec,
256 xfs_daddr_t rec_daddr)
257 {
258 if (info->low_daddr != XFS_BUF_DADDR_NULL)
259 return rec_daddr < info->low_daddr;
260 if (info->low.rm_blockcount)
261 return xfs_rmap_compare(rec, &info->low) < 0;
262 return false;
263 }
264
265 /*
266 * Format a reverse mapping for getfsmap, having translated rm_startblock
267 * into the appropriate daddr units. Pass in a nonzero @len_daddr if the
268 * length could be larger than rm_blockcount in struct xfs_rmap_irec.
269 */
270 STATIC int
xfs_getfsmap_helper(struct xfs_trans * tp,struct xfs_getfsmap_info * info,const struct xfs_rmap_irec * rec,xfs_daddr_t rec_daddr,xfs_daddr_t len_daddr)271 xfs_getfsmap_helper(
272 struct xfs_trans *tp,
273 struct xfs_getfsmap_info *info,
274 const struct xfs_rmap_irec *rec,
275 xfs_daddr_t rec_daddr,
276 xfs_daddr_t len_daddr)
277 {
278 struct xfs_fsmap fmr;
279 struct xfs_mount *mp = tp->t_mountp;
280 bool shared;
281 int error;
282
283 if (fatal_signal_pending(current))
284 return -EINTR;
285
286 if (len_daddr == 0)
287 len_daddr = XFS_FSB_TO_BB(mp, rec->rm_blockcount);
288
289 /*
290 * Filter out records that start before our startpoint, if the
291 * caller requested that.
292 */
293 if (xfs_getfsmap_rec_before_start(info, rec, rec_daddr)) {
294 rec_daddr += len_daddr;
295 if (info->next_daddr < rec_daddr)
296 info->next_daddr = rec_daddr;
297 return 0;
298 }
299
300 /*
301 * For an info->last query, we're looking for a gap between the last
302 * mapping emitted and the high key specified by userspace. If the
303 * user's query spans less than 1 fsblock, then info->high and
304 * info->low will have the same rm_startblock, which causes rec_daddr
305 * and next_daddr to be the same. Therefore, use the end_daddr that
306 * we calculated from userspace's high key to synthesize the record.
307 * Note that if the btree query found a mapping, there won't be a gap.
308 */
309 if (info->last && info->end_daddr != XFS_BUF_DADDR_NULL)
310 rec_daddr = info->end_daddr + 1;
311
312 /* Are we just counting mappings? */
313 if (info->head->fmh_count == 0) {
314 if (info->head->fmh_entries == UINT_MAX)
315 return -ECANCELED;
316
317 if (rec_daddr > info->next_daddr)
318 info->head->fmh_entries++;
319
320 if (info->last)
321 return 0;
322
323 info->head->fmh_entries++;
324
325 rec_daddr += len_daddr;
326 if (info->next_daddr < rec_daddr)
327 info->next_daddr = rec_daddr;
328 return 0;
329 }
330
331 /*
332 * If the record starts past the last physical block we saw,
333 * then we've found a gap. Report the gap as being owned by
334 * whatever the caller specified is the missing owner.
335 */
336 if (rec_daddr > info->next_daddr) {
337 if (info->head->fmh_entries >= info->head->fmh_count)
338 return -ECANCELED;
339
340 fmr.fmr_device = info->dev;
341 fmr.fmr_physical = info->next_daddr;
342 fmr.fmr_owner = info->missing_owner;
343 fmr.fmr_offset = 0;
344 fmr.fmr_length = rec_daddr - info->next_daddr;
345 fmr.fmr_flags = FMR_OF_SPECIAL_OWNER;
346 xfs_getfsmap_format(mp, &fmr, info);
347 }
348
349 if (info->last)
350 goto out;
351
352 /* Fill out the extent we found */
353 if (info->head->fmh_entries >= info->head->fmh_count)
354 return -ECANCELED;
355
356 trace_xfs_fsmap_mapping(mp, info->dev,
357 info->pag ? info->pag->pag_agno : NULLAGNUMBER, rec);
358
359 fmr.fmr_device = info->dev;
360 fmr.fmr_physical = rec_daddr;
361 error = xfs_fsmap_owner_from_rmap(&fmr, rec);
362 if (error)
363 return error;
364 fmr.fmr_offset = XFS_FSB_TO_BB(mp, rec->rm_offset);
365 fmr.fmr_length = len_daddr;
366 if (rec->rm_flags & XFS_RMAP_UNWRITTEN)
367 fmr.fmr_flags |= FMR_OF_PREALLOC;
368 if (rec->rm_flags & XFS_RMAP_ATTR_FORK)
369 fmr.fmr_flags |= FMR_OF_ATTR_FORK;
370 if (rec->rm_flags & XFS_RMAP_BMBT_BLOCK)
371 fmr.fmr_flags |= FMR_OF_EXTENT_MAP;
372 if (fmr.fmr_flags == 0) {
373 error = xfs_getfsmap_is_shared(tp, info, rec, &shared);
374 if (error)
375 return error;
376 if (shared)
377 fmr.fmr_flags |= FMR_OF_SHARED;
378 }
379
380 xfs_getfsmap_format(mp, &fmr, info);
381 out:
382 rec_daddr += len_daddr;
383 if (info->next_daddr < rec_daddr)
384 info->next_daddr = rec_daddr;
385 return 0;
386 }
387
388 /* Transform a rmapbt irec into a fsmap */
389 STATIC int
xfs_getfsmap_datadev_helper(struct xfs_btree_cur * cur,const struct xfs_rmap_irec * rec,void * priv)390 xfs_getfsmap_datadev_helper(
391 struct xfs_btree_cur *cur,
392 const struct xfs_rmap_irec *rec,
393 void *priv)
394 {
395 struct xfs_mount *mp = cur->bc_mp;
396 struct xfs_getfsmap_info *info = priv;
397 xfs_fsblock_t fsb;
398 xfs_daddr_t rec_daddr;
399
400 fsb = XFS_AGB_TO_FSB(mp, cur->bc_ag.pag->pag_agno, rec->rm_startblock);
401 rec_daddr = XFS_FSB_TO_DADDR(mp, fsb);
402
403 return xfs_getfsmap_helper(cur->bc_tp, info, rec, rec_daddr, 0);
404 }
405
406 /* Transform a bnobt irec into a fsmap */
407 STATIC int
xfs_getfsmap_datadev_bnobt_helper(struct xfs_btree_cur * cur,const struct xfs_alloc_rec_incore * rec,void * priv)408 xfs_getfsmap_datadev_bnobt_helper(
409 struct xfs_btree_cur *cur,
410 const struct xfs_alloc_rec_incore *rec,
411 void *priv)
412 {
413 struct xfs_mount *mp = cur->bc_mp;
414 struct xfs_getfsmap_info *info = priv;
415 struct xfs_rmap_irec irec;
416 xfs_daddr_t rec_daddr;
417
418 rec_daddr = XFS_AGB_TO_DADDR(mp, cur->bc_ag.pag->pag_agno,
419 rec->ar_startblock);
420
421 irec.rm_startblock = rec->ar_startblock;
422 irec.rm_blockcount = rec->ar_blockcount;
423 irec.rm_owner = XFS_RMAP_OWN_NULL; /* "free" */
424 irec.rm_offset = 0;
425 irec.rm_flags = 0;
426
427 return xfs_getfsmap_helper(cur->bc_tp, info, &irec, rec_daddr, 0);
428 }
429
430 /* Set rmap flags based on the getfsmap flags */
431 static void
xfs_getfsmap_set_irec_flags(struct xfs_rmap_irec * irec,const struct xfs_fsmap * fmr)432 xfs_getfsmap_set_irec_flags(
433 struct xfs_rmap_irec *irec,
434 const struct xfs_fsmap *fmr)
435 {
436 irec->rm_flags = 0;
437 if (fmr->fmr_flags & FMR_OF_ATTR_FORK)
438 irec->rm_flags |= XFS_RMAP_ATTR_FORK;
439 if (fmr->fmr_flags & FMR_OF_EXTENT_MAP)
440 irec->rm_flags |= XFS_RMAP_BMBT_BLOCK;
441 if (fmr->fmr_flags & FMR_OF_PREALLOC)
442 irec->rm_flags |= XFS_RMAP_UNWRITTEN;
443 }
444
445 static inline bool
rmap_not_shareable(struct xfs_mount * mp,const struct xfs_rmap_irec * r)446 rmap_not_shareable(struct xfs_mount *mp, const struct xfs_rmap_irec *r)
447 {
448 if (!xfs_has_reflink(mp))
449 return true;
450 if (XFS_RMAP_NON_INODE_OWNER(r->rm_owner))
451 return true;
452 if (r->rm_flags & (XFS_RMAP_ATTR_FORK | XFS_RMAP_BMBT_BLOCK |
453 XFS_RMAP_UNWRITTEN))
454 return true;
455 return false;
456 }
457
458 /* Execute a getfsmap query against the regular data device. */
459 STATIC int
__xfs_getfsmap_datadev(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info,int (* query_fn)(struct xfs_trans *,struct xfs_getfsmap_info *,struct xfs_btree_cur **,void *),void * priv)460 __xfs_getfsmap_datadev(
461 struct xfs_trans *tp,
462 const struct xfs_fsmap *keys,
463 struct xfs_getfsmap_info *info,
464 int (*query_fn)(struct xfs_trans *,
465 struct xfs_getfsmap_info *,
466 struct xfs_btree_cur **,
467 void *),
468 void *priv)
469 {
470 struct xfs_mount *mp = tp->t_mountp;
471 struct xfs_perag *pag;
472 struct xfs_btree_cur *bt_cur = NULL;
473 xfs_fsblock_t start_fsb;
474 xfs_fsblock_t end_fsb;
475 xfs_agnumber_t start_ag;
476 xfs_agnumber_t end_ag;
477 uint64_t eofs;
478 int error = 0;
479
480 eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
481 if (keys[0].fmr_physical >= eofs)
482 return 0;
483 start_fsb = XFS_DADDR_TO_FSB(mp, keys[0].fmr_physical);
484 end_fsb = XFS_DADDR_TO_FSB(mp, min(eofs - 1, keys[1].fmr_physical));
485
486 /*
487 * Convert the fsmap low/high keys to AG based keys. Initialize
488 * low to the fsmap low key and max out the high key to the end
489 * of the AG.
490 */
491 info->low.rm_offset = XFS_BB_TO_FSBT(mp, keys[0].fmr_offset);
492 error = xfs_fsmap_owner_to_rmap(&info->low, &keys[0]);
493 if (error)
494 return error;
495 info->low.rm_blockcount = XFS_BB_TO_FSBT(mp, keys[0].fmr_length);
496 xfs_getfsmap_set_irec_flags(&info->low, &keys[0]);
497
498 /* Adjust the low key if we are continuing from where we left off. */
499 if (info->low.rm_blockcount == 0) {
500 /* No previous record from which to continue */
501 } else if (rmap_not_shareable(mp, &info->low)) {
502 /* Last record seen was an unshareable extent */
503 info->low.rm_owner = 0;
504 info->low.rm_offset = 0;
505
506 start_fsb += info->low.rm_blockcount;
507 if (XFS_FSB_TO_DADDR(mp, start_fsb) >= eofs)
508 return 0;
509 } else {
510 /* Last record seen was a shareable file data extent */
511 info->low.rm_offset += info->low.rm_blockcount;
512 }
513 info->low.rm_startblock = XFS_FSB_TO_AGBNO(mp, start_fsb);
514
515 info->high.rm_startblock = -1U;
516 info->high.rm_owner = ULLONG_MAX;
517 info->high.rm_offset = ULLONG_MAX;
518 info->high.rm_blockcount = 0;
519 info->high.rm_flags = XFS_RMAP_KEY_FLAGS | XFS_RMAP_REC_FLAGS;
520
521 start_ag = XFS_FSB_TO_AGNO(mp, start_fsb);
522 end_ag = XFS_FSB_TO_AGNO(mp, end_fsb);
523
524 for_each_perag_range(mp, start_ag, end_ag, pag) {
525 /*
526 * Set the AG high key from the fsmap high key if this
527 * is the last AG that we're querying.
528 */
529 info->pag = pag;
530 if (pag->pag_agno == end_ag) {
531 info->high.rm_startblock = XFS_FSB_TO_AGBNO(mp,
532 end_fsb);
533 info->high.rm_offset = XFS_BB_TO_FSBT(mp,
534 keys[1].fmr_offset);
535 error = xfs_fsmap_owner_to_rmap(&info->high, &keys[1]);
536 if (error)
537 break;
538 xfs_getfsmap_set_irec_flags(&info->high, &keys[1]);
539 }
540
541 if (bt_cur) {
542 xfs_btree_del_cursor(bt_cur, XFS_BTREE_NOERROR);
543 bt_cur = NULL;
544 xfs_trans_brelse(tp, info->agf_bp);
545 info->agf_bp = NULL;
546 }
547
548 error = xfs_alloc_read_agf(pag, tp, 0, &info->agf_bp);
549 if (error)
550 break;
551
552 trace_xfs_fsmap_low_key(mp, info->dev, pag->pag_agno,
553 &info->low);
554 trace_xfs_fsmap_high_key(mp, info->dev, pag->pag_agno,
555 &info->high);
556
557 error = query_fn(tp, info, &bt_cur, priv);
558 if (error)
559 break;
560
561 /*
562 * Set the AG low key to the start of the AG prior to
563 * moving on to the next AG.
564 */
565 if (pag->pag_agno == start_ag)
566 memset(&info->low, 0, sizeof(info->low));
567
568 /*
569 * If this is the last AG, report any gap at the end of it
570 * before we drop the reference to the perag when the loop
571 * terminates.
572 */
573 if (pag->pag_agno == end_ag) {
574 info->last = true;
575 error = query_fn(tp, info, &bt_cur, priv);
576 if (error)
577 break;
578 }
579 info->pag = NULL;
580 }
581
582 if (bt_cur)
583 xfs_btree_del_cursor(bt_cur, error < 0 ? XFS_BTREE_ERROR :
584 XFS_BTREE_NOERROR);
585 if (info->agf_bp) {
586 xfs_trans_brelse(tp, info->agf_bp);
587 info->agf_bp = NULL;
588 }
589 if (info->pag) {
590 xfs_perag_rele(info->pag);
591 info->pag = NULL;
592 } else if (pag) {
593 /* loop termination case */
594 xfs_perag_rele(pag);
595 }
596
597 return error;
598 }
599
600 /* Actually query the rmap btree. */
601 STATIC int
xfs_getfsmap_datadev_rmapbt_query(struct xfs_trans * tp,struct xfs_getfsmap_info * info,struct xfs_btree_cur ** curpp,void * priv)602 xfs_getfsmap_datadev_rmapbt_query(
603 struct xfs_trans *tp,
604 struct xfs_getfsmap_info *info,
605 struct xfs_btree_cur **curpp,
606 void *priv)
607 {
608 /* Report any gap at the end of the last AG. */
609 if (info->last)
610 return xfs_getfsmap_datadev_helper(*curpp, &info->high, info);
611
612 /* Allocate cursor for this AG and query_range it. */
613 *curpp = xfs_rmapbt_init_cursor(tp->t_mountp, tp, info->agf_bp,
614 info->pag);
615 return xfs_rmap_query_range(*curpp, &info->low, &info->high,
616 xfs_getfsmap_datadev_helper, info);
617 }
618
619 /* Execute a getfsmap query against the regular data device rmapbt. */
620 STATIC int
xfs_getfsmap_datadev_rmapbt(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)621 xfs_getfsmap_datadev_rmapbt(
622 struct xfs_trans *tp,
623 const struct xfs_fsmap *keys,
624 struct xfs_getfsmap_info *info)
625 {
626 info->missing_owner = XFS_FMR_OWN_FREE;
627 return __xfs_getfsmap_datadev(tp, keys, info,
628 xfs_getfsmap_datadev_rmapbt_query, NULL);
629 }
630
631 /* Actually query the bno btree. */
632 STATIC int
xfs_getfsmap_datadev_bnobt_query(struct xfs_trans * tp,struct xfs_getfsmap_info * info,struct xfs_btree_cur ** curpp,void * priv)633 xfs_getfsmap_datadev_bnobt_query(
634 struct xfs_trans *tp,
635 struct xfs_getfsmap_info *info,
636 struct xfs_btree_cur **curpp,
637 void *priv)
638 {
639 struct xfs_alloc_rec_incore *key = priv;
640
641 /* Report any gap at the end of the last AG. */
642 if (info->last)
643 return xfs_getfsmap_datadev_bnobt_helper(*curpp, &key[1], info);
644
645 /* Allocate cursor for this AG and query_range it. */
646 *curpp = xfs_bnobt_init_cursor(tp->t_mountp, tp, info->agf_bp,
647 info->pag);
648 key->ar_startblock = info->low.rm_startblock;
649 key[1].ar_startblock = info->high.rm_startblock;
650 return xfs_alloc_query_range(*curpp, key, &key[1],
651 xfs_getfsmap_datadev_bnobt_helper, info);
652 }
653
654 /* Execute a getfsmap query against the regular data device's bnobt. */
655 STATIC int
xfs_getfsmap_datadev_bnobt(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)656 xfs_getfsmap_datadev_bnobt(
657 struct xfs_trans *tp,
658 const struct xfs_fsmap *keys,
659 struct xfs_getfsmap_info *info)
660 {
661 struct xfs_alloc_rec_incore akeys[2];
662
663 memset(akeys, 0, sizeof(akeys));
664 info->missing_owner = XFS_FMR_OWN_UNKNOWN;
665 return __xfs_getfsmap_datadev(tp, keys, info,
666 xfs_getfsmap_datadev_bnobt_query, &akeys[0]);
667 }
668
669 /* Execute a getfsmap query against the log device. */
670 STATIC int
xfs_getfsmap_logdev(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)671 xfs_getfsmap_logdev(
672 struct xfs_trans *tp,
673 const struct xfs_fsmap *keys,
674 struct xfs_getfsmap_info *info)
675 {
676 struct xfs_mount *mp = tp->t_mountp;
677 struct xfs_rmap_irec rmap;
678 xfs_daddr_t rec_daddr, len_daddr;
679 xfs_fsblock_t start_fsb, end_fsb;
680 uint64_t eofs;
681
682 eofs = XFS_FSB_TO_BB(mp, mp->m_sb.sb_logblocks);
683 if (keys[0].fmr_physical >= eofs)
684 return 0;
685 start_fsb = XFS_BB_TO_FSBT(mp,
686 keys[0].fmr_physical + keys[0].fmr_length);
687 end_fsb = XFS_BB_TO_FSB(mp, min(eofs - 1, keys[1].fmr_physical));
688
689 /* Adjust the low key if we are continuing from where we left off. */
690 if (keys[0].fmr_length > 0)
691 info->low_daddr = XFS_FSB_TO_BB(mp, start_fsb);
692
693 trace_xfs_fsmap_low_key_linear(mp, info->dev, start_fsb);
694 trace_xfs_fsmap_high_key_linear(mp, info->dev, end_fsb);
695
696 if (start_fsb > 0)
697 return 0;
698
699 /* Fabricate an rmap entry for the external log device. */
700 rmap.rm_startblock = 0;
701 rmap.rm_blockcount = mp->m_sb.sb_logblocks;
702 rmap.rm_owner = XFS_RMAP_OWN_LOG;
703 rmap.rm_offset = 0;
704 rmap.rm_flags = 0;
705
706 rec_daddr = XFS_FSB_TO_BB(mp, rmap.rm_startblock);
707 len_daddr = XFS_FSB_TO_BB(mp, rmap.rm_blockcount);
708 return xfs_getfsmap_helper(tp, info, &rmap, rec_daddr, len_daddr);
709 }
710
711 #ifdef CONFIG_XFS_RT
712 /* Transform a rtbitmap "record" into a fsmap */
713 STATIC int
xfs_getfsmap_rtdev_rtbitmap_helper(struct xfs_mount * mp,struct xfs_trans * tp,const struct xfs_rtalloc_rec * rec,void * priv)714 xfs_getfsmap_rtdev_rtbitmap_helper(
715 struct xfs_mount *mp,
716 struct xfs_trans *tp,
717 const struct xfs_rtalloc_rec *rec,
718 void *priv)
719 {
720 struct xfs_getfsmap_info *info = priv;
721 struct xfs_rmap_irec irec;
722 xfs_rtblock_t rtbno;
723 xfs_daddr_t rec_daddr, len_daddr;
724
725 rtbno = xfs_rtx_to_rtb(mp, rec->ar_startext);
726 rec_daddr = XFS_FSB_TO_BB(mp, rtbno);
727 irec.rm_startblock = rtbno;
728
729 rtbno = xfs_rtx_to_rtb(mp, rec->ar_extcount);
730 len_daddr = XFS_FSB_TO_BB(mp, rtbno);
731 irec.rm_blockcount = rtbno;
732
733 irec.rm_owner = XFS_RMAP_OWN_NULL; /* "free" */
734 irec.rm_offset = 0;
735 irec.rm_flags = 0;
736
737 return xfs_getfsmap_helper(tp, info, &irec, rec_daddr, len_daddr);
738 }
739
740 /* Execute a getfsmap query against the realtime device rtbitmap. */
741 STATIC int
xfs_getfsmap_rtdev_rtbitmap(struct xfs_trans * tp,const struct xfs_fsmap * keys,struct xfs_getfsmap_info * info)742 xfs_getfsmap_rtdev_rtbitmap(
743 struct xfs_trans *tp,
744 const struct xfs_fsmap *keys,
745 struct xfs_getfsmap_info *info)
746 {
747
748 struct xfs_rtalloc_rec ahigh = { 0 };
749 struct xfs_mount *mp = tp->t_mountp;
750 xfs_rtblock_t start_rtb;
751 xfs_rtblock_t end_rtb;
752 xfs_rtxnum_t high;
753 uint64_t eofs;
754 int error;
755
756 eofs = XFS_FSB_TO_BB(mp, xfs_rtx_to_rtb(mp, mp->m_sb.sb_rextents));
757 if (keys[0].fmr_physical >= eofs)
758 return 0;
759 start_rtb = XFS_BB_TO_FSBT(mp,
760 keys[0].fmr_physical + keys[0].fmr_length);
761 end_rtb = XFS_BB_TO_FSB(mp, min(eofs - 1, keys[1].fmr_physical));
762
763 info->missing_owner = XFS_FMR_OWN_UNKNOWN;
764
765 /* Adjust the low key if we are continuing from where we left off. */
766 if (keys[0].fmr_length > 0) {
767 info->low_daddr = XFS_FSB_TO_BB(mp, start_rtb);
768 if (info->low_daddr >= eofs)
769 return 0;
770 }
771
772 trace_xfs_fsmap_low_key_linear(mp, info->dev, start_rtb);
773 trace_xfs_fsmap_high_key_linear(mp, info->dev, end_rtb);
774
775 xfs_rtbitmap_lock_shared(mp, XFS_RBMLOCK_BITMAP);
776
777 /*
778 * Set up query parameters to return free rtextents covering the range
779 * we want.
780 */
781 high = xfs_rtb_to_rtxup(mp, end_rtb);
782 error = xfs_rtalloc_query_range(mp, tp, xfs_rtb_to_rtx(mp, start_rtb),
783 high, xfs_getfsmap_rtdev_rtbitmap_helper, info);
784 if (error)
785 goto err;
786
787 /*
788 * Report any gaps at the end of the rtbitmap by simulating a null
789 * rmap starting at the block after the end of the query range.
790 */
791 info->last = true;
792 ahigh.ar_startext = min(mp->m_sb.sb_rextents, high);
793
794 error = xfs_getfsmap_rtdev_rtbitmap_helper(mp, tp, &ahigh, info);
795 if (error)
796 goto err;
797 err:
798 xfs_rtbitmap_unlock_shared(mp, XFS_RBMLOCK_BITMAP);
799 return error;
800 }
801 #endif /* CONFIG_XFS_RT */
802
803 /* Do we recognize the device? */
804 STATIC bool
xfs_getfsmap_is_valid_device(struct xfs_mount * mp,struct xfs_fsmap * fm)805 xfs_getfsmap_is_valid_device(
806 struct xfs_mount *mp,
807 struct xfs_fsmap *fm)
808 {
809 if (fm->fmr_device == 0 || fm->fmr_device == UINT_MAX ||
810 fm->fmr_device == new_encode_dev(mp->m_ddev_targp->bt_dev))
811 return true;
812 if (mp->m_logdev_targp &&
813 fm->fmr_device == new_encode_dev(mp->m_logdev_targp->bt_dev))
814 return true;
815 if (mp->m_rtdev_targp &&
816 fm->fmr_device == new_encode_dev(mp->m_rtdev_targp->bt_dev))
817 return true;
818 return false;
819 }
820
821 /* Ensure that the low key is less than the high key. */
822 STATIC bool
xfs_getfsmap_check_keys(struct xfs_fsmap * low_key,struct xfs_fsmap * high_key)823 xfs_getfsmap_check_keys(
824 struct xfs_fsmap *low_key,
825 struct xfs_fsmap *high_key)
826 {
827 if (low_key->fmr_flags & (FMR_OF_SPECIAL_OWNER | FMR_OF_EXTENT_MAP)) {
828 if (low_key->fmr_offset)
829 return false;
830 }
831 if (high_key->fmr_flags != -1U &&
832 (high_key->fmr_flags & (FMR_OF_SPECIAL_OWNER |
833 FMR_OF_EXTENT_MAP))) {
834 if (high_key->fmr_offset && high_key->fmr_offset != -1ULL)
835 return false;
836 }
837 if (high_key->fmr_length && high_key->fmr_length != -1ULL)
838 return false;
839
840 if (low_key->fmr_device > high_key->fmr_device)
841 return false;
842 if (low_key->fmr_device < high_key->fmr_device)
843 return true;
844
845 if (low_key->fmr_physical > high_key->fmr_physical)
846 return false;
847 if (low_key->fmr_physical < high_key->fmr_physical)
848 return true;
849
850 if (low_key->fmr_owner > high_key->fmr_owner)
851 return false;
852 if (low_key->fmr_owner < high_key->fmr_owner)
853 return true;
854
855 if (low_key->fmr_offset > high_key->fmr_offset)
856 return false;
857 if (low_key->fmr_offset < high_key->fmr_offset)
858 return true;
859
860 return false;
861 }
862
863 /*
864 * There are only two devices if we didn't configure RT devices at build time.
865 */
866 #ifdef CONFIG_XFS_RT
867 #define XFS_GETFSMAP_DEVS 3
868 #else
869 #define XFS_GETFSMAP_DEVS 2
870 #endif /* CONFIG_XFS_RT */
871
872 /*
873 * Get filesystem's extents as described in head, and format for output. Fills
874 * in the supplied records array until there are no more reverse mappings to
875 * return or head.fmh_entries == head.fmh_count. In the second case, this
876 * function returns -ECANCELED to indicate that more records would have been
877 * returned.
878 *
879 * Key to Confusion
880 * ----------------
881 * There are multiple levels of keys and counters at work here:
882 * xfs_fsmap_head.fmh_keys -- low and high fsmap keys passed in;
883 * these reflect fs-wide sector addrs.
884 * dkeys -- fmh_keys used to query each device;
885 * these are fmh_keys but w/ the low key
886 * bumped up by fmr_length.
887 * xfs_getfsmap_info.next_daddr -- next disk addr we expect to see; this
888 * is how we detect gaps in the fsmap
889 records and report them.
890 * xfs_getfsmap_info.low/high -- per-AG low/high keys computed from
891 * dkeys; used to query the metadata.
892 */
893 STATIC int
xfs_getfsmap(struct xfs_mount * mp,struct xfs_fsmap_head * head,struct fsmap * fsmap_recs)894 xfs_getfsmap(
895 struct xfs_mount *mp,
896 struct xfs_fsmap_head *head,
897 struct fsmap *fsmap_recs)
898 {
899 struct xfs_trans *tp = NULL;
900 struct xfs_fsmap dkeys[2]; /* per-dev keys */
901 struct xfs_getfsmap_dev handlers[XFS_GETFSMAP_DEVS];
902 struct xfs_getfsmap_info info = {
903 .fsmap_recs = fsmap_recs,
904 .head = head,
905 };
906 bool use_rmap;
907 int i;
908 int error = 0;
909
910 if (head->fmh_iflags & ~FMH_IF_VALID)
911 return -EINVAL;
912 if (!xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[0]) ||
913 !xfs_getfsmap_is_valid_device(mp, &head->fmh_keys[1]))
914 return -EINVAL;
915 if (!xfs_getfsmap_check_keys(&head->fmh_keys[0], &head->fmh_keys[1]))
916 return -EINVAL;
917
918 use_rmap = xfs_has_rmapbt(mp) &&
919 has_capability_noaudit(current, CAP_SYS_ADMIN);
920 head->fmh_entries = 0;
921
922 /* Set up our device handlers. */
923 memset(handlers, 0, sizeof(handlers));
924 handlers[0].nr_sectors = XFS_FSB_TO_BB(mp, mp->m_sb.sb_dblocks);
925 handlers[0].dev = new_encode_dev(mp->m_ddev_targp->bt_dev);
926 if (use_rmap)
927 handlers[0].fn = xfs_getfsmap_datadev_rmapbt;
928 else
929 handlers[0].fn = xfs_getfsmap_datadev_bnobt;
930 if (mp->m_logdev_targp != mp->m_ddev_targp) {
931 handlers[1].nr_sectors = XFS_FSB_TO_BB(mp,
932 mp->m_sb.sb_logblocks);
933 handlers[1].dev = new_encode_dev(mp->m_logdev_targp->bt_dev);
934 handlers[1].fn = xfs_getfsmap_logdev;
935 }
936 #ifdef CONFIG_XFS_RT
937 if (mp->m_rtdev_targp) {
938 handlers[2].nr_sectors = XFS_FSB_TO_BB(mp, mp->m_sb.sb_rblocks);
939 handlers[2].dev = new_encode_dev(mp->m_rtdev_targp->bt_dev);
940 handlers[2].fn = xfs_getfsmap_rtdev_rtbitmap;
941 }
942 #endif /* CONFIG_XFS_RT */
943
944 xfs_sort(handlers, XFS_GETFSMAP_DEVS, sizeof(struct xfs_getfsmap_dev),
945 xfs_getfsmap_dev_compare);
946
947 /*
948 * To continue where we left off, we allow userspace to use the
949 * last mapping from a previous call as the low key of the next.
950 * This is identified by a non-zero length in the low key. We
951 * have to increment the low key in this scenario to ensure we
952 * don't return the same mapping again, and instead return the
953 * very next mapping.
954 *
955 * If the low key mapping refers to file data, the same physical
956 * blocks could be mapped to several other files/offsets.
957 * According to rmapbt record ordering, the minimal next
958 * possible record for the block range is the next starting
959 * offset in the same inode. Therefore, each fsmap backend bumps
960 * the file offset to continue the search appropriately. For
961 * all other low key mapping types (attr blocks, metadata), each
962 * fsmap backend bumps the physical offset as there can be no
963 * other mapping for the same physical block range.
964 */
965 dkeys[0] = head->fmh_keys[0];
966 memset(&dkeys[1], 0xFF, sizeof(struct xfs_fsmap));
967
968 info.next_daddr = head->fmh_keys[0].fmr_physical +
969 head->fmh_keys[0].fmr_length;
970
971 /* For each device we support... */
972 for (i = 0; i < XFS_GETFSMAP_DEVS; i++) {
973 /* Is this device within the range the user asked for? */
974 if (!handlers[i].fn)
975 continue;
976 if (head->fmh_keys[0].fmr_device > handlers[i].dev)
977 continue;
978 if (head->fmh_keys[1].fmr_device < handlers[i].dev)
979 break;
980
981 /*
982 * If this device number matches the high key, we have to pass
983 * the high key to the handler to limit the query results, and
984 * set the end_daddr so that we can synthesize records at the
985 * end of the query range or device.
986 */
987 if (handlers[i].dev == head->fmh_keys[1].fmr_device) {
988 dkeys[1] = head->fmh_keys[1];
989 info.end_daddr = min(handlers[i].nr_sectors - 1,
990 dkeys[1].fmr_physical);
991 } else {
992 info.end_daddr = handlers[i].nr_sectors - 1;
993 }
994
995 /*
996 * If the device number exceeds the low key, zero out the low
997 * key so that we get everything from the beginning.
998 */
999 if (handlers[i].dev > head->fmh_keys[0].fmr_device)
1000 memset(&dkeys[0], 0, sizeof(struct xfs_fsmap));
1001
1002 /*
1003 * Grab an empty transaction so that we can use its recursive
1004 * buffer locking abilities to detect cycles in the rmapbt
1005 * without deadlocking.
1006 */
1007 error = xfs_trans_alloc_empty(mp, &tp);
1008 if (error)
1009 break;
1010
1011 info.dev = handlers[i].dev;
1012 info.last = false;
1013 info.pag = NULL;
1014 info.low_daddr = XFS_BUF_DADDR_NULL;
1015 info.low.rm_blockcount = 0;
1016 error = handlers[i].fn(tp, dkeys, &info);
1017 if (error)
1018 break;
1019 xfs_trans_cancel(tp);
1020 tp = NULL;
1021 info.next_daddr = 0;
1022 }
1023
1024 if (tp)
1025 xfs_trans_cancel(tp);
1026 head->fmh_oflags = FMH_OF_DEV_T;
1027 return error;
1028 }
1029
1030 int
xfs_ioc_getfsmap(struct xfs_inode * ip,struct fsmap_head __user * arg)1031 xfs_ioc_getfsmap(
1032 struct xfs_inode *ip,
1033 struct fsmap_head __user *arg)
1034 {
1035 struct xfs_fsmap_head xhead = {0};
1036 struct fsmap_head head;
1037 struct fsmap *recs;
1038 unsigned int count;
1039 __u32 last_flags = 0;
1040 bool done = false;
1041 int error;
1042
1043 if (copy_from_user(&head, arg, sizeof(struct fsmap_head)))
1044 return -EFAULT;
1045 if (memchr_inv(head.fmh_reserved, 0, sizeof(head.fmh_reserved)) ||
1046 memchr_inv(head.fmh_keys[0].fmr_reserved, 0,
1047 sizeof(head.fmh_keys[0].fmr_reserved)) ||
1048 memchr_inv(head.fmh_keys[1].fmr_reserved, 0,
1049 sizeof(head.fmh_keys[1].fmr_reserved)))
1050 return -EINVAL;
1051
1052 /*
1053 * Use an internal memory buffer so that we don't have to copy fsmap
1054 * data to userspace while holding locks. Start by trying to allocate
1055 * up to 128k for the buffer, but fall back to a single page if needed.
1056 */
1057 count = min_t(unsigned int, head.fmh_count,
1058 131072 / sizeof(struct fsmap));
1059 recs = kvcalloc(count, sizeof(struct fsmap), GFP_KERNEL);
1060 if (!recs) {
1061 count = min_t(unsigned int, head.fmh_count,
1062 PAGE_SIZE / sizeof(struct fsmap));
1063 recs = kvcalloc(count, sizeof(struct fsmap), GFP_KERNEL);
1064 if (!recs)
1065 return -ENOMEM;
1066 }
1067
1068 xhead.fmh_iflags = head.fmh_iflags;
1069 xfs_fsmap_to_internal(&xhead.fmh_keys[0], &head.fmh_keys[0]);
1070 xfs_fsmap_to_internal(&xhead.fmh_keys[1], &head.fmh_keys[1]);
1071
1072 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1073 trace_xfs_getfsmap_high_key(ip->i_mount, &xhead.fmh_keys[1]);
1074
1075 head.fmh_entries = 0;
1076 do {
1077 struct fsmap __user *user_recs;
1078 struct fsmap *last_rec;
1079
1080 user_recs = &arg->fmh_recs[head.fmh_entries];
1081 xhead.fmh_entries = 0;
1082 xhead.fmh_count = min_t(unsigned int, count,
1083 head.fmh_count - head.fmh_entries);
1084
1085 /* Run query, record how many entries we got. */
1086 error = xfs_getfsmap(ip->i_mount, &xhead, recs);
1087 switch (error) {
1088 case 0:
1089 /*
1090 * There are no more records in the result set. Copy
1091 * whatever we got to userspace and break out.
1092 */
1093 done = true;
1094 break;
1095 case -ECANCELED:
1096 /*
1097 * The internal memory buffer is full. Copy whatever
1098 * records we got to userspace and go again if we have
1099 * not yet filled the userspace buffer.
1100 */
1101 error = 0;
1102 break;
1103 default:
1104 goto out_free;
1105 }
1106 head.fmh_entries += xhead.fmh_entries;
1107 head.fmh_oflags = xhead.fmh_oflags;
1108
1109 /*
1110 * If the caller wanted a record count or there aren't any
1111 * new records to return, we're done.
1112 */
1113 if (head.fmh_count == 0 || xhead.fmh_entries == 0)
1114 break;
1115
1116 /* Copy all the records we got out to userspace. */
1117 if (copy_to_user(user_recs, recs,
1118 xhead.fmh_entries * sizeof(struct fsmap))) {
1119 error = -EFAULT;
1120 goto out_free;
1121 }
1122
1123 /* Remember the last record flags we copied to userspace. */
1124 last_rec = &recs[xhead.fmh_entries - 1];
1125 last_flags = last_rec->fmr_flags;
1126
1127 /* Set up the low key for the next iteration. */
1128 xfs_fsmap_to_internal(&xhead.fmh_keys[0], last_rec);
1129 trace_xfs_getfsmap_low_key(ip->i_mount, &xhead.fmh_keys[0]);
1130 } while (!done && head.fmh_entries < head.fmh_count);
1131
1132 /*
1133 * If there are no more records in the query result set and we're not
1134 * in counting mode, mark the last record returned with the LAST flag.
1135 */
1136 if (done && head.fmh_count > 0 && head.fmh_entries > 0) {
1137 struct fsmap __user *user_rec;
1138
1139 last_flags |= FMR_OF_LAST;
1140 user_rec = &arg->fmh_recs[head.fmh_entries - 1];
1141
1142 if (copy_to_user(&user_rec->fmr_flags, &last_flags,
1143 sizeof(last_flags))) {
1144 error = -EFAULT;
1145 goto out_free;
1146 }
1147 }
1148
1149 /* copy back header */
1150 if (copy_to_user(arg, &head, sizeof(struct fsmap_head))) {
1151 error = -EFAULT;
1152 goto out_free;
1153 }
1154
1155 out_free:
1156 kvfree(recs);
1157 return error;
1158 }
1159