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