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