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