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