mount_setattr.2: SEE ALSO: place entries in correct order
[man-pages.git] / man2 / mount_setattr.2
blob201a1c496aed2846bf2b8523e11365117b5ac5c6
1 .\" Copyright (c) 2021 by Christian Brauner <christian.brauner@ubuntu.com>
2 .\"
3 .\" %%%LICENSE_START(VERBATIM)
4 .\" Permission is granted to make and distribute verbatim copies of this
5 .\" manual provided the copyright notice and this permission notice are
6 .\" preserved on all copies.
7 .\"
8 .\" Permission is granted to copy and distribute modified versions of this
9 .\" manual under the conditions for verbatim copying, provided that the
10 .\" entire resulting derived work is distributed under the terms of a
11 .\" permission notice identical to this one.
12 .\"
13 .\" Since the Linux kernel and libraries are constantly changing, this
14 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
15 .\" responsibility for errors or omissions, or for damages resulting from
16 .\" the use of the information contained herein.  The author(s) may not
17 .\" have taken the same level of care in the production of this manual,
18 .\" which is licensed free of charge, as they might when working
19 .\" professionally.
20 .\"
21 .\" Formatted or processed versions of this manual, if unaccompanied by
22 .\" the source, must acknowledge the copyright and authors of this work.
23 .\" %%%LICENSE_END
24 .\"
25 .TH MOUNT_SETATTR 2 2021-03-22 "Linux" "Linux Programmer's Manual"
26 .SH NAME
27 mount_setattr \- change mount properties of a mount or mount tree
28 .SH SYNOPSIS
29 .nf
31 .PP
32 .BR "#include <linux/fcntl.h>" " /* Definition of " AT_* " constants */"
33 .BR "#include <linux/mount.h>" " /* Definition of " MOUNT_ATTR_* " constants */"
34 .BR "#include <sys/syscall.h>" " /* Definition of " SYS_* " constants */"
35 .B #include <unistd.h>
36 .PP
37 .BI "int syscall(SYS_mount_setattr, int " dfd ", const char *" path ,
38 .BI "            unsigned int " flags ", struct mount_attr *" attr \
39 ", size_t " size );
40 .fi
41 .PP
42 .IR Note :
43 glibc provides no wrapper for
44 .BR mount_setattr (),
45 necessitating the use of
46 .BR syscall (2).
47 .SH DESCRIPTION
48 The
49 .BR mount_setattr ()
50 system call changes the mount properties of a mount or an entire mount tree.
52 .I path
53 is a relative pathname,
54 then it is interpreted relative to
55 the directory referred to by the file descriptor
56 .IR dfd .
58 .I dfd
59 is the special value
60 .BR AT_FDCWD ,
61 then
62 .I path
63 is interpreted relative to
64 the current working directory of the calling process.
66 .I path
67 is the empty string and
68 .B AT_EMPTY_PATH
69 is specified in
70 .IR flags ,
71 then the mount properties of the mount identified by
72 .I dfd
73 are changed.
74 .PP
75 The
76 .BR mount_setattr ()
77 system call uses an extensible structure
78 .RI ( "struct mount_attr" )
79 to allow for future extensions.
80 Any non-flag extensions to
81 .BR mount_setattr ()
82 will be implemented as new fields appended to the this structure,
83 with a zero value in a new field resulting in the kernel behaving
84 as though that extension field was not present.
85 Therefore,
86 the caller
87 .I must
88 zero-fill this structure on initialization.
89 See the "Extensibility" subsection under
90 .B NOTES
91 for more details.
92 .PP
93 The
94 .I size
95 argument should usually be specified as
96 .IR "sizeof(struct mount_attr)" .
97 However,
98 if the caller does not intend to make use of features that
99 got introduced after the initial version of
100 .IR "struct mount_attr" ,
101 it is possible to pass
102 the size of the initial struct together with the larger struct.
103 This allows the kernel to not copy later parts of the struct
104 that aren't used anyway.
105 With each extension that changes the size of
106 .IR "struct mount_attr" ,
107 the kernel will expose a definition of the form
108 .BI MOUNT_ATTR_SIZE_VER number\c
110 For example, the macro for the size of the initial version of
111 .I struct mount_attr
113 .BR MOUNT_ATTR_SIZE_VER0 .
116 .I flags
117 argument can be used to alter the path resolution behavior.
118 The supported values are:
120 .B AT_EMPTY_PATH
122 .I path
123 is the empty string,
124 change the mount properties on
125 .I dfd
126 itself.
128 .B AT_RECURSIVE
129 Change the mount properties of the entire mount tree.
131 .B AT_SYMLINK_NOFOLLOW
132 Don't follow trailing symbolic links.
134 .B AT_NO_AUTOMOUNT
135 Don't trigger automounts.
138 .I attr
139 argument of
140 .BR mount_setattr ()
141 is a structure of the following form:
143 .in +4n
145 struct mount_attr {
146     __u64 attr_set;     /* Mount properties to set */
147     __u64 attr_clr;     /* Mount properties to clear */
148     __u64 propagation;  /* Mount propagation type */
149     __u64 userns_fd;    /* User namespace file descriptor */
155 .I attr_set
157 .I attr_clr
158 members are used to specify the mount properties that
159 are supposed to be set or cleared for a mount or mount tree.
160 Flags set in
161 .I attr_set
162 enable a property on a mount or mount tree,
163 and flags set in
164 .I attr_clr
165 remove a property from a mount or mount tree.
167 When changing mount properties,
168 the kernel will first clear the flags specified
169 in the
170 .I attr_clr
171 field,
172 and then set the flags specified in the
173 .I attr_set
174 field:
176 .in +4n
178 struct mount_attr attr = {
179     .attr_clr = MOUNT_ATTR_NOEXEC | MOUNT_ATTR_NODEV,
180     .attr_set = MOUNT_ATTR_RDONLY | MOUNT_ATTR_NOSUID,
182 unsigned int current_mnt_flags = mnt->mnt_flags;
185  * Clear all flags set in .attr_clr,
186  * clearing MOUNT_ATTR_NOEXEC and MOUNT_ATTR_NODEV.
187  */
188 current_mnt_flags &= ~attr->attr_clr;
191  * Now set all flags set in .attr_set,
192  * applying MOUNT_ATTR_RDONLY and MOUNT_ATTR_NOSUID.
193  */
194 current_mnt_flags |= attr->attr_set;
196 mnt->mnt_flags = current_mnt_flags;
200 As a rsult of this change, the mount or mount tree (a) is read-only;
201 (b) blocks the execution of set-user-ID and set-group-ID programs;
202 (c) allows execution of programs; and (d) allows access to devices.
204 Multiple changes with the same set of flags requested
206 .I attr_clr
208 .I attr_set
209 are guaranteed to be idempotent after the changes have been applied.
211 The following mount attributes can be specified in the
212 .I attr_set
214 .I attr_clr
215 fields:
217 .B MOUNT_ATTR_RDONLY
218 If set in
219 .IR attr_set ,
220 makes the mount read-only.
221 If set in
222 .IR attr_clr ,
223 removes the read-only setting if set on the mount.
225 .B MOUNT_ATTR_NOSUID
226 If set in
227 .IR attr_set ,
228 causes the mount not to honor the set-user-ID and set-group-ID mode bits and
229 file capabilities when executing programs.
230 If set in
231 .IR attr_clr ,
232 clears the set-user-ID, set-group-ID,
233 and file capability restriction if set on this mount.
235 .B MOUNT_ATTR_NODEV
236 If set in
237 .IR attr_set ,
238 prevents access to devices on this mount.
239 If set in
240 .IR attr_clr ,
241 removes the restriction that prevented accessing devices on this mount.
243 .B MOUNT_ATTR_NOEXEC
244 If set in
245 .IR attr_set ,
246 prevents executing programs on this mount.
247 If set in
248 .IR attr_clr ,
249 removes the restriction that prevented executing programs on this mount.
251 .B MOUNT_ATTR_NOSYMFOLLOW
252 If set in
253 .IR attr_set ,
254 prevents following symbolic links on this mount.
255 If set in
256 .IR attr_clr ,
257 removes the restriction that prevented following symbolic links on this mount.
259 .B MOUNT_ATTR_NODIRATIME
260 If set in
261 .IR attr_set ,
262 prevents updating access time for directories on this mount.
263 If set in
264 .IR attr_clr ,
265 removes the restriction that prevented updating access time for directories.
266 Note that
267 .B MOUNT_ATTR_NODIRATIME
268 can be combined with other access-time settings
269 and is implied by the noatime setting.
270 All other access-time settings are mutually exclusive.
272 .BR MOUNT_ATTR__ATIME " - changing access-time settings"
273 In the new mount API, the access-time values are an enum starting from 0.
274 Even though they are an enum (in contrast to the other mount flags such as
275 .BR MOUNT_ATTR_NOEXEC ),
276 they are nonetheless passed in
277 .I attr_set
279 .I attr_clr
280 for consistency with
281 .BR fsmount (2),
282 which introduced this behavior.
284 Note that,
285 since access times are an enum
286 not a bit map,
287 users wanting to transition to a different access-time setting cannot simply
288 specify the access-time setting in
289 .I attr_set
290 but must also set
291 .B MOUNT_ATTR__ATIME
292 in the
293 .I attr_clr
294 field.
295 The kernel will verify that
296 .B MOUNT_ATTR__ATIME
297 isn't partially set in
298 .IR attr_clr ,
299 and that
300 .I attr_set
301 doesn't have any access-time bits set if
302 .B MOUNT_ATTR__ATIME
303 isn't set in
304 .IR attr_clr .
307 .B MOUNT_ATTR_RELATIME
308 When a file is accessed via this mount,
309 update the file's last access time (atime)
310 only if the current value of atime is less than or equal to
311 the file's last modification time (mtime) or last status change time (ctime).
313 To enable this access-time setting on a mount or mount tree,
314 .B MOUNT_ATTR_RELATIME
315 must be set in
316 .I attr_set
318 .B MOUNT_ATTR__ATIME
319 must be set in the
320 .I attr_clr
321 field.
323 .B MOUNT_ATTR_NOATIME
324 Do not update access times for (all types of) files on this mount.
326 To enable this access-time setting on a mount or mount tree,
327 .B MOUNT_ATTR_NOATIME
328 must be set in
329 .I attr_set
331 .B MOUNT_ATTR__ATIME
332 must be set in the
333 .I attr_clr
334 field.
336 .B MOUNT_ATTR_STRICTATIME
337 Always update the last access time (atime)
338 when files are accessed on this mount.
340 To enable this access-time setting on a mount or mount tree,
341 .B MOUNT_ATTR_STRICTATIME
342 must be set in
343 .I attr_set
345 .B MOUNT_ATTR__ATIME
346 must be set in the
347 .I attr_clr
348 field.
351 .B MOUNT_ATTR_IDMAP
352 If set in
353 .IR attr_set ,
354 creates an ID-mapped mount.
355 The ID mapping is taken from the user namespace specified in
356 .I userns_fd
357 and attached to the mount.
359 Since it is not supported to
360 change the ID mapping of a mount after it has been ID mapped,
361 it is invalid to specify
362 .B MOUNT_ATTR_IDMAP
364 .IR attr_clr .
366 Creating an ID-mapped mount makes it possible to
367 change the ownership of all files located under a mount.
368 Thus, ID-mapped mounts make it possible to
369 change ownership in a temporary and localized way.
370 It is a localized change because
371 ownership changes are restricted to a specific mount.
372 All other users and locations where the filesystem is exposed are unaffected.
373 And it is a temporary change because
374 ownership changes are tied to the lifetime of the mount.
376 Whenever callers interact with the filesystem through an ID-mapped mount,
377 the ID mapping of the mount will be applied to
378 user and group IDs associated with filesystem objects.
379 This encompasses the user and group IDs associated with inodes
380 and also the following
381 .BR xattr (7)
382 keys:
384 .IP \(bu 3
385 .IR security.capability ,
386 whenever filesystem capabilities
387 are stored or returned in the
388 .B VFS_CAP_REVISION_3
389 format,
390 which stores a root user ID alongside the capabilities
391 (see
392 .BR capabilities (7)).
393 .IP \(bu
394 .I system.posix_acl_access
396 .IR system.posix_acl_default ,
397 whenever user IDs or group IDs are stored in
398 .B ACL_USER
400 .B ACL_GROUP
401 entries.
404 The following conditions must be met in order to create an ID-mapped mount:
406 .IP \(bu 3
407 The caller must have the
408 .B CAP_SYS_ADMIN
409 capability in the initial user namespace.
410 .IP \(bu
411 The filesystem must be mounted in the initial user namespace.
412 .IP \(bu
413 The underlying filesystem must support ID-mapped mounts.
414 Currently,
415 .BR xfs (5),
416 .BR ext4 (5),
418 .B FAT
419 filesystems support ID-mapped mounts
420 with more filesystems being actively worked on.
421 .IP \(bu
422 The mount must not already be ID-mapped.
423 This also implies that the ID mapping of a mount cannot be altered.
424 .IP \(bu
425 The mount must be a detached/anonymous mount;
426 that is,
427 it must have been created by calling
428 .BR open_tree (2)
429 with the
430 .B OPEN_TREE_CLONE
431 flag and it must not already have been visible in the filesystem.
434 ID mappings can be created for user IDs, group IDs, and project IDs.
435 An ID mapping is essentially a mapping of a range of user or group IDs into
436 another or the same range of user or group IDs.
437 ID mappings are usually written as three numbers
438 either separated by white space or a full stop.
439 The first two numbers specify the starting user or group ID
440 in each of the two user namespaces.
441 The third number specifies the range of the ID mapping.
442 For example, a mapping for user IDs such as 1000:1001:1 would indicate that
443 user ID 1000 in the caller's user namespace is mapped to
444 user ID 1001 in its ancestor user namespace.
445 Since the map range is 1,
446 only user ID 1000 is mapped.
448 It is possible to specify up to 340 ID mappings for each ID mapping type.
449 If any user IDs or group IDs are not mapped,
450 all files owned by that unmapped user or group ID will appear as
451 being owned by the overflow user ID or overflow group ID respectively.
453 Further details and instructions for setting up ID mappings can be found in the
454 .BR user_namespaces (7)
455 man page.
457 In the common case, the user namespace passed in
458 .I userns_fd
459 together with
460 .B MOUNT_ATTR_IDMAP
462 .I attr_set
463 to create an ID-mapped mount will be the user namespace of a container.
464 In other scenarios it will be a dedicated user namespace associated with
465 a user's login session as is the case for portable home directories in
466 .BR systemd-homed.service (8)).
467 It is also perfectly fine to create a dedicated user namespace
468 for the sake of ID mapping a mount.
470 ID-mapped mounts can be useful in the following
471 and a variety of other scenarios:
473 .IP \(bu 3
474 Sharing files between multiple users or multiple machines,
475 especially in complex scenarios.
476 For example,
477 ID-mapped mounts are used to implement portable home directories in
478 .BR systemd-homed.service (8),
479 where they allow users to move their home directory
480 to an external storage device
481 and use it on multiple computers
482 where they are assigned different user IDs and group IDs.
483 This effectively makes it possible to
484 assign random user IDs and group IDs at login time.
485 .IP \(bu
486 Sharing files from the host with unprivileged containers.
487 This allows a user to avoid having to change ownership permanently through
488 .BR chown (2).
489 .IP \(bu
490 ID mapping a container's root filesystem.
491 Users don't need to change ownership permanently through
492 .BR chown (2).
493 Especially for large root filesystems, using
494 .BR chown (2)
495 can be prohibitively expensive.
496 .IP \(bu
497 Sharing files between containers with non-overlapping ID mappings.
498 .IP \(bu
499 Implementing discretionary access (DAC) permission checking
500 for filesystems lacking a concept of ownership.
501 .IP \(bu
502 Efficiently changing ownership on a per-mount basis.
503 In contrast to
504 .BR chown (2),
505 changing ownership of large sets of files is instantaneous with
506 ID-mapped mounts.
507 This is especially useful when ownership of
508 an entire root filesystem of a virtual machine or container
509 is to be changed as mentioned above.
510 With ID-mapped mounts,
511 a single
512 .BR mount_setattr ()
513 system call will be sufficient to change the ownership of all files.
514 .IP \(bu
515 Taking the current ownership into account.
516 ID mappings specify precisely
517 what a user or group ID is supposed to be mapped to.
518 This contrasts with the
519 .BR chown (2)
520 system call which cannot by itself
521 take the current ownership of the files it changes into account.
522 It simply changes the ownership to the specified user ID and group ID.
523 .IP \(bu
524 Locally and temporarily restricted ownership changes.
525 ID-mapped mounts make it possible to change ownership locally,
526 restricting it to specific mounts,
527 and temporarily as the ownership changes only apply as long as the mount exists.
528 By contrast,
529 changing ownership via the
530 .BR chown (2)
531 system call changes the ownership globally and permanently.
535 .I propagation
536 field is used to specify the propagation type of the mount or mount tree.
537 Mount propagation options are mutually exclusive;
538 that is,
539 the propagation values behave like an enum.
540 The supported mount propagation types are:
542 .B MS_PRIVATE
543 Turn all mounts into private mounts.
544 Mount and unmount events do not propagate into or out of this mount point.
546 .B MS_SHARED
547 Turn all mounts into shared mounts.
548 Mount points share events with members of a peer group.
549 Mount and unmount events immediately under this mount point
550 will propagate to the other mount points that are members of the peer group.
551 Propagation here means that the same mount or unmount will automatically occur
552 under all of the other mount points in the peer group.
553 Conversely,
554 mount and unmount events that take place under peer mount points
555 will propagate to this mount point.
557 .B MS_SLAVE
558 Turn all mounts into dependent mounts.
559 Mount and unmount events propagate into this mount point
560 from a shared peer group.
561 Mount and unmount events under this mount point do not propagate to any peer.
563 .B MS_UNBINDABLE
564 This is like a private mount,
565 and in addition this mount can't be bind mounted.
566 Attempts to bind mount this mount will fail.
567 When a recursive bind mount is performed on a directory subtree,
568 any bind mounts within the subtree are automatically pruned
569 (i.e., not replicated)
570 when replicating that subtree to produce the target subtree.
571 .SH RETURN VALUE
572 On success,
573 .BR mount_setattr ()
574 returns zero.
575 On error,
576 \-1 is returned and
577 .I errno
578 is set to indicate the cause of the error.
579 .SH ERRORS
581 .B EBADF
582 .I dfd
583 is not a valid file descriptor.
585 .B EBADF
586 .I userns_fd
587 is not a valid file descriptor.
589 .B EBUSY
590 The caller tried to change the mount to
591 .BR MOUNT_ATTR_RDONLY ,
592 but the mount still holds files open for writing.
594 .B EINVAL
595 The path specified via the
596 .I dfd
598 .I path
599 arguments to
600 .BR mount_setattr ()
601 isn't a mount point.
603 .B EINVAL
604 An unsupported value was set in
605 .I flags.
607 .B EINVAL
608 An unsupported value was specified in the
609 .I attr_set
610 field of
611 .IR mount_attr .
613 .B EINVAL
614 An unsupported value was specified in the
615 .I attr_clr
616 field of
617 .IR mount_attr .
619 .B EINVAL
620 An unsupported value was specified in the
621 .I propagation
622 field of
623 .IR mount_attr .
625 .B EINVAL
626 More than one of
627 .BR MS_SHARED ,
628 .BR MS_SLAVE ,
629 .BR MS_PRIVATE ,
631 .B MS_UNBINDABLE
632 was set in
633 .I propagation
634 field of
635 .IR mount_attr .
637 .B EINVAL
638 An access-time setting was specified in the
639 .I attr_set
640 field without
641 .B MOUNT_ATTR__ATIME
642 being set in the
643 .I attr_clr
644 field.
646 .B EINVAL
647 .B MOUNT_ATTR_IDMAP
648 was specified in
649 .IR attr_clr .
651 .B EINVAL
652 A file descriptor value was specified in
653 .I userns_fd
654 which exceeds
655 .BR INT_MAX .
657 .B EINVAL
658 A valid file descriptor value was specified in
659 .IR userns_fd ,
660 but the file descriptor wasn't a namespace file descriptor
661 or did not refer to a user namespace.
663 .B EINVAL
664 The underlying filesystem does not support ID-mapped mounts.
666 .B EINVAL
667 The mount that is to be ID mapped is not a detached/anonymous mount;
668 that is, the mount is already visible in the filesystem.
670 .B EINVAL
671 A partial access-time setting was specified in
672 .I attr_clr
673 instead of
674 .B MOUNT_ATTR__ATIME
675 being set.
677 .B EINVAL
678 The mount is located outside the caller's mount namespace.
680 .B EINVAL
681 The underlying filesystem is mounted in a user namespace.
683 .B ENOENT
684 A pathname was empty or had a nonexistent component.
686 .B ENOMEM
687 When changing mount propagation to
688 .BR MS_SHARED ,
689 a new peer group ID needs to be allocated for all mounts without a peer group
690 ID set.
691 Allocation of this peer group ID has failed.
693 .B ENOSPC
694 When changing mount propagation to
695 .BR MS_SHARED ,
696 a new peer group ID needs to be allocated for all mounts without a peer group
697 ID set.
698 Allocation of this peer group ID can fail.
699 Note that technically further error codes are possible that are specific to the
700 ID allocation implementation used.
702 .B EPERM
703 One of the mounts had at least one of
704 .BR MOUNT_ATTR_NOATIME ,
705 .BR MOUNT_ATTR_NODEV ,
706 .BR MOUNT_ATTR_NODIRATIME ,
707 .BR MOUNT_ATTR_NOEXEC ,
708 .BR MOUNT_ATTR_NOSUID ,
710 .B MOUNT_ATTR_RDONLY
711 set and the flag is locked.
712 Mount attributes become locked on a mount if:
714 .IP \(bu 3
715 A new mount or mount tree is created causing mount propagation across user
716 namespaces.
717 The kernel will lock the aforementioned flags to protect these sensitive
718 properties from being altered.
719 .IP \(bu
720 A new mount and user namespace pair is created.
721 This happens for example when specifying
722 .B CLONE_NEWUSER | CLONE_NEWNS
724 .BR unshare (2),
725 .BR clone (2),
727 .BR clone3 (2).
728 The aforementioned flags become locked to protect user namespaces from altering
729 sensitive mount properties.
732 .B EPERM
733 A valid file descriptor value was specified in
734 .IR userns_fd ,
735 but the file descriptor refers to the initial user namespace.
737 .B EPERM
738 An already ID-mapped mount was supposed to be ID mapped.
740 .B EPERM
741 The caller does not have
742 .B CAP_SYS_ADMIN
743 in the initial user namespace.
744 .SH VERSIONS
745 .BR mount_setattr ()
746 first appeared in Linux 5.12.
747 .\" commit 7d6beb71da3cc033649d641e1e608713b8220290
748 .\" commit 2a1867219c7b27f928e2545782b86daaf9ad50bd
749 .\" commit 9caccd41541a6f7d6279928d9f971f6642c361af
750 .SH CONFORMING TO
751 .BR mount_setattr ()
752 is Linux-specific.
753 .SH NOTES
754 .SS Extensibility
755 In order to allow for future extensibility,
756 .BR mount_setattr ()
757 requires the user-space application to specify the size of the
758 .I mount_attr
759 structure that it is passing.
760 By providing this information, it is possible for
761 .BR mount_setattr ()
762 to provide both forwards- and backwards-compatibility, with
763 .I size
764 acting as an implicit version number.
765 (Because new extension fields will always
766 be appended, the structure size will always increase.)
767 This extensibility design is very similar to other system calls such as
768 .BR perf_setattr (2),
769 .BR perf_event_open (2),
770 .BR clone3 (2)
772 .BR openat2 (2).
775 .I usize
776 be the size of the structure as specified by the user-space application,
777 and let
778 .I ksize
779 be the size of the structure which the kernel supports,
780 then there are three cases to consider:
781 .IP \(bu 3
783 .I ksize
784 equals
785 .IR usize ,
786 then there is no version mismatch and
787 .I attr
788 can be used verbatim.
789 .IP \(bu
791 .I ksize
792 is larger than
793 .IR usize ,
794 then there are some extension fields that the kernel supports
795 which the user-space application is unaware of.
796 Because a zero value in any added extension field signifies a no-op,
797 the kernel treats all of the extension fields
798 not provided by the user-space application
799 as having zero values.
800 This provides backwards-compatibility.
801 .IP \(bu
803 .I ksize
804 is smaller than
805 .IR usize ,
806 then there are some extension fields which the user-space application is aware
807 of but which the kernel does not support.
808 Because any extension field must have its zero values signify a no-op,
809 the kernel can safely ignore the unsupported extension fields
810 if they are all zero.
811 If any unsupported extension fields are non-zero,
812 then \-1 is returned and
813 .I errno
814 is set to
815 .BR E2BIG .
816 This provides forwards-compatibility.
818 Because the definition of
819 .I struct mount_attr
820 may change in the future
821 (with new fields being added when system headers are updated),
822 user-space applications should zero-fill
823 .I struct mount_attr
824 to ensure that recompiling the program with new headers will not result in
825 spurious errors at runtime.
826 The simplest way is to use a designated initializer:
828 .in +4n
830 struct mount_attr attr = {
831     .attr_set = MOUNT_ATTR_RDONLY,
832     .attr_clr = MOUNT_ATTR_NODEV
837 Alternatively, the structure can be zero-filled using
838 .BR memset (3)
839 or similar functions:
841 .in +4n
843 struct mount_attr attr;
844 memset(&attr, 0, sizeof(attr));
845 attr.attr_set = MOUNT_ATTR_RDONLY;
846 attr.attr_clr = MOUNT_ATTR_NODEV;
850 A user-space application that wishes to determine which extensions the running
851 kernel supports can do so by conducting a binary search on
852 .I size
853 with a structure which has every byte nonzero
854 (to find the largest value which doesn't produce an error of
855 .BR E2BIG ).
856 .SH EXAMPLES
859  * This program allows the caller to create a new detached mount
860  * and set various properties on it.
861  */
862 #define _GNU_SOURCE
863 #include <errno.h>
864 #include <fcntl.h>
865 #include <getopt.h>
866 #include <linux/mount.h>
867 #include <linux/types.h>
868 #include <stdbool.h>
869 #include <stdio.h>
870 #include <stdlib.h>
871 #include <string.h>
872 #include <sys/syscall.h>
873 #include <unistd.h>
875 static inline int
876 mount_setattr(int dfd, const char *path, unsigned int flags,
877               struct mount_attr *attr, size_t size)
879     return syscall(SYS_mount_setattr, dfd, path, flags, attr, size);
882 static inline int
883 open_tree(int dfd, const char *filename,
884                             unsigned int flags)
886     return syscall(SYS_open_tree, dfd, filename, flags);
889 static inline int
890 move_mount(int from_dfd, const char *from_pathname,
891            int to_dfd, const char *to_pathname, unsigned int flags)
893     return syscall(SYS_move_mount, from_dfd, from_pathname,
894                    to_dfd, to_pathname, flags);
897 static const struct option longopts[] = {
898     {"map-mount",       required_argument,  NULL,  'a'},
899     {"recursive",       no_argument,        NULL,  'b'},
900     {"read-only",       no_argument,        NULL,  'c'},
901     {"block-setid",     no_argument,        NULL,  'd'},
902     {"block-devices",   no_argument,        NULL,  'e'},
903     {"block-exec",      no_argument,        NULL,  'f'},
904     {"no-access-time",  no_argument,        NULL,  'g'},
905     { NULL,             0,                  NULL,   0 },
908 #define exit_log(format, ...)  do           \e
909 {                                           \e
910     fprintf(stderr, format, ##__VA_ARGS__); \e
911     exit(EXIT_FAILURE);                     \e
912 } while (0)
915 main(int argc, char *argv[])
917     int fd_userns = \-EBADF;
918     int index = 0;
919     bool recursive = false;
920     struct mount_attr *attr = &(struct mount_attr){};
921     const char *source, *target;
922     int fd_tree, new_argc, ret;
923     const char *const *new_argv;
925     while ((ret = getopt_long_only(argc, argv, "",
926                                    longopts, &index)) != \-1) {
927         switch (ret) {
928         case 'a':
929             fd_userns = open(optarg, O_RDONLY | O_CLOEXEC);
930             if (fd_userns == \-1)
931                 exit_log("%m \- Failed top open %s\en", optarg);
932             break;
933         case 'b':
934             recursive = true;
935             break;
936         case 'c':
937             attr->attr_set |= MOUNT_ATTR_RDONLY;
938             break;
939         case 'd':
940             attr->attr_set |= MOUNT_ATTR_NOSUID;
941             break;
942         case 'e':
943             attr->attr_set |= MOUNT_ATTR_NODEV;
944             break;
945         case 'f':
946             attr->attr_set |= MOUNT_ATTR_NOEXEC;
947             break;
948         case 'g':
949             attr->attr_set |= MOUNT_ATTR_NOATIME;
950             attr->attr_clr |= MOUNT_ATTR__ATIME;
951             break;
952         default:
953             exit_log("Invalid argument specified");
954         }
955     }
957     new_argv = &argv[optind];
958     new_argc = argc \- optind;
959     if (new_argc < 2)
960         exit_log("Missing source or target mount point\en");
961     source = new_argv[0];
962     target = new_argv[1];
964     fd_tree = open_tree(\-EBADF, source,
965                         OPEN_TREE_CLONE |
966                         OPEN_TREE_CLOEXEC |
967                         AT_EMPTY_PATH |
968                         (recursive ? AT_RECURSIVE : 0));
969     if (fd_tree == \-1)
970         exit_log("%m \- Failed to open %s\en", source);
972     if (fd_userns >= 0) {
973         attr->attr_set  |= MOUNT_ATTR_IDMAP;
974         attr->userns_fd = fd_userns;
975     }
976     ret = mount_setattr(fd_tree, "",
977                         AT_EMPTY_PATH |
978                         (recursive ? AT_RECURSIVE : 0),
979                         attr, sizeof(struct mount_attr));
980     if (ret == \-1)
981         exit_log("%m - Failed to change mount attributes\en");
982     close(fd_userns);
984     ret = move_mount(fd_tree, "", \-EBADF, target,
985                      MOVE_MOUNT_F_EMPTY_PATH);
986     if (ret == \-1)
987         exit_log("%m \- Failed to attach mount to %s\en", target);
988     close(fd_tree);
990     exit(EXIT_SUCCESS);
993 .SH SEE ALSO
994 .BR newuidmap (1),
995 .BR newgidmap (1),
996 .BR clone (2),
997 .BR mount (2),
998 .BR unshare (2),
999 .BR proc (5),
1000 .BR mount_namespaces (7),
1001 .BR capabilities (7),
1002 .BR user_namespaces (7),
1003 .BR xattr (7)