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