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