]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/contrib/openzfs/cmd/mount_zfs/mount_zfs.c
MFV 2.0-rc2
[FreeBSD/FreeBSD.git] / sys / contrib / openzfs / 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/mntent.h>
32 #include <sys/stat.h>
33 #include <libzfs.h>
34 #include <libzutil.h>
35 #include <locale.h>
36 #include <getopt.h>
37 #include <fcntl.h>
38 #include <errno.h>
39
40 #define ZS_COMMENT      0x00000000      /* comment */
41 #define ZS_ZFSUTIL      0x00000001      /* caller is zfs(8) */
42
43 libzfs_handle_t *g_zfs;
44
45 /*
46  * Return the pool/dataset to mount given the name passed to mount.  This
47  * is expected to be of the form pool/dataset, however may also refer to
48  * a block device if that device contains a valid zfs label.
49  */
50 static char *
51 parse_dataset(char *dataset)
52 {
53         char cwd[PATH_MAX];
54         struct stat64 statbuf;
55         int error;
56         int len;
57
58         /*
59          * We expect a pool/dataset to be provided, however if we're
60          * given a device which is a member of a zpool we attempt to
61          * extract the pool name stored in the label.  Given the pool
62          * name we can mount the root dataset.
63          */
64         error = stat64(dataset, &statbuf);
65         if (error == 0) {
66                 nvlist_t *config;
67                 char *name;
68                 int fd;
69
70                 fd = open(dataset, O_RDONLY);
71                 if (fd < 0)
72                         goto out;
73
74                 error = zpool_read_label(fd, &config, NULL);
75                 (void) close(fd);
76                 if (error)
77                         goto out;
78
79                 error = nvlist_lookup_string(config,
80                     ZPOOL_CONFIG_POOL_NAME, &name);
81                 if (error) {
82                         nvlist_free(config);
83                 } else {
84                         dataset = strdup(name);
85                         nvlist_free(config);
86                         return (dataset);
87                 }
88         }
89 out:
90         /*
91          * If a file or directory in your current working directory is
92          * named 'dataset' then mount(8) will prepend your current working
93          * directory to the dataset.  There is no way to prevent this
94          * behavior so we simply check for it and strip the prepended
95          * patch when it is added.
96          */
97         if (getcwd(cwd, PATH_MAX) == NULL)
98                 return (dataset);
99
100         len = strlen(cwd);
101
102         /* Do not add one when cwd already ends in a trailing '/' */
103         if (strncmp(cwd, dataset, len) == 0)
104                 return (dataset + len + (cwd[len-1] != '/'));
105
106         return (dataset);
107 }
108
109 /*
110  * Update the mtab_* code to use the libmount library when it is commonly
111  * available otherwise fallback to legacy mode.  The mount(8) utility will
112  * manage the lock file for us to prevent racing updates to /etc/mtab.
113  */
114 static int
115 mtab_is_writeable(void)
116 {
117         struct stat st;
118         int error, fd;
119
120         error = lstat("/etc/mtab", &st);
121         if (error || S_ISLNK(st.st_mode))
122                 return (0);
123
124         fd = open("/etc/mtab", O_RDWR | O_CREAT, 0644);
125         if (fd < 0)
126                 return (0);
127
128         close(fd);
129         return (1);
130 }
131
132 static int
133 mtab_update(char *dataset, char *mntpoint, char *type, char *mntopts)
134 {
135         struct mntent mnt;
136         FILE *fp;
137         int error;
138
139         mnt.mnt_fsname = dataset;
140         mnt.mnt_dir = mntpoint;
141         mnt.mnt_type = type;
142         mnt.mnt_opts = mntopts ? mntopts : "";
143         mnt.mnt_freq = 0;
144         mnt.mnt_passno = 0;
145
146         fp = setmntent("/etc/mtab", "a+");
147         if (!fp) {
148                 (void) fprintf(stderr, gettext(
149                     "filesystem '%s' was mounted, but /etc/mtab "
150                     "could not be opened due to error %d\n"),
151                     dataset, errno);
152                 return (MOUNT_FILEIO);
153         }
154
155         error = addmntent(fp, &mnt);
156         if (error) {
157                 (void) fprintf(stderr, gettext(
158                     "filesystem '%s' was mounted, but /etc/mtab "
159                     "could not be updated due to error %d\n"),
160                     dataset, errno);
161                 return (MOUNT_FILEIO);
162         }
163
164         (void) endmntent(fp);
165
166         return (MOUNT_SUCCESS);
167 }
168
169 int
170 main(int argc, char **argv)
171 {
172         zfs_handle_t *zhp;
173         char prop[ZFS_MAXPROPLEN];
174         uint64_t zfs_version = 0;
175         char mntopts[MNT_LINE_MAX] = { '\0' };
176         char badopt[MNT_LINE_MAX] = { '\0' };
177         char mtabopt[MNT_LINE_MAX] = { '\0' };
178         char mntpoint[PATH_MAX];
179         char *dataset;
180         unsigned long mntflags = 0, zfsflags = 0, remount = 0;
181         int sloppy = 0, fake = 0, verbose = 0, nomtab = 0, zfsutil = 0;
182         int error, c;
183
184         (void) setlocale(LC_ALL, "");
185         (void) setlocale(LC_NUMERIC, "C");
186         (void) textdomain(TEXT_DOMAIN);
187
188         opterr = 0;
189
190         /* check options */
191         while ((c = getopt_long(argc, argv, "sfnvo:h?", 0, 0)) != -1) {
192                 switch (c) {
193                 case 's':
194                         sloppy = 1;
195                         break;
196                 case 'f':
197                         fake = 1;
198                         break;
199                 case 'n':
200                         nomtab = 1;
201                         break;
202                 case 'v':
203                         verbose++;
204                         break;
205                 case 'o':
206                         (void) strlcpy(mntopts, optarg, sizeof (mntopts));
207                         break;
208                 case 'h':
209                 case '?':
210                         (void) fprintf(stderr, gettext("Invalid option '%c'\n"),
211                             optopt);
212                         (void) fprintf(stderr, gettext("Usage: mount.zfs "
213                             "[-sfnv] [-o options] <dataset> <mountpoint>\n"));
214                         return (MOUNT_USAGE);
215                 }
216         }
217
218         argc -= optind;
219         argv += optind;
220
221         /* check that we only have two arguments */
222         if (argc != 2) {
223                 if (argc == 0)
224                         (void) fprintf(stderr, gettext("missing dataset "
225                             "argument\n"));
226                 else if (argc == 1)
227                         (void) fprintf(stderr,
228                             gettext("missing mountpoint argument\n"));
229                 else
230                         (void) fprintf(stderr, gettext("too many arguments\n"));
231                 (void) fprintf(stderr, "usage: mount <dataset> <mountpoint>\n");
232                 return (MOUNT_USAGE);
233         }
234
235         dataset = parse_dataset(argv[0]);
236
237         /* canonicalize the mount point */
238         if (realpath(argv[1], mntpoint) == NULL) {
239                 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
240                     "mounted at '%s' due to canonicalization error %d.\n"),
241                     dataset, argv[1], errno);
242                 return (MOUNT_SYSERR);
243         }
244
245         /* validate mount options and set mntflags */
246         error = zfs_parse_mount_options(mntopts, &mntflags, &zfsflags, sloppy,
247             badopt, mtabopt);
248         if (error) {
249                 switch (error) {
250                 case ENOMEM:
251                         (void) fprintf(stderr, gettext("filesystem '%s' "
252                             "cannot be mounted due to a memory allocation "
253                             "failure.\n"), dataset);
254                         return (MOUNT_SYSERR);
255                 case ENOENT:
256                         (void) fprintf(stderr, gettext("filesystem '%s' "
257                             "cannot be mounted due to invalid option "
258                             "'%s'.\n"), dataset, badopt);
259                         (void) fprintf(stderr, gettext("Use the '-s' option "
260                             "to ignore the bad mount option.\n"));
261                         return (MOUNT_USAGE);
262                 default:
263                         (void) fprintf(stderr, gettext("filesystem '%s' "
264                             "cannot be mounted due to internal error %d.\n"),
265                             dataset, error);
266                         return (MOUNT_SOFTWARE);
267                 }
268         }
269
270         if (verbose)
271                 (void) fprintf(stdout, gettext("mount.zfs:\n"
272                     "  dataset:    \"%s\"\n  mountpoint: \"%s\"\n"
273                     "  mountflags: 0x%lx\n  zfsflags:   0x%lx\n"
274                     "  mountopts:  \"%s\"\n  mtabopts:   \"%s\"\n"),
275                     dataset, mntpoint, mntflags, zfsflags, mntopts, mtabopt);
276
277         if (mntflags & MS_REMOUNT) {
278                 nomtab = 1;
279                 remount = 1;
280         }
281
282         if (zfsflags & ZS_ZFSUTIL)
283                 zfsutil = 1;
284
285         if ((g_zfs = libzfs_init()) == NULL) {
286                 (void) fprintf(stderr, "%s\n", libzfs_error_init(errno));
287                 return (MOUNT_SYSERR);
288         }
289
290         /* try to open the dataset to access the mount point */
291         if ((zhp = zfs_open(g_zfs, dataset,
292             ZFS_TYPE_FILESYSTEM | ZFS_TYPE_SNAPSHOT)) == NULL) {
293                 (void) fprintf(stderr, gettext("filesystem '%s' cannot be "
294                     "mounted, unable to open the dataset\n"), dataset);
295                 libzfs_fini(g_zfs);
296                 return (MOUNT_USAGE);
297         }
298
299         zfs_adjust_mount_options(zhp, mntpoint, mntopts, mtabopt);
300
301         /* treat all snapshots as legacy mount points */
302         if (zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT)
303                 (void) strlcpy(prop, ZFS_MOUNTPOINT_LEGACY, ZFS_MAXPROPLEN);
304         else
305                 (void) zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, prop,
306                     sizeof (prop), NULL, NULL, 0, B_FALSE);
307
308         /*
309          * Fetch the max supported zfs version in case we get ENOTSUP
310          * back from the mount command, since we need the zfs handle
311          * to do so.
312          */
313         zfs_version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION);
314         if (zfs_version == 0) {
315                 fprintf(stderr, gettext("unable to fetch "
316                     "ZFS version for filesystem '%s'\n"), dataset);
317                 return (MOUNT_SYSERR);
318         }
319
320         zfs_close(zhp);
321         libzfs_fini(g_zfs);
322
323         /*
324          * Legacy mount points may only be mounted using 'mount', never using
325          * 'zfs mount'.  However, since 'zfs mount' actually invokes 'mount'
326          * we differentiate the two cases using the 'zfsutil' mount option.
327          * This mount option should only be supplied by the 'zfs mount' util.
328          *
329          * The only exception to the above rule is '-o remount' which is
330          * always allowed for non-legacy datasets.  This is done because when
331          * using zfs as your root file system both rc.sysinit/umountroot and
332          * systemd depend on 'mount -o remount <mountpoint>' to work.
333          */
334         if (zfsutil && (strcmp(prop, ZFS_MOUNTPOINT_LEGACY) == 0)) {
335                 (void) fprintf(stderr, gettext(
336                     "filesystem '%s' cannot be mounted using 'zfs mount'.\n"
337                     "Use 'zfs set mountpoint=%s' or 'mount -t zfs %s %s'.\n"
338                     "See zfs(8) for more information.\n"),
339                     dataset, mntpoint, dataset, mntpoint);
340                 return (MOUNT_USAGE);
341         }
342
343         if (!zfsutil && !(remount || fake) &&
344             strcmp(prop, ZFS_MOUNTPOINT_LEGACY)) {
345                 (void) fprintf(stderr, gettext(
346                     "filesystem '%s' cannot be mounted using 'mount'.\n"
347                     "Use 'zfs set mountpoint=%s' or 'zfs mount %s'.\n"
348                     "See zfs(8) for more information.\n"),
349                     dataset, "legacy", dataset);
350                 return (MOUNT_USAGE);
351         }
352
353         if (!fake) {
354                 error = mount(dataset, mntpoint, MNTTYPE_ZFS,
355                     mntflags, mntopts);
356         }
357
358         if (error) {
359                 switch (errno) {
360                 case ENOENT:
361                         (void) fprintf(stderr, gettext("mount point "
362                             "'%s' does not exist\n"), mntpoint);
363                         return (MOUNT_SYSERR);
364                 case EBUSY:
365                         (void) fprintf(stderr, gettext("filesystem "
366                             "'%s' is already mounted\n"), dataset);
367                         return (MOUNT_BUSY);
368                 case ENOTSUP:
369                         if (zfs_version > ZPL_VERSION) {
370                                 (void) fprintf(stderr,
371                                     gettext("filesystem '%s' (v%d) is not "
372                                     "supported by this implementation of "
373                                     "ZFS (max v%d).\n"), dataset,
374                                     (int)zfs_version, (int)ZPL_VERSION);
375                         } else {
376                                 (void) fprintf(stderr,
377                                     gettext("filesystem '%s' mount "
378                                     "failed for unknown reason.\n"), dataset);
379                         }
380                         return (MOUNT_SYSERR);
381 #ifdef MS_MANDLOCK
382                 case EPERM:
383                         if (mntflags & MS_MANDLOCK) {
384                                 (void) fprintf(stderr, gettext("filesystem "
385                                     "'%s' has the 'nbmand=on' property set, "
386                                     "this mount\noption may be disabled in "
387                                     "your kernel.  Use 'zfs set nbmand=off'\n"
388                                     "to disable this option and try to "
389                                     "mount the filesystem again.\n"), dataset);
390                                 return (MOUNT_SYSERR);
391                         }
392                         /* fallthru */
393 #endif
394                 default:
395                         (void) fprintf(stderr, gettext("filesystem "
396                             "'%s' can not be mounted: %s\n"), dataset,
397                             strerror(errno));
398                         return (MOUNT_USAGE);
399                 }
400         }
401
402         if (!nomtab && mtab_is_writeable()) {
403                 error = mtab_update(dataset, mntpoint, MNTTYPE_ZFS, mtabopt);
404                 if (error)
405                         return (error);
406         }
407
408         return (MOUNT_SUCCESS);
409 }