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, 2016 by Delphix. All rights reserved.
26 * Copyright 2016 Igor Kozhukhov <[email protected]>
27 * Copyright 2017 Joyent, Inc.
28 * Copyright 2017 RackTop Systems.
29 * Copyright 2018 OmniOS Community Edition (OmniOSce) Association.
30 */
31
32 /*
33 * Routines to manage ZFS mounts. We separate all the nasty routines that have
34 * to deal with the OS. The following functions are the main entry points --
35 * they are used by mount and unmount and when changing a filesystem's
36 * mountpoint.
37 *
38 * zfs_is_mounted()
39 * zfs_mount()
40 * zfs_unmount()
41 * zfs_unmountall()
42 *
43 * This file also contains the functions used to manage sharing filesystems via
44 * NFS and iSCSI:
45 *
46 * zfs_is_shared()
47 * zfs_share()
48 * zfs_unshare()
49 *
50 * zfs_is_shared_nfs()
51 * zfs_is_shared_smb()
52 * zfs_share_proto()
53 * zfs_shareall();
54 * zfs_unshare_nfs()
55 * zfs_unshare_smb()
56 * zfs_unshareall_nfs()
57 * zfs_unshareall_smb()
58 * zfs_unshareall()
59 * zfs_unshareall_bypath()
60 *
61 * The following functions are available for pool consumers, and will
62 * mount/unmount and share/unshare all datasets within pool:
63 *
64 * zpool_enable_datasets()
65 * zpool_disable_datasets()
66 */
67
68 #include <dirent.h>
69 #include <dlfcn.h>
70 #include <errno.h>
71 #include <fcntl.h>
72 #include <libgen.h>
73 #include <libintl.h>
74 #include <stdio.h>
75 #include <stdlib.h>
76 #include <strings.h>
77 #include <unistd.h>
78 #include <zone.h>
79 #include <sys/mntent.h>
80 #include <sys/mount.h>
81 #include <sys/stat.h>
82 #include <sys/statvfs.h>
83
84 #include <libzfs.h>
85
86 #include "libzfs_impl.h"
87 #include <thread_pool.h>
88
89 #include <libshare.h>
90 #define MAXISALEN 257 /* based on sysinfo(2) man page */
91
92 static int mount_tp_nthr = 512; /* tpool threads for multi-threaded mounting */
93
94 static void zfs_mount_task(void *);
95 static int zfs_share_proto(zfs_handle_t *, zfs_share_proto_t *);
96 zfs_share_type_t zfs_is_shared_proto(zfs_handle_t *, char **,
97 zfs_share_proto_t);
98
99 /*
100 * The share protocols table must be in the same order as the zfs_share_proto_t
101 * enum in libzfs_impl.h
102 */
103 typedef struct {
104 zfs_prop_t p_prop;
105 char *p_name;
106 int p_share_err;
107 int p_unshare_err;
108 } proto_table_t;
109
110 proto_table_t proto_table[PROTO_END] = {
111 {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
112 {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
113 };
114
115 zfs_share_proto_t nfs_only[] = {
116 PROTO_NFS,
117 PROTO_END
118 };
119
120 zfs_share_proto_t smb_only[] = {
121 PROTO_SMB,
122 PROTO_END
123 };
124 zfs_share_proto_t share_all_proto[] = {
125 PROTO_NFS,
126 PROTO_SMB,
127 PROTO_END
128 };
129
130 /*
131 * Search the sharetab for the given mountpoint and protocol, returning
132 * a zfs_share_type_t value.
133 */
134 static zfs_share_type_t
is_shared(libzfs_handle_t * hdl,const char * mountpoint,zfs_share_proto_t proto)135 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
136 {
137 char buf[MAXPATHLEN], *tab;
138 char *ptr;
139
140 if (hdl->libzfs_sharetab == NULL)
141 return (SHARED_NOT_SHARED);
142
143 (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
144
145 while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
146
147 /* the mountpoint is the first entry on each line */
148 if ((tab = strchr(buf, '\t')) == NULL)
149 continue;
150
151 *tab = '\0';
152 if (strcmp(buf, mountpoint) == 0) {
153 #ifdef illumos
154 /*
155 * the protocol field is the third field
156 * skip over second field
157 */
158 ptr = ++tab;
159 if ((tab = strchr(ptr, '\t')) == NULL)
160 continue;
161 ptr = ++tab;
162 if ((tab = strchr(ptr, '\t')) == NULL)
163 continue;
164 *tab = '\0';
165 if (strcmp(ptr,
166 proto_table[proto].p_name) == 0) {
167 switch (proto) {
168 case PROTO_NFS:
169 return (SHARED_NFS);
170 case PROTO_SMB:
171 return (SHARED_SMB);
172 default:
173 return (0);
174 }
175 }
176 #else
177 if (proto == PROTO_NFS)
178 return (SHARED_NFS);
179 #endif
180 }
181 }
182
183 return (SHARED_NOT_SHARED);
184 }
185
186 #ifdef illumos
187 static boolean_t
dir_is_empty_stat(const char * dirname)188 dir_is_empty_stat(const char *dirname)
189 {
190 struct stat st;
191
192 /*
193 * We only want to return false if the given path is a non empty
194 * directory, all other errors are handled elsewhere.
195 */
196 if (stat(dirname, &st) < 0 || !S_ISDIR(st.st_mode)) {
197 return (B_TRUE);
198 }
199
200 /*
201 * An empty directory will still have two entries in it, one
202 * entry for each of "." and "..".
203 */
204 if (st.st_size > 2) {
205 return (B_FALSE);
206 }
207
208 return (B_TRUE);
209 }
210
211 static boolean_t
dir_is_empty_readdir(const char * dirname)212 dir_is_empty_readdir(const char *dirname)
213 {
214 DIR *dirp;
215 struct dirent64 *dp;
216 int dirfd;
217
218 if ((dirfd = openat(AT_FDCWD, dirname,
219 O_RDONLY | O_NDELAY | O_LARGEFILE | O_CLOEXEC, 0)) < 0) {
220 return (B_TRUE);
221 }
222
223 if ((dirp = fdopendir(dirfd)) == NULL) {
224 (void) close(dirfd);
225 return (B_TRUE);
226 }
227
228 while ((dp = readdir64(dirp)) != NULL) {
229
230 if (strcmp(dp->d_name, ".") == 0 ||
231 strcmp(dp->d_name, "..") == 0)
232 continue;
233
234 (void) closedir(dirp);
235 return (B_FALSE);
236 }
237
238 (void) closedir(dirp);
239 return (B_TRUE);
240 }
241
242 /*
243 * Returns true if the specified directory is empty. If we can't open the
244 * directory at all, return true so that the mount can fail with a more
245 * informative error message.
246 */
247 static boolean_t
dir_is_empty(const char * dirname)248 dir_is_empty(const char *dirname)
249 {
250 struct statvfs64 st;
251
252 /*
253 * If the statvfs call fails or the filesystem is not a ZFS
254 * filesystem, fall back to the slow path which uses readdir.
255 */
256 if ((statvfs64(dirname, &st) != 0) ||
257 (strcmp(st.f_basetype, "zfs") != 0)) {
258 return (dir_is_empty_readdir(dirname));
259 }
260
261 /*
262 * At this point, we know the provided path is on a ZFS
263 * filesystem, so we can use stat instead of readdir to
264 * determine if the directory is empty or not. We try to avoid
265 * using readdir because that requires opening "dirname"; this
266 * open file descriptor can potentially end up in a child
267 * process if there's a concurrent fork, thus preventing the
268 * zfs_mount() from otherwise succeeding (the open file
269 * descriptor inherited by the child process will cause the
270 * parent's mount to fail with EBUSY). The performance
271 * implications of replacing the open, read, and close with a
272 * single stat is nice; but is not the main motivation for the
273 * added complexity.
274 */
275 return (dir_is_empty_stat(dirname));
276 }
277 #endif
278
279 /*
280 * Checks to see if the mount is active. If the filesystem is mounted, we fill
281 * in 'where' with the current mountpoint, and return 1. Otherwise, we return
282 * 0.
283 */
284 boolean_t
is_mounted(libzfs_handle_t * zfs_hdl,const char * special,char ** where)285 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
286 {
287 struct mnttab entry;
288
289 if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
290 return (B_FALSE);
291
292 if (where != NULL)
293 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
294
295 return (B_TRUE);
296 }
297
298 boolean_t
zfs_is_mounted(zfs_handle_t * zhp,char ** where)299 zfs_is_mounted(zfs_handle_t *zhp, char **where)
300 {
301 return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
302 }
303
304 /*
305 * Returns true if the given dataset is mountable, false otherwise. Returns the
306 * mountpoint in 'buf'.
307 */
308 static boolean_t
zfs_is_mountable(zfs_handle_t * zhp,char * buf,size_t buflen,zprop_source_t * source)309 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
310 zprop_source_t *source)
311 {
312 char sourceloc[MAXNAMELEN];
313 zprop_source_t sourcetype;
314
315 if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
316 return (B_FALSE);
317
318 verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
319 &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
320
321 if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
322 strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
323 return (B_FALSE);
324
325 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
326 return (B_FALSE);
327
328 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
329 getzoneid() == GLOBAL_ZONEID)
330 return (B_FALSE);
331
332 if (source)
333 *source = sourcetype;
334
335 return (B_TRUE);
336 }
337
338 /*
339 * Mount the given filesystem.
340 */
341 int
zfs_mount(zfs_handle_t * zhp,const char * options,int flags)342 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
343 {
344 struct stat buf;
345 char mountpoint[ZFS_MAXPROPLEN];
346 char mntopts[MNT_LINE_MAX];
347 libzfs_handle_t *hdl = zhp->zfs_hdl;
348
349 if (options == NULL)
350 mntopts[0] = '\0';
351 else
352 (void) strlcpy(mntopts, options, sizeof (mntopts));
353
354 /*
355 * If the pool is imported read-only then all mounts must be read-only
356 */
357 if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
358 flags |= MS_RDONLY;
359
360 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
361 return (0);
362
363 /* Create the directory if it doesn't already exist */
364 if (lstat(mountpoint, &buf) != 0) {
365 if (mkdirp(mountpoint, 0755) != 0) {
366 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
367 "failed to create mountpoint"));
368 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
369 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
370 mountpoint));
371 }
372 }
373
374 #ifdef illumos /* FreeBSD: overlay mounts are not checked. */
375 /*
376 * Determine if the mountpoint is empty. If so, refuse to perform the
377 * mount. We don't perform this check if MS_OVERLAY is specified, which
378 * would defeat the point. We also avoid this check if 'remount' is
379 * specified.
380 */
381 if ((flags & MS_OVERLAY) == 0 &&
382 strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
383 !dir_is_empty(mountpoint)) {
384 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
385 "directory is not empty"));
386 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
387 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
388 }
389 #endif
390
391 /* perform the mount */
392 if (zmount(zfs_get_name(zhp), mountpoint, flags,
393 MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
394 /*
395 * Generic errors are nasty, but there are just way too many
396 * from mount(), and they're well-understood. We pick a few
397 * common ones to improve upon.
398 */
399 if (errno == EBUSY) {
400 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
401 "mountpoint or dataset is busy"));
402 } else if (errno == EPERM) {
403 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
404 "Insufficient privileges"));
405 } else if (errno == ENOTSUP) {
406 char buf[256];
407 int spa_version;
408
409 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
410 (void) snprintf(buf, sizeof (buf),
411 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
412 "file system on a version %d pool. Pool must be"
413 " upgraded to mount this file system."),
414 (u_longlong_t)zfs_prop_get_int(zhp,
415 ZFS_PROP_VERSION), spa_version);
416 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
417 } else {
418 zfs_error_aux(hdl, strerror(errno));
419 }
420 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
421 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
422 zhp->zfs_name));
423 }
424
425 /* add the mounted entry into our cache */
426 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
427 mntopts);
428 return (0);
429 }
430
431 /*
432 * Unmount a single filesystem.
433 */
434 static int
unmount_one(libzfs_handle_t * hdl,const char * mountpoint,int flags)435 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
436 {
437 if (umount2(mountpoint, flags) != 0) {
438 zfs_error_aux(hdl, strerror(errno));
439 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
440 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
441 mountpoint));
442 }
443
444 return (0);
445 }
446
447 /*
448 * Unmount the given filesystem.
449 */
450 int
zfs_unmount(zfs_handle_t * zhp,const char * mountpoint,int flags)451 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
452 {
453 libzfs_handle_t *hdl = zhp->zfs_hdl;
454 struct mnttab entry;
455 char *mntpt = NULL;
456
457 /* check to see if we need to unmount the filesystem */
458 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
459 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
460 /*
461 * mountpoint may have come from a call to
462 * getmnt/getmntany if it isn't NULL. If it is NULL,
463 * we know it comes from libzfs_mnttab_find which can
464 * then get freed later. We strdup it to play it safe.
465 */
466 if (mountpoint == NULL)
467 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
468 else
469 mntpt = zfs_strdup(hdl, mountpoint);
470
471 /*
472 * Unshare and unmount the filesystem
473 */
474 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
475 return (-1);
476
477 if (unmount_one(hdl, mntpt, flags) != 0) {
478 free(mntpt);
479 (void) zfs_shareall(zhp);
480 return (-1);
481 }
482 libzfs_mnttab_remove(hdl, zhp->zfs_name);
483 free(mntpt);
484 }
485
486 return (0);
487 }
488
489 /*
490 * Unmount this filesystem and any children inheriting the mountpoint property.
491 * To do this, just act like we're changing the mountpoint property, but don't
492 * remount the filesystems afterwards.
493 */
494 int
zfs_unmountall(zfs_handle_t * zhp,int flags)495 zfs_unmountall(zfs_handle_t *zhp, int flags)
496 {
497 prop_changelist_t *clp;
498 int ret;
499
500 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
501 if (clp == NULL)
502 return (-1);
503
504 ret = changelist_prefix(clp);
505 changelist_free(clp);
506
507 return (ret);
508 }
509
510 boolean_t
zfs_is_shared(zfs_handle_t * zhp)511 zfs_is_shared(zfs_handle_t *zhp)
512 {
513 zfs_share_type_t rc = 0;
514 zfs_share_proto_t *curr_proto;
515
516 if (ZFS_IS_VOLUME(zhp))
517 return (B_FALSE);
518
519 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
520 curr_proto++)
521 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
522
523 return (rc ? B_TRUE : B_FALSE);
524 }
525
526 int
zfs_share(zfs_handle_t * zhp)527 zfs_share(zfs_handle_t *zhp)
528 {
529 assert(!ZFS_IS_VOLUME(zhp));
530 return (zfs_share_proto(zhp, share_all_proto));
531 }
532
533 int
zfs_unshare(zfs_handle_t * zhp)534 zfs_unshare(zfs_handle_t *zhp)
535 {
536 assert(!ZFS_IS_VOLUME(zhp));
537 return (zfs_unshareall(zhp));
538 }
539
540 /*
541 * Check to see if the filesystem is currently shared.
542 */
543 zfs_share_type_t
zfs_is_shared_proto(zfs_handle_t * zhp,char ** where,zfs_share_proto_t proto)544 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
545 {
546 char *mountpoint;
547 zfs_share_type_t rc;
548
549 if (!zfs_is_mounted(zhp, &mountpoint))
550 return (SHARED_NOT_SHARED);
551
552 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))
553 != SHARED_NOT_SHARED) {
554 if (where != NULL)
555 *where = mountpoint;
556 else
557 free(mountpoint);
558 return (rc);
559 } else {
560 free(mountpoint);
561 return (SHARED_NOT_SHARED);
562 }
563 }
564
565 boolean_t
zfs_is_shared_nfs(zfs_handle_t * zhp,char ** where)566 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
567 {
568 return (zfs_is_shared_proto(zhp, where,
569 PROTO_NFS) != SHARED_NOT_SHARED);
570 }
571
572 boolean_t
zfs_is_shared_smb(zfs_handle_t * zhp,char ** where)573 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
574 {
575 return (zfs_is_shared_proto(zhp, where,
576 PROTO_SMB) != SHARED_NOT_SHARED);
577 }
578
579 /*
580 * Make sure things will work if libshare isn't installed by using
581 * wrapper functions that check to see that the pointers to functions
582 * initialized in _zfs_init_libshare() are actually present.
583 */
584
585 #ifdef illumos
586 static sa_handle_t (*_sa_init)(int);
587 static sa_handle_t (*_sa_init_arg)(int, void *);
588 static void (*_sa_fini)(sa_handle_t);
589 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
590 static int (*_sa_enable_share)(sa_share_t, char *);
591 static int (*_sa_disable_share)(sa_share_t, char *);
592 static char *(*_sa_errorstr)(int);
593 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
594 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
595 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
596 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
597 char *, char *, zprop_source_t, char *, char *, char *);
598 static void (*_sa_update_sharetab_ts)(sa_handle_t);
599 #endif
600
601 /*
602 * _zfs_init_libshare()
603 *
604 * Find the libshare.so.1 entry points that we use here and save the
605 * values to be used later. This is triggered by the runtime loader.
606 * Make sure the correct ISA version is loaded.
607 */
608
609 #pragma init(_zfs_init_libshare)
610 static void
_zfs_init_libshare(void)611 _zfs_init_libshare(void)
612 {
613 #ifdef illumos
614 void *libshare;
615 char path[MAXPATHLEN];
616 char isa[MAXISALEN];
617
618 #if defined(_LP64)
619 if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
620 isa[0] = '\0';
621 #else
622 isa[0] = '\0';
623 #endif
624 (void) snprintf(path, MAXPATHLEN,
625 "/usr/lib/%s/libshare.so.1", isa);
626
627 if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
628 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
629 _sa_init_arg = (sa_handle_t (*)(int, void *))dlsym(libshare,
630 "sa_init_arg");
631 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
632 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
633 dlsym(libshare, "sa_find_share");
634 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
635 "sa_enable_share");
636 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
637 "sa_disable_share");
638 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
639 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
640 dlsym(libshare, "sa_parse_legacy_options");
641 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
642 dlsym(libshare, "sa_needs_refresh");
643 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
644 dlsym(libshare, "sa_get_zfs_handle");
645 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
646 sa_share_t, char *, char *, zprop_source_t, char *,
647 char *, char *))dlsym(libshare, "sa_zfs_process_share");
648 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
649 dlsym(libshare, "sa_update_sharetab_ts");
650 if (_sa_init == NULL || _sa_init_arg == NULL ||
651 _sa_fini == NULL || _sa_find_share == NULL ||
652 _sa_enable_share == NULL || _sa_disable_share == NULL ||
653 _sa_errorstr == NULL || _sa_parse_legacy_options == NULL ||
654 _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
655 _sa_zfs_process_share == NULL ||
656 _sa_update_sharetab_ts == NULL) {
657 _sa_init = NULL;
658 _sa_init_arg = NULL;
659 _sa_fini = NULL;
660 _sa_disable_share = NULL;
661 _sa_enable_share = NULL;
662 _sa_errorstr = NULL;
663 _sa_parse_legacy_options = NULL;
664 (void) dlclose(libshare);
665 _sa_needs_refresh = NULL;
666 _sa_get_zfs_handle = NULL;
667 _sa_zfs_process_share = NULL;
668 _sa_update_sharetab_ts = NULL;
669 }
670 }
671 #endif
672 }
673
674 /*
675 * zfs_init_libshare(zhandle, service)
676 *
677 * Initialize the libshare API if it hasn't already been initialized.
678 * In all cases it returns 0 if it succeeded and an error if not. The
679 * service value is which part(s) of the API to initialize and is a
680 * direct map to the libshare sa_init(service) interface.
681 */
682 static int
zfs_init_libshare_impl(libzfs_handle_t * zhandle,int service,void * arg)683 zfs_init_libshare_impl(libzfs_handle_t *zhandle, int service, void *arg)
684 {
685 #ifdef illumos
686 /*
687 * libshare is either not installed or we're in a branded zone. The
688 * rest of the wrapper functions around the libshare calls already
689 * handle NULL function pointers, but we don't want the callers of
690 * zfs_init_libshare() to fail prematurely if libshare is not available.
691 */
692 if (_sa_init == NULL)
693 return (SA_OK);
694
695 /*
696 * Attempt to refresh libshare. This is necessary if there was a cache
697 * miss for a new ZFS dataset that was just created, or if state of the
698 * sharetab file has changed since libshare was last initialized. We
699 * want to make sure so check timestamps to see if a different process
700 * has updated any of the configuration. If there was some non-ZFS
701 * change, we need to re-initialize the internal cache.
702 */
703 if (_sa_needs_refresh != NULL &&
704 _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
705 zfs_uninit_libshare(zhandle);
706 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
707 }
708
709 if (zhandle && zhandle->libzfs_sharehdl == NULL)
710 zhandle->libzfs_sharehdl = _sa_init_arg(service, arg);
711
712 if (zhandle->libzfs_sharehdl == NULL)
713 return (SA_NO_MEMORY);
714 #endif
715
716 return (SA_OK);
717 }
718 int
zfs_init_libshare(libzfs_handle_t * zhandle,int service)719 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
720 {
721 return (zfs_init_libshare_impl(zhandle, service, NULL));
722 }
723
724 int
zfs_init_libshare_arg(libzfs_handle_t * zhandle,int service,void * arg)725 zfs_init_libshare_arg(libzfs_handle_t *zhandle, int service, void *arg)
726 {
727 return (zfs_init_libshare_impl(zhandle, service, arg));
728 }
729
730
731 /*
732 * zfs_uninit_libshare(zhandle)
733 *
734 * Uninitialize the libshare API if it hasn't already been
735 * uninitialized. It is OK to call multiple times.
736 */
737 void
zfs_uninit_libshare(libzfs_handle_t * zhandle)738 zfs_uninit_libshare(libzfs_handle_t *zhandle)
739 {
740 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
741 #ifdef illumos
742 if (_sa_fini != NULL)
743 _sa_fini(zhandle->libzfs_sharehdl);
744 #endif
745 zhandle->libzfs_sharehdl = NULL;
746 }
747 }
748
749 /*
750 * zfs_parse_options(options, proto)
751 *
752 * Call the legacy parse interface to get the protocol specific
753 * options using the NULL arg to indicate that this is a "parse" only.
754 */
755 int
zfs_parse_options(char * options,zfs_share_proto_t proto)756 zfs_parse_options(char *options, zfs_share_proto_t proto)
757 {
758 #ifdef illumos
759 if (_sa_parse_legacy_options != NULL) {
760 return (_sa_parse_legacy_options(NULL, options,
761 proto_table[proto].p_name));
762 }
763 return (SA_CONFIG_ERR);
764 #else
765 return (SA_OK);
766 #endif
767 }
768
769 #ifdef illumos
770 /*
771 * zfs_sa_find_share(handle, path)
772 *
773 * wrapper around sa_find_share to find a share path in the
774 * configuration.
775 */
776 static sa_share_t
zfs_sa_find_share(sa_handle_t handle,char * path)777 zfs_sa_find_share(sa_handle_t handle, char *path)
778 {
779 if (_sa_find_share != NULL)
780 return (_sa_find_share(handle, path));
781 return (NULL);
782 }
783
784 /*
785 * zfs_sa_enable_share(share, proto)
786 *
787 * Wrapper for sa_enable_share which enables a share for a specified
788 * protocol.
789 */
790 static int
zfs_sa_enable_share(sa_share_t share,char * proto)791 zfs_sa_enable_share(sa_share_t share, char *proto)
792 {
793 if (_sa_enable_share != NULL)
794 return (_sa_enable_share(share, proto));
795 return (SA_CONFIG_ERR);
796 }
797
798 /*
799 * zfs_sa_disable_share(share, proto)
800 *
801 * Wrapper for sa_enable_share which disables a share for a specified
802 * protocol.
803 */
804 static int
zfs_sa_disable_share(sa_share_t share,char * proto)805 zfs_sa_disable_share(sa_share_t share, char *proto)
806 {
807 if (_sa_disable_share != NULL)
808 return (_sa_disable_share(share, proto));
809 return (SA_CONFIG_ERR);
810 }
811 #endif /* illumos */
812
813 /*
814 * Share the given filesystem according to the options in the specified
815 * protocol specific properties (sharenfs, sharesmb). We rely
816 * on "libshare" to the dirty work for us.
817 */
818 static int
zfs_share_proto(zfs_handle_t * zhp,zfs_share_proto_t * proto)819 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
820 {
821 char mountpoint[ZFS_MAXPROPLEN];
822 char shareopts[ZFS_MAXPROPLEN];
823 char sourcestr[ZFS_MAXPROPLEN];
824 libzfs_handle_t *hdl = zhp->zfs_hdl;
825 zfs_share_proto_t *curr_proto;
826 zprop_source_t sourcetype;
827 int error, ret;
828
829 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
830 return (0);
831
832 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
833 /*
834 * Return success if there are no share options.
835 */
836 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
837 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
838 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
839 strcmp(shareopts, "off") == 0)
840 continue;
841 #ifdef illumos
842 ret = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_HANDLE,
843 zhp);
844 if (ret != SA_OK) {
845 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
846 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
847 zfs_get_name(zhp), _sa_errorstr != NULL ?
848 _sa_errorstr(ret) : "");
849 return (-1);
850 }
851 #endif
852
853 /*
854 * If the 'zoned' property is set, then zfs_is_mountable()
855 * will have already bailed out if we are in the global zone.
856 * But local zones cannot be NFS servers, so we ignore it for
857 * local zones as well.
858 */
859 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
860 continue;
861
862 #ifdef illumos
863 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
864 if (share == NULL) {
865 /*
866 * This may be a new file system that was just
867 * created so isn't in the internal cache
868 * (second time through). Rather than
869 * reloading the entire configuration, we can
870 * assume ZFS has done the checking and it is
871 * safe to add this to the internal
872 * configuration.
873 */
874 if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
875 NULL, NULL, mountpoint,
876 proto_table[*curr_proto].p_name, sourcetype,
877 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
878 (void) zfs_error_fmt(hdl,
879 proto_table[*curr_proto].p_share_err,
880 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
881 zfs_get_name(zhp));
882 return (-1);
883 }
884 share = zfs_sa_find_share(hdl->libzfs_sharehdl,
885 mountpoint);
886 }
887 if (share != NULL) {
888 int err;
889 err = zfs_sa_enable_share(share,
890 proto_table[*curr_proto].p_name);
891 if (err != SA_OK) {
892 (void) zfs_error_fmt(hdl,
893 proto_table[*curr_proto].p_share_err,
894 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
895 zfs_get_name(zhp));
896 return (-1);
897 }
898 } else
899 #else
900 if (*curr_proto != PROTO_NFS) {
901 fprintf(stderr, "Unsupported share protocol: %d.\n",
902 *curr_proto);
903 continue;
904 }
905
906 if (strcmp(shareopts, "on") == 0)
907 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
908 else
909 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
910 if (error != 0)
911 #endif
912 {
913 (void) zfs_error_fmt(hdl,
914 proto_table[*curr_proto].p_share_err,
915 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
916 zfs_get_name(zhp));
917 return (-1);
918 }
919
920 }
921 return (0);
922 }
923
924
925 int
zfs_share_nfs(zfs_handle_t * zhp)926 zfs_share_nfs(zfs_handle_t *zhp)
927 {
928 return (zfs_share_proto(zhp, nfs_only));
929 }
930
931 int
zfs_share_smb(zfs_handle_t * zhp)932 zfs_share_smb(zfs_handle_t *zhp)
933 {
934 return (zfs_share_proto(zhp, smb_only));
935 }
936
937 int
zfs_shareall(zfs_handle_t * zhp)938 zfs_shareall(zfs_handle_t *zhp)
939 {
940 return (zfs_share_proto(zhp, share_all_proto));
941 }
942
943 /*
944 * Unshare a filesystem by mountpoint.
945 */
946 static int
unshare_one(libzfs_handle_t * hdl,const char * name,const char * mountpoint,zfs_share_proto_t proto)947 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
948 zfs_share_proto_t proto)
949 {
950 #ifdef illumos
951 sa_share_t share;
952 int err;
953 char *mntpt;
954
955 /*
956 * Mountpoint could get trashed if libshare calls getmntany
957 * which it does during API initialization, so strdup the
958 * value.
959 */
960 mntpt = zfs_strdup(hdl, mountpoint);
961
962 /*
963 * make sure libshare initialized, initialize everything because we
964 * don't know what other unsharing may happen later. Functions up the
965 * stack are allowed to initialize instead a subset of shares at the
966 * time the set is known.
967 */
968 if ((err = zfs_init_libshare_arg(hdl, SA_INIT_ONE_SHARE_FROM_NAME,
969 (void *)name)) != SA_OK) {
970 free(mntpt); /* don't need the copy anymore */
971 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
972 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
973 name, _sa_errorstr(err)));
974 }
975
976 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
977 free(mntpt); /* don't need the copy anymore */
978
979 if (share != NULL) {
980 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
981 if (err != SA_OK) {
982 return (zfs_error_fmt(hdl,
983 proto_table[proto].p_unshare_err,
984 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
985 name, _sa_errorstr(err)));
986 }
987 } else {
988 return (zfs_error_fmt(hdl, proto_table[proto].p_unshare_err,
989 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
990 name));
991 }
992 #else
993 char buf[MAXPATHLEN];
994 FILE *fp;
995 int err;
996
997 if (proto != PROTO_NFS) {
998 fprintf(stderr, "No SMB support in FreeBSD yet.\n");
999 return (EOPNOTSUPP);
1000 }
1001
1002 err = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
1003 if (err != 0) {
1004 zfs_error_aux(hdl, "%s", strerror(err));
1005 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
1006 dgettext(TEXT_DOMAIN,
1007 "cannot unshare '%s'"), name));
1008 }
1009 #endif
1010 return (0);
1011 }
1012
1013 /*
1014 * Unshare the given filesystem.
1015 */
1016 int
zfs_unshare_proto(zfs_handle_t * zhp,const char * mountpoint,zfs_share_proto_t * proto)1017 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
1018 zfs_share_proto_t *proto)
1019 {
1020 libzfs_handle_t *hdl = zhp->zfs_hdl;
1021 struct mnttab entry;
1022 char *mntpt = NULL;
1023
1024 /* check to see if need to unmount the filesystem */
1025 rewind(zhp->zfs_hdl->libzfs_mnttab);
1026 if (mountpoint != NULL)
1027 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
1028
1029 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
1030 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
1031 zfs_share_proto_t *curr_proto;
1032
1033 if (mountpoint == NULL)
1034 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
1035
1036 for (curr_proto = proto; *curr_proto != PROTO_END;
1037 curr_proto++) {
1038
1039 if (is_shared(hdl, mntpt, *curr_proto) &&
1040 unshare_one(hdl, zhp->zfs_name,
1041 mntpt, *curr_proto) != 0) {
1042 if (mntpt != NULL)
1043 free(mntpt);
1044 return (-1);
1045 }
1046 }
1047 }
1048 if (mntpt != NULL)
1049 free(mntpt);
1050
1051 return (0);
1052 }
1053
1054 int
zfs_unshare_nfs(zfs_handle_t * zhp,const char * mountpoint)1055 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
1056 {
1057 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
1058 }
1059
1060 int
zfs_unshare_smb(zfs_handle_t * zhp,const char * mountpoint)1061 zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
1062 {
1063 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
1064 }
1065
1066 /*
1067 * Same as zfs_unmountall(), but for NFS and SMB unshares.
1068 */
1069 int
zfs_unshareall_proto(zfs_handle_t * zhp,zfs_share_proto_t * proto)1070 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
1071 {
1072 prop_changelist_t *clp;
1073 int ret;
1074
1075 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
1076 if (clp == NULL)
1077 return (-1);
1078
1079 ret = changelist_unshare(clp, proto);
1080 changelist_free(clp);
1081
1082 return (ret);
1083 }
1084
1085 int
zfs_unshareall_nfs(zfs_handle_t * zhp)1086 zfs_unshareall_nfs(zfs_handle_t *zhp)
1087 {
1088 return (zfs_unshareall_proto(zhp, nfs_only));
1089 }
1090
1091 int
zfs_unshareall_smb(zfs_handle_t * zhp)1092 zfs_unshareall_smb(zfs_handle_t *zhp)
1093 {
1094 return (zfs_unshareall_proto(zhp, smb_only));
1095 }
1096
1097 int
zfs_unshareall(zfs_handle_t * zhp)1098 zfs_unshareall(zfs_handle_t *zhp)
1099 {
1100 return (zfs_unshareall_proto(zhp, share_all_proto));
1101 }
1102
1103 int
zfs_unshareall_bypath(zfs_handle_t * zhp,const char * mountpoint)1104 zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
1105 {
1106 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
1107 }
1108
1109 /*
1110 * Remove the mountpoint associated with the current dataset, if necessary.
1111 * We only remove the underlying directory if:
1112 *
1113 * - The mountpoint is not 'none' or 'legacy'
1114 * - The mountpoint is non-empty
1115 * - The mountpoint is the default or inherited
1116 * - The 'zoned' property is set, or we're in a local zone
1117 *
1118 * Any other directories we leave alone.
1119 */
1120 void
remove_mountpoint(zfs_handle_t * zhp)1121 remove_mountpoint(zfs_handle_t *zhp)
1122 {
1123 char mountpoint[ZFS_MAXPROPLEN];
1124 zprop_source_t source;
1125
1126 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
1127 &source))
1128 return;
1129
1130 if (source == ZPROP_SRC_DEFAULT ||
1131 source == ZPROP_SRC_INHERITED) {
1132 /*
1133 * Try to remove the directory, silently ignoring any errors.
1134 * The filesystem may have since been removed or moved around,
1135 * and this error isn't really useful to the administrator in
1136 * any way.
1137 */
1138 (void) rmdir(mountpoint);
1139 }
1140 }
1141
1142 /*
1143 * Add the given zfs handle to the cb_handles array, dynamically reallocating
1144 * the array if it is out of space
1145 */
1146 void
libzfs_add_handle(get_all_cb_t * cbp,zfs_handle_t * zhp)1147 libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
1148 {
1149 if (cbp->cb_alloc == cbp->cb_used) {
1150 size_t newsz;
1151 zfs_handle_t **newhandles;
1152
1153 newsz = cbp->cb_alloc != 0 ? cbp->cb_alloc * 2 : 64;
1154 newhandles = zfs_realloc(zhp->zfs_hdl,
1155 cbp->cb_handles, cbp->cb_alloc * sizeof (zfs_handle_t *),
1156 newsz * sizeof (zfs_handle_t *));
1157 cbp->cb_handles = newhandles;
1158 cbp->cb_alloc = newsz;
1159 }
1160 cbp->cb_handles[cbp->cb_used++] = zhp;
1161 }
1162
1163 /*
1164 * Recursive helper function used during file system enumeration
1165 */
1166 static int
zfs_iter_cb(zfs_handle_t * zhp,void * data)1167 zfs_iter_cb(zfs_handle_t *zhp, void *data)
1168 {
1169 get_all_cb_t *cbp = data;
1170
1171 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
1172 zfs_close(zhp);
1173 return (0);
1174 }
1175
1176 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1177 zfs_close(zhp);
1178 return (0);
1179 }
1180
1181 /*
1182 * If this filesystem is inconsistent and has a receive resume
1183 * token, we can not mount it.
1184 */
1185 if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) &&
1186 zfs_prop_get(zhp, ZFS_PROP_RECEIVE_RESUME_TOKEN,
1187 NULL, 0, NULL, NULL, 0, B_TRUE) == 0) {
1188 zfs_close(zhp);
1189 return (0);
1190 }
1191
1192 libzfs_add_handle(cbp, zhp);
1193 if (zfs_iter_filesystems(zhp, zfs_iter_cb, cbp) != 0) {
1194 zfs_close(zhp);
1195 return (-1);
1196 }
1197 return (0);
1198 }
1199
1200 /*
1201 * Sort comparator that compares two mountpoint paths. We sort these paths so
1202 * that subdirectories immediately follow their parents. This means that we
1203 * effectively treat the '/' character as the lowest value non-nul char.
1204 * Since filesystems from non-global zones can have the same mountpoint
1205 * as other filesystems, the comparator sorts global zone filesystems to
1206 * the top of the list. This means that the global zone will traverse the
1207 * filesystem list in the correct order and can stop when it sees the
1208 * first zoned filesystem. In a non-global zone, only the delegated
1209 * filesystems are seen.
1210 *
1211 * An example sorted list using this comparator would look like:
1212 *
1213 * /foo
1214 * /foo/bar
1215 * /foo/bar/baz
1216 * /foo/baz
1217 * /foo.bar
1218 * /foo (NGZ1)
1219 * /foo (NGZ2)
1220 *
1221 * The mount code depend on this ordering to deterministically iterate
1222 * over filesystems in order to spawn parallel mount tasks.
1223 */
1224 static int
mountpoint_cmp(const void * arga,const void * argb)1225 mountpoint_cmp(const void *arga, const void *argb)
1226 {
1227 zfs_handle_t *const *zap = arga;
1228 zfs_handle_t *za = *zap;
1229 zfs_handle_t *const *zbp = argb;
1230 zfs_handle_t *zb = *zbp;
1231 char mounta[MAXPATHLEN];
1232 char mountb[MAXPATHLEN];
1233 const char *a = mounta;
1234 const char *b = mountb;
1235 boolean_t gota, gotb;
1236 uint64_t zoneda, zonedb;
1237
1238 zoneda = zfs_prop_get_int(za, ZFS_PROP_ZONED);
1239 zonedb = zfs_prop_get_int(zb, ZFS_PROP_ZONED);
1240 if (zoneda && !zonedb)
1241 return (1);
1242 if (!zoneda && zonedb)
1243 return (-1);
1244 gota = (zfs_get_type(za) == ZFS_TYPE_FILESYSTEM);
1245 if (gota)
1246 verify(zfs_prop_get(za, ZFS_PROP_MOUNTPOINT, mounta,
1247 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1248 gotb = (zfs_get_type(zb) == ZFS_TYPE_FILESYSTEM);
1249 if (gotb)
1250 verify(zfs_prop_get(zb, ZFS_PROP_MOUNTPOINT, mountb,
1251 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1252
1253 if (gota && gotb) {
1254 while (*a != '\0' && (*a == *b)) {
1255 a++;
1256 b++;
1257 }
1258 if (*a == *b)
1259 return (0);
1260 if (*a == '\0')
1261 return (-1);
1262 if (*b == '\0')
1263 return (1);
1264 if (*a == '/')
1265 return (-1);
1266 if (*b == '/')
1267 return (1);
1268 return (*a < *b ? -1 : *a > *b);
1269 }
1270
1271 if (gota)
1272 return (-1);
1273 if (gotb)
1274 return (1);
1275
1276 /*
1277 * If neither filesystem has a mountpoint, revert to sorting by
1278 * datset name.
1279 */
1280 return (strcmp(zfs_get_name(za), zfs_get_name(zb)));
1281 }
1282
1283 /*
1284 * Return true if path2 is a child of path1 or path2 equals path1 or
1285 * path1 is "/" (path2 is always a child of "/").
1286 */
1287 static boolean_t
libzfs_path_contains(const char * path1,const char * path2)1288 libzfs_path_contains(const char *path1, const char *path2)
1289 {
1290 return (strcmp(path1, path2) == 0 || strcmp(path1, "/") == 0 ||
1291 (strstr(path2, path1) == path2 && path2[strlen(path1)] == '/'));
1292 }
1293
1294
1295 static int
non_descendant_idx(zfs_handle_t ** handles,size_t num_handles,int idx)1296 non_descendant_idx(zfs_handle_t **handles, size_t num_handles, int idx)
1297 {
1298 char parent[ZFS_MAXPROPLEN];
1299 char child[ZFS_MAXPROPLEN];
1300 int i;
1301
1302 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, parent,
1303 sizeof (parent), NULL, NULL, 0, B_FALSE) == 0);
1304
1305 for (i = idx + 1; i < num_handles; i++) {
1306 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT, child,
1307 sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1308 if (!libzfs_path_contains(parent, child))
1309 break;
1310 }
1311 return (i);
1312 }
1313
1314 typedef struct mnt_param {
1315 libzfs_handle_t *mnt_hdl;
1316 tpool_t *mnt_tp;
1317 zfs_handle_t **mnt_zhps; /* filesystems to mount */
1318 size_t mnt_num_handles;
1319 int mnt_idx; /* Index of selected entry to mount */
1320 zfs_iter_f mnt_func;
1321 void *mnt_data;
1322 } mnt_param_t;
1323
1324 /*
1325 * Allocate and populate the parameter struct for mount function, and
1326 * schedule mounting of the entry selected by idx.
1327 */
1328 static void
zfs_dispatch_mount(libzfs_handle_t * hdl,zfs_handle_t ** handles,size_t num_handles,int idx,zfs_iter_f func,void * data,tpool_t * tp)1329 zfs_dispatch_mount(libzfs_handle_t *hdl, zfs_handle_t **handles,
1330 size_t num_handles, int idx, zfs_iter_f func, void *data, tpool_t *tp)
1331 {
1332 mnt_param_t *mnt_param = zfs_alloc(hdl, sizeof (mnt_param_t));
1333
1334 mnt_param->mnt_hdl = hdl;
1335 mnt_param->mnt_tp = tp;
1336 mnt_param->mnt_zhps = handles;
1337 mnt_param->mnt_num_handles = num_handles;
1338 mnt_param->mnt_idx = idx;
1339 mnt_param->mnt_func = func;
1340 mnt_param->mnt_data = data;
1341
1342 (void) tpool_dispatch(tp, zfs_mount_task, (void*)mnt_param);
1343 }
1344
1345 /*
1346 * This is the structure used to keep state of mounting or sharing operations
1347 * during a call to zpool_enable_datasets().
1348 */
1349 typedef struct mount_state {
1350 /*
1351 * ms_mntstatus is set to -1 if any mount fails. While multiple threads
1352 * could update this variable concurrently, no synchronization is
1353 * needed as it's only ever set to -1.
1354 */
1355 int ms_mntstatus;
1356 int ms_mntflags;
1357 const char *ms_mntopts;
1358 } mount_state_t;
1359
1360 static int
zfs_mount_one(zfs_handle_t * zhp,void * arg)1361 zfs_mount_one(zfs_handle_t *zhp, void *arg)
1362 {
1363 mount_state_t *ms = arg;
1364 int ret = 0;
1365
1366 if (zfs_mount(zhp, ms->ms_mntopts, ms->ms_mntflags) != 0)
1367 ret = ms->ms_mntstatus = -1;
1368 return (ret);
1369 }
1370
1371 static int
zfs_share_one(zfs_handle_t * zhp,void * arg)1372 zfs_share_one(zfs_handle_t *zhp, void *arg)
1373 {
1374 mount_state_t *ms = arg;
1375 int ret = 0;
1376
1377 if (zfs_share(zhp) != 0)
1378 ret = ms->ms_mntstatus = -1;
1379 return (ret);
1380 }
1381
1382 /*
1383 * Thread pool function to mount one file system. On completion, it finds and
1384 * schedules its children to be mounted. This depends on the sorting done in
1385 * zfs_foreach_mountpoint(). Note that the degenerate case (chain of entries
1386 * each descending from the previous) will have no parallelism since we always
1387 * have to wait for the parent to finish mounting before we can schedule
1388 * its children.
1389 */
1390 static void
zfs_mount_task(void * arg)1391 zfs_mount_task(void *arg)
1392 {
1393 mnt_param_t *mp = arg;
1394 int idx = mp->mnt_idx;
1395 zfs_handle_t **handles = mp->mnt_zhps;
1396 size_t num_handles = mp->mnt_num_handles;
1397 char mountpoint[ZFS_MAXPROPLEN];
1398
1399 verify(zfs_prop_get(handles[idx], ZFS_PROP_MOUNTPOINT, mountpoint,
1400 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) == 0);
1401
1402 if (mp->mnt_func(handles[idx], mp->mnt_data) != 0)
1403 return;
1404
1405 /*
1406 * We dispatch tasks to mount filesystems with mountpoints underneath
1407 * this one. We do this by dispatching the next filesystem with a
1408 * descendant mountpoint of the one we just mounted, then skip all of
1409 * its descendants, dispatch the next descendant mountpoint, and so on.
1410 * The non_descendant_idx() function skips over filesystems that are
1411 * descendants of the filesystem we just dispatched.
1412 */
1413 for (int i = idx + 1; i < num_handles;
1414 i = non_descendant_idx(handles, num_handles, i)) {
1415 char child[ZFS_MAXPROPLEN];
1416 verify(zfs_prop_get(handles[i], ZFS_PROP_MOUNTPOINT,
1417 child, sizeof (child), NULL, NULL, 0, B_FALSE) == 0);
1418
1419 if (!libzfs_path_contains(mountpoint, child))
1420 break; /* not a descendant, return */
1421 zfs_dispatch_mount(mp->mnt_hdl, handles, num_handles, i,
1422 mp->mnt_func, mp->mnt_data, mp->mnt_tp);
1423 }
1424 free(mp);
1425 }
1426
1427 /*
1428 * Issue the func callback for each ZFS handle contained in the handles
1429 * array. This function is used to mount all datasets, and so this function
1430 * guarantees that filesystems for parent mountpoints are called before their
1431 * children. As such, before issuing any callbacks, we first sort the array
1432 * of handles by mountpoint.
1433 *
1434 * Callbacks are issued in one of two ways:
1435 *
1436 * 1. Sequentially: If the parallel argument is B_FALSE or the ZFS_SERIAL_MOUNT
1437 * environment variable is set, then we issue callbacks sequentially.
1438 *
1439 * 2. In parallel: If the parallel argument is B_TRUE and the ZFS_SERIAL_MOUNT
1440 * environment variable is not set, then we use a tpool to dispatch threads
1441 * to mount filesystems in parallel. This function dispatches tasks to mount
1442 * the filesystems at the top-level mountpoints, and these tasks in turn
1443 * are responsible for recursively mounting filesystems in their children
1444 * mountpoints.
1445 */
1446 void
zfs_foreach_mountpoint(libzfs_handle_t * hdl,zfs_handle_t ** handles,size_t num_handles,zfs_iter_f func,void * data,boolean_t parallel)1447 zfs_foreach_mountpoint(libzfs_handle_t *hdl, zfs_handle_t **handles,
1448 size_t num_handles, zfs_iter_f func, void *data, boolean_t parallel)
1449 {
1450 zoneid_t zoneid = getzoneid();
1451
1452 /*
1453 * The ZFS_SERIAL_MOUNT environment variable is an undocumented
1454 * variable that can be used as a convenience to do a/b comparison
1455 * of serial vs. parallel mounting.
1456 */
1457 boolean_t serial_mount = !parallel ||
1458 (getenv("ZFS_SERIAL_MOUNT") != NULL);
1459
1460 /*
1461 * Sort the datasets by mountpoint. See mountpoint_cmp for details
1462 * of how these are sorted.
1463 */
1464 qsort(handles, num_handles, sizeof (zfs_handle_t *), mountpoint_cmp);
1465
1466 if (serial_mount) {
1467 for (int i = 0; i < num_handles; i++) {
1468 func(handles[i], data);
1469 }
1470 return;
1471 }
1472
1473 /*
1474 * Issue the callback function for each dataset using a parallel
1475 * algorithm that uses a thread pool to manage threads.
1476 */
1477 tpool_t *tp = tpool_create(1, mount_tp_nthr, 0, NULL);
1478
1479 /*
1480 * There may be multiple "top level" mountpoints outside of the pool's
1481 * root mountpoint, e.g.: /foo /bar. Dispatch a mount task for each of
1482 * these.
1483 */
1484 for (int i = 0; i < num_handles;
1485 i = non_descendant_idx(handles, num_handles, i)) {
1486 /*
1487 * Since the mountpoints have been sorted so that the zoned
1488 * filesystems are at the end, a zoned filesystem seen from
1489 * the global zone means that we're done.
1490 */
1491 if (zoneid == GLOBAL_ZONEID &&
1492 zfs_prop_get_int(handles[i], ZFS_PROP_ZONED))
1493 break;
1494 zfs_dispatch_mount(hdl, handles, num_handles, i, func, data,
1495 tp);
1496 }
1497
1498 tpool_wait(tp); /* wait for all scheduled mounts to complete */
1499 tpool_destroy(tp);
1500 }
1501
1502 /*
1503 * Mount and share all datasets within the given pool. This assumes that no
1504 * datasets within the pool are currently mounted.
1505 */
1506 #pragma weak zpool_mount_datasets = zpool_enable_datasets
1507 int
zpool_enable_datasets(zpool_handle_t * zhp,const char * mntopts,int flags)1508 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1509 {
1510 get_all_cb_t cb = { 0 };
1511 mount_state_t ms = { 0 };
1512 zfs_handle_t *zfsp;
1513 int ret = 0;
1514
1515 if ((zfsp = zfs_open(zhp->zpool_hdl, zhp->zpool_name,
1516 ZFS_TYPE_DATASET)) == NULL)
1517 goto out;
1518
1519 /*
1520 * Gather all non-snapshot datasets within the pool. Start by adding
1521 * the root filesystem for this pool to the list, and then iterate
1522 * over all child filesystems.
1523 */
1524 libzfs_add_handle(&cb, zfsp);
1525 if (zfs_iter_filesystems(zfsp, zfs_iter_cb, &cb) != 0)
1526 goto out;
1527
1528 /*
1529 * Mount all filesystems
1530 */
1531 ms.ms_mntopts = mntopts;
1532 ms.ms_mntflags = flags;
1533 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1534 zfs_mount_one, &ms, B_TRUE);
1535 if (ms.ms_mntstatus != 0)
1536 ret = ms.ms_mntstatus;
1537
1538 /*
1539 * Share all filesystems that need to be shared. This needs to be
1540 * a separate pass because libshare is not mt-safe, and so we need
1541 * to share serially.
1542 */
1543 ms.ms_mntstatus = 0;
1544 zfs_foreach_mountpoint(zhp->zpool_hdl, cb.cb_handles, cb.cb_used,
1545 zfs_share_one, &ms, B_FALSE);
1546 if (ms.ms_mntstatus != 0)
1547 ret = ms.ms_mntstatus;
1548
1549 out:
1550 for (int i = 0; i < cb.cb_used; i++)
1551 zfs_close(cb.cb_handles[i]);
1552 free(cb.cb_handles);
1553
1554 return (ret);
1555 }
1556
1557 static int
mountpoint_compare(const void * a,const void * b)1558 mountpoint_compare(const void *a, const void *b)
1559 {
1560 const char *mounta = *((char **)a);
1561 const char *mountb = *((char **)b);
1562
1563 return (strcmp(mountb, mounta));
1564 }
1565
1566 /* alias for 2002/240 */
1567 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
1568 /*
1569 * Unshare and unmount all datasets within the given pool. We don't want to
1570 * rely on traversing the DSL to discover the filesystems within the pool,
1571 * because this may be expensive (if not all of them are mounted), and can fail
1572 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mnttab and
1573 * gather all the filesystems that are currently mounted.
1574 */
1575 int
zpool_disable_datasets(zpool_handle_t * zhp,boolean_t force)1576 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1577 {
1578 int used, alloc;
1579 struct mnttab entry;
1580 size_t namelen;
1581 char **mountpoints = NULL;
1582 zfs_handle_t **datasets = NULL;
1583 libzfs_handle_t *hdl = zhp->zpool_hdl;
1584 int i;
1585 int ret = -1;
1586 int flags = (force ? MS_FORCE : 0);
1587 #ifdef illumos
1588 sa_init_selective_arg_t sharearg;
1589 #endif
1590
1591 namelen = strlen(zhp->zpool_name);
1592
1593 rewind(hdl->libzfs_mnttab);
1594 used = alloc = 0;
1595 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1596 /*
1597 * Ignore non-ZFS entries.
1598 */
1599 if (entry.mnt_fstype == NULL ||
1600 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1601 continue;
1602
1603 /*
1604 * Ignore filesystems not within this pool.
1605 */
1606 if (entry.mnt_mountp == NULL ||
1607 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1608 (entry.mnt_special[namelen] != '/' &&
1609 entry.mnt_special[namelen] != '\0'))
1610 continue;
1611
1612 /*
1613 * At this point we've found a filesystem within our pool. Add
1614 * it to our growing list.
1615 */
1616 if (used == alloc) {
1617 if (alloc == 0) {
1618 if ((mountpoints = zfs_alloc(hdl,
1619 8 * sizeof (void *))) == NULL)
1620 goto out;
1621
1622 if ((datasets = zfs_alloc(hdl,
1623 8 * sizeof (void *))) == NULL)
1624 goto out;
1625
1626 alloc = 8;
1627 } else {
1628 void *ptr;
1629
1630 if ((ptr = zfs_realloc(hdl, mountpoints,
1631 alloc * sizeof (void *),
1632 alloc * 2 * sizeof (void *))) == NULL)
1633 goto out;
1634 mountpoints = ptr;
1635
1636 if ((ptr = zfs_realloc(hdl, datasets,
1637 alloc * sizeof (void *),
1638 alloc * 2 * sizeof (void *))) == NULL)
1639 goto out;
1640 datasets = ptr;
1641
1642 alloc *= 2;
1643 }
1644 }
1645
1646 if ((mountpoints[used] = zfs_strdup(hdl,
1647 entry.mnt_mountp)) == NULL)
1648 goto out;
1649
1650 /*
1651 * This is allowed to fail, in case there is some I/O error. It
1652 * is only used to determine if we need to remove the underlying
1653 * mountpoint, so failure is not fatal.
1654 */
1655 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1656
1657 used++;
1658 }
1659
1660 /*
1661 * At this point, we have the entire list of filesystems, so sort it by
1662 * mountpoint.
1663 */
1664 #ifdef illumos
1665 sharearg.zhandle_arr = datasets;
1666 sharearg.zhandle_len = used;
1667 ret = zfs_init_libshare_arg(hdl, SA_INIT_SHARE_API_SELECTIVE,
1668 &sharearg);
1669 if (ret != 0)
1670 goto out;
1671 #endif
1672 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1673
1674 /*
1675 * Walk through and first unshare everything.
1676 */
1677 for (i = 0; i < used; i++) {
1678 zfs_share_proto_t *curr_proto;
1679 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1680 curr_proto++) {
1681 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1682 unshare_one(hdl, mountpoints[i],
1683 mountpoints[i], *curr_proto) != 0)
1684 goto out;
1685 }
1686 }
1687
1688 /*
1689 * Now unmount everything, removing the underlying directories as
1690 * appropriate.
1691 */
1692 for (i = 0; i < used; i++) {
1693 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1694 goto out;
1695 }
1696
1697 for (i = 0; i < used; i++) {
1698 if (datasets[i])
1699 remove_mountpoint(datasets[i]);
1700 }
1701
1702 ret = 0;
1703 out:
1704 for (i = 0; i < used; i++) {
1705 if (datasets[i])
1706 zfs_close(datasets[i]);
1707 free(mountpoints[i]);
1708 }
1709 free(datasets);
1710 free(mountpoints);
1711
1712 return (ret);
1713 }
1714