Merge branch 'dracut'
[zfs.git] / cmd / mount_zfs / mount_zfs.c
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21
22 /*
23  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  * Copyright (c) 2011 Lawrence Livermore National Security, LLC.
25  */
26
27 #include <libintl.h>
28 #include <unistd.h>
29 #include <sys/file.h>
30 #include <sys/mount.h>
31 #include <sys/stat.h>
32 #include <libzfs.h>
33 #ifdef HAVE_LIBSELINUX
34 #include <selinux/selinux.h>
35 #endif /* HAVE_LIBSELINUX */
36
37 libzfs_handle_t *g_zfs;
38
39 typedef struct option_map {
40         const char *name;
41         unsigned long mntmask;
42         unsigned long zfsmask;
43 } option_map_t;
44
45 static const option_map_t option_map[] = {
46         /* Canonicalized filesystem independent options from mount(8) */
47         { MNTOPT_NOAUTO,        MS_COMMENT,     ZS_COMMENT      },
48         { MNTOPT_DEFAULTS,      MS_COMMENT,     ZS_COMMENT      },
49         { MNTOPT_NODEVICES,     MS_NODEV,       ZS_COMMENT      },
50         { MNTOPT_DIRSYNC,       MS_DIRSYNC,     ZS_COMMENT      },
51         { MNTOPT_NOEXEC,        MS_NOEXEC,      ZS_COMMENT      },
52         { MNTOPT_GROUP,         MS_GROUP,       ZS_COMMENT      },
53         { MNTOPT_NETDEV,        MS_COMMENT,     ZS_COMMENT      },
54         { MNTOPT_NOFAIL,        MS_COMMENT,     ZS_COMMENT      },
55         { MNTOPT_NOSUID,        MS_NOSUID,      ZS_COMMENT      },
56         { MNTOPT_OWNER,         MS_OWNER,       ZS_COMMENT      },
57         { MNTOPT_REMOUNT,       MS_REMOUNT,     ZS_COMMENT      },
58         { MNTOPT_RO,            MS_RDONLY,      ZS_COMMENT      },
59         { MNTOPT_RW,            MS_COMMENT,     ZS_COMMENT      },
60         { MNTOPT_SYNC,          MS_SYNCHRONOUS, ZS_COMMENT      },
61         { MNTOPT_USER,          MS_USERS,       ZS_COMMENT      },
62         { MNTOPT_USERS,         MS_USERS,       ZS_COMMENT      },
63 #ifdef MS_NOATIME
64         { MNTOPT_NOATIME,       MS_NOATIME,     ZS_COMMENT      },
65 #endif
66 #ifdef MS_NODIRATIME
67         { MNTOPT_NODIRATIME,    MS_NODIRATIME,  ZS_COMMENT      },
68 #endif
69 #ifdef MS_RELATIME
70         { MNTOPT_RELATIME,      MS_RELATIME,    ZS_COMMENT      },
71 #endif
72 #ifdef MS_STRICTATIME
73         { MNTOPT_DFRATIME,      MS_STRICTATIME, ZS_COMMENT      },
74 #endif
75         { MNTOPT_CONTEXT,       MS_COMMENT,     ZS_NOCONTEXT    },
76         { MNTOPT_NOCONTEXT,     MS_COMMENT,     ZS_NOCONTEXT    },
77         { MNTOPT_FSCONTEXT,     MS_COMMENT,     ZS_NOCONTEXT    },
78         { MNTOPT_DEFCONTEXT,    MS_COMMENT,     ZS_NOCONTEXT    },
79         { MNTOPT_ROOTCONTEXT,   MS_COMMENT,     ZS_NOCONTEXT    },
80 #ifdef MS_I_VERSION
81         { MNTOPT_IVERSION,      MS_I_VERSION,   ZS_COMMENT      },
82 #endif
83 #ifdef MS_MANDLOCK
84         { MNTOPT_NBMAND,        MS_MANDLOCK,    ZS_COMMENT      },
85 #endif
86         /* Valid options not found in mount(8) */
87         { MNTOPT_BIND,          MS_BIND,        ZS_COMMENT      },
88 #ifdef MS_REC
89         { MNTOPT_RBIND,         MS_BIND|MS_REC, ZS_COMMENT      },
90 #endif
91         { MNTOPT_COMMENT,       MS_COMMENT,     ZS_COMMENT      },
92 #ifdef MS_NOSUB
93         { MNTOPT_NOSUB,         MS_NOSUB,       ZS_COMMENT      },
94 #endif
95 #ifdef MS_SILENT
96         { MNTOPT_QUIET,         MS_SILENT,      ZS_COMMENT      },
97 #endif
98         /* Custom zfs options */
99         { MNTOPT_NOXATTR,       MS_COMMENT,     ZS_COMMENT      },
100         { MNTOPT_ZFSUTIL,       MS_COMMENT,     ZS_ZFSUTIL      },
101         { NULL,                 0,              0               } };
102
103 /*
104  * Break the mount option in to a name/value pair.  The name is
105  * validated against the option map and mount flags set accordingly.
106  */
107 static int
108 parse_option(char *mntopt, unsigned long *mntflags,
109     unsigned long *zfsflags, int sloppy)
110 {
111         const option_map_t *opt;
112         char *ptr, *name, *value = NULL;
113         int error = 0;
114
115         name = strdup(mntopt);
116         if (name == NULL)
117                 return (ENOMEM);
118
119         for (ptr = name; ptr && *ptr; ptr++) {
120                 if (*ptr == '=') {
121                         *ptr = '\0';
122                         value = ptr+1;
123                         break;
124                 }
125         }
126
127         for (opt = option_map; opt->name != NULL; opt++) {
128                 if (strncmp(name, opt->name, strlen(name)) == 0) {
129                         *mntflags |= opt->mntmask;
130                         *zfsflags |= opt->zfsmask;
131
132                         /* MS_USERS implies default user options */
133                         if (opt->mntmask & (MS_USERS))
134                                 *mntflags |= (MS_NOEXEC|MS_NOSUID|MS_NODEV);
135
136                         /* MS_OWNER|MS_GROUP imply default owner options */
137                         if (opt->mntmask & (MS_OWNER | MS_GROUP))
138                                 *mntflags |= (MS_NOSUID|MS_NODEV);
139
140                         error = 0;
141                         goto out;
142                 }
143         }
144
145         if (!sloppy)
146                 error = ENOENT;
147 out:
148         /* If required further process on the value may be done here */
149         free(name);
150         return (error);
151 }
152
153 /*
154  * Translate the mount option string in to MS_* mount flags for the
155  * kernel vfs.  When sloppy is non-zero unknown options will be ignored
156  * otherwise they are considered fatal are copied in to badopt.
157  */
158 static int
159 parse_options(char *mntopts, unsigned long *mntflags, unsigned long *zfsflags,
160     int sloppy, char *badopt, char *mtabopt)
161 {
162         int error = 0, quote = 0, flag = 0, count = 0;
163         char *ptr, *opt, *opts;
164
165         opts = strdup(mntopts);
166         if (opts == NULL)
167                 return (ENOMEM);
168
169         *mntflags = 0;
170         opt = NULL;
171
172         /*
173          * Scan through all mount options which must be comma delimited.
174          * We must be careful to notice regions which are double quoted
175          * and skip commas in these regions.  Each option is then checked
176          * to determine if it is a known option.
177          */
178         for (ptr = opts; ptr && !flag; ptr++) {
179                 if (opt == NULL)
180                         opt = ptr;
181
182                 if (*ptr == '"')
183                         quote = !quote;
184
185                 if (quote)
186                         continue;
187
188                 if (*ptr == '\0')
189                         flag = 1;
190
191                 if ((*ptr == ',') || (*ptr == '\0')) {
192                         *ptr = '\0';
193
194                         error = parse_option(opt, mntflags, zfsflags, sloppy);
195                         if (error) {
196                                 strcpy(badopt, opt);
197                                 goto out;
198
199                         }
200
201                         if (!(*mntflags & MS_REMOUNT) &&
202                             !(*zfsflags & ZS_ZFSUTIL)) {
203                                 if (count > 0)
204                                         strlcat(mtabopt, ",", MNT_LINE_MAX);
205
206                                 strlcat(mtabopt, opt, MNT_LINE_MAX);
207                                 count++;
208                         }
209
210                         opt = NULL;
211                 }
212         }
213
214 out:
215         free(opts);
216         return (error);
217 }
218
219 /*
220  * If a file or directory in your current working directory is named
221  * 'dataset' then mount(8) will prepend your current working directory
222  * to dataset.  The is no way to prevent this behavior so we simply
223  * check for it and strip the prepended patch when it is added.
224  */
225 static char *
226 parse_dataset(char *dataset)
227 {
228         char cwd[PATH_MAX];
229         int len;
230
231         if (getcwd(cwd, PATH_MAX) == NULL)
232                 return (dataset);
233
234         len = strlen(cwd);
235
236         /* Do not add one when cwd already ends in a trailing '/' */
237         if (!strncmp(cwd, dataset, len))
238                 return (dataset + len + (cwd[len-1] != '/'));
239
240         return (dataset);
241 }
242
243 /*
244  * Update the mtab_* code to use the libmount library when it is commonly
245  * available otherwise fallback to legacy mode.  The mount(8) utility will
246  * manage the lock file for us to prevent racing updates to /etc/mtab.
247  */
248 static int
249 mtab_is_writeable(void)
250 {
251         struct stat st;
252         int error, fd;
253
254         error = stat(MNTTAB, &st);
255         if (error || S_ISLNK(st.st_mode))
256                 return (0);
257
258         fd = open(MNTTAB, O_RDWR | O_CREAT, 0644);
259         if (fd < 0)
260                 return (0);
261
262         close(fd);
263         return (1);
264 }
265
266 static int
267 mtab_update(char *dataset, char *mntpoint, char *type, char *mntopts)
268 {
269         struct mntent mnt;
270         FILE *fp;
271         int error;
272
273         mnt.mnt_fsname = dataset;
274         mnt.mnt_dir = mntpoint;
275         mnt.mnt_type = type;
276         mnt.mnt_opts = mntopts ? mntopts : "";
277         mnt.mnt_freq = 0;
278         mnt.mnt_passno = 0;
279
280         fp = setmntent(MNTTAB, "a+");
281         if (!fp) {
282                 (void) fprintf(stderr, gettext(
283                     "filesystem '%s' was mounted, but %s "
284                     "could not be opened due to error %d\n"),
285                     dataset, MNTTAB, errno);
286                 return (MOUNT_FILEIO);
287         }
288
289         error = addmntent(fp, &mnt);
290         if (error) {
291                 (void) fprintf(stderr, gettext(
292                     "filesystem '%s' was mounted, but %s "
293                     "could not be updated due to error %d\n"),
294                     dataset, MNTTAB, errno);
295                 return (MOUNT_FILEIO);
296         }
297
298         (void) endmntent(fp);
299
300         return (MOUNT_SUCCESS);
301 }
302
303 int
304 main(int argc, char **argv)
305 {
306         zfs_handle_t *zhp;
307         char legacy[ZFS_MAXPROPLEN];
308         char mntopts[MNT_LINE_MAX] = { '\0' };
309         char badopt[MNT_LINE_MAX] = { '\0' };
310         char mtabopt[MNT_LINE_MAX] = { '\0' };
311         char *dataset, *mntpoint;
312         unsigned long mntflags = 0, zfsflags = 0, remount_ro = 0;
313         int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
314         int error, c;
315
316         (void) setlocale(LC_ALL, "");
317         (void) textdomain(TEXT_DOMAIN);
318
319         opterr = 0;
320
321         /* check options */
322         while ((c = getopt(argc, argv, "sfnvo:h?")) != -1) {
323                 switch (c) {
324                 case 's':
325                         sloppy = 1;
326                         break;
327                 case 'f':
328                         fake = 1;
329                         break;
330                 case 'n':
331                         nomtab = 1;
332                         break;
333                 case 'v':
334                         verbose++;
335                         break;
336                 case 'o':
337                         (void) strlcpy(mntopts, optarg, sizeof (mntopts));
338                         break;
339                 case 'h':
340                 case '?':
341                         (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
342                             optopt);
343                         (void) fprintf(stderr, gettext("Usage: mount.zfs "
344                             "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
345                         return (MOUNT_USAGE);
346                 }
347         }
348
349         argc -= optind;
350         argv += optind;
351
352         /* check that we only have two arguments */
353         if (argc != 2) {
354                 if (argc == 0)
355                         (void) fprintf(stderr, gettext("missing dataset "
356                             "argument\n"));
357                 else if (argc == 1)
358                         (void) fprintf(stderr,
359                             gettext("missing mountpoint argument\n"));
360                 else
361                         (void) fprintf(stderr, gettext("too many arguments\n"));
362                 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
363                 return (MOUNT_USAGE);
364         }
365
366         dataset = parse_dataset(argv[0]);
367         mntpoint = argv[1];
368
369         /* validate mount options and set mntflags */
370         error = parse_options(mntopts, &mntflags, &zfsflags, sloppy,
371             badopt, mtabopt);
372         if (error) {
373                 switch (error) {
374                 case ENOMEM:
375                         (void) fprintf(stderr, gettext("filesystem '%s' "
376                             "cannot be mounted due to a memory allocation "
377                             "failure.\n"), dataset);
378                         return (MOUNT_SYSERR);
379                 case ENOENT:
380                         (void) fprintf(stderr, gettext("filesystem '%s' "
381                             "cannot be mounted of due invalid option "
382                             "'%s'.\n"), dataset, badopt);
383                         (void) fprintf(stderr, gettext("Use the '-s' option "
384                             "to ignore the bad mount option.\n"));
385                         return (MOUNT_USAGE);
386                 default:
387                         (void) fprintf(stderr, gettext("filesystem '%s' "
388                             "cannot be mounted due to internal error %d.\n"),
389                             dataset, error);
390                         return (MOUNT_SOFTWARE);
391                 }
392         }
393
394 #ifdef HAVE_LIBSELINUX
395         /*
396          * Automatically add the default zfs context when selinux is enabled
397          * and the caller has not specified their own context.  This must be
398          * done until zfs is added to the default selinux policy configuration
399          * as a known filesystem type which supports xattrs.
400          */
401         if (is_selinux_enabled() && !(zfsflags & ZS_NOCONTEXT)) {
402                 (void) strlcat(mntopts, ",context=\"system_u:"
403                     "object_r:file_t:s0\"", sizeof (mntopts));
404                 (void) strlcat(mtabopt, ",context=\"system_u:"
405                     "object_r:file_t:s0\"", sizeof (mtabopt));
406         }
407 #endif /* HAVE_LIBSELINUX */
408
409
410         if (verbose)
411                 (void) fprintf(stdout, gettext("mount.zfs:\n"
412                     "  dataset:    \"%s\"\n  mountpoint: \"%s\"\n"
413                     "  mountflags: 0x%lx\n  zfsflags:   0x%lx\n"
414                     "  mountopts:  \"%s\"\n  mtabopts:   \"%s\"\n"),
415                     dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt);
416
417         if (mntflags & MS_REMOUNT)
418                 nomtab = 1;
419
420         if ((mntflags & MS_REMOUNT) && (mntflags & MS_RDONLY))
421                 remount_ro = 1;
422
423         if (zfsflags & ZS_ZFSUTIL)
424                 zfsutil = 1;
425
426         if ((g_zfs = libzfs_init()) == NULL)
427                 return (MOUNT_SYSERR);
428
429         /* try to open the dataset to access the mount point */
430         if ((zhp = zfs_open(g_zfs, dataset, ZFS_TYPE_FILESYSTEM)) == NULL) {
431                 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
432                     "mounted, unable to open the dataset\n"), dataset);
433                 libzfs_fini(g_zfs);
434                 return (MOUNT_USAGE);
435         }
436
437         (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, legacy,
438             sizeof (legacy), NULL, NULL, 0, B_FALSE);
439
440         zfs_close(zhp);
441         libzfs_fini(g_zfs);
442
443         /*
444          * Legacy mount points may only be mounted using 'mount', never using
445          * 'zfs mount'.  However, since 'zfs mount' actually invokes 'mount'
446          * we differentiate the two cases using the 'zfsutil' mount option.
447          * This mount option should only be supplied by the 'zfs mount' util.
448          *
449          * The only exception to the above rule is '-o remount,ro'.  This is
450          * always allowed for non-legacy datasets for rc.sysinit/umountroot
451          * to safely remount the root filesystem and flush its cache.
452          */
453         if (zfsutil && !strcmp(legacy, ZFS_MOUNTPOINT_LEGACY)) {
454                 (void) fprintf(stderr, gettext(
455                     "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
456                     "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
457                     "See zfs(8) for more information.\n"),
458                    dataset, mntpoint, dataset, mntpoint);
459                 return (MOUNT_USAGE);
460         }
461
462         if (!zfsutil && strcmp(legacy, ZFS_MOUNTPOINT_LEGACY) && !remount_ro) {
463                 (void) fprintf(stderr, gettext(
464                     "filesystem '%s' cannot be mounted using 'mount'.\n"
465                     "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
466                     "See zfs(8) for more information.\n"),
467                     dataset, "legacy", dataset);
468                 return (MOUNT_USAGE);
469         }
470
471         if (!fake) {
472                 error = mount(dataset, mntpoint, MNTTYPE_ZFS,
473                     mntflags, mntopts);
474                 if (error) {
475                         switch (errno) {
476                         case EBUSY:
477                                 (void) fprintf(stderr, gettext("filesystem "
478                                     "'%s' is already mounted\n"), dataset);
479                                 return (MOUNT_SYSERR);
480                         default:
481                                 (void) fprintf(stderr, gettext("filesystem "
482                                     "'%s' can not be mounted due to error "
483                                     "%d\n"), dataset, errno);
484                                 return (MOUNT_USAGE);
485                         }
486                 }
487         }
488
489         if (!nomtab && mtab_is_writeable()) {
490                 error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
491                 if (error)
492                         return (error);
493         }
494
495         return (MOUNT_SUCCESS);
496 }