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