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