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