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