Autoconf selinux support
[zfs.git] / lib / libzfs / 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 (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
24  */
25
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()
46  *      zfs_share_proto()
47  *      zfs_shareall();
48  *      zfs_unshare_nfs()
49  *      zfs_unshare_smb()
50  *      zfs_unshareall_nfs()
51  *      zfs_unshareall_smb()
52  *      zfs_unshareall()
53  *      zfs_unshareall_bypath()
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/mount.h>
74 #include <sys/stat.h>
75 #ifdef HAVE_LIBSELINUX
76 #include <selinux/selinux.h>
77 #endif /* HAVE_LIBSELINUX */
78
79 #include <libzfs.h>
80
81 #include "libzfs_impl.h"
82
83 #include <libshare.h>
84 #include <sys/systeminfo.h>
85 #define MAXISALEN       257     /* based on sysinfo(2) man page */
86
87 #ifdef HAVE_ZPL
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 /*
93  * The share protocols table must be in the same order as the zfs_share_prot_t
94  * enum in libzfs_impl.h
95  */
96 typedef struct {
97         zfs_prop_t p_prop;
98         char *p_name;
99         int p_share_err;
100         int p_unshare_err;
101 } proto_table_t;
102
103 proto_table_t proto_table[PROTO_END] = {
104         {ZFS_PROP_SHARENFS, "nfs", EZFS_SHARENFSFAILED, EZFS_UNSHARENFSFAILED},
105         {ZFS_PROP_SHARESMB, "smb", EZFS_SHARESMBFAILED, EZFS_UNSHARESMBFAILED},
106 };
107
108 zfs_share_proto_t nfs_only[] = {
109         PROTO_NFS,
110         PROTO_END
111 };
112
113 zfs_share_proto_t smb_only[] = {
114         PROTO_SMB,
115         PROTO_END
116 };
117 zfs_share_proto_t share_all_proto[] = {
118         PROTO_NFS,
119         PROTO_SMB,
120         PROTO_END
121 };
122
123 /*
124  * Search the sharetab for the given mountpoint and protocol, returning
125  * a zfs_share_type_t value.
126  */
127 static zfs_share_type_t
128 is_shared(libzfs_handle_t *hdl, const char *mountpoint, zfs_share_proto_t proto)
129 {
130         char buf[MAXPATHLEN], *tab;
131         char *ptr;
132
133         if (hdl->libzfs_sharetab == NULL)
134                 return (SHARED_NOT_SHARED);
135
136         (void) fseek(hdl->libzfs_sharetab, 0, SEEK_SET);
137
138         while (fgets(buf, sizeof (buf), hdl->libzfs_sharetab) != NULL) {
139
140                 /* the mountpoint is the first entry on each line */
141                 if ((tab = strchr(buf, '\t')) == NULL)
142                         continue;
143
144                 *tab = '\0';
145                 if (strcmp(buf, mountpoint) == 0) {
146                         /*
147                          * the protocol field is the third field
148                          * skip over second field
149                          */
150                         ptr = ++tab;
151                         if ((tab = strchr(ptr, '\t')) == NULL)
152                                 continue;
153                         ptr = ++tab;
154                         if ((tab = strchr(ptr, '\t')) == NULL)
155                                 continue;
156                         *tab = '\0';
157                         if (strcmp(ptr,
158                             proto_table[proto].p_name) == 0) {
159                                 switch (proto) {
160                                 case PROTO_NFS:
161                                         return (SHARED_NFS);
162                                 case PROTO_SMB:
163                                         return (SHARED_SMB);
164                                 default:
165                                         return (0);
166                                 }
167                         }
168                 }
169         }
170
171         return (SHARED_NOT_SHARED);
172 }
173
174 /*
175  * Returns true if the specified directory is empty.  If we can't open the
176  * directory at all, return true so that the mount can fail with a more
177  * informative error message.
178  */
179 static boolean_t
180 dir_is_empty(const char *dirname)
181 {
182         DIR *dirp;
183         struct dirent64 *dp;
184
185         if ((dirp = opendir(dirname)) == NULL)
186                 return (B_TRUE);
187
188         while ((dp = readdir64(dirp)) != NULL) {
189
190                 if (strcmp(dp->d_name, ".") == 0 ||
191                     strcmp(dp->d_name, "..") == 0)
192                         continue;
193
194                 (void) closedir(dirp);
195                 return (B_FALSE);
196         }
197
198         (void) closedir(dirp);
199         return (B_TRUE);
200 }
201
202 /*
203  * Checks to see if the mount is active.  If the filesystem is mounted, we fill
204  * in 'where' with the current mountpoint, and return 1.  Otherwise, we return
205  * 0.
206  */
207 boolean_t
208 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
209 {
210         struct mnttab entry;
211
212         if (libzfs_mnttab_find(zfs_hdl, special, &entry) != 0)
213                 return (B_FALSE);
214
215         if (where != NULL)
216                 *where = zfs_strdup(zfs_hdl, entry.mnt_mountp);
217
218         return (B_TRUE);
219 }
220
221 boolean_t
222 zfs_is_mounted(zfs_handle_t *zhp, char **where)
223 {
224         return (is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where));
225 }
226
227 /*
228  * Returns true if the given dataset is mountable, false otherwise.  Returns the
229  * mountpoint in 'buf'.
230  */
231 static boolean_t
232 zfs_is_mountable(zfs_handle_t *zhp, char *buf, size_t buflen,
233     zprop_source_t *source)
234 {
235         char sourceloc[ZFS_MAXNAMELEN];
236         zprop_source_t sourcetype;
237
238         if (!zfs_prop_valid_for_type(ZFS_PROP_MOUNTPOINT, zhp->zfs_type))
239                 return (B_FALSE);
240
241         verify(zfs_prop_get(zhp, ZFS_PROP_MOUNTPOINT, buf, buflen,
242             &sourcetype, sourceloc, sizeof (sourceloc), B_FALSE) == 0);
243
244         if (strcmp(buf, ZFS_MOUNTPOINT_NONE) == 0 ||
245             strcmp(buf, ZFS_MOUNTPOINT_LEGACY) == 0)
246                 return (B_FALSE);
247
248         if (zfs_prop_get_int(zhp, ZFS_PROP_CANMOUNT) == ZFS_CANMOUNT_OFF)
249                 return (B_FALSE);
250
251         if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED) &&
252             getzoneid() == GLOBAL_ZONEID)
253                 return (B_FALSE);
254
255         if (source)
256                 *source = sourcetype;
257
258         return (B_TRUE);
259 }
260
261 /*
262  * Mount the given filesystem.
263  */
264 int
265 zfs_mount(zfs_handle_t *zhp, const char *options, int flags)
266 {
267         struct stat buf;
268         char mountpoint[ZFS_MAXPROPLEN];
269         char mntopts[MNT_LINE_MAX];
270         libzfs_handle_t *hdl = zhp->zfs_hdl;
271
272         if (options == NULL)
273                 mntopts[0] = '\0';
274         else
275                 (void) strlcpy(mntopts, options, sizeof (mntopts));
276
277         /*
278          * If the pool is imported read-only then all mounts must be read-only
279          */
280         if (zpool_get_prop_int(zhp->zpool_hdl, ZPOOL_PROP_READONLY, NULL))
281                 flags |= MS_RDONLY;
282
283 #ifdef HAVE_LIBSELINUX
284         if (is_selinux_enabled())
285                 (void) strlcat(mntopts, ",context=\"system_u:"
286                     "object_r:file_t:s0\"", sizeof (mntopts));
287 #endif /* HAVE_LIBSELINUX */
288
289         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
290                 return (0);
291
292         /* Create the directory if it doesn't already exist */
293         if (lstat(mountpoint, &buf) != 0) {
294                 if (mkdirp(mountpoint, 0755) != 0) {
295                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
296                             "failed to create mountpoint"));
297                         return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
298                             dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
299                             mountpoint));
300                 }
301         }
302
303         /*
304          * Determine if the mountpoint is empty.  If so, refuse to perform the
305          * mount.  We don't perform this check if MS_OVERLAY is specified, which
306          * would defeat the point.  We also avoid this check if 'remount' is
307          * specified.
308          */
309         if ((flags & MS_OVERLAY) == 0 &&
310             strstr(mntopts, MNTOPT_REMOUNT) == NULL &&
311             !dir_is_empty(mountpoint)) {
312                 zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
313                     "directory is not empty"));
314                 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
315                     dgettext(TEXT_DOMAIN, "cannot mount '%s'"), mountpoint));
316         }
317
318         /* perform the mount */
319         if (mount(zfs_get_name(zhp), mountpoint, MS_OPTIONSTR | flags,
320             MNTTYPE_ZFS, NULL, 0, mntopts, sizeof (mntopts)) != 0) {
321                 /*
322                  * Generic errors are nasty, but there are just way too many
323                  * from mount(), and they're well-understood.  We pick a few
324                  * common ones to improve upon.
325                  */
326                 if (errno == EBUSY) {
327                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
328                             "mountpoint or dataset is busy"));
329                 } else if (errno == EPERM) {
330                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN,
331                             "Insufficient privileges"));
332                 } else if (errno == ENOTSUP) {
333                         char buf[256];
334                         int spa_version;
335
336                         VERIFY(zfs_spa_version(zhp, &spa_version) == 0);
337                         (void) snprintf(buf, sizeof (buf),
338                             dgettext(TEXT_DOMAIN, "Can't mount a version %lld "
339                             "file system on a version %d pool. Pool must be"
340                             " upgraded to mount this file system."),
341                             (u_longlong_t)zfs_prop_get_int(zhp,
342                             ZFS_PROP_VERSION), spa_version);
343                         zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, buf));
344                 } else {
345                         zfs_error_aux(hdl, strerror(errno));
346                 }
347                 return (zfs_error_fmt(hdl, EZFS_MOUNTFAILED,
348                     dgettext(TEXT_DOMAIN, "cannot mount '%s'"),
349                     zhp->zfs_name));
350         }
351
352         /* add the mounted entry into our cache */
353         libzfs_mnttab_add(hdl, zfs_get_name(zhp), mountpoint,
354             mntopts);
355         return (0);
356 }
357
358 /*
359  * Unmount a single filesystem.
360  */
361 static int
362 unmount_one(libzfs_handle_t *hdl, const char *mountpoint, int flags)
363 {
364         if (umount2(mountpoint, flags) != 0) {
365                 zfs_error_aux(hdl, strerror(errno));
366                 return (zfs_error_fmt(hdl, EZFS_UMOUNTFAILED,
367                     dgettext(TEXT_DOMAIN, "cannot unmount '%s'"),
368                     mountpoint));
369         }
370
371         return (0);
372 }
373
374 /*
375  * Unmount the given filesystem.
376  */
377 int
378 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
379 {
380         libzfs_handle_t *hdl = zhp->zfs_hdl;
381         struct mnttab entry;
382         char *mntpt = NULL;
383
384         /* check to see if we need to unmount the filesystem */
385         if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
386             libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0)) {
387                 /*
388                  * mountpoint may have come from a call to
389                  * getmnt/getmntany if it isn't NULL. If it is NULL,
390                  * we know it comes from libzfs_mnttab_find which can
391                  * then get freed later. We strdup it to play it safe.
392                  */
393                 if (mountpoint == NULL)
394                         mntpt = zfs_strdup(hdl, entry.mnt_mountp);
395                 else
396                         mntpt = zfs_strdup(hdl, mountpoint);
397
398                 /*
399                  * Unshare and unmount the filesystem
400                  */
401                 if (zfs_unshare_proto(zhp, mntpt, share_all_proto) != 0)
402                         return (-1);
403
404                 if (unmount_one(hdl, mntpt, flags) != 0) {
405                         free(mntpt);
406                         (void) zfs_shareall(zhp);
407                         return (-1);
408                 }
409                 libzfs_mnttab_remove(hdl, zhp->zfs_name);
410                 free(mntpt);
411         }
412
413         return (0);
414 }
415
416 /*
417  * Unmount this filesystem and any children inheriting the mountpoint property.
418  * To do this, just act like we're changing the mountpoint property, but don't
419  * remount the filesystems afterwards.
420  */
421 int
422 zfs_unmountall(zfs_handle_t *zhp, int flags)
423 {
424         prop_changelist_t *clp;
425         int ret;
426
427         clp = changelist_gather(zhp, ZFS_PROP_MOUNTPOINT, 0, flags);
428         if (clp == NULL)
429                 return (-1);
430
431         ret = changelist_prefix(clp);
432         changelist_free(clp);
433
434         return (ret);
435 }
436
437 boolean_t
438 zfs_is_shared(zfs_handle_t *zhp)
439 {
440         zfs_share_type_t rc = 0;
441         zfs_share_proto_t *curr_proto;
442
443         if (ZFS_IS_VOLUME(zhp))
444                 return (B_FALSE);
445
446         for (curr_proto = share_all_proto; *curr_proto != PROTO_END;
447             curr_proto++)
448                 rc |= zfs_is_shared_proto(zhp, NULL, *curr_proto);
449
450         return (rc ? B_TRUE : B_FALSE);
451 }
452
453 int
454 zfs_share(zfs_handle_t *zhp)
455 {
456         assert(!ZFS_IS_VOLUME(zhp));
457         return (zfs_share_proto(zhp, share_all_proto));
458 }
459
460 int
461 zfs_unshare(zfs_handle_t *zhp)
462 {
463         assert(!ZFS_IS_VOLUME(zhp));
464         return (zfs_unshareall(zhp));
465 }
466
467 /*
468  * Check to see if the filesystem is currently shared.
469  */
470 zfs_share_type_t
471 zfs_is_shared_proto(zfs_handle_t *zhp, char **where, zfs_share_proto_t proto)
472 {
473         char *mountpoint;
474         zfs_share_type_t rc;
475
476         if (!zfs_is_mounted(zhp, &mountpoint))
477                 return (SHARED_NOT_SHARED);
478
479         if ((rc = is_shared(zhp->zfs_hdl, mountpoint, proto))) {
480                 if (where != NULL)
481                         *where = mountpoint;
482                 else
483                         free(mountpoint);
484                 return (rc);
485         } else {
486                 free(mountpoint);
487                 return (SHARED_NOT_SHARED);
488         }
489 }
490
491 boolean_t
492 zfs_is_shared_nfs(zfs_handle_t *zhp, char **where)
493 {
494         return (zfs_is_shared_proto(zhp, where,
495             PROTO_NFS) != SHARED_NOT_SHARED);
496 }
497
498 boolean_t
499 zfs_is_shared_smb(zfs_handle_t *zhp, char **where)
500 {
501         return (zfs_is_shared_proto(zhp, where,
502             PROTO_SMB) != SHARED_NOT_SHARED);
503 }
504
505 /*
506  * Make sure things will work if libshare isn't installed by using
507  * wrapper functions that check to see that the pointers to functions
508  * initialized in _zfs_init_libshare() are actually present.
509  */
510
511 static sa_handle_t (*_sa_init)(int);
512 static void (*_sa_fini)(sa_handle_t);
513 static sa_share_t (*_sa_find_share)(sa_handle_t, char *);
514 static int (*_sa_enable_share)(sa_share_t, char *);
515 static int (*_sa_disable_share)(sa_share_t, char *);
516 static char *(*_sa_errorstr)(int);
517 static int (*_sa_parse_legacy_options)(sa_group_t, char *, char *);
518 static boolean_t (*_sa_needs_refresh)(sa_handle_t *);
519 static libzfs_handle_t *(*_sa_get_zfs_handle)(sa_handle_t);
520 static int (*_sa_zfs_process_share)(sa_handle_t, sa_group_t, sa_share_t,
521     char *, char *, zprop_source_t, char *, char *, char *);
522 static void (*_sa_update_sharetab_ts)(sa_handle_t);
523
524 /*
525  * _zfs_init_libshare()
526  *
527  * Find the libshare.so.1 entry points that we use here and save the
528  * values to be used later. This is triggered by the runtime loader.
529  * Make sure the correct ISA version is loaded.
530  */
531 #ifdef __GNUC__
532 static void
533 _zfs_init_libshare(void) __attribute__((constructor));
534 #else
535 #pragma init(_zfs_init_libshare)
536 #endif
537 static void
538 _zfs_init_libshare(void)
539 {
540         void *libshare;
541         char path[MAXPATHLEN];
542         char isa[MAXISALEN];
543
544 #if defined(_LP64)
545         if (sysinfo(SI_ARCHITECTURE_64, isa, MAXISALEN) == -1)
546                 isa[0] = '\0';
547 #else
548         isa[0] = '\0';
549 #endif
550         (void) snprintf(path, MAXPATHLEN,
551             "/usr/lib/%s/libshare.so.1", isa);
552
553         if ((libshare = dlopen(path, RTLD_LAZY | RTLD_GLOBAL)) != NULL) {
554                 _sa_init = (sa_handle_t (*)(int))dlsym(libshare, "sa_init");
555                 _sa_fini = (void (*)(sa_handle_t))dlsym(libshare, "sa_fini");
556                 _sa_find_share = (sa_share_t (*)(sa_handle_t, char *))
557                     dlsym(libshare, "sa_find_share");
558                 _sa_enable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
559                     "sa_enable_share");
560                 _sa_disable_share = (int (*)(sa_share_t, char *))dlsym(libshare,
561                     "sa_disable_share");
562                 _sa_errorstr = (char *(*)(int))dlsym(libshare, "sa_errorstr");
563                 _sa_parse_legacy_options = (int (*)(sa_group_t, char *, char *))
564                     dlsym(libshare, "sa_parse_legacy_options");
565                 _sa_needs_refresh = (boolean_t (*)(sa_handle_t *))
566                     dlsym(libshare, "sa_needs_refresh");
567                 _sa_get_zfs_handle = (libzfs_handle_t *(*)(sa_handle_t))
568                     dlsym(libshare, "sa_get_zfs_handle");
569                 _sa_zfs_process_share = (int (*)(sa_handle_t, sa_group_t,
570                     sa_share_t, char *, char *, zprop_source_t, char *,
571                     char *, char *))dlsym(libshare, "sa_zfs_process_share");
572                 _sa_update_sharetab_ts = (void (*)(sa_handle_t))
573                     dlsym(libshare, "sa_update_sharetab_ts");
574                 if (_sa_init == NULL || _sa_fini == NULL ||
575                     _sa_find_share == NULL || _sa_enable_share == NULL ||
576                     _sa_disable_share == NULL || _sa_errorstr == NULL ||
577                     _sa_parse_legacy_options == NULL ||
578                     _sa_needs_refresh == NULL || _sa_get_zfs_handle == NULL ||
579                     _sa_zfs_process_share == NULL ||
580                     _sa_update_sharetab_ts == NULL) {
581                         _sa_init = NULL;
582                         _sa_fini = NULL;
583                         _sa_disable_share = NULL;
584                         _sa_enable_share = NULL;
585                         _sa_errorstr = NULL;
586                         _sa_parse_legacy_options = NULL;
587                         (void) dlclose(libshare);
588                         _sa_needs_refresh = NULL;
589                         _sa_get_zfs_handle = NULL;
590                         _sa_zfs_process_share = NULL;
591                         _sa_update_sharetab_ts = NULL;
592                 }
593         }
594 }
595
596 /*
597  * zfs_init_libshare(zhandle, service)
598  *
599  * Initialize the libshare API if it hasn't already been initialized.
600  * In all cases it returns 0 if it succeeded and an error if not. The
601  * service value is which part(s) of the API to initialize and is a
602  * direct map to the libshare sa_init(service) interface.
603  */
604 int
605 zfs_init_libshare(libzfs_handle_t *zhandle, int service)
606 {
607         int ret = SA_OK;
608
609         if (_sa_init == NULL)
610                 ret = SA_CONFIG_ERR;
611
612         if (ret == SA_OK && zhandle->libzfs_shareflags & ZFSSHARE_MISS) {
613                 /*
614                  * We had a cache miss. Most likely it is a new ZFS
615                  * dataset that was just created. We want to make sure
616                  * so check timestamps to see if a different process
617                  * has updated any of the configuration. If there was
618                  * some non-ZFS change, we need to re-initialize the
619                  * internal cache.
620                  */
621                 zhandle->libzfs_shareflags &= ~ZFSSHARE_MISS;
622                 if (_sa_needs_refresh != NULL &&
623                     _sa_needs_refresh(zhandle->libzfs_sharehdl)) {
624                         zfs_uninit_libshare(zhandle);
625                         zhandle->libzfs_sharehdl = _sa_init(service);
626                 }
627         }
628
629         if (ret == SA_OK && zhandle && zhandle->libzfs_sharehdl == NULL)
630                 zhandle->libzfs_sharehdl = _sa_init(service);
631
632         if (ret == SA_OK && zhandle->libzfs_sharehdl == NULL)
633                 ret = SA_NO_MEMORY;
634
635         return (ret);
636 }
637
638 /*
639  * zfs_uninit_libshare(zhandle)
640  *
641  * Uninitialize the libshare API if it hasn't already been
642  * uninitialized. It is OK to call multiple times.
643  */
644 void
645 zfs_uninit_libshare(libzfs_handle_t *zhandle)
646 {
647         if (zhandle != NULL && zhandle->libzfs_sharehdl != NULL) {
648                 if (_sa_fini != NULL)
649                         _sa_fini(zhandle->libzfs_sharehdl);
650                 zhandle->libzfs_sharehdl = NULL;
651         }
652 }
653
654 /*
655  * zfs_parse_options(options, proto)
656  *
657  * Call the legacy parse interface to get the protocol specific
658  * options using the NULL arg to indicate that this is a "parse" only.
659  */
660 int
661 zfs_parse_options(char *options, zfs_share_proto_t proto)
662 {
663         if (_sa_parse_legacy_options != NULL) {
664                 return (_sa_parse_legacy_options(NULL, options,
665                     proto_table[proto].p_name));
666         }
667         return (SA_CONFIG_ERR);
668 }
669
670 /*
671  * zfs_sa_find_share(handle, path)
672  *
673  * wrapper around sa_find_share to find a share path in the
674  * configuration.
675  */
676 static sa_share_t
677 zfs_sa_find_share(sa_handle_t handle, char *path)
678 {
679         if (_sa_find_share != NULL)
680                 return (_sa_find_share(handle, path));
681         return (NULL);
682 }
683
684 /*
685  * zfs_sa_enable_share(share, proto)
686  *
687  * Wrapper for sa_enable_share which enables a share for a specified
688  * protocol.
689  */
690 static int
691 zfs_sa_enable_share(sa_share_t share, char *proto)
692 {
693         if (_sa_enable_share != NULL)
694                 return (_sa_enable_share(share, proto));
695         return (SA_CONFIG_ERR);
696 }
697
698 /*
699  * zfs_sa_disable_share(share, proto)
700  *
701  * Wrapper for sa_enable_share which disables a share for a specified
702  * protocol.
703  */
704 static int
705 zfs_sa_disable_share(sa_share_t share, char *proto)
706 {
707         if (_sa_disable_share != NULL)
708                 return (_sa_disable_share(share, proto));
709         return (SA_CONFIG_ERR);
710 }
711
712 /*
713  * Share the given filesystem according to the options in the specified
714  * protocol specific properties (sharenfs, sharesmb).  We rely
715  * on "libshare" to the dirty work for us.
716  */
717 static int
718 zfs_share_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
719 {
720         char mountpoint[ZFS_MAXPROPLEN];
721         char shareopts[ZFS_MAXPROPLEN];
722         char sourcestr[ZFS_MAXPROPLEN];
723         libzfs_handle_t *hdl = zhp->zfs_hdl;
724         sa_share_t share;
725         zfs_share_proto_t *curr_proto;
726         zprop_source_t sourcetype;
727         int ret;
728
729         if (!zfs_is_mountable(zhp, mountpoint, sizeof (mountpoint), NULL))
730                 return (0);
731
732         if ((ret = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
733                 (void) zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
734                     dgettext(TEXT_DOMAIN, "cannot share '%s': %s"),
735                     zfs_get_name(zhp), _sa_errorstr != NULL ?
736                     _sa_errorstr(ret) : "");
737                 return (-1);
738         }
739
740         for (curr_proto = proto; *curr_proto != PROTO_END; curr_proto++) {
741                 /*
742                  * Return success if there are no share options.
743                  */
744                 if (zfs_prop_get(zhp, proto_table[*curr_proto].p_prop,
745                     shareopts, sizeof (shareopts), &sourcetype, sourcestr,
746                     ZFS_MAXPROPLEN, B_FALSE) != 0 ||
747                     strcmp(shareopts, "off") == 0)
748                         continue;
749
750                 /*
751                  * If the 'zoned' property is set, then zfs_is_mountable()
752                  * will have already bailed out if we are in the global zone.
753                  * But local zones cannot be NFS servers, so we ignore it for
754                  * local zones as well.
755                  */
756                 if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED))
757                         continue;
758
759                 share = zfs_sa_find_share(hdl->libzfs_sharehdl, mountpoint);
760                 if (share == NULL) {
761                         /*
762                          * This may be a new file system that was just
763                          * created so isn't in the internal cache
764                          * (second time through). Rather than
765                          * reloading the entire configuration, we can
766                          * assume ZFS has done the checking and it is
767                          * safe to add this to the internal
768                          * configuration.
769                          */
770                         if (_sa_zfs_process_share(hdl->libzfs_sharehdl,
771                             NULL, NULL, mountpoint,
772                             proto_table[*curr_proto].p_name, sourcetype,
773                             shareopts, sourcestr, zhp->zfs_name) != SA_OK) {
774                                 (void) zfs_error_fmt(hdl,
775                                     proto_table[*curr_proto].p_share_err,
776                                     dgettext(TEXT_DOMAIN, "cannot share '%s'"),
777                                     zfs_get_name(zhp));
778                                 return (-1);
779                         }
780                         hdl->libzfs_shareflags |= ZFSSHARE_MISS;
781                         share = zfs_sa_find_share(hdl->libzfs_sharehdl,
782                             mountpoint);
783                 }
784                 if (share != NULL) {
785                         int err;
786                         err = zfs_sa_enable_share(share,
787                             proto_table[*curr_proto].p_name);
788                         if (err != SA_OK) {
789                                 (void) zfs_error_fmt(hdl,
790                                     proto_table[*curr_proto].p_share_err,
791                                     dgettext(TEXT_DOMAIN, "cannot share '%s'"),
792                                     zfs_get_name(zhp));
793                                 return (-1);
794                         }
795                 } else {
796                         (void) zfs_error_fmt(hdl,
797                             proto_table[*curr_proto].p_share_err,
798                             dgettext(TEXT_DOMAIN, "cannot share '%s'"),
799                             zfs_get_name(zhp));
800                         return (-1);
801                 }
802
803         }
804         return (0);
805 }
806
807
808 int
809 zfs_share_nfs(zfs_handle_t *zhp)
810 {
811         return (zfs_share_proto(zhp, nfs_only));
812 }
813
814 int
815 zfs_share_smb(zfs_handle_t *zhp)
816 {
817         return (zfs_share_proto(zhp, smb_only));
818 }
819
820 int
821 zfs_shareall(zfs_handle_t *zhp)
822 {
823         return (zfs_share_proto(zhp, share_all_proto));
824 }
825
826 /*
827  * Unshare a filesystem by mountpoint.
828  */
829 static int
830 unshare_one(libzfs_handle_t *hdl, const char *name, const char *mountpoint,
831     zfs_share_proto_t proto)
832 {
833         sa_share_t share;
834         int err;
835         char *mntpt;
836         /*
837          * Mountpoint could get trashed if libshare calls getmntany
838          * which it does during API initialization, so strdup the
839          * value.
840          */
841         mntpt = zfs_strdup(hdl, mountpoint);
842
843         /* make sure libshare initialized */
844         if ((err = zfs_init_libshare(hdl, SA_INIT_SHARE_API)) != SA_OK) {
845                 free(mntpt);    /* don't need the copy anymore */
846                 return (zfs_error_fmt(hdl, EZFS_SHARENFSFAILED,
847                     dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
848                     name, _sa_errorstr(err)));
849         }
850
851         share = zfs_sa_find_share(hdl->libzfs_sharehdl, mntpt);
852         free(mntpt);    /* don't need the copy anymore */
853
854         if (share != NULL) {
855                 err = zfs_sa_disable_share(share, proto_table[proto].p_name);
856                 if (err != SA_OK) {
857                         return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
858                             dgettext(TEXT_DOMAIN, "cannot unshare '%s': %s"),
859                             name, _sa_errorstr(err)));
860                 }
861         } else {
862                 return (zfs_error_fmt(hdl, EZFS_UNSHARENFSFAILED,
863                     dgettext(TEXT_DOMAIN, "cannot unshare '%s': not found"),
864                     name));
865         }
866         return (0);
867 }
868
869 /*
870  * Unshare the given filesystem.
871  */
872 int
873 zfs_unshare_proto(zfs_handle_t *zhp, const char *mountpoint,
874     zfs_share_proto_t *proto)
875 {
876         libzfs_handle_t *hdl = zhp->zfs_hdl;
877         struct mnttab entry;
878         char *mntpt = NULL;
879
880         /* check to see if need to unmount the filesystem */
881         rewind(zhp->zfs_hdl->libzfs_mnttab);
882         if (mountpoint != NULL)
883                 mountpoint = mntpt = zfs_strdup(hdl, mountpoint);
884
885         if (mountpoint != NULL || ((zfs_get_type(zhp) == ZFS_TYPE_FILESYSTEM) &&
886             libzfs_mnttab_find(hdl, zfs_get_name(zhp), &entry) == 0)) {
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;
893                     curr_proto++) {
894
895                         if (is_shared(hdl, mntpt, *curr_proto) &&
896                             unshare_one(hdl, zhp->zfs_name,
897                             mntpt, *curr_proto) != 0) {
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
910 int
911 zfs_unshare_nfs(zfs_handle_t *zhp, const char *mountpoint)
912 {
913         return (zfs_unshare_proto(zhp, mountpoint, nfs_only));
914 }
915
916 int
917 zfs_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  */
925 int
926 zfs_unshareall_proto(zfs_handle_t *zhp, zfs_share_proto_t *proto)
927 {
928         prop_changelist_t *clp;
929         int ret;
930
931         clp = changelist_gather(zhp, ZFS_PROP_SHARENFS, 0, 0);
932         if (clp == NULL)
933                 return (-1);
934
935         ret = changelist_unshare(clp, proto);
936         changelist_free(clp);
937
938         return (ret);
939 }
940
941 int
942 zfs_unshareall_nfs(zfs_handle_t *zhp)
943 {
944         return (zfs_unshareall_proto(zhp, nfs_only));
945 }
946
947 int
948 zfs_unshareall_smb(zfs_handle_t *zhp)
949 {
950         return (zfs_unshareall_proto(zhp, smb_only));
951 }
952
953 int
954 zfs_unshareall(zfs_handle_t *zhp)
955 {
956         return (zfs_unshareall_proto(zhp, share_all_proto));
957 }
958
959 int
960 zfs_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  */
976 void
977 remove_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
998 void
999 libzfs_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 }
1014
1015 static int
1016 mount_cb(zfs_handle_t *zhp, void *data)
1017 {
1018         get_all_cb_t *cbp = data;
1019
1020         if (!(zfs_get_type(zhp) & ZFS_TYPE_FILESYSTEM)) {
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
1030         libzfs_add_handle(cbp, zhp);
1031         if (zfs_iter_filesystems(zhp, mount_cb, cbp) != 0) {
1032                 zfs_close(zhp);
1033                 return (-1);
1034         }
1035         return (0);
1036 }
1037
1038 int
1039 libzfs_dataset_cmp(const void *a, const void *b)
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
1074 int
1075 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1076 {
1077         get_all_cb_t cb = { 0 };
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          */
1086         if ((zfsp = zfs_open(hdl, zhp->zpool_name, ZFS_TYPE_DATASET)) == NULL)
1087                 goto out;
1088
1089         libzfs_add_handle(&cb, zfsp);
1090         if (zfs_iter_filesystems(zfsp, mount_cb, &cb) != 0)
1091                 goto out;
1092         /*
1093          * Sort the datasets by mountpoint.
1094          */
1095         qsort(cb.cb_handles, cb.cb_used, sizeof (void *),
1096             libzfs_dataset_cmp);
1097
1098         /*
1099          * And mount all the datasets, keeping track of which ones
1100          * succeeded or failed.
1101          */
1102         if ((good = zfs_alloc(zhp->zpool_hdl,
1103             cb.cb_used * sizeof (int))) == NULL)
1104                 goto out;
1105
1106         ret = 0;
1107         for (i = 0; i < cb.cb_used; i++) {
1108                 if (zfs_mount(cb.cb_handles[i], mntopts, flags) != 0)
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++) {
1121                 if (good[i] && zfs_share(cb.cb_handles[i]) != 0)
1122                         ret = -1;
1123         }
1124
1125         free(good);
1126
1127 out:
1128         for (i = 0; i < cb.cb_used; i++)
1129                 zfs_close(cb.cb_handles[i]);
1130         free(cb.cb_handles);
1131
1132         return (ret);
1133 }
1134
1135 static int
1136 mountpoint_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
1144 /* alias for 2002/240 */
1145 #pragma weak zpool_unmount_datasets = zpool_disable_datasets
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
1150  * arbitrarily (on I/O error, for example).  Instead, we walk /etc/mnttab and
1151  * gather all the filesystems that are currently mounted.
1152  */
1153 int
1154 zpool_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
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;
1270 out:
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 }
1281
1282 #else  /* HAVE_ZPL */
1283
1284 int
1285 zfs_unshare_iscsi(zfs_handle_t *zhp)
1286 {
1287         return 0;
1288 }
1289
1290 int
1291 zfs_unmount(zfs_handle_t *zhp, const char *mountpoint, int flags)
1292 {
1293         return 0;
1294 }
1295
1296 void
1297 remove_mountpoint(zfs_handle_t *zhp) {
1298         return;
1299 }
1300
1301 boolean_t
1302 is_mounted(libzfs_handle_t *zfs_hdl, const char *special, char **where)
1303 {
1304         return B_FALSE;
1305 }
1306
1307 boolean_t
1308 zfs_is_mounted(zfs_handle_t *zhp, char **where)
1309 {
1310         return is_mounted(zhp->zfs_hdl, zfs_get_name(zhp), where);
1311 }
1312
1313 boolean_t
1314 zfs_is_shared(zfs_handle_t *zhp)
1315 {
1316         return B_FALSE;
1317 }
1318
1319 int
1320 zpool_enable_datasets(zpool_handle_t *zhp, const char *mntopts, int flags)
1321 {
1322         return B_FALSE;
1323 }
1324
1325 int
1326 zpool_disable_datasets(zpool_handle_t *zhp, boolean_t force)
1327 {
1328         return B_FALSE;
1329 }
1330 #endif /* HAVE_ZPL */