]>
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. |
34dc7c2f BB |
23 | */ |
24 | ||
34dc7c2f BB |
25 | #include <sys/dmu.h> |
26 | #include <sys/dmu_objset.h> | |
27 | #include <sys/dmu_tx.h> | |
28 | #include <sys/dsl_dataset.h> | |
29 | #include <sys/dsl_dir.h> | |
30 | #include <sys/dsl_prop.h> | |
31 | #include <sys/dsl_synctask.h> | |
32 | #include <sys/dsl_deleg.h> | |
33 | #include <sys/spa.h> | |
428870ff | 34 | #include <sys/metaslab.h> |
34dc7c2f BB |
35 | #include <sys/zap.h> |
36 | #include <sys/zio.h> | |
37 | #include <sys/arc.h> | |
38 | #include <sys/sunddi.h> | |
39 | #include "zfs_namecheck.h" | |
40 | ||
41 | static uint64_t dsl_dir_space_towrite(dsl_dir_t *dd); | |
428870ff | 42 | static void dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx); |
34dc7c2f BB |
43 | |
44 | ||
45 | /* ARGSUSED */ | |
46 | static void | |
47 | dsl_dir_evict(dmu_buf_t *db, void *arg) | |
48 | { | |
49 | dsl_dir_t *dd = arg; | |
1fde1e37 | 50 | ASSERTV(dsl_pool_t *dp = dd->dd_pool;) |
34dc7c2f BB |
51 | int t; |
52 | ||
53 | for (t = 0; t < TXG_SIZE; t++) { | |
54 | ASSERT(!txg_list_member(&dp->dp_dirty_dirs, dd, t)); | |
55 | ASSERT(dd->dd_tempreserved[t] == 0); | |
56 | ASSERT(dd->dd_space_towrite[t] == 0); | |
57 | } | |
58 | ||
34dc7c2f BB |
59 | if (dd->dd_parent) |
60 | dsl_dir_close(dd->dd_parent, dd); | |
61 | ||
62 | spa_close(dd->dd_pool->dp_spa, dd); | |
63 | ||
64 | /* | |
428870ff BB |
65 | * The props callback list should have been cleaned up by |
66 | * objset_evict(). | |
34dc7c2f BB |
67 | */ |
68 | list_destroy(&dd->dd_prop_cbs); | |
69 | mutex_destroy(&dd->dd_lock); | |
70 | kmem_free(dd, sizeof (dsl_dir_t)); | |
71 | } | |
72 | ||
73 | int | |
74 | dsl_dir_open_obj(dsl_pool_t *dp, uint64_t ddobj, | |
75 | const char *tail, void *tag, dsl_dir_t **ddp) | |
76 | { | |
77 | dmu_buf_t *dbuf; | |
78 | dsl_dir_t *dd; | |
79 | int err; | |
80 | ||
81 | ASSERT(RW_LOCK_HELD(&dp->dp_config_rwlock) || | |
82 | dsl_pool_sync_context(dp)); | |
83 | ||
84 | err = dmu_bonus_hold(dp->dp_meta_objset, ddobj, tag, &dbuf); | |
85 | if (err) | |
86 | return (err); | |
87 | dd = dmu_buf_get_user(dbuf); | |
88 | #ifdef ZFS_DEBUG | |
89 | { | |
90 | dmu_object_info_t doi; | |
91 | dmu_object_info_from_db(dbuf, &doi); | |
92 | ASSERT3U(doi.doi_type, ==, DMU_OT_DSL_DIR); | |
b128c09f | 93 | ASSERT3U(doi.doi_bonus_size, >=, sizeof (dsl_dir_phys_t)); |
34dc7c2f BB |
94 | } |
95 | #endif | |
34dc7c2f BB |
96 | if (dd == NULL) { |
97 | dsl_dir_t *winner; | |
34dc7c2f | 98 | |
b8d06fca | 99 | dd = kmem_zalloc(sizeof (dsl_dir_t), KM_PUSHPAGE); |
34dc7c2f BB |
100 | dd->dd_object = ddobj; |
101 | dd->dd_dbuf = dbuf; | |
102 | dd->dd_pool = dp; | |
103 | dd->dd_phys = dbuf->db_data; | |
34dc7c2f BB |
104 | mutex_init(&dd->dd_lock, NULL, MUTEX_DEFAULT, NULL); |
105 | ||
106 | list_create(&dd->dd_prop_cbs, sizeof (dsl_prop_cb_record_t), | |
107 | offsetof(dsl_prop_cb_record_t, cbr_node)); | |
108 | ||
428870ff BB |
109 | dsl_dir_snap_cmtime_update(dd); |
110 | ||
34dc7c2f BB |
111 | if (dd->dd_phys->dd_parent_obj) { |
112 | err = dsl_dir_open_obj(dp, dd->dd_phys->dd_parent_obj, | |
113 | NULL, dd, &dd->dd_parent); | |
b128c09f BB |
114 | if (err) |
115 | goto errout; | |
34dc7c2f BB |
116 | if (tail) { |
117 | #ifdef ZFS_DEBUG | |
118 | uint64_t foundobj; | |
119 | ||
120 | err = zap_lookup(dp->dp_meta_objset, | |
121 | dd->dd_parent->dd_phys->dd_child_dir_zapobj, | |
122 | tail, sizeof (foundobj), 1, &foundobj); | |
123 | ASSERT(err || foundobj == ddobj); | |
124 | #endif | |
125 | (void) strcpy(dd->dd_myname, tail); | |
126 | } else { | |
127 | err = zap_value_search(dp->dp_meta_objset, | |
128 | dd->dd_parent->dd_phys->dd_child_dir_zapobj, | |
129 | ddobj, 0, dd->dd_myname); | |
130 | } | |
b128c09f BB |
131 | if (err) |
132 | goto errout; | |
34dc7c2f BB |
133 | } else { |
134 | (void) strcpy(dd->dd_myname, spa_name(dp->dp_spa)); | |
135 | } | |
136 | ||
428870ff BB |
137 | if (dsl_dir_is_clone(dd)) { |
138 | dmu_buf_t *origin_bonus; | |
139 | dsl_dataset_phys_t *origin_phys; | |
140 | ||
141 | /* | |
142 | * We can't open the origin dataset, because | |
143 | * that would require opening this dsl_dir. | |
144 | * Just look at its phys directly instead. | |
145 | */ | |
146 | err = dmu_bonus_hold(dp->dp_meta_objset, | |
147 | dd->dd_phys->dd_origin_obj, FTAG, &origin_bonus); | |
148 | if (err) | |
149 | goto errout; | |
150 | origin_phys = origin_bonus->db_data; | |
151 | dd->dd_origin_txg = | |
152 | origin_phys->ds_creation_txg; | |
153 | dmu_buf_rele(origin_bonus, FTAG); | |
154 | } | |
155 | ||
34dc7c2f BB |
156 | winner = dmu_buf_set_user_ie(dbuf, dd, &dd->dd_phys, |
157 | dsl_dir_evict); | |
158 | if (winner) { | |
159 | if (dd->dd_parent) | |
160 | dsl_dir_close(dd->dd_parent, dd); | |
161 | mutex_destroy(&dd->dd_lock); | |
162 | kmem_free(dd, sizeof (dsl_dir_t)); | |
163 | dd = winner; | |
164 | } else { | |
165 | spa_open_ref(dp->dp_spa, dd); | |
166 | } | |
167 | } | |
168 | ||
169 | /* | |
170 | * The dsl_dir_t has both open-to-close and instantiate-to-evict | |
171 | * holds on the spa. We need the open-to-close holds because | |
172 | * otherwise the spa_refcnt wouldn't change when we open a | |
173 | * dir which the spa also has open, so we could incorrectly | |
174 | * think it was OK to unload/export/destroy the pool. We need | |
175 | * the instantiate-to-evict hold because the dsl_dir_t has a | |
176 | * pointer to the dd_pool, which has a pointer to the spa_t. | |
177 | */ | |
178 | spa_open_ref(dp->dp_spa, tag); | |
179 | ASSERT3P(dd->dd_pool, ==, dp); | |
180 | ASSERT3U(dd->dd_object, ==, ddobj); | |
181 | ASSERT3P(dd->dd_dbuf, ==, dbuf); | |
182 | *ddp = dd; | |
183 | return (0); | |
b128c09f BB |
184 | |
185 | errout: | |
186 | if (dd->dd_parent) | |
187 | dsl_dir_close(dd->dd_parent, dd); | |
188 | mutex_destroy(&dd->dd_lock); | |
189 | kmem_free(dd, sizeof (dsl_dir_t)); | |
190 | dmu_buf_rele(dbuf, tag); | |
191 | return (err); | |
192 | ||
34dc7c2f BB |
193 | } |
194 | ||
195 | void | |
196 | dsl_dir_close(dsl_dir_t *dd, void *tag) | |
197 | { | |
198 | dprintf_dd(dd, "%s\n", ""); | |
199 | spa_close(dd->dd_pool->dp_spa, tag); | |
200 | dmu_buf_rele(dd->dd_dbuf, tag); | |
201 | } | |
202 | ||
203 | /* buf must be long enough (MAXNAMELEN + strlen(MOS_DIR_NAME) + 1 should do) */ | |
204 | void | |
205 | dsl_dir_name(dsl_dir_t *dd, char *buf) | |
206 | { | |
207 | if (dd->dd_parent) { | |
208 | dsl_dir_name(dd->dd_parent, buf); | |
209 | (void) strcat(buf, "/"); | |
210 | } else { | |
211 | buf[0] = '\0'; | |
212 | } | |
213 | if (!MUTEX_HELD(&dd->dd_lock)) { | |
214 | /* | |
215 | * recursive mutex so that we can use | |
216 | * dprintf_dd() with dd_lock held | |
217 | */ | |
218 | mutex_enter(&dd->dd_lock); | |
219 | (void) strcat(buf, dd->dd_myname); | |
220 | mutex_exit(&dd->dd_lock); | |
221 | } else { | |
222 | (void) strcat(buf, dd->dd_myname); | |
223 | } | |
224 | } | |
225 | ||
226 | /* Calculate name legnth, avoiding all the strcat calls of dsl_dir_name */ | |
227 | int | |
228 | dsl_dir_namelen(dsl_dir_t *dd) | |
229 | { | |
230 | int result = 0; | |
231 | ||
232 | if (dd->dd_parent) { | |
233 | /* parent's name + 1 for the "/" */ | |
234 | result = dsl_dir_namelen(dd->dd_parent) + 1; | |
235 | } | |
236 | ||
237 | if (!MUTEX_HELD(&dd->dd_lock)) { | |
238 | /* see dsl_dir_name */ | |
239 | mutex_enter(&dd->dd_lock); | |
240 | result += strlen(dd->dd_myname); | |
241 | mutex_exit(&dd->dd_lock); | |
242 | } else { | |
243 | result += strlen(dd->dd_myname); | |
244 | } | |
245 | ||
246 | return (result); | |
247 | } | |
248 | ||
34dc7c2f BB |
249 | static int |
250 | getcomponent(const char *path, char *component, const char **nextp) | |
251 | { | |
252 | char *p; | |
9babb374 | 253 | if ((path == NULL) || (path[0] == '\0')) |
34dc7c2f BB |
254 | return (ENOENT); |
255 | /* This would be a good place to reserve some namespace... */ | |
256 | p = strpbrk(path, "/@"); | |
257 | if (p && (p[1] == '/' || p[1] == '@')) { | |
258 | /* two separators in a row */ | |
259 | return (EINVAL); | |
260 | } | |
261 | if (p == NULL || p == path) { | |
262 | /* | |
263 | * if the first thing is an @ or /, it had better be an | |
264 | * @ and it had better not have any more ats or slashes, | |
265 | * and it had better have something after the @. | |
266 | */ | |
267 | if (p != NULL && | |
268 | (p[0] != '@' || strpbrk(path+1, "/@") || p[1] == '\0')) | |
269 | return (EINVAL); | |
270 | if (strlen(path) >= MAXNAMELEN) | |
271 | return (ENAMETOOLONG); | |
272 | (void) strcpy(component, path); | |
273 | p = NULL; | |
274 | } else if (p[0] == '/') { | |
275 | if (p-path >= MAXNAMELEN) | |
276 | return (ENAMETOOLONG); | |
277 | (void) strncpy(component, path, p - path); | |
278 | component[p-path] = '\0'; | |
279 | p++; | |
280 | } else if (p[0] == '@') { | |
281 | /* | |
282 | * if the next separator is an @, there better not be | |
283 | * any more slashes. | |
284 | */ | |
285 | if (strchr(path, '/')) | |
286 | return (EINVAL); | |
287 | if (p-path >= MAXNAMELEN) | |
288 | return (ENAMETOOLONG); | |
289 | (void) strncpy(component, path, p - path); | |
290 | component[p-path] = '\0'; | |
291 | } else { | |
292 | ASSERT(!"invalid p"); | |
293 | } | |
294 | *nextp = p; | |
295 | return (0); | |
296 | } | |
297 | ||
298 | /* | |
299 | * same as dsl_open_dir, ignore the first component of name and use the | |
300 | * spa instead | |
301 | */ | |
302 | int | |
303 | dsl_dir_open_spa(spa_t *spa, const char *name, void *tag, | |
304 | dsl_dir_t **ddp, const char **tailp) | |
305 | { | |
fcf37ec6 | 306 | char *buf; |
34dc7c2f BB |
307 | const char *next, *nextnext = NULL; |
308 | int err; | |
309 | dsl_dir_t *dd; | |
310 | dsl_pool_t *dp; | |
311 | uint64_t ddobj; | |
312 | int openedspa = FALSE; | |
313 | ||
314 | dprintf("%s\n", name); | |
315 | ||
fcf37ec6 | 316 | buf = kmem_alloc(MAXNAMELEN, KM_SLEEP); |
34dc7c2f BB |
317 | err = getcomponent(name, buf, &next); |
318 | if (err) | |
fcf37ec6 | 319 | goto error; |
34dc7c2f BB |
320 | if (spa == NULL) { |
321 | err = spa_open(buf, &spa, FTAG); | |
322 | if (err) { | |
323 | dprintf("spa_open(%s) failed\n", buf); | |
fcf37ec6 | 324 | goto error; |
34dc7c2f BB |
325 | } |
326 | openedspa = TRUE; | |
327 | ||
328 | /* XXX this assertion belongs in spa_open */ | |
329 | ASSERT(!dsl_pool_sync_context(spa_get_dsl(spa))); | |
330 | } | |
331 | ||
332 | dp = spa_get_dsl(spa); | |
333 | ||
334 | rw_enter(&dp->dp_config_rwlock, RW_READER); | |
335 | err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj, NULL, tag, &dd); | |
336 | if (err) { | |
337 | rw_exit(&dp->dp_config_rwlock); | |
338 | if (openedspa) | |
339 | spa_close(spa, FTAG); | |
fcf37ec6 | 340 | goto error; |
34dc7c2f BB |
341 | } |
342 | ||
343 | while (next != NULL) { | |
344 | dsl_dir_t *child_ds; | |
345 | err = getcomponent(next, buf, &nextnext); | |
346 | if (err) | |
347 | break; | |
348 | ASSERT(next[0] != '\0'); | |
349 | if (next[0] == '@') | |
350 | break; | |
351 | dprintf("looking up %s in obj%lld\n", | |
352 | buf, dd->dd_phys->dd_child_dir_zapobj); | |
353 | ||
354 | err = zap_lookup(dp->dp_meta_objset, | |
355 | dd->dd_phys->dd_child_dir_zapobj, | |
356 | buf, sizeof (ddobj), 1, &ddobj); | |
357 | if (err) { | |
358 | if (err == ENOENT) | |
359 | err = 0; | |
360 | break; | |
361 | } | |
362 | ||
363 | err = dsl_dir_open_obj(dp, ddobj, buf, tag, &child_ds); | |
364 | if (err) | |
365 | break; | |
366 | dsl_dir_close(dd, tag); | |
367 | dd = child_ds; | |
368 | next = nextnext; | |
369 | } | |
370 | rw_exit(&dp->dp_config_rwlock); | |
371 | ||
372 | if (err) { | |
373 | dsl_dir_close(dd, tag); | |
374 | if (openedspa) | |
375 | spa_close(spa, FTAG); | |
fcf37ec6 | 376 | goto error; |
34dc7c2f BB |
377 | } |
378 | ||
379 | /* | |
380 | * It's an error if there's more than one component left, or | |
381 | * tailp==NULL and there's any component left. | |
382 | */ | |
383 | if (next != NULL && | |
384 | (tailp == NULL || (nextnext && nextnext[0] != '\0'))) { | |
385 | /* bad path name */ | |
386 | dsl_dir_close(dd, tag); | |
387 | dprintf("next=%p (%s) tail=%p\n", next, next?next:"", tailp); | |
388 | err = ENOENT; | |
389 | } | |
390 | if (tailp) | |
391 | *tailp = next; | |
392 | if (openedspa) | |
393 | spa_close(spa, FTAG); | |
394 | *ddp = dd; | |
fcf37ec6 BB |
395 | error: |
396 | kmem_free(buf, MAXNAMELEN); | |
34dc7c2f BB |
397 | return (err); |
398 | } | |
399 | ||
400 | /* | |
401 | * Return the dsl_dir_t, and possibly the last component which couldn't | |
402 | * be found in *tail. Return NULL if the path is bogus, or if | |
403 | * tail==NULL and we couldn't parse the whole name. (*tail)[0] == '@' | |
404 | * means that the last component is a snapshot. | |
405 | */ | |
406 | int | |
407 | dsl_dir_open(const char *name, void *tag, dsl_dir_t **ddp, const char **tailp) | |
408 | { | |
409 | return (dsl_dir_open_spa(NULL, name, tag, ddp, tailp)); | |
410 | } | |
411 | ||
412 | uint64_t | |
b128c09f BB |
413 | dsl_dir_create_sync(dsl_pool_t *dp, dsl_dir_t *pds, const char *name, |
414 | dmu_tx_t *tx) | |
34dc7c2f | 415 | { |
b128c09f | 416 | objset_t *mos = dp->dp_meta_objset; |
34dc7c2f | 417 | uint64_t ddobj; |
428870ff | 418 | dsl_dir_phys_t *ddphys; |
34dc7c2f BB |
419 | dmu_buf_t *dbuf; |
420 | ||
421 | ddobj = dmu_object_alloc(mos, DMU_OT_DSL_DIR, 0, | |
422 | DMU_OT_DSL_DIR, sizeof (dsl_dir_phys_t), tx); | |
b128c09f BB |
423 | if (pds) { |
424 | VERIFY(0 == zap_add(mos, pds->dd_phys->dd_child_dir_zapobj, | |
425 | name, sizeof (uint64_t), 1, &ddobj, tx)); | |
426 | } else { | |
427 | /* it's the root dir */ | |
428 | VERIFY(0 == zap_add(mos, DMU_POOL_DIRECTORY_OBJECT, | |
429 | DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1, &ddobj, tx)); | |
430 | } | |
34dc7c2f BB |
431 | VERIFY(0 == dmu_bonus_hold(mos, ddobj, FTAG, &dbuf)); |
432 | dmu_buf_will_dirty(dbuf, tx); | |
428870ff | 433 | ddphys = dbuf->db_data; |
34dc7c2f | 434 | |
428870ff | 435 | ddphys->dd_creation_time = gethrestime_sec(); |
b128c09f | 436 | if (pds) |
428870ff BB |
437 | ddphys->dd_parent_obj = pds->dd_object; |
438 | ddphys->dd_props_zapobj = zap_create(mos, | |
34dc7c2f | 439 | DMU_OT_DSL_PROPS, DMU_OT_NONE, 0, tx); |
428870ff | 440 | ddphys->dd_child_dir_zapobj = zap_create(mos, |
34dc7c2f | 441 | DMU_OT_DSL_DIR_CHILD_MAP, DMU_OT_NONE, 0, tx); |
b128c09f | 442 | if (spa_version(dp->dp_spa) >= SPA_VERSION_USED_BREAKDOWN) |
428870ff | 443 | ddphys->dd_flags |= DD_FLAG_USED_BREAKDOWN; |
34dc7c2f BB |
444 | dmu_buf_rele(dbuf, FTAG); |
445 | ||
446 | return (ddobj); | |
447 | } | |
448 | ||
449 | /* ARGSUSED */ | |
450 | int | |
451 | dsl_dir_destroy_check(void *arg1, void *arg2, dmu_tx_t *tx) | |
452 | { | |
428870ff BB |
453 | dsl_dataset_t *ds = arg1; |
454 | dsl_dir_t *dd = ds->ds_dir; | |
34dc7c2f BB |
455 | dsl_pool_t *dp = dd->dd_pool; |
456 | objset_t *mos = dp->dp_meta_objset; | |
457 | int err; | |
458 | uint64_t count; | |
459 | ||
460 | /* | |
461 | * There should be exactly two holds, both from | |
462 | * dsl_dataset_destroy: one on the dd directory, and one on its | |
65947351 GW |
463 | * head ds. If there are more holds, then a concurrent thread is |
464 | * performing a lookup inside this dir while we're trying to destroy | |
465 | * it. To minimize this possibility, we perform this check only | |
466 | * in syncing context and fail the operation if we encounter | |
467 | * additional holds. The dp_config_rwlock ensures that nobody else | |
468 | * opens it after we check. | |
34dc7c2f | 469 | */ |
65947351 | 470 | if (dmu_tx_is_syncing(tx) && dmu_buf_refcount(dd->dd_dbuf) > 2) |
34dc7c2f BB |
471 | return (EBUSY); |
472 | ||
473 | err = zap_count(mos, dd->dd_phys->dd_child_dir_zapobj, &count); | |
474 | if (err) | |
475 | return (err); | |
476 | if (count != 0) | |
477 | return (EEXIST); | |
478 | ||
479 | return (0); | |
480 | } | |
481 | ||
482 | void | |
428870ff | 483 | dsl_dir_destroy_sync(void *arg1, void *tag, dmu_tx_t *tx) |
34dc7c2f | 484 | { |
428870ff BB |
485 | dsl_dataset_t *ds = arg1; |
486 | dsl_dir_t *dd = ds->ds_dir; | |
34dc7c2f | 487 | objset_t *mos = dd->dd_pool->dp_meta_objset; |
428870ff BB |
488 | dsl_prop_setarg_t psa; |
489 | uint64_t value = 0; | |
490 | uint64_t obj; | |
b128c09f | 491 | dd_used_t t; |
34dc7c2f BB |
492 | |
493 | ASSERT(RW_WRITE_HELD(&dd->dd_pool->dp_config_rwlock)); | |
494 | ASSERT(dd->dd_phys->dd_head_dataset_obj == 0); | |
495 | ||
496 | /* Remove our reservation. */ | |
428870ff BB |
497 | dsl_prop_setarg_init_uint64(&psa, "reservation", |
498 | (ZPROP_SRC_NONE | ZPROP_SRC_LOCAL | ZPROP_SRC_RECEIVED), | |
499 | &value); | |
500 | psa.psa_effective_value = 0; /* predict default value */ | |
501 | ||
502 | dsl_dir_set_reservation_sync(ds, &psa, tx); | |
503 | ||
b128c09f | 504 | ASSERT3U(dd->dd_phys->dd_used_bytes, ==, 0); |
34dc7c2f | 505 | ASSERT3U(dd->dd_phys->dd_reserved, ==, 0); |
b128c09f BB |
506 | for (t = 0; t < DD_USED_NUM; t++) |
507 | ASSERT3U(dd->dd_phys->dd_used_breakdown[t], ==, 0); | |
34dc7c2f BB |
508 | |
509 | VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_child_dir_zapobj, tx)); | |
510 | VERIFY(0 == zap_destroy(mos, dd->dd_phys->dd_props_zapobj, tx)); | |
511 | VERIFY(0 == dsl_deleg_destroy(mos, dd->dd_phys->dd_deleg_zapobj, tx)); | |
512 | VERIFY(0 == zap_remove(mos, | |
513 | dd->dd_parent->dd_phys->dd_child_dir_zapobj, dd->dd_myname, tx)); | |
514 | ||
515 | obj = dd->dd_object; | |
516 | dsl_dir_close(dd, tag); | |
517 | VERIFY(0 == dmu_object_free(mos, obj, tx)); | |
518 | } | |
519 | ||
b128c09f BB |
520 | boolean_t |
521 | dsl_dir_is_clone(dsl_dir_t *dd) | |
34dc7c2f | 522 | { |
b128c09f BB |
523 | return (dd->dd_phys->dd_origin_obj && |
524 | (dd->dd_pool->dp_origin_snap == NULL || | |
525 | dd->dd_phys->dd_origin_obj != | |
526 | dd->dd_pool->dp_origin_snap->ds_object)); | |
34dc7c2f BB |
527 | } |
528 | ||
529 | void | |
530 | dsl_dir_stats(dsl_dir_t *dd, nvlist_t *nv) | |
531 | { | |
532 | mutex_enter(&dd->dd_lock); | |
b128c09f BB |
533 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED, |
534 | dd->dd_phys->dd_used_bytes); | |
34dc7c2f BB |
535 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_QUOTA, dd->dd_phys->dd_quota); |
536 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_RESERVATION, | |
537 | dd->dd_phys->dd_reserved); | |
538 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, | |
539 | dd->dd_phys->dd_compressed_bytes == 0 ? 100 : | |
540 | (dd->dd_phys->dd_uncompressed_bytes * 100 / | |
541 | dd->dd_phys->dd_compressed_bytes)); | |
b128c09f BB |
542 | if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { |
543 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDSNAP, | |
544 | dd->dd_phys->dd_used_breakdown[DD_USED_SNAP]); | |
545 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDDS, | |
546 | dd->dd_phys->dd_used_breakdown[DD_USED_HEAD]); | |
547 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDREFRESERV, | |
548 | dd->dd_phys->dd_used_breakdown[DD_USED_REFRSRV]); | |
549 | dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USEDCHILD, | |
550 | dd->dd_phys->dd_used_breakdown[DD_USED_CHILD] + | |
551 | dd->dd_phys->dd_used_breakdown[DD_USED_CHILD_RSRV]); | |
552 | } | |
34dc7c2f BB |
553 | mutex_exit(&dd->dd_lock); |
554 | ||
555 | rw_enter(&dd->dd_pool->dp_config_rwlock, RW_READER); | |
b128c09f | 556 | if (dsl_dir_is_clone(dd)) { |
34dc7c2f BB |
557 | dsl_dataset_t *ds; |
558 | char buf[MAXNAMELEN]; | |
559 | ||
b128c09f BB |
560 | VERIFY(0 == dsl_dataset_hold_obj(dd->dd_pool, |
561 | dd->dd_phys->dd_origin_obj, FTAG, &ds)); | |
34dc7c2f | 562 | dsl_dataset_name(ds, buf); |
b128c09f | 563 | dsl_dataset_rele(ds, FTAG); |
34dc7c2f BB |
564 | dsl_prop_nvlist_add_string(nv, ZFS_PROP_ORIGIN, buf); |
565 | } | |
566 | rw_exit(&dd->dd_pool->dp_config_rwlock); | |
567 | } | |
568 | ||
569 | void | |
570 | dsl_dir_dirty(dsl_dir_t *dd, dmu_tx_t *tx) | |
571 | { | |
572 | dsl_pool_t *dp = dd->dd_pool; | |
573 | ||
574 | ASSERT(dd->dd_phys); | |
575 | ||
576 | if (txg_list_add(&dp->dp_dirty_dirs, dd, tx->tx_txg) == 0) { | |
577 | /* up the hold count until we can be written out */ | |
578 | dmu_buf_add_ref(dd->dd_dbuf, dd); | |
579 | } | |
580 | } | |
581 | ||
582 | static int64_t | |
583 | parent_delta(dsl_dir_t *dd, uint64_t used, int64_t delta) | |
584 | { | |
585 | uint64_t old_accounted = MAX(used, dd->dd_phys->dd_reserved); | |
586 | uint64_t new_accounted = MAX(used + delta, dd->dd_phys->dd_reserved); | |
587 | return (new_accounted - old_accounted); | |
588 | } | |
589 | ||
590 | void | |
591 | dsl_dir_sync(dsl_dir_t *dd, dmu_tx_t *tx) | |
592 | { | |
593 | ASSERT(dmu_tx_is_syncing(tx)); | |
594 | ||
595 | dmu_buf_will_dirty(dd->dd_dbuf, tx); | |
596 | ||
597 | mutex_enter(&dd->dd_lock); | |
598 | ASSERT3U(dd->dd_tempreserved[tx->tx_txg&TXG_MASK], ==, 0); | |
599 | dprintf_dd(dd, "txg=%llu towrite=%lluK\n", tx->tx_txg, | |
600 | dd->dd_space_towrite[tx->tx_txg&TXG_MASK] / 1024); | |
601 | dd->dd_space_towrite[tx->tx_txg&TXG_MASK] = 0; | |
34dc7c2f BB |
602 | mutex_exit(&dd->dd_lock); |
603 | ||
604 | /* release the hold from dsl_dir_dirty */ | |
605 | dmu_buf_rele(dd->dd_dbuf, dd); | |
606 | } | |
607 | ||
608 | static uint64_t | |
609 | dsl_dir_space_towrite(dsl_dir_t *dd) | |
610 | { | |
611 | uint64_t space = 0; | |
612 | int i; | |
613 | ||
614 | ASSERT(MUTEX_HELD(&dd->dd_lock)); | |
615 | ||
616 | for (i = 0; i < TXG_SIZE; i++) { | |
617 | space += dd->dd_space_towrite[i&TXG_MASK]; | |
618 | ASSERT3U(dd->dd_space_towrite[i&TXG_MASK], >=, 0); | |
619 | } | |
620 | return (space); | |
621 | } | |
622 | ||
623 | /* | |
624 | * How much space would dd have available if ancestor had delta applied | |
625 | * to it? If ondiskonly is set, we're only interested in what's | |
626 | * on-disk, not estimated pending changes. | |
627 | */ | |
628 | uint64_t | |
629 | dsl_dir_space_available(dsl_dir_t *dd, | |
630 | dsl_dir_t *ancestor, int64_t delta, int ondiskonly) | |
631 | { | |
632 | uint64_t parentspace, myspace, quota, used; | |
633 | ||
634 | /* | |
635 | * If there are no restrictions otherwise, assume we have | |
636 | * unlimited space available. | |
637 | */ | |
638 | quota = UINT64_MAX; | |
639 | parentspace = UINT64_MAX; | |
640 | ||
641 | if (dd->dd_parent != NULL) { | |
642 | parentspace = dsl_dir_space_available(dd->dd_parent, | |
643 | ancestor, delta, ondiskonly); | |
644 | } | |
645 | ||
646 | mutex_enter(&dd->dd_lock); | |
647 | if (dd->dd_phys->dd_quota != 0) | |
648 | quota = dd->dd_phys->dd_quota; | |
b128c09f | 649 | used = dd->dd_phys->dd_used_bytes; |
34dc7c2f BB |
650 | if (!ondiskonly) |
651 | used += dsl_dir_space_towrite(dd); | |
34dc7c2f BB |
652 | |
653 | if (dd->dd_parent == NULL) { | |
654 | uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, FALSE); | |
655 | quota = MIN(quota, poolsize); | |
656 | } | |
657 | ||
658 | if (dd->dd_phys->dd_reserved > used && parentspace != UINT64_MAX) { | |
659 | /* | |
660 | * We have some space reserved, in addition to what our | |
661 | * parent gave us. | |
662 | */ | |
663 | parentspace += dd->dd_phys->dd_reserved - used; | |
664 | } | |
665 | ||
b128c09f BB |
666 | if (dd == ancestor) { |
667 | ASSERT(delta <= 0); | |
668 | ASSERT(used >= -delta); | |
669 | used += delta; | |
670 | if (parentspace != UINT64_MAX) | |
671 | parentspace -= delta; | |
672 | } | |
673 | ||
34dc7c2f BB |
674 | if (used > quota) { |
675 | /* over quota */ | |
676 | myspace = 0; | |
34dc7c2f BB |
677 | } else { |
678 | /* | |
679 | * the lesser of the space provided by our parent and | |
680 | * the space left in our quota | |
681 | */ | |
682 | myspace = MIN(parentspace, quota - used); | |
683 | } | |
684 | ||
685 | mutex_exit(&dd->dd_lock); | |
686 | ||
687 | return (myspace); | |
688 | } | |
689 | ||
690 | struct tempreserve { | |
691 | list_node_t tr_node; | |
692 | dsl_pool_t *tr_dp; | |
693 | dsl_dir_t *tr_ds; | |
694 | uint64_t tr_size; | |
695 | }; | |
696 | ||
697 | static int | |
698 | dsl_dir_tempreserve_impl(dsl_dir_t *dd, uint64_t asize, boolean_t netfree, | |
699 | boolean_t ignorequota, boolean_t checkrefquota, list_t *tr_list, | |
700 | dmu_tx_t *tx, boolean_t first) | |
701 | { | |
702 | uint64_t txg = tx->tx_txg; | |
703 | uint64_t est_inflight, used_on_disk, quota, parent_rsrv; | |
428870ff | 704 | uint64_t deferred = 0; |
34dc7c2f | 705 | struct tempreserve *tr; |
428870ff | 706 | int retval = EDQUOT; |
34dc7c2f BB |
707 | int txgidx = txg & TXG_MASK; |
708 | int i; | |
709 | uint64_t ref_rsrv = 0; | |
710 | ||
711 | ASSERT3U(txg, !=, 0); | |
712 | ASSERT3S(asize, >, 0); | |
713 | ||
714 | mutex_enter(&dd->dd_lock); | |
715 | ||
716 | /* | |
717 | * Check against the dsl_dir's quota. We don't add in the delta | |
718 | * when checking for over-quota because they get one free hit. | |
719 | */ | |
720 | est_inflight = dsl_dir_space_towrite(dd); | |
721 | for (i = 0; i < TXG_SIZE; i++) | |
722 | est_inflight += dd->dd_tempreserved[i]; | |
b128c09f | 723 | used_on_disk = dd->dd_phys->dd_used_bytes; |
34dc7c2f BB |
724 | |
725 | /* | |
726 | * On the first iteration, fetch the dataset's used-on-disk and | |
727 | * refreservation values. Also, if checkrefquota is set, test if | |
728 | * allocating this space would exceed the dataset's refquota. | |
729 | */ | |
730 | if (first && tx->tx_objset) { | |
731 | int error; | |
428870ff | 732 | dsl_dataset_t *ds = tx->tx_objset->os_dsl_dataset; |
34dc7c2f BB |
733 | |
734 | error = dsl_dataset_check_quota(ds, checkrefquota, | |
735 | asize, est_inflight, &used_on_disk, &ref_rsrv); | |
736 | if (error) { | |
737 | mutex_exit(&dd->dd_lock); | |
738 | return (error); | |
739 | } | |
740 | } | |
741 | ||
742 | /* | |
743 | * If this transaction will result in a net free of space, | |
744 | * we want to let it through. | |
745 | */ | |
746 | if (ignorequota || netfree || dd->dd_phys->dd_quota == 0) | |
747 | quota = UINT64_MAX; | |
748 | else | |
749 | quota = dd->dd_phys->dd_quota; | |
750 | ||
751 | /* | |
428870ff BB |
752 | * Adjust the quota against the actual pool size at the root |
753 | * minus any outstanding deferred frees. | |
34dc7c2f BB |
754 | * To ensure that it's possible to remove files from a full |
755 | * pool without inducing transient overcommits, we throttle | |
756 | * netfree transactions against a quota that is slightly larger, | |
757 | * but still within the pool's allocation slop. In cases where | |
758 | * we're very close to full, this will allow a steady trickle of | |
759 | * removes to get through. | |
760 | */ | |
761 | if (dd->dd_parent == NULL) { | |
428870ff | 762 | spa_t *spa = dd->dd_pool->dp_spa; |
34dc7c2f | 763 | uint64_t poolsize = dsl_pool_adjustedsize(dd->dd_pool, netfree); |
428870ff BB |
764 | deferred = metaslab_class_get_deferred(spa_normal_class(spa)); |
765 | if (poolsize - deferred < quota) { | |
766 | quota = poolsize - deferred; | |
767 | retval = ENOSPC; | |
34dc7c2f BB |
768 | } |
769 | } | |
770 | ||
771 | /* | |
772 | * If they are requesting more space, and our current estimate | |
773 | * is over quota, they get to try again unless the actual | |
774 | * on-disk is over quota and there are no pending changes (which | |
775 | * may free up space for us). | |
776 | */ | |
428870ff BB |
777 | if (used_on_disk + est_inflight >= quota) { |
778 | if (est_inflight > 0 || used_on_disk < quota || | |
779 | (retval == ENOSPC && used_on_disk < quota + deferred)) | |
780 | retval = ERESTART; | |
34dc7c2f BB |
781 | dprintf_dd(dd, "failing: used=%lluK inflight = %lluK " |
782 | "quota=%lluK tr=%lluK err=%d\n", | |
783 | used_on_disk>>10, est_inflight>>10, | |
428870ff | 784 | quota>>10, asize>>10, retval); |
34dc7c2f | 785 | mutex_exit(&dd->dd_lock); |
428870ff | 786 | return (retval); |
34dc7c2f BB |
787 | } |
788 | ||
789 | /* We need to up our estimated delta before dropping dd_lock */ | |
790 | dd->dd_tempreserved[txgidx] += asize; | |
791 | ||
792 | parent_rsrv = parent_delta(dd, used_on_disk + est_inflight, | |
793 | asize - ref_rsrv); | |
794 | mutex_exit(&dd->dd_lock); | |
795 | ||
b8d06fca | 796 | tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE); |
34dc7c2f BB |
797 | tr->tr_ds = dd; |
798 | tr->tr_size = asize; | |
799 | list_insert_tail(tr_list, tr); | |
800 | ||
801 | /* see if it's OK with our parent */ | |
802 | if (dd->dd_parent && parent_rsrv) { | |
803 | boolean_t ismos = (dd->dd_phys->dd_head_dataset_obj == 0); | |
804 | ||
805 | return (dsl_dir_tempreserve_impl(dd->dd_parent, | |
806 | parent_rsrv, netfree, ismos, TRUE, tr_list, tx, FALSE)); | |
807 | } else { | |
808 | return (0); | |
809 | } | |
810 | } | |
811 | ||
812 | /* | |
813 | * Reserve space in this dsl_dir, to be used in this tx's txg. | |
814 | * After the space has been dirtied (and dsl_dir_willuse_space() | |
815 | * has been called), the reservation should be canceled, using | |
816 | * dsl_dir_tempreserve_clear(). | |
817 | */ | |
818 | int | |
819 | dsl_dir_tempreserve_space(dsl_dir_t *dd, uint64_t lsize, uint64_t asize, | |
820 | uint64_t fsize, uint64_t usize, void **tr_cookiep, dmu_tx_t *tx) | |
821 | { | |
822 | int err; | |
823 | list_t *tr_list; | |
824 | ||
825 | if (asize == 0) { | |
826 | *tr_cookiep = NULL; | |
827 | return (0); | |
828 | } | |
829 | ||
b8d06fca | 830 | tr_list = kmem_alloc(sizeof (list_t), KM_PUSHPAGE); |
34dc7c2f BB |
831 | list_create(tr_list, sizeof (struct tempreserve), |
832 | offsetof(struct tempreserve, tr_node)); | |
833 | ASSERT3S(asize, >, 0); | |
834 | ASSERT3S(fsize, >=, 0); | |
835 | ||
836 | err = arc_tempreserve_space(lsize, tx->tx_txg); | |
837 | if (err == 0) { | |
838 | struct tempreserve *tr; | |
839 | ||
b8d06fca | 840 | tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE); |
34dc7c2f BB |
841 | tr->tr_size = lsize; |
842 | list_insert_tail(tr_list, tr); | |
843 | ||
844 | err = dsl_pool_tempreserve_space(dd->dd_pool, asize, tx); | |
845 | } else { | |
846 | if (err == EAGAIN) { | |
847 | txg_delay(dd->dd_pool, tx->tx_txg, 1); | |
848 | err = ERESTART; | |
849 | } | |
850 | dsl_pool_memory_pressure(dd->dd_pool); | |
851 | } | |
852 | ||
853 | if (err == 0) { | |
854 | struct tempreserve *tr; | |
855 | ||
b8d06fca | 856 | tr = kmem_zalloc(sizeof (struct tempreserve), KM_PUSHPAGE); |
34dc7c2f BB |
857 | tr->tr_dp = dd->dd_pool; |
858 | tr->tr_size = asize; | |
859 | list_insert_tail(tr_list, tr); | |
860 | ||
861 | err = dsl_dir_tempreserve_impl(dd, asize, fsize >= asize, | |
862 | FALSE, asize > usize, tr_list, tx, TRUE); | |
863 | } | |
864 | ||
865 | if (err) | |
866 | dsl_dir_tempreserve_clear(tr_list, tx); | |
867 | else | |
868 | *tr_cookiep = tr_list; | |
869 | ||
870 | return (err); | |
871 | } | |
872 | ||
873 | /* | |
874 | * Clear a temporary reservation that we previously made with | |
875 | * dsl_dir_tempreserve_space(). | |
876 | */ | |
877 | void | |
878 | dsl_dir_tempreserve_clear(void *tr_cookie, dmu_tx_t *tx) | |
879 | { | |
880 | int txgidx = tx->tx_txg & TXG_MASK; | |
881 | list_t *tr_list = tr_cookie; | |
882 | struct tempreserve *tr; | |
883 | ||
884 | ASSERT3U(tx->tx_txg, !=, 0); | |
885 | ||
886 | if (tr_cookie == NULL) | |
887 | return; | |
888 | ||
c65aa5b2 | 889 | while ((tr = list_head(tr_list))) { |
34dc7c2f BB |
890 | if (tr->tr_dp) { |
891 | dsl_pool_tempreserve_clear(tr->tr_dp, tr->tr_size, tx); | |
892 | } else if (tr->tr_ds) { | |
893 | mutex_enter(&tr->tr_ds->dd_lock); | |
894 | ASSERT3U(tr->tr_ds->dd_tempreserved[txgidx], >=, | |
895 | tr->tr_size); | |
896 | tr->tr_ds->dd_tempreserved[txgidx] -= tr->tr_size; | |
897 | mutex_exit(&tr->tr_ds->dd_lock); | |
898 | } else { | |
899 | arc_tempreserve_clear(tr->tr_size); | |
900 | } | |
901 | list_remove(tr_list, tr); | |
902 | kmem_free(tr, sizeof (struct tempreserve)); | |
903 | } | |
904 | ||
905 | kmem_free(tr_list, sizeof (list_t)); | |
906 | } | |
907 | ||
908 | static void | |
909 | dsl_dir_willuse_space_impl(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) | |
910 | { | |
911 | int64_t parent_space; | |
912 | uint64_t est_used; | |
913 | ||
914 | mutex_enter(&dd->dd_lock); | |
915 | if (space > 0) | |
916 | dd->dd_space_towrite[tx->tx_txg & TXG_MASK] += space; | |
917 | ||
b128c09f | 918 | est_used = dsl_dir_space_towrite(dd) + dd->dd_phys->dd_used_bytes; |
34dc7c2f BB |
919 | parent_space = parent_delta(dd, est_used, space); |
920 | mutex_exit(&dd->dd_lock); | |
921 | ||
922 | /* Make sure that we clean up dd_space_to* */ | |
923 | dsl_dir_dirty(dd, tx); | |
924 | ||
925 | /* XXX this is potentially expensive and unnecessary... */ | |
926 | if (parent_space && dd->dd_parent) | |
927 | dsl_dir_willuse_space_impl(dd->dd_parent, parent_space, tx); | |
928 | } | |
929 | ||
930 | /* | |
931 | * Call in open context when we think we're going to write/free space, | |
932 | * eg. when dirtying data. Be conservative (ie. OK to write less than | |
933 | * this or free more than this, but don't write more or free less). | |
934 | */ | |
935 | void | |
936 | dsl_dir_willuse_space(dsl_dir_t *dd, int64_t space, dmu_tx_t *tx) | |
937 | { | |
938 | dsl_pool_willuse_space(dd->dd_pool, space, tx); | |
939 | dsl_dir_willuse_space_impl(dd, space, tx); | |
940 | } | |
941 | ||
942 | /* call from syncing context when we actually write/free space for this dd */ | |
943 | void | |
b128c09f | 944 | dsl_dir_diduse_space(dsl_dir_t *dd, dd_used_t type, |
34dc7c2f BB |
945 | int64_t used, int64_t compressed, int64_t uncompressed, dmu_tx_t *tx) |
946 | { | |
947 | int64_t accounted_delta; | |
b128c09f | 948 | boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); |
34dc7c2f BB |
949 | |
950 | ASSERT(dmu_tx_is_syncing(tx)); | |
b128c09f | 951 | ASSERT(type < DD_USED_NUM); |
34dc7c2f BB |
952 | |
953 | dsl_dir_dirty(dd, tx); | |
954 | ||
b128c09f BB |
955 | if (needlock) |
956 | mutex_enter(&dd->dd_lock); | |
957 | accounted_delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, used); | |
958 | ASSERT(used >= 0 || dd->dd_phys->dd_used_bytes >= -used); | |
34dc7c2f BB |
959 | ASSERT(compressed >= 0 || |
960 | dd->dd_phys->dd_compressed_bytes >= -compressed); | |
961 | ASSERT(uncompressed >= 0 || | |
962 | dd->dd_phys->dd_uncompressed_bytes >= -uncompressed); | |
b128c09f | 963 | dd->dd_phys->dd_used_bytes += used; |
34dc7c2f BB |
964 | dd->dd_phys->dd_uncompressed_bytes += uncompressed; |
965 | dd->dd_phys->dd_compressed_bytes += compressed; | |
b128c09f BB |
966 | |
967 | if (dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) { | |
968 | ASSERT(used > 0 || | |
969 | dd->dd_phys->dd_used_breakdown[type] >= -used); | |
970 | dd->dd_phys->dd_used_breakdown[type] += used; | |
971 | #ifdef DEBUG | |
d6320ddb BB |
972 | { |
973 | dd_used_t t; | |
974 | uint64_t u = 0; | |
975 | for (t = 0; t < DD_USED_NUM; t++) | |
976 | u += dd->dd_phys->dd_used_breakdown[t]; | |
977 | ASSERT3U(u, ==, dd->dd_phys->dd_used_bytes); | |
978 | } | |
b128c09f BB |
979 | #endif |
980 | } | |
981 | if (needlock) | |
982 | mutex_exit(&dd->dd_lock); | |
34dc7c2f BB |
983 | |
984 | if (dd->dd_parent != NULL) { | |
b128c09f | 985 | dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, |
34dc7c2f | 986 | accounted_delta, compressed, uncompressed, tx); |
b128c09f BB |
987 | dsl_dir_transfer_space(dd->dd_parent, |
988 | used - accounted_delta, | |
989 | DD_USED_CHILD_RSRV, DD_USED_CHILD, tx); | |
34dc7c2f BB |
990 | } |
991 | } | |
992 | ||
b128c09f BB |
993 | void |
994 | dsl_dir_transfer_space(dsl_dir_t *dd, int64_t delta, | |
995 | dd_used_t oldtype, dd_used_t newtype, dmu_tx_t *tx) | |
996 | { | |
997 | boolean_t needlock = !MUTEX_HELD(&dd->dd_lock); | |
998 | ||
999 | ASSERT(dmu_tx_is_syncing(tx)); | |
1000 | ASSERT(oldtype < DD_USED_NUM); | |
1001 | ASSERT(newtype < DD_USED_NUM); | |
1002 | ||
1003 | if (delta == 0 || !(dd->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN)) | |
1004 | return; | |
1005 | ||
1006 | dsl_dir_dirty(dd, tx); | |
1007 | if (needlock) | |
1008 | mutex_enter(&dd->dd_lock); | |
1009 | ASSERT(delta > 0 ? | |
1010 | dd->dd_phys->dd_used_breakdown[oldtype] >= delta : | |
1011 | dd->dd_phys->dd_used_breakdown[newtype] >= -delta); | |
1012 | ASSERT(dd->dd_phys->dd_used_bytes >= ABS(delta)); | |
1013 | dd->dd_phys->dd_used_breakdown[oldtype] -= delta; | |
1014 | dd->dd_phys->dd_used_breakdown[newtype] += delta; | |
1015 | if (needlock) | |
1016 | mutex_exit(&dd->dd_lock); | |
1017 | } | |
1018 | ||
34dc7c2f BB |
1019 | static int |
1020 | dsl_dir_set_quota_check(void *arg1, void *arg2, dmu_tx_t *tx) | |
1021 | { | |
428870ff BB |
1022 | dsl_dataset_t *ds = arg1; |
1023 | dsl_dir_t *dd = ds->ds_dir; | |
1024 | dsl_prop_setarg_t *psa = arg2; | |
1025 | int err; | |
34dc7c2f BB |
1026 | uint64_t towrite; |
1027 | ||
428870ff BB |
1028 | if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0) |
1029 | return (err); | |
1030 | ||
1031 | if (psa->psa_effective_value == 0) | |
34dc7c2f BB |
1032 | return (0); |
1033 | ||
1034 | mutex_enter(&dd->dd_lock); | |
1035 | /* | |
1036 | * If we are doing the preliminary check in open context, and | |
1037 | * there are pending changes, then don't fail it, since the | |
1038 | * pending changes could under-estimate the amount of space to be | |
1039 | * freed up. | |
1040 | */ | |
1041 | towrite = dsl_dir_space_towrite(dd); | |
1042 | if ((dmu_tx_is_syncing(tx) || towrite == 0) && | |
428870ff BB |
1043 | (psa->psa_effective_value < dd->dd_phys->dd_reserved || |
1044 | psa->psa_effective_value < dd->dd_phys->dd_used_bytes + towrite)) { | |
34dc7c2f BB |
1045 | err = ENOSPC; |
1046 | } | |
1047 | mutex_exit(&dd->dd_lock); | |
1048 | return (err); | |
1049 | } | |
1050 | ||
428870ff BB |
1051 | extern dsl_syncfunc_t dsl_prop_set_sync; |
1052 | ||
34dc7c2f | 1053 | static void |
428870ff | 1054 | dsl_dir_set_quota_sync(void *arg1, void *arg2, dmu_tx_t *tx) |
34dc7c2f | 1055 | { |
428870ff BB |
1056 | dsl_dataset_t *ds = arg1; |
1057 | dsl_dir_t *dd = ds->ds_dir; | |
1058 | dsl_prop_setarg_t *psa = arg2; | |
1059 | uint64_t effective_value = psa->psa_effective_value; | |
1060 | ||
1061 | dsl_prop_set_sync(ds, psa, tx); | |
1062 | DSL_PROP_CHECK_PREDICTION(dd, psa); | |
34dc7c2f BB |
1063 | |
1064 | dmu_buf_will_dirty(dd->dd_dbuf, tx); | |
1065 | ||
1066 | mutex_enter(&dd->dd_lock); | |
428870ff | 1067 | dd->dd_phys->dd_quota = effective_value; |
34dc7c2f | 1068 | mutex_exit(&dd->dd_lock); |
34dc7c2f BB |
1069 | } |
1070 | ||
1071 | int | |
428870ff | 1072 | dsl_dir_set_quota(const char *ddname, zprop_source_t source, uint64_t quota) |
34dc7c2f BB |
1073 | { |
1074 | dsl_dir_t *dd; | |
428870ff BB |
1075 | dsl_dataset_t *ds; |
1076 | dsl_prop_setarg_t psa; | |
34dc7c2f BB |
1077 | int err; |
1078 | ||
428870ff BB |
1079 | dsl_prop_setarg_init_uint64(&psa, "quota", source, "a); |
1080 | ||
1081 | err = dsl_dataset_hold(ddname, FTAG, &ds); | |
34dc7c2f BB |
1082 | if (err) |
1083 | return (err); | |
1084 | ||
428870ff BB |
1085 | err = dsl_dir_open(ddname, FTAG, &dd, NULL); |
1086 | if (err) { | |
1087 | dsl_dataset_rele(ds, FTAG); | |
1088 | return (err); | |
34dc7c2f | 1089 | } |
428870ff BB |
1090 | |
1091 | ASSERT(ds->ds_dir == dd); | |
1092 | ||
1093 | /* | |
1094 | * If someone removes a file, then tries to set the quota, we want to | |
1095 | * make sure the file freeing takes effect. | |
1096 | */ | |
1097 | txg_wait_open(dd->dd_pool, 0); | |
1098 | ||
1099 | err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_quota_check, | |
1100 | dsl_dir_set_quota_sync, ds, &psa, 0); | |
1101 | ||
34dc7c2f | 1102 | dsl_dir_close(dd, FTAG); |
428870ff | 1103 | dsl_dataset_rele(ds, FTAG); |
34dc7c2f BB |
1104 | return (err); |
1105 | } | |
1106 | ||
1107 | int | |
1108 | dsl_dir_set_reservation_check(void *arg1, void *arg2, dmu_tx_t *tx) | |
1109 | { | |
428870ff BB |
1110 | dsl_dataset_t *ds = arg1; |
1111 | dsl_dir_t *dd = ds->ds_dir; | |
1112 | dsl_prop_setarg_t *psa = arg2; | |
1113 | uint64_t effective_value; | |
34dc7c2f | 1114 | uint64_t used, avail; |
428870ff BB |
1115 | int err; |
1116 | ||
1117 | if ((err = dsl_prop_predict_sync(ds->ds_dir, psa)) != 0) | |
1118 | return (err); | |
1119 | ||
1120 | effective_value = psa->psa_effective_value; | |
34dc7c2f BB |
1121 | |
1122 | /* | |
1123 | * If we are doing the preliminary check in open context, the | |
1124 | * space estimates may be inaccurate. | |
1125 | */ | |
1126 | if (!dmu_tx_is_syncing(tx)) | |
1127 | return (0); | |
1128 | ||
1129 | mutex_enter(&dd->dd_lock); | |
b128c09f | 1130 | used = dd->dd_phys->dd_used_bytes; |
34dc7c2f BB |
1131 | mutex_exit(&dd->dd_lock); |
1132 | ||
1133 | if (dd->dd_parent) { | |
1134 | avail = dsl_dir_space_available(dd->dd_parent, | |
1135 | NULL, 0, FALSE); | |
1136 | } else { | |
1137 | avail = dsl_pool_adjustedsize(dd->dd_pool, B_FALSE) - used; | |
1138 | } | |
1139 | ||
428870ff BB |
1140 | if (MAX(used, effective_value) > MAX(used, dd->dd_phys->dd_reserved)) { |
1141 | uint64_t delta = MAX(used, effective_value) - | |
d164b209 BB |
1142 | MAX(used, dd->dd_phys->dd_reserved); |
1143 | ||
1144 | if (delta > avail) | |
1145 | return (ENOSPC); | |
1146 | if (dd->dd_phys->dd_quota > 0 && | |
428870ff | 1147 | effective_value > dd->dd_phys->dd_quota) |
d164b209 BB |
1148 | return (ENOSPC); |
1149 | } | |
1150 | ||
34dc7c2f BB |
1151 | return (0); |
1152 | } | |
1153 | ||
34dc7c2f | 1154 | static void |
428870ff | 1155 | dsl_dir_set_reservation_sync(void *arg1, void *arg2, dmu_tx_t *tx) |
34dc7c2f | 1156 | { |
428870ff BB |
1157 | dsl_dataset_t *ds = arg1; |
1158 | dsl_dir_t *dd = ds->ds_dir; | |
1159 | dsl_prop_setarg_t *psa = arg2; | |
1160 | uint64_t effective_value = psa->psa_effective_value; | |
34dc7c2f BB |
1161 | uint64_t used; |
1162 | int64_t delta; | |
1163 | ||
428870ff BB |
1164 | dsl_prop_set_sync(ds, psa, tx); |
1165 | DSL_PROP_CHECK_PREDICTION(dd, psa); | |
1166 | ||
34dc7c2f BB |
1167 | dmu_buf_will_dirty(dd->dd_dbuf, tx); |
1168 | ||
1169 | mutex_enter(&dd->dd_lock); | |
b128c09f | 1170 | used = dd->dd_phys->dd_used_bytes; |
428870ff | 1171 | delta = MAX(used, effective_value) - |
34dc7c2f | 1172 | MAX(used, dd->dd_phys->dd_reserved); |
428870ff | 1173 | dd->dd_phys->dd_reserved = effective_value; |
34dc7c2f BB |
1174 | |
1175 | if (dd->dd_parent != NULL) { | |
1176 | /* Roll up this additional usage into our ancestors */ | |
b128c09f BB |
1177 | dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, |
1178 | delta, 0, 0, tx); | |
34dc7c2f | 1179 | } |
b128c09f | 1180 | mutex_exit(&dd->dd_lock); |
34dc7c2f BB |
1181 | } |
1182 | ||
1183 | int | |
428870ff BB |
1184 | dsl_dir_set_reservation(const char *ddname, zprop_source_t source, |
1185 | uint64_t reservation) | |
34dc7c2f BB |
1186 | { |
1187 | dsl_dir_t *dd; | |
428870ff BB |
1188 | dsl_dataset_t *ds; |
1189 | dsl_prop_setarg_t psa; | |
34dc7c2f BB |
1190 | int err; |
1191 | ||
428870ff BB |
1192 | dsl_prop_setarg_init_uint64(&psa, "reservation", source, &reservation); |
1193 | ||
1194 | err = dsl_dataset_hold(ddname, FTAG, &ds); | |
34dc7c2f BB |
1195 | if (err) |
1196 | return (err); | |
428870ff BB |
1197 | |
1198 | err = dsl_dir_open(ddname, FTAG, &dd, NULL); | |
1199 | if (err) { | |
1200 | dsl_dataset_rele(ds, FTAG); | |
1201 | return (err); | |
1202 | } | |
1203 | ||
1204 | ASSERT(ds->ds_dir == dd); | |
1205 | ||
34dc7c2f | 1206 | err = dsl_sync_task_do(dd->dd_pool, dsl_dir_set_reservation_check, |
428870ff BB |
1207 | dsl_dir_set_reservation_sync, ds, &psa, 0); |
1208 | ||
34dc7c2f | 1209 | dsl_dir_close(dd, FTAG); |
428870ff | 1210 | dsl_dataset_rele(ds, FTAG); |
34dc7c2f BB |
1211 | return (err); |
1212 | } | |
1213 | ||
1214 | static dsl_dir_t * | |
1215 | closest_common_ancestor(dsl_dir_t *ds1, dsl_dir_t *ds2) | |
1216 | { | |
1217 | for (; ds1; ds1 = ds1->dd_parent) { | |
1218 | dsl_dir_t *dd; | |
1219 | for (dd = ds2; dd; dd = dd->dd_parent) { | |
1220 | if (ds1 == dd) | |
1221 | return (dd); | |
1222 | } | |
1223 | } | |
1224 | return (NULL); | |
1225 | } | |
1226 | ||
1227 | /* | |
1228 | * If delta is applied to dd, how much of that delta would be applied to | |
1229 | * ancestor? Syncing context only. | |
1230 | */ | |
1231 | static int64_t | |
1232 | would_change(dsl_dir_t *dd, int64_t delta, dsl_dir_t *ancestor) | |
1233 | { | |
1234 | if (dd == ancestor) | |
1235 | return (delta); | |
1236 | ||
1237 | mutex_enter(&dd->dd_lock); | |
b128c09f | 1238 | delta = parent_delta(dd, dd->dd_phys->dd_used_bytes, delta); |
34dc7c2f BB |
1239 | mutex_exit(&dd->dd_lock); |
1240 | return (would_change(dd->dd_parent, delta, ancestor)); | |
1241 | } | |
1242 | ||
1243 | struct renamearg { | |
1244 | dsl_dir_t *newparent; | |
1245 | const char *mynewname; | |
1246 | }; | |
1247 | ||
34dc7c2f BB |
1248 | static int |
1249 | dsl_dir_rename_check(void *arg1, void *arg2, dmu_tx_t *tx) | |
1250 | { | |
1251 | dsl_dir_t *dd = arg1; | |
1252 | struct renamearg *ra = arg2; | |
1253 | dsl_pool_t *dp = dd->dd_pool; | |
1254 | objset_t *mos = dp->dp_meta_objset; | |
1255 | int err; | |
1256 | uint64_t val; | |
1257 | ||
428870ff BB |
1258 | /* |
1259 | * There should only be one reference, from dmu_objset_rename(). | |
1260 | * Fleeting holds are also possible (eg, from "zfs list" getting | |
1261 | * stats), but any that are present in open context will likely | |
1262 | * be gone by syncing context, so only fail from syncing | |
1263 | * context. | |
1264 | */ | |
1265 | if (dmu_tx_is_syncing(tx) && dmu_buf_refcount(dd->dd_dbuf) > 1) | |
34dc7c2f BB |
1266 | return (EBUSY); |
1267 | ||
1268 | /* check for existing name */ | |
1269 | err = zap_lookup(mos, ra->newparent->dd_phys->dd_child_dir_zapobj, | |
1270 | ra->mynewname, 8, 1, &val); | |
1271 | if (err == 0) | |
1272 | return (EEXIST); | |
1273 | if (err != ENOENT) | |
1274 | return (err); | |
1275 | ||
1276 | if (ra->newparent != dd->dd_parent) { | |
1277 | /* is there enough space? */ | |
1278 | uint64_t myspace = | |
b128c09f | 1279 | MAX(dd->dd_phys->dd_used_bytes, dd->dd_phys->dd_reserved); |
34dc7c2f BB |
1280 | |
1281 | /* no rename into our descendant */ | |
1282 | if (closest_common_ancestor(dd, ra->newparent) == dd) | |
1283 | return (EINVAL); | |
1284 | ||
c65aa5b2 BB |
1285 | if ((err = dsl_dir_transfer_possible(dd->dd_parent, |
1286 | ra->newparent, myspace))) | |
34dc7c2f BB |
1287 | return (err); |
1288 | } | |
1289 | ||
1290 | return (0); | |
1291 | } | |
1292 | ||
1293 | static void | |
428870ff | 1294 | dsl_dir_rename_sync(void *arg1, void *arg2, dmu_tx_t *tx) |
34dc7c2f BB |
1295 | { |
1296 | dsl_dir_t *dd = arg1; | |
1297 | struct renamearg *ra = arg2; | |
1298 | dsl_pool_t *dp = dd->dd_pool; | |
1299 | objset_t *mos = dp->dp_meta_objset; | |
1300 | int err; | |
1301 | ||
1302 | ASSERT(dmu_buf_refcount(dd->dd_dbuf) <= 2); | |
1303 | ||
1304 | if (ra->newparent != dd->dd_parent) { | |
b128c09f BB |
1305 | dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD, |
1306 | -dd->dd_phys->dd_used_bytes, | |
34dc7c2f BB |
1307 | -dd->dd_phys->dd_compressed_bytes, |
1308 | -dd->dd_phys->dd_uncompressed_bytes, tx); | |
b128c09f BB |
1309 | dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD, |
1310 | dd->dd_phys->dd_used_bytes, | |
34dc7c2f BB |
1311 | dd->dd_phys->dd_compressed_bytes, |
1312 | dd->dd_phys->dd_uncompressed_bytes, tx); | |
b128c09f BB |
1313 | |
1314 | if (dd->dd_phys->dd_reserved > dd->dd_phys->dd_used_bytes) { | |
1315 | uint64_t unused_rsrv = dd->dd_phys->dd_reserved - | |
1316 | dd->dd_phys->dd_used_bytes; | |
1317 | ||
1318 | dsl_dir_diduse_space(dd->dd_parent, DD_USED_CHILD_RSRV, | |
1319 | -unused_rsrv, 0, 0, tx); | |
1320 | dsl_dir_diduse_space(ra->newparent, DD_USED_CHILD_RSRV, | |
1321 | unused_rsrv, 0, 0, tx); | |
1322 | } | |
34dc7c2f BB |
1323 | } |
1324 | ||
1325 | dmu_buf_will_dirty(dd->dd_dbuf, tx); | |
1326 | ||
1327 | /* remove from old parent zapobj */ | |
1328 | err = zap_remove(mos, dd->dd_parent->dd_phys->dd_child_dir_zapobj, | |
1329 | dd->dd_myname, tx); | |
1330 | ASSERT3U(err, ==, 0); | |
1331 | ||
1332 | (void) strcpy(dd->dd_myname, ra->mynewname); | |
1333 | dsl_dir_close(dd->dd_parent, dd); | |
1334 | dd->dd_phys->dd_parent_obj = ra->newparent->dd_object; | |
1335 | VERIFY(0 == dsl_dir_open_obj(dd->dd_pool, | |
1336 | ra->newparent->dd_object, NULL, dd, &dd->dd_parent)); | |
1337 | ||
1338 | /* add to new parent zapobj */ | |
1339 | err = zap_add(mos, ra->newparent->dd_phys->dd_child_dir_zapobj, | |
1340 | dd->dd_myname, 8, 1, &dd->dd_object, tx); | |
1341 | ASSERT3U(err, ==, 0); | |
1342 | ||
428870ff BB |
1343 | spa_history_log_internal(LOG_DS_RENAME, dd->dd_pool->dp_spa, |
1344 | tx, "dataset = %llu", dd->dd_phys->dd_head_dataset_obj); | |
34dc7c2f BB |
1345 | } |
1346 | ||
1347 | int | |
1348 | dsl_dir_rename(dsl_dir_t *dd, const char *newname) | |
1349 | { | |
1350 | struct renamearg ra; | |
1351 | int err; | |
1352 | ||
1353 | /* new parent should exist */ | |
1354 | err = dsl_dir_open(newname, FTAG, &ra.newparent, &ra.mynewname); | |
1355 | if (err) | |
1356 | return (err); | |
1357 | ||
1358 | /* can't rename to different pool */ | |
1359 | if (dd->dd_pool != ra.newparent->dd_pool) { | |
1360 | err = ENXIO; | |
1361 | goto out; | |
1362 | } | |
1363 | ||
1364 | /* new name should not already exist */ | |
1365 | if (ra.mynewname == NULL) { | |
1366 | err = EEXIST; | |
1367 | goto out; | |
1368 | } | |
1369 | ||
1370 | err = dsl_sync_task_do(dd->dd_pool, | |
1371 | dsl_dir_rename_check, dsl_dir_rename_sync, dd, &ra, 3); | |
1372 | ||
1373 | out: | |
1374 | dsl_dir_close(ra.newparent, FTAG); | |
1375 | return (err); | |
1376 | } | |
1377 | ||
1378 | int | |
1379 | dsl_dir_transfer_possible(dsl_dir_t *sdd, dsl_dir_t *tdd, uint64_t space) | |
1380 | { | |
1381 | dsl_dir_t *ancestor; | |
1382 | int64_t adelta; | |
1383 | uint64_t avail; | |
1384 | ||
1385 | ancestor = closest_common_ancestor(sdd, tdd); | |
1386 | adelta = would_change(sdd, -space, ancestor); | |
1387 | avail = dsl_dir_space_available(tdd, ancestor, adelta, FALSE); | |
1388 | if (avail < space) | |
1389 | return (ENOSPC); | |
1390 | ||
1391 | return (0); | |
1392 | } | |
428870ff BB |
1393 | |
1394 | timestruc_t | |
1395 | dsl_dir_snap_cmtime(dsl_dir_t *dd) | |
1396 | { | |
1397 | timestruc_t t; | |
1398 | ||
1399 | mutex_enter(&dd->dd_lock); | |
1400 | t = dd->dd_snap_cmtime; | |
1401 | mutex_exit(&dd->dd_lock); | |
1402 | ||
1403 | return (t); | |
1404 | } | |
1405 | ||
1406 | void | |
1407 | dsl_dir_snap_cmtime_update(dsl_dir_t *dd) | |
1408 | { | |
1409 | timestruc_t t; | |
1410 | ||
1411 | gethrestime(&t); | |
1412 | mutex_enter(&dd->dd_lock); | |
1413 | dd->dd_snap_cmtime = t; | |
1414 | mutex_exit(&dd->dd_lock); | |
1415 | } | |
c28b2279 BB |
1416 | |
1417 | #if defined(_KERNEL) && defined(HAVE_SPL) | |
1418 | EXPORT_SYMBOL(dsl_dir_set_quota); | |
1419 | EXPORT_SYMBOL(dsl_dir_set_reservation); | |
1420 | EXPORT_SYMBOL(dsl_dir_open); | |
1421 | EXPORT_SYMBOL(dsl_dir_close); | |
1422 | #endif |