File: mount-api-utils.h

package info (click to toggle)
util-linux 2.41.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 92,868 kB
  • sloc: ansic: 179,124; sh: 22,714; yacc: 1,284; makefile: 525; xml: 422; python: 316; lex: 89; ruby: 75; csh: 37; exp: 19; sed: 16; perl: 15; sql: 9
file content (434 lines) | stat: -rw-r--r-- 12,518 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
/*
 * No copyright is claimed.  This code is in the public domain; do with
 * it what you wish.
 */
#ifndef UTIL_LINUX_MOUNT_API_UTILS
#define UTIL_LINUX_MOUNT_API_UTILS

#ifdef HAVE_LINUX_MOUNT_H
#include <linux/mount.h>
#include <linux/unistd.h>
#include <sys/syscall.h>
#include <inttypes.h>

/*
 * File descritors based mount API
 */
#ifdef HAVE_MOUNTFD_API

/* Accepted by both open_tree() and mount_setattr(). */
#ifndef AT_RECURSIVE
# define AT_RECURSIVE 0x8000
#endif

#ifndef OPEN_TREE_CLONE
# define OPEN_TREE_CLONE 1
#endif

#ifndef OPEN_TREE_CLOEXEC
# define OPEN_TREE_CLOEXEC O_CLOEXEC
#endif

#if !defined(HAVE_OPEN_TREE) && defined(SYS_open_tree)
static inline int open_tree(int dfd, const char *filename, unsigned int flags)
{
	return syscall(SYS_open_tree, dfd, filename, flags);
}
#endif

#ifndef MOVE_MOUNT_F_SYMLINKS
# define MOVE_MOUNT_F_SYMLINKS   0x00000001 /* Follow symlinks on from path */
#endif

#ifndef MOVE_MOUNT_F_AUTOMOUNTS
# define MOVE_MOUNT_F_AUTOMOUNTS 0x00000002 /* Follow automounts on from path */
#endif

#ifndef MOVE_MOUNT_F_EMPTY_PATH
# define MOVE_MOUNT_F_EMPTY_PATH 0x00000004 /* Empty from path permitted */
#endif

#ifndef MOVE_MOUNT_T_SYMLINKS
# define MOVE_MOUNT_T_SYMLINKS   0x00000010 /* Follow symlinks on to path */
#endif

#ifndef MOVE_MOUNT_T_AUTOMOUNTS
# define MOVE_MOUNT_T_AUTOMOUNTS 0x00000020 /* Follow automounts on to path */
#endif

#ifndef MOVE_MOUNT_T_EMPTY_PATH
# define MOVE_MOUNT_T_EMPTY_PATH 0x00000040 /* Empty to path permitted */
#endif

#ifndef MOVE_MOUNT_SET_GROUP
# define MOVE_MOUNT_SET_GROUP    0x00000100 /* Set sharing group instead */
#endif

#ifndef MOVE_MOUNT__MASK
# define MOVE_MOUNT__MASK 0x00000077
#endif

#if !defined(HAVE_MOVE_MOUNT) && defined(SYS_move_mount)
static inline int move_mount(int from_dfd, const char *from_pathname, int to_dfd,
			     const char *to_pathname, unsigned int flags)
{
	return syscall(SYS_move_mount, from_dfd, from_pathname, to_dfd,
		       to_pathname, flags);
}
#endif

#ifndef MOUNT_ATTR_RDONLY
# define MOUNT_ATTR_RDONLY 0x00000001
#endif

#ifndef MOUNT_ATTR_NOSUID
# define MOUNT_ATTR_NOSUID 0x00000002
#endif

#ifndef MOUNT_ATTR_NODEV
# define MOUNT_ATTR_NODEV 0x00000004
#endif

#ifndef MOUNT_ATTR_NOEXEC
# define MOUNT_ATTR_NOEXEC 0x00000008
#endif

#ifndef MOUNT_ATTR__ATIME
# define MOUNT_ATTR__ATIME 0x00000070
#endif

#ifndef MOUNT_ATTR_RELATIME
# define MOUNT_ATTR_RELATIME 0x00000000
#endif

#ifndef MOUNT_ATTR_NOATIME
# define MOUNT_ATTR_NOATIME 0x00000010
#endif

#ifndef MOUNT_ATTR_STRICTATIME
# define MOUNT_ATTR_STRICTATIME 0x00000020
#endif

