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