]> git.ipfire.org Git - thirdparty/util-linux.git/blob - libmount/src/optmap.c
3ad77273e34a887ac0ce6934adb6f59873cc8d7f
[thirdparty/util-linux.git] / libmount / src / optmap.c
1 /*
2 * Copyright (C) 2010 Karel Zak <kzak@redhat.com>
3 *
4 * This file may be redistributed under the terms of the
5 * GNU Lesser General Public License.
6 */
7
8 /**
9 * SECTION: optmap
10 * @title: Option maps
11 * @short_description: description for mount options
12 *
13 * The mount(2) linux syscall uses two arguments for mount options:
14 *
15 * @mountflags: (see MS_* macros in linux/fs.h)
16 *
17 * @mountdata: (usully a comma separated string of options)
18 *
19 * The libmount uses options-map(s) to describe mount options.
20 *
21 * The option description (map entry) includes:
22 *
23 * @name: and argument name
24 *
25 * @id: (in the map unique identifier or a mountflags, e.g MS_RDONLY)
26 *
27 * @mask: (MNT_INVERT, MNT_NOMTAB)
28 *
29 * The option argument value is defined by:
30 *
31 * "=" -- required argument, e.g "comment="
32 *
33 * "[=]" -- optional argument, e.g. "loop[=]"
34 *
35 * Example:
36 *
37 * <informalexample>
38 * <programlisting>
39 * #define MY_MS_FOO (1 << 1)
40 * #define MY_MS_BAR (1 << 2)
41 *
42 * libmnt_optmap myoptions[] = {
43 * { "foo", MY_MS_FOO },
44 * { "nofoo", MY_MS_FOO | MNT_INVERT },
45 * { "bar=", MY_MS_BAR },
46 * { NULL }
47 * };
48 * </programlisting>
49 * </informalexample>
50 *
51 * The libmount defines two basic built-in options maps:
52 *
53 * @MNT_LINUX_MAP: fs-independent kernel mount options (usually MS_* flags)
54 *
55 * @MNT_USERSPACE_MAP: userspace specific mount options (e.g. "user", "loop")
56 *
57 * For more details about option map struct see "struct mnt_optmap" in
58 * mount/mount.h.
59 */
60 #include "mountP.h"
61 #include "strutils.h"
62
63 /*
64 * fs-independent mount flags (built-in MNT_LINUX_MAP)
65 */
66 static const struct libmnt_optmap linux_flags_map[] =
67 {
68 { "ro", MS_RDONLY }, /* read-only */
69 { "rw", MS_RDONLY, MNT_INVERT }, /* read-write */
70 { "exec", MS_NOEXEC, MNT_INVERT }, /* permit execution of binaries */
71 { "noexec", MS_NOEXEC }, /* don't execute binaries */
72 { "suid", MS_NOSUID, MNT_INVERT }, /* honor suid executables */
73 { "nosuid", MS_NOSUID }, /* don't honor suid executables */
74 { "dev", MS_NODEV, MNT_INVERT }, /* interpret device files */
75 { "nodev", MS_NODEV }, /* don't interpret devices */
76
77 { "sync", MS_SYNCHRONOUS }, /* synchronous I/O */
78 { "async", MS_SYNCHRONOUS, MNT_INVERT },/* asynchronous I/O */
79
80 { "dirsync", MS_DIRSYNC }, /* synchronous directory modifications */
81 { "remount", MS_REMOUNT, MNT_NOMTAB }, /* alter flags of mounted FS */
82 { "bind", MS_BIND }, /* Remount part of the tree elsewhere */
83 { "rbind", MS_BIND | MS_REC }, /* Idem, plus mounted subtrees */
84 #ifdef MS_NOSUB
85 { "sub", MS_NOSUB, MNT_INVERT }, /* allow submounts */
86 { "nosub", MS_NOSUB }, /* don't allow submounts */
87 #endif
88 #ifdef MS_SILENT
89 { "silent", MS_SILENT }, /* be quiet */
90 { "loud", MS_SILENT, MNT_INVERT }, /* print out messages. */
91 #endif
92 #ifdef MS_MANDLOCK
93 { "mand", MS_MANDLOCK }, /* Allow mandatory locks on this FS */
94 { "nomand", MS_MANDLOCK, MNT_INVERT }, /* Forbid mandatory locks on this FS */
95 #endif
96 #ifdef MS_NOATIME
97 { "atime", MS_NOATIME, MNT_INVERT }, /* Update access time */
98 { "noatime", MS_NOATIME }, /* Do not update access time */
99 #endif
100 #ifdef MS_I_VERSION
101 { "iversion", MS_I_VERSION }, /* Update inode I_version time */
102 { "noiversion", MS_I_VERSION, MNT_INVERT},/* Don't update inode I_version time */
103 #endif
104 #ifdef MS_NODIRATIME
105 { "diratime", MS_NODIRATIME, MNT_INVERT }, /* Update dir access times */
106 { "nodiratime", MS_NODIRATIME }, /* Do not update dir access times */
107 #endif
108 #ifdef MS_RELATIME
109 { "relatime", MS_RELATIME }, /* Update access times relative to mtime/ctime */
110 { "norelatime", MS_RELATIME, MNT_INVERT }, /* Update access time without regard to mtime/ctime */
111 #endif
112 #ifdef MS_STRICTATIME
113 { "strictatime", MS_STRICTATIME }, /* Strict atime semantics */
114 { "nostrictatime", MS_STRICTATIME, MNT_INVERT }, /* kernel default atime */
115 #endif
116 #ifdef MS_LAZYTIME
117 { "lazytime", MS_LAZYTIME }, /* Update {a,m,c}time on the in-memory inode only */
118 { "nolazytime", MS_LAZYTIME, MNT_INVERT },
119 #endif
120 #ifdef MS_PROPAGATION
121 { "unbindable", MS_UNBINDABLE, MNT_NOHLPS | MNT_NOMTAB }, /* Unbindable */
122 { "runbindable", MS_UNBINDABLE | MS_REC, MNT_NOHLPS | MNT_NOMTAB },
123 { "private", MS_PRIVATE, MNT_NOHLPS | MNT_NOMTAB }, /* Private */
124 { "rprivate", MS_PRIVATE | MS_REC, MNT_NOHLPS | MNT_NOMTAB },
125 { "slave", MS_SLAVE, MNT_NOHLPS | MNT_NOMTAB }, /* Slave */
126 { "rslave", MS_SLAVE | MS_REC, MNT_NOHLPS | MNT_NOMTAB },
127 { "shared", MS_SHARED, MNT_NOHLPS | MNT_NOMTAB }, /* Shared */
128 { "rshared", MS_SHARED | MS_REC, MNT_NOHLPS | MNT_NOMTAB },
129 #endif
130 { NULL, 0, 0 }
131 };
132
133 /*
134 * userspace mount option (built-in MNT_USERSPACE_MAP)
135 */
136 static const struct libmnt_optmap userspace_opts_map[] =
137 {
138 { "defaults", 0, 0 }, /* default options */
139
140 { "auto", MNT_MS_NOAUTO, MNT_NOHLPS | MNT_INVERT | MNT_NOMTAB }, /* Can be mounted using -a */
141 { "noauto", MNT_MS_NOAUTO, MNT_NOHLPS | MNT_NOMTAB }, /* Can only be mounted explicitly */
142
143 { "user[=]", MNT_MS_USER }, /* Allow ordinary user to mount (mtab) */
144 { "nouser", MNT_MS_USER, MNT_INVERT | MNT_NOMTAB }, /* Forbid ordinary user to mount */
145
146 { "users", MNT_MS_USERS, MNT_NOMTAB }, /* Allow ordinary users to mount */
147 { "nousers", MNT_MS_USERS, MNT_INVERT | MNT_NOMTAB }, /* Forbid ordinary users to mount */
148
149 { "owner", MNT_MS_OWNER, MNT_NOMTAB }, /* Let the owner of the device mount */
150 { "noowner", MNT_MS_OWNER, MNT_INVERT | MNT_NOMTAB }, /* Device owner has no special privs */
151
152 { "group", MNT_MS_GROUP, MNT_NOMTAB }, /* Let the group of the device mount */
153 { "nogroup", MNT_MS_GROUP, MNT_INVERT | MNT_NOMTAB }, /* Device group has no special privs */
154
155 /*
156 * Note that traditional init scripts assume the _netdev option in /etc/mtab to
157 * umount network block devices on shutdown.
158 */
159 { "_netdev", MNT_MS_NETDEV }, /* Device requires network */
160
161 { "comment=", MNT_MS_COMMENT, MNT_NOHLPS | MNT_NOMTAB },/* fstab comment only */
162 { "x-", MNT_MS_XCOMMENT, MNT_NOHLPS | MNT_NOMTAB | MNT_PREFIX }, /* x- options */
163
164 { "loop[=]", MNT_MS_LOOP, MNT_NOHLPS }, /* use the loop device */
165 { "offset=", MNT_MS_OFFSET, MNT_NOHLPS | MNT_NOMTAB }, /* loop device offset */
166 { "sizelimit=", MNT_MS_SIZELIMIT, MNT_NOHLPS | MNT_NOMTAB }, /* loop device size limit */
167 { "encryption=", MNT_MS_ENCRYPTION, MNT_NOHLPS | MNT_NOMTAB }, /* loop device encryption */
168
169 { "nofail", MNT_MS_NOFAIL, MNT_NOMTAB }, /* Do not fail if ENOENT on dev */
170
171 { "uhelper=", MNT_MS_UHELPER }, /* /sbin/umount.<helper> */
172
173 { "helper=", MNT_MS_HELPER }, /* /sbin/mount.<helper> */
174
175 { NULL, 0, 0 }
176 };
177
178 /**
179 * mnt_get_builtin_map:
180 * @id: map id -- MNT_LINUX_MAP or MNT_USERSPACE_MAP
181 *
182 * MNT_LINUX_MAP - Linux kernel fs-independent mount options
183 * (usually MS_* flags, see linux/fs.h)
184 *
185 * MNT_USERSPACE_MAP - userpace mount(8) specific mount options
186 * (e.g user=, _netdev, ...)
187 *
188 * Returns: static built-in libmount map.
189 */
190 const struct libmnt_optmap *mnt_get_builtin_optmap(int id)
191 {
192 assert(id);
193
194 if (id == MNT_LINUX_MAP)
195 return linux_flags_map;
196 else if (id == MNT_USERSPACE_MAP)
197 return userspace_opts_map;
198 return NULL;
199 }
200
201 /*
202 * Looks up the @name in @maps and returns a map and in @mapent
203 * returns the map entry
204 */
205 const struct libmnt_optmap *mnt_optmap_get_entry(
206 struct libmnt_optmap const **maps,
207 int nmaps,
208 const char *name,
209 size_t namelen,
210 const struct libmnt_optmap **mapent)
211 {
212 int i;
213
214 assert(maps);
215 assert(nmaps);
216 assert(name);
217 assert(namelen);
218
219 if (mapent)
220 *mapent = NULL;
221
222 for (i = 0; i < nmaps; i++) {
223 const struct libmnt_optmap *map = maps[i];
224 const struct libmnt_optmap *ent;
225 const char *p;
226
227 for (ent = map; ent && ent->name; ent++) {
228 if (ent->mask & MNT_PREFIX) {
229 if (startswith(name, ent->name)) {
230 if (mapent)
231 *mapent = ent;
232 return map;
233 }
234 continue;
235 }
236 if (strncmp(ent->name, name, namelen))
237 continue;
238 p = ent->name + namelen;
239 if (*p == '\0' || *p == '=' || *p == '[') {
240 if (mapent)
241 *mapent = ent;
242 return map;
243 }
244 }
245 }
246 return NULL;
247 }
248