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