FreeBSD ZFS
The Zettabyte File System

dsl_pool.c

Go to the documentation of this file.
00001 /*
00002  * CDDL HEADER START
00003  *
00004  * The contents of this file are subject to the terms of the
00005  * Common Development and Distribution License (the "License").
00006  * You may not use this file except in compliance with the License.
00007  *
00008  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
00009  * or http://www.opensolaris.org/os/licensing.
00010  * See the License for the specific language governing permissions
00011  * and limitations under the License.
00012  *
00013  * When distributing Covered Code, include this CDDL HEADER in each
00014  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
00015  * If applicable, add the following below this CDDL HEADER, with the
00016  * fields enclosed by brackets "[]" replaced with your own identifying
00017  * information: Portions Copyright [yyyy] [name of copyright owner]
00018  *
00019  * CDDL HEADER END
00020  */
00021 /*
00022  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
00023  * Copyright (c) 2012 by Delphix. All rights reserved.
00024  */
00025 
00026 #include <sys/dsl_pool.h>
00027 #include <sys/dsl_dataset.h>
00028 #include <sys/dsl_prop.h>
00029 #include <sys/dsl_dir.h>
00030 #include <sys/dsl_synctask.h>
00031 #include <sys/dsl_scan.h>
00032 #include <sys/dnode.h>
00033 #include <sys/dmu_tx.h>
00034 #include <sys/dmu_objset.h>
00035 #include <sys/arc.h>
00036 #include <sys/zap.h>
00037 #include <sys/zio.h>
00038 #include <sys/zfs_context.h>
00039 #include <sys/fs/zfs.h>
00040 #include <sys/zfs_znode.h>
00041 #include <sys/spa_impl.h>
00042 #include <sys/dsl_deadlist.h>
00043 #include <sys/bptree.h>
00044 #include <sys/zfeature.h>
00045 #include <sys/zil_impl.h>
00046 
00048 /* \{ */
00049 int zfs_no_write_throttle = 0;
00050 int zfs_write_limit_shift = 3;                  
00051 int zfs_txg_synctime_ms = 1000;         
00053 uint64_t zfs_write_limit_min = 32 << 20;        
00054 uint64_t zfs_write_limit_max = 0;               
00055 uint64_t zfs_write_limit_inflated = 0;
00056 uint64_t zfs_write_limit_override = 0;
00057 /* \} */
00058 
00059 kmutex_t zfs_write_limit_lock;
00060 
00061 static pgcnt_t old_physmem = 0;
00062 
00063 SYSCTL_DECL(_vfs_zfs);
00064 TUNABLE_INT("vfs.zfs.no_write_throttle", &zfs_no_write_throttle);
00065 SYSCTL_INT(_vfs_zfs, OID_AUTO, no_write_throttle, CTLFLAG_RDTUN,
00066     &zfs_no_write_throttle, 0, "");
00067 TUNABLE_INT("vfs.zfs.write_limit_shift", &zfs_write_limit_shift);
00068 SYSCTL_INT(_vfs_zfs, OID_AUTO, write_limit_shift, CTLFLAG_RDTUN,
00069     &zfs_write_limit_shift, 0, "2^N of physical memory");
00070 SYSCTL_DECL(_vfs_zfs_txg);
00071 TUNABLE_INT("vfs.zfs.txg.synctime_ms", &zfs_txg_synctime_ms);
00072 SYSCTL_INT(_vfs_zfs_txg, OID_AUTO, synctime_ms, CTLFLAG_RDTUN,
00073     &zfs_txg_synctime_ms, 0, "Target milliseconds to sync a txg");
00074 
00075 TUNABLE_QUAD("vfs.zfs.write_limit_min", &zfs_write_limit_min);
00076 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_min, CTLFLAG_RDTUN,
00077     &zfs_write_limit_min, 0, "Minimum write limit");
00078 TUNABLE_QUAD("vfs.zfs.write_limit_max", &zfs_write_limit_max);
00079 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_max, CTLFLAG_RDTUN,
00080     &zfs_write_limit_max, 0, "Maximum data payload per txg");
00081 TUNABLE_QUAD("vfs.zfs.write_limit_inflated", &zfs_write_limit_inflated);
00082 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_inflated, CTLFLAG_RDTUN,
00083     &zfs_write_limit_inflated, 0, "");
00084 TUNABLE_QUAD("vfs.zfs.write_limit_override", &zfs_write_limit_override);
00085 SYSCTL_UQUAD(_vfs_zfs, OID_AUTO, write_limit_override, CTLFLAG_RDTUN,
00086     &zfs_write_limit_override, 0, "");
00087 
00088 int
00089 dsl_pool_open_special_dir(dsl_pool_t *dp, const char *name, dsl_dir_t **ddp)
00090 {
00091         uint64_t obj;
00092         int err;
00093 
00094         err = zap_lookup(dp->dp_meta_objset,
00095             dp->dp_root_dir->dd_phys->dd_child_dir_zapobj,
00096             name, sizeof (obj), 1, &obj);
00097         if (err)
00098                 return (err);
00099 
00100         return (dsl_dir_open_obj(dp, obj, name, dp, ddp));
00101 }
00102 
00103 static dsl_pool_t *
00104 dsl_pool_open_impl(spa_t *spa, uint64_t txg)
00105 {
00106         dsl_pool_t *dp;
00107         blkptr_t *bp = spa_get_rootblkptr(spa);
00108 
00109         dp = kmem_zalloc(sizeof (dsl_pool_t), KM_SLEEP);
00110         dp->dp_spa = spa;
00111         dp->dp_meta_rootbp = *bp;
00112         rw_init(&dp->dp_config_rwlock, NULL, RW_DEFAULT, NULL);
00113         dp->dp_write_limit = zfs_write_limit_min;
00114         txg_init(dp, txg);
00115 
00116         txg_list_create(&dp->dp_dirty_datasets,
00117             offsetof(dsl_dataset_t, ds_dirty_link));
00118         txg_list_create(&dp->dp_dirty_zilogs,
00119             offsetof(zilog_t, zl_dirty_link));
00120         txg_list_create(&dp->dp_dirty_dirs,
00121             offsetof(dsl_dir_t, dd_dirty_link));
00122         txg_list_create(&dp->dp_sync_tasks,
00123             offsetof(dsl_sync_task_group_t, dstg_node));
00124 
00125         mutex_init(&dp->dp_lock, NULL, MUTEX_DEFAULT, NULL);
00126 
00127         dp->dp_vnrele_taskq = taskq_create("zfs_vn_rele_taskq", 1, minclsyspri,
00128             1, 4, 0);
00129 
00130         return (dp);
00131 }
00132 
00133 int
00134 dsl_pool_init(spa_t *spa, uint64_t txg, dsl_pool_t **dpp)
00135 {
00136         int err;
00137         dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
00138 
00139         err = dmu_objset_open_impl(spa, NULL, &dp->dp_meta_rootbp,
00140             &dp->dp_meta_objset);
00141         if (err != 0)
00142                 dsl_pool_close(dp);
00143         else
00144                 *dpp = dp;
00145 
00146         return (err);
00147 }
00148 
00149 int
00150 dsl_pool_open(dsl_pool_t *dp)
00151 {
00152         int err;
00153         dsl_dir_t *dd;
00154         dsl_dataset_t *ds;
00155         uint64_t obj;
00156 
00157         rw_enter(&dp->dp_config_rwlock, RW_WRITER);
00158         err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
00159             DMU_POOL_ROOT_DATASET, sizeof (uint64_t), 1,
00160             &dp->dp_root_dir_obj);
00161         if (err)
00162                 goto out;
00163 
00164         err = dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
00165             NULL, dp, &dp->dp_root_dir);
00166         if (err)
00167                 goto out;
00168 
00169         err = dsl_pool_open_special_dir(dp, MOS_DIR_NAME, &dp->dp_mos_dir);
00170         if (err)
00171                 goto out;
00172 
00173         if (spa_version(dp->dp_spa) >= SPA_VERSION_ORIGIN) {
00174                 err = dsl_pool_open_special_dir(dp, ORIGIN_DIR_NAME, &dd);
00175                 if (err)
00176                         goto out;
00177                 err = dsl_dataset_hold_obj(dp, dd->dd_phys->dd_head_dataset_obj,
00178                     FTAG, &ds);
00179                 if (err == 0) {
00180                         err = dsl_dataset_hold_obj(dp,
00181                             ds->ds_phys->ds_prev_snap_obj, dp,
00182                             &dp->dp_origin_snap);
00183                         dsl_dataset_rele(ds, FTAG);
00184                 }
00185                 dsl_dir_close(dd, dp);
00186                 if (err)
00187                         goto out;
00188         }
00189 
00190         if (spa_version(dp->dp_spa) >= SPA_VERSION_DEADLISTS) {
00191                 err = dsl_pool_open_special_dir(dp, FREE_DIR_NAME,
00192                     &dp->dp_free_dir);
00193                 if (err)
00194                         goto out;
00195 
00196                 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
00197                     DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj);
00198                 if (err)
00199                         goto out;
00200                 VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
00201                     dp->dp_meta_objset, obj));
00202         }
00203 
00204         if (spa_feature_is_active(dp->dp_spa,
00205             &spa_feature_table[SPA_FEATURE_ASYNC_DESTROY])) {
00206                 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
00207                     DMU_POOL_BPTREE_OBJ, sizeof (uint64_t), 1,
00208                     &dp->dp_bptree_obj);
00209                 if (err != 0)
00210                         goto out;
00211         }
00212 
00213         if (spa_feature_is_active(dp->dp_spa,
00214             &spa_feature_table[SPA_FEATURE_EMPTY_BPOBJ])) {
00215                 err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
00216                     DMU_POOL_EMPTY_BPOBJ, sizeof (uint64_t), 1,
00217                     &dp->dp_empty_bpobj);
00218                 if (err != 0)
00219                         goto out;
00220         }
00221 
00222         err = zap_lookup(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
00223             DMU_POOL_TMP_USERREFS, sizeof (uint64_t), 1,
00224             &dp->dp_tmp_userrefs_obj);
00225         if (err == ENOENT)
00226                 err = 0;
00227         if (err)
00228                 goto out;
00229 
00230         err = dsl_scan_init(dp, dp->dp_tx.tx_open_txg);
00231 
00232 out:
00233         rw_exit(&dp->dp_config_rwlock);
00234         return (err);
00235 }
00236 
00237 void
00238 dsl_pool_close(dsl_pool_t *dp)
00239 {
00240         /* drop our references from dsl_pool_open() */
00241 
00242         /*
00243          * Since we held the origin_snap from "syncing" context (which
00244          * includes pool-opening context), it actually only got a "ref"
00245          * and not a hold, so just drop that here.
00246          */
00247         if (dp->dp_origin_snap)
00248                 dsl_dataset_drop_ref(dp->dp_origin_snap, dp);
00249         if (dp->dp_mos_dir)
00250                 dsl_dir_close(dp->dp_mos_dir, dp);
00251         if (dp->dp_free_dir)
00252                 dsl_dir_close(dp->dp_free_dir, dp);
00253         if (dp->dp_root_dir)
00254                 dsl_dir_close(dp->dp_root_dir, dp);
00255 
00256         bpobj_close(&dp->dp_free_bpobj);
00257 
00258         /* undo the dmu_objset_open_impl(mos) from dsl_pool_open() */
00259         if (dp->dp_meta_objset)
00260                 dmu_objset_evict(dp->dp_meta_objset);
00261 
00262         txg_list_destroy(&dp->dp_dirty_datasets);
00263         txg_list_destroy(&dp->dp_dirty_zilogs);
00264         txg_list_destroy(&dp->dp_sync_tasks);
00265         txg_list_destroy(&dp->dp_dirty_dirs);
00266 
00267         arc_flush(dp->dp_spa);
00268         txg_fini(dp);
00269         dsl_scan_fini(dp);
00270         rw_destroy(&dp->dp_config_rwlock);
00271         mutex_destroy(&dp->dp_lock);
00272         taskq_destroy(dp->dp_vnrele_taskq);
00273         if (dp->dp_blkstats)
00274                 kmem_free(dp->dp_blkstats, sizeof (zfs_all_blkstats_t));
00275         kmem_free(dp, sizeof (dsl_pool_t));
00276 }
00277 
00278 dsl_pool_t *
00279 dsl_pool_create(spa_t *spa, nvlist_t *zplprops, uint64_t txg)
00280 {
00281         int err;
00282         dsl_pool_t *dp = dsl_pool_open_impl(spa, txg);
00283         dmu_tx_t *tx = dmu_tx_create_assigned(dp, txg);
00284         objset_t *os;
00285         dsl_dataset_t *ds;
00286         uint64_t obj;
00287 
00288         /* create and open the MOS (meta-objset) */
00289         dp->dp_meta_objset = dmu_objset_create_impl(spa,
00290             NULL, &dp->dp_meta_rootbp, DMU_OST_META, tx);
00291 
00292         /* create the pool directory */
00293         err = zap_create_claim(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
00294             DMU_OT_OBJECT_DIRECTORY, DMU_OT_NONE, 0, tx);
00295         ASSERT0(err);
00296 
00297         /* Initialize scan structures */
00298         VERIFY3U(0, ==, dsl_scan_init(dp, txg));
00299 
00300         /* create and open the root dir */
00301         dp->dp_root_dir_obj = dsl_dir_create_sync(dp, NULL, NULL, tx);
00302         VERIFY(0 == dsl_dir_open_obj(dp, dp->dp_root_dir_obj,
00303             NULL, dp, &dp->dp_root_dir));
00304 
00305         /* create and open the meta-objset dir */
00306         (void) dsl_dir_create_sync(dp, dp->dp_root_dir, MOS_DIR_NAME, tx);
00307         VERIFY(0 == dsl_pool_open_special_dir(dp,
00308             MOS_DIR_NAME, &dp->dp_mos_dir));
00309 
00310         if (spa_version(spa) >= SPA_VERSION_DEADLISTS) {
00311                 /* create and open the free dir */
00312                 (void) dsl_dir_create_sync(dp, dp->dp_root_dir,
00313                     FREE_DIR_NAME, tx);
00314                 VERIFY(0 == dsl_pool_open_special_dir(dp,
00315                     FREE_DIR_NAME, &dp->dp_free_dir));
00316 
00317                 /* create and open the free_bplist */
00318                 obj = bpobj_alloc(dp->dp_meta_objset, SPA_MAXBLOCKSIZE, tx);
00319                 VERIFY(zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
00320                     DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx) == 0);
00321                 VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
00322                     dp->dp_meta_objset, obj));
00323         }
00324 
00325         if (spa_version(spa) >= SPA_VERSION_DSL_SCRUB)
00326                 dsl_pool_create_origin(dp, tx);
00327 
00328         /* create the root dataset */
00329         obj = dsl_dataset_create_sync_dd(dp->dp_root_dir, NULL, 0, tx);
00330 
00331         /* create the root objset */
00332         VERIFY(0 == dsl_dataset_hold_obj(dp, obj, FTAG, &ds));
00333         os = dmu_objset_create_impl(dp->dp_spa, ds,
00334             dsl_dataset_get_blkptr(ds), DMU_OST_ZFS, tx);
00335 #ifdef _KERNEL
00336         zfs_create_fs(os, kcred, zplprops, tx);
00337 #endif
00338         dsl_dataset_rele(ds, FTAG);
00339 
00340         dmu_tx_commit(tx);
00341 
00342         return (dp);
00343 }
00344 
00348 void
00349 dsl_pool_mos_diduse_space(dsl_pool_t *dp,
00350     int64_t used, int64_t comp, int64_t uncomp)
00351 {
00352         ASSERT3U(comp, ==, uncomp); /* it's all metadata */
00353         mutex_enter(&dp->dp_lock);
00354         dp->dp_mos_used_delta += used;
00355         dp->dp_mos_compressed_delta += comp;
00356         dp->dp_mos_uncompressed_delta += uncomp;
00357         mutex_exit(&dp->dp_lock);
00358 }
00359 
00360 static int
00361 deadlist_enqueue_cb(void *arg, const blkptr_t *bp, dmu_tx_t *tx)
00362 {
00363         dsl_deadlist_t *dl = arg;
00364         dsl_pool_t *dp = dmu_objset_pool(dl->dl_os);
00365         rw_enter(&dp->dp_config_rwlock, RW_READER);
00366         dsl_deadlist_insert(dl, bp, tx);
00367         rw_exit(&dp->dp_config_rwlock);
00368         return (0);
00369 }
00370 
00371 void
00372 dsl_pool_sync(dsl_pool_t *dp, uint64_t txg)
00373 {
00374         zio_t *zio;
00375         dmu_tx_t *tx;
00376         dsl_dir_t *dd;
00377         dsl_dataset_t *ds;
00378         objset_t *mos = dp->dp_meta_objset;
00379         hrtime_t start, write_time;
00380         uint64_t data_written;
00381         int err;
00382         list_t synced_datasets;
00383 
00384         list_create(&synced_datasets, sizeof (dsl_dataset_t),
00385             offsetof(dsl_dataset_t, ds_synced_link));
00386 
00387         /*
00388          * We need to copy dp_space_towrite() before doing
00389          * dsl_sync_task_group_sync(), because
00390          * dsl_dataset_snapshot_reserve_space() will increase
00391          * dp_space_towrite but not actually write anything.
00392          */
00393         data_written = dp->dp_space_towrite[txg & TXG_MASK];
00394 
00395         tx = dmu_tx_create_assigned(dp, txg);
00396 
00397         dp->dp_read_overhead = 0;
00398         start = gethrtime();
00399 
00400         zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
00401         while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
00402                 /*
00403                  * We must not sync any non-MOS datasets twice, because
00404                  * we may have taken a snapshot of them.  However, we
00405                  * may sync newly-created datasets on pass 2.
00406                  */
00407                 ASSERT(!list_link_active(&ds->ds_synced_link));
00408                 list_insert_tail(&synced_datasets, ds);
00409                 dsl_dataset_sync(ds, zio, tx);
00410         }
00411         DTRACE_PROBE(pool_sync__1setup);
00412         err = zio_wait(zio);
00413 
00414         write_time = gethrtime() - start;
00415         ASSERT(err == 0);
00416         DTRACE_PROBE(pool_sync__2rootzio);
00417 
00418         /*
00419          * After the data blocks have been written (ensured by the zio_wait()
00420          * above), update the user/group space accounting.
00421          */
00422         for (ds = list_head(&synced_datasets); ds;
00423             ds = list_next(&synced_datasets, ds))
00424                 dmu_objset_do_userquota_updates(ds->ds_objset, tx);
00425 
00426         /*
00427          * Sync the datasets again to push out the changes due to
00428          * userspace updates.  This must be done before we process the
00429          * sync tasks, so that any snapshots will have the correct
00430          * user accounting information (and we won't get confused
00431          * about which blocks are part of the snapshot).
00432          */
00433         zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
00434         while (ds = txg_list_remove(&dp->dp_dirty_datasets, txg)) {
00435                 ASSERT(list_link_active(&ds->ds_synced_link));
00436                 dmu_buf_rele(ds->ds_dbuf, ds);
00437                 dsl_dataset_sync(ds, zio, tx);
00438         }
00439         err = zio_wait(zio);
00440 
00441         /*
00442          * Now that the datasets have been completely synced, we can
00443          * clean up our in-memory structures accumulated while syncing:
00444          *
00445          *  - move dead blocks from the pending deadlist to the on-disk deadlist
00446          *  - clean up zil records
00447          *  - release hold from dsl_dataset_dirty()
00448          */
00449         while (ds = list_remove_head(&synced_datasets)) {
00450                 objset_t *os = ds->ds_objset;
00451                 bplist_iterate(&ds->ds_pending_deadlist,
00452                     deadlist_enqueue_cb, &ds->ds_deadlist, tx);
00453                 ASSERT(!dmu_objset_is_dirty(os, txg));
00454                 dmu_buf_rele(ds->ds_dbuf, ds);
00455         }
00456 
00457         start = gethrtime();
00458         while (dd = txg_list_remove(&dp->dp_dirty_dirs, txg))
00459                 dsl_dir_sync(dd, tx);
00460         write_time += gethrtime() - start;
00461 
00462         /*
00463          * The MOS's space is accounted for in the pool/$MOS
00464          * (dp_mos_dir).  We can't modify the mos while we're syncing
00465          * it, so we remember the deltas and apply them here.
00466          */
00467         if (dp->dp_mos_used_delta != 0 || dp->dp_mos_compressed_delta != 0 ||
00468             dp->dp_mos_uncompressed_delta != 0) {
00469                 dsl_dir_diduse_space(dp->dp_mos_dir, DD_USED_HEAD,
00470                     dp->dp_mos_used_delta,
00471                     dp->dp_mos_compressed_delta,
00472                     dp->dp_mos_uncompressed_delta, tx);
00473                 dp->dp_mos_used_delta = 0;
00474                 dp->dp_mos_compressed_delta = 0;
00475                 dp->dp_mos_uncompressed_delta = 0;
00476         }
00477 
00478         start = gethrtime();
00479         if (list_head(&mos->os_dirty_dnodes[txg & TXG_MASK]) != NULL ||
00480             list_head(&mos->os_free_dnodes[txg & TXG_MASK]) != NULL) {
00481                 zio = zio_root(dp->dp_spa, NULL, NULL, ZIO_FLAG_MUSTSUCCEED);
00482                 dmu_objset_sync(mos, zio, tx);
00483                 err = zio_wait(zio);
00484                 ASSERT(err == 0);
00485                 dprintf_bp(&dp->dp_meta_rootbp, "meta objset rootbp is %s", "");
00486                 spa_set_rootblkptr(dp->dp_spa, &dp->dp_meta_rootbp);
00487         }
00488         write_time += gethrtime() - start;
00489         DTRACE_PROBE2(pool_sync__4io, hrtime_t, write_time,
00490             hrtime_t, dp->dp_read_overhead);
00491         write_time -= dp->dp_read_overhead;
00492 
00493         /*
00494          * If we modify a dataset in the same txg that we want to destroy it,
00495          * its dsl_dir's dd_dbuf will be dirty, and thus have a hold on it.
00496          * dsl_dir_destroy_check() will fail if there are unexpected holds.
00497          * Therefore, we want to sync the MOS (thus syncing the dd_dbuf
00498          * and clearing the hold on it) before we process the sync_tasks.
00499          * The MOS data dirtied by the sync_tasks will be synced on the next
00500          * pass.
00501          */
00502         DTRACE_PROBE(pool_sync__3task);
00503         if (!txg_list_empty(&dp->dp_sync_tasks, txg)) {
00504                 dsl_sync_task_group_t *dstg;
00505                 /*
00506                  * No more sync tasks should have been added while we
00507                  * were syncing.
00508                  */
00509                 ASSERT(spa_sync_pass(dp->dp_spa) == 1);
00510                 while (dstg = txg_list_remove(&dp->dp_sync_tasks, txg))
00511                         dsl_sync_task_group_sync(dstg, tx);
00512         }
00513 
00514         dmu_tx_commit(tx);
00515 
00516         dp->dp_space_towrite[txg & TXG_MASK] = 0;
00517         ASSERT(dp->dp_tempreserved[txg & TXG_MASK] == 0);
00518 
00519         /*
00520          * If the write limit max has not been explicitly set, set it
00521          * to a fraction of available physical memory (default 1/8th).
00522          * Note that we must inflate the limit because the spa
00523          * inflates write sizes to account for data replication.
00524          * Check this each sync phase to catch changing memory size.
00525          */
00526         if (physmem != old_physmem && zfs_write_limit_shift) {
00527                 mutex_enter(&zfs_write_limit_lock);
00528                 old_physmem = physmem;
00529                 zfs_write_limit_max = ptob(physmem) >> zfs_write_limit_shift;
00530                 zfs_write_limit_inflated = MAX(zfs_write_limit_min,
00531                     spa_get_asize(dp->dp_spa, zfs_write_limit_max));
00532                 mutex_exit(&zfs_write_limit_lock);
00533         }
00534 
00535         /*
00536          * Attempt to keep the sync time consistent by adjusting the
00537          * amount of write traffic allowed into each transaction group.
00538          * Weight the throughput calculation towards the current value:
00539          *      thru = 3/4 old_thru + 1/4 new_thru
00540          *
00541          * Note: write_time is in nanosecs, so write_time/MICROSEC
00542          * yields millisecs
00543          */
00544         ASSERT(zfs_write_limit_min > 0);
00545         if (data_written > zfs_write_limit_min / 8 && write_time > MICROSEC) {
00546                 uint64_t throughput = data_written / (write_time / MICROSEC);
00547 
00548                 if (dp->dp_throughput)
00549                         dp->dp_throughput = throughput / 4 +
00550                             3 * dp->dp_throughput / 4;
00551                 else
00552                         dp->dp_throughput = throughput;
00553                 dp->dp_write_limit = MIN(zfs_write_limit_inflated,
00554                     MAX(zfs_write_limit_min,
00555                     dp->dp_throughput * zfs_txg_synctime_ms));
00556         }
00557 }
00558 
00559 void
00560 dsl_pool_sync_done(dsl_pool_t *dp, uint64_t txg)
00561 {
00562         zilog_t *zilog;
00563         dsl_dataset_t *ds;
00564 
00565         while (zilog = txg_list_remove(&dp->dp_dirty_zilogs, txg)) {
00566                 ds = dmu_objset_ds(zilog->zl_os);
00567                 zil_clean(zilog, txg);
00568                 ASSERT(!dmu_objset_is_dirty(zilog->zl_os, txg));
00569                 dmu_buf_rele(ds->ds_dbuf, zilog);
00570         }
00571         ASSERT(!dmu_objset_is_dirty(dp->dp_meta_objset, txg));
00572 }
00573 
00578 int
00579 dsl_pool_sync_context(dsl_pool_t *dp)
00580 {
00581         return (curthread == dp->dp_tx.tx_sync_thread ||
00582             spa_is_initializing(dp->dp_spa));
00583 }
00584 
00585 uint64_t
00586 dsl_pool_adjustedsize(dsl_pool_t *dp, boolean_t netfree)
00587 {
00588         uint64_t space, resv;
00589 
00590         /*
00591          * Reserve about 1.6% (1/64), or at least 32MB, for allocation
00592          * efficiency.
00593          * XXX The intent log is not accounted for, so it must fit
00594          * within this slop.
00595          *
00596          * If we're trying to assess whether it's OK to do a free,
00597          * cut the reservation in half to allow forward progress
00598          * (e.g. make it possible to rm(1) files from a full pool).
00599          */
00600         space = spa_get_dspace(dp->dp_spa);
00601         resv = MAX(space >> 6, SPA_MINDEVSIZE >> 1);
00602         if (netfree)
00603                 resv >>= 1;
00604 
00605         return (space - resv);
00606 }
00607 
00608 int
00609 dsl_pool_tempreserve_space(dsl_pool_t *dp, uint64_t space, dmu_tx_t *tx)
00610 {
00611         uint64_t reserved = 0;
00612         uint64_t write_limit = (zfs_write_limit_override ?
00613             zfs_write_limit_override : dp->dp_write_limit);
00614 
00615         if (zfs_no_write_throttle) {
00616                 atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK],
00617                     space);
00618                 return (0);
00619         }
00620 
00621         /*
00622          * Check to see if we have exceeded the maximum allowed IO for
00623          * this transaction group.  We can do this without locks since
00624          * a little slop here is ok.  Note that we do the reserved check
00625          * with only half the requested reserve: this is because the
00626          * reserve requests are worst-case, and we really don't want to
00627          * throttle based off of worst-case estimates.
00628          */
00629         if (write_limit > 0) {
00630                 reserved = dp->dp_space_towrite[tx->tx_txg & TXG_MASK]
00631                     + dp->dp_tempreserved[tx->tx_txg & TXG_MASK] / 2;
00632 
00633                 if (reserved && reserved > write_limit)
00634                         return (ERESTART);
00635         }
00636 
00637         atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], space);
00638 
00639         /*
00640          * If this transaction group is over 7/8ths capacity, delay
00641          * the caller 1 clock tick.  This will slow down the "fill"
00642          * rate until the sync process can catch up with us.
00643          */
00644         if (reserved && reserved > (write_limit - (write_limit >> 3)))
00645                 txg_delay(dp, tx->tx_txg, 1);
00646 
00647         return (0);
00648 }
00649 
00650 void
00651 dsl_pool_tempreserve_clear(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
00652 {
00653         ASSERT(dp->dp_tempreserved[tx->tx_txg & TXG_MASK] >= space);
00654         atomic_add_64(&dp->dp_tempreserved[tx->tx_txg & TXG_MASK], -space);
00655 }
00656 
00657 void
00658 dsl_pool_memory_pressure(dsl_pool_t *dp)
00659 {
00660         uint64_t space_inuse = 0;
00661         int i;
00662 
00663         if (dp->dp_write_limit == zfs_write_limit_min)
00664                 return;
00665 
00666         for (i = 0; i < TXG_SIZE; i++) {
00667                 space_inuse += dp->dp_space_towrite[i];
00668                 space_inuse += dp->dp_tempreserved[i];
00669         }
00670         dp->dp_write_limit = MAX(zfs_write_limit_min,
00671             MIN(dp->dp_write_limit, space_inuse / 4));
00672 }
00673 
00674 void
00675 dsl_pool_willuse_space(dsl_pool_t *dp, int64_t space, dmu_tx_t *tx)
00676 {
00677         if (space > 0) {
00678                 mutex_enter(&dp->dp_lock);
00679                 dp->dp_space_towrite[tx->tx_txg & TXG_MASK] += space;
00680                 mutex_exit(&dp->dp_lock);
00681         }
00682 }
00683 
00684 /* ARGSUSED */
00685 static int
00686 upgrade_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
00687 {
00688         dmu_tx_t *tx = arg;
00689         dsl_dataset_t *ds, *prev = NULL;
00690         int err;
00691         dsl_pool_t *dp = spa_get_dsl(spa);
00692 
00693         err = dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds);
00694         if (err)
00695                 return (err);
00696 
00697         while (ds->ds_phys->ds_prev_snap_obj != 0) {
00698                 err = dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
00699                     FTAG, &prev);
00700                 if (err) {
00701                         dsl_dataset_rele(ds, FTAG);
00702                         return (err);
00703                 }
00704 
00705                 if (prev->ds_phys->ds_next_snap_obj != ds->ds_object)
00706                         break;
00707                 dsl_dataset_rele(ds, FTAG);
00708                 ds = prev;
00709                 prev = NULL;
00710         }
00711 
00712         if (prev == NULL) {
00713                 prev = dp->dp_origin_snap;
00714 
00715                 /*
00716                  * The $ORIGIN can't have any data, or the accounting
00717                  * will be wrong.
00718                  */
00719                 ASSERT(prev->ds_phys->ds_bp.blk_birth == 0);
00720 
00721                 /* The origin doesn't get attached to itself */
00722                 if (ds->ds_object == prev->ds_object) {
00723                         dsl_dataset_rele(ds, FTAG);
00724                         return (0);
00725                 }
00726 
00727                 dmu_buf_will_dirty(ds->ds_dbuf, tx);
00728                 ds->ds_phys->ds_prev_snap_obj = prev->ds_object;
00729                 ds->ds_phys->ds_prev_snap_txg = prev->ds_phys->ds_creation_txg;
00730 
00731                 dmu_buf_will_dirty(ds->ds_dir->dd_dbuf, tx);
00732                 ds->ds_dir->dd_phys->dd_origin_obj = prev->ds_object;
00733 
00734                 dmu_buf_will_dirty(prev->ds_dbuf, tx);
00735                 prev->ds_phys->ds_num_children++;
00736 
00737                 if (ds->ds_phys->ds_next_snap_obj == 0) {
00738                         ASSERT(ds->ds_prev == NULL);
00739                         VERIFY(0 == dsl_dataset_hold_obj(dp,
00740                             ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
00741                 }
00742         }
00743 
00744         ASSERT(ds->ds_dir->dd_phys->dd_origin_obj == prev->ds_object);
00745         ASSERT(ds->ds_phys->ds_prev_snap_obj == prev->ds_object);
00746 
00747         if (prev->ds_phys->ds_next_clones_obj == 0) {
00748                 dmu_buf_will_dirty(prev->ds_dbuf, tx);
00749                 prev->ds_phys->ds_next_clones_obj =
00750                     zap_create(dp->dp_meta_objset,
00751                     DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
00752         }
00753         VERIFY(0 == zap_add_int(dp->dp_meta_objset,
00754             prev->ds_phys->ds_next_clones_obj, ds->ds_object, tx));
00755 
00756         dsl_dataset_rele(ds, FTAG);
00757         if (prev != dp->dp_origin_snap)
00758                 dsl_dataset_rele(prev, FTAG);
00759         return (0);
00760 }
00761 
00762 void
00763 dsl_pool_upgrade_clones(dsl_pool_t *dp, dmu_tx_t *tx)
00764 {
00765         ASSERT(dmu_tx_is_syncing(tx));
00766         ASSERT(dp->dp_origin_snap != NULL);
00767 
00768         VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL, upgrade_clones_cb,
00769             tx, DS_FIND_CHILDREN));
00770 }
00771 
00772 /* ARGSUSED */
00773 static int
00774 upgrade_dir_clones_cb(spa_t *spa, uint64_t dsobj, const char *dsname, void *arg)
00775 {
00776         dmu_tx_t *tx = arg;
00777         dsl_dataset_t *ds;
00778         dsl_pool_t *dp = spa_get_dsl(spa);
00779         objset_t *mos = dp->dp_meta_objset;
00780 
00781         VERIFY3U(0, ==, dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
00782 
00783         if (ds->ds_dir->dd_phys->dd_origin_obj) {
00784                 dsl_dataset_t *origin;
00785 
00786                 VERIFY3U(0, ==, dsl_dataset_hold_obj(dp,
00787                     ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin));
00788 
00789                 if (origin->ds_dir->dd_phys->dd_clones == 0) {
00790                         dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
00791                         origin->ds_dir->dd_phys->dd_clones = zap_create(mos,
00792                             DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
00793                 }
00794 
00795                 VERIFY3U(0, ==, zap_add_int(dp->dp_meta_objset,
00796                     origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
00797 
00798                 dsl_dataset_rele(origin, FTAG);
00799         }
00800 
00801         dsl_dataset_rele(ds, FTAG);
00802         return (0);
00803 }
00804 
00805 void
00806 dsl_pool_upgrade_dir_clones(dsl_pool_t *dp, dmu_tx_t *tx)
00807 {
00808         ASSERT(dmu_tx_is_syncing(tx));
00809         uint64_t obj;
00810 
00811         (void) dsl_dir_create_sync(dp, dp->dp_root_dir, FREE_DIR_NAME, tx);
00812         VERIFY(0 == dsl_pool_open_special_dir(dp,
00813             FREE_DIR_NAME, &dp->dp_free_dir));
00814 
00815         /*
00816          * We can't use bpobj_alloc(), because spa_version() still
00817          * returns the old version, and we need a new-version bpobj with
00818          * subobj support.  So call dmu_object_alloc() directly.
00819          */
00820         obj = dmu_object_alloc(dp->dp_meta_objset, DMU_OT_BPOBJ,
00821             SPA_MAXBLOCKSIZE, DMU_OT_BPOBJ_HDR, sizeof (bpobj_phys_t), tx);
00822         VERIFY3U(0, ==, zap_add(dp->dp_meta_objset, DMU_POOL_DIRECTORY_OBJECT,
00823             DMU_POOL_FREE_BPOBJ, sizeof (uint64_t), 1, &obj, tx));
00824         VERIFY3U(0, ==, bpobj_open(&dp->dp_free_bpobj,
00825             dp->dp_meta_objset, obj));
00826 
00827         VERIFY3U(0, ==, dmu_objset_find_spa(dp->dp_spa, NULL,
00828             upgrade_dir_clones_cb, tx, DS_FIND_CHILDREN));
00829 }
00830 
00831 void
00832 dsl_pool_create_origin(dsl_pool_t *dp, dmu_tx_t *tx)
00833 {
00834         uint64_t dsobj;
00835         dsl_dataset_t *ds;
00836 
00837         ASSERT(dmu_tx_is_syncing(tx));
00838         ASSERT(dp->dp_origin_snap == NULL);
00839 
00840         /* create the origin dir, ds, & snap-ds */
00841         rw_enter(&dp->dp_config_rwlock, RW_WRITER);
00842         dsobj = dsl_dataset_create_sync(dp->dp_root_dir, ORIGIN_DIR_NAME,
00843             NULL, 0, kcred, tx);
00844         VERIFY(0 == dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
00845         dsl_dataset_snapshot_sync(ds, ORIGIN_DIR_NAME, tx);
00846         VERIFY(0 == dsl_dataset_hold_obj(dp, ds->ds_phys->ds_prev_snap_obj,
00847             dp, &dp->dp_origin_snap));
00848         dsl_dataset_rele(ds, FTAG);
00849         rw_exit(&dp->dp_config_rwlock);
00850 }
00851 
00852 taskq_t *
00853 dsl_pool_vnrele_taskq(dsl_pool_t *dp)
00854 {
00855         return (dp->dp_vnrele_taskq);
00856 }
00857 
00862 void
00863 dsl_pool_clean_tmp_userrefs(dsl_pool_t *dp)
00864 {
00865         zap_attribute_t za;
00866         zap_cursor_t zc;
00867         objset_t *mos = dp->dp_meta_objset;
00868         uint64_t zapobj = dp->dp_tmp_userrefs_obj;
00869 
00870         if (zapobj == 0)
00871                 return;
00872         ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
00873 
00874         for (zap_cursor_init(&zc, mos, zapobj);
00875             zap_cursor_retrieve(&zc, &za) == 0;
00876             zap_cursor_advance(&zc)) {
00877                 char *htag;
00878                 uint64_t dsobj;
00879 
00880                 htag = strchr(za.za_name, '-');
00881                 *htag = '\0';
00882                 ++htag;
00883                 dsobj = strtonum(za.za_name, NULL);
00884                 (void) dsl_dataset_user_release_tmp(dp, dsobj, htag, B_FALSE);
00885         }
00886         zap_cursor_fini(&zc);
00887 }
00888 
00892 void
00893 dsl_pool_user_hold_create_obj(dsl_pool_t *dp, dmu_tx_t *tx)
00894 {
00895         objset_t *mos = dp->dp_meta_objset;
00896 
00897         ASSERT(dp->dp_tmp_userrefs_obj == 0);
00898         ASSERT(dmu_tx_is_syncing(tx));
00899 
00900         dp->dp_tmp_userrefs_obj = zap_create_link(mos, DMU_OT_USERREFS,
00901             DMU_POOL_DIRECTORY_OBJECT, DMU_POOL_TMP_USERREFS, tx);
00902 }
00903 
00904 static int
00905 dsl_pool_user_hold_rele_impl(dsl_pool_t *dp, uint64_t dsobj,
00906     const char *tag, uint64_t *now, dmu_tx_t *tx, boolean_t holding)
00907 {
00908         objset_t *mos = dp->dp_meta_objset;
00909         uint64_t zapobj = dp->dp_tmp_userrefs_obj;
00910         char *name;
00911         int error;
00912 
00913         ASSERT(spa_version(dp->dp_spa) >= SPA_VERSION_USERREFS);
00914         ASSERT(dmu_tx_is_syncing(tx));
00915 
00916         /*
00917          * If the pool was created prior to SPA_VERSION_USERREFS, the
00918          * zap object for temporary holds might not exist yet.
00919          */
00920         if (zapobj == 0) {
00921                 if (holding) {
00922                         dsl_pool_user_hold_create_obj(dp, tx);
00923                         zapobj = dp->dp_tmp_userrefs_obj;
00924                 } else {
00925                         return (ENOENT);
00926                 }
00927         }
00928 
00929         name = kmem_asprintf("%llx-%s", (u_longlong_t)dsobj, tag);
00930         if (holding)
00931                 error = zap_add(mos, zapobj, name, 8, 1, now, tx);
00932         else
00933                 error = zap_remove(mos, zapobj, name, tx);
00934         strfree(name);
00935 
00936         return (error);
00937 }
00938 
00942 int
00943 dsl_pool_user_hold(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
00944     uint64_t *now, dmu_tx_t *tx)
00945 {
00946         return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, now, tx, B_TRUE));
00947 }
00948 
00952 int
00953 dsl_pool_user_release(dsl_pool_t *dp, uint64_t dsobj, const char *tag,
00954     dmu_tx_t *tx)
00955 {
00956         return (dsl_pool_user_hold_rele_impl(dp, dsobj, tag, NULL,
00957             tx, B_FALSE));
00958 }
 All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Defines