]>
Commit | Line | Data |
---|---|---|
959ef981 | 1 | // SPDX-License-Identifier: LGPL-2.1 |
c0211f67 | 2 | /* |
f302e9e4 | 3 | * Copyright (c) 2004-2005 Silicon Graphics, Inc. All Rights Reserved. |
c0211f67 NS |
4 | */ |
5 | #ifndef __XFS_LINUX_H__ | |
6 | #define __XFS_LINUX_H__ | |
7 | ||
8 | #include <uuid/uuid.h> | |
9 | #include <sys/vfs.h> | |
10 | #include <sys/ioctl.h> | |
11 | #include <sys/param.h> | |
25d246df | 12 | #include <sys/sysmacros.h> |
66210ef2 | 13 | #include <sys/stat.h> |
5d1b7f0f | 14 | #include <inttypes.h> |
c0211f67 NS |
15 | #include <malloc.h> |
16 | #include <getopt.h> | |
dcabd4e7 | 17 | #include <errno.h> |
c0211f67 | 18 | #include <endian.h> |
a2ceac1f | 19 | #include <stdbool.h> |
7c66b2fd | 20 | #include <stdio.h> |
24842c5c | 21 | #include <asm/types.h> |
7141fc5b | 22 | #include <mntent.h> |
6529e656 DW |
23 | #ifdef OVERRIDE_SYSTEM_FSXATTR |
24 | # define fsxattr sys_fsxattr | |
25 | #endif | |
2be9e939 | 26 | #include <linux/fs.h> /* fsxattr defintion for new kernels */ |
6529e656 DW |
27 | #ifdef OVERRIDE_SYSTEM_FSXATTR |
28 | # undef fsxattr | |
29 | #endif | |
c0211f67 NS |
30 | |
31 | static __inline__ int xfsctl(const char *path, int fd, int cmd, void *p) | |
32 | { | |
33 | return ioctl(fd, cmd, p); | |
34 | } | |
35 | ||
fcd6fa7f ES |
36 | /* |
37 | * platform_test_xfs_*() implies that xfsctl will succeed on the file; | |
38 | * on Linux, at least, special files don't get xfs file ops, | |
39 | * so return 0 for those | |
40 | */ | |
41 | ||
c0211f67 NS |
42 | static __inline__ int platform_test_xfs_fd(int fd) |
43 | { | |
fcd6fa7f ES |
44 | struct statfs statfsbuf; |
45 | struct stat statbuf; | |
46 | ||
47 | if (fstatfs(fd, &statfsbuf) < 0) | |
48 | return 0; | |
49 | if (fstat(fd, &statbuf) < 0) | |
c0211f67 | 50 | return 0; |
fcd6fa7f ES |
51 | if (!S_ISREG(statbuf.st_mode) && !S_ISDIR(statbuf.st_mode)) |
52 | return 0; | |
53 | return (statfsbuf.f_type == 0x58465342); /* XFSB */ | |
c0211f67 NS |
54 | } |
55 | ||
56 | static __inline__ int platform_test_xfs_path(const char *path) | |
57 | { | |
fcd6fa7f ES |
58 | struct statfs statfsbuf; |
59 | struct stat statbuf; | |
60 | ||
61 | if (statfs(path, &statfsbuf) < 0) | |
62 | return 0; | |
63 | if (stat(path, &statbuf) < 0) | |
64 | return 0; | |
65 | if (!S_ISREG(statbuf.st_mode) && !S_ISDIR(statbuf.st_mode)) | |
c0211f67 | 66 | return 0; |
fcd6fa7f | 67 | return (statfsbuf.f_type == 0x58465342); /* XFSB */ |
c0211f67 NS |
68 | } |
69 | ||
70 | static __inline__ int platform_fstatfs(int fd, struct statfs *buf) | |
71 | { | |
72 | return fstatfs(fd, buf); | |
73 | } | |
74 | ||
75 | static __inline__ void platform_getoptreset(void) | |
76 | { | |
77 | extern int optind; | |
78 | optind = 0; | |
79 | } | |
80 | ||
1552a820 NS |
81 | static __inline__ int platform_uuid_compare(uuid_t *uu1, uuid_t *uu2) |
82 | { | |
83 | return uuid_compare(*uu1, *uu2); | |
84 | } | |
85 | ||
4d32d744 | 86 | static __inline__ void platform_uuid_unparse(uuid_t *uu, char *buffer) |
1552a820 | 87 | { |
4d32d744 NS |
88 | uuid_unparse(*uu, buffer); |
89 | } | |
90 | ||
91 | static __inline__ int platform_uuid_parse(char *buffer, uuid_t *uu) | |
92 | { | |
93 | return uuid_parse(buffer, *uu); | |
1552a820 NS |
94 | } |
95 | ||
96 | static __inline__ int platform_uuid_is_null(uuid_t *uu) | |
97 | { | |
98 | return uuid_is_null(*uu); | |
99 | } | |
c0211f67 | 100 | |
4d32d744 NS |
101 | static __inline__ void platform_uuid_generate(uuid_t *uu) |
102 | { | |
103 | uuid_generate(*uu); | |
104 | } | |
105 | ||
106 | static __inline__ void platform_uuid_clear(uuid_t *uu) | |
107 | { | |
108 | uuid_clear(*uu); | |
109 | } | |
110 | ||
111 | static __inline__ void platform_uuid_copy(uuid_t *dst, uuid_t *src) | |
112 | { | |
113 | uuid_copy(*dst, *src); | |
114 | } | |
115 | ||
ad136b33 CH |
116 | #ifndef BLKDISCARD |
117 | #define BLKDISCARD _IO(0x12,119) | |
118 | #endif | |
119 | ||
120 | static __inline__ int | |
f96aff0c | 121 | platform_discard_blocks(int fd, uint64_t start, uint64_t len) |
ad136b33 | 122 | { |
14f8b681 | 123 | uint64_t range[2] = { start, len }; |
ad136b33 CH |
124 | |
125 | if (ioctl(fd, BLKDISCARD, &range) < 0) | |
126 | return errno; | |
127 | return 0; | |
128 | } | |
129 | ||
c0211f67 | 130 | #define ENOATTR ENODATA /* Attribute not found */ |
1552a820 | 131 | #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */ |
99c1ec96 | 132 | #define EFSBADCRC EBADMSG /* Bad CRC detected */ |
c0211f67 | 133 | |
65b4f302 | 134 | typedef off_t xfs_off_t; |
14f8b681 DW |
135 | typedef uint64_t xfs_ino_t; |
136 | typedef uint32_t xfs_dev_t; | |
137 | typedef int64_t xfs_daddr_t; | |
d8079fe0 | 138 | typedef __u32 xfs_nlink_t; |
c0211f67 | 139 | |
7141fc5b JT |
140 | /** |
141 | * Abstraction of mountpoints. | |
142 | */ | |
143 | struct mntent_cursor { | |
144 | FILE *mtabp; | |
145 | }; | |
146 | ||
147 | static inline int platform_mntent_open(struct mntent_cursor * cursor, char *mtab) | |
148 | { | |
149 | cursor->mtabp = setmntent(mtab, "r"); | |
150 | if (!cursor->mtabp) { | |
151 | fprintf(stderr, "Error: cannot read %s\n", mtab); | |
152 | return 1; | |
153 | } | |
154 | return 0; | |
155 | } | |
156 | ||
157 | static inline struct mntent * platform_mntent_next(struct mntent_cursor * cursor) | |
158 | { | |
159 | return getmntent(cursor->mtabp); | |
160 | } | |
161 | ||
162 | static inline void platform_mntent_close(struct mntent_cursor * cursor) | |
163 | { | |
164 | endmntent(cursor->mtabp); | |
165 | } | |
166 | ||
83f4b5ac DC |
167 | /* |
168 | * Check whether we have to define FS_IOC_FS[GS]ETXATTR ourselves. These | |
169 | * are a copy of the definitions moved to linux/uapi/fs.h in the 4.5 kernel, | |
170 | * so this is purely for supporting builds against old kernel headers. | |
171 | */ | |
6529e656 | 172 | #if !defined FS_IOC_FSGETXATTR || defined OVERRIDE_SYSTEM_FSXATTR |
83f4b5ac DC |
173 | struct fsxattr { |
174 | __u32 fsx_xflags; /* xflags field value (get/set) */ | |
175 | __u32 fsx_extsize; /* extsize field value (get/set)*/ | |
176 | __u32 fsx_nextents; /* nextents field value (get) */ | |
177 | __u32 fsx_projid; /* project identifier (get/set) */ | |
1bc58d93 DW |
178 | __u32 fsx_cowextsize; /* cow extsize field value (get/set) */ |
179 | unsigned char fsx_pad[8]; | |
83f4b5ac | 180 | }; |
6529e656 | 181 | #endif |
83f4b5ac | 182 | |
6529e656 | 183 | #ifndef FS_IOC_FSGETXATTR |
83f4b5ac DC |
184 | /* |
185 | * Flags for the fsx_xflags field | |
186 | */ | |
187 | #define FS_XFLAG_REALTIME 0x00000001 /* data in realtime volume */ | |
188 | #define FS_XFLAG_PREALLOC 0x00000002 /* preallocated file extents */ | |
189 | #define FS_XFLAG_IMMUTABLE 0x00000008 /* file cannot be modified */ | |
190 | #define FS_XFLAG_APPEND 0x00000010 /* all writes append */ | |
191 | #define FS_XFLAG_SYNC 0x00000020 /* all writes synchronous */ | |
192 | #define FS_XFLAG_NOATIME 0x00000040 /* do not update access time */ | |
193 | #define FS_XFLAG_NODUMP 0x00000080 /* do not include in backups */ | |
194 | #define FS_XFLAG_RTINHERIT 0x00000100 /* create with rt bit set */ | |
195 | #define FS_XFLAG_PROJINHERIT 0x00000200 /* create with parents projid */ | |
196 | #define FS_XFLAG_NOSYMLINKS 0x00000400 /* disallow symlink creation */ | |
197 | #define FS_XFLAG_EXTSIZE 0x00000800 /* extent size allocator hint */ | |
198 | #define FS_XFLAG_EXTSZINHERIT 0x00001000 /* inherit inode extent size */ | |
199 | #define FS_XFLAG_NODEFRAG 0x00002000 /* do not defragment */ | |
200 | #define FS_XFLAG_FILESTREAM 0x00004000 /* use filestream allocator */ | |
7093f633 | 201 | #define FS_XFLAG_DAX 0x00008000 /* use DAX for IO */ |
83f4b5ac DC |
202 | #define FS_XFLAG_HASATTR 0x80000000 /* no DIFLAG for this */ |
203 | ||
204 | #define FS_IOC_FSGETXATTR _IOR ('X', 31, struct fsxattr) | |
205 | #define FS_IOC_FSSETXATTR _IOW ('X', 32, struct fsxattr) | |
206 | ||
207 | #endif | |
208 | ||
1bc58d93 DW |
209 | #ifndef FS_XFLAG_COWEXTSIZE |
210 | #define FS_XFLAG_COWEXTSIZE 0x00010000 /* CoW extent size allocator hint */ | |
211 | #endif | |
212 | ||
6644005b DW |
213 | #ifdef HAVE_GETFSMAP |
214 | # include <linux/fsmap.h> | |
215 | #else | |
71956d72 DW |
216 | /* |
217 | * Structure for FS_IOC_GETFSMAP. | |
218 | * | |
219 | * The memory layout for this call are the scalar values defined in | |
220 | * struct fsmap_head, followed by two struct fsmap that describe | |
221 | * the lower and upper bound of mappings to return, followed by an | |
222 | * array of struct fsmap mappings. | |
223 | * | |
224 | * fmh_iflags control the output of the call, whereas fmh_oflags report | |
225 | * on the overall record output. fmh_count should be set to the | |
226 | * length of the fmh_recs array, and fmh_entries will be set to the | |
227 | * number of entries filled out during each call. If fmh_count is | |
228 | * zero, the number of reverse mappings will be returned in | |
229 | * fmh_entries, though no mappings will be returned. fmh_reserved | |
230 | * must be set to zero. | |
231 | * | |
232 | * The two elements in the fmh_keys array are used to constrain the | |
233 | * output. The first element in the array should represent the | |
234 | * lowest disk mapping ("low key") that the user wants to learn | |
235 | * about. If this value is all zeroes, the filesystem will return | |
236 | * the first entry it knows about. For a subsequent call, the | |
237 | * contents of fsmap_head.fmh_recs[fsmap_head.fmh_count - 1] should be | |
238 | * copied into fmh_keys[0] to have the kernel start where it left off. | |
239 | * | |
240 | * The second element in the fmh_keys array should represent the | |
241 | * highest disk mapping ("high key") that the user wants to learn | |
242 | * about. If this value is all ones, the filesystem will not stop | |
243 | * until it runs out of mapping to return or runs out of space in | |
244 | * fmh_recs. | |
245 | * | |
246 | * fmr_device can be either a 32-bit cookie representing a device, or | |
247 | * a 32-bit dev_t if the FMH_OF_DEV_T flag is set. fmr_physical, | |
248 | * fmr_offset, and fmr_length are expressed in units of bytes. | |
249 | * fmr_owner is either an inode number, or a special value if | |
250 | * FMR_OF_SPECIAL_OWNER is set in fmr_flags. | |
251 | */ | |
252 | struct fsmap { | |
253 | __u32 fmr_device; /* device id */ | |
254 | __u32 fmr_flags; /* mapping flags */ | |
255 | __u64 fmr_physical; /* device offset of segment */ | |
256 | __u64 fmr_owner; /* owner id */ | |
257 | __u64 fmr_offset; /* file offset of segment */ | |
258 | __u64 fmr_length; /* length of segment */ | |
259 | __u64 fmr_reserved[3]; /* must be zero */ | |
260 | }; | |
261 | ||
262 | struct fsmap_head { | |
263 | __u32 fmh_iflags; /* control flags */ | |
264 | __u32 fmh_oflags; /* output flags */ | |
265 | __u32 fmh_count; /* # of entries in array incl. input */ | |
266 | __u32 fmh_entries; /* # of entries filled in (output). */ | |
267 | __u64 fmh_reserved[6]; /* must be zero */ | |
268 | ||
269 | struct fsmap fmh_keys[2]; /* low and high keys for the mapping search */ | |
270 | struct fsmap fmh_recs[]; /* returned records */ | |
271 | }; | |
272 | ||
273 | /* Size of an fsmap_head with room for nr records. */ | |
274 | static inline size_t | |
275 | fsmap_sizeof( | |
276 | unsigned int nr) | |
277 | { | |
278 | return sizeof(struct fsmap_head) + nr * sizeof(struct fsmap); | |
279 | } | |
280 | ||
281 | /* Start the next fsmap query at the end of the current query results. */ | |
282 | static inline void | |
283 | fsmap_advance( | |
284 | struct fsmap_head *head) | |
285 | { | |
286 | head->fmh_keys[0] = head->fmh_recs[head->fmh_entries - 1]; | |
287 | } | |
288 | ||
289 | /* fmh_iflags values - set by XFS_IOC_GETFSMAP caller in the header. */ | |
290 | /* no flags defined yet */ | |
291 | #define FMH_IF_VALID 0 | |
292 | ||
293 | /* fmh_oflags values - returned in the header segment only. */ | |
294 | #define FMH_OF_DEV_T 0x1 /* fmr_device values will be dev_t */ | |
295 | ||
296 | /* fmr_flags values - returned for each non-header segment */ | |
297 | #define FMR_OF_PREALLOC 0x1 /* segment = unwritten pre-allocation */ | |
298 | #define FMR_OF_ATTR_FORK 0x2 /* segment = attribute fork */ | |
299 | #define FMR_OF_EXTENT_MAP 0x4 /* segment = extent map */ | |
300 | #define FMR_OF_SHARED 0x8 /* segment = shared with another file */ | |
301 | #define FMR_OF_SPECIAL_OWNER 0x10 /* owner is a special value */ | |
302 | #define FMR_OF_LAST 0x20 /* segment is the last in the FS */ | |
303 | ||
304 | /* Each FS gets to define its own special owner codes. */ | |
305 | #define FMR_OWNER(type, code) (((__u64)type << 32) | \ | |
306 | ((__u64)code & 0xFFFFFFFFULL)) | |
307 | #define FMR_OWNER_TYPE(owner) ((__u32)((__u64)owner >> 32)) | |
308 | #define FMR_OWNER_CODE(owner) ((__u32)(((__u64)owner & 0xFFFFFFFFULL))) | |
309 | #define FMR_OWN_FREE FMR_OWNER(0, 1) /* free space */ | |
310 | #define FMR_OWN_UNKNOWN FMR_OWNER(0, 2) /* unknown owner */ | |
311 | #define FMR_OWN_METADATA FMR_OWNER(0, 3) /* metadata */ | |
312 | ||
313 | #define FS_IOC_GETFSMAP _IOWR('X', 59, struct fsmap_head) | |
314 | ||
315 | #define HAVE_GETFSMAP | |
316 | #endif /* HAVE_GETFSMAP */ | |
317 | ||
dad79683 RZ |
318 | #ifndef HAVE_MAP_SYNC |
319 | #define MAP_SYNC 0 | |
320 | #define MAP_SHARED_VALIDATE 0 | |
321 | #else | |
322 | #include <asm-generic/mman.h> | |
323 | #include <asm-generic/mman-common.h> | |
324 | #endif /* HAVE_MAP_SYNC */ | |
325 | ||
cb5b3ef4 | 326 | #endif /* __XFS_LINUX_H__ */ |