]> git.proxmox.com Git - mirror_zfs-debian.git/blame - lib/libzfs/libzfs_mount.c
Implemented sharing datasets via SMB 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
e18be9a6
SC
443 * mount. We don't perform this check if 'remount' is
444 * specified or if overlay option(-O) is given
34dc7c2f 445 */
e18be9a6
SC
446 if ((flags & MS_OVERLAY) == 0 && !remount &&
447 !dir_is_empty(mountpoint)) {
34dc7c2f
BB
448 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
449 "directory is not empty"));
450 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
451 dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
452 }
453
454 /* perform the mount */
3fb1fcde
BB
455 rc = do_mount(zfs_get_name(zhp), mountpoint, mntopts);
456 if (rc) {
34dc7c2f
BB
457 /*
458 * Generic errors are nasty, but there are just way too many
459 * from mount(), and they're well-understood. We pick a few
460 * common ones to improve upon.
461 */
3fb1fcde 462 if (rc == EBUSY) {
34dc7c2f
BB
463 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
464 "mountpoint or dataset is busy"));
3fb1fcde 465 } else if (rc == EPERM) {
34dc7c2f
BB
466 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
467 "Insufficient privileges"));
3fb1fcde 468 } else if (rc == ENOTSUP) {
428870ff
BB
469 char buf[256];
470 int spa_version;
471
472 VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
473 (void) snprintf(buf, sizeof (buf),
474 dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
475 "file system on a version %d pool. Pool must be"
476 " upgraded to mount this file system."),
477 (u_longlong_t)zfs_prop_get_int(zhp,
478 ZFS_PROP_VERSION), spa_version);
479 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
34dc7c2f 480 } else {
3fb1fcde 481 zfs_error_aux(hdl, strerror(rc));
34dc7c2f 482 }
34dc7c2f
BB
483 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
484 dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
485 zhp->zfs_name));
486 }
487
2cf7f52b
BB
488 /* remove the mounted entry before re-adding on remount */
489 if (remount)
490 libzfs_mnttab_remove(hdl, zhp->zfs_name);
491
fb5f0bc8 492 /* add the mounted entry into our cache */
3fb1fcde 493 libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint, mntopts);
34dc7c2f
BB
494 return (0);
495}
496
497/*
498 * Unmount a single filesystem.
499 */
500static int
501unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
502{
9ac97c2a
BB
503 int error;
504
505 error = do_unmount(mountpoint, flags);
506 if (error != 0) {
34dc7c2f
BB
507 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
508 dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
509 mountpoint));
510 }
511
512 return (0);
513}
514
515/*
516 * Unmount the given filesystem.
517 */
518int
519zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
520{
fb5f0bc8
BB
521 libzfs_handle_t *hdl = zhp->zfs_hdl;
522 struct mnttab entry;
34dc7c2f
BB
523 char *mntpt = NULL;
524
fb5f0bc8 525 /* check to see if we need to unmount the filesystem */
34dc7c2f 526 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
fb5f0bc8 527 libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
34dc7c2f
BB
528 /*
529 * mountpoint may have come from a call to
530 * getmnt/getmntany if it isn't NULL. If it is NULL,
fb5f0bc8
BB
531 * we know it comes from libzfs_mnttab_find which can
532 * then get freed later. We strdup it to play it safe.
34dc7c2f
BB
533 */
534 if (mountpoint == NULL)
fb5f0bc8 535 mntpt = zfs_strdup(hdl, entry.mnt_mountp);
34dc7c2f 536 else
fb5f0bc8 537 mntpt = zfs_strdup(hdl, mountpoint);
34dc7c2f
BB
538
539 /*
540 * Unshare and unmount the filesystem
541 */
542 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
543 return (-1);
544
fb5f0bc8 545 if (unmount_one(hdl, mntpt, flags) != 0) {
34dc7c2f
BB
546 free(mntpt);
547 (void) zfs_shareall(zhp);
548 return (-1);
549 }
fb5f0bc8 550 libzfs_mnttab_remove(hdl, zhp->zfs_name);
34dc7c2f
BB
551 free(mntpt);
552 }
553
554 return (0);
555}
556
557/*
558 * Unmount this filesystem and any children inheriting the mountpoint property.
559 * To do this, just act like we're changing the mountpoint property, but don't
560 * remount the filesystems afterwards.
561 */
562int
563zfs_unmountall(zfs_handle_t *zhp, int flags)
564{
565 prop_changelist_t *clp;
566 int ret;
567
b128c09f 568 clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
34dc7c2f
BB
569 if (clp == NULL)
570 return (-1);
571
572 ret = changelist_prefix(clp);
573 changelist_free(clp);
574
575 return (ret);
576}
577
578boolean_t
579zfs_is_shared(zfs_handle_t *zhp)
580{
581 zfs_share_type_t rc = 0;
582 zfs_share_proto_t *curr_proto;
583
584 if (ZFS_IS_VOLUME(zhp))
428870ff 585 return (B_FALSE);
34dc7c2f
BB
586
587 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
588 curr_proto++)
589 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
590
591 return (rc ? B_TRUE : B_FALSE);
592}
593
594int
595zfs_share(zfs_handle_t *zhp)
596{
572e2857 597 assert(!ZFS_IS_VOLUME(zhp));
34dc7c2f
BB
598 return (zfs_share_proto(zhp, share_all_proto));
599}
600
601int
602zfs_unshare(zfs_handle_t *zhp)
603{
572e2857 604 assert(!ZFS_IS_VOLUME(zhp));
34dc7c2f
BB
605 return (zfs_unshareall(zhp));
606}
607
608/*
609 * Check to see if the filesystem is currently shared.
610 */
611zfs_share_type_t
612zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
613{
614 char *mountpoint;
615 zfs_share_type_t rc;
616
617 if (!zfs_is_mounted(zhp, &mountpoint))
618 return (SHARED_NOT_SHARED);
619
149e873a 620 if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))) {
34dc7c2f
BB
621 if (where != NULL)
622 *where = mountpoint;
623 else
624 free(mountpoint);
625 return (rc);
626 } else {
627 free(mountpoint);
628 return (SHARED_NOT_SHARED);
629 }
630}
631
632boolean_t
633zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
634{
635 return (zfs_is_shared_proto(zhp, where,
636 PROTO_NFS) != SHARED_NOT_SHARED);
637}
638
639boolean_t
640zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
641{
642 return (zfs_is_shared_proto(zhp, where,
643 PROTO_SMB) != SHARED_NOT_SHARED);
644}
645
34dc7c2f
BB
646/*
647 * zfs_init_libshare(zhandle, service)
648 *
649 * Initialize the libshare API if it hasn't already been initialized.
650 * In all cases it returns 0 if it succeeded and an error if not. The
651 * service value is which part(s) of the API to initialize and is a
652 * direct map to the libshare sa_init(service) interface.
653 */
654int
655zfs_init_libshare(libzfs_handle_t *zhandle, int service)
656{
657 int ret = SA_OK;
658
34dc7c2f
BB
659 if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
660 /*
661 * We had a cache miss. Most likely it is a new ZFS
662 * dataset that was just created. We want to make sure
663 * so check timestamps to see if a different process
664 * has updated any of the configuration. If there was
665 * some non-ZFS change, we need to re-initialize the
666 * internal cache.
667 */
668 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
52e7c3a2 669 if (sa_needs_refresh(zhandle->libzfs_sharehdl)) {
34dc7c2f 670 zfs_uninit_libshare(zhandle);
52e7c3a2 671 zhandle->libzfs_sharehdl = sa_init(service);
34dc7c2f
BB
672 }
673 }
674
675 if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
52e7c3a2 676 zhandle->libzfs_sharehdl = sa_init(service);
34dc7c2f
BB
677
678 if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
679 ret = SA_NO_MEMORY;
680
681 return (ret);
682}
683
684/*
685 * zfs_uninit_libshare(zhandle)
686 *
687 * Uninitialize the libshare API if it hasn't already been
688 * uninitialized. It is OK to call multiple times.
689 */
690void
691zfs_uninit_libshare(libzfs_handle_t *zhandle)
692{
693 if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
52e7c3a2 694 sa_fini(zhandle->libzfs_sharehdl);
34dc7c2f
BB
695 zhandle->libzfs_sharehdl = NULL;
696 }
697}
698
699/*
700 * zfs_parse_options(options, proto)
701 *
702 * Call the legacy parse interface to get the protocol specific
703 * options using the NULL arg to indicate that this is a "parse" only.
704 */
705int
706zfs_parse_options(char *options, zfs_share_proto_t proto)
707{
52e7c3a2
GB
708 return (sa_parse_legacy_options(NULL, options,
709 proto_table[proto].p_name));
34dc7c2f
BB
710}
711
712/*
713 * Share the given filesystem according to the options in the specified
714 * protocol specific properties (sharenfs, sharesmb). We rely
645fb9cc 715 * on "libshare" to do the dirty work for us.
34dc7c2f
BB
716 */
717static int
718zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
719{
720 char mountpoint[ZFS_MAXPROPLEN];
721 char shareopts[ZFS_MAXPROPLEN];
722 char sourcestr[ZFS_MAXPROPLEN];
723 libzfs_handle_t *hdl = zhp->zfs_hdl;
724 sa_share_t share;
725 zfs_share_proto_t *curr_proto;
726 zprop_source_t sourcetype;
727 int ret;
728
729 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
730 return (0);
731
732 if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
733 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
734 dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
52e7c3a2 735 zfs_get_name(zhp), sa_errorstr(ret));
34dc7c2f
BB
736 return (-1);
737 }
738
739 for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
740 /*
741 * Return success if there are no share options.
742 */
743 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
744 shareopts, sizeof (shareopts), &sourcetype, sourcestr,
745 ZFS_MAXPROPLEN, B_FALSE) != 0 ||
746 strcmp(shareopts, "off") == 0)
747 continue;
748
749 /*
750 * If the 'zoned' property is set, then zfs_is_mountable()
751 * will have already bailed out if we are in the global zone.
752 * But local zones cannot be NFS servers, so we ignore it for
753 * local zones as well.
754 */
755 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
756 continue;
757
52e7c3a2 758 share = sa_find_share(hdl->libzfs_sharehdl, mountpoint);
34dc7c2f
BB
759 if (share == NULL) {
760 /*
761 * This may be a new file system that was just
762 * created so isn't in the internal cache
763 * (second time through). Rather than
764 * reloading the entire configuration, we can
765 * assume ZFS has done the checking and it is
766 * safe to add this to the internal
767 * configuration.
768 */
52e7c3a2 769 if (sa_zfs_process_share(hdl->libzfs_sharehdl,
34dc7c2f
BB
770 NULL, NULL, mountpoint,
771 proto_table[*curr_proto].p_name, sourcetype,
772 shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
773 (void) zfs_error_fmt(hdl,
774 proto_table[*curr_proto].p_share_err,
775 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
776 zfs_get_name(zhp));
777 return (-1);
778 }
779 hdl->libzfs_shareflags |= ZFSSHARE_MISS;
52e7c3a2 780 share = sa_find_share(hdl->libzfs_sharehdl,
34dc7c2f
BB
781 mountpoint);
782 }
783 if (share != NULL) {
784 int err;
52e7c3a2 785 err = sa_enable_share(share,
34dc7c2f
BB
786 proto_table[*curr_proto].p_name);
787 if (err != SA_OK) {
788 (void) zfs_error_fmt(hdl,
789 proto_table[*curr_proto].p_share_err,
790 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
791 zfs_get_name(zhp));
792 return (-1);
793 }
794 } else {
795 (void) zfs_error_fmt(hdl,
796 proto_table[*curr_proto].p_share_err,
797 dgettext(TEXT_DOMAIN, "cannot share '%s'"),
798 zfs_get_name(zhp));
799 return (-1);
800 }
801
802 }
803 return (0);
804}
805
806
807int
808zfs_share_nfs(zfs_handle_t *zhp)
809{
810 return (zfs_share_proto(zhp, nfs_only));
811}
812
813int
814zfs_share_smb(zfs_handle_t *zhp)
815{
816 return (zfs_share_proto(zhp, smb_only));
817}
818
819int
820zfs_shareall(zfs_handle_t *zhp)
821{
822 return (zfs_share_proto(zhp, share_all_proto));
823}
824
825/*
826 * Unshare a filesystem by mountpoint.
827 */
828static int
829unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
830 zfs_share_proto_t proto)
831{
832 sa_share_t share;
833 int err;
834 char *mntpt;
835 /*
836 * Mountpoint could get trashed if libshare calls getmntany
fb5f0bc8 837 * which it does during API initialization, so strdup the
34dc7c2f
BB
838 * value.
839 */
840 mntpt = zfs_strdup(hdl, mountpoint);
841
842 /* make sure libshare initialized */
843 if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
844 free(mntpt); /* don't need the copy anymore */
845 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
846 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
52e7c3a2 847 name, sa_errorstr(err)));
34dc7c2f
BB
848 }
849
52e7c3a2 850 share = sa_find_share(hdl->libzfs_sharehdl, mntpt);
34dc7c2f
BB
851 free(mntpt); /* don't need the copy anymore */
852
853 if (share != NULL) {
52e7c3a2 854 err = sa_disable_share(share, proto_table[proto].p_name);
34dc7c2f
BB
855 if (err != SA_OK) {
856 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
857 dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
52e7c3a2 858 name, sa_errorstr(err)));
34dc7c2f
BB
859 }
860 } else {
861 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
862 dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
863 name));
864 }
865 return (0);
866}
867
868/*
869 * Unshare the given filesystem.
870 */
871int
872zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
873 zfs_share_proto_t *proto)
874{
fb5f0bc8
BB
875 libzfs_handle_t *hdl = zhp->zfs_hdl;
876 struct mnttab entry;
34dc7c2f
BB
877 char *mntpt = NULL;
878
879 /* check to see if need to unmount the filesystem */
34dc7c2f
BB
880 rewind(zhp->zfs_hdl->libzfs_mnttab);
881 if (mountpoint != NULL)
fb5f0bc8 882 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
34dc7c2f
BB
883
884 if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
fb5f0bc8 885 libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
34dc7c2f
BB
886 zfs_share_proto_t *curr_proto;
887
888 if (mountpoint == NULL)
889 mntpt = zfs_strdup(zhp->zfs_hdl, entry.mnt_mountp);
890
891 for (curr_proto = proto; *curr_proto != PROTO_END;
645fb9cc 892 curr_proto++) {
34dc7c2f 893
fb5f0bc8
BB
894 if (is_shared(hdl, mntpt, *curr_proto) &&
895 unshare_one(hdl, zhp->zfs_name,
645fb9cc 896 mntpt, *curr_proto) != 0) {
34dc7c2f
BB
897 if (mntpt != NULL)
898 free(mntpt);
899 return (-1);
900 }
901 }
902 }
903 if (mntpt != NULL)
904 free(mntpt);
905
906 return (0);
907}
908
909int
910zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
911{
912 return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
913}
914
915int
916zfs_unshare_smb(zfs_handle_t *zhp, const char *mountpoint)
917{
918 return (zfs_unshare_proto(zhp, mountpoint, smb_only));
919}
920
921/*
922 * Same as zfs_unmountall(), but for NFS and SMB unshares.
923 */
924int
925zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
926{
927 prop_changelist_t *clp;
928 int ret;
929
b128c09f 930 clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
34dc7c2f
BB
931 if (clp == NULL)
932 return (-1);
933
934 ret = changelist_unshare(clp, proto);
935 changelist_free(clp);
936
937 return (ret);
938}
939
940int
941zfs_unshareall_nfs(zfs_handle_t *zhp)
942{
943 return (zfs_unshareall_proto(zhp, nfs_only));
944}
945
946int
947zfs_unshareall_smb(zfs_handle_t *zhp)
948{
949 return (zfs_unshareall_proto(zhp, smb_only));
950}
951
952int
953zfs_unshareall(zfs_handle_t *zhp)
954{
955 return (zfs_unshareall_proto(zhp, share_all_proto));
956}
957
958int
959zfs_unshareall_bypath(zfs_handle_t *zhp, const char *mountpoint)
960{
961 return (zfs_unshare_proto(zhp, mountpoint, share_all_proto));
962}
963
964/*
965 * Remove the mountpoint associated with the current dataset, if necessary.
966 * We only remove the underlying directory if:
967 *
968 * - The mountpoint is not 'none' or 'legacy'
969 * - The mountpoint is non-empty
970 * - The mountpoint is the default or inherited
971 * - The 'zoned' property is set, or we're in a local zone
972 *
973 * Any other directories we leave alone.
974 */
975void
976remove_mountpoint(zfs_handle_t *zhp)
977{
978 char mountpoint[ZFS_MAXPROPLEN];
979 zprop_source_t source;
980
981 if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
982 &source))
983 return;
984
985 if (source == ZPROP_SRC_DEFAULT ||
986 source == ZPROP_SRC_INHERITED) {
987 /*
988 * Try to remove the directory, silently ignoring any errors.
989 * The filesystem may have since been removed or moved around,
990 * and this error isn't really useful to the administrator in
991 * any way.
992 */
993 (void) rmdir(mountpoint);
994 }
995}
996
572e2857
BB
997void
998libzfs_add_handle(get_all_cb_t *cbp, zfs_handle_t *zhp)
999{
1000 if (cbp->cb_alloc == cbp->cb_used) {
1001 size_t newsz;
1002 void *ptr;
1003
1004 newsz = cbp->cb_alloc ? cbp->cb_alloc * 2 : 64;
1005 ptr = zfs_realloc(zhp->zfs_hdl,
1006 cbp->cb_handles, cbp->cb_alloc * sizeof (void *),
1007 newsz * sizeof (void *));
1008 cbp->cb_handles = ptr;
1009 cbp->cb_alloc = newsz;
1010 }
1011 cbp->cb_handles[cbp->cb_used++] = zhp;
1012}
34dc7c2f
BB
1013
1014static int
1015mount_cb(zfs_handle_t *zhp, void *data)
1016{
572e2857 1017 get_all_cb_t *cbp = data;
34dc7c2f 1018
572e2857 1019 if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
34dc7c2f
BB
1020 zfs_close(zhp);
1021 return (0);
1022 }
1023
1024 if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_NOAUTO) {
1025 zfs_close(zhp);
1026 return (0);
1027 }
1028
572e2857
BB
1029 libzfs_add_handle(cbp, zhp);
1030 if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1031 zfs_close(zhp);
1032 return (-1);
34dc7c2f 1033 }
572e2857 1034 return (0);
34dc7c2f
BB
1035}
1036
572e2857
BB
1037int
1038libzfs_dataset_cmp(const void *a, const void *b)
34dc7c2f
BB
1039{
1040 zfs_handle_t **za = (zfs_handle_t **)a;
1041 zfs_handle_t **zb = (zfs_handle_t **)b;
1042 char mounta[MAXPATHLEN];
1043 char mountb[MAXPATHLEN];
1044 boolean_t gota, gotb;
1045
1046 if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
1047 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
1048 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
1049 if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
1050 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
1051 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
1052
1053 if (gota && gotb)
1054 return (strcmp(mounta, mountb));
1055
1056 if (gota)
1057 return (-1);
1058 if (gotb)
1059 return (1);
1060
1061 return (strcmp(zfs_get_name(a), zfs_get_name(b)));
1062}
1063
1064/*
1065 * Mount and share all datasets within the given pool. This assumes that no
1066 * datasets within the pool are currently mounted. Because users can create
1067 * complicated nested hierarchies of mountpoints, we first gather all the
1068 * datasets and mountpoints within the pool, and sort them by mountpoint. Once
1069 * we have the list of all filesystems, we iterate over them in order and mount
1070 * and/or share each one.
1071 */
1072#pragma weak zpool_mount_datasets = zpool_enable_datasets
1073int
1074zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1075{
572e2857 1076 get_all_cb_t cb = { 0 };
34dc7c2f
BB
1077 libzfs_handle_t *hdl = zhp->zpool_hdl;
1078 zfs_handle_t *zfsp;
1079 int i, ret = -1;
1080 int *good;
1081
1082 /*
1083 * Gather all non-snap datasets within the pool.
1084 */
34dc7c2f
BB
1085 if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1086 goto out;
1087
572e2857 1088 libzfs_add_handle(&cb, zfsp);
34dc7c2f
BB
1089 if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1090 goto out;
34dc7c2f
BB
1091 /*
1092 * Sort the datasets by mountpoint.
1093 */
572e2857
BB
1094 qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1095 libzfs_dataset_cmp);
34dc7c2f
BB
1096
1097 /*
1098 * And mount all the datasets, keeping track of which ones
d164b209 1099 * succeeded or failed.
34dc7c2f 1100 */
d164b209
BB
1101 if ((good = zfs_alloc(zhp->zpool_hdl,
1102 cb.cb_used * sizeof (int))) == NULL)
1103 goto out;
1104
34dc7c2f
BB
1105 ret = 0;
1106 for (i = 0; i < cb.cb_used; i++) {
572e2857 1107 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
34dc7c2f
BB
1108 ret = -1;
1109 else
1110 good[i] = 1;
1111 }
1112
1113 /*
1114 * Then share all the ones that need to be shared. This needs
1115 * to be a separate pass in order to avoid excessive reloading
1116 * of the configuration. Good should never be NULL since
1117 * zfs_alloc is supposed to exit if memory isn't available.
1118 */
1119 for (i = 0; i < cb.cb_used; i++) {
572e2857 1120 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
34dc7c2f
BB
1121 ret = -1;
1122 }
1123
1124 free(good);
1125
1126out:
1127 for (i = 0; i < cb.cb_used; i++)
572e2857
BB
1128 zfs_close(cb.cb_handles[i]);
1129 free(cb.cb_handles);
34dc7c2f
BB
1130
1131 return (ret);
1132}
1133
34dc7c2f
BB
1134static int
1135mountpoint_compare(const void *a, const void *b)
1136{
1137 const char *mounta = *((char **)a);
1138 const char *mountb = *((char **)b);
1139
1140 return (strcmp(mountb, mounta));
1141}
1142
428870ff
BB
1143/* alias for 2002/240 */
1144#pragma weak zpool_unmount_datasets = zpool_disable_datasets
34dc7c2f
BB
1145/*
1146 * Unshare and unmount all datasets within the given pool. We don't want to
1147 * rely on traversing the DSL to discover the filesystems within the pool,
1148 * because this may be expensive (if not all of them are mounted), and can fail
9a616b5d 1149 * arbitrarily (on I/O error, for example). Instead, we walk /etc/mtab and
34dc7c2f
BB
1150 * gather all the filesystems that are currently mounted.
1151 */
34dc7c2f
BB
1152int
1153zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1154{
1155 int used, alloc;
1156 struct mnttab entry;
1157 size_t namelen;
1158 char **mountpoints = NULL;
1159 zfs_handle_t **datasets = NULL;
1160 libzfs_handle_t *hdl = zhp->zpool_hdl;
1161 int i;
1162 int ret = -1;
1163 int flags = (force ? MS_FORCE : 0);
1164
34dc7c2f
BB
1165 namelen = strlen(zhp->zpool_name);
1166
1167 rewind(hdl->libzfs_mnttab);
1168 used = alloc = 0;
1169 while (getmntent(hdl->libzfs_mnttab, &entry) == 0) {
1170 /*
1171 * Ignore non-ZFS entries.
1172 */
1173 if (entry.mnt_fstype == NULL ||
1174 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
1175 continue;
1176
1177 /*
1178 * Ignore filesystems not within this pool.
1179 */
1180 if (entry.mnt_mountp == NULL ||
1181 strncmp(entry.mnt_special, zhp->zpool_name, namelen) != 0 ||
1182 (entry.mnt_special[namelen] != '/' &&
1183 entry.mnt_special[namelen] != '\0'))
1184 continue;
1185
1186 /*
1187 * At this point we've found a filesystem within our pool. Add
1188 * it to our growing list.
1189 */
1190 if (used == alloc) {
1191 if (alloc == 0) {
1192 if ((mountpoints = zfs_alloc(hdl,
1193 8 * sizeof (void *))) == NULL)
1194 goto out;
1195
1196 if ((datasets = zfs_alloc(hdl,
1197 8 * sizeof (void *))) == NULL)
1198 goto out;
1199
1200 alloc = 8;
1201 } else {
1202 void *ptr;
1203
1204 if ((ptr = zfs_realloc(hdl, mountpoints,
1205 alloc * sizeof (void *),
1206 alloc * 2 * sizeof (void *))) == NULL)
1207 goto out;
1208 mountpoints = ptr;
1209
1210 if ((ptr = zfs_realloc(hdl, datasets,
1211 alloc * sizeof (void *),
1212 alloc * 2 * sizeof (void *))) == NULL)
1213 goto out;
1214 datasets = ptr;
1215
1216 alloc *= 2;
1217 }
1218 }
1219
1220 if ((mountpoints[used] = zfs_strdup(hdl,
1221 entry.mnt_mountp)) == NULL)
1222 goto out;
1223
1224 /*
1225 * This is allowed to fail, in case there is some I/O error. It
1226 * is only used to determine if we need to remove the underlying
1227 * mountpoint, so failure is not fatal.
1228 */
1229 datasets[used] = make_dataset_handle(hdl, entry.mnt_special);
1230
1231 used++;
1232 }
1233
1234 /*
1235 * At this point, we have the entire list of filesystems, so sort it by
1236 * mountpoint.
1237 */
1238 qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
1239
1240 /*
1241 * Walk through and first unshare everything.
1242 */
1243 for (i = 0; i < used; i++) {
1244 zfs_share_proto_t *curr_proto;
1245 for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
1246 curr_proto++) {
1247 if (is_shared(hdl, mountpoints[i], *curr_proto) &&
1248 unshare_one(hdl, mountpoints[i],
1249 mountpoints[i], *curr_proto) != 0)
1250 goto out;
1251 }
1252 }
1253
1254 /*
1255 * Now unmount everything, removing the underlying directories as
1256 * appropriate.
1257 */
1258 for (i = 0; i < used; i++) {
1259 if (unmount_one(hdl, mountpoints[i], flags) != 0)
1260 goto out;
1261 }
1262
1263 for (i = 0; i < used; i++) {
1264 if (datasets[i])
1265 remove_mountpoint(datasets[i]);
1266 }
1267
1268 ret = 0;
1269out:
1270 for (i = 0; i < used; i++) {
1271 if (datasets[i])
1272 zfs_close(datasets[i]);
1273 free(mountpoints[i]);
1274 }
1275 free(datasets);
1276 free(mountpoints);
1277
1278 return (ret);
1279}