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