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