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) 2011, 2015 by Delphix. All rights reserved.
  24  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
  25  * Copyright (c) 2014 RackTop Systems.
  26  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
  27  * Copyright (c) 2014 Integros [integros.com]
  28  */
  29 
  30 #include <sys/dmu_objset.h>
  31 #include <sys/dsl_dataset.h>
  32 #include <sys/dsl_dir.h>
  33 #include <sys/dsl_prop.h>
  34 #include <sys/dsl_synctask.h>
  35 #include <sys/dmu_traverse.h>
  36 #include <sys/dmu_impl.h>
  37 #include <sys/dmu_tx.h>
  38 #include <sys/arc.h>
  39 #include <sys/zio.h>
  40 #include <sys/zap.h>
  41 #include <sys/zfeature.h>
  42 #include <sys/unique.h>
  43 #include <sys/zfs_context.h>
  44 #include <sys/zfs_ioctl.h>
  45 #include <sys/spa.h>
  46 #include <sys/zfs_znode.h>
  47 #include <sys/zfs_onexit.h>
 
 
  61  * can have an impact on i/o latency (e.g. tying up a spinning disk for
  62  * ~300ms), and also potentially on the memory allocator.  Therefore,
  63  * we do not allow the recordsize to be set larger than zfs_max_recordsize
  64  * (default 1MB).  Larger blocks can be created by changing this tunable,
  65  * and pools with larger blocks can always be imported and used, regardless
  66  * of this setting.
  67  */
  68 int zfs_max_recordsize = 1 * 1024 * 1024;
  69 
  70 #define SWITCH64(x, y) \
  71         { \
  72                 uint64_t __tmp = (x); \
  73                 (x) = (y); \
  74                 (y) = __tmp; \
  75         }
  76 
  77 #define DS_REF_MAX      (1ULL << 62)
  78 
  79 extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
  80 
  81 /*
  82  * Figure out how much of this delta should be propogated to the dsl_dir
  83  * layer.  If there's a refreservation, that space has already been
  84  * partially accounted for in our ancestors.
  85  */
  86 static int64_t
  87 parent_delta(dsl_dataset_t *ds, int64_t delta)
  88 {
  89         dsl_dataset_phys_t *ds_phys;
  90         uint64_t old_bytes, new_bytes;
  91 
  92         if (ds->ds_reserved == 0)
  93                 return (delta);
  94 
  95         ds_phys = dsl_dataset_phys(ds);
  96         old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
  97         new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
  98 
  99         ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
 100         return (new_bytes - old_bytes);
 
2771         error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2772             dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2773             &numsnaps);
2774         dmu_objset_rele(os, FTAG);
2775         if (error != 0)
2776                 return (error);
2777 
2778         ddpa.ddpa_clonename = name;
2779         ddpa.err_ds = conflsnap;
2780         ddpa.cr = CRED();
2781 
2782         return (dsl_sync_task(name, dsl_dataset_promote_check,
2783             dsl_dataset_promote_sync, &ddpa,
2784             2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2785 }
2786 
2787 int
2788 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2789     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2790 {
2791         int64_t unused_refres_delta;
2792 
2793         /* they should both be heads */
2794         if (clone->ds_is_snapshot ||
2795             origin_head->ds_is_snapshot)
2796                 return (SET_ERROR(EINVAL));
2797 
2798         /* if we are not forcing, the branch point should be just before them */
2799         if (!force && clone->ds_prev != origin_head->ds_prev)
2800                 return (SET_ERROR(EINVAL));
2801 
2802         /* clone should be the clone (unless they are unrelated) */
2803         if (clone->ds_prev != NULL &&
2804             clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2805             origin_head->ds_dir != clone->ds_prev->ds_dir)
2806                 return (SET_ERROR(EINVAL));
2807 
2808         /* the clone should be a child of the origin */
2809         if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2810                 return (SET_ERROR(EINVAL));
 
2813         if (!force &&
2814             dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2815                 return (SET_ERROR(ETXTBSY));
2816 
2817         /* origin_head should have no long holds (e.g. is not mounted) */
2818         if (dsl_dataset_handoff_check(origin_head, owner, tx))
2819                 return (SET_ERROR(EBUSY));
2820 
2821         /* check amount of any unconsumed refreservation */
2822         unused_refres_delta =
2823             (int64_t)MIN(origin_head->ds_reserved,
2824             dsl_dataset_phys(origin_head)->ds_unique_bytes) -
2825             (int64_t)MIN(origin_head->ds_reserved,
2826             dsl_dataset_phys(clone)->ds_unique_bytes);
2827 
2828         if (unused_refres_delta > 0 &&
2829             unused_refres_delta >
2830             dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2831                 return (SET_ERROR(ENOSPC));
2832 
2833         /* clone can't be over the head's refquota */
2834         if (origin_head->ds_quota != 0 &&
2835             dsl_dataset_phys(clone)->ds_referenced_bytes >
2836             origin_head->ds_quota)
2837                 return (SET_ERROR(EDQUOT));
2838 
2839         return (0);
2840 }
2841 
2842 void
2843 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2844     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2845 {
2846         dsl_pool_t *dp = dmu_tx_pool(tx);
2847         int64_t unused_refres_delta;
2848 
2849         ASSERT(clone->ds_reserved == 0);
2850         ASSERT(origin_head->ds_quota == 0 ||
2851             dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota);
2852         ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2853 
2854         /*
2855          * Swap per-dataset feature flags.
2856          */
2857         for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2858                 if (!(spa_feature_table[f].fi_flags &
2859                     ZFEATURE_FLAG_PER_DATASET)) {
2860                         ASSERT(!clone->ds_feature_inuse[f]);
2861                         ASSERT(!origin_head->ds_feature_inuse[f]);
2862                         continue;
2863                 }
2864 
2865                 boolean_t clone_inuse = clone->ds_feature_inuse[f];
2866                 boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
2867 
2868                 if (clone_inuse) {
2869                         dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
2870                         clone->ds_feature_inuse[f] = B_FALSE;
2871                 }
 
 | 
 
 
   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) 2011, 2015 by Delphix. All rights reserved.
  24  * Copyright (c) 2014, Joyent, Inc. All rights reserved.
  25  * Copyright (c) 2014 RackTop Systems.
  26  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved.
  27  * Copyright (c) 2014 Integros [integros.com]
  28  * Copyright 2016, OmniTI Computer Consulting, Inc. All rights reserved.
  29  */
  30 
  31 #include <sys/dmu_objset.h>
  32 #include <sys/dsl_dataset.h>
  33 #include <sys/dsl_dir.h>
  34 #include <sys/dsl_prop.h>
  35 #include <sys/dsl_synctask.h>
  36 #include <sys/dmu_traverse.h>
  37 #include <sys/dmu_impl.h>
  38 #include <sys/dmu_tx.h>
  39 #include <sys/arc.h>
  40 #include <sys/zio.h>
  41 #include <sys/zap.h>
  42 #include <sys/zfeature.h>
  43 #include <sys/unique.h>
  44 #include <sys/zfs_context.h>
  45 #include <sys/zfs_ioctl.h>
  46 #include <sys/spa.h>
  47 #include <sys/zfs_znode.h>
  48 #include <sys/zfs_onexit.h>
 
 
  62  * can have an impact on i/o latency (e.g. tying up a spinning disk for
  63  * ~300ms), and also potentially on the memory allocator.  Therefore,
  64  * we do not allow the recordsize to be set larger than zfs_max_recordsize
  65  * (default 1MB).  Larger blocks can be created by changing this tunable,
  66  * and pools with larger blocks can always be imported and used, regardless
  67  * of this setting.
  68  */
  69 int zfs_max_recordsize = 1 * 1024 * 1024;
  70 
  71 #define SWITCH64(x, y) \
  72         { \
  73                 uint64_t __tmp = (x); \
  74                 (x) = (y); \
  75                 (y) = __tmp; \
  76         }
  77 
  78 #define DS_REF_MAX      (1ULL << 62)
  79 
  80 extern inline dsl_dataset_phys_t *dsl_dataset_phys(dsl_dataset_t *ds);
  81 
  82 extern int spa_asize_inflation;
  83 
  84 /*
  85  * Figure out how much of this delta should be propogated to the dsl_dir
  86  * layer.  If there's a refreservation, that space has already been
  87  * partially accounted for in our ancestors.
  88  */
  89 static int64_t
  90 parent_delta(dsl_dataset_t *ds, int64_t delta)
  91 {
  92         dsl_dataset_phys_t *ds_phys;
  93         uint64_t old_bytes, new_bytes;
  94 
  95         if (ds->ds_reserved == 0)
  96                 return (delta);
  97 
  98         ds_phys = dsl_dataset_phys(ds);
  99         old_bytes = MAX(ds_phys->ds_unique_bytes, ds->ds_reserved);
 100         new_bytes = MAX(ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
 101 
 102         ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
 103         return (new_bytes - old_bytes);
 
2774         error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
2775             dsl_dataset_phys(dmu_objset_ds(os))->ds_snapnames_zapobj,
2776             &numsnaps);
2777         dmu_objset_rele(os, FTAG);
2778         if (error != 0)
2779                 return (error);
2780 
2781         ddpa.ddpa_clonename = name;
2782         ddpa.err_ds = conflsnap;
2783         ddpa.cr = CRED();
2784 
2785         return (dsl_sync_task(name, dsl_dataset_promote_check,
2786             dsl_dataset_promote_sync, &ddpa,
2787             2 + numsnaps, ZFS_SPACE_CHECK_RESERVED));
2788 }
2789 
2790 int
2791 dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
2792     dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
2793 {
2794         /*
2795          * "slack" factor for received datasets with refquota set on them.
2796          * See the bottom of this function for details on its use.
2797          */
2798         uint64_t refquota_slack = DMU_MAX_ACCESS * spa_asize_inflation;
2799         int64_t unused_refres_delta;
2800 
2801         /* they should both be heads */
2802         if (clone->ds_is_snapshot ||
2803             origin_head->ds_is_snapshot)
2804                 return (SET_ERROR(EINVAL));
2805 
2806         /* if we are not forcing, the branch point should be just before them */
2807         if (!force && clone->ds_prev != origin_head->ds_prev)
2808                 return (SET_ERROR(EINVAL));
2809 
2810         /* clone should be the clone (unless they are unrelated) */
2811         if (clone->ds_prev != NULL &&
2812             clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
2813             origin_head->ds_dir != clone->ds_prev->ds_dir)
2814                 return (SET_ERROR(EINVAL));
2815 
2816         /* the clone should be a child of the origin */
2817         if (clone->ds_dir->dd_parent != origin_head->ds_dir)
2818                 return (SET_ERROR(EINVAL));
 
2821         if (!force &&
2822             dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
2823                 return (SET_ERROR(ETXTBSY));
2824 
2825         /* origin_head should have no long holds (e.g. is not mounted) */
2826         if (dsl_dataset_handoff_check(origin_head, owner, tx))
2827                 return (SET_ERROR(EBUSY));
2828 
2829         /* check amount of any unconsumed refreservation */
2830         unused_refres_delta =
2831             (int64_t)MIN(origin_head->ds_reserved,
2832             dsl_dataset_phys(origin_head)->ds_unique_bytes) -
2833             (int64_t)MIN(origin_head->ds_reserved,
2834             dsl_dataset_phys(clone)->ds_unique_bytes);
2835 
2836         if (unused_refres_delta > 0 &&
2837             unused_refres_delta >
2838             dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
2839                 return (SET_ERROR(ENOSPC));
2840 
2841         /*
2842          * The clone can't be too much over the head's refquota.
2843          *
2844          * To ensure that the entire refquota can be used, we allow one
2845          * transaction to exceed the the refquota.  Therefore, this check
2846          * needs to also allow for the space referenced to be more than the
2847          * refquota.  The maximum amount of space that one transaction can use
2848          * on disk is DMU_MAX_ACCESS * spa_asize_inflation.  Allowing this
2849          * overage ensures that we are able to receive a filesystem that
2850          * exceeds the refquota on the source system.
2851          *
2852          * So that overage is the refquota_slack we use below.
2853          */
2854         if (origin_head->ds_quota != 0 &&
2855             dsl_dataset_phys(clone)->ds_referenced_bytes >
2856             origin_head->ds_quota + refquota_slack)
2857                 return (SET_ERROR(EDQUOT));
2858 
2859         return (0);
2860 }
2861 
2862 void
2863 dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
2864     dsl_dataset_t *origin_head, dmu_tx_t *tx)
2865 {
2866         dsl_pool_t *dp = dmu_tx_pool(tx);
2867         int64_t unused_refres_delta;
2868 
2869         ASSERT(clone->ds_reserved == 0);
2870         /*
2871          * NOTE: On DEBUG kernels there could be a race between this and
2872          * the check function if spa_asize_inflation is adjusted...
2873          */
2874         ASSERT(origin_head->ds_quota == 0 ||
2875             dsl_dataset_phys(clone)->ds_unique_bytes <= origin_head->ds_quota +
2876             DMU_MAX_ACCESS * spa_asize_inflation);
2877         ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
2878 
2879         /*
2880          * Swap per-dataset feature flags.
2881          */
2882         for (spa_feature_t f = 0; f < SPA_FEATURES; f++) {
2883                 if (!(spa_feature_table[f].fi_flags &
2884                     ZFEATURE_FLAG_PER_DATASET)) {
2885                         ASSERT(!clone->ds_feature_inuse[f]);
2886                         ASSERT(!origin_head->ds_feature_inuse[f]);
2887                         continue;
2888                 }
2889 
2890                 boolean_t clone_inuse = clone->ds_feature_inuse[f];
2891                 boolean_t origin_head_inuse = origin_head->ds_feature_inuse[f];
2892 
2893                 if (clone_inuse) {
2894                         dsl_dataset_deactivate_feature(clone->ds_object, f, tx);
2895                         clone->ds_feature_inuse[f] = B_FALSE;
2896                 }
 
 |