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