#ifndef MOUNT_ATTR_NODIRATIME
# define MOUNT_ATTR_NODIRATIME 0x00000080
#endif

#ifndef MOUNT_ATTR_IDMAP
# define MOUNT_ATTR_IDMAP 0x00100000
#endif

#ifndef MOUNT_ATTR_NOSYMFOLLOW
# define MOUNT_ATTR_NOSYMFOLLOW 0x00200000
#endif

#ifndef HAVE_STRUCT_MOUNT_ATTR
# ifndef MOUNT_ATTR_SIZE_VER0 /* For case mount.h comes from a place invisible for autotools/meson */
# include <inttypes.h>
struct mount_attr {
	uint64_t attr_set;
	uint64_t attr_clr;
	uint64_t propagation;
	uint64_t userns_fd;
};
# endif
#endif

#if !defined(HAVE_MOUNT_SETATTR) && defined(SYS_mount_setattr)
static inline int mount_setattr(int dfd, const char *path, unsigned int flags,
				struct mount_attr *attr, size_t size)
{
	return syscall(SYS_mount_setattr, dfd, path, flags, attr, size);
}
#endif

#ifndef HAVE_ENUM_FSCONFIG_COMMAND
# ifndef FSOPEN_CLOEXEC /* For case mount.h comes from a place invisible for autotools/meson */
enum fsconfig_command {
	FSCONFIG_SET_FLAG	= 0,	/* Set parameter, supplying no value */
	FSCONFIG_SET_STRING	= 1,	/* Set parameter, supplying a string value */
	FSCONFIG_SET_BINARY	= 2,	/* Set parameter, supplying a binary blob value */
	FSCONFIG_SET_PATH	= 3,	/* Set parameter, supplying an object by path */
	FSCONFIG_SET_PATH_EMPTY	= 4,	/* Set parameter, supplying an object by (empty) path */
	FSCONFIG_SET_FD		= 5,	/* Set parameter, supplying an object by fd */
	FSCONFIG_CMD_CREATE	= 6,	/* Invoke superblock creation */
	FSCONFIG_CMD_RECONFIGURE = 7,	/* Invoke superblock reconfiguration */
};
# endif
#endif

#if !defined(HAVE_FSCONFIG) && defined(SYS_fsconfig)
static inline int fsconfig(int fd, unsigned int cmd, const char *key,
                    const void *value, int aux)
{
        return syscall(SYS_fsconfig, fd, cmd, key, value, aux);
}
#endif

#ifndef FSOPEN_CLOEXEC
# define FSOPEN_CLOEXEC          0x00000001
#endif

#if !defined(HAVE_FSOPEN) && defined(SYS_fsopen)
static inline int fsopen(const char *fsname, unsigned int flags)
{
        return syscall(SYS_fsopen, fsname, flags);
}
#endif

#ifndef FSMOUNT_CLOEXEC
# define FSMOUNT_CLOEXEC         0x00000001
#endif

#if !defined(HAVE_FSMOUNT) && defined(SYS_fsmount)
static inline int fsmount(int fd, unsigned int flags, unsigned int mount_attrs)
{
        return syscall(SYS_fsmount, fd, flags, mount_attrs);
}
#endif

#ifndef FSPICK_CLOEXEC
# define FSPICK_CLOEXEC          0x00000001
#endif

#ifndef FSPICK_SYMLINK_NOFOLLOW
# define FSPICK_SYMLINK_NOFOLLOW 0x00000002
#endif

#ifndef FSPICK_NO_AUTOMOUNT
# define FSPICK_NO_AUTOMOUNT     0x00000004
#endif

#ifdef FSPICK_EMPTY_PATH
# define FSPICK_EMPTY_PATH       0x00000008
#endif

#if !defined(HAVE_FSPICK) && defined(SYS_fspick)
static inline int fspick(int dfd, const char *pathname, unsigned int flags)
{
        return syscall(SYS_fspick, dfd, pathname, flags);
}
#endif

#endif /* HAVE_MOUNTFD_API */

/*
 * statmount() and listmount()
 */
#ifdef HAVE_STATMOUNT_API

#ifndef MNT_ID_REQ_SIZE_VER0
# define MNT_ID_REQ_SIZE_VER0    24 /* sizeof first published struct */
#endif
#ifndef MNT_ID_REQ_SIZE_VER1
# define MNT_ID_REQ_SIZE_VER1    32 /* sizeof second published struct */
#endif

