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