]>
Commit | Line | Data |
---|---|---|
34dc7c2f BB |
1 | /* |
2 | * CDDL HEADER START | |
3 | * | |
4 | * The contents of this file are subject to the terms of the | |
5 | * Common Development and Distribution License (the "License"). | |
6 | * You may not use this file except in compliance with the License. | |
7 | * | |
8 | * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | |
9 | * or http://www.opensolaris.org/os/licensing. | |
10 | * See the License for the specific language governing permissions | |
11 | * and limitations under the License. | |
12 | * | |
13 | * When distributing Covered Code, include this CDDL HEADER in each | |
14 | * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | |
15 | * If applicable, add the following below this CDDL HEADER, with the | |
16 | * fields enclosed by brackets "[]" replaced with your own identifying | |
17 | * information: Portions Copyright [yyyy] [name of copyright owner] | |
18 | * | |
19 | * CDDL HEADER END | |
20 | */ | |
21 | /* | |
428870ff | 22 | * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. |
cae5b340 | 23 | * Copyright (c) 2012, 2016 by Delphix. All rights reserved. |
a08ee875 | 24 | * Copyright (c) 2013 Martin Matuska. All rights reserved. |
e10b0808 AX |
25 | * Copyright (c) 2014 Joyent, Inc. All rights reserved. |
26 | * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. | |
4e820b5a | 27 | * Copyright (c) 2016 Actifio, Inc. All rights reserved. |
34dc7c2f BB |
28 | */ |
29 | ||
34dc7c2f BB |
30 | #include <sys/dmu.h> |
31 | #include <sys/dmu_objset.h> | |
32 | #include <sys/dmu_tx.h> | |
33 | #include <sys/dsl_dataset.h> | |
34 | #include <sys/dsl_dir.h> | |
35 | #include <sys/dsl_prop.h> | |
36 | #include <sys/dsl_synctask.h> | |
37 | #include <sys/dsl_deleg.h> | |
ea04106b | 38 | #include <sys/dmu_impl.h> |
34dc7c2f | 39 | #include <sys/spa.h> |
428870ff | 40 | #include <sys/metaslab.h> |
34dc7c2f BB |
41 | #include <sys/zap.h> |
42 | #include <sys/zio.h> | |
43 | #include <sys/arc.h> | |
44 | #include <sys/sunddi.h> | |
e10b0808 AX |
45 | #include <sys/zfeature.h> |
46 | #include <sys/policy.h> | |
47 | #include <sys/zfs_znode.h> | |
a08ee875 | 48 | #include <sys/zvol.h> |
34dc7c2f | 49 | #include "zfs_namecheck.h" |
e10b0808 AX |
50 | #include "zfs_prop.h" |
51 | ||
52 | /* | |
53 | * Filesystem and Snapshot Limits | |
54 | * ------------------------------ | |
55 | * | |
56 | * These limits are used to restrict the number of filesystems and/or snapshots | |
57 | * that can be created at a given level in the tree or below. A typical | |
58 | * use-case is with a delegated dataset where the administrator wants to ensure | |
59 | * that a user within the zone is not creating too many additional filesystems | |
60 | * or snapshots, even though they're not exceeding their space quota. | |
61 | * | |
62 | * The filesystem and snapshot counts are stored as extensible properties. This | |
63 | * capability is controlled by a feature flag and must be enabled to be used. | |
64 | * Once enabled, the feature is not active until the first limit is set. At | |
65 | * that point, future operations to create/destroy filesystems or snapshots | |
66 | * will validate and update the counts. | |
67 | * | |
68 | * Because the count properties will not exist before the feature is active, | |
69 | * the counts are updated when a limit is first set on an uninitialized | |
70 | * dsl_dir node in the tree (The filesystem/snapshot count on a node includes | |
71 | * all of the nested filesystems/snapshots. Thus, a new leaf node has a | |
72 | * filesystem count of 0 and a snapshot count of 0. Non-existent filesystem and | |
73 | * snapshot count properties on a node indicate uninitialized counts on that | |
74 | * node.) When first setting a limit on an uninitialized node, the code starts | |
75 | * at the filesystem with the new limit and descends into all sub-filesystems | |
76 | * to add the count properties. | |
77 | * | |
78 | * In practice this is lightweight since a limit is typically set when the | |
79 | * filesystem is created and thus has no children. Once valid, changing the | |
80 | * limit value won't require a re-traversal since the counts are already valid. | |
81 | * When recursively fixing the counts, if a node with a limit is encountered | |
82 | * during the descent, the counts are known to be valid and there is no need to | |
83 | * descend into that filesystem's children. The counts on filesystems above the | |
84 | * one with the new limit will still be uninitialized, unless a limit is | |
85 | * eventually set on one of those filesystems. The counts are always recursively | |
86 | * updated when a limit is set on a dataset, unless there is already a limit. | |
87 | * When a new limit value is set on a filesystem with an existing limit, it is | |
88 | * possible for the new limit to be less than the current count at that level | |
89 | * since a user who can change the limit is also allowed to exceed the limit. | |
90 | * | |
91 | * Once the feature is active, then whenever a filesystem or snapshot is | |
92 | * created, the code recurses up the tree, validating the new count against the | |
93 | * limit at each initialized level. In practice, most levels will not have a | |
94 | * limit set. If there is a limit at any initialized level up the tree, the | |
95 | * check must pass or the creation will fail. Likewise, when a filesystem or | |
96 | * snapshot is destroyed, the counts are recursively adjusted all the way up | |
97 | * the initizized nodes in the tree. Renaming a filesystem into different point | |
98 | * in the tree will first validate, then update the counts on each branch up to | |
99 | * the common ancestor. A receive will also validate the counts and then update | |
100 | * them. | |
101 | * | |
102 | * An exception to the above behavior is that the limit is not enforced if the | |
103 | * user has permission to modify the limit. This is primarily so that | |
104 | * recursive snapshots in the global zone always work. We want to prevent a | |
105 | * denial-of-service in which a lower level delegated dataset could max out its | |
106 | * limit and thus block recursive snapshots from being taken in the global zone. | |
107 | * Because of this, it is possible for the snapshot count to be over the limit | |
108 | * and snapshots taken in the global zone could cause a lower level dataset to | |
109 | * hit or exceed its limit. The administrator taking the global zone recursive | |
110 | * snapshot should be aware of this side-effect and behave accordingly. | |
111 | * For consistency, the filesystem limit is also not enforced if the user can | |
112 | * modify the limit. | |
113 | * | |
114 | * The filesystem and snapshot limits are validated by dsl_fs_ss_limit_check() | |
115 | * and updated by dsl_fs_ss_count_adjust(). A new limit value is setup in | |
116 | * dsl_dir_activate_fs_ss_limit() and the counts are adjusted, if necessary, by | |
117 | * dsl_dir_init_fs_ss_count(). | |
118 | * | |
119 | * There is a special case when we receive a filesystem that already exists. In | |
120 | * this case a temporary clone name of %X is created (see dmu_recv_begin). We | |
121 | * never update the filesystem counts for temporary clones. | |
122 | * | |
123 | * Likewise, we do not update the snapshot counts for temporary snapshots, | |
124 | * such as those created by zfs diff. | |
125 | */ | |
126 | ||
127 | extern inline dsl_dir_phys_t *dsl_dir_phys(dsl_dir_t *dd); | |
34dc7c2f BB |
128 | |
129 | static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd); | |
34dc7c2f | 130 | |
34dc7c2f | 131 | static void |
cae5b340 | 132 | dsl_dir_evict_async(void *dbu) |
34dc7c2f | 133 | { |
e10b0808 | 134 | dsl_dir_t *dd = dbu; |
34dc7c2f | 135 | int t; |
a08ee875 | 136 | ASSERTV(dsl_pool_t *dp = dd->dd_pool); |
34dc7c2f | 137 | |
e10b0808 AX |
138 | dd->dd_dbuf = NULL; |
139 | ||
34dc7c2f BB |
140 | for (t = 0; t < TXG_SIZE; t++) { |
141 | ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t)); | |
142 | ASSERT(dd->dd_tempreserved[t] == 0); | |
143 | ASSERT(dd->dd_space_towrite[t] == 0); | |
144 | } | |
145 | ||
34dc7c2f | 146 | if (dd->dd_parent) |
e10b0808 | 147 | dsl_dir_async_rele(dd->dd_parent, dd); |
34dc7c2f | 148 | |
e10b0808 | 149 | spa_async_close(dd->dd_pool->dp_spa, dd); |
34dc7c2f | 150 | |
cae5b340 | 151 | dsl_prop_fini(dd); |
34dc7c2f BB |
152 | mutex_destroy(&dd->dd_lock); |
153 | kmem_free(dd, sizeof (dsl_dir_t)); | |
154 | } | |
155 | ||
156 | int | |
a08ee875 | 157 | dsl_dir_hold_obj(dsl_pool_t *dp, uint64_t ddobj, |
34dc7c2f BB |
158 | const char *tail, void *tag, dsl_dir_t **ddp) |
159 | { | |
160 | dmu_buf_t *dbuf; | |
161 | dsl_dir_t *dd; | |
162 | int err; | |
163 | ||
a08ee875 | 164 | ASSERT(dsl_pool_config_held(dp)); |
34dc7c2f BB |
165 | |
166 | err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf); | |
a08ee875 | 167 | if (err != 0) |
34dc7c2f BB |
168 | return (err); |
169 | dd = dmu_buf_get_user(dbuf); | |
170 | #ifdef ZFS_DEBUG | |
171 | { | |
172 | dmu_object_info_t doi; | |
173 | dmu_object_info_from_db(dbuf, &doi); | |
ea04106b | 174 | ASSERT3U(doi.doi_bonus_type, ==, DMU_OT_DSL_DIR); |
b128c09f | 175 | ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t)); |
34dc7c2f BB |
176 | } |
177 | #endif | |
34dc7c2f BB |
178 | if (dd == NULL) { |
179 | dsl_dir_t *winner; | |
34dc7c2f | 180 | |
ea04106b | 181 | dd = kmem_zalloc(sizeof (dsl_dir_t), KM_SLEEP); |
34dc7c2f BB |
182 | dd->dd_object = ddobj; |
183 | dd->dd_dbuf = dbuf; | |
184 | dd->dd_pool = dp; | |
34dc7c2f | 185 | mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); |
cae5b340 | 186 | dsl_prop_init(dd); |
34dc7c2f | 187 | |
428870ff BB |
188 | dsl_dir_snap_cmtime_update(dd); |
189 | ||
e10b0808 AX |
190 | if (dsl_dir_phys(dd)->dd_parent_obj) { |
191 | err = dsl_dir_hold_obj(dp, | |
192 | dsl_dir_phys(dd)->dd_parent_obj, NULL, dd, | |
193 | &dd->dd_parent); | |
a08ee875 | 194 | if (err != 0) |
b128c09f | 195 | goto errout; |
34dc7c2f BB |
196 | if (tail) { |
197 | #ifdef ZFS_DEBUG | |
198 | uint64_t foundobj; | |
199 | ||
200 | err = zap_lookup(dp->dp_meta_objset, | |
e10b0808 AX |
201 | dsl_dir_phys(dd->dd_parent)-> |
202 | dd_child_dir_zapobj, tail, | |
203 | sizeof (foundobj), 1, &foundobj); | |
34dc7c2f BB |
204 | ASSERT(err || foundobj == ddobj); |
205 | #endif | |
cae5b340 AX |
206 | (void) strlcpy(dd->dd_myname, tail, |
207 | sizeof (dd->dd_myname)); | |
34dc7c2f BB |
208 | } else { |
209 | err = zap_value_search(dp->dp_meta_objset, | |
e10b0808 AX |
210 | dsl_dir_phys(dd->dd_parent)-> |
211 | dd_child_dir_zapobj, | |
34dc7c2f BB |
212 | ddobj, 0, dd->dd_myname); |
213 | } | |
a08ee875 | 214 | if (err != 0) |
b128c09f | 215 | goto errout; |
34dc7c2f BB |
216 | } else { |
217 | (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa)); | |
218 | } | |
219 | ||
428870ff BB |
220 | if (dsl_dir_is_clone(dd)) { |
221 | dmu_buf_t *origin_bonus; | |
222 | dsl_dataset_phys_t *origin_phys; | |
223 | ||
224 | /* | |
225 | * We can't open the origin dataset, because | |
226 | * that would require opening this dsl_dir. | |
227 | * Just look at its phys directly instead. | |
228 | */ | |
229 | err = dmu_bonus_hold(dp->dp_meta_objset, | |
e10b0808 AX |
230 | dsl_dir_phys(dd)->dd_origin_obj, FTAG, |
231 | &origin_bonus); | |
a08ee875 | 232 | if (err != 0) |
428870ff BB |
233 | goto errout; |
234 | origin_phys = origin_bonus->db_data; | |
235 | dd->dd_origin_txg = | |
236 | origin_phys->ds_creation_txg; | |
237 | dmu_buf_rele(origin_bonus, FTAG); | |
238 | } | |
239 | ||
cae5b340 AX |
240 | dmu_buf_init_user(&dd->dd_dbu, NULL, dsl_dir_evict_async, |
241 | &dd->dd_dbuf); | |
e10b0808 AX |
242 | winner = dmu_buf_set_user_ie(dbuf, &dd->dd_dbu); |
243 | if (winner != NULL) { | |
34dc7c2f | 244 | if (dd->dd_parent) |
a08ee875 | 245 | dsl_dir_rele(dd->dd_parent, dd); |
cae5b340 | 246 | dsl_prop_fini(dd); |
34dc7c2f BB |
247 | mutex_destroy(&dd->dd_lock); |
248 | kmem_free(dd, sizeof (dsl_dir_t)); | |
249 | dd = winner; | |
250 | } else { | |
251 | spa_open_ref(dp->dp_spa, dd); | |
252 | } | |
253 | } | |
254 | ||
255 | /* | |
256 | * The dsl_dir_t has both open-to-close and instantiate-to-evict | |
257 | * holds on the spa. We need the open-to-close holds because | |
258 | * otherwise the spa_refcnt wouldn't change when we open a | |
259 | * dir which the spa also has open, so we could incorrectly | |
260 | * think it was OK to unload/export/destroy the pool. We need | |
261 | * the instantiate-to-evict hold because the dsl_dir_t has a | |
262 | * pointer to the dd_pool, which has a pointer to the spa_t. | |
263 | */ | |
264 | spa_open_ref(dp->dp_spa, tag); | |
265 | ASSERT3P(dd->dd_pool, ==, dp); | |
266 | ASSERT3U(dd->dd_object, ==, ddobj); | |
267 | ASSERT3P(dd->dd_dbuf, ==, dbuf); | |
268 | *ddp = dd; | |
269 | return (0); | |
b128c09f BB |
270 | |
271 | errout: | |
272 | if (dd->dd_parent) | |
a08ee875 | 273 | dsl_dir_rele(dd->dd_parent, dd); |
cae5b340 | 274 | dsl_prop_fini(dd); |
b128c09f BB |
275 | mutex_destroy(&dd->dd_lock); |
276 | kmem_free(dd, sizeof (dsl_dir_t)); | |
277 | dmu_buf_rele(dbuf, tag); | |
278 | return (err); | |
34dc7c2f BB |
279 | } |
280 | ||
281 | void | |
a08ee875 | 282 | dsl_dir_rele(dsl_dir_t *dd, void *tag) |
34dc7c2f BB |
283 | { |
284 | dprintf_dd(dd, "%s\n", ""); | |
285 | spa_close(dd->dd_pool->dp_spa, tag); | |
286 | dmu_buf_rele(dd->dd_dbuf, tag); | |
287 | } | |
288 | ||
e10b0808 AX |
289 | /* |
290 | * Remove a reference to the given dsl dir that is being asynchronously | |
291 | * released. Async releases occur from a taskq performing eviction of | |
292 | * dsl datasets and dirs. This process is identical to a normal release | |
293 | * with the exception of using the async API for releasing the reference on | |
294 | * the spa. | |
295 | */ | |
296 | void | |
297 | dsl_dir_async_rele(dsl_dir_t *dd, void *tag) | |
298 | { | |
299 | dprintf_dd(dd, "%s\n", ""); | |
300 | spa_async_close(dd->dd_pool->dp_spa, tag); | |
301 | dmu_buf_rele(dd->dd_dbuf, tag); | |
302 | } | |
303 | ||
cae5b340 | 304 | /* buf must be at least ZFS_MAX_DATASET_NAME_LEN bytes */ |
34dc7c2f BB |
305 | void |
306 | dsl_dir_name(dsl_dir_t *dd, char *buf) | |
307 | { | |
308 | if (dd->dd_parent) { | |
309 | dsl_dir_name(dd->dd_parent, buf); | |
cae5b340 AX |
310 | VERIFY3U(strlcat(buf, "/", ZFS_MAX_DATASET_NAME_LEN), <, |
311 | ZFS_MAX_DATASET_NAME_LEN); | |
34dc7c2f BB |
312 | } else { |
313 | buf[0] = '\0'; | |
314 | } | |
315 | if (!MUTEX_HELD(&dd->dd_lock)) { | |
316 | /* | |
317 | * recursive mutex so that we can use | |
318 | * dprintf_dd() with dd_lock held | |
319 | */ | |
320 | mutex_enter(&dd->dd_lock); | |
cae5b340 AX |
321 | VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN), |
322 | <, ZFS_MAX_DATASET_NAME_LEN); | |
34dc7c2f BB |
323 | mutex_exit(&dd->dd_lock); |
324 | } else { | |
cae5b340 AX |
325 | VERIFY3U(strlcat(buf, dd->dd_myname, ZFS_MAX_DATASET_NAME_LEN), |
326 | <, ZFS_MAX_DATASET_NAME_LEN); | |
34dc7c2f BB |
327 | } |
328 | } | |
329 | ||
29809a6c | 330 | /* Calculate name length, avoiding all the strcat calls of dsl_dir_name */ |
34dc7c2f BB |
331 | int |
332 | dsl_dir_namelen(dsl_dir_t *dd) | |
333 | { | |
334 | int result = 0; | |
335 | ||
336 | if (dd->dd_parent) { | |
337 | /* parent's name + 1 for the "/" */ | |
338 | result = dsl_dir_namelen(dd->dd_parent) + 1; | |
339 | } | |
340 | ||
341 | if (!MUTEX_HELD(&dd->dd_lock)) { | |
342 | /* see dsl_dir_name */ | |
343 | mutex_enter(&dd->dd_lock); | |
344 | result += strlen(dd->dd_myname); | |
345 | mutex_exit(&dd->dd_lock); | |
346 | } else { | |
347 | result += strlen(dd->dd_myname); | |
348 | } | |
349 | ||
350 | return (result); | |
351 | } | |
352 | ||
34dc7c2f BB |
353 | static int |
354 | getcomponent(const char *path, char *component, const char **nextp) | |
355 | { | |
356 | char *p; | |
a08ee875 | 357 | |
9babb374 | 358 | if ((path == NULL) || (path[0] == '\0')) |
a08ee875 | 359 | return (SET_ERROR(ENOENT)); |
34dc7c2f BB |
360 | /* This would be a good place to reserve some namespace... */ |
361 | p = strpbrk(path, "/@"); | |
362 | if (p && (p[1] == '/' || p[1] == '@')) { | |
363 | /* two separators in a row */ | |
a08ee875 | 364 | return (SET_ERROR(EINVAL)); |
34dc7c2f BB |
365 | } |
366 | if (p == NULL || p == path) { | |
367 | /* | |
368 | * if the first thing is an @ or /, it had better be an | |
369 | * @ and it had better not have any more ats or slashes, | |
370 | * and it had better have something after the @. | |
371 | */ | |
372 | if (p != NULL && | |
373 | (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) | |
a08ee875 | 374 | return (SET_ERROR(EINVAL)); |
cae5b340 | 375 | if (strlen(path) >= ZFS_MAX_DATASET_NAME_LEN) |
a08ee875 | 376 | return (SET_ERROR(ENAMETOOLONG)); |
34dc7c2f BB |
377 | (void) strcpy(component, path); |
378 | p = NULL; | |
379 | } else if (p[0] == '/') { | |
cae5b340 | 380 | if (p - path >= ZFS_MAX_DATASET_NAME_LEN) |
a08ee875 | 381 | return (SET_ERROR(ENAMETOOLONG)); |
34dc7c2f | 382 | (void) strncpy(component, path, p - path); |
a08ee875 | 383 | component[p - path] = '\0'; |
34dc7c2f BB |
384 | p++; |
385 | } else if (p[0] == '@') { | |
386 | /* | |
387 | * if the next separator is an @, there better not be | |
388 | * any more slashes. | |
389 | */ | |
390 | if (strchr(path, '/')) | |
a08ee875 | 391 | return (SET_ERROR(EINVAL)); |
cae5b340 | 392 | if (p - path >= ZFS_MAX_DATASET_NAME_LEN) |
a08ee875 | 393 | return (SET_ERROR(ENAMETOOLONG)); |
34dc7c2f | 394 | (void) strncpy(component, path, p - path); |
a08ee875 | 395 | component[p - path] = '\0'; |
34dc7c2f | 396 | } else { |
a08ee875 | 397 | panic("invalid p=%p", (void *)p); |
34dc7c2f BB |
398 | } |
399 | *nextp = p; | |
400 | return (0); | |
401 | } | |
402 | ||
403 | /* | |
a08ee875 LG |
404 | * Return the dsl_dir_t, and possibly the last component which couldn't |
405 | * be found in *tail. The name must be in the specified dsl_pool_t. This | |
406 | * thread must hold the dp_config_rwlock for the pool. Returns NULL if the | |
407 | * path is bogus, or if tail==NULL and we couldn't parse the whole name. | |
408 | * (*tail)[0] == '@' means that the last component is a snapshot. | |
34dc7c2f BB |
409 | */ |
410 | int | |
a08ee875 | 411 | dsl_dir_hold(dsl_pool_t *dp, const char *name, void *tag, |
34dc7c2f BB |
412 | dsl_dir_t **ddp, const char **tailp) |
413 | { | |
fcf37ec6 | 414 | char *buf; |
a08ee875 | 415 | const char *spaname, *next, *nextnext = NULL; |
34dc7c2f BB |
416 | int err; |
417 | dsl_dir_t *dd; | |
34dc7c2f | 418 | uint64_t ddobj; |
34dc7c2f | 419 | |
cae5b340 | 420 | buf = kmem_alloc(ZFS_MAX_DATASET_NAME_LEN, KM_SLEEP); |
34dc7c2f | 421 | err = getcomponent(name, buf, &next); |
a08ee875 | 422 | if (err != 0) |
fcf37ec6 | 423 | goto error; |
34dc7c2f | 424 | |
a08ee875 LG |
425 | /* Make sure the name is in the specified pool. */ |
426 | spaname = spa_name(dp->dp_spa); | |
427 | if (strcmp(buf, spaname) != 0) { | |
ea04106b | 428 | err = SET_ERROR(EXDEV); |
a08ee875 | 429 | goto error; |
34dc7c2f BB |
430 | } |
431 | ||
a08ee875 | 432 | ASSERT(dsl_pool_config_held(dp)); |
34dc7c2f | 433 | |
a08ee875 LG |
434 | err = dsl_dir_hold_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); |
435 | if (err != 0) { | |
fcf37ec6 | 436 | goto error; |
34dc7c2f BB |
437 | } |
438 | ||
439 | while (next != NULL) { | |
e10b0808 | 440 | dsl_dir_t *child_dd; |
34dc7c2f | 441 | err = getcomponent(next, buf, &nextnext); |
a08ee875 | 442 | if (err != 0) |
34dc7c2f BB |
443 | break; |
444 | ASSERT(next[0] != '\0'); | |
445 | if (next[0] == '@') | |
446 | break; | |
447 | dprintf("looking up %s in obj%lld\n", | |
e10b0808 | 448 | buf, dsl_dir_phys(dd)->dd_child_dir_zapobj); |
34dc7c2f BB |
449 | |
450 | err = zap_lookup(dp->dp_meta_objset, | |
e10b0808 | 451 | dsl_dir_phys(dd)->dd_child_dir_zapobj, |
34dc7c2f | 452 | buf, sizeof (ddobj), 1, &ddobj); |
a08ee875 | 453 | if (err != 0) { |
34dc7c2f BB |
454 | if (err == ENOENT) |
455 | err = 0; | |
456 | break; | |
457 | } | |
458 | ||
e10b0808 | 459 | err = dsl_dir_hold_obj(dp, ddobj, buf, tag, &child_dd); |
a08ee875 | 460 | if (err != 0) |
34dc7c2f | 461 | break; |
a08ee875 | 462 | dsl_dir_rele(dd, tag); |
e10b0808 | 463 | dd = child_dd; |
34dc7c2f BB |
464 | next = nextnext; |
465 | } | |
34dc7c2f | 466 | |
a08ee875 LG |
467 | if (err != 0) { |
468 | dsl_dir_rele(dd, tag); | |
fcf37ec6 | 469 | goto error; |
34dc7c2f BB |
470 | } |
471 | ||
472 | /* | |
473 | * It's an error if there's more than one component left, or | |
474 | * tailp==NULL and there's any component left. | |
475 | */ | |
476 | if (next != NULL && | |
477 | (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { | |
478 | /* bad path name */ | |
a08ee875 | 479 | dsl_dir_rele(dd, tag); |
34dc7c2f | 480 | dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); |
a08ee875 | 481 | err = SET_ERROR(ENOENT); |
34dc7c2f | 482 | } |
a08ee875 | 483 | if (tailp != NULL) |
34dc7c2f | 484 | *tailp = next; |
34dc7c2f | 485 | *ddp = dd; |
fcf37ec6 | 486 | error: |
cae5b340 | 487 | kmem_free(buf, ZFS_MAX_DATASET_NAME_LEN); |
34dc7c2f BB |
488 | return (err); |
489 | } | |
490 | ||
e10b0808 AX |
491 | /* |
492 | * If the counts are already initialized for this filesystem and its | |
493 | * descendants then do nothing, otherwise initialize the counts. | |
494 | * | |
495 | * The counts on this filesystem, and those below, may be uninitialized due to | |
496 | * either the use of a pre-existing pool which did not support the | |
497 | * filesystem/snapshot limit feature, or one in which the feature had not yet | |
498 | * been enabled. | |
499 | * | |
500 | * Recursively descend the filesystem tree and update the filesystem/snapshot | |
501 | * counts on each filesystem below, then update the cumulative count on the | |
502 | * current filesystem. If the filesystem already has a count set on it, | |
503 | * then we know that its counts, and the counts on the filesystems below it, | |
504 | * are already correct, so we don't have to update this filesystem. | |
505 | */ | |
506 | static void | |
507 | dsl_dir_init_fs_ss_count(dsl_dir_t *dd, dmu_tx_t *tx) | |
508 | { | |
509 | uint64_t my_fs_cnt = 0; | |
510 | uint64_t my_ss_cnt = 0; | |
511 | dsl_pool_t *dp = dd->dd_pool; | |
512 | objset_t *os = dp->dp_meta_objset; | |
513 | zap_cursor_t *zc; | |
514 | zap_attribute_t *za; | |
515 | dsl_dataset_t *ds; | |
516 | ||
517 | ASSERT(spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)); | |
518 | ASSERT(dsl_pool_config_held(dp)); | |
519 | ASSERT(dmu_tx_is_syncing(tx)); | |
520 | ||
521 | dsl_dir_zapify(dd, tx); | |
522 | ||
523 | /* | |
524 | * If the filesystem count has already been initialized then we | |
525 | * don't need to recurse down any further. | |
526 | */ | |
527 | if (zap_contains(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT) == 0) | |
528 | return; | |
529 | ||
530 | zc = kmem_alloc(sizeof (zap_cursor_t), KM_SLEEP); | |
531 | za = kmem_alloc(sizeof (zap_attribute_t), KM_SLEEP); | |
532 | ||
533 | /* Iterate my child dirs */ | |
534 | for (zap_cursor_init(zc, os, dsl_dir_phys(dd)->dd_child_dir_zapobj); | |
535 | zap_cursor_retrieve(zc, za) == 0; zap_cursor_advance(zc)) { | |
536 | dsl_dir_t *chld_dd; | |
537 | uint64_t count; | |
538 | ||
539 | VERIFY0(dsl_dir_hold_obj(dp, za->za_first_integer, NULL, FTAG, | |
540 | &chld_dd)); | |
541 | ||
542 | /* | |
543 | * Ignore hidden ($FREE, $MOS & $ORIGIN) objsets and | |
544 | * temporary datasets. | |
545 | */ | |
546 | if (chld_dd->dd_myname[0] == '$' || | |
547 | chld_dd->dd_myname[0] == '%') { | |
548 | dsl_dir_rele(chld_dd, FTAG); | |
549 | continue; | |
550 | } | |
551 | ||
552 | my_fs_cnt++; /* count this child */ | |
553 | ||
554 | dsl_dir_init_fs_ss_count(chld_dd, tx); | |
555 | ||
556 | VERIFY0(zap_lookup(os, chld_dd->dd_object, | |
557 | DD_FIELD_FILESYSTEM_COUNT, sizeof (count), 1, &count)); | |
558 | my_fs_cnt += count; | |
559 | VERIFY0(zap_lookup(os, chld_dd->dd_object, | |
560 | DD_FIELD_SNAPSHOT_COUNT, sizeof (count), 1, &count)); | |
561 | my_ss_cnt += count; | |
562 | ||
563 | dsl_dir_rele(chld_dd, FTAG); | |
564 | } | |
565 | zap_cursor_fini(zc); | |
566 | /* Count my snapshots (we counted children's snapshots above) */ | |
567 | VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, | |
568 | dsl_dir_phys(dd)->dd_head_dataset_obj, FTAG, &ds)); | |
569 | ||
570 | for (zap_cursor_init(zc, os, dsl_dataset_phys(ds)->ds_snapnames_zapobj); | |
571 | zap_cursor_retrieve(zc, za) == 0; | |
572 | zap_cursor_advance(zc)) { | |
573 | /* Don't count temporary snapshots */ | |
574 | if (za->za_name[0] != '%') | |
575 | my_ss_cnt++; | |
576 | } | |
577 | zap_cursor_fini(zc); | |
578 | ||
579 | dsl_dataset_rele(ds, FTAG); | |
580 | ||
581 | kmem_free(zc, sizeof (zap_cursor_t)); | |
582 | kmem_free(za, sizeof (zap_attribute_t)); | |
583 | ||
584 | /* we're in a sync task, update counts */ | |
585 | dmu_buf_will_dirty(dd->dd_dbuf, tx); | |
586 | VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, | |
587 | sizeof (my_fs_cnt), 1, &my_fs_cnt, tx)); | |
588 | VERIFY0(zap_add(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, | |
589 | sizeof (my_ss_cnt), 1, &my_ss_cnt, tx)); | |
590 | } | |
591 | ||
592 | static int | |
593 | dsl_dir_actv_fs_ss_limit_check(void *arg, dmu_tx_t *tx) | |
594 | { | |
595 | char *ddname = (char *)arg; | |
596 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
597 | dsl_dataset_t *ds; | |
598 | dsl_dir_t *dd; | |
599 | int error; | |
600 | ||
601 | error = dsl_dataset_hold(dp, ddname, FTAG, &ds); | |
602 | if (error != 0) | |
603 | return (error); | |
604 | ||
605 | if (!spa_feature_is_enabled(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT)) { | |
606 | dsl_dataset_rele(ds, FTAG); | |
607 | return (SET_ERROR(ENOTSUP)); | |
608 | } | |
609 | ||
610 | dd = ds->ds_dir; | |
611 | if (spa_feature_is_active(dp->dp_spa, SPA_FEATURE_FS_SS_LIMIT) && | |
612 | dsl_dir_is_zapified(dd) && | |
613 | zap_contains(dp->dp_meta_objset, dd->dd_object, | |
614 | DD_FIELD_FILESYSTEM_COUNT) == 0) { | |
615 | dsl_dataset_rele(ds, FTAG); | |
616 | return (SET_ERROR(EALREADY)); | |
617 | } | |
618 | ||
619 | dsl_dataset_rele(ds, FTAG); | |
620 | return (0); | |
621 | } | |
622 | ||
623 | static void | |
624 | dsl_dir_actv_fs_ss_limit_sync(void *arg, dmu_tx_t *tx) | |
625 | { | |
626 | char *ddname = (char *)arg; | |
627 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
628 | dsl_dataset_t *ds; | |
629 | spa_t *spa; | |
630 | ||
631 | VERIFY0(dsl_dataset_hold(dp, ddname, FTAG, &ds)); | |
632 | ||
633 | spa = dsl_dataset_get_spa(ds); | |
634 | ||
635 | if (!spa_feature_is_active(spa, SPA_FEATURE_FS_SS_LIMIT)) { | |
636 | /* | |
637 | * Since the feature was not active and we're now setting a | |
638 | * limit, increment the feature-active counter so that the | |
639 | * feature becomes active for the first time. | |
640 | * | |
641 | * We are already in a sync task so we can update the MOS. | |
642 | */ | |
643 | spa_feature_incr(spa, SPA_FEATURE_FS_SS_LIMIT, tx); | |
644 | } | |
645 | ||
646 | /* | |
647 | * Since we are now setting a non-UINT64_MAX limit on the filesystem, | |
648 | * we need to ensure the counts are correct. Descend down the tree from | |
649 | * this point and update all of the counts to be accurate. | |
650 | */ | |
651 | dsl_dir_init_fs_ss_count(ds->ds_dir, tx); | |
652 | ||
653 | dsl_dataset_rele(ds, FTAG); | |
654 | } | |
655 | ||
656 | /* | |
657 | * Make sure the feature is enabled and activate it if necessary. | |
658 | * Since we're setting a limit, ensure the on-disk counts are valid. | |
659 | * This is only called by the ioctl path when setting a limit value. | |
660 | * | |
661 | * We do not need to validate the new limit, since users who can change the | |
662 | * limit are also allowed to exceed the limit. | |
663 | */ | |
664 | int | |
665 | dsl_dir_activate_fs_ss_limit(const char *ddname) | |
666 | { | |
667 | int error; | |
668 | ||
669 | error = dsl_sync_task(ddname, dsl_dir_actv_fs_ss_limit_check, | |
670 | dsl_dir_actv_fs_ss_limit_sync, (void *)ddname, 0, | |
671 | ZFS_SPACE_CHECK_RESERVED); | |
672 | ||
673 | if (error == EALREADY) | |
674 | error = 0; | |
675 | ||
676 | return (error); | |
677 | } | |
678 | ||
679 | /* | |
680 | * Used to determine if the filesystem_limit or snapshot_limit should be | |
681 | * enforced. We allow the limit to be exceeded if the user has permission to | |
682 | * write the property value. We pass in the creds that we got in the open | |
683 | * context since we will always be the GZ root in syncing context. We also have | |
684 | * to handle the case where we are allowed to change the limit on the current | |
685 | * dataset, but there may be another limit in the tree above. | |
686 | * | |
687 | * We can never modify these two properties within a non-global zone. In | |
688 | * addition, the other checks are modeled on zfs_secpolicy_write_perms. We | |
689 | * can't use that function since we are already holding the dp_config_rwlock. | |
690 | * In addition, we already have the dd and dealing with snapshots is simplified | |
691 | * in this code. | |
692 | */ | |
693 | ||
694 | typedef enum { | |
695 | ENFORCE_ALWAYS, | |
696 | ENFORCE_NEVER, | |
697 | ENFORCE_ABOVE | |
698 | } enforce_res_t; | |
699 | ||
700 | static enforce_res_t | |
701 | dsl_enforce_ds_ss_limits(dsl_dir_t *dd, zfs_prop_t prop, cred_t *cr) | |
702 | { | |
703 | enforce_res_t enforce = ENFORCE_ALWAYS; | |
704 | uint64_t obj; | |
705 | dsl_dataset_t *ds; | |
706 | uint64_t zoned; | |
707 | ||
708 | ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || | |
709 | prop == ZFS_PROP_SNAPSHOT_LIMIT); | |
710 | ||
711 | #ifdef _KERNEL | |
712 | if (crgetzoneid(cr) != GLOBAL_ZONEID) | |
713 | return (ENFORCE_ALWAYS); | |
714 | ||
715 | if (secpolicy_zfs(cr) == 0) | |
716 | return (ENFORCE_NEVER); | |
717 | #endif | |
718 | ||
719 | if ((obj = dsl_dir_phys(dd)->dd_head_dataset_obj) == 0) | |
720 | return (ENFORCE_ALWAYS); | |
721 | ||
722 | ASSERT(dsl_pool_config_held(dd->dd_pool)); | |
723 | ||
724 | if (dsl_dataset_hold_obj(dd->dd_pool, obj, FTAG, &ds) != 0) | |
725 | return (ENFORCE_ALWAYS); | |
726 | ||
727 | if (dsl_prop_get_ds(ds, "zoned", 8, 1, &zoned, NULL) || zoned) { | |
728 | /* Only root can access zoned fs's from the GZ */ | |
729 | enforce = ENFORCE_ALWAYS; | |
730 | } else { | |
731 | if (dsl_deleg_access_impl(ds, zfs_prop_to_name(prop), cr) == 0) | |
732 | enforce = ENFORCE_ABOVE; | |
733 | } | |
734 | ||
735 | dsl_dataset_rele(ds, FTAG); | |
736 | return (enforce); | |
737 | } | |
738 | ||
739 | /* | |
740 | * Check if adding additional child filesystem(s) would exceed any filesystem | |
741 | * limits or adding additional snapshot(s) would exceed any snapshot limits. | |
742 | * The prop argument indicates which limit to check. | |
743 | * | |
744 | * Note that all filesystem limits up to the root (or the highest | |
745 | * initialized) filesystem or the given ancestor must be satisfied. | |
746 | */ | |
747 | int | |
748 | dsl_fs_ss_limit_check(dsl_dir_t *dd, uint64_t delta, zfs_prop_t prop, | |
749 | dsl_dir_t *ancestor, cred_t *cr) | |
750 | { | |
751 | objset_t *os = dd->dd_pool->dp_meta_objset; | |
752 | uint64_t limit, count; | |
753 | char *count_prop; | |
754 | enforce_res_t enforce; | |
755 | int err = 0; | |
756 | ||
757 | ASSERT(dsl_pool_config_held(dd->dd_pool)); | |
758 | ASSERT(prop == ZFS_PROP_FILESYSTEM_LIMIT || | |
759 | prop == ZFS_PROP_SNAPSHOT_LIMIT); | |
760 | ||
761 | /* | |
762 | * If we're allowed to change the limit, don't enforce the limit | |
763 | * e.g. this can happen if a snapshot is taken by an administrative | |
764 | * user in the global zone (i.e. a recursive snapshot by root). | |
765 | * However, we must handle the case of delegated permissions where we | |
766 | * are allowed to change the limit on the current dataset, but there | |
767 | * is another limit in the tree above. | |
768 | */ | |
769 | enforce = dsl_enforce_ds_ss_limits(dd, prop, cr); | |
770 | if (enforce == ENFORCE_NEVER) | |
771 | return (0); | |
772 | ||
773 | /* | |
774 | * e.g. if renaming a dataset with no snapshots, count adjustment | |
775 | * is 0. | |
776 | */ | |
777 | if (delta == 0) | |
778 | return (0); | |
779 | ||
780 | if (prop == ZFS_PROP_SNAPSHOT_LIMIT) { | |
781 | /* | |
782 | * We don't enforce the limit for temporary snapshots. This is | |
783 | * indicated by a NULL cred_t argument. | |
784 | */ | |
785 | if (cr == NULL) | |
786 | return (0); | |
787 | ||
788 | count_prop = DD_FIELD_SNAPSHOT_COUNT; | |
789 | } else { | |
790 | count_prop = DD_FIELD_FILESYSTEM_COUNT; | |
791 | } | |
792 | ||
793 | /* | |
794 | * If an ancestor has been provided, stop checking the limit once we | |
795 | * hit that dir. We need this during rename so that we don't overcount | |
796 | * the check once we recurse up to the common ancestor. | |
797 | */ | |
798 | if (ancestor == dd) | |
799 | return (0); | |
800 | ||
801 | /* | |
802 | * If we hit an uninitialized node while recursing up the tree, we can | |
803 | * stop since we know there is no limit here (or above). The counts are | |
804 | * not valid on this node and we know we won't touch this node's counts. | |
805 | */ | |
806 | if (!dsl_dir_is_zapified(dd) || zap_lookup(os, dd->dd_object, | |
807 | count_prop, sizeof (count), 1, &count) == ENOENT) | |
808 | return (0); | |
809 | ||
810 | err = dsl_prop_get_dd(dd, zfs_prop_to_name(prop), 8, 1, &limit, NULL, | |
811 | B_FALSE); | |
812 | if (err != 0) | |
813 | return (err); | |
814 | ||
815 | /* Is there a limit which we've hit? */ | |
816 | if (enforce == ENFORCE_ALWAYS && (count + delta) > limit) | |
817 | return (SET_ERROR(EDQUOT)); | |
818 | ||
819 | if (dd->dd_parent != NULL) | |
820 | err = dsl_fs_ss_limit_check(dd->dd_parent, delta, prop, | |
821 | ancestor, cr); | |
822 | ||
823 | return (err); | |
824 | } | |
825 | ||
826 | /* | |
827 | * Adjust the filesystem or snapshot count for the specified dsl_dir_t and all | |
828 | * parents. When a new filesystem/snapshot is created, increment the count on | |
829 | * all parents, and when a filesystem/snapshot is destroyed, decrement the | |
830 | * count. | |
831 | */ | |
832 | void | |
833 | dsl_fs_ss_count_adjust(dsl_dir_t *dd, int64_t delta, const char *prop, | |
834 | dmu_tx_t *tx) | |
835 | { | |
836 | int err; | |
837 | objset_t *os = dd->dd_pool->dp_meta_objset; | |
838 | uint64_t count; | |
839 | ||
840 | ASSERT(dsl_pool_config_held(dd->dd_pool)); | |
841 | ASSERT(dmu_tx_is_syncing(tx)); | |
842 | ASSERT(strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0 || | |
843 | strcmp(prop, DD_FIELD_SNAPSHOT_COUNT) == 0); | |
844 | ||
845 | /* | |
846 | * When we receive an incremental stream into a filesystem that already | |
847 | * exists, a temporary clone is created. We don't count this temporary | |
848 | * clone, whose name begins with a '%'. We also ignore hidden ($FREE, | |
849 | * $MOS & $ORIGIN) objsets. | |
850 | */ | |
851 | if ((dd->dd_myname[0] == '%' || dd->dd_myname[0] == '$') && | |
852 | strcmp(prop, DD_FIELD_FILESYSTEM_COUNT) == 0) | |
853 | return; | |
854 | ||
855 | /* | |
856 | * e.g. if renaming a dataset with no snapshots, count adjustment is 0 | |
857 | */ | |
858 | if (delta == 0) | |
859 | return; | |
860 | ||
861 | /* | |
862 | * If we hit an uninitialized node while recursing up the tree, we can | |
863 | * stop since we know the counts are not valid on this node and we | |
864 | * know we shouldn't touch this node's counts. An uninitialized count | |
865 | * on the node indicates that either the feature has not yet been | |
866 | * activated or there are no limits on this part of the tree. | |
867 | */ | |
868 | if (!dsl_dir_is_zapified(dd) || (err = zap_lookup(os, dd->dd_object, | |
869 | prop, sizeof (count), 1, &count)) == ENOENT) | |
870 | return; | |
871 | VERIFY0(err); | |
872 | ||
873 | count += delta; | |
874 | /* Use a signed verify to make sure we're not neg. */ | |
875 | VERIFY3S(count, >=, 0); | |
876 | ||
877 | VERIFY0(zap_update(os, dd->dd_object, prop, sizeof (count), 1, &count, | |
878 | tx)); | |
879 | ||
880 | /* Roll up this additional count into our ancestors */ | |
881 | if (dd->dd_parent != NULL) | |
882 | dsl_fs_ss_count_adjust(dd->dd_parent, delta, prop, tx); | |
883 | } | |
884 | ||
34dc7c2f | 885 | uint64_t |
b128c09f BB |
886 | dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, |
887 | dmu_tx_t *tx) | |
34dc7c2f | 888 | { |
b128c09f | 889 | objset_t *mos = dp->dp_meta_objset; |
34dc7c2f | 890 | uint64_t ddobj; |
428870ff | 891 | dsl_dir_phys_t *ddphys; |
34dc7c2f BB |
892 | dmu_buf_t *dbuf; |
893 | ||
894 | ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, | |
895 | DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); | |
b128c09f | 896 | if (pds) { |
e10b0808 | 897 | VERIFY(0 == zap_add(mos, dsl_dir_phys(pds)->dd_child_dir_zapobj, |
b128c09f BB |
898 | name, sizeof (uint64_t), 1, &ddobj, tx)); |
899 | } else { | |
900 | /* it's the root dir */ | |
901 | VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, | |
902 | DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); | |
903 | } | |
34dc7c2f BB |
904 | VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); |
905 | dmu_buf_will_dirty(dbuf, tx); | |
428870ff | 906 | ddphys = dbuf->db_data; |
34dc7c2f | 907 | |
428870ff | 908 | ddphys->dd_creation_time = gethrestime_sec(); |
e10b0808 | 909 | if (pds) { |
428870ff | 910 | ddphys->dd_parent_obj = pds->dd_object; |
e10b0808 AX |
911 | |
912 | /* update the filesystem counts */ | |
913 | dsl_fs_ss_count_adjust(pds, 1, DD_FIELD_FILESYSTEM_COUNT, tx); | |
914 | } | |
428870ff | 915 | ddphys->dd_props_zapobj = zap_create(mos, |
34dc7c2f | 916 | DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); |
428870ff | 917 | ddphys->dd_child_dir_zapobj = zap_create(mos, |
34dc7c2f | 918 | DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); |
b128c09f | 919 | if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) |
428870ff | 920 | ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; |
34dc7c2f BB |
921 | dmu_buf_rele(dbuf, FTAG); |
922 | ||
923 | return (ddobj); | |
924 | } | |
925 | ||
b128c09f BB |
926 | boolean_t |
927 | dsl_dir_is_clone(dsl_dir_t *dd) | |
34dc7c2f | 928 | { |
e10b0808 | 929 | return (dsl_dir_phys(dd)->dd_origin_obj && |
b128c09f | 930 | (dd->dd_pool->dp_origin_snap == NULL || |
e10b0808 | 931 | dsl_dir_phys(dd)->dd_origin_obj != |
b128c09f | 932 | dd->dd_pool->dp_origin_snap->ds_object)); |
34dc7c2f BB |
933 | } |
934 | ||
935 | void | |
936 | dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) | |
937 | { | |
938 | mutex_enter(&dd->dd_lock); | |
b128c09f | 939 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, |
e10b0808 AX |
940 | dsl_dir_phys(dd)->dd_used_bytes); |
941 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, | |
942 | dsl_dir_phys(dd)->dd_quota); | |
34dc7c2f | 943 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, |
e10b0808 | 944 | dsl_dir_phys(dd)->dd_reserved); |
34dc7c2f | 945 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, |
e10b0808 AX |
946 | dsl_dir_phys(dd)->dd_compressed_bytes == 0 ? 100 : |
947 | (dsl_dir_phys(dd)->dd_uncompressed_bytes * 100 / | |
948 | dsl_dir_phys(dd)->dd_compressed_bytes)); | |
a08ee875 | 949 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALUSED, |
e10b0808 AX |
950 | dsl_dir_phys(dd)->dd_uncompressed_bytes); |
951 | if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) { | |
b128c09f | 952 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, |
e10b0808 | 953 | dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_SNAP]); |
b128c09f | 954 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, |
e10b0808 | 955 | dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_HEAD]); |
b128c09f | 956 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, |
e10b0808 | 957 | dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_REFRSRV]); |
b128c09f | 958 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, |
e10b0808 AX |
959 | dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD] + |
960 | dsl_dir_phys(dd)->dd_used_breakdown[DD_USED_CHILD_RSRV]); | |
b128c09f | 961 | } |
34dc7c2f BB |
962 | mutex_exit(&dd->dd_lock); |
963 | ||
e10b0808 AX |
964 | if (dsl_dir_is_zapified(dd)) { |
965 | uint64_t count; | |
966 | objset_t *os = dd->dd_pool->dp_meta_objset; | |
967 | ||
968 | if (zap_lookup(os, dd->dd_object, DD_FIELD_FILESYSTEM_COUNT, | |
969 | sizeof (count), 1, &count) == 0) { | |
970 | dsl_prop_nvlist_add_uint64(nv, | |
971 | ZFS_PROP_FILESYSTEM_COUNT, count); | |
972 | } | |
973 | if (zap_lookup(os, dd->dd_object, DD_FIELD_SNAPSHOT_COUNT, | |
974 | sizeof (count), 1, &count) == 0) { | |
975 | dsl_prop_nvlist_add_uint64(nv, | |
976 | ZFS_PROP_SNAPSHOT_COUNT, count); | |
977 | } | |
978 | } | |
979 | ||
b128c09f | 980 | if (dsl_dir_is_clone(dd)) { |
34dc7c2f | 981 | dsl_dataset_t *ds; |
cae5b340 | 982 | char buf[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f | 983 | |
a08ee875 | 984 | VERIFY0(dsl_dataset_hold_obj(dd->dd_pool, |
e10b0808 | 985 | dsl_dir_phys(dd)->dd_origin_obj, FTAG, &ds)); |
34dc7c2f | 986 | dsl_dataset_name(ds, buf); |
b128c09f | 987 | dsl_dataset_rele(ds, FTAG); |
34dc7c2f BB |
988 | dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); |
989 | } | |
34dc7c2f BB |
990 | } |
991 | ||
992 | void | |
993 | dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) | |
994 | { | |
995 | dsl_pool_t *dp = dd->dd_pool; | |
996 | ||
e10b0808 | 997 | ASSERT(dsl_dir_phys(dd)); |
34dc7c2f | 998 | |
a08ee875 | 999 | if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg)) { |
34dc7c2f BB |
1000 | /* up the hold count until we can be written out */ |
1001 | dmu_buf_add_ref(dd->dd_dbuf, dd); | |
1002 | } | |
1003 | } | |
1004 | ||
1005 | static int64_t | |
1006 | parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) | |
1007 | { | |
e10b0808 AX |
1008 | uint64_t old_accounted = MAX(used, dsl_dir_phys(dd)->dd_reserved); |
1009 | uint64_t new_accounted = | |
1010 | MAX(used + delta, dsl_dir_phys(dd)->dd_reserved); | |
34dc7c2f BB |
1011 | return (new_accounted - old_accounted); |
1012 | } | |
1013 | ||
1014 | void | |
1015 | dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) | |
1016 | { | |
1017 | ASSERT(dmu_tx_is_syncing(tx)); | |
1018 | ||
34dc7c2f | 1019 | mutex_enter(&dd->dd_lock); |
c06d4368 | 1020 | ASSERT0(dd->dd_tempreserved[tx->tx_txg&TXG_MASK]); |
34dc7c2f BB |
1021 | dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg, |
1022 | dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024); | |
1023 | dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0; | |
34dc7c2f BB |
1024 | mutex_exit(&dd->dd_lock); |
1025 | ||
1026 | /* release the hold from dsl_dir_dirty */ | |
1027 | dmu_buf_rele(dd->dd_dbuf, dd); | |
1028 | } | |
1029 | ||
1030 | static uint64_t | |
1031 | dsl_dir_space_towrite(dsl_dir_t *dd) | |
1032 | { | |
1033 | uint64_t space = 0; | |
34dc7c2f BB |
1034 | |
1035 | ASSERT(MUTEX_HELD(&dd->dd_lock)); | |
1036 | ||
cae5b340 AX |
1037 | for (int i = 0; i < TXG_SIZE; i++) { |
1038 | space += dd->dd_space_towrite[i & TXG_MASK]; | |
1039 | ASSERT3U(dd->dd_space_towrite[i & TXG_MASK], >=, 0); | |
34dc7c2f BB |
1040 | } |
1041 | return (space); | |
1042 | } | |
1043 | ||
1044 | /* | |
1045 | * How much space would dd have available if ancestor had delta applied | |
1046 | * to it? If ondiskonly is set, we're only interested in what's | |
1047 | * on-disk, not estimated pending changes. | |
1048 | */ | |
1049 | uint64_t | |
1050 | dsl_dir_space_available(dsl_dir_t *dd, | |
1051 | dsl_dir_t *ancestor, int64_t delta, int ondiskonly) | |
1052 | { | |
1053 | uint64_t parentspace, myspace, quota, used; | |
1054 | ||
1055 | /* | |
1056 | * If there are no restrictions otherwise, assume we have | |
1057 | * unlimited space available. | |
1058 | */ | |
1059 | quota = UINT64_MAX; | |
1060 | parentspace = UINT64_MAX; | |
1061 | ||
1062 | if (dd->dd_parent != NULL) { | |
1063 | parentspace = dsl_dir_space_available(dd->dd_parent, | |
1064 | ancestor, delta, ondiskonly); | |
1065 | } | |
1066 | ||
1067 | mutex_enter(&dd->dd_lock); | |
e10b0808 AX |
1068 | if (dsl_dir_phys(dd)->dd_quota != 0) |
1069 | quota = dsl_dir_phys(dd)->dd_quota; | |
1070 | used = dsl_dir_phys(dd)->dd_used_bytes; | |
34dc7c2f BB |
1071 | if (!ondiskonly) |
1072 | used += dsl_dir_space_towrite(dd); | |
34dc7c2f BB |
1073 | |
1074 | if (dd->dd_parent == NULL) { | |
1075 | uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE); | |
1076 | quota = MIN(quota, poolsize); | |
1077 | } | |
1078 | ||
e10b0808 | 1079 | if (dsl_dir_phys(dd)->dd_reserved > used && parentspace != UINT64_MAX) { |
34dc7c2f BB |
1080 | /* |
1081 | * We have some space reserved, in addition to what our | |
1082 | * parent gave us. | |
1083 | */ | |
e10b0808 | 1084 | parentspace += dsl_dir_phys(dd)->dd_reserved - used; |
34dc7c2f BB |
1085 | } |
1086 | ||
b128c09f BB |
1087 | if (dd == ancestor) { |
1088 | ASSERT(delta <= 0); | |
1089 | ASSERT(used >= -delta); | |
1090 | used += delta; | |
1091 | if (parentspace != UINT64_MAX) | |
1092 | parentspace -= delta; | |
1093 | } | |
1094 | ||
34dc7c2f BB |
1095 | if (used > quota) { |
1096 | /* over quota */ | |
1097 | myspace = 0; | |
34dc7c2f BB |
1098 | } else { |
1099 | /* | |
1100 | * the lesser of the space provided by our parent and | |
1101 | * the space left in our quota | |
1102 | */ | |
1103 | myspace = MIN(parentspace, quota - used); | |
1104 | } | |
1105 | ||
1106 | mutex_exit(&dd->dd_lock); | |
1107 | ||
1108 | return (myspace); | |
1109 | } | |
1110 | ||
1111 | struct tempreserve { | |
1112 | list_node_t tr_node; | |
34dc7c2f BB |
1113 | dsl_dir_t *tr_ds; |
1114 | uint64_t tr_size; | |
1115 | }; | |
1116 | ||
1117 | static int | |
1118 | dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, | |
cae5b340 | 1119 | boolean_t ignorequota, list_t *tr_list, |
34dc7c2f BB |
1120 | dmu_tx_t *tx, boolean_t first) |
1121 | { | |
cae5b340 AX |
1122 | uint64_t txg; |
1123 | uint64_t quota; | |
34dc7c2f | 1124 | struct tempreserve *tr; |
cae5b340 AX |
1125 | int retval; |
1126 | uint64_t ref_rsrv; | |
1127 | ||
1128 | top_of_function: | |
1129 | txg = tx->tx_txg; | |
1130 | retval = EDQUOT; | |
1131 | ref_rsrv = 0; | |
34dc7c2f BB |
1132 | |
1133 | ASSERT3U(txg, !=, 0); | |
1134 | ASSERT3S(asize, >, 0); | |
1135 | ||
1136 | mutex_enter(&dd->dd_lock); | |
1137 | ||
1138 | /* | |
1139 | * Check against the dsl_dir's quota. We don't add in the delta | |
1140 | * when checking for over-quota because they get one free hit. | |
1141 | */ | |
cae5b340 AX |
1142 | uint64_t est_inflight = dsl_dir_space_towrite(dd); |
1143 | for (int i = 0; i < TXG_SIZE; i++) | |
34dc7c2f | 1144 | est_inflight += dd->dd_tempreserved[i]; |
cae5b340 | 1145 | uint64_t used_on_disk = dsl_dir_phys(dd)->dd_used_bytes; |
34dc7c2f BB |
1146 | |
1147 | /* | |
1148 | * On the first iteration, fetch the dataset's used-on-disk and | |
1149 | * refreservation values. Also, if checkrefquota is set, test if | |
1150 | * allocating this space would exceed the dataset's refquota. | |
1151 | */ | |
1152 | if (first && tx->tx_objset) { | |
1153 | int error; | |
428870ff | 1154 | dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset; |
34dc7c2f | 1155 | |
cae5b340 | 1156 | error = dsl_dataset_check_quota(ds, !netfree, |
34dc7c2f | 1157 | asize, est_inflight, &used_on_disk, &ref_rsrv); |
cae5b340 | 1158 | if (error != 0) { |
34dc7c2f | 1159 | mutex_exit(&dd->dd_lock); |
ea04106b | 1160 | DMU_TX_STAT_BUMP(dmu_tx_quota); |
34dc7c2f BB |
1161 | return (error); |
1162 | } | |
1163 | } | |
1164 | ||
1165 | /* | |
1166 | * If this transaction will result in a net free of space, | |
1167 | * we want to let it through. | |
1168 | */ | |
e10b0808 | 1169 | if (ignorequota || netfree || dsl_dir_phys(dd)->dd_quota == 0) |
34dc7c2f BB |
1170 | quota = UINT64_MAX; |
1171 | else | |
e10b0808 | 1172 | quota = dsl_dir_phys(dd)->dd_quota; |
34dc7c2f BB |
1173 | |
1174 | /* | |
428870ff BB |
1175 | * Adjust the quota against the actual pool size at the root |
1176 | * minus any outstanding deferred frees. | |
34dc7c2f BB |
1177 | * To ensure that it's possible to remove files from a full |
1178 | * pool without inducing transient overcommits, we throttle | |
1179 | * netfree transactions against a quota that is slightly larger, | |
1180 | * but still within the pool's allocation slop. In cases where | |
1181 | * we're very close to full, this will allow a steady trickle of | |
1182 | * removes to get through. | |
1183 | */ | |
cae5b340 | 1184 | uint64_t deferred = 0; |
34dc7c2f | 1185 | if (dd->dd_parent == NULL) { |
428870ff | 1186 | spa_t *spa = dd->dd_pool->dp_spa; |
34dc7c2f | 1187 | uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree); |
428870ff BB |
1188 | deferred = metaslab_class_get_deferred(spa_normal_class(spa)); |
1189 | if (poolsize - deferred < quota) { | |
1190 | quota = poolsize - deferred; | |
1191 | retval = ENOSPC; | |
34dc7c2f BB |
1192 | } |
1193 | } | |
1194 | ||
1195 | /* | |
1196 | * If they are requesting more space, and our current estimate | |
1197 | * is over quota, they get to try again unless the actual | |
1198 | * on-disk is over quota and there are no pending changes (which | |
1199 | * may free up space for us). | |
1200 | */ | |
428870ff BB |
1201 | if (used_on_disk + est_inflight >= quota) { |
1202 | if (est_inflight > 0 || used_on_disk < quota || | |
1203 | (retval == ENOSPC && used_on_disk < quota + deferred)) | |
1204 | retval = ERESTART; | |
34dc7c2f BB |
1205 | dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " |
1206 | "quota=%lluK tr=%lluK err=%d\n", | |
1207 | used_on_disk>>10, est_inflight>>10, | |
428870ff | 1208 | quota>>10, asize>>10, retval); |
34dc7c2f | 1209 | mutex_exit(&dd->dd_lock); |
ea04106b | 1210 | DMU_TX_STAT_BUMP(dmu_tx_quota); |
a08ee875 | 1211 | return (SET_ERROR(retval)); |
34dc7c2f BB |
1212 | } |
1213 | ||
1214 | /* We need to up our estimated delta before dropping dd_lock */ | |
cae5b340 | 1215 | dd->dd_tempreserved[txg & TXG_MASK] += asize; |
34dc7c2f | 1216 | |
cae5b340 | 1217 | uint64_t parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, |
34dc7c2f BB |
1218 | asize - ref_rsrv); |
1219 | mutex_exit(&dd->dd_lock); | |
1220 | ||
ea04106b | 1221 | tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); |
34dc7c2f BB |
1222 | tr->tr_ds = dd; |
1223 | tr->tr_size = asize; | |
1224 | list_insert_tail(tr_list, tr); | |
1225 | ||
1226 | /* see if it's OK with our parent */ | |
cae5b340 AX |
1227 | if (dd->dd_parent != NULL && parent_rsrv != 0) { |
1228 | /* | |
1229 | * Recurse on our parent without recursion. This has been | |
1230 | * observed to be potentially large stack usage even within | |
1231 | * the test suite. Largest seen stack was 7632 bytes on linux. | |
1232 | */ | |
1233 | ||
1234 | dd = dd->dd_parent; | |
1235 | asize = parent_rsrv; | |
1236 | ignorequota = (dsl_dir_phys(dd)->dd_head_dataset_obj == 0); | |
1237 | first = B_FALSE; | |
1238 | goto top_of_function; | |
34dc7c2f | 1239 | |
34dc7c2f BB |
1240 | } else { |
1241 | return (0); | |
1242 | } | |
1243 | } | |
1244 | ||
1245 | /* | |
1246 | * Reserve space in this dsl_dir, to be used in this tx's txg. | |
1247 | * After the space has been dirtied (and dsl_dir_willuse_space() | |
1248 | * has been called), the reservation should be canceled, using | |
1249 | * dsl_dir_tempreserve_clear(). | |
1250 | */ | |
1251 | int | |
1252 | dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, | |
cae5b340 | 1253 | boolean_t netfree, void **tr_cookiep, dmu_tx_t *tx) |
34dc7c2f BB |
1254 | { |
1255 | int err; | |
1256 | list_t *tr_list; | |
1257 | ||
1258 | if (asize == 0) { | |
1259 | *tr_cookiep = NULL; | |
1260 | return (0); | |
1261 | } | |
1262 | ||
ea04106b | 1263 | tr_list = kmem_alloc(sizeof (list_t), KM_SLEEP); |
34dc7c2f BB |
1264 | list_create(tr_list, sizeof (struct tempreserve), |
1265 | offsetof(struct tempreserve, tr_node)); | |
1266 | ASSERT3S(asize, >, 0); | |
34dc7c2f BB |
1267 | |
1268 | err = arc_tempreserve_space(lsize, tx->tx_txg); | |
1269 | if (err == 0) { | |
1270 | struct tempreserve *tr; | |
1271 | ||
ea04106b | 1272 | tr = kmem_zalloc(sizeof (struct tempreserve), KM_SLEEP); |
34dc7c2f BB |
1273 | tr->tr_size = lsize; |
1274 | list_insert_tail(tr_list, tr); | |
34dc7c2f BB |
1275 | } else { |
1276 | if (err == EAGAIN) { | |
a08ee875 LG |
1277 | /* |
1278 | * If arc_memory_throttle() detected that pageout | |
1279 | * is running and we are low on memory, we delay new | |
1280 | * non-pageout transactions to give pageout an | |
1281 | * advantage. | |
1282 | * | |
1283 | * It is unfortunate to be delaying while the caller's | |
1284 | * locks are held. | |
1285 | */ | |
1286 | txg_delay(dd->dd_pool, tx->tx_txg, | |
1287 | MSEC2NSEC(10), MSEC2NSEC(10)); | |
1288 | err = SET_ERROR(ERESTART); | |
34dc7c2f | 1289 | } |
34dc7c2f BB |
1290 | } |
1291 | ||
1292 | if (err == 0) { | |
cae5b340 AX |
1293 | err = dsl_dir_tempreserve_impl(dd, asize, netfree, |
1294 | B_FALSE, tr_list, tx, B_TRUE); | |
34dc7c2f BB |
1295 | } |
1296 | ||
a08ee875 | 1297 | if (err != 0) |
34dc7c2f BB |
1298 | dsl_dir_tempreserve_clear(tr_list, tx); |
1299 | else | |
1300 | *tr_cookiep = tr_list; | |
1301 | ||
1302 | return (err); | |
1303 | } | |
1304 | ||
1305 | /* | |
1306 | * Clear a temporary reservation that we previously made with | |
1307 | * dsl_dir_tempreserve_space(). | |
1308 | */ | |
1309 | void | |
1310 | dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) | |
1311 | { | |
1312 | int txgidx = tx->tx_txg & TXG_MASK; | |
1313 | list_t *tr_list = tr_cookie; | |
1314 | struct tempreserve *tr; | |
1315 | ||
1316 | ASSERT3U(tx->tx_txg, !=, 0); | |
1317 | ||
1318 | if (tr_cookie == NULL) | |
1319 | return; | |
1320 | ||
a08ee875 LG |
1321 | while ((tr = list_head(tr_list)) != NULL) { |
1322 | if (tr->tr_ds) { | |
34dc7c2f BB |
1323 | mutex_enter(&tr->tr_ds->dd_lock); |
1324 | ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, | |
1325 | tr->tr_size); | |
1326 | tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; | |
1327 | mutex_exit(&tr->tr_ds->dd_lock); | |
1328 | } else { | |
1329 | arc_tempreserve_clear(tr->tr_size); | |
1330 | } | |
1331 | list_remove(tr_list, tr); | |
1332 | kmem_free(tr, sizeof (struct tempreserve)); | |
1333 | } | |
1334 | ||
1335 | kmem_free(tr_list, sizeof (list_t)); | |
1336 | } | |
1337 | ||
a08ee875 LG |
1338 | /* |
1339 | * This should be called from open context when we think we're going to write | |
1340 | * or free space, for example when dirtying data. Be conservative; it's okay | |
1341 | * to write less space or free more, but we don't want to write more or free | |
1342 | * less than the amount specified. | |
ea04106b AX |
1343 | * |
1344 | * NOTE: The behavior of this function is identical to the Illumos / FreeBSD | |
1345 | * version however it has been adjusted to use an iterative rather then | |
1346 | * recursive algorithm to minimize stack usage. | |
a08ee875 LG |
1347 | */ |
1348 | void | |
1349 | dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) | |
34dc7c2f BB |
1350 | { |
1351 | int64_t parent_space; | |
1352 | uint64_t est_used; | |
1353 | ||
ea04106b AX |
1354 | do { |
1355 | mutex_enter(&dd->dd_lock); | |
1356 | if (space > 0) | |
1357 | dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; | |
34dc7c2f | 1358 | |
ea04106b | 1359 | est_used = dsl_dir_space_towrite(dd) + |
e10b0808 | 1360 | dsl_dir_phys(dd)->dd_used_bytes; |
ea04106b AX |
1361 | parent_space = parent_delta(dd, est_used, space); |
1362 | mutex_exit(&dd->dd_lock); | |
34dc7c2f | 1363 | |
ea04106b AX |
1364 | /* Make sure that we clean up dd_space_to* */ |
1365 | dsl_dir_dirty(dd, tx); | |
34dc7c2f | 1366 | |
ea04106b AX |
1367 | dd = dd->dd_parent; |
1368 | space = parent_space; | |
1369 | } while (space && dd); | |
34dc7c2f BB |
1370 | } |
1371 | ||
1372 | /* call from syncing context when we actually write/free space for this dd */ | |
1373 | void | |
b128c09f | 1374 | dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, |
34dc7c2f BB |
1375 | int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) |
1376 | { | |
1377 | int64_t accounted_delta; | |
a08ee875 LG |
1378 | |
1379 | /* | |
1380 | * dsl_dataset_set_refreservation_sync_impl() calls this with | |
1381 | * dd_lock held, so that it can atomically update | |
1382 | * ds->ds_reserved and the dsl_dir accounting, so that | |
1383 | * dsl_dataset_check_quota() can see dataset and dir accounting | |
1384 | * consistently. | |
1385 | */ | |
b128c09f | 1386 | boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); |
34dc7c2f BB |
1387 | |
1388 | ASSERT(dmu_tx_is_syncing(tx)); | |
b128c09f | 1389 | ASSERT(type < DD_USED_NUM); |
34dc7c2f | 1390 | |
a08ee875 LG |
1391 | dmu_buf_will_dirty(dd->dd_dbuf, tx); |
1392 | ||
b128c09f BB |
1393 | if (needlock) |
1394 | mutex_enter(&dd->dd_lock); | |
e10b0808 AX |
1395 | accounted_delta = |
1396 | parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, used); | |
1397 | ASSERT(used >= 0 || dsl_dir_phys(dd)->dd_used_bytes >= -used); | |
34dc7c2f | 1398 | ASSERT(compressed >= 0 || |
e10b0808 | 1399 | dsl_dir_phys(dd)->dd_compressed_bytes >= -compressed); |
34dc7c2f | 1400 | ASSERT(uncompressed >= 0 || |
e10b0808 AX |
1401 | dsl_dir_phys(dd)->dd_uncompressed_bytes >= -uncompressed); |
1402 | dsl_dir_phys(dd)->dd_used_bytes += used; | |
1403 | dsl_dir_phys(dd)->dd_uncompressed_bytes += uncompressed; | |
1404 | dsl_dir_phys(dd)->dd_compressed_bytes += compressed; | |
b128c09f | 1405 | |
e10b0808 | 1406 | if (dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN) { |
b128c09f | 1407 | ASSERT(used > 0 || |
e10b0808 AX |
1408 | dsl_dir_phys(dd)->dd_used_breakdown[type] >= -used); |
1409 | dsl_dir_phys(dd)->dd_used_breakdown[type] += used; | |
b128c09f | 1410 | #ifdef DEBUG |
d6320ddb BB |
1411 | { |
1412 | dd_used_t t; | |
1413 | uint64_t u = 0; | |
1414 | for (t = 0; t < DD_USED_NUM; t++) | |
e10b0808 AX |
1415 | u += dsl_dir_phys(dd)->dd_used_breakdown[t]; |
1416 | ASSERT3U(u, ==, dsl_dir_phys(dd)->dd_used_bytes); | |
d6320ddb | 1417 | } |
b128c09f BB |
1418 | #endif |
1419 | } | |
1420 | if (needlock) | |
1421 | mutex_exit(&dd->dd_lock); | |
34dc7c2f BB |
1422 | |
1423 | if (dd->dd_parent != NULL) { | |
b128c09f | 1424 | dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, |
34dc7c2f | 1425 | accounted_delta, compressed, uncompressed, tx); |
b128c09f BB |
1426 | dsl_dir_transfer_space(dd->dd_parent, |
1427 | used - accounted_delta, | |
1428 | DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); | |
34dc7c2f BB |
1429 | } |
1430 | } | |
1431 | ||
b128c09f BB |
1432 | void |
1433 | dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, | |
1434 | dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) | |
1435 | { | |
b128c09f BB |
1436 | ASSERT(dmu_tx_is_syncing(tx)); |
1437 | ASSERT(oldtype < DD_USED_NUM); | |
1438 | ASSERT(newtype < DD_USED_NUM); | |
1439 | ||
e10b0808 AX |
1440 | if (delta == 0 || |
1441 | !(dsl_dir_phys(dd)->dd_flags & DD_FLAG_USED_BREAKDOWN)) | |
b128c09f BB |
1442 | return; |
1443 | ||
a08ee875 LG |
1444 | dmu_buf_will_dirty(dd->dd_dbuf, tx); |
1445 | mutex_enter(&dd->dd_lock); | |
b128c09f | 1446 | ASSERT(delta > 0 ? |
e10b0808 AX |
1447 | dsl_dir_phys(dd)->dd_used_breakdown[oldtype] >= delta : |
1448 | dsl_dir_phys(dd)->dd_used_breakdown[newtype] >= -delta); | |
1449 | ASSERT(dsl_dir_phys(dd)->dd_used_bytes >= ABS(delta)); | |
1450 | dsl_dir_phys(dd)->dd_used_breakdown[oldtype] -= delta; | |
1451 | dsl_dir_phys(dd)->dd_used_breakdown[newtype] += delta; | |
a08ee875 | 1452 | mutex_exit(&dd->dd_lock); |
b128c09f BB |
1453 | } |
1454 | ||
a08ee875 LG |
1455 | typedef struct dsl_dir_set_qr_arg { |
1456 | const char *ddsqra_name; | |
1457 | zprop_source_t ddsqra_source; | |
1458 | uint64_t ddsqra_value; | |
1459 | } dsl_dir_set_qr_arg_t; | |
1460 | ||
34dc7c2f | 1461 | static int |
a08ee875 | 1462 | dsl_dir_set_quota_check(void *arg, dmu_tx_t *tx) |
34dc7c2f | 1463 | { |
a08ee875 LG |
1464 | dsl_dir_set_qr_arg_t *ddsqra = arg; |
1465 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1466 | dsl_dataset_t *ds; | |
1467 | int error; | |
1468 | uint64_t towrite, newval; | |
34dc7c2f | 1469 | |
a08ee875 LG |
1470 | error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); |
1471 | if (error != 0) | |
1472 | return (error); | |
428870ff | 1473 | |
a08ee875 LG |
1474 | error = dsl_prop_predict(ds->ds_dir, "quota", |
1475 | ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); | |
1476 | if (error != 0) { | |
1477 | dsl_dataset_rele(ds, FTAG); | |
1478 | return (error); | |
1479 | } | |
1480 | ||
1481 | if (newval == 0) { | |
1482 | dsl_dataset_rele(ds, FTAG); | |
34dc7c2f | 1483 | return (0); |
a08ee875 | 1484 | } |
34dc7c2f | 1485 | |
a08ee875 | 1486 | mutex_enter(&ds->ds_dir->dd_lock); |
34dc7c2f BB |
1487 | /* |
1488 | * If we are doing the preliminary check in open context, and | |
1489 | * there are pending changes, then don't fail it, since the | |
1490 | * pending changes could under-estimate the amount of space to be | |
1491 | * freed up. | |
1492 | */ | |
a08ee875 | 1493 | towrite = dsl_dir_space_towrite(ds->ds_dir); |
34dc7c2f | 1494 | if ((dmu_tx_is_syncing(tx) || towrite == 0) && |
e10b0808 AX |
1495 | (newval < dsl_dir_phys(ds->ds_dir)->dd_reserved || |
1496 | newval < dsl_dir_phys(ds->ds_dir)->dd_used_bytes + towrite)) { | |
a08ee875 | 1497 | error = SET_ERROR(ENOSPC); |
34dc7c2f | 1498 | } |
a08ee875 LG |
1499 | mutex_exit(&ds->ds_dir->dd_lock); |
1500 | dsl_dataset_rele(ds, FTAG); | |
1501 | return (error); | |
34dc7c2f BB |
1502 | } |
1503 | ||
34dc7c2f | 1504 | static void |
a08ee875 | 1505 | dsl_dir_set_quota_sync(void *arg, dmu_tx_t *tx) |
34dc7c2f | 1506 | { |
a08ee875 LG |
1507 | dsl_dir_set_qr_arg_t *ddsqra = arg; |
1508 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1509 | dsl_dataset_t *ds; | |
1510 | uint64_t newval; | |
428870ff | 1511 | |
a08ee875 | 1512 | VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); |
34dc7c2f | 1513 | |
a08ee875 LG |
1514 | if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { |
1515 | dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_QUOTA), | |
1516 | ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, | |
1517 | &ddsqra->ddsqra_value, tx); | |
34dc7c2f | 1518 | |
a08ee875 LG |
1519 | VERIFY0(dsl_prop_get_int_ds(ds, |
1520 | zfs_prop_to_name(ZFS_PROP_QUOTA), &newval)); | |
1521 | } else { | |
1522 | newval = ddsqra->ddsqra_value; | |
1523 | spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", | |
1524 | zfs_prop_to_name(ZFS_PROP_QUOTA), (longlong_t)newval); | |
1525 | } | |
1526 | ||
1527 | dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx); | |
1528 | mutex_enter(&ds->ds_dir->dd_lock); | |
e10b0808 | 1529 | dsl_dir_phys(ds->ds_dir)->dd_quota = newval; |
a08ee875 LG |
1530 | mutex_exit(&ds->ds_dir->dd_lock); |
1531 | dsl_dataset_rele(ds, FTAG); | |
34dc7c2f BB |
1532 | } |
1533 | ||
1534 | int | |
428870ff | 1535 | dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota) |
34dc7c2f | 1536 | { |
a08ee875 | 1537 | dsl_dir_set_qr_arg_t ddsqra; |
34dc7c2f | 1538 | |
a08ee875 LG |
1539 | ddsqra.ddsqra_name = ddname; |
1540 | ddsqra.ddsqra_source = source; | |
1541 | ddsqra.ddsqra_value = quota; | |
428870ff | 1542 | |
a08ee875 | 1543 | return (dsl_sync_task(ddname, dsl_dir_set_quota_check, |
e10b0808 | 1544 | dsl_dir_set_quota_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); |
34dc7c2f BB |
1545 | } |
1546 | ||
1547 | int | |
a08ee875 | 1548 | dsl_dir_set_reservation_check(void *arg, dmu_tx_t *tx) |
34dc7c2f | 1549 | { |
a08ee875 LG |
1550 | dsl_dir_set_qr_arg_t *ddsqra = arg; |
1551 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1552 | dsl_dataset_t *ds; | |
1553 | dsl_dir_t *dd; | |
1554 | uint64_t newval, used, avail; | |
1555 | int error; | |
428870ff | 1556 | |
a08ee875 LG |
1557 | error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds); |
1558 | if (error != 0) | |
1559 | return (error); | |
1560 | dd = ds->ds_dir; | |
34dc7c2f BB |
1561 | |
1562 | /* | |
1563 | * If we are doing the preliminary check in open context, the | |
1564 | * space estimates may be inaccurate. | |
1565 | */ | |
a08ee875 LG |
1566 | if (!dmu_tx_is_syncing(tx)) { |
1567 | dsl_dataset_rele(ds, FTAG); | |
34dc7c2f | 1568 | return (0); |
a08ee875 LG |
1569 | } |
1570 | ||
1571 | error = dsl_prop_predict(ds->ds_dir, | |
1572 | zfs_prop_to_name(ZFS_PROP_RESERVATION), | |
1573 | ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval); | |
1574 | if (error != 0) { | |
1575 | dsl_dataset_rele(ds, FTAG); | |
1576 | return (error); | |
1577 | } | |
34dc7c2f BB |
1578 | |
1579 | mutex_enter(&dd->dd_lock); | |
e10b0808 | 1580 | used = dsl_dir_phys(dd)->dd_used_bytes; |
34dc7c2f BB |
1581 | mutex_exit(&dd->dd_lock); |
1582 | ||
1583 | if (dd->dd_parent) { | |
1584 | avail = dsl_dir_space_available(dd->dd_parent, | |
1585 | NULL, 0, FALSE); | |
1586 | } else { | |
1587 | avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used; | |
1588 | } | |
1589 | ||
e10b0808 | 1590 | if (MAX(used, newval) > MAX(used, dsl_dir_phys(dd)->dd_reserved)) { |
a08ee875 | 1591 | uint64_t delta = MAX(used, newval) - |
e10b0808 | 1592 | MAX(used, dsl_dir_phys(dd)->dd_reserved); |
d164b209 | 1593 | |
a08ee875 | 1594 | if (delta > avail || |
e10b0808 AX |
1595 | (dsl_dir_phys(dd)->dd_quota > 0 && |
1596 | newval > dsl_dir_phys(dd)->dd_quota)) | |
a08ee875 | 1597 | error = SET_ERROR(ENOSPC); |
d164b209 BB |
1598 | } |
1599 | ||
a08ee875 LG |
1600 | dsl_dataset_rele(ds, FTAG); |
1601 | return (error); | |
34dc7c2f BB |
1602 | } |
1603 | ||
a08ee875 LG |
1604 | void |
1605 | dsl_dir_set_reservation_sync_impl(dsl_dir_t *dd, uint64_t value, dmu_tx_t *tx) | |
34dc7c2f | 1606 | { |
34dc7c2f BB |
1607 | uint64_t used; |
1608 | int64_t delta; | |
1609 | ||
1610 | dmu_buf_will_dirty(dd->dd_dbuf, tx); | |
1611 | ||
1612 | mutex_enter(&dd->dd_lock); | |
e10b0808 AX |
1613 | used = dsl_dir_phys(dd)->dd_used_bytes; |
1614 | delta = MAX(used, value) - MAX(used, dsl_dir_phys(dd)->dd_reserved); | |
1615 | dsl_dir_phys(dd)->dd_reserved = value; | |
34dc7c2f BB |
1616 | |
1617 | if (dd->dd_parent != NULL) { | |
1618 | /* Roll up this additional usage into our ancestors */ | |
b128c09f BB |
1619 | dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, |
1620 | delta, 0, 0, tx); | |
34dc7c2f | 1621 | } |
b128c09f | 1622 | mutex_exit(&dd->dd_lock); |
34dc7c2f BB |
1623 | } |
1624 | ||
a08ee875 LG |
1625 | static void |
1626 | dsl_dir_set_reservation_sync(void *arg, dmu_tx_t *tx) | |
34dc7c2f | 1627 | { |
a08ee875 LG |
1628 | dsl_dir_set_qr_arg_t *ddsqra = arg; |
1629 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
428870ff | 1630 | dsl_dataset_t *ds; |
a08ee875 | 1631 | uint64_t newval; |
34dc7c2f | 1632 | |
a08ee875 | 1633 | VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds)); |
428870ff | 1634 | |
a08ee875 LG |
1635 | if (spa_version(dp->dp_spa) >= SPA_VERSION_RECVD_PROPS) { |
1636 | dsl_prop_set_sync_impl(ds, | |
1637 | zfs_prop_to_name(ZFS_PROP_RESERVATION), | |
1638 | ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1, | |
1639 | &ddsqra->ddsqra_value, tx); | |
428870ff | 1640 | |
a08ee875 LG |
1641 | VERIFY0(dsl_prop_get_int_ds(ds, |
1642 | zfs_prop_to_name(ZFS_PROP_RESERVATION), &newval)); | |
1643 | } else { | |
1644 | newval = ddsqra->ddsqra_value; | |
1645 | spa_history_log_internal_ds(ds, "set", tx, "%s=%lld", | |
1646 | zfs_prop_to_name(ZFS_PROP_RESERVATION), | |
1647 | (longlong_t)newval); | |
428870ff BB |
1648 | } |
1649 | ||
a08ee875 LG |
1650 | dsl_dir_set_reservation_sync_impl(ds->ds_dir, newval, tx); |
1651 | dsl_dataset_rele(ds, FTAG); | |
1652 | } | |
1653 | ||
1654 | int | |
1655 | dsl_dir_set_reservation(const char *ddname, zprop_source_t source, | |
1656 | uint64_t reservation) | |
1657 | { | |
1658 | dsl_dir_set_qr_arg_t ddsqra; | |
428870ff | 1659 | |
a08ee875 LG |
1660 | ddsqra.ddsqra_name = ddname; |
1661 | ddsqra.ddsqra_source = source; | |
1662 | ddsqra.ddsqra_value = reservation; | |
428870ff | 1663 | |
a08ee875 | 1664 | return (dsl_sync_task(ddname, dsl_dir_set_reservation_check, |
e10b0808 | 1665 | dsl_dir_set_reservation_sync, &ddsqra, 0, ZFS_SPACE_CHECK_NONE)); |
34dc7c2f BB |
1666 | } |
1667 | ||
1668 | static dsl_dir_t * | |
1669 | closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) | |
1670 | { | |
1671 | for (; ds1; ds1 = ds1->dd_parent) { | |
1672 | dsl_dir_t *dd; | |
1673 | for (dd = ds2; dd; dd = dd->dd_parent) { | |
1674 | if (ds1 == dd) | |
1675 | return (dd); | |
1676 | } | |
1677 | } | |
1678 | return (NULL); | |
1679 | } | |
1680 | ||
1681 | /* | |
1682 | * If delta is applied to dd, how much of that delta would be applied to | |
1683 | * ancestor? Syncing context only. | |
1684 | */ | |
1685 | static int64_t | |
1686 | would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) | |
1687 | { | |
1688 | if (dd == ancestor) | |
1689 | return (delta); | |
1690 | ||
1691 | mutex_enter(&dd->dd_lock); | |
e10b0808 | 1692 | delta = parent_delta(dd, dsl_dir_phys(dd)->dd_used_bytes, delta); |
34dc7c2f BB |
1693 | mutex_exit(&dd->dd_lock); |
1694 | return (would_change(dd->dd_parent, delta, ancestor)); | |
1695 | } | |
1696 | ||
a08ee875 LG |
1697 | typedef struct dsl_dir_rename_arg { |
1698 | const char *ddra_oldname; | |
1699 | const char *ddra_newname; | |
e10b0808 | 1700 | cred_t *ddra_cred; |
a08ee875 | 1701 | } dsl_dir_rename_arg_t; |
34dc7c2f | 1702 | |
a08ee875 | 1703 | /* ARGSUSED */ |
34dc7c2f | 1704 | static int |
a08ee875 | 1705 | dsl_valid_rename(dsl_pool_t *dp, dsl_dataset_t *ds, void *arg) |
34dc7c2f | 1706 | { |
a08ee875 | 1707 | int *deltap = arg; |
cae5b340 | 1708 | char namebuf[ZFS_MAX_DATASET_NAME_LEN]; |
34dc7c2f | 1709 | |
a08ee875 LG |
1710 | dsl_dataset_name(ds, namebuf); |
1711 | ||
cae5b340 | 1712 | if (strlen(namebuf) + *deltap >= ZFS_MAX_DATASET_NAME_LEN) |
a08ee875 LG |
1713 | return (SET_ERROR(ENAMETOOLONG)); |
1714 | return (0); | |
1715 | } | |
1716 | ||
1717 | static int | |
1718 | dsl_dir_rename_check(void *arg, dmu_tx_t *tx) | |
1719 | { | |
1720 | dsl_dir_rename_arg_t *ddra = arg; | |
1721 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1722 | dsl_dir_t *dd, *newparent; | |
1723 | const char *mynewname; | |
1724 | int error; | |
1725 | int delta = strlen(ddra->ddra_newname) - strlen(ddra->ddra_oldname); | |
34dc7c2f | 1726 | |
a08ee875 LG |
1727 | /* target dir should exist */ |
1728 | error = dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL); | |
1729 | if (error != 0) | |
1730 | return (error); | |
1731 | ||
1732 | /* new parent should exist */ | |
1733 | error = dsl_dir_hold(dp, ddra->ddra_newname, FTAG, | |
1734 | &newparent, &mynewname); | |
1735 | if (error != 0) { | |
1736 | dsl_dir_rele(dd, FTAG); | |
1737 | return (error); | |
1738 | } | |
1739 | ||
1740 | /* can't rename to different pool */ | |
1741 | if (dd->dd_pool != newparent->dd_pool) { | |
1742 | dsl_dir_rele(newparent, FTAG); | |
1743 | dsl_dir_rele(dd, FTAG); | |
ea04106b | 1744 | return (SET_ERROR(EXDEV)); |
a08ee875 LG |
1745 | } |
1746 | ||
1747 | /* new name should not already exist */ | |
1748 | if (mynewname == NULL) { | |
1749 | dsl_dir_rele(newparent, FTAG); | |
1750 | dsl_dir_rele(dd, FTAG); | |
1751 | return (SET_ERROR(EEXIST)); | |
1752 | } | |
1753 | ||
1754 | /* if the name length is growing, validate child name lengths */ | |
1755 | if (delta > 0) { | |
1756 | error = dmu_objset_find_dp(dp, dd->dd_object, dsl_valid_rename, | |
1757 | &delta, DS_FIND_CHILDREN | DS_FIND_SNAPSHOTS); | |
1758 | if (error != 0) { | |
1759 | dsl_dir_rele(newparent, FTAG); | |
1760 | dsl_dir_rele(dd, FTAG); | |
1761 | return (error); | |
1762 | } | |
1763 | } | |
1764 | ||
e10b0808 AX |
1765 | if (dmu_tx_is_syncing(tx)) { |
1766 | if (spa_feature_is_active(dp->dp_spa, | |
1767 | SPA_FEATURE_FS_SS_LIMIT)) { | |
1768 | /* | |
1769 | * Although this is the check function and we don't | |
1770 | * normally make on-disk changes in check functions, | |
1771 | * we need to do that here. | |
1772 | * | |
1773 | * Ensure this portion of the tree's counts have been | |
1774 | * initialized in case the new parent has limits set. | |
1775 | */ | |
1776 | dsl_dir_init_fs_ss_count(dd, tx); | |
1777 | } | |
1778 | } | |
1779 | ||
a08ee875 | 1780 | if (newparent != dd->dd_parent) { |
34dc7c2f BB |
1781 | /* is there enough space? */ |
1782 | uint64_t myspace = | |
e10b0808 AX |
1783 | MAX(dsl_dir_phys(dd)->dd_used_bytes, |
1784 | dsl_dir_phys(dd)->dd_reserved); | |
1785 | objset_t *os = dd->dd_pool->dp_meta_objset; | |
1786 | uint64_t fs_cnt = 0; | |
1787 | uint64_t ss_cnt = 0; | |
1788 | ||
1789 | if (dsl_dir_is_zapified(dd)) { | |
1790 | int err; | |
1791 | ||
1792 | err = zap_lookup(os, dd->dd_object, | |
1793 | DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, | |
1794 | &fs_cnt); | |
1795 | if (err != ENOENT && err != 0) { | |
1796 | dsl_dir_rele(newparent, FTAG); | |
1797 | dsl_dir_rele(dd, FTAG); | |
1798 | return (err); | |
1799 | } | |
1800 | ||
1801 | /* | |
1802 | * have to add 1 for the filesystem itself that we're | |
1803 | * moving | |
1804 | */ | |
1805 | fs_cnt++; | |
1806 | ||
1807 | err = zap_lookup(os, dd->dd_object, | |
1808 | DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, | |
1809 | &ss_cnt); | |
1810 | if (err != ENOENT && err != 0) { | |
1811 | dsl_dir_rele(newparent, FTAG); | |
1812 | dsl_dir_rele(dd, FTAG); | |
1813 | return (err); | |
1814 | } | |
1815 | } | |
34dc7c2f BB |
1816 | |
1817 | /* no rename into our descendant */ | |
a08ee875 LG |
1818 | if (closest_common_ancestor(dd, newparent) == dd) { |
1819 | dsl_dir_rele(newparent, FTAG); | |
1820 | dsl_dir_rele(dd, FTAG); | |
1821 | return (SET_ERROR(EINVAL)); | |
1822 | } | |
34dc7c2f | 1823 | |
a08ee875 | 1824 | error = dsl_dir_transfer_possible(dd->dd_parent, |
e10b0808 | 1825 | newparent, fs_cnt, ss_cnt, myspace, ddra->ddra_cred); |
a08ee875 LG |
1826 | if (error != 0) { |
1827 | dsl_dir_rele(newparent, FTAG); | |
1828 | dsl_dir_rele(dd, FTAG); | |
1829 | return (error); | |
1830 | } | |
34dc7c2f BB |
1831 | } |
1832 | ||
a08ee875 LG |
1833 | dsl_dir_rele(newparent, FTAG); |
1834 | dsl_dir_rele(dd, FTAG); | |
34dc7c2f BB |
1835 | return (0); |
1836 | } | |
1837 | ||
1838 | static void | |
a08ee875 | 1839 | dsl_dir_rename_sync(void *arg, dmu_tx_t *tx) |
34dc7c2f | 1840 | { |
a08ee875 LG |
1841 | dsl_dir_rename_arg_t *ddra = arg; |
1842 | dsl_pool_t *dp = dmu_tx_pool(tx); | |
1843 | dsl_dir_t *dd, *newparent; | |
1844 | const char *mynewname; | |
1845 | int error; | |
34dc7c2f | 1846 | objset_t *mos = dp->dp_meta_objset; |
34dc7c2f | 1847 | |
a08ee875 LG |
1848 | VERIFY0(dsl_dir_hold(dp, ddra->ddra_oldname, FTAG, &dd, NULL)); |
1849 | VERIFY0(dsl_dir_hold(dp, ddra->ddra_newname, FTAG, &newparent, | |
1850 | &mynewname)); | |
1851 | ||
1852 | /* Log this before we change the name. */ | |
1853 | spa_history_log_internal_dd(dd, "rename", tx, | |
1854 | "-> %s", ddra->ddra_newname); | |
34dc7c2f | 1855 | |
a08ee875 | 1856 | if (newparent != dd->dd_parent) { |
e10b0808 AX |
1857 | objset_t *os = dd->dd_pool->dp_meta_objset; |
1858 | uint64_t fs_cnt = 0; | |
1859 | uint64_t ss_cnt = 0; | |
1860 | ||
1861 | /* | |
1862 | * We already made sure the dd counts were initialized in the | |
1863 | * check function. | |
1864 | */ | |
1865 | if (spa_feature_is_active(dp->dp_spa, | |
1866 | SPA_FEATURE_FS_SS_LIMIT)) { | |
1867 | VERIFY0(zap_lookup(os, dd->dd_object, | |
1868 | DD_FIELD_FILESYSTEM_COUNT, sizeof (fs_cnt), 1, | |
1869 | &fs_cnt)); | |
1870 | /* add 1 for the filesystem itself that we're moving */ | |
1871 | fs_cnt++; | |
1872 | ||
1873 | VERIFY0(zap_lookup(os, dd->dd_object, | |
1874 | DD_FIELD_SNAPSHOT_COUNT, sizeof (ss_cnt), 1, | |
1875 | &ss_cnt)); | |
1876 | } | |
1877 | ||
1878 | dsl_fs_ss_count_adjust(dd->dd_parent, -fs_cnt, | |
1879 | DD_FIELD_FILESYSTEM_COUNT, tx); | |
1880 | dsl_fs_ss_count_adjust(newparent, fs_cnt, | |
1881 | DD_FIELD_FILESYSTEM_COUNT, tx); | |
1882 | ||
1883 | dsl_fs_ss_count_adjust(dd->dd_parent, -ss_cnt, | |
1884 | DD_FIELD_SNAPSHOT_COUNT, tx); | |
1885 | dsl_fs_ss_count_adjust(newparent, ss_cnt, | |
1886 | DD_FIELD_SNAPSHOT_COUNT, tx); | |
1887 | ||
b128c09f | 1888 | dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, |
e10b0808 AX |
1889 | -dsl_dir_phys(dd)->dd_used_bytes, |
1890 | -dsl_dir_phys(dd)->dd_compressed_bytes, | |
1891 | -dsl_dir_phys(dd)->dd_uncompressed_bytes, tx); | |
a08ee875 | 1892 | dsl_dir_diduse_space(newparent, DD_USED_CHILD, |
e10b0808 AX |
1893 | dsl_dir_phys(dd)->dd_used_bytes, |
1894 | dsl_dir_phys(dd)->dd_compressed_bytes, | |
1895 | dsl_dir_phys(dd)->dd_uncompressed_bytes, tx); | |
b128c09f | 1896 | |
e10b0808 AX |
1897 | if (dsl_dir_phys(dd)->dd_reserved > |
1898 | dsl_dir_phys(dd)->dd_used_bytes) { | |
1899 | uint64_t unused_rsrv = dsl_dir_phys(dd)->dd_reserved - | |
1900 | dsl_dir_phys(dd)->dd_used_bytes; | |
b128c09f BB |
1901 | |
1902 | dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, | |
1903 | -unused_rsrv, 0, 0, tx); | |
a08ee875 | 1904 | dsl_dir_diduse_space(newparent, DD_USED_CHILD_RSRV, |
b128c09f BB |
1905 | unused_rsrv, 0, 0, tx); |
1906 | } | |
34dc7c2f BB |
1907 | } |
1908 | ||
1909 | dmu_buf_will_dirty(dd->dd_dbuf, tx); | |
1910 | ||
1911 | /* remove from old parent zapobj */ | |
e10b0808 AX |
1912 | error = zap_remove(mos, |
1913 | dsl_dir_phys(dd->dd_parent)->dd_child_dir_zapobj, | |
34dc7c2f | 1914 | dd->dd_myname, tx); |
a08ee875 | 1915 | ASSERT0(error); |
34dc7c2f | 1916 | |
cae5b340 AX |
1917 | (void) strlcpy(dd->dd_myname, mynewname, |
1918 | sizeof (dd->dd_myname)); | |
a08ee875 | 1919 | dsl_dir_rele(dd->dd_parent, dd); |
e10b0808 | 1920 | dsl_dir_phys(dd)->dd_parent_obj = newparent->dd_object; |
a08ee875 LG |
1921 | VERIFY0(dsl_dir_hold_obj(dp, |
1922 | newparent->dd_object, NULL, dd, &dd->dd_parent)); | |
34dc7c2f BB |
1923 | |
1924 | /* add to new parent zapobj */ | |
e10b0808 | 1925 | VERIFY0(zap_add(mos, dsl_dir_phys(newparent)->dd_child_dir_zapobj, |
a08ee875 LG |
1926 | dd->dd_myname, 8, 1, &dd->dd_object, tx)); |
1927 | ||
4e820b5a AX |
1928 | zvol_rename_minors(dp->dp_spa, ddra->ddra_oldname, |
1929 | ddra->ddra_newname, B_TRUE); | |
a08ee875 LG |
1930 | |
1931 | dsl_prop_notify_all(dd); | |
34dc7c2f | 1932 | |
a08ee875 LG |
1933 | dsl_dir_rele(newparent, FTAG); |
1934 | dsl_dir_rele(dd, FTAG); | |
34dc7c2f BB |
1935 | } |
1936 | ||
1937 | int | |
a08ee875 | 1938 | dsl_dir_rename(const char *oldname, const char *newname) |
34dc7c2f | 1939 | { |
a08ee875 | 1940 | dsl_dir_rename_arg_t ddra; |
34dc7c2f | 1941 | |
a08ee875 LG |
1942 | ddra.ddra_oldname = oldname; |
1943 | ddra.ddra_newname = newname; | |
e10b0808 | 1944 | ddra.ddra_cred = CRED(); |
34dc7c2f | 1945 | |
a08ee875 | 1946 | return (dsl_sync_task(oldname, |
e10b0808 AX |
1947 | dsl_dir_rename_check, dsl_dir_rename_sync, &ddra, |
1948 | 3, ZFS_SPACE_CHECK_RESERVED)); | |
34dc7c2f BB |
1949 | } |
1950 | ||
1951 | int | |
e10b0808 AX |
1952 | dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, |
1953 | uint64_t fs_cnt, uint64_t ss_cnt, uint64_t space, cred_t *cr) | |
34dc7c2f BB |
1954 | { |
1955 | dsl_dir_t *ancestor; | |
1956 | int64_t adelta; | |
1957 | uint64_t avail; | |
e10b0808 | 1958 | int err; |
34dc7c2f BB |
1959 | |
1960 | ancestor = closest_common_ancestor(sdd, tdd); | |
1961 | adelta = would_change(sdd, -space, ancestor); | |
1962 | avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); | |
1963 | if (avail < space) | |
a08ee875 | 1964 | return (SET_ERROR(ENOSPC)); |
34dc7c2f | 1965 | |
e10b0808 AX |
1966 | err = dsl_fs_ss_limit_check(tdd, fs_cnt, ZFS_PROP_FILESYSTEM_LIMIT, |
1967 | ancestor, cr); | |
1968 | if (err != 0) | |
1969 | return (err); | |
1970 | err = dsl_fs_ss_limit_check(tdd, ss_cnt, ZFS_PROP_SNAPSHOT_LIMIT, | |
1971 | ancestor, cr); | |
1972 | if (err != 0) | |
1973 | return (err); | |
1974 | ||
34dc7c2f BB |
1975 | return (0); |
1976 | } | |
428870ff | 1977 | |
a07c8b41 | 1978 | inode_timespec_t |
428870ff BB |
1979 | dsl_dir_snap_cmtime(dsl_dir_t *dd) |
1980 | { | |
a07c8b41 | 1981 | inode_timespec_t t; |
428870ff BB |
1982 | |
1983 | mutex_enter(&dd->dd_lock); | |
1984 | t = dd->dd_snap_cmtime; | |
1985 | mutex_exit(&dd->dd_lock); | |
1986 | ||
1987 | return (t); | |
1988 | } | |
1989 | ||
1990 | void | |
1991 | dsl_dir_snap_cmtime_update(dsl_dir_t *dd) | |
1992 | { | |
a07c8b41 | 1993 | inode_timespec_t t; |
428870ff BB |
1994 | |
1995 | gethrestime(&t); | |
1996 | mutex_enter(&dd->dd_lock); | |
1997 | dd->dd_snap_cmtime = t; | |
1998 | mutex_exit(&dd->dd_lock); | |
1999 | } | |
c28b2279 | 2000 | |
ea04106b AX |
2001 | void |
2002 | dsl_dir_zapify(dsl_dir_t *dd, dmu_tx_t *tx) | |
2003 | { | |
2004 | objset_t *mos = dd->dd_pool->dp_meta_objset; | |
2005 | dmu_object_zapify(mos, dd->dd_object, DMU_OT_DSL_DIR, tx); | |
2006 | } | |
2007 | ||
e10b0808 AX |
2008 | boolean_t |
2009 | dsl_dir_is_zapified(dsl_dir_t *dd) | |
2010 | { | |
2011 | dmu_object_info_t doi; | |
2012 | ||
2013 | dmu_object_info_from_db(dd->dd_dbuf, &doi); | |
2014 | return (doi.doi_type == DMU_OTN_ZAP_METADATA); | |
2015 | } | |
2016 | ||
c28b2279 BB |
2017 | #if defined(_KERNEL) && defined(HAVE_SPL) |
2018 | EXPORT_SYMBOL(dsl_dir_set_quota); | |
2019 | EXPORT_SYMBOL(dsl_dir_set_reservation); | |
c28b2279 | 2020 | #endif |