/*
 * The structs mnt_id_req and statmount may differ between kernel versions, so
 * we must ensure that the structs contain everything we need. For now (during
 * development), it seems best to define local copies of the structs to avoid
 * relying on installed kernel headers and to avoid a storm of #ifdefs.
 */

/*
 * listmount() and statmount() request
 */
struct ul_mnt_id_req {
	uint32_t size;
	uint32_t spare;
	uint64_t mnt_id;
	uint64_t param;
	uint64_t mnt_ns_id;
};

/*
 * Please note that due to the variable length of the statmount buffer, the
 * struct cannot be versioned by size (like struct mnt_id_req).
 */
struct ul_statmount {
	uint32_t size;            /* Total size, including strings */
	uint32_t mnt_opts;        /* [str] Mount options of the mount */
	uint64_t mask;            /* What results were written */
	uint32_t sb_dev_major;    /* Device ID */
	uint32_t sb_dev_minor;
	uint64_t sb_magic;        /* ..._SUPER_MAGIC */
	uint32_t sb_flags;        /* SB_{RDONLY,SYNCHRONOUS,DIRSYNC,LAZYTIME} */
	uint32_t fs_type;         /* [str] Filesystem type */
	uint64_t mnt_id;          /* Unique ID of mount */
	uint64_t mnt_parent_id;   /* Unique ID of parent (for root == mnt_id) */
	uint32_t mnt_id_old;      /* Reused IDs used in proc/.../mountinfo */
	uint32_t mnt_parent_id_old;
	uint64_t mnt_attr;        /* MOUNT_ATTR_... */
	uint64_t mnt_propagation; /* MS_{SHARED,SLAVE,PRIVATE,UNBINDABLE} */
	uint64_t mnt_peer_group;  /* ID of shared peer group */
	uint64_t mnt_master;      /* Mount receives propagation from this ID */
	uint64_t propagate_from;  /* Propagation from in current namespace */
	uint32_t mnt_root;        /* [str] Root of mount relative to root of fs */
	uint32_t mnt_point;       /* [str] Mountpoint relative to current root */
	uint64_t mnt_ns_id;       /* ID of the mount namespace */
	uint32_t fs_subtype;      /* [str] Subtype of fs_type (if any) */
	uint32_t sb_source;       /* [str] Source string of the mount */
	uint32_t opt_num;         /* Number of fs options */
	uint32_t opt_array;       /* [str] Array of nul terminated fs options */
	uint32_t opt_sec_num;     /* Number of security options */
	uint32_t opt_sec_array;   /* [str] Array of nul terminated security options */
	uint64_t __spare2[46];
	char str[];               /* Variable size part containing strings */
};

/* sb_flags (defined in kernel include/linux/fs.h) */
#ifndef SB_RDONLY
# define SB_RDONLY       BIT(0)   /* Mount read-only */
# define SB_NOSUID       BIT(1)   /* Ignore suid and sgid bits */
# define SB_NODEV        BIT(2)   /* Disallow access to device special files */
# define SB_NOEXEC       BIT(3)   /* Disallow program execution */
# define SB_SYNCHRONOUS  BIT(4)   /* Writes are synced at once */
# define SB_MANDLOCK     BIT(6)   /* Allow mandatory locks on an FS */
# define SB_DIRSYNC      BIT(7)   /* Directory modifications are synchronous */
# define SB_NOATIME      BIT(10)  /* Do not update access times. */
# define SB_NODIRATIME   BIT(11)  /* Do not update directory access times */
# define SB_SILENT       BIT(15)
# define SB_POSIXACL     BIT(16)  /* Supports POSIX ACLs */
# define SB_INLINECRYPT  BIT(17)  /* Use blk-crypto for encrypted files */
# define SB_KERNMOUNT    BIT(22)  /* this is a kern_mount call */
# define SB_I_VERSION    BIT(23)  /* Update inode I_version field */
# define SB_LAZYTIME     BIT(25)  /* Update the on-disk [acm]times lazily */
#endif

/*
 * @mask bits for statmount(2)
 */
