]> git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - include/linux.h
eddc4ad9c899ba1b35d0c718039bad4be2dd060e
[thirdparty/xfsprogs-dev.git] / include / linux.h
1 // SPDX-License-Identifier: LGPL-2.1
2 /*
3 * Copyright (c) 2004-2005 Silicon Graphics, Inc. All Rights Reserved.
4 */
5 #ifndef __XFS_LINUX_H__
6 #define __XFS_LINUX_H__
7
8 /*
9 * Reminder: anything added to this file will be compiled into downstream
10 * userspace projects!
11 */
12
13 #include <uuid/uuid.h>
14 #include <sys/vfs.h>
15 #include <sys/ioctl.h>
16 #include <sys/param.h>
17 #include <sys/sysmacros.h>
18 #include <sys/stat.h>
19 #include <inttypes.h>
20 #include <malloc.h>
21 #include <getopt.h>
22 #include <errno.h>
23 #include <endian.h>
24 #include <stdbool.h>
25 #include <stdio.h>
26 #include <signal.h>
27 #include <asm/types.h>
28 #include <mntent.h>
29 #include <fcntl.h>
30 #if defined(HAVE_FALLOCATE)
31 #include <linux/falloc.h>
32 #endif
33 #ifdef OVERRIDE_SYSTEM_FSXATTR
34 # define fsxattr sys_fsxattr
35 #endif
36 #include <linux/fs.h> /* fsxattr defintion for new kernels */
37 #ifdef OVERRIDE_SYSTEM_FSXATTR
38 # undef fsxattr
39 #endif
40 #include <unistd.h>
41 #include <assert.h>
42
43 static __inline__ int xfsctl(const char *path, int fd, int cmd, void *p)
44 {
45 return ioctl(fd, cmd, p);
46 }
47
48 /*
49 * platform_test_xfs_*() implies that xfsctl will succeed on the file;
50 * on Linux, at least, special files don't get xfs file ops,
51 * so return 0 for those
52 */
53
54 static __inline__ int platform_test_xfs_fd(int fd)
55 {
56 struct statfs statfsbuf;
57 struct stat statbuf;
58
59 if (fstatfs(fd, &statfsbuf) < 0)
60 return 0;
61 if (fstat(fd, &statbuf) < 0)
62 return 0;
63 if (!S_ISREG(statbuf.st_mode) && !S_ISDIR(statbuf.st_mode))
64 return 0;
65 return (statfsbuf.f_type == 0x58465342); /* XFSB */
66 }
67
68 static __inline__ int platform_test_xfs_path(const char *path)
69 {
70 struct statfs statfsbuf;
71 struct stat statbuf;
72
73 if (statfs(path, &statfsbuf) < 0)
74 return 0;
75 if (stat(path, &statbuf) < 0)
76 return 0;
77 if (!S_ISREG(statbuf.st_mode) && !S_ISDIR(statbuf.st_mode))
78 return 0;
79 return (statfsbuf.f_type == 0x58465342); /* XFSB */
80 }
81
82 static __inline__ int platform_fstatfs(int fd, struct statfs *buf)
83 {
84 return fstatfs(fd, buf);
85 }
86
87 static __inline__ void platform_getoptreset(void)
88 {
89 extern int optind;
90 optind = 0;
91 }
92
93 static __inline__ int platform_uuid_compare(uuid_t *uu1, uuid_t *uu2)
94 {
95 return uuid_compare(*uu1, *uu2);
96 }
97
98 static __inline__ void platform_uuid_unparse(uuid_t *uu, char *buffer)
99 {
100 uuid_unparse(*uu, buffer);
101 }
102
103 static __inline__ int platform_uuid_parse(const char *buffer, uuid_t *uu)
104 {
105 return uuid_parse(buffer, *uu);
106 }
107
108 static __inline__ int platform_uuid_is_null(uuid_t *uu)
109 {
110 return uuid_is_null(*uu);
111 }
112
113 static __inline__ void platform_uuid_generate(uuid_t *uu)
114 {
115 uuid_generate(*uu);
116 }
117
118 static __inline__ void platform_uuid_clear(uuid_t *uu)
119 {
120 uuid_clear(*uu);
121 }
122
123 static __inline__ void platform_uuid_copy(uuid_t *dst, uuid_t *src)
124 {
125 uuid_copy(*dst, *src);
126 }
127
128 #ifndef BLKDISCARD
129 #define BLKDISCARD _IO(0x12,119)
130 #endif
131
132 static __inline__ int
133 platform_discard_blocks(int fd, uint64_t start, uint64_t len)
134 {
135 uint64_t range[2] = { start, len };
136
137 if (ioctl(fd, BLKDISCARD, &range) < 0)
138 return errno;
139 return 0;
140 }
141
142 #define ENOATTR ENODATA /* Attribute not found */
143 #define EFSCORRUPTED EUCLEAN /* Filesystem is corrupted */
144 #define EFSBADCRC EBADMSG /* Bad CRC detected */
145
146 typedef off_t xfs_off_t;
147 typedef uint64_t xfs_ino_t;
148 typedef uint32_t xfs_dev_t;
149 typedef int64_t xfs_daddr_t;
150 typedef __u32 xfs_nlink_t;
151
152 /**
153 * Abstraction of mountpoints.
154 */
155 struct mntent_cursor {
156 FILE *mtabp;
157 };
158
159 static inline int platform_mntent_open(struct mntent_cursor * cursor, char *mtab)
160 {
161 cursor->mtabp = setmntent(mtab, "r");
162 if (!cursor->mtabp) {
163 fprintf(stderr, "Error: cannot read %s\n", mtab);
164 return 1;
165 }
166 return 0;
167 }
168
169 static inline struct mntent * platform_mntent_next(struct mntent_cursor * cursor)
170 {
171 return getmntent(cursor->mtabp);
172 }
173
174 static inline void platform_mntent_close(struct mntent_cursor * cursor)
175 {
176 endmntent(cursor->mtabp);
177 }
178
179 #if defined(FALLOC_FL_ZERO_RANGE)
180 static inline int
181 platform_zero_range(
182 int fd,
183 xfs_off_t start,
184 size_t len)
185 {
186 int ret;
187
188 ret = fallocate(fd, FALLOC_FL_ZERO_RANGE, start, len);
189 if (!ret)
190 return 0;
191 return -errno;
192 }
193 #else
194 #define platform_zero_range(fd, s, l) (-EOPNOTSUPP)
195 #endif
196
197 /*
198 * Use SIGKILL to simulate an immediate program crash, without a chance to run
199 * atexit handlers.
200 */
201 static inline void
202 platform_crash(void)
203 {
204 kill(getpid(), SIGKILL);
205 assert(0);
206 }
207
208 /*
209 * Check whether we have to define FS_IOC_FS[GS]ETXATTR ourselves. These
210 * are a copy of the definitions moved to linux/uapi/fs.h in the 4.5 kernel,
211 * so this is purely for supporting builds against old kernel headers.
212 */
213 #if !defined FS_IOC_FSGETXATTR || defined OVERRIDE_SYSTEM_FSXATTR
214 struct fsxattr {
215 __u32 fsx_xflags; /* xflags field value (get/set) */
216 __u32 fsx_extsize; /* extsize field value (get/set)*/
217 __u32 fsx_nextents; /* nextents field value (get) */
218 __u32 fsx_projid; /* project identifier (get/set) */
219 __u32 fsx_cowextsize; /* cow extsize field value (get/set) */
220 unsigned char fsx_pad[8];
221 };
222 #endif
223
224 #ifndef FS_IOC_FSGETXATTR
225 /*
226 * Flags for the fsx_xflags field
227 */
228 #define FS_XFLAG_REALTIME 0x00000001 /* data in realtime volume */
229 #define FS_XFLAG_PREALLOC 0x00000002 /* preallocated file extents */
230 #define FS_XFLAG_IMMUTABLE 0x00000008 /* file cannot be modified */
231 #define FS_XFLAG_APPEND 0x00000010 /* all writes append */
232 #define FS_XFLAG_SYNC 0x00000020 /* all writes synchronous */
233 #define FS_XFLAG_NOATIME 0x00000040 /* do not update access time */
234 #define FS_XFLAG_NODUMP 0x00000080 /* do not include in backups */
235 #define FS_XFLAG_RTINHERIT 0x00000100 /* create with rt bit set */
236 #define FS_XFLAG_PROJINHERIT 0x00000200 /* create with parents projid */
237 #define FS_XFLAG_NOSYMLINKS 0x00000400 /* disallow symlink creation */
238 #define FS_XFLAG_EXTSIZE 0x00000800 /* extent size allocator hint */
239 #define FS_XFLAG_EXTSZINHERIT 0x00001000 /* inherit inode extent size */
240 #define FS_XFLAG_NODEFRAG 0x00002000 /* do not defragment */
241 #define FS_XFLAG_FILESTREAM 0x00004000 /* use filestream allocator */
242 #define FS_XFLAG_DAX 0x00008000 /* use DAX for IO */
243 #define FS_XFLAG_HASATTR 0x80000000 /* no DIFLAG for this */
244
245 #define FS_IOC_FSGETXATTR _IOR ('X', 31, struct fsxattr)
246 #define FS_IOC_FSSETXATTR _IOW ('X', 32, struct fsxattr)
247
248 #endif
249
250 #ifndef FS_XFLAG_COWEXTSIZE
251 #define FS_XFLAG_COWEXTSIZE 0x00010000 /* CoW extent size allocator hint */
252 #endif
253
254 /*
255 * Reminder: anything added to this file will be compiled into downstream
256 * userspace projects!
257 */
258
259 #endif /* __XFS_LINUX_H__ */