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