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