author | nd150628 |
Tue, 25 Jul 2006 15:31:56 -0700 | |
changeset 2440 | 9281a87e8a9e |
parent 2417 | 694d5de97348 |
child 2474 | c001ad7e0c25 |
permissions | -rw-r--r-- |
789 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
1544 | 5 |
* Common Development and Distribution License (the "License"). |
6 |
* You may not use this file except in compliance with the License. |
|
789 | 7 |
* |
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
21 |
/* |
|
1294
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
22 |
* Copyright 2006 Sun Microsystems, Inc. All rights reserved. |
789 | 23 |
* Use is subject to license terms. |
24 |
*/ |
|
25 |
||
26 |
#pragma ident "%Z%%M% %I% %E% SMI" |
|
27 |
||
28 |
#include <assert.h> |
|
29 |
#include <ctype.h> |
|
30 |
#include <errno.h> |
|
31 |
#include <libdevinfo.h> |
|
32 |
#include <libintl.h> |
|
33 |
#include <math.h> |
|
34 |
#include <stdio.h> |
|
35 |
#include <stdlib.h> |
|
36 |
#include <strings.h> |
|
37 |
#include <unistd.h> |
|
38 |
#include <zone.h> |
|
2082 | 39 |
#include <fcntl.h> |
789 | 40 |
#include <sys/mntent.h> |
41 |
#include <sys/mnttab.h> |
|
1294
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
42 |
#include <sys/mount.h> |
789 | 43 |
|
44 |
#include <sys/spa.h> |
|
45 |
#include <sys/zio.h> |
|
46 |
#include <libzfs.h> |
|
47 |
||
48 |
#include "zfs_namecheck.h" |
|
49 |
#include "zfs_prop.h" |
|
50 |
#include "libzfs_impl.h" |
|
51 |
||
52 |
/* |
|
53 |
* Given a single type (not a mask of types), return the type in a human |
|
54 |
* readable form. |
|
55 |
*/ |
|
56 |
const char * |
|
57 |
zfs_type_to_name(zfs_type_t type) |
|
58 |
{ |
|
59 |
switch (type) { |
|
60 |
case ZFS_TYPE_FILESYSTEM: |
|
61 |
return (dgettext(TEXT_DOMAIN, "filesystem")); |
|
62 |
case ZFS_TYPE_SNAPSHOT: |
|
63 |
return (dgettext(TEXT_DOMAIN, "snapshot")); |
|
64 |
case ZFS_TYPE_VOLUME: |
|
65 |
return (dgettext(TEXT_DOMAIN, "volume")); |
|
66 |
} |
|
67 |
||
68 |
return (NULL); |
|
69 |
} |
|
70 |
||
71 |
/* |
|
72 |
* Given a path and mask of ZFS types, return a string describing this dataset. |
|
73 |
* This is used when we fail to open a dataset and we cannot get an exact type. |
|
74 |
* We guess what the type would have been based on the path and the mask of |
|
75 |
* acceptable types. |
|
76 |
*/ |
|
77 |
static const char * |
|
78 |
path_to_str(const char *path, int types) |
|
79 |
{ |
|
80 |
/* |
|
81 |
* When given a single type, always report the exact type. |
|
82 |
*/ |
|
83 |
if (types == ZFS_TYPE_SNAPSHOT) |
|
84 |
return (dgettext(TEXT_DOMAIN, "snapshot")); |
|
85 |
if (types == ZFS_TYPE_FILESYSTEM) |
|
86 |
return (dgettext(TEXT_DOMAIN, "filesystem")); |
|
87 |
if (types == ZFS_TYPE_VOLUME) |
|
88 |
return (dgettext(TEXT_DOMAIN, "volume")); |
|
89 |
||
90 |
/* |
|
91 |
* The user is requesting more than one type of dataset. If this is the |
|
92 |
* case, consult the path itself. If we're looking for a snapshot, and |
|
93 |
* a '@' is found, then report it as "snapshot". Otherwise, remove the |
|
94 |
* snapshot attribute and try again. |
|
95 |
*/ |
|
96 |
if (types & ZFS_TYPE_SNAPSHOT) { |
|
97 |
if (strchr(path, '@') != NULL) |
|
98 |
return (dgettext(TEXT_DOMAIN, "snapshot")); |
|
99 |
return (path_to_str(path, types & ~ZFS_TYPE_SNAPSHOT)); |
|
100 |
} |
|
101 |
||
102 |
||
103 |
/* |
|
104 |
* The user has requested either filesystems or volumes. |
|
105 |
* We have no way of knowing a priori what type this would be, so always |
|
106 |
* report it as "filesystem" or "volume", our two primitive types. |
|
107 |
*/ |
|
108 |
if (types & ZFS_TYPE_FILESYSTEM) |
|
109 |
return (dgettext(TEXT_DOMAIN, "filesystem")); |
|
110 |
||
111 |
assert(types & ZFS_TYPE_VOLUME); |
|
112 |
return (dgettext(TEXT_DOMAIN, "volume")); |
|
113 |
} |
|
114 |
||
115 |
/* |
|
116 |
* Validate a ZFS path. This is used even before trying to open the dataset, to |
|
117 |
* provide a more meaningful error message. We place a more useful message in |
|
118 |
* 'buf' detailing exactly why the name was not valid. |
|
119 |
*/ |
|
120 |
static int |
|
2082 | 121 |
zfs_validate_name(libzfs_handle_t *hdl, const char *path, int type) |
789 | 122 |
{ |
123 |
namecheck_err_t why; |
|
124 |
char what; |
|
125 |
||
126 |
if (dataset_namecheck(path, &why, &what) != 0) { |
|
2082 | 127 |
if (hdl != NULL) { |
789 | 128 |
switch (why) { |
1003 | 129 |
case NAME_ERR_TOOLONG: |
2082 | 130 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
131 |
"name is too long")); |
|
1003 | 132 |
break; |
133 |
||
789 | 134 |
case NAME_ERR_LEADING_SLASH: |
2082 | 135 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
136 |
"leading slash in name")); |
|
789 | 137 |
break; |
138 |
||
139 |
case NAME_ERR_EMPTY_COMPONENT: |
|
2082 | 140 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
141 |
"empty component in name")); |
|
789 | 142 |
break; |
143 |
||
144 |
case NAME_ERR_TRAILING_SLASH: |
|
2082 | 145 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
146 |
"trailing slash in name")); |
|
789 | 147 |
break; |
148 |
||
149 |
case NAME_ERR_INVALCHAR: |
|
2082 | 150 |
zfs_error_aux(hdl, |
789 | 151 |
dgettext(TEXT_DOMAIN, "invalid character " |
2082 | 152 |
"'%c' in name"), what); |
789 | 153 |
break; |
154 |
||
155 |
case NAME_ERR_MULTIPLE_AT: |
|
2082 | 156 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
157 |
"multiple '@' delimiters in name")); |
|
789 | 158 |
break; |
159 |
} |
|
160 |
} |
|
161 |
||
162 |
return (0); |
|
163 |
} |
|
164 |
||
165 |
if (!(type & ZFS_TYPE_SNAPSHOT) && strchr(path, '@') != NULL) { |
|
2082 | 166 |
if (hdl != NULL) |
167 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
168 |
"snapshot delimiter '@' in filesystem name")); |
|
789 | 169 |
return (0); |
170 |
} |
|
171 |
||
2199 | 172 |
if (type == ZFS_TYPE_SNAPSHOT && strchr(path, '@') == NULL) { |
173 |
if (hdl != NULL) |
|
174 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
175 |
"missing '@' delimeter in snapshot name")); |
|
176 |
return (0); |
|
177 |
} |
|
178 |
||
2082 | 179 |
return (-1); |
789 | 180 |
} |
181 |
||
182 |
int |
|
183 |
zfs_name_valid(const char *name, zfs_type_t type) |
|
184 |
{ |
|
2082 | 185 |
return (zfs_validate_name(NULL, name, type)); |
789 | 186 |
} |
187 |
||
188 |
/* |
|
189 |
* Utility function to gather stats (objset and zpl) for the given object. |
|
190 |
*/ |
|
191 |
static int |
|
192 |
get_stats(zfs_handle_t *zhp) |
|
193 |
{ |
|
194 |
zfs_cmd_t zc = { 0 }; |
|
195 |
||
196 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
197 |
||
2082 | 198 |
if ((zc.zc_config_src = (uint64_t)(uintptr_t)malloc(1024)) == NULL) |
199 |
return (-1); |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
200 |
zc.zc_config_src_size = 1024; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
201 |
|
2082 | 202 |
while (ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0) { |
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
203 |
if (errno == ENOMEM) { |
2082 | 204 |
free((void *)(uintptr_t)zc.zc_config_src); |
205 |
if ((zc.zc_config_src = (uint64_t)(uintptr_t) |
|
206 |
malloc(zc.zc_config_src_size)) == NULL) |
|
207 |
return (-1); |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
208 |
} else { |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
209 |
free((void *)(uintptr_t)zc.zc_config_src); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
210 |
return (-1); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
211 |
} |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
212 |
} |
789 | 213 |
|
214 |
bcopy(&zc.zc_objset_stats, &zhp->zfs_dmustats, |
|
215 |
sizeof (zc.zc_objset_stats)); |
|
216 |
||
1544 | 217 |
(void) strcpy(zhp->zfs_root, zc.zc_root); |
218 |
||
2082 | 219 |
if (zhp->zfs_props) { |
220 |
nvlist_free(zhp->zfs_props); |
|
221 |
zhp->zfs_props = NULL; |
|
222 |
} |
|
223 |
||
224 |
if (nvlist_unpack((void *)(uintptr_t)zc.zc_config_src, |
|
225 |
zc.zc_config_src_size, &zhp->zfs_props, 0) != 0) { |
|
226 |
free((void *)(uintptr_t)zc.zc_config_src); |
|
227 |
return (-1); |
|
228 |
} |
|
789 | 229 |
|
230 |
zhp->zfs_volsize = zc.zc_volsize; |
|
231 |
zhp->zfs_volblocksize = zc.zc_volblocksize; |
|
232 |
||
2082 | 233 |
free((void *)(uintptr_t)zc.zc_config_src); |
234 |
||
789 | 235 |
return (0); |
236 |
} |
|
237 |
||
238 |
/* |
|
239 |
* Refresh the properties currently stored in the handle. |
|
240 |
*/ |
|
241 |
void |
|
242 |
zfs_refresh_properties(zfs_handle_t *zhp) |
|
243 |
{ |
|
244 |
(void) get_stats(zhp); |
|
245 |
} |
|
246 |
||
247 |
/* |
|
248 |
* Makes a handle from the given dataset name. Used by zfs_open() and |
|
249 |
* zfs_iter_* to create child handles on the fly. |
|
250 |
*/ |
|
251 |
zfs_handle_t * |
|
2082 | 252 |
make_dataset_handle(libzfs_handle_t *hdl, const char *path) |
789 | 253 |
{ |
2082 | 254 |
zfs_handle_t *zhp = calloc(sizeof (zfs_handle_t), 1); |
255 |
||
256 |
if (zhp == NULL) |
|
257 |
return (NULL); |
|
258 |
||
259 |
zhp->zfs_hdl = hdl; |
|
789 | 260 |
|
1758
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
261 |
top: |
789 | 262 |
(void) strlcpy(zhp->zfs_name, path, sizeof (zhp->zfs_name)); |
263 |
||
264 |
if (get_stats(zhp) != 0) { |
|
265 |
free(zhp); |
|
266 |
return (NULL); |
|
267 |
} |
|
268 |
||
1758
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
269 |
if (zhp->zfs_dmustats.dds_inconsistent) { |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
270 |
zfs_cmd_t zc = { 0 }; |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
271 |
|
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
272 |
/* |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
273 |
* If it is dds_inconsistent, then we've caught it in |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
274 |
* the middle of a 'zfs receive' or 'zfs destroy', and |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
275 |
* it is inconsistent from the ZPL's point of view, so |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
276 |
* can't be mounted. However, it could also be that we |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
277 |
* have crashed in the middle of one of those |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
278 |
* operations, in which case we need to get rid of the |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
279 |
* inconsistent state. We do that by either rolling |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
280 |
* back to the previous snapshot (which will fail if |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
281 |
* there is none), or destroying the filesystem. Note |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
282 |
* that if we are still in the middle of an active |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
283 |
* 'receive' or 'destroy', then the rollback and destroy |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
284 |
* will fail with EBUSY and we will drive on as usual. |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
285 |
*/ |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
286 |
|
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
287 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
288 |
|
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
289 |
if (zhp->zfs_type == ZFS_TYPE_VOLUME) { |
2082 | 290 |
(void) zvol_remove_link(hdl, zhp->zfs_name); |
1758
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
291 |
zc.zc_objset_type = DMU_OST_ZVOL; |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
292 |
} else { |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
293 |
zc.zc_objset_type = DMU_OST_ZFS; |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
294 |
} |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
295 |
|
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
296 |
/* If we can successfully roll it back, reget the stats */ |
2082 | 297 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_ROLLBACK, &zc) == 0) |
1758
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
298 |
goto top; |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
299 |
/* |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
300 |
* If we can sucessfully destroy it, pretend that it |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
301 |
* never existed. |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
302 |
*/ |
2082 | 303 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc) == 0) { |
1758
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
304 |
free(zhp); |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
305 |
errno = ENOENT; |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
306 |
return (NULL); |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
307 |
} |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
308 |
} |
d0750a16db04
6397267 assertion failed: (link->list_next == 0) == (link->list_prev == 0)
ahrens
parents:
1749
diff
changeset
|
309 |
|
789 | 310 |
/* |
311 |
* We've managed to open the dataset and gather statistics. Determine |
|
312 |
* the high-level type. |
|
313 |
*/ |
|
314 |
if (zhp->zfs_dmustats.dds_is_snapshot) |
|
315 |
zhp->zfs_type = ZFS_TYPE_SNAPSHOT; |
|
316 |
else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZVOL) |
|
317 |
zhp->zfs_type = ZFS_TYPE_VOLUME; |
|
318 |
else if (zhp->zfs_dmustats.dds_type == DMU_OST_ZFS) |
|
319 |
zhp->zfs_type = ZFS_TYPE_FILESYSTEM; |
|
320 |
else |
|
2082 | 321 |
abort(); /* we should never see any other types */ |
789 | 322 |
|
323 |
return (zhp); |
|
324 |
} |
|
325 |
||
326 |
/* |
|
327 |
* Opens the given snapshot, filesystem, or volume. The 'types' |
|
328 |
* argument is a mask of acceptable types. The function will print an |
|
329 |
* appropriate error message and return NULL if it can't be opened. |
|
330 |
*/ |
|
331 |
zfs_handle_t * |
|
2082 | 332 |
zfs_open(libzfs_handle_t *hdl, const char *path, int types) |
789 | 333 |
{ |
334 |
zfs_handle_t *zhp; |
|
2082 | 335 |
char errbuf[1024]; |
336 |
||
337 |
(void) snprintf(errbuf, sizeof (errbuf), |
|
338 |
dgettext(TEXT_DOMAIN, "cannot open '%s'"), path); |
|
789 | 339 |
|
340 |
/* |
|
2082 | 341 |
* Validate the name before we even try to open it. |
789 | 342 |
*/ |
2082 | 343 |
if (!zfs_validate_name(hdl, path, ZFS_TYPE_ANY)) { |
344 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
345 |
"invalid dataset name")); |
|
346 |
(void) zfs_error(hdl, EZFS_INVALIDNAME, errbuf); |
|
789 | 347 |
return (NULL); |
348 |
} |
|
349 |
||
350 |
/* |
|
351 |
* Try to get stats for the dataset, which will tell us if it exists. |
|
352 |
*/ |
|
353 |
errno = 0; |
|
2082 | 354 |
if ((zhp = make_dataset_handle(hdl, path)) == NULL) { |
355 |
(void) zfs_standard_error(hdl, errno, errbuf, path); |
|
789 | 356 |
return (NULL); |
357 |
} |
|
358 |
||
359 |
if (!(types & zhp->zfs_type)) { |
|
2082 | 360 |
(void) zfs_error(hdl, EZFS_BADTYPE, errbuf); |
2142
f6e0487aa9a3
6433264 crash when adding spare: nvlist_lookup_string(cnv, "path", &path) == 0
eschrock
parents:
2082
diff
changeset
|
361 |
zfs_close(zhp); |
789 | 362 |
return (NULL); |
363 |
} |
|
364 |
||
365 |
return (zhp); |
|
366 |
} |
|
367 |
||
368 |
/* |
|
369 |
* Release a ZFS handle. Nothing to do but free the associated memory. |
|
370 |
*/ |
|
371 |
void |
|
372 |
zfs_close(zfs_handle_t *zhp) |
|
373 |
{ |
|
374 |
if (zhp->zfs_mntopts) |
|
375 |
free(zhp->zfs_mntopts); |
|
2082 | 376 |
if (zhp->zfs_props) |
377 |
nvlist_free(zhp->zfs_props); |
|
789 | 378 |
free(zhp); |
379 |
} |
|
380 |
||
381 |
struct { |
|
382 |
const char *name; |
|
383 |
uint64_t value; |
|
384 |
} checksum_table[] = { |
|
385 |
{ "on", ZIO_CHECKSUM_ON }, |
|
386 |
{ "off", ZIO_CHECKSUM_OFF }, |
|
387 |
{ "fletcher2", ZIO_CHECKSUM_FLETCHER_2 }, |
|
388 |
{ "fletcher4", ZIO_CHECKSUM_FLETCHER_4 }, |
|
389 |
{ "sha256", ZIO_CHECKSUM_SHA256 }, |
|
390 |
{ NULL } |
|
391 |
}; |
|
392 |
||
393 |
struct { |
|
394 |
const char *name; |
|
395 |
uint64_t value; |
|
396 |
} compress_table[] = { |
|
397 |
{ "on", ZIO_COMPRESS_ON }, |
|
398 |
{ "off", ZIO_COMPRESS_OFF }, |
|
399 |
{ "lzjb", ZIO_COMPRESS_LZJB }, |
|
400 |
{ NULL } |
|
401 |
}; |
|
402 |
||
403 |
struct { |
|
404 |
const char *name; |
|
405 |
uint64_t value; |
|
406 |
} snapdir_table[] = { |
|
849
8d799fd81a9b
6345023 /dev/zfs fails to open once ZFS module is unloaded
bonwick
parents:
789
diff
changeset
|
407 |
{ "hidden", ZFS_SNAPDIR_HIDDEN }, |
8d799fd81a9b
6345023 /dev/zfs fails to open once ZFS module is unloaded
bonwick
parents:
789
diff
changeset
|
408 |
{ "visible", ZFS_SNAPDIR_VISIBLE }, |
789 | 409 |
{ NULL } |
410 |
}; |
|
411 |
||
412 |
struct { |
|
413 |
const char *name; |
|
414 |
uint64_t value; |
|
415 |
} acl_mode_table[] = { |
|
416 |
{ "discard", DISCARD }, |
|
417 |
{ "groupmask", GROUPMASK }, |
|
418 |
{ "passthrough", PASSTHROUGH }, |
|
419 |
{ NULL } |
|
420 |
}; |
|
421 |
||
422 |
struct { |
|
423 |
const char *name; |
|
424 |
uint64_t value; |
|
425 |
} acl_inherit_table[] = { |
|
426 |
{ "discard", DISCARD }, |
|
427 |
{ "noallow", NOALLOW }, |
|
428 |
{ "secure", SECURE }, |
|
429 |
{ "passthrough", PASSTHROUGH }, |
|
430 |
{ NULL } |
|
431 |
}; |
|
432 |
||
433 |
||
434 |
/* |
|
435 |
* Given a numeric suffix, convert the value into a number of bits that the |
|
436 |
* resulting value must be shifted. |
|
437 |
*/ |
|
438 |
static int |
|
2082 | 439 |
str2shift(libzfs_handle_t *hdl, const char *buf) |
789 | 440 |
{ |
441 |
const char *ends = "BKMGTPEZ"; |
|
442 |
int i; |
|
443 |
||
444 |
if (buf[0] == '\0') |
|
445 |
return (0); |
|
446 |
for (i = 0; i < strlen(ends); i++) { |
|
447 |
if (toupper(buf[0]) == ends[i]) |
|
448 |
break; |
|
449 |
} |
|
450 |
if (i == strlen(ends)) { |
|
2082 | 451 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
452 |
"invalid numeric suffix '%s'"), buf); |
|
789 | 453 |
return (-1); |
454 |
} |
|
455 |
||
456 |
/* |
|
457 |
* We want to allow trailing 'b' characters for 'GB' or 'Mb'. But don't |
|
458 |
* allow 'BB' - that's just weird. |
|
459 |
*/ |
|
460 |
if (buf[1] == '\0' || (toupper(buf[1]) == 'B' && buf[2] == '\0' && |
|
2082 | 461 |
toupper(buf[0]) != 'B')) |
789 | 462 |
return (10*i); |
2082 | 463 |
|
464 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
465 |
"invalid numeric suffix '%s'"), buf); |
|
789 | 466 |
return (-1); |
467 |
} |
|
468 |
||
469 |
/* |
|
470 |
* Convert a string of the form '100G' into a real number. Used when setting |
|
471 |
* properties or creating a volume. 'buf' is used to place an extended error |
|
472 |
* message for the caller to use. |
|
473 |
*/ |
|
474 |
static int |
|
2082 | 475 |
nicestrtonum(libzfs_handle_t *hdl, const char *value, uint64_t *num) |
789 | 476 |
{ |
477 |
char *end; |
|
478 |
int shift; |
|
479 |
||
480 |
*num = 0; |
|
481 |
||
482 |
/* Check to see if this looks like a number. */ |
|
483 |
if ((value[0] < '0' || value[0] > '9') && value[0] != '.') { |
|
2082 | 484 |
if (hdl) |
485 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
486 |
"bad numeric value '%s'"), value); |
|
789 | 487 |
return (-1); |
488 |
} |
|
489 |
||
490 |
/* Rely on stroll() to process the numeric portion. */ |
|
491 |
errno = 0; |
|
492 |
*num = strtoll(value, &end, 10); |
|
493 |
||
494 |
/* |
|
495 |
* Check for ERANGE, which indicates that the value is too large to fit |
|
496 |
* in a 64-bit value. |
|
497 |
*/ |
|
498 |
if (errno == ERANGE) { |
|
2082 | 499 |
if (hdl) |
500 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
501 |
"numeric value is too large")); |
|
789 | 502 |
return (-1); |
503 |
} |
|
504 |
||
505 |
/* |
|
506 |
* If we have a decimal value, then do the computation with floating |
|
507 |
* point arithmetic. Otherwise, use standard arithmetic. |
|
508 |
*/ |
|
509 |
if (*end == '.') { |
|
510 |
double fval = strtod(value, &end); |
|
511 |
||
2082 | 512 |
if ((shift = str2shift(hdl, end)) == -1) |
789 | 513 |
return (-1); |
514 |
||
515 |
fval *= pow(2, shift); |
|
516 |
||
517 |
if (fval > UINT64_MAX) { |
|
2082 | 518 |
if (hdl) |
519 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
520 |
"numeric value is too large")); |
|
789 | 521 |
return (-1); |
522 |
} |
|
523 |
||
524 |
*num = (uint64_t)fval; |
|
525 |
} else { |
|
2082 | 526 |
if ((shift = str2shift(hdl, end)) == -1) |
789 | 527 |
return (-1); |
528 |
||
529 |
/* Check for overflow */ |
|
530 |
if (shift >= 64 || (*num << shift) >> shift != *num) { |
|
2082 | 531 |
if (hdl) |
532 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
533 |
"numeric value is too large")); |
|
789 | 534 |
return (-1); |
535 |
} |
|
536 |
||
537 |
*num <<= shift; |
|
538 |
} |
|
539 |
||
540 |
return (0); |
|
541 |
} |
|
542 |
||
543 |
int |
|
544 |
zfs_nicestrtonum(const char *str, uint64_t *val) |
|
545 |
{ |
|
2082 | 546 |
return (nicestrtonum(NULL, str, val)); |
789 | 547 |
} |
548 |
||
549 |
/* |
|
550 |
* Given a property type and value, verify that the value is appropriate. Used |
|
551 |
* by zfs_prop_set() and some libzfs consumers. |
|
552 |
*/ |
|
553 |
int |
|
2082 | 554 |
zfs_prop_validate(libzfs_handle_t *hdl, zfs_prop_t prop, const char *value, |
555 |
uint64_t *intval) |
|
789 | 556 |
{ |
557 |
const char *propname = zfs_prop_to_name(prop); |
|
558 |
uint64_t number; |
|
2082 | 559 |
char errbuf[1024]; |
789 | 560 |
int i; |
561 |
||
562 |
/* |
|
563 |
* Check to see if this a read-only property. |
|
564 |
*/ |
|
2082 | 565 |
if (zfs_prop_readonly(prop)) |
566 |
return (zfs_error(hdl, EZFS_PROPREADONLY, |
|
567 |
dgettext(TEXT_DOMAIN, "cannot set %s property"), propname)); |
|
568 |
||
569 |
(void) snprintf(errbuf, sizeof (errbuf), |
|
570 |
dgettext(TEXT_DOMAIN, "bad %s value '%s'"), propname, value); |
|
789 | 571 |
|
572 |
/* See if the property value is too long */ |
|
573 |
if (strlen(value) >= ZFS_MAXPROPLEN) { |
|
2082 | 574 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "value is too long")); |
575 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 576 |
} |
577 |
||
578 |
/* Perform basic checking based on property type */ |
|
579 |
switch (zfs_prop_get_type(prop)) { |
|
580 |
case prop_type_boolean: |
|
581 |
if (strcmp(value, "on") == 0) { |
|
582 |
number = 1; |
|
583 |
} else if (strcmp(value, "off") == 0) { |
|
584 |
number = 0; |
|
585 |
} else { |
|
2082 | 586 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
587 |
"must be 'on' or 'off'")); |
|
588 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 589 |
} |
590 |
break; |
|
591 |
||
592 |
case prop_type_number: |
|
593 |
/* treat 'none' as 0 */ |
|
594 |
if (strcmp(value, "none") == 0) { |
|
595 |
number = 0; |
|
596 |
break; |
|
597 |
} |
|
598 |
||
2082 | 599 |
if (nicestrtonum(hdl, value, &number) != 0) |
600 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 601 |
|
602 |
/* don't allow 0 for quota, use 'none' instead */ |
|
603 |
if (prop == ZFS_PROP_QUOTA && number == 0 && |
|
604 |
strcmp(value, "none") != 0) { |
|
2082 | 605 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
606 |
"use 'quota=none' to disable")); |
|
607 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 608 |
} |
609 |
||
610 |
/* must be power of two within SPA_{MIN,MAX}BLOCKSIZE */ |
|
611 |
if (prop == ZFS_PROP_RECORDSIZE || |
|
612 |
prop == ZFS_PROP_VOLBLOCKSIZE) { |
|
613 |
if (number < SPA_MINBLOCKSIZE || |
|
614 |
number > SPA_MAXBLOCKSIZE || !ISP2(number)) { |
|
2082 | 615 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
789 | 616 |
"must be power of 2 from %u to %uk"), |
617 |
(uint_t)SPA_MINBLOCKSIZE, |
|
618 |
(uint_t)SPA_MAXBLOCKSIZE >> 10); |
|
2082 | 619 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
789 | 620 |
} |
621 |
} |
|
622 |
||
623 |
break; |
|
624 |
||
625 |
case prop_type_string: |
|
626 |
case prop_type_index: |
|
627 |
/* |
|
628 |
* The two writable string values, 'mountpoint' and |
|
629 |
* 'checksum' need special consideration. The 'index' types are |
|
630 |
* specified as strings by the user, but passed to the kernel as |
|
631 |
* integers. |
|
632 |
*/ |
|
633 |
switch (prop) { |
|
634 |
case ZFS_PROP_MOUNTPOINT: |
|
635 |
if (strcmp(value, ZFS_MOUNTPOINT_NONE) == 0 || |
|
636 |
strcmp(value, ZFS_MOUNTPOINT_LEGACY) == 0) |
|
637 |
break; |
|
638 |
||
639 |
if (value[0] != '/') { |
|
2082 | 640 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
641 |
"must be an absolute path, 'none', or " |
|
642 |
"'legacy'")); |
|
643 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 644 |
} |
645 |
break; |
|
646 |
||
647 |
case ZFS_PROP_CHECKSUM: |
|
648 |
for (i = 0; checksum_table[i].name != NULL; i++) { |
|
649 |
if (strcmp(value, checksum_table[i].name) |
|
650 |
== 0) { |
|
651 |
number = checksum_table[i].value; |
|
652 |
break; |
|
653 |
} |
|
654 |
} |
|
655 |
||
656 |
if (checksum_table[i].name == NULL) { |
|
2082 | 657 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
658 |
"must be 'on', 'off', 'fletcher2', " |
|
659 |
"'fletcher4', or 'sha256'")); |
|
660 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 661 |
} |
662 |
break; |
|
663 |
||
664 |
case ZFS_PROP_COMPRESSION: |
|
665 |
for (i = 0; compress_table[i].name != NULL; i++) { |
|
666 |
if (strcmp(value, compress_table[i].name) |
|
667 |
== 0) { |
|
668 |
number = compress_table[i].value; |
|
669 |
break; |
|
670 |
} |
|
671 |
} |
|
672 |
||
673 |
if (compress_table[i].name == NULL) { |
|
2082 | 674 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
675 |
"must be 'on', 'off', or 'lzjb'")); |
|
676 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 677 |
} |
678 |
break; |
|
679 |
||
680 |
case ZFS_PROP_SNAPDIR: |
|
681 |
for (i = 0; snapdir_table[i].name != NULL; i++) { |
|
682 |
if (strcmp(value, snapdir_table[i].name) == 0) { |
|
683 |
number = snapdir_table[i].value; |
|
684 |
break; |
|
685 |
} |
|
686 |
} |
|
687 |
||
688 |
if (snapdir_table[i].name == NULL) { |
|
2082 | 689 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
690 |
"must be 'hidden' or 'visible'")); |
|
691 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 692 |
} |
693 |
break; |
|
694 |
||
695 |
case ZFS_PROP_ACLMODE: |
|
696 |
for (i = 0; acl_mode_table[i].name != NULL; i++) { |
|
697 |
if (strcmp(value, acl_mode_table[i].name) |
|
698 |
== 0) { |
|
699 |
number = acl_mode_table[i].value; |
|
700 |
break; |
|
701 |
} |
|
702 |
} |
|
703 |
||
704 |
if (acl_mode_table[i].name == NULL) { |
|
2082 | 705 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
706 |
"must be 'disacard', 'groupmask', or " |
|
707 |
"'passthrough'")); |
|
708 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 709 |
} |
710 |
break; |
|
711 |
||
712 |
case ZFS_PROP_ACLINHERIT: |
|
713 |
for (i = 0; acl_inherit_table[i].name != NULL; i++) { |
|
714 |
if (strcmp(value, acl_inherit_table[i].name) |
|
715 |
== 0) { |
|
716 |
number = acl_inherit_table[i].value; |
|
717 |
break; |
|
718 |
} |
|
719 |
} |
|
720 |
||
721 |
if (acl_inherit_table[i].name == NULL) { |
|
2082 | 722 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
723 |
"must be 'discard, 'noallow', 'secure', " |
|
724 |
"or 'passthrough'")); |
|
725 |
return (zfs_error(hdl, EZFS_BADPROP, errbuf)); |
|
789 | 726 |
} |
727 |
break; |
|
728 |
||
729 |
case ZFS_PROP_SHARENFS: |
|
730 |
/* |
|
731 |
* Nothing to do for 'sharenfs', this gets passed on to |
|
732 |
* share(1M) verbatim. |
|
733 |
*/ |
|
734 |
break; |
|
735 |
} |
|
736 |
} |
|
737 |
||
738 |
if (intval != NULL) |
|
739 |
*intval = number; |
|
740 |
||
741 |
return (0); |
|
742 |
} |
|
743 |
||
744 |
/* |
|
745 |
* Given a property name and value, set the property for the given dataset. |
|
746 |
*/ |
|
747 |
int |
|
748 |
zfs_prop_set(zfs_handle_t *zhp, zfs_prop_t prop, const char *propval) |
|
749 |
{ |
|
750 |
const char *propname = zfs_prop_to_name(prop); |
|
751 |
uint64_t number; |
|
752 |
zfs_cmd_t zc = { 0 }; |
|
753 |
int ret; |
|
754 |
prop_changelist_t *cl; |
|
2082 | 755 |
char errbuf[1024]; |
756 |
libzfs_handle_t *hdl = zhp->zfs_hdl; |
|
757 |
||
758 |
if (zfs_prop_validate(zhp->zfs_hdl, prop, propval, &number) != 0) |
|
789 | 759 |
return (-1); |
760 |
||
2082 | 761 |
|
762 |
(void) snprintf(errbuf, sizeof (errbuf), |
|
763 |
dgettext(TEXT_DOMAIN, "cannot set %s for '%s'"), propname, |
|
764 |
zhp->zfs_name); |
|
765 |
||
789 | 766 |
/* |
767 |
* Check to see if the value applies to this type |
|
768 |
*/ |
|
2082 | 769 |
if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) |
770 |
return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); |
|
789 | 771 |
|
772 |
/* |
|
773 |
* For the mountpoint and sharenfs properties, check if it can be set |
|
774 |
* in a global/non-global zone based on the zoned property value: |
|
775 |
* |
|
776 |
* global zone non-global zone |
|
777 |
* ----------------------------------------------------- |
|
778 |
* zoned=on mountpoint (no) mountpoint (yes) |
|
779 |
* sharenfs (no) sharenfs (no) |
|
780 |
* |
|
781 |
* zoned=off mountpoint (yes) N/A |
|
782 |
* sharenfs (yes) |
|
783 |
*/ |
|
784 |
if (prop == ZFS_PROP_MOUNTPOINT || prop == ZFS_PROP_SHARENFS) { |
|
785 |
if (zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { |
|
786 |
if (getzoneid() == GLOBAL_ZONEID) { |
|
2082 | 787 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
788 |
"dataset is used in a non-global zone")); |
|
789 |
return (zfs_error(hdl, EZFS_ZONED, errbuf)); |
|
789 | 790 |
} else if (prop == ZFS_PROP_SHARENFS) { |
2082 | 791 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
792 |
"filesystems cannot be shared in a " |
|
793 |
"non-global zone")); |
|
794 |
return (zfs_error(hdl, EZFS_ZONED, errbuf)); |
|
789 | 795 |
} |
796 |
} else if (getzoneid() != GLOBAL_ZONEID) { |
|
797 |
/* |
|
798 |
* If zoned property is 'off', this must be in |
|
799 |
* a globle zone. If not, something is wrong. |
|
800 |
*/ |
|
2082 | 801 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
802 |
"dataset is used in a non-global zone, but " |
|
803 |
"'zoned' property is not set")); |
|
804 |
return (zfs_error(hdl, EZFS_ZONED, errbuf)); |
|
789 | 805 |
} |
806 |
} |
|
807 |
||
808 |
if ((cl = changelist_gather(zhp, prop, 0)) == NULL) |
|
809 |
return (-1); |
|
810 |
||
811 |
if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { |
|
2082 | 812 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
813 |
"child dataset with inherited mountpoint is used " |
|
814 |
"in a non-global zone")); |
|
815 |
ret = zfs_error(hdl, EZFS_ZONED, errbuf); |
|
789 | 816 |
goto error; |
817 |
} |
|
818 |
||
819 |
if ((ret = changelist_prefix(cl)) != 0) |
|
820 |
goto error; |
|
821 |
||
822 |
/* |
|
823 |
* Execute the corresponding ioctl() to set this property. |
|
824 |
*/ |
|
825 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
826 |
||
827 |
switch (prop) { |
|
828 |
case ZFS_PROP_QUOTA: |
|
829 |
zc.zc_cookie = number; |
|
2082 | 830 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SET_QUOTA, &zc); |
789 | 831 |
break; |
832 |
case ZFS_PROP_RESERVATION: |
|
833 |
zc.zc_cookie = number; |
|
2082 | 834 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SET_RESERVATION, |
835 |
&zc); |
|
789 | 836 |
break; |
837 |
case ZFS_PROP_MOUNTPOINT: |
|
838 |
case ZFS_PROP_SHARENFS: |
|
839 |
/* |
|
840 |
* These properties are passed down as real strings. |
|
841 |
*/ |
|
842 |
(void) strlcpy(zc.zc_prop_name, propname, |
|
843 |
sizeof (zc.zc_prop_name)); |
|
844 |
(void) strlcpy(zc.zc_prop_value, propval, |
|
845 |
sizeof (zc.zc_prop_value)); |
|
846 |
zc.zc_intsz = 1; |
|
847 |
zc.zc_numints = strlen(propval) + 1; |
|
2082 | 848 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SET_PROP, &zc); |
789 | 849 |
break; |
850 |
case ZFS_PROP_VOLSIZE: |
|
851 |
zc.zc_volsize = number; |
|
2082 | 852 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SET_VOLSIZE, &zc); |
789 | 853 |
break; |
854 |
case ZFS_PROP_VOLBLOCKSIZE: |
|
855 |
zc.zc_volblocksize = number; |
|
2082 | 856 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SET_VOLBLOCKSIZE, |
857 |
&zc); |
|
789 | 858 |
break; |
859 |
default: |
|
860 |
(void) strlcpy(zc.zc_prop_name, propname, |
|
861 |
sizeof (zc.zc_prop_name)); |
|
862 |
/* LINTED - alignment */ |
|
863 |
*(uint64_t *)zc.zc_prop_value = number; |
|
864 |
zc.zc_intsz = 8; |
|
865 |
zc.zc_numints = 1; |
|
2082 | 866 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SET_PROP, &zc); |
789 | 867 |
break; |
868 |
} |
|
869 |
||
870 |
if (ret != 0) { |
|
871 |
switch (errno) { |
|
872 |
||
873 |
case ENOSPC: |
|
874 |
/* |
|
875 |
* For quotas and reservations, ENOSPC indicates |
|
876 |
* something different; setting a quota or reservation |
|
877 |
* doesn't use any disk space. |
|
878 |
*/ |
|
879 |
switch (prop) { |
|
880 |
case ZFS_PROP_QUOTA: |
|
2082 | 881 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
882 |
"size is less than current used or " |
|
883 |
"reserved space")); |
|
884 |
(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); |
|
789 | 885 |
break; |
886 |
||
887 |
case ZFS_PROP_RESERVATION: |
|
2082 | 888 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
889 |
"size is greater than available space")); |
|
890 |
(void) zfs_error(hdl, EZFS_PROPSPACE, errbuf); |
|
789 | 891 |
break; |
892 |
||
893 |
default: |
|
2082 | 894 |
(void) zfs_standard_error(hdl, errno, errbuf); |
789 | 895 |
break; |
896 |
} |
|
897 |
break; |
|
898 |
||
899 |
case EBUSY: |
|
2082 | 900 |
if (prop == ZFS_PROP_VOLBLOCKSIZE) |
901 |
(void) zfs_error(hdl, EZFS_VOLHASDATA, errbuf); |
|
902 |
else |
|
903 |
return (zfs_standard_error(hdl, EBUSY, errbuf)); |
|
789 | 904 |
break; |
905 |
||
1175
759d20c7e57b
6366265 attach/replace should allow a new device size at least the min of all devs in a mirror/raidz
lling
parents:
1133
diff
changeset
|
906 |
case EROFS: |
2082 | 907 |
(void) zfs_error(hdl, EZFS_DSREADONLY, errbuf); |
1175
759d20c7e57b
6366265 attach/replace should allow a new device size at least the min of all devs in a mirror/raidz
lling
parents:
1133
diff
changeset
|
908 |
break; |
759d20c7e57b
6366265 attach/replace should allow a new device size at least the min of all devs in a mirror/raidz
lling
parents:
1133
diff
changeset
|
909 |
|
789 | 910 |
case EOVERFLOW: |
911 |
/* |
|
912 |
* This platform can't address a volume this big. |
|
913 |
*/ |
|
914 |
#ifdef _ILP32 |
|
915 |
if (prop == ZFS_PROP_VOLSIZE) { |
|
2082 | 916 |
(void) zfs_error(hdl, EZFS_VOLTOOBIG, errbuf); |
789 | 917 |
break; |
918 |
} |
|
919 |
#endif |
|
2082 | 920 |
/* FALLTHROUGH */ |
789 | 921 |
default: |
2082 | 922 |
(void) zfs_standard_error(hdl, errno, errbuf); |
789 | 923 |
} |
924 |
} else { |
|
925 |
/* |
|
926 |
* Refresh the statistics so the new property value |
|
927 |
* is reflected. |
|
928 |
*/ |
|
929 |
if ((ret = changelist_postfix(cl)) != 0) |
|
930 |
goto error; |
|
931 |
||
932 |
(void) get_stats(zhp); |
|
933 |
} |
|
934 |
||
935 |
error: |
|
936 |
changelist_free(cl); |
|
937 |
return (ret); |
|
938 |
} |
|
939 |
||
940 |
/* |
|
941 |
* Given a property, inherit the value from the parent dataset. |
|
942 |
*/ |
|
943 |
int |
|
944 |
zfs_prop_inherit(zfs_handle_t *zhp, zfs_prop_t prop) |
|
945 |
{ |
|
946 |
const char *propname = zfs_prop_to_name(prop); |
|
947 |
zfs_cmd_t zc = { 0 }; |
|
948 |
int ret; |
|
949 |
prop_changelist_t *cl; |
|
2082 | 950 |
libzfs_handle_t *hdl = zhp->zfs_hdl; |
951 |
char errbuf[1024]; |
|
952 |
||
953 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
|
954 |
"cannot inherit %s for '%s'"), propname, zhp->zfs_name); |
|
789 | 955 |
|
956 |
/* |
|
957 |
* Verify that this property is inheritable. |
|
958 |
*/ |
|
2082 | 959 |
if (zfs_prop_readonly(prop)) |
960 |
return (zfs_error(hdl, EZFS_PROPREADONLY, errbuf)); |
|
961 |
||
962 |
if (!zfs_prop_inheritable(prop)) |
|
963 |
return (zfs_error(hdl, EZFS_PROPNONINHERIT, errbuf)); |
|
789 | 964 |
|
965 |
/* |
|
966 |
* Check to see if the value applies to this type |
|
967 |
*/ |
|
2082 | 968 |
if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) |
969 |
return (zfs_error(hdl, EZFS_PROPTYPE, errbuf)); |
|
789 | 970 |
|
971 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
972 |
(void) strlcpy(zc.zc_prop_name, propname, sizeof (zc.zc_prop_name)); |
|
973 |
||
974 |
if (prop == ZFS_PROP_MOUNTPOINT && getzoneid() == GLOBAL_ZONEID && |
|
975 |
zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { |
|
2082 | 976 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
977 |
"dataset is used in a non-global zone")); |
|
978 |
return (zfs_error(hdl, EZFS_ZONED, errbuf)); |
|
789 | 979 |
} |
980 |
||
981 |
/* |
|
982 |
* Determine datasets which will be affected by this change, if any. |
|
983 |
*/ |
|
984 |
if ((cl = changelist_gather(zhp, prop, 0)) == NULL) |
|
985 |
return (-1); |
|
986 |
||
987 |
if (prop == ZFS_PROP_MOUNTPOINT && changelist_haszonedchild(cl)) { |
|
2082 | 988 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
989 |
"child dataset with inherited mountpoint is used " |
|
990 |
"in a non-global zone")); |
|
991 |
ret = zfs_error(hdl, EZFS_ZONED, errbuf); |
|
789 | 992 |
goto error; |
993 |
} |
|
994 |
||
995 |
if ((ret = changelist_prefix(cl)) != 0) |
|
996 |
goto error; |
|
997 |
||
998 |
zc.zc_numints = 0; |
|
999 |
||
2082 | 1000 |
if ((ret = ioctl(zhp->zfs_hdl->libzfs_fd, |
1001 |
ZFS_IOC_SET_PROP, &zc)) != 0) { |
|
1002 |
return (zfs_standard_error(hdl, errno, errbuf)); |
|
789 | 1003 |
} else { |
1004 |
||
2169
0152c8ed1663
6385436 zfs set <property> returns an error, but still sets property value (fix build)
nd150628
parents:
2166
diff
changeset
|
1005 |
if ((ret = changelist_postfix(cl)) != 0) |
789 | 1006 |
goto error; |
1007 |
||
1008 |
/* |
|
1009 |
* Refresh the statistics so the new property is reflected. |
|
1010 |
*/ |
|
1011 |
(void) get_stats(zhp); |
|
1012 |
} |
|
1013 |
||
1014 |
||
1015 |
error: |
|
1016 |
changelist_free(cl); |
|
1017 |
return (ret); |
|
1018 |
} |
|
1019 |
||
1020 |
static void |
|
1021 |
nicebool(int value, char *buf, size_t buflen) |
|
1022 |
{ |
|
1023 |
if (value) |
|
1024 |
(void) strlcpy(buf, "on", buflen); |
|
1025 |
else |
|
1026 |
(void) strlcpy(buf, "off", buflen); |
|
1027 |
} |
|
1028 |
||
1029 |
/* |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1030 |
* True DSL properties are stored in an nvlist. The following two functions |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1031 |
* extract them appropriately. |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1032 |
*/ |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1033 |
static uint64_t |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1034 |
getprop_uint64(zfs_handle_t *zhp, zfs_prop_t prop, char **source) |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1035 |
{ |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1036 |
nvlist_t *nv; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1037 |
uint64_t value; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1038 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1039 |
if (nvlist_lookup_nvlist(zhp->zfs_props, |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1040 |
zfs_prop_to_name(prop), &nv) == 0) { |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1041 |
verify(nvlist_lookup_uint64(nv, ZFS_PROP_VALUE, &value) == 0); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1042 |
verify(nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source) == 0); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1043 |
} else { |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1044 |
value = zfs_prop_default_numeric(prop); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1045 |
*source = ""; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1046 |
} |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1047 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1048 |
return (value); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1049 |
} |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1050 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1051 |
static char * |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1052 |
getprop_string(zfs_handle_t *zhp, zfs_prop_t prop, char **source) |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1053 |
{ |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1054 |
nvlist_t *nv; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1055 |
char *value; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1056 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1057 |
if (nvlist_lookup_nvlist(zhp->zfs_props, |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1058 |
zfs_prop_to_name(prop), &nv) == 0) { |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1059 |
verify(nvlist_lookup_string(nv, ZFS_PROP_VALUE, &value) == 0); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1060 |
verify(nvlist_lookup_string(nv, ZFS_PROP_SOURCE, source) == 0); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1061 |
} else { |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1062 |
if ((value = (char *)zfs_prop_default_string(prop)) == NULL) |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1063 |
value = ""; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1064 |
*source = ""; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1065 |
} |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1066 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1067 |
return (value); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1068 |
} |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1069 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1070 |
/* |
789 | 1071 |
* Internal function for getting a numeric property. Both zfs_prop_get() and |
1072 |
* zfs_prop_get_int() are built using this interface. |
|
1073 |
* |
|
1074 |
* Certain properties can be overridden using 'mount -o'. In this case, scan |
|
1075 |
* the contents of the /etc/mnttab entry, searching for the appropriate options. |
|
1076 |
* If they differ from the on-disk values, report the current values and mark |
|
1077 |
* the source "temporary". |
|
1078 |
*/ |
|
2082 | 1079 |
static int |
789 | 1080 |
get_numeric_property(zfs_handle_t *zhp, zfs_prop_t prop, zfs_source_t *src, |
2082 | 1081 |
char **source, uint64_t *val) |
789 | 1082 |
{ |
1083 |
struct mnttab mnt; |
|
1084 |
||
1085 |
*source = NULL; |
|
1086 |
||
1087 |
if (zhp->zfs_mntopts == NULL) |
|
1088 |
mnt.mnt_mntopts = ""; |
|
1089 |
else |
|
1090 |
mnt.mnt_mntopts = zhp->zfs_mntopts; |
|
1091 |
||
1092 |
switch (prop) { |
|
1093 |
case ZFS_PROP_ATIME: |
|
2082 | 1094 |
*val = getprop_uint64(zhp, prop, source); |
1095 |
||
1096 |
if (hasmntopt(&mnt, MNTOPT_ATIME) && !*val) { |
|
1097 |
*val = B_TRUE; |
|
789 | 1098 |
if (src) |
1099 |
*src = ZFS_SRC_TEMPORARY; |
|
2082 | 1100 |
} else if (hasmntopt(&mnt, MNTOPT_NOATIME) && *val) { |
1101 |
*val = B_FALSE; |
|
789 | 1102 |
if (src) |
1103 |
*src = ZFS_SRC_TEMPORARY; |
|
1104 |
} |
|
2082 | 1105 |
break; |
789 | 1106 |
|
1107 |
case ZFS_PROP_AVAILABLE: |
|
2082 | 1108 |
*val = zhp->zfs_dmustats.dds_available; |
1109 |
break; |
|
789 | 1110 |
|
1111 |
case ZFS_PROP_DEVICES: |
|
2082 | 1112 |
*val = getprop_uint64(zhp, prop, source); |
1113 |
||
1114 |
if (hasmntopt(&mnt, MNTOPT_DEVICES) && !*val) { |
|
1115 |
*val = B_TRUE; |
|
789 | 1116 |
if (src) |
1117 |
*src = ZFS_SRC_TEMPORARY; |
|
2082 | 1118 |
} else if (hasmntopt(&mnt, MNTOPT_NODEVICES) && *val) { |
1119 |
*val = B_FALSE; |
|
789 | 1120 |
if (src) |
1121 |
*src = ZFS_SRC_TEMPORARY; |
|
1122 |
} |
|
2082 | 1123 |
break; |
789 | 1124 |
|
1125 |
case ZFS_PROP_EXEC: |
|
2082 | 1126 |
*val = getprop_uint64(zhp, prop, source); |
1127 |
||
1128 |
if (hasmntopt(&mnt, MNTOPT_EXEC) && !*val) { |
|
1129 |
*val = B_TRUE; |
|
789 | 1130 |
if (src) |
1131 |
*src = ZFS_SRC_TEMPORARY; |
|
2082 | 1132 |
} else if (hasmntopt(&mnt, MNTOPT_NOEXEC) && *val) { |
1133 |
*val = B_FALSE; |
|
789 | 1134 |
if (src) |
1135 |
*src = ZFS_SRC_TEMPORARY; |
|
1136 |
} |
|
2082 | 1137 |
break; |
789 | 1138 |
|
1139 |
case ZFS_PROP_RECORDSIZE: |
|
1140 |
case ZFS_PROP_COMPRESSION: |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1141 |
case ZFS_PROP_ZONED: |
2082 | 1142 |
*val = getprop_uint64(zhp, prop, source); |
1143 |
break; |
|
789 | 1144 |
|
1145 |
case ZFS_PROP_READONLY: |
|
2082 | 1146 |
*val = getprop_uint64(zhp, prop, source); |
1147 |
||
1148 |
if (hasmntopt(&mnt, MNTOPT_RO) && !*val) { |
|
1149 |
*val = B_TRUE; |
|
789 | 1150 |
if (src) |
1151 |
*src = ZFS_SRC_TEMPORARY; |
|
2082 | 1152 |
} else if (hasmntopt(&mnt, MNTOPT_RW) && *val) { |
1153 |
*val = B_FALSE; |
|
789 | 1154 |
if (src) |
1155 |
*src = ZFS_SRC_TEMPORARY; |
|
1156 |
} |
|
2082 | 1157 |
break; |
789 | 1158 |
|
1544 | 1159 |
case ZFS_PROP_CREATION: |
2082 | 1160 |
*val = zhp->zfs_dmustats.dds_creation_time; |
1161 |
break; |
|
1544 | 1162 |
|
789 | 1163 |
case ZFS_PROP_QUOTA: |
1164 |
if (zhp->zfs_dmustats.dds_quota == 0) |
|
1165 |
*source = ""; /* default */ |
|
1166 |
else |
|
1167 |
*source = zhp->zfs_name; |
|
2082 | 1168 |
*val = zhp->zfs_dmustats.dds_quota; |
1169 |
break; |
|
789 | 1170 |
|
1171 |
case ZFS_PROP_RESERVATION: |
|
1172 |
if (zhp->zfs_dmustats.dds_reserved == 0) |
|
1173 |
*source = ""; /* default */ |
|
1174 |
else |
|
1175 |
*source = zhp->zfs_name; |
|
2082 | 1176 |
*val = zhp->zfs_dmustats.dds_reserved; |
1177 |
break; |
|
789 | 1178 |
|
1179 |
case ZFS_PROP_COMPRESSRATIO: |
|
1180 |
/* |
|
1181 |
* Using physical space and logical space, calculate the |
|
1182 |
* compression ratio. We return the number as a multiple of |
|
1183 |
* 100, so '2.5x' would be returned as 250. |
|
1184 |
*/ |
|
1185 |
if (zhp->zfs_dmustats.dds_compressed_bytes == 0) |
|
2082 | 1186 |
*val = 100ULL; |
789 | 1187 |
else |
2082 | 1188 |
*val = |
1189 |
(zhp->zfs_dmustats.dds_uncompressed_bytes * 100 / |
|
789 | 1190 |
zhp->zfs_dmustats.dds_compressed_bytes); |
2082 | 1191 |
break; |
789 | 1192 |
|
1193 |
case ZFS_PROP_REFERENCED: |
|
1194 |
/* |
|
1195 |
* 'referenced' refers to the amount of physical space |
|
1196 |
* referenced (possibly shared) by this object. |
|
1197 |
*/ |
|
2082 | 1198 |
*val = zhp->zfs_dmustats.dds_space_refd; |
1199 |
break; |
|
789 | 1200 |
|
1201 |
case ZFS_PROP_SETUID: |
|
2082 | 1202 |
*val = getprop_uint64(zhp, prop, source); |
1203 |
||
1204 |
if (hasmntopt(&mnt, MNTOPT_SETUID) && !*val) { |
|
1205 |
*val = B_TRUE; |
|
789 | 1206 |
if (src) |
1207 |
*src = ZFS_SRC_TEMPORARY; |
|
2082 | 1208 |
} else if (hasmntopt(&mnt, MNTOPT_NOSETUID) && *val) { |
1209 |
*val = B_FALSE; |
|
789 | 1210 |
if (src) |
1211 |
*src = ZFS_SRC_TEMPORARY; |
|
1212 |
} |
|
2082 | 1213 |
break; |
789 | 1214 |
|
1215 |
case ZFS_PROP_VOLSIZE: |
|
2082 | 1216 |
*val = zhp->zfs_volsize; |
1217 |
break; |
|
789 | 1218 |
|
1219 |
case ZFS_PROP_VOLBLOCKSIZE: |
|
2082 | 1220 |
*val = zhp->zfs_volblocksize; |
1221 |
break; |
|
789 | 1222 |
|
1223 |
case ZFS_PROP_USED: |
|
2082 | 1224 |
*val = zhp->zfs_dmustats.dds_space_used; |
1225 |
break; |
|
789 | 1226 |
|
1227 |
case ZFS_PROP_CREATETXG: |
|
2082 | 1228 |
*val = zhp->zfs_dmustats.dds_creation_txg; |
1229 |
break; |
|
789 | 1230 |
|
1231 |
case ZFS_PROP_MOUNTED: |
|
1232 |
/* |
|
1233 |
* Unlike other properties, we defer calculation of 'MOUNTED' |
|
1234 |
* until actually requested. This is because the getmntany() |
|
1235 |
* call can be extremely expensive on systems with a large |
|
1236 |
* number of filesystems, and the property isn't needed in |
|
1237 |
* normal use cases. |
|
1238 |
*/ |
|
1239 |
if (zhp->zfs_mntopts == NULL) { |
|
1240 |
struct mnttab search = { 0 }, entry; |
|
1241 |
||
1242 |
search.mnt_special = (char *)zhp->zfs_name; |
|
1407
f178d5f7681f
6381720 zfs_unmount() not checking fstype in /etc/mnttab
nd150628
parents:
1393
diff
changeset
|
1243 |
search.mnt_fstype = MNTTYPE_ZFS; |
2082 | 1244 |
rewind(zhp->zfs_hdl->libzfs_mnttab); |
1245 |
||
1246 |
if (getmntany(zhp->zfs_hdl->libzfs_mnttab, &entry, |
|
1247 |
&search) == 0 && (zhp->zfs_mntopts = |
|
1248 |
zfs_strdup(zhp->zfs_hdl, |
|
1249 |
entry.mnt_mntopts)) == NULL) |
|
1250 |
return (-1); |
|
789 | 1251 |
} |
2082 | 1252 |
*val = (zhp->zfs_mntopts != NULL); |
1253 |
break; |
|
789 | 1254 |
|
1255 |
default: |
|
2082 | 1256 |
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, |
1257 |
"cannot get non-numeric property")); |
|
1258 |
return (zfs_error(zhp->zfs_hdl, EZFS_BADPROP, |
|
1259 |
dgettext(TEXT_DOMAIN, "internal error"))); |
|
789 | 1260 |
} |
1261 |
||
1262 |
return (0); |
|
1263 |
} |
|
1264 |
||
1265 |
/* |
|
1266 |
* Calculate the source type, given the raw source string. |
|
1267 |
*/ |
|
1268 |
static void |
|
1269 |
get_source(zfs_handle_t *zhp, zfs_source_t *srctype, char *source, |
|
1270 |
char *statbuf, size_t statlen) |
|
1271 |
{ |
|
1272 |
if (statbuf == NULL || *srctype == ZFS_SRC_TEMPORARY) |
|
1273 |
return; |
|
1274 |
||
1275 |
if (source == NULL) { |
|
1276 |
*srctype = ZFS_SRC_NONE; |
|
1277 |
} else if (source[0] == '\0') { |
|
1278 |
*srctype = ZFS_SRC_DEFAULT; |
|
1279 |
} else { |
|
1280 |
if (strcmp(source, zhp->zfs_name) == 0) { |
|
1281 |
*srctype = ZFS_SRC_LOCAL; |
|
1282 |
} else { |
|
1283 |
(void) strlcpy(statbuf, source, statlen); |
|
1284 |
*srctype = ZFS_SRC_INHERITED; |
|
1285 |
} |
|
1286 |
} |
|
1287 |
||
1288 |
} |
|
1289 |
||
1290 |
/* |
|
1291 |
* Retrieve a property from the given object. If 'literal' is specified, then |
|
1292 |
* numbers are left as exact values. Otherwise, numbers are converted to a |
|
1293 |
* human-readable form. |
|
1294 |
* |
|
1295 |
* Returns 0 on success, or -1 on error. |
|
1296 |
*/ |
|
1297 |
int |
|
1298 |
zfs_prop_get(zfs_handle_t *zhp, zfs_prop_t prop, char *propbuf, size_t proplen, |
|
2082 | 1299 |
zfs_source_t *src, char *statbuf, size_t statlen, boolean_t literal) |
789 | 1300 |
{ |
1301 |
char *source = NULL; |
|
1302 |
uint64_t val; |
|
1303 |
char *str; |
|
1304 |
int i; |
|
1305 |
const char *root; |
|
1306 |
||
1307 |
/* |
|
1308 |
* Check to see if this property applies to our object |
|
1309 |
*/ |
|
1310 |
if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) |
|
1311 |
return (-1); |
|
1312 |
||
1313 |
if (src) |
|
1314 |
*src = ZFS_SRC_NONE; |
|
1315 |
||
1316 |
switch (prop) { |
|
1317 |
case ZFS_PROP_ATIME: |
|
1318 |
case ZFS_PROP_READONLY: |
|
1319 |
case ZFS_PROP_SETUID: |
|
1320 |
case ZFS_PROP_ZONED: |
|
1321 |
case ZFS_PROP_DEVICES: |
|
1322 |
case ZFS_PROP_EXEC: |
|
1323 |
/* |
|
1324 |
* Basic boolean values are built on top of |
|
1325 |
* get_numeric_property(). |
|
1326 |
*/ |
|
2082 | 1327 |
if (get_numeric_property(zhp, prop, src, &source, &val) != 0) |
1328 |
return (-1); |
|
1329 |
nicebool(val, propbuf, proplen); |
|
789 | 1330 |
|
1331 |
break; |
|
1332 |
||
1333 |
case ZFS_PROP_AVAILABLE: |
|
1334 |
case ZFS_PROP_RECORDSIZE: |
|
1335 |
case ZFS_PROP_CREATETXG: |
|
1336 |
case ZFS_PROP_REFERENCED: |
|
1337 |
case ZFS_PROP_USED: |
|
1338 |
case ZFS_PROP_VOLSIZE: |
|
1339 |
case ZFS_PROP_VOLBLOCKSIZE: |
|
1340 |
/* |
|
1341 |
* Basic numeric values are built on top of |
|
1342 |
* get_numeric_property(). |
|
1343 |
*/ |
|
2082 | 1344 |
if (get_numeric_property(zhp, prop, src, &source, &val) != 0) |
1345 |
return (-1); |
|
789 | 1346 |
if (literal) |
1347 |
(void) snprintf(propbuf, proplen, "%llu", val); |
|
1348 |
else |
|
1349 |
zfs_nicenum(val, propbuf, proplen); |
|
1350 |
break; |
|
1351 |
||
1352 |
case ZFS_PROP_COMPRESSION: |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1353 |
val = getprop_uint64(zhp, prop, &source); |
789 | 1354 |
for (i = 0; compress_table[i].name != NULL; i++) { |
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1355 |
if (compress_table[i].value == val) |
789 | 1356 |
break; |
1357 |
} |
|
1358 |
assert(compress_table[i].name != NULL); |
|
1359 |
(void) strlcpy(propbuf, compress_table[i].name, proplen); |
|
1360 |
break; |
|
1361 |
||
1362 |
case ZFS_PROP_CHECKSUM: |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1363 |
val = getprop_uint64(zhp, prop, &source); |
789 | 1364 |
for (i = 0; checksum_table[i].name != NULL; i++) { |
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1365 |
if (checksum_table[i].value == val) |
789 | 1366 |
break; |
1367 |
} |
|
1368 |
assert(checksum_table[i].name != NULL); |
|
1369 |
(void) strlcpy(propbuf, checksum_table[i].name, proplen); |
|
1370 |
break; |
|
1371 |
||
1372 |
case ZFS_PROP_SNAPDIR: |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1373 |
val = getprop_uint64(zhp, prop, &source); |
789 | 1374 |
for (i = 0; snapdir_table[i].name != NULL; i++) { |
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1375 |
if (snapdir_table[i].value == val) |
789 | 1376 |
break; |
1377 |
} |
|
1378 |
assert(snapdir_table[i].name != NULL); |
|
1379 |
(void) strlcpy(propbuf, snapdir_table[i].name, proplen); |
|
1380 |
break; |
|
1381 |
||
1382 |
case ZFS_PROP_ACLMODE: |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1383 |
val = getprop_uint64(zhp, prop, &source); |
789 | 1384 |
for (i = 0; acl_mode_table[i].name != NULL; i++) { |
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1385 |
if (acl_mode_table[i].value == val) |
789 | 1386 |
break; |
1387 |
} |
|
1388 |
assert(acl_mode_table[i].name != NULL); |
|
1389 |
(void) strlcpy(propbuf, acl_mode_table[i].name, proplen); |
|
1390 |
break; |
|
1391 |
||
1392 |
case ZFS_PROP_ACLINHERIT: |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1393 |
val = getprop_uint64(zhp, prop, &source); |
789 | 1394 |
for (i = 0; acl_inherit_table[i].name != NULL; i++) { |
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1395 |
if (acl_inherit_table[i].value == val) |
789 | 1396 |
break; |
1397 |
} |
|
1398 |
assert(acl_inherit_table[i].name != NULL); |
|
1399 |
(void) strlcpy(propbuf, acl_inherit_table[i].name, proplen); |
|
1400 |
break; |
|
1401 |
||
1402 |
case ZFS_PROP_CREATION: |
|
1403 |
/* |
|
1404 |
* 'creation' is a time_t stored in the statistics. We convert |
|
1405 |
* this into a string unless 'literal' is specified. |
|
1406 |
*/ |
|
1407 |
{ |
|
1408 |
time_t time = (time_t) |
|
1409 |
zhp->zfs_dmustats.dds_creation_time; |
|
1410 |
struct tm t; |
|
1411 |
||
1412 |
if (literal || |
|
1413 |
localtime_r(&time, &t) == NULL || |
|
1414 |
strftime(propbuf, proplen, "%a %b %e %k:%M %Y", |
|
1415 |
&t) == 0) |
|
1416 |
(void) snprintf(propbuf, proplen, "%llu", |
|
1417 |
zhp->zfs_dmustats.dds_creation_time); |
|
1418 |
} |
|
1419 |
break; |
|
1420 |
||
1421 |
case ZFS_PROP_MOUNTPOINT: |
|
1422 |
/* |
|
1423 |
* Getting the precise mountpoint can be tricky. |
|
1424 |
* |
|
1425 |
* - for 'none' or 'legacy', return those values. |
|
1426 |
* - for default mountpoints, construct it as /zfs/<dataset> |
|
1427 |
* - for inherited mountpoints, we want to take everything |
|
1428 |
* after our ancestor and append it to the inherited value. |
|
1429 |
* |
|
1430 |
* If the pool has an alternate root, we want to prepend that |
|
1431 |
* root to any values we return. |
|
1432 |
*/ |
|
1544 | 1433 |
root = zhp->zfs_root; |
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1434 |
str = getprop_string(zhp, prop, &source); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1435 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1436 |
if (str[0] == '\0') { |
789 | 1437 |
(void) snprintf(propbuf, proplen, "%s/zfs/%s", |
1438 |
root, zhp->zfs_name); |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1439 |
} else if (str[0] == '/') { |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1440 |
const char *relpath = zhp->zfs_name + strlen(source); |
789 | 1441 |
|
1442 |
if (relpath[0] == '/') |
|
1443 |
relpath++; |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1444 |
if (str[1] == '\0') |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1445 |
str++; |
789 | 1446 |
|
1447 |
if (relpath[0] == '\0') |
|
1448 |
(void) snprintf(propbuf, proplen, "%s%s", |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1449 |
root, str); |
789 | 1450 |
else |
1451 |
(void) snprintf(propbuf, proplen, "%s%s%s%s", |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1452 |
root, str, relpath[0] == '@' ? "" : "/", |
789 | 1453 |
relpath); |
1454 |
} else { |
|
1455 |
/* 'legacy' or 'none' */ |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1456 |
(void) strlcpy(propbuf, str, proplen); |
789 | 1457 |
} |
1458 |
||
1459 |
break; |
|
1460 |
||
1461 |
case ZFS_PROP_SHARENFS: |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1462 |
(void) strlcpy(propbuf, getprop_string(zhp, prop, &source), |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1463 |
proplen); |
789 | 1464 |
break; |
1465 |
||
1466 |
case ZFS_PROP_ORIGIN: |
|
1544 | 1467 |
(void) strlcpy(propbuf, zhp->zfs_dmustats.dds_clone_of, |
789 | 1468 |
proplen); |
1469 |
/* |
|
1470 |
* If there is no parent at all, return failure to indicate that |
|
1471 |
* it doesn't apply to this dataset. |
|
1472 |
*/ |
|
1473 |
if (propbuf[0] == '\0') |
|
1474 |
return (-1); |
|
1475 |
break; |
|
1476 |
||
1477 |
case ZFS_PROP_QUOTA: |
|
1478 |
case ZFS_PROP_RESERVATION: |
|
2082 | 1479 |
if (get_numeric_property(zhp, prop, src, &source, &val) != 0) |
1480 |
return (-1); |
|
789 | 1481 |
|
1482 |
/* |
|
1483 |
* If quota or reservation is 0, we translate this into 'none' |
|
1484 |
* (unless literal is set), and indicate that it's the default |
|
1485 |
* value. Otherwise, we print the number nicely and indicate |
|
1486 |
* that its set locally. |
|
1487 |
*/ |
|
1488 |
if (val == 0) { |
|
1489 |
if (literal) |
|
1490 |
(void) strlcpy(propbuf, "0", proplen); |
|
1491 |
else |
|
1492 |
(void) strlcpy(propbuf, "none", proplen); |
|
1493 |
} else { |
|
1494 |
if (literal) |
|
1495 |
(void) snprintf(propbuf, proplen, "%llu", val); |
|
1496 |
else |
|
1497 |
zfs_nicenum(val, propbuf, proplen); |
|
1498 |
} |
|
1499 |
break; |
|
1500 |
||
1501 |
case ZFS_PROP_COMPRESSRATIO: |
|
2082 | 1502 |
if (get_numeric_property(zhp, prop, src, &source, &val) != 0) |
1503 |
return (-1); |
|
789 | 1504 |
(void) snprintf(propbuf, proplen, "%lld.%02lldx", val / 100, |
1505 |
val % 100); |
|
1506 |
break; |
|
1507 |
||
1508 |
case ZFS_PROP_TYPE: |
|
1509 |
switch (zhp->zfs_type) { |
|
1510 |
case ZFS_TYPE_FILESYSTEM: |
|
1511 |
str = "filesystem"; |
|
1512 |
break; |
|
1513 |
case ZFS_TYPE_VOLUME: |
|
1514 |
str = "volume"; |
|
1515 |
break; |
|
1516 |
case ZFS_TYPE_SNAPSHOT: |
|
1517 |
str = "snapshot"; |
|
1518 |
break; |
|
1519 |
default: |
|
2082 | 1520 |
abort(); |
789 | 1521 |
} |
1522 |
(void) snprintf(propbuf, proplen, "%s", str); |
|
1523 |
break; |
|
1524 |
||
1525 |
case ZFS_PROP_MOUNTED: |
|
1526 |
/* |
|
1527 |
* The 'mounted' property is a pseudo-property that described |
|
1528 |
* whether the filesystem is currently mounted. Even though |
|
1529 |
* it's a boolean value, the typical values of "on" and "off" |
|
1530 |
* don't make sense, so we translate to "yes" and "no". |
|
1531 |
*/ |
|
2082 | 1532 |
if (get_numeric_property(zhp, ZFS_PROP_MOUNTED, |
1533 |
src, &source, &val) != 0) |
|
1534 |
return (-1); |
|
1535 |
if (val) |
|
789 | 1536 |
(void) strlcpy(propbuf, "yes", proplen); |
1537 |
else |
|
1538 |
(void) strlcpy(propbuf, "no", proplen); |
|
1539 |
break; |
|
1540 |
||
1541 |
case ZFS_PROP_NAME: |
|
1542 |
/* |
|
1543 |
* The 'name' property is a pseudo-property derived from the |
|
1544 |
* dataset name. It is presented as a real property to simplify |
|
1545 |
* consumers. |
|
1546 |
*/ |
|
1547 |
(void) strlcpy(propbuf, zhp->zfs_name, proplen); |
|
1548 |
break; |
|
1549 |
||
1550 |
default: |
|
2082 | 1551 |
abort(); |
789 | 1552 |
} |
1553 |
||
1554 |
get_source(zhp, src, source, statbuf, statlen); |
|
1555 |
||
1556 |
return (0); |
|
1557 |
} |
|
1558 |
||
1559 |
/* |
|
1560 |
* Utility function to get the given numeric property. Does no validation that |
|
1561 |
* the given property is the appropriate type; should only be used with |
|
1562 |
* hard-coded property types. |
|
1563 |
*/ |
|
1564 |
uint64_t |
|
1565 |
zfs_prop_get_int(zfs_handle_t *zhp, zfs_prop_t prop) |
|
1566 |
{ |
|
1567 |
char *source; |
|
1568 |
zfs_source_t sourcetype = ZFS_SRC_NONE; |
|
2082 | 1569 |
uint64_t val; |
1570 |
||
1571 |
(void) get_numeric_property(zhp, prop, &sourcetype, &source, &val); |
|
1572 |
||
1573 |
return (val); |
|
789 | 1574 |
} |
1575 |
||
1576 |
/* |
|
1577 |
* Similar to zfs_prop_get(), but returns the value as an integer. |
|
1578 |
*/ |
|
1579 |
int |
|
1580 |
zfs_prop_get_numeric(zfs_handle_t *zhp, zfs_prop_t prop, uint64_t *value, |
|
1581 |
zfs_source_t *src, char *statbuf, size_t statlen) |
|
1582 |
{ |
|
1583 |
char *source; |
|
1584 |
||
1585 |
/* |
|
1586 |
* Check to see if this property applies to our object |
|
1587 |
*/ |
|
1588 |
if (!zfs_prop_valid_for_type(prop, zhp->zfs_type)) |
|
2082 | 1589 |
return (zfs_error(zhp->zfs_hdl, EZFS_PROPTYPE, |
1590 |
dgettext(TEXT_DOMAIN, "cannot get property '%s'"), |
|
1591 |
zfs_prop_to_name(prop))); |
|
789 | 1592 |
|
1593 |
if (src) |
|
1594 |
*src = ZFS_SRC_NONE; |
|
1595 |
||
2082 | 1596 |
if (get_numeric_property(zhp, prop, src, &source, value) != 0) |
1597 |
return (-1); |
|
789 | 1598 |
|
1599 |
get_source(zhp, src, source, statbuf, statlen); |
|
1600 |
||
1601 |
return (0); |
|
1602 |
} |
|
1603 |
||
1604 |
/* |
|
1605 |
* Returns the name of the given zfs handle. |
|
1606 |
*/ |
|
1607 |
const char * |
|
1608 |
zfs_get_name(const zfs_handle_t *zhp) |
|
1609 |
{ |
|
1610 |
return (zhp->zfs_name); |
|
1611 |
} |
|
1612 |
||
1613 |
/* |
|
1614 |
* Returns the type of the given zfs handle. |
|
1615 |
*/ |
|
1616 |
zfs_type_t |
|
1617 |
zfs_get_type(const zfs_handle_t *zhp) |
|
1618 |
{ |
|
1619 |
return (zhp->zfs_type); |
|
1620 |
} |
|
1621 |
||
1622 |
/* |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1623 |
* Iterate over all child filesystems |
789 | 1624 |
*/ |
1625 |
int |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1626 |
zfs_iter_filesystems(zfs_handle_t *zhp, zfs_iter_f func, void *data) |
789 | 1627 |
{ |
1628 |
zfs_cmd_t zc = { 0 }; |
|
1629 |
zfs_handle_t *nzhp; |
|
1630 |
int ret; |
|
1631 |
||
1632 |
for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
2082 | 1633 |
ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DATASET_LIST_NEXT, &zc) == 0; |
789 | 1634 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { |
1635 |
/* |
|
1636 |
* Ignore private dataset names. |
|
1637 |
*/ |
|
1638 |
if (dataset_name_hidden(zc.zc_name)) |
|
1639 |
continue; |
|
1640 |
||
1641 |
/* |
|
1642 |
* Silently ignore errors, as the only plausible explanation is |
|
1643 |
* that the pool has since been removed. |
|
1644 |
*/ |
|
2082 | 1645 |
if ((nzhp = make_dataset_handle(zhp->zfs_hdl, |
1646 |
zc.zc_name)) == NULL) |
|
789 | 1647 |
continue; |
1648 |
||
1649 |
if ((ret = func(nzhp, data)) != 0) |
|
1650 |
return (ret); |
|
1651 |
} |
|
1652 |
||
1653 |
/* |
|
1654 |
* An errno value of ESRCH indicates normal completion. If ENOENT is |
|
1655 |
* returned, then the underlying dataset has been removed since we |
|
1656 |
* obtained the handle. |
|
1657 |
*/ |
|
1658 |
if (errno != ESRCH && errno != ENOENT) |
|
2082 | 1659 |
return (zfs_standard_error(zhp->zfs_hdl, errno, |
1660 |
dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); |
|
789 | 1661 |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1662 |
return (0); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1663 |
} |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1664 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1665 |
/* |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1666 |
* Iterate over all snapshots |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1667 |
*/ |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1668 |
int |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1669 |
zfs_iter_snapshots(zfs_handle_t *zhp, zfs_iter_f func, void *data) |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1670 |
{ |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1671 |
zfs_cmd_t zc = { 0 }; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1672 |
zfs_handle_t *nzhp; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1673 |
int ret; |
789 | 1674 |
|
1675 |
for ((void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
2082 | 1676 |
ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT_LIST_NEXT, |
1677 |
&zc) == 0; |
|
789 | 1678 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name))) { |
1679 |
||
2082 | 1680 |
if ((nzhp = make_dataset_handle(zhp->zfs_hdl, |
1681 |
zc.zc_name)) == NULL) |
|
789 | 1682 |
continue; |
1683 |
||
1684 |
if ((ret = func(nzhp, data)) != 0) |
|
1685 |
return (ret); |
|
1686 |
} |
|
1687 |
||
1688 |
/* |
|
1689 |
* An errno value of ESRCH indicates normal completion. If ENOENT is |
|
1690 |
* returned, then the underlying dataset has been removed since we |
|
1691 |
* obtained the handle. Silently ignore this case, and return success. |
|
1692 |
*/ |
|
1693 |
if (errno != ESRCH && errno != ENOENT) |
|
2082 | 1694 |
return (zfs_standard_error(zhp->zfs_hdl, errno, |
1695 |
dgettext(TEXT_DOMAIN, "cannot iterate filesystems"))); |
|
789 | 1696 |
|
1697 |
return (0); |
|
1698 |
} |
|
1699 |
||
1700 |
/* |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1701 |
* Iterate over all children, snapshots and filesystems |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1702 |
*/ |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1703 |
int |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1704 |
zfs_iter_children(zfs_handle_t *zhp, zfs_iter_f func, void *data) |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1705 |
{ |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1706 |
int ret; |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1707 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1708 |
if ((ret = zfs_iter_filesystems(zhp, func, data)) != 0) |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1709 |
return (ret); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1710 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1711 |
return (zfs_iter_snapshots(zhp, func, data)); |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1712 |
} |
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1713 |
|
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1714 |
/* |
789 | 1715 |
* Given a complete name, return just the portion that refers to the parent. |
1716 |
* Can return NULL if this is a pool. |
|
1717 |
*/ |
|
1718 |
static int |
|
1719 |
parent_name(const char *path, char *buf, size_t buflen) |
|
1720 |
{ |
|
1721 |
char *loc; |
|
1722 |
||
1723 |
if ((loc = strrchr(path, '/')) == NULL) |
|
1724 |
return (-1); |
|
1725 |
||
1726 |
(void) strncpy(buf, path, MIN(buflen, loc - path)); |
|
1727 |
buf[loc - path] = '\0'; |
|
1728 |
||
1729 |
return (0); |
|
1730 |
} |
|
1731 |
||
1732 |
/* |
|
1733 |
* Checks to make sure that the given path has a parent, and that it exists. |
|
1734 |
*/ |
|
1735 |
static int |
|
2082 | 1736 |
check_parents(libzfs_handle_t *hdl, const char *path) |
789 | 1737 |
{ |
1738 |
zfs_cmd_t zc = { 0 }; |
|
1739 |
char parent[ZFS_MAXNAMELEN]; |
|
1740 |
char *slash; |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1741 |
zfs_handle_t *zhp; |
2082 | 1742 |
char errbuf[1024]; |
1743 |
||
1744 |
(void) snprintf(errbuf, sizeof (errbuf), "cannot create '%s'", |
|
1745 |
path); |
|
789 | 1746 |
|
1747 |
/* get parent, and check to see if this is just a pool */ |
|
1748 |
if (parent_name(path, parent, sizeof (parent)) != 0) { |
|
2082 | 1749 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1750 |
"missing dataset name")); |
|
1751 |
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); |
|
789 | 1752 |
} |
1753 |
||
1754 |
/* check to see if the pool exists */ |
|
1755 |
if ((slash = strchr(parent, '/')) == NULL) |
|
1756 |
slash = parent + strlen(parent); |
|
1757 |
(void) strncpy(zc.zc_name, parent, slash - parent); |
|
1758 |
zc.zc_name[slash - parent] = '\0'; |
|
2082 | 1759 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) != 0 && |
789 | 1760 |
errno == ENOENT) { |
2082 | 1761 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1762 |
"no such pool '%s'"), zc.zc_name); |
|
1763 |
return (zfs_error(hdl, EZFS_NOENT, errbuf)); |
|
789 | 1764 |
} |
1765 |
||
1766 |
/* check to see if the parent dataset exists */ |
|
2082 | 1767 |
if ((zhp = make_dataset_handle(hdl, parent)) == NULL) { |
789 | 1768 |
switch (errno) { |
1769 |
case ENOENT: |
|
2082 | 1770 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1771 |
"parent does not exist")); |
|
1772 |
return (zfs_error(hdl, EZFS_NOENT, errbuf)); |
|
789 | 1773 |
|
1774 |
default: |
|
2082 | 1775 |
return (zfs_standard_error(hdl, errno, errbuf)); |
789 | 1776 |
} |
1777 |
} |
|
1778 |
||
1779 |
/* we are in a non-global zone, but parent is in the global zone */ |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1780 |
if (getzoneid() != GLOBAL_ZONEID && |
1393
72ffb78e31b0
6347497 'add fs' of a ZFS filesystem doesn't work for a local zone
lling
parents:
1356
diff
changeset
|
1781 |
!zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { |
2082 | 1782 |
(void) zfs_standard_error(hdl, EPERM, errbuf); |
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1783 |
zfs_close(zhp); |
789 | 1784 |
return (-1); |
1785 |
} |
|
1786 |
||
1787 |
/* make sure parent is a filesystem */ |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1788 |
if (zfs_get_type(zhp) != ZFS_TYPE_FILESYSTEM) { |
2082 | 1789 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1790 |
"parent is not a filesystem")); |
|
1791 |
(void) zfs_error(hdl, EZFS_BADTYPE, errbuf); |
|
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1792 |
zfs_close(zhp); |
789 | 1793 |
return (-1); |
1794 |
} |
|
1795 |
||
1356
e021b5e4aa0e
6377671 zfs mount -a shouldn't bother checking snapshots
eschrock
parents:
1294
diff
changeset
|
1796 |
zfs_close(zhp); |
789 | 1797 |
return (0); |
1798 |
} |
|
1799 |
||
1800 |
/* |
|
1801 |
* Create a new filesystem or volume. 'sizestr' and 'blocksizestr' are used |
|
1802 |
* only for volumes, and indicate the size and blocksize of the volume. |
|
1803 |
*/ |
|
1804 |
int |
|
2082 | 1805 |
zfs_create(libzfs_handle_t *hdl, const char *path, zfs_type_t type, |
789 | 1806 |
const char *sizestr, const char *blocksizestr) |
1807 |
{ |
|
1808 |
zfs_cmd_t zc = { 0 }; |
|
1809 |
int ret; |
|
1810 |
uint64_t size = 0; |
|
1811 |
uint64_t blocksize = zfs_prop_default_numeric(ZFS_PROP_VOLBLOCKSIZE); |
|
2082 | 1812 |
char errbuf[1024]; |
789 | 1813 |
|
1814 |
/* convert sizestr into integer size */ |
|
2082 | 1815 |
if (sizestr != NULL && nicestrtonum(hdl, sizestr, &size) != 0) |
1816 |
return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, |
|
1817 |
"bad volume size '%s'"), sizestr)); |
|
789 | 1818 |
|
1819 |
/* convert blocksizestr into integer blocksize */ |
|
2082 | 1820 |
if (blocksizestr != NULL && nicestrtonum(hdl, blocksizestr, |
1821 |
&blocksize) != 0) |
|
1822 |
return (zfs_error(hdl, EZFS_BADPROP, dgettext(TEXT_DOMAIN, |
|
1823 |
"bad volume blocksize '%s'"), blocksizestr)); |
|
1824 |
||
1825 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
|
1826 |
"cannot create '%s'"), path); |
|
789 | 1827 |
|
1828 |
/* validate the path, taking care to note the extended error message */ |
|
2082 | 1829 |
if (!zfs_validate_name(hdl, path, type)) |
1830 |
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); |
|
789 | 1831 |
|
1832 |
/* validate parents exist */ |
|
2082 | 1833 |
if (check_parents(hdl, path) != 0) |
789 | 1834 |
return (-1); |
1835 |
||
1836 |
/* |
|
1837 |
* The failure modes when creating a dataset of a different type over |
|
1838 |
* one that already exists is a little strange. In particular, if you |
|
1839 |
* try to create a dataset on top of an existing dataset, the ioctl() |
|
1840 |
* will return ENOENT, not EEXIST. To prevent this from happening, we |
|
1841 |
* first try to see if the dataset exists. |
|
1842 |
*/ |
|
1843 |
(void) strlcpy(zc.zc_name, path, sizeof (zc.zc_name)); |
|
2082 | 1844 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0) { |
1845 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
1846 |
"dataset already exists")); |
|
1847 |
return (zfs_error(hdl, EZFS_EXISTS, errbuf)); |
|
789 | 1848 |
} |
1849 |
||
1850 |
if (type == ZFS_TYPE_VOLUME) |
|
1851 |
zc.zc_objset_type = DMU_OST_ZVOL; |
|
1852 |
else |
|
1853 |
zc.zc_objset_type = DMU_OST_ZFS; |
|
1854 |
||
1855 |
if (type == ZFS_TYPE_VOLUME) { |
|
1133
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1856 |
/* |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1857 |
* If we are creating a volume, the size and block size must |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1858 |
* satisfy a few restraints. First, the blocksize must be a |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1859 |
* valid block size between SPA_{MIN,MAX}BLOCKSIZE. Second, the |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1860 |
* volsize must be a multiple of the block size, and cannot be |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1861 |
* zero. |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1862 |
*/ |
789 | 1863 |
if (size == 0) { |
2082 | 1864 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1865 |
"cannot be zero")); |
|
1866 |
return (zfs_error(hdl, EZFS_BADPROP, |
|
1867 |
dgettext(TEXT_DOMAIN, "bad volume size '%s'"), |
|
1868 |
sizestr)); |
|
789 | 1869 |
} |
1870 |
||
1133
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1871 |
if (blocksize < SPA_MINBLOCKSIZE || |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1872 |
blocksize > SPA_MAXBLOCKSIZE || !ISP2(blocksize)) { |
2082 | 1873 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1133
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1874 |
"must be power of 2 from %u to %uk"), |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1875 |
(uint_t)SPA_MINBLOCKSIZE, |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1876 |
(uint_t)SPA_MAXBLOCKSIZE >> 10); |
2082 | 1877 |
return (zfs_error(hdl, EZFS_BADPROP, |
1878 |
dgettext(TEXT_DOMAIN, |
|
1879 |
"bad volume block size '%s'"), blocksizestr)); |
|
1133
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1880 |
} |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1881 |
|
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1882 |
if (size % blocksize != 0) { |
2082 | 1883 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1884 |
"must be a multiple of volume block size")); |
|
1885 |
return (zfs_error(hdl, EZFS_BADPROP, |
|
1886 |
dgettext(TEXT_DOMAIN, "bad volume size '%s'"), |
|
1887 |
sizestr)); |
|
1133
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1888 |
} |
335d069294d1
6357470 vdev_raidz.c has unused RAIDZ_SINGLE define, code
eschrock
parents:
1003
diff
changeset
|
1889 |
|
789 | 1890 |
zc.zc_volsize = size; |
1891 |
zc.zc_volblocksize = blocksize; |
|
1892 |
} |
|
1893 |
||
1894 |
/* create the dataset */ |
|
2082 | 1895 |
ret = ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE, &zc); |
789 | 1896 |
|
1897 |
if (ret == 0 && type == ZFS_TYPE_VOLUME) |
|
2082 | 1898 |
ret = zvol_create_link(hdl, path); |
789 | 1899 |
|
1900 |
/* check for failure */ |
|
1901 |
if (ret != 0) { |
|
1902 |
char parent[ZFS_MAXNAMELEN]; |
|
1903 |
(void) parent_name(path, parent, sizeof (parent)); |
|
1904 |
||
1905 |
switch (errno) { |
|
1906 |
case ENOENT: |
|
2082 | 1907 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1908 |
"no such parent '%s'"), parent); |
|
1909 |
return (zfs_error(hdl, EZFS_NOENT, errbuf)); |
|
789 | 1910 |
|
1911 |
case EINVAL: |
|
2082 | 1912 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
1913 |
"parent '%s' is not a filesysem"), parent); |
|
1914 |
return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); |
|
789 | 1915 |
|
1916 |
case EDOM: |
|
2082 | 1917 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
789 | 1918 |
"must be power of 2 from %u to %uk"), |
1919 |
(uint_t)SPA_MINBLOCKSIZE, |
|
1920 |
(uint_t)SPA_MAXBLOCKSIZE >> 10); |
|
2082 | 1921 |
|
1922 |
return (zfs_error(hdl, EZFS_BADPROP, |
|
1923 |
dgettext(TEXT_DOMAIN, "bad block size '%s'"), |
|
1924 |
blocksizestr ? blocksizestr : "<unknown>")); |
|
1925 |
||
789 | 1926 |
#ifdef _ILP32 |
1927 |
case EOVERFLOW: |
|
1928 |
/* |
|
1929 |
* This platform can't address a volume this big. |
|
1930 |
*/ |
|
2082 | 1931 |
if (type == ZFS_TYPE_VOLUME) |
1932 |
return (zfs_error(hdl, EZFS_VOLTOOBIG, |
|
1933 |
errbuf)); |
|
789 | 1934 |
#endif |
2082 | 1935 |
/* FALLTHROUGH */ |
789 | 1936 |
default: |
2082 | 1937 |
return (zfs_standard_error(hdl, errno, errbuf)); |
789 | 1938 |
} |
1939 |
} |
|
1940 |
||
1941 |
return (0); |
|
1942 |
} |
|
1943 |
||
1944 |
/* |
|
1945 |
* Destroys the given dataset. The caller must make sure that the filesystem |
|
1946 |
* isn't mounted, and that there are no active dependents. |
|
1947 |
*/ |
|
1948 |
int |
|
1949 |
zfs_destroy(zfs_handle_t *zhp) |
|
1950 |
{ |
|
1951 |
zfs_cmd_t zc = { 0 }; |
|
1952 |
int ret; |
|
1953 |
||
1954 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
1955 |
||
1956 |
/* |
|
1957 |
* We use the check for 'zfs_volblocksize' instead of ZFS_TYPE_VOLUME |
|
1958 |
* so that we do the right thing for snapshots of volumes. |
|
1959 |
*/ |
|
1960 |
if (zhp->zfs_volblocksize != 0) { |
|
2082 | 1961 |
if (zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) |
789 | 1962 |
return (-1); |
1963 |
||
1964 |
zc.zc_objset_type = DMU_OST_ZVOL; |
|
1965 |
} else { |
|
1966 |
zc.zc_objset_type = DMU_OST_ZFS; |
|
1967 |
} |
|
1968 |
||
2082 | 1969 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DESTROY, &zc); |
2199 | 1970 |
if (ret != 0) { |
2082 | 1971 |
return (zfs_standard_error(zhp->zfs_hdl, errno, |
1972 |
dgettext(TEXT_DOMAIN, "cannot destroy '%s'"), |
|
1973 |
zhp->zfs_name)); |
|
2199 | 1974 |
} |
789 | 1975 |
|
1976 |
remove_mountpoint(zhp); |
|
1977 |
||
1978 |
return (0); |
|
1979 |
} |
|
1980 |
||
2199 | 1981 |
struct destroydata { |
1982 |
char *snapname; |
|
1983 |
boolean_t gotone; |
|
1984 |
}; |
|
1985 |
||
1986 |
static int |
|
1987 |
zfs_remove_link_cb(zfs_handle_t *zhp, void *arg) |
|
1988 |
{ |
|
1989 |
struct destroydata *dd = arg; |
|
1990 |
zfs_handle_t *szhp; |
|
1991 |
char name[ZFS_MAXNAMELEN]; |
|
1992 |
||
1993 |
(void) strcpy(name, zhp->zfs_name); |
|
1994 |
(void) strcat(name, "@"); |
|
1995 |
(void) strcat(name, dd->snapname); |
|
1996 |
||
1997 |
szhp = make_dataset_handle(zhp->zfs_hdl, name); |
|
1998 |
if (szhp) { |
|
1999 |
dd->gotone = B_TRUE; |
|
2000 |
zfs_close(szhp); |
|
2001 |
} |
|
2002 |
||
2003 |
if (zhp->zfs_type == ZFS_TYPE_VOLUME) { |
|
2004 |
(void) zvol_remove_link(zhp->zfs_hdl, name); |
|
2005 |
/* |
|
2006 |
* NB: this is simply a best-effort. We don't want to |
|
2007 |
* return an error, because then we wouldn't visit all |
|
2008 |
* the volumes. |
|
2009 |
*/ |
|
2010 |
} |
|
2011 |
||
2012 |
return (zfs_iter_filesystems(zhp, zfs_remove_link_cb, arg)); |
|
2013 |
} |
|
2014 |
||
2015 |
/* |
|
2016 |
* Destroys all snapshots with the given name in zhp & descendants. |
|
2017 |
*/ |
|
2018 |
int |
|
2019 |
zfs_destroy_snaps(zfs_handle_t *zhp, char *snapname) |
|
2020 |
{ |
|
2021 |
zfs_cmd_t zc = { 0 }; |
|
2022 |
int ret; |
|
2023 |
struct destroydata dd = { 0 }; |
|
2024 |
||
2025 |
dd.snapname = snapname; |
|
2026 |
(void) zfs_remove_link_cb(zhp, &dd); |
|
2027 |
||
2028 |
if (!dd.gotone) { |
|
2029 |
return (zfs_standard_error(zhp->zfs_hdl, ENOENT, |
|
2030 |
dgettext(TEXT_DOMAIN, "cannot destroy '%s@%s'"), |
|
2031 |
zhp->zfs_name, snapname)); |
|
2032 |
} |
|
2033 |
||
2034 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
2035 |
(void) strlcpy(zc.zc_prop_value, snapname, sizeof (zc.zc_prop_value)); |
|
2036 |
||
2037 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DESTROY_SNAPS, &zc); |
|
2038 |
if (ret != 0) { |
|
2039 |
char errbuf[1024]; |
|
2040 |
||
2041 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
|
2042 |
"cannot destroy '%s@%s'"), zc.zc_name, snapname); |
|
2043 |
||
2044 |
switch (errno) { |
|
2045 |
case EEXIST: |
|
2046 |
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, |
|
2047 |
"snapshot is cloned")); |
|
2048 |
return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, errbuf)); |
|
2049 |
||
2050 |
default: |
|
2051 |
return (zfs_standard_error(zhp->zfs_hdl, errno, |
|
2052 |
errbuf)); |
|
2053 |
} |
|
2054 |
} |
|
2055 |
||
2056 |
return (0); |
|
2057 |
} |
|
2058 |
||
789 | 2059 |
/* |
2060 |
* Clones the given dataset. The target must be of the same type as the source. |
|
2061 |
*/ |
|
2062 |
int |
|
2063 |
zfs_clone(zfs_handle_t *zhp, const char *target) |
|
2064 |
{ |
|
2065 |
zfs_cmd_t zc = { 0 }; |
|
2066 |
char parent[ZFS_MAXNAMELEN]; |
|
2067 |
int ret; |
|
2082 | 2068 |
char errbuf[1024]; |
2069 |
libzfs_handle_t *hdl = zhp->zfs_hdl; |
|
789 | 2070 |
|
2071 |
assert(zhp->zfs_type == ZFS_TYPE_SNAPSHOT); |
|
2072 |
||
2082 | 2073 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
2074 |
"cannot create '%s'"), target); |
|
2075 |
||
789 | 2076 |
/* validate the target name */ |
2082 | 2077 |
if (!zfs_validate_name(hdl, target, ZFS_TYPE_FILESYSTEM)) |
2078 |
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); |
|
789 | 2079 |
|
2080 |
/* validate parents exist */ |
|
2082 | 2081 |
if (check_parents(zhp->zfs_hdl, target) != 0) |
789 | 2082 |
return (-1); |
2083 |
||
2084 |
(void) parent_name(target, parent, sizeof (parent)); |
|
2085 |
||
2086 |
/* do the clone */ |
|
2087 |
if (zhp->zfs_volblocksize != 0) |
|
2088 |
zc.zc_objset_type = DMU_OST_ZVOL; |
|
2089 |
else |
|
2090 |
zc.zc_objset_type = DMU_OST_ZFS; |
|
2091 |
||
2092 |
(void) strlcpy(zc.zc_name, target, sizeof (zc.zc_name)); |
|
2093 |
(void) strlcpy(zc.zc_filename, zhp->zfs_name, sizeof (zc.zc_filename)); |
|
2082 | 2094 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_CREATE, &zc); |
789 | 2095 |
|
2096 |
if (ret != 0) { |
|
2097 |
switch (errno) { |
|
2098 |
||
2099 |
case ENOENT: |
|
2100 |
/* |
|
2101 |
* The parent doesn't exist. We should have caught this |
|
2102 |
* above, but there may a race condition that has since |
|
2103 |
* destroyed the parent. |
|
2104 |
* |
|
2105 |
* At this point, we don't know whether it's the source |
|
2106 |
* that doesn't exist anymore, or whether the target |
|
2107 |
* dataset doesn't exist. |
|
2108 |
*/ |
|
2082 | 2109 |
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, |
2110 |
"no such parent '%s'"), parent); |
|
2111 |
return (zfs_error(zhp->zfs_hdl, EZFS_NOENT, errbuf)); |
|
2112 |
||
2113 |
case EXDEV: |
|
2114 |
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, |
|
2115 |
"source and target pools differ")); |
|
2116 |
return (zfs_error(zhp->zfs_hdl, EZFS_CROSSTARGET, |
|
2117 |
errbuf)); |
|
2118 |
||
2119 |
default: |
|
2120 |
return (zfs_standard_error(zhp->zfs_hdl, errno, |
|
2121 |
errbuf)); |
|
2122 |
} |
|
2123 |
} else if (zhp->zfs_volblocksize != 0) { |
|
2124 |
ret = zvol_create_link(zhp->zfs_hdl, target); |
|
2125 |
} |
|
2126 |
||
2127 |
return (ret); |
|
2128 |
} |
|
2129 |
||
2130 |
typedef struct promote_data { |
|
2131 |
char cb_mountpoint[MAXPATHLEN]; |
|
2132 |
const char *cb_target; |
|
2133 |
const char *cb_errbuf; |
|
2134 |
uint64_t cb_pivot_txg; |
|
2135 |
} promote_data_t; |
|
2136 |
||
2137 |
static int |
|
2138 |
promote_snap_cb(zfs_handle_t *zhp, void *data) |
|
2139 |
{ |
|
2140 |
promote_data_t *pd = data; |
|
2141 |
zfs_handle_t *szhp; |
|
2142 |
char snapname[MAXPATHLEN]; |
|
2143 |
||
2144 |
/* We don't care about snapshots after the pivot point */ |
|
2145 |
if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) |
|
2146 |
return (0); |
|
2147 |
||
2417 | 2148 |
/* Remove the device link if it's a zvol. */ |
2149 |
if (zhp->zfs_volblocksize != 0) |
|
2150 |
(void) zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name); |
|
2082 | 2151 |
|
2152 |
/* Check for conflicting names */ |
|
2153 |
(void) strcpy(snapname, pd->cb_target); |
|
2417 | 2154 |
(void) strcat(snapname, strchr(zhp->zfs_name, '@')); |
2082 | 2155 |
szhp = make_dataset_handle(zhp->zfs_hdl, snapname); |
2156 |
if (szhp != NULL) { |
|
2157 |
zfs_close(szhp); |
|
2158 |
zfs_error_aux(zhp->zfs_hdl, dgettext(TEXT_DOMAIN, |
|
2159 |
"snapshot name '%s' from origin \n" |
|
2160 |
"conflicts with '%s' from target"), |
|
2161 |
zhp->zfs_name, snapname); |
|
2162 |
return (zfs_error(zhp->zfs_hdl, EZFS_EXISTS, pd->cb_errbuf)); |
|
2163 |
} |
|
2164 |
return (0); |
|
2165 |
} |
|
2166 |
||
2417 | 2167 |
static int |
2168 |
promote_snap_done_cb(zfs_handle_t *zhp, void *data) |
|
2169 |
{ |
|
2170 |
promote_data_t *pd = data; |
|
2171 |
||
2172 |
/* We don't care about snapshots after the pivot point */ |
|
2173 |
if (zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > pd->cb_pivot_txg) |
|
2174 |
return (0); |
|
2175 |
||
2176 |
/* Create the device link if it's a zvol. */ |
|
2177 |
if (zhp->zfs_volblocksize != 0) |
|
2178 |
(void) zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); |
|
2179 |
||
2180 |
return (0); |
|
2181 |
} |
|
2182 |
||
2082 | 2183 |
/* |
2184 |
* Promotes the given clone fs to be the clone parent. |
|
2185 |
*/ |
|
2186 |
int |
|
2187 |
zfs_promote(zfs_handle_t *zhp) |
|
2188 |
{ |
|
2189 |
libzfs_handle_t *hdl = zhp->zfs_hdl; |
|
2190 |
zfs_cmd_t zc = { 0 }; |
|
2191 |
char parent[MAXPATHLEN]; |
|
2192 |
char *cp; |
|
2193 |
int ret; |
|
2194 |
zfs_handle_t *pzhp; |
|
2195 |
promote_data_t pd; |
|
2196 |
char errbuf[1024]; |
|
2197 |
||
2198 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
|
2199 |
"cannot promote '%s'"), zhp->zfs_name); |
|
2200 |
||
2201 |
if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { |
|
2202 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
2203 |
"snapshots can not be promoted")); |
|
2204 |
return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); |
|
2205 |
} |
|
2206 |
||
2207 |
(void) strcpy(parent, zhp->zfs_dmustats.dds_clone_of); |
|
2208 |
if (parent[0] == '\0') { |
|
2209 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
2210 |
"not a cloned filesystem")); |
|
2211 |
return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); |
|
2212 |
} |
|
2213 |
cp = strchr(parent, '@'); |
|
2214 |
*cp = '\0'; |
|
2215 |
||
2216 |
/* Walk the snapshots we will be moving */ |
|
2217 |
pzhp = zfs_open(hdl, zhp->zfs_dmustats.dds_clone_of, ZFS_TYPE_SNAPSHOT); |
|
2218 |
if (pzhp == NULL) |
|
2219 |
return (-1); |
|
2220 |
pd.cb_pivot_txg = zfs_prop_get_int(pzhp, ZFS_PROP_CREATETXG); |
|
2221 |
zfs_close(pzhp); |
|
2222 |
pd.cb_target = zhp->zfs_name; |
|
2223 |
pd.cb_errbuf = errbuf; |
|
2224 |
pzhp = zfs_open(hdl, parent, ZFS_TYPE_ANY); |
|
2225 |
if (pzhp == NULL) |
|
2226 |
return (-1); |
|
2227 |
(void) zfs_prop_get(pzhp, ZFS_PROP_MOUNTPOINT, pd.cb_mountpoint, |
|
2228 |
sizeof (pd.cb_mountpoint), NULL, NULL, 0, FALSE); |
|
2229 |
ret = zfs_iter_snapshots(pzhp, promote_snap_cb, &pd); |
|
2417 | 2230 |
if (ret != 0) { |
2231 |
zfs_close(pzhp); |
|
2082 | 2232 |
return (-1); |
2417 | 2233 |
} |
2082 | 2234 |
|
2235 |
/* issue the ioctl */ |
|
2417 | 2236 |
(void) strlcpy(zc.zc_prop_value, zhp->zfs_dmustats.dds_clone_of, |
2237 |
sizeof (zc.zc_prop_value)); |
|
2082 | 2238 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
2239 |
ret = ioctl(hdl->libzfs_fd, ZFS_IOC_PROMOTE, &zc); |
|
2240 |
||
2241 |
if (ret != 0) { |
|
2417 | 2242 |
int save_errno = errno; |
2243 |
||
2244 |
(void) zfs_iter_snapshots(pzhp, promote_snap_done_cb, &pd); |
|
2245 |
zfs_close(pzhp); |
|
2246 |
||
2247 |
switch (save_errno) { |
|
789 | 2248 |
case EEXIST: |
2249 |
/* |
|
2082 | 2250 |
* There is a conflicting snapshot name. We |
2251 |
* should have caught this above, but they could |
|
2252 |
* have renamed something in the mean time. |
|
789 | 2253 |
*/ |
2082 | 2254 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2255 |
"conflicting snapshot name from parent '%s'"), |
|
2256 |
parent); |
|
2257 |
return (zfs_error(hdl, EZFS_EXISTS, errbuf)); |
|
789 | 2258 |
|
2259 |
default: |
|
2417 | 2260 |
return (zfs_standard_error(hdl, save_errno, errbuf)); |
789 | 2261 |
} |
2417 | 2262 |
} else { |
2263 |
(void) zfs_iter_snapshots(zhp, promote_snap_done_cb, &pd); |
|
789 | 2264 |
} |
2265 |
||
2417 | 2266 |
zfs_close(pzhp); |
789 | 2267 |
return (ret); |
2268 |
} |
|
2269 |
||
2199 | 2270 |
static int |
2271 |
zfs_create_link_cb(zfs_handle_t *zhp, void *arg) |
|
2272 |
{ |
|
2273 |
char *snapname = arg; |
|
2274 |
||
2275 |
if (zhp->zfs_type == ZFS_TYPE_VOLUME) { |
|
2276 |
char name[MAXPATHLEN]; |
|
2277 |
||
2278 |
(void) strcpy(name, zhp->zfs_name); |
|
2279 |
(void) strcat(name, "@"); |
|
2280 |
(void) strcat(name, snapname); |
|
2281 |
(void) zvol_create_link(zhp->zfs_hdl, name); |
|
2282 |
/* |
|
2283 |
* NB: this is simply a best-effort. We don't want to |
|
2284 |
* return an error, because then we wouldn't visit all |
|
2285 |
* the volumes. |
|
2286 |
*/ |
|
2287 |
} |
|
2288 |
return (zfs_iter_filesystems(zhp, zfs_create_link_cb, snapname)); |
|
2289 |
} |
|
2290 |
||
789 | 2291 |
/* |
2292 |
* Takes a snapshot of the given dataset |
|
2293 |
*/ |
|
2294 |
int |
|
2199 | 2295 |
zfs_snapshot(libzfs_handle_t *hdl, const char *path, boolean_t recursive) |
789 | 2296 |
{ |
2297 |
const char *delim; |
|
2298 |
char *parent; |
|
2299 |
zfs_handle_t *zhp; |
|
2300 |
zfs_cmd_t zc = { 0 }; |
|
2301 |
int ret; |
|
2082 | 2302 |
char errbuf[1024]; |
2303 |
||
2304 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
|
2305 |
"cannot snapshot '%s'"), path); |
|
2306 |
||
2307 |
/* validate the target name */ |
|
2308 |
if (!zfs_validate_name(hdl, path, ZFS_TYPE_SNAPSHOT)) |
|
2309 |
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); |
|
789 | 2310 |
|
2311 |
/* make sure the parent exists and is of the appropriate type */ |
|
2199 | 2312 |
delim = strchr(path, '@'); |
2082 | 2313 |
if ((parent = zfs_alloc(hdl, delim - path + 1)) == NULL) |
2314 |
return (-1); |
|
789 | 2315 |
(void) strncpy(parent, path, delim - path); |
2316 |
parent[delim - path] = '\0'; |
|
2317 |
||
2082 | 2318 |
if ((zhp = zfs_open(hdl, parent, ZFS_TYPE_FILESYSTEM | |
789 | 2319 |
ZFS_TYPE_VOLUME)) == NULL) { |
2320 |
free(parent); |
|
2321 |
return (-1); |
|
2322 |
} |
|
2323 |
||
2199 | 2324 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
2325 |
(void) strlcpy(zc.zc_prop_value, delim+1, sizeof (zc.zc_prop_value)); |
|
2326 |
zc.zc_cookie = recursive; |
|
2327 |
ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_SNAPSHOT, &zc); |
|
2328 |
||
2329 |
/* |
|
2330 |
* if it was recursive, the one that actually failed will be in |
|
2331 |
* zc.zc_name. |
|
2332 |
*/ |
|
2333 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
|
2334 |
"cannot create snapshot '%s@%s'"), zc.zc_name, zc.zc_prop_value); |
|
2335 |
if (ret == 0 && recursive) { |
|
2336 |
(void) zfs_iter_filesystems(zhp, |
|
2337 |
zfs_create_link_cb, (char *)delim+1); |
|
2338 |
} |
|
789 | 2339 |
if (ret == 0 && zhp->zfs_type == ZFS_TYPE_VOLUME) { |
2082 | 2340 |
ret = zvol_create_link(zhp->zfs_hdl, path); |
2199 | 2341 |
if (ret != 0) { |
2082 | 2342 |
(void) ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_DESTROY, |
2343 |
&zc); |
|
2199 | 2344 |
} |
789 | 2345 |
} |
2346 |
||
2082 | 2347 |
if (ret != 0) |
2348 |
(void) zfs_standard_error(hdl, errno, errbuf); |
|
789 | 2349 |
|
2350 |
free(parent); |
|
2351 |
zfs_close(zhp); |
|
2352 |
||
2353 |
return (ret); |
|
2354 |
} |
|
2355 |
||
2356 |
/* |
|
2357 |
* Dumps a backup of tosnap, incremental from fromsnap if it isn't NULL. |
|
2358 |
*/ |
|
2359 |
int |
|
1749 | 2360 |
zfs_send(zfs_handle_t *zhp_to, zfs_handle_t *zhp_from) |
789 | 2361 |
{ |
2362 |
zfs_cmd_t zc = { 0 }; |
|
2363 |
int ret; |
|
2082 | 2364 |
char errbuf[1024]; |
2365 |
libzfs_handle_t *hdl = zhp_to->zfs_hdl; |
|
2366 |
||
2367 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
|
2368 |
"cannot send '%s'"), zhp_to->zfs_name); |
|
789 | 2369 |
|
2370 |
/* do the ioctl() */ |
|
2371 |
(void) strlcpy(zc.zc_name, zhp_to->zfs_name, sizeof (zc.zc_name)); |
|
2372 |
if (zhp_from) { |
|
2373 |
(void) strlcpy(zc.zc_prop_value, zhp_from->zfs_name, |
|
2374 |
sizeof (zc.zc_name)); |
|
2375 |
} else { |
|
2376 |
zc.zc_prop_value[0] = '\0'; |
|
2377 |
} |
|
2378 |
zc.zc_cookie = STDOUT_FILENO; |
|
2379 |
||
2082 | 2380 |
ret = ioctl(zhp_to->zfs_hdl->libzfs_fd, ZFS_IOC_SENDBACKUP, &zc); |
789 | 2381 |
if (ret != 0) { |
2382 |
switch (errno) { |
|
2383 |
||
2384 |
case EXDEV: |
|
2082 | 2385 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2386 |
"not an ealier snapshot from the same fs")); |
|
2387 |
return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); |
|
789 | 2388 |
|
2389 |
case EDQUOT: |
|
2390 |
case EFBIG: |
|
2391 |
case EIO: |
|
2392 |
case ENOLINK: |
|
2393 |
case ENOSPC: |
|
2394 |
case ENOSTR: |
|
2395 |
case ENXIO: |
|
2396 |
case EPIPE: |
|
2397 |
case ERANGE: |
|
2398 |
case EFAULT: |
|
2399 |
case EROFS: |
|
2082 | 2400 |
zfs_error_aux(hdl, strerror(errno)); |
2401 |
return (zfs_error(hdl, EZFS_BADBACKUP, errbuf)); |
|
789 | 2402 |
|
2403 |
default: |
|
2082 | 2404 |
return (zfs_standard_error(hdl, errno, errbuf)); |
789 | 2405 |
} |
2406 |
} |
|
2407 |
||
2408 |
return (ret); |
|
2409 |
} |
|
2410 |
||
2411 |
/* |
|
2412 |
* Restores a backup of tosnap from stdin. |
|
2413 |
*/ |
|
2414 |
int |
|
2082 | 2415 |
zfs_receive(libzfs_handle_t *hdl, const char *tosnap, int isprefix, |
2416 |
int verbose, int dryrun) |
|
789 | 2417 |
{ |
2418 |
zfs_cmd_t zc = { 0 }; |
|
2419 |
time_t begin_time; |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2420 |
int ioctl_err, err, bytes, size; |
789 | 2421 |
char *cp; |
2422 |
dmu_replay_record_t drr; |
|
2423 |
struct drr_begin *drrb = &zc.zc_begin_record; |
|
2082 | 2424 |
char errbuf[1024]; |
789 | 2425 |
|
2426 |
begin_time = time(NULL); |
|
2427 |
||
2082 | 2428 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
2429 |
"cannot receive")); |
|
2430 |
||
789 | 2431 |
/* trim off snapname, if any */ |
2432 |
(void) strcpy(zc.zc_name, tosnap); |
|
2433 |
cp = strchr(zc.zc_name, '@'); |
|
2434 |
if (cp) |
|
2435 |
*cp = '\0'; |
|
2436 |
||
2437 |
/* read in the BEGIN record */ |
|
2438 |
cp = (char *)&drr; |
|
2439 |
bytes = 0; |
|
2440 |
do { |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2441 |
size = read(STDIN_FILENO, cp, sizeof (drr) - bytes); |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2442 |
cp += size; |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2443 |
bytes += size; |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2444 |
} while (size > 0); |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2445 |
|
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2446 |
if (size < 0 || bytes != sizeof (drr)) { |
2082 | 2447 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " |
2448 |
"stream (failed to read first record)")); |
|
2449 |
return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); |
|
789 | 2450 |
} |
2451 |
||
2452 |
zc.zc_begin_record = drr.drr_u.drr_begin; |
|
2453 |
||
2454 |
if (drrb->drr_magic != DMU_BACKUP_MAGIC && |
|
2455 |
drrb->drr_magic != BSWAP_64(DMU_BACKUP_MAGIC)) { |
|
2082 | 2456 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "invalid " |
2457 |
"stream (bad magic number)")); |
|
2458 |
return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); |
|
789 | 2459 |
} |
2460 |
||
2461 |
if (drrb->drr_version != DMU_BACKUP_VERSION && |
|
2462 |
drrb->drr_version != BSWAP_64(DMU_BACKUP_VERSION)) { |
|
2082 | 2463 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, "only version " |
2464 |
"0x%llx is supported (stream is version 0x%llx)"), |
|
789 | 2465 |
DMU_BACKUP_VERSION, drrb->drr_version); |
2082 | 2466 |
return (zfs_error(hdl, EZFS_BADSTREAM, errbuf)); |
789 | 2467 |
} |
2468 |
||
2469 |
/* |
|
2470 |
* Determine name of destination snapshot. |
|
2471 |
*/ |
|
1544 | 2472 |
(void) strcpy(zc.zc_filename, tosnap); |
789 | 2473 |
if (isprefix) { |
2474 |
if (strchr(tosnap, '@') != NULL) { |
|
2082 | 2475 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2476 |
"destination must be a filesystem")); |
|
2477 |
return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); |
|
789 | 2478 |
} |
2479 |
||
2480 |
cp = strchr(drr.drr_u.drr_begin.drr_toname, '/'); |
|
2481 |
if (cp == NULL) |
|
2482 |
cp = drr.drr_u.drr_begin.drr_toname; |
|
2483 |
else |
|
2484 |
cp++; |
|
2485 |
||
1544 | 2486 |
(void) strcat(zc.zc_filename, "/"); |
2487 |
(void) strcat(zc.zc_filename, cp); |
|
789 | 2488 |
} else if (strchr(tosnap, '@') == NULL) { |
2489 |
/* |
|
2490 |
* they specified just a filesystem; tack on the |
|
2491 |
* snapname from the backup. |
|
2492 |
*/ |
|
2493 |
cp = strchr(drr.drr_u.drr_begin.drr_toname, '@'); |
|
2082 | 2494 |
if (cp == NULL || strlen(tosnap) + strlen(cp) >= MAXNAMELEN) |
2495 |
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); |
|
1544 | 2496 |
(void) strcat(zc.zc_filename, cp); |
789 | 2497 |
} |
2498 |
||
2499 |
if (drrb->drr_fromguid) { |
|
2500 |
zfs_handle_t *h; |
|
2501 |
/* incremental backup stream */ |
|
2502 |
||
2503 |
/* do the ioctl to the containing fs */ |
|
1544 | 2504 |
(void) strcpy(zc.zc_name, zc.zc_filename); |
789 | 2505 |
cp = strchr(zc.zc_name, '@'); |
2506 |
*cp = '\0'; |
|
2507 |
||
2508 |
/* make sure destination fs exists */ |
|
2082 | 2509 |
h = zfs_open(hdl, zc.zc_name, |
2510 |
ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); |
|
2511 |
if (h == NULL) |
|
789 | 2512 |
return (-1); |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2513 |
if (!dryrun) { |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2514 |
/* unmount destination fs or remove device link. */ |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2515 |
if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2516 |
(void) zfs_unmount(h, NULL, 0); |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2517 |
} else { |
2082 | 2518 |
(void) zvol_remove_link(hdl, h->zfs_name); |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2519 |
} |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2520 |
} |
789 | 2521 |
zfs_close(h); |
2522 |
} else { |
|
2523 |
/* full backup stream */ |
|
2524 |
||
1544 | 2525 |
(void) strcpy(zc.zc_name, zc.zc_filename); |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2526 |
|
1749 | 2527 |
/* make sure they aren't trying to receive into the root */ |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2528 |
if (strchr(zc.zc_name, '/') == NULL) { |
789 | 2529 |
cp = strchr(zc.zc_name, '@'); |
2530 |
if (cp) |
|
2531 |
*cp = '\0'; |
|
2082 | 2532 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2533 |
"destination '%s' already exists"), zc.zc_name); |
|
2534 |
return (zfs_error(hdl, EZFS_EXISTS, errbuf)); |
|
789 | 2535 |
} |
2536 |
||
2537 |
if (isprefix) { |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2538 |
zfs_handle_t *h; |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2539 |
|
789 | 2540 |
/* make sure prefix exists */ |
2082 | 2541 |
h = zfs_open(hdl, tosnap, ZFS_TYPE_FILESYSTEM); |
2542 |
if (h == NULL) |
|
789 | 2543 |
return (-1); |
1544 | 2544 |
zfs_close(h); |
789 | 2545 |
|
2546 |
/* create any necessary ancestors up to prefix */ |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2547 |
zc.zc_objset_type = DMU_OST_ZFS; |
1544 | 2548 |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2549 |
/* |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2550 |
* zc.zc_name is now the full name of the snap |
1544 | 2551 |
* we're restoring into. Attempt to create, |
2552 |
* mount, and share any ancestor filesystems, up |
|
2553 |
* to the one that was named. |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2554 |
*/ |
1544 | 2555 |
for (cp = zc.zc_name + strlen(tosnap) + 1; |
2556 |
cp = strchr(cp, '/'); *cp = '/', cp++) { |
|
2557 |
const char *opname; |
|
789 | 2558 |
*cp = '\0'; |
1544 | 2559 |
|
2082 | 2560 |
opname = dgettext(TEXT_DOMAIN, "create"); |
2561 |
if (zfs_create(hdl, zc.zc_name, |
|
2562 |
ZFS_TYPE_FILESYSTEM, NULL, NULL) != 0) { |
|
1544 | 2563 |
if (errno == EEXIST) |
2564 |
continue; |
|
2565 |
goto ancestorerr; |
|
789 | 2566 |
} |
1544 | 2567 |
|
2082 | 2568 |
opname = dgettext(TEXT_DOMAIN, "open"); |
2569 |
h = zfs_open(hdl, zc.zc_name, |
|
2570 |
ZFS_TYPE_FILESYSTEM); |
|
1544 | 2571 |
if (h == NULL) |
2572 |
goto ancestorerr; |
|
2573 |
||
2082 | 2574 |
opname = dgettext(TEXT_DOMAIN, "mount"); |
1544 | 2575 |
if (zfs_mount(h, NULL, 0) != 0) |
2576 |
goto ancestorerr; |
|
2577 |
||
2082 | 2578 |
opname = dgettext(TEXT_DOMAIN, "share"); |
1544 | 2579 |
if (zfs_share(h) != 0) |
2580 |
goto ancestorerr; |
|
2581 |
||
2582 |
zfs_close(h); |
|
2583 |
||
2584 |
continue; |
|
2585 |
ancestorerr: |
|
2082 | 2586 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2587 |
"failed to %s ancestor '%s'"), opname, |
|
2588 |
zc.zc_name); |
|
2589 |
return (zfs_error(hdl, EZFS_BADRESTORE, |
|
2590 |
errbuf)); |
|
789 | 2591 |
} |
2592 |
} |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2593 |
|
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2594 |
/* Make sure destination fs does not exist */ |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2595 |
cp = strchr(zc.zc_name, '@'); |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2596 |
*cp = '\0'; |
2082 | 2597 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_OBJSET_STATS, &zc) == 0) { |
2598 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
|
2599 |
"destination '%s' exists"), zc.zc_name); |
|
2600 |
return (zfs_error(hdl, EZFS_EXISTS, errbuf)); |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2601 |
} |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2602 |
|
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2603 |
/* Do the recvbackup ioctl to the fs's parent. */ |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2604 |
cp = strrchr(zc.zc_name, '/'); |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2605 |
*cp = '\0'; |
789 | 2606 |
} |
2607 |
||
2608 |
(void) strcpy(zc.zc_prop_value, tosnap); |
|
2609 |
zc.zc_cookie = STDIN_FILENO; |
|
2610 |
zc.zc_intsz = isprefix; |
|
2611 |
if (verbose) { |
|
1749 | 2612 |
(void) printf("%s %s stream of %s into %s\n", |
2613 |
dryrun ? "would receive" : "receiving", |
|
789 | 2614 |
drrb->drr_fromguid ? "incremental" : "full", |
2615 |
drr.drr_u.drr_begin.drr_toname, |
|
1544 | 2616 |
zc.zc_filename); |
789 | 2617 |
(void) fflush(stdout); |
2618 |
} |
|
2619 |
if (dryrun) |
|
2620 |
return (0); |
|
2082 | 2621 |
err = ioctl_err = ioctl(hdl->libzfs_fd, ZFS_IOC_RECVBACKUP, &zc); |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2622 |
if (ioctl_err != 0) { |
789 | 2623 |
switch (errno) { |
2624 |
case ENODEV: |
|
2082 | 2625 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2626 |
"most recent snapshot does not match incremental " |
|
2627 |
"source")); |
|
2628 |
(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); |
|
789 | 2629 |
break; |
2630 |
case ETXTBSY: |
|
2082 | 2631 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2632 |
"destination has been modified since most recent " |
|
2633 |
"snapshot")); |
|
2634 |
(void) zfs_error(hdl, EZFS_BADRESTORE, errbuf); |
|
789 | 2635 |
break; |
2636 |
case EEXIST: |
|
2637 |
if (drrb->drr_fromguid == 0) { |
|
2638 |
/* it's the containing fs that exists */ |
|
1544 | 2639 |
cp = strchr(zc.zc_filename, '@'); |
789 | 2640 |
*cp = '\0'; |
2641 |
} |
|
2082 | 2642 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2643 |
"destination already exists")); |
|
2644 |
(void) zfs_error(hdl, EZFS_EXISTS, dgettext(TEXT_DOMAIN, |
|
2645 |
"cannot restore to %s"), zc.zc_filename); |
|
789 | 2646 |
break; |
2647 |
case EINVAL: |
|
2082 | 2648 |
(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2649 |
break; |
1544 | 2650 |
case ECKSUM: |
2082 | 2651 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2652 |
"invalid stream (checksum mismatch)")); |
|
2653 |
(void) zfs_error(hdl, EZFS_BADSTREAM, errbuf); |
|
789 | 2654 |
break; |
2655 |
default: |
|
2082 | 2656 |
(void) zfs_standard_error(hdl, errno, errbuf); |
789 | 2657 |
} |
2658 |
} |
|
2659 |
||
2660 |
/* |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2661 |
* Mount or recreate the /dev links for the target filesystem |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2662 |
* (if created, or if we tore them down to do an incremental |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2663 |
* restore), and the /dev links for the new snapshot (if |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2664 |
* created). |
789 | 2665 |
*/ |
1544 | 2666 |
cp = strchr(zc.zc_filename, '@'); |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2667 |
if (cp && (ioctl_err == 0 || drrb->drr_fromguid)) { |
789 | 2668 |
zfs_handle_t *h; |
2669 |
||
2670 |
*cp = '\0'; |
|
2082 | 2671 |
h = zfs_open(hdl, zc.zc_filename, |
789 | 2672 |
ZFS_TYPE_FILESYSTEM | ZFS_TYPE_VOLUME); |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2673 |
*cp = '@'; |
789 | 2674 |
if (h) { |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2675 |
if (h->zfs_type == ZFS_TYPE_FILESYSTEM) { |
789 | 2676 |
err = zfs_mount(h, NULL, 0); |
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2677 |
} else { |
2082 | 2678 |
err = zvol_create_link(hdl, h->zfs_name); |
1544 | 2679 |
if (err == 0 && ioctl_err == 0) |
2082 | 2680 |
err = zvol_create_link(hdl, |
2681 |
zc.zc_filename); |
|
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2682 |
} |
789 | 2683 |
zfs_close(h); |
2684 |
} |
|
2685 |
} |
|
2686 |
||
868
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2687 |
if (err || ioctl_err) |
09ef7c98db8f
6344651 LZ, 'zfs restore', internal error: unexpected error 1 ...
ahrens
parents:
849
diff
changeset
|
2688 |
return (-1); |
789 | 2689 |
|
2690 |
if (verbose) { |
|
2691 |
char buf1[64]; |
|
2692 |
char buf2[64]; |
|
2693 |
uint64_t bytes = zc.zc_cookie; |
|
2694 |
time_t delta = time(NULL) - begin_time; |
|
2695 |
if (delta == 0) |
|
2696 |
delta = 1; |
|
2697 |
zfs_nicenum(bytes, buf1, sizeof (buf1)); |
|
2698 |
zfs_nicenum(bytes/delta, buf2, sizeof (buf1)); |
|
2699 |
||
1749 | 2700 |
(void) printf("received %sb stream in %lu seconds (%sb/sec)\n", |
789 | 2701 |
buf1, delta, buf2); |
2702 |
} |
|
2703 |
return (0); |
|
2704 |
} |
|
2705 |
||
2706 |
/* |
|
1294
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2707 |
* Destroy any more recent snapshots. We invoke this callback on any dependents |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2708 |
* of the snapshot first. If the 'cb_dependent' member is non-zero, then this |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2709 |
* is a dependent and we should just destroy it without checking the transaction |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2710 |
* group. |
789 | 2711 |
*/ |
1294
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2712 |
typedef struct rollback_data { |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2713 |
const char *cb_target; /* the snapshot */ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2714 |
uint64_t cb_create; /* creation time reference */ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2715 |
prop_changelist_t *cb_clp; /* changelist pointer */ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2716 |
int cb_error; |
2082 | 2717 |
boolean_t cb_dependent; |
1294
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2718 |
} rollback_data_t; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2719 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2720 |
static int |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2721 |
rollback_destroy(zfs_handle_t *zhp, void *data) |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2722 |
{ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2723 |
rollback_data_t *cbp = data; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2724 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2725 |
if (!cbp->cb_dependent) { |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2726 |
if (strcmp(zhp->zfs_name, cbp->cb_target) != 0 && |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2727 |
zfs_get_type(zhp) == ZFS_TYPE_SNAPSHOT && |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2728 |
zfs_prop_get_int(zhp, ZFS_PROP_CREATETXG) > |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2729 |
cbp->cb_create) { |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2730 |
|
2082 | 2731 |
cbp->cb_dependent = B_TRUE; |
1294
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2732 |
(void) zfs_iter_dependents(zhp, rollback_destroy, cbp); |
2082 | 2733 |
cbp->cb_dependent = B_FALSE; |
1294
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2734 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2735 |
if (zfs_destroy(zhp) != 0) |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2736 |
cbp->cb_error = 1; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2737 |
else |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2738 |
changelist_remove(zhp, cbp->cb_clp); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2739 |
} |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2740 |
} else { |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2741 |
if (zfs_destroy(zhp) != 0) |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2742 |
cbp->cb_error = 1; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2743 |
else |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2744 |
changelist_remove(zhp, cbp->cb_clp); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2745 |
} |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2746 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2747 |
zfs_close(zhp); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2748 |
return (0); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2749 |
} |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2750 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2751 |
/* |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2752 |
* Rollback the dataset to its latest snapshot. |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2753 |
*/ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2754 |
static int |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2755 |
do_rollback(zfs_handle_t *zhp) |
789 | 2756 |
{ |
2757 |
int ret; |
|
2758 |
zfs_cmd_t zc = { 0 }; |
|
2759 |
||
2760 |
assert(zhp->zfs_type == ZFS_TYPE_FILESYSTEM || |
|
2761 |
zhp->zfs_type == ZFS_TYPE_VOLUME); |
|
2762 |
||
2763 |
if (zhp->zfs_type == ZFS_TYPE_VOLUME && |
|
2082 | 2764 |
zvol_remove_link(zhp->zfs_hdl, zhp->zfs_name) != 0) |
789 | 2765 |
return (-1); |
2766 |
||
2767 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
2768 |
||
2769 |
if (zhp->zfs_volblocksize != 0) |
|
2770 |
zc.zc_objset_type = DMU_OST_ZVOL; |
|
2771 |
else |
|
2772 |
zc.zc_objset_type = DMU_OST_ZFS; |
|
2773 |
||
2774 |
/* |
|
2775 |
* We rely on the consumer to verify that there are no newer snapshots |
|
2776 |
* for the given dataset. Given these constraints, we can simply pass |
|
2777 |
* the name on to the ioctl() call. There is still an unlikely race |
|
2778 |
* condition where the user has taken a snapshot since we verified that |
|
2779 |
* this was the most recent. |
|
2780 |
*/ |
|
2082 | 2781 |
if ((ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_ROLLBACK, |
2782 |
&zc)) != 0) { |
|
2783 |
(void) zfs_standard_error(zhp->zfs_hdl, errno, |
|
2784 |
dgettext(TEXT_DOMAIN, "cannot rollback '%s'"), |
|
2785 |
zhp->zfs_name); |
|
789 | 2786 |
} else if (zhp->zfs_type == ZFS_TYPE_VOLUME) { |
2082 | 2787 |
ret = zvol_create_link(zhp->zfs_hdl, zhp->zfs_name); |
789 | 2788 |
} |
2789 |
||
2790 |
return (ret); |
|
2791 |
} |
|
2792 |
||
2793 |
/* |
|
1294
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2794 |
* Given a dataset, rollback to a specific snapshot, discarding any |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2795 |
* data changes since then and making it the active dataset. |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2796 |
* |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2797 |
* Any snapshots more recent than the target are destroyed, along with |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2798 |
* their dependents. |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2799 |
*/ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2800 |
int |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2801 |
zfs_rollback(zfs_handle_t *zhp, zfs_handle_t *snap, int flag) |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2802 |
{ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2803 |
int ret; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2804 |
rollback_data_t cb = { 0 }; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2805 |
prop_changelist_t *clp; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2806 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2807 |
/* |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2808 |
* Unmount all dependendents of the dataset and the dataset itself. |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2809 |
* The list we need to gather is the same as for doing rename |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2810 |
*/ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2811 |
clp = changelist_gather(zhp, ZFS_PROP_NAME, flag ? MS_FORCE: 0); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2812 |
if (clp == NULL) |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2813 |
return (-1); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2814 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2815 |
if ((ret = changelist_prefix(clp)) != 0) |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2816 |
goto out; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2817 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2818 |
/* |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2819 |
* Destroy all recent snapshots and its dependends. |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2820 |
*/ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2821 |
cb.cb_target = snap->zfs_name; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2822 |
cb.cb_create = zfs_prop_get_int(snap, ZFS_PROP_CREATETXG); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2823 |
cb.cb_clp = clp; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2824 |
(void) zfs_iter_children(zhp, rollback_destroy, &cb); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2825 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2826 |
if ((ret = cb.cb_error) != 0) { |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2827 |
(void) changelist_postfix(clp); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2828 |
goto out; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2829 |
} |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2830 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2831 |
/* |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2832 |
* Now that we have verified that the snapshot is the latest, |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2833 |
* rollback to the given snapshot. |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2834 |
*/ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2835 |
ret = do_rollback(zhp); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2836 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2837 |
if (ret != 0) { |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2838 |
(void) changelist_postfix(clp); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2839 |
goto out; |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2840 |
} |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2841 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2842 |
/* |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2843 |
* We only want to re-mount the filesystem if it was mounted in the |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2844 |
* first place. |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2845 |
*/ |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2846 |
ret = changelist_postfix(clp); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2847 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2848 |
out: |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2849 |
changelist_free(clp); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2850 |
return (ret); |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2851 |
} |
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2852 |
|
87b43e5de5ee
6364129 need to unmount any child datasets before doing the rollback
lling
parents:
1175
diff
changeset
|
2853 |
/* |
789 | 2854 |
* Iterate over all dependents for a given dataset. This includes both |
2855 |
* hierarchical dependents (children) and data dependents (snapshots and |
|
2856 |
* clones). The bulk of the processing occurs in get_dependents() in |
|
2857 |
* libzfs_graph.c. |
|
2858 |
*/ |
|
2859 |
int |
|
2860 |
zfs_iter_dependents(zfs_handle_t *zhp, zfs_iter_f func, void *data) |
|
2861 |
{ |
|
2862 |
char **dependents; |
|
2863 |
size_t count; |
|
2864 |
int i; |
|
2865 |
zfs_handle_t *child; |
|
2866 |
int ret = 0; |
|
2867 |
||
2082 | 2868 |
dependents = get_dependents(zhp->zfs_hdl, zhp->zfs_name, &count); |
789 | 2869 |
for (i = 0; i < count; i++) { |
2082 | 2870 |
if ((child = make_dataset_handle(zhp->zfs_hdl, |
2871 |
dependents[i])) == NULL) |
|
789 | 2872 |
continue; |
2873 |
||
2874 |
if ((ret = func(child, data)) != 0) |
|
2875 |
break; |
|
2876 |
} |
|
2877 |
||
2878 |
for (i = 0; i < count; i++) |
|
2879 |
free(dependents[i]); |
|
2880 |
free(dependents); |
|
2881 |
||
2882 |
return (ret); |
|
2883 |
} |
|
2884 |
||
2885 |
/* |
|
2886 |
* Renames the given dataset. |
|
2887 |
*/ |
|
2888 |
int |
|
2889 |
zfs_rename(zfs_handle_t *zhp, const char *target) |
|
2890 |
{ |
|
2891 |
int ret; |
|
2892 |
zfs_cmd_t zc = { 0 }; |
|
2893 |
char *delim; |
|
2894 |
prop_changelist_t *cl; |
|
2895 |
char parent[ZFS_MAXNAMELEN]; |
|
2082 | 2896 |
libzfs_handle_t *hdl = zhp->zfs_hdl; |
2897 |
char errbuf[1024]; |
|
789 | 2898 |
|
2899 |
(void) strlcpy(zc.zc_name, zhp->zfs_name, sizeof (zc.zc_name)); |
|
2900 |
(void) strlcpy(zc.zc_prop_value, target, sizeof (zc.zc_prop_value)); |
|
2901 |
||
2902 |
/* if we have the same exact name, just return success */ |
|
2903 |
if (strcmp(zhp->zfs_name, target) == 0) |
|
2904 |
return (0); |
|
2905 |
||
2082 | 2906 |
(void) snprintf(errbuf, sizeof (errbuf), dgettext(TEXT_DOMAIN, |
2907 |
"cannot rename to '%s'"), target); |
|
2908 |
||
789 | 2909 |
/* |
2910 |
* Make sure the target name is valid |
|
2911 |
*/ |
|
2082 | 2912 |
if (!zfs_validate_name(hdl, target, zhp->zfs_type)) |
2913 |
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); |
|
789 | 2914 |
|
2915 |
if (zhp->zfs_type == ZFS_TYPE_SNAPSHOT) { |
|
2082 | 2916 |
|
789 | 2917 |
if ((delim = strchr(target, '@')) == NULL) { |
2082 | 2918 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2919 |
"not a snapshot")); |
|
2920 |
return (zfs_error(hdl, EZFS_BADTYPE, errbuf)); |
|
789 | 2921 |
} |
2922 |
||
2923 |
/* |
|
2924 |
* Make sure we're renaming within the same dataset. |
|
2925 |
*/ |
|
2926 |
if (strncmp(zhp->zfs_name, target, delim - target) != 0 || |
|
2927 |
zhp->zfs_name[delim - target] != '@') { |
|
2082 | 2928 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2929 |
"snapshots must be part of same dataset")); |
|
2930 |
return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); |
|
789 | 2931 |
} |
2932 |
||
2933 |
(void) strncpy(parent, target, delim - target); |
|
2934 |
parent[delim - target] = '\0'; |
|
2935 |
} else { |
|
2936 |
/* validate parents */ |
|
2082 | 2937 |
if (check_parents(hdl, target) != 0) |
789 | 2938 |
return (-1); |
2939 |
||
2940 |
(void) parent_name(target, parent, sizeof (parent)); |
|
2941 |
||
2942 |
/* make sure we're in the same pool */ |
|
2943 |
verify((delim = strchr(target, '/')) != NULL); |
|
2944 |
if (strncmp(zhp->zfs_name, target, delim - target) != 0 || |
|
2945 |
zhp->zfs_name[delim - target] != '/') { |
|
2082 | 2946 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2947 |
"datasets must be within same pool")); |
|
2948 |
return (zfs_error(hdl, EZFS_CROSSTARGET, errbuf)); |
|
789 | 2949 |
} |
2440
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2950 |
|
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2951 |
/* new name cannot be a child of the current dataset name */ |
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2952 |
if (strncmp(parent, zhp->zfs_name, |
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2953 |
strlen(zhp->zfs_name)) == 0) { |
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2954 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2955 |
"New dataset name cannot be a descendent of " |
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2956 |
"current dataset name")); |
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2957 |
return (zfs_error(hdl, EZFS_INVALIDNAME, errbuf)); |
9281a87e8a9e
6451860 zfs rename' a filesystem|clone to its direct child will cause internal error
nd150628
parents:
2417
diff
changeset
|
2958 |
} |
789 | 2959 |
} |
2960 |
||
2082 | 2961 |
(void) snprintf(errbuf, sizeof (errbuf), |
2962 |
dgettext(TEXT_DOMAIN, "cannot rename '%s'"), zhp->zfs_name); |
|
2963 |
||
789 | 2964 |
if (getzoneid() == GLOBAL_ZONEID && |
2965 |
zfs_prop_get_int(zhp, ZFS_PROP_ZONED)) { |
|
2082 | 2966 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2967 |
"dataset is used in a non-global zone")); |
|
2968 |
return (zfs_error(hdl, EZFS_ZONED, errbuf)); |
|
789 | 2969 |
} |
2970 |
||
2971 |
if ((cl = changelist_gather(zhp, ZFS_PROP_NAME, 0)) == NULL) |
|
2082 | 2972 |
return (-1); |
789 | 2973 |
|
2974 |
if (changelist_haszonedchild(cl)) { |
|
2082 | 2975 |
zfs_error_aux(hdl, dgettext(TEXT_DOMAIN, |
2976 |
"child dataset with inherited mountpoint is used " |
|
2977 |
"in a non-global zone")); |
|
2978 |
ret = zfs_error(hdl, EZFS_ZONED, errbuf); |
|
789 | 2979 |
goto error; |
2980 |
} |
|
2981 |
||
2982 |
if ((ret = changelist_prefix(cl)) != 0) |
|
2983 |
goto error; |
|
2984 |
||
2985 |
if (zhp->zfs_volblocksize != 0) |
|
2986 |
zc.zc_objset_type = DMU_OST_ZVOL; |
|
2987 |
else |
|
2988 |
zc.zc_objset_type = DMU_OST_ZFS; |
|
2989 |
||
2082 | 2990 |
if ((ret = ioctl(zhp->zfs_hdl->libzfs_fd, ZFS_IOC_RENAME, &zc)) != 0) { |
2991 |
(void) zfs_standard_error(zhp->zfs_hdl, errno, errbuf); |
|
789 | 2992 |
|
2993 |
/* |
|
2994 |
* On failure, we still want to remount any filesystems that |
|
2995 |
* were previously mounted, so we don't alter the system state. |
|
2996 |
*/ |
|
2997 |
(void) changelist_postfix(cl); |
|
2998 |
} else { |
|
2999 |
changelist_rename(cl, zfs_get_name(zhp), target); |
|
3000 |
||
3001 |
ret = changelist_postfix(cl); |
|
3002 |
} |
|
3003 |
||
3004 |
error: |
|
3005 |
changelist_free(cl); |
|
3006 |
return (ret); |
|
3007 |
} |
|
3008 |
||
3009 |
/* |
|
3010 |
* Given a zvol dataset, issue the ioctl to create the appropriate minor node, |
|
3011 |
* poke devfsadm to create the /dev link, and then wait for the link to appear. |
|
3012 |
*/ |
|
3013 |
int |
|
2082 | 3014 |
zvol_create_link(libzfs_handle_t *hdl, const char *dataset) |
789 | 3015 |
{ |
3016 |
zfs_cmd_t zc = { 0 }; |
|
2082 | 3017 |
di_devlink_handle_t dhdl; |
789 | 3018 |
|
3019 |
(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); |
|
3020 |
||
3021 |
/* |
|
3022 |
* Issue the appropriate ioctl. |
|
3023 |
*/ |
|
2082 | 3024 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_CREATE_MINOR, &zc) != 0) { |
789 | 3025 |
switch (errno) { |
3026 |
case EEXIST: |
|
3027 |
/* |
|
3028 |
* Silently ignore the case where the link already |
|
3029 |
* exists. This allows 'zfs volinit' to be run multiple |
|
3030 |
* times without errors. |
|
3031 |
*/ |
|
3032 |
return (0); |
|
3033 |
||
3034 |
default: |
|
2082 | 3035 |
return (zfs_standard_error(hdl, errno, |
3036 |
dgettext(TEXT_DOMAIN, "cannot create device links " |
|
3037 |
"for '%s'"), dataset)); |
|
789 | 3038 |
} |
3039 |
} |
|
3040 |
||
3041 |
/* |
|
3042 |
* Call devfsadm and wait for the links to magically appear. |
|
3043 |
*/ |
|
2082 | 3044 |
if ((dhdl = di_devlink_init(ZFS_DRIVER, DI_MAKE_LINK)) == NULL) { |
3045 |
zfs_error_aux(hdl, strerror(errno)); |
|
3046 |
(void) zfs_error(hdl, EZFS_DEVLINKS, |
|
3047 |
dgettext(TEXT_DOMAIN, "cannot create device links " |
|
3048 |
"for '%s'"), dataset); |
|
3049 |
(void) ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc); |
|
789 | 3050 |
return (-1); |
3051 |
} else { |
|
2082 | 3052 |
(void) di_devlink_fini(&dhdl); |
789 | 3053 |
} |
3054 |
||
3055 |
return (0); |
|
3056 |
} |
|
3057 |
||
3058 |
/* |
|
3059 |
* Remove a minor node for the given zvol and the associated /dev links. |
|
3060 |
*/ |
|
3061 |
int |
|
2082 | 3062 |
zvol_remove_link(libzfs_handle_t *hdl, const char *dataset) |
789 | 3063 |
{ |
3064 |
zfs_cmd_t zc = { 0 }; |
|
3065 |
||
3066 |
(void) strlcpy(zc.zc_name, dataset, sizeof (zc.zc_name)); |
|
3067 |
||
2082 | 3068 |
if (ioctl(hdl->libzfs_fd, ZFS_IOC_REMOVE_MINOR, &zc) != 0) { |
789 | 3069 |
switch (errno) { |
3070 |
case ENXIO: |
|
3071 |
/* |
|
3072 |
* Silently ignore the case where the link no longer |
|
3073 |
* exists, so that 'zfs volfini' can be run multiple |
|
3074 |
* times without errors. |
|
3075 |
*/ |
|
3076 |
return (0); |
|
3077 |
||
3078 |
default: |
|
2082 | 3079 |
return (zfs_standard_error(hdl, errno, |
3080 |
dgettext(TEXT_DOMAIN, "cannot remove device " |
|
3081 |
"links for '%s'"), dataset)); |
|
789 | 3082 |
} |
3083 |
} |
|
3084 |
||
3085 |
return (0); |
|
3086 |
} |