]> CyberLeo.Net >> Repos - FreeBSD/releng/7.2.git/blob - cddl/contrib/opensolaris/lib/libzfs/common/libzfs_mount.c
Create releng/7.2 from stable/7 in preparation for 7.2-RELEASE.
[FreeBSD/releng/7.2.git] / cddl / contrib / opensolaris / lib / libzfs / common / libzfs_mount.c
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 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26
27 #pragma ident   "%Z%%M% %I%     %E% SMI"
28
29 /*
30  * Routines to manage ZFS mounts.  We separate all the nasty routines that have
31  * to deal with the OS.  The following functions are the main entry points --
32  * they are used by mount and unmount and when changing a filesystem's
33  * mountpoint.
34  *
35  *      zfs_is_mounted()
36  *      zfs_mount()
37  *      zfs_unmount()
38  *      zfs_unmountall()
39  *
40  * This file also contains the functions used to manage sharing filesystems via
41  * NFS and iSCSI:
42  *
43  *      zfs_is_shared()
44  *      zfs_share()
45  *      zfs_unshare()
46  *
47  *      zfs_is_shared_nfs()
48  *      zfs_share_nfs()
49  *      zfs_unshare_nfs()
50  *      zfs_unshareall_nfs()
51  *      zfs_is_shared_iscsi()
52  *      zfs_share_iscsi()
53  *      zfs_unshare_iscsi()
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>
73 #include <sys/mnttab.h>
74 #include <sys/mount.h>
75 #include <sys/stat.h>
76
77 #include <libzfs.h>
78
79 #include "libzfs_impl.h"
80
81 static int (*iscsitgt_zfs_share)(const char *);
82 static int (*iscsitgt_zfs_unshare)(const char *);
83 static int (*iscsitgt_zfs_is_shared)(const char *);
84
85 #pragma init(zfs_iscsi_init)
86 static void
87 zfs_iscsi_init(void)
88 {
89         void *libiscsitgt;
90
91         if ((libiscsitgt = dlopen("/lib/libiscsitgt.so.1",
92             RTLD_LAZY | RTLD_GLOBAL)) == NULL ||
93             (iscsitgt_zfs_share = (int (*)(const char *))dlsym(libiscsitgt,
94             "iscsitgt_zfs_share")) == NULL ||
95             (iscsitgt_zfs_unshare = (int (*)(const char *))dlsym(libiscsitgt,
96             "iscsitgt_zfs_unshare")) == NULL ||
97             (iscsitgt_zfs_is_shared = (int (*)(const char *))dlsym(libiscsitgt,
98             "iscsitgt_zfs_is_shared")) == NULL) {
99                 iscsitgt_zfs_share = NULL;
100                 iscsitgt_zfs_unshare = NULL;
101                 iscsitgt_zfs_is_shared = NULL;
102         }
103 }
104
105 /*
106  * Search the sharetab for the given mountpoint, returning true if it is found.
107  */
108 static boolean_t
109 is_shared(libzfs_handle_t *hdl, const char *mountpoint)
110 {
111         char buf[MAXPATHLEN], *tab;
112
113         if (hdl->libzfs_sharetab == NULL)
114                 return (0);
115
116         (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
117
118         while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
119
120                 /* the mountpoint is the first entry on each line */
121                 if ((tab = strchr(buf, '\t')) != NULL) {
122                         *tab = '\0';
123                         if (strcmp(buf, mountpoint) == 0)
124                                 return (B_TRUE);
125                 }
126         }
127
128         return (B_FALSE);
129 }
130
131 #if 0
132 /*
133  * Returns true if the specified directory is empty.  If we can't open the
134  * directory at all, return true so that the mount can fail with a more
135  * informative error message.
136  */
137 static boolean_t
138 dir_is_empty(const char *dirname)
139 {
140         DIR *dirp;
141         struct dirent64 *dp;
142
143         if ((dirp = opendir(dirname)) == NULL)
144                 return (B_TRUE);
145
146         while ((dp = readdir64(dirp)) != NULL) {
147
148                 if (strcmp(dp->d_name, ".") == 0 ||
149                     strcmp(dp->d_name, "..") == 0)
150                         continue;
151
152                 (void) closedir(dirp);
153                 return (B_FALSE);
154         }
155
156         (void) closedir(dirp);
157         return (B_TRUE);
158 }
159 #endif
160
161 /*
162  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
163  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
164  * 0.
165  */
166 boolean_t
167 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
168 {
169         struct mnttab search = { 0 }, entry;
170
171         /*
172          * Search for the entry in /etc/mnttab.  We don't bother getting the
173          * mountpoint, as we can just search for the special device.  This will
174          * also let us find mounts when the mountpoint is 'legacy'.
175          */
176         search.mnt_special = (char *)special;
177         search.mnt_fstype = MNTTYPE_ZFS;
178
179         rewind(zfs_hdl->libzfs_mnttab);
180         if (getmntany(zfs_hdl->libzfs_mnttab, &entry, &search) != 0)
181                 return (B_FALSE);
182
183         if (where != NULL)
184                 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
185
186         return (B_TRUE);
187 }
188
189 boolean_t
190 zfs_is_mounted(zfs_handle_t *zhp, char **where)
191 {
192         return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
193 }
194
195 /*
196  * Returns true if the given dataset is mountable, false otherwise.  Returns the
197  * mountpoint in 'buf'.
198  */
199 static boolean_t
200 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
201     zfs_source_t *source)
202 {
203         char sourceloc[ZFS_MAXNAMELEN];
204         zfs_source_t sourcetype;
205
206         if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
207                 return (B_FALSE);
208
209         verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
210             &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
211
212         if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
213             strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
214                 return (B_FALSE);
215
216         if (!zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT))
217                 return (B_FALSE);
218
219         if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
220             getzoneid() == GLOBAL_ZONEID)
221                 return (B_FALSE);
222
223         if (source)
224                 *source = sourcetype;
225
226         return (B_TRUE);
227 }
228
229 /*
230  * Mount the given filesystem.
231  */
232 int
233 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
234 {
235         struct stat buf;
236         char mountpoint[ZFS_MAXPROPLEN];
237         char mntopts[MNT_LINE_MAX];
238         libzfs_handle_t *hdl = zhp->zfs_hdl;
239
240         if (options == NULL)
241                 mntopts[0] = '\0';
242         else
243                 (void) strlcpy(mntopts, options, sizeof (mntopts));
244
245         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
246                 return (0);
247
248         /* Create the directory if it doesn't already exist */
249         if (lstat(mountpoint, &buf) != 0) {
250                 if (mkdirp(mountpoint, 0755) != 0) {
251                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
252                             "failed to create mountpoint"));
253                         return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
254                             dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
255                             mountpoint));
256                 }
257         }
258
259 #if 0   /* FreeBSD: overlay mounts are not checked. */
260         /*
261          * Determine if the mountpoint is empty.  If so, refuse to perform the
262          * mount.  We don't perform this check if MS_OVERLAY is specified, which
263          * would defeat the point.  We also avoid this check if 'remount' is
264          * specified.
265          */
266         if ((flags & MS_OVERLAY) == 0 &&
267             strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
268             !dir_is_empty(mountpoint)) {
269                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
270                     "directory is not empty"));
271                 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
272                     dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
273         }
274 #endif
275
276         /* perform the mount */
277         if (zmount(zfs_get_name(zhp), mountpoint, flags,
278             MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
279                 /*
280                  * Generic errors are nasty, but there are just way too many
281                  * from mount(), and they're well-understood.  We pick a few
282                  * common ones to improve upon.
283                  */
284                 if (errno == EBUSY)
285                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
286                             "mountpoint or dataset is busy"));
287                 else
288                         zfs_error_aux(hdl, strerror(errno));
289
290                 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
291                     dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
292                     zhp->zfs_name));
293         }
294
295         return (0);
296 }
297
298 /*
299  * Unmount a single filesystem.
300  */
301 static int
302 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
303 {
304         if (unmount(mountpoint, flags) != 0) {
305                 zfs_error_aux(hdl, strerror(errno));
306                 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
307                     dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
308                     mountpoint));
309         }
310
311         return (0);
312 }
313
314 /*
315  * Unmount the given filesystem.
316  */
317 int
318 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
319 {
320         struct mnttab search = { 0 }, entry;
321
322         /* check to see if need to unmount the filesystem */
323         search.mnt_special = zhp->zfs_name;
324         search.mnt_fstype = MNTTYPE_ZFS;
325         rewind(zhp->zfs_hdl->libzfs_mnttab);
326         if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
327             getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
328
329                 if (mountpoint == NULL)
330                         mountpoint = entry.mnt_mountp;
331
332                 /*
333                  * Unshare and unmount the filesystem
334                  */
335                 if (zfs_unshare_nfs(zhp, mountpoint) != 0 ||
336                     unmount_one(zhp->zfs_hdl, mountpoint, flags) != 0)
337                         return (-1);
338         }
339
340         return (0);
341 }
342
343 /*
344  * Unmount this filesystem and any children inheriting the mountpoint property.
345  * To do this, just act like we're changing the mountpoint property, but don't
346  * remount the filesystems afterwards.
347  */
348 int
349 zfs_unmountall(zfs_handle_t *zhp, int flags)
350 {
351         prop_changelist_t *clp;
352         int ret;
353
354         clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, flags);
355         if (clp == NULL)
356                 return (-1);
357
358         ret = changelist_prefix(clp);
359         changelist_free(clp);
360
361         return (ret);
362 }
363
364 boolean_t
365 zfs_is_shared(zfs_handle_t *zhp)
366 {
367         if (ZFS_IS_VOLUME(zhp))
368                 return (zfs_is_shared_iscsi(zhp));
369
370         return (zfs_is_shared_nfs(zhp, NULL));
371 }
372
373 int
374 zfs_share(zfs_handle_t *zhp)
375 {
376         if (ZFS_IS_VOLUME(zhp))
377                 return (zfs_share_iscsi(zhp));
378
379         return (zfs_share_nfs(zhp));
380 }
381
382 int
383 zfs_unshare(zfs_handle_t *zhp)
384 {
385         if (ZFS_IS_VOLUME(zhp))
386                 return (zfs_unshare_iscsi(zhp));
387
388         return (zfs_unshare_nfs(zhp, NULL));
389 }
390
391 /*
392  * Check to see if the filesystem is currently shared.
393  */
394 boolean_t
395 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
396 {
397         char *mountpoint;
398
399         if (!zfs_is_mounted(zhp, &mountpoint))
400                 return (B_FALSE);
401
402         if (is_shared(zhp->zfs_hdl, mountpoint)) {
403                 if (where != NULL)
404                         *where = mountpoint;
405                 else
406                         free(mountpoint);
407                 return (B_TRUE);
408         } else {
409                 free(mountpoint);
410                 return (B_FALSE);
411         }
412 }
413
414 /*
415  * Share the given filesystem according to the options in 'sharenfs'.  We rely
416  * on share(1M) to the dirty work for us.
417  */
418 int
419 zfs_share_nfs(zfs_handle_t *zhp)
420 {
421         char mountpoint[ZFS_MAXPROPLEN];
422         char shareopts[ZFS_MAXPROPLEN];
423         char buf[MAXPATHLEN];
424         FILE *fp;
425         libzfs_handle_t *hdl = zhp->zfs_hdl;
426
427         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
428                 return (0);
429
430         /*
431          * Return success if there are no share options.
432          */
433         if (zfs_prop_get(zhp, ZFS_PROP_SHARENFS, shareopts, sizeof (shareopts),
434             NULL, NULL, 0, B_FALSE) != 0 ||
435             strcmp(shareopts, "off") == 0)
436                 return (0);
437
438         /*
439          * If the 'zoned' property is set, then zfs_is_mountable() will have
440          * already bailed out if we are in the global zone.  But local
441          * zones cannot be NFS servers, so we ignore it for local zones as well.
442          */
443         if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
444                 return (0);
445
446 #ifdef __FreeBSD__
447         {
448         int error;
449
450         if (strcmp(shareopts, "on") == 0)
451                 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, "");
452         else
453                 error = fsshare(ZFS_EXPORTS_PATH, mountpoint, shareopts);
454         if (error != 0) {
455                 zfs_error_aux(hdl, "%s", strerror(error));
456                 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
457                     dgettext(TEXT_DOMAIN, "cannot share '%s'"),
458                     zfs_get_name(zhp));
459                 return (-1);
460         }
461         }
462 #else
463         /*
464          * Invoke the share(1M) command.  We always do this, even if it's
465          * currently shared, as the options may have changed.
466          */
467         if (strcmp(shareopts, "on") == 0)
468                 (void) snprintf(buf, sizeof (buf), "/usr/sbin/share "
469                     "-F nfs \"%s\" 2>&1", mountpoint);
470         else
471                 (void) snprintf(buf, sizeof (buf), "/usr/sbin/share "
472                     "-F nfs -o \"%s\" \"%s\" 2>&1", shareopts,
473                     mountpoint);
474
475         if ((fp = popen(buf, "r")) == NULL)
476                 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
477                     dgettext(TEXT_DOMAIN, "cannot share '%s'"),
478                     zfs_get_name(zhp)));
479
480         /*
481          * share(1M) should only produce output if there is some kind
482          * of error.  All output begins with "share_nfs: ", so we trim
483          * this off to get to the real error.
484          */
485         if (fgets(buf, sizeof (buf), fp) != NULL) {
486                 char *colon = strchr(buf, ':');
487
488                 while (buf[strlen(buf) - 1] == '\n')
489                         buf[strlen(buf) - 1] = '\0';
490
491                 if (colon != NULL)
492                         zfs_error_aux(hdl, colon + 2);
493
494                 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
495                     dgettext(TEXT_DOMAIN, "cannot share '%s'"),
496                     zfs_get_name(zhp));
497
498                 verify(pclose(fp) != 0);
499                 return (-1);
500         }
501
502         verify(pclose(fp) == 0);
503 #endif
504
505         return (0);
506 }
507
508 /*
509  * Unshare a filesystem by mountpoint.
510  */
511 static int
512 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint)
513 {
514         char buf[MAXPATHLEN];
515         FILE *fp;
516
517 #ifdef __FreeBSD__
518         {
519         int error;
520
521         error = fsunshare(ZFS_EXPORTS_PATH, mountpoint);
522         if (error != 0) {
523                 zfs_error_aux(hdl, "%s", strerror(error));
524                 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
525                     dgettext(TEXT_DOMAIN,
526                     "cannot unshare '%s'"), name));
527         }
528         }
529 #else
530         (void) snprintf(buf, sizeof (buf),
531             "/usr/sbin/unshare  \"%s\" 2>&1",
532             mountpoint);
533
534         if ((fp = popen(buf, "r")) == NULL)
535                 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
536                     dgettext(TEXT_DOMAIN,
537                     "cannot unshare '%s'"), name));
538
539         /*
540          * unshare(1M) should only produce output if there is
541          * some kind of error.  All output begins with "unshare
542          * nfs: ", so we trim this off to get to the real error.
543          */
544         if (fgets(buf, sizeof (buf), fp) != NULL) {
545                 char *colon = strchr(buf, ':');
546
547                 while (buf[strlen(buf) - 1] == '\n')
548                         buf[strlen(buf) - 1] = '\0';
549
550                 if (colon != NULL)
551                         zfs_error_aux(hdl, colon + 2);
552
553                 verify(pclose(fp) != 0);
554
555                 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
556                     dgettext(TEXT_DOMAIN,
557                     "cannot unshare '%s'"), name));
558         }
559
560         verify(pclose(fp) == 0);
561 #endif
562
563         return (0);
564 }
565
566 /*
567  * Unshare the given filesystem.
568  */
569 int
570 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
571 {
572         struct mnttab search = { 0 }, entry;
573
574         /* check to see if need to unmount the filesystem */
575         search.mnt_special = (char *)zfs_get_name(zhp);
576         search.mnt_fstype = MNTTYPE_ZFS;
577         rewind(zhp->zfs_hdl->libzfs_mnttab);
578         if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
579             getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, &search) == 0)) {
580
581                 if (mountpoint == NULL)
582                         mountpoint = entry.mnt_mountp;
583
584                 if (is_shared(zhp->zfs_hdl, mountpoint) &&
585                     unshare_one(zhp->zfs_hdl, zhp->zfs_name, mountpoint) != 0)
586                         return (-1);
587         }
588
589         return (0);
590 }
591
592 /*
593  * Same as zfs_unmountall(), but for NFS unshares.
594  */
595 int
596 zfs_unshareall_nfs(zfs_handle_t *zhp)
597 {
598         prop_changelist_t *clp;
599         int ret;
600
601         clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0);
602         if (clp == NULL)
603                 return (-1);
604
605         ret = changelist_unshare(clp);
606         changelist_free(clp);
607
608         return (ret);
609 }
610
611 /*
612  * Remove the mountpoint associated with the current dataset, if necessary.
613  * We only remove the underlying directory if:
614  *
615  *      - The mountpoint is not 'none' or 'legacy'
616  *      - The mountpoint is non-empty
617  *      - The mountpoint is the default or inherited
618  *      - The 'zoned' property is set, or we're in a local zone
619  *
620  * Any other directories we leave alone.
621  */
622 void
623 remove_mountpoint(zfs_handle_t *zhp)
624 {
625         char mountpoint[ZFS_MAXPROPLEN];
626         zfs_source_t source;
627
628         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint),
629             &source))
630                 return;
631
632         if (source == ZFS_SRC_DEFAULT ||
633             source == ZFS_SRC_INHERITED) {
634                 /*
635                  * Try to remove the directory, silently ignoring any errors.
636                  * The filesystem may have since been removed or moved around,
637                  * and this error isn't really useful to the administrator in
638                  * any way.
639                  */
640                 (void) rmdir(mountpoint);
641         }
642 }
643
644 boolean_t
645 zfs_is_shared_iscsi(zfs_handle_t *zhp)
646 {
647         return (iscsitgt_zfs_is_shared != NULL &&
648             iscsitgt_zfs_is_shared(zhp->zfs_name) != 0);
649 }
650
651 int
652 zfs_share_iscsi(zfs_handle_t *zhp)
653 {
654         char shareopts[ZFS_MAXPROPLEN];
655         const char *dataset = zhp->zfs_name;
656         libzfs_handle_t *hdl = zhp->zfs_hdl;
657
658         /*
659          * Return success if there are no share options.
660          */
661         if (zfs_prop_get(zhp, ZFS_PROP_SHAREISCSI, shareopts,
662             sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0 ||
663             strcmp(shareopts, "off") == 0)
664                 return (0);
665
666 /* We don't support iSCSI on FreeBSD yet. */
667 #ifdef TODO
668         if (iscsitgt_zfs_share == NULL || iscsitgt_zfs_share(dataset) != 0)
669                 return (zfs_error_fmt(hdl, EZFS_SHAREISCSIFAILED,
670                     dgettext(TEXT_DOMAIN, "cannot share '%s'"), dataset));
671 #endif
672
673         return (0);
674 }
675
676 int
677 zfs_unshare_iscsi(zfs_handle_t *zhp)
678 {
679         const char *dataset = zfs_get_name(zhp);
680         libzfs_handle_t *hdl = zhp->zfs_hdl;
681
682 /* We don't support iSCSI on FreeBSD yet. */
683 #ifdef TODO
684         /*
685          * Return if the volume is not shared
686          */
687         if (!zfs_is_shared_iscsi(zhp))
688                 return (0);
689
690         /*
691          * If this fails with ENODEV it indicates that zvol wasn't shared so
692          * we should return success in that case.
693          */
694         if (iscsitgt_zfs_unshare == NULL ||
695             (iscsitgt_zfs_unshare(dataset) != 0 && errno != ENODEV))
696                 return (zfs_error_fmt(hdl, EZFS_UNSHAREISCSIFAILED,
697                     dgettext(TEXT_DOMAIN, "cannot unshare '%s'"), dataset));
698 #endif
699
700         return (0);
701 }
702
703 typedef struct mount_cbdata {
704         zfs_handle_t    **cb_datasets;
705         int             cb_used;
706         int             cb_alloc;
707 } mount_cbdata_t;
708
709 static int
710 mount_cb(zfs_handle_t *zhp, void *data)
711 {
712         mount_cbdata_t *cbp = data;
713
714         if (!(zfs_get_type(zhp) & (ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME))) {
715                 zfs_close(zhp);
716                 return (0);
717         }
718
719         if (cbp->cb_alloc == cbp->cb_used) {
720                 void *ptr;
721
722                 if ((ptr = zfs_realloc(zhp->zfs_hdl,
723                     cbp->cb_datasets, cbp->cb_alloc * sizeof (void *),
724                     cbp->cb_alloc * 2 * sizeof (void *))) == NULL)
725                         return (-1);
726                 cbp->cb_datasets = ptr;
727
728                 cbp->cb_alloc *= 2;
729         }
730
731         cbp->cb_datasets[cbp->cb_used++] = zhp;
732
733         return (zfs_iter_children(zhp, mount_cb, cbp));
734 }
735
736 static int
737 dataset_cmp(const void *a, const void *b)
738 {
739         zfs_handle_t **za = (zfs_handle_t **)a;
740         zfs_handle_t **zb = (zfs_handle_t **)b;
741         char mounta[MAXPATHLEN];
742         char mountb[MAXPATHLEN];
743         boolean_t gota, gotb;
744
745         if ((gota = (zfs_get_type(*za) == ZFS_TYPE_FILESYSTEM)) != 0)
746                 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
747                     sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
748         if ((gotb = (zfs_get_type(*zb) == ZFS_TYPE_FILESYSTEM)) != 0)
749                 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
750                     sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
751
752         if (gota && gotb)
753                 return (strcmp(mounta, mountb));
754
755         if (gota)
756                 return (-1);
757         if (gotb)
758                 return (1);
759
760         return (strcmp(zfs_get_name(a), zfs_get_name(b)));
761 }
762
763 /*
764  * Mount and share all datasets within the given pool.  This assumes that no
765  * datasets within the pool are currently mounted.  Because users can create
766  * complicated nested hierarchies of mountpoints, we first gather all the
767  * datasets and mountpoints within the pool, and sort them by mountpoint.  Once
768  * we have the list of all filesystems, we iterate over them in order and mount
769  * and/or share each one.
770  */
771 #pragma weak zpool_mount_datasets = zpool_enable_datasets
772 int
773 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
774 {
775         mount_cbdata_t cb = { 0 };
776         libzfs_handle_t *hdl = zhp->zpool_hdl;
777         zfs_handle_t *zfsp;
778         int i, ret = -1;
779
780         /*
781          * Gather all datasets within the pool.
782          */
783         if ((cb.cb_datasets = zfs_alloc(hdl, 4 * sizeof (void *))) == NULL)
784                 return (-1);
785         cb.cb_alloc = 4;
786
787         if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_ANY)) == NULL)
788                 goto out;
789
790         cb.cb_datasets[0] = zfsp;
791         cb.cb_used = 1;
792
793         if (zfs_iter_children(zfsp, mount_cb, &cb) != 0)
794                 goto out;
795
796         /*
797          * Sort the datasets by mountpoint.
798          */
799         qsort(cb.cb_datasets, cb.cb_used, sizeof (void *), dataset_cmp);
800
801         /*
802          * And mount all the datasets.
803          */
804         ret = 0;
805         for (i = 0; i < cb.cb_used; i++) {
806                 if (zfs_mount(cb.cb_datasets[i], mntopts, flags) != 0 ||
807                     zfs_share(cb.cb_datasets[i]) != 0)
808                         ret = -1;
809         }
810
811 out:
812         for (i = 0; i < cb.cb_used; i++)
813                 zfs_close(cb.cb_datasets[i]);
814         free(cb.cb_datasets);
815
816         return (ret);
817 }
818
819
820 static int
821 zvol_cb(const char *dataset, void *data)
822 {
823         libzfs_handle_t *hdl = data;
824         zfs_handle_t *zhp;
825
826         /*
827          * Ignore snapshots and ignore failures from non-existant datasets.
828          */
829         if (strchr(dataset, '@') != NULL ||
830             (zhp = zfs_open(hdl, dataset, ZFS_TYPE_VOLUME)) == NULL)
831                 return (0);
832
833         (void) zfs_unshare_iscsi(zhp);
834
835         zfs_close(zhp);
836
837         return (0);
838 }
839
840 static int
841 mountpoint_compare(const void *a, const void *b)
842 {
843         const char *mounta = *((char **)a);
844         const char *mountb = *((char **)b);
845
846         return (strcmp(mountb, mounta));
847 }
848
849 /*
850  * Unshare and unmount all datasets within the given pool.  We don't want to
851  * rely on traversing the DSL to discover the filesystems within the pool,
852  * because this may be expensive (if not all of them are mounted), and can fail
853  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
854  * gather all the filesystems that are currently mounted.
855  */
856 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
857 int
858 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
859 {
860         int used, alloc;
861         struct statfs *sfs;
862         size_t namelen;
863         char **mountpoints = NULL;
864         zfs_handle_t **datasets = NULL;
865         libzfs_handle_t *hdl = zhp->zpool_hdl;
866         int i, j, n;
867         int ret = -1;
868         int flags = (force ? MS_FORCE : 0);
869
870         /*
871          * First unshare all zvols.
872          */
873         if (zpool_iter_zvol(zhp, zvol_cb, hdl) != 0)
874                 return (-1);
875
876         namelen = strlen(zhp->zpool_name);
877
878         used = alloc = 0;
879         if ((n = getmntinfo(&sfs, MNT_WAIT)) == 0) {
880                 fprintf(stderr, "getmntinfo(): %s\n", strerror(errno));
881                 return (-1);
882         }
883         for (j = 0; j < n; j++) {
884                 /*
885                  * Ignore non-ZFS entries.
886                  */
887                 if (strcmp(sfs[j].f_fstypename, MNTTYPE_ZFS) != 0)
888                         continue;
889
890                 /*
891                  * Ignore filesystems not within this pool.
892                  */
893                 if (strncmp(sfs[j].f_mntfromname, zhp->zpool_name, namelen) != 0 ||
894                     (sfs[j].f_mntfromname[namelen] != '/' &&
895                     sfs[j].f_mntfromname[namelen] != '\0'))
896                         continue;
897
898                 /*
899                  * At this point we've found a filesystem within our pool.  Add
900                  * it to our growing list.
901                  */
902                 if (used == alloc) {
903                         if (alloc == 0) {
904                                 if ((mountpoints = zfs_alloc(hdl,
905                                     8 * sizeof (void *))) == NULL)
906                                         goto out;
907
908                                 if ((datasets = zfs_alloc(hdl,
909                                     8 * sizeof (void *))) == NULL)
910                                         goto out;
911
912                                 alloc = 8;
913                         } else {
914                                 void *ptr;
915
916                                 if ((ptr = zfs_realloc(hdl, mountpoints,
917                                     alloc * sizeof (void *),
918                                     alloc * 2 * sizeof (void *))) == NULL)
919                                         goto out;
920                                 mountpoints = ptr;
921
922                                 if ((ptr = zfs_realloc(hdl, datasets,
923                                     alloc * sizeof (void *),
924                                     alloc * 2 * sizeof (void *))) == NULL)
925                                         goto out;
926                                 datasets = ptr;
927
928                                 alloc *= 2;
929                         }
930                 }
931
932                 if ((mountpoints[used] = zfs_strdup(hdl,
933                     sfs[j].f_mntonname)) == NULL)
934                         goto out;
935
936                 /*
937                  * This is allowed to fail, in case there is some I/O error.  It
938                  * is only used to determine if we need to remove the underlying
939                  * mountpoint, so failure is not fatal.
940                  */
941                 datasets[used] = make_dataset_handle(hdl, sfs[j].f_mntfromname);
942
943                 used++;
944         }
945
946         /*
947          * At this point, we have the entire list of filesystems, so sort it by
948          * mountpoint.
949          */
950         qsort(mountpoints, used, sizeof (char *), mountpoint_compare);
951
952         /*
953          * Walk through and first unshare everything.
954          */
955         for (i = 0; i < used; i++) {
956                 if (is_shared(hdl, mountpoints[i]) &&
957                     unshare_one(hdl, mountpoints[i], mountpoints[i]) != 0)
958                         goto out;
959         }
960
961         /*
962          * Now unmount everything, removing the underlying directories as
963          * appropriate.
964          */
965         for (i = 0; i < used; i++) {
966                 if (unmount_one(hdl, mountpoints[i], flags) != 0)
967                         goto out;
968         }
969
970         for (i = 0; i < used; i++) {
971                 if (datasets[i])
972                         remove_mountpoint(datasets[i]);
973         }
974
975         ret = 0;
976 out:
977         for (i = 0; i < used; i++) {
978                 if (datasets[i])
979                         zfs_close(datasets[i]);
980                 free(mountpoints[i]);
981         }
982         free(datasets);
983         free(mountpoints);
984
985         return (ret);
986 }