#ifndef STATMOUNT_SB_BASIC
# define STATMOUNT_SB_BASIC            0x00000001U   /* Want/got sb_... */
#endif
#ifndef STATMOUNT_MNT_BASIC
# define STATMOUNT_MNT_BASIC           0x00000002U   /* Want/got mnt_... */
#endif
#ifndef STATMOUNT_PROPAGATE_FROM
# define STATMOUNT_PROPAGATE_FROM      0x00000004U   /* Want/got propagate_from */
#endif
#ifndef STATMOUNT_MNT_ROOT
# define STATMOUNT_MNT_ROOT            0x00000008U   /* Want/got mnt_root  */
#endif
#ifndef STATMOUNT_MNT_POINT
# define STATMOUNT_MNT_POINT           0x00000010U   /* Want/got mnt_point */
#endif
#ifndef STATMOUNT_FS_TYPE
# define STATMOUNT_FS_TYPE             0x00000020U   /* Want/got fs_type */
#endif
#ifndef STATMOUNT_MNT_NS_ID
# define STATMOUNT_MNT_NS_ID           0x00000040U   /* Want/got mnt_ns_id */
#endif
#ifndef STATMOUNT_MNT_OPTS
# define STATMOUNT_MNT_OPTS            0x00000080U   /* Want/got mnt_opts */
#endif
#ifndef STATMOUNT_FS_SUBTYPE
# define STATMOUNT_FS_SUBTYPE          0x00000100U   /* Want/got fs_subtype */
#endif
#ifndef STATMOUNT_SB_SOURCE
# define STATMOUNT_SB_SOURCE           0x00000200U   /* Want/got sb_source */
#endif
#ifndef STATMOUNT_OPT_ARRAY
# define STATMOUNT_OPT_ARRAY           0x00000400U   /* Want/got opt_... */
#endif
#ifndef STATMOUNT_OPT_SEC_ARRAY
# define STATMOUNT_OPT_SEC_ARRAY       0x00000800U   /* Want/got opt_sec... */
#endif


/*
 * Special @mnt_id values that can be passed to listmount
 */
#ifdef LSMT_ROOT
# define LSMT_ROOT              0xffffffffffffffff    /* root mount */
#endif

#ifndef LISTMOUNT_REVERSE
# define LISTMOUNT_REVERSE      BIT(0)               /* List later mounts first */
#endif

#if defined(SYS_statmount)
static inline int ul_statmount(uint64_t mnt_id,
			uint64_t ns_id,
			uint64_t mask,
			struct ul_statmount *buf,
			size_t bufsize, unsigned int flags)
{
       struct ul_mnt_id_req req = {
		.size = MNT_ID_REQ_SIZE_VER1,
		.mnt_id = mnt_id,
		.param = mask,
		.mnt_ns_id = ns_id
       };

       return syscall(SYS_statmount, &req, buf, bufsize, flags);
}
#endif

#if defined(SYS_listmount)
static inline ssize_t ul_listmount(uint64_t mnt_id,
			uint64_t ns_id,
			uint64_t last_mnt_id,
			uint64_t list[], size_t num,
			unsigned int flags)
{
       struct ul_mnt_id_req req = {
		.size = MNT_ID_REQ_SIZE_VER1,
		.mnt_id = mnt_id,
		.param = last_mnt_id,
		.mnt_ns_id = ns_id
       };

       return syscall(SYS_listmount, &req, list, num, flags);
}
#endif

/* This is a version of statmount() that reallocates @buf to be large enough to
 * store data for the requested @id. This function never deallocates; it is the
 * caller's responsibility.
 */
static inline int sys_statmount(uint64_t id,
			uint64_t ns_id,
			uint64_t mask,
			struct ul_statmount **buf,
			size_t *bufsiz,
			unsigned int flags)
{
	size_t sz;
	int rc = 0;

	if (!buf || !bufsiz)
		return -EINVAL;

	sz = *bufsiz;
	if (!sz)
		sz = 32 * 1024;

	do {
		if (sz > *bufsiz) {
			struct ul_statmount *tmp = realloc(*buf, sz);
			if (!tmp)
				return -ENOMEM;
			*buf = tmp;
			*bufsiz = sz;
		}

		errno = 0;
		rc = ul_statmount(id, ns_id, mask, *buf, *bufsiz, flags);
		if (!rc)
			break;
		if (errno != EOVERFLOW)
			break;
		if (sz >= SIZE_MAX / 2)
			break;
		sz <<= 1;
	} while (rc);

	return rc;
}

#endif /* HAVE_STATMOUNT_API */

#endif /* HAVE_LINUX_MOUNT_H */

#endif /* UTIL_LINUX_MOUNT_API_UTILS */