]> git.proxmox.com Git - mirror_zfs.git/blob - lib/libzfs/libzfs_mount.c
Allow for '-o feature@<feature>=disabled' on the command line
[mirror_zfs.git] / lib / libzfs / libzfs_mount.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 2015 Nexenta Systems, Inc. All rights reserved.
24 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
25 * Copyright (c) 2014 by Delphix. All rights reserved.
26 */
27
28 /*
29 * Routines to manage ZFS mounts. We separate all the nasty routines that have
30 * to deal with the OS. The following functions are the main entry points --
31 * they are used by mount and unmount and when changing a filesystem's
32 * mountpoint.
33 *
34 * zfs_is_mounted()
35 * zfs_mount()
36 * zfs_unmount()
37 * zfs_unmountall()
38 *
39 * This file also contains the functions used to manage sharing filesystems via
40 * NFS and iSCSI:
41 *
42 * zfs_is_shared()
43 * zfs_share()
44 * zfs_unshare()
45 *
46 * zfs_is_shared_nfs()
47 * zfs_is_shared_smb()
48 * zfs_share_proto()
49 * zfs_shareall();
50 * zfs_unshare_nfs()
51 * zfs_unshare_smb()
52 * zfs_unshareall_nfs()
53 * zfs_unshareall_smb()
54 * zfs_unshareall()
55 * zfs_unshareall_bypath()
56 *
57 * The following functions are available for pool consumers, and will
58 * mount/unmount and share/unshare all datasets within pool:
59 *
60 * zpool_enable_datasets()
61 * zpool_disable_datasets()
62 */
63
64 #include <dirent.h>
65 #include <dlfcn.h>
66 #include <errno.h>
67 #include <libgen.h>
68 #include <libintl.h>
69 #include <stdio.h>
70 #include <stdlib.h>
71 #include <strings.h>
72 #include <unistd.h>
73 #include <zone.h>
74 #include <sys/mntent.h>
75 #include <sys/mount.h>
76 #include <sys/stat.h>
77
78 #include <libzfs.h>
79
80 #include "libzfs_impl.h"
81
82 #include <libshare.h>
83 #include <sys/systeminfo.h>
84 #define MAXISALEN 257 /* based on sysinfo(2) man page */
85
86 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
87 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
88 zfs_share_proto_t);
89
90 /*
91 * The share protocols table must be in the same order as the zfs_share_prot_t
92 * enum in libzfs_impl.h
93 */
94 typedef struct {
95 zfs_prop_t p_prop;
96 char *p_name;
97 int p_share_err;
98 int p_unshare_err;
99 } proto_table_t;
100
101 proto_table_t proto_table[PROTO_END] = {
102 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
103 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
104 };
105
106 zfs_share_proto_t nfs_only[] = {
107 PROTO_NFS,
108 PROTO_END
109 };
110
111 zfs_share_proto_t smb_only[] = {
112 PROTO_SMB,
113 PROTO_END
114 };
115 zfs_share_proto_t share_all_proto[] = {
116 PROTO_NFS,
117 PROTO_SMB,
118 PROTO_END
119 };
120
121 /*
122 * Search the sharetab for the given mountpoint and protocol, returning
123 * a zfs_share_type_t value.
124 */
125 static zfs_share_type_t
126 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
127 {
128 char buf[MAXPATHLEN], *tab;
129 char *ptr;
130
131 if (hdl->libzfs_sharetab == NULL)
132 return (SHARED_NOT_SHARED);
133
134 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
135
136 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
137
138 /* the mountpoint is the first entry on each line */
139 if ((tab = strchr(buf, '\t')) == NULL)
140 continue;
141
142 *tab = '\0';
143 if (strcmp(buf, mountpoint) == 0) {
144 /*
145 * the protocol field is the third field
146 * skip over second field
147 */
148 ptr = ++tab;
149 if ((tab = strchr(ptr, '\t')) == NULL)
150 continue;
151 ptr = ++tab;
152 if ((tab = strchr(ptr, '\t')) == NULL)
153 continue;
154 *tab = '\0';
155 if (strcmp(ptr,
156 proto_table[proto].p_name) == 0) {
157 switch (proto) {
158 case PROTO_NFS:
159 return (SHARED_NFS);
160 case PROTO_SMB:
161 return (SHARED_SMB);
162 default:
163 return (0);
164 }
165 }
166 }
167 }
168
169 return (SHARED_NOT_SHARED);
170 }
171
172 /*
173 * Returns true if the specified directory is empty. If we can't open the
174 * directory at all, return true so that the mount can fail with a more
175 * informative error message.
176 */
177 static boolean_t
178 dir_is_empty(const char *dirname)
179 {
180 DIR *dirp;
181 struct dirent64 *dp;
182
183 if ((dirp = opendir(dirname)) == NULL)
184 return (B_TRUE);
185
186 while ((dp = readdir64(dirp)) != NULL) {
187
188 if (strcmp(dp->d_name, ".") == 0 ||
189 strcmp(dp->d_name, "..") == 0)
190 continue;
191
192 (void) closedir(dirp);
193 return (B_FALSE);
194 }
195
196 (void) closedir(dirp);
197 return (B_TRUE);
198 }
199
200 /*
201 * Checks to see if the mount is active. If the filesystem is mounted, we fill
202 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
203 * 0.
204 */
205 boolean_t
206 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
207 {
208 struct mnttab entry;
209
210 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
211 return (B_FALSE);
212
213 if (where != NULL)
214 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
215
216 return (B_TRUE);
217 }
218
219 boolean_t
220 zfs_is_mounted(zfs_handle_t *zhp, char **where)
221 {
222 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
223 }
224
225 /*
226 * Returns true if the given dataset is mountable, false otherwise. Returns the
227 * mountpoint in 'buf'.
228 */
229 static boolean_t
230 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
231 zprop_source_t *source)
232 {
233 char sourceloc[MAXNAMELEN];
234 zprop_source_t sourcetype;
235
236 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type,
237 B_FALSE))
238 return (B_FALSE);
239
240 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
241 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
242
243 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
244 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
245 return (B_FALSE);
246
247 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
248 return (B_FALSE);
249
250 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
251 getzoneid() == GLOBAL_ZONEID)
252 return (B_FALSE);
253
254 if (source)
255 *source = sourcetype;
256
257 return (B_TRUE);
258 }
259
260 /*
261 * The filesystem is mounted by invoking the system mount utility rather
262 * than by the system call mount(2). This ensures that the /etc/mtab
263 * file is correctly locked for the update. Performing our own locking
264 * and /etc/mtab update requires making an unsafe assumption about how
265 * the mount utility performs its locking. Unfortunately, this also means
266 * in the case of a mount failure we do not have the exact errno. We must
267 * make due with return value from the mount process.
268 *
269 * In the long term a shared library called libmount is under development
270 * which provides a common API to address the locking and errno issues.
271 * Once the standard mount utility has been updated to use this library
272 * we can add an autoconf check to conditionally use it.
273 *
274 * http://www.kernel.org/pub/linux/utils/util-linux/libmount-docs/index.html
275 */
276
277 static int
278 do_mount(const char *src, const char *mntpt, char *opts)
279 {
280 char *argv[8] = {
281 "/bin/mount",
282 "-t", MNTTYPE_ZFS,
283 "-o", opts,
284 (char *)src,
285 (char *)mntpt,
286 (char *)NULL };
287 int rc;
288
289 /* Return only the most critical mount error */
290 rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
291 if (rc) {
292 if (rc & MOUNT_FILEIO)
293 return (EIO);
294 if (rc & MOUNT_USER)
295 return (EINTR);
296 if (rc & MOUNT_SOFTWARE)
297 return (EPIPE);
298 if (rc & MOUNT_BUSY)
299 return (EBUSY);
300 if (rc & MOUNT_SYSERR)
301 return (EAGAIN);
302 if (rc & MOUNT_USAGE)
303 return (EINVAL);
304
305 return (ENXIO); /* Generic error */
306 }
307
308 return (0);
309 }
310
311 static int
312 do_unmount(const char *mntpt, int flags)
313 {
314 char force_opt[] = "-f";
315 char lazy_opt[] = "-l";
316 char *argv[7] = {
317 "/bin/umount",
318 "-t", MNTTYPE_ZFS,
319 NULL, NULL, NULL, NULL };
320 int rc, count = 3;
321
322 if (flags & MS_FORCE) {
323 argv[count] = force_opt;
324 count++;
325 }
326
327 if (flags & MS_DETACH) {
328 argv[count] = lazy_opt;
329 count++;
330 }
331
332 argv[count] = (char *)mntpt;
333 rc = libzfs_run_process(argv[0], argv, STDOUT_VERBOSE|STDERR_VERBOSE);
334
335 return (rc ? EINVAL : 0);
336 }
337
338 static int
339 zfs_add_option(zfs_handle_t *zhp, char *options, int len,
340 zfs_prop_t prop, char *on, char *off)
341 {
342 char *source;
343 uint64_t value;
344
345 /* Skip adding duplicate default options */
346 if ((strstr(options, on) != NULL) || (strstr(options, off) != NULL))
347 return (0);
348
349 /*
350 * zfs_prop_get_int() is not used to ensure our mount options
351 * are not influenced by the current /proc/self/mounts contents.
352 */
353 value = getprop_uint64(zhp, prop, &source);
354
355 (void) strlcat(options, ",", len);
356 (void) strlcat(options, value ? on : off, len);
357
358 return (0);
359 }
360
361 static int
362 zfs_add_options(zfs_handle_t *zhp, char *options, int len)
363 {
364 int error = 0;
365
366 error = zfs_add_option(zhp, options, len,
367 ZFS_PROP_ATIME, MNTOPT_ATIME, MNTOPT_NOATIME);
368 /*
369 * don't add relatime/strictatime when atime=off, otherwise strictatime
370 * will force atime=on
371 */
372 if (strstr(options, MNTOPT_NOATIME) == NULL) {
373 error = zfs_add_option(zhp, options, len,
374 ZFS_PROP_RELATIME, MNTOPT_RELATIME, MNTOPT_STRICTATIME);
375 }
376 error = error ? error : zfs_add_option(zhp, options, len,
377 ZFS_PROP_DEVICES, MNTOPT_DEVICES, MNTOPT_NODEVICES);
378 error = error ? error : zfs_add_option(zhp, options, len,
379 ZFS_PROP_EXEC, MNTOPT_EXEC, MNTOPT_NOEXEC);
380 error = error ? error : zfs_add_option(zhp, options, len,
381 ZFS_PROP_READONLY, MNTOPT_RO, MNTOPT_RW);
382 error = error ? error : zfs_add_option(zhp, options, len,
383 ZFS_PROP_SETUID, MNTOPT_SETUID, MNTOPT_NOSETUID);
384 error = error ? error : zfs_add_option(zhp, options, len,
385 ZFS_PROP_NBMAND, MNTOPT_NBMAND, MNTOPT_NONBMAND);
386
387 return (error);
388 }
389
390 /*
391 * Mount the given filesystem.
392 */
393 int
394 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
395 {
396 struct stat buf;
397 char mountpoint[ZFS_MAXPROPLEN];
398 char mntopts[MNT_LINE_MAX];
399 char overlay[ZFS_MAXPROPLEN];
400 libzfs_handle_t *hdl = zhp->zfs_hdl;
401 int remount = 0, rc;
402
403 if (options == NULL) {
404 (void) strlcpy(mntopts, MNTOPT_DEFAULTS, sizeof (mntopts));
405 } else {
406 (void) strlcpy(mntopts, options, sizeof (mntopts));
407 }
408
409 if (strstr(mntopts, MNTOPT_REMOUNT) != NULL)
410 remount = 1;
411
412 /*
413 * If the pool is imported read-only then all mounts must be read-only
414 */
415 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
416 (void) strlcat(mntopts, "," MNTOPT_RO, sizeof (mntopts));
417
418 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
419 return (0);
420
421 /*
422 * Append default mount options which apply to the mount point.
423 * This is done because under Linux (unlike Solaris) multiple mount
424 * points may reference a single super block. This means that just
425 * given a super block there is no back reference to update the per
426 * mount point options.
427 */
428 rc = zfs_add_options(zhp, mntopts, sizeof (mntopts));
429 if (rc) {
430 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
431 "default options unavailable"));
432 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
433 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
434 mountpoint));
435 }
436
437 /*
438 * Append zfsutil option so the mount helper allow the mount
439 */
440 strlcat(mntopts, "," MNTOPT_ZFSUTIL, sizeof (mntopts));
441
442 /* Create the directory if it doesn't already exist */
443 if (lstat(mountpoint, &buf) != 0) {
444 if (mkdirp(mountpoint, 0755) != 0) {
445 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
446 "failed to create mountpoint"));
447 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
448 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
449 mountpoint));
450 }
451 }
452
453 /*
454 * Overlay mounts are disabled by default but may be enabled
455 * via the 'overlay' property or the 'zfs mount -O' option.
456 */
457 if (!(flags & MS_OVERLAY)) {
458 if (zfs_prop_get(zhp, ZFS_PROP_OVERLAY, overlay,
459 sizeof (overlay), NULL, NULL, 0, B_FALSE) == 0) {
460 if (strcmp(overlay, "on") == 0) {
461 flags |= MS_OVERLAY;
462 }
463 }
464 }
465
466 /*
467 * Determine if the mountpoint is empty. If so, refuse to perform the
468 * mount. We don't perform this check if 'remount' is
469 * specified or if overlay option(-O) is given
470 */
471 if ((flags & MS_OVERLAY) == 0 && !remount &&
472 !dir_is_empty(mountpoint)) {
473 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
474 "directory is not empty"));
475 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
476 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
477 }
478
479 /* perform the mount */
480 rc = do_mount(zfs_get_name(zhp), mountpoint, mntopts);
481 if (rc) {
482 /*
483 * Generic errors are nasty, but there are just way too many
484 * from mount(), and they're well-understood. We pick a few
485 * common ones to improve upon.
486 */
487 if (rc == EBUSY) {
488 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
489 "mountpoint or dataset is busy"));
490 } else if (rc == EPERM) {
491 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
492 "Insufficient privileges"));
493 } else if (rc == ENOTSUP) {
494 char buf[256];
495 int spa_version;
496
497 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
498 (void) snprintf(buf, sizeof (buf),
499 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
500 "file system on a version %d pool. Pool must be"
501 " upgraded to mount this file system."),
502 (u_longlong_t)zfs_prop_get_int(zhp,
503 ZFS_PROP_VERSION), spa_version);
504 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
505 } else {
506 zfs_error_aux(hdl, strerror(rc));
507 }
508 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
509 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
510 zhp->zfs_name));
511 }
512
513 /* remove the mounted entry before re-adding on remount */
514 if (remount)
515 libzfs_mnttab_remove(hdl, zhp->zfs_name);
516
517 /* add the mounted entry into our cache */
518 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
519 return (0);
520 }
521
522 /*
523 * Unmount a single filesystem.
524 */
525 static int
526 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
527 {
528 int error;
529
530 error = do_unmount(mountpoint, flags);
531 if (error != 0) {
532 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
533 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
534 mountpoint));
535 }
536
537 return (0);
538 }
539
540 /*
541 * Unmount the given filesystem.
542 */
543 int
544 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
545 {
546 libzfs_handle_t *hdl = zhp->zfs_hdl;
547 struct mnttab entry;
548 char *mntpt = NULL;
549
550 /* check to see if we need to unmount the filesystem */
551 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
552 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
553 /*
554 * mountpoint may have come from a call to
555 * getmnt/getmntany if it isn't NULL. If it is NULL,
556 * we know it comes from libzfs_mnttab_find which can
557 * then get freed later. We strdup it to play it safe.
558 */
559 if (mountpoint == NULL)
560 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
561 else
562 mntpt = zfs_strdup(hdl, mountpoint);
563
564 /*
565 * Unshare and unmount the filesystem
566 */
567 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0) {
568 free(mntpt);
569 return (-1);
570 }
571
572 if (unmount_one(hdl, mntpt, flags) != 0) {
573 free(mntpt);
574 (void) zfs_shareall(zhp);
575 return (-1);
576 }
577 libzfs_mnttab_remove(hdl, zhp->zfs_name);
578 free(mntpt);
579 }
580
581 return (0);
582 }
583
584 /*
585 * Unmount this filesystem and any children inheriting the mountpoint property.
586 * To do this, just act like we're changing the mountpoint property, but don't
587 * remount the filesystems afterwards.
588 */
589 int
590 zfs_unmountall(zfs_handle_t *zhp, int flags)
591 {
592 prop_changelist_t *clp;
593 int ret;
594
595 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
596 if (clp == NULL)
597 return (-1);
598
599 ret = changelist_prefix(clp);
600 changelist_free(clp);
601
602 return (ret);
603 }
604
605 boolean_t
606 zfs_is_shared(zfs_handle_t *zhp)
607 {
608 zfs_share_type_t rc = 0;
609 zfs_share_proto_t *curr_proto;
610
611 if (ZFS_IS_VOLUME(zhp))
612 return (B_FALSE);
613
614 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
615 curr_proto++)
616 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
617
618 return (rc ? B_TRUE : B_FALSE);
619 }
620
621 int
622 zfs_share(zfs_handle_t *zhp)
623 {
624 assert(!ZFS_IS_VOLUME(zhp));
625 return (zfs_share_proto(zhp, share_all_proto));
626 }
627
628 int
629 zfs_unshare(zfs_handle_t *zhp)
630 {
631 assert(!ZFS_IS_VOLUME(zhp));
632 return (zfs_unshareall(zhp));
633 }
634
635 /*
636 * Check to see if the filesystem is currently shared.
637 */
638 zfs_share_type_t
639 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
640 {
641 char *mountpoint;
642 zfs_share_type_t rc;
643
644 if (!zfs_is_mounted(zhp, &mountpoint))
645 return (SHARED_NOT_SHARED);
646
647 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))) {
648 if (where != NULL)
649 *where = mountpoint;
650 else
651 free(mountpoint);
652 return (rc);
653 } else {
654 free(mountpoint);
655 return (SHARED_NOT_SHARED);
656 }
657 }
658
659 boolean_t
660 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
661 {
662 return (zfs_is_shared_proto(zhp, where,
663 PROTO_NFS) != SHARED_NOT_SHARED);
664 }
665
666 boolean_t
667 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
668 {
669 return (zfs_is_shared_proto(zhp, where,
670 PROTO_SMB) != SHARED_NOT_SHARED);
671 }
672
673 /*
674 * zfs_init_libshare(zhandle, service)
675 *
676 * Initialize the libshare API if it hasn't already been initialized.
677 * In all cases it returns 0 if it succeeded and an error if not. The
678 * service value is which part(s) of the API to initialize and is a
679 * direct map to the libshare sa_init(service) interface.
680 */
681 int
682 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
683 {
684 int ret = SA_OK;
685
686 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
687 /*
688 * We had a cache miss. Most likely it is a new ZFS
689 * dataset that was just created. We want to make sure
690 * so check timestamps to see if a different process
691 * has updated any of the configuration. If there was
692 * some non-ZFS change, we need to re-initialize the
693 * internal cache.
694 */
695 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
696 if (sa_needs_refresh(zhandle->libzfs_sharehdl)) {
697 zfs_uninit_libshare(zhandle);
698 zhandle->libzfs_sharehdl = sa_init(service);
699 }
700 }
701
702 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
703 zhandle->libzfs_sharehdl = sa_init(service);
704
705 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
706 ret = SA_NO_MEMORY;
707
708 return (ret);
709 }
710
711 /*
712 * zfs_uninit_libshare(zhandle)
713 *
714 * Uninitialize the libshare API if it hasn't already been
715 * uninitialized. It is OK to call multiple times.
716 */
717 void
718 zfs_uninit_libshare(libzfs_handle_t *zhandle)
719 {
720 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
721 sa_fini(zhandle->libzfs_sharehdl);
722 zhandle->libzfs_sharehdl = NULL;
723 }
724 }
725
726 /*
727 * zfs_parse_options(options, proto)
728 *
729 * Call the legacy parse interface to get the protocol specific
730 * options using the NULL arg to indicate that this is a "parse" only.
731 */
732 int
733 zfs_parse_options(char *options, zfs_share_proto_t proto)
734 {
735 return (sa_parse_legacy_options(NULL, options,
736 proto_table[proto].p_name));
737 }
738
739 /*
740 * Share the given filesystem according to the options in the specified
741 * protocol specific properties (sharenfs, sharesmb). We rely
742 * on "libshare" to do the dirty work for us.
743 */
744 static int
745 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
746 {
747 char mountpoint[ZFS_MAXPROPLEN];
748 char shareopts[ZFS_MAXPROPLEN];
749 char sourcestr[ZFS_MAXPROPLEN];
750 libzfs_handle_t *hdl = zhp->zfs_hdl;
751 sa_share_t share;
752 zfs_share_proto_t *curr_proto;
753 zprop_source_t sourcetype;
754 int ret;
755
756 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
757 return (0);
758
759 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
760 /*
761 * Return success if there are no share options.
762 */
763 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
764 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
765 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
766 strcmp(shareopts, "off") == 0)
767 continue;
768
769 ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API);
770 if (ret != SA_OK) {
771 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
772 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
773 zfs_get_name(zhp), sa_errorstr(ret));
774 return (-1);
775 }
776
777 /*
778 * If the 'zoned' property is set, then zfs_is_mountable()
779 * will have already bailed out if we are in the global zone.
780 * But local zones cannot be NFS servers, so we ignore it for
781 * local zones as well.
782 */
783 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
784 continue;
785
786 share = sa_find_share(hdl->libzfs_sharehdl, mountpoint);
787 if (share == NULL) {
788 /*
789 * This may be a new file system that was just
790 * created so isn't in the internal cache
791 * (second time through). Rather than
792 * reloading the entire configuration, we can
793 * assume ZFS has done the checking and it is
794 * safe to add this to the internal
795 * configuration.
796 */
797 if (sa_zfs_process_share(hdl->libzfs_sharehdl,
798 NULL, NULL, mountpoint,
799 proto_table[*curr_proto].p_name, sourcetype,
800 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
801 (void) zfs_error_fmt(hdl,
802 proto_table[*curr_proto].p_share_err,
803 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
804 zfs_get_name(zhp));
805 return (-1);
806 }
807 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
808 share = sa_find_share(hdl->libzfs_sharehdl,
809 mountpoint);
810 }
811 if (share != NULL) {
812 int err;
813 err = sa_enable_share(share,
814 proto_table[*curr_proto].p_name);
815 if (err != SA_OK) {
816 (void) zfs_error_fmt(hdl,
817 proto_table[*curr_proto].p_share_err,
818 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
819 zfs_get_name(zhp));
820 return (-1);
821 }
822 } else {
823 (void) zfs_error_fmt(hdl,
824 proto_table[*curr_proto].p_share_err,
825 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
826 zfs_get_name(zhp));
827 return (-1);
828 }
829
830 }
831 return (0);
832 }
833
834
835 int
836 zfs_share_nfs(zfs_handle_t *zhp)
837 {
838 return (zfs_share_proto(zhp, nfs_only));
839 }
840
841 int
842 zfs_share_smb(zfs_handle_t *zhp)
843 {
844 return (zfs_share_proto(zhp, smb_only));
845 }
846
847 int
848 zfs_shareall(zfs_handle_t *zhp)
849 {
850 return (zfs_share_proto(zhp, share_all_proto));
851 }
852
853 /*
854 * Unshare a filesystem by mountpoint.
855 */
856 static int
857 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
858 zfs_share_proto_t proto)
859 {
860 sa_share_t share;
861 int err;
862 char *mntpt;
863 /*
864 * Mountpoint could get trashed if libshare calls getmntany
865 * which it does during API initialization, so strdup the
866 * value.
867 */
868 mntpt = zfs_strdup(hdl, mountpoint);
869
870 /* make sure libshare initialized */
871 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
872 free(mntpt); /* don't need the copy anymore */
873 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
874 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
875 name, sa_errorstr(err)));
876 }
877
878 share = sa_find_share(hdl->libzfs_sharehdl, mntpt);
879 free(mntpt); /* don't need the copy anymore */
880
881 if (share != NULL) {
882 err = sa_disable_share(share, proto_table[proto].p_name);
883 if (err != SA_OK) {
884 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
885 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
886 name, sa_errorstr(err)));
887 }
888 } else {
889 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
890 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
891 name));
892 }
893 return (0);
894 }
895
896 /*
897 * Unshare the given filesystem.
898 */
899 int
900 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
901 zfs_share_proto_t *proto)
902 {
903 libzfs_handle_t *hdl = zhp->zfs_hdl;
904 struct mnttab entry;
905 char *mntpt = NULL;
906
907 /* check to see if need to unmount the filesystem */
908 if (mountpoint != NULL)
909 mntpt = zfs_strdup(hdl, mountpoint);
910
911 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
912 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
913 zfs_share_proto_t *curr_proto;
914
915 if (mountpoint == NULL)
916 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
917
918 for (curr_proto = proto; *curr_proto != PROTO_END;
919 curr_proto++) {
920
921 if (is_shared(hdl, mntpt, *curr_proto) &&
922 unshare_one(hdl, zhp->zfs_name,
923 mntpt, *curr_proto) != 0) {
924 if (mntpt != NULL)
925 free(mntpt);
926 return (-1);
927 }
928 }
929 }
930 if (mntpt != NULL)
931 free(mntpt);
932
933 return (0);
934 }
935
936 int
937 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
938 {
939 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
940 }
941
942 int
943 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
944 {
945 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
946 }
947
948 /*
949 * Same as zfs_unmountall(), but for NFS and SMB unshares.
950 */
951 int
952 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
953 {
954 prop_changelist_t *clp;
955 int ret;
956
957 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
958 if (clp == NULL)
959 return (-1);
960
961 ret = changelist_unshare(clp, proto);
962 changelist_free(clp);
963
964 return (ret);
965 }
966
967 int
968 zfs_unshareall_nfs(zfs_handle_t *zhp)
969 {
970 return (zfs_unshareall_proto(zhp, nfs_only));
971 }
972
973 int
974 zfs_unshareall_smb(zfs_handle_t *zhp)
975 {
976 return (zfs_unshareall_proto(zhp, smb_only));
977 }
978
979 int
980 zfs_unshareall(zfs_handle_t *zhp)
981 {
982 return (zfs_unshareall_proto(zhp, share_all_proto));
983 }
984
985 int
986 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
987 {
988 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
989 }
990
991 /*
992 * Remove the mountpoint associated with the current dataset, if necessary.
993 * We only remove the underlying directory if:
994 *
995 * - The mountpoint is not 'none' or 'legacy'
996 * - The mountpoint is non-empty
997 * - The mountpoint is the default or inherited
998 * - The 'zoned' property is set, or we're in a local zone
999 *
1000 * Any other directories we leave alone.
1001 */
1002 void
1003 remove_mountpoint(zfs_handle_t *zhp)
1004 {
1005 char mountpoint[ZFS_MAXPROPLEN];
1006 zprop_source_t source;
1007
1008 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1009 &source))
1010 return;
1011
1012 if (source == ZPROP_SRC_DEFAULT ||
1013 source == ZPROP_SRC_INHERITED) {
1014 /*
1015 * Try to remove the directory, silently ignoring any errors.
1016 * The filesystem may have since been removed or moved around,
1017 * and this error isn't really useful to the administrator in
1018 * any way.
1019 */
1020 (void) rmdir(mountpoint);
1021 }
1022 }
1023
1024 void
1025 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1026 {
1027 if (cbp->cb_alloc == cbp->cb_used) {
1028 size_t newsz;
1029 void *ptr;
1030
1031 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1032 ptr = zfs_realloc(zhp->zfs_hdl,
1033 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1034 newsz * sizeof (void *));
1035 cbp->cb_handles = ptr;
1036 cbp->cb_alloc = newsz;
1037 }
1038 cbp->cb_handles[cbp->cb_used++] = zhp;
1039 }
1040
1041 static int
1042 mount_cb(zfs_handle_t *zhp, void *data)
1043 {
1044 get_all_cb_t *cbp = data;
1045
1046 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1047 zfs_close(zhp);
1048 return (0);
1049 }
1050
1051 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1052 zfs_close(zhp);
1053 return (0);
1054 }
1055
1056 /*
1057 * If this filesystem is inconsistent and has a receive resume
1058 * token, we can not mount it.
1059 */
1060 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1061 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1062 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1063 zfs_close(zhp);
1064 return (0);
1065 }
1066
1067 libzfs_add_handle(cbp, zhp);
1068 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1069 zfs_close(zhp);
1070 return (-1);
1071 }
1072 return (0);
1073 }
1074
1075 int
1076 libzfs_dataset_cmp(const void *a, const void *b)
1077 {
1078 zfs_handle_t **za = (zfs_handle_t **)a;
1079 zfs_handle_t **zb = (zfs_handle_t **)b;
1080 char mounta[MAXPATHLEN];
1081 char mountb[MAXPATHLEN];
1082 boolean_t gota, gotb;
1083
1084 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1085 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1086 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1087 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1088 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1089 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1090
1091 if (gota && gotb)
1092 return (strcmp(mounta, mountb));
1093
1094 if (gota)
1095 return (-1);
1096 if (gotb)
1097 return (1);
1098
1099 return (strcmp(zfs_get_name(*za), zfs_get_name(*zb)));
1100 }
1101
1102 /*
1103 * Mount and share all datasets within the given pool. This assumes that no
1104 * datasets within the pool are currently mounted. Because users can create
1105 * complicated nested hierarchies of mountpoints, we first gather all the
1106 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1107 * we have the list of all filesystems, we iterate over them in order and mount
1108 * and/or share each one.
1109 */
1110 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1111 int
1112 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1113 {
1114 get_all_cb_t cb = { 0 };
1115 libzfs_handle_t *hdl = zhp->zpool_hdl;
1116 zfs_handle_t *zfsp;
1117 int i, ret = -1;
1118 int *good;
1119
1120 /*
1121 * Gather all non-snap datasets within the pool.
1122 */
1123 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1124 goto out;
1125
1126 libzfs_add_handle(&cb, zfsp);
1127 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1128 goto out;
1129 /*
1130 * Sort the datasets by mountpoint.
1131 */
1132 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1133 libzfs_dataset_cmp);
1134
1135 /*
1136 * And mount all the datasets, keeping track of which ones
1137 * succeeded or failed.
1138 */
1139 if ((good = zfs_alloc(zhp->zpool_hdl,
1140 cb.cb_used * sizeof (int))) == NULL)
1141 goto out;
1142
1143 ret = 0;
1144 for (i = 0; i < cb.cb_used; i++) {
1145 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
1146 ret = -1;
1147 else
1148 good[i] = 1;
1149 }
1150
1151 /*
1152 * Then share all the ones that need to be shared. This needs
1153 * to be a separate pass in order to avoid excessive reloading
1154 * of the configuration. Good should never be NULL since
1155 * zfs_alloc is supposed to exit if memory isn't available.
1156 */
1157 for (i = 0; i < cb.cb_used; i++) {
1158 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1159 ret = -1;
1160 }
1161
1162 free(good);
1163
1164 out:
1165 for (i = 0; i < cb.cb_used; i++)
1166 zfs_close(cb.cb_handles[i]);
1167 free(cb.cb_handles);
1168
1169 return (ret);
1170 }
1171
1172 static int
1173 mountpoint_compare(const void *a, const void *b)
1174 {
1175 const char *mounta = *((char **)a);
1176 const char *mountb = *((char **)b);
1177
1178 return (strcmp(mountb, mounta));
1179 }
1180
1181 /* alias for 2002/240 */
1182 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1183 /*
1184 * Unshare and unmount all datasets within the given pool. We don't want to
1185 * rely on traversing the DSL to discover the filesystems within the pool,
1186 * because this may be expensive (if not all of them are mounted), and can fail
1187 * arbitrarily (on I/O error, for example). Instead, we walk /proc/self/mounts
1188 * and gather all the filesystems that are currently mounted.
1189 */
1190 int
1191 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1192 {
1193 int used, alloc;
1194 struct mnttab entry;
1195 size_t namelen;
1196 char **mountpoints = NULL;
1197 zfs_handle_t **datasets = NULL;
1198 libzfs_handle_t *hdl = zhp->zpool_hdl;
1199 int i;
1200 int ret = -1;
1201 int flags = (force ? MS_FORCE : 0);
1202
1203 namelen = strlen(zhp->zpool_name);
1204
1205 /* Reopen MNTTAB to prevent reading stale data from open file */
1206 if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL)
1207 return (ENOENT);
1208
1209 used = alloc = 0;
1210 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1211 /*
1212 * Ignore non-ZFS entries.
1213 */
1214 if (entry.mnt_fstype == NULL ||
1215 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1216 continue;
1217
1218 /*
1219 * Ignore filesystems not within this pool.
1220 */
1221 if (entry.mnt_mountp == NULL ||
1222 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1223 (entry.mnt_special[namelen] != '/' &&
1224 entry.mnt_special[namelen] != '\0'))
1225 continue;
1226
1227 /*
1228 * At this point we've found a filesystem within our pool. Add
1229 * it to our growing list.
1230 */
1231 if (used == alloc) {
1232 if (alloc == 0) {
1233 if ((mountpoints = zfs_alloc(hdl,
1234 8 * sizeof (void *))) == NULL)
1235 goto out;
1236
1237 if ((datasets = zfs_alloc(hdl,
1238 8 * sizeof (void *))) == NULL)
1239 goto out;
1240
1241 alloc = 8;
1242 } else {
1243 void *ptr;
1244
1245 if ((ptr = zfs_realloc(hdl, mountpoints,
1246 alloc * sizeof (void *),
1247 alloc * 2 * sizeof (void *))) == NULL)
1248 goto out;
1249 mountpoints = ptr;
1250
1251 if ((ptr = zfs_realloc(hdl, datasets,
1252 alloc * sizeof (void *),
1253 alloc * 2 * sizeof (void *))) == NULL)
1254 goto out;
1255 datasets = ptr;
1256
1257 alloc *= 2;
1258 }
1259 }
1260
1261 if ((mountpoints[used] = zfs_strdup(hdl,
1262 entry.mnt_mountp)) == NULL)
1263 goto out;
1264
1265 /*
1266 * This is allowed to fail, in case there is some I/O error. It
1267 * is only used to determine if we need to remove the underlying
1268 * mountpoint, so failure is not fatal.
1269 */
1270 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1271
1272 used++;
1273 }
1274
1275 /*
1276 * At this point, we have the entire list of filesystems, so sort it by
1277 * mountpoint.
1278 */
1279 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1280
1281 /*
1282 * Walk through and first unshare everything.
1283 */
1284 for (i = 0; i < used; i++) {
1285 zfs_share_proto_t *curr_proto;
1286 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1287 curr_proto++) {
1288 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1289 unshare_one(hdl, mountpoints[i],
1290 mountpoints[i], *curr_proto) != 0)
1291 goto out;
1292 }
1293 }
1294
1295 /*
1296 * Now unmount everything, removing the underlying directories as
1297 * appropriate.
1298 */
1299 for (i = 0; i < used; i++) {
1300 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1301 goto out;
1302 }
1303
1304 for (i = 0; i < used; i++) {
1305 if (datasets[i])
1306 remove_mountpoint(datasets[i]);
1307 }
1308
1309 ret = 0;
1310 out:
1311 for (i = 0; i < used; i++) {
1312 if (datasets[i])
1313 zfs_close(datasets[i]);
1314 free(mountpoints[i]);
1315 }
1316 free(datasets);
1317 free(mountpoints);
1318
1319 return (ret);
1320 }