]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - scrub/filemap.c
xfs_scrub_fail: advise recipients not to reply
[thirdparty/xfsprogs-dev.git] / scrub / filemap.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (C) 2018-2024 Oracle. All Rights Reserved.
4 * Author: Darrick J. Wong <djwong@kernel.org>
5 */
6 #include "xfs.h"
7 #include <stdint.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/statvfs.h>
13 #include "libfrog/paths.h"
14 #include "xfs_scrub.h"
15 #include "common.h"
16 #include "filemap.h"
17
18 /*
19 * These routines provide a simple interface to query the block
20 * mappings of the fork of a given inode via GETBMAPX and call a
21 * function to iterate each mapping result.
22 */
23
24 #define BMAP_NR 2048
25
26 /*
27 * Iterate all the extent block mappings between the key and fork end.
28 * Returns 0 or a positive error number.
29 */
30 int
31 scrub_iterate_filemaps(
32 struct scrub_ctx *ctx,
33 int fd,
34 int whichfork,
35 struct file_bmap *key,
36 scrub_bmap_iter_fn fn,
37 void *arg)
38 {
39 struct fsxattr fsx;
40 struct getbmapx *map;
41 struct getbmapx *p;
42 struct file_bmap bmap;
43 xfs_off_t new_off;
44 int getxattr_type;
45 int i;
46 int ret;
47
48 map = calloc(BMAP_NR, sizeof(struct getbmapx));
49 if (!map)
50 return errno;
51
52 map->bmv_offset = BTOBB(key->bm_offset);
53 map->bmv_block = BTOBB(key->bm_physical);
54 if (key->bm_length == 0)
55 map->bmv_length = ULLONG_MAX;
56 else
57 map->bmv_length = BTOBB(key->bm_length);
58 map->bmv_iflags = BMV_IF_PREALLOC | BMV_IF_NO_HOLES;
59 switch (whichfork) {
60 case XFS_ATTR_FORK:
61 getxattr_type = XFS_IOC_FSGETXATTRA;
62 map->bmv_iflags |= BMV_IF_ATTRFORK;
63 break;
64 case XFS_COW_FORK:
65 map->bmv_iflags |= BMV_IF_COWFORK;
66 getxattr_type = FS_IOC_FSGETXATTR;
67 break;
68 case XFS_DATA_FORK:
69 getxattr_type = FS_IOC_FSGETXATTR;
70 break;
71 default:
72 abort();
73 }
74
75 ret = ioctl(fd, getxattr_type, &fsx);
76 if (ret < 0) {
77 ret = errno;
78 goto out;
79 }
80
81 if (fsx.fsx_nextents == 0)
82 goto out;
83
84 map->bmv_count = min(fsx.fsx_nextents + 1, BMAP_NR);
85
86 while ((ret = ioctl(fd, XFS_IOC_GETBMAPX, map)) == 0) {
87 for (i = 0, p = &map[i + 1]; i < map->bmv_entries; i++, p++) {
88 bmap.bm_offset = BBTOB(p->bmv_offset);
89 bmap.bm_physical = BBTOB(p->bmv_block);
90 bmap.bm_length = BBTOB(p->bmv_length);
91 bmap.bm_flags = p->bmv_oflags;
92 ret = fn(ctx, fd, whichfork, &fsx, &bmap, arg);
93 if (ret)
94 goto out;
95 if (scrub_excessive_errors(ctx))
96 goto out;
97 }
98
99 if (map->bmv_entries == 0)
100 break;
101 p = map + map->bmv_entries;
102 if (p->bmv_oflags & BMV_OF_LAST)
103 break;
104
105 new_off = p->bmv_offset + p->bmv_length;
106 map->bmv_length -= new_off - map->bmv_offset;
107 map->bmv_offset = new_off;
108 }
109 if (ret < 0)
110 ret = errno;
111
112 /*
113 * Pre-reflink filesystems don't know about CoW forks, so don't
114 * be too surprised if it fails.
115 */
116 if (whichfork == XFS_COW_FORK && ret == EINVAL)
117 ret = 0;
118 out:
119 free(map);
120 return ret;
121 }