]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | ||
22 | /* | |
572e2857 | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
d22f3a82 | 24 | * Copyright (c) 2018, Joyent, Inc. All rights reserved. |
5b72a38d | 25 | * Copyright (c) 2011, 2017 by Delphix. All rights reserved. |
08b1b21d | 26 | * Copyright (c) 2012 DEY Storage Systems, Inc. All rights reserved. |
0cee2406 | 27 | * Copyright (c) 2012 Pawel Jakub Dawidek <pawel@dawidek.net>. |
b1118acb | 28 | * Copyright (c) 2013 Martin Matuska. All rights reserved. |
95fd54a1 | 29 | * Copyright (c) 2013 Steven Hartland. All rights reserved. |
bcb1a8a2 | 30 | * Copyright 2017 Nexenta Systems, Inc. |
23d70cde | 31 | * Copyright 2016 Igor Kozhukhov <ikozhukhov@gmail.com> |
84ddd4b0 | 32 | * Copyright 2017-2018 RackTop Systems. |
50a343d8 | 33 | * Copyright (c) 2018 Datto Inc. |
34dc7c2f BB |
34 | */ |
35 | ||
34dc7c2f BB |
36 | #include <ctype.h> |
37 | #include <errno.h> | |
34dc7c2f | 38 | #include <libintl.h> |
34dc7c2f BB |
39 | #include <stdio.h> |
40 | #include <stdlib.h> | |
41 | #include <strings.h> | |
42 | #include <unistd.h> | |
43 | #include <stddef.h> | |
44 | #include <zone.h> | |
45 | #include <fcntl.h> | |
46 | #include <sys/mntent.h> | |
34dc7c2f | 47 | #include <sys/mount.h> |
34dc7c2f BB |
48 | #include <pwd.h> |
49 | #include <grp.h> | |
50 | #include <stddef.h> | |
51 | #include <ucred.h> | |
be160928 | 52 | #ifdef HAVE_IDMAP |
9babb374 BB |
53 | #include <idmap.h> |
54 | #include <aclutils.h> | |
45d1cae3 | 55 | #include <directory.h> |
be160928 | 56 | #endif /* HAVE_IDMAP */ |
34dc7c2f | 57 | |
428870ff | 58 | #include <sys/dnode.h> |
34dc7c2f BB |
59 | #include <sys/spa.h> |
60 | #include <sys/zap.h> | |
b5256303 | 61 | #include <sys/dsl_crypt.h> |
34dc7c2f | 62 | #include <libzfs.h> |
e89f1295 | 63 | #include <libzutil.h> |
34dc7c2f BB |
64 | |
65 | #include "zfs_namecheck.h" | |
66 | #include "zfs_prop.h" | |
67 | #include "libzfs_impl.h" | |
6044cf59 | 68 | #include "libzfs.h" |
34dc7c2f BB |
69 | #include "zfs_deleg.h" |
70 | ||
9babb374 BB |
71 | static int userquota_propname_decode(const char *propname, boolean_t zoned, |
72 | zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp); | |
34dc7c2f BB |
73 | |
74 | /* | |
75 | * Given a single type (not a mask of types), return the type in a human | |
76 | * readable form. | |
77 | */ | |
78 | const char * | |
79 | zfs_type_to_name(zfs_type_t type) | |
80 | { | |
81 | switch (type) { | |
82 | case ZFS_TYPE_FILESYSTEM: | |
83 | return (dgettext(TEXT_DOMAIN, "filesystem")); | |
84 | case ZFS_TYPE_SNAPSHOT: | |
85 | return (dgettext(TEXT_DOMAIN, "snapshot")); | |
86 | case ZFS_TYPE_VOLUME: | |
87 | return (dgettext(TEXT_DOMAIN, "volume")); | |
23d70cde GM |
88 | case ZFS_TYPE_POOL: |
89 | return (dgettext(TEXT_DOMAIN, "pool")); | |
90 | case ZFS_TYPE_BOOKMARK: | |
91 | return (dgettext(TEXT_DOMAIN, "bookmark")); | |
e75c13c3 | 92 | default: |
23d70cde | 93 | assert(!"unhandled zfs_type_t"); |
34dc7c2f BB |
94 | } |
95 | ||
96 | return (NULL); | |
97 | } | |
98 | ||
34dc7c2f BB |
99 | /* |
100 | * Validate a ZFS path. This is used even before trying to open the dataset, to | |
9babb374 BB |
101 | * provide a more meaningful error message. We call zfs_error_aux() to |
102 | * explain exactly why the name was not valid. | |
34dc7c2f | 103 | */ |
572e2857 | 104 | int |
34dc7c2f BB |
105 | zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type, |
106 | boolean_t modifying) | |
107 | { | |
108 | namecheck_err_t why; | |
109 | char what; | |
110 | ||
aeacdefe | 111 | if (entity_namecheck(path, &why, &what) != 0) { |
34dc7c2f BB |
112 | if (hdl != NULL) { |
113 | switch (why) { | |
114 | case NAME_ERR_TOOLONG: | |
115 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
116 | "name is too long")); | |
117 | break; | |
118 | ||
119 | case NAME_ERR_LEADING_SLASH: | |
120 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
121 | "leading slash in name")); | |
122 | break; | |
123 | ||
124 | case NAME_ERR_EMPTY_COMPONENT: | |
125 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
126 | "empty component in name")); | |
127 | break; | |
128 | ||
129 | case NAME_ERR_TRAILING_SLASH: | |
130 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
131 | "trailing slash in name")); | |
132 | break; | |
133 | ||
134 | case NAME_ERR_INVALCHAR: | |
135 | zfs_error_aux(hdl, | |
136 | dgettext(TEXT_DOMAIN, "invalid character " | |
137 | "'%c' in name"), what); | |
138 | break; | |
139 | ||
aeacdefe | 140 | case NAME_ERR_MULTIPLE_DELIMITERS: |
34dc7c2f | 141 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
aeacdefe GM |
142 | "multiple '@' and/or '#' delimiters in " |
143 | "name")); | |
34dc7c2f BB |
144 | break; |
145 | ||
146 | case NAME_ERR_NOLETTER: | |
147 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
148 | "pool doesn't begin with a letter")); | |
149 | break; | |
150 | ||
151 | case NAME_ERR_RESERVED: | |
152 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
153 | "name is reserved")); | |
154 | break; | |
155 | ||
156 | case NAME_ERR_DISKLIKE: | |
157 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
158 | "reserved disk name")); | |
159 | break; | |
23d70cde | 160 | |
e75c13c3 | 161 | default: |
23d70cde GM |
162 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
163 | "(%d) not defined"), why); | |
e75c13c3 | 164 | break; |
34dc7c2f BB |
165 | } |
166 | } | |
167 | ||
168 | return (0); | |
169 | } | |
170 | ||
171 | if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { | |
172 | if (hdl != NULL) | |
173 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
aeacdefe | 174 | "snapshot delimiter '@' is not expected here")); |
34dc7c2f BB |
175 | return (0); |
176 | } | |
177 | ||
178 | if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { | |
179 | if (hdl != NULL) | |
180 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
9042f603 MI |
181 | "missing '@' delimiter in snapshot name, " |
182 | "did you mean to use -r?")); | |
34dc7c2f BB |
183 | return (0); |
184 | } | |
185 | ||
aeacdefe GM |
186 | if (!(type & ZFS_TYPE_BOOKMARK) && strchr(path, '#') != NULL) { |
187 | if (hdl != NULL) | |
188 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
189 | "bookmark delimiter '#' is not expected here")); | |
190 | return (0); | |
191 | } | |
192 | ||
193 | if (type == ZFS_TYPE_BOOKMARK && strchr(path, '#') == NULL) { | |
194 | if (hdl != NULL) | |
195 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
9042f603 MI |
196 | "missing '#' delimiter in bookmark name, " |
197 | "did you mean to use -r?")); | |
aeacdefe GM |
198 | return (0); |
199 | } | |
200 | ||
34dc7c2f BB |
201 | if (modifying && strchr(path, '%') != NULL) { |
202 | if (hdl != NULL) | |
203 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
204 | "invalid character %c in name"), '%'); | |
205 | return (0); | |
206 | } | |
207 | ||
208 | return (-1); | |
209 | } | |
210 | ||
211 | int | |
212 | zfs_name_valid(const char *name, zfs_type_t type) | |
213 | { | |
214 | if (type == ZFS_TYPE_POOL) | |
215 | return (zpool_name_valid(NULL, B_FALSE, name)); | |
216 | return (zfs_validate_name(NULL, name, type, B_FALSE)); | |
217 | } | |
218 | ||
219 | /* | |
220 | * This function takes the raw DSL properties, and filters out the user-defined | |
221 | * properties into a separate nvlist. | |
222 | */ | |
223 | static nvlist_t * | |
224 | process_user_props(zfs_handle_t *zhp, nvlist_t *props) | |
225 | { | |
226 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
227 | nvpair_t *elem; | |
228 | nvlist_t *propval; | |
229 | nvlist_t *nvl; | |
230 | ||
231 | if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) { | |
232 | (void) no_memory(hdl); | |
233 | return (NULL); | |
234 | } | |
235 | ||
236 | elem = NULL; | |
237 | while ((elem = nvlist_next_nvpair(props, elem)) != NULL) { | |
238 | if (!zfs_prop_user(nvpair_name(elem))) | |
239 | continue; | |
240 | ||
241 | verify(nvpair_value_nvlist(elem, &propval) == 0); | |
242 | if (nvlist_add_nvlist(nvl, nvpair_name(elem), propval) != 0) { | |
243 | nvlist_free(nvl); | |
244 | (void) no_memory(hdl); | |
245 | return (NULL); | |
246 | } | |
247 | } | |
248 | ||
249 | return (nvl); | |
250 | } | |
251 | ||
b128c09f BB |
252 | static zpool_handle_t * |
253 | zpool_add_handle(zfs_handle_t *zhp, const char *pool_name) | |
254 | { | |
255 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
256 | zpool_handle_t *zph; | |
257 | ||
258 | if ((zph = zpool_open_canfail(hdl, pool_name)) != NULL) { | |
259 | if (hdl->libzfs_pool_handles != NULL) | |
260 | zph->zpool_next = hdl->libzfs_pool_handles; | |
261 | hdl->libzfs_pool_handles = zph; | |
262 | } | |
263 | return (zph); | |
264 | } | |
265 | ||
266 | static zpool_handle_t * | |
267 | zpool_find_handle(zfs_handle_t *zhp, const char *pool_name, int len) | |
268 | { | |
269 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
270 | zpool_handle_t *zph = hdl->libzfs_pool_handles; | |
271 | ||
272 | while ((zph != NULL) && | |
273 | (strncmp(pool_name, zpool_get_name(zph), len) != 0)) | |
274 | zph = zph->zpool_next; | |
275 | return (zph); | |
276 | } | |
277 | ||
278 | /* | |
279 | * Returns a handle to the pool that contains the provided dataset. | |
280 | * If a handle to that pool already exists then that handle is returned. | |
281 | * Otherwise, a new handle is created and added to the list of handles. | |
282 | */ | |
283 | static zpool_handle_t * | |
284 | zpool_handle(zfs_handle_t *zhp) | |
285 | { | |
286 | char *pool_name; | |
287 | int len; | |
288 | zpool_handle_t *zph; | |
289 | ||
da536844 | 290 | len = strcspn(zhp->zfs_name, "/@#") + 1; |
b128c09f BB |
291 | pool_name = zfs_alloc(zhp->zfs_hdl, len); |
292 | (void) strlcpy(pool_name, zhp->zfs_name, len); | |
293 | ||
294 | zph = zpool_find_handle(zhp, pool_name, len); | |
295 | if (zph == NULL) | |
296 | zph = zpool_add_handle(zhp, pool_name); | |
297 | ||
298 | free(pool_name); | |
299 | return (zph); | |
300 | } | |
301 | ||
302 | void | |
303 | zpool_free_handles(libzfs_handle_t *hdl) | |
304 | { | |
305 | zpool_handle_t *next, *zph = hdl->libzfs_pool_handles; | |
306 | ||
307 | while (zph != NULL) { | |
308 | next = zph->zpool_next; | |
309 | zpool_close(zph); | |
310 | zph = next; | |
311 | } | |
312 | hdl->libzfs_pool_handles = NULL; | |
313 | } | |
314 | ||
34dc7c2f BB |
315 | /* |
316 | * Utility function to gather stats (objset and zpl) for the given object. | |
317 | */ | |
318 | static int | |
fb5f0bc8 | 319 | get_stats_ioctl(zfs_handle_t *zhp, zfs_cmd_t *zc) |
34dc7c2f | 320 | { |
34dc7c2f | 321 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
34dc7c2f | 322 | |
fb5f0bc8 | 323 | (void) strlcpy(zc->zc_name, zhp->zfs_name, sizeof (zc->zc_name)); |
34dc7c2f | 324 | |
fb5f0bc8 | 325 | while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, zc) != 0) { |
34dc7c2f | 326 | if (errno == ENOMEM) { |
fb5f0bc8 | 327 | if (zcmd_expand_dst_nvlist(hdl, zc) != 0) { |
34dc7c2f BB |
328 | return (-1); |
329 | } | |
330 | } else { | |
34dc7c2f BB |
331 | return (-1); |
332 | } | |
333 | } | |
fb5f0bc8 BB |
334 | return (0); |
335 | } | |
34dc7c2f | 336 | |
428870ff BB |
337 | /* |
338 | * Utility function to get the received properties of the given object. | |
339 | */ | |
340 | static int | |
341 | get_recvd_props_ioctl(zfs_handle_t *zhp) | |
342 | { | |
343 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
344 | nvlist_t *recvdprops; | |
13fe0198 | 345 | zfs_cmd_t zc = {"\0"}; |
428870ff BB |
346 | int err; |
347 | ||
348 | if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) | |
349 | return (-1); | |
350 | ||
351 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
352 | ||
353 | while (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_RECVD_PROPS, &zc) != 0) { | |
354 | if (errno == ENOMEM) { | |
355 | if (zcmd_expand_dst_nvlist(hdl, &zc) != 0) { | |
356 | return (-1); | |
357 | } | |
358 | } else { | |
359 | zcmd_free_nvlists(&zc); | |
360 | return (-1); | |
361 | } | |
362 | } | |
363 | ||
364 | err = zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &recvdprops); | |
365 | zcmd_free_nvlists(&zc); | |
366 | if (err != 0) | |
367 | return (-1); | |
368 | ||
369 | nvlist_free(zhp->zfs_recvd_props); | |
370 | zhp->zfs_recvd_props = recvdprops; | |
371 | ||
372 | return (0); | |
373 | } | |
374 | ||
fb5f0bc8 BB |
375 | static int |
376 | put_stats_zhdl(zfs_handle_t *zhp, zfs_cmd_t *zc) | |
377 | { | |
378 | nvlist_t *allprops, *userprops; | |
34dc7c2f | 379 | |
fb5f0bc8 BB |
380 | zhp->zfs_dmustats = zc->zc_objset_stats; /* structure assignment */ |
381 | ||
382 | if (zcmd_read_dst_nvlist(zhp->zfs_hdl, zc, &allprops) != 0) { | |
34dc7c2f BB |
383 | return (-1); |
384 | } | |
385 | ||
9babb374 BB |
386 | /* |
387 | * XXX Why do we store the user props separately, in addition to | |
388 | * storing them in zfs_props? | |
389 | */ | |
34dc7c2f BB |
390 | if ((userprops = process_user_props(zhp, allprops)) == NULL) { |
391 | nvlist_free(allprops); | |
392 | return (-1); | |
393 | } | |
394 | ||
395 | nvlist_free(zhp->zfs_props); | |
396 | nvlist_free(zhp->zfs_user_props); | |
397 | ||
398 | zhp->zfs_props = allprops; | |
399 | zhp->zfs_user_props = userprops; | |
400 | ||
401 | return (0); | |
402 | } | |
403 | ||
fb5f0bc8 BB |
404 | static int |
405 | get_stats(zfs_handle_t *zhp) | |
406 | { | |
407 | int rc = 0; | |
13fe0198 | 408 | zfs_cmd_t zc = {"\0"}; |
fb5f0bc8 BB |
409 | |
410 | if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) | |
411 | return (-1); | |
412 | if (get_stats_ioctl(zhp, &zc) != 0) | |
413 | rc = -1; | |
414 | else if (put_stats_zhdl(zhp, &zc) != 0) | |
415 | rc = -1; | |
416 | zcmd_free_nvlists(&zc); | |
417 | return (rc); | |
418 | } | |
419 | ||
34dc7c2f BB |
420 | /* |
421 | * Refresh the properties currently stored in the handle. | |
422 | */ | |
423 | void | |
424 | zfs_refresh_properties(zfs_handle_t *zhp) | |
425 | { | |
426 | (void) get_stats(zhp); | |
427 | } | |
428 | ||
429 | /* | |
430 | * Makes a handle from the given dataset name. Used by zfs_open() and | |
431 | * zfs_iter_* to create child handles on the fly. | |
432 | */ | |
fb5f0bc8 BB |
433 | static int |
434 | make_dataset_handle_common(zfs_handle_t *zhp, zfs_cmd_t *zc) | |
34dc7c2f | 435 | { |
428870ff | 436 | if (put_stats_zhdl(zhp, zc) != 0) |
fb5f0bc8 | 437 | return (-1); |
34dc7c2f BB |
438 | |
439 | /* | |
440 | * We've managed to open the dataset and gather statistics. Determine | |
441 | * the high-level type. | |
442 | */ | |
443 | if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) | |
444 | zhp->zfs_head_type = ZFS_TYPE_VOLUME; | |
445 | else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) | |
446 | zhp->zfs_head_type = ZFS_TYPE_FILESYSTEM; | |
ff998d80 | 447 | else if (zhp->zfs_dmustats.dds_type == DMU_OST_OTHER) |
ba6a2402 | 448 | return (-1); |
34dc7c2f BB |
449 | else |
450 | abort(); | |
451 | ||
452 | if (zhp->zfs_dmustats.dds_is_snapshot) | |
453 | zhp->zfs_type = ZFS_TYPE_SNAPSHOT; | |
454 | else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) | |
455 | zhp->zfs_type = ZFS_TYPE_VOLUME; | |
456 | else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) | |
457 | zhp->zfs_type = ZFS_TYPE_FILESYSTEM; | |
458 | else | |
459 | abort(); /* we should never see any other types */ | |
460 | ||
428870ff BB |
461 | if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) |
462 | return (-1); | |
463 | ||
fb5f0bc8 BB |
464 | return (0); |
465 | } | |
466 | ||
467 | zfs_handle_t * | |
468 | make_dataset_handle(libzfs_handle_t *hdl, const char *path) | |
469 | { | |
13fe0198 | 470 | zfs_cmd_t zc = {"\0"}; |
fb5f0bc8 | 471 | |
8111eb4a | 472 | zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); |
fb5f0bc8 BB |
473 | |
474 | if (zhp == NULL) | |
475 | return (NULL); | |
476 | ||
477 | zhp->zfs_hdl = hdl; | |
478 | (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); | |
479 | if (zcmd_alloc_dst_nvlist(hdl, &zc, 0) != 0) { | |
480 | free(zhp); | |
481 | return (NULL); | |
482 | } | |
483 | if (get_stats_ioctl(zhp, &zc) == -1) { | |
484 | zcmd_free_nvlists(&zc); | |
485 | free(zhp); | |
486 | return (NULL); | |
487 | } | |
488 | if (make_dataset_handle_common(zhp, &zc) == -1) { | |
489 | free(zhp); | |
490 | zhp = NULL; | |
491 | } | |
492 | zcmd_free_nvlists(&zc); | |
493 | return (zhp); | |
494 | } | |
495 | ||
330d06f9 | 496 | zfs_handle_t * |
fb5f0bc8 BB |
497 | make_dataset_handle_zc(libzfs_handle_t *hdl, zfs_cmd_t *zc) |
498 | { | |
8111eb4a | 499 | zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); |
fb5f0bc8 BB |
500 | |
501 | if (zhp == NULL) | |
502 | return (NULL); | |
503 | ||
504 | zhp->zfs_hdl = hdl; | |
505 | (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); | |
506 | if (make_dataset_handle_common(zhp, zc) == -1) { | |
507 | free(zhp); | |
508 | return (NULL); | |
509 | } | |
34dc7c2f BB |
510 | return (zhp); |
511 | } | |
512 | ||
330d06f9 | 513 | zfs_handle_t * |
0cee2406 PJD |
514 | make_dataset_simple_handle_zc(zfs_handle_t *pzhp, zfs_cmd_t *zc) |
515 | { | |
8111eb4a | 516 | zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); |
0cee2406 PJD |
517 | |
518 | if (zhp == NULL) | |
519 | return (NULL); | |
520 | ||
521 | zhp->zfs_hdl = pzhp->zfs_hdl; | |
522 | (void) strlcpy(zhp->zfs_name, zc->zc_name, sizeof (zhp->zfs_name)); | |
523 | zhp->zfs_head_type = pzhp->zfs_type; | |
524 | zhp->zfs_type = ZFS_TYPE_SNAPSHOT; | |
525 | zhp->zpool_hdl = zpool_handle(zhp); | |
526 | ||
527 | return (zhp); | |
528 | } | |
529 | ||
330d06f9 MA |
530 | zfs_handle_t * |
531 | zfs_handle_dup(zfs_handle_t *zhp_orig) | |
532 | { | |
8111eb4a | 533 | zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); |
330d06f9 MA |
534 | |
535 | if (zhp == NULL) | |
536 | return (NULL); | |
537 | ||
538 | zhp->zfs_hdl = zhp_orig->zfs_hdl; | |
539 | zhp->zpool_hdl = zhp_orig->zpool_hdl; | |
540 | (void) strlcpy(zhp->zfs_name, zhp_orig->zfs_name, | |
541 | sizeof (zhp->zfs_name)); | |
542 | zhp->zfs_type = zhp_orig->zfs_type; | |
543 | zhp->zfs_head_type = zhp_orig->zfs_head_type; | |
544 | zhp->zfs_dmustats = zhp_orig->zfs_dmustats; | |
545 | if (zhp_orig->zfs_props != NULL) { | |
546 | if (nvlist_dup(zhp_orig->zfs_props, &zhp->zfs_props, 0) != 0) { | |
547 | (void) no_memory(zhp->zfs_hdl); | |
548 | zfs_close(zhp); | |
549 | return (NULL); | |
550 | } | |
551 | } | |
552 | if (zhp_orig->zfs_user_props != NULL) { | |
553 | if (nvlist_dup(zhp_orig->zfs_user_props, | |
554 | &zhp->zfs_user_props, 0) != 0) { | |
555 | (void) no_memory(zhp->zfs_hdl); | |
556 | zfs_close(zhp); | |
557 | return (NULL); | |
558 | } | |
559 | } | |
560 | if (zhp_orig->zfs_recvd_props != NULL) { | |
561 | if (nvlist_dup(zhp_orig->zfs_recvd_props, | |
562 | &zhp->zfs_recvd_props, 0)) { | |
563 | (void) no_memory(zhp->zfs_hdl); | |
564 | zfs_close(zhp); | |
565 | return (NULL); | |
566 | } | |
567 | } | |
568 | zhp->zfs_mntcheck = zhp_orig->zfs_mntcheck; | |
569 | if (zhp_orig->zfs_mntopts != NULL) { | |
570 | zhp->zfs_mntopts = zfs_strdup(zhp_orig->zfs_hdl, | |
571 | zhp_orig->zfs_mntopts); | |
572 | } | |
573 | zhp->zfs_props_table = zhp_orig->zfs_props_table; | |
574 | return (zhp); | |
575 | } | |
576 | ||
da536844 MA |
577 | boolean_t |
578 | zfs_bookmark_exists(const char *path) | |
579 | { | |
580 | nvlist_t *bmarks; | |
581 | nvlist_t *props; | |
eca7b760 | 582 | char fsname[ZFS_MAX_DATASET_NAME_LEN]; |
da536844 MA |
583 | char *bmark_name; |
584 | char *pound; | |
585 | int err; | |
586 | boolean_t rv; | |
587 | ||
588 | ||
589 | (void) strlcpy(fsname, path, sizeof (fsname)); | |
590 | pound = strchr(fsname, '#'); | |
591 | if (pound == NULL) | |
592 | return (B_FALSE); | |
593 | ||
594 | *pound = '\0'; | |
595 | bmark_name = pound + 1; | |
596 | props = fnvlist_alloc(); | |
597 | err = lzc_get_bookmarks(fsname, props, &bmarks); | |
598 | nvlist_free(props); | |
599 | if (err != 0) { | |
600 | nvlist_free(bmarks); | |
601 | return (B_FALSE); | |
602 | } | |
603 | ||
604 | rv = nvlist_exists(bmarks, bmark_name); | |
605 | nvlist_free(bmarks); | |
606 | return (rv); | |
607 | } | |
608 | ||
609 | zfs_handle_t * | |
610 | make_bookmark_handle(zfs_handle_t *parent, const char *path, | |
611 | nvlist_t *bmark_props) | |
612 | { | |
8111eb4a | 613 | zfs_handle_t *zhp = calloc(1, sizeof (zfs_handle_t)); |
da536844 MA |
614 | |
615 | if (zhp == NULL) | |
616 | return (NULL); | |
617 | ||
618 | /* Fill in the name. */ | |
619 | zhp->zfs_hdl = parent->zfs_hdl; | |
620 | (void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); | |
621 | ||
622 | /* Set the property lists. */ | |
623 | if (nvlist_dup(bmark_props, &zhp->zfs_props, 0) != 0) { | |
624 | free(zhp); | |
625 | return (NULL); | |
626 | } | |
627 | ||
628 | /* Set the types. */ | |
629 | zhp->zfs_head_type = parent->zfs_head_type; | |
630 | zhp->zfs_type = ZFS_TYPE_BOOKMARK; | |
631 | ||
632 | if ((zhp->zpool_hdl = zpool_handle(zhp)) == NULL) { | |
633 | nvlist_free(zhp->zfs_props); | |
634 | free(zhp); | |
635 | return (NULL); | |
636 | } | |
637 | ||
638 | return (zhp); | |
639 | } | |
640 | ||
aeacdefe GM |
641 | struct zfs_open_bookmarks_cb_data { |
642 | const char *path; | |
643 | zfs_handle_t *zhp; | |
644 | }; | |
645 | ||
646 | static int | |
647 | zfs_open_bookmarks_cb(zfs_handle_t *zhp, void *data) | |
648 | { | |
649 | struct zfs_open_bookmarks_cb_data *dp = data; | |
650 | ||
651 | /* | |
652 | * Is it the one we are looking for? | |
653 | */ | |
654 | if (strcmp(dp->path, zfs_get_name(zhp)) == 0) { | |
655 | /* | |
656 | * We found it. Save it and let the caller know we are done. | |
657 | */ | |
658 | dp->zhp = zhp; | |
659 | return (EEXIST); | |
660 | } | |
661 | ||
662 | /* | |
663 | * Not found. Close the handle and ask for another one. | |
664 | */ | |
665 | zfs_close(zhp); | |
666 | return (0); | |
667 | } | |
668 | ||
34dc7c2f | 669 | /* |
aeacdefe | 670 | * Opens the given snapshot, bookmark, filesystem, or volume. The 'types' |
34dc7c2f BB |
671 | * argument is a mask of acceptable types. The function will print an |
672 | * appropriate error message and return NULL if it can't be opened. | |
673 | */ | |
674 | zfs_handle_t * | |
675 | zfs_open(libzfs_handle_t *hdl, const char *path, int types) | |
676 | { | |
677 | zfs_handle_t *zhp; | |
678 | char errbuf[1024]; | |
aeacdefe | 679 | char *bookp; |
34dc7c2f BB |
680 | |
681 | (void) snprintf(errbuf, sizeof (errbuf), | |
682 | dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); | |
683 | ||
684 | /* | |
685 | * Validate the name before we even try to open it. | |
686 | */ | |
aeacdefe | 687 | if (!zfs_validate_name(hdl, path, types, B_FALSE)) { |
34dc7c2f BB |
688 | (void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); |
689 | return (NULL); | |
690 | } | |
691 | ||
692 | /* | |
aeacdefe | 693 | * Bookmarks needs to be handled separately. |
34dc7c2f | 694 | */ |
aeacdefe GM |
695 | bookp = strchr(path, '#'); |
696 | if (bookp == NULL) { | |
697 | /* | |
698 | * Try to get stats for the dataset, which will tell us if it | |
699 | * exists. | |
700 | */ | |
701 | errno = 0; | |
702 | if ((zhp = make_dataset_handle(hdl, path)) == NULL) { | |
703 | (void) zfs_standard_error(hdl, errno, errbuf); | |
704 | return (NULL); | |
705 | } | |
706 | } else { | |
707 | char dsname[ZFS_MAX_DATASET_NAME_LEN]; | |
708 | zfs_handle_t *pzhp; | |
709 | struct zfs_open_bookmarks_cb_data cb_data = {path, NULL}; | |
710 | ||
711 | /* | |
712 | * We need to cut out '#' and everything after '#' | |
713 | * to get the parent dataset name only. | |
714 | */ | |
715 | assert(bookp - path < sizeof (dsname)); | |
716 | (void) strncpy(dsname, path, bookp - path); | |
717 | dsname[bookp - path] = '\0'; | |
718 | ||
719 | /* | |
720 | * Create handle for the parent dataset. | |
721 | */ | |
722 | errno = 0; | |
723 | if ((pzhp = make_dataset_handle(hdl, dsname)) == NULL) { | |
724 | (void) zfs_standard_error(hdl, errno, errbuf); | |
725 | return (NULL); | |
726 | } | |
727 | ||
728 | /* | |
729 | * Iterate bookmarks to find the right one. | |
730 | */ | |
731 | errno = 0; | |
732 | if ((zfs_iter_bookmarks(pzhp, zfs_open_bookmarks_cb, | |
733 | &cb_data) == 0) && (cb_data.zhp == NULL)) { | |
734 | (void) zfs_error(hdl, EZFS_NOENT, errbuf); | |
735 | zfs_close(pzhp); | |
736 | return (NULL); | |
737 | } | |
738 | if (cb_data.zhp == NULL) { | |
739 | (void) zfs_standard_error(hdl, errno, errbuf); | |
740 | zfs_close(pzhp); | |
741 | return (NULL); | |
742 | } | |
743 | zhp = cb_data.zhp; | |
744 | ||
745 | /* | |
746 | * Cleanup. | |
747 | */ | |
748 | zfs_close(pzhp); | |
34dc7c2f BB |
749 | } |
750 | ||
751 | if (!(types & zhp->zfs_type)) { | |
752 | (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); | |
753 | zfs_close(zhp); | |
754 | return (NULL); | |
755 | } | |
756 | ||
757 | return (zhp); | |
758 | } | |
759 | ||
760 | /* | |
761 | * Release a ZFS handle. Nothing to do but free the associated memory. | |
762 | */ | |
763 | void | |
764 | zfs_close(zfs_handle_t *zhp) | |
765 | { | |
766 | if (zhp->zfs_mntopts) | |
767 | free(zhp->zfs_mntopts); | |
768 | nvlist_free(zhp->zfs_props); | |
769 | nvlist_free(zhp->zfs_user_props); | |
428870ff | 770 | nvlist_free(zhp->zfs_recvd_props); |
34dc7c2f BB |
771 | free(zhp); |
772 | } | |
773 | ||
fb5f0bc8 BB |
774 | typedef struct mnttab_node { |
775 | struct mnttab mtn_mt; | |
776 | avl_node_t mtn_node; | |
777 | } mnttab_node_t; | |
778 | ||
779 | static int | |
780 | libzfs_mnttab_cache_compare(const void *arg1, const void *arg2) | |
781 | { | |
ee36c709 GN |
782 | const mnttab_node_t *mtn1 = (const mnttab_node_t *)arg1; |
783 | const mnttab_node_t *mtn2 = (const mnttab_node_t *)arg2; | |
fb5f0bc8 BB |
784 | int rv; |
785 | ||
786 | rv = strcmp(mtn1->mtn_mt.mnt_special, mtn2->mtn_mt.mnt_special); | |
787 | ||
ee36c709 | 788 | return (AVL_ISIGN(rv)); |
fb5f0bc8 BB |
789 | } |
790 | ||
791 | void | |
792 | libzfs_mnttab_init(libzfs_handle_t *hdl) | |
793 | { | |
a10d50f9 | 794 | pthread_mutex_init(&hdl->libzfs_mnttab_cache_lock, NULL); |
fb5f0bc8 BB |
795 | assert(avl_numnodes(&hdl->libzfs_mnttab_cache) == 0); |
796 | avl_create(&hdl->libzfs_mnttab_cache, libzfs_mnttab_cache_compare, | |
797 | sizeof (mnttab_node_t), offsetof(mnttab_node_t, mtn_node)); | |
9babb374 BB |
798 | } |
799 | ||
fb5c53ea | 800 | int |
9babb374 BB |
801 | libzfs_mnttab_update(libzfs_handle_t *hdl) |
802 | { | |
803 | struct mnttab entry; | |
fb5f0bc8 | 804 | |
fb5c53ea JL |
805 | /* Reopen MNTTAB to prevent reading stale data from open file */ |
806 | if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL) | |
807 | return (ENOENT); | |
808 | ||
fb5f0bc8 BB |
809 | while (getmntent(hdl->libzfs_mnttab, &entry) == 0) { |
810 | mnttab_node_t *mtn; | |
74130450 | 811 | avl_index_t where; |
fb5f0bc8 BB |
812 | |
813 | if (strcmp(entry.mnt_fstype, MNTTYPE_ZFS) != 0) | |
814 | continue; | |
74130450 | 815 | |
fb5f0bc8 BB |
816 | mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); |
817 | mtn->mtn_mt.mnt_special = zfs_strdup(hdl, entry.mnt_special); | |
818 | mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, entry.mnt_mountp); | |
819 | mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, entry.mnt_fstype); | |
820 | mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, entry.mnt_mntopts); | |
74130450 BB |
821 | |
822 | /* Exclude duplicate mounts */ | |
823 | if (avl_find(&hdl->libzfs_mnttab_cache, mtn, &where) != NULL) { | |
824 | free(mtn->mtn_mt.mnt_special); | |
825 | free(mtn->mtn_mt.mnt_mountp); | |
826 | free(mtn->mtn_mt.mnt_fstype); | |
827 | free(mtn->mtn_mt.mnt_mntopts); | |
828 | free(mtn); | |
829 | continue; | |
830 | } | |
831 | ||
fb5f0bc8 BB |
832 | avl_add(&hdl->libzfs_mnttab_cache, mtn); |
833 | } | |
fb5c53ea JL |
834 | |
835 | return (0); | |
fb5f0bc8 BB |
836 | } |
837 | ||
838 | void | |
839 | libzfs_mnttab_fini(libzfs_handle_t *hdl) | |
840 | { | |
841 | void *cookie = NULL; | |
842 | mnttab_node_t *mtn; | |
843 | ||
23d70cde GM |
844 | while ((mtn = avl_destroy_nodes(&hdl->libzfs_mnttab_cache, &cookie)) |
845 | != NULL) { | |
fb5f0bc8 BB |
846 | free(mtn->mtn_mt.mnt_special); |
847 | free(mtn->mtn_mt.mnt_mountp); | |
848 | free(mtn->mtn_mt.mnt_fstype); | |
849 | free(mtn->mtn_mt.mnt_mntopts); | |
850 | free(mtn); | |
851 | } | |
852 | avl_destroy(&hdl->libzfs_mnttab_cache); | |
a10d50f9 | 853 | (void) pthread_mutex_destroy(&hdl->libzfs_mnttab_cache_lock); |
fb5f0bc8 BB |
854 | } |
855 | ||
9babb374 BB |
856 | void |
857 | libzfs_mnttab_cache(libzfs_handle_t *hdl, boolean_t enable) | |
858 | { | |
859 | hdl->libzfs_mnttab_enable = enable; | |
860 | } | |
861 | ||
fb5f0bc8 BB |
862 | int |
863 | libzfs_mnttab_find(libzfs_handle_t *hdl, const char *fsname, | |
864 | struct mnttab *entry) | |
865 | { | |
866 | mnttab_node_t find; | |
867 | mnttab_node_t *mtn; | |
a10d50f9 | 868 | int ret = ENOENT; |
fb5f0bc8 | 869 | |
9babb374 BB |
870 | if (!hdl->libzfs_mnttab_enable) { |
871 | struct mnttab srch = { 0 }; | |
872 | ||
873 | if (avl_numnodes(&hdl->libzfs_mnttab_cache)) | |
874 | libzfs_mnttab_fini(hdl); | |
fb5c53ea JL |
875 | |
876 | /* Reopen MNTTAB to prevent reading stale data from open file */ | |
877 | if (freopen(MNTTAB, "r", hdl->libzfs_mnttab) == NULL) | |
878 | return (ENOENT); | |
879 | ||
9babb374 BB |
880 | srch.mnt_special = (char *)fsname; |
881 | srch.mnt_fstype = MNTTYPE_ZFS; | |
882 | if (getmntany(hdl->libzfs_mnttab, entry, &srch) == 0) | |
883 | return (0); | |
884 | else | |
885 | return (ENOENT); | |
886 | } | |
887 | ||
a10d50f9 SR |
888 | pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock); |
889 | if (avl_numnodes(&hdl->libzfs_mnttab_cache) == 0) { | |
890 | int error; | |
891 | ||
892 | if ((error = libzfs_mnttab_update(hdl)) != 0) { | |
893 | pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock); | |
fb5c53ea | 894 | return (error); |
a10d50f9 SR |
895 | } |
896 | } | |
fb5f0bc8 BB |
897 | |
898 | find.mtn_mt.mnt_special = (char *)fsname; | |
899 | mtn = avl_find(&hdl->libzfs_mnttab_cache, &find, NULL); | |
900 | if (mtn) { | |
901 | *entry = mtn->mtn_mt; | |
a10d50f9 | 902 | ret = 0; |
fb5f0bc8 | 903 | } |
a10d50f9 SR |
904 | pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock); |
905 | return (ret); | |
fb5f0bc8 BB |
906 | } |
907 | ||
908 | void | |
909 | libzfs_mnttab_add(libzfs_handle_t *hdl, const char *special, | |
910 | const char *mountp, const char *mntopts) | |
911 | { | |
912 | mnttab_node_t *mtn; | |
913 | ||
a10d50f9 SR |
914 | pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock); |
915 | if (avl_numnodes(&hdl->libzfs_mnttab_cache) != 0) { | |
916 | mtn = zfs_alloc(hdl, sizeof (mnttab_node_t)); | |
917 | mtn->mtn_mt.mnt_special = zfs_strdup(hdl, special); | |
918 | mtn->mtn_mt.mnt_mountp = zfs_strdup(hdl, mountp); | |
919 | mtn->mtn_mt.mnt_fstype = zfs_strdup(hdl, MNTTYPE_ZFS); | |
920 | mtn->mtn_mt.mnt_mntopts = zfs_strdup(hdl, mntopts); | |
921 | /* | |
922 | * Another thread may have already added this entry | |
923 | * via libzfs_mnttab_update. If so we should skip it. | |
924 | */ | |
925 | if (avl_find(&hdl->libzfs_mnttab_cache, mtn, NULL) != NULL) | |
926 | free(mtn); | |
927 | else | |
928 | avl_add(&hdl->libzfs_mnttab_cache, mtn); | |
929 | } | |
930 | pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock); | |
fb5f0bc8 BB |
931 | } |
932 | ||
933 | void | |
934 | libzfs_mnttab_remove(libzfs_handle_t *hdl, const char *fsname) | |
935 | { | |
936 | mnttab_node_t find; | |
937 | mnttab_node_t *ret; | |
938 | ||
a10d50f9 | 939 | pthread_mutex_lock(&hdl->libzfs_mnttab_cache_lock); |
fb5f0bc8 | 940 | find.mtn_mt.mnt_special = (char *)fsname; |
23d70cde GM |
941 | if ((ret = avl_find(&hdl->libzfs_mnttab_cache, (void *)&find, NULL)) |
942 | != NULL) { | |
fb5f0bc8 BB |
943 | avl_remove(&hdl->libzfs_mnttab_cache, ret); |
944 | free(ret->mtn_mt.mnt_special); | |
945 | free(ret->mtn_mt.mnt_mountp); | |
946 | free(ret->mtn_mt.mnt_fstype); | |
947 | free(ret->mtn_mt.mnt_mntopts); | |
948 | free(ret); | |
949 | } | |
a10d50f9 | 950 | pthread_mutex_unlock(&hdl->libzfs_mnttab_cache_lock); |
fb5f0bc8 BB |
951 | } |
952 | ||
34dc7c2f BB |
953 | int |
954 | zfs_spa_version(zfs_handle_t *zhp, int *spa_version) | |
955 | { | |
b128c09f | 956 | zpool_handle_t *zpool_handle = zhp->zpool_hdl; |
34dc7c2f | 957 | |
34dc7c2f BB |
958 | if (zpool_handle == NULL) |
959 | return (-1); | |
960 | ||
961 | *spa_version = zpool_get_prop_int(zpool_handle, | |
962 | ZPOOL_PROP_VERSION, NULL); | |
34dc7c2f BB |
963 | return (0); |
964 | } | |
965 | ||
966 | /* | |
967 | * The choice of reservation property depends on the SPA version. | |
968 | */ | |
969 | static int | |
970 | zfs_which_resv_prop(zfs_handle_t *zhp, zfs_prop_t *resv_prop) | |
971 | { | |
972 | int spa_version; | |
973 | ||
974 | if (zfs_spa_version(zhp, &spa_version) < 0) | |
975 | return (-1); | |
976 | ||
977 | if (spa_version >= SPA_VERSION_REFRESERVATION) | |
978 | *resv_prop = ZFS_PROP_REFRESERVATION; | |
979 | else | |
980 | *resv_prop = ZFS_PROP_RESERVATION; | |
981 | ||
982 | return (0); | |
983 | } | |
984 | ||
985 | /* | |
986 | * Given an nvlist of properties to set, validates that they are correct, and | |
987 | * parses any numeric properties (index, boolean, etc) if they are specified as | |
988 | * strings. | |
989 | */ | |
b128c09f BB |
990 | nvlist_t * |
991 | zfs_valid_proplist(libzfs_handle_t *hdl, zfs_type_t type, nvlist_t *nvl, | |
82f6f6e6 | 992 | uint64_t zoned, zfs_handle_t *zhp, zpool_handle_t *zpool_hdl, |
b5256303 | 993 | boolean_t key_params_ok, const char *errbuf) |
34dc7c2f BB |
994 | { |
995 | nvpair_t *elem; | |
996 | uint64_t intval; | |
997 | char *strval; | |
998 | zfs_prop_t prop; | |
999 | nvlist_t *ret; | |
1000 | int chosen_normal = -1; | |
1001 | int chosen_utf = -1; | |
1002 | ||
34dc7c2f BB |
1003 | if (nvlist_alloc(&ret, NV_UNIQUE_NAME, 0) != 0) { |
1004 | (void) no_memory(hdl); | |
1005 | return (NULL); | |
1006 | } | |
1007 | ||
9babb374 BB |
1008 | /* |
1009 | * Make sure this property is valid and applies to this type. | |
1010 | */ | |
1011 | ||
34dc7c2f BB |
1012 | elem = NULL; |
1013 | while ((elem = nvlist_next_nvpair(nvl, elem)) != NULL) { | |
1014 | const char *propname = nvpair_name(elem); | |
1015 | ||
9babb374 BB |
1016 | prop = zfs_name_to_prop(propname); |
1017 | if (prop == ZPROP_INVAL && zfs_prop_user(propname)) { | |
34dc7c2f | 1018 | /* |
9babb374 | 1019 | * This is a user property: make sure it's a |
34dc7c2f BB |
1020 | * string, and that it's less than ZAP_MAXNAMELEN. |
1021 | */ | |
1022 | if (nvpair_type(elem) != DATA_TYPE_STRING) { | |
1023 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1024 | "'%s' must be a string"), propname); | |
1025 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1026 | goto error; | |
1027 | } | |
1028 | ||
1029 | if (strlen(nvpair_name(elem)) >= ZAP_MAXNAMELEN) { | |
1030 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1031 | "property name '%s' is too long"), | |
1032 | propname); | |
1033 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1034 | goto error; | |
1035 | } | |
1036 | ||
1037 | (void) nvpair_value_string(elem, &strval); | |
1038 | if (nvlist_add_string(ret, propname, strval) != 0) { | |
1039 | (void) no_memory(hdl); | |
1040 | goto error; | |
1041 | } | |
1042 | continue; | |
1043 | } | |
1044 | ||
9babb374 BB |
1045 | /* |
1046 | * Currently, only user properties can be modified on | |
1047 | * snapshots. | |
1048 | */ | |
b128c09f BB |
1049 | if (type == ZFS_TYPE_SNAPSHOT) { |
1050 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1051 | "this property can not be modified for snapshots")); | |
1052 | (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); | |
1053 | goto error; | |
1054 | } | |
1055 | ||
9babb374 BB |
1056 | if (prop == ZPROP_INVAL && zfs_prop_userquota(propname)) { |
1057 | zfs_userquota_prop_t uqtype; | |
21a4f5cc | 1058 | char *newpropname = NULL; |
9babb374 BB |
1059 | char domain[128]; |
1060 | uint64_t rid; | |
1061 | uint64_t valary[3]; | |
21a4f5cc | 1062 | int rc; |
9babb374 BB |
1063 | |
1064 | if (userquota_propname_decode(propname, zoned, | |
1065 | &uqtype, domain, sizeof (domain), &rid) != 0) { | |
1066 | zfs_error_aux(hdl, | |
1067 | dgettext(TEXT_DOMAIN, | |
1068 | "'%s' has an invalid user/group name"), | |
1069 | propname); | |
1070 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1071 | goto error; | |
1072 | } | |
1073 | ||
1074 | if (uqtype != ZFS_PROP_USERQUOTA && | |
1de321e6 JX |
1075 | uqtype != ZFS_PROP_GROUPQUOTA && |
1076 | uqtype != ZFS_PROP_USEROBJQUOTA && | |
9c5167d1 NF |
1077 | uqtype != ZFS_PROP_GROUPOBJQUOTA && |
1078 | uqtype != ZFS_PROP_PROJECTQUOTA && | |
1079 | uqtype != ZFS_PROP_PROJECTOBJQUOTA) { | |
9babb374 BB |
1080 | zfs_error_aux(hdl, |
1081 | dgettext(TEXT_DOMAIN, "'%s' is readonly"), | |
1082 | propname); | |
1083 | (void) zfs_error(hdl, EZFS_PROPREADONLY, | |
1084 | errbuf); | |
1085 | goto error; | |
1086 | } | |
1087 | ||
1088 | if (nvpair_type(elem) == DATA_TYPE_STRING) { | |
1089 | (void) nvpair_value_string(elem, &strval); | |
1090 | if (strcmp(strval, "none") == 0) { | |
1091 | intval = 0; | |
1092 | } else if (zfs_nicestrtonum(hdl, | |
1093 | strval, &intval) != 0) { | |
1094 | (void) zfs_error(hdl, | |
1095 | EZFS_BADPROP, errbuf); | |
1096 | goto error; | |
1097 | } | |
1098 | } else if (nvpair_type(elem) == | |
1099 | DATA_TYPE_UINT64) { | |
1100 | (void) nvpair_value_uint64(elem, &intval); | |
1101 | if (intval == 0) { | |
1102 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1103 | "use 'none' to disable " | |
9c5167d1 | 1104 | "{user|group|project}quota")); |
9babb374 BB |
1105 | goto error; |
1106 | } | |
1107 | } else { | |
1108 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1109 | "'%s' must be a number"), propname); | |
1110 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1111 | goto error; | |
1112 | } | |
1113 | ||
428870ff BB |
1114 | /* |
1115 | * Encode the prop name as | |
1116 | * userquota@<hex-rid>-domain, to make it easy | |
1117 | * for the kernel to decode. | |
1118 | */ | |
21a4f5cc TH |
1119 | rc = asprintf(&newpropname, "%s%llx-%s", |
1120 | zfs_userquota_prop_prefixes[uqtype], | |
428870ff | 1121 | (longlong_t)rid, domain); |
21a4f5cc TH |
1122 | if (rc == -1 || newpropname == NULL) { |
1123 | (void) no_memory(hdl); | |
1124 | goto error; | |
1125 | } | |
1126 | ||
9babb374 BB |
1127 | valary[0] = uqtype; |
1128 | valary[1] = rid; | |
1129 | valary[2] = intval; | |
1130 | if (nvlist_add_uint64_array(ret, newpropname, | |
1131 | valary, 3) != 0) { | |
21a4f5cc | 1132 | free(newpropname); |
9babb374 BB |
1133 | (void) no_memory(hdl); |
1134 | goto error; | |
1135 | } | |
21a4f5cc | 1136 | free(newpropname); |
9babb374 | 1137 | continue; |
330d06f9 MA |
1138 | } else if (prop == ZPROP_INVAL && zfs_prop_written(propname)) { |
1139 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1140 | "'%s' is readonly"), | |
1141 | propname); | |
1142 | (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); | |
1143 | goto error; | |
9babb374 BB |
1144 | } |
1145 | ||
1146 | if (prop == ZPROP_INVAL) { | |
1147 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1148 | "invalid property '%s'"), propname); | |
1149 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1150 | goto error; | |
1151 | } | |
1152 | ||
962d5242 | 1153 | if (!zfs_prop_valid_for_type(prop, type, B_FALSE)) { |
34dc7c2f BB |
1154 | zfs_error_aux(hdl, |
1155 | dgettext(TEXT_DOMAIN, "'%s' does not " | |
1156 | "apply to datasets of this type"), propname); | |
1157 | (void) zfs_error(hdl, EZFS_PROPTYPE, errbuf); | |
1158 | goto error; | |
1159 | } | |
1160 | ||
1161 | if (zfs_prop_readonly(prop) && | |
b5256303 TC |
1162 | !(zfs_prop_setonce(prop) && zhp == NULL) && |
1163 | !(zfs_prop_encryption_key_param(prop) && key_params_ok)) { | |
34dc7c2f BB |
1164 | zfs_error_aux(hdl, |
1165 | dgettext(TEXT_DOMAIN, "'%s' is readonly"), | |
1166 | propname); | |
1167 | (void) zfs_error(hdl, EZFS_PROPREADONLY, errbuf); | |
1168 | goto error; | |
1169 | } | |
1170 | ||
1171 | if (zprop_parse_value(hdl, elem, prop, type, ret, | |
1172 | &strval, &intval, errbuf) != 0) | |
1173 | goto error; | |
1174 | ||
1175 | /* | |
1176 | * Perform some additional checks for specific properties. | |
1177 | */ | |
1178 | switch (prop) { | |
1179 | case ZFS_PROP_VERSION: | |
1180 | { | |
1181 | int version; | |
1182 | ||
1183 | if (zhp == NULL) | |
1184 | break; | |
1185 | version = zfs_prop_get_int(zhp, ZFS_PROP_VERSION); | |
1186 | if (intval < version) { | |
1187 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1188 | "Can not downgrade; already at version %u"), | |
1189 | version); | |
1190 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1191 | goto error; | |
1192 | } | |
1193 | break; | |
1194 | } | |
1195 | ||
34dc7c2f | 1196 | case ZFS_PROP_VOLBLOCKSIZE: |
f1512ee6 MA |
1197 | case ZFS_PROP_RECORDSIZE: |
1198 | { | |
1199 | int maxbs = SPA_MAXBLOCKSIZE; | |
4cb7b9c5 BB |
1200 | char buf[64]; |
1201 | ||
82f6f6e6 JS |
1202 | if (zpool_hdl != NULL) { |
1203 | maxbs = zpool_get_prop_int(zpool_hdl, | |
f1512ee6 MA |
1204 | ZPOOL_PROP_MAXBLOCKSIZE, NULL); |
1205 | } | |
1206 | /* | |
1207 | * The value must be a power of two between | |
1208 | * SPA_MINBLOCKSIZE and maxbs. | |
1209 | */ | |
34dc7c2f | 1210 | if (intval < SPA_MINBLOCKSIZE || |
f1512ee6 | 1211 | intval > maxbs || !ISP2(intval)) { |
e7fbeb60 | 1212 | zfs_nicebytes(maxbs, buf, sizeof (buf)); |
34dc7c2f | 1213 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
f1512ee6 | 1214 | "'%s' must be power of 2 from 512B " |
4cb7b9c5 | 1215 | "to %s"), propname, buf); |
34dc7c2f BB |
1216 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); |
1217 | goto error; | |
1218 | } | |
1219 | break; | |
f1512ee6 | 1220 | } |
cc99f275 DB |
1221 | |
1222 | case ZFS_PROP_SPECIAL_SMALL_BLOCKS: | |
1223 | if (zpool_hdl != NULL) { | |
1224 | char state[64] = ""; | |
1225 | ||
1226 | /* | |
1227 | * Issue a warning but do not fail so that | |
1228 | * tests for setable properties succeed. | |
1229 | */ | |
1230 | if (zpool_prop_get_feature(zpool_hdl, | |
1231 | "feature@allocation_classes", state, | |
1232 | sizeof (state)) != 0 || | |
1233 | strcmp(state, ZFS_FEATURE_ACTIVE) != 0) { | |
1234 | (void) fprintf(stderr, gettext( | |
1235 | "%s: property requires a special " | |
1236 | "device in the pool\n"), propname); | |
1237 | } | |
1238 | } | |
1239 | if (intval != 0 && | |
1240 | (intval < SPA_MINBLOCKSIZE || | |
1241 | intval > SPA_OLD_MAXBLOCKSIZE || !ISP2(intval))) { | |
1242 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1243 | "invalid '%s=%d' property: must be zero or " | |
1244 | "a power of 2 from 512B to 128K"), propname, | |
1245 | intval); | |
1246 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1247 | goto error; | |
1248 | } | |
1249 | break; | |
1250 | ||
428870ff BB |
1251 | case ZFS_PROP_MLSLABEL: |
1252 | { | |
d2c15e84 | 1253 | #ifdef HAVE_MLSLABEL |
428870ff BB |
1254 | /* |
1255 | * Verify the mlslabel string and convert to | |
1256 | * internal hex label string. | |
1257 | */ | |
1258 | ||
1259 | m_label_t *new_sl; | |
1260 | char *hex = NULL; /* internal label string */ | |
1261 | ||
1262 | /* Default value is already OK. */ | |
1263 | if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) | |
1264 | break; | |
1265 | ||
1266 | /* Verify the label can be converted to binary form */ | |
1267 | if (((new_sl = m_label_alloc(MAC_LABEL)) == NULL) || | |
1268 | (str_to_label(strval, &new_sl, MAC_LABEL, | |
1269 | L_NO_CORRECTION, NULL) == -1)) { | |
1270 | goto badlabel; | |
34dc7c2f BB |
1271 | } |
1272 | ||
428870ff BB |
1273 | /* Now translate to hex internal label string */ |
1274 | if (label_to_str(new_sl, &hex, M_INTERNAL, | |
1275 | DEF_NAMES) != 0) { | |
1276 | if (hex) | |
1277 | free(hex); | |
1278 | goto badlabel; | |
1279 | } | |
1280 | m_label_free(new_sl); | |
1281 | ||
1282 | /* If string is already in internal form, we're done. */ | |
1283 | if (strcmp(strval, hex) == 0) { | |
1284 | free(hex); | |
1285 | break; | |
1286 | } | |
1287 | ||
1288 | /* Replace the label string with the internal form. */ | |
1289 | (void) nvlist_remove(ret, zfs_prop_to_name(prop), | |
1290 | DATA_TYPE_STRING); | |
1291 | verify(nvlist_add_string(ret, zfs_prop_to_name(prop), | |
1292 | hex) == 0); | |
1293 | free(hex); | |
1294 | ||
34dc7c2f BB |
1295 | break; |
1296 | ||
428870ff BB |
1297 | badlabel: |
1298 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1299 | "invalid mlslabel '%s'"), strval); | |
1300 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1301 | m_label_free(new_sl); /* OK if null */ | |
1302 | goto error; | |
d2c15e84 BB |
1303 | #else |
1304 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1305 | "mlslabels are unsupported")); | |
1306 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1307 | goto error; | |
1308 | #endif /* HAVE_MLSLABEL */ | |
428870ff BB |
1309 | } |
1310 | ||
34dc7c2f BB |
1311 | case ZFS_PROP_MOUNTPOINT: |
1312 | { | |
1313 | namecheck_err_t why; | |
1314 | ||
1315 | if (strcmp(strval, ZFS_MOUNTPOINT_NONE) == 0 || | |
1316 | strcmp(strval, ZFS_MOUNTPOINT_LEGACY) == 0) | |
1317 | break; | |
1318 | ||
1319 | if (mountpoint_namecheck(strval, &why)) { | |
1320 | switch (why) { | |
1321 | case NAME_ERR_LEADING_SLASH: | |
1322 | zfs_error_aux(hdl, | |
1323 | dgettext(TEXT_DOMAIN, | |
1324 | "'%s' must be an absolute path, " | |
1325 | "'none', or 'legacy'"), propname); | |
1326 | break; | |
1327 | case NAME_ERR_TOOLONG: | |
1328 | zfs_error_aux(hdl, | |
1329 | dgettext(TEXT_DOMAIN, | |
1330 | "component of '%s' is too long"), | |
1331 | propname); | |
1332 | break; | |
23d70cde | 1333 | |
e75c13c3 | 1334 | default: |
23d70cde GM |
1335 | zfs_error_aux(hdl, |
1336 | dgettext(TEXT_DOMAIN, | |
1337 | "(%d) not defined"), | |
1338 | why); | |
e75c13c3 | 1339 | break; |
34dc7c2f BB |
1340 | } |
1341 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1342 | goto error; | |
1343 | } | |
1344 | } | |
1345 | ||
1346 | /*FALLTHRU*/ | |
1347 | ||
1348 | case ZFS_PROP_SHARESMB: | |
1349 | case ZFS_PROP_SHARENFS: | |
1350 | /* | |
1351 | * For the mountpoint and sharenfs or sharesmb | |
1352 | * properties, check if it can be set in a | |
1353 | * global/non-global zone based on | |
1354 | * the zoned property value: | |
1355 | * | |
1356 | * global zone non-global zone | |
1357 | * -------------------------------------------------- | |
1358 | * zoned=on mountpoint (no) mountpoint (yes) | |
1359 | * sharenfs (no) sharenfs (no) | |
1360 | * sharesmb (no) sharesmb (no) | |
1361 | * | |
1362 | * zoned=off mountpoint (yes) N/A | |
1363 | * sharenfs (yes) | |
1364 | * sharesmb (yes) | |
1365 | */ | |
1366 | if (zoned) { | |
1367 | if (getzoneid() == GLOBAL_ZONEID) { | |
1368 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1369 | "'%s' cannot be set on " | |
1370 | "dataset in a non-global zone"), | |
1371 | propname); | |
1372 | (void) zfs_error(hdl, EZFS_ZONED, | |
1373 | errbuf); | |
1374 | goto error; | |
1375 | } else if (prop == ZFS_PROP_SHARENFS || | |
1376 | prop == ZFS_PROP_SHARESMB) { | |
1377 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1378 | "'%s' cannot be set in " | |
1379 | "a non-global zone"), propname); | |
1380 | (void) zfs_error(hdl, EZFS_ZONED, | |
1381 | errbuf); | |
1382 | goto error; | |
1383 | } | |
1384 | } else if (getzoneid() != GLOBAL_ZONEID) { | |
1385 | /* | |
1386 | * If zoned property is 'off', this must be in | |
9babb374 | 1387 | * a global zone. If not, something is wrong. |
34dc7c2f BB |
1388 | */ |
1389 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1390 | "'%s' cannot be set while dataset " | |
1391 | "'zoned' property is set"), propname); | |
1392 | (void) zfs_error(hdl, EZFS_ZONED, errbuf); | |
1393 | goto error; | |
1394 | } | |
1395 | ||
1396 | /* | |
1397 | * At this point, it is legitimate to set the | |
1398 | * property. Now we want to make sure that the | |
1399 | * property value is valid if it is sharenfs. | |
1400 | */ | |
1401 | if ((prop == ZFS_PROP_SHARENFS || | |
1402 | prop == ZFS_PROP_SHARESMB) && | |
1403 | strcmp(strval, "on") != 0 && | |
1404 | strcmp(strval, "off") != 0) { | |
1405 | zfs_share_proto_t proto; | |
1406 | ||
1407 | if (prop == ZFS_PROP_SHARESMB) | |
1408 | proto = PROTO_SMB; | |
1409 | else | |
1410 | proto = PROTO_NFS; | |
1411 | ||
1412 | /* | |
1413 | * Must be an valid sharing protocol | |
1414 | * option string so init the libshare | |
1415 | * in order to enable the parser and | |
1416 | * then parse the options. We use the | |
1417 | * control API since we don't care about | |
1418 | * the current configuration and don't | |
1419 | * want the overhead of loading it | |
1420 | * until we actually do something. | |
1421 | */ | |
1422 | ||
1423 | if (zfs_init_libshare(hdl, | |
1424 | SA_INIT_CONTROL_API) != SA_OK) { | |
1425 | /* | |
1426 | * An error occurred so we can't do | |
1427 | * anything | |
1428 | */ | |
1429 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1430 | "'%s' cannot be set: problem " | |
1431 | "in share initialization"), | |
1432 | propname); | |
1433 | (void) zfs_error(hdl, EZFS_BADPROP, | |
1434 | errbuf); | |
1435 | goto error; | |
1436 | } | |
1437 | ||
1438 | if (zfs_parse_options(strval, proto) != SA_OK) { | |
1439 | /* | |
1440 | * There was an error in parsing so | |
1441 | * deal with it by issuing an error | |
1442 | * message and leaving after | |
1443 | * uninitializing the the libshare | |
1444 | * interface. | |
1445 | */ | |
1446 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1447 | "'%s' cannot be set to invalid " | |
1448 | "options"), propname); | |
1449 | (void) zfs_error(hdl, EZFS_BADPROP, | |
1450 | errbuf); | |
1451 | zfs_uninit_libshare(hdl); | |
1452 | goto error; | |
1453 | } | |
1454 | zfs_uninit_libshare(hdl); | |
1455 | } | |
1456 | ||
1457 | break; | |
23d70cde | 1458 | |
b5256303 TC |
1459 | case ZFS_PROP_KEYLOCATION: |
1460 | if (!zfs_prop_valid_keylocation(strval, B_FALSE)) { | |
1461 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1462 | "invalid keylocation")); | |
1463 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1464 | goto error; | |
1465 | } | |
1466 | ||
1467 | if (zhp != NULL) { | |
1468 | uint64_t crypt = | |
1469 | zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION); | |
1470 | ||
1471 | if (crypt == ZIO_CRYPT_OFF && | |
1472 | strcmp(strval, "none") != 0) { | |
1473 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
74df0c5e TC |
1474 | "keylocation must be 'none' " |
1475 | "for unencrypted datasets")); | |
b5256303 TC |
1476 | (void) zfs_error(hdl, EZFS_BADPROP, |
1477 | errbuf); | |
1478 | goto error; | |
1479 | } else if (crypt != ZIO_CRYPT_OFF && | |
1480 | strcmp(strval, "none") == 0) { | |
1481 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
74df0c5e TC |
1482 | "keylocation must not be 'none' " |
1483 | "for encrypted datasets")); | |
b5256303 TC |
1484 | (void) zfs_error(hdl, EZFS_BADPROP, |
1485 | errbuf); | |
1486 | goto error; | |
1487 | } | |
1488 | } | |
1489 | break; | |
1490 | ||
1491 | case ZFS_PROP_PBKDF2_ITERS: | |
1492 | if (intval < MIN_PBKDF2_ITERATIONS) { | |
1493 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1494 | "minimum pbkdf2 iterations is %u"), | |
1495 | MIN_PBKDF2_ITERATIONS); | |
1496 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1497 | goto error; | |
1498 | } | |
1499 | break; | |
1500 | ||
34dc7c2f BB |
1501 | case ZFS_PROP_UTF8ONLY: |
1502 | chosen_utf = (int)intval; | |
1503 | break; | |
23d70cde | 1504 | |
34dc7c2f BB |
1505 | case ZFS_PROP_NORMALIZE: |
1506 | chosen_normal = (int)intval; | |
1507 | break; | |
23d70cde | 1508 | |
e75c13c3 BB |
1509 | default: |
1510 | break; | |
34dc7c2f BB |
1511 | } |
1512 | ||
1513 | /* | |
1514 | * For changes to existing volumes, we have some additional | |
1515 | * checks to enforce. | |
1516 | */ | |
1517 | if (type == ZFS_TYPE_VOLUME && zhp != NULL) { | |
34dc7c2f BB |
1518 | uint64_t blocksize = zfs_prop_get_int(zhp, |
1519 | ZFS_PROP_VOLBLOCKSIZE); | |
1520 | char buf[64]; | |
1521 | ||
1522 | switch (prop) { | |
34dc7c2f BB |
1523 | case ZFS_PROP_VOLSIZE: |
1524 | if (intval % blocksize != 0) { | |
e7fbeb60 | 1525 | zfs_nicebytes(blocksize, buf, |
34dc7c2f BB |
1526 | sizeof (buf)); |
1527 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1528 | "'%s' must be a multiple of " | |
1529 | "volume block size (%s)"), | |
1530 | propname, buf); | |
1531 | (void) zfs_error(hdl, EZFS_BADPROP, | |
1532 | errbuf); | |
1533 | goto error; | |
1534 | } | |
1535 | ||
1536 | if (intval == 0) { | |
1537 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1538 | "'%s' cannot be zero"), | |
1539 | propname); | |
1540 | (void) zfs_error(hdl, EZFS_BADPROP, | |
1541 | errbuf); | |
1542 | goto error; | |
1543 | } | |
1544 | break; | |
23d70cde | 1545 | |
e75c13c3 BB |
1546 | default: |
1547 | break; | |
34dc7c2f BB |
1548 | } |
1549 | } | |
b5256303 TC |
1550 | |
1551 | /* check encryption properties */ | |
1552 | if (zhp != NULL) { | |
1553 | int64_t crypt = zfs_prop_get_int(zhp, | |
1554 | ZFS_PROP_ENCRYPTION); | |
1555 | ||
1556 | switch (prop) { | |
1557 | case ZFS_PROP_COPIES: | |
1558 | if (crypt != ZIO_CRYPT_OFF && intval > 2) { | |
1559 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1560 | "encrypted datasets cannot have " | |
1561 | "3 copies")); | |
1562 | (void) zfs_error(hdl, EZFS_BADPROP, | |
1563 | errbuf); | |
1564 | goto error; | |
1565 | } | |
1566 | break; | |
1567 | default: | |
1568 | break; | |
1569 | } | |
1570 | } | |
34dc7c2f BB |
1571 | } |
1572 | ||
1573 | /* | |
1574 | * If normalization was chosen, but no UTF8 choice was made, | |
1575 | * enforce rejection of non-UTF8 names. | |
1576 | * | |
1577 | * If normalization was chosen, but rejecting non-UTF8 names | |
1578 | * was explicitly not chosen, it is an error. | |
1579 | */ | |
1580 | if (chosen_normal > 0 && chosen_utf < 0) { | |
1581 | if (nvlist_add_uint64(ret, | |
1582 | zfs_prop_to_name(ZFS_PROP_UTF8ONLY), 1) != 0) { | |
1583 | (void) no_memory(hdl); | |
1584 | goto error; | |
1585 | } | |
1586 | } else if (chosen_normal > 0 && chosen_utf == 0) { | |
1587 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1588 | "'%s' must be set 'on' if normalization chosen"), | |
1589 | zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); | |
1590 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1591 | goto error; | |
1592 | } | |
572e2857 BB |
1593 | return (ret); |
1594 | ||
1595 | error: | |
1596 | nvlist_free(ret); | |
1597 | return (NULL); | |
1598 | } | |
1599 | ||
1600 | int | |
1601 | zfs_add_synthetic_resv(zfs_handle_t *zhp, nvlist_t *nvl) | |
1602 | { | |
1603 | uint64_t old_volsize; | |
1604 | uint64_t new_volsize; | |
1605 | uint64_t old_reservation; | |
1606 | uint64_t new_reservation; | |
1607 | zfs_prop_t resv_prop; | |
59d4c71c | 1608 | nvlist_t *props; |
34dc7c2f BB |
1609 | |
1610 | /* | |
1611 | * If this is an existing volume, and someone is setting the volsize, | |
1612 | * make sure that it matches the reservation, or add it if necessary. | |
1613 | */ | |
572e2857 BB |
1614 | old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); |
1615 | if (zfs_which_resv_prop(zhp, &resv_prop) < 0) | |
1616 | return (-1); | |
1617 | old_reservation = zfs_prop_get_int(zhp, resv_prop); | |
59d4c71c GW |
1618 | |
1619 | props = fnvlist_alloc(); | |
1620 | fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), | |
1621 | zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); | |
1622 | ||
1623 | if ((zvol_volsize_to_reservation(old_volsize, props) != | |
1624 | old_reservation) || nvlist_exists(nvl, | |
1625 | zfs_prop_to_name(resv_prop))) { | |
1626 | fnvlist_free(props); | |
572e2857 | 1627 | return (0); |
34dc7c2f | 1628 | } |
572e2857 | 1629 | if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), |
59d4c71c GW |
1630 | &new_volsize) != 0) { |
1631 | fnvlist_free(props); | |
572e2857 | 1632 | return (-1); |
59d4c71c GW |
1633 | } |
1634 | new_reservation = zvol_volsize_to_reservation(new_volsize, props); | |
1635 | fnvlist_free(props); | |
1636 | ||
572e2857 BB |
1637 | if (nvlist_add_uint64(nvl, zfs_prop_to_name(resv_prop), |
1638 | new_reservation) != 0) { | |
1639 | (void) no_memory(zhp->zfs_hdl); | |
1640 | return (-1); | |
1641 | } | |
1642 | return (1); | |
34dc7c2f BB |
1643 | } |
1644 | ||
d22f3a82 MG |
1645 | /* |
1646 | * Helper for 'zfs {set|clone} refreservation=auto'. Must be called after | |
1647 | * zfs_valid_proplist(), as it is what sets the UINT64_MAX sentinal value. | |
1648 | * Return codes must match zfs_add_synthetic_resv(). | |
1649 | */ | |
1650 | static int | |
1651 | zfs_fix_auto_resv(zfs_handle_t *zhp, nvlist_t *nvl) | |
1652 | { | |
1653 | uint64_t volsize; | |
1654 | uint64_t resvsize; | |
1655 | zfs_prop_t prop; | |
1656 | nvlist_t *props; | |
1657 | ||
1658 | if (!ZFS_IS_VOLUME(zhp)) { | |
1659 | return (0); | |
1660 | } | |
1661 | ||
1662 | if (zfs_which_resv_prop(zhp, &prop) != 0) { | |
1663 | return (-1); | |
1664 | } | |
1665 | ||
1666 | if (prop != ZFS_PROP_REFRESERVATION) { | |
1667 | return (0); | |
1668 | } | |
1669 | ||
1670 | if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(prop), &resvsize) != 0) { | |
1671 | /* No value being set, so it can't be "auto" */ | |
1672 | return (0); | |
1673 | } | |
1674 | if (resvsize != UINT64_MAX) { | |
1675 | /* Being set to a value other than "auto" */ | |
1676 | return (0); | |
1677 | } | |
1678 | ||
1679 | props = fnvlist_alloc(); | |
1680 | ||
1681 | fnvlist_add_uint64(props, zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), | |
1682 | zfs_prop_get_int(zhp, ZFS_PROP_VOLBLOCKSIZE)); | |
1683 | ||
1684 | if (nvlist_lookup_uint64(nvl, zfs_prop_to_name(ZFS_PROP_VOLSIZE), | |
1685 | &volsize) != 0) { | |
1686 | volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); | |
1687 | } | |
1688 | ||
1689 | resvsize = zvol_volsize_to_reservation(volsize, props); | |
1690 | fnvlist_free(props); | |
1691 | ||
1692 | (void) nvlist_remove_all(nvl, zfs_prop_to_name(prop)); | |
1693 | if (nvlist_add_uint64(nvl, zfs_prop_to_name(prop), resvsize) != 0) { | |
1694 | (void) no_memory(zhp->zfs_hdl); | |
1695 | return (-1); | |
1696 | } | |
1697 | return (1); | |
1698 | } | |
1699 | ||
428870ff BB |
1700 | void |
1701 | zfs_setprop_error(libzfs_handle_t *hdl, zfs_prop_t prop, int err, | |
1702 | char *errbuf) | |
1703 | { | |
1704 | switch (err) { | |
1705 | ||
1706 | case ENOSPC: | |
1707 | /* | |
1708 | * For quotas and reservations, ENOSPC indicates | |
1709 | * something different; setting a quota or reservation | |
1710 | * doesn't use any disk space. | |
1711 | */ | |
1712 | switch (prop) { | |
1713 | case ZFS_PROP_QUOTA: | |
1714 | case ZFS_PROP_REFQUOTA: | |
1715 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1716 | "size is less than current used or " | |
1717 | "reserved space")); | |
1718 | (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); | |
1719 | break; | |
1720 | ||
1721 | case ZFS_PROP_RESERVATION: | |
1722 | case ZFS_PROP_REFRESERVATION: | |
1723 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1724 | "size is greater than available space")); | |
1725 | (void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); | |
1726 | break; | |
1727 | ||
1728 | default: | |
1729 | (void) zfs_standard_error(hdl, err, errbuf); | |
1730 | break; | |
1731 | } | |
1732 | break; | |
1733 | ||
1734 | case EBUSY: | |
1735 | (void) zfs_standard_error(hdl, EBUSY, errbuf); | |
1736 | break; | |
1737 | ||
1738 | case EROFS: | |
1739 | (void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); | |
1740 | break; | |
1741 | ||
98cb3a76 WA |
1742 | case E2BIG: |
1743 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1744 | "property value too long")); | |
1745 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1746 | break; | |
1747 | ||
428870ff BB |
1748 | case ENOTSUP: |
1749 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1750 | "pool and or dataset must be upgraded to set this " | |
1751 | "property or value")); | |
1752 | (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
1753 | break; | |
1754 | ||
1755 | case ERANGE: | |
f1512ee6 | 1756 | if (prop == ZFS_PROP_COMPRESSION || |
50c957f7 | 1757 | prop == ZFS_PROP_DNODESIZE || |
f1512ee6 | 1758 | prop == ZFS_PROP_RECORDSIZE) { |
428870ff BB |
1759 | (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1760 | "property setting is not allowed on " | |
1761 | "bootable datasets")); | |
1762 | (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); | |
3c67d83a TH |
1763 | } else if (prop == ZFS_PROP_CHECKSUM || |
1764 | prop == ZFS_PROP_DEDUP) { | |
1765 | (void) zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1766 | "property setting is not allowed on " | |
1767 | "root pools")); | |
1768 | (void) zfs_error(hdl, EZFS_NOTSUP, errbuf); | |
428870ff BB |
1769 | } else { |
1770 | (void) zfs_standard_error(hdl, err, errbuf); | |
1771 | } | |
1772 | break; | |
1773 | ||
1774 | case EINVAL: | |
1775 | if (prop == ZPROP_INVAL) { | |
1776 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1777 | } else { | |
1778 | (void) zfs_standard_error(hdl, err, errbuf); | |
1779 | } | |
1780 | break; | |
1781 | ||
b5256303 TC |
1782 | case EACCES: |
1783 | if (prop == ZFS_PROP_KEYLOCATION) { | |
1784 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1785 | "keylocation may only be set on encryption roots")); | |
1786 | (void) zfs_error(hdl, EZFS_BADPROP, errbuf); | |
1787 | } else { | |
1788 | (void) zfs_standard_error(hdl, err, errbuf); | |
1789 | } | |
1790 | break; | |
1791 | ||
428870ff BB |
1792 | case EOVERFLOW: |
1793 | /* | |
1794 | * This platform can't address a volume this big. | |
1795 | */ | |
1796 | #ifdef _ILP32 | |
1797 | if (prop == ZFS_PROP_VOLSIZE) { | |
1798 | (void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); | |
1799 | break; | |
1800 | } | |
1801 | #endif | |
1802 | /* FALLTHROUGH */ | |
1803 | default: | |
1804 | (void) zfs_standard_error(hdl, err, errbuf); | |
1805 | } | |
1806 | } | |
1807 | ||
2cf7f52b BB |
1808 | static boolean_t |
1809 | zfs_is_namespace_prop(zfs_prop_t prop) | |
1810 | { | |
1811 | switch (prop) { | |
1812 | ||
1813 | case ZFS_PROP_ATIME: | |
6d111134 | 1814 | case ZFS_PROP_RELATIME: |
2cf7f52b BB |
1815 | case ZFS_PROP_DEVICES: |
1816 | case ZFS_PROP_EXEC: | |
1817 | case ZFS_PROP_SETUID: | |
1818 | case ZFS_PROP_READONLY: | |
1819 | case ZFS_PROP_XATTR: | |
1820 | case ZFS_PROP_NBMAND: | |
1821 | return (B_TRUE); | |
1822 | ||
1823 | default: | |
1824 | return (B_FALSE); | |
1825 | } | |
1826 | } | |
1827 | ||
34dc7c2f BB |
1828 | /* |
1829 | * Given a property name and value, set the property for the given dataset. | |
1830 | */ | |
1831 | int | |
1832 | zfs_prop_set(zfs_handle_t *zhp, const char *propname, const char *propval) | |
1833 | { | |
34dc7c2f | 1834 | int ret = -1; |
34dc7c2f BB |
1835 | char errbuf[1024]; |
1836 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
23de906c | 1837 | nvlist_t *nvl = NULL; |
34dc7c2f BB |
1838 | |
1839 | (void) snprintf(errbuf, sizeof (errbuf), | |
1840 | dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), | |
1841 | zhp->zfs_name); | |
1842 | ||
1843 | if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0 || | |
1844 | nvlist_add_string(nvl, propname, propval) != 0) { | |
1845 | (void) no_memory(hdl); | |
1846 | goto error; | |
1847 | } | |
1848 | ||
23de906c | 1849 | ret = zfs_prop_set_list(zhp, nvl); |
34dc7c2f | 1850 | |
23de906c | 1851 | error: |
34dc7c2f | 1852 | nvlist_free(nvl); |
23de906c CW |
1853 | return (ret); |
1854 | } | |
34dc7c2f | 1855 | |
34dc7c2f | 1856 | |
572e2857 | 1857 | |
23de906c CW |
1858 | /* |
1859 | * Given an nvlist of property names and values, set the properties for the | |
1860 | * given dataset. | |
1861 | */ | |
1862 | int | |
1863 | zfs_prop_set_list(zfs_handle_t *zhp, nvlist_t *props) | |
1864 | { | |
1865 | zfs_cmd_t zc = {"\0"}; | |
1866 | int ret = -1; | |
1867 | prop_changelist_t **cls = NULL; | |
1868 | int cl_idx; | |
1869 | char errbuf[1024]; | |
1870 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
1871 | nvlist_t *nvl; | |
c40db193 | 1872 | int nvl_len = 0; |
23de906c CW |
1873 | int added_resv = 0; |
1874 | zfs_prop_t prop = 0; | |
1875 | nvpair_t *elem; | |
34dc7c2f | 1876 | |
23de906c CW |
1877 | (void) snprintf(errbuf, sizeof (errbuf), |
1878 | dgettext(TEXT_DOMAIN, "cannot set property for '%s'"), | |
1879 | zhp->zfs_name); | |
1880 | ||
1881 | if ((nvl = zfs_valid_proplist(hdl, zhp->zfs_type, props, | |
82f6f6e6 | 1882 | zfs_prop_get_int(zhp, ZFS_PROP_ZONED), zhp, zhp->zpool_hdl, |
b5256303 | 1883 | B_FALSE, errbuf)) == NULL) |
34dc7c2f | 1884 | goto error; |
34dc7c2f | 1885 | |
b128c09f | 1886 | /* |
23de906c CW |
1887 | * We have to check for any extra properties which need to be added |
1888 | * before computing the length of the nvlist. | |
b128c09f | 1889 | */ |
23de906c CW |
1890 | for (elem = nvlist_next_nvpair(nvl, NULL); |
1891 | elem != NULL; | |
1892 | elem = nvlist_next_nvpair(nvl, elem)) { | |
1893 | if (zfs_name_to_prop(nvpair_name(elem)) == ZFS_PROP_VOLSIZE && | |
1894 | (added_resv = zfs_add_synthetic_resv(zhp, nvl)) == -1) { | |
1895 | goto error; | |
1896 | } | |
6f1ffb06 | 1897 | } |
d22f3a82 MG |
1898 | |
1899 | if (added_resv != 1 && | |
1900 | (added_resv = zfs_fix_auto_resv(zhp, nvl)) == -1) { | |
1901 | goto error; | |
1902 | } | |
1903 | ||
23de906c CW |
1904 | /* |
1905 | * Check how many properties we're setting and allocate an array to | |
1906 | * store changelist pointers for postfix(). | |
1907 | */ | |
23de906c CW |
1908 | for (elem = nvlist_next_nvpair(nvl, NULL); |
1909 | elem != NULL; | |
1910 | elem = nvlist_next_nvpair(nvl, elem)) | |
1911 | nvl_len++; | |
1912 | if ((cls = calloc(nvl_len, sizeof (prop_changelist_t *))) == NULL) | |
b128c09f | 1913 | goto error; |
34dc7c2f | 1914 | |
23de906c CW |
1915 | cl_idx = 0; |
1916 | for (elem = nvlist_next_nvpair(nvl, NULL); | |
1917 | elem != NULL; | |
1918 | elem = nvlist_next_nvpair(nvl, elem)) { | |
1919 | ||
1920 | prop = zfs_name_to_prop(nvpair_name(elem)); | |
1921 | ||
1922 | assert(cl_idx < nvl_len); | |
1923 | /* | |
1924 | * We don't want to unmount & remount the dataset when changing | |
1925 | * its canmount property to 'on' or 'noauto'. We only use | |
1926 | * the changelist logic to unmount when setting canmount=off. | |
1927 | */ | |
b87baa7e GM |
1928 | if (prop != ZFS_PROP_CANMOUNT || |
1929 | (fnvpair_value_uint64(elem) == ZFS_CANMOUNT_OFF && | |
1930 | zfs_is_mounted(zhp, NULL))) { | |
23de906c CW |
1931 | cls[cl_idx] = changelist_gather(zhp, prop, 0, 0); |
1932 | if (cls[cl_idx] == NULL) | |
1933 | goto error; | |
1934 | } | |
1935 | ||
1936 | if (prop == ZFS_PROP_MOUNTPOINT && | |
1937 | changelist_haszonedchild(cls[cl_idx])) { | |
1938 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
1939 | "child dataset with inherited mountpoint is used " | |
1940 | "in a non-global zone")); | |
1941 | ret = zfs_error(hdl, EZFS_ZONED, errbuf); | |
1942 | goto error; | |
1943 | } | |
1944 | ||
1945 | if (cls[cl_idx] != NULL && | |
1946 | (ret = changelist_prefix(cls[cl_idx])) != 0) | |
1947 | goto error; | |
1948 | ||
1949 | cl_idx++; | |
1950 | } | |
1951 | assert(cl_idx == nvl_len); | |
1952 | ||
34dc7c2f | 1953 | /* |
23de906c | 1954 | * Execute the corresponding ioctl() to set this list of properties. |
34dc7c2f BB |
1955 | */ |
1956 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
1957 | ||
23de906c CW |
1958 | if ((ret = zcmd_write_src_nvlist(hdl, &zc, nvl)) != 0 || |
1959 | (ret = zcmd_alloc_dst_nvlist(hdl, &zc, 0)) != 0) | |
34dc7c2f BB |
1960 | goto error; |
1961 | ||
1962 | ret = zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); | |
9babb374 | 1963 | |
34dc7c2f | 1964 | if (ret != 0) { |
84ddd4b0 AS |
1965 | if (zc.zc_nvlist_dst_filled == B_FALSE) { |
1966 | (void) zfs_standard_error(hdl, errno, errbuf); | |
1967 | goto error; | |
1968 | } | |
1969 | ||
23de906c CW |
1970 | /* Get the list of unset properties back and report them. */ |
1971 | nvlist_t *errorprops = NULL; | |
1972 | if (zcmd_read_dst_nvlist(hdl, &zc, &errorprops) != 0) | |
1973 | goto error; | |
84ddd4b0 | 1974 | for (nvpair_t *elem = nvlist_next_nvpair(errorprops, NULL); |
23de906c | 1975 | elem != NULL; |
84ddd4b0 | 1976 | elem = nvlist_next_nvpair(errorprops, elem)) { |
23de906c CW |
1977 | prop = zfs_name_to_prop(nvpair_name(elem)); |
1978 | zfs_setprop_error(hdl, prop, errno, errbuf); | |
1979 | } | |
1980 | nvlist_free(errorprops); | |
1981 | ||
572e2857 BB |
1982 | if (added_resv && errno == ENOSPC) { |
1983 | /* clean up the volsize property we tried to set */ | |
1984 | uint64_t old_volsize = zfs_prop_get_int(zhp, | |
1985 | ZFS_PROP_VOLSIZE); | |
1986 | nvlist_free(nvl); | |
23de906c | 1987 | nvl = NULL; |
572e2857 | 1988 | zcmd_free_nvlists(&zc); |
23de906c | 1989 | |
572e2857 BB |
1990 | if (nvlist_alloc(&nvl, NV_UNIQUE_NAME, 0) != 0) |
1991 | goto error; | |
1992 | if (nvlist_add_uint64(nvl, | |
1993 | zfs_prop_to_name(ZFS_PROP_VOLSIZE), | |
1994 | old_volsize) != 0) | |
1995 | goto error; | |
1996 | if (zcmd_write_src_nvlist(hdl, &zc, nvl) != 0) | |
1997 | goto error; | |
1998 | (void) zfs_ioctl(hdl, ZFS_IOC_SET_PROP, &zc); | |
1999 | } | |
34dc7c2f | 2000 | } else { |
23de906c CW |
2001 | for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { |
2002 | if (cls[cl_idx] != NULL) { | |
2003 | int clp_err = changelist_postfix(cls[cl_idx]); | |
2004 | if (clp_err != 0) | |
2005 | ret = clp_err; | |
2006 | } | |
2007 | } | |
34dc7c2f | 2008 | |
2cf7f52b BB |
2009 | if (ret == 0) { |
2010 | /* | |
2011 | * Refresh the statistics so the new property | |
2012 | * value is reflected. | |
2013 | */ | |
34dc7c2f | 2014 | (void) get_stats(zhp); |
2cf7f52b BB |
2015 | |
2016 | /* | |
2017 | * Remount the filesystem to propagate the change | |
2018 | * if one of the options handled by the generic | |
2019 | * Linux namespace layer has been modified. | |
2020 | */ | |
2021 | if (zfs_is_namespace_prop(prop) && | |
2022 | zfs_is_mounted(zhp, NULL)) | |
2023 | ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0); | |
2024 | } | |
34dc7c2f BB |
2025 | } |
2026 | ||
2027 | error: | |
2028 | nvlist_free(nvl); | |
2029 | zcmd_free_nvlists(&zc); | |
23de906c CW |
2030 | if (cls != NULL) { |
2031 | for (cl_idx = 0; cl_idx < nvl_len; cl_idx++) { | |
2032 | if (cls[cl_idx] != NULL) | |
2033 | changelist_free(cls[cl_idx]); | |
2034 | } | |
2035 | free(cls); | |
2036 | } | |
34dc7c2f BB |
2037 | return (ret); |
2038 | } | |
2039 | ||
2040 | /* | |
428870ff BB |
2041 | * Given a property, inherit the value from the parent dataset, or if received |
2042 | * is TRUE, revert to the received value, if any. | |
34dc7c2f BB |
2043 | */ |
2044 | int | |
428870ff | 2045 | zfs_prop_inherit(zfs_handle_t *zhp, const char *propname, boolean_t received) |
34dc7c2f | 2046 | { |
13fe0198 | 2047 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
2048 | int ret; |
2049 | prop_changelist_t *cl; | |
2050 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
2051 | char errbuf[1024]; | |
2052 | zfs_prop_t prop; | |
2053 | ||
2054 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
2055 | "cannot inherit %s for '%s'"), propname, zhp->zfs_name); | |
2056 | ||
428870ff | 2057 | zc.zc_cookie = received; |
34dc7c2f BB |
2058 | if ((prop = zfs_name_to_prop(propname)) == ZPROP_INVAL) { |
2059 | /* | |
2060 | * For user properties, the amount of work we have to do is very | |
2061 | * small, so just do it here. | |
2062 | */ | |
2063 | if (!zfs_prop_user(propname)) { | |
2064 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2065 | "invalid property")); | |
2066 | return (zfs_error(hdl, EZFS_BADPROP, errbuf)); | |
2067 | } | |
2068 | ||
2069 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
2070 | (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); | |
2071 | ||
2072 | if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc) != 0) | |
2073 | return (zfs_standard_error(hdl, errno, errbuf)); | |
2074 | ||
2075 | return (0); | |
2076 | } | |
2077 | ||
2078 | /* | |
2079 | * Verify that this property is inheritable. | |
2080 | */ | |
2081 | if (zfs_prop_readonly(prop)) | |
2082 | return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); | |
2083 | ||
428870ff | 2084 | if (!zfs_prop_inheritable(prop) && !received) |
34dc7c2f BB |
2085 | return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); |
2086 | ||
2087 | /* | |
2088 | * Check to see if the value applies to this type | |
2089 | */ | |
962d5242 | 2090 | if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) |
34dc7c2f BB |
2091 | return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); |
2092 | ||
2093 | /* | |
572e2857 | 2094 | * Normalize the name, to get rid of shorthand abbreviations. |
34dc7c2f BB |
2095 | */ |
2096 | propname = zfs_prop_to_name(prop); | |
2097 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
2098 | (void) strlcpy(zc.zc_value, propname, sizeof (zc.zc_value)); | |
2099 | ||
2100 | if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && | |
2101 | zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { | |
2102 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2103 | "dataset is used in a non-global zone")); | |
2104 | return (zfs_error(hdl, EZFS_ZONED, errbuf)); | |
2105 | } | |
2106 | ||
2107 | /* | |
2108 | * Determine datasets which will be affected by this change, if any. | |
2109 | */ | |
b128c09f | 2110 | if ((cl = changelist_gather(zhp, prop, 0, 0)) == NULL) |
34dc7c2f BB |
2111 | return (-1); |
2112 | ||
2113 | if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { | |
2114 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
2115 | "child dataset with inherited mountpoint is used " | |
2116 | "in a non-global zone")); | |
2117 | ret = zfs_error(hdl, EZFS_ZONED, errbuf); | |
2118 | goto error; | |
2119 | } | |
2120 | ||
2121 | if ((ret = changelist_prefix(cl)) != 0) | |
2122 | goto error; | |
2123 | ||
2124 | if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_INHERIT_PROP, &zc)) != 0) { | |
2125 | return (zfs_standard_error(hdl, errno, errbuf)); | |
2126 | } else { | |
2127 | ||
2128 | if ((ret = changelist_postfix(cl)) != 0) | |
2129 | goto error; | |
2130 | ||
2131 | /* | |
2132 | * Refresh the statistics so the new property is reflected. | |
2133 | */ | |
2134 | (void) get_stats(zhp); | |
4d8c78c8 GB |
2135 | |
2136 | /* | |
2137 | * Remount the filesystem to propagate the change | |
2138 | * if one of the options handled by the generic | |
2139 | * Linux namespace layer has been modified. | |
2140 | */ | |
2141 | if (zfs_is_namespace_prop(prop) && | |
2142 | zfs_is_mounted(zhp, NULL)) | |
2143 | ret = zfs_mount(zhp, MNTOPT_REMOUNT, 0); | |
34dc7c2f BB |
2144 | } |
2145 | ||
2146 | error: | |
2147 | changelist_free(cl); | |
2148 | return (ret); | |
2149 | } | |
2150 | ||
2151 | /* | |
2152 | * True DSL properties are stored in an nvlist. The following two functions | |
2153 | * extract them appropriately. | |
2154 | */ | |
2cf7f52b | 2155 | uint64_t |
34dc7c2f BB |
2156 | getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) |
2157 | { | |
2158 | nvlist_t *nv; | |
2159 | uint64_t value; | |
2160 | ||
2161 | *source = NULL; | |
2162 | if (nvlist_lookup_nvlist(zhp->zfs_props, | |
2163 | zfs_prop_to_name(prop), &nv) == 0) { | |
2164 | verify(nvlist_lookup_uint64(nv, ZPROP_VALUE, &value) == 0); | |
2165 | (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); | |
2166 | } else { | |
9babb374 BB |
2167 | verify(!zhp->zfs_props_table || |
2168 | zhp->zfs_props_table[prop] == B_TRUE); | |
34dc7c2f BB |
2169 | value = zfs_prop_default_numeric(prop); |
2170 | *source = ""; | |
2171 | } | |
2172 | ||
2173 | return (value); | |
2174 | } | |
2175 | ||
47dfff3b | 2176 | static const char * |
34dc7c2f BB |
2177 | getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) |
2178 | { | |
2179 | nvlist_t *nv; | |
47dfff3b | 2180 | const char *value; |
34dc7c2f BB |
2181 | |
2182 | *source = NULL; | |
2183 | if (nvlist_lookup_nvlist(zhp->zfs_props, | |
2184 | zfs_prop_to_name(prop), &nv) == 0) { | |
47dfff3b | 2185 | value = fnvlist_lookup_string(nv, ZPROP_VALUE); |
34dc7c2f BB |
2186 | (void) nvlist_lookup_string(nv, ZPROP_SOURCE, source); |
2187 | } else { | |
9babb374 BB |
2188 | verify(!zhp->zfs_props_table || |
2189 | zhp->zfs_props_table[prop] == B_TRUE); | |
47dfff3b | 2190 | value = zfs_prop_default_string(prop); |
34dc7c2f BB |
2191 | *source = ""; |
2192 | } | |
2193 | ||
2194 | return (value); | |
2195 | } | |
2196 | ||
428870ff BB |
2197 | static boolean_t |
2198 | zfs_is_recvd_props_mode(zfs_handle_t *zhp) | |
2199 | { | |
2200 | return (zhp->zfs_props == zhp->zfs_recvd_props); | |
2201 | } | |
2202 | ||
2203 | static void | |
2204 | zfs_set_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) | |
2205 | { | |
2206 | *cookie = (uint64_t)(uintptr_t)zhp->zfs_props; | |
2207 | zhp->zfs_props = zhp->zfs_recvd_props; | |
2208 | } | |
2209 | ||
2210 | static void | |
2211 | zfs_unset_recvd_props_mode(zfs_handle_t *zhp, uint64_t *cookie) | |
2212 | { | |
2213 | zhp->zfs_props = (nvlist_t *)(uintptr_t)*cookie; | |
2214 | *cookie = 0; | |
2215 | } | |
2216 | ||
34dc7c2f BB |
2217 | /* |
2218 | * Internal function for getting a numeric property. Both zfs_prop_get() and | |
2219 | * zfs_prop_get_int() are built using this interface. | |
2220 | * | |
2221 | * Certain properties can be overridden using 'mount -o'. In this case, scan | |
79251738 | 2222 | * the contents of the /proc/self/mounts entry, searching for the |
2223 | * appropriate options. If they differ from the on-disk values, report the | |
2224 | * current values and mark the source "temporary". | |
34dc7c2f BB |
2225 | */ |
2226 | static int | |
2227 | get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zprop_source_t *src, | |
2228 | char **source, uint64_t *val) | |
2229 | { | |
13fe0198 | 2230 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
2231 | nvlist_t *zplprops = NULL; |
2232 | struct mnttab mnt; | |
2233 | char *mntopt_on = NULL; | |
2234 | char *mntopt_off = NULL; | |
428870ff | 2235 | boolean_t received = zfs_is_recvd_props_mode(zhp); |
34dc7c2f BB |
2236 | |
2237 | *source = NULL; | |
2238 | ||
962d5242 TC |
2239 | /* |
2240 | * If the property is being fetched for a snapshot, check whether | |
2241 | * the property is valid for the snapshot's head dataset type. | |
2242 | */ | |
2243 | if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT && | |
02730c33 BB |
2244 | !zfs_prop_valid_for_type(prop, zhp->zfs_head_type, B_TRUE)) { |
2245 | *val = zfs_prop_default_numeric(prop); | |
2246 | return (-1); | |
09c0b8fe | 2247 | } |
962d5242 | 2248 | |
34dc7c2f BB |
2249 | switch (prop) { |
2250 | case ZFS_PROP_ATIME: | |
2251 | mntopt_on = MNTOPT_ATIME; | |
2252 | mntopt_off = MNTOPT_NOATIME; | |
2253 | break; | |
2254 | ||
6d111134 TC |
2255 | case ZFS_PROP_RELATIME: |
2256 | mntopt_on = MNTOPT_RELATIME; | |
2257 | mntopt_off = MNTOPT_NORELATIME; | |
2258 | break; | |
2259 | ||
34dc7c2f BB |
2260 | case ZFS_PROP_DEVICES: |
2261 | mntopt_on = MNTOPT_DEVICES; | |
2262 | mntopt_off = MNTOPT_NODEVICES; | |
2263 | break; | |
2264 | ||
2265 | case ZFS_PROP_EXEC: | |
2266 | mntopt_on = MNTOPT_EXEC; | |
2267 | mntopt_off = MNTOPT_NOEXEC; | |
2268 | break; | |
2269 | ||
2270 | case ZFS_PROP_READONLY: | |
2271 | mntopt_on = MNTOPT_RO; | |
2272 | mntopt_off = MNTOPT_RW; | |
2273 | break; | |
2274 | ||
2275 | case ZFS_PROP_SETUID: | |
2276 | mntopt_on = MNTOPT_SETUID; | |
2277 | mntopt_off = MNTOPT_NOSETUID; | |
2278 | break; | |
2279 | ||
2280 | case ZFS_PROP_XATTR: | |
2281 | mntopt_on = MNTOPT_XATTR; | |
2282 | mntopt_off = MNTOPT_NOXATTR; | |
2283 | break; | |
2284 | ||
2285 | case ZFS_PROP_NBMAND: | |
2286 | mntopt_on = MNTOPT_NBMAND; | |
2287 | mntopt_off = MNTOPT_NONBMAND; | |
2288 | break; | |
23d70cde | 2289 | |
e75c13c3 BB |
2290 | default: |
2291 | break; | |
34dc7c2f BB |
2292 | } |
2293 | ||
2294 | /* | |
2295 | * Because looking up the mount options is potentially expensive | |
79251738 | 2296 | * (iterating over all of /proc/self/mounts), we defer its |
2297 | * calculation until we're looking up a property which requires | |
2298 | * its presence. | |
34dc7c2f BB |
2299 | */ |
2300 | if (!zhp->zfs_mntcheck && | |
2301 | (mntopt_on != NULL || prop == ZFS_PROP_MOUNTED)) { | |
fb5f0bc8 BB |
2302 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
2303 | struct mnttab entry; | |
34dc7c2f | 2304 | |
fb5f0bc8 BB |
2305 | if (libzfs_mnttab_find(hdl, zhp->zfs_name, &entry) == 0) { |
2306 | zhp->zfs_mntopts = zfs_strdup(hdl, | |
34dc7c2f BB |
2307 | entry.mnt_mntopts); |
2308 | if (zhp->zfs_mntopts == NULL) | |
2309 | return (-1); | |
2310 | } | |
2311 | ||
2312 | zhp->zfs_mntcheck = B_TRUE; | |
2313 | } | |
2314 | ||
2315 | if (zhp->zfs_mntopts == NULL) | |
2316 | mnt.mnt_mntopts = ""; | |
2317 | else | |
2318 | mnt.mnt_mntopts = zhp->zfs_mntopts; | |
2319 | ||
2320 | switch (prop) { | |
0282c413 BB |
2321 | case ZFS_PROP_ATIME: |
2322 | case ZFS_PROP_RELATIME: | |
34dc7c2f BB |
2323 | case ZFS_PROP_DEVICES: |
2324 | case ZFS_PROP_EXEC: | |
2325 | case ZFS_PROP_READONLY: | |
2326 | case ZFS_PROP_SETUID: | |
2327 | case ZFS_PROP_XATTR: | |
2328 | case ZFS_PROP_NBMAND: | |
2329 | *val = getprop_uint64(zhp, prop, source); | |
2330 | ||
428870ff BB |
2331 | if (received) |
2332 | break; | |
2333 | ||
34dc7c2f BB |
2334 | if (hasmntopt(&mnt, mntopt_on) && !*val) { |
2335 | *val = B_TRUE; | |
2336 | if (src) | |
2337 | *src = ZPROP_SRC_TEMPORARY; | |
2338 | } else if (hasmntopt(&mnt, mntopt_off) && *val) { | |
2339 | *val = B_FALSE; | |
2340 | if (src) | |
2341 | *src = ZPROP_SRC_TEMPORARY; | |
2342 | } | |
2343 | break; | |
2344 | ||
2345 | case ZFS_PROP_CANMOUNT: | |
428870ff | 2346 | case ZFS_PROP_VOLSIZE: |
34dc7c2f BB |
2347 | case ZFS_PROP_QUOTA: |
2348 | case ZFS_PROP_REFQUOTA: | |
2349 | case ZFS_PROP_RESERVATION: | |
2350 | case ZFS_PROP_REFRESERVATION: | |
788eb90c JJ |
2351 | case ZFS_PROP_FILESYSTEM_LIMIT: |
2352 | case ZFS_PROP_SNAPSHOT_LIMIT: | |
2353 | case ZFS_PROP_FILESYSTEM_COUNT: | |
2354 | case ZFS_PROP_SNAPSHOT_COUNT: | |
34dc7c2f | 2355 | *val = getprop_uint64(zhp, prop, source); |
428870ff BB |
2356 | |
2357 | if (*source == NULL) { | |
2358 | /* not default, must be local */ | |
34dc7c2f | 2359 | *source = zhp->zfs_name; |
428870ff | 2360 | } |
34dc7c2f BB |
2361 | break; |
2362 | ||
2363 | case ZFS_PROP_MOUNTED: | |
2364 | *val = (zhp->zfs_mntopts != NULL); | |
2365 | break; | |
2366 | ||
2367 | case ZFS_PROP_NUMCLONES: | |
2368 | *val = zhp->zfs_dmustats.dds_num_clones; | |
2369 | break; | |
2370 | ||
2371 | case ZFS_PROP_VERSION: | |
2372 | case ZFS_PROP_NORMALIZE: | |
2373 | case ZFS_PROP_UTF8ONLY: | |
2374 | case ZFS_PROP_CASE: | |
962d5242 | 2375 | if (zcmd_alloc_dst_nvlist(zhp->zfs_hdl, &zc, 0) != 0) |
34dc7c2f BB |
2376 | return (-1); |
2377 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
2378 | if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_OBJSET_ZPLPROPS, &zc)) { | |
2379 | zcmd_free_nvlists(&zc); | |
59199d90 TC |
2380 | if (prop == ZFS_PROP_VERSION && |
2381 | zhp->zfs_type == ZFS_TYPE_VOLUME) | |
2382 | *val = zfs_prop_default_numeric(prop); | |
45d1cae3 | 2383 | return (-1); |
34dc7c2f BB |
2384 | } |
2385 | if (zcmd_read_dst_nvlist(zhp->zfs_hdl, &zc, &zplprops) != 0 || | |
2386 | nvlist_lookup_uint64(zplprops, zfs_prop_to_name(prop), | |
2387 | val) != 0) { | |
2388 | zcmd_free_nvlists(&zc); | |
45d1cae3 | 2389 | return (-1); |
34dc7c2f | 2390 | } |
8a5fc748 | 2391 | nvlist_free(zplprops); |
34dc7c2f BB |
2392 | zcmd_free_nvlists(&zc); |
2393 | break; | |
2394 | ||
96c2e961 KW |
2395 | case ZFS_PROP_INCONSISTENT: |
2396 | *val = zhp->zfs_dmustats.dds_inconsistent; | |
2397 | break; | |
2398 | ||
34dc7c2f BB |
2399 | default: |
2400 | switch (zfs_prop_get_type(prop)) { | |
2401 | case PROP_TYPE_NUMBER: | |
2402 | case PROP_TYPE_INDEX: | |
2403 | *val = getprop_uint64(zhp, prop, source); | |
b128c09f | 2404 | /* |
9babb374 | 2405 | * If we tried to use a default value for a |
b128c09f | 2406 | * readonly property, it means that it was not |
3d43125f GM |
2407 | * present. Note this only applies to "truly" |
2408 | * readonly properties, not set-once properties | |
2409 | * like volblocksize. | |
b128c09f BB |
2410 | */ |
2411 | if (zfs_prop_readonly(prop) && | |
3d43125f | 2412 | !zfs_prop_setonce(prop) && |
428870ff BB |
2413 | *source != NULL && (*source)[0] == '\0') { |
2414 | *source = NULL; | |
7e8dbd93 | 2415 | return (-1); |
b128c09f | 2416 | } |
34dc7c2f BB |
2417 | break; |
2418 | ||
2419 | case PROP_TYPE_STRING: | |
2420 | default: | |
2421 | zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, | |
2422 | "cannot get non-numeric property")); | |
2423 | return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, | |
2424 | dgettext(TEXT_DOMAIN, "internal error"))); | |
2425 | } | |
2426 | } | |
2427 | ||
2428 | return (0); | |
2429 | } | |
2430 | ||
2431 | /* | |
2432 | * Calculate the source type, given the raw source string. | |
2433 | */ | |
2434 | static void | |
2435 | get_source(zfs_handle_t *zhp, zprop_source_t *srctype, char *source, | |
2436 | char *statbuf, size_t statlen) | |
2437 | { | |
94183a9d BB |
2438 | if (statbuf == NULL || |
2439 | srctype == NULL || *srctype == ZPROP_SRC_TEMPORARY) { | |
34dc7c2f | 2440 | return; |
94183a9d | 2441 | } |
34dc7c2f BB |
2442 | |
2443 | if (source == NULL) { | |
2444 | *srctype = ZPROP_SRC_NONE; | |
2445 | } else if (source[0] == '\0') { | |
2446 | *srctype = ZPROP_SRC_DEFAULT; | |
428870ff BB |
2447 | } else if (strstr(source, ZPROP_SOURCE_VAL_RECVD) != NULL) { |
2448 | *srctype = ZPROP_SRC_RECEIVED; | |
34dc7c2f BB |
2449 | } else { |
2450 | if (strcmp(source, zhp->zfs_name) == 0) { | |
2451 | *srctype = ZPROP_SRC_LOCAL; | |
2452 | } else { | |
2453 | (void) strlcpy(statbuf, source, statlen); | |
2454 | *srctype = ZPROP_SRC_INHERITED; | |
2455 | } | |
2456 | } | |
2457 | ||
2458 | } | |
2459 | ||
428870ff BB |
2460 | int |
2461 | zfs_prop_get_recvd(zfs_handle_t *zhp, const char *propname, char *propbuf, | |
2462 | size_t proplen, boolean_t literal) | |
2463 | { | |
2464 | zfs_prop_t prop; | |
2465 | int err = 0; | |
2466 | ||
2467 | if (zhp->zfs_recvd_props == NULL) | |
2468 | if (get_recvd_props_ioctl(zhp) != 0) | |
2469 | return (-1); | |
2470 | ||
2471 | prop = zfs_name_to_prop(propname); | |
2472 | ||
2473 | if (prop != ZPROP_INVAL) { | |
2474 | uint64_t cookie; | |
2475 | if (!nvlist_exists(zhp->zfs_recvd_props, propname)) | |
2476 | return (-1); | |
2477 | zfs_set_recvd_props_mode(zhp, &cookie); | |
2478 | err = zfs_prop_get(zhp, prop, propbuf, proplen, | |
2479 | NULL, NULL, 0, literal); | |
2480 | zfs_unset_recvd_props_mode(zhp, &cookie); | |
428870ff BB |
2481 | } else { |
2482 | nvlist_t *propval; | |
2483 | char *recvdval; | |
2484 | if (nvlist_lookup_nvlist(zhp->zfs_recvd_props, | |
2485 | propname, &propval) != 0) | |
2486 | return (-1); | |
2487 | verify(nvlist_lookup_string(propval, ZPROP_VALUE, | |
2488 | &recvdval) == 0); | |
2489 | (void) strlcpy(propbuf, recvdval, proplen); | |
2490 | } | |
2491 | ||
2492 | return (err == 0 ? 0 : -1); | |
2493 | } | |
2494 | ||
330d06f9 MA |
2495 | static int |
2496 | get_clones_string(zfs_handle_t *zhp, char *propbuf, size_t proplen) | |
2497 | { | |
2498 | nvlist_t *value; | |
2499 | nvpair_t *pair; | |
2500 | ||
2501 | value = zfs_get_clones_nvl(zhp); | |
2502 | if (value == NULL) | |
2503 | return (-1); | |
2504 | ||
2505 | propbuf[0] = '\0'; | |
2506 | for (pair = nvlist_next_nvpair(value, NULL); pair != NULL; | |
2507 | pair = nvlist_next_nvpair(value, pair)) { | |
2508 | if (propbuf[0] != '\0') | |
2509 | (void) strlcat(propbuf, ",", proplen); | |
2510 | (void) strlcat(propbuf, nvpair_name(pair), proplen); | |
2511 | } | |
2512 | ||
2513 | return (0); | |
2514 | } | |
2515 | ||
2516 | struct get_clones_arg { | |
2517 | uint64_t numclones; | |
2518 | nvlist_t *value; | |
2519 | const char *origin; | |
eca7b760 | 2520 | char buf[ZFS_MAX_DATASET_NAME_LEN]; |
330d06f9 MA |
2521 | }; |
2522 | ||
2523 | int | |
2524 | get_clones_cb(zfs_handle_t *zhp, void *arg) | |
2525 | { | |
2526 | struct get_clones_arg *gca = arg; | |
2527 | ||
2528 | if (gca->numclones == 0) { | |
2529 | zfs_close(zhp); | |
2530 | return (0); | |
2531 | } | |
2532 | ||
2533 | if (zfs_prop_get(zhp, ZFS_PROP_ORIGIN, gca->buf, sizeof (gca->buf), | |
2534 | NULL, NULL, 0, B_TRUE) != 0) | |
2535 | goto out; | |
2536 | if (strcmp(gca->buf, gca->origin) == 0) { | |
13fe0198 | 2537 | fnvlist_add_boolean(gca->value, zfs_get_name(zhp)); |
330d06f9 MA |
2538 | gca->numclones--; |
2539 | } | |
2540 | ||
2541 | out: | |
2542 | (void) zfs_iter_children(zhp, get_clones_cb, gca); | |
2543 | zfs_close(zhp); | |
2544 | return (0); | |
2545 | } | |
2546 | ||
2547 | nvlist_t * | |
2548 | zfs_get_clones_nvl(zfs_handle_t *zhp) | |
2549 | { | |
2550 | nvlist_t *nv, *value; | |
2551 | ||
2552 | if (nvlist_lookup_nvlist(zhp->zfs_props, | |
2553 | zfs_prop_to_name(ZFS_PROP_CLONES), &nv) != 0) { | |
2554 | struct get_clones_arg gca; | |
2555 | ||
2556 | /* | |
2557 | * if this is a snapshot, then the kernel wasn't able | |
2558 | * to get the clones. Do it by slowly iterating. | |
2559 | */ | |
2560 | if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) | |
2561 | return (NULL); | |
2562 | if (nvlist_alloc(&nv, NV_UNIQUE_NAME, 0) != 0) | |
2563 | return (NULL); | |
2564 | if (nvlist_alloc(&value, NV_UNIQUE_NAME, 0) != 0) { | |
2565 | nvlist_free(nv); | |
2566 | return (NULL); | |
2567 | } | |
2568 | ||
2569 | gca.numclones = zfs_prop_get_int(zhp, ZFS_PROP_NUMCLONES); | |
2570 | gca.value = value; | |
2571 | gca.origin = zhp->zfs_name; | |
2572 | ||
2573 | if (gca.numclones != 0) { | |
2574 | zfs_handle_t *root; | |
eca7b760 | 2575 | char pool[ZFS_MAX_DATASET_NAME_LEN]; |
330d06f9 MA |
2576 | char *cp = pool; |
2577 | ||
2578 | /* get the pool name */ | |
2579 | (void) strlcpy(pool, zhp->zfs_name, sizeof (pool)); | |
2580 | (void) strsep(&cp, "/@"); | |
2581 | root = zfs_open(zhp->zfs_hdl, pool, | |
2582 | ZFS_TYPE_FILESYSTEM); | |
0a8f18f9 | 2583 | if (root == NULL) { |
2584 | nvlist_free(nv); | |
2585 | nvlist_free(value); | |
2586 | return (NULL); | |
2587 | } | |
330d06f9 MA |
2588 | |
2589 | (void) get_clones_cb(root, &gca); | |
2590 | } | |
2591 | ||
2592 | if (gca.numclones != 0 || | |
2593 | nvlist_add_nvlist(nv, ZPROP_VALUE, value) != 0 || | |
2594 | nvlist_add_nvlist(zhp->zfs_props, | |
2595 | zfs_prop_to_name(ZFS_PROP_CLONES), nv) != 0) { | |
2596 | nvlist_free(nv); | |
2597 | nvlist_free(value); | |
2598 | return (NULL); | |
2599 | } | |
2600 | nvlist_free(nv); | |
2601 | nvlist_free(value); | |
2602 | verify(0 == nvlist_lookup_nvlist(zhp->zfs_props, | |
2603 | zfs_prop_to_name(ZFS_PROP_CLONES), &nv)); | |
2604 | } | |
2605 | ||
2606 | verify(nvlist_lookup_nvlist(nv, ZPROP_VALUE, &value) == 0); | |
2607 | ||
2608 | return (value); | |
2609 | } | |
2610 | ||
d99a0153 CW |
2611 | /* |
2612 | * Accepts a property and value and checks that the value | |
2613 | * matches the one found by the channel program. If they are | |
2614 | * not equal, print both of them. | |
2615 | */ | |
2616 | static void | |
2617 | zcp_check(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t intval, | |
2618 | const char *strval) | |
2619 | { | |
2620 | if (!zhp->zfs_hdl->libzfs_prop_debug) | |
2621 | return; | |
2622 | int error; | |
2623 | char *poolname = zhp->zpool_hdl->zpool_name; | |
2624 | const char *prop_name = zfs_prop_to_name(prop); | |
2625 | const char *program = | |
2626 | "args = ...\n" | |
2627 | "ds = args['dataset']\n" | |
2628 | "prop = args['property']\n" | |
2629 | "value, setpoint = zfs.get_prop(ds, prop)\n" | |
2630 | "return {value=value, setpoint=setpoint}\n"; | |
2631 | nvlist_t *outnvl; | |
2632 | nvlist_t *retnvl; | |
2633 | nvlist_t *argnvl = fnvlist_alloc(); | |
2634 | ||
2635 | fnvlist_add_string(argnvl, "dataset", zhp->zfs_name); | |
2636 | fnvlist_add_string(argnvl, "property", zfs_prop_to_name(prop)); | |
2637 | ||
5b72a38d | 2638 | error = lzc_channel_program_nosync(poolname, program, |
d99a0153 CW |
2639 | 10 * 1000 * 1000, 10 * 1024 * 1024, argnvl, &outnvl); |
2640 | ||
2641 | if (error == 0) { | |
2642 | retnvl = fnvlist_lookup_nvlist(outnvl, "return"); | |
2643 | if (zfs_prop_get_type(prop) == PROP_TYPE_NUMBER) { | |
2644 | int64_t ans; | |
2645 | error = nvlist_lookup_int64(retnvl, "value", &ans); | |
2646 | if (error != 0) { | |
2647 | (void) fprintf(stderr, "%s: zcp check error: " | |
2648 | "%u\n", prop_name, error); | |
2649 | return; | |
2650 | } | |
2651 | if (ans != intval) { | |
2652 | (void) fprintf(stderr, "%s: zfs found %llu, " | |
2653 | "but zcp found %llu\n", prop_name, | |
2654 | (u_longlong_t)intval, (u_longlong_t)ans); | |
2655 | } | |
2656 | } else { | |
2657 | char *str_ans; | |
2658 | error = nvlist_lookup_string(retnvl, "value", &str_ans); | |
2659 | if (error != 0) { | |
2660 | (void) fprintf(stderr, "%s: zcp check error: " | |
2661 | "%u\n", prop_name, error); | |
2662 | return; | |
2663 | } | |
2664 | if (strcmp(strval, str_ans) != 0) { | |
2665 | (void) fprintf(stderr, | |
2666 | "%s: zfs found '%s', but zcp found '%s'\n", | |
2667 | prop_name, strval, str_ans); | |
2668 | } | |
2669 | } | |
2670 | } else { | |
2671 | (void) fprintf(stderr, "%s: zcp check failed, channel program " | |
2672 | "error: %u\n", prop_name, error); | |
2673 | } | |
2674 | nvlist_free(argnvl); | |
2675 | nvlist_free(outnvl); | |
2676 | } | |
2677 | ||
34dc7c2f BB |
2678 | /* |
2679 | * Retrieve a property from the given object. If 'literal' is specified, then | |
2680 | * numbers are left as exact values. Otherwise, numbers are converted to a | |
2681 | * human-readable form. | |
2682 | * | |
2683 | * Returns 0 on success, or -1 on error. | |
2684 | */ | |
2685 | int | |
2686 | zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, | |
2687 | zprop_source_t *src, char *statbuf, size_t statlen, boolean_t literal) | |
2688 | { | |
2689 | char *source = NULL; | |
2690 | uint64_t val; | |
47dfff3b | 2691 | const char *str; |
34dc7c2f | 2692 | const char *strval; |
428870ff | 2693 | boolean_t received = zfs_is_recvd_props_mode(zhp); |
34dc7c2f BB |
2694 | |
2695 | /* | |
2696 | * Check to see if this property applies to our object | |
2697 | */ | |
962d5242 | 2698 | if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) |
34dc7c2f BB |
2699 | return (-1); |
2700 | ||
428870ff BB |
2701 | if (received && zfs_prop_readonly(prop)) |
2702 | return (-1); | |
2703 | ||
34dc7c2f BB |
2704 | if (src) |
2705 | *src = ZPROP_SRC_NONE; | |
2706 | ||
2707 | switch (prop) { | |
2708 | case ZFS_PROP_CREATION: | |
2709 | /* | |
2710 | * 'creation' is a time_t stored in the statistics. We convert | |
2711 | * this into a string unless 'literal' is specified. | |
2712 | */ | |
2713 | { | |
2714 | val = getprop_uint64(zhp, prop, &source); | |
2715 | time_t time = (time_t)val; | |
2716 | struct tm t; | |
2717 | ||
2718 | if (literal || | |
2719 | localtime_r(&time, &t) == NULL || | |
2720 | strftime(propbuf, proplen, "%a %b %e %k:%M %Y", | |
2721 | &t) == 0) | |
ba6a2402 | 2722 | (void) snprintf(propbuf, proplen, "%llu", |
02730c33 | 2723 | (u_longlong_t)val); |
34dc7c2f | 2724 | } |
d99a0153 | 2725 | zcp_check(zhp, prop, val, NULL); |
34dc7c2f BB |
2726 | break; |
2727 | ||
2728 | case ZFS_PROP_MOUNTPOINT: | |
2729 | /* | |
2730 | * Getting the precise mountpoint can be tricky. | |
2731 | * | |
2732 | * - for 'none' or 'legacy', return those values. | |
34dc7c2f BB |
2733 | * - for inherited mountpoints, we want to take everything |
2734 | * after our ancestor and append it to the inherited value. | |
2735 | * | |
2736 | * If the pool has an alternate root, we want to prepend that | |
2737 | * root to any values we return. | |
2738 | */ | |
b128c09f | 2739 | |
34dc7c2f BB |
2740 | str = getprop_string(zhp, prop, &source); |
2741 | ||
b128c09f BB |
2742 | if (str[0] == '/') { |
2743 | char buf[MAXPATHLEN]; | |
2744 | char *root = buf; | |
428870ff | 2745 | const char *relpath; |
34dc7c2f | 2746 | |
428870ff BB |
2747 | /* |
2748 | * If we inherit the mountpoint, even from a dataset | |
2749 | * with a received value, the source will be the path of | |
2750 | * the dataset we inherit from. If source is | |
2751 | * ZPROP_SOURCE_VAL_RECVD, the received value is not | |
2752 | * inherited. | |
2753 | */ | |
2754 | if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) { | |
2755 | relpath = ""; | |
2756 | } else { | |
2757 | relpath = zhp->zfs_name + strlen(source); | |
2758 | if (relpath[0] == '/') | |
2759 | relpath++; | |
2760 | } | |
b128c09f BB |
2761 | |
2762 | if ((zpool_get_prop(zhp->zpool_hdl, | |
2a8b84b7 AS |
2763 | ZPOOL_PROP_ALTROOT, buf, MAXPATHLEN, NULL, |
2764 | B_FALSE)) || (strcmp(root, "-") == 0)) | |
b128c09f BB |
2765 | root[0] = '\0'; |
2766 | /* | |
2767 | * Special case an alternate root of '/'. This will | |
2768 | * avoid having multiple leading slashes in the | |
2769 | * mountpoint path. | |
2770 | */ | |
2771 | if (strcmp(root, "/") == 0) | |
2772 | root++; | |
2773 | ||
2774 | /* | |
2775 | * If the mountpoint is '/' then skip over this | |
2776 | * if we are obtaining either an alternate root or | |
2777 | * an inherited mountpoint. | |
2778 | */ | |
2779 | if (str[1] == '\0' && (root[0] != '\0' || | |
2780 | relpath[0] != '\0')) | |
34dc7c2f BB |
2781 | str++; |
2782 | ||
2783 | if (relpath[0] == '\0') | |
2784 | (void) snprintf(propbuf, proplen, "%s%s", | |
2785 | root, str); | |
2786 | else | |
2787 | (void) snprintf(propbuf, proplen, "%s%s%s%s", | |
2788 | root, str, relpath[0] == '@' ? "" : "/", | |
2789 | relpath); | |
2790 | } else { | |
2791 | /* 'legacy' or 'none' */ | |
2792 | (void) strlcpy(propbuf, str, proplen); | |
2793 | } | |
d99a0153 | 2794 | zcp_check(zhp, prop, 0, propbuf); |
34dc7c2f BB |
2795 | break; |
2796 | ||
2797 | case ZFS_PROP_ORIGIN: | |
47dfff3b MA |
2798 | str = getprop_string(zhp, prop, &source); |
2799 | if (str == NULL) | |
34dc7c2f | 2800 | return (-1); |
47dfff3b | 2801 | (void) strlcpy(propbuf, str, proplen); |
d99a0153 | 2802 | zcp_check(zhp, prop, 0, str); |
34dc7c2f BB |
2803 | break; |
2804 | ||
330d06f9 MA |
2805 | case ZFS_PROP_CLONES: |
2806 | if (get_clones_string(zhp, propbuf, proplen) != 0) | |
2807 | return (-1); | |
2808 | break; | |
2809 | ||
34dc7c2f BB |
2810 | case ZFS_PROP_QUOTA: |
2811 | case ZFS_PROP_REFQUOTA: | |
2812 | case ZFS_PROP_RESERVATION: | |
2813 | case ZFS_PROP_REFRESERVATION: | |
2814 | ||
2815 | if (get_numeric_property(zhp, prop, src, &source, &val) != 0) | |
2816 | return (-1); | |
34dc7c2f BB |
2817 | /* |
2818 | * If quota or reservation is 0, we translate this into 'none' | |
2819 | * (unless literal is set), and indicate that it's the default | |
2820 | * value. Otherwise, we print the number nicely and indicate | |
2821 | * that its set locally. | |
2822 | */ | |
2823 | if (val == 0) { | |
2824 | if (literal) | |
2825 | (void) strlcpy(propbuf, "0", proplen); | |
2826 | else | |
2827 | (void) strlcpy(propbuf, "none", proplen); | |
2828 | } else { | |
2829 | if (literal) | |
2830 | (void) snprintf(propbuf, proplen, "%llu", | |
2831 | (u_longlong_t)val); | |
2832 | else | |
e7fbeb60 | 2833 | zfs_nicebytes(val, propbuf, proplen); |
34dc7c2f | 2834 | } |
d99a0153 | 2835 | zcp_check(zhp, prop, val, NULL); |
34dc7c2f BB |
2836 | break; |
2837 | ||
788eb90c JJ |
2838 | case ZFS_PROP_FILESYSTEM_LIMIT: |
2839 | case ZFS_PROP_SNAPSHOT_LIMIT: | |
2840 | case ZFS_PROP_FILESYSTEM_COUNT: | |
2841 | case ZFS_PROP_SNAPSHOT_COUNT: | |
2842 | ||
2843 | if (get_numeric_property(zhp, prop, src, &source, &val) != 0) | |
2844 | return (-1); | |
2845 | ||
2846 | /* | |
2847 | * If limit is UINT64_MAX, we translate this into 'none' (unless | |
2848 | * literal is set), and indicate that it's the default value. | |
2849 | * Otherwise, we print the number nicely and indicate that it's | |
2850 | * set locally. | |
2851 | */ | |
2852 | if (literal) { | |
2853 | (void) snprintf(propbuf, proplen, "%llu", | |
2854 | (u_longlong_t)val); | |
2855 | } else if (val == UINT64_MAX) { | |
2856 | (void) strlcpy(propbuf, "none", proplen); | |
2857 | } else { | |
2858 | zfs_nicenum(val, propbuf, proplen); | |
2859 | } | |
d99a0153 CW |
2860 | |
2861 | zcp_check(zhp, prop, val, NULL); | |
788eb90c JJ |
2862 | break; |
2863 | ||
f5fc4aca | 2864 | case ZFS_PROP_REFRATIO: |
34dc7c2f BB |
2865 | case ZFS_PROP_COMPRESSRATIO: |
2866 | if (get_numeric_property(zhp, prop, src, &source, &val) != 0) | |
2867 | return (-1); | |
b0bd8ffe | 2868 | if (literal) |
2869 | (void) snprintf(propbuf, proplen, "%llu.%02llu", | |
2870 | (u_longlong_t)(val / 100), | |
2871 | (u_longlong_t)(val % 100)); | |
2872 | else | |
2873 | (void) snprintf(propbuf, proplen, "%llu.%02llux", | |
2874 | (u_longlong_t)(val / 100), | |
2875 | (u_longlong_t)(val % 100)); | |
d99a0153 | 2876 | zcp_check(zhp, prop, val, NULL); |
34dc7c2f BB |
2877 | break; |
2878 | ||
2879 | case ZFS_PROP_TYPE: | |
2880 | switch (zhp->zfs_type) { | |
2881 | case ZFS_TYPE_FILESYSTEM: | |
2882 | str = "filesystem"; | |
2883 | break; | |
2884 | case ZFS_TYPE_VOLUME: | |
2885 | str = "volume"; | |
2886 | break; | |
2887 | case ZFS_TYPE_SNAPSHOT: | |
2888 | str = "snapshot"; | |
2889 | break; | |
da536844 MA |
2890 | case ZFS_TYPE_BOOKMARK: |
2891 | str = "bookmark"; | |
2892 | break; | |
34dc7c2f BB |
2893 | default: |
2894 | abort(); | |
2895 | } | |
2896 | (void) snprintf(propbuf, proplen, "%s", str); | |
d99a0153 | 2897 | zcp_check(zhp, prop, 0, propbuf); |
34dc7c2f BB |
2898 | break; |
2899 | ||
2900 | case ZFS_PROP_MOUNTED: | |
2901 | /* | |
2902 | * The 'mounted' property is a pseudo-property that described | |
2903 | * whether the filesystem is currently mounted. Even though | |
2904 | * it's a boolean value, the typical values of "on" and "off" | |
2905 | * don't make sense, so we translate to "yes" and "no". | |
2906 | */ | |
2907 | if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, | |
2908 | src, &source, &val) != 0) | |
2909 | return (-1); | |
2910 | if (val) | |
2911 | (void) strlcpy(propbuf, "yes", proplen); | |
2912 | else | |
2913 | (void) strlcpy(propbuf, "no", proplen); | |
2914 | break; | |
2915 | ||
2916 | case ZFS_PROP_NAME: | |
2917 | /* | |
2918 | * The 'name' property is a pseudo-property derived from the | |
2919 | * dataset name. It is presented as a real property to simplify | |
2920 | * consumers. | |
2921 | */ | |
2922 | (void) strlcpy(propbuf, zhp->zfs_name, proplen); | |
d99a0153 | 2923 | zcp_check(zhp, prop, 0, propbuf); |
34dc7c2f BB |
2924 | break; |
2925 | ||
428870ff BB |
2926 | case ZFS_PROP_MLSLABEL: |
2927 | { | |
d2c15e84 | 2928 | #ifdef HAVE_MLSLABEL |
428870ff BB |
2929 | m_label_t *new_sl = NULL; |
2930 | char *ascii = NULL; /* human readable label */ | |
2931 | ||
2932 | (void) strlcpy(propbuf, | |
2933 | getprop_string(zhp, prop, &source), proplen); | |
2934 | ||
2935 | if (literal || (strcasecmp(propbuf, | |
2936 | ZFS_MLSLABEL_DEFAULT) == 0)) | |
2937 | break; | |
2938 | ||
2939 | /* | |
2940 | * Try to translate the internal hex string to | |
2941 | * human-readable output. If there are any | |
2942 | * problems just use the hex string. | |
2943 | */ | |
2944 | ||
2945 | if (str_to_label(propbuf, &new_sl, MAC_LABEL, | |
2946 | L_NO_CORRECTION, NULL) == -1) { | |
2947 | m_label_free(new_sl); | |
2948 | break; | |
2949 | } | |
2950 | ||
2951 | if (label_to_str(new_sl, &ascii, M_LABEL, | |
2952 | DEF_NAMES) != 0) { | |
2953 | if (ascii) | |
2954 | free(ascii); | |
2955 | m_label_free(new_sl); | |
2956 | break; | |
2957 | } | |
2958 | m_label_free(new_sl); | |
2959 | ||
2960 | (void) strlcpy(propbuf, ascii, proplen); | |
2961 | free(ascii); | |
d2c15e84 BB |
2962 | #else |
2963 | (void) strlcpy(propbuf, | |
2964 | getprop_string(zhp, prop, &source), proplen); | |
2965 | #endif /* HAVE_MLSLABEL */ | |
428870ff BB |
2966 | } |
2967 | break; | |
2968 | ||
08b1b21d | 2969 | case ZFS_PROP_GUID: |
305bc4b3 | 2970 | case ZFS_PROP_CREATETXG: |
08b1b21d GA |
2971 | /* |
2972 | * GUIDs are stored as numbers, but they are identifiers. | |
2973 | * We don't want them to be pretty printed, because pretty | |
2974 | * printing mangles the ID into a truncated and useless value. | |
2975 | */ | |
2976 | if (get_numeric_property(zhp, prop, src, &source, &val) != 0) | |
2977 | return (-1); | |
2978 | (void) snprintf(propbuf, proplen, "%llu", (u_longlong_t)val); | |
d99a0153 | 2979 | zcp_check(zhp, prop, val, NULL); |
08b1b21d GA |
2980 | break; |
2981 | ||
e7fbeb60 | 2982 | case ZFS_PROP_REFERENCED: |
2983 | case ZFS_PROP_AVAILABLE: | |
2984 | case ZFS_PROP_USED: | |
2985 | case ZFS_PROP_USEDSNAP: | |
2986 | case ZFS_PROP_USEDDS: | |
2987 | case ZFS_PROP_USEDREFRESERV: | |
2988 | case ZFS_PROP_USEDCHILD: | |
2989 | if (get_numeric_property(zhp, prop, src, &source, &val) != 0) | |
2990 | return (-1); | |
d99a0153 | 2991 | if (literal) { |
e7fbeb60 | 2992 | (void) snprintf(propbuf, proplen, "%llu", |
2993 | (u_longlong_t)val); | |
d99a0153 | 2994 | } else { |
e7fbeb60 | 2995 | zfs_nicebytes(val, propbuf, proplen); |
d99a0153 CW |
2996 | } |
2997 | zcp_check(zhp, prop, val, NULL); | |
e7fbeb60 | 2998 | break; |
2999 | ||
34dc7c2f BB |
3000 | default: |
3001 | switch (zfs_prop_get_type(prop)) { | |
3002 | case PROP_TYPE_NUMBER: | |
3003 | if (get_numeric_property(zhp, prop, src, | |
d99a0153 | 3004 | &source, &val) != 0) { |
34dc7c2f | 3005 | return (-1); |
d99a0153 CW |
3006 | } |
3007 | ||
3008 | if (literal) { | |
34dc7c2f BB |
3009 | (void) snprintf(propbuf, proplen, "%llu", |
3010 | (u_longlong_t)val); | |
d99a0153 | 3011 | } else { |
34dc7c2f | 3012 | zfs_nicenum(val, propbuf, proplen); |
d99a0153 CW |
3013 | } |
3014 | zcp_check(zhp, prop, val, NULL); | |
34dc7c2f BB |
3015 | break; |
3016 | ||
3017 | case PROP_TYPE_STRING: | |
47dfff3b MA |
3018 | str = getprop_string(zhp, prop, &source); |
3019 | if (str == NULL) | |
3020 | return (-1); | |
d99a0153 | 3021 | |
47dfff3b | 3022 | (void) strlcpy(propbuf, str, proplen); |
d99a0153 | 3023 | zcp_check(zhp, prop, 0, str); |
34dc7c2f BB |
3024 | break; |
3025 | ||
3026 | case PROP_TYPE_INDEX: | |
3027 | if (get_numeric_property(zhp, prop, src, | |
3028 | &source, &val) != 0) | |
3029 | return (-1); | |
3030 | if (zfs_prop_index_to_string(prop, val, &strval) != 0) | |
3031 | return (-1); | |
d99a0153 | 3032 | |
34dc7c2f | 3033 | (void) strlcpy(propbuf, strval, proplen); |
d99a0153 | 3034 | zcp_check(zhp, prop, 0, strval); |
34dc7c2f BB |
3035 | break; |
3036 | ||
3037 | default: | |
3038 | abort(); | |
3039 | } | |
3040 | } | |
3041 | ||
3042 | get_source(zhp, src, source, statbuf, statlen); | |
3043 | ||
3044 | return (0); | |
3045 | } | |
3046 | ||
3047 | /* | |
3048 | * Utility function to get the given numeric property. Does no validation that | |
3049 | * the given property is the appropriate type; should only be used with | |
3050 | * hard-coded property types. | |
3051 | */ | |
3052 | uint64_t | |
3053 | zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) | |
3054 | { | |
3055 | char *source; | |
689f093e | 3056 | uint64_t val = 0; |
34dc7c2f BB |
3057 | |
3058 | (void) get_numeric_property(zhp, prop, NULL, &source, &val); | |
3059 | ||
3060 | return (val); | |
3061 | } | |
3062 | ||
3063 | int | |
3064 | zfs_prop_set_int(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t val) | |
3065 | { | |
3066 | char buf[64]; | |
3067 | ||
9babb374 | 3068 | (void) snprintf(buf, sizeof (buf), "%llu", (longlong_t)val); |
34dc7c2f BB |
3069 | return (zfs_prop_set(zhp, zfs_prop_to_name(prop), buf)); |
3070 | } | |
3071 | ||
3072 | /* | |
3073 | * Similar to zfs_prop_get(), but returns the value as an integer. | |
3074 | */ | |
3075 | int | |
3076 | zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, | |
3077 | zprop_source_t *src, char *statbuf, size_t statlen) | |
3078 | { | |
3079 | char *source; | |
3080 | ||
3081 | /* | |
3082 | * Check to see if this property applies to our object | |
3083 | */ | |
962d5242 | 3084 | if (!zfs_prop_valid_for_type(prop, zhp->zfs_type, B_FALSE)) { |
34dc7c2f BB |
3085 | return (zfs_error_fmt(zhp->zfs_hdl, EZFS_PROPTYPE, |
3086 | dgettext(TEXT_DOMAIN, "cannot get property '%s'"), | |
3087 | zfs_prop_to_name(prop))); | |
3088 | } | |
3089 | ||
3090 | if (src) | |
3091 | *src = ZPROP_SRC_NONE; | |
3092 | ||
3093 | if (get_numeric_property(zhp, prop, src, &source, value) != 0) | |
3094 | return (-1); | |
3095 | ||
3096 | get_source(zhp, src, source, statbuf, statlen); | |
3097 | ||
3098 | return (0); | |
3099 | } | |
3100 | ||
be160928 | 3101 | #ifdef HAVE_IDMAP |
9babb374 BB |
3102 | static int |
3103 | idmap_id_to_numeric_domain_rid(uid_t id, boolean_t isuser, | |
3104 | char **domainp, idmap_rid_t *ridp) | |
3105 | { | |
9babb374 BB |
3106 | idmap_get_handle_t *get_hdl = NULL; |
3107 | idmap_stat status; | |
3108 | int err = EINVAL; | |
3109 | ||
572e2857 | 3110 | if (idmap_get_create(&get_hdl) != IDMAP_SUCCESS) |
9babb374 BB |
3111 | goto out; |
3112 | ||
3113 | if (isuser) { | |
3114 | err = idmap_get_sidbyuid(get_hdl, id, | |
3115 | IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); | |
3116 | } else { | |
3117 | err = idmap_get_sidbygid(get_hdl, id, | |
3118 | IDMAP_REQ_FLG_USE_CACHE, domainp, ridp, &status); | |
3119 | } | |
3120 | if (err == IDMAP_SUCCESS && | |
3121 | idmap_get_mappings(get_hdl) == IDMAP_SUCCESS && | |
3122 | status == IDMAP_SUCCESS) | |
3123 | err = 0; | |
3124 | else | |
3125 | err = EINVAL; | |
3126 | out: | |
3127 | if (get_hdl) | |
3128 | idmap_get_destroy(get_hdl); | |
9babb374 BB |
3129 | return (err); |
3130 | } | |
be160928 | 3131 | #endif /* HAVE_IDMAP */ |
9babb374 BB |
3132 | |
3133 | /* | |
3134 | * convert the propname into parameters needed by kernel | |
3135 | * Eg: userquota@ahrens -> ZFS_PROP_USERQUOTA, "", 126829 | |
3136 | * Eg: userused@matt@domain -> ZFS_PROP_USERUSED, "S-1-123-456", 789 | |
ada8ec1e SC |
3137 | * Eg: groupquota@staff -> ZFS_PROP_GROUPQUOTA, "", 1234 |
3138 | * Eg: groupused@staff -> ZFS_PROP_GROUPUSED, "", 1234 | |
9c5167d1 NF |
3139 | * Eg: projectquota@123 -> ZFS_PROP_PROJECTQUOTA, "", 123 |
3140 | * Eg: projectused@789 -> ZFS_PROP_PROJECTUSED, "", 789 | |
9babb374 BB |
3141 | */ |
3142 | static int | |
3143 | userquota_propname_decode(const char *propname, boolean_t zoned, | |
3144 | zfs_userquota_prop_t *typep, char *domain, int domainlen, uint64_t *ridp) | |
3145 | { | |
3146 | zfs_userquota_prop_t type; | |
ada8ec1e | 3147 | char *cp; |
9babb374 | 3148 | boolean_t isuser; |
ada8ec1e | 3149 | boolean_t isgroup; |
9c5167d1 | 3150 | boolean_t isproject; |
ada8ec1e SC |
3151 | struct passwd *pw; |
3152 | struct group *gr; | |
9babb374 BB |
3153 | |
3154 | domain[0] = '\0'; | |
3155 | ||
9c5167d1 | 3156 | /* Figure out the property type ({user|group|project}{quota|space}) */ |
9babb374 BB |
3157 | for (type = 0; type < ZFS_NUM_USERQUOTA_PROPS; type++) { |
3158 | if (strncmp(propname, zfs_userquota_prop_prefixes[type], | |
3159 | strlen(zfs_userquota_prop_prefixes[type])) == 0) | |
3160 | break; | |
3161 | } | |
3162 | if (type == ZFS_NUM_USERQUOTA_PROPS) | |
3163 | return (EINVAL); | |
3164 | *typep = type; | |
3165 | ||
1de321e6 | 3166 | isuser = (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_USERUSED || |
02730c33 BB |
3167 | type == ZFS_PROP_USEROBJQUOTA || |
3168 | type == ZFS_PROP_USEROBJUSED); | |
1de321e6 | 3169 | isgroup = (type == ZFS_PROP_GROUPQUOTA || type == ZFS_PROP_GROUPUSED || |
02730c33 BB |
3170 | type == ZFS_PROP_GROUPOBJQUOTA || |
3171 | type == ZFS_PROP_GROUPOBJUSED); | |
9c5167d1 NF |
3172 | isproject = (type == ZFS_PROP_PROJECTQUOTA || |
3173 | type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTOBJQUOTA || | |
3174 | type == ZFS_PROP_PROJECTOBJUSED); | |
9babb374 BB |
3175 | |
3176 | cp = strchr(propname, '@') + 1; | |
3177 | ||
ada8ec1e SC |
3178 | if (isuser && (pw = getpwnam(cp)) != NULL) { |
3179 | if (zoned && getzoneid() == GLOBAL_ZONEID) | |
3180 | return (ENOENT); | |
3181 | *ridp = pw->pw_uid; | |
3182 | } else if (isgroup && (gr = getgrnam(cp)) != NULL) { | |
3183 | if (zoned && getzoneid() == GLOBAL_ZONEID) | |
3184 | return (ENOENT); | |
3185 | *ridp = gr->gr_gid; | |
9c5167d1 | 3186 | } else if (!isproject && strchr(cp, '@')) { |
be160928 | 3187 | #ifdef HAVE_IDMAP |
9babb374 BB |
3188 | /* |
3189 | * It's a SID name (eg "user@domain") that needs to be | |
45d1cae3 | 3190 | * turned into S-1-domainID-RID. |
9babb374 | 3191 | */ |
45d1cae3 | 3192 | directory_error_t e; |
ada8ec1e SC |
3193 | char *numericsid = NULL; |
3194 | char *end; | |
3195 | ||
9babb374 BB |
3196 | if (zoned && getzoneid() == GLOBAL_ZONEID) |
3197 | return (ENOENT); | |
45d1cae3 BB |
3198 | if (isuser) { |
3199 | e = directory_sid_from_user_name(NULL, | |
3200 | cp, &numericsid); | |
3201 | } else { | |
3202 | e = directory_sid_from_group_name(NULL, | |
3203 | cp, &numericsid); | |
3204 | } | |
3205 | if (e != NULL) { | |
3206 | directory_error_free(e); | |
9babb374 | 3207 | return (ENOENT); |
45d1cae3 BB |
3208 | } |
3209 | if (numericsid == NULL) | |
9babb374 | 3210 | return (ENOENT); |
45d1cae3 | 3211 | cp = numericsid; |
45d1cae3 | 3212 | (void) strlcpy(domain, cp, domainlen); |
9babb374 BB |
3213 | cp = strrchr(domain, '-'); |
3214 | *cp = '\0'; | |
3215 | cp++; | |
3216 | ||
3217 | errno = 0; | |
3218 | *ridp = strtoull(cp, &end, 10); | |
ada8ec1e SC |
3219 | free(numericsid); |
3220 | ||
9babb374 BB |
3221 | if (errno != 0 || *end != '\0') |
3222 | return (EINVAL); | |
ada8ec1e SC |
3223 | #else |
3224 | return (ENOSYS); | |
3225 | #endif /* HAVE_IDMAP */ | |
9babb374 | 3226 | } else { |
9c5167d1 | 3227 | /* It's a user/group/project ID (eg "12345"). */ |
ada8ec1e | 3228 | uid_t id; |
ada8ec1e | 3229 | char *end; |
ada8ec1e | 3230 | id = strtoul(cp, &end, 10); |
9babb374 BB |
3231 | if (*end != '\0') |
3232 | return (EINVAL); | |
9c5167d1 | 3233 | if (id > MAXUID && !isproject) { |
5e6320cd | 3234 | #ifdef HAVE_IDMAP |
9babb374 | 3235 | /* It's an ephemeral ID. */ |
5e6320cd MM |
3236 | idmap_rid_t rid; |
3237 | char *mapdomain; | |
3238 | ||
9babb374 BB |
3239 | if (idmap_id_to_numeric_domain_rid(id, isuser, |
3240 | &mapdomain, &rid) != 0) | |
3241 | return (ENOENT); | |
45d1cae3 | 3242 | (void) strlcpy(domain, mapdomain, domainlen); |
9babb374 | 3243 | *ridp = rid; |
5e6320cd MM |
3244 | #else |
3245 | return (ENOSYS); | |
3246 | #endif /* HAVE_IDMAP */ | |
9babb374 BB |
3247 | } else { |
3248 | *ridp = id; | |
3249 | } | |
3250 | } | |
3251 | ||
3252 | return (0); | |
3253 | } | |
3254 | ||
3255 | static int | |
3256 | zfs_prop_get_userquota_common(zfs_handle_t *zhp, const char *propname, | |
3257 | uint64_t *propvalue, zfs_userquota_prop_t *typep) | |
3258 | { | |
3259 | int err; | |
13fe0198 | 3260 | zfs_cmd_t zc = {"\0"}; |
9babb374 | 3261 | |
330d06f9 | 3262 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
9babb374 BB |
3263 | |
3264 | err = userquota_propname_decode(propname, | |
3265 | zfs_prop_get_int(zhp, ZFS_PROP_ZONED), | |
3266 | typep, zc.zc_value, sizeof (zc.zc_value), &zc.zc_guid); | |
3267 | zc.zc_objset_type = *typep; | |
3268 | if (err) | |
3269 | return (err); | |
3270 | ||
3271 | err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_USERSPACE_ONE, &zc); | |
3272 | if (err) | |
3273 | return (err); | |
3274 | ||
3275 | *propvalue = zc.zc_cookie; | |
3276 | return (0); | |
3277 | } | |
3278 | ||
3279 | int | |
3280 | zfs_prop_get_userquota_int(zfs_handle_t *zhp, const char *propname, | |
3281 | uint64_t *propvalue) | |
3282 | { | |
3283 | zfs_userquota_prop_t type; | |
3284 | ||
3285 | return (zfs_prop_get_userquota_common(zhp, propname, propvalue, | |
3286 | &type)); | |
3287 | } | |
3288 | ||
3289 | int | |
3290 | zfs_prop_get_userquota(zfs_handle_t *zhp, const char *propname, | |
3291 | char *propbuf, int proplen, boolean_t literal) | |
3292 | { | |
3293 | int err; | |
3294 | uint64_t propvalue; | |
3295 | zfs_userquota_prop_t type; | |
3296 | ||
3297 | err = zfs_prop_get_userquota_common(zhp, propname, &propvalue, | |
3298 | &type); | |
3299 | ||
3300 | if (err) | |
3301 | return (err); | |
3302 | ||
3303 | if (literal) { | |
b8864a23 | 3304 | (void) snprintf(propbuf, proplen, "%llu", |
ba6a2402 | 3305 | (u_longlong_t)propvalue); |
9babb374 | 3306 | } else if (propvalue == 0 && |
1de321e6 | 3307 | (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA || |
9c5167d1 | 3308 | type == ZFS_PROP_USEROBJQUOTA || type == ZFS_PROP_GROUPOBJQUOTA || |
2705ebf0 NF |
3309 | type == ZFS_PROP_PROJECTQUOTA || |
3310 | type == ZFS_PROP_PROJECTOBJQUOTA)) { | |
9babb374 | 3311 | (void) strlcpy(propbuf, "none", proplen); |
e7fbeb60 | 3312 | } else if (type == ZFS_PROP_USERQUOTA || type == ZFS_PROP_GROUPQUOTA || |
9c5167d1 NF |
3313 | type == ZFS_PROP_USERUSED || type == ZFS_PROP_GROUPUSED || |
3314 | type == ZFS_PROP_PROJECTUSED || type == ZFS_PROP_PROJECTQUOTA) { | |
e7fbeb60 | 3315 | zfs_nicebytes(propvalue, propbuf, proplen); |
9babb374 BB |
3316 | } else { |
3317 | zfs_nicenum(propvalue, propbuf, proplen); | |
3318 | } | |
3319 | return (0); | |
3320 | } | |
3321 | ||
330d06f9 MA |
3322 | int |
3323 | zfs_prop_get_written_int(zfs_handle_t *zhp, const char *propname, | |
3324 | uint64_t *propvalue) | |
34dc7c2f | 3325 | { |
330d06f9 | 3326 | int err; |
13fe0198 | 3327 | zfs_cmd_t zc = {"\0"}; |
330d06f9 | 3328 | const char *snapname; |
34dc7c2f | 3329 | |
330d06f9 | 3330 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
34dc7c2f | 3331 | |
330d06f9 MA |
3332 | snapname = strchr(propname, '@') + 1; |
3333 | if (strchr(snapname, '@')) { | |
3334 | (void) strlcpy(zc.zc_value, snapname, sizeof (zc.zc_value)); | |
3335 | } else { | |
3336 | /* snapname is the short name, append it to zhp's fsname */ | |
3337 | char *cp; | |
3338 | ||
3339 | (void) strlcpy(zc.zc_value, zhp->zfs_name, | |
3340 | sizeof (zc.zc_value)); | |
3341 | cp = strchr(zc.zc_value, '@'); | |
3342 | if (cp != NULL) | |
3343 | *cp = '\0'; | |
3344 | (void) strlcat(zc.zc_value, "@", sizeof (zc.zc_value)); | |
3345 | (void) strlcat(zc.zc_value, snapname, sizeof (zc.zc_value)); | |
3346 | } | |
fb5f0bc8 | 3347 | |
330d06f9 MA |
3348 | err = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SPACE_WRITTEN, &zc); |
3349 | if (err) | |
3350 | return (err); | |
fb5f0bc8 | 3351 | |
330d06f9 MA |
3352 | *propvalue = zc.zc_cookie; |
3353 | return (0); | |
fb5f0bc8 BB |
3354 | } |
3355 | ||
34dc7c2f | 3356 | int |
330d06f9 MA |
3357 | zfs_prop_get_written(zfs_handle_t *zhp, const char *propname, |
3358 | char *propbuf, int proplen, boolean_t literal) | |
34dc7c2f | 3359 | { |
330d06f9 MA |
3360 | int err; |
3361 | uint64_t propvalue; | |
34dc7c2f | 3362 | |
330d06f9 | 3363 | err = zfs_prop_get_written_int(zhp, propname, &propvalue); |
fb5f0bc8 | 3364 | |
330d06f9 MA |
3365 | if (err) |
3366 | return (err); | |
34dc7c2f | 3367 | |
330d06f9 | 3368 | if (literal) { |
ba6a2402 BB |
3369 | (void) snprintf(propbuf, proplen, "%llu", |
3370 | (u_longlong_t)propvalue); | |
330d06f9 | 3371 | } else { |
e7fbeb60 | 3372 | zfs_nicebytes(propvalue, propbuf, proplen); |
34dc7c2f | 3373 | } |
330d06f9 MA |
3374 | |
3375 | return (0); | |
34dc7c2f BB |
3376 | } |
3377 | ||
34dc7c2f | 3378 | /* |
330d06f9 | 3379 | * Returns the name of the given zfs handle. |
34dc7c2f | 3380 | */ |
330d06f9 MA |
3381 | const char * |
3382 | zfs_get_name(const zfs_handle_t *zhp) | |
34dc7c2f | 3383 | { |
330d06f9 MA |
3384 | return (zhp->zfs_name); |
3385 | } | |
34dc7c2f | 3386 | |
d21d5b82 GDN |
3387 | /* |
3388 | * Returns the name of the parent pool for the given zfs handle. | |
3389 | */ | |
3390 | const char * | |
3391 | zfs_get_pool_name(const zfs_handle_t *zhp) | |
3392 | { | |
3393 | return (zhp->zpool_hdl->zpool_name); | |
3394 | } | |
3395 | ||
330d06f9 MA |
3396 | /* |
3397 | * Returns the type of the given zfs handle. | |
3398 | */ | |
3399 | zfs_type_t | |
3400 | zfs_get_type(const zfs_handle_t *zhp) | |
3401 | { | |
3402 | return (zhp->zfs_type); | |
34dc7c2f BB |
3403 | } |
3404 | ||
428870ff BB |
3405 | /* |
3406 | * Is one dataset name a child dataset of another? | |
3407 | * | |
3408 | * Needs to handle these cases: | |
3409 | * Dataset 1 "a/foo" "a/foo" "a/foo" "a/foo" | |
3410 | * Dataset 2 "a/fo" "a/foobar" "a/bar/baz" "a/foo/bar" | |
3411 | * Descendant? No. No. No. Yes. | |
3412 | */ | |
3413 | static boolean_t | |
3414 | is_descendant(const char *ds1, const char *ds2) | |
3415 | { | |
3416 | size_t d1len = strlen(ds1); | |
3417 | ||
3418 | /* ds2 can't be a descendant if it's smaller */ | |
3419 | if (strlen(ds2) < d1len) | |
3420 | return (B_FALSE); | |
3421 | ||
3422 | /* otherwise, compare strings and verify that there's a '/' char */ | |
3423 | return (ds2[d1len] == '/' && (strncmp(ds1, ds2, d1len) == 0)); | |
3424 | } | |
3425 | ||
34dc7c2f BB |
3426 | /* |
3427 | * Given a complete name, return just the portion that refers to the parent. | |
330d06f9 MA |
3428 | * Will return -1 if there is no parent (path is just the name of the |
3429 | * pool). | |
34dc7c2f BB |
3430 | */ |
3431 | static int | |
3432 | parent_name(const char *path, char *buf, size_t buflen) | |
3433 | { | |
330d06f9 | 3434 | char *slashp; |
34dc7c2f | 3435 | |
330d06f9 | 3436 | (void) strlcpy(buf, path, buflen); |
34dc7c2f | 3437 | |
330d06f9 MA |
3438 | if ((slashp = strrchr(buf, '/')) == NULL) |
3439 | return (-1); | |
3440 | *slashp = '\0'; | |
34dc7c2f BB |
3441 | |
3442 | return (0); | |
3443 | } | |
3444 | ||
b5256303 TC |
3445 | int |
3446 | zfs_parent_name(zfs_handle_t *zhp, char *buf, size_t buflen) | |
3447 | { | |
3448 | return (parent_name(zfs_get_name(zhp), buf, buflen)); | |
3449 | } | |
3450 | ||
34dc7c2f BB |
3451 | /* |
3452 | * If accept_ancestor is false, then check to make sure that the given path has | |
3453 | * a parent, and that it exists. If accept_ancestor is true, then find the | |
3454 | * closest existing ancestor for the given path. In prefixlen return the | |
3455 | * length of already existing prefix of the given path. We also fetch the | |
3456 | * 'zoned' property, which is used to validate property settings when creating | |
3457 | * new datasets. | |
3458 | */ | |
3459 | static int | |
3460 | check_parents(libzfs_handle_t *hdl, const char *path, uint64_t *zoned, | |
3461 | boolean_t accept_ancestor, int *prefixlen) | |
3462 | { | |
13fe0198 | 3463 | zfs_cmd_t zc = {"\0"}; |
eca7b760 | 3464 | char parent[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f BB |
3465 | char *slash; |
3466 | zfs_handle_t *zhp; | |
3467 | char errbuf[1024]; | |
428870ff | 3468 | uint64_t is_zoned; |
34dc7c2f | 3469 | |
fb5f0bc8 BB |
3470 | (void) snprintf(errbuf, sizeof (errbuf), |
3471 | dgettext(TEXT_DOMAIN, "cannot create '%s'"), path); | |
34dc7c2f BB |
3472 | |
3473 | /* get parent, and check to see if this is just a pool */ | |
3474 | if (parent_name(path, parent, sizeof (parent)) != 0) { | |
3475 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3476 | "missing dataset name")); | |
3477 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
3478 | } | |
3479 | ||
3480 | /* check to see if the pool exists */ | |
3481 | if ((slash = strchr(parent, '/')) == NULL) | |
3482 | slash = parent + strlen(parent); | |
3483 | (void) strncpy(zc.zc_name, parent, slash - parent); | |
3484 | zc.zc_name[slash - parent] = '\0'; | |
3485 | if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && | |
3486 | errno == ENOENT) { | |
3487 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3488 | "no such pool '%s'"), zc.zc_name); | |
3489 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
3490 | } | |
3491 | ||
3492 | /* check to see if the parent dataset exists */ | |
3493 | while ((zhp = make_dataset_handle(hdl, parent)) == NULL) { | |
3494 | if (errno == ENOENT && accept_ancestor) { | |
3495 | /* | |
3496 | * Go deeper to find an ancestor, give up on top level. | |
3497 | */ | |
3498 | if (parent_name(parent, parent, sizeof (parent)) != 0) { | |
3499 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3500 | "no such pool '%s'"), zc.zc_name); | |
3501 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
3502 | } | |
3503 | } else if (errno == ENOENT) { | |
3504 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3505 | "parent does not exist")); | |
3506 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
3507 | } else | |
3508 | return (zfs_standard_error(hdl, errno, errbuf)); | |
3509 | } | |
3510 | ||
428870ff BB |
3511 | is_zoned = zfs_prop_get_int(zhp, ZFS_PROP_ZONED); |
3512 | if (zoned != NULL) | |
3513 | *zoned = is_zoned; | |
3514 | ||
34dc7c2f | 3515 | /* we are in a non-global zone, but parent is in the global zone */ |
428870ff | 3516 | if (getzoneid() != GLOBAL_ZONEID && !is_zoned) { |
34dc7c2f BB |
3517 | (void) zfs_standard_error(hdl, EPERM, errbuf); |
3518 | zfs_close(zhp); | |
3519 | return (-1); | |
3520 | } | |
3521 | ||
3522 | /* make sure parent is a filesystem */ | |
3523 | if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { | |
3524 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3525 | "parent is not a filesystem")); | |
3526 | (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); | |
3527 | zfs_close(zhp); | |
3528 | return (-1); | |
3529 | } | |
3530 | ||
3531 | zfs_close(zhp); | |
3532 | if (prefixlen != NULL) | |
3533 | *prefixlen = strlen(parent); | |
3534 | return (0); | |
3535 | } | |
3536 | ||
3537 | /* | |
3538 | * Finds whether the dataset of the given type(s) exists. | |
3539 | */ | |
3540 | boolean_t | |
3541 | zfs_dataset_exists(libzfs_handle_t *hdl, const char *path, zfs_type_t types) | |
3542 | { | |
3543 | zfs_handle_t *zhp; | |
3544 | ||
3545 | if (!zfs_validate_name(hdl, path, types, B_FALSE)) | |
3546 | return (B_FALSE); | |
3547 | ||
3548 | /* | |
3549 | * Try to get stats for the dataset, which will tell us if it exists. | |
3550 | */ | |
3551 | if ((zhp = make_dataset_handle(hdl, path)) != NULL) { | |
3552 | int ds_type = zhp->zfs_type; | |
3553 | ||
3554 | zfs_close(zhp); | |
3555 | if (types & ds_type) | |
3556 | return (B_TRUE); | |
3557 | } | |
3558 | return (B_FALSE); | |
3559 | } | |
3560 | ||
3561 | /* | |
3562 | * Given a path to 'target', create all the ancestors between | |
3563 | * the prefixlen portion of the path, and the target itself. | |
3564 | * Fail if the initial prefixlen-ancestor does not already exist. | |
3565 | */ | |
3566 | int | |
3567 | create_parents(libzfs_handle_t *hdl, char *target, int prefixlen) | |
3568 | { | |
3569 | zfs_handle_t *h; | |
3570 | char *cp; | |
3571 | const char *opname; | |
3572 | ||
3573 | /* make sure prefix exists */ | |
3574 | cp = target + prefixlen; | |
3575 | if (*cp != '/') { | |
3576 | assert(strchr(cp, '/') == NULL); | |
3577 | h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); | |
3578 | } else { | |
3579 | *cp = '\0'; | |
3580 | h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); | |
3581 | *cp = '/'; | |
3582 | } | |
3583 | if (h == NULL) | |
3584 | return (-1); | |
3585 | zfs_close(h); | |
3586 | ||
3587 | /* | |
3588 | * Attempt to create, mount, and share any ancestor filesystems, | |
3589 | * up to the prefixlen-long one. | |
3590 | */ | |
3591 | for (cp = target + prefixlen + 1; | |
23d70cde | 3592 | (cp = strchr(cp, '/')) != NULL; *cp = '/', cp++) { |
34dc7c2f BB |
3593 | |
3594 | *cp = '\0'; | |
3595 | ||
3596 | h = make_dataset_handle(hdl, target); | |
3597 | if (h) { | |
3598 | /* it already exists, nothing to do here */ | |
3599 | zfs_close(h); | |
3600 | continue; | |
3601 | } | |
3602 | ||
34dc7c2f BB |
3603 | if (zfs_create(hdl, target, ZFS_TYPE_FILESYSTEM, |
3604 | NULL) != 0) { | |
34dc7c2f BB |
3605 | opname = dgettext(TEXT_DOMAIN, "create"); |
3606 | goto ancestorerr; | |
3607 | } | |
3608 | ||
34dc7c2f BB |
3609 | h = zfs_open(hdl, target, ZFS_TYPE_FILESYSTEM); |
3610 | if (h == NULL) { | |
3611 | opname = dgettext(TEXT_DOMAIN, "open"); | |
3612 | goto ancestorerr; | |
3613 | } | |
3614 | ||
3615 | if (zfs_mount(h, NULL, 0) != 0) { | |
3616 | opname = dgettext(TEXT_DOMAIN, "mount"); | |
3617 | goto ancestorerr; | |
3618 | } | |
3619 | ||
3620 | if (zfs_share(h) != 0) { | |
3621 | opname = dgettext(TEXT_DOMAIN, "share"); | |
3622 | goto ancestorerr; | |
3623 | } | |
3624 | ||
3625 | zfs_close(h); | |
3626 | } | |
3627 | ||
3628 | return (0); | |
3629 | ||
3630 | ancestorerr: | |
3631 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3632 | "failed to %s ancestor '%s'"), opname, target); | |
3633 | return (-1); | |
3634 | } | |
3635 | ||
3636 | /* | |
3637 | * Creates non-existing ancestors of the given path. | |
3638 | */ | |
3639 | int | |
3640 | zfs_create_ancestors(libzfs_handle_t *hdl, const char *path) | |
3641 | { | |
3642 | int prefix; | |
34dc7c2f | 3643 | char *path_copy; |
a7ed98d8 | 3644 | char errbuf[1024]; |
d4ed6673 | 3645 | int rc = 0; |
34dc7c2f | 3646 | |
a7ed98d8 SD |
3647 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
3648 | "cannot create '%s'"), path); | |
3649 | ||
3650 | /* | |
3651 | * Check that we are not passing the nesting limit | |
3652 | * before we start creating any ancestors. | |
3653 | */ | |
3654 | if (dataset_nestcheck(path) != 0) { | |
3655 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3656 | "maximum name nesting depth exceeded")); | |
3657 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
3658 | } | |
3659 | ||
428870ff | 3660 | if (check_parents(hdl, path, NULL, B_TRUE, &prefix) != 0) |
34dc7c2f BB |
3661 | return (-1); |
3662 | ||
3663 | if ((path_copy = strdup(path)) != NULL) { | |
3664 | rc = create_parents(hdl, path_copy, prefix); | |
3665 | free(path_copy); | |
3666 | } | |
3667 | if (path_copy == NULL || rc != 0) | |
3668 | return (-1); | |
3669 | ||
3670 | return (0); | |
3671 | } | |
3672 | ||
3673 | /* | |
3674 | * Create a new filesystem or volume. | |
3675 | */ | |
3676 | int | |
3677 | zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, | |
3678 | nvlist_t *props) | |
3679 | { | |
34dc7c2f BB |
3680 | int ret; |
3681 | uint64_t size = 0; | |
3682 | uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); | |
34dc7c2f | 3683 | uint64_t zoned; |
e67a7ffb | 3684 | enum lzc_dataset_type ost; |
78d95eaa | 3685 | zpool_handle_t *zpool_handle; |
b5256303 TC |
3686 | uint8_t *wkeydata = NULL; |
3687 | uint_t wkeylen = 0; | |
3688 | char errbuf[1024]; | |
3689 | char parent[ZFS_MAX_DATASET_NAME_LEN]; | |
34dc7c2f BB |
3690 | |
3691 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
3692 | "cannot create '%s'"), path); | |
3693 | ||
3694 | /* validate the path, taking care to note the extended error message */ | |
3695 | if (!zfs_validate_name(hdl, path, type, B_TRUE)) | |
3696 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
3697 | ||
a7ed98d8 SD |
3698 | if (dataset_nestcheck(path) != 0) { |
3699 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3700 | "maximum name nesting depth exceeded")); | |
3701 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
3702 | } | |
3703 | ||
34dc7c2f BB |
3704 | /* validate parents exist */ |
3705 | if (check_parents(hdl, path, &zoned, B_FALSE, NULL) != 0) | |
3706 | return (-1); | |
3707 | ||
3708 | /* | |
3709 | * The failure modes when creating a dataset of a different type over | |
3710 | * one that already exists is a little strange. In particular, if you | |
3711 | * try to create a dataset on top of an existing dataset, the ioctl() | |
3712 | * will return ENOENT, not EEXIST. To prevent this from happening, we | |
3713 | * first try to see if the dataset exists. | |
3714 | */ | |
6f1ffb06 | 3715 | if (zfs_dataset_exists(hdl, path, ZFS_TYPE_DATASET)) { |
34dc7c2f BB |
3716 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
3717 | "dataset already exists")); | |
3718 | return (zfs_error(hdl, EZFS_EXISTS, errbuf)); | |
3719 | } | |
3720 | ||
3721 | if (type == ZFS_TYPE_VOLUME) | |
e67a7ffb | 3722 | ost = LZC_DATSET_TYPE_ZVOL; |
34dc7c2f | 3723 | else |
e67a7ffb | 3724 | ost = LZC_DATSET_TYPE_ZFS; |
34dc7c2f | 3725 | |
82f6f6e6 | 3726 | /* open zpool handle for prop validation */ |
eca7b760 | 3727 | char pool_path[ZFS_MAX_DATASET_NAME_LEN]; |
82f6f6e6 JS |
3728 | (void) strlcpy(pool_path, path, sizeof (pool_path)); |
3729 | ||
3730 | /* truncate pool_path at first slash */ | |
3731 | char *p = strchr(pool_path, '/'); | |
3732 | if (p != NULL) | |
3733 | *p = '\0'; | |
3734 | ||
78d95eaa | 3735 | if ((zpool_handle = zpool_open(hdl, pool_path)) == NULL) |
3736 | return (-1); | |
82f6f6e6 | 3737 | |
b128c09f | 3738 | if (props && (props = zfs_valid_proplist(hdl, type, props, |
b5256303 | 3739 | zoned, NULL, zpool_handle, B_TRUE, errbuf)) == 0) { |
82f6f6e6 | 3740 | zpool_close(zpool_handle); |
34dc7c2f | 3741 | return (-1); |
82f6f6e6 JS |
3742 | } |
3743 | zpool_close(zpool_handle); | |
34dc7c2f BB |
3744 | |
3745 | if (type == ZFS_TYPE_VOLUME) { | |
3746 | /* | |
3747 | * If we are creating a volume, the size and block size must | |
3748 | * satisfy a few restraints. First, the blocksize must be a | |
3749 | * valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the | |
3750 | * volsize must be a multiple of the block size, and cannot be | |
3751 | * zero. | |
3752 | */ | |
3753 | if (props == NULL || nvlist_lookup_uint64(props, | |
3754 | zfs_prop_to_name(ZFS_PROP_VOLSIZE), &size) != 0) { | |
3755 | nvlist_free(props); | |
3756 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3757 | "missing volume size")); | |
3758 | return (zfs_error(hdl, EZFS_BADPROP, errbuf)); | |
3759 | } | |
3760 | ||
3761 | if ((ret = nvlist_lookup_uint64(props, | |
3762 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), | |
3763 | &blocksize)) != 0) { | |
3764 | if (ret == ENOENT) { | |
3765 | blocksize = zfs_prop_default_numeric( | |
3766 | ZFS_PROP_VOLBLOCKSIZE); | |
3767 | } else { | |
3768 | nvlist_free(props); | |
3769 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3770 | "missing volume block size")); | |
3771 | return (zfs_error(hdl, EZFS_BADPROP, errbuf)); | |
3772 | } | |
3773 | } | |
3774 | ||
3775 | if (size == 0) { | |
3776 | nvlist_free(props); | |
3777 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3778 | "volume size cannot be zero")); | |
3779 | return (zfs_error(hdl, EZFS_BADPROP, errbuf)); | |
3780 | } | |
3781 | ||
3782 | if (size % blocksize != 0) { | |
3783 | nvlist_free(props); | |
3784 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3785 | "volume size must be a multiple of volume block " | |
3786 | "size")); | |
3787 | return (zfs_error(hdl, EZFS_BADPROP, errbuf)); | |
3788 | } | |
3789 | } | |
3790 | ||
b5256303 | 3791 | (void) parent_name(path, parent, sizeof (parent)); |
d9c460a0 TC |
3792 | if (zfs_crypto_create(hdl, parent, props, NULL, B_TRUE, |
3793 | &wkeydata, &wkeylen) != 0) { | |
b5256303 TC |
3794 | nvlist_free(props); |
3795 | return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); | |
3796 | } | |
3797 | ||
34dc7c2f | 3798 | /* create the dataset */ |
b5256303 | 3799 | ret = lzc_create(path, ost, props, wkeydata, wkeylen); |
6f1ffb06 | 3800 | nvlist_free(props); |
b5256303 TC |
3801 | if (wkeydata != NULL) |
3802 | free(wkeydata); | |
34dc7c2f | 3803 | |
34dc7c2f BB |
3804 | /* check for failure */ |
3805 | if (ret != 0) { | |
34dc7c2f BB |
3806 | switch (errno) { |
3807 | case ENOENT: | |
3808 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3809 | "no such parent '%s'"), parent); | |
3810 | return (zfs_error(hdl, EZFS_NOENT, errbuf)); | |
3811 | ||
3812 | case EINVAL: | |
3813 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3814 | "parent '%s' is not a filesystem"), parent); | |
3815 | return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); | |
3816 | ||
34dc7c2f BB |
3817 | case ENOTSUP: |
3818 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3819 | "pool must be upgraded to set this " | |
3820 | "property or value")); | |
3821 | return (zfs_error(hdl, EZFS_BADVERSION, errbuf)); | |
b5256303 TC |
3822 | |
3823 | case EACCES: | |
3824 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3825 | "encryption root's key is not loaded " | |
3826 | "or provided")); | |
3827 | return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); | |
3828 | ||
bcb1a8a2 YP |
3829 | case ERANGE: |
3830 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
3831 | "invalid property value(s) specified")); | |
3832 | return (zfs_error(hdl, EZFS_BADPROP, errbuf)); | |
34dc7c2f BB |
3833 | #ifdef _ILP32 |
3834 | case EOVERFLOW: | |
3835 | /* | |
3836 | * This platform can't address a volume this big. | |
3837 | */ | |
3838 | if (type == ZFS_TYPE_VOLUME) | |
3839 | return (zfs_error(hdl, EZFS_VOLTOOBIG, | |
3840 | errbuf)); | |
3841 | #endif | |
3842 | /* FALLTHROUGH */ | |
3843 | default: | |
3844 | return (zfs_standard_error(hdl, errno, errbuf)); | |
3845 | } | |
3846 | } | |
3847 | ||
3848 | return (0); | |
3849 | } | |
3850 | ||
3851 | /* | |
3852 | * Destroys the given dataset. The caller must make sure that the filesystem | |
e956d651 CS |
3853 | * isn't mounted, and that there are no active dependents. If the file system |
3854 | * does not exist this function does nothing. | |
34dc7c2f BB |
3855 | */ |
3856 | int | |
45d1cae3 | 3857 | zfs_destroy(zfs_handle_t *zhp, boolean_t defer) |
34dc7c2f | 3858 | { |
13fe0198 | 3859 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f | 3860 | |
da536844 MA |
3861 | if (zhp->zfs_type == ZFS_TYPE_BOOKMARK) { |
3862 | nvlist_t *nv = fnvlist_alloc(); | |
3863 | fnvlist_add_boolean(nv, zhp->zfs_name); | |
3864 | int error = lzc_destroy_bookmarks(nv, NULL); | |
3865 | fnvlist_free(nv); | |
3866 | if (error != 0) { | |
3867 | return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, | |
3868 | dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), | |
3869 | zhp->zfs_name)); | |
3870 | } | |
3871 | return (0); | |
3872 | } | |
3873 | ||
34dc7c2f BB |
3874 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
3875 | ||
3876 | if (ZFS_IS_VOLUME(zhp)) { | |
34dc7c2f BB |
3877 | zc.zc_objset_type = DMU_OST_ZVOL; |
3878 | } else { | |
3879 | zc.zc_objset_type = DMU_OST_ZFS; | |
3880 | } | |
3881 | ||
45d1cae3 | 3882 | zc.zc_defer_destroy = defer; |
e956d651 CS |
3883 | if (zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_DESTROY, &zc) != 0 && |
3884 | errno != ENOENT) { | |
34dc7c2f BB |
3885 | return (zfs_standard_error_fmt(zhp->zfs_hdl, errno, |
3886 | dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), | |
3887 | zhp->zfs_name)); | |
3888 | } | |
3889 | ||
3890 | remove_mountpoint(zhp); | |
3891 | ||
3892 | return (0); | |
3893 | } | |
3894 | ||
3895 | struct destroydata { | |
330d06f9 MA |
3896 | nvlist_t *nvl; |
3897 | const char *snapname; | |
34dc7c2f BB |
3898 | }; |
3899 | ||
3900 | static int | |
428870ff | 3901 | zfs_check_snap_cb(zfs_handle_t *zhp, void *arg) |
34dc7c2f BB |
3902 | { |
3903 | struct destroydata *dd = arg; | |
eca7b760 | 3904 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
428870ff | 3905 | int rv = 0; |
34dc7c2f | 3906 | |
682ce104 TH |
3907 | if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, |
3908 | dd->snapname) >= sizeof (name)) | |
3909 | return (EINVAL); | |
34dc7c2f | 3910 | |
95fd54a1 | 3911 | if (lzc_exists(name)) |
330d06f9 | 3912 | verify(nvlist_add_boolean(dd->nvl, name) == 0); |
34dc7c2f | 3913 | |
330d06f9 MA |
3914 | rv = zfs_iter_filesystems(zhp, zfs_check_snap_cb, dd); |
3915 | zfs_close(zhp); | |
34dc7c2f BB |
3916 | return (rv); |
3917 | } | |
3918 | ||
3919 | /* | |
3920 | * Destroys all snapshots with the given name in zhp & descendants. | |
3921 | */ | |
3922 | int | |
45d1cae3 | 3923 | zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname, boolean_t defer) |
34dc7c2f | 3924 | { |
34dc7c2f BB |
3925 | int ret; |
3926 | struct destroydata dd = { 0 }; | |
3927 | ||
3928 | dd.snapname = snapname; | |
330d06f9 MA |
3929 | verify(nvlist_alloc(&dd.nvl, NV_UNIQUE_NAME, 0) == 0); |
3930 | (void) zfs_check_snap_cb(zfs_handle_dup(zhp), &dd); | |
34dc7c2f | 3931 | |
95fd54a1 | 3932 | if (nvlist_empty(dd.nvl)) { |
330d06f9 | 3933 | ret = zfs_standard_error_fmt(zhp->zfs_hdl, ENOENT, |
34dc7c2f | 3934 | dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), |
330d06f9 MA |
3935 | zhp->zfs_name, snapname); |
3936 | } else { | |
13fe0198 | 3937 | ret = zfs_destroy_snaps_nvl(zhp->zfs_hdl, dd.nvl, defer); |
34dc7c2f | 3938 | } |
330d06f9 MA |
3939 | nvlist_free(dd.nvl); |
3940 | return (ret); | |
3941 | } | |
3942 | ||
3943 | /* | |
13fe0198 | 3944 | * Destroys all the snapshots named in the nvlist. |
330d06f9 MA |
3945 | */ |
3946 | int | |
13fe0198 | 3947 | zfs_destroy_snaps_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, boolean_t defer) |
330d06f9 MA |
3948 | { |
3949 | int ret; | |
b3744ae6 | 3950 | nvlist_t *errlist = NULL; |
13fe0198 | 3951 | nvpair_t *pair; |
34dc7c2f | 3952 | |
6f1ffb06 | 3953 | ret = lzc_destroy_snaps(snaps, defer, &errlist); |
34dc7c2f | 3954 | |
b3744ae6 CW |
3955 | if (ret == 0) { |
3956 | nvlist_free(errlist); | |
13fe0198 | 3957 | return (0); |
b3744ae6 | 3958 | } |
34dc7c2f | 3959 | |
95fd54a1 | 3960 | if (nvlist_empty(errlist)) { |
13fe0198 MA |
3961 | char errbuf[1024]; |
3962 | (void) snprintf(errbuf, sizeof (errbuf), | |
3963 | dgettext(TEXT_DOMAIN, "cannot destroy snapshots")); | |
3964 | ||
3965 | ret = zfs_standard_error(hdl, ret, errbuf); | |
3966 | } | |
3967 | for (pair = nvlist_next_nvpair(errlist, NULL); | |
3968 | pair != NULL; pair = nvlist_next_nvpair(errlist, pair)) { | |
3969 | char errbuf[1024]; | |
3970 | (void) snprintf(errbuf, sizeof (errbuf), | |
3971 | dgettext(TEXT_DOMAIN, "cannot destroy snapshot %s"), | |
3972 | nvpair_name(pair)); | |
3973 | ||
3974 | switch (fnvpair_value_int32(pair)) { | |
3975 | case EEXIST: | |
3976 | zfs_error_aux(hdl, | |
3977 | dgettext(TEXT_DOMAIN, "snapshot is cloned")); | |
3978 | ret = zfs_error(hdl, EZFS_EXISTS, errbuf); | |
3979 | break; | |
3980 | default: | |
3981 | ret = zfs_standard_error(hdl, errno, errbuf); | |
3982 | break; | |
34dc7c2f BB |
3983 | } |
3984 | } | |
3985 | ||
b3744ae6 | 3986 | nvlist_free(errlist); |
6f1ffb06 | 3987 | return (ret); |
34dc7c2f BB |
3988 | } |
3989 | ||
3990 | /* | |
3991 | * Clones the given dataset. The target must be of the same type as the source. | |
3992 | */ | |
3993 | int | |
3994 | zfs_clone(zfs_handle_t *zhp, const char *target, nvlist_t *props) | |
3995 | { | |
eca7b760 | 3996 | char parent[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f BB |
3997 | int ret; |
3998 | char errbuf[1024]; | |
3999 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
34dc7c2f BB |
4000 | uint64_t zoned; |
4001 | ||
4002 | assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); | |
4003 | ||
4004 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
4005 | "cannot create '%s'"), target); | |
4006 | ||
330d06f9 | 4007 | /* validate the target/clone name */ |
34dc7c2f BB |
4008 | if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM, B_TRUE)) |
4009 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
4010 | ||
4011 | /* validate parents exist */ | |
4012 | if (check_parents(hdl, target, &zoned, B_FALSE, NULL) != 0) | |
4013 | return (-1); | |
4014 | ||
4015 | (void) parent_name(target, parent, sizeof (parent)); | |
4016 | ||
4017 | /* do the clone */ | |
34dc7c2f BB |
4018 | |
4019 | if (props) { | |
6f1ffb06 | 4020 | zfs_type_t type; |
d22f3a82 | 4021 | |
6f1ffb06 MA |
4022 | if (ZFS_IS_VOLUME(zhp)) { |
4023 | type = ZFS_TYPE_VOLUME; | |
4024 | } else { | |
4025 | type = ZFS_TYPE_FILESYSTEM; | |
4026 | } | |
b128c09f | 4027 | if ((props = zfs_valid_proplist(hdl, type, props, zoned, |
b5256303 | 4028 | zhp, zhp->zpool_hdl, B_TRUE, errbuf)) == NULL) |
34dc7c2f | 4029 | return (-1); |
d22f3a82 MG |
4030 | if (zfs_fix_auto_resv(zhp, props) == -1) { |
4031 | nvlist_free(props); | |
4032 | return (-1); | |
4033 | } | |
34dc7c2f BB |
4034 | } |
4035 | ||
b5256303 TC |
4036 | if (zfs_crypto_clone_check(hdl, zhp, parent, props) != 0) { |
4037 | nvlist_free(props); | |
4038 | return (zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf)); | |
4039 | } | |
4040 | ||
6f1ffb06 MA |
4041 | ret = lzc_clone(target, zhp->zfs_name, props); |
4042 | nvlist_free(props); | |
34dc7c2f BB |
4043 | |
4044 | if (ret != 0) { | |
4045 | switch (errno) { | |
4046 | ||
4047 | case ENOENT: | |
4048 | /* | |
4049 | * The parent doesn't exist. We should have caught this | |
4050 | * above, but there may a race condition that has since | |
4051 | * destroyed the parent. | |
4052 | * | |
4053 | * At this point, we don't know whether it's the source | |
4054 | * that doesn't exist anymore, or whether the target | |
4055 | * dataset doesn't exist. | |
4056 | */ | |
4057 | zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, | |
4058 | "no such parent '%s'"), parent); | |
4059 | return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); | |
4060 | ||
4061 | case EXDEV: | |
4062 | zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, | |
4063 | "source and target pools differ")); | |
4064 | return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, | |
4065 | errbuf)); | |
4066 | ||
4067 | default: | |
4068 | return (zfs_standard_error(zhp->zfs_hdl, errno, | |
4069 | errbuf)); | |
4070 | } | |
34dc7c2f BB |
4071 | } |
4072 | ||
4073 | return (ret); | |
4074 | } | |
4075 | ||
34dc7c2f BB |
4076 | /* |
4077 | * Promotes the given clone fs to be the clone parent. | |
4078 | */ | |
4079 | int | |
4080 | zfs_promote(zfs_handle_t *zhp) | |
4081 | { | |
4082 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
d12f91fd | 4083 | char snapname[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f | 4084 | int ret; |
34dc7c2f BB |
4085 | char errbuf[1024]; |
4086 | ||
4087 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
4088 | "cannot promote '%s'"), zhp->zfs_name); | |
4089 | ||
4090 | if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { | |
4091 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4092 | "snapshots can not be promoted")); | |
4093 | return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); | |
4094 | } | |
4095 | ||
d12f91fd | 4096 | if (zhp->zfs_dmustats.dds_origin[0] == '\0') { |
34dc7c2f BB |
4097 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
4098 | "not a cloned filesystem")); | |
4099 | return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); | |
4100 | } | |
34dc7c2f | 4101 | |
650258d7 | 4102 | if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) |
4103 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
4104 | ||
d12f91fd | 4105 | ret = lzc_promote(zhp->zfs_name, snapname, sizeof (snapname)); |
34dc7c2f BB |
4106 | |
4107 | if (ret != 0) { | |
d12f91fd | 4108 | switch (ret) { |
34dc7c2f | 4109 | case EEXIST: |
ba6a2402 | 4110 | /* There is a conflicting snapshot name. */ |
34dc7c2f | 4111 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
428870ff | 4112 | "conflicting snapshot '%s' from parent '%s'"), |
d12f91fd | 4113 | snapname, zhp->zfs_dmustats.dds_origin); |
34dc7c2f BB |
4114 | return (zfs_error(hdl, EZFS_EXISTS, errbuf)); |
4115 | ||
4116 | default: | |
d12f91fd | 4117 | return (zfs_standard_error(hdl, ret, errbuf)); |
34dc7c2f | 4118 | } |
d603ed6c | 4119 | } |
34dc7c2f BB |
4120 | return (ret); |
4121 | } | |
4122 | ||
6f1ffb06 MA |
4123 | typedef struct snapdata { |
4124 | nvlist_t *sd_nvl; | |
4125 | const char *sd_snapname; | |
4126 | } snapdata_t; | |
4127 | ||
4128 | static int | |
4129 | zfs_snapshot_cb(zfs_handle_t *zhp, void *arg) | |
4130 | { | |
4131 | snapdata_t *sd = arg; | |
eca7b760 | 4132 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
6f1ffb06 MA |
4133 | int rv = 0; |
4134 | ||
96c2e961 | 4135 | if (zfs_prop_get_int(zhp, ZFS_PROP_INCONSISTENT) == 0) { |
682ce104 TH |
4136 | if (snprintf(name, sizeof (name), "%s@%s", zfs_get_name(zhp), |
4137 | sd->sd_snapname) >= sizeof (name)) | |
4138 | return (EINVAL); | |
6f1ffb06 | 4139 | |
96c2e961 | 4140 | fnvlist_add_boolean(sd->sd_nvl, name); |
6f1ffb06 | 4141 | |
96c2e961 KW |
4142 | rv = zfs_iter_filesystems(zhp, zfs_snapshot_cb, sd); |
4143 | } | |
6f1ffb06 | 4144 | zfs_close(zhp); |
96c2e961 | 4145 | |
6f1ffb06 MA |
4146 | return (rv); |
4147 | } | |
4148 | ||
a1d477c2 MA |
4149 | int |
4150 | zfs_remap_indirects(libzfs_handle_t *hdl, const char *fs) | |
4151 | { | |
4152 | int err; | |
4153 | char errbuf[1024]; | |
4154 | ||
4155 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
b4555c77 | 4156 | "cannot remap dataset '%s'"), fs); |
a1d477c2 MA |
4157 | |
4158 | err = lzc_remap(fs); | |
4159 | ||
4160 | if (err != 0) { | |
b4555c77 | 4161 | switch (err) { |
4162 | case ENOTSUP: | |
4163 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4164 | "pool must be upgraded")); | |
4165 | (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
4166 | break; | |
4167 | case EINVAL: | |
4168 | (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); | |
4169 | break; | |
4170 | default: | |
4171 | (void) zfs_standard_error(hdl, err, errbuf); | |
4172 | break; | |
4173 | } | |
a1d477c2 MA |
4174 | } |
4175 | ||
4176 | return (err); | |
4177 | } | |
4178 | ||
34dc7c2f | 4179 | /* |
6f1ffb06 MA |
4180 | * Creates snapshots. The keys in the snaps nvlist are the snapshots to be |
4181 | * created. | |
34dc7c2f BB |
4182 | */ |
4183 | int | |
6f1ffb06 | 4184 | zfs_snapshot_nvl(libzfs_handle_t *hdl, nvlist_t *snaps, nvlist_t *props) |
34dc7c2f | 4185 | { |
34dc7c2f BB |
4186 | int ret; |
4187 | char errbuf[1024]; | |
6f1ffb06 MA |
4188 | nvpair_t *elem; |
4189 | nvlist_t *errors; | |
9d016804 | 4190 | zpool_handle_t *zpool_hdl; |
4191 | char pool[ZFS_MAX_DATASET_NAME_LEN]; | |
34dc7c2f BB |
4192 | |
4193 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
6f1ffb06 | 4194 | "cannot create snapshots ")); |
34dc7c2f | 4195 | |
6f1ffb06 MA |
4196 | elem = NULL; |
4197 | while ((elem = nvlist_next_nvpair(snaps, elem)) != NULL) { | |
4198 | const char *snapname = nvpair_name(elem); | |
b128c09f | 4199 | |
6f1ffb06 MA |
4200 | /* validate the target name */ |
4201 | if (!zfs_validate_name(hdl, snapname, ZFS_TYPE_SNAPSHOT, | |
4202 | B_TRUE)) { | |
4203 | (void) snprintf(errbuf, sizeof (errbuf), | |
4204 | dgettext(TEXT_DOMAIN, | |
4205 | "cannot create snapshot '%s'"), snapname); | |
4206 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
b128c09f | 4207 | } |
b128c09f BB |
4208 | } |
4209 | ||
82f6f6e6 JS |
4210 | /* |
4211 | * get pool handle for prop validation. assumes all snaps are in the | |
4212 | * same pool, as does lzc_snapshot (below). | |
4213 | */ | |
82f6f6e6 JS |
4214 | elem = nvlist_next_nvpair(snaps, NULL); |
4215 | (void) strlcpy(pool, nvpair_name(elem), sizeof (pool)); | |
4216 | pool[strcspn(pool, "/@")] = '\0'; | |
9d016804 | 4217 | zpool_hdl = zpool_open(hdl, pool); |
4218 | if (zpool_hdl == NULL) | |
4219 | return (-1); | |
82f6f6e6 | 4220 | |
6f1ffb06 MA |
4221 | if (props != NULL && |
4222 | (props = zfs_valid_proplist(hdl, ZFS_TYPE_SNAPSHOT, | |
b5256303 | 4223 | props, B_FALSE, NULL, zpool_hdl, B_FALSE, errbuf)) == NULL) { |
82f6f6e6 | 4224 | zpool_close(zpool_hdl); |
34dc7c2f BB |
4225 | return (-1); |
4226 | } | |
82f6f6e6 | 4227 | zpool_close(zpool_hdl); |
34dc7c2f | 4228 | |
6f1ffb06 | 4229 | ret = lzc_snapshot(snaps, props, &errors); |
34dc7c2f | 4230 | |
6f1ffb06 MA |
4231 | if (ret != 0) { |
4232 | boolean_t printed = B_FALSE; | |
4233 | for (elem = nvlist_next_nvpair(errors, NULL); | |
4234 | elem != NULL; | |
4235 | elem = nvlist_next_nvpair(errors, elem)) { | |
4236 | (void) snprintf(errbuf, sizeof (errbuf), | |
4237 | dgettext(TEXT_DOMAIN, | |
4238 | "cannot create snapshot '%s'"), nvpair_name(elem)); | |
4239 | (void) zfs_standard_error(hdl, | |
4240 | fnvpair_value_int32(elem), errbuf); | |
4241 | printed = B_TRUE; | |
4242 | } | |
4243 | if (!printed) { | |
4244 | switch (ret) { | |
4245 | case EXDEV: | |
4246 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4247 | "multiple snapshots of same " | |
4248 | "fs not allowed")); | |
4249 | (void) zfs_error(hdl, EZFS_EXISTS, errbuf); | |
b128c09f | 4250 | |
6f1ffb06 MA |
4251 | break; |
4252 | default: | |
4253 | (void) zfs_standard_error(hdl, ret, errbuf); | |
4254 | } | |
4255 | } | |
428870ff | 4256 | } |
34dc7c2f | 4257 | |
6f1ffb06 MA |
4258 | nvlist_free(props); |
4259 | nvlist_free(errors); | |
4260 | return (ret); | |
4261 | } | |
d603ed6c | 4262 | |
6f1ffb06 MA |
4263 | int |
4264 | zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive, | |
4265 | nvlist_t *props) | |
4266 | { | |
4267 | int ret; | |
4268 | snapdata_t sd = { 0 }; | |
eca7b760 | 4269 | char fsname[ZFS_MAX_DATASET_NAME_LEN]; |
6f1ffb06 MA |
4270 | char *cp; |
4271 | zfs_handle_t *zhp; | |
4272 | char errbuf[1024]; | |
4273 | ||
4274 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
4275 | "cannot snapshot %s"), path); | |
4276 | ||
4277 | if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT, B_TRUE)) | |
4278 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
ba6a2402 | 4279 | |
6f1ffb06 MA |
4280 | (void) strlcpy(fsname, path, sizeof (fsname)); |
4281 | cp = strchr(fsname, '@'); | |
4282 | *cp = '\0'; | |
4283 | sd.sd_snapname = cp + 1; | |
34dc7c2f | 4284 | |
6f1ffb06 MA |
4285 | if ((zhp = zfs_open(hdl, fsname, ZFS_TYPE_FILESYSTEM | |
4286 | ZFS_TYPE_VOLUME)) == NULL) { | |
4287 | return (-1); | |
4288 | } | |
4289 | ||
4290 | verify(nvlist_alloc(&sd.sd_nvl, NV_UNIQUE_NAME, 0) == 0); | |
4291 | if (recursive) { | |
4292 | (void) zfs_snapshot_cb(zfs_handle_dup(zhp), &sd); | |
4293 | } else { | |
4294 | fnvlist_add_boolean(sd.sd_nvl, path); | |
4295 | } | |
4296 | ||
4297 | ret = zfs_snapshot_nvl(hdl, sd.sd_nvl, props); | |
4298 | nvlist_free(sd.sd_nvl); | |
4299 | zfs_close(zhp); | |
34dc7c2f BB |
4300 | return (ret); |
4301 | } | |
4302 | ||
4303 | /* | |
4304 | * Destroy any more recent snapshots. We invoke this callback on any dependents | |
4305 | * of the snapshot first. If the 'cb_dependent' member is non-zero, then this | |
4306 | * is a dependent and we should just destroy it without checking the transaction | |
4307 | * group. | |
4308 | */ | |
4309 | typedef struct rollback_data { | |
4310 | const char *cb_target; /* the snapshot */ | |
4311 | uint64_t cb_create; /* creation time reference */ | |
4312 | boolean_t cb_error; | |
34dc7c2f BB |
4313 | boolean_t cb_force; |
4314 | } rollback_data_t; | |
4315 | ||
4316 | static int | |
da536844 | 4317 | rollback_destroy_dependent(zfs_handle_t *zhp, void *data) |
34dc7c2f BB |
4318 | { |
4319 | rollback_data_t *cbp = data; | |
da536844 MA |
4320 | prop_changelist_t *clp; |
4321 | ||
4322 | /* We must destroy this clone; first unmount it */ | |
4323 | clp = changelist_gather(zhp, ZFS_PROP_NAME, 0, | |
4324 | cbp->cb_force ? MS_FORCE: 0); | |
4325 | if (clp == NULL || changelist_prefix(clp) != 0) { | |
4326 | cbp->cb_error = B_TRUE; | |
4327 | zfs_close(zhp); | |
4328 | return (0); | |
4329 | } | |
4330 | if (zfs_destroy(zhp, B_FALSE) != 0) | |
4331 | cbp->cb_error = B_TRUE; | |
4332 | else | |
4333 | changelist_remove(clp, zhp->zfs_name); | |
4334 | (void) changelist_postfix(clp); | |
4335 | changelist_free(clp); | |
34dc7c2f | 4336 | |
da536844 MA |
4337 | zfs_close(zhp); |
4338 | return (0); | |
4339 | } | |
34dc7c2f | 4340 | |
da536844 MA |
4341 | static int |
4342 | rollback_destroy(zfs_handle_t *zhp, void *data) | |
4343 | { | |
4344 | rollback_data_t *cbp = data; | |
34dc7c2f | 4345 | |
da536844 MA |
4346 | if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > cbp->cb_create) { |
4347 | cbp->cb_error |= zfs_iter_dependents(zhp, B_FALSE, | |
4348 | rollback_destroy_dependent, cbp); | |
34dc7c2f | 4349 | |
da536844 | 4350 | cbp->cb_error |= zfs_destroy(zhp, B_FALSE); |
34dc7c2f BB |
4351 | } |
4352 | ||
4353 | zfs_close(zhp); | |
4354 | return (0); | |
4355 | } | |
4356 | ||
4357 | /* | |
4358 | * Given a dataset, rollback to a specific snapshot, discarding any | |
4359 | * data changes since then and making it the active dataset. | |
4360 | * | |
da536844 MA |
4361 | * Any snapshots and bookmarks more recent than the target are |
4362 | * destroyed, along with their dependents (i.e. clones). | |
34dc7c2f BB |
4363 | */ |
4364 | int | |
4365 | zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, boolean_t force) | |
4366 | { | |
4367 | rollback_data_t cb = { 0 }; | |
4368 | int err; | |
34dc7c2f | 4369 | boolean_t restore_resv = 0; |
d4ed6673 BB |
4370 | uint64_t old_volsize = 0, new_volsize; |
4371 | zfs_prop_t resv_prop = { 0 }; | |
34dc7c2f BB |
4372 | |
4373 | assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || | |
4374 | zhp->zfs_type == ZFS_TYPE_VOLUME); | |
4375 | ||
4376 | /* | |
04434775 | 4377 | * Destroy all recent snapshots and their dependents. |
34dc7c2f BB |
4378 | */ |
4379 | cb.cb_force = force; | |
4380 | cb.cb_target = snap->zfs_name; | |
4381 | cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); | |
da536844 MA |
4382 | (void) zfs_iter_snapshots(zhp, B_FALSE, rollback_destroy, &cb); |
4383 | (void) zfs_iter_bookmarks(zhp, rollback_destroy, &cb); | |
34dc7c2f BB |
4384 | |
4385 | if (cb.cb_error) | |
4386 | return (-1); | |
4387 | ||
4388 | /* | |
4389 | * Now that we have verified that the snapshot is the latest, | |
4390 | * rollback to the given snapshot. | |
4391 | */ | |
4392 | ||
4393 | if (zhp->zfs_type == ZFS_TYPE_VOLUME) { | |
34dc7c2f BB |
4394 | if (zfs_which_resv_prop(zhp, &resv_prop) < 0) |
4395 | return (-1); | |
4396 | old_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); | |
4397 | restore_resv = | |
4398 | (old_volsize == zfs_prop_get_int(zhp, resv_prop)); | |
4399 | } | |
4400 | ||
34dc7c2f | 4401 | /* |
8ca78ab0 AG |
4402 | * Pass both the filesystem and the wanted snapshot names, |
4403 | * we would get an error back if the snapshot is destroyed or | |
4404 | * a new snapshot is created before this request is processed. | |
34dc7c2f | 4405 | */ |
8ca78ab0 | 4406 | err = lzc_rollback_to(zhp->zfs_name, snap->zfs_name); |
13342832 AG |
4407 | if (err != 0) { |
4408 | char errbuf[1024]; | |
4409 | ||
4410 | (void) snprintf(errbuf, sizeof (errbuf), | |
34dc7c2f BB |
4411 | dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), |
4412 | zhp->zfs_name); | |
13342832 AG |
4413 | switch (err) { |
4414 | case EEXIST: | |
4415 | zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, | |
4416 | "there is a snapshot or bookmark more recent " | |
4417 | "than '%s'"), snap->zfs_name); | |
4418 | (void) zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf); | |
4419 | break; | |
4420 | case ESRCH: | |
4421 | zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, | |
4422 | "'%s' is not found among snapshots of '%s'"), | |
4423 | snap->zfs_name, zhp->zfs_name); | |
4424 | (void) zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf); | |
4425 | break; | |
4426 | case EINVAL: | |
4427 | (void) zfs_error(zhp->zfs_hdl, EZFS_BADTYPE, errbuf); | |
4428 | break; | |
4429 | default: | |
4430 | (void) zfs_standard_error(zhp->zfs_hdl, err, errbuf); | |
4431 | } | |
34dc7c2f BB |
4432 | return (err); |
4433 | } | |
4434 | ||
4435 | /* | |
4436 | * For volumes, if the pre-rollback volsize matched the pre- | |
4437 | * rollback reservation and the volsize has changed then set | |
4438 | * the reservation property to the post-rollback volsize. | |
4439 | * Make a new handle since the rollback closed the dataset. | |
4440 | */ | |
4441 | if ((zhp->zfs_type == ZFS_TYPE_VOLUME) && | |
4442 | (zhp = make_dataset_handle(zhp->zfs_hdl, zhp->zfs_name))) { | |
34dc7c2f BB |
4443 | if (restore_resv) { |
4444 | new_volsize = zfs_prop_get_int(zhp, ZFS_PROP_VOLSIZE); | |
4445 | if (old_volsize != new_volsize) | |
4446 | err = zfs_prop_set_int(zhp, resv_prop, | |
4447 | new_volsize); | |
4448 | } | |
4449 | zfs_close(zhp); | |
4450 | } | |
4451 | return (err); | |
4452 | } | |
4453 | ||
34dc7c2f BB |
4454 | /* |
4455 | * Renames the given dataset. | |
4456 | */ | |
4457 | int | |
db49968e ES |
4458 | zfs_rename(zfs_handle_t *zhp, const char *target, boolean_t recursive, |
4459 | boolean_t force_unmount) | |
34dc7c2f | 4460 | { |
23d70cde | 4461 | int ret = 0; |
13fe0198 | 4462 | zfs_cmd_t zc = {"\0"}; |
34dc7c2f BB |
4463 | char *delim; |
4464 | prop_changelist_t *cl = NULL; | |
4465 | zfs_handle_t *zhrp = NULL; | |
4466 | char *parentname = NULL; | |
eca7b760 | 4467 | char parent[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f BB |
4468 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
4469 | char errbuf[1024]; | |
4470 | ||
4471 | /* if we have the same exact name, just return success */ | |
4472 | if (strcmp(zhp->zfs_name, target) == 0) | |
4473 | return (0); | |
4474 | ||
4475 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
4476 | "cannot rename to '%s'"), target); | |
4477 | ||
650258d7 | 4478 | /* make sure source name is valid */ |
4479 | if (!zfs_validate_name(hdl, zhp->zfs_name, zhp->zfs_type, B_TRUE)) | |
4480 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
4481 | ||
34dc7c2f BB |
4482 | /* |
4483 | * Make sure the target name is valid | |
4484 | */ | |
4485 | if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { | |
4486 | if ((strchr(target, '@') == NULL) || | |
4487 | *target == '@') { | |
4488 | /* | |
4489 | * Snapshot target name is abbreviated, | |
4490 | * reconstruct full dataset name | |
4491 | */ | |
4492 | (void) strlcpy(parent, zhp->zfs_name, | |
4493 | sizeof (parent)); | |
4494 | delim = strchr(parent, '@'); | |
4495 | if (strchr(target, '@') == NULL) | |
4496 | *(++delim) = '\0'; | |
4497 | else | |
4498 | *delim = '\0'; | |
4499 | (void) strlcat(parent, target, sizeof (parent)); | |
4500 | target = parent; | |
4501 | } else { | |
4502 | /* | |
4503 | * Make sure we're renaming within the same dataset. | |
4504 | */ | |
4505 | delim = strchr(target, '@'); | |
4506 | if (strncmp(zhp->zfs_name, target, delim - target) | |
4507 | != 0 || zhp->zfs_name[delim - target] != '@') { | |
4508 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4509 | "snapshots must be part of same " | |
4510 | "dataset")); | |
4511 | return (zfs_error(hdl, EZFS_CROSSTARGET, | |
4512 | errbuf)); | |
4513 | } | |
4514 | } | |
a7ed98d8 | 4515 | |
34dc7c2f BB |
4516 | if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) |
4517 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
4518 | } else { | |
4519 | if (recursive) { | |
4520 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4521 | "recursive rename must be a snapshot")); | |
4522 | return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); | |
4523 | } | |
4524 | ||
4525 | if (!zfs_validate_name(hdl, target, zhp->zfs_type, B_TRUE)) | |
4526 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
34dc7c2f BB |
4527 | |
4528 | /* validate parents */ | |
428870ff | 4529 | if (check_parents(hdl, target, NULL, B_FALSE, NULL) != 0) |
34dc7c2f BB |
4530 | return (-1); |
4531 | ||
34dc7c2f BB |
4532 | /* make sure we're in the same pool */ |
4533 | verify((delim = strchr(target, '/')) != NULL); | |
4534 | if (strncmp(zhp->zfs_name, target, delim - target) != 0 || | |
4535 | zhp->zfs_name[delim - target] != '/') { | |
4536 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4537 | "datasets must be within same pool")); | |
4538 | return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); | |
4539 | } | |
4540 | ||
4541 | /* new name cannot be a child of the current dataset name */ | |
428870ff | 4542 | if (is_descendant(zhp->zfs_name, target)) { |
34dc7c2f | 4543 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
428870ff | 4544 | "New dataset name cannot be a descendant of " |
34dc7c2f BB |
4545 | "current dataset name")); |
4546 | return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); | |
4547 | } | |
4548 | } | |
4549 | ||
4550 | (void) snprintf(errbuf, sizeof (errbuf), | |
4551 | dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); | |
4552 | ||
4553 | if (getzoneid() == GLOBAL_ZONEID && | |
4554 | zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { | |
4555 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4556 | "dataset is used in a non-global zone")); | |
4557 | return (zfs_error(hdl, EZFS_ZONED, errbuf)); | |
4558 | } | |
4559 | ||
4560 | if (recursive) { | |
34dc7c2f BB |
4561 | parentname = zfs_strdup(zhp->zfs_hdl, zhp->zfs_name); |
4562 | if (parentname == NULL) { | |
4563 | ret = -1; | |
4564 | goto error; | |
4565 | } | |
4566 | delim = strchr(parentname, '@'); | |
4567 | *delim = '\0'; | |
4568 | zhrp = zfs_open(zhp->zfs_hdl, parentname, ZFS_TYPE_DATASET); | |
4569 | if (zhrp == NULL) { | |
4570 | ret = -1; | |
4571 | goto error; | |
4572 | } | |
e3e670d0 | 4573 | } else if (zhp->zfs_type != ZFS_TYPE_SNAPSHOT) { |
50a343d8 AP |
4574 | if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, |
4575 | CL_GATHER_ITER_MOUNTED, | |
db49968e | 4576 | force_unmount ? MS_FORCE : 0)) == NULL) |
34dc7c2f BB |
4577 | return (-1); |
4578 | ||
4579 | if (changelist_haszonedchild(cl)) { | |
4580 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4581 | "child dataset with inherited mountpoint is used " | |
4582 | "in a non-global zone")); | |
4583 | (void) zfs_error(hdl, EZFS_ZONED, errbuf); | |
d4ed6673 | 4584 | ret = -1; |
34dc7c2f BB |
4585 | goto error; |
4586 | } | |
4587 | ||
4588 | if ((ret = changelist_prefix(cl)) != 0) | |
4589 | goto error; | |
4590 | } | |
4591 | ||
4592 | if (ZFS_IS_VOLUME(zhp)) | |
4593 | zc.zc_objset_type = DMU_OST_ZVOL; | |
4594 | else | |
4595 | zc.zc_objset_type = DMU_OST_ZFS; | |
4596 | ||
4597 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
4598 | (void) strlcpy(zc.zc_value, target, sizeof (zc.zc_value)); | |
4599 | ||
4600 | zc.zc_cookie = recursive; | |
4601 | ||
4602 | if ((ret = zfs_ioctl(zhp->zfs_hdl, ZFS_IOC_RENAME, &zc)) != 0) { | |
4603 | /* | |
4604 | * if it was recursive, the one that actually failed will | |
4605 | * be in zc.zc_name | |
4606 | */ | |
4607 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, | |
4608 | "cannot rename '%s'"), zc.zc_name); | |
4609 | ||
4610 | if (recursive && errno == EEXIST) { | |
4611 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4612 | "a child dataset already has a snapshot " | |
4613 | "with the new name")); | |
4614 | (void) zfs_error(hdl, EZFS_EXISTS, errbuf); | |
b5256303 TC |
4615 | } else if (errno == EACCES) { |
4616 | if (zfs_prop_get_int(zhp, ZFS_PROP_ENCRYPTION) == | |
4617 | ZIO_CRYPT_OFF) { | |
4618 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4619 | "cannot rename an unencrypted dataset to " | |
4620 | "be a decendent of an encrypted one")); | |
4621 | } else { | |
4622 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
4623 | "cannot move encryption child outside of " | |
4624 | "its encryption root")); | |
4625 | } | |
4626 | (void) zfs_error(hdl, EZFS_CRYPTOFAILED, errbuf); | |
34dc7c2f BB |
4627 | } else { |
4628 | (void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); | |
4629 | } | |
4630 | ||
4631 | /* | |
4632 | * On failure, we still want to remount any filesystems that | |
4633 | * were previously mounted, so we don't alter the system state. | |
4634 | */ | |
e3e670d0 | 4635 | if (cl != NULL) |
34dc7c2f | 4636 | (void) changelist_postfix(cl); |
34dc7c2f | 4637 | } else { |
e3e670d0 | 4638 | if (cl != NULL) { |
34dc7c2f BB |
4639 | changelist_rename(cl, zfs_get_name(zhp), target); |
4640 | ret = changelist_postfix(cl); | |
4641 | } | |
4642 | } | |
4643 | ||
4644 | error: | |
e3e670d0 | 4645 | if (parentname != NULL) { |
34dc7c2f BB |
4646 | free(parentname); |
4647 | } | |
e3e670d0 | 4648 | if (zhrp != NULL) { |
34dc7c2f BB |
4649 | zfs_close(zhrp); |
4650 | } | |
e3e670d0 | 4651 | if (cl != NULL) { |
34dc7c2f BB |
4652 | changelist_free(cl); |
4653 | } | |
4654 | return (ret); | |
4655 | } | |
4656 | ||
6044cf59 JR |
4657 | nvlist_t * |
4658 | zfs_get_all_props(zfs_handle_t *zhp) | |
4659 | { | |
4660 | return (zhp->zfs_props); | |
4661 | } | |
4662 | ||
4663 | nvlist_t * | |
4664 | zfs_get_recvd_props(zfs_handle_t *zhp) | |
4665 | { | |
4666 | if (zhp->zfs_recvd_props == NULL) | |
4667 | if (get_recvd_props_ioctl(zhp) != 0) | |
4668 | return (NULL); | |
4669 | return (zhp->zfs_recvd_props); | |
4670 | } | |
4671 | ||
34dc7c2f | 4672 | nvlist_t * |
d603ed6c | 4673 | zfs_get_user_props(zfs_handle_t *zhp) |
34dc7c2f | 4674 | { |
d603ed6c | 4675 | return (zhp->zfs_user_props); |
34dc7c2f BB |
4676 | } |
4677 | ||
4678 | /* | |
4679 | * This function is used by 'zfs list' to determine the exact set of columns to | |
4680 | * display, and their maximum widths. This does two main things: | |
4681 | * | |
4682 | * - If this is a list of all properties, then expand the list to include | |
4683 | * all native properties, and set a flag so that for each dataset we look | |
4684 | * for new unique user properties and add them to the list. | |
4685 | * | |
4686 | * - For non fixed-width properties, keep track of the maximum width seen | |
428870ff BB |
4687 | * so that we can size the column appropriately. If the user has |
4688 | * requested received property values, we also need to compute the width | |
4689 | * of the RECEIVED column. | |
34dc7c2f BB |
4690 | */ |
4691 | int | |
54d5378f YP |
4692 | zfs_expand_proplist(zfs_handle_t *zhp, zprop_list_t **plp, boolean_t received, |
4693 | boolean_t literal) | |
34dc7c2f BB |
4694 | { |
4695 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
4696 | zprop_list_t *entry; | |
4697 | zprop_list_t **last, **start; | |
4698 | nvlist_t *userprops, *propval; | |
4699 | nvpair_t *elem; | |
4700 | char *strval; | |
4701 | char buf[ZFS_MAXPROPLEN]; | |
4702 | ||
4703 | if (zprop_expand_list(hdl, plp, ZFS_TYPE_DATASET) != 0) | |
4704 | return (-1); | |
4705 | ||
4706 | userprops = zfs_get_user_props(zhp); | |
4707 | ||
4708 | entry = *plp; | |
4709 | if (entry->pl_all && nvlist_next_nvpair(userprops, NULL) != NULL) { | |
4710 | /* | |
4711 | * Go through and add any user properties as necessary. We | |
4712 | * start by incrementing our list pointer to the first | |
4713 | * non-native property. | |
4714 | */ | |
4715 | start = plp; | |
4716 | while (*start != NULL) { | |
4717 | if ((*start)->pl_prop == ZPROP_INVAL) | |
4718 | break; | |
4719 | start = &(*start)->pl_next; | |
4720 | } | |
4721 | ||
4722 | elem = NULL; | |
4723 | while ((elem = nvlist_next_nvpair(userprops, elem)) != NULL) { | |
4724 | /* | |
4725 | * See if we've already found this property in our list. | |
4726 | */ | |
4727 | for (last = start; *last != NULL; | |
4728 | last = &(*last)->pl_next) { | |
4729 | if (strcmp((*last)->pl_user_prop, | |
4730 | nvpair_name(elem)) == 0) | |
4731 | break; | |
4732 | } | |
4733 | ||
4734 | if (*last == NULL) { | |
4735 | if ((entry = zfs_alloc(hdl, | |
4736 | sizeof (zprop_list_t))) == NULL || | |
4737 | ((entry->pl_user_prop = zfs_strdup(hdl, | |
4738 | nvpair_name(elem)))) == NULL) { | |
4739 | free(entry); | |
4740 | return (-1); | |
4741 | } | |
4742 | ||
4743 | entry->pl_prop = ZPROP_INVAL; | |
4744 | entry->pl_width = strlen(nvpair_name(elem)); | |
4745 | entry->pl_all = B_TRUE; | |
4746 | *last = entry; | |
4747 | } | |
4748 | } | |
4749 | } | |
4750 | ||
4751 | /* | |
4752 | * Now go through and check the width of any non-fixed columns | |
4753 | */ | |
4754 | for (entry = *plp; entry != NULL; entry = entry->pl_next) { | |
54d5378f | 4755 | if (entry->pl_fixed && !literal) |
34dc7c2f BB |
4756 | continue; |
4757 | ||
4758 | if (entry->pl_prop != ZPROP_INVAL) { | |
4759 | if (zfs_prop_get(zhp, entry->pl_prop, | |
54d5378f | 4760 | buf, sizeof (buf), NULL, NULL, 0, literal) == 0) { |
34dc7c2f BB |
4761 | if (strlen(buf) > entry->pl_width) |
4762 | entry->pl_width = strlen(buf); | |
4763 | } | |
428870ff BB |
4764 | if (received && zfs_prop_get_recvd(zhp, |
4765 | zfs_prop_to_name(entry->pl_prop), | |
54d5378f | 4766 | buf, sizeof (buf), literal) == 0) |
428870ff BB |
4767 | if (strlen(buf) > entry->pl_recvd_width) |
4768 | entry->pl_recvd_width = strlen(buf); | |
4769 | } else { | |
4770 | if (nvlist_lookup_nvlist(userprops, entry->pl_user_prop, | |
4771 | &propval) == 0) { | |
4772 | verify(nvlist_lookup_string(propval, | |
4773 | ZPROP_VALUE, &strval) == 0); | |
4774 | if (strlen(strval) > entry->pl_width) | |
4775 | entry->pl_width = strlen(strval); | |
4776 | } | |
4777 | if (received && zfs_prop_get_recvd(zhp, | |
4778 | entry->pl_user_prop, | |
54d5378f | 4779 | buf, sizeof (buf), literal) == 0) |
428870ff BB |
4780 | if (strlen(buf) > entry->pl_recvd_width) |
4781 | entry->pl_recvd_width = strlen(buf); | |
34dc7c2f BB |
4782 | } |
4783 | } | |
4784 | ||
4785 | return (0); | |
4786 | } | |
4787 | ||
9babb374 BB |
4788 | void |
4789 | zfs_prune_proplist(zfs_handle_t *zhp, uint8_t *props) | |
4790 | { | |
4791 | nvpair_t *curr; | |
23de906c | 4792 | nvpair_t *next; |
9babb374 BB |
4793 | |
4794 | /* | |
4795 | * Keep a reference to the props-table against which we prune the | |
4796 | * properties. | |
4797 | */ | |
4798 | zhp->zfs_props_table = props; | |
4799 | ||
4800 | curr = nvlist_next_nvpair(zhp->zfs_props, NULL); | |
4801 | ||
4802 | while (curr) { | |
4803 | zfs_prop_t zfs_prop = zfs_name_to_prop(nvpair_name(curr)); | |
23de906c | 4804 | next = nvlist_next_nvpair(zhp->zfs_props, curr); |
9babb374 BB |
4805 | |
4806 | /* | |
428870ff BB |
4807 | * User properties will result in ZPROP_INVAL, and since we |
4808 | * only know how to prune standard ZFS properties, we always | |
4809 | * leave these in the list. This can also happen if we | |
4810 | * encounter an unknown DSL property (when running older | |
4811 | * software, for example). | |
9babb374 BB |
4812 | */ |
4813 | if (zfs_prop != ZPROP_INVAL && props[zfs_prop] == B_FALSE) | |
4814 | (void) nvlist_remove(zhp->zfs_props, | |
4815 | nvpair_name(curr), nvpair_type(curr)); | |
4816 | curr = next; | |
4817 | } | |
4818 | } | |
4819 | ||
4820 | static int | |
4821 | zfs_smb_acl_mgmt(libzfs_handle_t *hdl, char *dataset, char *path, | |
4822 | zfs_smb_acl_op_t cmd, char *resource1, char *resource2) | |
4823 | { | |
13fe0198 | 4824 | zfs_cmd_t zc = {"\0"}; |
9babb374 BB |
4825 | nvlist_t *nvlist = NULL; |
4826 | int error; | |
4827 | ||
4828 | (void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); | |
4829 | (void) strlcpy(zc.zc_value, path, sizeof (zc.zc_value)); | |
4830 | zc.zc_cookie = (uint64_t)cmd; | |
4831 | ||
4832 | if (cmd == ZFS_SMB_ACL_RENAME) { | |
4833 | if (nvlist_alloc(&nvlist, NV_UNIQUE_NAME, 0) != 0) { | |
4834 | (void) no_memory(hdl); | |
23de906c | 4835 | return (0); |
9babb374 BB |
4836 | } |
4837 | } | |
4838 | ||
4839 | switch (cmd) { | |
4840 | case ZFS_SMB_ACL_ADD: | |
4841 | case ZFS_SMB_ACL_REMOVE: | |
4842 | (void) strlcpy(zc.zc_string, resource1, sizeof (zc.zc_string)); | |
4843 | break; | |
4844 | case ZFS_SMB_ACL_RENAME: | |
4845 | if (nvlist_add_string(nvlist, ZFS_SMB_ACL_SRC, | |
4846 | resource1) != 0) { | |
4847 | (void) no_memory(hdl); | |
4848 | return (-1); | |
4849 | } | |
4850 | if (nvlist_add_string(nvlist, ZFS_SMB_ACL_TARGET, | |
4851 | resource2) != 0) { | |
4852 | (void) no_memory(hdl); | |
4853 | return (-1); | |
4854 | } | |
4855 | if (zcmd_write_src_nvlist(hdl, &zc, nvlist) != 0) { | |
4856 | nvlist_free(nvlist); | |
4857 | return (-1); | |
4858 | } | |
4859 | break; | |
4860 | case ZFS_SMB_ACL_PURGE: | |
4861 | break; | |
4862 | default: | |
4863 | return (-1); | |
4864 | } | |
4865 | error = ioctl(hdl->libzfs_fd, ZFS_IOC_SMB_ACL, &zc); | |
8a5fc748 | 4866 | nvlist_free(nvlist); |
9babb374 BB |
4867 | return (error); |
4868 | } | |
4869 | ||
4870 | int | |
4871 | zfs_smb_acl_add(libzfs_handle_t *hdl, char *dataset, | |
4872 | char *path, char *resource) | |
4873 | { | |
4874 | return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_ADD, | |
4875 | resource, NULL)); | |
4876 | } | |
4877 | ||
4878 | int | |
4879 | zfs_smb_acl_remove(libzfs_handle_t *hdl, char *dataset, | |
4880 | char *path, char *resource) | |
4881 | { | |
4882 | return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_REMOVE, | |
4883 | resource, NULL)); | |
4884 | } | |
4885 | ||
4886 | int | |
4887 | zfs_smb_acl_purge(libzfs_handle_t *hdl, char *dataset, char *path) | |
4888 | { | |
4889 | return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_PURGE, | |
4890 | NULL, NULL)); | |
4891 | } | |
4892 | ||
4893 | int | |
4894 | zfs_smb_acl_rename(libzfs_handle_t *hdl, char *dataset, char *path, | |
4895 | char *oldname, char *newname) | |
4896 | { | |
4897 | return (zfs_smb_acl_mgmt(hdl, dataset, path, ZFS_SMB_ACL_RENAME, | |
4898 | oldname, newname)); | |
4899 | } | |
4900 | ||
4901 | int | |
4902 | zfs_userspace(zfs_handle_t *zhp, zfs_userquota_prop_t type, | |
4903 | zfs_userspace_cb_t func, void *arg) | |
4904 | { | |
13fe0198 | 4905 | zfs_cmd_t zc = {"\0"}; |
9babb374 | 4906 | zfs_useracct_t buf[100]; |
105afebb YP |
4907 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
4908 | int ret; | |
9babb374 | 4909 | |
330d06f9 | 4910 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
9babb374 BB |
4911 | |
4912 | zc.zc_objset_type = type; | |
4913 | zc.zc_nvlist_dst = (uintptr_t)buf; | |
4914 | ||
105afebb | 4915 | for (;;) { |
9babb374 BB |
4916 | zfs_useracct_t *zua = buf; |
4917 | ||
4918 | zc.zc_nvlist_dst_size = sizeof (buf); | |
105afebb | 4919 | if (zfs_ioctl(hdl, ZFS_IOC_USERSPACE_MANY, &zc) != 0) { |
eca7b760 | 4920 | char errbuf[1024]; |
105afebb | 4921 | |
1de321e6 JX |
4922 | if ((errno == ENOTSUP && |
4923 | (type == ZFS_PROP_USEROBJUSED || | |
4924 | type == ZFS_PROP_GROUPOBJUSED || | |
4925 | type == ZFS_PROP_USEROBJQUOTA || | |
9c5167d1 NF |
4926 | type == ZFS_PROP_GROUPOBJQUOTA || |
4927 | type == ZFS_PROP_PROJECTOBJUSED || | |
4928 | type == ZFS_PROP_PROJECTOBJQUOTA || | |
4929 | type == ZFS_PROP_PROJECTUSED || | |
4930 | type == ZFS_PROP_PROJECTQUOTA))) | |
1de321e6 JX |
4931 | break; |
4932 | ||
105afebb YP |
4933 | (void) snprintf(errbuf, sizeof (errbuf), |
4934 | dgettext(TEXT_DOMAIN, | |
4935 | "cannot get used/quota for %s"), zc.zc_name); | |
4936 | return (zfs_standard_error_fmt(hdl, errno, errbuf)); | |
4937 | } | |
4938 | if (zc.zc_nvlist_dst_size == 0) | |
9babb374 BB |
4939 | break; |
4940 | ||
4941 | while (zc.zc_nvlist_dst_size > 0) { | |
105afebb YP |
4942 | if ((ret = func(arg, zua->zu_domain, zua->zu_rid, |
4943 | zua->zu_space)) != 0) | |
4944 | return (ret); | |
9babb374 BB |
4945 | zua++; |
4946 | zc.zc_nvlist_dst_size -= sizeof (zfs_useracct_t); | |
4947 | } | |
4948 | } | |
4949 | ||
105afebb | 4950 | return (0); |
9babb374 | 4951 | } |
45d1cae3 | 4952 | |
13fe0198 MA |
4953 | struct holdarg { |
4954 | nvlist_t *nvl; | |
4955 | const char *snapname; | |
4956 | const char *tag; | |
4957 | boolean_t recursive; | |
1a077756 | 4958 | int error; |
13fe0198 MA |
4959 | }; |
4960 | ||
4961 | static int | |
4962 | zfs_hold_one(zfs_handle_t *zhp, void *arg) | |
4963 | { | |
4964 | struct holdarg *ha = arg; | |
eca7b760 | 4965 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
13fe0198 MA |
4966 | int rv = 0; |
4967 | ||
682ce104 TH |
4968 | if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, |
4969 | ha->snapname) >= sizeof (name)) | |
4970 | return (EINVAL); | |
13fe0198 | 4971 | |
95fd54a1 | 4972 | if (lzc_exists(name)) |
13fe0198 | 4973 | fnvlist_add_string(ha->nvl, name, ha->tag); |
13fe0198 MA |
4974 | |
4975 | if (ha->recursive) | |
4976 | rv = zfs_iter_filesystems(zhp, zfs_hold_one, ha); | |
4977 | zfs_close(zhp); | |
4978 | return (rv); | |
4979 | } | |
4980 | ||
45d1cae3 BB |
4981 | int |
4982 | zfs_hold(zfs_handle_t *zhp, const char *snapname, const char *tag, | |
95fd54a1 | 4983 | boolean_t recursive, int cleanup_fd) |
45d1cae3 | 4984 | { |
13fe0198 MA |
4985 | int ret; |
4986 | struct holdarg ha; | |
45d1cae3 | 4987 | |
13fe0198 MA |
4988 | ha.nvl = fnvlist_alloc(); |
4989 | ha.snapname = snapname; | |
4990 | ha.tag = tag; | |
4991 | ha.recursive = recursive; | |
4992 | (void) zfs_hold_one(zfs_handle_dup(zhp), &ha); | |
b1118acb | 4993 | |
95fd54a1 SH |
4994 | if (nvlist_empty(ha.nvl)) { |
4995 | char errbuf[1024]; | |
4996 | ||
b1118acb MM |
4997 | fnvlist_free(ha.nvl); |
4998 | ret = ENOENT; | |
95fd54a1 SH |
4999 | (void) snprintf(errbuf, sizeof (errbuf), |
5000 | dgettext(TEXT_DOMAIN, | |
5001 | "cannot hold snapshot '%s@%s'"), | |
5002 | zhp->zfs_name, snapname); | |
5003 | (void) zfs_standard_error(zhp->zfs_hdl, ret, errbuf); | |
b1118acb MM |
5004 | return (ret); |
5005 | } | |
5006 | ||
95fd54a1 | 5007 | ret = zfs_hold_nvl(zhp, cleanup_fd, ha.nvl); |
13fe0198 | 5008 | fnvlist_free(ha.nvl); |
572e2857 | 5009 | |
95fd54a1 SH |
5010 | return (ret); |
5011 | } | |
5012 | ||
5013 | int | |
5014 | zfs_hold_nvl(zfs_handle_t *zhp, int cleanup_fd, nvlist_t *holds) | |
5015 | { | |
5016 | int ret; | |
5017 | nvlist_t *errors; | |
5018 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
5019 | char errbuf[1024]; | |
5020 | nvpair_t *elem; | |
5021 | ||
5022 | errors = NULL; | |
5023 | ret = lzc_hold(holds, cleanup_fd, &errors); | |
5024 | ||
5025 | if (ret == 0) { | |
5026 | /* There may be errors even in the success case. */ | |
5027 | fnvlist_free(errors); | |
13fe0198 | 5028 | return (0); |
95fd54a1 | 5029 | } |
45d1cae3 | 5030 | |
95fd54a1 | 5031 | if (nvlist_empty(errors)) { |
13fe0198 MA |
5032 | /* no hold-specific errors */ |
5033 | (void) snprintf(errbuf, sizeof (errbuf), | |
5034 | dgettext(TEXT_DOMAIN, "cannot hold")); | |
5035 | switch (ret) { | |
5036 | case ENOTSUP: | |
5037 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
5038 | "pool must be upgraded")); | |
5039 | (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
5040 | break; | |
5041 | case EINVAL: | |
5042 | (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); | |
5043 | break; | |
5044 | default: | |
5045 | (void) zfs_standard_error(hdl, ret, errbuf); | |
5046 | } | |
5047 | } | |
45d1cae3 | 5048 | |
13fe0198 MA |
5049 | for (elem = nvlist_next_nvpair(errors, NULL); |
5050 | elem != NULL; | |
5051 | elem = nvlist_next_nvpair(errors, elem)) { | |
5052 | (void) snprintf(errbuf, sizeof (errbuf), | |
5053 | dgettext(TEXT_DOMAIN, | |
5054 | "cannot hold snapshot '%s'"), nvpair_name(elem)); | |
5055 | switch (fnvpair_value_int32(elem)) { | |
428870ff BB |
5056 | case E2BIG: |
5057 | /* | |
5058 | * Temporary tags wind up having the ds object id | |
5059 | * prepended. So even if we passed the length check | |
5060 | * above, it's still possible for the tag to wind | |
5061 | * up being slightly too long. | |
5062 | */ | |
13fe0198 MA |
5063 | (void) zfs_error(hdl, EZFS_TAGTOOLONG, errbuf); |
5064 | break; | |
45d1cae3 | 5065 | case EINVAL: |
13fe0198 MA |
5066 | (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); |
5067 | break; | |
45d1cae3 | 5068 | case EEXIST: |
13fe0198 MA |
5069 | (void) zfs_error(hdl, EZFS_REFTAG_HOLD, errbuf); |
5070 | break; | |
45d1cae3 | 5071 | default: |
13fe0198 MA |
5072 | (void) zfs_standard_error(hdl, |
5073 | fnvpair_value_int32(elem), errbuf); | |
45d1cae3 BB |
5074 | } |
5075 | } | |
5076 | ||
13fe0198 MA |
5077 | fnvlist_free(errors); |
5078 | return (ret); | |
5079 | } | |
5080 | ||
13fe0198 MA |
5081 | static int |
5082 | zfs_release_one(zfs_handle_t *zhp, void *arg) | |
5083 | { | |
5084 | struct holdarg *ha = arg; | |
eca7b760 | 5085 | char name[ZFS_MAX_DATASET_NAME_LEN]; |
13fe0198 | 5086 | int rv = 0; |
1a077756 | 5087 | nvlist_t *existing_holds; |
13fe0198 | 5088 | |
682ce104 TH |
5089 | if (snprintf(name, sizeof (name), "%s@%s", zhp->zfs_name, |
5090 | ha->snapname) >= sizeof (name)) { | |
5091 | ha->error = EINVAL; | |
5092 | rv = EINVAL; | |
5093 | } | |
13fe0198 | 5094 | |
1a077756 MA |
5095 | if (lzc_get_holds(name, &existing_holds) != 0) { |
5096 | ha->error = ENOENT; | |
5097 | } else if (!nvlist_exists(existing_holds, ha->tag)) { | |
5098 | ha->error = ESRCH; | |
5099 | } else { | |
5100 | nvlist_t *torelease = fnvlist_alloc(); | |
5101 | fnvlist_add_boolean(torelease, ha->tag); | |
5102 | fnvlist_add_nvlist(ha->nvl, name, torelease); | |
5103 | fnvlist_free(torelease); | |
13fe0198 MA |
5104 | } |
5105 | ||
5106 | if (ha->recursive) | |
5107 | rv = zfs_iter_filesystems(zhp, zfs_release_one, ha); | |
5108 | zfs_close(zhp); | |
5109 | return (rv); | |
45d1cae3 BB |
5110 | } |
5111 | ||
5112 | int | |
5113 | zfs_release(zfs_handle_t *zhp, const char *snapname, const char *tag, | |
5114 | boolean_t recursive) | |
5115 | { | |
13fe0198 MA |
5116 | int ret; |
5117 | struct holdarg ha; | |
95fd54a1 | 5118 | nvlist_t *errors = NULL; |
13fe0198 | 5119 | nvpair_t *elem; |
45d1cae3 | 5120 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
b1118acb | 5121 | char errbuf[1024]; |
45d1cae3 | 5122 | |
13fe0198 MA |
5123 | ha.nvl = fnvlist_alloc(); |
5124 | ha.snapname = snapname; | |
5125 | ha.tag = tag; | |
5126 | ha.recursive = recursive; | |
1a077756 | 5127 | ha.error = 0; |
13fe0198 | 5128 | (void) zfs_release_one(zfs_handle_dup(zhp), &ha); |
b1118acb | 5129 | |
95fd54a1 | 5130 | if (nvlist_empty(ha.nvl)) { |
b1118acb | 5131 | fnvlist_free(ha.nvl); |
1a077756 | 5132 | ret = ha.error; |
b1118acb MM |
5133 | (void) snprintf(errbuf, sizeof (errbuf), |
5134 | dgettext(TEXT_DOMAIN, | |
5135 | "cannot release hold from snapshot '%s@%s'"), | |
5136 | zhp->zfs_name, snapname); | |
1a077756 MA |
5137 | if (ret == ESRCH) { |
5138 | (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); | |
5139 | } else { | |
5140 | (void) zfs_standard_error(hdl, ret, errbuf); | |
5141 | } | |
b1118acb MM |
5142 | return (ret); |
5143 | } | |
5144 | ||
13fe0198 MA |
5145 | ret = lzc_release(ha.nvl, &errors); |
5146 | fnvlist_free(ha.nvl); | |
45d1cae3 | 5147 | |
95fd54a1 SH |
5148 | if (ret == 0) { |
5149 | /* There may be errors even in the success case. */ | |
5150 | fnvlist_free(errors); | |
13fe0198 | 5151 | return (0); |
95fd54a1 | 5152 | } |
13fe0198 | 5153 | |
95fd54a1 | 5154 | if (nvlist_empty(errors)) { |
13fe0198 | 5155 | /* no hold-specific errors */ |
45d1cae3 | 5156 | (void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
13fe0198 | 5157 | "cannot release")); |
45d1cae3 | 5158 | switch (errno) { |
45d1cae3 BB |
5159 | case ENOTSUP: |
5160 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
5161 | "pool must be upgraded")); | |
13fe0198 MA |
5162 | (void) zfs_error(hdl, EZFS_BADVERSION, errbuf); |
5163 | break; | |
5164 | default: | |
5165 | (void) zfs_standard_error_fmt(hdl, errno, errbuf); | |
5166 | } | |
5167 | } | |
5168 | ||
5169 | for (elem = nvlist_next_nvpair(errors, NULL); | |
5170 | elem != NULL; | |
5171 | elem = nvlist_next_nvpair(errors, elem)) { | |
13fe0198 MA |
5172 | (void) snprintf(errbuf, sizeof (errbuf), |
5173 | dgettext(TEXT_DOMAIN, | |
5174 | "cannot release hold from snapshot '%s'"), | |
5175 | nvpair_name(elem)); | |
5176 | switch (fnvpair_value_int32(elem)) { | |
5177 | case ESRCH: | |
5178 | (void) zfs_error(hdl, EZFS_REFTAG_RELE, errbuf); | |
5179 | break; | |
45d1cae3 | 5180 | case EINVAL: |
13fe0198 MA |
5181 | (void) zfs_error(hdl, EZFS_BADTYPE, errbuf); |
5182 | break; | |
45d1cae3 | 5183 | default: |
13fe0198 MA |
5184 | (void) zfs_standard_error_fmt(hdl, |
5185 | fnvpair_value_int32(elem), errbuf); | |
45d1cae3 BB |
5186 | } |
5187 | } | |
5188 | ||
13fe0198 MA |
5189 | fnvlist_free(errors); |
5190 | return (ret); | |
45d1cae3 | 5191 | } |
428870ff | 5192 | |
0b7936d5 AS |
5193 | int |
5194 | zfs_get_fsacl(zfs_handle_t *zhp, nvlist_t **nvl) | |
5195 | { | |
13fe0198 | 5196 | zfs_cmd_t zc = {"\0"}; |
0b7936d5 AS |
5197 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
5198 | int nvsz = 2048; | |
5199 | void *nvbuf; | |
5200 | int err = 0; | |
13fe0198 | 5201 | char errbuf[1024]; |
0b7936d5 AS |
5202 | |
5203 | assert(zhp->zfs_type == ZFS_TYPE_VOLUME || | |
5204 | zhp->zfs_type == ZFS_TYPE_FILESYSTEM); | |
5205 | ||
5206 | tryagain: | |
5207 | ||
5208 | nvbuf = malloc(nvsz); | |
5209 | if (nvbuf == NULL) { | |
5210 | err = (zfs_error(hdl, EZFS_NOMEM, strerror(errno))); | |
5211 | goto out; | |
5212 | } | |
5213 | ||
5214 | zc.zc_nvlist_dst_size = nvsz; | |
5215 | zc.zc_nvlist_dst = (uintptr_t)nvbuf; | |
5216 | ||
eca7b760 | 5217 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
0b7936d5 | 5218 | |
c7f2d69d | 5219 | if (ioctl(hdl->libzfs_fd, ZFS_IOC_GET_FSACL, &zc) != 0) { |
0b7936d5 AS |
5220 | (void) snprintf(errbuf, sizeof (errbuf), |
5221 | dgettext(TEXT_DOMAIN, "cannot get permissions on '%s'"), | |
5222 | zc.zc_name); | |
5223 | switch (errno) { | |
5224 | case ENOMEM: | |
5225 | free(nvbuf); | |
5226 | nvsz = zc.zc_nvlist_dst_size; | |
5227 | goto tryagain; | |
5228 | ||
5229 | case ENOTSUP: | |
5230 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
5231 | "pool must be upgraded")); | |
5232 | err = zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
5233 | break; | |
5234 | case EINVAL: | |
5235 | err = zfs_error(hdl, EZFS_BADTYPE, errbuf); | |
5236 | break; | |
5237 | case ENOENT: | |
5238 | err = zfs_error(hdl, EZFS_NOENT, errbuf); | |
5239 | break; | |
5240 | default: | |
5241 | err = zfs_standard_error_fmt(hdl, errno, errbuf); | |
5242 | break; | |
5243 | } | |
5244 | } else { | |
5245 | /* success */ | |
5246 | int rc = nvlist_unpack(nvbuf, zc.zc_nvlist_dst_size, nvl, 0); | |
5247 | if (rc) { | |
5248 | (void) snprintf(errbuf, sizeof (errbuf), dgettext( | |
5249 | TEXT_DOMAIN, "cannot get permissions on '%s'"), | |
5250 | zc.zc_name); | |
5251 | err = zfs_standard_error_fmt(hdl, rc, errbuf); | |
5252 | } | |
5253 | } | |
5254 | ||
5255 | free(nvbuf); | |
5256 | out: | |
5257 | return (err); | |
5258 | } | |
5259 | ||
5260 | int | |
5261 | zfs_set_fsacl(zfs_handle_t *zhp, boolean_t un, nvlist_t *nvl) | |
5262 | { | |
13fe0198 | 5263 | zfs_cmd_t zc = {"\0"}; |
0b7936d5 AS |
5264 | libzfs_handle_t *hdl = zhp->zfs_hdl; |
5265 | char *nvbuf; | |
13fe0198 | 5266 | char errbuf[1024]; |
0b7936d5 AS |
5267 | size_t nvsz; |
5268 | int err; | |
5269 | ||
5270 | assert(zhp->zfs_type == ZFS_TYPE_VOLUME || | |
5271 | zhp->zfs_type == ZFS_TYPE_FILESYSTEM); | |
5272 | ||
5273 | err = nvlist_size(nvl, &nvsz, NV_ENCODE_NATIVE); | |
5274 | assert(err == 0); | |
5275 | ||
5276 | nvbuf = malloc(nvsz); | |
5277 | ||
5278 | err = nvlist_pack(nvl, &nvbuf, &nvsz, NV_ENCODE_NATIVE, 0); | |
5279 | assert(err == 0); | |
5280 | ||
5281 | zc.zc_nvlist_src_size = nvsz; | |
5282 | zc.zc_nvlist_src = (uintptr_t)nvbuf; | |
5283 | zc.zc_perm_action = un; | |
5284 | ||
5285 | (void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); | |
5286 | ||
5287 | if (zfs_ioctl(hdl, ZFS_IOC_SET_FSACL, &zc) != 0) { | |
5288 | (void) snprintf(errbuf, sizeof (errbuf), | |
5289 | dgettext(TEXT_DOMAIN, "cannot set permissions on '%s'"), | |
5290 | zc.zc_name); | |
5291 | switch (errno) { | |
5292 | case ENOTSUP: | |
5293 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
5294 | "pool must be upgraded")); | |
5295 | err = zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
5296 | break; | |
5297 | case EINVAL: | |
5298 | err = zfs_error(hdl, EZFS_BADTYPE, errbuf); | |
5299 | break; | |
5300 | case ENOENT: | |
5301 | err = zfs_error(hdl, EZFS_NOENT, errbuf); | |
5302 | break; | |
5303 | default: | |
5304 | err = zfs_standard_error_fmt(hdl, errno, errbuf); | |
5305 | break; | |
5306 | } | |
5307 | } | |
5308 | ||
5309 | free(nvbuf); | |
5310 | ||
5311 | return (err); | |
5312 | } | |
5313 | ||
5314 | int | |
5315 | zfs_get_holds(zfs_handle_t *zhp, nvlist_t **nvl) | |
5316 | { | |
13fe0198 MA |
5317 | int err; |
5318 | char errbuf[1024]; | |
0b7936d5 | 5319 | |
13fe0198 | 5320 | err = lzc_get_holds(zhp->zfs_name, nvl); |
0b7936d5 | 5321 | |
13fe0198 MA |
5322 | if (err != 0) { |
5323 | libzfs_handle_t *hdl = zhp->zfs_hdl; | |
0b7936d5 | 5324 | |
0b7936d5 AS |
5325 | (void) snprintf(errbuf, sizeof (errbuf), |
5326 | dgettext(TEXT_DOMAIN, "cannot get holds for '%s'"), | |
13fe0198 MA |
5327 | zhp->zfs_name); |
5328 | switch (err) { | |
0b7936d5 AS |
5329 | case ENOTSUP: |
5330 | zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, | |
5331 | "pool must be upgraded")); | |
5332 | err = zfs_error(hdl, EZFS_BADVERSION, errbuf); | |
5333 | break; | |
5334 | case EINVAL: | |
5335 | err = zfs_error(hdl, EZFS_BADTYPE, errbuf); | |
5336 | break; | |
5337 | case ENOENT: | |
5338 | err = zfs_error(hdl, EZFS_NOENT, errbuf); | |
5339 | break; | |
5340 | default: | |
5341 | err = zfs_standard_error_fmt(hdl, errno, errbuf); | |
5342 | break; | |
5343 | } | |
0b7936d5 AS |
5344 | } |
5345 | ||
0b7936d5 AS |
5346 | return (err); |
5347 | } | |
5348 | ||
e49f1e20 WA |
5349 | /* |
5350 | * Convert the zvol's volume size to an appropriate reservation. | |
5351 | * Note: If this routine is updated, it is necessary to update the ZFS test | |
5352 | * suite's shell version in reservation.kshlib. | |
5353 | */ | |
428870ff BB |
5354 | uint64_t |
5355 | zvol_volsize_to_reservation(uint64_t volsize, nvlist_t *props) | |
5356 | { | |
5357 | uint64_t numdb; | |
5358 | uint64_t nblocks, volblocksize; | |
5359 | int ncopies; | |
5360 | char *strval; | |
5361 | ||
5362 | if (nvlist_lookup_string(props, | |
5363 | zfs_prop_to_name(ZFS_PROP_COPIES), &strval) == 0) | |
5364 | ncopies = atoi(strval); | |
5365 | else | |
5366 | ncopies = 1; | |
5367 | if (nvlist_lookup_uint64(props, | |
5368 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), | |
5369 | &volblocksize) != 0) | |
5370 | volblocksize = ZVOL_DEFAULT_BLOCKSIZE; | |
5371 | nblocks = volsize/volblocksize; | |
5372 | /* start with metadnode L0-L6 */ | |
5373 | numdb = 7; | |
5374 | /* calculate number of indirects */ | |
5375 | while (nblocks > 1) { | |
5376 | nblocks += DNODES_PER_LEVEL - 1; | |
5377 | nblocks /= DNODES_PER_LEVEL; | |
5378 | numdb += nblocks; | |
5379 | } | |
5380 | numdb *= MIN(SPA_DVAS_PER_BP, ncopies + 1); | |
5381 | volsize *= ncopies; | |
5382 | /* | |
5383 | * this is exactly DN_MAX_INDBLKSHIFT when metadata isn't | |
5384 | * compressed, but in practice they compress down to about | |
5385 | * 1100 bytes | |
5386 | */ | |
5387 | numdb *= 1ULL << DN_MAX_INDBLKSHIFT; | |
5388 | volsize += numdb; | |
5389 | return (volsize); | |
5390 | } |