Print this page
NEX-19178 Changing the NFS export path makes the SMB share offline
Reviewed by: Evan Layton <evan.layton@nexenta.com>
Reviewed by: Yuri Pankov <yuri.pankov@nexenta.com>
Reviewed by: Matt Barden <matt.barden@nexenta.com>
NEX-15279 support NFS server in zone
NEX-15520 online NFS shares cause zoneadm halt to hang in nfs_export_zone_fini
Portions contributed by: Dan Kruchinin dan.kruchinin@nexenta.com
Portions contributed by: Stepan Zastupov stepan.zastupov@gmail.com
Reviewed by: Joyce McIntosh <joyce.mcintosh@nexenta.com>
Reviewed by: Rob Gittins <rob.gittins@nexenta.com>
Reviewed by: Gordon Ross <gordon.ross@nexenta.com>
| Split |
Close |
| Expand all |
| Collapse all |
--- old/usr/src/lib/libshare/common/libshare_zfs.c
+++ new/usr/src/lib/libshare/common/libshare_zfs.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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
24 24 */
25 +
25 26 /*
26 - * Copyright 2012 Nexenta Systems, Inc. All rights reserved.
27 27 * Copyright (c) 2012, 2016 by Delphix. All rights reserved.
28 28 * Copyright 2017 RackTop Systems.
29 + * Copyright 2019 Nexenta Systems, Inc.
29 30 */
30 31
31 32 #include <stdio.h>
32 33 #include <libzfs.h>
33 34 #include <string.h>
34 35 #include <strings.h>
35 36 #include <errno.h>
37 +#include <zone.h>
36 38 #include <libshare.h>
37 39 #include "libshare_impl.h"
38 40 #include <libintl.h>
39 41 #include <sys/mnttab.h>
40 42 #include <sys/mntent.h>
41 43 #include <assert.h>
42 44
43 45 extern sa_share_t _sa_add_share(sa_group_t, char *, int, int *, uint64_t);
44 46 extern sa_group_t _sa_create_zfs_group(sa_group_t, char *);
45 47 extern char *sa_fstype(char *);
46 48 extern void set_node_attr(void *, char *, char *);
47 49 extern int sa_is_share(void *);
48 50 extern void sa_update_sharetab_ts(sa_handle_t);
49 51
50 52 /*
51 53 * File system specific code for ZFS. The original code was stolen
52 54 * from the "zfs" command and modified to better suit this library's
53 55 * usage.
54 56 */
55 57
56 58 typedef struct get_all_cbdata {
57 59 zfs_handle_t **cb_handles;
58 60 size_t cb_alloc;
59 61 size_t cb_used;
60 62 uint_t cb_types;
61 63 } get_all_cbdata_t;
62 64
63 65 /*
64 66 * sa_zfs_init(impl_handle)
65 67 *
66 68 * Initialize an access handle into libzfs. The handle needs to stay
67 69 * around until sa_zfs_fini() in order to maintain the cache of
68 70 * mounts.
69 71 */
70 72
71 73 int
72 74 sa_zfs_init(sa_handle_impl_t impl_handle)
73 75 {
74 76 impl_handle->zfs_libhandle = libzfs_init();
75 77 if (impl_handle->zfs_libhandle != NULL) {
76 78 libzfs_print_on_error(impl_handle->zfs_libhandle, B_TRUE);
77 79 return (B_TRUE);
78 80 }
79 81 return (B_FALSE);
80 82 }
81 83
82 84 /*
83 85 * sa_zfs_fini(impl_handle)
84 86 *
85 87 * cleanup data structures and the libzfs handle used for accessing
86 88 * zfs file share info.
87 89 */
88 90
89 91 void
90 92 sa_zfs_fini(sa_handle_impl_t impl_handle)
91 93 {
92 94 if (impl_handle->zfs_libhandle != NULL) {
93 95 if (impl_handle->zfs_list != NULL) {
94 96 zfs_handle_t **zhp = impl_handle->zfs_list;
95 97 size_t i;
96 98
97 99 /*
98 100 * Contents of zfs_list need to be freed so we
99 101 * don't lose ZFS handles.
100 102 */
101 103 for (i = 0; i < impl_handle->zfs_list_count; i++) {
102 104 zfs_close(zhp[i]);
103 105 }
104 106 free(impl_handle->zfs_list);
105 107 impl_handle->zfs_list = NULL;
106 108 impl_handle->zfs_list_count = 0;
107 109 }
108 110
109 111 libzfs_fini(impl_handle->zfs_libhandle);
110 112 impl_handle->zfs_libhandle = NULL;
111 113 }
112 114 }
113 115
114 116 /*
115 117 * get_one_filesystem(zfs_handle_t, data)
116 118 *
117 119 * an iterator function called while iterating through the ZFS
118 120 * root. It accumulates into an array of file system handles that can
119 121 * be used to derive info about those file systems.
120 122 *
121 123 * Note that as this function is called, we close all zhp handles that
122 124 * are not going to be places into the cp_handles list. We don't want
123 125 * to close the ones we are keeping, but all others would be leaked if
124 126 * not closed here.
125 127 */
126 128
127 129 static int
128 130 get_one_filesystem(zfs_handle_t *zhp, void *data)
129 131 {
130 132 get_all_cbdata_t *cbp = data;
131 133 zfs_type_t type = zfs_get_type(zhp);
132 134
133 135 /*
134 136 * Interate over any nested datasets.
135 137 */
136 138 if (type == ZFS_TYPE_FILESYSTEM &&
137 139 zfs_iter_filesystems(zhp, get_one_filesystem, data) != 0) {
138 140 zfs_close(zhp);
139 141 return (1);
140 142 }
141 143
142 144 /*
143 145 * Skip any datasets whose type does not match.
144 146 */
145 147 if ((type & cbp->cb_types) == 0) {
146 148 zfs_close(zhp);
147 149 return (0);
148 150 }
149 151
150 152 if (cbp->cb_alloc == cbp->cb_used) {
151 153 zfs_handle_t **handles;
152 154
153 155 if (cbp->cb_alloc == 0)
154 156 cbp->cb_alloc = 64;
155 157 else
156 158 cbp->cb_alloc *= 2;
157 159
158 160 handles = (zfs_handle_t **)calloc(1,
159 161 cbp->cb_alloc * sizeof (void *));
160 162
161 163 if (handles == NULL) {
162 164 zfs_close(zhp);
163 165 return (0);
164 166 }
165 167 if (cbp->cb_handles) {
166 168 bcopy(cbp->cb_handles, handles,
167 169 cbp->cb_used * sizeof (void *));
168 170 free(cbp->cb_handles);
169 171 }
170 172
171 173 cbp->cb_handles = handles;
172 174 }
173 175
174 176 cbp->cb_handles[cbp->cb_used++] = zhp;
175 177
176 178 return (0);
177 179 }
178 180
179 181 /*
180 182 * get_all_filesystems(zfs_handle_t ***fslist, size_t *count)
181 183 *
182 184 * iterate through all ZFS file systems starting at the root. Returns
183 185 * a count and an array of handle pointers. Allocating is only done
184 186 * once. The caller does not need to free since it will be done at
185 187 * sa_zfs_fini() time.
186 188 */
187 189
188 190 static void
189 191 get_all_filesystems(sa_handle_impl_t impl_handle,
190 192 zfs_handle_t ***fslist, size_t *count)
191 193 {
192 194 get_all_cbdata_t cb = { 0 };
193 195 cb.cb_types = ZFS_TYPE_FILESYSTEM;
194 196
195 197 if (impl_handle->zfs_list != NULL) {
196 198 *fslist = impl_handle->zfs_list;
197 199 *count = impl_handle->zfs_list_count;
198 200 return;
199 201 }
200 202
201 203 (void) zfs_iter_root(impl_handle->zfs_libhandle,
202 204 get_one_filesystem, &cb);
203 205
204 206 impl_handle->zfs_list = *fslist = cb.cb_handles;
205 207 impl_handle->zfs_list_count = *count = cb.cb_used;
206 208 }
207 209
208 210 /*
209 211 * mountpoint_compare(a, b)
210 212 *
211 213 * compares the mountpoint on two zfs file systems handles.
212 214 * returns values following strcmp() model.
213 215 */
214 216
215 217 static int
216 218 mountpoint_compare(const void *a, const void *b)
217 219 {
218 220 zfs_handle_t **za = (zfs_handle_t **)a;
219 221 zfs_handle_t **zb = (zfs_handle_t **)b;
220 222 char mounta[MAXPATHLEN];
221 223 char mountb[MAXPATHLEN];
222 224
223 225 verify(zfs_prop_get(*za, ZFS_PROP_MOUNTPOINT, mounta,
224 226 sizeof (mounta), NULL, NULL, 0, B_FALSE) == 0);
225 227 verify(zfs_prop_get(*zb, ZFS_PROP_MOUNTPOINT, mountb,
226 228 sizeof (mountb), NULL, NULL, 0, B_FALSE) == 0);
227 229
228 230 return (strcmp(mounta, mountb));
229 231 }
230 232
|
↓ open down ↓ |
185 lines elided |
↑ open up ↑ |
231 233 /*
232 234 * return legacy mountpoint. Caller provides space for mountpoint and
233 235 * dataset.
234 236 */
235 237 int
236 238 get_legacy_mountpoint(const char *path, char *dataset, size_t dlen,
237 239 char *mountpoint, size_t mlen)
238 240 {
239 241 FILE *fp;
240 242 struct mnttab entry;
243 + int rc = 1;
241 244
242 245 if ((fp = fopen(MNTTAB, "r")) == NULL) {
243 246 return (1);
244 247 }
245 248
246 249 while (getmntent(fp, &entry) == 0) {
247 250
248 251 if (entry.mnt_fstype == NULL ||
249 252 strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0)
250 253 continue;
251 254
252 255 if (strcmp(entry.mnt_mountp, path) == 0) {
253 256 if (mlen > 0)
254 257 (void) strlcpy(mountpoint, entry.mnt_mountp,
255 258 mlen);
256 259 if (dlen > 0)
257 260 (void) strlcpy(dataset, entry.mnt_special,
258 261 dlen);
262 + rc = 0;
259 263 break;
260 264 }
261 265 }
262 266 (void) fclose(fp);
263 - return (1);
267 + return (rc);
264 268 }
265 269
266 270
267 271 /*
268 272 * Verifies that a specific zfs filesystem handle meets the criteria necessary
269 273 * to be used by libshare operations. See get_zfs_dataset.
270 274 */
271 275 static char *
272 276 verify_zfs_handle(zfs_handle_t *hdl, const char *path, boolean_t search_mnttab)
273 277 {
274 278 char mountpoint[ZFS_MAXPROPLEN];
275 279 char canmount[ZFS_MAXPROPLEN] = { 0 };
276 280 /* must have a mountpoint */
277 281 if (zfs_prop_get(hdl, ZFS_PROP_MOUNTPOINT, mountpoint,
278 282 sizeof (mountpoint), NULL, NULL, 0, B_FALSE) != 0) {
279 283 /* no mountpoint */
280 284 return (NULL);
281 285 }
282 286
283 287 /* mountpoint must be a path */
284 288 if (strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) == 0 ||
285 289 strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
286 290 /*
287 291 * Search mmttab for mountpoint and get dataset.
288 292 */
289 293
290 294 if (search_mnttab == B_TRUE &&
291 295 get_legacy_mountpoint(path, mountpoint,
292 296 sizeof (mountpoint), NULL, 0) == 0) {
293 297 return (strdup(mountpoint));
294 298 }
295 299 return (NULL);
296 300 }
297 301
298 302 /* canmount must be set */
299 303 if (zfs_prop_get(hdl, ZFS_PROP_CANMOUNT, canmount,
300 304 sizeof (canmount), NULL, NULL, 0, B_FALSE) != 0 ||
301 305 strcmp(canmount, "off") == 0)
302 306 return (NULL);
303 307
304 308 /*
305 309 * have a mountable handle but want to skip those marked none
306 310 * and legacy
307 311 */
308 312 if (strcmp(mountpoint, path) == 0) {
309 313 return (strdup((char *)zfs_get_name(hdl)));
310 314 }
311 315
312 316 return (NULL);
313 317 }
314 318
315 319 /*
316 320 * get_zfs_dataset(impl_handle, path)
317 321 *
318 322 * get the name of the ZFS dataset the path is equivalent to. The
319 323 * dataset name is used for get/set of ZFS properties since libzfs
320 324 * requires a dataset to do a zfs_open().
321 325 */
322 326
323 327 static char *
324 328 get_zfs_dataset(sa_handle_impl_t impl_handle, char *path,
325 329 boolean_t search_mnttab)
326 330 {
327 331 size_t i, count = 0;
328 332 zfs_handle_t **zlist;
329 333 char *cutpath;
330 334 zfs_handle_t *handle_from_path;
331 335 char *ret = NULL;
332 336
333 337 /*
334 338 * First we optimistically assume that the mount path for the filesystem
335 339 * is the same as the name of the filesystem (minus some number of
336 340 * leading slashes). If this is true, then zfs_open should properly open
337 341 * the filesystem. We duplicate the error checking done later in the
338 342 * function for consistency. If anything fails, we resort to the
339 343 * (extremely slow) search of all the filesystems.
340 344 */
341 345 cutpath = path + strspn(path, "/");
342 346
343 347 assert(impl_handle->zfs_libhandle != NULL);
344 348 libzfs_print_on_error(impl_handle->zfs_libhandle, B_FALSE);
345 349 handle_from_path = zfs_open(impl_handle->zfs_libhandle, cutpath,
346 350 ZFS_TYPE_FILESYSTEM);
347 351 libzfs_print_on_error(impl_handle->zfs_libhandle, B_TRUE);
348 352 if (handle_from_path != NULL) {
349 353 ret = verify_zfs_handle(handle_from_path, path, search_mnttab);
350 354 zfs_close(handle_from_path);
351 355 if (ret != NULL) {
352 356 return (ret);
353 357 }
354 358 }
355 359 /*
356 360 * Couldn't find a filesystem optimistically, check all the handles we
357 361 * can.
358 362 */
359 363 get_all_filesystems(impl_handle, &zlist, &count);
360 364 for (i = 0; i < count; i++) {
361 365 assert(zlist[i]);
362 366 if ((ret = verify_zfs_handle(zlist[i], path,
363 367 search_mnttab)) != NULL)
364 368 return (ret);
365 369 }
366 370
367 371 /* Couldn't find a matching dataset */
368 372 return (NULL);
369 373 }
370 374
371 375 /*
372 376 * get_zfs_property(dataset, property)
373 377 *
374 378 * Get the file system property specified from the ZFS dataset.
375 379 */
376 380
377 381 static char *
378 382 get_zfs_property(char *dataset, zfs_prop_t property)
379 383 {
380 384 zfs_handle_t *handle = NULL;
381 385 char shareopts[ZFS_MAXPROPLEN];
382 386 libzfs_handle_t *libhandle;
383 387
384 388 libhandle = libzfs_init();
385 389 if (libhandle != NULL) {
386 390 handle = zfs_open(libhandle, dataset, ZFS_TYPE_FILESYSTEM);
387 391 if (handle != NULL) {
388 392 if (zfs_prop_get(handle, property, shareopts,
389 393 sizeof (shareopts), NULL, NULL, 0,
390 394 B_FALSE) == 0) {
391 395 zfs_close(handle);
392 396 libzfs_fini(libhandle);
393 397 return (strdup(shareopts));
394 398 }
395 399 zfs_close(handle);
396 400 }
397 401 libzfs_fini(libhandle);
398 402 }
399 403 return (NULL);
400 404 }
401 405
402 406 /*
403 407 * sa_zfs_is_shared(handle, path)
404 408 *
405 409 * Check to see if the ZFS path provided has the sharenfs option set
406 410 * or not.
407 411 */
408 412
409 413 int
410 414 sa_zfs_is_shared(sa_handle_t sahandle, char *path)
411 415 {
412 416 int ret = 0;
413 417 char *dataset;
414 418 zfs_handle_t *handle = NULL;
415 419 char shareopts[ZFS_MAXPROPLEN];
416 420 libzfs_handle_t *libhandle;
417 421
418 422 dataset = get_zfs_dataset((sa_handle_t)sahandle, path, B_FALSE);
419 423 if (dataset != NULL) {
420 424 libhandle = libzfs_init();
421 425 if (libhandle != NULL) {
422 426 handle = zfs_open(libhandle, dataset,
423 427 ZFS_TYPE_FILESYSTEM);
424 428 if (handle != NULL) {
425 429 if (zfs_prop_get(handle, ZFS_PROP_SHARENFS,
426 430 shareopts, sizeof (shareopts), NULL, NULL,
427 431 0, B_FALSE) == 0 &&
428 432 strcmp(shareopts, "off") != 0) {
429 433 ret = 1; /* it is shared */
430 434 }
431 435 zfs_close(handle);
432 436 }
433 437 libzfs_fini(libhandle);
434 438 }
435 439 free(dataset);
436 440 }
437 441 return (ret);
438 442 }
439 443
440 444 /*
441 445 * find_or_create_group(handle, groupname, proto, *err)
442 446 *
443 447 * While walking the ZFS tree, we need to add shares to a defined
444 448 * group. If the group doesn't exist, create it first, making sure it
445 449 * is marked as a ZFS group.
446 450 *
447 451 * Note that all ZFS shares are in a subgroup of the top level group
448 452 * called "zfs".
449 453 */
450 454
451 455 static sa_group_t
452 456 find_or_create_group(sa_handle_t handle, char *groupname, char *proto, int *err)
453 457 {
454 458 sa_group_t group;
455 459 sa_optionset_t optionset;
456 460 int ret = SA_OK;
457 461
458 462 /*
459 463 * we check to see if the "zfs" group exists. Since this
460 464 * should be the top level group, we don't want the
461 465 * parent. This is to make sure the zfs group has been created
462 466 * and to created if it hasn't been.
463 467 */
464 468 group = sa_get_group(handle, groupname);
465 469 if (group == NULL) {
466 470 group = sa_create_group(handle, groupname, &ret);
467 471
468 472 /* make sure this is flagged as a ZFS group */
469 473 if (group != NULL)
470 474 ret = sa_set_group_attr(group, "zfs", "true");
471 475 }
472 476 if (group != NULL) {
473 477 if (proto != NULL) {
474 478 optionset = sa_get_optionset(group, proto);
475 479 if (optionset == NULL)
476 480 optionset = sa_create_optionset(group, proto);
477 481 }
478 482 }
479 483 if (err != NULL)
480 484 *err = ret;
481 485 return (group);
482 486 }
483 487
484 488 /*
485 489 * find_or_create_zfs_subgroup(groupname, optstring, *err)
486 490 *
487 491 * ZFS shares will be in a subgroup of the "zfs" master group. This
488 492 * function looks to see if the groupname exists and returns it if it
489 493 * does or else creates a new one with the specified name and returns
490 494 * that. The "zfs" group will exist before we get here, but we make
491 495 * sure just in case.
492 496 *
493 497 * err must be a valid pointer.
494 498 */
495 499
496 500 static sa_group_t
497 501 find_or_create_zfs_subgroup(sa_handle_t handle, char *groupname, char *proto,
498 502 char *optstring, int *err)
499 503 {
500 504 sa_group_t group = NULL;
501 505 sa_group_t zfs;
502 506 char *name;
503 507 char *options;
504 508
505 509 /* start with the top-level "zfs" group */
506 510 zfs = sa_get_group(handle, "zfs");
507 511 *err = SA_OK;
508 512 if (zfs != NULL) {
509 513 for (group = sa_get_sub_group(zfs); group != NULL;
510 514 group = sa_get_next_group(group)) {
511 515 name = sa_get_group_attr(group, "name");
512 516 if (name != NULL && strcmp(name, groupname) == 0) {
513 517 /* have the group so break out of here */
514 518 sa_free_attr_string(name);
515 519 break;
516 520 }
517 521 if (name != NULL)
518 522 sa_free_attr_string(name);
519 523 }
520 524
521 525 if (group == NULL) {
522 526 /*
523 527 * Need to create the sub-group since it doesn't exist
524 528 */
525 529 group = _sa_create_zfs_group(zfs, groupname);
526 530 if (group == NULL) {
527 531 *err = SA_NO_MEMORY;
528 532 return (NULL);
529 533 }
530 534 set_node_attr(group, "zfs", "true");
531 535 }
532 536 if (strcmp(optstring, "on") == 0)
533 537 optstring = "rw";
534 538 options = strdup(optstring);
535 539 if (options != NULL) {
536 540 *err = sa_parse_legacy_options(group, options,
537 541 proto);
538 542 /* If no optionset, add one. */
539 543 if (sa_get_optionset(group, proto) == NULL)
540 544 (void) sa_create_optionset(group, proto);
541 545
542 546 /*
543 547 * Do not forget to update an optionset of
544 548 * the parent group so that it contains
545 549 * all protocols its subgroups have.
546 550 */
547 551 if (sa_get_optionset(zfs, proto) == NULL)
548 552 (void) sa_create_optionset(zfs, proto);
549 553
550 554 free(options);
551 555 } else {
552 556 *err = SA_NO_MEMORY;
553 557 }
554 558 }
555 559 return (group);
556 560 }
557 561
558 562 /*
559 563 * zfs_construct_resource(share, name, base, dataset)
560 564 *
561 565 * Add a resource to the share using name as a template. If name ==
562 566 * NULL, then construct a name based on the dataset value.
563 567 * name.
564 568 */
565 569 static void
566 570 zfs_construct_resource(sa_share_t share, char *dataset)
567 571 {
568 572 char buff[SA_MAX_RESOURCE_NAME + 1];
569 573 int ret = SA_OK;
570 574
571 575 (void) snprintf(buff, SA_MAX_RESOURCE_NAME, "%s", dataset);
572 576 sa_fix_resource_name(buff);
573 577 (void) sa_add_resource(share, buff, SA_SHARE_TRANSIENT, &ret);
574 578 }
575 579
576 580 /*
577 581 * zfs_inherited(handle, source, sourcestr)
578 582 *
579 583 * handle case of inherited share{nfs,smb}. Pulled out of sa_get_zfs_shares
580 584 * for readability.
581 585 */
582 586 static int
583 587 zfs_inherited(sa_handle_t handle, sa_share_t share, char *sourcestr,
584 588 char *shareopts, char *mountpoint, char *proto, char *dataset)
585 589 {
586 590 int doshopt = 0;
587 591 int err = SA_OK;
588 592 sa_group_t group;
589 593 sa_resource_t resource;
590 594 uint64_t features;
591 595
592 596 /*
593 597 * Need to find the "real" parent sub-group. It may not be
594 598 * mounted, but it was identified in the "sourcestr"
595 599 * variable. The real parent not mounted can occur if
596 600 * "canmount=off and sharenfs=on".
597 601 */
598 602 group = find_or_create_zfs_subgroup(handle, sourcestr, proto,
599 603 shareopts, &doshopt);
600 604 if (group != NULL) {
601 605 /*
602 606 * We may need the first share for resource
603 607 * prototype. We only care about it if it has a
604 608 * resource that sets a prefix value.
605 609 */
606 610 if (share == NULL)
607 611 share = _sa_add_share(group, mountpoint,
608 612 SA_SHARE_TRANSIENT, &err,
609 613 (uint64_t)SA_FEATURE_NONE);
610 614 /*
611 615 * some options may only be on shares. If the opt
612 616 * string contains one of those, we put it just on the
613 617 * share.
614 618 */
615 619 if (share != NULL && doshopt == SA_PROP_SHARE_ONLY) {
616 620 char *options;
617 621 options = strdup(shareopts);
618 622 if (options != NULL) {
619 623 set_node_attr(share, "dataset", dataset);
620 624 err = sa_parse_legacy_options(share, options,
621 625 proto);
622 626 set_node_attr(share, "dataset", NULL);
623 627 free(options);
624 628 }
625 629 if (sa_get_optionset(group, proto) == NULL)
626 630 (void) sa_create_optionset(group, proto);
627 631 }
628 632 features = sa_proto_get_featureset(proto);
629 633 if (share != NULL && features & SA_FEATURE_RESOURCE) {
630 634 /*
631 635 * We have a share and the protocol requires
632 636 * that at least one resource exist (probably
633 637 * SMB). We need to make sure that there is at
634 638 * least one.
635 639 */
636 640 resource = sa_get_share_resource(share, NULL);
637 641 if (resource == NULL) {
638 642 zfs_construct_resource(share, dataset);
639 643 }
640 644 }
641 645 } else {
642 646 err = SA_NO_MEMORY;
643 647 }
644 648 return (err);
645 649 }
646 650
647 651 /*
648 652 * zfs_notinherited(group, share, mountpoint, shareopts, proto, dataset,
649 653 * grouperr)
650 654 *
651 655 * handle case where this is the top of a sub-group in ZFS. Pulled out
652 656 * of sa_get_zfs_shares for readability. We need the grouperr from the
653 657 * creation of the subgroup to know whether to add the public
654 658 * property, etc. to the specific share.
655 659 */
656 660 static int
657 661 zfs_notinherited(sa_group_t group, sa_share_t share, char *mountpoint,
658 662 char *shareopts, char *proto, char *dataset, int grouperr)
659 663 {
660 664 int err = SA_OK;
661 665 sa_resource_t resource;
662 666 uint64_t features;
663 667
664 668 set_node_attr(group, "zfs", "true");
665 669 if (share == NULL)
666 670 share = _sa_add_share(group, mountpoint, SA_SHARE_TRANSIENT,
667 671 &err, (uint64_t)SA_FEATURE_NONE);
668 672
669 673 if (err != SA_OK)
670 674 return (err);
671 675
672 676 if (strcmp(shareopts, "on") == 0)
673 677 shareopts = "";
674 678 if (shareopts != NULL) {
675 679 char *options;
676 680 if (grouperr == SA_PROP_SHARE_ONLY) {
677 681 /*
678 682 * Some properties may only be on shares, but
679 683 * due to the ZFS sub-groups being artificial,
680 684 * we sometimes get this and have to deal with
681 685 * it. We do it by attempting to put it on the
682 686 * share.
683 687 */
684 688 options = strdup(shareopts);
685 689 if (options != NULL) {
686 690 err = sa_parse_legacy_options(share,
687 691 options, proto);
688 692 free(options);
689 693 }
690 694 }
691 695 /* Unmark the share's changed state */
692 696 set_node_attr(share, "changed", NULL);
693 697 }
694 698 features = sa_proto_get_featureset(proto);
695 699 if (share != NULL && features & SA_FEATURE_RESOURCE) {
696 700 /*
697 701 * We have a share and the protocol requires that at
698 702 * least one resource exist (probably SMB). We need to
699 703 * make sure that there is at least one.
700 704 */
701 705 resource = sa_get_share_resource(share, NULL);
702 706 if (resource == NULL) {
703 707 zfs_construct_resource(share, dataset);
704 708 }
705 709 }
706 710 return (err);
707 711 }
708 712
709 713 /*
710 714 * zfs_grp_error(err)
711 715 *
712 716 * Print group create error, but only once. If err is 0 do the
713 717 * print else don't.
714 718 */
715 719
716 720 static void
717 721 zfs_grp_error(int err)
718 722 {
719 723 if (err == 0) {
720 724 /* only print error once */
721 725 (void) fprintf(stderr, dgettext(TEXT_DOMAIN,
722 726 "Cannot create ZFS subgroup during initialization:"
723 727 " %s\n"), sa_errorstr(SA_SYSTEM_ERR));
724 728 }
725 729 }
726 730
727 731 /*
728 732 * zfs_process_share(handle, share, mountpoint, proto, source,
729 733 * shareopts, sourcestr)
730 734 *
731 735 * Creates the subgroup, if necessary and adds shares, resources
732 736 * and properties.
733 737 */
734 738 int
735 739 sa_zfs_process_share(sa_handle_t handle, sa_group_t group, sa_share_t share,
736 740 char *mountpoint, char *proto, zprop_source_t source, char *shareopts,
737 741 char *sourcestr, char *dataset)
738 742 {
739 743 int err = SA_OK;
740 744
741 745 if (source & ZPROP_SRC_INHERITED) {
742 746 err = zfs_inherited(handle, share, sourcestr, shareopts,
743 747 mountpoint, proto, dataset);
744 748 } else {
745 749 group = find_or_create_zfs_subgroup(handle, dataset, proto,
746 750 shareopts, &err);
747 751 if (group == NULL) {
748 752 static boolean_t reported_error = B_FALSE;
749 753 /*
750 754 * There is a problem, but we can't do
751 755 * anything about it at this point so we issue
752 756 * a warning and move on.
753 757 */
754 758 zfs_grp_error(reported_error);
755 759 reported_error = B_TRUE;
756 760 }
757 761 set_node_attr(group, "zfs", "true");
758 762 /*
759 763 * Add share with local opts via zfs_notinherited.
760 764 */
761 765 err = zfs_notinherited(group, share, mountpoint, shareopts,
762 766 proto, dataset, err);
763 767 }
764 768 return (err);
765 769 }
766 770
767 771 /*
768 772 * Walk the mnttab for all zfs mounts and determine which are
769 773 * shared. Find or create the appropriate group/sub-group to contain
770 774 * the shares.
771 775 *
772 776 * All shares are in a sub-group that will hold the properties. This
773 777 * allows representing the inherited property model.
774 778 *
775 779 * One area of complication is if "sharenfs" is set at one level of
776 780 * the directory tree and "sharesmb" is set at a different level, the
777 781 * a sub-group must be formed at the lower level for both
778 782 * protocols. That is the nature of the problem in CR 6667349.
779 783 */
780 784 static int
781 785 sa_get_zfs_share_common(sa_handle_t handle, zfs_handle_t *fs_handle, char *path,
782 786 sa_group_t zfsgroup)
783 787 {
784 788 boolean_t smb, nfs;
785 789 boolean_t smb_inherited, nfs_inherited;
786 790 char nfsshareopts[ZFS_MAXPROPLEN];
787 791 char smbshareopts[ZFS_MAXPROPLEN];
788 792 char nfssourcestr[ZFS_MAXPROPLEN];
789 793 char smbsourcestr[ZFS_MAXPROPLEN];
790 794 char mountpoint[ZFS_MAXPROPLEN];
791 795 int err = SA_OK;
792 796 zprop_source_t source;
793 797 sa_share_t share;
794 798 char *dataset;
795 799
796 800 source = ZPROP_SRC_ALL;
797 801 /* If no mountpoint, skip. */
798 802 if (zfs_prop_get(fs_handle, ZFS_PROP_MOUNTPOINT,
799 803 mountpoint, sizeof (mountpoint), NULL, NULL, 0,
800 804 B_FALSE) != 0)
801 805 return (SA_SYSTEM_ERR);
802 806
803 807 if (path != NULL)
804 808 (void) strncpy(path, mountpoint, sizeof (mountpoint));
805 809 /*
806 810 * zfs_get_name value must not be freed. It is just a
807 811 * pointer to a value in the handle.
808 812 */
809 813 if ((dataset = (char *)zfs_get_name(fs_handle)) == NULL)
|
↓ open down ↓ |
536 lines elided |
↑ open up ↑ |
810 814 return (SA_SYSTEM_ERR);
811 815
812 816 /*
813 817 * only deal with "mounted" file systems since
814 818 * unmounted file systems can't actually be shared.
815 819 */
816 820
817 821 if (!zfs_is_mounted(fs_handle, NULL))
818 822 return (SA_SYSTEM_ERR);
819 823
824 + /*
825 + * Ignore "zoned" datasets in global zone.
826 + */
827 + if (getzoneid() == GLOBAL_ZONEID &&
828 + zfs_prop_get_int(fs_handle, ZFS_PROP_ZONED))
829 + return (SA_SYSTEM_ERR);
830 +
820 831 nfs = nfs_inherited = B_FALSE;
821 832
822 833 if (zfs_prop_get(fs_handle, ZFS_PROP_SHARENFS, nfsshareopts,
823 834 sizeof (nfsshareopts), &source, nfssourcestr,
824 835 ZFS_MAXPROPLEN, B_FALSE) == 0 &&
825 836 strcmp(nfsshareopts, "off") != 0) {
826 837 if (source & ZPROP_SRC_INHERITED)
827 838 nfs_inherited = B_TRUE;
828 839 else
829 840 nfs = B_TRUE;
830 841 }
831 842
832 843 smb = smb_inherited = B_FALSE;
833 844 if (zfs_prop_get(fs_handle, ZFS_PROP_SHARESMB, smbshareopts,
834 845 sizeof (smbshareopts), &source, smbsourcestr,
835 846 ZFS_MAXPROPLEN, B_FALSE) == 0 &&
836 847 strcmp(smbshareopts, "off") != 0) {
837 848 if (source & ZPROP_SRC_INHERITED)
838 849 smb_inherited = B_TRUE;
839 850 else
840 851 smb = B_TRUE;
841 852 }
842 853
843 854 /*
844 855 * If the mountpoint is already shared, it must be a
845 856 * non-ZFS share. We want to remove the share from its
846 857 * parent group and reshare it under ZFS.
847 858 */
848 859 share = sa_find_share(handle, mountpoint);
849 860 if (share != NULL &&
850 861 (nfs || smb || nfs_inherited || smb_inherited)) {
851 862 err = sa_remove_share(share);
852 863 share = NULL;
853 864 }
854 865
855 866 /*
856 867 * At this point, we have the information needed to
857 868 * determine what to do with the share.
858 869 *
859 870 * If smb or nfs is set, we have a new sub-group.
860 871 * If smb_inherit and/or nfs_inherit is set, then
861 872 * place on an existing sub-group. If both are set,
862 873 * the existing sub-group is the closest up the tree.
863 874 */
864 875 if (nfs || smb) {
865 876 /*
866 877 * Non-inherited is the straightforward
867 878 * case. sa_zfs_process_share handles it
868 879 * directly. Make sure that if the "other"
869 880 * protocol is inherited, that we treat it as
870 881 * non-inherited as well.
871 882 */
872 883 if (nfs || nfs_inherited) {
873 884 err = sa_zfs_process_share(handle, zfsgroup,
874 885 share, mountpoint, "nfs",
875 886 0, nfsshareopts,
876 887 nfssourcestr, dataset);
877 888 share = sa_find_share(handle, mountpoint);
878 889 }
879 890 if (smb || smb_inherited) {
880 891 err = sa_zfs_process_share(handle, zfsgroup,
881 892 share, mountpoint, "smb",
882 893 0, smbshareopts,
883 894 smbsourcestr, dataset);
884 895 }
885 896 } else if (nfs_inherited || smb_inherited) {
886 897 char *grpdataset;
887 898 /*
888 899 * If we only have inherited groups, it is
889 900 * important to find the closer of the two if
890 901 * the protocols are set at different
891 902 * levels. The closest sub-group is the one we
892 903 * want to work with.
893 904 */
894 905 if (nfs_inherited && smb_inherited) {
895 906 if (strcmp(nfssourcestr, smbsourcestr) <= 0)
896 907 grpdataset = nfssourcestr;
897 908 else
898 909 grpdataset = smbsourcestr;
899 910 } else if (nfs_inherited) {
900 911 grpdataset = nfssourcestr;
901 912 } else if (smb_inherited) {
902 913 grpdataset = smbsourcestr;
903 914 }
904 915 if (nfs_inherited) {
905 916 err = sa_zfs_process_share(handle, zfsgroup,
906 917 share, mountpoint, "nfs",
907 918 ZPROP_SRC_INHERITED, nfsshareopts,
908 919 grpdataset, dataset);
909 920 share = sa_find_share(handle, mountpoint);
910 921 }
911 922 if (smb_inherited) {
912 923 err = sa_zfs_process_share(handle, zfsgroup,
913 924 share, mountpoint, "smb",
914 925 ZPROP_SRC_INHERITED, smbshareopts,
915 926 grpdataset, dataset);
916 927 }
917 928 }
918 929 return (err);
919 930 }
920 931
921 932 /*
922 933 * Handles preparing generic objects such as the libzfs handle and group for
923 934 * sa_get_one_zfs_share, sa_get_zfs_share_for_name, and sa_get_zfs_shares.
924 935 */
925 936 static int
926 937 prep_zfs_handle_and_group(sa_handle_t handle, char *groupname,
927 938 libzfs_handle_t **zfs_libhandle, sa_group_t *zfsgroup, int *err)
928 939 {
929 940 /*
930 941 * If we can't access libzfs, don't bother doing anything.
931 942 */
932 943 *zfs_libhandle = ((sa_handle_impl_t)handle)->zfs_libhandle;
933 944 if (*zfs_libhandle == NULL)
934 945 return (SA_SYSTEM_ERR);
935 946
936 947 *zfsgroup = find_or_create_group(handle, groupname, NULL, err);
937 948 return (SA_OK);
938 949 }
939 950
940 951 /*
941 952 * The O.G. zfs share preparation function. This initializes all zfs shares for
942 953 * use with libshare.
943 954 */
944 955 int
945 956 sa_get_zfs_shares(sa_handle_t handle, char *groupname)
946 957 {
947 958 sa_group_t zfsgroup;
948 959 zfs_handle_t **zlist;
949 960 size_t count = 0;
950 961 libzfs_handle_t *zfs_libhandle;
951 962 int err;
952 963
953 964 if ((err = prep_zfs_handle_and_group(handle, groupname, &zfs_libhandle,
954 965 &zfsgroup, &err)) != SA_OK) {
955 966 return (err);
956 967 }
957 968 /* Not an error, this could be a legacy condition */
958 969 if (zfsgroup == NULL)
959 970 return (SA_OK);
960 971
961 972 /*
962 973 * need to walk the mounted ZFS pools and datasets to
963 974 * find shares that are possible.
964 975 */
965 976 get_all_filesystems((sa_handle_impl_t)handle, &zlist, &count);
966 977 qsort(zlist, count, sizeof (void *), mountpoint_compare);
967 978
|
↓ open down ↓ |
138 lines elided |
↑ open up ↑ |
968 979 for (int i = 0; i < count; i++) {
969 980 err = sa_get_zfs_share_common(handle, zlist[i], NULL, zfsgroup);
970 981 }
971 982 /*
972 983 * Don't need to free the "zlist" variable since it is only a
973 984 * pointer to a cached value that will be freed when
974 985 * sa_fini() is called.
975 986 */
976 987 return (err);
977 988 }
989 +
990 +/*
991 + * Initializes shares for only the dataset specified fs_handle.
992 + * This is used as a performance optimization relative to sa_get_zfs_shares.
993 + */
994 +int
995 +sa_get_zfs_share(sa_handle_t handle, char *groupname, zfs_handle_t *fs_handle)
996 +{
997 + sa_group_t zfsgroup;
998 + libzfs_handle_t *zfs_libhandle;
999 + int err;
1000 +
1001 + if ((err = prep_zfs_handle_and_group(handle, groupname, &zfs_libhandle,
1002 + &zfsgroup, &err)) != SA_OK) {
1003 + return (err);
1004 + }
1005 + /* Not an error, this could be a legacy condition */
1006 + if (zfsgroup == NULL)
1007 + return (SA_OK);
1008 +
1009 + err = sa_get_zfs_share_common(handle, fs_handle, NULL, zfsgroup);
1010 + return (err);
1011 +}
978 1012
979 1013 /*
980 1014 * Initializes only the handles specified in the sharearg for use with libshare.
981 1015 * This is used as a performance optimization relative to sa_get_zfs_shares.
982 1016 */
983 1017 int
984 1018 sa_get_one_zfs_share(sa_handle_t handle, char *groupname,
985 1019 sa_init_selective_arg_t *sharearg, char ***paths, size_t *paths_len)
986 1020 {
987 1021 sa_group_t zfsgroup;
988 1022 libzfs_handle_t *zfs_libhandle;
989 1023 int err;
990 1024
991 1025 if ((err = prep_zfs_handle_and_group(handle, groupname, &zfs_libhandle,
992 1026 &zfsgroup, &err)) != SA_OK) {
993 1027 return (err);
994 1028 }
995 1029 /* Not an error, this could be a legacy condition */
996 1030 if (zfsgroup == NULL)
997 1031 return (SA_OK);
998 1032
999 1033 *paths_len = sharearg->zhandle_len;
1000 1034 *paths = calloc(*paths_len, sizeof (char *));
1001 1035 for (int i = 0; i < sharearg->zhandle_len; ++i) {
1002 1036 zfs_handle_t *fs_handle =
1003 1037 ((zfs_handle_t **)(sharearg->zhandle_arr))[i];
1004 1038 if (fs_handle == NULL) {
1005 1039 /* Free non-null elements of the paths array */
1006 1040 for (int free_idx = 0; free_idx < *paths_len;
1007 1041 ++free_idx) {
1008 1042 if ((*paths)[free_idx] != NULL)
1009 1043 free((*paths)[free_idx]);
1010 1044 }
1011 1045 free(*paths);
1012 1046 *paths = NULL;
1013 1047 *paths_len = 0;
1014 1048 return (SA_SYSTEM_ERR);
1015 1049 }
1016 1050 (*paths)[i] = malloc(sizeof (char) * ZFS_MAXPROPLEN);
1017 1051 err |= sa_get_zfs_share_common(handle, fs_handle, (*paths)[i],
1018 1052 zfsgroup);
1019 1053 }
1020 1054
1021 1055 return (err);
1022 1056 }
1023 1057
1024 1058 /*
1025 1059 * Initializes only the share with the specified sharename for use with
1026 1060 * libshare.
1027 1061 */
1028 1062 int
1029 1063 sa_get_zfs_share_for_name(sa_handle_t handle, char *groupname,
1030 1064 const char *sharename, char *outpath)
1031 1065 {
1032 1066 sa_group_t zfsgroup;
1033 1067 libzfs_handle_t *zfs_libhandle;
1034 1068 int err;
1035 1069
1036 1070 if ((err = prep_zfs_handle_and_group(handle, groupname, &zfs_libhandle,
1037 1071 &zfsgroup, &err)) != SA_OK) {
1038 1072 return (err);
1039 1073 }
1040 1074 /* Not an error, this could be a legacy condition */
1041 1075 if (zfsgroup == NULL)
1042 1076 return (SA_OK);
1043 1077
1044 1078 zfs_handle_t *fs_handle = zfs_open(zfs_libhandle,
1045 1079 sharename + strspn(sharename, "/"), ZFS_TYPE_DATASET);
1046 1080 if (fs_handle == NULL)
1047 1081 return (SA_SYSTEM_ERR);
1048 1082
1049 1083 err = sa_get_zfs_share_common(handle, fs_handle, outpath, zfsgroup);
1050 1084 zfs_close(fs_handle);
1051 1085 return (err);
1052 1086 }
1053 1087
1054 1088
1055 1089
1056 1090 #define COMMAND "/usr/sbin/zfs"
1057 1091
1058 1092 /*
1059 1093 * sa_zfs_set_sharenfs(group, path, on)
1060 1094 *
1061 1095 * Update the "sharenfs" property on the path. If on is true, then set
1062 1096 * to the properties on the group or "on" if no properties are
1063 1097 * defined. Set to "off" if on is false.
1064 1098 */
1065 1099
1066 1100 int
1067 1101 sa_zfs_set_sharenfs(sa_group_t group, char *path, int on)
1068 1102 {
1069 1103 int ret = SA_NOT_IMPLEMENTED;
1070 1104 char *command;
1071 1105
1072 1106 command = malloc(ZFS_MAXPROPLEN * 2);
1073 1107 if (command != NULL) {
1074 1108 char *opts = NULL;
1075 1109 char *dataset = NULL;
1076 1110 FILE *pfile;
1077 1111 sa_handle_impl_t impl_handle;
1078 1112 /* for now, NFS is always available for "zfs" */
1079 1113 if (on) {
1080 1114 opts = sa_proto_legacy_format("nfs", group, 1);
1081 1115 if (opts != NULL && strlen(opts) == 0) {
1082 1116 free(opts);
1083 1117 opts = strdup("on");
1084 1118 }
1085 1119 }
1086 1120
1087 1121 impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1088 1122 assert(impl_handle != NULL);
1089 1123 if (impl_handle != NULL)
1090 1124 dataset = get_zfs_dataset(impl_handle, path, B_FALSE);
1091 1125 else
1092 1126 ret = SA_SYSTEM_ERR;
1093 1127
1094 1128 if (dataset != NULL) {
1095 1129 (void) snprintf(command, ZFS_MAXPROPLEN * 2,
1096 1130 "%s set sharenfs=\"%s\" %s", COMMAND,
1097 1131 opts != NULL ? opts : "off", dataset);
1098 1132 pfile = popen(command, "r");
1099 1133 if (pfile != NULL) {
1100 1134 ret = pclose(pfile);
1101 1135 if (ret != 0)
1102 1136 ret = SA_SYSTEM_ERR;
1103 1137 }
1104 1138 }
1105 1139 if (opts != NULL)
1106 1140 free(opts);
1107 1141 if (dataset != NULL)
1108 1142 free(dataset);
1109 1143 free(command);
1110 1144 }
1111 1145 return (ret);
1112 1146 }
1113 1147
1114 1148 /*
1115 1149 * add_resources(share, opt)
1116 1150 *
1117 1151 * Add resource properties to those in "opt". Resources are prefixed
1118 1152 * with name=resourcename.
1119 1153 */
1120 1154 static char *
1121 1155 add_resources(sa_share_t share, char *opt)
1122 1156 {
1123 1157 char *newopt = NULL;
1124 1158 char *propstr;
1125 1159 sa_resource_t resource;
1126 1160
1127 1161 newopt = strdup(opt);
1128 1162 if (newopt == NULL)
1129 1163 return (newopt);
1130 1164
1131 1165 for (resource = sa_get_share_resource(share, NULL);
1132 1166 resource != NULL;
1133 1167 resource = sa_get_next_resource(resource)) {
1134 1168 char *name;
1135 1169 size_t size;
1136 1170
1137 1171 name = sa_get_resource_attr(resource, "name");
1138 1172 if (name == NULL) {
1139 1173 free(newopt);
1140 1174 return (NULL);
1141 1175 }
1142 1176 size = strlen(name) + strlen(opt) + sizeof ("name=") + 1;
1143 1177 newopt = calloc(1, size);
1144 1178 if (newopt != NULL)
1145 1179 (void) snprintf(newopt, size, "%s,name=%s", opt, name);
1146 1180 sa_free_attr_string(name);
1147 1181 free(opt);
1148 1182 opt = newopt;
1149 1183 propstr = sa_proto_legacy_format("smb", resource, 0);
1150 1184 if (propstr == NULL) {
1151 1185 free(opt);
1152 1186 return (NULL);
1153 1187 }
1154 1188 size = strlen(propstr) + strlen(opt) + 2;
1155 1189 newopt = calloc(1, size);
1156 1190 if (newopt != NULL)
1157 1191 (void) snprintf(newopt, size, "%s,%s", opt, propstr);
1158 1192 free(opt);
1159 1193 opt = newopt;
1160 1194 }
1161 1195 return (opt);
1162 1196 }
1163 1197
1164 1198 /*
1165 1199 * sa_zfs_set_sharesmb(group, path, on)
1166 1200 *
1167 1201 * Update the "sharesmb" property on the path. If on is true, then set
1168 1202 * to the properties on the group or "on" if no properties are
1169 1203 * defined. Set to "off" if on is false.
1170 1204 */
1171 1205
1172 1206 int
1173 1207 sa_zfs_set_sharesmb(sa_group_t group, char *path, int on)
1174 1208 {
1175 1209 int ret = SA_NOT_IMPLEMENTED;
1176 1210 char *command;
1177 1211 sa_share_t share;
1178 1212
1179 1213 /* In case SMB not enabled */
1180 1214 if (sa_get_optionset(group, "smb") == NULL)
1181 1215 return (SA_NOT_SUPPORTED);
1182 1216
1183 1217 command = malloc(ZFS_MAXPROPLEN * 2);
1184 1218 if (command != NULL) {
1185 1219 char *opts = NULL;
1186 1220 char *dataset = NULL;
1187 1221 FILE *pfile;
1188 1222 sa_handle_impl_t impl_handle;
1189 1223
1190 1224 if (on) {
1191 1225 char *newopt;
1192 1226
1193 1227 share = sa_get_share(group, NULL);
1194 1228 opts = sa_proto_legacy_format("smb", share, 1);
1195 1229 if (opts != NULL && strlen(opts) == 0) {
1196 1230 free(opts);
1197 1231 opts = strdup("on");
1198 1232 }
1199 1233 newopt = add_resources(opts, share);
1200 1234 free(opts);
1201 1235 opts = newopt;
1202 1236 }
1203 1237
1204 1238 impl_handle = (sa_handle_impl_t)sa_find_group_handle(group);
1205 1239 assert(impl_handle != NULL);
1206 1240 if (impl_handle != NULL)
1207 1241 dataset = get_zfs_dataset(impl_handle, path, B_FALSE);
1208 1242 else
1209 1243 ret = SA_SYSTEM_ERR;
1210 1244
1211 1245 if (dataset != NULL) {
1212 1246 (void) snprintf(command, ZFS_MAXPROPLEN * 2,
1213 1247 "echo %s set sharesmb=\"%s\" %s", COMMAND,
1214 1248 opts != NULL ? opts : "off", dataset);
1215 1249 pfile = popen(command, "r");
1216 1250 if (pfile != NULL) {
1217 1251 ret = pclose(pfile);
1218 1252 if (ret != 0)
1219 1253 ret = SA_SYSTEM_ERR;
1220 1254 }
1221 1255 }
1222 1256 if (opts != NULL)
1223 1257 free(opts);
1224 1258 if (dataset != NULL)
1225 1259 free(dataset);
1226 1260 free(command);
1227 1261 }
1228 1262 return (ret);
1229 1263 }
1230 1264
1231 1265 /*
1232 1266 * sa_zfs_update(group)
1233 1267 *
1234 1268 * call back to ZFS to update the share if necessary.
1235 1269 * Don't do it if it isn't a real change.
1236 1270 */
1237 1271 int
1238 1272 sa_zfs_update(sa_group_t group)
1239 1273 {
1240 1274 sa_optionset_t protopt;
1241 1275 sa_group_t parent;
1242 1276 char *command;
1243 1277 char *optstring;
1244 1278 int ret = SA_OK;
1245 1279 int doupdate = 0;
1246 1280 FILE *pfile;
1247 1281
1248 1282 if (sa_is_share(group))
1249 1283 parent = sa_get_parent_group(group);
1250 1284 else
1251 1285 parent = group;
1252 1286
1253 1287 if (parent != NULL) {
1254 1288 command = malloc(ZFS_MAXPROPLEN * 2);
1255 1289 if (command == NULL)
1256 1290 return (SA_NO_MEMORY);
1257 1291
1258 1292 *command = '\0';
1259 1293 for (protopt = sa_get_optionset(parent, NULL); protopt != NULL;
1260 1294 protopt = sa_get_next_optionset(protopt)) {
1261 1295
1262 1296 char *proto = sa_get_optionset_attr(protopt, "type");
1263 1297 char *path;
1264 1298 char *dataset = NULL;
1265 1299 char *zfsopts = NULL;
1266 1300
1267 1301 if (sa_is_share(group)) {
1268 1302 path = sa_get_share_attr((sa_share_t)group,
1269 1303 "path");
1270 1304 if (path != NULL) {
1271 1305 sa_handle_impl_t impl_handle;
1272 1306
1273 1307 impl_handle = sa_find_group_handle(
1274 1308 group);
1275 1309 if (impl_handle != NULL)
1276 1310 dataset = get_zfs_dataset(
1277 1311 impl_handle, path, B_FALSE);
1278 1312 else
1279 1313 ret = SA_SYSTEM_ERR;
1280 1314
1281 1315 sa_free_attr_string(path);
1282 1316 }
1283 1317 } else {
1284 1318 dataset = sa_get_group_attr(group, "name");
1285 1319 }
1286 1320 /* update only when there is an optstring found */
1287 1321 doupdate = 0;
1288 1322 if (proto != NULL && dataset != NULL) {
1289 1323 optstring = sa_proto_legacy_format(proto,
1290 1324 group, 1);
1291 1325 zfsopts = get_zfs_property(dataset,
1292 1326 ZFS_PROP_SHARENFS);
1293 1327
1294 1328 if (optstring != NULL && zfsopts != NULL) {
1295 1329 if (strcmp(optstring, zfsopts) != 0)
1296 1330 doupdate++;
1297 1331 }
1298 1332 if (doupdate) {
1299 1333 if (optstring != NULL &&
1300 1334 strlen(optstring) > 0) {
1301 1335 (void) snprintf(command,
1302 1336 ZFS_MAXPROPLEN * 2,
1303 1337 "%s set share%s=%s %s",
1304 1338 COMMAND, proto,
1305 1339 optstring, dataset);
1306 1340 } else {
1307 1341 (void) snprintf(command,
1308 1342 ZFS_MAXPROPLEN * 2,
1309 1343 "%s set share%s=on %s",
1310 1344 COMMAND, proto,
1311 1345 dataset);
1312 1346 }
1313 1347 pfile = popen(command, "r");
1314 1348 if (pfile != NULL)
1315 1349 ret = pclose(pfile);
1316 1350 switch (ret) {
1317 1351 default:
1318 1352 case 1:
1319 1353 ret = SA_SYSTEM_ERR;
1320 1354 break;
1321 1355 case 2:
1322 1356 ret = SA_SYNTAX_ERR;
1323 1357 break;
1324 1358 case 0:
1325 1359 break;
1326 1360 }
1327 1361 }
1328 1362 if (optstring != NULL)
1329 1363 free(optstring);
1330 1364 if (zfsopts != NULL)
1331 1365 free(zfsopts);
1332 1366 }
1333 1367 if (proto != NULL)
1334 1368 sa_free_attr_string(proto);
1335 1369 if (dataset != NULL)
1336 1370 free(dataset);
1337 1371 }
1338 1372 free(command);
1339 1373 }
1340 1374 return (ret);
1341 1375 }
1342 1376
1343 1377 /*
1344 1378 * sa_group_is_zfs(group)
1345 1379 *
1346 1380 * Given the group, determine if the zfs attribute is set.
1347 1381 */
1348 1382
1349 1383 int
1350 1384 sa_group_is_zfs(sa_group_t group)
1351 1385 {
1352 1386 char *zfs;
1353 1387 int ret = 0;
1354 1388
1355 1389 zfs = sa_get_group_attr(group, "zfs");
1356 1390 if (zfs != NULL) {
1357 1391 ret = 1;
1358 1392 sa_free_attr_string(zfs);
1359 1393 }
1360 1394 return (ret);
1361 1395 }
1362 1396
1363 1397 /*
1364 1398 * sa_path_is_zfs(path)
1365 1399 *
1366 1400 * Check to see if the file system path represents is of type "zfs".
1367 1401 */
1368 1402
1369 1403 int
1370 1404 sa_path_is_zfs(char *path)
1371 1405 {
1372 1406 char *fstype;
1373 1407 int ret = 0;
1374 1408
1375 1409 fstype = sa_fstype(path);
1376 1410 if (fstype != NULL && strcmp(fstype, "zfs") == 0)
1377 1411 ret = 1;
1378 1412 if (fstype != NULL)
1379 1413 sa_free_fstype(fstype);
1380 1414 return (ret);
1381 1415 }
1382 1416
1383 1417 int
1384 1418 sa_sharetab_fill_zfs(sa_share_t share, share_t *sh, char *proto)
1385 1419 {
1386 1420 char *path;
1387 1421
1388 1422 /* Make sure path is valid */
1389 1423
1390 1424 path = sa_get_share_attr(share, "path");
1391 1425 if (path != NULL) {
1392 1426 (void) memset(sh, 0, sizeof (sh));
1393 1427 (void) sa_fillshare(share, proto, sh);
1394 1428 sa_free_attr_string(path);
1395 1429 return (0);
1396 1430 } else
1397 1431 return (1);
1398 1432 }
1399 1433
1400 1434 #define SMAX(i, j) \
1401 1435 if ((j) > (i)) { \
1402 1436 (i) = (j); \
1403 1437 }
1404 1438
1405 1439 int
1406 1440 sa_share_zfs(sa_share_t share, sa_resource_t resource, char *path, share_t *sh,
1407 1441 void *exportdata, zfs_share_op_t operation)
1408 1442 {
1409 1443 libzfs_handle_t *libhandle;
1410 1444 sa_group_t group;
1411 1445 sa_handle_t sahandle;
1412 1446 char *dataset;
1413 1447 int err = EINVAL;
1414 1448 int i, j;
1415 1449 char newpath[MAXPATHLEN];
1416 1450 char *pathp;
1417 1451
1418 1452 /*
1419 1453 * First find the dataset name
1420 1454 */
1421 1455 if ((group = sa_get_parent_group(share)) == NULL) {
1422 1456 return (EINVAL);
1423 1457 }
1424 1458 if ((sahandle = sa_find_group_handle(group)) == NULL) {
1425 1459 return (EINVAL);
1426 1460 }
1427 1461
1428 1462 /*
1429 1463 * If get_zfs_dataset fails, see if it is a subdirectory
1430 1464 */
1431 1465
1432 1466 pathp = path;
1433 1467 while ((dataset = get_zfs_dataset(sahandle, pathp, B_TRUE)) == NULL) {
1434 1468 char *p;
1435 1469
1436 1470 if (pathp == path) {
1437 1471 (void) strlcpy(newpath, path, sizeof (newpath));
1438 1472 pathp = newpath;
1439 1473 }
1440 1474
1441 1475 /*
1442 1476 * Make sure only one leading '/' This condition came
1443 1477 * about when using HAStoragePlus which insisted on
1444 1478 * putting an extra leading '/' in the ZFS path
1445 1479 * name. The problem is fixed in other areas, but this
1446 1480 * will catch any other ways that a double slash might
1447 1481 * get introduced.
1448 1482 */
1449 1483 while (*pathp == '/' && *(pathp + 1) == '/')
1450 1484 pathp++;
1451 1485
1452 1486 /*
1453 1487 * chop off part of path, but if we are at root then
1454 1488 * make sure path is a /
1455 1489 */
1456 1490 if ((strlen(pathp) > 1) && (p = strrchr(pathp, '/'))) {
1457 1491 if (pathp == p) {
1458 1492 *(p + 1) = '\0'; /* skip over /, root case */
1459 1493 } else {
1460 1494 *p = '\0';
1461 1495 }
1462 1496 } else {
1463 1497 return (EINVAL);
1464 1498 }
1465 1499 }
1466 1500
1467 1501 libhandle = libzfs_init();
1468 1502 if (libhandle != NULL) {
1469 1503 char *resource_name;
1470 1504
1471 1505 i = (sh->sh_path ? strlen(sh->sh_path) : 0);
1472 1506 sh->sh_size = i;
1473 1507
1474 1508 j = (sh->sh_res ? strlen(sh->sh_res) : 0);
1475 1509 sh->sh_size += j;
1476 1510 SMAX(i, j);
1477 1511
1478 1512 j = (sh->sh_fstype ? strlen(sh->sh_fstype) : 0);
1479 1513 sh->sh_size += j;
1480 1514 SMAX(i, j);
1481 1515
1482 1516 j = (sh->sh_opts ? strlen(sh->sh_opts) : 0);
1483 1517 sh->sh_size += j;
1484 1518 SMAX(i, j);
1485 1519
1486 1520 j = (sh->sh_descr ? strlen(sh->sh_descr) : 0);
1487 1521 sh->sh_size += j;
1488 1522 SMAX(i, j);
1489 1523
1490 1524 resource_name = sa_get_resource_attr(resource, "name");
1491 1525
1492 1526 err = zfs_deleg_share_nfs(libhandle, dataset, path,
1493 1527 resource_name, exportdata, sh, i, operation);
1494 1528 if (err == SA_OK)
1495 1529 sa_update_sharetab_ts(sahandle);
1496 1530 else
1497 1531 err = errno;
1498 1532 if (resource_name)
1499 1533 sa_free_attr_string(resource_name);
1500 1534
1501 1535 libzfs_fini(libhandle);
1502 1536 }
1503 1537 free(dataset);
1504 1538 return (err);
1505 1539 }
1506 1540
1507 1541 /*
1508 1542 * sa_get_zfs_handle(handle)
1509 1543 *
1510 1544 * Given an sa_handle_t, return the libzfs_handle_t *. This is only
1511 1545 * used internally by libzfs. Needed in order to avoid including
1512 1546 * libshare_impl.h in libzfs.
1513 1547 */
1514 1548
1515 1549 libzfs_handle_t *
1516 1550 sa_get_zfs_handle(sa_handle_t handle)
1517 1551 {
1518 1552 sa_handle_impl_t implhandle = (sa_handle_impl_t)handle;
1519 1553
1520 1554 return (implhandle->zfs_libhandle);
1521 1555 }
1522 1556
1523 1557 /*
1524 1558 * sa_get_zfs_info(libzfs, path, mountpoint, dataset)
1525 1559 *
1526 1560 * Find the ZFS dataset and mountpoint for a given path
1527 1561 */
1528 1562 int
1529 1563 sa_zfs_get_info(libzfs_handle_t *libzfs, char *path, char *mountpointp,
1530 1564 char *datasetp)
1531 1565 {
1532 1566 get_all_cbdata_t cb = { 0 };
1533 1567 int i;
1534 1568 char mountpoint[ZFS_MAXPROPLEN];
1535 1569 char dataset[ZFS_MAXPROPLEN];
1536 1570 char canmount[ZFS_MAXPROPLEN];
1537 1571 char *dp;
1538 1572 int count;
1539 1573 int ret = 0;
1540 1574
1541 1575 cb.cb_types = ZFS_TYPE_FILESYSTEM;
1542 1576
1543 1577 if (libzfs == NULL)
1544 1578 return (0);
1545 1579
1546 1580 (void) zfs_iter_root(libzfs, get_one_filesystem, &cb);
1547 1581 count = cb.cb_used;
1548 1582
1549 1583 qsort(cb.cb_handles, count, sizeof (void *), mountpoint_compare);
1550 1584 for (i = 0; i < count; i++) {
1551 1585 /* must have a mountpoint */
1552 1586 if (zfs_prop_get(cb.cb_handles[i], ZFS_PROP_MOUNTPOINT,
1553 1587 mountpoint, sizeof (mountpoint),
1554 1588 NULL, NULL, 0, B_FALSE) != 0) {
1555 1589 /* no mountpoint */
1556 1590 continue;
1557 1591 }
1558 1592
1559 1593 /* mountpoint must be a path */
1560 1594 if (strcmp(mountpoint, ZFS_MOUNTPOINT_NONE) == 0 ||
1561 1595 strcmp(mountpoint, ZFS_MOUNTPOINT_LEGACY) == 0) {
1562 1596 /*
1563 1597 * Search mmttab for mountpoint
1564 1598 */
1565 1599
1566 1600 if (get_legacy_mountpoint(path, dataset,
1567 1601 ZFS_MAXPROPLEN, mountpoint,
1568 1602 ZFS_MAXPROPLEN) == 0) {
1569 1603 ret = 1;
1570 1604 break;
1571 1605 }
1572 1606 continue;
1573 1607 }
1574 1608
1575 1609 /* canmount must be set */
1576 1610 canmount[0] = '\0';
1577 1611 if (zfs_prop_get(cb.cb_handles[i], ZFS_PROP_CANMOUNT, canmount,
1578 1612 sizeof (canmount), NULL, NULL, 0, B_FALSE) != 0 ||
1579 1613 strcmp(canmount, "off") == 0)
1580 1614 continue;
1581 1615
1582 1616 /*
1583 1617 * have a mountable handle but want to skip those marked none
1584 1618 * and legacy
1585 1619 */
1586 1620 if (strcmp(mountpoint, path) == 0) {
1587 1621 dp = (char *)zfs_get_name(cb.cb_handles[i]);
1588 1622 if (dp != NULL) {
1589 1623 if (datasetp != NULL)
1590 1624 (void) strcpy(datasetp, dp);
1591 1625 if (mountpointp != NULL)
1592 1626 (void) strcpy(mountpointp, mountpoint);
1593 1627 ret = 1;
1594 1628 }
1595 1629 break;
1596 1630 }
1597 1631
1598 1632 }
1599 1633
1600 1634 return (ret);
1601 1635 }
1602 1636
1603 1637 /*
1604 1638 * This method builds values for "sharesmb" property from the
1605 1639 * nvlist argument. The values are returned in sharesmb_val variable.
1606 1640 */
1607 1641 static int
1608 1642 sa_zfs_sprintf_new_prop(nvlist_t *nvl, char *sharesmb_val)
1609 1643 {
1610 1644 char cur_val[MAXPATHLEN];
1611 1645 char *name, *val;
1612 1646 nvpair_t *cur;
1613 1647 int err = 0;
1614 1648
1615 1649 cur = nvlist_next_nvpair(nvl, NULL);
1616 1650 while (cur != NULL) {
1617 1651 name = nvpair_name(cur);
1618 1652 err = nvpair_value_string(cur, &val);
1619 1653 if ((err != 0) || (name == NULL) || (val == NULL))
1620 1654 return (-1);
1621 1655
1622 1656 (void) snprintf(cur_val, MAXPATHLEN, "%s=%s,", name, val);
1623 1657 (void) strlcat(sharesmb_val, cur_val, MAXPATHLEN);
1624 1658
1625 1659 cur = nvlist_next_nvpair(nvl, cur);
1626 1660 }
1627 1661
1628 1662 return (0);
1629 1663 }
1630 1664
1631 1665 /*
1632 1666 * This method builds values for "sharesmb" property from values
1633 1667 * already existing on the share. The properties set via sa_zfs_sprint_new_prop
1634 1668 * method are passed in sharesmb_val. If a existing property is already
1635 1669 * set via sa_zfs_sprint_new_prop method, then they are not appended
1636 1670 * to the sharesmb_val string. The returned sharesmb_val string is a combination
1637 1671 * of new and existing values for 'sharesmb' property.
1638 1672 */
1639 1673 static int
1640 1674 sa_zfs_sprintf_existing_prop(zfs_handle_t *handle, char *sharesmb_val)
1641 1675 {
1642 1676 char shareopts[ZFS_MAXPROPLEN], cur_val[MAXPATHLEN];
1643 1677 char *token, *last, *value;
1644 1678
1645 1679 if (zfs_prop_get(handle, ZFS_PROP_SHARESMB, shareopts,
1646 1680 sizeof (shareopts), NULL, NULL, 0, B_FALSE) != 0)
1647 1681 return (-1);
1648 1682
1649 1683 if (strstr(shareopts, "=") == NULL)
1650 1684 return (0);
1651 1685
1652 1686 for (token = strtok_r(shareopts, ",", &last); token != NULL;
1653 1687 token = strtok_r(NULL, ",", &last)) {
1654 1688 value = strchr(token, '=');
1655 1689 if (value == NULL)
1656 1690 return (-1);
1657 1691 *value++ = '\0';
1658 1692
1659 1693 (void) snprintf(cur_val, MAXPATHLEN, "%s=", token);
1660 1694 if (strstr(sharesmb_val, cur_val) == NULL) {
1661 1695 (void) strlcat(cur_val, value, MAXPATHLEN);
1662 1696 (void) strlcat(cur_val, ",", MAXPATHLEN);
1663 1697 (void) strlcat(sharesmb_val, cur_val, MAXPATHLEN);
1664 1698 }
1665 1699 }
1666 1700
1667 1701 return (0);
1668 1702 }
1669 1703
1670 1704 /*
1671 1705 * Sets the share properties on a ZFS share. For now, this method sets only
1672 1706 * the "sharesmb" property.
1673 1707 *
1674 1708 * This method includes building a comma seperated name-value string to be
1675 1709 * set on the "sharesmb" property of a ZFS share. This name-value string is
1676 1710 * build in 2 steps:
1677 1711 * - New property values given as name-value pair are set first.
1678 1712 * - Existing optionset properties, which are not part of the new properties
1679 1713 * passed in step 1, are appended to the newly set properties.
1680 1714 */
1681 1715 int
1682 1716 sa_zfs_setprop(sa_handle_t handle, char *path, nvlist_t *nvl)
1683 1717 {
1684 1718 zfs_handle_t *z_fs;
1685 1719 libzfs_handle_t *z_lib;
1686 1720 char sharesmb_val[MAXPATHLEN];
1687 1721 char *dataset, *lastcomma;
1688 1722
1689 1723 if (nvlist_empty(nvl))
1690 1724 return (0);
1691 1725
1692 1726 if ((handle == NULL) || (path == NULL))
1693 1727 return (-1);
1694 1728
1695 1729 if ((dataset = get_zfs_dataset(handle, path, B_FALSE)) == NULL)
1696 1730 return (-1);
1697 1731
1698 1732 if ((z_lib = libzfs_init()) == NULL) {
1699 1733 free(dataset);
1700 1734 return (-1);
1701 1735 }
1702 1736
1703 1737 z_fs = zfs_open(z_lib, dataset, ZFS_TYPE_DATASET);
1704 1738 if (z_fs == NULL) {
1705 1739 free(dataset);
1706 1740 libzfs_fini(z_lib);
1707 1741 return (-1);
1708 1742 }
1709 1743
1710 1744 bzero(sharesmb_val, MAXPATHLEN);
1711 1745 if (sa_zfs_sprintf_new_prop(nvl, sharesmb_val) != 0) {
1712 1746 free(dataset);
1713 1747 zfs_close(z_fs);
1714 1748 libzfs_fini(z_lib);
1715 1749 return (-1);
1716 1750 }
1717 1751
1718 1752 if (sa_zfs_sprintf_existing_prop(z_fs, sharesmb_val) != 0) {
1719 1753 free(dataset);
1720 1754 zfs_close(z_fs);
1721 1755 libzfs_fini(z_lib);
1722 1756 return (-1);
1723 1757 }
1724 1758
1725 1759 lastcomma = strrchr(sharesmb_val, ',');
1726 1760 if ((lastcomma != NULL) && (lastcomma[1] == '\0'))
1727 1761 *lastcomma = '\0';
1728 1762
1729 1763 (void) zfs_prop_set(z_fs, zfs_prop_to_name(ZFS_PROP_SHARESMB),
1730 1764 sharesmb_val);
1731 1765 free(dataset);
1732 1766 zfs_close(z_fs);
1733 1767 libzfs_fini(z_lib);
1734 1768
1735 1769 return (0);
1736 1770 }
|
↓ open down ↓ |
749 lines elided |
↑ open up ↑ |
XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX