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