]>
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 | */ | |
9ae529ec | 21 | |
34dc7c2f | 22 | /* |
428870ff | 23 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
b129c659 | 24 | * Portions Copyright 2011 Martin Matuska |
671c9354 | 25 | * Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved. |
0cee2406 | 26 | * Portions Copyright 2012 Pawel Jakub Dawidek <pawel@dawidek.net> |
005e27e3 | 27 | * Copyright (c) 2014, 2016 Joyent, Inc. All rights reserved. |
1b87e0f5 | 28 | * Copyright 2016 Nexenta Systems, Inc. All rights reserved. |
788eb90c | 29 | * Copyright (c) 2014, Joyent, Inc. All rights reserved. |
4b2a3e0c | 30 | * Copyright (c) 2011, 2015 by Delphix. All rights reserved. |
9759c60f | 31 | * Copyright (c) 2013 by Saso Kiselkov. All rights reserved. |
95fd54a1 | 32 | * Copyright (c) 2013 Steven Hartland. All rights reserved. |
e550644f BB |
33 | * Copyright (c) 2014 Integros [integros.com] |
34 | * Copyright 2016 Toomas Soome <tsoome@me.com> | |
a0bd735a | 35 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
a3eeab2d | 36 | * Copyright (c) 2017, loli10K <ezomori.nozomu@gmail.com>. All rights reserved. |
b5256303 | 37 | * Copyright (c) 2017 Datto Inc. All rights reserved. |
d12f91fd | 38 | * Copyright 2017 RackTop Systems. |
6f1ffb06 MA |
39 | */ |
40 | ||
41 | /* | |
42 | * ZFS ioctls. | |
43 | * | |
44 | * This file handles the ioctls to /dev/zfs, used for configuring ZFS storage | |
45 | * pools and filesystems, e.g. with /sbin/zfs and /sbin/zpool. | |
46 | * | |
47 | * There are two ways that we handle ioctls: the legacy way where almost | |
48 | * all of the logic is in the ioctl callback, and the new way where most | |
49 | * of the marshalling is handled in the common entry point, zfsdev_ioctl(). | |
50 | * | |
51 | * Non-legacy ioctls should be registered by calling | |
52 | * zfs_ioctl_register() from zfs_ioctl_init(). The ioctl is invoked | |
53 | * from userland by lzc_ioctl(). | |
54 | * | |
55 | * The registration arguments are as follows: | |
56 | * | |
57 | * const char *name | |
58 | * The name of the ioctl. This is used for history logging. If the | |
59 | * ioctl returns successfully (the callback returns 0), and allow_log | |
60 | * is true, then a history log entry will be recorded with the input & | |
61 | * output nvlists. The log entry can be printed with "zpool history -i". | |
62 | * | |
63 | * zfs_ioc_t ioc | |
64 | * The ioctl request number, which userland will pass to ioctl(2). | |
65 | * The ioctl numbers can change from release to release, because | |
66 | * the caller (libzfs) must be matched to the kernel. | |
67 | * | |
68 | * zfs_secpolicy_func_t *secpolicy | |
69 | * This function will be called before the zfs_ioc_func_t, to | |
70 | * determine if this operation is permitted. It should return EPERM | |
71 | * on failure, and 0 on success. Checks include determining if the | |
72 | * dataset is visible in this zone, and if the user has either all | |
73 | * zfs privileges in the zone (SYS_MOUNT), or has been granted permission | |
74 | * to do this operation on this dataset with "zfs allow". | |
75 | * | |
76 | * zfs_ioc_namecheck_t namecheck | |
77 | * This specifies what to expect in the zfs_cmd_t:zc_name -- a pool | |
78 | * name, a dataset name, or nothing. If the name is not well-formed, | |
79 | * the ioctl will fail and the callback will not be called. | |
80 | * Therefore, the callback can assume that the name is well-formed | |
81 | * (e.g. is null-terminated, doesn't have more than one '@' character, | |
82 | * doesn't have invalid characters). | |
83 | * | |
84 | * zfs_ioc_poolcheck_t pool_check | |
85 | * This specifies requirements on the pool state. If the pool does | |
86 | * not meet them (is suspended or is readonly), the ioctl will fail | |
87 | * and the callback will not be called. If any checks are specified | |
88 | * (i.e. it is not POOL_CHECK_NONE), namecheck must not be NO_NAME. | |
89 | * Multiple checks can be or-ed together (e.g. POOL_CHECK_SUSPENDED | | |
90 | * POOL_CHECK_READONLY). | |
91 | * | |
92 | * boolean_t smush_outnvlist | |
93 | * If smush_outnvlist is true, then the output is presumed to be a | |
94 | * list of errors, and it will be "smushed" down to fit into the | |
95 | * caller's buffer, by removing some entries and replacing them with a | |
96 | * single "N_MORE_ERRORS" entry indicating how many were removed. See | |
97 | * nvlist_smush() for details. If smush_outnvlist is false, and the | |
98 | * outnvlist does not fit into the userland-provided buffer, then the | |
99 | * ioctl will fail with ENOMEM. | |
100 | * | |
101 | * zfs_ioc_func_t *func | |
102 | * The callback function that will perform the operation. | |
103 | * | |
104 | * The callback should return 0 on success, or an error number on | |
105 | * failure. If the function fails, the userland ioctl will return -1, | |
106 | * and errno will be set to the callback's return value. The callback | |
107 | * will be called with the following arguments: | |
108 | * | |
109 | * const char *name | |
110 | * The name of the pool or dataset to operate on, from | |
111 | * zfs_cmd_t:zc_name. The 'namecheck' argument specifies the | |
112 | * expected type (pool, dataset, or none). | |
113 | * | |
114 | * nvlist_t *innvl | |
115 | * The input nvlist, deserialized from zfs_cmd_t:zc_nvlist_src. Or | |
116 | * NULL if no input nvlist was provided. Changes to this nvlist are | |
117 | * ignored. If the input nvlist could not be deserialized, the | |
118 | * ioctl will fail and the callback will not be called. | |
119 | * | |
120 | * nvlist_t *outnvl | |
121 | * The output nvlist, initially empty. The callback can fill it in, | |
122 | * and it will be returned to userland by serializing it into | |
123 | * zfs_cmd_t:zc_nvlist_dst. If it is non-empty, and serialization | |
124 | * fails (e.g. because the caller didn't supply a large enough | |
125 | * buffer), then the overall ioctl will fail. See the | |
126 | * 'smush_nvlist' argument above for additional behaviors. | |
127 | * | |
128 | * There are two typical uses of the output nvlist: | |
129 | * - To return state, e.g. property values. In this case, | |
130 | * smush_outnvlist should be false. If the buffer was not large | |
131 | * enough, the caller will reallocate a larger buffer and try | |
132 | * the ioctl again. | |
133 | * | |
134 | * - To return multiple errors from an ioctl which makes on-disk | |
135 | * changes. In this case, smush_outnvlist should be true. | |
136 | * Ioctls which make on-disk modifications should generally not | |
137 | * use the outnvl if they succeed, because the caller can not | |
138 | * distinguish between the operation failing, and | |
139 | * deserialization failing. | |
3541dc6d | 140 | */ |
34dc7c2f | 141 | |
34dc7c2f BB |
142 | #include <sys/types.h> |
143 | #include <sys/param.h> | |
144 | #include <sys/errno.h> | |
145 | #include <sys/uio.h> | |
146 | #include <sys/buf.h> | |
147 | #include <sys/modctl.h> | |
148 | #include <sys/open.h> | |
149 | #include <sys/file.h> | |
150 | #include <sys/kmem.h> | |
151 | #include <sys/conf.h> | |
152 | #include <sys/cmn_err.h> | |
153 | #include <sys/stat.h> | |
154 | #include <sys/zfs_ioctl.h> | |
428870ff | 155 | #include <sys/zfs_vfsops.h> |
34dc7c2f BB |
156 | #include <sys/zfs_znode.h> |
157 | #include <sys/zap.h> | |
158 | #include <sys/spa.h> | |
159 | #include <sys/spa_impl.h> | |
160 | #include <sys/vdev.h> | |
4a283c7f | 161 | #include <sys/vdev_impl.h> |
428870ff | 162 | #include <sys/priv_impl.h> |
34dc7c2f BB |
163 | #include <sys/dmu.h> |
164 | #include <sys/dsl_dir.h> | |
165 | #include <sys/dsl_dataset.h> | |
166 | #include <sys/dsl_prop.h> | |
167 | #include <sys/dsl_deleg.h> | |
168 | #include <sys/dmu_objset.h> | |
37abac6d | 169 | #include <sys/dmu_impl.h> |
13fe0198 | 170 | #include <sys/dmu_tx.h> |
34dc7c2f BB |
171 | #include <sys/ddi.h> |
172 | #include <sys/sunddi.h> | |
173 | #include <sys/sunldi.h> | |
174 | #include <sys/policy.h> | |
175 | #include <sys/zone.h> | |
176 | #include <sys/nvpair.h> | |
177 | #include <sys/pathname.h> | |
178 | #include <sys/mount.h> | |
179 | #include <sys/sdt.h> | |
180 | #include <sys/fs/zfs.h> | |
ebe7e575 | 181 | #include <sys/zfs_ctldir.h> |
34dc7c2f | 182 | #include <sys/zfs_dir.h> |
572e2857 | 183 | #include <sys/zfs_onexit.h> |
34dc7c2f | 184 | #include <sys/zvol.h> |
428870ff | 185 | #include <sys/dsl_scan.h> |
34dc7c2f | 186 | #include <sharefs/share.h> |
325f0235 | 187 | #include <sys/fm/util.h> |
b5256303 | 188 | #include <sys/dsl_crypt.h> |
325f0235 | 189 | |
13fe0198 MA |
190 | #include <sys/dmu_send.h> |
191 | #include <sys/dsl_destroy.h> | |
da536844 | 192 | #include <sys/dsl_bookmark.h> |
13fe0198 | 193 | #include <sys/dsl_userhold.h> |
9759c60f | 194 | #include <sys/zfeature.h> |
3c67d83a | 195 | #include <sys/zio_checksum.h> |
9759c60f | 196 | |
325f0235 | 197 | #include <linux/miscdevice.h> |
f74b821a | 198 | #include <linux/slab.h> |
34dc7c2f BB |
199 | |
200 | #include "zfs_namecheck.h" | |
201 | #include "zfs_prop.h" | |
202 | #include "zfs_deleg.h" | |
428870ff | 203 | #include "zfs_comutil.h" |
34dc7c2f | 204 | |
f74b821a BB |
205 | /* |
206 | * Limit maximum nvlist size. We don't want users passing in insane values | |
207 | * for zc->zc_nvlist_src_size, since we will need to allocate that much memory. | |
208 | */ | |
209 | #define MAX_NVLIST_SRC_SIZE KMALLOC_MAX_SIZE | |
210 | ||
325f0235 | 211 | kmutex_t zfsdev_state_lock; |
3937ab20 | 212 | zfsdev_state_t *zfsdev_state_list; |
34dc7c2f BB |
213 | |
214 | extern void zfs_init(void); | |
215 | extern void zfs_fini(void); | |
216 | ||
6f1ffb06 MA |
217 | uint_t zfs_fsyncer_key; |
218 | extern uint_t rrw_tsd_key; | |
219 | static uint_t zfs_allow_log_key; | |
220 | ||
221 | typedef int zfs_ioc_legacy_func_t(zfs_cmd_t *); | |
222 | typedef int zfs_ioc_func_t(const char *, nvlist_t *, nvlist_t *); | |
223 | typedef int zfs_secpolicy_func_t(zfs_cmd_t *, nvlist_t *, cred_t *); | |
34dc7c2f | 224 | |
9babb374 BB |
225 | typedef enum { |
226 | NO_NAME, | |
227 | POOL_NAME, | |
228 | DATASET_NAME | |
229 | } zfs_ioc_namecheck_t; | |
230 | ||
572e2857 BB |
231 | typedef enum { |
232 | POOL_CHECK_NONE = 1 << 0, | |
233 | POOL_CHECK_SUSPENDED = 1 << 1, | |
6f1ffb06 | 234 | POOL_CHECK_READONLY = 1 << 2, |
572e2857 BB |
235 | } zfs_ioc_poolcheck_t; |
236 | ||
34dc7c2f | 237 | typedef struct zfs_ioc_vec { |
6f1ffb06 | 238 | zfs_ioc_legacy_func_t *zvec_legacy_func; |
34dc7c2f BB |
239 | zfs_ioc_func_t *zvec_func; |
240 | zfs_secpolicy_func_t *zvec_secpolicy; | |
9babb374 | 241 | zfs_ioc_namecheck_t zvec_namecheck; |
6f1ffb06 | 242 | boolean_t zvec_allow_log; |
572e2857 | 243 | zfs_ioc_poolcheck_t zvec_pool_check; |
6f1ffb06 MA |
244 | boolean_t zvec_smush_outnvlist; |
245 | const char *zvec_name; | |
34dc7c2f BB |
246 | } zfs_ioc_vec_t; |
247 | ||
9babb374 BB |
248 | /* This array is indexed by zfs_userquota_prop_t */ |
249 | static const char *userquota_perms[] = { | |
250 | ZFS_DELEG_PERM_USERUSED, | |
251 | ZFS_DELEG_PERM_USERQUOTA, | |
252 | ZFS_DELEG_PERM_GROUPUSED, | |
253 | ZFS_DELEG_PERM_GROUPQUOTA, | |
1de321e6 JX |
254 | ZFS_DELEG_PERM_USEROBJUSED, |
255 | ZFS_DELEG_PERM_USEROBJQUOTA, | |
256 | ZFS_DELEG_PERM_GROUPOBJUSED, | |
257 | ZFS_DELEG_PERM_GROUPOBJQUOTA, | |
9babb374 BB |
258 | }; |
259 | ||
260 | static int zfs_ioc_userspace_upgrade(zfs_cmd_t *zc); | |
1de321e6 | 261 | static int zfs_ioc_userobjspace_upgrade(zfs_cmd_t *zc); |
428870ff BB |
262 | static int zfs_check_settable(const char *name, nvpair_t *property, |
263 | cred_t *cr); | |
264 | static int zfs_check_clearable(char *dataset, nvlist_t *props, | |
265 | nvlist_t **errors); | |
b128c09f BB |
266 | static int zfs_fill_zplprops_root(uint64_t, nvlist_t *, nvlist_t *, |
267 | boolean_t *); | |
6f1ffb06 MA |
268 | int zfs_set_prop_nvlist(const char *, zprop_source_t, nvlist_t *, nvlist_t *); |
269 | static int get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp); | |
b128c09f | 270 | |
34dc7c2f BB |
271 | static void |
272 | history_str_free(char *buf) | |
273 | { | |
274 | kmem_free(buf, HIS_MAX_RECORD_LEN); | |
275 | } | |
276 | ||
277 | static char * | |
278 | history_str_get(zfs_cmd_t *zc) | |
279 | { | |
280 | char *buf; | |
281 | ||
b8864a23 | 282 | if (zc->zc_history == 0) |
34dc7c2f BB |
283 | return (NULL); |
284 | ||
efcd79a8 | 285 | buf = kmem_alloc(HIS_MAX_RECORD_LEN, KM_SLEEP); |
34dc7c2f BB |
286 | if (copyinstr((void *)(uintptr_t)zc->zc_history, |
287 | buf, HIS_MAX_RECORD_LEN, NULL) != 0) { | |
288 | history_str_free(buf); | |
289 | return (NULL); | |
290 | } | |
291 | ||
292 | buf[HIS_MAX_RECORD_LEN -1] = '\0'; | |
293 | ||
294 | return (buf); | |
295 | } | |
296 | ||
297 | /* | |
b128c09f BB |
298 | * Check to see if the named dataset is currently defined as bootable |
299 | */ | |
300 | static boolean_t | |
301 | zfs_is_bootfs(const char *name) | |
302 | { | |
428870ff | 303 | objset_t *os; |
b128c09f | 304 | |
428870ff BB |
305 | if (dmu_objset_hold(name, FTAG, &os) == 0) { |
306 | boolean_t ret; | |
307 | ret = (dmu_objset_id(os) == spa_bootfs(dmu_objset_spa(os))); | |
308 | dmu_objset_rele(os, FTAG); | |
309 | return (ret); | |
b128c09f | 310 | } |
428870ff | 311 | return (B_FALSE); |
b128c09f BB |
312 | } |
313 | ||
314 | /* | |
d3cc8b15 | 315 | * Return non-zero if the spa version is less than requested version. |
34dc7c2f BB |
316 | */ |
317 | static int | |
b128c09f | 318 | zfs_earlier_version(const char *name, int version) |
34dc7c2f | 319 | { |
34dc7c2f BB |
320 | spa_t *spa; |
321 | ||
322 | if (spa_open(name, &spa, FTAG) == 0) { | |
323 | if (spa_version(spa) < version) { | |
324 | spa_close(spa, FTAG); | |
325 | return (1); | |
326 | } | |
327 | spa_close(spa, FTAG); | |
328 | } | |
329 | return (0); | |
330 | } | |
331 | ||
332 | /* | |
b128c09f | 333 | * Return TRUE if the ZPL version is less than requested version. |
34dc7c2f | 334 | */ |
b128c09f BB |
335 | static boolean_t |
336 | zpl_earlier_version(const char *name, int version) | |
34dc7c2f BB |
337 | { |
338 | objset_t *os; | |
b128c09f | 339 | boolean_t rc = B_TRUE; |
34dc7c2f | 340 | |
428870ff | 341 | if (dmu_objset_hold(name, FTAG, &os) == 0) { |
b128c09f | 342 | uint64_t zplversion; |
34dc7c2f | 343 | |
428870ff BB |
344 | if (dmu_objset_type(os) != DMU_OST_ZFS) { |
345 | dmu_objset_rele(os, FTAG); | |
346 | return (B_TRUE); | |
347 | } | |
348 | /* XXX reading from non-owned objset */ | |
b128c09f BB |
349 | if (zfs_get_zplprop(os, ZFS_PROP_VERSION, &zplversion) == 0) |
350 | rc = zplversion < version; | |
428870ff | 351 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
352 | } |
353 | return (rc); | |
354 | } | |
355 | ||
356 | static void | |
357 | zfs_log_history(zfs_cmd_t *zc) | |
358 | { | |
359 | spa_t *spa; | |
360 | char *buf; | |
361 | ||
362 | if ((buf = history_str_get(zc)) == NULL) | |
363 | return; | |
364 | ||
365 | if (spa_open(zc->zc_name, &spa, FTAG) == 0) { | |
366 | if (spa_version(spa) >= SPA_VERSION_ZPOOL_HISTORY) | |
6f1ffb06 | 367 | (void) spa_history_log(spa, buf); |
34dc7c2f BB |
368 | spa_close(spa, FTAG); |
369 | } | |
370 | history_str_free(buf); | |
371 | } | |
372 | ||
373 | /* | |
374 | * Policy for top-level read operations (list pools). Requires no privileges, | |
375 | * and can be used in the local zone, as there is no associated dataset. | |
376 | */ | |
377 | /* ARGSUSED */ | |
378 | static int | |
6f1ffb06 | 379 | zfs_secpolicy_none(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f BB |
380 | { |
381 | return (0); | |
382 | } | |
383 | ||
384 | /* | |
385 | * Policy for dataset read operations (list children, get statistics). Requires | |
386 | * no privileges, but must be visible in the local zone. | |
387 | */ | |
388 | /* ARGSUSED */ | |
389 | static int | |
6f1ffb06 | 390 | zfs_secpolicy_read(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f BB |
391 | { |
392 | if (INGLOBALZONE(curproc) || | |
393 | zone_dataset_visible(zc->zc_name, NULL)) | |
394 | return (0); | |
395 | ||
2e528b49 | 396 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
397 | } |
398 | ||
399 | static int | |
572e2857 | 400 | zfs_dozonecheck_impl(const char *dataset, uint64_t zoned, cred_t *cr) |
34dc7c2f | 401 | { |
34dc7c2f BB |
402 | int writable = 1; |
403 | ||
404 | /* | |
405 | * The dataset must be visible by this zone -- check this first | |
406 | * so they don't see EPERM on something they shouldn't know about. | |
407 | */ | |
408 | if (!INGLOBALZONE(curproc) && | |
409 | !zone_dataset_visible(dataset, &writable)) | |
2e528b49 | 410 | return (SET_ERROR(ENOENT)); |
34dc7c2f | 411 | |
34dc7c2f BB |
412 | if (INGLOBALZONE(curproc)) { |
413 | /* | |
414 | * If the fs is zoned, only root can access it from the | |
415 | * global zone. | |
416 | */ | |
417 | if (secpolicy_zfs(cr) && zoned) | |
2e528b49 | 418 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
419 | } else { |
420 | /* | |
421 | * If we are in a local zone, the 'zoned' property must be set. | |
422 | */ | |
423 | if (!zoned) | |
2e528b49 | 424 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
425 | |
426 | /* must be writable by this zone */ | |
427 | if (!writable) | |
2e528b49 | 428 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
429 | } |
430 | return (0); | |
431 | } | |
432 | ||
572e2857 BB |
433 | static int |
434 | zfs_dozonecheck(const char *dataset, cred_t *cr) | |
435 | { | |
436 | uint64_t zoned; | |
437 | ||
438 | if (dsl_prop_get_integer(dataset, "zoned", &zoned, NULL)) | |
2e528b49 | 439 | return (SET_ERROR(ENOENT)); |
572e2857 BB |
440 | |
441 | return (zfs_dozonecheck_impl(dataset, zoned, cr)); | |
442 | } | |
443 | ||
444 | static int | |
445 | zfs_dozonecheck_ds(const char *dataset, dsl_dataset_t *ds, cred_t *cr) | |
446 | { | |
447 | uint64_t zoned; | |
448 | ||
13fe0198 | 449 | if (dsl_prop_get_int_ds(ds, "zoned", &zoned)) |
2e528b49 | 450 | return (SET_ERROR(ENOENT)); |
572e2857 BB |
451 | |
452 | return (zfs_dozonecheck_impl(dataset, zoned, cr)); | |
453 | } | |
454 | ||
6f1ffb06 | 455 | static int |
13fe0198 MA |
456 | zfs_secpolicy_write_perms_ds(const char *name, dsl_dataset_t *ds, |
457 | const char *perm, cred_t *cr) | |
34dc7c2f BB |
458 | { |
459 | int error; | |
460 | ||
330d06f9 | 461 | error = zfs_dozonecheck_ds(name, ds, cr); |
34dc7c2f BB |
462 | if (error == 0) { |
463 | error = secpolicy_zfs(cr); | |
13fe0198 | 464 | if (error != 0) |
6f1ffb06 | 465 | error = dsl_deleg_access_impl(ds, perm, cr); |
34dc7c2f BB |
466 | } |
467 | return (error); | |
468 | } | |
469 | ||
6f1ffb06 | 470 | static int |
13fe0198 | 471 | zfs_secpolicy_write_perms(const char *name, const char *perm, cred_t *cr) |
572e2857 BB |
472 | { |
473 | int error; | |
13fe0198 MA |
474 | dsl_dataset_t *ds; |
475 | dsl_pool_t *dp; | |
572e2857 | 476 | |
e88551d5 GM |
477 | /* |
478 | * First do a quick check for root in the global zone, which | |
479 | * is allowed to do all write_perms. This ensures that zfs_ioc_* | |
480 | * will get to handle nonexistent datasets. | |
481 | */ | |
482 | if (INGLOBALZONE(curproc) && secpolicy_zfs(cr) == 0) | |
483 | return (0); | |
484 | ||
13fe0198 MA |
485 | error = dsl_pool_hold(name, FTAG, &dp); |
486 | if (error != 0) | |
487 | return (error); | |
488 | ||
489 | error = dsl_dataset_hold(dp, name, FTAG, &ds); | |
490 | if (error != 0) { | |
491 | dsl_pool_rele(dp, FTAG); | |
492 | return (error); | |
572e2857 | 493 | } |
13fe0198 MA |
494 | |
495 | error = zfs_secpolicy_write_perms_ds(name, ds, perm, cr); | |
496 | ||
497 | dsl_dataset_rele(ds, FTAG); | |
498 | dsl_pool_rele(dp, FTAG); | |
572e2857 BB |
499 | return (error); |
500 | } | |
501 | ||
428870ff BB |
502 | /* |
503 | * Policy for setting the security label property. | |
504 | * | |
505 | * Returns 0 for success, non-zero for access and other errors. | |
506 | */ | |
34dc7c2f | 507 | static int |
428870ff | 508 | zfs_set_slabel_policy(const char *name, char *strval, cred_t *cr) |
34dc7c2f | 509 | { |
d2c15e84 | 510 | #ifdef HAVE_MLSLABEL |
428870ff BB |
511 | char ds_hexsl[MAXNAMELEN]; |
512 | bslabel_t ds_sl, new_sl; | |
513 | boolean_t new_default = FALSE; | |
514 | uint64_t zoned; | |
515 | int needed_priv = -1; | |
516 | int error; | |
517 | ||
518 | /* First get the existing dataset label. */ | |
519 | error = dsl_prop_get(name, zfs_prop_to_name(ZFS_PROP_MLSLABEL), | |
520 | 1, sizeof (ds_hexsl), &ds_hexsl, NULL); | |
13fe0198 | 521 | if (error != 0) |
2e528b49 | 522 | return (SET_ERROR(EPERM)); |
428870ff BB |
523 | |
524 | if (strcasecmp(strval, ZFS_MLSLABEL_DEFAULT) == 0) | |
525 | new_default = TRUE; | |
526 | ||
527 | /* The label must be translatable */ | |
528 | if (!new_default && (hexstr_to_label(strval, &new_sl) != 0)) | |
2e528b49 | 529 | return (SET_ERROR(EINVAL)); |
428870ff BB |
530 | |
531 | /* | |
532 | * In a non-global zone, disallow attempts to set a label that | |
533 | * doesn't match that of the zone; otherwise no other checks | |
534 | * are needed. | |
535 | */ | |
536 | if (!INGLOBALZONE(curproc)) { | |
537 | if (new_default || !blequal(&new_sl, CR_SL(CRED()))) | |
2e528b49 | 538 | return (SET_ERROR(EPERM)); |
428870ff BB |
539 | return (0); |
540 | } | |
541 | ||
542 | /* | |
543 | * For global-zone datasets (i.e., those whose zoned property is | |
544 | * "off", verify that the specified new label is valid for the | |
545 | * global zone. | |
546 | */ | |
547 | if (dsl_prop_get_integer(name, | |
548 | zfs_prop_to_name(ZFS_PROP_ZONED), &zoned, NULL)) | |
2e528b49 | 549 | return (SET_ERROR(EPERM)); |
428870ff BB |
550 | if (!zoned) { |
551 | if (zfs_check_global_label(name, strval) != 0) | |
2e528b49 | 552 | return (SET_ERROR(EPERM)); |
428870ff BB |
553 | } |
554 | ||
555 | /* | |
556 | * If the existing dataset label is nondefault, check if the | |
557 | * dataset is mounted (label cannot be changed while mounted). | |
0037b49e | 558 | * Get the zfsvfs_t; if there isn't one, then the dataset isn't |
428870ff BB |
559 | * mounted (or isn't a dataset, doesn't exist, ...). |
560 | */ | |
561 | if (strcasecmp(ds_hexsl, ZFS_MLSLABEL_DEFAULT) != 0) { | |
562 | objset_t *os; | |
563 | static char *setsl_tag = "setsl_tag"; | |
564 | ||
565 | /* | |
566 | * Try to own the dataset; abort if there is any error, | |
567 | * (e.g., already mounted, in use, or other error). | |
568 | */ | |
b5256303 | 569 | error = dmu_objset_own(name, DMU_OST_ZFS, B_TRUE, B_TRUE, |
428870ff | 570 | setsl_tag, &os); |
13fe0198 | 571 | if (error != 0) |
2e528b49 | 572 | return (SET_ERROR(EPERM)); |
428870ff | 573 | |
b5256303 | 574 | dmu_objset_disown(os, B_TRUE, setsl_tag); |
428870ff BB |
575 | |
576 | if (new_default) { | |
577 | needed_priv = PRIV_FILE_DOWNGRADE_SL; | |
578 | goto out_check; | |
579 | } | |
580 | ||
581 | if (hexstr_to_label(strval, &new_sl) != 0) | |
2e528b49 | 582 | return (SET_ERROR(EPERM)); |
428870ff BB |
583 | |
584 | if (blstrictdom(&ds_sl, &new_sl)) | |
585 | needed_priv = PRIV_FILE_DOWNGRADE_SL; | |
586 | else if (blstrictdom(&new_sl, &ds_sl)) | |
587 | needed_priv = PRIV_FILE_UPGRADE_SL; | |
588 | } else { | |
589 | /* dataset currently has a default label */ | |
590 | if (!new_default) | |
591 | needed_priv = PRIV_FILE_UPGRADE_SL; | |
592 | } | |
593 | ||
594 | out_check: | |
595 | if (needed_priv != -1) | |
596 | return (PRIV_POLICY(cr, needed_priv, B_FALSE, EPERM, NULL)); | |
597 | return (0); | |
d2c15e84 | 598 | #else |
ecb2b7dc | 599 | return (SET_ERROR(ENOTSUP)); |
d2c15e84 | 600 | #endif /* HAVE_MLSLABEL */ |
428870ff BB |
601 | } |
602 | ||
603 | static int | |
604 | zfs_secpolicy_setprop(const char *dsname, zfs_prop_t prop, nvpair_t *propval, | |
605 | cred_t *cr) | |
606 | { | |
607 | char *strval; | |
608 | ||
34dc7c2f BB |
609 | /* |
610 | * Check permissions for special properties. | |
611 | */ | |
612 | switch (prop) { | |
e75c13c3 BB |
613 | default: |
614 | break; | |
34dc7c2f BB |
615 | case ZFS_PROP_ZONED: |
616 | /* | |
617 | * Disallow setting of 'zoned' from within a local zone. | |
618 | */ | |
619 | if (!INGLOBALZONE(curproc)) | |
2e528b49 | 620 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
621 | break; |
622 | ||
623 | case ZFS_PROP_QUOTA: | |
788eb90c JJ |
624 | case ZFS_PROP_FILESYSTEM_LIMIT: |
625 | case ZFS_PROP_SNAPSHOT_LIMIT: | |
34dc7c2f BB |
626 | if (!INGLOBALZONE(curproc)) { |
627 | uint64_t zoned; | |
eca7b760 | 628 | char setpoint[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f BB |
629 | /* |
630 | * Unprivileged users are allowed to modify the | |
788eb90c | 631 | * limit on things *under* (ie. contained by) |
34dc7c2f BB |
632 | * the thing they own. |
633 | */ | |
428870ff | 634 | if (dsl_prop_get_integer(dsname, "zoned", &zoned, |
34dc7c2f | 635 | setpoint)) |
2e528b49 | 636 | return (SET_ERROR(EPERM)); |
428870ff | 637 | if (!zoned || strlen(dsname) <= strlen(setpoint)) |
2e528b49 | 638 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
639 | } |
640 | break; | |
428870ff BB |
641 | |
642 | case ZFS_PROP_MLSLABEL: | |
643 | if (!is_system_labeled()) | |
2e528b49 | 644 | return (SET_ERROR(EPERM)); |
428870ff BB |
645 | |
646 | if (nvpair_value_string(propval, &strval) == 0) { | |
647 | int err; | |
648 | ||
649 | err = zfs_set_slabel_policy(dsname, strval, CRED()); | |
650 | if (err != 0) | |
651 | return (err); | |
652 | } | |
653 | break; | |
34dc7c2f BB |
654 | } |
655 | ||
428870ff | 656 | return (zfs_secpolicy_write_perms(dsname, zfs_prop_to_name(prop), cr)); |
34dc7c2f BB |
657 | } |
658 | ||
6f1ffb06 MA |
659 | /* ARGSUSED */ |
660 | static int | |
661 | zfs_secpolicy_set_fsacl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
34dc7c2f BB |
662 | { |
663 | int error; | |
664 | ||
665 | error = zfs_dozonecheck(zc->zc_name, cr); | |
13fe0198 | 666 | if (error != 0) |
34dc7c2f BB |
667 | return (error); |
668 | ||
669 | /* | |
670 | * permission to set permissions will be evaluated later in | |
671 | * dsl_deleg_can_allow() | |
672 | */ | |
673 | return (0); | |
674 | } | |
675 | ||
6f1ffb06 MA |
676 | /* ARGSUSED */ |
677 | static int | |
678 | zfs_secpolicy_rollback(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
34dc7c2f | 679 | { |
428870ff BB |
680 | return (zfs_secpolicy_write_perms(zc->zc_name, |
681 | ZFS_DELEG_PERM_ROLLBACK, cr)); | |
34dc7c2f BB |
682 | } |
683 | ||
6f1ffb06 MA |
684 | /* ARGSUSED */ |
685 | static int | |
686 | zfs_secpolicy_send(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
34dc7c2f | 687 | { |
572e2857 BB |
688 | dsl_pool_t *dp; |
689 | dsl_dataset_t *ds; | |
690 | char *cp; | |
691 | int error; | |
692 | ||
693 | /* | |
694 | * Generate the current snapshot name from the given objsetid, then | |
695 | * use that name for the secpolicy/zone checks. | |
696 | */ | |
697 | cp = strchr(zc->zc_name, '@'); | |
698 | if (cp == NULL) | |
2e528b49 | 699 | return (SET_ERROR(EINVAL)); |
13fe0198 MA |
700 | error = dsl_pool_hold(zc->zc_name, FTAG, &dp); |
701 | if (error != 0) | |
572e2857 BB |
702 | return (error); |
703 | ||
572e2857 | 704 | error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &ds); |
13fe0198 MA |
705 | if (error != 0) { |
706 | dsl_pool_rele(dp, FTAG); | |
572e2857 | 707 | return (error); |
13fe0198 | 708 | } |
572e2857 BB |
709 | |
710 | dsl_dataset_name(ds, zc->zc_name); | |
711 | ||
712 | error = zfs_secpolicy_write_perms_ds(zc->zc_name, ds, | |
713 | ZFS_DELEG_PERM_SEND, cr); | |
714 | dsl_dataset_rele(ds, FTAG); | |
13fe0198 | 715 | dsl_pool_rele(dp, FTAG); |
572e2857 BB |
716 | |
717 | return (error); | |
34dc7c2f BB |
718 | } |
719 | ||
6f1ffb06 MA |
720 | /* ARGSUSED */ |
721 | static int | |
722 | zfs_secpolicy_send_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
723 | { | |
724 | return (zfs_secpolicy_write_perms(zc->zc_name, | |
725 | ZFS_DELEG_PERM_SEND, cr)); | |
726 | } | |
727 | ||
3c9609b3 | 728 | #ifdef HAVE_SMB_SHARE |
6f1ffb06 | 729 | /* ARGSUSED */ |
9babb374 | 730 | static int |
6f1ffb06 | 731 | zfs_secpolicy_deleg_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
9babb374 BB |
732 | { |
733 | vnode_t *vp; | |
734 | int error; | |
735 | ||
736 | if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, | |
737 | NO_FOLLOW, NULL, &vp)) != 0) | |
738 | return (error); | |
739 | ||
740 | /* Now make sure mntpnt and dataset are ZFS */ | |
741 | ||
742 | if (vp->v_vfsp->vfs_fstype != zfsfstype || | |
743 | (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), | |
744 | zc->zc_name) != 0)) { | |
745 | VN_RELE(vp); | |
2e528b49 | 746 | return (SET_ERROR(EPERM)); |
9babb374 BB |
747 | } |
748 | ||
749 | VN_RELE(vp); | |
750 | return (dsl_deleg_access(zc->zc_name, | |
751 | ZFS_DELEG_PERM_SHARE, cr)); | |
752 | } | |
3c9609b3 | 753 | #endif /* HAVE_SMB_SHARE */ |
9babb374 | 754 | |
34dc7c2f | 755 | int |
6f1ffb06 | 756 | zfs_secpolicy_share(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f | 757 | { |
3c9609b3 | 758 | #ifdef HAVE_SMB_SHARE |
34dc7c2f | 759 | if (!INGLOBALZONE(curproc)) |
2e528b49 | 760 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
761 | |
762 | if (secpolicy_nfs(cr) == 0) { | |
763 | return (0); | |
764 | } else { | |
6f1ffb06 | 765 | return (zfs_secpolicy_deleg_share(zc, innvl, cr)); |
9babb374 | 766 | } |
325f0235 | 767 | #else |
2e528b49 | 768 | return (SET_ERROR(ENOTSUP)); |
3c9609b3 | 769 | #endif /* HAVE_SMB_SHARE */ |
9babb374 | 770 | } |
34dc7c2f | 771 | |
9babb374 | 772 | int |
6f1ffb06 | 773 | zfs_secpolicy_smb_acl(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
9babb374 | 774 | { |
3c9609b3 | 775 | #ifdef HAVE_SMB_SHARE |
9babb374 | 776 | if (!INGLOBALZONE(curproc)) |
2e528b49 | 777 | return (SET_ERROR(EPERM)); |
34dc7c2f | 778 | |
9babb374 BB |
779 | if (secpolicy_smb(cr) == 0) { |
780 | return (0); | |
781 | } else { | |
6f1ffb06 | 782 | return (zfs_secpolicy_deleg_share(zc, innvl, cr)); |
34dc7c2f | 783 | } |
325f0235 | 784 | #else |
2e528b49 | 785 | return (SET_ERROR(ENOTSUP)); |
3c9609b3 | 786 | #endif /* HAVE_SMB_SHARE */ |
34dc7c2f BB |
787 | } |
788 | ||
789 | static int | |
790 | zfs_get_parent(const char *datasetname, char *parent, int parentsize) | |
791 | { | |
792 | char *cp; | |
793 | ||
794 | /* | |
795 | * Remove the @bla or /bla from the end of the name to get the parent. | |
796 | */ | |
797 | (void) strncpy(parent, datasetname, parentsize); | |
798 | cp = strrchr(parent, '@'); | |
799 | if (cp != NULL) { | |
800 | cp[0] = '\0'; | |
801 | } else { | |
802 | cp = strrchr(parent, '/'); | |
803 | if (cp == NULL) | |
2e528b49 | 804 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
805 | cp[0] = '\0'; |
806 | } | |
807 | ||
808 | return (0); | |
809 | } | |
810 | ||
811 | int | |
812 | zfs_secpolicy_destroy_perms(const char *name, cred_t *cr) | |
813 | { | |
814 | int error; | |
815 | ||
816 | if ((error = zfs_secpolicy_write_perms(name, | |
817 | ZFS_DELEG_PERM_MOUNT, cr)) != 0) | |
818 | return (error); | |
819 | ||
820 | return (zfs_secpolicy_write_perms(name, ZFS_DELEG_PERM_DESTROY, cr)); | |
821 | } | |
822 | ||
6f1ffb06 | 823 | /* ARGSUSED */ |
34dc7c2f | 824 | static int |
6f1ffb06 | 825 | zfs_secpolicy_destroy(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f BB |
826 | { |
827 | return (zfs_secpolicy_destroy_perms(zc->zc_name, cr)); | |
828 | } | |
829 | ||
830 | /* | |
428870ff | 831 | * Destroying snapshots with delegated permissions requires |
6f1ffb06 | 832 | * descendant mount and destroy permissions. |
34dc7c2f | 833 | */ |
6f1ffb06 | 834 | /* ARGSUSED */ |
34dc7c2f | 835 | static int |
6f1ffb06 | 836 | zfs_secpolicy_destroy_snaps(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f | 837 | { |
6f1ffb06 MA |
838 | nvlist_t *snaps; |
839 | nvpair_t *pair, *nextpair; | |
840 | int error = 0; | |
428870ff | 841 | |
6f1ffb06 | 842 | if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) |
2e528b49 | 843 | return (SET_ERROR(EINVAL)); |
6f1ffb06 MA |
844 | for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; |
845 | pair = nextpair) { | |
6f1ffb06 | 846 | nextpair = nvlist_next_nvpair(snaps, pair); |
da536844 MA |
847 | error = zfs_secpolicy_destroy_perms(nvpair_name(pair), cr); |
848 | if (error == ENOENT) { | |
6f1ffb06 MA |
849 | /* |
850 | * Ignore any snapshots that don't exist (we consider | |
851 | * them "already destroyed"). Remove the name from the | |
852 | * nvl here in case the snapshot is created between | |
853 | * now and when we try to destroy it (in which case | |
854 | * we don't want to destroy it since we haven't | |
855 | * checked for permission). | |
856 | */ | |
857 | fnvlist_remove_nvpair(snaps, pair); | |
858 | error = 0; | |
6f1ffb06 | 859 | } |
6f1ffb06 MA |
860 | if (error != 0) |
861 | break; | |
862 | } | |
428870ff | 863 | |
428870ff | 864 | return (error); |
34dc7c2f BB |
865 | } |
866 | ||
867 | int | |
868 | zfs_secpolicy_rename_perms(const char *from, const char *to, cred_t *cr) | |
869 | { | |
eca7b760 | 870 | char parentname[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f BB |
871 | int error; |
872 | ||
873 | if ((error = zfs_secpolicy_write_perms(from, | |
874 | ZFS_DELEG_PERM_RENAME, cr)) != 0) | |
875 | return (error); | |
876 | ||
877 | if ((error = zfs_secpolicy_write_perms(from, | |
878 | ZFS_DELEG_PERM_MOUNT, cr)) != 0) | |
879 | return (error); | |
880 | ||
881 | if ((error = zfs_get_parent(to, parentname, | |
882 | sizeof (parentname))) != 0) | |
883 | return (error); | |
884 | ||
885 | if ((error = zfs_secpolicy_write_perms(parentname, | |
886 | ZFS_DELEG_PERM_CREATE, cr)) != 0) | |
887 | return (error); | |
888 | ||
889 | if ((error = zfs_secpolicy_write_perms(parentname, | |
890 | ZFS_DELEG_PERM_MOUNT, cr)) != 0) | |
891 | return (error); | |
892 | ||
893 | return (error); | |
894 | } | |
895 | ||
6f1ffb06 | 896 | /* ARGSUSED */ |
34dc7c2f | 897 | static int |
6f1ffb06 | 898 | zfs_secpolicy_rename(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f BB |
899 | { |
900 | return (zfs_secpolicy_rename_perms(zc->zc_name, zc->zc_value, cr)); | |
901 | } | |
902 | ||
6f1ffb06 | 903 | /* ARGSUSED */ |
34dc7c2f | 904 | static int |
6f1ffb06 | 905 | zfs_secpolicy_promote(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f | 906 | { |
13fe0198 MA |
907 | dsl_pool_t *dp; |
908 | dsl_dataset_t *clone; | |
34dc7c2f BB |
909 | int error; |
910 | ||
911 | error = zfs_secpolicy_write_perms(zc->zc_name, | |
912 | ZFS_DELEG_PERM_PROMOTE, cr); | |
13fe0198 MA |
913 | if (error != 0) |
914 | return (error); | |
915 | ||
916 | error = dsl_pool_hold(zc->zc_name, FTAG, &dp); | |
917 | if (error != 0) | |
34dc7c2f BB |
918 | return (error); |
919 | ||
13fe0198 | 920 | error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &clone); |
34dc7c2f BB |
921 | |
922 | if (error == 0) { | |
eca7b760 | 923 | char parentname[ZFS_MAX_DATASET_NAME_LEN]; |
13fe0198 | 924 | dsl_dataset_t *origin = NULL; |
34dc7c2f | 925 | dsl_dir_t *dd; |
13fe0198 | 926 | dd = clone->ds_dir; |
34dc7c2f | 927 | |
b128c09f | 928 | error = dsl_dataset_hold_obj(dd->dd_pool, |
d683ddbb | 929 | dsl_dir_phys(dd)->dd_origin_obj, FTAG, &origin); |
13fe0198 MA |
930 | if (error != 0) { |
931 | dsl_dataset_rele(clone, FTAG); | |
932 | dsl_pool_rele(dp, FTAG); | |
34dc7c2f BB |
933 | return (error); |
934 | } | |
935 | ||
13fe0198 | 936 | error = zfs_secpolicy_write_perms_ds(zc->zc_name, clone, |
34dc7c2f BB |
937 | ZFS_DELEG_PERM_MOUNT, cr); |
938 | ||
13fe0198 MA |
939 | dsl_dataset_name(origin, parentname); |
940 | if (error == 0) { | |
941 | error = zfs_secpolicy_write_perms_ds(parentname, origin, | |
34dc7c2f | 942 | ZFS_DELEG_PERM_PROMOTE, cr); |
13fe0198 MA |
943 | } |
944 | dsl_dataset_rele(clone, FTAG); | |
945 | dsl_dataset_rele(origin, FTAG); | |
34dc7c2f | 946 | } |
13fe0198 | 947 | dsl_pool_rele(dp, FTAG); |
34dc7c2f BB |
948 | return (error); |
949 | } | |
950 | ||
6f1ffb06 | 951 | /* ARGSUSED */ |
34dc7c2f | 952 | static int |
6f1ffb06 | 953 | zfs_secpolicy_recv(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f BB |
954 | { |
955 | int error; | |
956 | ||
957 | if ((error = zfs_secpolicy_write_perms(zc->zc_name, | |
958 | ZFS_DELEG_PERM_RECEIVE, cr)) != 0) | |
959 | return (error); | |
960 | ||
961 | if ((error = zfs_secpolicy_write_perms(zc->zc_name, | |
962 | ZFS_DELEG_PERM_MOUNT, cr)) != 0) | |
963 | return (error); | |
964 | ||
965 | return (zfs_secpolicy_write_perms(zc->zc_name, | |
966 | ZFS_DELEG_PERM_CREATE, cr)); | |
967 | } | |
968 | ||
43e52edd BB |
969 | /* ARGSUSED */ |
970 | static int | |
971 | zfs_secpolicy_recv_new(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
972 | { | |
973 | return (zfs_secpolicy_recv(zc, innvl, cr)); | |
974 | } | |
975 | ||
34dc7c2f BB |
976 | int |
977 | zfs_secpolicy_snapshot_perms(const char *name, cred_t *cr) | |
978 | { | |
428870ff BB |
979 | return (zfs_secpolicy_write_perms(name, |
980 | ZFS_DELEG_PERM_SNAPSHOT, cr)); | |
34dc7c2f BB |
981 | } |
982 | ||
6f1ffb06 MA |
983 | /* |
984 | * Check for permission to create each snapshot in the nvlist. | |
985 | */ | |
986 | /* ARGSUSED */ | |
34dc7c2f | 987 | static int |
6f1ffb06 | 988 | zfs_secpolicy_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f | 989 | { |
6f1ffb06 MA |
990 | nvlist_t *snaps; |
991 | int error = 0; | |
992 | nvpair_t *pair; | |
993 | ||
994 | if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) | |
2e528b49 | 995 | return (SET_ERROR(EINVAL)); |
6f1ffb06 MA |
996 | for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; |
997 | pair = nvlist_next_nvpair(snaps, pair)) { | |
998 | char *name = nvpair_name(pair); | |
999 | char *atp = strchr(name, '@'); | |
34dc7c2f | 1000 | |
6f1ffb06 | 1001 | if (atp == NULL) { |
2e528b49 | 1002 | error = SET_ERROR(EINVAL); |
6f1ffb06 MA |
1003 | break; |
1004 | } | |
1005 | *atp = '\0'; | |
1006 | error = zfs_secpolicy_snapshot_perms(name, cr); | |
1007 | *atp = '@'; | |
1008 | if (error != 0) | |
1009 | break; | |
1010 | } | |
1011 | return (error); | |
1012 | } | |
1013 | ||
da536844 MA |
1014 | /* |
1015 | * Check for permission to create each snapshot in the nvlist. | |
1016 | */ | |
1017 | /* ARGSUSED */ | |
1018 | static int | |
1019 | zfs_secpolicy_bookmark(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
1020 | { | |
1021 | int error = 0; | |
1022 | nvpair_t *pair; | |
1023 | ||
1024 | for (pair = nvlist_next_nvpair(innvl, NULL); | |
1025 | pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { | |
1026 | char *name = nvpair_name(pair); | |
1027 | char *hashp = strchr(name, '#'); | |
1028 | ||
1029 | if (hashp == NULL) { | |
1030 | error = SET_ERROR(EINVAL); | |
1031 | break; | |
1032 | } | |
1033 | *hashp = '\0'; | |
1034 | error = zfs_secpolicy_write_perms(name, | |
1035 | ZFS_DELEG_PERM_BOOKMARK, cr); | |
1036 | *hashp = '#'; | |
1037 | if (error != 0) | |
1038 | break; | |
1039 | } | |
1040 | return (error); | |
1041 | } | |
1042 | ||
1043 | /* ARGSUSED */ | |
1044 | static int | |
1045 | zfs_secpolicy_destroy_bookmarks(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
1046 | { | |
1047 | nvpair_t *pair, *nextpair; | |
1048 | int error = 0; | |
1049 | ||
1050 | for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; | |
1051 | pair = nextpair) { | |
1052 | char *name = nvpair_name(pair); | |
1053 | char *hashp = strchr(name, '#'); | |
1054 | nextpair = nvlist_next_nvpair(innvl, pair); | |
1055 | ||
1056 | if (hashp == NULL) { | |
1057 | error = SET_ERROR(EINVAL); | |
1058 | break; | |
1059 | } | |
1060 | ||
1061 | *hashp = '\0'; | |
1062 | error = zfs_secpolicy_write_perms(name, | |
1063 | ZFS_DELEG_PERM_DESTROY, cr); | |
1064 | *hashp = '#'; | |
1065 | if (error == ENOENT) { | |
1066 | /* | |
1067 | * Ignore any filesystems that don't exist (we consider | |
1068 | * their bookmarks "already destroyed"). Remove | |
1069 | * the name from the nvl here in case the filesystem | |
1070 | * is created between now and when we try to destroy | |
1071 | * the bookmark (in which case we don't want to | |
1072 | * destroy it since we haven't checked for permission). | |
1073 | */ | |
1074 | fnvlist_remove_nvpair(innvl, pair); | |
1075 | error = 0; | |
1076 | } | |
1077 | if (error != 0) | |
1078 | break; | |
1079 | } | |
1080 | ||
1081 | return (error); | |
1082 | } | |
1083 | ||
6f1ffb06 MA |
1084 | /* ARGSUSED */ |
1085 | static int | |
1086 | zfs_secpolicy_log_history(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
1087 | { | |
1088 | /* | |
1089 | * Even root must have a proper TSD so that we know what pool | |
1090 | * to log to. | |
1091 | */ | |
1092 | if (tsd_get(zfs_allow_log_key) == NULL) | |
2e528b49 | 1093 | return (SET_ERROR(EPERM)); |
6f1ffb06 | 1094 | return (0); |
34dc7c2f BB |
1095 | } |
1096 | ||
1097 | static int | |
6f1ffb06 | 1098 | zfs_secpolicy_create_clone(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f | 1099 | { |
eca7b760 | 1100 | char parentname[ZFS_MAX_DATASET_NAME_LEN]; |
428870ff | 1101 | int error; |
6f1ffb06 | 1102 | char *origin; |
34dc7c2f BB |
1103 | |
1104 | if ((error = zfs_get_parent(zc->zc_name, parentname, | |
1105 | sizeof (parentname))) != 0) | |
1106 | return (error); | |
1107 | ||
6f1ffb06 MA |
1108 | if (nvlist_lookup_string(innvl, "origin", &origin) == 0 && |
1109 | (error = zfs_secpolicy_write_perms(origin, | |
1110 | ZFS_DELEG_PERM_CLONE, cr)) != 0) | |
1111 | return (error); | |
34dc7c2f BB |
1112 | |
1113 | if ((error = zfs_secpolicy_write_perms(parentname, | |
1114 | ZFS_DELEG_PERM_CREATE, cr)) != 0) | |
1115 | return (error); | |
1116 | ||
6f1ffb06 MA |
1117 | return (zfs_secpolicy_write_perms(parentname, |
1118 | ZFS_DELEG_PERM_MOUNT, cr)); | |
34dc7c2f BB |
1119 | } |
1120 | ||
34dc7c2f BB |
1121 | /* |
1122 | * Policy for pool operations - create/destroy pools, add vdevs, etc. Requires | |
1123 | * SYS_CONFIG privilege, which is not available in a local zone. | |
1124 | */ | |
1125 | /* ARGSUSED */ | |
1126 | static int | |
6f1ffb06 | 1127 | zfs_secpolicy_config(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f BB |
1128 | { |
1129 | if (secpolicy_sys_config(cr, B_FALSE) != 0) | |
2e528b49 | 1130 | return (SET_ERROR(EPERM)); |
34dc7c2f BB |
1131 | |
1132 | return (0); | |
1133 | } | |
1134 | ||
572e2857 BB |
1135 | /* |
1136 | * Policy for object to name lookups. | |
1137 | */ | |
1138 | /* ARGSUSED */ | |
1139 | static int | |
6f1ffb06 | 1140 | zfs_secpolicy_diff(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
572e2857 BB |
1141 | { |
1142 | int error; | |
1143 | ||
1144 | if ((error = secpolicy_sys_config(cr, B_FALSE)) == 0) | |
1145 | return (0); | |
1146 | ||
1147 | error = zfs_secpolicy_write_perms(zc->zc_name, ZFS_DELEG_PERM_DIFF, cr); | |
1148 | return (error); | |
1149 | } | |
1150 | ||
34dc7c2f BB |
1151 | /* |
1152 | * Policy for fault injection. Requires all privileges. | |
1153 | */ | |
1154 | /* ARGSUSED */ | |
1155 | static int | |
6f1ffb06 | 1156 | zfs_secpolicy_inject(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f BB |
1157 | { |
1158 | return (secpolicy_zinject(cr)); | |
1159 | } | |
1160 | ||
6f1ffb06 | 1161 | /* ARGSUSED */ |
34dc7c2f | 1162 | static int |
6f1ffb06 | 1163 | zfs_secpolicy_inherit_prop(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
34dc7c2f BB |
1164 | { |
1165 | zfs_prop_t prop = zfs_name_to_prop(zc->zc_value); | |
1166 | ||
1167 | if (prop == ZPROP_INVAL) { | |
1168 | if (!zfs_prop_user(zc->zc_value)) | |
2e528b49 | 1169 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1170 | return (zfs_secpolicy_write_perms(zc->zc_name, |
1171 | ZFS_DELEG_PERM_USERPROP, cr)); | |
1172 | } else { | |
428870ff BB |
1173 | return (zfs_secpolicy_setprop(zc->zc_name, prop, |
1174 | NULL, cr)); | |
34dc7c2f BB |
1175 | } |
1176 | } | |
1177 | ||
9babb374 | 1178 | static int |
6f1ffb06 | 1179 | zfs_secpolicy_userspace_one(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
9babb374 | 1180 | { |
6f1ffb06 | 1181 | int err = zfs_secpolicy_read(zc, innvl, cr); |
9babb374 BB |
1182 | if (err) |
1183 | return (err); | |
1184 | ||
1185 | if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) | |
2e528b49 | 1186 | return (SET_ERROR(EINVAL)); |
9babb374 BB |
1187 | |
1188 | if (zc->zc_value[0] == 0) { | |
1189 | /* | |
1190 | * They are asking about a posix uid/gid. If it's | |
1191 | * themself, allow it. | |
1192 | */ | |
1193 | if (zc->zc_objset_type == ZFS_PROP_USERUSED || | |
1de321e6 JX |
1194 | zc->zc_objset_type == ZFS_PROP_USERQUOTA || |
1195 | zc->zc_objset_type == ZFS_PROP_USEROBJUSED || | |
1196 | zc->zc_objset_type == ZFS_PROP_USEROBJQUOTA) { | |
9babb374 BB |
1197 | if (zc->zc_guid == crgetuid(cr)) |
1198 | return (0); | |
1199 | } else { | |
1200 | if (groupmember(zc->zc_guid, cr)) | |
1201 | return (0); | |
1202 | } | |
1203 | } | |
1204 | ||
1205 | return (zfs_secpolicy_write_perms(zc->zc_name, | |
1206 | userquota_perms[zc->zc_objset_type], cr)); | |
1207 | } | |
1208 | ||
1209 | static int | |
6f1ffb06 | 1210 | zfs_secpolicy_userspace_many(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
9babb374 | 1211 | { |
6f1ffb06 | 1212 | int err = zfs_secpolicy_read(zc, innvl, cr); |
9babb374 BB |
1213 | if (err) |
1214 | return (err); | |
1215 | ||
1216 | if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) | |
2e528b49 | 1217 | return (SET_ERROR(EINVAL)); |
9babb374 BB |
1218 | |
1219 | return (zfs_secpolicy_write_perms(zc->zc_name, | |
1220 | userquota_perms[zc->zc_objset_type], cr)); | |
1221 | } | |
1222 | ||
6f1ffb06 | 1223 | /* ARGSUSED */ |
9babb374 | 1224 | static int |
6f1ffb06 | 1225 | zfs_secpolicy_userspace_upgrade(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
9babb374 | 1226 | { |
428870ff BB |
1227 | return (zfs_secpolicy_setprop(zc->zc_name, ZFS_PROP_VERSION, |
1228 | NULL, cr)); | |
9babb374 BB |
1229 | } |
1230 | ||
6f1ffb06 | 1231 | /* ARGSUSED */ |
45d1cae3 | 1232 | static int |
6f1ffb06 | 1233 | zfs_secpolicy_hold(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
45d1cae3 | 1234 | { |
13fe0198 MA |
1235 | nvpair_t *pair; |
1236 | nvlist_t *holds; | |
1237 | int error; | |
1238 | ||
1239 | error = nvlist_lookup_nvlist(innvl, "holds", &holds); | |
1240 | if (error != 0) | |
2e528b49 | 1241 | return (SET_ERROR(EINVAL)); |
13fe0198 MA |
1242 | |
1243 | for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; | |
1244 | pair = nvlist_next_nvpair(holds, pair)) { | |
eca7b760 | 1245 | char fsname[ZFS_MAX_DATASET_NAME_LEN]; |
13fe0198 MA |
1246 | error = dmu_fsname(nvpair_name(pair), fsname); |
1247 | if (error != 0) | |
1248 | return (error); | |
1249 | error = zfs_secpolicy_write_perms(fsname, | |
1250 | ZFS_DELEG_PERM_HOLD, cr); | |
1251 | if (error != 0) | |
1252 | return (error); | |
1253 | } | |
1254 | return (0); | |
45d1cae3 BB |
1255 | } |
1256 | ||
6f1ffb06 | 1257 | /* ARGSUSED */ |
45d1cae3 | 1258 | static int |
6f1ffb06 | 1259 | zfs_secpolicy_release(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
45d1cae3 | 1260 | { |
13fe0198 MA |
1261 | nvpair_t *pair; |
1262 | int error; | |
1263 | ||
1264 | for (pair = nvlist_next_nvpair(innvl, NULL); pair != NULL; | |
1265 | pair = nvlist_next_nvpair(innvl, pair)) { | |
eca7b760 | 1266 | char fsname[ZFS_MAX_DATASET_NAME_LEN]; |
13fe0198 MA |
1267 | error = dmu_fsname(nvpair_name(pair), fsname); |
1268 | if (error != 0) | |
1269 | return (error); | |
1270 | error = zfs_secpolicy_write_perms(fsname, | |
1271 | ZFS_DELEG_PERM_RELEASE, cr); | |
1272 | if (error != 0) | |
1273 | return (error); | |
1274 | } | |
1275 | return (0); | |
45d1cae3 BB |
1276 | } |
1277 | ||
572e2857 BB |
1278 | /* |
1279 | * Policy for allowing temporary snapshots to be taken or released | |
1280 | */ | |
1281 | static int | |
6f1ffb06 | 1282 | zfs_secpolicy_tmp_snapshot(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) |
572e2857 BB |
1283 | { |
1284 | /* | |
1285 | * A temporary snapshot is the same as a snapshot, | |
1286 | * hold, destroy and release all rolled into one. | |
1287 | * Delegated diff alone is sufficient that we allow this. | |
1288 | */ | |
1289 | int error; | |
1290 | ||
1291 | if ((error = zfs_secpolicy_write_perms(zc->zc_name, | |
1292 | ZFS_DELEG_PERM_DIFF, cr)) == 0) | |
1293 | return (0); | |
1294 | ||
6f1ffb06 | 1295 | error = zfs_secpolicy_snapshot_perms(zc->zc_name, cr); |
13fe0198 | 1296 | if (error == 0) |
6f1ffb06 | 1297 | error = zfs_secpolicy_hold(zc, innvl, cr); |
13fe0198 | 1298 | if (error == 0) |
6f1ffb06 | 1299 | error = zfs_secpolicy_release(zc, innvl, cr); |
13fe0198 | 1300 | if (error == 0) |
6f1ffb06 | 1301 | error = zfs_secpolicy_destroy(zc, innvl, cr); |
572e2857 BB |
1302 | return (error); |
1303 | } | |
1304 | ||
b5256303 TC |
1305 | static int |
1306 | zfs_secpolicy_load_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
1307 | { | |
1308 | return (zfs_secpolicy_write_perms(zc->zc_name, | |
1309 | ZFS_DELEG_PERM_LOAD_KEY, cr)); | |
1310 | } | |
1311 | ||
1312 | static int | |
1313 | zfs_secpolicy_change_key(zfs_cmd_t *zc, nvlist_t *innvl, cred_t *cr) | |
1314 | { | |
1315 | return (zfs_secpolicy_write_perms(zc->zc_name, | |
1316 | ZFS_DELEG_PERM_CHANGE_KEY, cr)); | |
1317 | } | |
1318 | ||
34dc7c2f BB |
1319 | /* |
1320 | * Returns the nvlist as specified by the user in the zfs_cmd_t. | |
1321 | */ | |
1322 | static int | |
9babb374 | 1323 | get_nvlist(uint64_t nvl, uint64_t size, int iflag, nvlist_t **nvp) |
34dc7c2f BB |
1324 | { |
1325 | char *packed; | |
1326 | int error; | |
1327 | nvlist_t *list = NULL; | |
1328 | ||
1329 | /* | |
1330 | * Read in and unpack the user-supplied nvlist. | |
1331 | */ | |
1332 | if (size == 0) | |
2e528b49 | 1333 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 1334 | |
77aef6f6 | 1335 | packed = vmem_alloc(size, KM_SLEEP); |
34dc7c2f | 1336 | |
9babb374 BB |
1337 | if ((error = ddi_copyin((void *)(uintptr_t)nvl, packed, size, |
1338 | iflag)) != 0) { | |
77aef6f6 | 1339 | vmem_free(packed, size); |
0de7c552 | 1340 | return (SET_ERROR(EFAULT)); |
34dc7c2f BB |
1341 | } |
1342 | ||
1343 | if ((error = nvlist_unpack(packed, size, &list, 0)) != 0) { | |
77aef6f6 | 1344 | vmem_free(packed, size); |
34dc7c2f BB |
1345 | return (error); |
1346 | } | |
1347 | ||
77aef6f6 | 1348 | vmem_free(packed, size); |
34dc7c2f BB |
1349 | |
1350 | *nvp = list; | |
1351 | return (0); | |
1352 | } | |
1353 | ||
6f1ffb06 MA |
1354 | /* |
1355 | * Reduce the size of this nvlist until it can be serialized in 'max' bytes. | |
1356 | * Entries will be removed from the end of the nvlist, and one int32 entry | |
1357 | * named "N_MORE_ERRORS" will be added indicating how many entries were | |
1358 | * removed. | |
1359 | */ | |
428870ff | 1360 | static int |
6f1ffb06 | 1361 | nvlist_smush(nvlist_t *errors, size_t max) |
428870ff BB |
1362 | { |
1363 | size_t size; | |
1364 | ||
6f1ffb06 | 1365 | size = fnvlist_size(errors); |
428870ff | 1366 | |
6f1ffb06 | 1367 | if (size > max) { |
428870ff BB |
1368 | nvpair_t *more_errors; |
1369 | int n = 0; | |
1370 | ||
6f1ffb06 | 1371 | if (max < 1024) |
2e528b49 | 1372 | return (SET_ERROR(ENOMEM)); |
428870ff | 1373 | |
6f1ffb06 MA |
1374 | fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, 0); |
1375 | more_errors = nvlist_prev_nvpair(errors, NULL); | |
428870ff BB |
1376 | |
1377 | do { | |
6f1ffb06 | 1378 | nvpair_t *pair = nvlist_prev_nvpair(errors, |
428870ff | 1379 | more_errors); |
6f1ffb06 | 1380 | fnvlist_remove_nvpair(errors, pair); |
428870ff | 1381 | n++; |
6f1ffb06 MA |
1382 | size = fnvlist_size(errors); |
1383 | } while (size > max); | |
428870ff | 1384 | |
6f1ffb06 MA |
1385 | fnvlist_remove_nvpair(errors, more_errors); |
1386 | fnvlist_add_int32(errors, ZPROP_N_MORE_ERRORS, n); | |
1387 | ASSERT3U(fnvlist_size(errors), <=, max); | |
428870ff BB |
1388 | } |
1389 | ||
1390 | return (0); | |
1391 | } | |
1392 | ||
34dc7c2f BB |
1393 | static int |
1394 | put_nvlist(zfs_cmd_t *zc, nvlist_t *nvl) | |
1395 | { | |
1396 | char *packed = NULL; | |
428870ff | 1397 | int error = 0; |
34dc7c2f | 1398 | size_t size; |
34dc7c2f | 1399 | |
6f1ffb06 | 1400 | size = fnvlist_size(nvl); |
34dc7c2f BB |
1401 | |
1402 | if (size > zc->zc_nvlist_dst_size) { | |
2e528b49 | 1403 | error = SET_ERROR(ENOMEM); |
34dc7c2f | 1404 | } else { |
6f1ffb06 | 1405 | packed = fnvlist_pack(nvl, &size); |
428870ff BB |
1406 | if (ddi_copyout(packed, (void *)(uintptr_t)zc->zc_nvlist_dst, |
1407 | size, zc->zc_iflags) != 0) | |
2e528b49 | 1408 | error = SET_ERROR(EFAULT); |
6f1ffb06 | 1409 | fnvlist_pack_free(packed, size); |
34dc7c2f BB |
1410 | } |
1411 | ||
1412 | zc->zc_nvlist_dst_size = size; | |
6f1ffb06 | 1413 | zc->zc_nvlist_dst_filled = B_TRUE; |
34dc7c2f BB |
1414 | return (error); |
1415 | } | |
1416 | ||
9babb374 | 1417 | static int |
f298b24d | 1418 | getzfsvfs(const char *dsname, zfsvfs_t **zfvp) |
9babb374 BB |
1419 | { |
1420 | objset_t *os; | |
1421 | int error; | |
1422 | ||
428870ff | 1423 | error = dmu_objset_hold(dsname, FTAG, &os); |
13fe0198 | 1424 | if (error != 0) |
9babb374 | 1425 | return (error); |
428870ff BB |
1426 | if (dmu_objset_type(os) != DMU_OST_ZFS) { |
1427 | dmu_objset_rele(os, FTAG); | |
2e528b49 | 1428 | return (SET_ERROR(EINVAL)); |
428870ff | 1429 | } |
9babb374 | 1430 | |
428870ff | 1431 | mutex_enter(&os->os_user_ptr_lock); |
0037b49e | 1432 | *zfvp = dmu_objset_get_user(os); |
061460df | 1433 | /* bump s_active only when non-zero to prevent umount race */ |
0037b49e BB |
1434 | if (*zfvp == NULL || (*zfvp)->z_sb == NULL || |
1435 | !atomic_inc_not_zero(&((*zfvp)->z_sb->s_active))) { | |
2e528b49 | 1436 | error = SET_ERROR(ESRCH); |
9babb374 | 1437 | } |
428870ff BB |
1438 | mutex_exit(&os->os_user_ptr_lock); |
1439 | dmu_objset_rele(os, FTAG); | |
9babb374 BB |
1440 | return (error); |
1441 | } | |
1442 | ||
1443 | /* | |
0037b49e | 1444 | * Find a zfsvfs_t for a mounted filesystem, or create our own, in which |
2cf7f52b | 1445 | * case its z_sb will be NULL, and it will be opened as the owner. |
9ae529ec CS |
1446 | * If 'writer' is set, the z_teardown_lock will be held for RW_WRITER, |
1447 | * which prevents all inode ops from running. | |
9babb374 BB |
1448 | */ |
1449 | static int | |
f298b24d | 1450 | zfsvfs_hold(const char *name, void *tag, zfsvfs_t **zfvp, boolean_t writer) |
9babb374 BB |
1451 | { |
1452 | int error = 0; | |
9babb374 | 1453 | |
f298b24d | 1454 | if (getzfsvfs(name, zfvp) != 0) |
1c2555ef | 1455 | error = zfsvfs_create(name, zfvp); |
9babb374 | 1456 | if (error == 0) { |
0037b49e | 1457 | rrm_enter(&(*zfvp)->z_teardown_lock, (writer) ? RW_WRITER : |
572e2857 | 1458 | RW_READER, tag); |
0037b49e | 1459 | if ((*zfvp)->z_unmounted) { |
9babb374 BB |
1460 | /* |
1461 | * XXX we could probably try again, since the unmounting | |
1462 | * thread should be just about to disassociate the | |
0037b49e | 1463 | * objset from the zfsvfs. |
9babb374 | 1464 | */ |
0037b49e | 1465 | rrm_exit(&(*zfvp)->z_teardown_lock, tag); |
2e528b49 | 1466 | return (SET_ERROR(EBUSY)); |
9babb374 BB |
1467 | } |
1468 | } | |
1469 | return (error); | |
1470 | } | |
1471 | ||
1472 | static void | |
f298b24d | 1473 | zfsvfs_rele(zfsvfs_t *zfsvfs, void *tag) |
9babb374 | 1474 | { |
0037b49e | 1475 | rrm_exit(&zfsvfs->z_teardown_lock, tag); |
9babb374 | 1476 | |
0037b49e BB |
1477 | if (zfsvfs->z_sb) { |
1478 | deactivate_super(zfsvfs->z_sb); | |
9babb374 | 1479 | } else { |
b5256303 | 1480 | dmu_objset_disown(zfsvfs->z_os, B_TRUE, zfsvfs); |
f298b24d | 1481 | zfsvfs_free(zfsvfs); |
9babb374 BB |
1482 | } |
1483 | } | |
1484 | ||
34dc7c2f BB |
1485 | static int |
1486 | zfs_ioc_pool_create(zfs_cmd_t *zc) | |
1487 | { | |
1488 | int error; | |
1489 | nvlist_t *config, *props = NULL; | |
b128c09f BB |
1490 | nvlist_t *rootprops = NULL; |
1491 | nvlist_t *zplprops = NULL; | |
b5256303 | 1492 | dsl_crypto_params_t *dcp = NULL; |
34dc7c2f | 1493 | |
c65aa5b2 BB |
1494 | if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, |
1495 | zc->zc_iflags, &config))) | |
34dc7c2f BB |
1496 | return (error); |
1497 | ||
1498 | if (zc->zc_nvlist_src_size != 0 && (error = | |
9babb374 BB |
1499 | get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, |
1500 | zc->zc_iflags, &props))) { | |
34dc7c2f BB |
1501 | nvlist_free(config); |
1502 | return (error); | |
1503 | } | |
1504 | ||
b128c09f BB |
1505 | if (props) { |
1506 | nvlist_t *nvl = NULL; | |
b5256303 | 1507 | nvlist_t *hidden_args = NULL; |
b128c09f BB |
1508 | uint64_t version = SPA_VERSION; |
1509 | ||
1510 | (void) nvlist_lookup_uint64(props, | |
1511 | zpool_prop_to_name(ZPOOL_PROP_VERSION), &version); | |
9ae529ec | 1512 | if (!SPA_VERSION_IS_SUPPORTED(version)) { |
2e528b49 | 1513 | error = SET_ERROR(EINVAL); |
b128c09f BB |
1514 | goto pool_props_bad; |
1515 | } | |
1516 | (void) nvlist_lookup_nvlist(props, ZPOOL_ROOTFS_PROPS, &nvl); | |
1517 | if (nvl) { | |
1518 | error = nvlist_dup(nvl, &rootprops, KM_SLEEP); | |
1519 | if (error != 0) { | |
1520 | nvlist_free(config); | |
1521 | nvlist_free(props); | |
1522 | return (error); | |
1523 | } | |
1524 | (void) nvlist_remove_all(props, ZPOOL_ROOTFS_PROPS); | |
1525 | } | |
b5256303 TC |
1526 | |
1527 | (void) nvlist_lookup_nvlist(props, ZPOOL_HIDDEN_ARGS, | |
1528 | &hidden_args); | |
1529 | error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, | |
1530 | rootprops, hidden_args, &dcp); | |
1531 | if (error != 0) { | |
1532 | nvlist_free(config); | |
1533 | nvlist_free(props); | |
1534 | return (error); | |
1535 | } | |
1536 | (void) nvlist_remove_all(props, ZPOOL_HIDDEN_ARGS); | |
1537 | ||
b128c09f BB |
1538 | VERIFY(nvlist_alloc(&zplprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); |
1539 | error = zfs_fill_zplprops_root(version, rootprops, | |
1540 | zplprops, NULL); | |
13fe0198 | 1541 | if (error != 0) |
b128c09f BB |
1542 | goto pool_props_bad; |
1543 | } | |
1544 | ||
b5256303 | 1545 | error = spa_create(zc->zc_name, config, props, zplprops, dcp); |
b128c09f BB |
1546 | |
1547 | /* | |
1548 | * Set the remaining root properties | |
1549 | */ | |
428870ff BB |
1550 | if (!error && (error = zfs_set_prop_nvlist(zc->zc_name, |
1551 | ZPROP_SRC_LOCAL, rootprops, NULL)) != 0) | |
b128c09f | 1552 | (void) spa_destroy(zc->zc_name); |
34dc7c2f | 1553 | |
b128c09f BB |
1554 | pool_props_bad: |
1555 | nvlist_free(rootprops); | |
1556 | nvlist_free(zplprops); | |
34dc7c2f | 1557 | nvlist_free(config); |
b128c09f | 1558 | nvlist_free(props); |
b5256303 | 1559 | dsl_crypto_params_free(dcp, !!error); |
34dc7c2f BB |
1560 | |
1561 | return (error); | |
1562 | } | |
1563 | ||
1564 | static int | |
1565 | zfs_ioc_pool_destroy(zfs_cmd_t *zc) | |
1566 | { | |
1567 | int error; | |
1568 | zfs_log_history(zc); | |
1569 | error = spa_destroy(zc->zc_name); | |
a0bd735a | 1570 | |
34dc7c2f BB |
1571 | return (error); |
1572 | } | |
1573 | ||
1574 | static int | |
1575 | zfs_ioc_pool_import(zfs_cmd_t *zc) | |
1576 | { | |
34dc7c2f BB |
1577 | nvlist_t *config, *props = NULL; |
1578 | uint64_t guid; | |
428870ff | 1579 | int error; |
34dc7c2f BB |
1580 | |
1581 | if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, | |
9babb374 | 1582 | zc->zc_iflags, &config)) != 0) |
34dc7c2f BB |
1583 | return (error); |
1584 | ||
1585 | if (zc->zc_nvlist_src_size != 0 && (error = | |
9babb374 BB |
1586 | get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, |
1587 | zc->zc_iflags, &props))) { | |
34dc7c2f BB |
1588 | nvlist_free(config); |
1589 | return (error); | |
1590 | } | |
1591 | ||
1592 | if (nvlist_lookup_uint64(config, ZPOOL_CONFIG_POOL_GUID, &guid) != 0 || | |
1593 | guid != zc->zc_guid) | |
2e528b49 | 1594 | error = SET_ERROR(EINVAL); |
34dc7c2f | 1595 | else |
572e2857 | 1596 | error = spa_import(zc->zc_name, config, props, zc->zc_cookie); |
34dc7c2f | 1597 | |
572e2857 BB |
1598 | if (zc->zc_nvlist_dst != 0) { |
1599 | int err; | |
1600 | ||
1601 | if ((err = put_nvlist(zc, config)) != 0) | |
1602 | error = err; | |
1603 | } | |
428870ff | 1604 | |
34dc7c2f | 1605 | nvlist_free(config); |
8a5fc748 | 1606 | nvlist_free(props); |
34dc7c2f BB |
1607 | |
1608 | return (error); | |
1609 | } | |
1610 | ||
1611 | static int | |
1612 | zfs_ioc_pool_export(zfs_cmd_t *zc) | |
1613 | { | |
1614 | int error; | |
b128c09f | 1615 | boolean_t force = (boolean_t)zc->zc_cookie; |
fb5f0bc8 | 1616 | boolean_t hardforce = (boolean_t)zc->zc_guid; |
b128c09f | 1617 | |
34dc7c2f | 1618 | zfs_log_history(zc); |
fb5f0bc8 | 1619 | error = spa_export(zc->zc_name, NULL, force, hardforce); |
a0bd735a | 1620 | |
34dc7c2f BB |
1621 | return (error); |
1622 | } | |
1623 | ||
1624 | static int | |
1625 | zfs_ioc_pool_configs(zfs_cmd_t *zc) | |
1626 | { | |
1627 | nvlist_t *configs; | |
1628 | int error; | |
1629 | ||
1630 | if ((configs = spa_all_configs(&zc->zc_cookie)) == NULL) | |
2e528b49 | 1631 | return (SET_ERROR(EEXIST)); |
34dc7c2f BB |
1632 | |
1633 | error = put_nvlist(zc, configs); | |
1634 | ||
1635 | nvlist_free(configs); | |
1636 | ||
1637 | return (error); | |
1638 | } | |
1639 | ||
9ae529ec CS |
1640 | /* |
1641 | * inputs: | |
1642 | * zc_name name of the pool | |
1643 | * | |
1644 | * outputs: | |
1645 | * zc_cookie real errno | |
1646 | * zc_nvlist_dst config nvlist | |
1647 | * zc_nvlist_dst_size size of config nvlist | |
1648 | */ | |
34dc7c2f BB |
1649 | static int |
1650 | zfs_ioc_pool_stats(zfs_cmd_t *zc) | |
1651 | { | |
1652 | nvlist_t *config; | |
1653 | int error; | |
1654 | int ret = 0; | |
1655 | ||
1656 | error = spa_get_stats(zc->zc_name, &config, zc->zc_value, | |
1657 | sizeof (zc->zc_value)); | |
1658 | ||
1659 | if (config != NULL) { | |
1660 | ret = put_nvlist(zc, config); | |
1661 | nvlist_free(config); | |
1662 | ||
1663 | /* | |
1664 | * The config may be present even if 'error' is non-zero. | |
1665 | * In this case we return success, and preserve the real errno | |
1666 | * in 'zc_cookie'. | |
1667 | */ | |
1668 | zc->zc_cookie = error; | |
1669 | } else { | |
1670 | ret = error; | |
1671 | } | |
1672 | ||
1673 | return (ret); | |
1674 | } | |
1675 | ||
1676 | /* | |
1677 | * Try to import the given pool, returning pool stats as appropriate so that | |
1678 | * user land knows which devices are available and overall pool health. | |
1679 | */ | |
1680 | static int | |
1681 | zfs_ioc_pool_tryimport(zfs_cmd_t *zc) | |
1682 | { | |
379ca9cf | 1683 | nvlist_t *tryconfig, *config = NULL; |
34dc7c2f BB |
1684 | int error; |
1685 | ||
1686 | if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, | |
9babb374 | 1687 | zc->zc_iflags, &tryconfig)) != 0) |
34dc7c2f BB |
1688 | return (error); |
1689 | ||
1690 | config = spa_tryimport(tryconfig); | |
1691 | ||
1692 | nvlist_free(tryconfig); | |
1693 | ||
1694 | if (config == NULL) | |
2e528b49 | 1695 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1696 | |
1697 | error = put_nvlist(zc, config); | |
1698 | nvlist_free(config); | |
1699 | ||
1700 | return (error); | |
1701 | } | |
1702 | ||
428870ff BB |
1703 | /* |
1704 | * inputs: | |
1705 | * zc_name name of the pool | |
1706 | * zc_cookie scan func (pool_scan_func_t) | |
0ea05c64 | 1707 | * zc_flags scrub pause/resume flag (pool_scrub_cmd_t) |
428870ff | 1708 | */ |
34dc7c2f | 1709 | static int |
428870ff | 1710 | zfs_ioc_pool_scan(zfs_cmd_t *zc) |
34dc7c2f BB |
1711 | { |
1712 | spa_t *spa; | |
1713 | int error; | |
1714 | ||
1715 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) | |
1716 | return (error); | |
1717 | ||
0ea05c64 AP |
1718 | if (zc->zc_flags >= POOL_SCRUB_FLAGS_END) |
1719 | return (SET_ERROR(EINVAL)); | |
1720 | ||
1721 | if (zc->zc_flags == POOL_SCRUB_PAUSE) | |
1722 | error = spa_scrub_pause_resume(spa, POOL_SCRUB_PAUSE); | |
1723 | else if (zc->zc_cookie == POOL_SCAN_NONE) | |
428870ff BB |
1724 | error = spa_scan_stop(spa); |
1725 | else | |
1726 | error = spa_scan(spa, zc->zc_cookie); | |
34dc7c2f BB |
1727 | |
1728 | spa_close(spa, FTAG); | |
1729 | ||
1730 | return (error); | |
1731 | } | |
1732 | ||
1733 | static int | |
1734 | zfs_ioc_pool_freeze(zfs_cmd_t *zc) | |
1735 | { | |
1736 | spa_t *spa; | |
1737 | int error; | |
1738 | ||
1739 | error = spa_open(zc->zc_name, &spa, FTAG); | |
1740 | if (error == 0) { | |
1741 | spa_freeze(spa); | |
1742 | spa_close(spa, FTAG); | |
1743 | } | |
1744 | return (error); | |
1745 | } | |
1746 | ||
1747 | static int | |
1748 | zfs_ioc_pool_upgrade(zfs_cmd_t *zc) | |
1749 | { | |
1750 | spa_t *spa; | |
1751 | int error; | |
1752 | ||
1753 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) | |
1754 | return (error); | |
1755 | ||
9ae529ec CS |
1756 | if (zc->zc_cookie < spa_version(spa) || |
1757 | !SPA_VERSION_IS_SUPPORTED(zc->zc_cookie)) { | |
34dc7c2f | 1758 | spa_close(spa, FTAG); |
2e528b49 | 1759 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1760 | } |
1761 | ||
1762 | spa_upgrade(spa, zc->zc_cookie); | |
1763 | spa_close(spa, FTAG); | |
1764 | ||
1765 | return (error); | |
1766 | } | |
1767 | ||
1768 | static int | |
1769 | zfs_ioc_pool_get_history(zfs_cmd_t *zc) | |
1770 | { | |
1771 | spa_t *spa; | |
1772 | char *hist_buf; | |
1773 | uint64_t size; | |
1774 | int error; | |
1775 | ||
1776 | if ((size = zc->zc_history_len) == 0) | |
2e528b49 | 1777 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
1778 | |
1779 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) | |
1780 | return (error); | |
1781 | ||
1782 | if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { | |
1783 | spa_close(spa, FTAG); | |
2e528b49 | 1784 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
1785 | } |
1786 | ||
34b84cb8 | 1787 | hist_buf = vmem_alloc(size, KM_SLEEP); |
34dc7c2f BB |
1788 | if ((error = spa_history_get(spa, &zc->zc_history_offset, |
1789 | &zc->zc_history_len, hist_buf)) == 0) { | |
9babb374 BB |
1790 | error = ddi_copyout(hist_buf, |
1791 | (void *)(uintptr_t)zc->zc_history, | |
1792 | zc->zc_history_len, zc->zc_iflags); | |
34dc7c2f BB |
1793 | } |
1794 | ||
1795 | spa_close(spa, FTAG); | |
34b84cb8 | 1796 | vmem_free(hist_buf, size); |
34dc7c2f BB |
1797 | return (error); |
1798 | } | |
1799 | ||
3541dc6d GA |
1800 | static int |
1801 | zfs_ioc_pool_reguid(zfs_cmd_t *zc) | |
1802 | { | |
1803 | spa_t *spa; | |
1804 | int error; | |
1805 | ||
1806 | error = spa_open(zc->zc_name, &spa, FTAG); | |
1807 | if (error == 0) { | |
1808 | error = spa_change_guid(spa); | |
1809 | spa_close(spa, FTAG); | |
1810 | } | |
1811 | return (error); | |
1812 | } | |
1813 | ||
34dc7c2f BB |
1814 | static int |
1815 | zfs_ioc_dsobj_to_dsname(zfs_cmd_t *zc) | |
1816 | { | |
13fe0198 | 1817 | return (dsl_dsobj_to_dsname(zc->zc_name, zc->zc_obj, zc->zc_value)); |
34dc7c2f BB |
1818 | } |
1819 | ||
428870ff BB |
1820 | /* |
1821 | * inputs: | |
1822 | * zc_name name of filesystem | |
1823 | * zc_obj object to find | |
1824 | * | |
1825 | * outputs: | |
1826 | * zc_value name of object | |
1827 | */ | |
34dc7c2f BB |
1828 | static int |
1829 | zfs_ioc_obj_to_path(zfs_cmd_t *zc) | |
1830 | { | |
428870ff | 1831 | objset_t *os; |
34dc7c2f BB |
1832 | int error; |
1833 | ||
428870ff | 1834 | /* XXX reading from objset not owned */ |
b5256303 TC |
1835 | if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, |
1836 | FTAG, &os)) != 0) | |
34dc7c2f | 1837 | return (error); |
428870ff | 1838 | if (dmu_objset_type(os) != DMU_OST_ZFS) { |
b5256303 | 1839 | dmu_objset_rele_flags(os, B_TRUE, FTAG); |
2e528b49 | 1840 | return (SET_ERROR(EINVAL)); |
428870ff BB |
1841 | } |
1842 | error = zfs_obj_to_path(os, zc->zc_obj, zc->zc_value, | |
34dc7c2f | 1843 | sizeof (zc->zc_value)); |
b5256303 | 1844 | dmu_objset_rele_flags(os, B_TRUE, FTAG); |
34dc7c2f BB |
1845 | |
1846 | return (error); | |
1847 | } | |
1848 | ||
572e2857 BB |
1849 | /* |
1850 | * inputs: | |
1851 | * zc_name name of filesystem | |
1852 | * zc_obj object to find | |
1853 | * | |
1854 | * outputs: | |
1855 | * zc_stat stats on object | |
1856 | * zc_value path to object | |
1857 | */ | |
1858 | static int | |
1859 | zfs_ioc_obj_to_stats(zfs_cmd_t *zc) | |
1860 | { | |
1861 | objset_t *os; | |
1862 | int error; | |
1863 | ||
1864 | /* XXX reading from objset not owned */ | |
b5256303 TC |
1865 | if ((error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, |
1866 | FTAG, &os)) != 0) | |
572e2857 BB |
1867 | return (error); |
1868 | if (dmu_objset_type(os) != DMU_OST_ZFS) { | |
b5256303 | 1869 | dmu_objset_rele_flags(os, B_TRUE, FTAG); |
2e528b49 | 1870 | return (SET_ERROR(EINVAL)); |
572e2857 BB |
1871 | } |
1872 | error = zfs_obj_to_stats(os, zc->zc_obj, &zc->zc_stat, zc->zc_value, | |
1873 | sizeof (zc->zc_value)); | |
b5256303 | 1874 | dmu_objset_rele_flags(os, B_TRUE, FTAG); |
572e2857 BB |
1875 | |
1876 | return (error); | |
1877 | } | |
1878 | ||
34dc7c2f BB |
1879 | static int |
1880 | zfs_ioc_vdev_add(zfs_cmd_t *zc) | |
1881 | { | |
1882 | spa_t *spa; | |
1883 | int error; | |
64ad2b26 | 1884 | nvlist_t *config; |
34dc7c2f BB |
1885 | |
1886 | error = spa_open(zc->zc_name, &spa, FTAG); | |
1887 | if (error != 0) | |
1888 | return (error); | |
1889 | ||
1890 | error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, | |
9babb374 | 1891 | zc->zc_iflags, &config); |
34dc7c2f BB |
1892 | if (error == 0) { |
1893 | error = spa_vdev_add(spa, config); | |
1894 | nvlist_free(config); | |
1895 | } | |
1896 | spa_close(spa, FTAG); | |
1897 | return (error); | |
1898 | } | |
1899 | ||
428870ff BB |
1900 | /* |
1901 | * inputs: | |
1902 | * zc_name name of the pool | |
1903 | * zc_nvlist_conf nvlist of devices to remove | |
1904 | * zc_cookie to stop the remove? | |
1905 | */ | |
34dc7c2f BB |
1906 | static int |
1907 | zfs_ioc_vdev_remove(zfs_cmd_t *zc) | |
1908 | { | |
1909 | spa_t *spa; | |
1910 | int error; | |
1911 | ||
1912 | error = spa_open(zc->zc_name, &spa, FTAG); | |
1913 | if (error != 0) | |
1914 | return (error); | |
1915 | error = spa_vdev_remove(spa, zc->zc_guid, B_FALSE); | |
1916 | spa_close(spa, FTAG); | |
1917 | return (error); | |
1918 | } | |
1919 | ||
1920 | static int | |
1921 | zfs_ioc_vdev_set_state(zfs_cmd_t *zc) | |
1922 | { | |
1923 | spa_t *spa; | |
1924 | int error; | |
1925 | vdev_state_t newstate = VDEV_STATE_UNKNOWN; | |
1926 | ||
1927 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) | |
1928 | return (error); | |
1929 | switch (zc->zc_cookie) { | |
1930 | case VDEV_STATE_ONLINE: | |
1931 | error = vdev_online(spa, zc->zc_guid, zc->zc_obj, &newstate); | |
1932 | break; | |
1933 | ||
1934 | case VDEV_STATE_OFFLINE: | |
1935 | error = vdev_offline(spa, zc->zc_guid, zc->zc_obj); | |
1936 | break; | |
1937 | ||
1938 | case VDEV_STATE_FAULTED: | |
428870ff | 1939 | if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && |
4a283c7f TH |
1940 | zc->zc_obj != VDEV_AUX_EXTERNAL && |
1941 | zc->zc_obj != VDEV_AUX_EXTERNAL_PERSIST) | |
428870ff BB |
1942 | zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; |
1943 | ||
1944 | error = vdev_fault(spa, zc->zc_guid, zc->zc_obj); | |
34dc7c2f BB |
1945 | break; |
1946 | ||
1947 | case VDEV_STATE_DEGRADED: | |
428870ff BB |
1948 | if (zc->zc_obj != VDEV_AUX_ERR_EXCEEDED && |
1949 | zc->zc_obj != VDEV_AUX_EXTERNAL) | |
1950 | zc->zc_obj = VDEV_AUX_ERR_EXCEEDED; | |
1951 | ||
1952 | error = vdev_degrade(spa, zc->zc_guid, zc->zc_obj); | |
34dc7c2f BB |
1953 | break; |
1954 | ||
1955 | default: | |
2e528b49 | 1956 | error = SET_ERROR(EINVAL); |
34dc7c2f BB |
1957 | } |
1958 | zc->zc_cookie = newstate; | |
1959 | spa_close(spa, FTAG); | |
1960 | return (error); | |
1961 | } | |
1962 | ||
1963 | static int | |
1964 | zfs_ioc_vdev_attach(zfs_cmd_t *zc) | |
1965 | { | |
1966 | spa_t *spa; | |
1967 | int replacing = zc->zc_cookie; | |
1968 | nvlist_t *config; | |
1969 | int error; | |
1970 | ||
1971 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) | |
1972 | return (error); | |
1973 | ||
1974 | if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, | |
9babb374 | 1975 | zc->zc_iflags, &config)) == 0) { |
34dc7c2f BB |
1976 | error = spa_vdev_attach(spa, zc->zc_guid, config, replacing); |
1977 | nvlist_free(config); | |
1978 | } | |
1979 | ||
1980 | spa_close(spa, FTAG); | |
1981 | return (error); | |
1982 | } | |
1983 | ||
1984 | static int | |
1985 | zfs_ioc_vdev_detach(zfs_cmd_t *zc) | |
1986 | { | |
1987 | spa_t *spa; | |
1988 | int error; | |
1989 | ||
1990 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) | |
1991 | return (error); | |
1992 | ||
fb5f0bc8 | 1993 | error = spa_vdev_detach(spa, zc->zc_guid, 0, B_FALSE); |
34dc7c2f BB |
1994 | |
1995 | spa_close(spa, FTAG); | |
1996 | return (error); | |
1997 | } | |
1998 | ||
428870ff BB |
1999 | static int |
2000 | zfs_ioc_vdev_split(zfs_cmd_t *zc) | |
2001 | { | |
2002 | spa_t *spa; | |
2003 | nvlist_t *config, *props = NULL; | |
2004 | int error; | |
2005 | boolean_t exp = !!(zc->zc_cookie & ZPOOL_EXPORT_AFTER_SPLIT); | |
2006 | ||
2007 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) | |
2008 | return (error); | |
2009 | ||
c65aa5b2 BB |
2010 | if ((error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, |
2011 | zc->zc_iflags, &config))) { | |
428870ff BB |
2012 | spa_close(spa, FTAG); |
2013 | return (error); | |
2014 | } | |
2015 | ||
2016 | if (zc->zc_nvlist_src_size != 0 && (error = | |
2017 | get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, | |
2018 | zc->zc_iflags, &props))) { | |
2019 | spa_close(spa, FTAG); | |
2020 | nvlist_free(config); | |
2021 | return (error); | |
2022 | } | |
2023 | ||
2024 | error = spa_vdev_split_mirror(spa, zc->zc_string, config, props, exp); | |
2025 | ||
2026 | spa_close(spa, FTAG); | |
2027 | ||
2028 | nvlist_free(config); | |
2029 | nvlist_free(props); | |
2030 | ||
2031 | return (error); | |
2032 | } | |
2033 | ||
34dc7c2f BB |
2034 | static int |
2035 | zfs_ioc_vdev_setpath(zfs_cmd_t *zc) | |
2036 | { | |
2037 | spa_t *spa; | |
2038 | char *path = zc->zc_value; | |
2039 | uint64_t guid = zc->zc_guid; | |
2040 | int error; | |
2041 | ||
2042 | error = spa_open(zc->zc_name, &spa, FTAG); | |
2043 | if (error != 0) | |
2044 | return (error); | |
2045 | ||
2046 | error = spa_vdev_setpath(spa, guid, path); | |
2047 | spa_close(spa, FTAG); | |
2048 | return (error); | |
2049 | } | |
2050 | ||
9babb374 BB |
2051 | static int |
2052 | zfs_ioc_vdev_setfru(zfs_cmd_t *zc) | |
2053 | { | |
2054 | spa_t *spa; | |
2055 | char *fru = zc->zc_value; | |
2056 | uint64_t guid = zc->zc_guid; | |
2057 | int error; | |
2058 | ||
2059 | error = spa_open(zc->zc_name, &spa, FTAG); | |
2060 | if (error != 0) | |
2061 | return (error); | |
2062 | ||
2063 | error = spa_vdev_setfru(spa, guid, fru); | |
2064 | spa_close(spa, FTAG); | |
2065 | return (error); | |
2066 | } | |
2067 | ||
34dc7c2f | 2068 | static int |
572e2857 | 2069 | zfs_ioc_objset_stats_impl(zfs_cmd_t *zc, objset_t *os) |
34dc7c2f | 2070 | { |
572e2857 | 2071 | int error = 0; |
34dc7c2f BB |
2072 | nvlist_t *nv; |
2073 | ||
34dc7c2f BB |
2074 | dmu_objset_fast_stat(os, &zc->zc_objset_stats); |
2075 | ||
2076 | if (zc->zc_nvlist_dst != 0 && | |
428870ff | 2077 | (error = dsl_prop_get_all(os, &nv)) == 0) { |
34dc7c2f BB |
2078 | dmu_objset_stats(os, nv); |
2079 | /* | |
2080 | * NB: zvol_get_stats() will read the objset contents, | |
2081 | * which we aren't supposed to do with a | |
b128c09f | 2082 | * DS_MODE_USER hold, because it could be |
34dc7c2f | 2083 | * inconsistent. So this is a bit of a workaround... |
428870ff | 2084 | * XXX reading with out owning |
34dc7c2f | 2085 | */ |
330d06f9 MA |
2086 | if (!zc->zc_objset_stats.dds_inconsistent && |
2087 | dmu_objset_type(os) == DMU_OST_ZVOL) { | |
2088 | error = zvol_get_stats(os, nv); | |
6d421005 B |
2089 | if (error == EIO) { |
2090 | nvlist_free(nv); | |
330d06f9 | 2091 | return (error); |
6d421005 | 2092 | } |
c99c9001 | 2093 | VERIFY0(error); |
34dc7c2f | 2094 | } |
8a8f5c6b BB |
2095 | if (error == 0) |
2096 | error = put_nvlist(zc, nv); | |
34dc7c2f BB |
2097 | nvlist_free(nv); |
2098 | } | |
2099 | ||
572e2857 BB |
2100 | return (error); |
2101 | } | |
2102 | ||
2103 | /* | |
2104 | * inputs: | |
2105 | * zc_name name of filesystem | |
2106 | * zc_nvlist_dst_size size of buffer for property nvlist | |
2107 | * | |
2108 | * outputs: | |
2109 | * zc_objset_stats stats | |
2110 | * zc_nvlist_dst property nvlist | |
2111 | * zc_nvlist_dst_size size of property nvlist | |
2112 | */ | |
2113 | static int | |
2114 | zfs_ioc_objset_stats(zfs_cmd_t *zc) | |
2115 | { | |
13fe0198 | 2116 | objset_t *os; |
572e2857 BB |
2117 | int error; |
2118 | ||
13fe0198 MA |
2119 | error = dmu_objset_hold(zc->zc_name, FTAG, &os); |
2120 | if (error == 0) { | |
2121 | error = zfs_ioc_objset_stats_impl(zc, os); | |
2122 | dmu_objset_rele(os, FTAG); | |
2123 | } | |
572e2857 | 2124 | |
428870ff BB |
2125 | return (error); |
2126 | } | |
2127 | ||
2128 | /* | |
2129 | * inputs: | |
2130 | * zc_name name of filesystem | |
2131 | * zc_nvlist_dst_size size of buffer for property nvlist | |
2132 | * | |
2133 | * outputs: | |
2134 | * zc_nvlist_dst received property nvlist | |
2135 | * zc_nvlist_dst_size size of received property nvlist | |
2136 | * | |
2137 | * Gets received properties (distinct from local properties on or after | |
2138 | * SPA_VERSION_RECVD_PROPS) for callers who want to differentiate received from | |
2139 | * local property values. | |
2140 | */ | |
2141 | static int | |
26685276 | 2142 | zfs_ioc_objset_recvd_props(zfs_cmd_t *zc) |
428870ff | 2143 | { |
13fe0198 | 2144 | int error = 0; |
428870ff BB |
2145 | nvlist_t *nv; |
2146 | ||
428870ff BB |
2147 | /* |
2148 | * Without this check, we would return local property values if the | |
2149 | * caller has not already received properties on or after | |
2150 | * SPA_VERSION_RECVD_PROPS. | |
2151 | */ | |
13fe0198 | 2152 | if (!dsl_prop_get_hasrecvd(zc->zc_name)) |
2e528b49 | 2153 | return (SET_ERROR(ENOTSUP)); |
428870ff BB |
2154 | |
2155 | if (zc->zc_nvlist_dst != 0 && | |
13fe0198 | 2156 | (error = dsl_prop_get_received(zc->zc_name, &nv)) == 0) { |
428870ff BB |
2157 | error = put_nvlist(zc, nv); |
2158 | nvlist_free(nv); | |
2159 | } | |
2160 | ||
34dc7c2f BB |
2161 | return (error); |
2162 | } | |
2163 | ||
2164 | static int | |
2165 | nvl_add_zplprop(objset_t *os, nvlist_t *props, zfs_prop_t prop) | |
2166 | { | |
2167 | uint64_t value; | |
2168 | int error; | |
2169 | ||
2170 | /* | |
2171 | * zfs_get_zplprop() will either find a value or give us | |
2172 | * the default value (if there is one). | |
2173 | */ | |
2174 | if ((error = zfs_get_zplprop(os, prop, &value)) != 0) | |
2175 | return (error); | |
2176 | VERIFY(nvlist_add_uint64(props, zfs_prop_to_name(prop), value) == 0); | |
2177 | return (0); | |
2178 | } | |
2179 | ||
2180 | /* | |
2181 | * inputs: | |
2182 | * zc_name name of filesystem | |
2183 | * zc_nvlist_dst_size size of buffer for zpl property nvlist | |
2184 | * | |
2185 | * outputs: | |
2186 | * zc_nvlist_dst zpl property nvlist | |
2187 | * zc_nvlist_dst_size size of zpl property nvlist | |
2188 | */ | |
2189 | static int | |
2190 | zfs_ioc_objset_zplprops(zfs_cmd_t *zc) | |
2191 | { | |
2192 | objset_t *os; | |
2193 | int err; | |
2194 | ||
428870ff | 2195 | /* XXX reading without owning */ |
c65aa5b2 | 2196 | if ((err = dmu_objset_hold(zc->zc_name, FTAG, &os))) |
34dc7c2f BB |
2197 | return (err); |
2198 | ||
2199 | dmu_objset_fast_stat(os, &zc->zc_objset_stats); | |
2200 | ||
2201 | /* | |
2202 | * NB: nvl_add_zplprop() will read the objset contents, | |
b128c09f BB |
2203 | * which we aren't supposed to do with a DS_MODE_USER |
2204 | * hold, because it could be inconsistent. | |
34dc7c2f | 2205 | */ |
b8864a23 | 2206 | if (zc->zc_nvlist_dst != 0 && |
34dc7c2f BB |
2207 | !zc->zc_objset_stats.dds_inconsistent && |
2208 | dmu_objset_type(os) == DMU_OST_ZFS) { | |
2209 | nvlist_t *nv; | |
2210 | ||
2211 | VERIFY(nvlist_alloc(&nv, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
2212 | if ((err = nvl_add_zplprop(os, nv, ZFS_PROP_VERSION)) == 0 && | |
2213 | (err = nvl_add_zplprop(os, nv, ZFS_PROP_NORMALIZE)) == 0 && | |
2214 | (err = nvl_add_zplprop(os, nv, ZFS_PROP_UTF8ONLY)) == 0 && | |
2215 | (err = nvl_add_zplprop(os, nv, ZFS_PROP_CASE)) == 0) | |
2216 | err = put_nvlist(zc, nv); | |
2217 | nvlist_free(nv); | |
2218 | } else { | |
2e528b49 | 2219 | err = SET_ERROR(ENOENT); |
34dc7c2f | 2220 | } |
428870ff | 2221 | dmu_objset_rele(os, FTAG); |
34dc7c2f BB |
2222 | return (err); |
2223 | } | |
2224 | ||
ba6a2402 | 2225 | boolean_t |
9babb374 BB |
2226 | dataset_name_hidden(const char *name) |
2227 | { | |
2228 | /* | |
2229 | * Skip over datasets that are not visible in this zone, | |
2230 | * internal datasets (which have a $ in their name), and | |
2231 | * temporary datasets (which have a % in their name). | |
2232 | */ | |
2233 | if (strchr(name, '$') != NULL) | |
2234 | return (B_TRUE); | |
2235 | if (strchr(name, '%') != NULL) | |
2236 | return (B_TRUE); | |
2237 | if (!INGLOBALZONE(curproc) && !zone_dataset_visible(name, NULL)) | |
2238 | return (B_TRUE); | |
2239 | return (B_FALSE); | |
2240 | } | |
2241 | ||
34dc7c2f BB |
2242 | /* |
2243 | * inputs: | |
2244 | * zc_name name of filesystem | |
2245 | * zc_cookie zap cursor | |
2246 | * zc_nvlist_dst_size size of buffer for property nvlist | |
2247 | * | |
2248 | * outputs: | |
2249 | * zc_name name of next filesystem | |
9babb374 | 2250 | * zc_cookie zap cursor |
34dc7c2f BB |
2251 | * zc_objset_stats stats |
2252 | * zc_nvlist_dst property nvlist | |
2253 | * zc_nvlist_dst_size size of property nvlist | |
34dc7c2f BB |
2254 | */ |
2255 | static int | |
2256 | zfs_ioc_dataset_list_next(zfs_cmd_t *zc) | |
2257 | { | |
2258 | objset_t *os; | |
2259 | int error; | |
2260 | char *p; | |
428870ff | 2261 | size_t orig_len = strlen(zc->zc_name); |
34dc7c2f | 2262 | |
428870ff | 2263 | top: |
c65aa5b2 | 2264 | if ((error = dmu_objset_hold(zc->zc_name, FTAG, &os))) { |
34dc7c2f | 2265 | if (error == ENOENT) |
2e528b49 | 2266 | error = SET_ERROR(ESRCH); |
34dc7c2f BB |
2267 | return (error); |
2268 | } | |
2269 | ||
2270 | p = strrchr(zc->zc_name, '/'); | |
2271 | if (p == NULL || p[1] != '\0') | |
2272 | (void) strlcat(zc->zc_name, "/", sizeof (zc->zc_name)); | |
2273 | p = zc->zc_name + strlen(zc->zc_name); | |
2274 | ||
2275 | do { | |
2276 | error = dmu_dir_list_next(os, | |
2277 | sizeof (zc->zc_name) - (p - zc->zc_name), p, | |
2278 | NULL, &zc->zc_cookie); | |
2279 | if (error == ENOENT) | |
2e528b49 | 2280 | error = SET_ERROR(ESRCH); |
330d06f9 | 2281 | } while (error == 0 && dataset_name_hidden(zc->zc_name)); |
428870ff | 2282 | dmu_objset_rele(os, FTAG); |
34dc7c2f | 2283 | |
428870ff BB |
2284 | /* |
2285 | * If it's an internal dataset (ie. with a '$' in its name), | |
2286 | * don't try to get stats for it, otherwise we'll return ENOENT. | |
2287 | */ | |
2288 | if (error == 0 && strchr(zc->zc_name, '$') == NULL) { | |
34dc7c2f | 2289 | error = zfs_ioc_objset_stats(zc); /* fill in the stats */ |
428870ff BB |
2290 | if (error == ENOENT) { |
2291 | /* We lost a race with destroy, get the next one. */ | |
2292 | zc->zc_name[orig_len] = '\0'; | |
2293 | goto top; | |
2294 | } | |
2295 | } | |
34dc7c2f BB |
2296 | return (error); |
2297 | } | |
2298 | ||
2299 | /* | |
2300 | * inputs: | |
2301 | * zc_name name of filesystem | |
2302 | * zc_cookie zap cursor | |
2303 | * zc_nvlist_dst_size size of buffer for property nvlist | |
2304 | * | |
2305 | * outputs: | |
2306 | * zc_name name of next snapshot | |
2307 | * zc_objset_stats stats | |
2308 | * zc_nvlist_dst property nvlist | |
2309 | * zc_nvlist_dst_size size of property nvlist | |
34dc7c2f BB |
2310 | */ |
2311 | static int | |
2312 | zfs_ioc_snapshot_list_next(zfs_cmd_t *zc) | |
2313 | { | |
2314 | objset_t *os; | |
2315 | int error; | |
2316 | ||
428870ff | 2317 | error = dmu_objset_hold(zc->zc_name, FTAG, &os); |
13fe0198 | 2318 | if (error != 0) { |
b128c09f | 2319 | return (error == ENOENT ? ESRCH : error); |
13fe0198 | 2320 | } |
34dc7c2f BB |
2321 | |
2322 | /* | |
2323 | * A dataset name of maximum length cannot have any snapshots, | |
2324 | * so exit immediately. | |
2325 | */ | |
eca7b760 IK |
2326 | if (strlcat(zc->zc_name, "@", sizeof (zc->zc_name)) >= |
2327 | ZFS_MAX_DATASET_NAME_LEN) { | |
428870ff | 2328 | dmu_objset_rele(os, FTAG); |
2e528b49 | 2329 | return (SET_ERROR(ESRCH)); |
34dc7c2f BB |
2330 | } |
2331 | ||
2332 | error = dmu_snapshot_list_next(os, | |
2333 | sizeof (zc->zc_name) - strlen(zc->zc_name), | |
572e2857 BB |
2334 | zc->zc_name + strlen(zc->zc_name), &zc->zc_obj, &zc->zc_cookie, |
2335 | NULL); | |
2336 | ||
0cee2406 | 2337 | if (error == 0 && !zc->zc_simple) { |
572e2857 BB |
2338 | dsl_dataset_t *ds; |
2339 | dsl_pool_t *dp = os->os_dsl_dataset->ds_dir->dd_pool; | |
2340 | ||
572e2857 | 2341 | error = dsl_dataset_hold_obj(dp, zc->zc_obj, FTAG, &ds); |
13fe0198 | 2342 | if (error == 0) { |
572e2857 BB |
2343 | objset_t *ossnap; |
2344 | ||
2345 | error = dmu_objset_from_ds(ds, &ossnap); | |
2346 | if (error == 0) | |
2347 | error = zfs_ioc_objset_stats_impl(zc, ossnap); | |
2348 | dsl_dataset_rele(ds, FTAG); | |
428870ff BB |
2349 | } |
2350 | } else if (error == ENOENT) { | |
2e528b49 | 2351 | error = SET_ERROR(ESRCH); |
428870ff | 2352 | } |
34dc7c2f | 2353 | |
572e2857 | 2354 | dmu_objset_rele(os, FTAG); |
34dc7c2f | 2355 | /* if we failed, undo the @ that we tacked on to zc_name */ |
13fe0198 | 2356 | if (error != 0) |
34dc7c2f | 2357 | *strchr(zc->zc_name, '@') = '\0'; |
34dc7c2f BB |
2358 | return (error); |
2359 | } | |
2360 | ||
428870ff BB |
2361 | static int |
2362 | zfs_prop_set_userquota(const char *dsname, nvpair_t *pair) | |
34dc7c2f | 2363 | { |
428870ff BB |
2364 | const char *propname = nvpair_name(pair); |
2365 | uint64_t *valary; | |
2366 | unsigned int vallen; | |
2367 | const char *domain; | |
2368 | char *dash; | |
2369 | zfs_userquota_prop_t type; | |
2370 | uint64_t rid; | |
2371 | uint64_t quota; | |
0037b49e | 2372 | zfsvfs_t *zfsvfs; |
428870ff BB |
2373 | int err; |
2374 | ||
2375 | if (nvpair_type(pair) == DATA_TYPE_NVLIST) { | |
2376 | nvlist_t *attrs; | |
2377 | VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); | |
2378 | if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, | |
2379 | &pair) != 0) | |
2e528b49 | 2380 | return (SET_ERROR(EINVAL)); |
428870ff | 2381 | } |
34dc7c2f BB |
2382 | |
2383 | /* | |
428870ff BB |
2384 | * A correctly constructed propname is encoded as |
2385 | * userquota@<rid>-<domain>. | |
34dc7c2f | 2386 | */ |
428870ff BB |
2387 | if ((dash = strchr(propname, '-')) == NULL || |
2388 | nvpair_value_uint64_array(pair, &valary, &vallen) != 0 || | |
2389 | vallen != 3) | |
2e528b49 | 2390 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 2391 | |
428870ff BB |
2392 | domain = dash + 1; |
2393 | type = valary[0]; | |
2394 | rid = valary[1]; | |
2395 | quota = valary[2]; | |
34dc7c2f | 2396 | |
f298b24d | 2397 | err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_FALSE); |
428870ff | 2398 | if (err == 0) { |
0037b49e | 2399 | err = zfs_set_userquota(zfsvfs, type, domain, rid, quota); |
f298b24d | 2400 | zfsvfs_rele(zfsvfs, FTAG); |
428870ff | 2401 | } |
9babb374 | 2402 | |
428870ff BB |
2403 | return (err); |
2404 | } | |
34dc7c2f | 2405 | |
428870ff BB |
2406 | /* |
2407 | * If the named property is one that has a special function to set its value, | |
2408 | * return 0 on success and a positive error code on failure; otherwise if it is | |
2409 | * not one of the special properties handled by this function, return -1. | |
2410 | * | |
2411 | * XXX: It would be better for callers of the property interface if we handled | |
2412 | * these special cases in dsl_prop.c (in the dsl layer). | |
2413 | */ | |
2414 | static int | |
2415 | zfs_prop_set_special(const char *dsname, zprop_source_t source, | |
2416 | nvpair_t *pair) | |
2417 | { | |
2418 | const char *propname = nvpair_name(pair); | |
2419 | zfs_prop_t prop = zfs_name_to_prop(propname); | |
b5256303 TC |
2420 | uint64_t intval = 0; |
2421 | char *strval = NULL; | |
f1512ee6 | 2422 | int err = -1; |
9babb374 | 2423 | |
428870ff BB |
2424 | if (prop == ZPROP_INVAL) { |
2425 | if (zfs_prop_userquota(propname)) | |
2426 | return (zfs_prop_set_userquota(dsname, pair)); | |
2427 | return (-1); | |
2428 | } | |
34dc7c2f | 2429 | |
428870ff BB |
2430 | if (nvpair_type(pair) == DATA_TYPE_NVLIST) { |
2431 | nvlist_t *attrs; | |
2432 | VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); | |
2433 | VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, | |
2434 | &pair) == 0); | |
2435 | } | |
b128c09f | 2436 | |
b5256303 TC |
2437 | /* all special properties are numeric except for keylocation */ |
2438 | if (zfs_prop_get_type(prop) == PROP_TYPE_STRING) { | |
2439 | strval = fnvpair_value_string(pair); | |
2440 | } else { | |
2441 | intval = fnvpair_value_uint64(pair); | |
2442 | } | |
34dc7c2f | 2443 | |
428870ff BB |
2444 | switch (prop) { |
2445 | case ZFS_PROP_QUOTA: | |
2446 | err = dsl_dir_set_quota(dsname, source, intval); | |
2447 | break; | |
2448 | case ZFS_PROP_REFQUOTA: | |
13fe0198 | 2449 | err = dsl_dataset_set_refquota(dsname, source, intval); |
428870ff | 2450 | break; |
788eb90c JJ |
2451 | case ZFS_PROP_FILESYSTEM_LIMIT: |
2452 | case ZFS_PROP_SNAPSHOT_LIMIT: | |
2453 | if (intval == UINT64_MAX) { | |
2454 | /* clearing the limit, just do it */ | |
2455 | err = 0; | |
2456 | } else { | |
2457 | err = dsl_dir_activate_fs_ss_limit(dsname); | |
2458 | } | |
b5256303 TC |
2459 | /* |
2460 | * Set err to -1 to force the zfs_set_prop_nvlist code down the | |
2461 | * default path to set the value in the nvlist. | |
2462 | */ | |
2463 | if (err == 0) | |
2464 | err = -1; | |
2465 | break; | |
2466 | case ZFS_PROP_KEYLOCATION: | |
2467 | err = dsl_crypto_can_set_keylocation(dsname, strval); | |
2468 | ||
788eb90c JJ |
2469 | /* |
2470 | * Set err to -1 to force the zfs_set_prop_nvlist code down the | |
2471 | * default path to set the value in the nvlist. | |
2472 | */ | |
2473 | if (err == 0) | |
2474 | err = -1; | |
2475 | break; | |
428870ff BB |
2476 | case ZFS_PROP_RESERVATION: |
2477 | err = dsl_dir_set_reservation(dsname, source, intval); | |
2478 | break; | |
2479 | case ZFS_PROP_REFRESERVATION: | |
13fe0198 | 2480 | err = dsl_dataset_set_refreservation(dsname, source, intval); |
428870ff BB |
2481 | break; |
2482 | case ZFS_PROP_VOLSIZE: | |
60101509 | 2483 | err = zvol_set_volsize(dsname, intval); |
428870ff | 2484 | break; |
0b4d1b58 | 2485 | case ZFS_PROP_SNAPDEV: |
a0bd735a | 2486 | err = zvol_set_snapdev(dsname, source, intval); |
0b4d1b58 | 2487 | break; |
cf8738d8 | 2488 | case ZFS_PROP_VOLMODE: |
2489 | err = zvol_set_volmode(dsname, source, intval); | |
2490 | break; | |
428870ff BB |
2491 | case ZFS_PROP_VERSION: |
2492 | { | |
0037b49e | 2493 | zfsvfs_t *zfsvfs; |
428870ff | 2494 | |
f298b24d | 2495 | if ((err = zfsvfs_hold(dsname, FTAG, &zfsvfs, B_TRUE)) != 0) |
34dc7c2f | 2496 | break; |
b128c09f | 2497 | |
0037b49e | 2498 | err = zfs_set_version(zfsvfs, intval); |
f298b24d | 2499 | zfsvfs_rele(zfsvfs, FTAG); |
34dc7c2f | 2500 | |
428870ff BB |
2501 | if (err == 0 && intval >= ZPL_VERSION_USERSPACE) { |
2502 | zfs_cmd_t *zc; | |
34dc7c2f | 2503 | |
efcd79a8 | 2504 | zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); |
428870ff BB |
2505 | (void) strcpy(zc->zc_name, dsname); |
2506 | (void) zfs_ioc_userspace_upgrade(zc); | |
1de321e6 | 2507 | (void) zfs_ioc_userobjspace_upgrade(zc); |
428870ff | 2508 | kmem_free(zc, sizeof (zfs_cmd_t)); |
34dc7c2f | 2509 | } |
428870ff BB |
2510 | break; |
2511 | } | |
428870ff BB |
2512 | default: |
2513 | err = -1; | |
2514 | } | |
34dc7c2f | 2515 | |
428870ff BB |
2516 | return (err); |
2517 | } | |
34dc7c2f | 2518 | |
428870ff BB |
2519 | /* |
2520 | * This function is best effort. If it fails to set any of the given properties, | |
6f1ffb06 MA |
2521 | * it continues to set as many as it can and returns the last error |
2522 | * encountered. If the caller provides a non-NULL errlist, it will be filled in | |
2523 | * with the list of names of all the properties that failed along with the | |
2524 | * corresponding error numbers. | |
428870ff | 2525 | * |
6f1ffb06 MA |
2526 | * If every property is set successfully, zero is returned and errlist is not |
2527 | * modified. | |
428870ff BB |
2528 | */ |
2529 | int | |
2530 | zfs_set_prop_nvlist(const char *dsname, zprop_source_t source, nvlist_t *nvl, | |
6f1ffb06 | 2531 | nvlist_t *errlist) |
428870ff BB |
2532 | { |
2533 | nvpair_t *pair; | |
2534 | nvpair_t *propval; | |
2535 | int rv = 0; | |
2536 | uint64_t intval; | |
2537 | char *strval; | |
34dc7c2f | 2538 | |
6f1ffb06 MA |
2539 | nvlist_t *genericnvl = fnvlist_alloc(); |
2540 | nvlist_t *retrynvl = fnvlist_alloc(); | |
428870ff BB |
2541 | retry: |
2542 | pair = NULL; | |
2543 | while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { | |
2544 | const char *propname = nvpair_name(pair); | |
2545 | zfs_prop_t prop = zfs_name_to_prop(propname); | |
2546 | int err = 0; | |
2547 | ||
2548 | /* decode the property value */ | |
2549 | propval = pair; | |
2550 | if (nvpair_type(pair) == DATA_TYPE_NVLIST) { | |
2551 | nvlist_t *attrs; | |
6f1ffb06 | 2552 | attrs = fnvpair_value_nvlist(pair); |
428870ff BB |
2553 | if (nvlist_lookup_nvpair(attrs, ZPROP_VALUE, |
2554 | &propval) != 0) | |
2e528b49 | 2555 | err = SET_ERROR(EINVAL); |
9babb374 | 2556 | } |
34dc7c2f | 2557 | |
428870ff | 2558 | /* Validate value type */ |
a3eeab2d | 2559 | if (err == 0 && source == ZPROP_SRC_INHERITED) { |
2560 | /* inherited properties are expected to be booleans */ | |
2561 | if (nvpair_type(propval) != DATA_TYPE_BOOLEAN) | |
2562 | err = SET_ERROR(EINVAL); | |
2563 | } else if (err == 0 && prop == ZPROP_INVAL) { | |
428870ff BB |
2564 | if (zfs_prop_user(propname)) { |
2565 | if (nvpair_type(propval) != DATA_TYPE_STRING) | |
2e528b49 | 2566 | err = SET_ERROR(EINVAL); |
428870ff BB |
2567 | } else if (zfs_prop_userquota(propname)) { |
2568 | if (nvpair_type(propval) != | |
2569 | DATA_TYPE_UINT64_ARRAY) | |
2e528b49 | 2570 | err = SET_ERROR(EINVAL); |
330d06f9 | 2571 | } else { |
2e528b49 | 2572 | err = SET_ERROR(EINVAL); |
428870ff BB |
2573 | } |
2574 | } else if (err == 0) { | |
2575 | if (nvpair_type(propval) == DATA_TYPE_STRING) { | |
2576 | if (zfs_prop_get_type(prop) != PROP_TYPE_STRING) | |
2e528b49 | 2577 | err = SET_ERROR(EINVAL); |
428870ff | 2578 | } else if (nvpair_type(propval) == DATA_TYPE_UINT64) { |
34dc7c2f BB |
2579 | const char *unused; |
2580 | ||
6f1ffb06 | 2581 | intval = fnvpair_value_uint64(propval); |
34dc7c2f BB |
2582 | |
2583 | switch (zfs_prop_get_type(prop)) { | |
2584 | case PROP_TYPE_NUMBER: | |
2585 | break; | |
2586 | case PROP_TYPE_STRING: | |
2e528b49 | 2587 | err = SET_ERROR(EINVAL); |
428870ff | 2588 | break; |
34dc7c2f BB |
2589 | case PROP_TYPE_INDEX: |
2590 | if (zfs_prop_index_to_string(prop, | |
428870ff | 2591 | intval, &unused) != 0) |
2e528b49 | 2592 | err = SET_ERROR(EINVAL); |
34dc7c2f BB |
2593 | break; |
2594 | default: | |
2595 | cmn_err(CE_PANIC, | |
2596 | "unknown property type"); | |
34dc7c2f | 2597 | } |
34dc7c2f | 2598 | } else { |
2e528b49 | 2599 | err = SET_ERROR(EINVAL); |
34dc7c2f | 2600 | } |
34dc7c2f | 2601 | } |
428870ff BB |
2602 | |
2603 | /* Validate permissions */ | |
2604 | if (err == 0) | |
2605 | err = zfs_check_settable(dsname, pair, CRED()); | |
2606 | ||
2607 | if (err == 0) { | |
a3eeab2d | 2608 | if (source == ZPROP_SRC_INHERITED) |
2609 | err = -1; /* does not need special handling */ | |
2610 | else | |
2611 | err = zfs_prop_set_special(dsname, source, | |
2612 | pair); | |
428870ff BB |
2613 | if (err == -1) { |
2614 | /* | |
2615 | * For better performance we build up a list of | |
2616 | * properties to set in a single transaction. | |
2617 | */ | |
2618 | err = nvlist_add_nvpair(genericnvl, pair); | |
2619 | } else if (err != 0 && nvl != retrynvl) { | |
2620 | /* | |
2621 | * This may be a spurious error caused by | |
2622 | * receiving quota and reservation out of order. | |
2623 | * Try again in a second pass. | |
2624 | */ | |
2625 | err = nvlist_add_nvpair(retrynvl, pair); | |
2626 | } | |
2627 | } | |
2628 | ||
6f1ffb06 MA |
2629 | if (err != 0) { |
2630 | if (errlist != NULL) | |
2631 | fnvlist_add_int32(errlist, propname, err); | |
2632 | rv = err; | |
2633 | } | |
34dc7c2f BB |
2634 | } |
2635 | ||
428870ff BB |
2636 | if (nvl != retrynvl && !nvlist_empty(retrynvl)) { |
2637 | nvl = retrynvl; | |
2638 | goto retry; | |
2639 | } | |
2640 | ||
2641 | if (!nvlist_empty(genericnvl) && | |
2642 | dsl_props_set(dsname, source, genericnvl) != 0) { | |
2643 | /* | |
2644 | * If this fails, we still want to set as many properties as we | |
2645 | * can, so try setting them individually. | |
2646 | */ | |
2647 | pair = NULL; | |
2648 | while ((pair = nvlist_next_nvpair(genericnvl, pair)) != NULL) { | |
2649 | const char *propname = nvpair_name(pair); | |
2650 | int err = 0; | |
2651 | ||
2652 | propval = pair; | |
2653 | if (nvpair_type(pair) == DATA_TYPE_NVLIST) { | |
2654 | nvlist_t *attrs; | |
6f1ffb06 MA |
2655 | attrs = fnvpair_value_nvlist(pair); |
2656 | propval = fnvlist_lookup_nvpair(attrs, | |
2657 | ZPROP_VALUE); | |
428870ff BB |
2658 | } |
2659 | ||
2660 | if (nvpair_type(propval) == DATA_TYPE_STRING) { | |
6f1ffb06 | 2661 | strval = fnvpair_value_string(propval); |
13fe0198 MA |
2662 | err = dsl_prop_set_string(dsname, propname, |
2663 | source, strval); | |
a3eeab2d | 2664 | } else if (nvpair_type(propval) == DATA_TYPE_BOOLEAN) { |
2665 | err = dsl_prop_inherit(dsname, propname, | |
2666 | source); | |
428870ff | 2667 | } else { |
6f1ffb06 | 2668 | intval = fnvpair_value_uint64(propval); |
13fe0198 MA |
2669 | err = dsl_prop_set_int(dsname, propname, source, |
2670 | intval); | |
428870ff BB |
2671 | } |
2672 | ||
2673 | if (err != 0) { | |
6f1ffb06 MA |
2674 | if (errlist != NULL) { |
2675 | fnvlist_add_int32(errlist, propname, | |
2676 | err); | |
2677 | } | |
2678 | rv = err; | |
428870ff BB |
2679 | } |
2680 | } | |
9babb374 | 2681 | } |
9babb374 | 2682 | nvlist_free(genericnvl); |
428870ff BB |
2683 | nvlist_free(retrynvl); |
2684 | ||
428870ff | 2685 | return (rv); |
9babb374 BB |
2686 | } |
2687 | ||
2688 | /* | |
2689 | * Check that all the properties are valid user properties. | |
2690 | */ | |
2691 | static int | |
6f1ffb06 | 2692 | zfs_check_userprops(const char *fsname, nvlist_t *nvl) |
9babb374 | 2693 | { |
428870ff | 2694 | nvpair_t *pair = NULL; |
9babb374 BB |
2695 | int error = 0; |
2696 | ||
428870ff BB |
2697 | while ((pair = nvlist_next_nvpair(nvl, pair)) != NULL) { |
2698 | const char *propname = nvpair_name(pair); | |
9babb374 BB |
2699 | |
2700 | if (!zfs_prop_user(propname) || | |
428870ff | 2701 | nvpair_type(pair) != DATA_TYPE_STRING) |
2e528b49 | 2702 | return (SET_ERROR(EINVAL)); |
9babb374 | 2703 | |
c65aa5b2 BB |
2704 | if ((error = zfs_secpolicy_write_perms(fsname, |
2705 | ZFS_DELEG_PERM_USERPROP, CRED()))) | |
9babb374 BB |
2706 | return (error); |
2707 | ||
2708 | if (strlen(propname) >= ZAP_MAXNAMELEN) | |
2e528b49 | 2709 | return (SET_ERROR(ENAMETOOLONG)); |
9babb374 | 2710 | |
da536844 | 2711 | if (strlen(fnvpair_value_string(pair)) >= ZAP_MAXVALUELEN) |
2e528b49 | 2712 | return (SET_ERROR(E2BIG)); |
9babb374 | 2713 | } |
34dc7c2f BB |
2714 | return (0); |
2715 | } | |
2716 | ||
428870ff BB |
2717 | static void |
2718 | props_skip(nvlist_t *props, nvlist_t *skipped, nvlist_t **newprops) | |
2719 | { | |
2720 | nvpair_t *pair; | |
2721 | ||
2722 | VERIFY(nvlist_alloc(newprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
2723 | ||
2724 | pair = NULL; | |
2725 | while ((pair = nvlist_next_nvpair(props, pair)) != NULL) { | |
2726 | if (nvlist_exists(skipped, nvpair_name(pair))) | |
2727 | continue; | |
2728 | ||
2729 | VERIFY(nvlist_add_nvpair(*newprops, pair) == 0); | |
2730 | } | |
2731 | } | |
2732 | ||
2733 | static int | |
13fe0198 | 2734 | clear_received_props(const char *dsname, nvlist_t *props, |
428870ff BB |
2735 | nvlist_t *skipped) |
2736 | { | |
2737 | int err = 0; | |
2738 | nvlist_t *cleared_props = NULL; | |
2739 | props_skip(props, skipped, &cleared_props); | |
2740 | if (!nvlist_empty(cleared_props)) { | |
2741 | /* | |
2742 | * Acts on local properties until the dataset has received | |
2743 | * properties at least once on or after SPA_VERSION_RECVD_PROPS. | |
2744 | */ | |
2745 | zprop_source_t flags = (ZPROP_SRC_NONE | | |
13fe0198 MA |
2746 | (dsl_prop_get_hasrecvd(dsname) ? ZPROP_SRC_RECEIVED : 0)); |
2747 | err = zfs_set_prop_nvlist(dsname, flags, cleared_props, NULL); | |
428870ff BB |
2748 | } |
2749 | nvlist_free(cleared_props); | |
2750 | return (err); | |
2751 | } | |
2752 | ||
34dc7c2f BB |
2753 | /* |
2754 | * inputs: | |
2755 | * zc_name name of filesystem | |
9babb374 | 2756 | * zc_value name of property to set |
34dc7c2f | 2757 | * zc_nvlist_src{_size} nvlist of properties to apply |
428870ff | 2758 | * zc_cookie received properties flag |
34dc7c2f | 2759 | * |
428870ff BB |
2760 | * outputs: |
2761 | * zc_nvlist_dst{_size} error for each unapplied received property | |
34dc7c2f BB |
2762 | */ |
2763 | static int | |
2764 | zfs_ioc_set_prop(zfs_cmd_t *zc) | |
2765 | { | |
2766 | nvlist_t *nvl; | |
428870ff BB |
2767 | boolean_t received = zc->zc_cookie; |
2768 | zprop_source_t source = (received ? ZPROP_SRC_RECEIVED : | |
2769 | ZPROP_SRC_LOCAL); | |
6f1ffb06 | 2770 | nvlist_t *errors; |
34dc7c2f BB |
2771 | int error; |
2772 | ||
2773 | if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, | |
9babb374 | 2774 | zc->zc_iflags, &nvl)) != 0) |
34dc7c2f BB |
2775 | return (error); |
2776 | ||
428870ff | 2777 | if (received) { |
b128c09f | 2778 | nvlist_t *origprops; |
b128c09f | 2779 | |
13fe0198 MA |
2780 | if (dsl_prop_get_received(zc->zc_name, &origprops) == 0) { |
2781 | (void) clear_received_props(zc->zc_name, | |
2782 | origprops, nvl); | |
2783 | nvlist_free(origprops); | |
428870ff | 2784 | } |
13fe0198 MA |
2785 | |
2786 | error = dsl_prop_set_hasrecvd(zc->zc_name); | |
b128c09f BB |
2787 | } |
2788 | ||
6f1ffb06 | 2789 | errors = fnvlist_alloc(); |
13fe0198 MA |
2790 | if (error == 0) |
2791 | error = zfs_set_prop_nvlist(zc->zc_name, source, nvl, errors); | |
34dc7c2f | 2792 | |
b8864a23 | 2793 | if (zc->zc_nvlist_dst != 0 && errors != NULL) { |
428870ff BB |
2794 | (void) put_nvlist(zc, errors); |
2795 | } | |
2796 | ||
2797 | nvlist_free(errors); | |
34dc7c2f BB |
2798 | nvlist_free(nvl); |
2799 | return (error); | |
2800 | } | |
2801 | ||
2802 | /* | |
2803 | * inputs: | |
2804 | * zc_name name of filesystem | |
2805 | * zc_value name of property to inherit | |
428870ff | 2806 | * zc_cookie revert to received value if TRUE |
34dc7c2f BB |
2807 | * |
2808 | * outputs: none | |
2809 | */ | |
2810 | static int | |
2811 | zfs_ioc_inherit_prop(zfs_cmd_t *zc) | |
2812 | { | |
428870ff BB |
2813 | const char *propname = zc->zc_value; |
2814 | zfs_prop_t prop = zfs_name_to_prop(propname); | |
2815 | boolean_t received = zc->zc_cookie; | |
2816 | zprop_source_t source = (received | |
2817 | ? ZPROP_SRC_NONE /* revert to received value, if any */ | |
2818 | : ZPROP_SRC_INHERITED); /* explicitly inherit */ | |
92aceb2a | 2819 | nvlist_t *dummy; |
2820 | nvpair_t *pair; | |
2821 | zprop_type_t type; | |
2822 | int err; | |
428870ff | 2823 | |
92aceb2a | 2824 | if (!received) { |
261c013f BB |
2825 | /* |
2826 | * Only check this in the non-received case. We want to allow | |
2827 | * 'inherit -S' to revert non-inheritable properties like quota | |
2828 | * and reservation to the received or default values even though | |
2829 | * they are not considered inheritable. | |
2830 | */ | |
2831 | if (prop != ZPROP_INVAL && !zfs_prop_inheritable(prop)) | |
2832 | return (SET_ERROR(EINVAL)); | |
959f56b9 | 2833 | } |
2834 | ||
92aceb2a | 2835 | if (prop == ZPROP_INVAL) { |
2836 | if (!zfs_prop_user(propname)) | |
2837 | return (SET_ERROR(EINVAL)); | |
2838 | ||
2839 | type = PROP_TYPE_STRING; | |
2840 | } else if (prop == ZFS_PROP_VOLSIZE || prop == ZFS_PROP_VERSION) { | |
2841 | return (SET_ERROR(EINVAL)); | |
2842 | } else { | |
2843 | type = zfs_prop_get_type(prop); | |
2844 | } | |
2845 | ||
2846 | /* | |
2847 | * zfs_prop_set_special() expects properties in the form of an | |
2848 | * nvpair with type info. | |
2849 | */ | |
2850 | dummy = fnvlist_alloc(); | |
2851 | ||
2852 | switch (type) { | |
2853 | case PROP_TYPE_STRING: | |
2854 | VERIFY(0 == nvlist_add_string(dummy, propname, "")); | |
2855 | break; | |
2856 | case PROP_TYPE_NUMBER: | |
2857 | case PROP_TYPE_INDEX: | |
2858 | VERIFY(0 == nvlist_add_uint64(dummy, propname, 0)); | |
2859 | break; | |
2860 | default: | |
2861 | err = SET_ERROR(EINVAL); | |
2862 | goto errout; | |
2863 | } | |
2864 | ||
2865 | pair = nvlist_next_nvpair(dummy, NULL); | |
2866 | if (pair == NULL) { | |
2867 | err = SET_ERROR(EINVAL); | |
2868 | } else { | |
2869 | err = zfs_prop_set_special(zc->zc_name, source, pair); | |
2870 | if (err == -1) /* property is not "special", needs handling */ | |
2871 | err = dsl_prop_inherit(zc->zc_name, zc->zc_value, | |
2872 | source); | |
2873 | } | |
2874 | ||
2875 | errout: | |
2876 | nvlist_free(dummy); | |
2877 | return (err); | |
34dc7c2f BB |
2878 | } |
2879 | ||
2880 | static int | |
2881 | zfs_ioc_pool_set_props(zfs_cmd_t *zc) | |
2882 | { | |
2883 | nvlist_t *props; | |
2884 | spa_t *spa; | |
2885 | int error; | |
428870ff | 2886 | nvpair_t *pair; |
34dc7c2f | 2887 | |
c65aa5b2 BB |
2888 | if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, |
2889 | zc->zc_iflags, &props))) | |
34dc7c2f BB |
2890 | return (error); |
2891 | ||
d164b209 BB |
2892 | /* |
2893 | * If the only property is the configfile, then just do a spa_lookup() | |
2894 | * to handle the faulted case. | |
2895 | */ | |
428870ff BB |
2896 | pair = nvlist_next_nvpair(props, NULL); |
2897 | if (pair != NULL && strcmp(nvpair_name(pair), | |
d164b209 | 2898 | zpool_prop_to_name(ZPOOL_PROP_CACHEFILE)) == 0 && |
428870ff | 2899 | nvlist_next_nvpair(props, pair) == NULL) { |
d164b209 BB |
2900 | mutex_enter(&spa_namespace_lock); |
2901 | if ((spa = spa_lookup(zc->zc_name)) != NULL) { | |
2902 | spa_configfile_set(spa, props, B_FALSE); | |
2903 | spa_config_sync(spa, B_FALSE, B_TRUE); | |
2904 | } | |
2905 | mutex_exit(&spa_namespace_lock); | |
428870ff BB |
2906 | if (spa != NULL) { |
2907 | nvlist_free(props); | |
d164b209 | 2908 | return (0); |
428870ff | 2909 | } |
d164b209 BB |
2910 | } |
2911 | ||
34dc7c2f BB |
2912 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { |
2913 | nvlist_free(props); | |
2914 | return (error); | |
2915 | } | |
2916 | ||
2917 | error = spa_prop_set(spa, props); | |
2918 | ||
2919 | nvlist_free(props); | |
2920 | spa_close(spa, FTAG); | |
2921 | ||
2922 | return (error); | |
2923 | } | |
2924 | ||
2925 | static int | |
2926 | zfs_ioc_pool_get_props(zfs_cmd_t *zc) | |
2927 | { | |
2928 | spa_t *spa; | |
2929 | int error; | |
2930 | nvlist_t *nvp = NULL; | |
2931 | ||
d164b209 BB |
2932 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) { |
2933 | /* | |
2934 | * If the pool is faulted, there may be properties we can still | |
2935 | * get (such as altroot and cachefile), so attempt to get them | |
2936 | * anyway. | |
2937 | */ | |
2938 | mutex_enter(&spa_namespace_lock); | |
2939 | if ((spa = spa_lookup(zc->zc_name)) != NULL) | |
2940 | error = spa_prop_get(spa, &nvp); | |
2941 | mutex_exit(&spa_namespace_lock); | |
2942 | } else { | |
2943 | error = spa_prop_get(spa, &nvp); | |
2944 | spa_close(spa, FTAG); | |
2945 | } | |
34dc7c2f | 2946 | |
b8864a23 | 2947 | if (error == 0 && zc->zc_nvlist_dst != 0) |
34dc7c2f BB |
2948 | error = put_nvlist(zc, nvp); |
2949 | else | |
2e528b49 | 2950 | error = SET_ERROR(EFAULT); |
34dc7c2f | 2951 | |
d164b209 | 2952 | nvlist_free(nvp); |
34dc7c2f BB |
2953 | return (error); |
2954 | } | |
2955 | ||
34dc7c2f BB |
2956 | /* |
2957 | * inputs: | |
2958 | * zc_name name of filesystem | |
2959 | * zc_nvlist_src{_size} nvlist of delegated permissions | |
2960 | * zc_perm_action allow/unallow flag | |
2961 | * | |
2962 | * outputs: none | |
2963 | */ | |
2964 | static int | |
2965 | zfs_ioc_set_fsacl(zfs_cmd_t *zc) | |
2966 | { | |
2967 | int error; | |
2968 | nvlist_t *fsaclnv = NULL; | |
2969 | ||
2970 | if ((error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, | |
9babb374 | 2971 | zc->zc_iflags, &fsaclnv)) != 0) |
34dc7c2f BB |
2972 | return (error); |
2973 | ||
2974 | /* | |
2975 | * Verify nvlist is constructed correctly | |
2976 | */ | |
2977 | if ((error = zfs_deleg_verify_nvlist(fsaclnv)) != 0) { | |
2978 | nvlist_free(fsaclnv); | |
2e528b49 | 2979 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
2980 | } |
2981 | ||
2982 | /* | |
2983 | * If we don't have PRIV_SYS_MOUNT, then validate | |
2984 | * that user is allowed to hand out each permission in | |
2985 | * the nvlist(s) | |
2986 | */ | |
2987 | ||
2988 | error = secpolicy_zfs(CRED()); | |
13fe0198 | 2989 | if (error != 0) { |
34dc7c2f BB |
2990 | if (zc->zc_perm_action == B_FALSE) { |
2991 | error = dsl_deleg_can_allow(zc->zc_name, | |
2992 | fsaclnv, CRED()); | |
2993 | } else { | |
2994 | error = dsl_deleg_can_unallow(zc->zc_name, | |
2995 | fsaclnv, CRED()); | |
2996 | } | |
2997 | } | |
2998 | ||
2999 | if (error == 0) | |
3000 | error = dsl_deleg_set(zc->zc_name, fsaclnv, zc->zc_perm_action); | |
3001 | ||
3002 | nvlist_free(fsaclnv); | |
3003 | return (error); | |
3004 | } | |
3005 | ||
3006 | /* | |
3007 | * inputs: | |
3008 | * zc_name name of filesystem | |
3009 | * | |
3010 | * outputs: | |
3011 | * zc_nvlist_src{_size} nvlist of delegated permissions | |
3012 | */ | |
3013 | static int | |
3014 | zfs_ioc_get_fsacl(zfs_cmd_t *zc) | |
3015 | { | |
3016 | nvlist_t *nvp; | |
3017 | int error; | |
3018 | ||
3019 | if ((error = dsl_deleg_get(zc->zc_name, &nvp)) == 0) { | |
3020 | error = put_nvlist(zc, nvp); | |
3021 | nvlist_free(nvp); | |
3022 | } | |
3023 | ||
3024 | return (error); | |
3025 | } | |
3026 | ||
34dc7c2f BB |
3027 | /* ARGSUSED */ |
3028 | static void | |
3029 | zfs_create_cb(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx) | |
3030 | { | |
3031 | zfs_creat_t *zct = arg; | |
3032 | ||
3033 | zfs_create_fs(os, cr, zct->zct_zplprops, tx); | |
3034 | } | |
3035 | ||
3036 | #define ZFS_PROP_UNDEFINED ((uint64_t)-1) | |
3037 | ||
3038 | /* | |
3039 | * inputs: | |
b128c09f | 3040 | * os parent objset pointer (NULL if root fs) |
d3cc8b15 WA |
3041 | * fuids_ok fuids allowed in this version of the spa? |
3042 | * sa_ok SAs allowed in this version of the spa? | |
3043 | * createprops list of properties requested by creator | |
34dc7c2f BB |
3044 | * |
3045 | * outputs: | |
3046 | * zplprops values for the zplprops we attach to the master node object | |
b128c09f | 3047 | * is_ci true if requested file system will be purely case-insensitive |
34dc7c2f BB |
3048 | * |
3049 | * Determine the settings for utf8only, normalization and | |
3050 | * casesensitivity. Specific values may have been requested by the | |
3051 | * creator and/or we can inherit values from the parent dataset. If | |
3052 | * the file system is of too early a vintage, a creator can not | |
3053 | * request settings for these properties, even if the requested | |
3054 | * setting is the default value. We don't actually want to create dsl | |
3055 | * properties for these, so remove them from the source nvlist after | |
3056 | * processing. | |
3057 | */ | |
3058 | static int | |
9babb374 | 3059 | zfs_fill_zplprops_impl(objset_t *os, uint64_t zplver, |
428870ff BB |
3060 | boolean_t fuids_ok, boolean_t sa_ok, nvlist_t *createprops, |
3061 | nvlist_t *zplprops, boolean_t *is_ci) | |
34dc7c2f | 3062 | { |
34dc7c2f BB |
3063 | uint64_t sense = ZFS_PROP_UNDEFINED; |
3064 | uint64_t norm = ZFS_PROP_UNDEFINED; | |
3065 | uint64_t u8 = ZFS_PROP_UNDEFINED; | |
b129c659 | 3066 | int error; |
34dc7c2f BB |
3067 | |
3068 | ASSERT(zplprops != NULL); | |
3069 | ||
87a275d9 AG |
3070 | if (os != NULL && os->os_phys->os_type != DMU_OST_ZFS) |
3071 | return (SET_ERROR(EINVAL)); | |
3072 | ||
34dc7c2f BB |
3073 | /* |
3074 | * Pull out creator prop choices, if any. | |
3075 | */ | |
3076 | if (createprops) { | |
b128c09f BB |
3077 | (void) nvlist_lookup_uint64(createprops, |
3078 | zfs_prop_to_name(ZFS_PROP_VERSION), &zplver); | |
34dc7c2f BB |
3079 | (void) nvlist_lookup_uint64(createprops, |
3080 | zfs_prop_to_name(ZFS_PROP_NORMALIZE), &norm); | |
3081 | (void) nvlist_remove_all(createprops, | |
3082 | zfs_prop_to_name(ZFS_PROP_NORMALIZE)); | |
3083 | (void) nvlist_lookup_uint64(createprops, | |
3084 | zfs_prop_to_name(ZFS_PROP_UTF8ONLY), &u8); | |
3085 | (void) nvlist_remove_all(createprops, | |
3086 | zfs_prop_to_name(ZFS_PROP_UTF8ONLY)); | |
3087 | (void) nvlist_lookup_uint64(createprops, | |
3088 | zfs_prop_to_name(ZFS_PROP_CASE), &sense); | |
3089 | (void) nvlist_remove_all(createprops, | |
3090 | zfs_prop_to_name(ZFS_PROP_CASE)); | |
3091 | } | |
3092 | ||
3093 | /* | |
b128c09f BB |
3094 | * If the zpl version requested is whacky or the file system |
3095 | * or pool is version is too "young" to support normalization | |
3096 | * and the creator tried to set a value for one of the props, | |
3097 | * error out. | |
34dc7c2f | 3098 | */ |
b128c09f BB |
3099 | if ((zplver < ZPL_VERSION_INITIAL || zplver > ZPL_VERSION) || |
3100 | (zplver >= ZPL_VERSION_FUID && !fuids_ok) || | |
428870ff | 3101 | (zplver >= ZPL_VERSION_SA && !sa_ok) || |
b128c09f | 3102 | (zplver < ZPL_VERSION_NORMALIZATION && |
34dc7c2f | 3103 | (norm != ZFS_PROP_UNDEFINED || u8 != ZFS_PROP_UNDEFINED || |
b128c09f | 3104 | sense != ZFS_PROP_UNDEFINED))) |
2e528b49 | 3105 | return (SET_ERROR(ENOTSUP)); |
34dc7c2f BB |
3106 | |
3107 | /* | |
3108 | * Put the version in the zplprops | |
3109 | */ | |
3110 | VERIFY(nvlist_add_uint64(zplprops, | |
3111 | zfs_prop_to_name(ZFS_PROP_VERSION), zplver) == 0); | |
3112 | ||
b129c659 MM |
3113 | if (norm == ZFS_PROP_UNDEFINED && |
3114 | (error = zfs_get_zplprop(os, ZFS_PROP_NORMALIZE, &norm)) != 0) | |
3115 | return (error); | |
34dc7c2f BB |
3116 | VERIFY(nvlist_add_uint64(zplprops, |
3117 | zfs_prop_to_name(ZFS_PROP_NORMALIZE), norm) == 0); | |
3118 | ||
3119 | /* | |
3120 | * If we're normalizing, names must always be valid UTF-8 strings. | |
3121 | */ | |
3122 | if (norm) | |
3123 | u8 = 1; | |
b129c659 MM |
3124 | if (u8 == ZFS_PROP_UNDEFINED && |
3125 | (error = zfs_get_zplprop(os, ZFS_PROP_UTF8ONLY, &u8)) != 0) | |
3126 | return (error); | |
34dc7c2f BB |
3127 | VERIFY(nvlist_add_uint64(zplprops, |
3128 | zfs_prop_to_name(ZFS_PROP_UTF8ONLY), u8) == 0); | |
3129 | ||
b129c659 MM |
3130 | if (sense == ZFS_PROP_UNDEFINED && |
3131 | (error = zfs_get_zplprop(os, ZFS_PROP_CASE, &sense)) != 0) | |
3132 | return (error); | |
34dc7c2f BB |
3133 | VERIFY(nvlist_add_uint64(zplprops, |
3134 | zfs_prop_to_name(ZFS_PROP_CASE), sense) == 0); | |
3135 | ||
3136 | if (is_ci) | |
3137 | *is_ci = (sense == ZFS_CASE_INSENSITIVE); | |
3138 | ||
34dc7c2f BB |
3139 | return (0); |
3140 | } | |
3141 | ||
b128c09f BB |
3142 | static int |
3143 | zfs_fill_zplprops(const char *dataset, nvlist_t *createprops, | |
3144 | nvlist_t *zplprops, boolean_t *is_ci) | |
3145 | { | |
428870ff | 3146 | boolean_t fuids_ok, sa_ok; |
b128c09f BB |
3147 | uint64_t zplver = ZPL_VERSION; |
3148 | objset_t *os = NULL; | |
eca7b760 | 3149 | char parentname[ZFS_MAX_DATASET_NAME_LEN]; |
b128c09f | 3150 | char *cp; |
428870ff BB |
3151 | spa_t *spa; |
3152 | uint64_t spa_vers; | |
b128c09f BB |
3153 | int error; |
3154 | ||
3155 | (void) strlcpy(parentname, dataset, sizeof (parentname)); | |
3156 | cp = strrchr(parentname, '/'); | |
3157 | ASSERT(cp != NULL); | |
3158 | cp[0] = '\0'; | |
3159 | ||
428870ff BB |
3160 | if ((error = spa_open(dataset, &spa, FTAG)) != 0) |
3161 | return (error); | |
3162 | ||
3163 | spa_vers = spa_version(spa); | |
3164 | spa_close(spa, FTAG); | |
3165 | ||
3166 | zplver = zfs_zpl_version_map(spa_vers); | |
3167 | fuids_ok = (zplver >= ZPL_VERSION_FUID); | |
3168 | sa_ok = (zplver >= ZPL_VERSION_SA); | |
b128c09f BB |
3169 | |
3170 | /* | |
3171 | * Open parent object set so we can inherit zplprop values. | |
3172 | */ | |
428870ff | 3173 | if ((error = dmu_objset_hold(parentname, FTAG, &os)) != 0) |
b128c09f BB |
3174 | return (error); |
3175 | ||
428870ff | 3176 | error = zfs_fill_zplprops_impl(os, zplver, fuids_ok, sa_ok, createprops, |
b128c09f | 3177 | zplprops, is_ci); |
428870ff | 3178 | dmu_objset_rele(os, FTAG); |
b128c09f BB |
3179 | return (error); |
3180 | } | |
3181 | ||
3182 | static int | |
3183 | zfs_fill_zplprops_root(uint64_t spa_vers, nvlist_t *createprops, | |
3184 | nvlist_t *zplprops, boolean_t *is_ci) | |
3185 | { | |
428870ff BB |
3186 | boolean_t fuids_ok; |
3187 | boolean_t sa_ok; | |
b128c09f BB |
3188 | uint64_t zplver = ZPL_VERSION; |
3189 | int error; | |
3190 | ||
428870ff BB |
3191 | zplver = zfs_zpl_version_map(spa_vers); |
3192 | fuids_ok = (zplver >= ZPL_VERSION_FUID); | |
3193 | sa_ok = (zplver >= ZPL_VERSION_SA); | |
b128c09f | 3194 | |
428870ff BB |
3195 | error = zfs_fill_zplprops_impl(NULL, zplver, fuids_ok, sa_ok, |
3196 | createprops, zplprops, is_ci); | |
b128c09f BB |
3197 | return (error); |
3198 | } | |
3199 | ||
34dc7c2f | 3200 | /* |
6f1ffb06 MA |
3201 | * innvl: { |
3202 | * "type" -> dmu_objset_type_t (int32) | |
3203 | * (optional) "props" -> { prop -> value } | |
b5256303 TC |
3204 | * (optional) "hidden_args" -> { "wkeydata" -> value } |
3205 | * raw uint8_t array of encryption wrapping key data (32 bytes) | |
6f1ffb06 | 3206 | * } |
34dc7c2f | 3207 | * |
6f1ffb06 | 3208 | * outnvl: propname -> error code (int32) |
34dc7c2f BB |
3209 | */ |
3210 | static int | |
6f1ffb06 | 3211 | zfs_ioc_create(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) |
34dc7c2f | 3212 | { |
34dc7c2f | 3213 | int error = 0; |
6f1ffb06 | 3214 | zfs_creat_t zct = { 0 }; |
34dc7c2f | 3215 | nvlist_t *nvprops = NULL; |
b5256303 | 3216 | nvlist_t *hidden_args = NULL; |
34dc7c2f | 3217 | void (*cbfunc)(objset_t *os, void *arg, cred_t *cr, dmu_tx_t *tx); |
6f1ffb06 MA |
3218 | int32_t type32; |
3219 | dmu_objset_type_t type; | |
3220 | boolean_t is_insensitive = B_FALSE; | |
b5256303 | 3221 | dsl_crypto_params_t *dcp = NULL; |
34dc7c2f | 3222 | |
6f1ffb06 | 3223 | if (nvlist_lookup_int32(innvl, "type", &type32) != 0) |
2e528b49 | 3224 | return (SET_ERROR(EINVAL)); |
6f1ffb06 MA |
3225 | type = type32; |
3226 | (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); | |
b5256303 | 3227 | (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args); |
34dc7c2f | 3228 | |
6f1ffb06 | 3229 | switch (type) { |
34dc7c2f BB |
3230 | case DMU_OST_ZFS: |
3231 | cbfunc = zfs_create_cb; | |
3232 | break; | |
3233 | ||
3234 | case DMU_OST_ZVOL: | |
3235 | cbfunc = zvol_create_cb; | |
3236 | break; | |
3237 | ||
3238 | default: | |
3239 | cbfunc = NULL; | |
3240 | break; | |
3241 | } | |
6f1ffb06 MA |
3242 | if (strchr(fsname, '@') || |
3243 | strchr(fsname, '%')) | |
2e528b49 | 3244 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 3245 | |
34dc7c2f BB |
3246 | zct.zct_props = nvprops; |
3247 | ||
6f1ffb06 | 3248 | if (cbfunc == NULL) |
2e528b49 | 3249 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 3250 | |
6f1ffb06 MA |
3251 | if (type == DMU_OST_ZVOL) { |
3252 | uint64_t volsize, volblocksize; | |
34dc7c2f | 3253 | |
6f1ffb06 | 3254 | if (nvprops == NULL) |
2e528b49 | 3255 | return (SET_ERROR(EINVAL)); |
6f1ffb06 MA |
3256 | if (nvlist_lookup_uint64(nvprops, |
3257 | zfs_prop_to_name(ZFS_PROP_VOLSIZE), &volsize) != 0) | |
2e528b49 | 3258 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 3259 | |
6f1ffb06 MA |
3260 | if ((error = nvlist_lookup_uint64(nvprops, |
3261 | zfs_prop_to_name(ZFS_PROP_VOLBLOCKSIZE), | |
3262 | &volblocksize)) != 0 && error != ENOENT) | |
2e528b49 | 3263 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 3264 | |
6f1ffb06 MA |
3265 | if (error != 0) |
3266 | volblocksize = zfs_prop_default_numeric( | |
3267 | ZFS_PROP_VOLBLOCKSIZE); | |
34dc7c2f | 3268 | |
4cb7b9c5 | 3269 | if ((error = zvol_check_volblocksize(fsname, |
6f1ffb06 MA |
3270 | volblocksize)) != 0 || |
3271 | (error = zvol_check_volsize(volsize, | |
3272 | volblocksize)) != 0) | |
3273 | return (error); | |
3274 | } else if (type == DMU_OST_ZFS) { | |
3275 | int error; | |
34dc7c2f | 3276 | |
6f1ffb06 MA |
3277 | /* |
3278 | * We have to have normalization and | |
3279 | * case-folding flags correct when we do the | |
3280 | * file system creation, so go figure them out | |
3281 | * now. | |
3282 | */ | |
3283 | VERIFY(nvlist_alloc(&zct.zct_zplprops, | |
3284 | NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
3285 | error = zfs_fill_zplprops(fsname, nvprops, | |
3286 | zct.zct_zplprops, &is_insensitive); | |
3287 | if (error != 0) { | |
3288 | nvlist_free(zct.zct_zplprops); | |
3289 | return (error); | |
34dc7c2f | 3290 | } |
34dc7c2f BB |
3291 | } |
3292 | ||
b5256303 TC |
3293 | error = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, nvprops, |
3294 | hidden_args, &dcp); | |
3295 | if (error != 0) { | |
3296 | nvlist_free(zct.zct_zplprops); | |
3297 | return (error); | |
3298 | } | |
3299 | ||
6f1ffb06 | 3300 | error = dmu_objset_create(fsname, type, |
b5256303 TC |
3301 | is_insensitive ? DS_FLAG_CI_DATASET : 0, dcp, cbfunc, &zct); |
3302 | ||
6f1ffb06 | 3303 | nvlist_free(zct.zct_zplprops); |
b5256303 | 3304 | dsl_crypto_params_free(dcp, !!error); |
6f1ffb06 | 3305 | |
34dc7c2f BB |
3306 | /* |
3307 | * It would be nice to do this atomically. | |
3308 | */ | |
3309 | if (error == 0) { | |
6f1ffb06 MA |
3310 | error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, |
3311 | nvprops, outnvl); | |
f74b821a BB |
3312 | if (error != 0) { |
3313 | spa_t *spa; | |
3314 | int error2; | |
3315 | ||
3316 | /* | |
3317 | * Volumes will return EBUSY and cannot be destroyed | |
3318 | * until all asynchronous minor handling has completed. | |
3319 | * Wait for the spa_zvol_taskq to drain then retry. | |
3320 | */ | |
3321 | error2 = dsl_destroy_head(fsname); | |
3322 | while ((error2 == EBUSY) && (type == DMU_OST_ZVOL)) { | |
3323 | error2 = spa_open(fsname, &spa, FTAG); | |
3324 | if (error2 == 0) { | |
3325 | taskq_wait(spa->spa_zvol_taskq); | |
3326 | spa_close(spa, FTAG); | |
3327 | } | |
3328 | error2 = dsl_destroy_head(fsname); | |
3329 | } | |
3330 | } | |
34dc7c2f | 3331 | } |
34dc7c2f BB |
3332 | return (error); |
3333 | } | |
3334 | ||
3335 | /* | |
6f1ffb06 MA |
3336 | * innvl: { |
3337 | * "origin" -> name of origin snapshot | |
3338 | * (optional) "props" -> { prop -> value } | |
b5256303 TC |
3339 | * (optional) "hidden_args" -> { "wkeydata" -> value } |
3340 | * raw uint8_t array of encryption wrapping key data (32 bytes) | |
6f1ffb06 | 3341 | * } |
34dc7c2f | 3342 | * |
428870ff | 3343 | * outputs: |
6f1ffb06 | 3344 | * outnvl: propname -> error code (int32) |
34dc7c2f BB |
3345 | */ |
3346 | static int | |
6f1ffb06 | 3347 | zfs_ioc_clone(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) |
34dc7c2f | 3348 | { |
6f1ffb06 | 3349 | int error = 0; |
b128c09f | 3350 | nvlist_t *nvprops = NULL; |
6f1ffb06 | 3351 | char *origin_name; |
b128c09f | 3352 | |
6f1ffb06 | 3353 | if (nvlist_lookup_string(innvl, "origin", &origin_name) != 0) |
2e528b49 | 3354 | return (SET_ERROR(EINVAL)); |
6f1ffb06 | 3355 | (void) nvlist_lookup_nvlist(innvl, "props", &nvprops); |
b128c09f | 3356 | |
6f1ffb06 MA |
3357 | if (strchr(fsname, '@') || |
3358 | strchr(fsname, '%')) | |
2e528b49 | 3359 | return (SET_ERROR(EINVAL)); |
6f1ffb06 MA |
3360 | |
3361 | if (dataset_namecheck(origin_name, NULL, NULL) != 0) | |
2e528b49 | 3362 | return (SET_ERROR(EINVAL)); |
b5256303 | 3363 | |
13fe0198 | 3364 | error = dmu_objset_clone(fsname, origin_name); |
b128c09f | 3365 | |
6f1ffb06 MA |
3366 | /* |
3367 | * It would be nice to do this atomically. | |
3368 | */ | |
3369 | if (error == 0) { | |
3370 | error = zfs_set_prop_nvlist(fsname, ZPROP_SRC_LOCAL, | |
3371 | nvprops, outnvl); | |
3372 | if (error != 0) | |
13fe0198 | 3373 | (void) dsl_destroy_head(fsname); |
b128c09f | 3374 | } |
6f1ffb06 MA |
3375 | return (error); |
3376 | } | |
9babb374 | 3377 | |
6f1ffb06 MA |
3378 | /* |
3379 | * innvl: { | |
3380 | * "snaps" -> { snapshot1, snapshot2 } | |
3381 | * (optional) "props" -> { prop -> value (string) } | |
3382 | * } | |
3383 | * | |
3384 | * outnvl: snapshot -> error code (int32) | |
6f1ffb06 MA |
3385 | */ |
3386 | static int | |
3387 | zfs_ioc_snapshot(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) | |
3388 | { | |
3389 | nvlist_t *snaps; | |
3390 | nvlist_t *props = NULL; | |
3391 | int error, poollen; | |
3392 | nvpair_t *pair, *pair2; | |
9babb374 | 3393 | |
6f1ffb06 MA |
3394 | (void) nvlist_lookup_nvlist(innvl, "props", &props); |
3395 | if ((error = zfs_check_userprops(poolname, props)) != 0) | |
3396 | return (error); | |
3397 | ||
3398 | if (!nvlist_empty(props) && | |
3399 | zfs_earlier_version(poolname, SPA_VERSION_SNAP_PROPS)) | |
2e528b49 | 3400 | return (SET_ERROR(ENOTSUP)); |
6f1ffb06 MA |
3401 | |
3402 | if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) | |
2e528b49 | 3403 | return (SET_ERROR(EINVAL)); |
6f1ffb06 MA |
3404 | poollen = strlen(poolname); |
3405 | for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; | |
3406 | pair = nvlist_next_nvpair(snaps, pair)) { | |
3407 | const char *name = nvpair_name(pair); | |
3408 | const char *cp = strchr(name, '@'); | |
3409 | ||
3410 | /* | |
3411 | * The snap name must contain an @, and the part after it must | |
3412 | * contain only valid characters. | |
3413 | */ | |
da536844 MA |
3414 | if (cp == NULL || |
3415 | zfs_component_namecheck(cp + 1, NULL, NULL) != 0) | |
2e528b49 | 3416 | return (SET_ERROR(EINVAL)); |
6f1ffb06 MA |
3417 | |
3418 | /* | |
3419 | * The snap must be in the specified pool. | |
3420 | */ | |
3421 | if (strncmp(name, poolname, poollen) != 0 || | |
3422 | (name[poollen] != '/' && name[poollen] != '@')) | |
2e528b49 | 3423 | return (SET_ERROR(EXDEV)); |
6f1ffb06 MA |
3424 | |
3425 | /* This must be the only snap of this fs. */ | |
3426 | for (pair2 = nvlist_next_nvpair(snaps, pair); | |
3427 | pair2 != NULL; pair2 = nvlist_next_nvpair(snaps, pair2)) { | |
3428 | if (strncmp(name, nvpair_name(pair2), cp - name + 1) | |
3429 | == 0) { | |
2e528b49 | 3430 | return (SET_ERROR(EXDEV)); |
6f1ffb06 MA |
3431 | } |
3432 | } | |
3433 | } | |
3434 | ||
13fe0198 | 3435 | error = dsl_dataset_snapshot(snaps, props, outnvl); |
ba6a2402 | 3436 | |
6f1ffb06 MA |
3437 | return (error); |
3438 | } | |
3439 | ||
3440 | /* | |
3441 | * innvl: "message" -> string | |
3442 | */ | |
3443 | /* ARGSUSED */ | |
3444 | static int | |
3445 | zfs_ioc_log_history(const char *unused, nvlist_t *innvl, nvlist_t *outnvl) | |
3446 | { | |
3447 | char *message; | |
3448 | spa_t *spa; | |
3449 | int error; | |
3450 | char *poolname; | |
3451 | ||
3452 | /* | |
3453 | * The poolname in the ioctl is not set, we get it from the TSD, | |
3454 | * which was set at the end of the last successful ioctl that allows | |
3455 | * logging. The secpolicy func already checked that it is set. | |
3456 | * Only one log ioctl is allowed after each successful ioctl, so | |
3457 | * we clear the TSD here. | |
3458 | */ | |
3459 | poolname = tsd_get(zfs_allow_log_key); | |
9f3d1407 | 3460 | if (poolname == NULL) |
3461 | return (SET_ERROR(EINVAL)); | |
6f1ffb06 MA |
3462 | (void) tsd_set(zfs_allow_log_key, NULL); |
3463 | error = spa_open(poolname, &spa, FTAG); | |
3464 | strfree(poolname); | |
3465 | if (error != 0) | |
3466 | return (error); | |
3467 | ||
3468 | if (nvlist_lookup_string(innvl, "message", &message) != 0) { | |
3469 | spa_close(spa, FTAG); | |
2e528b49 | 3470 | return (SET_ERROR(EINVAL)); |
6f1ffb06 MA |
3471 | } |
3472 | ||
3473 | if (spa_version(spa) < SPA_VERSION_ZPOOL_HISTORY) { | |
3474 | spa_close(spa, FTAG); | |
2e528b49 | 3475 | return (SET_ERROR(ENOTSUP)); |
6f1ffb06 MA |
3476 | } |
3477 | ||
3478 | error = spa_history_log(spa, message); | |
3479 | spa_close(spa, FTAG); | |
b128c09f | 3480 | return (error); |
34dc7c2f BB |
3481 | } |
3482 | ||
ebe7e575 | 3483 | /* |
13fe0198 MA |
3484 | * The dp_config_rwlock must not be held when calling this, because the |
3485 | * unmount may need to write out data. | |
3486 | * | |
3487 | * This function is best-effort. Callers must deal gracefully if it | |
3488 | * remains mounted (or is remounted after this call). | |
d09f25dc | 3489 | * |
278bee93 BB |
3490 | * Returns 0 if the argument is not a snapshot, or it is not currently a |
3491 | * filesystem, or we were able to unmount it. Returns error code otherwise. | |
ebe7e575 | 3492 | */ |
d09f25dc | 3493 | int |
13fe0198 | 3494 | zfs_unmount_snap(const char *snapname) |
34dc7c2f | 3495 | { |
278bee93 | 3496 | int err; |
34dc7c2f | 3497 | |
278bee93 | 3498 | if (strchr(snapname, '@') == NULL) |
d09f25dc | 3499 | return (0); |
34dc7c2f | 3500 | |
278bee93 BB |
3501 | err = zfsctl_snapshot_unmount((char *)snapname, MNT_FORCE); |
3502 | if (err != 0 && err != ENOENT) | |
3503 | return (SET_ERROR(err)); | |
ebe7e575 | 3504 | |
d09f25dc | 3505 | return (0); |
13fe0198 MA |
3506 | } |
3507 | ||
3508 | /* ARGSUSED */ | |
3509 | static int | |
3510 | zfs_unmount_snap_cb(const char *snapname, void *arg) | |
3511 | { | |
d09f25dc | 3512 | return (zfs_unmount_snap(snapname)); |
13fe0198 MA |
3513 | } |
3514 | ||
3515 | /* | |
3516 | * When a clone is destroyed, its origin may also need to be destroyed, | |
3517 | * in which case it must be unmounted. This routine will do that unmount | |
3518 | * if necessary. | |
3519 | */ | |
3520 | void | |
3521 | zfs_destroy_unmount_origin(const char *fsname) | |
3522 | { | |
3523 | int error; | |
3524 | objset_t *os; | |
3525 | dsl_dataset_t *ds; | |
3526 | ||
3527 | error = dmu_objset_hold(fsname, FTAG, &os); | |
3528 | if (error != 0) | |
3529 | return; | |
3530 | ds = dmu_objset_ds(os); | |
3531 | if (dsl_dir_is_clone(ds->ds_dir) && DS_IS_DEFER_DESTROY(ds->ds_prev)) { | |
eca7b760 | 3532 | char originname[ZFS_MAX_DATASET_NAME_LEN]; |
13fe0198 MA |
3533 | dsl_dataset_name(ds->ds_prev, originname); |
3534 | dmu_objset_rele(os, FTAG); | |
d09f25dc | 3535 | (void) zfs_unmount_snap(originname); |
13fe0198 MA |
3536 | } else { |
3537 | dmu_objset_rele(os, FTAG); | |
3538 | } | |
34dc7c2f BB |
3539 | } |
3540 | ||
3541 | /* | |
6f1ffb06 MA |
3542 | * innvl: { |
3543 | * "snaps" -> { snapshot1, snapshot2 } | |
3544 | * (optional boolean) "defer" | |
3545 | * } | |
34dc7c2f | 3546 | * |
6f1ffb06 | 3547 | * outnvl: snapshot -> error code (int32) |
34dc7c2f | 3548 | */ |
da536844 | 3549 | /* ARGSUSED */ |
34dc7c2f | 3550 | static int |
6f1ffb06 | 3551 | zfs_ioc_destroy_snaps(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) |
34dc7c2f | 3552 | { |
6f1ffb06 | 3553 | nvlist_t *snaps; |
330d06f9 | 3554 | nvpair_t *pair; |
6f1ffb06 | 3555 | boolean_t defer; |
34dc7c2f | 3556 | |
6f1ffb06 | 3557 | if (nvlist_lookup_nvlist(innvl, "snaps", &snaps) != 0) |
2e528b49 | 3558 | return (SET_ERROR(EINVAL)); |
6f1ffb06 | 3559 | defer = nvlist_exists(innvl, "defer"); |
330d06f9 | 3560 | |
6f1ffb06 MA |
3561 | for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL; |
3562 | pair = nvlist_next_nvpair(snaps, pair)) { | |
da536844 | 3563 | (void) zfs_unmount_snap(nvpair_name(pair)); |
da536844 MA |
3564 | } |
3565 | ||
3566 | return (dsl_destroy_snapshots_nvl(snaps, defer, outnvl)); | |
3567 | } | |
3568 | ||
3569 | /* | |
3570 | * Create bookmarks. Bookmark names are of the form <fs>#<bmark>. | |
3571 | * All bookmarks must be in the same pool. | |
3572 | * | |
3573 | * innvl: { | |
3574 | * bookmark1 -> snapshot1, bookmark2 -> snapshot2 | |
3575 | * } | |
3576 | * | |
3577 | * outnvl: bookmark -> error code (int32) | |
3578 | * | |
3579 | */ | |
3580 | /* ARGSUSED */ | |
3581 | static int | |
3582 | zfs_ioc_bookmark(const char *poolname, nvlist_t *innvl, nvlist_t *outnvl) | |
3583 | { | |
3584 | nvpair_t *pair, *pair2; | |
3585 | ||
3586 | for (pair = nvlist_next_nvpair(innvl, NULL); | |
3587 | pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { | |
3588 | char *snap_name; | |
3589 | ||
3590 | /* | |
3591 | * Verify the snapshot argument. | |
3592 | */ | |
3593 | if (nvpair_value_string(pair, &snap_name) != 0) | |
3594 | return (SET_ERROR(EINVAL)); | |
3595 | ||
3596 | ||
3597 | /* Verify that the keys (bookmarks) are unique */ | |
3598 | for (pair2 = nvlist_next_nvpair(innvl, pair); | |
3599 | pair2 != NULL; pair2 = nvlist_next_nvpair(innvl, pair2)) { | |
3600 | if (strcmp(nvpair_name(pair), nvpair_name(pair2)) == 0) | |
3601 | return (SET_ERROR(EINVAL)); | |
3602 | } | |
3603 | } | |
3604 | ||
3605 | return (dsl_bookmark_create(innvl, outnvl)); | |
3606 | } | |
3607 | ||
3608 | /* | |
3609 | * innvl: { | |
3610 | * property 1, property 2, ... | |
3611 | * } | |
3612 | * | |
3613 | * outnvl: { | |
3614 | * bookmark name 1 -> { property 1, property 2, ... }, | |
3615 | * bookmark name 2 -> { property 1, property 2, ... } | |
3616 | * } | |
3617 | * | |
3618 | */ | |
3619 | static int | |
3620 | zfs_ioc_get_bookmarks(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) | |
3621 | { | |
3622 | return (dsl_get_bookmarks(fsname, innvl, outnvl)); | |
3623 | } | |
3624 | ||
3625 | /* | |
3626 | * innvl: { | |
3627 | * bookmark name 1, bookmark name 2 | |
3628 | * } | |
3629 | * | |
3630 | * outnvl: bookmark -> error code (int32) | |
3631 | * | |
3632 | */ | |
3633 | static int | |
3634 | zfs_ioc_destroy_bookmarks(const char *poolname, nvlist_t *innvl, | |
3635 | nvlist_t *outnvl) | |
3636 | { | |
3637 | int error, poollen; | |
3638 | nvpair_t *pair; | |
3639 | ||
3640 | poollen = strlen(poolname); | |
3641 | for (pair = nvlist_next_nvpair(innvl, NULL); | |
3642 | pair != NULL; pair = nvlist_next_nvpair(innvl, pair)) { | |
330d06f9 | 3643 | const char *name = nvpair_name(pair); |
da536844 | 3644 | const char *cp = strchr(name, '#'); |
6f1ffb06 | 3645 | |
330d06f9 | 3646 | /* |
da536844 MA |
3647 | * The bookmark name must contain an #, and the part after it |
3648 | * must contain only valid characters. | |
3649 | */ | |
3650 | if (cp == NULL || | |
3651 | zfs_component_namecheck(cp + 1, NULL, NULL) != 0) | |
3652 | return (SET_ERROR(EINVAL)); | |
3653 | ||
3654 | /* | |
3655 | * The bookmark must be in the specified pool. | |
330d06f9 | 3656 | */ |
6f1ffb06 | 3657 | if (strncmp(name, poolname, poollen) != 0 || |
da536844 | 3658 | (name[poollen] != '/' && name[poollen] != '#')) |
2e528b49 | 3659 | return (SET_ERROR(EXDEV)); |
330d06f9 MA |
3660 | } |
3661 | ||
da536844 MA |
3662 | error = dsl_bookmark_destroy(innvl, outnvl); |
3663 | return (error); | |
34dc7c2f BB |
3664 | } |
3665 | ||
3666 | /* | |
3667 | * inputs: | |
3668 | * zc_name name of dataset to destroy | |
3669 | * zc_objset_type type of objset | |
45d1cae3 | 3670 | * zc_defer_destroy mark for deferred destroy |
34dc7c2f BB |
3671 | * |
3672 | * outputs: none | |
3673 | */ | |
3674 | static int | |
3675 | zfs_ioc_destroy(zfs_cmd_t *zc) | |
3676 | { | |
428870ff | 3677 | int err; |
d09f25dc WA |
3678 | |
3679 | if (zc->zc_objset_type == DMU_OST_ZFS) { | |
3680 | err = zfs_unmount_snap(zc->zc_name); | |
3681 | if (err != 0) | |
3682 | return (err); | |
3683 | } | |
34dc7c2f | 3684 | |
1b87e0f5 | 3685 | if (strchr(zc->zc_name, '@')) { |
13fe0198 | 3686 | err = dsl_destroy_snapshot(zc->zc_name, zc->zc_defer_destroy); |
1b87e0f5 | 3687 | } else { |
13fe0198 | 3688 | err = dsl_destroy_head(zc->zc_name); |
1b87e0f5 RS |
3689 | if (err == EEXIST) { |
3690 | /* | |
3691 | * It is possible that the given DS may have | |
3692 | * hidden child (%recv) datasets - "leftovers" | |
3693 | * resulting from the previously interrupted | |
3694 | * 'zfs receive'. | |
3695 | * | |
3696 | * 6 extra bytes for /%recv | |
3697 | */ | |
3698 | char namebuf[ZFS_MAX_DATASET_NAME_LEN + 6]; | |
3699 | ||
682ce104 TH |
3700 | if (snprintf(namebuf, sizeof (namebuf), "%s/%s", |
3701 | zc->zc_name, recv_clone_name) >= | |
3702 | sizeof (namebuf)) | |
3703 | return (SET_ERROR(EINVAL)); | |
1b87e0f5 RS |
3704 | |
3705 | /* | |
3706 | * Try to remove the hidden child (%recv) and after | |
3707 | * that try to remove the target dataset. | |
3708 | * If the hidden child (%recv) does not exist | |
3709 | * the original error (EEXIST) will be returned | |
3710 | */ | |
3711 | err = dsl_destroy_head(namebuf); | |
3712 | if (err == 0) | |
3713 | err = dsl_destroy_head(zc->zc_name); | |
3714 | else if (err == ENOENT) | |
ecb2b7dc | 3715 | err = SET_ERROR(EEXIST); |
1b87e0f5 RS |
3716 | } |
3717 | } | |
a0bd735a | 3718 | |
428870ff | 3719 | return (err); |
34dc7c2f BB |
3720 | } |
3721 | ||
3722 | /* | |
46ba1e59 | 3723 | * fsname is name of dataset to rollback (to most recent snapshot) |
34dc7c2f | 3724 | * |
8ca78ab0 | 3725 | * innvl may contain name of expected target snapshot |
46ba1e59 MA |
3726 | * |
3727 | * outnvl: "target" -> name of most recent snapshot | |
3728 | * } | |
34dc7c2f | 3729 | */ |
46ba1e59 | 3730 | /* ARGSUSED */ |
34dc7c2f | 3731 | static int |
8ca78ab0 | 3732 | zfs_ioc_rollback(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) |
34dc7c2f | 3733 | { |
0037b49e | 3734 | zfsvfs_t *zfsvfs; |
040dab99 | 3735 | zvol_state_t *zv; |
8ca78ab0 | 3736 | char *target = NULL; |
13fe0198 | 3737 | int error; |
34dc7c2f | 3738 | |
8ca78ab0 AG |
3739 | (void) nvlist_lookup_string(innvl, "target", &target); |
3740 | if (target != NULL) { | |
3741 | int fslen = strlen(fsname); | |
3742 | ||
3743 | if (strncmp(fsname, target, fslen) != 0) | |
3744 | return (SET_ERROR(EINVAL)); | |
3745 | if (target[fslen] != '@') | |
3746 | return (SET_ERROR(EINVAL)); | |
3747 | } | |
3748 | ||
f298b24d | 3749 | if (getzfsvfs(fsname, &zfsvfs) == 0) { |
ec923db2 GM |
3750 | dsl_dataset_t *ds; |
3751 | ||
0037b49e BB |
3752 | ds = dmu_objset_ds(zfsvfs->z_os); |
3753 | error = zfs_suspend_fs(zfsvfs); | |
34dc7c2f BB |
3754 | if (error == 0) { |
3755 | int resume_err; | |
3756 | ||
8ca78ab0 AG |
3757 | error = dsl_dataset_rollback(fsname, target, zfsvfs, |
3758 | outnvl); | |
0037b49e | 3759 | resume_err = zfs_resume_fs(zfsvfs, ds); |
34dc7c2f | 3760 | error = error ? error : resume_err; |
34dc7c2f | 3761 | } |
0037b49e | 3762 | deactivate_super(zfsvfs->z_sb); |
040dab99 | 3763 | } else if ((zv = zvol_suspend(fsname)) != NULL) { |
8ca78ab0 AG |
3764 | error = dsl_dataset_rollback(fsname, target, zvol_tag(zv), |
3765 | outnvl); | |
040dab99 | 3766 | zvol_resume(zv); |
34dc7c2f | 3767 | } else { |
8ca78ab0 | 3768 | error = dsl_dataset_rollback(fsname, target, NULL, outnvl); |
34dc7c2f | 3769 | } |
13fe0198 MA |
3770 | return (error); |
3771 | } | |
34dc7c2f | 3772 | |
13fe0198 MA |
3773 | static int |
3774 | recursive_unmount(const char *fsname, void *arg) | |
3775 | { | |
3776 | const char *snapname = arg; | |
3777 | char *fullname; | |
00fcdee1 | 3778 | int error; |
428870ff | 3779 | |
13fe0198 | 3780 | fullname = kmem_asprintf("%s@%s", fsname, snapname); |
00fcdee1 | 3781 | error = zfs_unmount_snap(fullname); |
13fe0198 | 3782 | strfree(fullname); |
00fcdee1 AV |
3783 | |
3784 | return (error); | |
34dc7c2f BB |
3785 | } |
3786 | ||
3787 | /* | |
3788 | * inputs: | |
3789 | * zc_name old name of dataset | |
3790 | * zc_value new name of dataset | |
3791 | * zc_cookie recursive flag (only valid for snapshots) | |
3792 | * | |
3793 | * outputs: none | |
3794 | */ | |
3795 | static int | |
3796 | zfs_ioc_rename(zfs_cmd_t *zc) | |
3797 | { | |
3798 | boolean_t recursive = zc->zc_cookie & 1; | |
13fe0198 | 3799 | char *at; |
34dc7c2f | 3800 | |
650258d7 | 3801 | /* "zfs rename" from and to ...%recv datasets should both fail */ |
3802 | zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; | |
34dc7c2f | 3803 | zc->zc_value[sizeof (zc->zc_value) - 1] = '\0'; |
650258d7 | 3804 | if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 || |
3805 | dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || | |
3806 | strchr(zc->zc_name, '%') || strchr(zc->zc_value, '%')) | |
2e528b49 | 3807 | return (SET_ERROR(EINVAL)); |
34dc7c2f | 3808 | |
13fe0198 MA |
3809 | at = strchr(zc->zc_name, '@'); |
3810 | if (at != NULL) { | |
3811 | /* snaps must be in same fs */ | |
9554185d SH |
3812 | int error; |
3813 | ||
13fe0198 | 3814 | if (strncmp(zc->zc_name, zc->zc_value, at - zc->zc_name + 1)) |
2e528b49 | 3815 | return (SET_ERROR(EXDEV)); |
13fe0198 MA |
3816 | *at = '\0'; |
3817 | if (zc->zc_objset_type == DMU_OST_ZFS) { | |
9554185d | 3818 | error = dmu_objset_find(zc->zc_name, |
13fe0198 MA |
3819 | recursive_unmount, at + 1, |
3820 | recursive ? DS_FIND_CHILDREN : 0); | |
9554185d SH |
3821 | if (error != 0) { |
3822 | *at = '@'; | |
13fe0198 | 3823 | return (error); |
9554185d | 3824 | } |
13fe0198 | 3825 | } |
9554185d SH |
3826 | error = dsl_dataset_rename_snapshot(zc->zc_name, |
3827 | at + 1, strchr(zc->zc_value, '@') + 1, recursive); | |
3828 | *at = '@'; | |
3829 | ||
3830 | return (error); | |
13fe0198 | 3831 | } else { |
ba6a2402 | 3832 | return (dsl_dir_rename(zc->zc_name, zc->zc_value)); |
95c73795 | 3833 | } |
34dc7c2f BB |
3834 | } |
3835 | ||
428870ff BB |
3836 | static int |
3837 | zfs_check_settable(const char *dsname, nvpair_t *pair, cred_t *cr) | |
3838 | { | |
3839 | const char *propname = nvpair_name(pair); | |
3840 | boolean_t issnap = (strchr(dsname, '@') != NULL); | |
3841 | zfs_prop_t prop = zfs_name_to_prop(propname); | |
3842 | uint64_t intval; | |
3843 | int err; | |
3844 | ||
3845 | if (prop == ZPROP_INVAL) { | |
3846 | if (zfs_prop_user(propname)) { | |
c65aa5b2 BB |
3847 | if ((err = zfs_secpolicy_write_perms(dsname, |
3848 | ZFS_DELEG_PERM_USERPROP, cr))) | |
428870ff BB |
3849 | return (err); |
3850 | return (0); | |
3851 | } | |
3852 | ||
3853 | if (!issnap && zfs_prop_userquota(propname)) { | |
3854 | const char *perm = NULL; | |
3855 | const char *uq_prefix = | |
3856 | zfs_userquota_prop_prefixes[ZFS_PROP_USERQUOTA]; | |
3857 | const char *gq_prefix = | |
3858 | zfs_userquota_prop_prefixes[ZFS_PROP_GROUPQUOTA]; | |
1de321e6 JX |
3859 | const char *uiq_prefix = |
3860 | zfs_userquota_prop_prefixes[ZFS_PROP_USEROBJQUOTA]; | |
3861 | const char *giq_prefix = | |
3862 | zfs_userquota_prop_prefixes[ZFS_PROP_GROUPOBJQUOTA]; | |
428870ff BB |
3863 | |
3864 | if (strncmp(propname, uq_prefix, | |
3865 | strlen(uq_prefix)) == 0) { | |
3866 | perm = ZFS_DELEG_PERM_USERQUOTA; | |
1de321e6 JX |
3867 | } else if (strncmp(propname, uiq_prefix, |
3868 | strlen(uiq_prefix)) == 0) { | |
3869 | perm = ZFS_DELEG_PERM_USEROBJQUOTA; | |
428870ff BB |
3870 | } else if (strncmp(propname, gq_prefix, |
3871 | strlen(gq_prefix)) == 0) { | |
3872 | perm = ZFS_DELEG_PERM_GROUPQUOTA; | |
1de321e6 JX |
3873 | } else if (strncmp(propname, giq_prefix, |
3874 | strlen(giq_prefix)) == 0) { | |
3875 | perm = ZFS_DELEG_PERM_GROUPOBJQUOTA; | |
428870ff BB |
3876 | } else { |
3877 | /* USERUSED and GROUPUSED are read-only */ | |
2e528b49 | 3878 | return (SET_ERROR(EINVAL)); |
428870ff BB |
3879 | } |
3880 | ||
c65aa5b2 | 3881 | if ((err = zfs_secpolicy_write_perms(dsname, perm, cr))) |
428870ff BB |
3882 | return (err); |
3883 | return (0); | |
3884 | } | |
3885 | ||
2e528b49 | 3886 | return (SET_ERROR(EINVAL)); |
428870ff BB |
3887 | } |
3888 | ||
3889 | if (issnap) | |
2e528b49 | 3890 | return (SET_ERROR(EINVAL)); |
428870ff BB |
3891 | |
3892 | if (nvpair_type(pair) == DATA_TYPE_NVLIST) { | |
3893 | /* | |
3894 | * dsl_prop_get_all_impl() returns properties in this | |
3895 | * format. | |
3896 | */ | |
3897 | nvlist_t *attrs; | |
3898 | VERIFY(nvpair_value_nvlist(pair, &attrs) == 0); | |
3899 | VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, | |
3900 | &pair) == 0); | |
3901 | } | |
3902 | ||
3903 | /* | |
3904 | * Check that this value is valid for this pool version | |
3905 | */ | |
3906 | switch (prop) { | |
3907 | case ZFS_PROP_COMPRESSION: | |
3908 | /* | |
3909 | * If the user specified gzip compression, make sure | |
3910 | * the SPA supports it. We ignore any errors here since | |
3911 | * we'll catch them later. | |
3912 | */ | |
f1512ee6 | 3913 | if (nvpair_value_uint64(pair, &intval) == 0) { |
428870ff BB |
3914 | if (intval >= ZIO_COMPRESS_GZIP_1 && |
3915 | intval <= ZIO_COMPRESS_GZIP_9 && | |
3916 | zfs_earlier_version(dsname, | |
3917 | SPA_VERSION_GZIP_COMPRESSION)) { | |
2e528b49 | 3918 | return (SET_ERROR(ENOTSUP)); |
428870ff BB |
3919 | } |
3920 | ||
3921 | if (intval == ZIO_COMPRESS_ZLE && | |
3922 | zfs_earlier_version(dsname, | |
3923 | SPA_VERSION_ZLE_COMPRESSION)) | |
2e528b49 | 3924 | return (SET_ERROR(ENOTSUP)); |
428870ff | 3925 | |
9759c60f | 3926 | if (intval == ZIO_COMPRESS_LZ4) { |
9759c60f ED |
3927 | spa_t *spa; |
3928 | ||
3929 | if ((err = spa_open(dsname, &spa, FTAG)) != 0) | |
3930 | return (err); | |
3931 | ||
fa86b5db MA |
3932 | if (!spa_feature_is_enabled(spa, |
3933 | SPA_FEATURE_LZ4_COMPRESS)) { | |
9759c60f | 3934 | spa_close(spa, FTAG); |
2e528b49 | 3935 | return (SET_ERROR(ENOTSUP)); |
9759c60f ED |
3936 | } |
3937 | spa_close(spa, FTAG); | |
3938 | } | |
3939 | ||
428870ff BB |
3940 | /* |
3941 | * If this is a bootable dataset then | |
3942 | * verify that the compression algorithm | |
3943 | * is supported for booting. We must return | |
3944 | * something other than ENOTSUP since it | |
3945 | * implies a downrev pool version. | |
3946 | */ | |
3947 | if (zfs_is_bootfs(dsname) && | |
3948 | !BOOTFS_COMPRESS_VALID(intval)) { | |
2e528b49 | 3949 | return (SET_ERROR(ERANGE)); |
428870ff BB |
3950 | } |
3951 | } | |
3952 | break; | |
3953 | ||
3954 | case ZFS_PROP_COPIES: | |
3955 | if (zfs_earlier_version(dsname, SPA_VERSION_DITTO_BLOCKS)) | |
2e528b49 | 3956 | return (SET_ERROR(ENOTSUP)); |
428870ff BB |
3957 | break; |
3958 | ||
4cb7b9c5 | 3959 | case ZFS_PROP_VOLBLOCKSIZE: |
f1512ee6 MA |
3960 | case ZFS_PROP_RECORDSIZE: |
3961 | /* Record sizes above 128k need the feature to be enabled */ | |
3962 | if (nvpair_value_uint64(pair, &intval) == 0 && | |
3963 | intval > SPA_OLD_MAXBLOCKSIZE) { | |
3964 | spa_t *spa; | |
3965 | ||
f1512ee6 MA |
3966 | /* |
3967 | * We don't allow setting the property above 1MB, | |
3968 | * unless the tunable has been changed. | |
3969 | */ | |
3970 | if (intval > zfs_max_recordsize || | |
3971 | intval > SPA_MAXBLOCKSIZE) | |
4b2a3e0c | 3972 | return (SET_ERROR(ERANGE)); |
f1512ee6 MA |
3973 | |
3974 | if ((err = spa_open(dsname, &spa, FTAG)) != 0) | |
3975 | return (err); | |
3976 | ||
3977 | if (!spa_feature_is_enabled(spa, | |
3978 | SPA_FEATURE_LARGE_BLOCKS)) { | |
3979 | spa_close(spa, FTAG); | |
3980 | return (SET_ERROR(ENOTSUP)); | |
3981 | } | |
3982 | spa_close(spa, FTAG); | |
3983 | } | |
3984 | break; | |
3985 | ||
50c957f7 NB |
3986 | case ZFS_PROP_DNODESIZE: |
3987 | /* Dnode sizes above 512 need the feature to be enabled */ | |
3988 | if (nvpair_value_uint64(pair, &intval) == 0 && | |
3989 | intval != ZFS_DNSIZE_LEGACY) { | |
3990 | spa_t *spa; | |
3991 | ||
3992 | /* | |
3993 | * If this is a bootable dataset then | |
3994 | * we don't allow large (>512B) dnodes, | |
3995 | * because GRUB doesn't support them. | |
3996 | */ | |
3997 | if (zfs_is_bootfs(dsname) && | |
02730c33 | 3998 | intval != ZFS_DNSIZE_LEGACY) { |
50c957f7 NB |
3999 | return (SET_ERROR(EDOM)); |
4000 | } | |
4001 | ||
4002 | if ((err = spa_open(dsname, &spa, FTAG)) != 0) | |
4003 | return (err); | |
4004 | ||
4005 | if (!spa_feature_is_enabled(spa, | |
4006 | SPA_FEATURE_LARGE_DNODE)) { | |
4007 | spa_close(spa, FTAG); | |
4008 | return (SET_ERROR(ENOTSUP)); | |
4009 | } | |
4010 | spa_close(spa, FTAG); | |
4011 | } | |
4012 | break; | |
4013 | ||
428870ff BB |
4014 | case ZFS_PROP_SHARESMB: |
4015 | if (zpl_earlier_version(dsname, ZPL_VERSION_FUID)) | |
2e528b49 | 4016 | return (SET_ERROR(ENOTSUP)); |
428870ff BB |
4017 | break; |
4018 | ||
4019 | case ZFS_PROP_ACLINHERIT: | |
4020 | if (nvpair_type(pair) == DATA_TYPE_UINT64 && | |
4021 | nvpair_value_uint64(pair, &intval) == 0) { | |
4022 | if (intval == ZFS_ACL_PASSTHROUGH_X && | |
4023 | zfs_earlier_version(dsname, | |
4024 | SPA_VERSION_PASSTHROUGH_X)) | |
2e528b49 | 4025 | return (SET_ERROR(ENOTSUP)); |
428870ff BB |
4026 | } |
4027 | break; | |
3c67d83a TH |
4028 | case ZFS_PROP_CHECKSUM: |
4029 | case ZFS_PROP_DEDUP: | |
4030 | { | |
4031 | spa_feature_t feature; | |
4032 | spa_t *spa; | |
4033 | uint64_t intval; | |
4034 | int err; | |
4035 | ||
4036 | /* dedup feature version checks */ | |
4037 | if (prop == ZFS_PROP_DEDUP && | |
4038 | zfs_earlier_version(dsname, SPA_VERSION_DEDUP)) | |
4039 | return (SET_ERROR(ENOTSUP)); | |
4040 | ||
4041 | if (nvpair_value_uint64(pair, &intval) != 0) | |
4042 | return (SET_ERROR(EINVAL)); | |
4043 | ||
4044 | /* check prop value is enabled in features */ | |
4a2e9a17 | 4045 | feature = zio_checksum_to_feature(intval & ZIO_CHECKSUM_MASK); |
3c67d83a TH |
4046 | if (feature == SPA_FEATURE_NONE) |
4047 | break; | |
4048 | ||
4049 | if ((err = spa_open(dsname, &spa, FTAG)) != 0) | |
4050 | return (err); | |
4051 | /* | |
4052 | * Salted checksums are not supported on root pools. | |
4053 | */ | |
4054 | if (spa_bootfs(spa) != 0 && | |
4055 | intval < ZIO_CHECKSUM_FUNCTIONS && | |
4056 | (zio_checksum_table[intval].ci_flags & | |
4057 | ZCHECKSUM_FLAG_SALTED)) { | |
4058 | spa_close(spa, FTAG); | |
4059 | return (SET_ERROR(ERANGE)); | |
4060 | } | |
4061 | if (!spa_feature_is_enabled(spa, feature)) { | |
4062 | spa_close(spa, FTAG); | |
4063 | return (SET_ERROR(ENOTSUP)); | |
4064 | } | |
4065 | spa_close(spa, FTAG); | |
4066 | break; | |
4067 | } | |
4068 | ||
e75c13c3 BB |
4069 | default: |
4070 | break; | |
428870ff BB |
4071 | } |
4072 | ||
4073 | return (zfs_secpolicy_setprop(dsname, prop, pair, CRED())); | |
4074 | } | |
4075 | ||
4076 | /* | |
4077 | * Removes properties from the given props list that fail permission checks | |
4078 | * needed to clear them and to restore them in case of a receive error. For each | |
4079 | * property, make sure we have both set and inherit permissions. | |
4080 | * | |
4081 | * Returns the first error encountered if any permission checks fail. If the | |
4082 | * caller provides a non-NULL errlist, it also gives the complete list of names | |
4083 | * of all the properties that failed a permission check along with the | |
4084 | * corresponding error numbers. The caller is responsible for freeing the | |
4085 | * returned errlist. | |
4086 | * | |
4087 | * If every property checks out successfully, zero is returned and the list | |
4088 | * pointed at by errlist is NULL. | |
4089 | */ | |
4090 | static int | |
4091 | zfs_check_clearable(char *dataset, nvlist_t *props, nvlist_t **errlist) | |
b128c09f BB |
4092 | { |
4093 | zfs_cmd_t *zc; | |
428870ff BB |
4094 | nvpair_t *pair, *next_pair; |
4095 | nvlist_t *errors; | |
4096 | int err, rv = 0; | |
b128c09f BB |
4097 | |
4098 | if (props == NULL) | |
428870ff BB |
4099 | return (0); |
4100 | ||
4101 | VERIFY(nvlist_alloc(&errors, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
4102 | ||
efcd79a8 | 4103 | zc = kmem_alloc(sizeof (zfs_cmd_t), KM_SLEEP); |
680eada9 | 4104 | (void) strlcpy(zc->zc_name, dataset, sizeof (zc->zc_name)); |
428870ff BB |
4105 | pair = nvlist_next_nvpair(props, NULL); |
4106 | while (pair != NULL) { | |
4107 | next_pair = nvlist_next_nvpair(props, pair); | |
4108 | ||
680eada9 | 4109 | (void) strlcpy(zc->zc_value, nvpair_name(pair), |
4110 | sizeof (zc->zc_value)); | |
428870ff | 4111 | if ((err = zfs_check_settable(dataset, pair, CRED())) != 0 || |
6f1ffb06 | 4112 | (err = zfs_secpolicy_inherit_prop(zc, NULL, CRED())) != 0) { |
428870ff BB |
4113 | VERIFY(nvlist_remove_nvpair(props, pair) == 0); |
4114 | VERIFY(nvlist_add_int32(errors, | |
4115 | zc->zc_value, err) == 0); | |
4116 | } | |
4117 | pair = next_pair; | |
b128c09f BB |
4118 | } |
4119 | kmem_free(zc, sizeof (zfs_cmd_t)); | |
428870ff BB |
4120 | |
4121 | if ((pair = nvlist_next_nvpair(errors, NULL)) == NULL) { | |
4122 | nvlist_free(errors); | |
4123 | errors = NULL; | |
4124 | } else { | |
4125 | VERIFY(nvpair_value_int32(pair, &rv) == 0); | |
4126 | } | |
4127 | ||
4128 | if (errlist == NULL) | |
4129 | nvlist_free(errors); | |
4130 | else | |
4131 | *errlist = errors; | |
4132 | ||
4133 | return (rv); | |
4134 | } | |
4135 | ||
4136 | static boolean_t | |
4137 | propval_equals(nvpair_t *p1, nvpair_t *p2) | |
4138 | { | |
4139 | if (nvpair_type(p1) == DATA_TYPE_NVLIST) { | |
4140 | /* dsl_prop_get_all_impl() format */ | |
4141 | nvlist_t *attrs; | |
4142 | VERIFY(nvpair_value_nvlist(p1, &attrs) == 0); | |
4143 | VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, | |
4144 | &p1) == 0); | |
4145 | } | |
4146 | ||
4147 | if (nvpair_type(p2) == DATA_TYPE_NVLIST) { | |
4148 | nvlist_t *attrs; | |
4149 | VERIFY(nvpair_value_nvlist(p2, &attrs) == 0); | |
4150 | VERIFY(nvlist_lookup_nvpair(attrs, ZPROP_VALUE, | |
4151 | &p2) == 0); | |
4152 | } | |
4153 | ||
4154 | if (nvpair_type(p1) != nvpair_type(p2)) | |
4155 | return (B_FALSE); | |
4156 | ||
4157 | if (nvpair_type(p1) == DATA_TYPE_STRING) { | |
4158 | char *valstr1, *valstr2; | |
4159 | ||
4160 | VERIFY(nvpair_value_string(p1, (char **)&valstr1) == 0); | |
4161 | VERIFY(nvpair_value_string(p2, (char **)&valstr2) == 0); | |
4162 | return (strcmp(valstr1, valstr2) == 0); | |
4163 | } else { | |
4164 | uint64_t intval1, intval2; | |
4165 | ||
4166 | VERIFY(nvpair_value_uint64(p1, &intval1) == 0); | |
4167 | VERIFY(nvpair_value_uint64(p2, &intval2) == 0); | |
4168 | return (intval1 == intval2); | |
4169 | } | |
b128c09f BB |
4170 | } |
4171 | ||
428870ff BB |
4172 | /* |
4173 | * Remove properties from props if they are not going to change (as determined | |
4174 | * by comparison with origprops). Remove them from origprops as well, since we | |
4175 | * do not need to clear or restore properties that won't change. | |
4176 | */ | |
4177 | static void | |
4178 | props_reduce(nvlist_t *props, nvlist_t *origprops) | |
4179 | { | |
4180 | nvpair_t *pair, *next_pair; | |
4181 | ||
4182 | if (origprops == NULL) | |
4183 | return; /* all props need to be received */ | |
4184 | ||
4185 | pair = nvlist_next_nvpair(props, NULL); | |
4186 | while (pair != NULL) { | |
4187 | const char *propname = nvpair_name(pair); | |
4188 | nvpair_t *match; | |
4189 | ||
4190 | next_pair = nvlist_next_nvpair(props, pair); | |
4191 | ||
4192 | if ((nvlist_lookup_nvpair(origprops, propname, | |
4193 | &match) != 0) || !propval_equals(pair, match)) | |
4194 | goto next; /* need to set received value */ | |
4195 | ||
4196 | /* don't clear the existing received value */ | |
4197 | (void) nvlist_remove_nvpair(origprops, match); | |
4198 | /* don't bother receiving the property */ | |
4199 | (void) nvlist_remove_nvpair(props, pair); | |
4200 | next: | |
4201 | pair = next_pair; | |
4202 | } | |
4203 | } | |
4204 | ||
671c9354 DM |
4205 | /* |
4206 | * Extract properties that cannot be set PRIOR to the receipt of a dataset. | |
4207 | * For example, refquota cannot be set until after the receipt of a dataset, | |
4208 | * because in replication streams, an older/earlier snapshot may exceed the | |
4209 | * refquota. We want to receive the older/earlier snapshot, but setting | |
4210 | * refquota pre-receipt will set the dsl's ACTUAL quota, which will prevent | |
4211 | * the older/earlier snapshot from being received (with EDQUOT). | |
4212 | * | |
4213 | * The ZFS test "zfs_receive_011_pos" demonstrates such a scenario. | |
4214 | * | |
4215 | * libzfs will need to be judicious handling errors encountered by props | |
4216 | * extracted by this function. | |
4217 | */ | |
4218 | static nvlist_t * | |
4219 | extract_delay_props(nvlist_t *props) | |
4220 | { | |
4221 | nvlist_t *delayprops; | |
4222 | nvpair_t *nvp, *tmp; | |
b5256303 TC |
4223 | static const zfs_prop_t delayable[] = { |
4224 | ZFS_PROP_REFQUOTA, | |
4225 | ZFS_PROP_KEYLOCATION, | |
4226 | 0 | |
4227 | }; | |
671c9354 DM |
4228 | int i; |
4229 | ||
4230 | VERIFY(nvlist_alloc(&delayprops, NV_UNIQUE_NAME, KM_SLEEP) == 0); | |
4231 | ||
4232 | for (nvp = nvlist_next_nvpair(props, NULL); nvp != NULL; | |
4233 | nvp = nvlist_next_nvpair(props, nvp)) { | |
4234 | /* | |
4235 | * strcmp() is safe because zfs_prop_to_name() always returns | |
4236 | * a bounded string. | |
4237 | */ | |
4238 | for (i = 0; delayable[i] != 0; i++) { | |
4239 | if (strcmp(zfs_prop_to_name(delayable[i]), | |
4240 | nvpair_name(nvp)) == 0) { | |
4241 | break; | |
4242 | } | |
4243 | } | |
4244 | if (delayable[i] != 0) { | |
4245 | tmp = nvlist_prev_nvpair(props, nvp); | |
4246 | VERIFY(nvlist_add_nvpair(delayprops, nvp) == 0); | |
4247 | VERIFY(nvlist_remove_nvpair(props, nvp) == 0); | |
4248 | nvp = tmp; | |
4249 | } | |
4250 | } | |
4251 | ||
4252 | if (nvlist_empty(delayprops)) { | |
4253 | nvlist_free(delayprops); | |
4254 | delayprops = NULL; | |
4255 | } | |
4256 | return (delayprops); | |
4257 | } | |
4258 | ||
428870ff BB |
4259 | #ifdef DEBUG |
4260 | static boolean_t zfs_ioc_recv_inject_err; | |
4261 | #endif | |
4262 | ||
34dc7c2f | 4263 | /* |
1bf3bf0e GN |
4264 | * nvlist 'errors' is always allocated. It will contain descriptions of |
4265 | * encountered errors, if any. It's the callers responsibility to free. | |
34dc7c2f BB |
4266 | */ |
4267 | static int | |
a3eeab2d | 4268 | zfs_ioc_recv_impl(char *tofs, char *tosnap, char *origin, nvlist_t *recvprops, |
4269 | nvlist_t *localprops, boolean_t force, boolean_t resumable, int input_fd, | |
43e52edd BB |
4270 | dmu_replay_record_t *begin_record, int cleanup_fd, uint64_t *read_bytes, |
4271 | uint64_t *errflags, uint64_t *action_handle, nvlist_t **errors) | |
34dc7c2f | 4272 | { |
34dc7c2f | 4273 | dmu_recv_cookie_t drc; |
428870ff BB |
4274 | int error = 0; |
4275 | int props_error = 0; | |
34dc7c2f | 4276 | offset_t off; |
671c9354 | 4277 | nvlist_t *delayprops = NULL; /* sent properties applied post-receive */ |
43e52edd | 4278 | nvlist_t *origprops = NULL; /* existing properties */ |
a3eeab2d | 4279 | nvlist_t *origrecvd = NULL; /* existing received properties */ |
428870ff | 4280 | boolean_t first_recvd_props = B_FALSE; |
43e52edd | 4281 | file_t *input_fp; |
34dc7c2f | 4282 | |
1bf3bf0e GN |
4283 | *read_bytes = 0; |
4284 | *errflags = 0; | |
4285 | *errors = fnvlist_alloc(); | |
4286 | ||
43e52edd BB |
4287 | input_fp = getf(input_fd); |
4288 | if (input_fp == NULL) | |
2e528b49 | 4289 | return (SET_ERROR(EBADF)); |
13fe0198 MA |
4290 | |
4291 | error = dmu_recv_begin(tofs, tosnap, | |
43e52edd | 4292 | begin_record, force, resumable, origin, &drc); |
13fe0198 MA |
4293 | if (error != 0) |
4294 | goto out; | |
4295 | ||
4296 | /* | |
4297 | * Set properties before we receive the stream so that they are applied | |
4298 | * to the new data. Note that we must call dmu_recv_stream() if | |
4299 | * dmu_recv_begin() succeeds. | |
4300 | */ | |
a3eeab2d | 4301 | if (recvprops != NULL && !drc.drc_newfs) { |
13fe0198 MA |
4302 | if (spa_version(dsl_dataset_get_spa(drc.drc_ds)) >= |
4303 | SPA_VERSION_RECVD_PROPS && | |
4304 | !dsl_prop_get_hasrecvd(tofs)) | |
428870ff | 4305 | first_recvd_props = B_TRUE; |
428870ff | 4306 | |
b128c09f | 4307 | /* |
428870ff BB |
4308 | * If new received properties are supplied, they are to |
4309 | * completely replace the existing received properties, so stash | |
4310 | * away the existing ones. | |
b128c09f | 4311 | */ |
a3eeab2d | 4312 | if (dsl_prop_get_received(tofs, &origrecvd) == 0) { |
428870ff BB |
4313 | nvlist_t *errlist = NULL; |
4314 | /* | |
4315 | * Don't bother writing a property if its value won't | |
4316 | * change (and avoid the unnecessary security checks). | |
4317 | * | |
4318 | * The first receive after SPA_VERSION_RECVD_PROPS is a | |
4319 | * special case where we blow away all local properties | |
4320 | * regardless. | |
4321 | */ | |
4322 | if (!first_recvd_props) | |
a3eeab2d | 4323 | props_reduce(recvprops, origrecvd); |
4324 | if (zfs_check_clearable(tofs, origrecvd, &errlist) != 0) | |
43e52edd | 4325 | (void) nvlist_merge(*errors, errlist, 0); |
428870ff | 4326 | nvlist_free(errlist); |
b128c09f | 4327 | |
a3eeab2d | 4328 | if (clear_received_props(tofs, origrecvd, |
4329 | first_recvd_props ? NULL : recvprops) != 0) | |
4330 | *errflags |= ZPROP_ERR_NOCLEAR; | |
4331 | } else { | |
4332 | *errflags |= ZPROP_ERR_NOCLEAR; | |
4333 | } | |
4334 | } | |
4335 | ||
4336 | /* | |
4337 | * Stash away existing properties so we can restore them on error unless | |
4338 | * we're doing the first receive after SPA_VERSION_RECVD_PROPS, in which | |
4339 | * case "origrecvd" will take care of that. | |
4340 | */ | |
4341 | if (localprops != NULL && !drc.drc_newfs && !first_recvd_props) { | |
4342 | objset_t *os; | |
4343 | if (dmu_objset_hold(tofs, FTAG, &os) == 0) { | |
4344 | if (dsl_prop_get_all(os, &origprops) != 0) { | |
43e52edd | 4345 | *errflags |= ZPROP_ERR_NOCLEAR; |
a3eeab2d | 4346 | } |
4347 | dmu_objset_rele(os, FTAG); | |
13fe0198 | 4348 | } else { |
43e52edd | 4349 | *errflags |= ZPROP_ERR_NOCLEAR; |
428870ff | 4350 | } |
13fe0198 MA |
4351 | } |
4352 | ||
a3eeab2d | 4353 | if (recvprops != NULL) { |
13fe0198 | 4354 | props_error = dsl_prop_set_hasrecvd(tofs); |
428870ff | 4355 | |
13fe0198 | 4356 | if (props_error == 0) { |
a3eeab2d | 4357 | delayprops = extract_delay_props(recvprops); |
13fe0198 | 4358 | (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, |
a3eeab2d | 4359 | recvprops, *errors); |
13fe0198 | 4360 | } |
428870ff BB |
4361 | } |
4362 | ||
a3eeab2d | 4363 | if (localprops != NULL) { |
4364 | nvlist_t *oprops = fnvlist_alloc(); | |
4365 | nvlist_t *xprops = fnvlist_alloc(); | |
4366 | nvpair_t *nvp = NULL; | |
4367 | ||
4368 | while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) { | |
4369 | if (nvpair_type(nvp) == DATA_TYPE_BOOLEAN) { | |
4370 | /* -x property */ | |
4371 | const char *name = nvpair_name(nvp); | |
4372 | zfs_prop_t prop = zfs_name_to_prop(name); | |
4373 | if (prop != ZPROP_INVAL) { | |
4374 | if (!zfs_prop_inheritable(prop)) | |
4375 | continue; | |
4376 | } else if (!zfs_prop_user(name)) | |
4377 | continue; | |
4378 | fnvlist_add_boolean(xprops, name); | |
4379 | } else { | |
4380 | /* -o property=value */ | |
4381 | fnvlist_add_nvpair(oprops, nvp); | |
4382 | } | |
4383 | } | |
4384 | (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, | |
4385 | oprops, *errors); | |
4386 | (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, | |
4387 | xprops, *errors); | |
4388 | ||
4389 | nvlist_free(oprops); | |
4390 | nvlist_free(xprops); | |
4391 | } | |
4392 | ||
43e52edd BB |
4393 | off = input_fp->f_offset; |
4394 | error = dmu_recv_stream(&drc, input_fp->f_vnode, &off, cleanup_fd, | |
4395 | action_handle); | |
34dc7c2f | 4396 | |
45d1cae3 | 4397 | if (error == 0) { |
0037b49e | 4398 | zfsvfs_t *zfsvfs = NULL; |
040dab99 | 4399 | zvol_state_t *zv = NULL; |
b128c09f | 4400 | |
f298b24d | 4401 | if (getzfsvfs(tofs, &zfsvfs) == 0) { |
45d1cae3 | 4402 | /* online recv */ |
ec923db2 | 4403 | dsl_dataset_t *ds; |
45d1cae3 | 4404 | int end_err; |
b128c09f | 4405 | |
0037b49e BB |
4406 | ds = dmu_objset_ds(zfsvfs->z_os); |
4407 | error = zfs_suspend_fs(zfsvfs); | |
45d1cae3 BB |
4408 | /* |
4409 | * If the suspend fails, then the recv_end will | |
4410 | * likely also fail, and clean up after itself. | |
4411 | */ | |
0037b49e | 4412 | end_err = dmu_recv_end(&drc, zfsvfs); |
428870ff | 4413 | if (error == 0) |
0037b49e | 4414 | error = zfs_resume_fs(zfsvfs, ds); |
45d1cae3 | 4415 | error = error ? error : end_err; |
0037b49e | 4416 | deactivate_super(zfsvfs->z_sb); |
040dab99 CC |
4417 | } else if ((zv = zvol_suspend(tofs)) != NULL) { |
4418 | error = dmu_recv_end(&drc, zvol_tag(zv)); | |
4419 | zvol_resume(zv); | |
b128c09f | 4420 | } else { |
831baf06 | 4421 | error = dmu_recv_end(&drc, NULL); |
34dc7c2f | 4422 | } |
671c9354 DM |
4423 | |
4424 | /* Set delayed properties now, after we're done receiving. */ | |
4425 | if (delayprops != NULL && error == 0) { | |
4426 | (void) zfs_set_prop_nvlist(tofs, ZPROP_SRC_RECEIVED, | |
43e52edd | 4427 | delayprops, *errors); |
671c9354 DM |
4428 | } |
4429 | } | |
4430 | ||
4431 | if (delayprops != NULL) { | |
4432 | /* | |
4433 | * Merge delayed props back in with initial props, in case | |
4434 | * we're DEBUG and zfs_ioc_recv_inject_err is set (which means | |
4435 | * we have to make sure clear_received_props() includes | |
4436 | * the delayed properties). | |
4437 | * | |
4438 | * Since zfs_ioc_recv_inject_err is only in DEBUG kernels, | |
4439 | * using ASSERT() will be just like a VERIFY. | |
4440 | */ | |
a3eeab2d | 4441 | ASSERT(nvlist_merge(recvprops, delayprops, 0) == 0); |
671c9354 DM |
4442 | nvlist_free(delayprops); |
4443 | } | |
4444 | ||
34dc7c2f | 4445 | |
43e52edd BB |
4446 | *read_bytes = off - input_fp->f_offset; |
4447 | if (VOP_SEEK(input_fp->f_vnode, input_fp->f_offset, &off, NULL) == 0) | |
02730c33 | 4448 | input_fp->f_offset = off; |
34dc7c2f | 4449 | |
428870ff BB |
4450 | #ifdef DEBUG |
4451 | if (zfs_ioc_recv_inject_err) { | |
4452 | zfs_ioc_recv_inject_err = B_FALSE; | |
4453 | error = 1; | |
4454 | } | |
4455 | #endif | |
ba6a2402 | 4456 | |
b128c09f BB |
4457 | /* |
4458 | * On error, restore the original props. | |
4459 | */ | |
a3eeab2d | 4460 | if (error != 0 && recvprops != NULL && !drc.drc_newfs) { |
4461 | if (clear_received_props(tofs, recvprops, NULL) != 0) { | |
13fe0198 MA |
4462 | /* |
4463 | * We failed to clear the received properties. | |
4464 | * Since we may have left a $recvd value on the | |
4465 | * system, we can't clear the $hasrecvd flag. | |
4466 | */ | |
43e52edd | 4467 | *errflags |= ZPROP_ERR_NORESTORE; |
13fe0198 MA |
4468 | } else if (first_recvd_props) { |
4469 | dsl_prop_unset_hasrecvd(tofs); | |
428870ff BB |
4470 | } |
4471 | ||
a3eeab2d | 4472 | if (origrecvd == NULL && !drc.drc_newfs) { |
428870ff | 4473 | /* We failed to stash the original properties. */ |
43e52edd | 4474 | *errflags |= ZPROP_ERR_NORESTORE; |
428870ff BB |
4475 | } |
4476 | ||
4477 | /* | |
4478 | * dsl_props_set() will not convert RECEIVED to LOCAL on or | |
4479 | * after SPA_VERSION_RECVD_PROPS, so we need to specify LOCAL | |
4e33ba4c | 4480 | * explicitly if we're restoring local properties cleared in the |
428870ff BB |
4481 | * first new-style receive. |
4482 | */ | |
a3eeab2d | 4483 | if (origrecvd != NULL && |
428870ff BB |
4484 | zfs_set_prop_nvlist(tofs, (first_recvd_props ? |
4485 | ZPROP_SRC_LOCAL : ZPROP_SRC_RECEIVED), | |
a3eeab2d | 4486 | origrecvd, NULL) != 0) { |
428870ff BB |
4487 | /* |
4488 | * We stashed the original properties but failed to | |
4489 | * restore them. | |
4490 | */ | |
43e52edd | 4491 | *errflags |= ZPROP_ERR_NORESTORE; |
428870ff | 4492 | } |
b128c09f | 4493 | } |
a3eeab2d | 4494 | if (error != 0 && localprops != NULL && !drc.drc_newfs && |
4495 | !first_recvd_props) { | |
4496 | nvlist_t *setprops; | |
4497 | nvlist_t *inheritprops; | |
4498 | nvpair_t *nvp; | |
4499 | ||
4500 | if (origprops == NULL) { | |
4501 | /* We failed to stash the original properties. */ | |
4502 | *errflags |= ZPROP_ERR_NORESTORE; | |
4503 | goto out; | |
4504 | } | |
4505 | ||
4506 | /* Restore original props */ | |
4507 | setprops = fnvlist_alloc(); | |
4508 | inheritprops = fnvlist_alloc(); | |
4509 | nvp = NULL; | |
4510 | while ((nvp = nvlist_next_nvpair(localprops, nvp)) != NULL) { | |
4511 | const char *name = nvpair_name(nvp); | |
4512 | const char *source; | |
4513 | nvlist_t *attrs; | |
4514 | ||
4515 | if (!nvlist_exists(origprops, name)) { | |
4516 | /* | |
4517 | * Property was not present or was explicitly | |
4518 | * inherited before the receive, restore this. | |
4519 | */ | |
4520 | fnvlist_add_boolean(inheritprops, name); | |
4521 | continue; | |
4522 | } | |
4523 | attrs = fnvlist_lookup_nvlist(origprops, name); | |
4524 | source = fnvlist_lookup_string(attrs, ZPROP_SOURCE); | |
4525 | ||
4526 | /* Skip received properties */ | |
4527 | if (strcmp(source, ZPROP_SOURCE_VAL_RECVD) == 0) | |
4528 | continue; | |
4529 | ||
4530 | if (strcmp(source, tofs) == 0) { | |
4531 | /* Property was locally set */ | |
4532 | fnvlist_add_nvlist(setprops, name, attrs); | |
4533 | } else { | |
4534 | /* Property was implicitly inherited */ | |
4535 | fnvlist_add_boolean(inheritprops, name); | |
4536 | } | |
4537 | } | |
4538 | ||
4539 | if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_LOCAL, setprops, | |
4540 | NULL) != 0) | |
4541 | *errflags |= ZPROP_ERR_NORESTORE; | |
4542 | if (zfs_set_prop_nvlist(tofs, ZPROP_SRC_INHERITED, inheritprops, | |
4543 | NULL) != 0) | |
4544 | *errflags |= ZPROP_ERR_NORESTORE; | |
4545 | ||
4546 | nvlist_free(setprops); | |
4547 | nvlist_free(inheritprops); | |
4548 | } | |
b128c09f | 4549 | out: |
43e52edd | 4550 | releasef(input_fd); |
a3eeab2d | 4551 | nvlist_free(origrecvd); |
b128c09f | 4552 | nvlist_free(origprops); |
428870ff BB |
4553 | |
4554 | if (error == 0) | |
4555 | error = props_error; | |
4556 | ||
34dc7c2f BB |
4557 | return (error); |
4558 | } | |
4559 | ||
43e52edd BB |
4560 | /* |
4561 | * inputs: | |
4562 | * zc_name name of containing filesystem (unused) | |
4563 | * zc_nvlist_src{_size} nvlist of properties to apply | |
a3eeab2d | 4564 | * zc_nvlist_conf{_size} nvlist of properties to exclude |
4565 | * (DATA_TYPE_BOOLEAN) and override (everything else) | |
43e52edd BB |
4566 | * zc_value name of snapshot to create |
4567 | * zc_string name of clone origin (if DRR_FLAG_CLONE) | |
4568 | * zc_cookie file descriptor to recv from | |
4569 | * zc_begin_record the BEGIN record of the stream (not byteswapped) | |
4570 | * zc_guid force flag | |
4571 | * zc_cleanup_fd cleanup-on-exit file descriptor | |
4572 | * zc_action_handle handle for this guid/ds mapping (or zero on first call) | |
4573 | * | |
4574 | * outputs: | |
4575 | * zc_cookie number of bytes read | |
4576 | * zc_obj zprop_errflags_t | |
4577 | * zc_action_handle handle for this guid/ds mapping | |
4578 | * zc_nvlist_dst{_size} error for each unapplied received property | |
4579 | */ | |
4580 | static int | |
4581 | zfs_ioc_recv(zfs_cmd_t *zc) | |
4582 | { | |
4583 | dmu_replay_record_t begin_record; | |
4584 | nvlist_t *errors = NULL; | |
a3eeab2d | 4585 | nvlist_t *recvdprops = NULL; |
4586 | nvlist_t *localprops = NULL; | |
43e52edd BB |
4587 | char *origin = NULL; |
4588 | char *tosnap; | |
eca7b760 | 4589 | char tofs[ZFS_MAX_DATASET_NAME_LEN]; |
43e52edd BB |
4590 | int error = 0; |
4591 | ||
4592 | if (dataset_namecheck(zc->zc_value, NULL, NULL) != 0 || | |
4593 | strchr(zc->zc_value, '@') == NULL || | |
4594 | strchr(zc->zc_value, '%')) | |
4595 | return (SET_ERROR(EINVAL)); | |
4596 | ||
30f3f2e1 | 4597 | (void) strlcpy(tofs, zc->zc_value, sizeof (tofs)); |
43e52edd BB |
4598 | tosnap = strchr(tofs, '@'); |
4599 | *tosnap++ = '\0'; | |
4600 | ||
4601 | if (zc->zc_nvlist_src != 0 && | |
4602 | (error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, | |
a3eeab2d | 4603 | zc->zc_iflags, &recvdprops)) != 0) |
4604 | return (error); | |
4605 | ||
4606 | if (zc->zc_nvlist_conf != 0 && | |
4607 | (error = get_nvlist(zc->zc_nvlist_conf, zc->zc_nvlist_conf_size, | |
4608 | zc->zc_iflags, &localprops)) != 0) | |
43e52edd BB |
4609 | return (error); |
4610 | ||
4611 | if (zc->zc_string[0]) | |
4612 | origin = zc->zc_string; | |
4613 | ||
4614 | begin_record.drr_type = DRR_BEGIN; | |
4615 | begin_record.drr_payloadlen = 0; | |
4616 | begin_record.drr_u.drr_begin = zc->zc_begin_record; | |
4617 | ||
a3eeab2d | 4618 | error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvdprops, localprops, |
4619 | zc->zc_guid, B_FALSE, zc->zc_cookie, &begin_record, | |
4620 | zc->zc_cleanup_fd, &zc->zc_cookie, &zc->zc_obj, | |
4621 | &zc->zc_action_handle, &errors); | |
4622 | nvlist_free(recvdprops); | |
4623 | nvlist_free(localprops); | |
43e52edd BB |
4624 | |
4625 | /* | |
4626 | * Now that all props, initial and delayed, are set, report the prop | |
4627 | * errors to the caller. | |
4628 | */ | |
4629 | if (zc->zc_nvlist_dst_size != 0 && errors != NULL && | |
4630 | (nvlist_smush(errors, zc->zc_nvlist_dst_size) != 0 || | |
4631 | put_nvlist(zc, errors) != 0)) { | |
4632 | /* | |
4633 | * Caller made zc->zc_nvlist_dst less than the minimum expected | |
4634 | * size or supplied an invalid address. | |
4635 | */ | |
4636 | error = SET_ERROR(EINVAL); | |
4637 | } | |
4638 | ||
4639 | nvlist_free(errors); | |
4640 | ||
4641 | return (error); | |
4642 | } | |
4643 | ||
4644 | /* | |
4645 | * innvl: { | |
4646 | * "snapname" -> full name of the snapshot to create | |
a3eeab2d | 4647 | * (optional) "props" -> received properties to set (nvlist) |
4648 | * (optional) "localprops" -> override and exclude properties (nvlist) | |
43e52edd BB |
4649 | * (optional) "origin" -> name of clone origin (DRR_FLAG_CLONE) |
4650 | * "begin_record" -> non-byteswapped dmu_replay_record_t | |
4651 | * "input_fd" -> file descriptor to read stream from (int32) | |
4652 | * (optional) "force" -> force flag (value ignored) | |
4653 | * (optional) "resumable" -> resumable flag (value ignored) | |
4654 | * (optional) "cleanup_fd" -> cleanup-on-exit file descriptor | |
4655 | * (optional) "action_handle" -> handle for this guid/ds mapping | |
4656 | * } | |
4657 | * | |
4658 | * outnvl: { | |
4659 | * "read_bytes" -> number of bytes read | |
4660 | * "error_flags" -> zprop_errflags_t | |
4661 | * "action_handle" -> handle for this guid/ds mapping | |
4662 | * "errors" -> error for each unapplied received property (nvlist) | |
4663 | * } | |
4664 | */ | |
4665 | static int | |
4666 | zfs_ioc_recv_new(const char *fsname, nvlist_t *innvl, nvlist_t *outnvl) | |
4667 | { | |
4668 | dmu_replay_record_t *begin_record; | |
4669 | uint_t begin_record_size; | |
4670 | nvlist_t *errors = NULL; | |
a3eeab2d | 4671 | nvlist_t *recvprops = NULL; |
4672 | nvlist_t *localprops = NULL; | |
43e52edd BB |
4673 | char *snapname = NULL; |
4674 | char *origin = NULL; | |
4675 | char *tosnap; | |
eca7b760 | 4676 | char tofs[ZFS_MAX_DATASET_NAME_LEN]; |
43e52edd BB |
4677 | boolean_t force; |
4678 | boolean_t resumable; | |
4679 | uint64_t action_handle = 0; | |
4680 | uint64_t read_bytes = 0; | |
4681 | uint64_t errflags = 0; | |
4682 | int input_fd = -1; | |
4683 | int cleanup_fd = -1; | |
4684 | int error; | |
4685 | ||
4686 | error = nvlist_lookup_string(innvl, "snapname", &snapname); | |
4687 | if (error != 0) | |
4688 | return (SET_ERROR(EINVAL)); | |
4689 | ||
4690 | if (dataset_namecheck(snapname, NULL, NULL) != 0 || | |
4691 | strchr(snapname, '@') == NULL || | |
4692 | strchr(snapname, '%')) | |
4693 | return (SET_ERROR(EINVAL)); | |
4694 | ||
4695 | (void) strcpy(tofs, snapname); | |
4696 | tosnap = strchr(tofs, '@'); | |
4697 | *tosnap++ = '\0'; | |
4698 | ||
4699 | error = nvlist_lookup_string(innvl, "origin", &origin); | |
4700 | if (error && error != ENOENT) | |
4701 | return (error); | |
4702 | ||
4703 | error = nvlist_lookup_byte_array(innvl, "begin_record", | |
02730c33 | 4704 | (uchar_t **)&begin_record, &begin_record_size); |
43e52edd BB |
4705 | if (error != 0 || begin_record_size != sizeof (*begin_record)) |
4706 | return (SET_ERROR(EINVAL)); | |
4707 | ||
4708 | error = nvlist_lookup_int32(innvl, "input_fd", &input_fd); | |
4709 | if (error != 0) | |
4710 | return (SET_ERROR(EINVAL)); | |
4711 | ||
4712 | force = nvlist_exists(innvl, "force"); | |
4713 | resumable = nvlist_exists(innvl, "resumable"); | |
4714 | ||
4715 | error = nvlist_lookup_int32(innvl, "cleanup_fd", &cleanup_fd); | |
4716 | if (error && error != ENOENT) | |
4717 | return (error); | |
4718 | ||
4719 | error = nvlist_lookup_uint64(innvl, "action_handle", &action_handle); | |
4720 | if (error && error != ENOENT) | |
4721 | return (error); | |
4722 | ||
a3eeab2d | 4723 | /* we still use "props" here for backwards compatibility */ |
4724 | error = nvlist_lookup_nvlist(innvl, "props", &recvprops); | |
43e52edd BB |
4725 | if (error && error != ENOENT) |
4726 | return (error); | |
4727 | ||
a3eeab2d | 4728 | error = nvlist_lookup_nvlist(innvl, "localprops", &localprops); |
4729 | if (error && error != ENOENT) | |
4730 | return (error); | |
4731 | ||
4732 | error = zfs_ioc_recv_impl(tofs, tosnap, origin, recvprops, localprops, | |
4733 | force, resumable, input_fd, begin_record, cleanup_fd, &read_bytes, | |
43e52edd BB |
4734 | &errflags, &action_handle, &errors); |
4735 | ||
4736 | fnvlist_add_uint64(outnvl, "read_bytes", read_bytes); | |
4737 | fnvlist_add_uint64(outnvl, "error_flags", errflags); | |
4738 | fnvlist_add_uint64(outnvl, "action_handle", action_handle); | |
4739 | fnvlist_add_nvlist(outnvl, "errors", errors); | |
4740 | ||
4741 | nvlist_free(errors); | |
a3eeab2d | 4742 | nvlist_free(recvprops); |
4743 | nvlist_free(localprops); | |
43e52edd BB |
4744 | |
4745 | return (error); | |
4746 | } | |
4747 | ||
34dc7c2f BB |
4748 | /* |
4749 | * inputs: | |
4750 | * zc_name name of snapshot to send | |
34dc7c2f | 4751 | * zc_cookie file descriptor to send stream to |
572e2857 BB |
4752 | * zc_obj fromorigin flag (mutually exclusive with zc_fromobj) |
4753 | * zc_sendobj objsetid of snapshot to send | |
4754 | * zc_fromobj objsetid of incremental fromsnap (may be zero) | |
330d06f9 MA |
4755 | * zc_guid if set, estimate size of stream only. zc_cookie is ignored. |
4756 | * output size in zc_objset_type. | |
f1512ee6 | 4757 | * zc_flags lzc_send_flags |
34dc7c2f | 4758 | * |
da536844 MA |
4759 | * outputs: |
4760 | * zc_objset_type estimated size, if zc_guid is set | |
cf7684bc | 4761 | * |
4762 | * NOTE: This is no longer the preferred interface, any new functionality | |
4763 | * should be added to zfs_ioc_send_new() instead. | |
34dc7c2f BB |
4764 | */ |
4765 | static int | |
4766 | zfs_ioc_send(zfs_cmd_t *zc) | |
4767 | { | |
34dc7c2f BB |
4768 | int error; |
4769 | offset_t off; | |
330d06f9 | 4770 | boolean_t estimate = (zc->zc_guid != 0); |
9b67f605 | 4771 | boolean_t embedok = (zc->zc_flags & 0x1); |
f1512ee6 | 4772 | boolean_t large_block_ok = (zc->zc_flags & 0x2); |
2aa34383 | 4773 | boolean_t compressok = (zc->zc_flags & 0x4); |
b5256303 | 4774 | boolean_t rawok = (zc->zc_flags & 0x8); |
34dc7c2f | 4775 | |
13fe0198 MA |
4776 | if (zc->zc_obj != 0) { |
4777 | dsl_pool_t *dp; | |
4778 | dsl_dataset_t *tosnap; | |
34dc7c2f | 4779 | |
13fe0198 MA |
4780 | error = dsl_pool_hold(zc->zc_name, FTAG, &dp); |
4781 | if (error != 0) | |
572e2857 | 4782 | return (error); |
13fe0198 MA |
4783 | |
4784 | error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, FTAG, &tosnap); | |
4785 | if (error != 0) { | |
4786 | dsl_pool_rele(dp, FTAG); | |
34dc7c2f BB |
4787 | return (error); |
4788 | } | |
13fe0198 MA |
4789 | |
4790 | if (dsl_dir_is_clone(tosnap->ds_dir)) | |
d683ddbb JG |
4791 | zc->zc_fromobj = |
4792 | dsl_dir_phys(tosnap->ds_dir)->dd_origin_obj; | |
13fe0198 MA |
4793 | dsl_dataset_rele(tosnap, FTAG); |
4794 | dsl_pool_rele(dp, FTAG); | |
6f1ffb06 MA |
4795 | } |
4796 | ||
13fe0198 MA |
4797 | if (estimate) { |
4798 | dsl_pool_t *dp; | |
4799 | dsl_dataset_t *tosnap; | |
4800 | dsl_dataset_t *fromsnap = NULL; | |
4801 | ||
4802 | error = dsl_pool_hold(zc->zc_name, FTAG, &dp); | |
4803 | if (error != 0) | |
4804 | return (error); | |
6f1ffb06 | 4805 | |
b5256303 TC |
4806 | error = dsl_dataset_hold_obj(dp, zc->zc_sendobj, |
4807 | FTAG, &tosnap); | |
13fe0198 MA |
4808 | if (error != 0) { |
4809 | dsl_pool_rele(dp, FTAG); | |
4810 | return (error); | |
6f1ffb06 MA |
4811 | } |
4812 | ||
13fe0198 MA |
4813 | if (zc->zc_fromobj != 0) { |
4814 | error = dsl_dataset_hold_obj(dp, zc->zc_fromobj, | |
4815 | FTAG, &fromsnap); | |
4816 | if (error != 0) { | |
4817 | dsl_dataset_rele(tosnap, FTAG); | |
4818 | dsl_pool_rele(dp, FTAG); | |
6f1ffb06 MA |
4819 | return (error); |
4820 | } | |
4821 | } | |
34dc7c2f | 4822 | |
b5256303 | 4823 | error = dmu_send_estimate(tosnap, fromsnap, compressok || rawok, |
330d06f9 | 4824 | &zc->zc_objset_type); |
13fe0198 MA |
4825 | |
4826 | if (fromsnap != NULL) | |
4827 | dsl_dataset_rele(fromsnap, FTAG); | |
4828 | dsl_dataset_rele(tosnap, FTAG); | |
4829 | dsl_pool_rele(dp, FTAG); | |
330d06f9 MA |
4830 | } else { |
4831 | file_t *fp = getf(zc->zc_cookie); | |
13fe0198 | 4832 | if (fp == NULL) |
2e528b49 | 4833 | return (SET_ERROR(EBADF)); |
34dc7c2f | 4834 | |
330d06f9 | 4835 | off = fp->f_offset; |
13fe0198 | 4836 | error = dmu_send_obj(zc->zc_name, zc->zc_sendobj, |
b5256303 | 4837 | zc->zc_fromobj, embedok, large_block_ok, compressok, rawok, |
f1512ee6 | 4838 | zc->zc_cookie, fp->f_vnode, &off); |
34dc7c2f | 4839 | |
330d06f9 MA |
4840 | if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) |
4841 | fp->f_offset = off; | |
4842 | releasef(zc->zc_cookie); | |
4843 | } | |
34dc7c2f BB |
4844 | return (error); |
4845 | } | |
4846 | ||
37abac6d BP |
4847 | /* |
4848 | * inputs: | |
4849 | * zc_name name of snapshot on which to report progress | |
4850 | * zc_cookie file descriptor of send stream | |
4851 | * | |
4852 | * outputs: | |
4853 | * zc_cookie number of bytes written in send stream thus far | |
4854 | */ | |
4855 | static int | |
4856 | zfs_ioc_send_progress(zfs_cmd_t *zc) | |
4857 | { | |
13fe0198 | 4858 | dsl_pool_t *dp; |
37abac6d BP |
4859 | dsl_dataset_t *ds; |
4860 | dmu_sendarg_t *dsp = NULL; | |
4861 | int error; | |
4862 | ||
13fe0198 MA |
4863 | error = dsl_pool_hold(zc->zc_name, FTAG, &dp); |
4864 | if (error != 0) | |
37abac6d BP |
4865 | return (error); |
4866 | ||
13fe0198 MA |
4867 | error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); |
4868 | if (error != 0) { | |
4869 | dsl_pool_rele(dp, FTAG); | |
4870 | return (error); | |
4871 | } | |
4872 | ||
37abac6d BP |
4873 | mutex_enter(&ds->ds_sendstream_lock); |
4874 | ||
4875 | /* | |
4876 | * Iterate over all the send streams currently active on this dataset. | |
4877 | * If there's one which matches the specified file descriptor _and_ the | |
4878 | * stream was started by the current process, return the progress of | |
4879 | * that stream. | |
4880 | */ | |
4881 | ||
4882 | for (dsp = list_head(&ds->ds_sendstreams); dsp != NULL; | |
4883 | dsp = list_next(&ds->ds_sendstreams, dsp)) { | |
4884 | if (dsp->dsa_outfd == zc->zc_cookie && | |
4885 | dsp->dsa_proc->group_leader == curproc->group_leader) | |
4886 | break; | |
4887 | } | |
4888 | ||
4889 | if (dsp != NULL) | |
4890 | zc->zc_cookie = *(dsp->dsa_off); | |
4891 | else | |
2e528b49 | 4892 | error = SET_ERROR(ENOENT); |
37abac6d BP |
4893 | |
4894 | mutex_exit(&ds->ds_sendstream_lock); | |
4895 | dsl_dataset_rele(ds, FTAG); | |
13fe0198 | 4896 | dsl_pool_rele(dp, FTAG); |
37abac6d BP |
4897 | return (error); |
4898 | } | |
4899 | ||
34dc7c2f BB |
4900 | static int |
4901 | zfs_ioc_inject_fault(zfs_cmd_t *zc) | |
4902 | { | |
4903 | int id, error; | |
4904 | ||
4905 | error = zio_inject_fault(zc->zc_name, (int)zc->zc_guid, &id, | |
4906 | &zc->zc_inject_record); | |
4907 | ||
4908 | if (error == 0) | |
4909 | zc->zc_guid = (uint64_t)id; | |
4910 | ||
4911 | return (error); | |
4912 | } | |
4913 | ||
4914 | static int | |
4915 | zfs_ioc_clear_fault(zfs_cmd_t *zc) | |
4916 | { | |
4917 | return (zio_clear_fault((int)zc->zc_guid)); | |
4918 | } | |
4919 | ||
4920 | static int | |
4921 | zfs_ioc_inject_list_next(zfs_cmd_t *zc) | |
4922 | { | |
4923 | int id = (int)zc->zc_guid; | |
4924 | int error; | |
4925 | ||
4926 | error = zio_inject_list_next(&id, zc->zc_name, sizeof (zc->zc_name), | |
4927 | &zc->zc_inject_record); | |
4928 | ||
4929 | zc->zc_guid = id; | |
4930 | ||
4931 | return (error); | |
4932 | } | |
4933 | ||
4934 | static int | |
4935 | zfs_ioc_error_log(zfs_cmd_t *zc) | |
4936 | { | |
4937 | spa_t *spa; | |
4938 | int error; | |
4939 | size_t count = (size_t)zc->zc_nvlist_dst_size; | |
4940 | ||
4941 | if ((error = spa_open(zc->zc_name, &spa, FTAG)) != 0) | |
4942 | return (error); | |
4943 | ||
4944 | error = spa_get_errlog(spa, (void *)(uintptr_t)zc->zc_nvlist_dst, | |
4945 | &count); | |
4946 | if (error == 0) | |
4947 | zc->zc_nvlist_dst_size = count; | |
4948 | else | |
4949 | zc->zc_nvlist_dst_size = spa_get_errlog_size(spa); | |
4950 | ||
4951 | spa_close(spa, FTAG); | |
4952 | ||
4953 | return (error); | |
4954 | } | |
4955 | ||
4956 | static int | |
4957 | zfs_ioc_clear(zfs_cmd_t *zc) | |
4958 | { | |
4959 | spa_t *spa; | |
4960 | vdev_t *vd; | |
34dc7c2f BB |
4961 | int error; |
4962 | ||
34dc7c2f | 4963 | /* |
b128c09f | 4964 | * On zpool clear we also fix up missing slogs |
34dc7c2f | 4965 | */ |
b128c09f BB |
4966 | mutex_enter(&spa_namespace_lock); |
4967 | spa = spa_lookup(zc->zc_name); | |
4968 | if (spa == NULL) { | |
4969 | mutex_exit(&spa_namespace_lock); | |
2e528b49 | 4970 | return (SET_ERROR(EIO)); |
b128c09f | 4971 | } |
428870ff | 4972 | if (spa_get_log_state(spa) == SPA_LOG_MISSING) { |
b128c09f | 4973 | /* we need to let spa_open/spa_load clear the chains */ |
428870ff | 4974 | spa_set_log_state(spa, SPA_LOG_CLEAR); |
34dc7c2f | 4975 | } |
428870ff | 4976 | spa->spa_last_open_failed = 0; |
b128c09f | 4977 | mutex_exit(&spa_namespace_lock); |
34dc7c2f | 4978 | |
428870ff BB |
4979 | if (zc->zc_cookie & ZPOOL_NO_REWIND) { |
4980 | error = spa_open(zc->zc_name, &spa, FTAG); | |
4981 | } else { | |
4982 | nvlist_t *policy; | |
4983 | nvlist_t *config = NULL; | |
4984 | ||
b8864a23 | 4985 | if (zc->zc_nvlist_src == 0) |
2e528b49 | 4986 | return (SET_ERROR(EINVAL)); |
428870ff BB |
4987 | |
4988 | if ((error = get_nvlist(zc->zc_nvlist_src, | |
4989 | zc->zc_nvlist_src_size, zc->zc_iflags, &policy)) == 0) { | |
4990 | error = spa_open_rewind(zc->zc_name, &spa, FTAG, | |
4991 | policy, &config); | |
4992 | if (config != NULL) { | |
572e2857 BB |
4993 | int err; |
4994 | ||
4995 | if ((err = put_nvlist(zc, config)) != 0) | |
4996 | error = err; | |
428870ff BB |
4997 | nvlist_free(config); |
4998 | } | |
4999 | nvlist_free(policy); | |
5000 | } | |
5001 | } | |
5002 | ||
13fe0198 | 5003 | if (error != 0) |
b128c09f BB |
5004 | return (error); |
5005 | ||
428870ff | 5006 | spa_vdev_state_enter(spa, SCL_NONE); |
34dc7c2f BB |
5007 | |
5008 | if (zc->zc_guid == 0) { | |
5009 | vd = NULL; | |
b128c09f BB |
5010 | } else { |
5011 | vd = spa_lookup_by_guid(spa, zc->zc_guid, B_TRUE); | |
34dc7c2f | 5012 | if (vd == NULL) { |
b128c09f | 5013 | (void) spa_vdev_state_exit(spa, NULL, ENODEV); |
34dc7c2f | 5014 | spa_close(spa, FTAG); |
2e528b49 | 5015 | return (SET_ERROR(ENODEV)); |
34dc7c2f BB |
5016 | } |
5017 | } | |
5018 | ||
b128c09f BB |
5019 | vdev_clear(spa, vd); |
5020 | ||
3f759c0c BB |
5021 | (void) spa_vdev_state_exit(spa, spa_suspended(spa) ? |
5022 | NULL : spa->spa_root_vdev, 0); | |
34dc7c2f | 5023 | |
b128c09f BB |
5024 | /* |
5025 | * Resume any suspended I/Os. | |
5026 | */ | |
9babb374 | 5027 | if (zio_resume(spa) != 0) |
2e528b49 | 5028 | error = SET_ERROR(EIO); |
34dc7c2f BB |
5029 | |
5030 | spa_close(spa, FTAG); | |
5031 | ||
9babb374 | 5032 | return (error); |
34dc7c2f BB |
5033 | } |
5034 | ||
1bd201e7 CS |
5035 | static int |
5036 | zfs_ioc_pool_reopen(zfs_cmd_t *zc) | |
5037 | { | |
5038 | spa_t *spa; | |
5039 | int error; | |
5040 | ||
5041 | error = spa_open(zc->zc_name, &spa, FTAG); | |
13fe0198 | 5042 | if (error != 0) |
1bd201e7 CS |
5043 | return (error); |
5044 | ||
5045 | spa_vdev_state_enter(spa, SCL_NONE); | |
65947351 GW |
5046 | |
5047 | /* | |
5048 | * If a resilver is already in progress then set the | |
5049 | * spa_scrub_reopen flag to B_TRUE so that we don't restart | |
5050 | * the scan as a side effect of the reopen. Otherwise, let | |
5051 | * vdev_open() decided if a resilver is required. | |
5052 | */ | |
5053 | spa->spa_scrub_reopen = dsl_scan_resilvering(spa->spa_dsl_pool); | |
1bd201e7 | 5054 | vdev_reopen(spa->spa_root_vdev); |
65947351 GW |
5055 | spa->spa_scrub_reopen = B_FALSE; |
5056 | ||
1bd201e7 CS |
5057 | (void) spa_vdev_state_exit(spa, NULL, 0); |
5058 | spa_close(spa, FTAG); | |
5059 | return (0); | |
5060 | } | |
34dc7c2f BB |
5061 | /* |
5062 | * inputs: | |
5063 | * zc_name name of filesystem | |
34dc7c2f | 5064 | * |
428870ff BB |
5065 | * outputs: |
5066 | * zc_string name of conflicting snapshot, if there is one | |
34dc7c2f BB |
5067 | */ |
5068 | static int | |
5069 | zfs_ioc_promote(zfs_cmd_t *zc) | |
5070 | { | |
d12f91fd GDN |
5071 | dsl_pool_t *dp; |
5072 | dsl_dataset_t *ds, *ods; | |
5073 | char origin[ZFS_MAX_DATASET_NAME_LEN]; | |
34dc7c2f | 5074 | char *cp; |
d12f91fd GDN |
5075 | int error; |
5076 | ||
650258d7 | 5077 | zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; |
5078 | if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0 || | |
5079 | strchr(zc->zc_name, '%')) | |
5080 | return (SET_ERROR(EINVAL)); | |
5081 | ||
d12f91fd GDN |
5082 | error = dsl_pool_hold(zc->zc_name, FTAG, &dp); |
5083 | if (error != 0) | |
5084 | return (error); | |
5085 | ||
5086 | error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &ds); | |
5087 | if (error != 0) { | |
5088 | dsl_pool_rele(dp, FTAG); | |
5089 | return (error); | |
5090 | } | |
5091 | ||
5092 | if (!dsl_dir_is_clone(ds->ds_dir)) { | |
5093 | dsl_dataset_rele(ds, FTAG); | |
5094 | dsl_pool_rele(dp, FTAG); | |
5095 | return (SET_ERROR(EINVAL)); | |
5096 | } | |
5097 | ||
5098 | error = dsl_dataset_hold_obj(dp, | |
5099 | dsl_dir_phys(ds->ds_dir)->dd_origin_obj, FTAG, &ods); | |
5100 | if (error != 0) { | |
5101 | dsl_dataset_rele(ds, FTAG); | |
5102 | dsl_pool_rele(dp, FTAG); | |
5103 | return (error); | |
5104 | } | |
5105 | ||
5106 | dsl_dataset_name(ods, origin); | |
5107 | dsl_dataset_rele(ods, FTAG); | |
5108 | dsl_dataset_rele(ds, FTAG); | |
5109 | dsl_pool_rele(dp, FTAG); | |
34dc7c2f BB |
5110 | |
5111 | /* | |
5112 | * We don't need to unmount *all* the origin fs's snapshots, but | |
5113 | * it's easier. | |
5114 | */ | |
d12f91fd | 5115 | cp = strchr(origin, '@'); |
34dc7c2f BB |
5116 | if (cp) |
5117 | *cp = '\0'; | |
d12f91fd | 5118 | (void) dmu_objset_find(origin, |
13fe0198 | 5119 | zfs_unmount_snap_cb, NULL, DS_FIND_SNAPSHOTS); |
428870ff | 5120 | return (dsl_dataset_promote(zc->zc_name, zc->zc_string)); |
34dc7c2f BB |
5121 | } |
5122 | ||
9babb374 BB |
5123 | /* |
5124 | * Retrieve a single {user|group}{used|quota}@... property. | |
5125 | * | |
5126 | * inputs: | |
5127 | * zc_name name of filesystem | |
5128 | * zc_objset_type zfs_userquota_prop_t | |
5129 | * zc_value domain name (eg. "S-1-234-567-89") | |
5130 | * zc_guid RID/UID/GID | |
5131 | * | |
5132 | * outputs: | |
5133 | * zc_cookie property value | |
5134 | */ | |
5135 | static int | |
5136 | zfs_ioc_userspace_one(zfs_cmd_t *zc) | |
5137 | { | |
0037b49e | 5138 | zfsvfs_t *zfsvfs; |
9babb374 BB |
5139 | int error; |
5140 | ||
5141 | if (zc->zc_objset_type >= ZFS_NUM_USERQUOTA_PROPS) | |
2e528b49 | 5142 | return (SET_ERROR(EINVAL)); |
9babb374 | 5143 | |
f298b24d | 5144 | error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); |
13fe0198 | 5145 | if (error != 0) |
9babb374 BB |
5146 | return (error); |
5147 | ||
0037b49e | 5148 | error = zfs_userspace_one(zfsvfs, |
9babb374 | 5149 | zc->zc_objset_type, zc->zc_value, zc->zc_guid, &zc->zc_cookie); |
f298b24d | 5150 | zfsvfs_rele(zfsvfs, FTAG); |
9babb374 BB |
5151 | |
5152 | return (error); | |
5153 | } | |
5154 | ||
5155 | /* | |
5156 | * inputs: | |
5157 | * zc_name name of filesystem | |
5158 | * zc_cookie zap cursor | |
5159 | * zc_objset_type zfs_userquota_prop_t | |
5160 | * zc_nvlist_dst[_size] buffer to fill (not really an nvlist) | |
5161 | * | |
5162 | * outputs: | |
5163 | * zc_nvlist_dst[_size] data buffer (array of zfs_useracct_t) | |
5164 | * zc_cookie zap cursor | |
5165 | */ | |
5166 | static int | |
5167 | zfs_ioc_userspace_many(zfs_cmd_t *zc) | |
5168 | { | |
0037b49e | 5169 | zfsvfs_t *zfsvfs; |
428870ff | 5170 | int bufsize = zc->zc_nvlist_dst_size; |
3558fd73 BB |
5171 | int error; |
5172 | void *buf; | |
428870ff BB |
5173 | |
5174 | if (bufsize <= 0) | |
2e528b49 | 5175 | return (SET_ERROR(ENOMEM)); |
9babb374 | 5176 | |
f298b24d | 5177 | error = zfsvfs_hold(zc->zc_name, FTAG, &zfsvfs, B_FALSE); |
13fe0198 | 5178 | if (error != 0) |
9babb374 BB |
5179 | return (error); |
5180 | ||
2b8cad61 | 5181 | buf = vmem_alloc(bufsize, KM_SLEEP); |
9babb374 | 5182 | |
0037b49e | 5183 | error = zfs_userspace_many(zfsvfs, zc->zc_objset_type, &zc->zc_cookie, |
9babb374 BB |
5184 | buf, &zc->zc_nvlist_dst_size); |
5185 | ||
5186 | if (error == 0) { | |
5187 | error = xcopyout(buf, | |
5188 | (void *)(uintptr_t)zc->zc_nvlist_dst, | |
5189 | zc->zc_nvlist_dst_size); | |
5190 | } | |
2b8cad61 | 5191 | vmem_free(buf, bufsize); |
f298b24d | 5192 | zfsvfs_rele(zfsvfs, FTAG); |
9babb374 BB |
5193 | |
5194 | return (error); | |
5195 | } | |
5196 | ||
5197 | /* | |
5198 | * inputs: | |
5199 | * zc_name name of filesystem | |
5200 | * | |
5201 | * outputs: | |
5202 | * none | |
5203 | */ | |
5204 | static int | |
5205 | zfs_ioc_userspace_upgrade(zfs_cmd_t *zc) | |
5206 | { | |
5207 | objset_t *os; | |
428870ff | 5208 | int error = 0; |
0037b49e | 5209 | zfsvfs_t *zfsvfs; |
9babb374 | 5210 | |
f298b24d | 5211 | if (getzfsvfs(zc->zc_name, &zfsvfs) == 0) { |
0037b49e | 5212 | if (!dmu_objset_userused_enabled(zfsvfs->z_os)) { |
9babb374 BB |
5213 | /* |
5214 | * If userused is not enabled, it may be because the | |
5215 | * objset needs to be closed & reopened (to grow the | |
5216 | * objset_phys_t). Suspend/resume the fs will do that. | |
5217 | */ | |
ec923db2 GM |
5218 | dsl_dataset_t *ds; |
5219 | ||
0037b49e BB |
5220 | ds = dmu_objset_ds(zfsvfs->z_os); |
5221 | error = zfs_suspend_fs(zfsvfs); | |
831baf06 | 5222 | if (error == 0) { |
0037b49e | 5223 | dmu_objset_refresh_ownership(zfsvfs->z_os, |
b5256303 | 5224 | B_TRUE, zfsvfs); |
0037b49e | 5225 | error = zfs_resume_fs(zfsvfs, ds); |
831baf06 | 5226 | } |
9babb374 BB |
5227 | } |
5228 | if (error == 0) | |
0037b49e BB |
5229 | error = dmu_objset_userspace_upgrade(zfsvfs->z_os); |
5230 | deactivate_super(zfsvfs->z_sb); | |
9babb374 | 5231 | } else { |
428870ff | 5232 | /* XXX kind of reading contents without owning */ |
b5256303 | 5233 | error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os); |
13fe0198 | 5234 | if (error != 0) |
9babb374 BB |
5235 | return (error); |
5236 | ||
5237 | error = dmu_objset_userspace_upgrade(os); | |
b5256303 | 5238 | dmu_objset_rele_flags(os, B_TRUE, FTAG); |
9babb374 BB |
5239 | } |
5240 | ||
5241 | return (error); | |
5242 | } | |
5243 | ||
1de321e6 JX |
5244 | /* |
5245 | * inputs: | |
5246 | * zc_name name of filesystem | |
5247 | * | |
5248 | * outputs: | |
5249 | * none | |
5250 | */ | |
5251 | static int | |
5252 | zfs_ioc_userobjspace_upgrade(zfs_cmd_t *zc) | |
5253 | { | |
5254 | objset_t *os; | |
5255 | int error; | |
5256 | ||
b5256303 | 5257 | error = dmu_objset_hold_flags(zc->zc_name, B_TRUE, FTAG, &os); |
1de321e6 JX |
5258 | if (error != 0) |
5259 | return (error); | |
5260 | ||
5261 | dsl_dataset_long_hold(dmu_objset_ds(os), FTAG); | |
5262 | dsl_pool_rele(dmu_objset_pool(os), FTAG); | |
5263 | ||
5264 | if (dmu_objset_userobjspace_upgradable(os)) { | |
5265 | mutex_enter(&os->os_upgrade_lock); | |
5266 | if (os->os_upgrade_id == 0) { | |
5267 | /* clear potential error code and retry */ | |
5268 | os->os_upgrade_status = 0; | |
5269 | mutex_exit(&os->os_upgrade_lock); | |
5270 | ||
5271 | dmu_objset_userobjspace_upgrade(os); | |
5272 | } else { | |
5273 | mutex_exit(&os->os_upgrade_lock); | |
5274 | } | |
5275 | ||
5276 | taskq_wait_id(os->os_spa->spa_upgrade_taskq, os->os_upgrade_id); | |
5277 | error = os->os_upgrade_status; | |
5278 | } | |
5279 | ||
5280 | dsl_dataset_long_rele(dmu_objset_ds(os), FTAG); | |
b5256303 | 5281 | dsl_dataset_rele_flags(dmu_objset_ds(os), DS_HOLD_FLAG_DECRYPT, FTAG); |
1de321e6 JX |
5282 | |
5283 | return (error); | |
5284 | } | |
5285 | ||
34dc7c2f BB |
5286 | static int |
5287 | zfs_ioc_share(zfs_cmd_t *zc) | |
5288 | { | |
2e528b49 | 5289 | return (SET_ERROR(ENOSYS)); |
34dc7c2f BB |
5290 | } |
5291 | ||
9babb374 BB |
5292 | ace_t full_access[] = { |
5293 | {(uid_t)-1, ACE_ALL_PERMS, ACE_EVERYONE, 0} | |
5294 | }; | |
5295 | ||
572e2857 BB |
5296 | /* |
5297 | * inputs: | |
5298 | * zc_name name of containing filesystem | |
5299 | * zc_obj object # beyond which we want next in-use object # | |
5300 | * | |
5301 | * outputs: | |
5302 | * zc_obj next in-use object # | |
5303 | */ | |
5304 | static int | |
5305 | zfs_ioc_next_obj(zfs_cmd_t *zc) | |
5306 | { | |
5307 | objset_t *os = NULL; | |
5308 | int error; | |
5309 | ||
5310 | error = dmu_objset_hold(zc->zc_name, FTAG, &os); | |
13fe0198 | 5311 | if (error != 0) |
572e2857 BB |
5312 | return (error); |
5313 | ||
7290cd3c | 5314 | error = dmu_object_next(os, &zc->zc_obj, B_FALSE, 0); |
572e2857 BB |
5315 | |
5316 | dmu_objset_rele(os, FTAG); | |
5317 | return (error); | |
5318 | } | |
5319 | ||
5320 | /* | |
5321 | * inputs: | |
5322 | * zc_name name of filesystem | |
5323 | * zc_value prefix name for snapshot | |
5324 | * zc_cleanup_fd cleanup-on-exit file descriptor for calling process | |
5325 | * | |
5326 | * outputs: | |
6f1ffb06 | 5327 | * zc_value short name of new snapshot |
572e2857 BB |
5328 | */ |
5329 | static int | |
5330 | zfs_ioc_tmp_snapshot(zfs_cmd_t *zc) | |
5331 | { | |
5332 | char *snap_name; | |
13fe0198 | 5333 | char *hold_name; |
572e2857 | 5334 | int error; |
13fe0198 | 5335 | minor_t minor; |
572e2857 | 5336 | |
13fe0198 MA |
5337 | error = zfs_onexit_fd_hold(zc->zc_cleanup_fd, &minor); |
5338 | if (error != 0) | |
572e2857 | 5339 | return (error); |
572e2857 | 5340 | |
13fe0198 MA |
5341 | snap_name = kmem_asprintf("%s-%016llx", zc->zc_value, |
5342 | (u_longlong_t)ddi_get_lbolt64()); | |
5343 | hold_name = kmem_asprintf("%%%s", zc->zc_value); | |
5344 | ||
5345 | error = dsl_dataset_snapshot_tmp(zc->zc_name, snap_name, minor, | |
5346 | hold_name); | |
5347 | if (error == 0) | |
680eada9 | 5348 | (void) strlcpy(zc->zc_value, snap_name, |
5349 | sizeof (zc->zc_value)); | |
572e2857 | 5350 | strfree(snap_name); |
13fe0198 MA |
5351 | strfree(hold_name); |
5352 | zfs_onexit_fd_rele(zc->zc_cleanup_fd); | |
5353 | return (error); | |
572e2857 BB |
5354 | } |
5355 | ||
5356 | /* | |
5357 | * inputs: | |
5358 | * zc_name name of "to" snapshot | |
5359 | * zc_value name of "from" snapshot | |
5360 | * zc_cookie file descriptor to write diff data on | |
5361 | * | |
5362 | * outputs: | |
5363 | * dmu_diff_record_t's to the file descriptor | |
5364 | */ | |
5365 | static int | |
5366 | zfs_ioc_diff(zfs_cmd_t *zc) | |
5367 | { | |
572e2857 BB |
5368 | file_t *fp; |
5369 | offset_t off; | |
5370 | int error; | |
5371 | ||
572e2857 | 5372 | fp = getf(zc->zc_cookie); |
13fe0198 | 5373 | if (fp == NULL) |
2e528b49 | 5374 | return (SET_ERROR(EBADF)); |
572e2857 BB |
5375 | |
5376 | off = fp->f_offset; | |
5377 | ||
13fe0198 | 5378 | error = dmu_diff(zc->zc_name, zc->zc_value, fp->f_vnode, &off); |
572e2857 BB |
5379 | |
5380 | if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) | |
5381 | fp->f_offset = off; | |
5382 | releasef(zc->zc_cookie); | |
5383 | ||
572e2857 BB |
5384 | return (error); |
5385 | } | |
5386 | ||
9babb374 BB |
5387 | /* |
5388 | * Remove all ACL files in shares dir | |
5389 | */ | |
3c9609b3 | 5390 | #ifdef HAVE_SMB_SHARE |
9babb374 BB |
5391 | static int |
5392 | zfs_smb_acl_purge(znode_t *dzp) | |
5393 | { | |
5394 | zap_cursor_t zc; | |
5395 | zap_attribute_t zap; | |
0037b49e | 5396 | zfsvfs_t *zfsvfs = ZTOZSB(dzp); |
9babb374 BB |
5397 | int error; |
5398 | ||
0037b49e | 5399 | for (zap_cursor_init(&zc, zfsvfs->z_os, dzp->z_id); |
9babb374 BB |
5400 | (error = zap_cursor_retrieve(&zc, &zap)) == 0; |
5401 | zap_cursor_advance(&zc)) { | |
5402 | if ((error = VOP_REMOVE(ZTOV(dzp), zap.za_name, kcred, | |
5403 | NULL, 0)) != 0) | |
5404 | break; | |
5405 | } | |
5406 | zap_cursor_fini(&zc); | |
5407 | return (error); | |
5408 | } | |
3c9609b3 | 5409 | #endif /* HAVE_SMB_SHARE */ |
9babb374 BB |
5410 | |
5411 | static int | |
5412 | zfs_ioc_smb_acl(zfs_cmd_t *zc) | |
5413 | { | |
3c9609b3 | 5414 | #ifdef HAVE_SMB_SHARE |
9babb374 BB |
5415 | vnode_t *vp; |
5416 | znode_t *dzp; | |
5417 | vnode_t *resourcevp = NULL; | |
5418 | znode_t *sharedir; | |
0037b49e | 5419 | zfsvfs_t *zfsvfs; |
9babb374 BB |
5420 | nvlist_t *nvlist; |
5421 | char *src, *target; | |
5422 | vattr_t vattr; | |
5423 | vsecattr_t vsec; | |
5424 | int error = 0; | |
5425 | ||
5426 | if ((error = lookupname(zc->zc_value, UIO_SYSSPACE, | |
5427 | NO_FOLLOW, NULL, &vp)) != 0) | |
5428 | return (error); | |
5429 | ||
5430 | /* Now make sure mntpnt and dataset are ZFS */ | |
5431 | ||
5432 | if (vp->v_vfsp->vfs_fstype != zfsfstype || | |
5433 | (strcmp((char *)refstr_value(vp->v_vfsp->vfs_resource), | |
5434 | zc->zc_name) != 0)) { | |
5435 | VN_RELE(vp); | |
2e528b49 | 5436 | return (SET_ERROR(EINVAL)); |
9babb374 BB |
5437 | } |
5438 | ||
5439 | dzp = VTOZ(vp); | |
0037b49e BB |
5440 | zfsvfs = ZTOZSB(dzp); |
5441 | ZFS_ENTER(zfsvfs); | |
9babb374 BB |
5442 | |
5443 | /* | |
5444 | * Create share dir if its missing. | |
5445 | */ | |
0037b49e BB |
5446 | mutex_enter(&zfsvfs->z_lock); |
5447 | if (zfsvfs->z_shares_dir == 0) { | |
9babb374 BB |
5448 | dmu_tx_t *tx; |
5449 | ||
0037b49e | 5450 | tx = dmu_tx_create(zfsvfs->z_os); |
9babb374 BB |
5451 | dmu_tx_hold_zap(tx, MASTER_NODE_OBJ, TRUE, |
5452 | ZFS_SHARES_DIR); | |
5453 | dmu_tx_hold_zap(tx, DMU_NEW_OBJECT, FALSE, NULL); | |
5454 | error = dmu_tx_assign(tx, TXG_WAIT); | |
13fe0198 | 5455 | if (error != 0) { |
9babb374 BB |
5456 | dmu_tx_abort(tx); |
5457 | } else { | |
0037b49e | 5458 | error = zfs_create_share_dir(zfsvfs, tx); |
9babb374 BB |
5459 | dmu_tx_commit(tx); |
5460 | } | |
13fe0198 | 5461 | if (error != 0) { |
0037b49e | 5462 | mutex_exit(&zfsvfs->z_lock); |
9babb374 | 5463 | VN_RELE(vp); |
0037b49e | 5464 | ZFS_EXIT(zfsvfs); |
9babb374 BB |
5465 | return (error); |
5466 | } | |
5467 | } | |
0037b49e | 5468 | mutex_exit(&zfsvfs->z_lock); |
9babb374 | 5469 | |
0037b49e BB |
5470 | ASSERT(zfsvfs->z_shares_dir); |
5471 | if ((error = zfs_zget(zfsvfs, zfsvfs->z_shares_dir, &sharedir)) != 0) { | |
9babb374 | 5472 | VN_RELE(vp); |
0037b49e | 5473 | ZFS_EXIT(zfsvfs); |
9babb374 BB |
5474 | return (error); |
5475 | } | |
5476 | ||
5477 | switch (zc->zc_cookie) { | |
5478 | case ZFS_SMB_ACL_ADD: | |
5479 | vattr.va_mask = AT_MODE|AT_UID|AT_GID|AT_TYPE; | |
9babb374 BB |
5480 | vattr.va_mode = S_IFREG|0777; |
5481 | vattr.va_uid = 0; | |
5482 | vattr.va_gid = 0; | |
5483 | ||
5484 | vsec.vsa_mask = VSA_ACE; | |
5485 | vsec.vsa_aclentp = &full_access; | |
5486 | vsec.vsa_aclentsz = sizeof (full_access); | |
5487 | vsec.vsa_aclcnt = 1; | |
5488 | ||
5489 | error = VOP_CREATE(ZTOV(sharedir), zc->zc_string, | |
5490 | &vattr, EXCL, 0, &resourcevp, kcred, 0, NULL, &vsec); | |
5491 | if (resourcevp) | |
5492 | VN_RELE(resourcevp); | |
5493 | break; | |
5494 | ||
5495 | case ZFS_SMB_ACL_REMOVE: | |
5496 | error = VOP_REMOVE(ZTOV(sharedir), zc->zc_string, kcred, | |
5497 | NULL, 0); | |
5498 | break; | |
5499 | ||
5500 | case ZFS_SMB_ACL_RENAME: | |
5501 | if ((error = get_nvlist(zc->zc_nvlist_src, | |
5502 | zc->zc_nvlist_src_size, zc->zc_iflags, &nvlist)) != 0) { | |
5503 | VN_RELE(vp); | |
a4d179ef | 5504 | VN_RELE(ZTOV(sharedir)); |
0037b49e | 5505 | ZFS_EXIT(zfsvfs); |
9babb374 BB |
5506 | return (error); |
5507 | } | |
5508 | if (nvlist_lookup_string(nvlist, ZFS_SMB_ACL_SRC, &src) || | |
5509 | nvlist_lookup_string(nvlist, ZFS_SMB_ACL_TARGET, | |
5510 | &target)) { | |
5511 | VN_RELE(vp); | |
5512 | VN_RELE(ZTOV(sharedir)); | |
0037b49e | 5513 | ZFS_EXIT(zfsvfs); |
428870ff | 5514 | nvlist_free(nvlist); |
9babb374 BB |
5515 | return (error); |
5516 | } | |
5517 | error = VOP_RENAME(ZTOV(sharedir), src, ZTOV(sharedir), target, | |
5518 | kcred, NULL, 0); | |
5519 | nvlist_free(nvlist); | |
5520 | break; | |
5521 | ||
5522 | case ZFS_SMB_ACL_PURGE: | |
5523 | error = zfs_smb_acl_purge(sharedir); | |
5524 | break; | |
5525 | ||
5526 | default: | |
2e528b49 | 5527 | error = SET_ERROR(EINVAL); |
9babb374 BB |
5528 | break; |
5529 | } | |
5530 | ||
5531 | VN_RELE(vp); | |
5532 | VN_RELE(ZTOV(sharedir)); | |
5533 | ||
0037b49e | 5534 | ZFS_EXIT(zfsvfs); |
9babb374 BB |
5535 | |
5536 | return (error); | |
325f0235 | 5537 | #else |
2e528b49 | 5538 | return (SET_ERROR(ENOTSUP)); |
3c9609b3 | 5539 | #endif /* HAVE_SMB_SHARE */ |
9babb374 BB |
5540 | } |
5541 | ||
45d1cae3 | 5542 | /* |
13fe0198 MA |
5543 | * innvl: { |
5544 | * "holds" -> { snapname -> holdname (string), ... } | |
5545 | * (optional) "cleanup_fd" -> fd (int32) | |
5546 | * } | |
45d1cae3 | 5547 | * |
13fe0198 MA |
5548 | * outnvl: { |
5549 | * snapname -> error value (int32) | |
5550 | * ... | |
5551 | * } | |
45d1cae3 | 5552 | */ |
13fe0198 | 5553 | /* ARGSUSED */ |
45d1cae3 | 5554 | static int |
13fe0198 | 5555 | zfs_ioc_hold(const char *pool, nvlist_t *args, nvlist_t *errlist) |
45d1cae3 | 5556 | { |
fc581e05 | 5557 | nvpair_t *pair; |
13fe0198 MA |
5558 | nvlist_t *holds; |
5559 | int cleanup_fd = -1; | |
572e2857 BB |
5560 | int error; |
5561 | minor_t minor = 0; | |
45d1cae3 | 5562 | |
13fe0198 MA |
5563 | error = nvlist_lookup_nvlist(args, "holds", &holds); |
5564 | if (error != 0) | |
2e528b49 | 5565 | return (SET_ERROR(EINVAL)); |
572e2857 | 5566 | |
fc581e05 JJS |
5567 | /* make sure the user didn't pass us any invalid (empty) tags */ |
5568 | for (pair = nvlist_next_nvpair(holds, NULL); pair != NULL; | |
5569 | pair = nvlist_next_nvpair(holds, pair)) { | |
5570 | char *htag; | |
5571 | ||
5572 | error = nvpair_value_string(pair, &htag); | |
5573 | if (error != 0) | |
5574 | return (SET_ERROR(error)); | |
5575 | ||
5576 | if (strlen(htag) == 0) | |
5577 | return (SET_ERROR(EINVAL)); | |
5578 | } | |
5579 | ||
13fe0198 MA |
5580 | if (nvlist_lookup_int32(args, "cleanup_fd", &cleanup_fd) == 0) { |
5581 | error = zfs_onexit_fd_hold(cleanup_fd, &minor); | |
5582 | if (error != 0) | |
572e2857 | 5583 | return (error); |
572e2857 | 5584 | } |
572e2857 | 5585 | |
13fe0198 MA |
5586 | error = dsl_dataset_user_hold(holds, minor, errlist); |
5587 | if (minor != 0) | |
5588 | zfs_onexit_fd_rele(cleanup_fd); | |
572e2857 | 5589 | return (error); |
45d1cae3 BB |
5590 | } |
5591 | ||
5592 | /* | |
13fe0198 | 5593 | * innvl is not used. |
45d1cae3 | 5594 | * |
13fe0198 MA |
5595 | * outnvl: { |
5596 | * holdname -> time added (uint64 seconds since epoch) | |
5597 | * ... | |
5598 | * } | |
45d1cae3 | 5599 | */ |
13fe0198 | 5600 | /* ARGSUSED */ |
45d1cae3 | 5601 | static int |
13fe0198 | 5602 | zfs_ioc_get_holds(const char *snapname, nvlist_t *args, nvlist_t *outnvl) |
45d1cae3 | 5603 | { |
bec1067d | 5604 | ASSERT3P(args, ==, NULL); |
13fe0198 | 5605 | return (dsl_dataset_get_holds(snapname, outnvl)); |
45d1cae3 BB |
5606 | } |
5607 | ||
5608 | /* | |
13fe0198 MA |
5609 | * innvl: { |
5610 | * snapname -> { holdname, ... } | |
5611 | * ... | |
5612 | * } | |
45d1cae3 | 5613 | * |
13fe0198 MA |
5614 | * outnvl: { |
5615 | * snapname -> error value (int32) | |
5616 | * ... | |
5617 | * } | |
45d1cae3 | 5618 | */ |
13fe0198 | 5619 | /* ARGSUSED */ |
45d1cae3 | 5620 | static int |
13fe0198 | 5621 | zfs_ioc_release(const char *pool, nvlist_t *holds, nvlist_t *errlist) |
45d1cae3 | 5622 | { |
13fe0198 | 5623 | return (dsl_dataset_user_release(holds, errlist)); |
45d1cae3 BB |
5624 | } |
5625 | ||
26685276 BB |
5626 | /* |
5627 | * inputs: | |
5628 | * zc_guid flags (ZEVENT_NONBLOCK) | |
9b101a73 | 5629 | * zc_cleanup_fd zevent file descriptor |
26685276 BB |
5630 | * |
5631 | * outputs: | |
5632 | * zc_nvlist_dst next nvlist event | |
5633 | * zc_cookie dropped events since last get | |
26685276 BB |
5634 | */ |
5635 | static int | |
5636 | zfs_ioc_events_next(zfs_cmd_t *zc) | |
5637 | { | |
5638 | zfs_zevent_t *ze; | |
5639 | nvlist_t *event = NULL; | |
5640 | minor_t minor; | |
5641 | uint64_t dropped = 0; | |
5642 | int error; | |
5643 | ||
5644 | error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze); | |
5645 | if (error != 0) | |
5646 | return (error); | |
5647 | ||
5648 | do { | |
baa40d45 | 5649 | error = zfs_zevent_next(ze, &event, |
02730c33 | 5650 | &zc->zc_nvlist_dst_size, &dropped); |
26685276 BB |
5651 | if (event != NULL) { |
5652 | zc->zc_cookie = dropped; | |
5653 | error = put_nvlist(zc, event); | |
baa40d45 | 5654 | nvlist_free(event); |
26685276 BB |
5655 | } |
5656 | ||
5657 | if (zc->zc_guid & ZEVENT_NONBLOCK) | |
5658 | break; | |
5659 | ||
5660 | if ((error == 0) || (error != ENOENT)) | |
5661 | break; | |
5662 | ||
5663 | error = zfs_zevent_wait(ze); | |
13fe0198 | 5664 | if (error != 0) |
26685276 BB |
5665 | break; |
5666 | } while (1); | |
5667 | ||
5668 | zfs_zevent_fd_rele(zc->zc_cleanup_fd); | |
5669 | ||
5670 | return (error); | |
5671 | } | |
5672 | ||
5673 | /* | |
5674 | * outputs: | |
5675 | * zc_cookie cleared events count | |
5676 | */ | |
5677 | static int | |
5678 | zfs_ioc_events_clear(zfs_cmd_t *zc) | |
5679 | { | |
5680 | int count; | |
5681 | ||
5682 | zfs_zevent_drain_all(&count); | |
5683 | zc->zc_cookie = count; | |
5684 | ||
d1d7e268 | 5685 | return (0); |
26685276 BB |
5686 | } |
5687 | ||
75e3ff58 BB |
5688 | /* |
5689 | * inputs: | |
5690 | * zc_guid eid | ZEVENT_SEEK_START | ZEVENT_SEEK_END | |
5691 | * zc_cleanup zevent file descriptor | |
5692 | */ | |
5693 | static int | |
5694 | zfs_ioc_events_seek(zfs_cmd_t *zc) | |
5695 | { | |
5696 | zfs_zevent_t *ze; | |
5697 | minor_t minor; | |
5698 | int error; | |
5699 | ||
5700 | error = zfs_zevent_fd_hold(zc->zc_cleanup_fd, &minor, &ze); | |
5701 | if (error != 0) | |
5702 | return (error); | |
5703 | ||
5704 | error = zfs_zevent_seek(ze, zc->zc_guid); | |
5705 | zfs_zevent_fd_rele(zc->zc_cleanup_fd); | |
5706 | ||
5707 | return (error); | |
5708 | } | |
5709 | ||
330d06f9 MA |
5710 | /* |
5711 | * inputs: | |
5712 | * zc_name name of new filesystem or snapshot | |
5713 | * zc_value full name of old snapshot | |
5714 | * | |
5715 | * outputs: | |
5716 | * zc_cookie space in bytes | |
5717 | * zc_objset_type compressed space in bytes | |
5718 | * zc_perm_action uncompressed space in bytes | |
5719 | */ | |
5720 | static int | |
5721 | zfs_ioc_space_written(zfs_cmd_t *zc) | |
5722 | { | |
5723 | int error; | |
13fe0198 | 5724 | dsl_pool_t *dp; |
330d06f9 MA |
5725 | dsl_dataset_t *new, *old; |
5726 | ||
13fe0198 | 5727 | error = dsl_pool_hold(zc->zc_name, FTAG, &dp); |
330d06f9 MA |
5728 | if (error != 0) |
5729 | return (error); | |
13fe0198 MA |
5730 | error = dsl_dataset_hold(dp, zc->zc_name, FTAG, &new); |
5731 | if (error != 0) { | |
5732 | dsl_pool_rele(dp, FTAG); | |
5733 | return (error); | |
5734 | } | |
5735 | error = dsl_dataset_hold(dp, zc->zc_value, FTAG, &old); | |
330d06f9 MA |
5736 | if (error != 0) { |
5737 | dsl_dataset_rele(new, FTAG); | |
13fe0198 | 5738 | dsl_pool_rele(dp, FTAG); |
330d06f9 MA |
5739 | return (error); |
5740 | } | |
5741 | ||
5742 | error = dsl_dataset_space_written(old, new, &zc->zc_cookie, | |
5743 | &zc->zc_objset_type, &zc->zc_perm_action); | |
5744 | dsl_dataset_rele(old, FTAG); | |
5745 | dsl_dataset_rele(new, FTAG); | |
13fe0198 | 5746 | dsl_pool_rele(dp, FTAG); |
330d06f9 MA |
5747 | return (error); |
5748 | } | |
5749 | ||
5750 | /* | |
6f1ffb06 MA |
5751 | * innvl: { |
5752 | * "firstsnap" -> snapshot name | |
5753 | * } | |
330d06f9 | 5754 | * |
6f1ffb06 MA |
5755 | * outnvl: { |
5756 | * "used" -> space in bytes | |
5757 | * "compressed" -> compressed space in bytes | |
5758 | * "uncompressed" -> uncompressed space in bytes | |
5759 | * } | |
330d06f9 MA |
5760 | */ |
5761 | static int | |
6f1ffb06 | 5762 | zfs_ioc_space_snaps(const char *lastsnap, nvlist_t *innvl, nvlist_t *outnvl) |
330d06f9 MA |
5763 | { |
5764 | int error; | |
13fe0198 | 5765 | dsl_pool_t *dp; |
330d06f9 | 5766 | dsl_dataset_t *new, *old; |
6f1ffb06 MA |
5767 | char *firstsnap; |
5768 | uint64_t used, comp, uncomp; | |
330d06f9 | 5769 | |
6f1ffb06 | 5770 | if (nvlist_lookup_string(innvl, "firstsnap", &firstsnap) != 0) |
2e528b49 | 5771 | return (SET_ERROR(EINVAL)); |
6f1ffb06 | 5772 | |
13fe0198 | 5773 | error = dsl_pool_hold(lastsnap, FTAG, &dp); |
330d06f9 MA |
5774 | if (error != 0) |
5775 | return (error); | |
13fe0198 MA |
5776 | |
5777 | error = dsl_dataset_hold(dp, lastsnap, FTAG, &new); | |
71e2fe41 AG |
5778 | if (error == 0 && !new->ds_is_snapshot) { |
5779 | dsl_dataset_rele(new, FTAG); | |
5780 | error = SET_ERROR(EINVAL); | |
5781 | } | |
13fe0198 MA |
5782 | if (error != 0) { |
5783 | dsl_pool_rele(dp, FTAG); | |
5784 | return (error); | |
5785 | } | |
5786 | error = dsl_dataset_hold(dp, firstsnap, FTAG, &old); | |
71e2fe41 AG |
5787 | if (error == 0 && !old->ds_is_snapshot) { |
5788 | dsl_dataset_rele(old, FTAG); | |
5789 | error = SET_ERROR(EINVAL); | |
5790 | } | |
330d06f9 MA |
5791 | if (error != 0) { |
5792 | dsl_dataset_rele(new, FTAG); | |
13fe0198 | 5793 | dsl_pool_rele(dp, FTAG); |
330d06f9 MA |
5794 | return (error); |
5795 | } | |
5796 | ||
6f1ffb06 | 5797 | error = dsl_dataset_space_wouldfree(old, new, &used, &comp, &uncomp); |
330d06f9 MA |
5798 | dsl_dataset_rele(old, FTAG); |
5799 | dsl_dataset_rele(new, FTAG); | |
13fe0198 | 5800 | dsl_pool_rele(dp, FTAG); |
6f1ffb06 MA |
5801 | fnvlist_add_uint64(outnvl, "used", used); |
5802 | fnvlist_add_uint64(outnvl, "compressed", comp); | |
5803 | fnvlist_add_uint64(outnvl, "uncompressed", uncomp); | |
330d06f9 MA |
5804 | return (error); |
5805 | } | |
5806 | ||
34dc7c2f | 5807 | /* |
6f1ffb06 MA |
5808 | * innvl: { |
5809 | * "fd" -> file descriptor to write stream to (int32) | |
5810 | * (optional) "fromsnap" -> full snap name to send an incremental from | |
f1512ee6 MA |
5811 | * (optional) "largeblockok" -> (value ignored) |
5812 | * indicates that blocks > 128KB are permitted | |
9b67f605 MA |
5813 | * (optional) "embedok" -> (value ignored) |
5814 | * presence indicates DRR_WRITE_EMBEDDED records are permitted | |
2aa34383 DK |
5815 | * (optional) "compressok" -> (value ignored) |
5816 | * presence indicates compressed DRR_WRITE records are permitted | |
b5256303 TC |
5817 | * (optional) "rawok" -> (value ignored) |
5818 | * presence indicates raw encrypted records should be used. | |
47dfff3b MA |
5819 | * (optional) "resume_object" and "resume_offset" -> (uint64) |
5820 | * if present, resume send stream from specified object and offset. | |
6f1ffb06 MA |
5821 | * } |
5822 | * | |
5823 | * outnvl is unused | |
34dc7c2f | 5824 | */ |
6f1ffb06 MA |
5825 | /* ARGSUSED */ |
5826 | static int | |
5827 | zfs_ioc_send_new(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) | |
5828 | { | |
6f1ffb06 MA |
5829 | int error; |
5830 | offset_t off; | |
13fe0198 | 5831 | char *fromname = NULL; |
6f1ffb06 | 5832 | int fd; |
13fe0198 | 5833 | file_t *fp; |
f1512ee6 | 5834 | boolean_t largeblockok; |
9b67f605 | 5835 | boolean_t embedok; |
2aa34383 | 5836 | boolean_t compressok; |
b5256303 | 5837 | boolean_t rawok; |
47dfff3b MA |
5838 | uint64_t resumeobj = 0; |
5839 | uint64_t resumeoff = 0; | |
6f1ffb06 MA |
5840 | |
5841 | error = nvlist_lookup_int32(innvl, "fd", &fd); | |
5842 | if (error != 0) | |
2e528b49 | 5843 | return (SET_ERROR(EINVAL)); |
6f1ffb06 | 5844 | |
13fe0198 | 5845 | (void) nvlist_lookup_string(innvl, "fromsnap", &fromname); |
6f1ffb06 | 5846 | |
f1512ee6 | 5847 | largeblockok = nvlist_exists(innvl, "largeblockok"); |
9b67f605 | 5848 | embedok = nvlist_exists(innvl, "embedok"); |
2aa34383 | 5849 | compressok = nvlist_exists(innvl, "compressok"); |
b5256303 | 5850 | rawok = nvlist_exists(innvl, "rawok"); |
9b67f605 | 5851 | |
47dfff3b MA |
5852 | (void) nvlist_lookup_uint64(innvl, "resume_object", &resumeobj); |
5853 | (void) nvlist_lookup_uint64(innvl, "resume_offset", &resumeoff); | |
5854 | ||
13fe0198 | 5855 | if ((fp = getf(fd)) == NULL) |
2e528b49 | 5856 | return (SET_ERROR(EBADF)); |
6f1ffb06 MA |
5857 | |
5858 | off = fp->f_offset; | |
2aa34383 | 5859 | error = dmu_send(snapname, fromname, embedok, largeblockok, compressok, |
b5256303 | 5860 | rawok, fd, resumeobj, resumeoff, fp->f_vnode, &off); |
6f1ffb06 MA |
5861 | |
5862 | if (VOP_SEEK(fp->f_vnode, fp->f_offset, &off, NULL) == 0) | |
5863 | fp->f_offset = off; | |
13fe0198 | 5864 | |
6f1ffb06 | 5865 | releasef(fd); |
6f1ffb06 MA |
5866 | return (error); |
5867 | } | |
5868 | ||
5869 | /* | |
5870 | * Determine approximately how large a zfs send stream will be -- the number | |
5871 | * of bytes that will be written to the fd supplied to zfs_ioc_send_new(). | |
5872 | * | |
5873 | * innvl: { | |
5dc8b736 MG |
5874 | * (optional) "from" -> full snap or bookmark name to send an incremental |
5875 | * from | |
2aa34383 DK |
5876 | * (optional) "largeblockok" -> (value ignored) |
5877 | * indicates that blocks > 128KB are permitted | |
5878 | * (optional) "embedok" -> (value ignored) | |
5879 | * presence indicates DRR_WRITE_EMBEDDED records are permitted | |
5880 | * (optional) "compressok" -> (value ignored) | |
5881 | * presence indicates compressed DRR_WRITE records are permitted | |
cf7684bc | 5882 | * (optional) "rawok" -> (value ignored) |
5883 | * presence indicates raw encrypted records should be used. | |
6f1ffb06 MA |
5884 | * } |
5885 | * | |
5886 | * outnvl: { | |
5887 | * "space" -> bytes of space (uint64) | |
5888 | * } | |
5889 | */ | |
5890 | static int | |
5891 | zfs_ioc_send_space(const char *snapname, nvlist_t *innvl, nvlist_t *outnvl) | |
5892 | { | |
13fe0198 | 5893 | dsl_pool_t *dp; |
13fe0198 | 5894 | dsl_dataset_t *tosnap; |
6f1ffb06 MA |
5895 | int error; |
5896 | char *fromname; | |
2aa34383 DK |
5897 | /* LINTED E_FUNC_SET_NOT_USED */ |
5898 | boolean_t largeblockok; | |
5899 | /* LINTED E_FUNC_SET_NOT_USED */ | |
5900 | boolean_t embedok; | |
5901 | boolean_t compressok; | |
b5256303 | 5902 | boolean_t rawok; |
6f1ffb06 MA |
5903 | uint64_t space; |
5904 | ||
13fe0198 MA |
5905 | error = dsl_pool_hold(snapname, FTAG, &dp); |
5906 | if (error != 0) | |
6f1ffb06 MA |
5907 | return (error); |
5908 | ||
13fe0198 MA |
5909 | error = dsl_dataset_hold(dp, snapname, FTAG, &tosnap); |
5910 | if (error != 0) { | |
5911 | dsl_pool_rele(dp, FTAG); | |
5912 | return (error); | |
5913 | } | |
5914 | ||
2aa34383 DK |
5915 | largeblockok = nvlist_exists(innvl, "largeblockok"); |
5916 | embedok = nvlist_exists(innvl, "embedok"); | |
5917 | compressok = nvlist_exists(innvl, "compressok"); | |
b5256303 | 5918 | rawok = nvlist_exists(innvl, "rawok"); |
2aa34383 | 5919 | |
5dc8b736 | 5920 | error = nvlist_lookup_string(innvl, "from", &fromname); |
6f1ffb06 | 5921 | if (error == 0) { |
5dc8b736 MG |
5922 | if (strchr(fromname, '@') != NULL) { |
5923 | /* | |
5924 | * If from is a snapshot, hold it and use the more | |
5925 | * efficient dmu_send_estimate to estimate send space | |
5926 | * size using deadlists. | |
5927 | */ | |
5928 | dsl_dataset_t *fromsnap; | |
5929 | error = dsl_dataset_hold(dp, fromname, FTAG, &fromsnap); | |
5930 | if (error != 0) | |
5931 | goto out; | |
b5256303 TC |
5932 | error = dmu_send_estimate(tosnap, fromsnap, |
5933 | compressok || rawok, &space); | |
5dc8b736 MG |
5934 | dsl_dataset_rele(fromsnap, FTAG); |
5935 | } else if (strchr(fromname, '#') != NULL) { | |
5936 | /* | |
5937 | * If from is a bookmark, fetch the creation TXG of the | |
5938 | * snapshot it was created from and use that to find | |
5939 | * blocks that were born after it. | |
5940 | */ | |
5941 | zfs_bookmark_phys_t frombm; | |
5942 | ||
5943 | error = dsl_bookmark_lookup(dp, fromname, tosnap, | |
5944 | &frombm); | |
5945 | if (error != 0) | |
5946 | goto out; | |
5947 | error = dmu_send_estimate_from_txg(tosnap, | |
b5256303 TC |
5948 | frombm.zbm_creation_txg, compressok || rawok, |
5949 | &space); | |
5dc8b736 MG |
5950 | } else { |
5951 | /* | |
5952 | * from is not properly formatted as a snapshot or | |
5953 | * bookmark | |
5954 | */ | |
5955 | error = SET_ERROR(EINVAL); | |
5956 | goto out; | |
6f1ffb06 | 5957 | } |
5dc8b736 MG |
5958 | } else { |
5959 | // If estimating the size of a full send, use dmu_send_estimate | |
b5256303 TC |
5960 | error = dmu_send_estimate(tosnap, NULL, compressok || rawok, |
5961 | &space); | |
6f1ffb06 MA |
5962 | } |
5963 | ||
6f1ffb06 MA |
5964 | fnvlist_add_uint64(outnvl, "space", space); |
5965 | ||
5dc8b736 | 5966 | out: |
13fe0198 MA |
5967 | dsl_dataset_rele(tosnap, FTAG); |
5968 | dsl_pool_rele(dp, FTAG); | |
6f1ffb06 MA |
5969 | return (error); |
5970 | } | |
5971 | ||
bec1067d AP |
5972 | /* |
5973 | * Sync the currently open TXG to disk for the specified pool. | |
5974 | * This is somewhat similar to 'zfs_sync()'. | |
5975 | * For cases that do not result in error this ioctl will wait for | |
5976 | * the currently open TXG to commit before returning back to the caller. | |
5977 | * | |
5978 | * innvl: { | |
5979 | * "force" -> when true, force uberblock update even if there is no dirty data. | |
5980 | * In addition this will cause the vdev configuration to be written | |
5981 | * out including updating the zpool cache file. (boolean_t) | |
5982 | * } | |
5983 | * | |
5984 | * onvl is unused | |
5985 | */ | |
5986 | /* ARGSUSED */ | |
5987 | static int | |
5988 | zfs_ioc_pool_sync(const char *pool, nvlist_t *innvl, nvlist_t *onvl) | |
5989 | { | |
5990 | int err; | |
05f85a6a | 5991 | boolean_t force = B_FALSE; |
bec1067d AP |
5992 | spa_t *spa; |
5993 | ||
5994 | if ((err = spa_open(pool, &spa, FTAG)) != 0) | |
5995 | return (err); | |
5996 | ||
05f85a6a CC |
5997 | if (innvl) { |
5998 | if (nvlist_lookup_boolean_value(innvl, "force", &force) != 0) { | |
5999 | err = SET_ERROR(EINVAL); | |
6000 | goto out; | |
6001 | } | |
6002 | } | |
6003 | ||
bec1067d AP |
6004 | if (force) { |
6005 | spa_config_enter(spa, SCL_CONFIG, FTAG, RW_WRITER); | |
6006 | vdev_config_dirty(spa->spa_root_vdev); | |
6007 | spa_config_exit(spa, SCL_CONFIG, FTAG); | |
6008 | } | |
6009 | txg_wait_synced(spa_get_dsl(spa), 0); | |
05f85a6a | 6010 | out: |
bec1067d AP |
6011 | spa_close(spa, FTAG); |
6012 | ||
6013 | return (err); | |
6014 | } | |
6015 | ||
b5256303 TC |
6016 | /* |
6017 | * Load a user's wrapping key into the kernel. | |
6018 | * innvl: { | |
6019 | * "hidden_args" -> { "wkeydata" -> value } | |
6020 | * raw uint8_t array of encryption wrapping key data (32 bytes) | |
6021 | * (optional) "noop" -> (value ignored) | |
6022 | * presence indicated key should only be verified, not loaded | |
6023 | * } | |
6024 | */ | |
6025 | /* ARGSUSED */ | |
6026 | static int | |
6027 | zfs_ioc_load_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl) | |
6028 | { | |
6029 | int ret; | |
6030 | dsl_crypto_params_t *dcp = NULL; | |
6031 | nvlist_t *hidden_args; | |
6032 | boolean_t noop = nvlist_exists(innvl, "noop"); | |
6033 | ||
6034 | if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) { | |
6035 | ret = SET_ERROR(EINVAL); | |
6036 | goto error; | |
6037 | } | |
6038 | ||
6039 | ret = nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args); | |
6040 | if (ret != 0) { | |
6041 | ret = SET_ERROR(EINVAL); | |
6042 | goto error; | |
6043 | } | |
6044 | ||
6045 | ret = dsl_crypto_params_create_nvlist(DCP_CMD_NONE, NULL, | |
6046 | hidden_args, &dcp); | |
6047 | if (ret != 0) | |
6048 | goto error; | |
6049 | ||
6050 | ret = spa_keystore_load_wkey(dsname, dcp, noop); | |
6051 | if (ret != 0) | |
6052 | goto error; | |
6053 | ||
6054 | dsl_crypto_params_free(dcp, noop); | |
6055 | ||
6056 | return (0); | |
6057 | ||
6058 | error: | |
6059 | dsl_crypto_params_free(dcp, B_TRUE); | |
6060 | return (ret); | |
6061 | } | |
6062 | ||
6063 | /* | |
6064 | * Unload a user's wrapping key from the kernel. | |
6065 | * Both innvl and outnvl are unused. | |
6066 | */ | |
6067 | /* ARGSUSED */ | |
6068 | static int | |
6069 | zfs_ioc_unload_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl) | |
6070 | { | |
6071 | int ret = 0; | |
6072 | ||
6073 | if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) { | |
6074 | ret = (SET_ERROR(EINVAL)); | |
6075 | goto out; | |
6076 | } | |
6077 | ||
6078 | ret = spa_keystore_unload_wkey(dsname); | |
6079 | if (ret != 0) | |
6080 | goto out; | |
6081 | ||
6082 | out: | |
6083 | return (ret); | |
6084 | } | |
6085 | ||
6086 | /* | |
6087 | * Changes a user's wrapping key used to decrypt a dataset. The keyformat, | |
6088 | * keylocation, pbkdf2salt, and pbkdf2iters properties can also be specified | |
6089 | * here to change how the key is derived in userspace. | |
6090 | * | |
6091 | * innvl: { | |
6092 | * "hidden_args" (optional) -> { "wkeydata" -> value } | |
6093 | * raw uint8_t array of new encryption wrapping key data (32 bytes) | |
6094 | * "props" (optional) -> { prop -> value } | |
6095 | * } | |
6096 | * | |
6097 | * outnvl is unused | |
6098 | */ | |
6099 | /* ARGSUSED */ | |
6100 | static int | |
6101 | zfs_ioc_change_key(const char *dsname, nvlist_t *innvl, nvlist_t *outnvl) | |
6102 | { | |
6103 | int ret; | |
6104 | uint64_t cmd = DCP_CMD_NONE; | |
6105 | dsl_crypto_params_t *dcp = NULL; | |
6106 | nvlist_t *args = NULL, *hidden_args = NULL; | |
6107 | ||
6108 | if (strchr(dsname, '@') != NULL || strchr(dsname, '%') != NULL) { | |
6109 | ret = (SET_ERROR(EINVAL)); | |
6110 | goto error; | |
6111 | } | |
6112 | ||
6113 | (void) nvlist_lookup_uint64(innvl, "crypt_cmd", &cmd); | |
6114 | (void) nvlist_lookup_nvlist(innvl, "props", &args); | |
6115 | (void) nvlist_lookup_nvlist(innvl, ZPOOL_HIDDEN_ARGS, &hidden_args); | |
6116 | ||
6117 | ret = dsl_crypto_params_create_nvlist(cmd, args, hidden_args, &dcp); | |
6118 | if (ret != 0) | |
6119 | goto error; | |
6120 | ||
6121 | ret = spa_keystore_change_key(dsname, dcp); | |
6122 | if (ret != 0) | |
6123 | goto error; | |
6124 | ||
6125 | dsl_crypto_params_free(dcp, B_FALSE); | |
6126 | ||
6127 | return (0); | |
6128 | ||
6129 | error: | |
6130 | dsl_crypto_params_free(dcp, B_TRUE); | |
6131 | return (ret); | |
6132 | } | |
6133 | ||
6f1ffb06 MA |
6134 | static zfs_ioc_vec_t zfs_ioc_vec[ZFS_IOC_LAST - ZFS_IOC_FIRST]; |
6135 | ||
6136 | static void | |
6137 | zfs_ioctl_register_legacy(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, | |
6138 | zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, | |
6139 | boolean_t log_history, zfs_ioc_poolcheck_t pool_check) | |
6140 | { | |
6141 | zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; | |
6142 | ||
6143 | ASSERT3U(ioc, >=, ZFS_IOC_FIRST); | |
6144 | ASSERT3U(ioc, <, ZFS_IOC_LAST); | |
6145 | ASSERT3P(vec->zvec_legacy_func, ==, NULL); | |
6146 | ASSERT3P(vec->zvec_func, ==, NULL); | |
6147 | ||
6148 | vec->zvec_legacy_func = func; | |
6149 | vec->zvec_secpolicy = secpolicy; | |
6150 | vec->zvec_namecheck = namecheck; | |
6151 | vec->zvec_allow_log = log_history; | |
6152 | vec->zvec_pool_check = pool_check; | |
6153 | } | |
6154 | ||
6155 | /* | |
6156 | * See the block comment at the beginning of this file for details on | |
6157 | * each argument to this function. | |
6158 | */ | |
6159 | static void | |
6160 | zfs_ioctl_register(const char *name, zfs_ioc_t ioc, zfs_ioc_func_t *func, | |
6161 | zfs_secpolicy_func_t *secpolicy, zfs_ioc_namecheck_t namecheck, | |
6162 | zfs_ioc_poolcheck_t pool_check, boolean_t smush_outnvlist, | |
6163 | boolean_t allow_log) | |
6164 | { | |
6165 | zfs_ioc_vec_t *vec = &zfs_ioc_vec[ioc - ZFS_IOC_FIRST]; | |
6166 | ||
6167 | ASSERT3U(ioc, >=, ZFS_IOC_FIRST); | |
6168 | ASSERT3U(ioc, <, ZFS_IOC_LAST); | |
6169 | ASSERT3P(vec->zvec_legacy_func, ==, NULL); | |
6170 | ASSERT3P(vec->zvec_func, ==, NULL); | |
6171 | ||
6172 | /* if we are logging, the name must be valid */ | |
6173 | ASSERT(!allow_log || namecheck != NO_NAME); | |
6174 | ||
6175 | vec->zvec_name = name; | |
6176 | vec->zvec_func = func; | |
6177 | vec->zvec_secpolicy = secpolicy; | |
6178 | vec->zvec_namecheck = namecheck; | |
6179 | vec->zvec_pool_check = pool_check; | |
6180 | vec->zvec_smush_outnvlist = smush_outnvlist; | |
6181 | vec->zvec_allow_log = allow_log; | |
6182 | } | |
6183 | ||
6184 | static void | |
6185 | zfs_ioctl_register_pool(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, | |
6186 | zfs_secpolicy_func_t *secpolicy, boolean_t log_history, | |
6187 | zfs_ioc_poolcheck_t pool_check) | |
6188 | { | |
6189 | zfs_ioctl_register_legacy(ioc, func, secpolicy, | |
6190 | POOL_NAME, log_history, pool_check); | |
6191 | } | |
6192 | ||
6193 | static void | |
6194 | zfs_ioctl_register_dataset_nolog(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, | |
6195 | zfs_secpolicy_func_t *secpolicy, zfs_ioc_poolcheck_t pool_check) | |
6196 | { | |
6197 | zfs_ioctl_register_legacy(ioc, func, secpolicy, | |
6198 | DATASET_NAME, B_FALSE, pool_check); | |
6199 | } | |
6200 | ||
6201 | static void | |
6202 | zfs_ioctl_register_pool_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) | |
6203 | { | |
6204 | zfs_ioctl_register_legacy(ioc, func, zfs_secpolicy_config, | |
6205 | POOL_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); | |
6206 | } | |
6207 | ||
6208 | static void | |
6209 | zfs_ioctl_register_pool_meta(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, | |
6210 | zfs_secpolicy_func_t *secpolicy) | |
6211 | { | |
6212 | zfs_ioctl_register_legacy(ioc, func, secpolicy, | |
6213 | NO_NAME, B_FALSE, POOL_CHECK_NONE); | |
6214 | } | |
6215 | ||
6216 | static void | |
6217 | zfs_ioctl_register_dataset_read_secpolicy(zfs_ioc_t ioc, | |
6218 | zfs_ioc_legacy_func_t *func, zfs_secpolicy_func_t *secpolicy) | |
6219 | { | |
6220 | zfs_ioctl_register_legacy(ioc, func, secpolicy, | |
6221 | DATASET_NAME, B_FALSE, POOL_CHECK_SUSPENDED); | |
6222 | } | |
6223 | ||
6224 | static void | |
6225 | zfs_ioctl_register_dataset_read(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func) | |
6226 | { | |
6227 | zfs_ioctl_register_dataset_read_secpolicy(ioc, func, | |
6228 | zfs_secpolicy_read); | |
6229 | } | |
6230 | ||
6231 | static void | |
6232 | zfs_ioctl_register_dataset_modify(zfs_ioc_t ioc, zfs_ioc_legacy_func_t *func, | |
e9aa730c | 6233 | zfs_secpolicy_func_t *secpolicy) |
6f1ffb06 MA |
6234 | { |
6235 | zfs_ioctl_register_legacy(ioc, func, secpolicy, | |
6236 | DATASET_NAME, B_TRUE, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); | |
6237 | } | |
6238 | ||
6239 | static void | |
6240 | zfs_ioctl_init(void) | |
6241 | { | |
6242 | zfs_ioctl_register("snapshot", ZFS_IOC_SNAPSHOT, | |
6243 | zfs_ioc_snapshot, zfs_secpolicy_snapshot, POOL_NAME, | |
6244 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
6245 | ||
6246 | zfs_ioctl_register("log_history", ZFS_IOC_LOG_HISTORY, | |
6247 | zfs_ioc_log_history, zfs_secpolicy_log_history, NO_NAME, | |
6248 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE); | |
6249 | ||
6250 | zfs_ioctl_register("space_snaps", ZFS_IOC_SPACE_SNAPS, | |
6251 | zfs_ioc_space_snaps, zfs_secpolicy_read, DATASET_NAME, | |
6252 | POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); | |
6253 | ||
6254 | zfs_ioctl_register("send", ZFS_IOC_SEND_NEW, | |
6255 | zfs_ioc_send_new, zfs_secpolicy_send_new, DATASET_NAME, | |
6256 | POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); | |
6257 | ||
6258 | zfs_ioctl_register("send_space", ZFS_IOC_SEND_SPACE, | |
6259 | zfs_ioc_send_space, zfs_secpolicy_read, DATASET_NAME, | |
6260 | POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); | |
6261 | ||
6262 | zfs_ioctl_register("create", ZFS_IOC_CREATE, | |
6263 | zfs_ioc_create, zfs_secpolicy_create_clone, DATASET_NAME, | |
6264 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
6265 | ||
6266 | zfs_ioctl_register("clone", ZFS_IOC_CLONE, | |
6267 | zfs_ioc_clone, zfs_secpolicy_create_clone, DATASET_NAME, | |
6268 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
6269 | ||
6270 | zfs_ioctl_register("destroy_snaps", ZFS_IOC_DESTROY_SNAPS, | |
6271 | zfs_ioc_destroy_snaps, zfs_secpolicy_destroy_snaps, POOL_NAME, | |
6272 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
6273 | ||
13fe0198 MA |
6274 | zfs_ioctl_register("hold", ZFS_IOC_HOLD, |
6275 | zfs_ioc_hold, zfs_secpolicy_hold, POOL_NAME, | |
6276 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
6277 | zfs_ioctl_register("release", ZFS_IOC_RELEASE, | |
6278 | zfs_ioc_release, zfs_secpolicy_release, POOL_NAME, | |
6279 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
6280 | ||
6281 | zfs_ioctl_register("get_holds", ZFS_IOC_GET_HOLDS, | |
6282 | zfs_ioc_get_holds, zfs_secpolicy_read, DATASET_NAME, | |
6283 | POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); | |
6284 | ||
46ba1e59 MA |
6285 | zfs_ioctl_register("rollback", ZFS_IOC_ROLLBACK, |
6286 | zfs_ioc_rollback, zfs_secpolicy_rollback, DATASET_NAME, | |
6287 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_TRUE); | |
6288 | ||
da536844 MA |
6289 | zfs_ioctl_register("bookmark", ZFS_IOC_BOOKMARK, |
6290 | zfs_ioc_bookmark, zfs_secpolicy_bookmark, POOL_NAME, | |
6291 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
6292 | ||
6293 | zfs_ioctl_register("get_bookmarks", ZFS_IOC_GET_BOOKMARKS, | |
6294 | zfs_ioc_get_bookmarks, zfs_secpolicy_read, DATASET_NAME, | |
6295 | POOL_CHECK_SUSPENDED, B_FALSE, B_FALSE); | |
6296 | ||
6297 | zfs_ioctl_register("destroy_bookmarks", ZFS_IOC_DESTROY_BOOKMARKS, | |
6298 | zfs_ioc_destroy_bookmarks, zfs_secpolicy_destroy_bookmarks, | |
6299 | POOL_NAME, | |
6300 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
6301 | ||
43e52edd BB |
6302 | zfs_ioctl_register("receive", ZFS_IOC_RECV_NEW, |
6303 | zfs_ioc_recv_new, zfs_secpolicy_recv_new, DATASET_NAME, | |
6304 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_TRUE, B_TRUE); | |
b5256303 TC |
6305 | zfs_ioctl_register("load-key", ZFS_IOC_LOAD_KEY, |
6306 | zfs_ioc_load_key, zfs_secpolicy_load_key, | |
6307 | DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE); | |
6308 | zfs_ioctl_register("unload-key", ZFS_IOC_UNLOAD_KEY, | |
6309 | zfs_ioc_unload_key, zfs_secpolicy_load_key, | |
6310 | DATASET_NAME, POOL_CHECK_SUSPENDED, B_TRUE, B_TRUE); | |
6311 | zfs_ioctl_register("change-key", ZFS_IOC_CHANGE_KEY, | |
6312 | zfs_ioc_change_key, zfs_secpolicy_change_key, | |
6313 | DATASET_NAME, POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, | |
6314 | B_TRUE, B_TRUE); | |
43e52edd | 6315 | |
bec1067d AP |
6316 | zfs_ioctl_register("sync", ZFS_IOC_POOL_SYNC, |
6317 | zfs_ioc_pool_sync, zfs_secpolicy_none, POOL_NAME, | |
6318 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY, B_FALSE, B_FALSE); | |
6319 | ||
6f1ffb06 MA |
6320 | /* IOCTLS that use the legacy function signature */ |
6321 | ||
6322 | zfs_ioctl_register_legacy(ZFS_IOC_POOL_FREEZE, zfs_ioc_pool_freeze, | |
6323 | zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_READONLY); | |
6324 | ||
6325 | zfs_ioctl_register_pool(ZFS_IOC_POOL_CREATE, zfs_ioc_pool_create, | |
6326 | zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); | |
6327 | zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SCAN, | |
6328 | zfs_ioc_pool_scan); | |
6329 | zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_UPGRADE, | |
6330 | zfs_ioc_pool_upgrade); | |
6331 | zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ADD, | |
6332 | zfs_ioc_vdev_add); | |
6333 | zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_REMOVE, | |
6334 | zfs_ioc_vdev_remove); | |
6335 | zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SET_STATE, | |
6336 | zfs_ioc_vdev_set_state); | |
6337 | zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_ATTACH, | |
6338 | zfs_ioc_vdev_attach); | |
6339 | zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_DETACH, | |
6340 | zfs_ioc_vdev_detach); | |
6341 | zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETPATH, | |
6342 | zfs_ioc_vdev_setpath); | |
6343 | zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SETFRU, | |
6344 | zfs_ioc_vdev_setfru); | |
6345 | zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_SET_PROPS, | |
6346 | zfs_ioc_pool_set_props); | |
6347 | zfs_ioctl_register_pool_modify(ZFS_IOC_VDEV_SPLIT, | |
6348 | zfs_ioc_vdev_split); | |
6349 | zfs_ioctl_register_pool_modify(ZFS_IOC_POOL_REGUID, | |
6350 | zfs_ioc_pool_reguid); | |
6351 | ||
6352 | zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_CONFIGS, | |
6353 | zfs_ioc_pool_configs, zfs_secpolicy_none); | |
6354 | zfs_ioctl_register_pool_meta(ZFS_IOC_POOL_TRYIMPORT, | |
6355 | zfs_ioc_pool_tryimport, zfs_secpolicy_config); | |
6356 | zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_FAULT, | |
6357 | zfs_ioc_inject_fault, zfs_secpolicy_inject); | |
6358 | zfs_ioctl_register_pool_meta(ZFS_IOC_CLEAR_FAULT, | |
6359 | zfs_ioc_clear_fault, zfs_secpolicy_inject); | |
6360 | zfs_ioctl_register_pool_meta(ZFS_IOC_INJECT_LIST_NEXT, | |
6361 | zfs_ioc_inject_list_next, zfs_secpolicy_inject); | |
6362 | ||
6363 | /* | |
6364 | * pool destroy, and export don't log the history as part of | |
6365 | * zfsdev_ioctl, but rather zfs_ioc_pool_export | |
6366 | * does the logging of those commands. | |
6367 | */ | |
6368 | zfs_ioctl_register_pool(ZFS_IOC_POOL_DESTROY, zfs_ioc_pool_destroy, | |
87a63dd7 | 6369 | zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); |
6f1ffb06 | 6370 | zfs_ioctl_register_pool(ZFS_IOC_POOL_EXPORT, zfs_ioc_pool_export, |
87a63dd7 | 6371 | zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); |
6f1ffb06 MA |
6372 | |
6373 | zfs_ioctl_register_pool(ZFS_IOC_POOL_STATS, zfs_ioc_pool_stats, | |
6374 | zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); | |
6375 | zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_PROPS, zfs_ioc_pool_get_props, | |
6376 | zfs_secpolicy_read, B_FALSE, POOL_CHECK_NONE); | |
6377 | ||
6378 | zfs_ioctl_register_pool(ZFS_IOC_ERROR_LOG, zfs_ioc_error_log, | |
6379 | zfs_secpolicy_inject, B_FALSE, POOL_CHECK_SUSPENDED); | |
6380 | zfs_ioctl_register_pool(ZFS_IOC_DSOBJ_TO_DSNAME, | |
6381 | zfs_ioc_dsobj_to_dsname, | |
6382 | zfs_secpolicy_diff, B_FALSE, POOL_CHECK_SUSPENDED); | |
6383 | zfs_ioctl_register_pool(ZFS_IOC_POOL_GET_HISTORY, | |
6384 | zfs_ioc_pool_get_history, | |
6385 | zfs_secpolicy_config, B_FALSE, POOL_CHECK_SUSPENDED); | |
6386 | ||
6387 | zfs_ioctl_register_pool(ZFS_IOC_POOL_IMPORT, zfs_ioc_pool_import, | |
6388 | zfs_secpolicy_config, B_TRUE, POOL_CHECK_NONE); | |
6389 | ||
6390 | zfs_ioctl_register_pool(ZFS_IOC_CLEAR, zfs_ioc_clear, | |
92e43c17 | 6391 | zfs_secpolicy_config, B_TRUE, POOL_CHECK_READONLY); |
6f1ffb06 MA |
6392 | zfs_ioctl_register_pool(ZFS_IOC_POOL_REOPEN, zfs_ioc_pool_reopen, |
6393 | zfs_secpolicy_config, B_TRUE, POOL_CHECK_SUSPENDED); | |
6394 | ||
6395 | zfs_ioctl_register_dataset_read(ZFS_IOC_SPACE_WRITTEN, | |
6396 | zfs_ioc_space_written); | |
6f1ffb06 MA |
6397 | zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_RECVD_PROPS, |
6398 | zfs_ioc_objset_recvd_props); | |
6399 | zfs_ioctl_register_dataset_read(ZFS_IOC_NEXT_OBJ, | |
6400 | zfs_ioc_next_obj); | |
6401 | zfs_ioctl_register_dataset_read(ZFS_IOC_GET_FSACL, | |
6402 | zfs_ioc_get_fsacl); | |
6403 | zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_STATS, | |
6404 | zfs_ioc_objset_stats); | |
6405 | zfs_ioctl_register_dataset_read(ZFS_IOC_OBJSET_ZPLPROPS, | |
6406 | zfs_ioc_objset_zplprops); | |
6407 | zfs_ioctl_register_dataset_read(ZFS_IOC_DATASET_LIST_NEXT, | |
6408 | zfs_ioc_dataset_list_next); | |
6409 | zfs_ioctl_register_dataset_read(ZFS_IOC_SNAPSHOT_LIST_NEXT, | |
6410 | zfs_ioc_snapshot_list_next); | |
6411 | zfs_ioctl_register_dataset_read(ZFS_IOC_SEND_PROGRESS, | |
6412 | zfs_ioc_send_progress); | |
6413 | ||
6414 | zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_DIFF, | |
6415 | zfs_ioc_diff, zfs_secpolicy_diff); | |
6416 | zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_STATS, | |
6417 | zfs_ioc_obj_to_stats, zfs_secpolicy_diff); | |
6418 | zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_OBJ_TO_PATH, | |
6419 | zfs_ioc_obj_to_path, zfs_secpolicy_diff); | |
6420 | zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_ONE, | |
6421 | zfs_ioc_userspace_one, zfs_secpolicy_userspace_one); | |
6422 | zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_USERSPACE_MANY, | |
6423 | zfs_ioc_userspace_many, zfs_secpolicy_userspace_many); | |
6424 | zfs_ioctl_register_dataset_read_secpolicy(ZFS_IOC_SEND, | |
6425 | zfs_ioc_send, zfs_secpolicy_send); | |
6426 | ||
6427 | zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_PROP, zfs_ioc_set_prop, | |
6428 | zfs_secpolicy_none); | |
6429 | zfs_ioctl_register_dataset_modify(ZFS_IOC_DESTROY, zfs_ioc_destroy, | |
6430 | zfs_secpolicy_destroy); | |
6f1ffb06 MA |
6431 | zfs_ioctl_register_dataset_modify(ZFS_IOC_RENAME, zfs_ioc_rename, |
6432 | zfs_secpolicy_rename); | |
6433 | zfs_ioctl_register_dataset_modify(ZFS_IOC_RECV, zfs_ioc_recv, | |
6434 | zfs_secpolicy_recv); | |
6435 | zfs_ioctl_register_dataset_modify(ZFS_IOC_PROMOTE, zfs_ioc_promote, | |
6436 | zfs_secpolicy_promote); | |
6f1ffb06 MA |
6437 | zfs_ioctl_register_dataset_modify(ZFS_IOC_INHERIT_PROP, |
6438 | zfs_ioc_inherit_prop, zfs_secpolicy_inherit_prop); | |
6439 | zfs_ioctl_register_dataset_modify(ZFS_IOC_SET_FSACL, zfs_ioc_set_fsacl, | |
6440 | zfs_secpolicy_set_fsacl); | |
6441 | ||
6442 | zfs_ioctl_register_dataset_nolog(ZFS_IOC_SHARE, zfs_ioc_share, | |
6443 | zfs_secpolicy_share, POOL_CHECK_NONE); | |
6444 | zfs_ioctl_register_dataset_nolog(ZFS_IOC_SMB_ACL, zfs_ioc_smb_acl, | |
6445 | zfs_secpolicy_smb_acl, POOL_CHECK_NONE); | |
6446 | zfs_ioctl_register_dataset_nolog(ZFS_IOC_USERSPACE_UPGRADE, | |
6447 | zfs_ioc_userspace_upgrade, zfs_secpolicy_userspace_upgrade, | |
6448 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); | |
6449 | zfs_ioctl_register_dataset_nolog(ZFS_IOC_TMP_SNAPSHOT, | |
6450 | zfs_ioc_tmp_snapshot, zfs_secpolicy_tmp_snapshot, | |
6451 | POOL_CHECK_SUSPENDED | POOL_CHECK_READONLY); | |
6452 | ||
6453 | /* | |
ba6a2402 | 6454 | * ZoL functions |
6f1ffb06 | 6455 | */ |
6f1ffb06 MA |
6456 | zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_NEXT, zfs_ioc_events_next, |
6457 | zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE); | |
6458 | zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_CLEAR, zfs_ioc_events_clear, | |
6459 | zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE); | |
75e3ff58 BB |
6460 | zfs_ioctl_register_legacy(ZFS_IOC_EVENTS_SEEK, zfs_ioc_events_seek, |
6461 | zfs_secpolicy_config, NO_NAME, B_FALSE, POOL_CHECK_NONE); | |
6f1ffb06 | 6462 | } |
34dc7c2f | 6463 | |
9babb374 | 6464 | int |
572e2857 BB |
6465 | pool_status_check(const char *name, zfs_ioc_namecheck_t type, |
6466 | zfs_ioc_poolcheck_t check) | |
9babb374 BB |
6467 | { |
6468 | spa_t *spa; | |
6469 | int error; | |
6470 | ||
6471 | ASSERT(type == POOL_NAME || type == DATASET_NAME); | |
6472 | ||
572e2857 BB |
6473 | if (check & POOL_CHECK_NONE) |
6474 | return (0); | |
6475 | ||
9babb374 BB |
6476 | error = spa_open(name, &spa, FTAG); |
6477 | if (error == 0) { | |
572e2857 | 6478 | if ((check & POOL_CHECK_SUSPENDED) && spa_suspended(spa)) |
2e528b49 | 6479 | error = SET_ERROR(EAGAIN); |
572e2857 | 6480 | else if ((check & POOL_CHECK_READONLY) && !spa_writeable(spa)) |
2e528b49 | 6481 | error = SET_ERROR(EROFS); |
9babb374 BB |
6482 | spa_close(spa, FTAG); |
6483 | } | |
6484 | return (error); | |
6485 | } | |
6486 | ||
325f0235 BB |
6487 | static void * |
6488 | zfsdev_get_state_impl(minor_t minor, enum zfsdev_state_type which) | |
6489 | { | |
6490 | zfsdev_state_t *zs; | |
6491 | ||
3937ab20 | 6492 | for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) { |
325f0235 | 6493 | if (zs->zs_minor == minor) { |
3937ab20 | 6494 | smp_rmb(); |
325f0235 | 6495 | switch (which) { |
d1d7e268 MK |
6496 | case ZST_ONEXIT: |
6497 | return (zs->zs_onexit); | |
6498 | case ZST_ZEVENT: | |
6499 | return (zs->zs_zevent); | |
6500 | case ZST_ALL: | |
6501 | return (zs); | |
325f0235 BB |
6502 | } |
6503 | } | |
6504 | } | |
6505 | ||
d1d7e268 | 6506 | return (NULL); |
325f0235 BB |
6507 | } |
6508 | ||
6509 | void * | |
6510 | zfsdev_get_state(minor_t minor, enum zfsdev_state_type which) | |
6511 | { | |
6512 | void *ptr; | |
6513 | ||
325f0235 | 6514 | ptr = zfsdev_get_state_impl(minor, which); |
325f0235 | 6515 | |
d1d7e268 | 6516 | return (ptr); |
325f0235 BB |
6517 | } |
6518 | ||
72540ea3 RY |
6519 | int |
6520 | zfsdev_getminor(struct file *filp, minor_t *minorp) | |
325f0235 | 6521 | { |
72540ea3 RY |
6522 | zfsdev_state_t *zs, *fpd; |
6523 | ||
325f0235 | 6524 | ASSERT(filp != NULL); |
72540ea3 RY |
6525 | ASSERT(!MUTEX_HELD(&zfsdev_state_lock)); |
6526 | ||
6527 | fpd = filp->private_data; | |
6528 | if (fpd == NULL) | |
ecb2b7dc | 6529 | return (SET_ERROR(EBADF)); |
72540ea3 RY |
6530 | |
6531 | mutex_enter(&zfsdev_state_lock); | |
6532 | ||
6533 | for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) { | |
6534 | ||
6535 | if (zs->zs_minor == -1) | |
6536 | continue; | |
6537 | ||
6538 | if (fpd == zs) { | |
6539 | *minorp = fpd->zs_minor; | |
6540 | mutex_exit(&zfsdev_state_lock); | |
6541 | return (0); | |
6542 | } | |
6543 | } | |
6544 | ||
6545 | mutex_exit(&zfsdev_state_lock); | |
325f0235 | 6546 | |
ecb2b7dc | 6547 | return (SET_ERROR(EBADF)); |
325f0235 BB |
6548 | } |
6549 | ||
572e2857 | 6550 | /* |
325f0235 BB |
6551 | * Find a free minor number. The zfsdev_state_list is expected to |
6552 | * be short since it is only a list of currently open file handles. | |
572e2857 BB |
6553 | */ |
6554 | minor_t | |
6555 | zfsdev_minor_alloc(void) | |
6556 | { | |
325f0235 | 6557 | static minor_t last_minor = 0; |
572e2857 BB |
6558 | minor_t m; |
6559 | ||
6560 | ASSERT(MUTEX_HELD(&zfsdev_state_lock)); | |
6561 | ||
6562 | for (m = last_minor + 1; m != last_minor; m++) { | |
6563 | if (m > ZFSDEV_MAX_MINOR) | |
6564 | m = 1; | |
325f0235 | 6565 | if (zfsdev_get_state_impl(m, ZST_ALL) == NULL) { |
572e2857 BB |
6566 | last_minor = m; |
6567 | return (m); | |
6568 | } | |
6569 | } | |
6570 | ||
6571 | return (0); | |
6572 | } | |
6573 | ||
6574 | static int | |
325f0235 | 6575 | zfsdev_state_init(struct file *filp) |
572e2857 | 6576 | { |
3937ab20 | 6577 | zfsdev_state_t *zs, *zsprev = NULL; |
572e2857 | 6578 | minor_t minor; |
3937ab20 | 6579 | boolean_t newzs = B_FALSE; |
572e2857 BB |
6580 | |
6581 | ASSERT(MUTEX_HELD(&zfsdev_state_lock)); | |
572e2857 | 6582 | |
d1d7e268 MK |
6583 | minor = zfsdev_minor_alloc(); |
6584 | if (minor == 0) | |
6585 | return (SET_ERROR(ENXIO)); | |
325f0235 | 6586 | |
3937ab20 TC |
6587 | for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) { |
6588 | if (zs->zs_minor == -1) | |
6589 | break; | |
6590 | zsprev = zs; | |
6591 | } | |
6592 | ||
6593 | if (!zs) { | |
6594 | zs = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP); | |
6595 | newzs = B_TRUE; | |
6596 | } | |
572e2857 | 6597 | |
325f0235 | 6598 | zs->zs_file = filp; |
325f0235 | 6599 | filp->private_data = zs; |
572e2857 | 6600 | |
325f0235 BB |
6601 | zfs_onexit_init((zfs_onexit_t **)&zs->zs_onexit); |
6602 | zfs_zevent_init((zfs_zevent_t **)&zs->zs_zevent); | |
572e2857 | 6603 | |
3937ab20 TC |
6604 | |
6605 | /* | |
6606 | * In order to provide for lock-free concurrent read access | |
6607 | * to the minor list in zfsdev_get_state_impl(), new entries | |
6608 | * must be completely written before linking them into the | |
6609 | * list whereas existing entries are already linked; the last | |
6610 | * operation must be updating zs_minor (from -1 to the new | |
6611 | * value). | |
6612 | */ | |
6613 | if (newzs) { | |
6614 | zs->zs_minor = minor; | |
6615 | smp_wmb(); | |
6616 | zsprev->zs_next = zs; | |
6617 | } else { | |
6618 | smp_wmb(); | |
6619 | zs->zs_minor = minor; | |
6620 | } | |
572e2857 BB |
6621 | |
6622 | return (0); | |
6623 | } | |
6624 | ||
325f0235 BB |
6625 | static int |
6626 | zfsdev_state_destroy(struct file *filp) | |
572e2857 | 6627 | { |
325f0235 | 6628 | zfsdev_state_t *zs; |
572e2857 | 6629 | |
325f0235 BB |
6630 | ASSERT(MUTEX_HELD(&zfsdev_state_lock)); |
6631 | ASSERT(filp->private_data != NULL); | |
572e2857 | 6632 | |
325f0235 | 6633 | zs = filp->private_data; |
3937ab20 | 6634 | zs->zs_minor = -1; |
325f0235 BB |
6635 | zfs_onexit_destroy(zs->zs_onexit); |
6636 | zfs_zevent_destroy(zs->zs_zevent); | |
572e2857 | 6637 | |
d1d7e268 | 6638 | return (0); |
572e2857 BB |
6639 | } |
6640 | ||
6641 | static int | |
325f0235 | 6642 | zfsdev_open(struct inode *ino, struct file *filp) |
572e2857 | 6643 | { |
325f0235 | 6644 | int error; |
572e2857 | 6645 | |
325f0235 BB |
6646 | mutex_enter(&zfsdev_state_lock); |
6647 | error = zfsdev_state_init(filp); | |
6648 | mutex_exit(&zfsdev_state_lock); | |
572e2857 | 6649 | |
325f0235 | 6650 | return (-error); |
572e2857 BB |
6651 | } |
6652 | ||
6653 | static int | |
325f0235 | 6654 | zfsdev_release(struct inode *ino, struct file *filp) |
572e2857 | 6655 | { |
325f0235 | 6656 | int error; |
572e2857 BB |
6657 | |
6658 | mutex_enter(&zfsdev_state_lock); | |
325f0235 | 6659 | error = zfsdev_state_destroy(filp); |
572e2857 BB |
6660 | mutex_exit(&zfsdev_state_lock); |
6661 | ||
325f0235 | 6662 | return (-error); |
572e2857 BB |
6663 | } |
6664 | ||
325f0235 BB |
6665 | static long |
6666 | zfsdev_ioctl(struct file *filp, unsigned cmd, unsigned long arg) | |
34dc7c2f BB |
6667 | { |
6668 | zfs_cmd_t *zc; | |
6f1ffb06 | 6669 | uint_t vecnum; |
4fd762f8 | 6670 | int error, rc, flag = 0; |
6f1ffb06 | 6671 | const zfs_ioc_vec_t *vec; |
fb8e608d | 6672 | char *saved_poolname = NULL; |
6f1ffb06 | 6673 | nvlist_t *innvl = NULL; |
40d06e3c | 6674 | fstrans_cookie_t cookie; |
6f1ffb06 MA |
6675 | |
6676 | vecnum = cmd - ZFS_IOC_FIRST; | |
6677 | if (vecnum >= sizeof (zfs_ioc_vec) / sizeof (zfs_ioc_vec[0])) | |
2e528b49 | 6678 | return (-SET_ERROR(EINVAL)); |
6f1ffb06 | 6679 | vec = &zfs_ioc_vec[vecnum]; |
34dc7c2f | 6680 | |
2e0358cb BB |
6681 | /* |
6682 | * The registered ioctl list may be sparse, verify that either | |
6683 | * a normal or legacy handler are registered. | |
6684 | */ | |
6685 | if (vec->zvec_func == NULL && vec->zvec_legacy_func == NULL) | |
6686 | return (-SET_ERROR(EINVAL)); | |
6687 | ||
efcd79a8 | 6688 | zc = kmem_zalloc(sizeof (zfs_cmd_t), KM_SLEEP); |
34dc7c2f | 6689 | |
9babb374 | 6690 | error = ddi_copyin((void *)arg, zc, sizeof (zfs_cmd_t), flag); |
6f1ffb06 | 6691 | if (error != 0) { |
2e528b49 | 6692 | error = SET_ERROR(EFAULT); |
6f1ffb06 MA |
6693 | goto out; |
6694 | } | |
34dc7c2f | 6695 | |
6f1ffb06 | 6696 | zc->zc_iflags = flag & FKIOCTL; |
f74b821a BB |
6697 | if (zc->zc_nvlist_src_size > MAX_NVLIST_SRC_SIZE) { |
6698 | /* | |
6699 | * Make sure the user doesn't pass in an insane value for | |
6700 | * zc_nvlist_src_size. We have to check, since we will end | |
6701 | * up allocating that much memory inside of get_nvlist(). This | |
6702 | * prevents a nefarious user from allocating tons of kernel | |
6703 | * memory. | |
6704 | * | |
6705 | * Also, we return EINVAL instead of ENOMEM here. The reason | |
6706 | * being that returning ENOMEM from an ioctl() has a special | |
6707 | * connotation; that the user's size value is too small and | |
6708 | * needs to be expanded to hold the nvlist. See | |
6709 | * zcmd_expand_dst_nvlist() for details. | |
6710 | */ | |
6711 | error = SET_ERROR(EINVAL); /* User's size too big */ | |
6712 | ||
6713 | } else if (zc->zc_nvlist_src_size != 0) { | |
6f1ffb06 MA |
6714 | error = get_nvlist(zc->zc_nvlist_src, zc->zc_nvlist_src_size, |
6715 | zc->zc_iflags, &innvl); | |
6716 | if (error != 0) | |
6717 | goto out; | |
6718 | } | |
34dc7c2f BB |
6719 | |
6720 | /* | |
6721 | * Ensure that all pool/dataset names are valid before we pass down to | |
6722 | * the lower layers. | |
6723 | */ | |
6f1ffb06 MA |
6724 | zc->zc_name[sizeof (zc->zc_name) - 1] = '\0'; |
6725 | switch (vec->zvec_namecheck) { | |
6726 | case POOL_NAME: | |
6727 | if (pool_namecheck(zc->zc_name, NULL, NULL) != 0) | |
2e528b49 | 6728 | error = SET_ERROR(EINVAL); |
6f1ffb06 | 6729 | else |
572e2857 | 6730 | error = pool_status_check(zc->zc_name, |
6f1ffb06 MA |
6731 | vec->zvec_namecheck, vec->zvec_pool_check); |
6732 | break; | |
34dc7c2f | 6733 | |
6f1ffb06 MA |
6734 | case DATASET_NAME: |
6735 | if (dataset_namecheck(zc->zc_name, NULL, NULL) != 0) | |
2e528b49 | 6736 | error = SET_ERROR(EINVAL); |
6f1ffb06 | 6737 | else |
572e2857 | 6738 | error = pool_status_check(zc->zc_name, |
6f1ffb06 MA |
6739 | vec->zvec_namecheck, vec->zvec_pool_check); |
6740 | break; | |
34dc7c2f | 6741 | |
6f1ffb06 MA |
6742 | case NO_NAME: |
6743 | break; | |
34dc7c2f BB |
6744 | } |
6745 | ||
34dc7c2f | 6746 | |
005e27e3 | 6747 | if (error == 0) { |
ddab862d | 6748 | cookie = spl_fstrans_mark(); |
6f1ffb06 | 6749 | error = vec->zvec_secpolicy(zc, innvl, CRED()); |
ddab862d TC |
6750 | spl_fstrans_unmark(cookie); |
6751 | } | |
6f1ffb06 MA |
6752 | |
6753 | if (error != 0) | |
6754 | goto out; | |
6755 | ||
6756 | /* legacy ioctls can modify zc_name */ | |
4fd762f8 BB |
6757 | saved_poolname = strdup(zc->zc_name); |
6758 | if (saved_poolname == NULL) { | |
6759 | error = SET_ERROR(ENOMEM); | |
6760 | goto out; | |
6761 | } else { | |
6762 | saved_poolname[strcspn(saved_poolname, "/@#")] = '\0'; | |
6763 | } | |
6f1ffb06 MA |
6764 | |
6765 | if (vec->zvec_func != NULL) { | |
6766 | nvlist_t *outnvl; | |
6767 | int puterror = 0; | |
6768 | spa_t *spa; | |
6769 | nvlist_t *lognv = NULL; | |
6770 | ||
6771 | ASSERT(vec->zvec_legacy_func == NULL); | |
6772 | ||
6773 | /* | |
6774 | * Add the innvl to the lognv before calling the func, | |
6775 | * in case the func changes the innvl. | |
6776 | */ | |
6777 | if (vec->zvec_allow_log) { | |
6778 | lognv = fnvlist_alloc(); | |
6779 | fnvlist_add_string(lognv, ZPOOL_HIST_IOCTL, | |
6780 | vec->zvec_name); | |
6781 | if (!nvlist_empty(innvl)) { | |
6782 | fnvlist_add_nvlist(lognv, ZPOOL_HIST_INPUT_NVL, | |
6783 | innvl); | |
6784 | } | |
6785 | } | |
6786 | ||
79c76d5b | 6787 | outnvl = fnvlist_alloc(); |
40d06e3c | 6788 | cookie = spl_fstrans_mark(); |
6f1ffb06 | 6789 | error = vec->zvec_func(zc->zc_name, innvl, outnvl); |
40d06e3c | 6790 | spl_fstrans_unmark(cookie); |
6f1ffb06 MA |
6791 | |
6792 | if (error == 0 && vec->zvec_allow_log && | |
6793 | spa_open(zc->zc_name, &spa, FTAG) == 0) { | |
6794 | if (!nvlist_empty(outnvl)) { | |
6795 | fnvlist_add_nvlist(lognv, ZPOOL_HIST_OUTPUT_NVL, | |
6796 | outnvl); | |
6797 | } | |
6798 | (void) spa_history_log_nvl(spa, lognv); | |
6799 | spa_close(spa, FTAG); | |
6800 | } | |
6801 | fnvlist_free(lognv); | |
6802 | ||
6803 | if (!nvlist_empty(outnvl) || zc->zc_nvlist_dst_size != 0) { | |
6804 | int smusherror = 0; | |
6805 | if (vec->zvec_smush_outnvlist) { | |
6806 | smusherror = nvlist_smush(outnvl, | |
6807 | zc->zc_nvlist_dst_size); | |
6808 | } | |
6809 | if (smusherror == 0) | |
6810 | puterror = put_nvlist(zc, outnvl); | |
6811 | } | |
6812 | ||
6813 | if (puterror != 0) | |
6814 | error = puterror; | |
6815 | ||
6816 | nvlist_free(outnvl); | |
6817 | } else { | |
40d06e3c | 6818 | cookie = spl_fstrans_mark(); |
6f1ffb06 | 6819 | error = vec->zvec_legacy_func(zc); |
40d06e3c | 6820 | spl_fstrans_unmark(cookie); |
6f1ffb06 MA |
6821 | } |
6822 | ||
6823 | out: | |
6824 | nvlist_free(innvl); | |
9babb374 | 6825 | rc = ddi_copyout(zc, (void *)arg, sizeof (zfs_cmd_t), flag); |
6f1ffb06 | 6826 | if (error == 0 && rc != 0) |
2e528b49 | 6827 | error = SET_ERROR(EFAULT); |
6f1ffb06 MA |
6828 | if (error == 0 && vec->zvec_allow_log) { |
6829 | char *s = tsd_get(zfs_allow_log_key); | |
6830 | if (s != NULL) | |
6831 | strfree(s); | |
fb8e608d TC |
6832 | (void) tsd_set(zfs_allow_log_key, saved_poolname); |
6833 | } else { | |
6834 | if (saved_poolname != NULL) | |
4fd762f8 | 6835 | strfree(saved_poolname); |
34dc7c2f BB |
6836 | } |
6837 | ||
6838 | kmem_free(zc, sizeof (zfs_cmd_t)); | |
325f0235 | 6839 | return (-error); |
34dc7c2f BB |
6840 | } |
6841 | ||
325f0235 BB |
6842 | #ifdef CONFIG_COMPAT |
6843 | static long | |
6844 | zfsdev_compat_ioctl(struct file *filp, unsigned cmd, unsigned long arg) | |
34dc7c2f | 6845 | { |
d1d7e268 | 6846 | return (zfsdev_ioctl(filp, cmd, arg)); |
325f0235 BB |
6847 | } |
6848 | #else | |
d1d7e268 | 6849 | #define zfsdev_compat_ioctl NULL |
325f0235 | 6850 | #endif |
34dc7c2f | 6851 | |
325f0235 | 6852 | static const struct file_operations zfsdev_fops = { |
d1d7e268 MK |
6853 | .open = zfsdev_open, |
6854 | .release = zfsdev_release, | |
6855 | .unlocked_ioctl = zfsdev_ioctl, | |
6856 | .compat_ioctl = zfsdev_compat_ioctl, | |
6857 | .owner = THIS_MODULE, | |
325f0235 | 6858 | }; |
34dc7c2f | 6859 | |
325f0235 | 6860 | static struct miscdevice zfs_misc = { |
d1d7e268 MK |
6861 | .minor = MISC_DYNAMIC_MINOR, |
6862 | .name = ZFS_DRIVER, | |
6863 | .fops = &zfsdev_fops, | |
325f0235 | 6864 | }; |
34dc7c2f BB |
6865 | |
6866 | static int | |
325f0235 | 6867 | zfs_attach(void) |
34dc7c2f | 6868 | { |
325f0235 | 6869 | int error; |
34dc7c2f | 6870 | |
325f0235 | 6871 | mutex_init(&zfsdev_state_lock, NULL, MUTEX_DEFAULT, NULL); |
3937ab20 TC |
6872 | zfsdev_state_list = kmem_zalloc(sizeof (zfsdev_state_t), KM_SLEEP); |
6873 | zfsdev_state_list->zs_minor = -1; | |
34dc7c2f | 6874 | |
325f0235 | 6875 | error = misc_register(&zfs_misc); |
d1d7e268 | 6876 | if (error != 0) { |
325f0235 BB |
6877 | printk(KERN_INFO "ZFS: misc_register() failed %d\n", error); |
6878 | return (error); | |
6879 | } | |
34dc7c2f | 6880 | |
325f0235 | 6881 | return (0); |
34dc7c2f BB |
6882 | } |
6883 | ||
325f0235 BB |
6884 | static void |
6885 | zfs_detach(void) | |
34dc7c2f | 6886 | { |
3937ab20 | 6887 | zfsdev_state_t *zs, *zsprev = NULL; |
34dc7c2f | 6888 | |
324dcd37 | 6889 | misc_deregister(&zfs_misc); |
325f0235 | 6890 | mutex_destroy(&zfsdev_state_lock); |
3937ab20 TC |
6891 | |
6892 | for (zs = zfsdev_state_list; zs != NULL; zs = zs->zs_next) { | |
6893 | if (zsprev) | |
6894 | kmem_free(zsprev, sizeof (zfsdev_state_t)); | |
6895 | zsprev = zs; | |
6896 | } | |
6897 | if (zsprev) | |
6898 | kmem_free(zsprev, sizeof (zfsdev_state_t)); | |
34dc7c2f BB |
6899 | } |
6900 | ||
6f1ffb06 MA |
6901 | static void |
6902 | zfs_allow_log_destroy(void *arg) | |
6903 | { | |
6904 | char *poolname = arg; | |
9f3d1407 | 6905 | |
6906 | if (poolname != NULL) | |
6907 | strfree(poolname); | |
6f1ffb06 | 6908 | } |
325f0235 BB |
6909 | |
6910 | #ifdef DEBUG | |
d1d7e268 | 6911 | #define ZFS_DEBUG_STR " (DEBUG mode)" |
325f0235 | 6912 | #else |
d1d7e268 | 6913 | #define ZFS_DEBUG_STR "" |
325f0235 | 6914 | #endif |
34dc7c2f | 6915 | |
b4f3666a | 6916 | static int __init |
34dc7c2f BB |
6917 | _init(void) |
6918 | { | |
6919 | int error; | |
6920 | ||
3d8d245f | 6921 | error = -vn_set_pwd("/"); |
b4f3666a BB |
6922 | if (error) { |
6923 | printk(KERN_NOTICE | |
6924 | "ZFS: Warning unable to set pwd to '/': %d\n", error); | |
6925 | return (error); | |
6926 | } | |
6927 | ||
a0bd735a BP |
6928 | if ((error = -zvol_init()) != 0) |
6929 | return (error); | |
6930 | ||
34dc7c2f BB |
6931 | spa_init(FREAD | FWRITE); |
6932 | zfs_init(); | |
34dc7c2f | 6933 | |
6f1ffb06 MA |
6934 | zfs_ioctl_init(); |
6935 | ||
325f0235 | 6936 | if ((error = zfs_attach()) != 0) |
a0bd735a | 6937 | goto out; |
34dc7c2f | 6938 | |
d5446cfc | 6939 | tsd_create(&zfs_fsyncer_key, NULL); |
6f1ffb06 MA |
6940 | tsd_create(&rrw_tsd_key, rrw_tsd_destroy); |
6941 | tsd_create(&zfs_allow_log_key, zfs_allow_log_destroy); | |
34dc7c2f | 6942 | |
4b5d425f | 6943 | printk(KERN_NOTICE "ZFS: Loaded module v%s-%s%s, " |
d1d7e268 MK |
6944 | "ZFS pool version %s, ZFS filesystem version %s\n", |
6945 | ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR, | |
6946 | SPA_VERSION_STRING, ZPL_VERSION_STRING); | |
b695c34e MM |
6947 | #ifndef CONFIG_FS_POSIX_ACL |
6948 | printk(KERN_NOTICE "ZFS: Posix ACLs disabled by kernel\n"); | |
6949 | #endif /* CONFIG_FS_POSIX_ACL */ | |
34dc7c2f BB |
6950 | |
6951 | return (0); | |
325f0235 | 6952 | |
a0bd735a | 6953 | out: |
325f0235 BB |
6954 | zfs_fini(); |
6955 | spa_fini(); | |
a0bd735a | 6956 | (void) zvol_fini(); |
4b5d425f | 6957 | printk(KERN_NOTICE "ZFS: Failed to Load ZFS Filesystem v%s-%s%s" |
d1d7e268 MK |
6958 | ", rc = %d\n", ZFS_META_VERSION, ZFS_META_RELEASE, |
6959 | ZFS_DEBUG_STR, error); | |
325f0235 BB |
6960 | |
6961 | return (error); | |
34dc7c2f BB |
6962 | } |
6963 | ||
b4f3666a | 6964 | static void __exit |
34dc7c2f BB |
6965 | _fini(void) |
6966 | { | |
325f0235 | 6967 | zfs_detach(); |
34dc7c2f BB |
6968 | zfs_fini(); |
6969 | spa_fini(); | |
a0bd735a | 6970 | zvol_fini(); |
46e18b3f | 6971 | |
d5446cfc | 6972 | tsd_destroy(&zfs_fsyncer_key); |
3fc050aa | 6973 | tsd_destroy(&rrw_tsd_key); |
6f1ffb06 | 6974 | tsd_destroy(&zfs_allow_log_key); |
34dc7c2f | 6975 | |
4b5d425f | 6976 | printk(KERN_NOTICE "ZFS: Unloaded module v%s-%s%s\n", |
d1d7e268 | 6977 | ZFS_META_VERSION, ZFS_META_RELEASE, ZFS_DEBUG_STR); |
34dc7c2f | 6978 | } |
325f0235 BB |
6979 | |
6980 | #ifdef HAVE_SPL | |
b4f3666a BB |
6981 | module_init(_init); |
6982 | module_exit(_fini); | |
325f0235 BB |
6983 | |
6984 | MODULE_DESCRIPTION("ZFS"); | |
6985 | MODULE_AUTHOR(ZFS_META_AUTHOR); | |
6986 | MODULE_LICENSE(ZFS_META_LICENSE); | |
99e349db | 6987 | MODULE_VERSION(ZFS_META_VERSION "-" ZFS_META_RELEASE); |
325f0235 | 6988 | #endif /* HAVE_SPL */ |