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