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