usr/src/uts/common/fs/zfs/dsl_dataset.c
author Matthew Ahrens <mahrens@delphix.com>
Wed, 14 Aug 2013 11:42:31 -0800
changeset 14140 f7523d207d74
parent 14097 8a2884391fc2
child 14160 85f7bb2246ca
permissions -rw-r--r--
3996 want a libzfs_core API to rollback to latest snapshot Reviewed by: Christopher Siden <[email protected]> Reviewed by: Adam Leventhal <[email protected]> Reviewed by: George Wilson <[email protected]> Reviewed by: Andy Stormont <[email protected]> Approved by: Richard Lowe <[email protected]>
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     1
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     2
 * CDDL HEADER START
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     3
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     4
 * The contents of this file are subject to the terms of the
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
     5
 * Common Development and Distribution License (the "License").
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
     6
 * You may not use this file except in compliance with the License.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
/*
12115
3655f38d3bea 6942295 use-after-free panic in dsl_dataset_user_release_one
Chris Kirby <Chris.Kirby@oracle.com>
parents: 11823
diff changeset
    22
 * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved.
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
    23
 * Copyright (c) 2013 by Delphix. All rights reserved.
13686
4bc0783f6064 2703 add mechanism to report ZFS send progress
Bill Pijewski <wdp@joyent.com>
parents: 13524
diff changeset
    24
 * Copyright (c) 2012, Joyent, Inc. All rights reserved.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    25
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    26
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    27
#include <sys/dmu_objset.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
#include <sys/dsl_dataset.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <sys/dsl_dir.h>
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
    30
#include <sys/dsl_prop.h>
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
    31
#include <sys/dsl_synctask.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <sys/dmu_traverse.h>
13686
4bc0783f6064 2703 add mechanism to report ZFS send progress
Bill Pijewski <wdp@joyent.com>
parents: 13524
diff changeset
    33
#include <sys/dmu_impl.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <sys/dmu_tx.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <sys/arc.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <sys/zio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <sys/zap.h>
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
    38
#include <sys/zfeature.h>
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <sys/unique.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
#include <sys/zfs_context.h>
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
    41
#include <sys/zfs_ioctl.h>
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4009
diff changeset
    42
#include <sys/spa.h>
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
    43
#include <sys/zfs_znode.h>
12527
693dd2cad55f 6884007 zfs_send() can leave temporary holds around
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12470
diff changeset
    44
#include <sys/zfs_onexit.h>
10242
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
    45
#include <sys/zvol.h>
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
    46
#include <sys/dsl_scan.h>
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
    47
#include <sys/dsl_deadlist.h>
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
    48
#include <sys/dsl_destroy.h>
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
    49
#include <sys/dsl_userhold.h>
1731
1efa8b3d1296 6402598 'zfs destroy <fs>' can take a long time, stopping up the txg train
bonwick
parents: 1544
diff changeset
    50
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
    51
#define	SWITCH64(x, y) \
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
    52
	{ \
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
    53
		uint64_t __tmp = (x); \
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
    54
		(x) = (y); \
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
    55
		(y) = __tmp; \
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
    56
	}
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
    57
3444
dc160a70a50d 6410433 'zpool status -v' would be more useful with filenames
ek110237
parents: 3025
diff changeset
    58
#define	DS_REF_MAX	(1ULL << 62)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
#define	DSL_DEADLIST_BLOCKSIZE	SPA_MAXBLOCKSIZE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    62
/*
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    63
 * Figure out how much of this delta should be propogated to the dsl_dir
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    64
 * layer.  If there's a refreservation, that space has already been
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    65
 * partially accounted for in our ancestors.
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    66
 */
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    67
static int64_t
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    68
parent_delta(dsl_dataset_t *ds, int64_t delta)
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    69
{
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    70
	uint64_t old_bytes, new_bytes;
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    71
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    72
	if (ds->ds_reserved == 0)
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    73
		return (delta);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    74
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    75
	old_bytes = MAX(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    76
	new_bytes = MAX(ds->ds_phys->ds_unique_bytes + delta, ds->ds_reserved);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    77
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    78
	ASSERT3U(ABS((int64_t)(new_bytes - old_bytes)), <=, ABS(delta));
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    79
	return (new_bytes - old_bytes);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    80
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
void
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
    83
dsl_dataset_block_born(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
{
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
    85
	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
	int compressed = BP_GET_PSIZE(bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
	int uncompressed = BP_GET_UCSIZE(bp);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
    88
	int64_t delta;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
    90
	dprintf_bp(bp, "ds=%p", ds);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
	ASSERT(dmu_tx_is_syncing(tx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
	/* It could have been compressed away to nothing */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
	if (BP_IS_HOLE(bp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
	ASSERT(BP_GET_TYPE(bp) != DMU_OT_NONE);
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
    97
	ASSERT(DMU_OT_IS_VALID(BP_GET_TYPE(bp)));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
	if (ds == NULL) {
13776
cd512c80fd75 3086 unnecessarily setting DS_FLAG_INCONSISTENT on async destroyed datasets
Matthew Ahrens <mahrens@delphix.com>
parents: 13772
diff changeset
    99
		dsl_pool_mos_diduse_space(tx->tx_pool,
cd512c80fd75 3086 unnecessarily setting DS_FLAG_INCONSISTENT on async destroyed datasets
Matthew Ahrens <mahrens@delphix.com>
parents: 13772
diff changeset
   100
		    used, compressed, uncompressed);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
	dmu_buf_will_dirty(ds->ds_dbuf, tx);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
   104
7595
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
   105
	mutex_enter(&ds->ds_dir->dd_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
	mutex_enter(&ds->ds_lock);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   107
	delta = parent_delta(ds, used);
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
   108
	ds->ds_phys->ds_referenced_bytes += used;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
	ds->ds_phys->ds_compressed_bytes += compressed;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
	ds->ds_phys->ds_uncompressed_bytes += uncompressed;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
	ds->ds_phys->ds_unique_bytes += used;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
	mutex_exit(&ds->ds_lock);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   113
	dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD, delta,
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   114
	    compressed, uncompressed, tx);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   115
	dsl_dir_transfer_space(ds->ds_dir, used - delta,
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   116
	    DD_USED_REFRSRV, DD_USED_HEAD, tx);
7595
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
   117
	mutex_exit(&ds->ds_dir->dd_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6975
diff changeset
   120
int
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   121
dsl_dataset_block_kill(dsl_dataset_t *ds, const blkptr_t *bp, dmu_tx_t *tx,
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   122
    boolean_t async)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
{
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   124
	if (BP_IS_HOLE(bp))
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   125
		return (0);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   126
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   127
	ASSERT(dmu_tx_is_syncing(tx));
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   128
	ASSERT(bp->blk_birth <= tx->tx_txg);
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   129
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   130
	int used = bp_get_dsize_sync(tx->tx_pool->dp_spa, bp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
	int compressed = BP_GET_PSIZE(bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
	int uncompressed = BP_GET_UCSIZE(bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
	ASSERT(used > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
	if (ds == NULL) {
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   136
		dsl_free(tx->tx_pool, tx->tx_txg, bp);
13776
cd512c80fd75 3086 unnecessarily setting DS_FLAG_INCONSISTENT on async destroyed datasets
Matthew Ahrens <mahrens@delphix.com>
parents: 13772
diff changeset
   137
		dsl_pool_mos_diduse_space(tx->tx_pool,
cd512c80fd75 3086 unnecessarily setting DS_FLAG_INCONSISTENT on async destroyed datasets
Matthew Ahrens <mahrens@delphix.com>
parents: 13772
diff changeset
   138
		    -used, -compressed, -uncompressed);
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6975
diff changeset
   139
		return (used);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
	ASSERT3P(tx->tx_pool, ==, ds->ds_dir->dd_pool);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   143
	ASSERT(!dsl_dataset_is_snapshot(ds));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
	dmu_buf_will_dirty(ds->ds_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
	if (bp->blk_birth > ds->ds_phys->ds_prev_snap_txg) {
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   147
		int64_t delta;
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3444
diff changeset
   148
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
   149
		dprintf_bp(bp, "freeing ds=%llu", ds->ds_object);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   150
		dsl_free(tx->tx_pool, tx->tx_txg, bp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
7595
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
   152
		mutex_enter(&ds->ds_dir->dd_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
		mutex_enter(&ds->ds_lock);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   154
		ASSERT(ds->ds_phys->ds_unique_bytes >= used ||
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   155
		    !DS_UNIQUE_IS_ACCURATE(ds));
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   156
		delta = parent_delta(ds, -used);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
		ds->ds_phys->ds_unique_bytes -= used;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
		mutex_exit(&ds->ds_lock);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   159
		dsl_dir_diduse_space(ds->ds_dir, DD_USED_HEAD,
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   160
		    delta, -compressed, -uncompressed, tx);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   161
		dsl_dir_transfer_space(ds->ds_dir, -used - delta,
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   162
		    DD_USED_REFRSRV, DD_USED_HEAD, tx);
7595
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
   163
		mutex_exit(&ds->ds_dir->dd_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   164
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
		dprintf_bp(bp, "putting on dead list: %s", "");
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   166
		if (async) {
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   167
			/*
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   168
			 * We are here as part of zio's write done callback,
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   169
			 * which means we're a zio interrupt thread.  We can't
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   170
			 * call dsl_deadlist_insert() now because it may block
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   171
			 * waiting for I/O.  Instead, put bp on the deferred
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   172
			 * queue and let dsl_pool_sync() finish the job.
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   173
			 */
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   174
			bplist_append(&ds->ds_pending_deadlist, bp);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   175
		} else {
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   176
			dsl_deadlist_insert(&ds->ds_deadlist, bp, tx);
10922
e2081f502306 PSARC 2009/571 ZFS Deduplication Properties
Jeff Bonwick <Jeff.Bonwick@Sun.COM>
parents: 10816
diff changeset
   177
		}
5712
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   178
		ASSERT3U(ds->ds_prev->ds_object, ==,
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   179
		    ds->ds_phys->ds_prev_snap_obj);
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   180
		ASSERT(ds->ds_prev->ds_phys->ds_num_children > 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   181
		/* if (bp->blk_birth > prev prev snap txg) prev unique += bs */
5712
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   182
		if (ds->ds_prev->ds_phys->ds_next_snap_obj ==
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   183
		    ds->ds_object && bp->blk_birth >
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   184
		    ds->ds_prev->ds_phys->ds_prev_snap_txg) {
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   185
			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   186
			mutex_enter(&ds->ds_prev->ds_lock);
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   187
			ds->ds_prev->ds_phys->ds_unique_bytes += used;
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
   188
			mutex_exit(&ds->ds_prev->ds_lock);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   189
		}
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
   190
		if (bp->blk_birth > ds->ds_dir->dd_origin_txg) {
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   191
			dsl_dir_transfer_space(ds->ds_dir, used,
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   192
			    DD_USED_HEAD, DD_USED_SNAP, tx);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   193
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   194
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   195
	mutex_enter(&ds->ds_lock);
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
   196
	ASSERT3U(ds->ds_phys->ds_referenced_bytes, >=, used);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
   197
	ds->ds_phys->ds_referenced_bytes -= used;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   198
	ASSERT3U(ds->ds_phys->ds_compressed_bytes, >=, compressed);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   199
	ds->ds_phys->ds_compressed_bytes -= compressed;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   200
	ASSERT3U(ds->ds_phys->ds_uncompressed_bytes, >=, uncompressed);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   201
	ds->ds_phys->ds_uncompressed_bytes -= uncompressed;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   202
	mutex_exit(&ds->ds_lock);
6992
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6975
diff changeset
   203
20c04e18c58c 6573681 deleting a very large file can be slow
maybee
parents: 6975
diff changeset
   204
	return (used);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   205
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   207
uint64_t
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   208
dsl_dataset_prev_snap_txg(dsl_dataset_t *ds)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   209
{
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   210
	uint64_t trysnap = 0;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   211
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   212
	if (ds == NULL)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   213
		return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
	 * The snapshot creation could fail, but that would cause an
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   216
	 * incorrect FALSE return, which would only result in an
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
	 * overestimation of the amount of space that an operation would
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   218
	 * consume, which is OK.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
	 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
	 * There's also a small window where we could miss a pending
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
	 * snapshot, because we could set the sync task in the quiescing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
	 * phase.  So this should only be used as a guess.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   223
	 */
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   224
	if (ds->ds_trysnap_txg >
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   225
	    spa_last_synced_txg(ds->ds_dir->dd_pool->dp_spa))
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   226
		trysnap = ds->ds_trysnap_txg;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   227
	return (MAX(ds->ds_phys->ds_prev_snap_txg, trysnap));
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   228
}
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   229
9653
a70048a304d1 6664765 Unable to remove files when using fat-zap and quota exceeded on ZFS filesystem
Sanjeev Bagewadi <Sanjeev.Bagewadi@Sun.COM>
parents: 9396
diff changeset
   230
boolean_t
12450
c77e20e4e046 6938089 dedup-induced latency causes FC initiator logouts/FC port resets
George Wilson <George.Wilson@Sun.COM>
parents: 12296
diff changeset
   231
dsl_dataset_block_freeable(dsl_dataset_t *ds, const blkptr_t *bp,
c77e20e4e046 6938089 dedup-induced latency causes FC initiator logouts/FC port resets
George Wilson <George.Wilson@Sun.COM>
parents: 12296
diff changeset
   232
    uint64_t blk_birth)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   233
{
12450
c77e20e4e046 6938089 dedup-induced latency causes FC initiator logouts/FC port resets
George Wilson <George.Wilson@Sun.COM>
parents: 12296
diff changeset
   234
	if (blk_birth <= dsl_dataset_prev_snap_txg(ds))
c77e20e4e046 6938089 dedup-induced latency causes FC initiator logouts/FC port resets
George Wilson <George.Wilson@Sun.COM>
parents: 12296
diff changeset
   235
		return (B_FALSE);
c77e20e4e046 6938089 dedup-induced latency causes FC initiator logouts/FC port resets
George Wilson <George.Wilson@Sun.COM>
parents: 12296
diff changeset
   236
12587
16aef3d16470 6957090 ddt_zap_prefetch() induces deadlock, panic
George Wilson <George.Wilson@Sun.COM>
parents: 12527
diff changeset
   237
	ddt_prefetch(dsl_dataset_get_spa(ds), bp);
12450
c77e20e4e046 6938089 dedup-induced latency causes FC initiator logouts/FC port resets
George Wilson <George.Wilson@Sun.COM>
parents: 12296
diff changeset
   238
c77e20e4e046 6938089 dedup-induced latency causes FC initiator logouts/FC port resets
George Wilson <George.Wilson@Sun.COM>
parents: 12296
diff changeset
   239
	return (B_TRUE);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   243
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
dsl_dataset_evict(dmu_buf_t *db, void *dsv)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   246
	dsl_dataset_t *ds = dsv;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   248
	ASSERT(ds->ds_owner == NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
   250
	unique_remove(ds->ds_fsid_guid);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   252
	if (ds->ds_objset != NULL)
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   253
		dmu_objset_evict(ds->ds_objset);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   254
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   255
	if (ds->ds_prev) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   256
		dsl_dataset_rele(ds->ds_prev, ds);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   257
		ds->ds_prev = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   258
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   260
	bplist_destroy(&ds->ds_pending_deadlist);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   261
	if (ds->ds_phys->ds_deadlist_obj != 0)
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   262
		dsl_deadlist_close(&ds->ds_deadlist);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   263
	if (ds->ds_dir)
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   264
		dsl_dir_rele(ds->ds_dir, ds);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
   266
	ASSERT(!list_link_active(&ds->ds_synced_link));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2532
diff changeset
   268
	mutex_destroy(&ds->ds_lock);
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
   269
	mutex_destroy(&ds->ds_opening_lock);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   270
	refcount_destroy(&ds->ds_longholds);
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2532
diff changeset
   271
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
	kmem_free(ds, sizeof (dsl_dataset_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   275
int
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
dsl_dataset_get_snapname(dsl_dataset_t *ds)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
	dsl_dataset_phys_t *headphys;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   279
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
	dmu_buf_t *headdbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
	dsl_pool_t *dp = ds->ds_dir->dd_pool;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   282
	objset_t *mos = dp->dp_meta_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   284
	if (ds->ds_snapname[0])
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   285
		return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
	if (ds->ds_phys->ds_next_snap_obj == 0)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   287
		return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   289
	err = dmu_bonus_hold(mos, ds->ds_dir->dd_phys->dd_head_dataset_obj,
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   290
	    FTAG, &headdbuf);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   291
	if (err != 0)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   292
		return (err);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   293
	headphys = headdbuf->db_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
	err = zap_value_search(dp->dp_meta_objset,
4577
ed36b0e652bc PSARC/2007/328 zfs upgrade
ahrens
parents: 4569
diff changeset
   295
	    headphys->ds_snapnames_zapobj, ds->ds_object, 0, ds->ds_snapname);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   296
	dmu_buf_rele(headdbuf, FTAG);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   297
	return (err);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   300
int
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   301
dsl_dataset_snap_lookup(dsl_dataset_t *ds, const char *name, uint64_t *value)
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   302
{
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   303
	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   304
	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   305
	matchtype_t mt;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   306
	int err;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   307
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   308
	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   309
		mt = MT_FIRST;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   310
	else
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   311
		mt = MT_EXACT;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   312
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   313
	err = zap_lookup_norm(mos, snapobj, name, 8, 1,
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   314
	    value, mt, NULL, 0, NULL);
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   315
	if (err == ENOTSUP && mt == MT_FIRST)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   316
		err = zap_lookup(mos, snapobj, name, 8, 1, value);
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   317
	return (err);
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   318
}
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   319
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   320
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   321
dsl_dataset_snap_remove(dsl_dataset_t *ds, const char *name, dmu_tx_t *tx)
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   322
{
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   323
	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   324
	uint64_t snapobj = ds->ds_phys->ds_snapnames_zapobj;
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   325
	matchtype_t mt;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   326
	int err;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   327
10373
bcf97ee54990 6395956 snapshot dir needs real c/mtime
Chris Kirby <chris.kirby@sun.com>
parents: 10342
diff changeset
   328
	dsl_dir_snap_cmtime_update(ds->ds_dir);
bcf97ee54990 6395956 snapshot dir needs real c/mtime
Chris Kirby <chris.kirby@sun.com>
parents: 10342
diff changeset
   329
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   330
	if (ds->ds_phys->ds_flags & DS_FLAG_CI_DATASET)
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   331
		mt = MT_FIRST;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   332
	else
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   333
		mt = MT_EXACT;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   334
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   335
	err = zap_remove_norm(mos, snapobj, name, mt, tx);
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   336
	if (err == ENOTSUP && mt == MT_FIRST)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   337
		err = zap_remove(mos, snapobj, name, tx);
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   338
	return (err);
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   339
}
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   340
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   341
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   342
dsl_dataset_hold_obj(dsl_pool_t *dp, uint64_t dsobj, void *tag,
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   343
    dsl_dataset_t **dsp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
	objset_t *mos = dp->dp_meta_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
	dmu_buf_t *dbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
	dsl_dataset_t *ds;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   348
	int err;
12798
f6c8601080b4 6938335 zfs send -R can still miss renamed snapshots
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12711
diff changeset
   349
	dmu_object_info_t doi;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   350
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   351
	ASSERT(dsl_pool_config_held(dp));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   353
	err = dmu_bonus_hold(mos, dsobj, tag, &dbuf);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   354
	if (err != 0)
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   355
		return (err);
12798
f6c8601080b4 6938335 zfs send -R can still miss renamed snapshots
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12711
diff changeset
   356
f6c8601080b4 6938335 zfs send -R can still miss renamed snapshots
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12711
diff changeset
   357
	/* Make sure dsobj has the correct object type. */
f6c8601080b4 6938335 zfs send -R can still miss renamed snapshots
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12711
diff changeset
   358
	dmu_object_info_from_db(dbuf, &doi);
14047
e8c1f215cb15 3743 zfs needs a refcount audit
Will Andrews <will@firepipe.net>
parents: 13980
diff changeset
   359
	if (doi.doi_type != DMU_OT_DSL_DATASET) {
e8c1f215cb15 3743 zfs needs a refcount audit
Will Andrews <will@firepipe.net>
parents: 13980
diff changeset
   360
		dmu_buf_rele(dbuf, tag);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
   361
		return (SET_ERROR(EINVAL));
14047
e8c1f215cb15 3743 zfs needs a refcount audit
Will Andrews <will@firepipe.net>
parents: 13980
diff changeset
   362
	}
12798
f6c8601080b4 6938335 zfs send -R can still miss renamed snapshots
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12711
diff changeset
   363
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
	ds = dmu_buf_get_user(dbuf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
	if (ds == NULL) {
13952
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
   366
		dsl_dataset_t *winner = NULL;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   367
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   368
		ds = kmem_zalloc(sizeof (dsl_dataset_t), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   369
		ds->ds_dbuf = dbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
		ds->ds_object = dsobj;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   371
		ds->ds_phys = dbuf->db_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2532
diff changeset
   373
		mutex_init(&ds->ds_lock, NULL, MUTEX_DEFAULT, NULL);
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
   374
		mutex_init(&ds->ds_opening_lock, NULL, MUTEX_DEFAULT, NULL);
13686
4bc0783f6064 2703 add mechanism to report ZFS send progress
Bill Pijewski <wdp@joyent.com>
parents: 13524
diff changeset
   375
		mutex_init(&ds->ds_sendstream_lock, NULL, MUTEX_DEFAULT, NULL);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   376
		refcount_create(&ds->ds_longholds);
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   377
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   378
		bplist_create(&ds->ds_pending_deadlist);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   379
		dsl_deadlist_open(&ds->ds_deadlist,
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   380
		    mos, ds->ds_phys->ds_deadlist_obj);
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   381
13686
4bc0783f6064 2703 add mechanism to report ZFS send progress
Bill Pijewski <wdp@joyent.com>
parents: 13524
diff changeset
   382
		list_create(&ds->ds_sendstreams, sizeof (dmu_sendarg_t),
4bc0783f6064 2703 add mechanism to report ZFS send progress
Bill Pijewski <wdp@joyent.com>
parents: 13524
diff changeset
   383
		    offsetof(dmu_sendarg_t, dsa_link));
4bc0783f6064 2703 add mechanism to report ZFS send progress
Bill Pijewski <wdp@joyent.com>
parents: 13524
diff changeset
   384
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   385
		if (err == 0) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   386
			err = dsl_dir_hold_obj(dp,
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   387
			    ds->ds_phys->ds_dir_obj, NULL, ds, &ds->ds_dir);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   388
		}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   389
		if (err != 0) {
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2532
diff changeset
   390
			mutex_destroy(&ds->ds_lock);
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
   391
			mutex_destroy(&ds->ds_opening_lock);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   392
			refcount_destroy(&ds->ds_longholds);
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   393
			bplist_destroy(&ds->ds_pending_deadlist);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   394
			dsl_deadlist_close(&ds->ds_deadlist);
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   395
			kmem_free(ds, sizeof (dsl_dataset_t));
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   396
			dmu_buf_rele(dbuf, tag);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   397
			return (err);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   398
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   400
		if (!dsl_dataset_is_snapshot(ds)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
			ds->ds_snapname[0] = '\0';
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   402
			if (ds->ds_phys->ds_prev_snap_obj != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   403
				err = dsl_dataset_hold_obj(dp,
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   404
				    ds->ds_phys->ds_prev_snap_obj,
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   405
				    ds, &ds->ds_prev);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
			}
10242
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   407
		} else {
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   408
			if (zfs_flags & ZFS_DEBUG_SNAPNAMES)
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   409
				err = dsl_dataset_get_snapname(ds);
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   410
			if (err == 0 && ds->ds_phys->ds_userrefs_obj != 0) {
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   411
				err = zap_count(
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   412
				    ds->ds_dir->dd_pool->dp_meta_objset,
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   413
				    ds->ds_phys->ds_userrefs_obj,
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   414
				    &ds->ds_userrefs);
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   415
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
   418
		if (err == 0 && !dsl_dataset_is_snapshot(ds)) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   419
			err = dsl_prop_get_int_ds(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   420
			    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   421
			    &ds->ds_reserved);
5475
e140313199cc 6626338 assertion failed: 0 == dsl_prop_get_ds_locked(ds->ds_dir, "refreservation", sizeof (uint64_t), 1, &d
ck153898
parents: 5446
diff changeset
   422
			if (err == 0) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   423
				err = dsl_prop_get_int_ds(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   424
				    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   425
				    &ds->ds_quota);
5475
e140313199cc 6626338 assertion failed: 0 == dsl_prop_get_ds_locked(ds->ds_dir, "refreservation", sizeof (uint64_t), 1, &d
ck153898
parents: 5446
diff changeset
   426
			}
e140313199cc 6626338 assertion failed: 0 == dsl_prop_get_ds_locked(ds->ds_dir, "refreservation", sizeof (uint64_t), 1, &d
ck153898
parents: 5446
diff changeset
   427
		} else {
e140313199cc 6626338 assertion failed: 0 == dsl_prop_get_ds_locked(ds->ds_dir, "refreservation", sizeof (uint64_t), 1, &d
ck153898
parents: 5446
diff changeset
   428
			ds->ds_reserved = ds->ds_quota = 0;
e140313199cc 6626338 assertion failed: 0 == dsl_prop_get_ds_locked(ds->ds_dir, "refreservation", sizeof (uint64_t), 1, &d
ck153898
parents: 5446
diff changeset
   429
		}
e140313199cc 6626338 assertion failed: 0 == dsl_prop_get_ds_locked(ds->ds_dir, "refreservation", sizeof (uint64_t), 1, &d
ck153898
parents: 5446
diff changeset
   430
13952
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
   431
		if (err != 0 || (winner = dmu_buf_set_user_ie(dbuf, ds,
7a22d0770fc8 3522 zfs module should not allow uninitialized variables
George Wilson <george.wilson@delphix.com>
parents: 13941
diff changeset
   432
		    &ds->ds_phys, dsl_dataset_evict)) != NULL) {
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   433
			bplist_destroy(&ds->ds_pending_deadlist);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   434
			dsl_deadlist_close(&ds->ds_deadlist);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   435
			if (ds->ds_prev)
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   436
				dsl_dataset_rele(ds->ds_prev, ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   437
			dsl_dir_rele(ds->ds_dir, ds);
2856
6f4d5ee1906a 6463348 ZFS code could be more portable
nd150628
parents: 2532
diff changeset
   438
			mutex_destroy(&ds->ds_lock);
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
   439
			mutex_destroy(&ds->ds_opening_lock);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   440
			refcount_destroy(&ds->ds_longholds);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
			kmem_free(ds, sizeof (dsl_dataset_t));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   442
			if (err != 0) {
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   443
				dmu_buf_rele(dbuf, tag);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   444
				return (err);
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   445
			}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
			ds = winner;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
		} else {
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
   448
			ds->ds_fsid_guid =
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
			    unique_insert(ds->ds_phys->ds_fsid_guid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   450
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   451
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   452
	ASSERT3P(ds->ds_dbuf, ==, dbuf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
	ASSERT3P(ds->ds_phys, ==, dbuf->db_data);
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   454
	ASSERT(ds->ds_phys->ds_prev_snap_obj != 0 ||
7061
524fec1acf1c 6723703 zpool upgrade -> assertion failed: ds->ds_phys->ds_prev_snap_obj != 0 || ...
ahrens
parents: 7046
diff changeset
   455
	    spa_version(dp->dp_spa) < SPA_VERSION_ORIGIN ||
7077
a63bdc0b8352 6724675 assertion failed in dsl_dataset_get_ref(): ds->ds_phys->ds_prev_snap_obj != 0 ...
ahrens
parents: 7061
diff changeset
   456
	    dp->dp_origin_snap == NULL || ds == dp->dp_origin_snap);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   457
	*dsp = ds;
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   458
	return (0);
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   459
}
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   460
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   462
dsl_dataset_hold(dsl_pool_t *dp, const char *name,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   463
    void *tag, dsl_dataset_t **dsp)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   464
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   465
	dsl_dir_t *dd;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   466
	const char *snapname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   467
	uint64_t obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   468
	int err = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   469
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   470
	err = dsl_dir_hold(dp, name, FTAG, &dd, &snapname);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   471
	if (err != 0)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   472
		return (err);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   473
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   474
	ASSERT(dsl_pool_config_held(dp));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   475
	obj = dd->dd_phys->dd_head_dataset_obj;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   476
	if (obj != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   477
		err = dsl_dataset_hold_obj(dp, obj, tag, dsp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   478
	else
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
   479
		err = SET_ERROR(ENOENT);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   480
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   481
	/* we may be looking for a snapshot */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   482
	if (err == 0 && snapname != NULL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   483
		dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   484
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   485
		if (*snapname++ != '@') {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   486
			dsl_dataset_rele(*dsp, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   487
			dsl_dir_rele(dd, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
   488
			return (SET_ERROR(ENOENT));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   489
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   490
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   491
		dprintf("looking for snapshot '%s'\n", snapname);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   492
		err = dsl_dataset_snap_lookup(*dsp, snapname, &obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   493
		if (err == 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   494
			err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   495
		dsl_dataset_rele(*dsp, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   496
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   497
		if (err == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   498
			mutex_enter(&ds->ds_lock);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   499
			if (ds->ds_snapname[0] == 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   500
				(void) strlcpy(ds->ds_snapname, snapname,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   501
				    sizeof (ds->ds_snapname));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   502
			mutex_exit(&ds->ds_lock);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   503
			*dsp = ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   504
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   505
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   506
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   507
	dsl_dir_rele(dd, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   508
	return (err);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   509
}
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   510
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   511
int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   512
dsl_dataset_own_obj(dsl_pool_t *dp, uint64_t dsobj,
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   513
    void *tag, dsl_dataset_t **dsp)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   514
{
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   515
	int err = dsl_dataset_hold_obj(dp, dsobj, tag, dsp);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   516
	if (err != 0)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   517
		return (err);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   518
	if (!dsl_dataset_tryown(*dsp, tag)) {
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   519
		dsl_dataset_rele(*dsp, tag);
8779
f164e0e90508 6784924 panic while ludelete (zfs destroy)
Mark J Musante <Mark.Musante@Sun.COM>
parents: 8746
diff changeset
   520
		*dsp = NULL;
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
   521
		return (SET_ERROR(EBUSY));
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   522
	}
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   523
	return (0);
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   524
}
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   525
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   526
int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   527
dsl_dataset_own(dsl_pool_t *dp, const char *name,
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   528
    void *tag, dsl_dataset_t **dsp)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   530
	int err = dsl_dataset_hold(dp, name, tag, dsp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   531
	if (err != 0)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   532
		return (err);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   533
	if (!dsl_dataset_tryown(*dsp, tag)) {
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   534
		dsl_dataset_rele(*dsp, tag);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
   535
		return (SET_ERROR(EBUSY));
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   536
	}
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   537
	return (0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   540
/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   541
 * See the comment above dsl_pool_hold() for details.  In summary, a long
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   542
 * hold is used to prevent destruction of a dataset while the pool hold
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   543
 * is dropped, allowing other concurrent operations (e.g. spa_sync()).
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   544
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   545
 * The dataset and pool must be held when this function is called.  After it
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   546
 * is called, the pool hold may be released while the dataset is still held
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   547
 * and accessed.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   548
 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   549
void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   550
dsl_dataset_long_hold(dsl_dataset_t *ds, void *tag)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   551
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   552
	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   553
	(void) refcount_add(&ds->ds_longholds, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   554
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   555
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   556
void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   557
dsl_dataset_long_rele(dsl_dataset_t *ds, void *tag)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   558
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   559
	(void) refcount_remove(&ds->ds_longholds, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   560
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   561
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   562
/* Return B_TRUE if there are any long holds on this dataset. */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   563
boolean_t
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   564
dsl_dataset_long_held(dsl_dataset_t *ds)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   565
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   566
	return (!refcount_is_zero(&ds->ds_longholds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   567
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   568
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
dsl_dataset_name(dsl_dataset_t *ds, char *name)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
	if (ds == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
		(void) strcpy(name, "mos");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
		dsl_dir_name(ds->ds_dir, name);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   576
		VERIFY0(dsl_dataset_get_snapname(ds));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
		if (ds->ds_snapname[0]) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
			(void) strcat(name, "@");
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   579
			/*
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   580
			 * We use a "recursive" mutex so that we
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   581
			 * can call dprintf_ds() with ds_lock held.
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   582
			 */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
			if (!MUTEX_HELD(&ds->ds_lock)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
				mutex_enter(&ds->ds_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
				(void) strcat(name, ds->ds_snapname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
				mutex_exit(&ds->ds_lock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
				(void) strcat(name, ds->ds_snapname);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   589
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   590
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   591
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
3978
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   594
static int
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   595
dsl_dataset_namelen(dsl_dataset_t *ds)
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   596
{
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   597
	int result;
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   598
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   599
	if (ds == NULL) {
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   600
		result = 3;	/* "mos" */
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   601
	} else {
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   602
		result = dsl_dir_namelen(ds->ds_dir);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   603
		VERIFY0(dsl_dataset_get_snapname(ds));
3978
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   604
		if (ds->ds_snapname[0]) {
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   605
			++result;	/* adding one for the @-sign */
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   606
			if (!MUTEX_HELD(&ds->ds_lock)) {
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   607
				mutex_enter(&ds->ds_lock);
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   608
				result += strlen(ds->ds_snapname);
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   609
				mutex_exit(&ds->ds_lock);
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   610
			} else {
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   611
				result += strlen(ds->ds_snapname);
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   612
			}
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   613
		}
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   614
	}
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   615
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   616
	return (result);
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   617
}
2dd668007b7a 6533813 recursive snapshotting resulted in a bad stack overflow
mmusante
parents: 3912
diff changeset
   618
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   619
void
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   620
dsl_dataset_rele(dsl_dataset_t *ds, void *tag)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
{
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   622
	dmu_buf_rele(ds->ds_dbuf, tag);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
void
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   626
dsl_dataset_disown(dsl_dataset_t *ds, void *tag)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   627
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   628
	ASSERT(ds->ds_owner == tag && ds->ds_dbuf != NULL);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   629
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   630
	mutex_enter(&ds->ds_lock);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   631
	ds->ds_owner = NULL;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   632
	mutex_exit(&ds->ds_lock);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   633
	dsl_dataset_long_rele(ds, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   634
	if (ds->ds_dbuf != NULL)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   635
		dsl_dataset_rele(ds, tag);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   636
	else
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   637
		dsl_dataset_evict(NULL, ds);
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   638
}
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   639
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   640
boolean_t
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   641
dsl_dataset_tryown(dsl_dataset_t *ds, void *tag)
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   642
{
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   643
	boolean_t gotit = FALSE;
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   644
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   645
	mutex_enter(&ds->ds_lock);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   646
	if (ds->ds_owner == NULL && !DS_IS_INCONSISTENT(ds)) {
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   647
		ds->ds_owner = tag;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   648
		dsl_dataset_long_hold(ds, tag);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   649
		gotit = TRUE;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   650
	}
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   651
	mutex_exit(&ds->ds_lock);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   652
	return (gotit);
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   653
}
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   654
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   655
uint64_t
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   656
dsl_dataset_create_sync_dd(dsl_dir_t *dd, dsl_dataset_t *origin,
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   657
    uint64_t flags, dmu_tx_t *tx)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
{
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   659
	dsl_pool_t *dp = dd->dd_pool;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
	dmu_buf_t *dbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
	dsl_dataset_phys_t *dsphys;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   662
	uint64_t dsobj;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
	objset_t *mos = dp->dp_meta_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   665
	if (origin == NULL)
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   666
		origin = dp->dp_origin_snap;
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   667
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   668
	ASSERT(origin == NULL || origin->ds_dir->dd_pool == dp);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   669
	ASSERT(origin == NULL || origin->ds_phys->ds_num_children > 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
	ASSERT(dmu_tx_is_syncing(tx));
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   671
	ASSERT(dd->dd_phys->dd_head_dataset_obj == 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
928
36d72fe4da29 6349314 dmu_object_type names incorrect for DSL Directories and DSL Datasets
tabriz
parents: 885
diff changeset
   673
	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
36d72fe4da29 6349314 dmu_object_type names incorrect for DSL Directories and DSL Datasets
tabriz
parents: 885
diff changeset
   674
	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   675
	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
	dmu_buf_will_dirty(dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	dsphys = dbuf->db_data;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
   678
	bzero(dsphys, sizeof (dsl_dataset_phys_t));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
	dsphys->ds_dir_obj = dd->dd_object;
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   680
	dsphys->ds_flags = flags;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
	dsphys->ds_fsid_guid = unique_create();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
	    sizeof (dsphys->ds_guid));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
	dsphys->ds_snapnames_zapobj =
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   685
	    zap_create_norm(mos, U8_TEXTPREP_TOUPPER, DMU_OT_DSL_DS_SNAP_MAP,
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   686
	    DMU_OT_NONE, 0, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
	dsphys->ds_creation_time = gethrestime_sec();
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   688
	dsphys->ds_creation_txg = tx->tx_txg == TXG_INITIAL ? 1 : tx->tx_txg;
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   689
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   690
	if (origin == NULL) {
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   691
		dsphys->ds_deadlist_obj = dsl_deadlist_alloc(mos, tx);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   692
	} else {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   693
		dsl_dataset_t *ohds; /* head of the origin snapshot */
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   694
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   695
		dsphys->ds_prev_snap_obj = origin->ds_object;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
		dsphys->ds_prev_snap_txg =
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   697
		    origin->ds_phys->ds_creation_txg;
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
   698
		dsphys->ds_referenced_bytes =
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
   699
		    origin->ds_phys->ds_referenced_bytes;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
		dsphys->ds_compressed_bytes =
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   701
		    origin->ds_phys->ds_compressed_bytes;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
		dsphys->ds_uncompressed_bytes =
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   703
		    origin->ds_phys->ds_uncompressed_bytes;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   704
		dsphys->ds_bp = origin->ds_phys->ds_bp;
6502
66aef5f20377 6694731 drc->drc_real_ds->ds_phys->ds_flags & (1ULL<<0), file: ../../common/fs/zfs/dmu_send.c, line: 1069
timh
parents: 6492
diff changeset
   705
		dsphys->ds_flags |= origin->ds_phys->ds_flags;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   707
		dmu_buf_will_dirty(origin->ds_dbuf, tx);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   708
		origin->ds_phys->ds_num_children++;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   710
		VERIFY0(dsl_dataset_hold_obj(dp,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   711
		    origin->ds_dir->dd_phys->dd_head_dataset_obj, FTAG, &ohds));
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   712
		dsphys->ds_deadlist_obj = dsl_deadlist_clone(&ohds->ds_deadlist,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   713
		    dsphys->ds_prev_snap_txg, dsphys->ds_prev_snap_obj, tx);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   714
		dsl_dataset_rele(ohds, FTAG);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   715
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   716
		if (spa_version(dp->dp_spa) >= SPA_VERSION_NEXT_CLONES) {
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   717
			if (origin->ds_phys->ds_next_clones_obj == 0) {
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   718
				origin->ds_phys->ds_next_clones_obj =
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   719
				    zap_create(mos,
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   720
				    DMU_OT_NEXT_CLONES, DMU_OT_NONE, 0, tx);
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   721
			}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   722
			VERIFY0(zap_add_int(mos,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   723
			    origin->ds_phys->ds_next_clones_obj, dsobj, tx));
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   724
		}
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   725
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
		dmu_buf_will_dirty(dd->dd_dbuf, tx);
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   727
		dd->dd_phys->dd_origin_obj = origin->ds_object;
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   728
		if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   729
			if (origin->ds_dir->dd_phys->dd_clones == 0) {
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   730
				dmu_buf_will_dirty(origin->ds_dir->dd_dbuf, tx);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   731
				origin->ds_dir->dd_phys->dd_clones =
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   732
				    zap_create(mos,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   733
				    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   734
			}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   735
			VERIFY0(zap_add_int(mos,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   736
			    origin->ds_dir->dd_phys->dd_clones, dsobj, tx));
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
   737
		}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
	}
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   739
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   740
	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   741
		dsphys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   742
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
   743
	dmu_buf_rele(dbuf, FTAG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   744
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   745
	dmu_buf_will_dirty(dd->dd_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
	dd->dd_phys->dd_head_dataset_obj = dsobj;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   747
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   748
	return (dsobj);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   749
}
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   750
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   751
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   752
dsl_dataset_zero_zil(dsl_dataset_t *ds, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   753
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   754
	objset_t *os;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   755
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   756
	VERIFY0(dmu_objset_from_ds(ds, &os));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   757
	bzero(&os->os_zil_header, sizeof (os->os_zil_header));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   758
	dsl_dataset_dirty(ds, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   759
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   760
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   761
uint64_t
6492
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   762
dsl_dataset_create_sync(dsl_dir_t *pdd, const char *lastname,
903545192033 6654808 FIGNORECASE lookups in a zfs xattr dir don't provide 'realname' data
timh
parents: 6047
diff changeset
   763
    dsl_dataset_t *origin, uint64_t flags, cred_t *cr, dmu_tx_t *tx)
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   764
{
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   765
	dsl_pool_t *dp = pdd->dd_pool;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   766
	uint64_t dsobj, ddobj;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   767
	dsl_dir_t *dd;
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   768
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   769
	ASSERT(dmu_tx_is_syncing(tx));
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   770
	ASSERT(lastname[0] != '@');
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   771
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
   772
	ddobj = dsl_dir_create_sync(dp, pdd, lastname, tx);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   773
	VERIFY0(dsl_dir_hold_obj(dp, ddobj, lastname, FTAG, &dd));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   774
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   775
	dsobj = dsl_dataset_create_sync_dd(dd, origin,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   776
	    flags & ~DS_CREATE_FLAG_NODIRTY, tx);
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   777
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   778
	dsl_deleg_set_create_perms(dd, tx, cr);
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
   779
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   780
	dsl_dir_rele(dd, FTAG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
12982
5d7f2db1e620 6972862 rollback can leak log blocks
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 12840
diff changeset
   782
	/*
5d7f2db1e620 6972862 rollback can leak log blocks
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 12840
diff changeset
   783
	 * If we are creating a clone, make sure we zero out any stale
5d7f2db1e620 6972862 rollback can leak log blocks
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 12840
diff changeset
   784
	 * data from the origin snapshots zil header.
5d7f2db1e620 6972862 rollback can leak log blocks
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 12840
diff changeset
   785
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   786
	if (origin != NULL && !(flags & DS_CREATE_FLAG_NODIRTY)) {
12982
5d7f2db1e620 6972862 rollback can leak log blocks
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 12840
diff changeset
   787
		dsl_dataset_t *ds;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   788
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   789
		VERIFY0(dsl_dataset_hold_obj(dp, dsobj, FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   790
		dsl_dataset_zero_zil(ds, tx);
12982
5d7f2db1e620 6972862 rollback can leak log blocks
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 12840
diff changeset
   791
		dsl_dataset_rele(ds, FTAG);
5d7f2db1e620 6972862 rollback can leak log blocks
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 12840
diff changeset
   792
	}
5d7f2db1e620 6972862 rollback can leak log blocks
Mark Maybee <Mark.Maybee@Sun.COM>
parents: 12840
diff changeset
   793
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   794
	return (dsobj);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   795
}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   796
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   797
/*
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   798
 * The unique space in the head dataset can be calculated by subtracting
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   799
 * the space used in the most recent snapshot, that is still being used
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   800
 * in this file system, from the space currently in use.  To figure out
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   801
 * the space in the most recent snapshot still in use, we need to take
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   802
 * the total space used in the snapshot and subtract out the space that
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   803
 * has been freed up since the snapshot was taken.
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   804
 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   805
void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   806
dsl_dataset_recalc_head_uniq(dsl_dataset_t *ds)
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   807
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   808
	uint64_t mrs_used;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   809
	uint64_t dlused, dlcomp, dluncomp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   810
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   811
	ASSERT(!dsl_dataset_is_snapshot(ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   812
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   813
	if (ds->ds_phys->ds_prev_snap_obj != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   814
		mrs_used = ds->ds_prev->ds_phys->ds_referenced_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   815
	else
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   816
		mrs_used = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   817
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   818
	dsl_deadlist_space(&ds->ds_deadlist, &dlused, &dlcomp, &dluncomp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   819
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   820
	ASSERT3U(dlused, <=, mrs_used);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   821
	ds->ds_phys->ds_unique_bytes =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   822
	    ds->ds_phys->ds_referenced_bytes - (mrs_used - dlused);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   823
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   824
	if (spa_version(ds->ds_dir->dd_pool->dp_spa) >=
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   825
	    SPA_VERSION_UNIQUE_ACCURATE)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   826
		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   827
}
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   828
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   829
void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   830
dsl_dataset_remove_from_next_clones(dsl_dataset_t *ds, uint64_t obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   831
    dmu_tx_t *tx)
10242
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   832
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   833
	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   834
	uint64_t count;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
	int err;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   836
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   837
	ASSERT(ds->ds_phys->ds_num_children >= 2);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   838
	err = zap_remove_int(mos, ds->ds_phys->ds_next_clones_obj, obj, tx);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   839
	/*
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   840
	 * The err should not be ENOENT, but a bug in a previous version
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   841
	 * of the code could cause upgrade_clones_cb() to not set
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   842
	 * ds_next_snap_obj when it should, leading to a missing entry.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   843
	 * If we knew that the pool was created after
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   844
	 * SPA_VERSION_NEXT_CLONES, we could assert that it isn't
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   845
	 * ENOENT.  However, at least we can check that we don't have
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   846
	 * too many entries in the next_clones_obj even after failing to
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   847
	 * remove this one.
10242
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
   848
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   849
	if (err != ENOENT)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   850
		VERIFY0(err);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   851
	ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   852
	    &count));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   853
	ASSERT3U(count, <=, ds->ds_phys->ds_num_children - 2);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   854
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   856
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3444
diff changeset
   857
blkptr_t *
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3444
diff changeset
   858
dsl_dataset_get_blkptr(dsl_dataset_t *ds)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   859
{
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3444
diff changeset
   860
	return (&ds->ds_phys->ds_bp);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
dsl_dataset_set_blkptr(dsl_dataset_t *ds, blkptr_t *bp, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
	ASSERT(dmu_tx_is_syncing(tx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
	/* If it's the meta-objset, set dp_meta_rootbp */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
	if (ds == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
		tx->tx_pool->dp_meta_rootbp = *bp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
		dmu_buf_will_dirty(ds->ds_dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   872
		ds->ds_phys->ds_bp = *bp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   873
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   875
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   876
spa_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   877
dsl_dataset_get_spa(dsl_dataset_t *ds)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   878
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   879
	return (ds->ds_dir->dd_pool->dp_spa);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   880
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   881
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   882
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   883
dsl_dataset_dirty(dsl_dataset_t *ds, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   884
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
	dsl_pool_t *dp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   886
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
	if (ds == NULL) /* this is the meta-objset */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
   890
	ASSERT(ds->ds_objset != NULL);
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   891
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   892
	if (ds->ds_phys->ds_next_snap_obj != 0)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
   893
		panic("dirtying snapshot!");
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   894
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   895
	dp = ds->ds_dir->dd_pool;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   896
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   897
	if (txg_list_add(&dp->dp_dirty_datasets, ds, tx->tx_txg)) {
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   898
		/* up the hold count until we can be written out */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   899
		dmu_buf_add_ref(ds->ds_dbuf, ds);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   900
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   901
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   902
13780
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   903
boolean_t
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   904
dsl_dataset_is_dirty(dsl_dataset_t *ds)
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   905
{
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   906
	for (int t = 0; t < TXG_SIZE; t++) {
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   907
		if (txg_list_member(&ds->ds_dir->dd_pool->dp_dirty_datasets,
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   908
		    ds, t))
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   909
			return (B_TRUE);
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   910
	}
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   911
	return (B_FALSE);
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   912
}
6da32a929222 3100 zvol rename fails with EBUSY when dirty
Matthew Ahrens <mahrens@delphix.com>
parents: 13776
diff changeset
   913
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   914
static int
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   915
dsl_dataset_snapshot_reserve_space(dsl_dataset_t *ds, dmu_tx_t *tx)
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   916
{
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   917
	uint64_t asize;
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   918
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   919
	if (!dmu_tx_is_syncing(tx))
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   920
		return (0);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   921
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   922
	/*
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   923
	 * If there's an fs-only reservation, any blocks that might become
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   924
	 * owned by the snapshot dataset must be accommodated by space
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   925
	 * outside of the reservation.
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   926
	 */
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
   927
	ASSERT(ds->ds_reserved == 0 || DS_UNIQUE_IS_ACCURATE(ds));
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
   928
	asize = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
12839
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
   929
	if (asize > dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE))
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
   930
		return (SET_ERROR(ENOSPC));
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   931
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   932
	/*
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
   933
	 * Propagate any reserved space for this snapshot to other
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   934
	 * snapshot checks in this sync group.
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   935
	 */
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   936
	if (asize > 0)
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   937
		dsl_dir_willuse_space(ds->ds_dir, asize, tx);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   938
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   939
	return (0);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   940
}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
   941
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   942
typedef struct dsl_dataset_snapshot_arg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   943
	nvlist_t *ddsa_snaps;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   944
	nvlist_t *ddsa_props;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   945
	nvlist_t *ddsa_errors;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   946
} dsl_dataset_snapshot_arg_t;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   947
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   948
int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   949
dsl_dataset_snapshot_check_impl(dsl_dataset_t *ds, const char *snapname,
14092
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   950
    dmu_tx_t *tx, boolean_t recv)
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   951
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   952
	int error;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   953
	uint64_t value;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   955
	ds->ds_trysnap_txg = tx->tx_txg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   956
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   957
	if (!dmu_tx_is_syncing(tx))
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   958
		return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   959
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
	/*
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   961
	 * We don't allow multiple snapshots of the same txg.  If there
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   962
	 * is already one, try again.
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   963
	 */
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   964
	if (ds->ds_phys->ds_prev_snap_txg >= tx->tx_txg)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
   965
		return (SET_ERROR(EAGAIN));
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   966
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
   967
	/*
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
   968
	 * Check for conflicting snapshot name.
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   969
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   970
	error = dsl_dataset_snap_lookup(ds, snapname, &value);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   971
	if (error == 0)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
   972
		return (SET_ERROR(EEXIST));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   973
	if (error != ENOENT)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   974
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   975
14092
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   976
	/*
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   977
	 * We don't allow taking snapshots of inconsistent datasets, such as
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   978
	 * those into which we are currently receiving.  However, if we are
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   979
	 * creating this snapshot as part of a receive, this check will be
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   980
	 * executed atomically with respect to the completion of the receive
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   981
	 * itself but prior to the clearing of DS_FLAG_INCONSISTENT; in this
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   982
	 * case we ignore this, knowing it will be fixed up for us shortly in
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   983
	 * dmu_recv_end_sync().
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   984
	 */
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   985
	if (!recv && DS_IS_INCONSISTENT(ds))
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   986
		return (SET_ERROR(EBUSY));
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
   987
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   988
	error = dsl_dataset_snapshot_reserve_space(ds, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   989
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   990
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   991
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   992
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   993
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   995
static int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   996
dsl_dataset_snapshot_check(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   997
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   998
	dsl_dataset_snapshot_arg_t *ddsa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
   999
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1000
	nvpair_t *pair;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1001
	int rv = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1002
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1003
	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1004
	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1005
		int error = 0;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1006
		dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1007
		char *name, *atp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1008
		char dsname[MAXNAMELEN];
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1009
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1010
		name = nvpair_name(pair);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1011
		if (strlen(name) >= MAXNAMELEN)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1012
			error = SET_ERROR(ENAMETOOLONG);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1013
		if (error == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1014
			atp = strchr(name, '@');
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1015
			if (atp == NULL)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1016
				error = SET_ERROR(EINVAL);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1017
			if (error == 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1018
				(void) strlcpy(dsname, name, atp - name + 1);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1019
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1020
		if (error == 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1021
			error = dsl_dataset_hold(dp, dsname, FTAG, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1022
		if (error == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1023
			error = dsl_dataset_snapshot_check_impl(ds,
14092
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
  1024
			    atp + 1, tx, B_FALSE);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1025
			dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1026
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1027
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1028
		if (error != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1029
			if (ddsa->ddsa_errors != NULL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1030
				fnvlist_add_int32(ddsa->ddsa_errors,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1031
				    name, error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1032
			}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1033
			rv = error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1034
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1035
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1036
	return (rv);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1037
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1038
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1039
void
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1040
dsl_dataset_snapshot_sync_impl(dsl_dataset_t *ds, const char *snapname,
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1041
    dmu_tx_t *tx)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1042
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1043
	static zil_header_t zero_zil;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1044
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1045
	dsl_pool_t *dp = ds->ds_dir->dd_pool;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1046
	dmu_buf_t *dbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1047
	dsl_dataset_phys_t *dsphys;
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1048
	uint64_t dsobj, crtxg;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1049
	objset_t *mos = dp->dp_meta_objset;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1050
	objset_t *os;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1051
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1052
	ASSERT(RRW_WRITE_HELD(&dp->dp_config_rwlock));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1053
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1054
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1055
	 * If we are on an old pool, the zil must not be active, in which
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1056
	 * case it will be zeroed.  Usually zil_suspend() accomplishes this.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1057
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1058
	ASSERT(spa_version(dmu_tx_pool(tx)->dp_spa) >= SPA_VERSION_FAST_SNAP ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1059
	    dmu_objset_from_ds(ds, &os) != 0 ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1060
	    bcmp(&os->os_phys->os_zil_header, &zero_zil,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1061
	    sizeof (zero_zil)) == 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1062
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1063
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1064
	/*
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1065
	 * The origin's ds_creation_txg has to be < TXG_INITIAL
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1066
	 */
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1067
	if (strcmp(snapname, ORIGIN_DIR_NAME) == 0)
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1068
		crtxg = 1;
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1069
	else
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1070
		crtxg = tx->tx_txg;
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1071
928
36d72fe4da29 6349314 dmu_object_type names incorrect for DSL Directories and DSL Datasets
tabriz
parents: 885
diff changeset
  1072
	dsobj = dmu_object_alloc(mos, DMU_OT_DSL_DATASET, 0,
36d72fe4da29 6349314 dmu_object_type names incorrect for DSL Directories and DSL Datasets
tabriz
parents: 885
diff changeset
  1073
	    DMU_OT_DSL_DATASET, sizeof (dsl_dataset_phys_t), tx);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1074
	VERIFY0(dmu_bonus_hold(mos, dsobj, FTAG, &dbuf));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1075
	dmu_buf_will_dirty(dbuf, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1076
	dsphys = dbuf->db_data;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1077
	bzero(dsphys, sizeof (dsl_dataset_phys_t));
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1078
	dsphys->ds_dir_obj = ds->ds_dir->dd_object;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1079
	dsphys->ds_fsid_guid = unique_create();
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1080
	(void) random_get_pseudo_bytes((void*)&dsphys->ds_guid,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1081
	    sizeof (dsphys->ds_guid));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1082
	dsphys->ds_prev_snap_obj = ds->ds_phys->ds_prev_snap_obj;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1083
	dsphys->ds_prev_snap_txg = ds->ds_phys->ds_prev_snap_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1084
	dsphys->ds_next_snap_obj = ds->ds_object;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1085
	dsphys->ds_num_children = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1086
	dsphys->ds_creation_time = gethrestime_sec();
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1087
	dsphys->ds_creation_txg = crtxg;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1088
	dsphys->ds_deadlist_obj = ds->ds_phys->ds_deadlist_obj;
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  1089
	dsphys->ds_referenced_bytes = ds->ds_phys->ds_referenced_bytes;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1090
	dsphys->ds_compressed_bytes = ds->ds_phys->ds_compressed_bytes;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1091
	dsphys->ds_uncompressed_bytes = ds->ds_phys->ds_uncompressed_bytes;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1092
	dsphys->ds_flags = ds->ds_phys->ds_flags;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1093
	dsphys->ds_bp = ds->ds_phys->ds_bp;
1544
938876158511 PSARC 2006/077 zpool clear
eschrock
parents: 928
diff changeset
  1094
	dmu_buf_rele(dbuf, FTAG);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1095
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1096
	ASSERT3U(ds->ds_prev != 0, ==, ds->ds_phys->ds_prev_snap_obj != 0);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1097
	if (ds->ds_prev) {
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1098
		uint64_t next_clones_obj =
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1099
		    ds->ds_prev->ds_phys->ds_next_clones_obj;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1100
		ASSERT(ds->ds_prev->ds_phys->ds_next_snap_obj ==
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1101
		    ds->ds_object ||
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1102
		    ds->ds_prev->ds_phys->ds_num_children > 1);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1103
		if (ds->ds_prev->ds_phys->ds_next_snap_obj == ds->ds_object) {
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1104
			dmu_buf_will_dirty(ds->ds_prev->ds_dbuf, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1105
			ASSERT3U(ds->ds_phys->ds_prev_snap_txg, ==,
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1106
			    ds->ds_prev->ds_phys->ds_creation_txg);
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1107
			ds->ds_prev->ds_phys->ds_next_snap_obj = dsobj;
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1108
		} else if (next_clones_obj != 0) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1109
			dsl_dataset_remove_from_next_clones(ds->ds_prev,
10801
e0bf032e8673 6822816 assertion failed: zap_remove_int(ds_next_clones_obj) returns ENOENT
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10799
diff changeset
  1110
			    dsphys->ds_next_snap_obj, tx);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1111
			VERIFY0(zap_add_int(mos,
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1112
			    next_clones_obj, dsobj, tx));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1113
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1114
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1115
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1116
	/*
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1117
	 * If we have a reference-reservation on this dataset, we will
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1118
	 * need to increase the amount of refreservation being charged
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1119
	 * since our unique space is going to zero.
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1120
	 */
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1121
	if (ds->ds_reserved) {
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1122
		int64_t delta;
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1123
		ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1124
		delta = MIN(ds->ds_phys->ds_unique_bytes, ds->ds_reserved);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1125
		dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV,
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1126
		    delta, 0, 0, tx);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1127
	}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1128
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1129
	dmu_buf_will_dirty(ds->ds_dbuf, tx);
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1130
	ds->ds_phys->ds_deadlist_obj = dsl_deadlist_clone(&ds->ds_deadlist,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1131
	    UINT64_MAX, ds->ds_phys->ds_prev_snap_obj, tx);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1132
	dsl_deadlist_close(&ds->ds_deadlist);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1133
	dsl_deadlist_open(&ds->ds_deadlist, mos, ds->ds_phys->ds_deadlist_obj);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1134
	dsl_deadlist_add_key(&ds->ds_deadlist,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1135
	    ds->ds_phys->ds_prev_snap_txg, tx);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1136
5712
81f1af42bafc 6628232 zfs snapshot -r is very slow, causes systemic slowdown
ahrens
parents: 5569
diff changeset
  1137
	ASSERT3U(ds->ds_phys->ds_prev_snap_txg, <, tx->tx_txg);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1138
	ds->ds_phys->ds_prev_snap_obj = dsobj;
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1139
	ds->ds_phys->ds_prev_snap_txg = crtxg;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
	ds->ds_phys->ds_unique_bytes = 0;
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1141
	if (spa_version(dp->dp_spa) >= SPA_VERSION_UNIQUE_ACCURATE)
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1142
		ds->ds_phys->ds_flags |= DS_FLAG_UNIQUE_ACCURATE;
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1143
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1144
	VERIFY0(zap_add(mos, ds->ds_phys->ds_snapnames_zapobj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1145
	    snapname, 8, 1, &dsobj, tx));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1146
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1147
	if (ds->ds_prev)
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1148
		dsl_dataset_rele(ds->ds_prev, ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1149
	VERIFY0(dsl_dataset_hold_obj(dp,
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1150
	    ds->ds_phys->ds_prev_snap_obj, ds, &ds->ds_prev));
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4009
diff changeset
  1151
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1152
	dsl_scan_ds_snapshotted(ds, tx);
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1153
10373
bcf97ee54990 6395956 snapshot dir needs real c/mtime
Chris Kirby <chris.kirby@sun.com>
parents: 10342
diff changeset
  1154
	dsl_dir_snap_cmtime_update(ds->ds_dir);
bcf97ee54990 6395956 snapshot dir needs real c/mtime
Chris Kirby <chris.kirby@sun.com>
parents: 10342
diff changeset
  1155
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1156
	spa_history_log_internal_ds(ds->ds_prev, "snapshot", tx, "");
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1159
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1160
dsl_dataset_snapshot_sync(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1161
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1162
	dsl_dataset_snapshot_arg_t *ddsa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1163
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1164
	nvpair_t *pair;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1165
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1166
	for (pair = nvlist_next_nvpair(ddsa->ddsa_snaps, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1167
	    pair != NULL; pair = nvlist_next_nvpair(ddsa->ddsa_snaps, pair)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1168
		dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1169
		char *name, *atp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1170
		char dsname[MAXNAMELEN];
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1171
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1172
		name = nvpair_name(pair);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1173
		atp = strchr(name, '@');
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1174
		(void) strlcpy(dsname, name, atp - name + 1);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1175
		VERIFY0(dsl_dataset_hold(dp, dsname, FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1176
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1177
		dsl_dataset_snapshot_sync_impl(ds, atp + 1, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1178
		if (ddsa->ddsa_props != NULL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1179
			dsl_props_set_sync_impl(ds->ds_prev,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1180
			    ZPROP_SRC_LOCAL, ddsa->ddsa_props, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1181
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1182
		dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1183
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1184
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1185
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1186
/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1187
 * The snapshots must all be in the same pool.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1188
 * All-or-nothing: if there are any failures, nothing will be modified.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1189
 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1190
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1191
dsl_dataset_snapshot(nvlist_t *snaps, nvlist_t *props, nvlist_t *errors)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1192
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1193
	dsl_dataset_snapshot_arg_t ddsa;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1194
	nvpair_t *pair;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1195
	boolean_t needsuspend;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1196
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1197
	spa_t *spa;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1198
	char *firstname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1199
	nvlist_t *suspended = NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1200
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1201
	pair = nvlist_next_nvpair(snaps, NULL);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1202
	if (pair == NULL)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1203
		return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1204
	firstname = nvpair_name(pair);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1205
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1206
	error = spa_open(firstname, &spa, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1207
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1208
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1209
	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1210
	spa_close(spa, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1211
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1212
	if (needsuspend) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1213
		suspended = fnvlist_alloc();
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1214
		for (pair = nvlist_next_nvpair(snaps, NULL); pair != NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1215
		    pair = nvlist_next_nvpair(snaps, pair)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1216
			char fsname[MAXNAMELEN];
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1217
			char *snapname = nvpair_name(pair);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1218
			char *atp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1219
			void *cookie;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1220
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1221
			atp = strchr(snapname, '@');
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1222
			if (atp == NULL) {
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1223
				error = SET_ERROR(EINVAL);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1224
				break;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1225
			}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1226
			(void) strlcpy(fsname, snapname, atp - snapname + 1);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1227
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1228
			error = zil_suspend(fsname, &cookie);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1229
			if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1230
				break;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1231
			fnvlist_add_uint64(suspended, fsname,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1232
			    (uintptr_t)cookie);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1233
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1234
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1235
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1236
	ddsa.ddsa_snaps = snaps;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1237
	ddsa.ddsa_props = props;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1238
	ddsa.ddsa_errors = errors;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1239
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1240
	if (error == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1241
		error = dsl_sync_task(firstname, dsl_dataset_snapshot_check,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1242
		    dsl_dataset_snapshot_sync, &ddsa,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1243
		    fnvlist_num_pairs(snaps) * 3);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1244
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1245
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1246
	if (suspended != NULL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1247
		for (pair = nvlist_next_nvpair(suspended, NULL); pair != NULL;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1248
		    pair = nvlist_next_nvpair(suspended, pair)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1249
			zil_resume((void *)(uintptr_t)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1250
			    fnvpair_value_uint64(pair));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1251
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1252
		fnvlist_free(suspended);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1253
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1254
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1255
	return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1256
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1257
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1258
typedef struct dsl_dataset_snapshot_tmp_arg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1259
	const char *ddsta_fsname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1260
	const char *ddsta_snapname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1261
	minor_t ddsta_cleanup_minor;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1262
	const char *ddsta_htag;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1263
} dsl_dataset_snapshot_tmp_arg_t;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1264
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1265
static int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1266
dsl_dataset_snapshot_tmp_check(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1267
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1268
	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1269
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1270
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1271
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1272
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1273
	error = dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1274
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1275
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1276
14092
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
  1277
	error = dsl_dataset_snapshot_check_impl(ds, ddsta->ddsta_snapname,
520f3adc5d46 3894 zfs should not allow snapshot of inconsistent dataset
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14091
diff changeset
  1278
	    tx, B_FALSE);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1279
	if (error != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1280
		dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1281
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1282
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1283
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1284
	if (spa_version(dp->dp_spa) < SPA_VERSION_USERREFS) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1285
		dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1286
		return (SET_ERROR(ENOTSUP));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1287
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1288
	error = dsl_dataset_user_hold_check_one(NULL, ddsta->ddsta_htag,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1289
	    B_TRUE, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1290
	if (error != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1291
		dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1292
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1293
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1294
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1295
	dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1296
	return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1297
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1298
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1299
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1300
dsl_dataset_snapshot_tmp_sync(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1301
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1302
	dsl_dataset_snapshot_tmp_arg_t *ddsta = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1303
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1304
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1305
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1306
	VERIFY0(dsl_dataset_hold(dp, ddsta->ddsta_fsname, FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1307
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1308
	dsl_dataset_snapshot_sync_impl(ds, ddsta->ddsta_snapname, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1309
	dsl_dataset_user_hold_sync_one(ds->ds_prev, ddsta->ddsta_htag,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1310
	    ddsta->ddsta_cleanup_minor, gethrestime_sec(), tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1311
	dsl_destroy_snapshot_sync_impl(ds->ds_prev, B_TRUE, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1312
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1313
	dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1314
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1315
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1316
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1317
dsl_dataset_snapshot_tmp(const char *fsname, const char *snapname,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1318
    minor_t cleanup_minor, const char *htag)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1319
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1320
	dsl_dataset_snapshot_tmp_arg_t ddsta;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1321
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1322
	spa_t *spa;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1323
	boolean_t needsuspend;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1324
	void *cookie;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1325
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1326
	ddsta.ddsta_fsname = fsname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1327
	ddsta.ddsta_snapname = snapname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1328
	ddsta.ddsta_cleanup_minor = cleanup_minor;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1329
	ddsta.ddsta_htag = htag;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1330
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1331
	error = spa_open(fsname, &spa, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1332
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1333
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1334
	needsuspend = (spa_version(spa) < SPA_VERSION_FAST_SNAP);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1335
	spa_close(spa, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1336
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1337
	if (needsuspend) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1338
		error = zil_suspend(fsname, &cookie);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1339
		if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1340
			return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1341
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1342
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1343
	error = dsl_sync_task(fsname, dsl_dataset_snapshot_tmp_check,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1344
	    dsl_dataset_snapshot_tmp_sync, &ddsta, 3);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1345
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1346
	if (needsuspend)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1347
		zil_resume(cookie);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1348
	return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1349
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1350
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1351
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1352
void
3547
e396e0a440b1 6512391 DMU should leverage ZIO dependencies to achieve greater parallelism
maybee
parents: 3444
diff changeset
  1353
dsl_dataset_sync(dsl_dataset_t *ds, zio_t *zio, dmu_tx_t *tx)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1354
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1355
	ASSERT(dmu_tx_is_syncing(tx));
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
  1356
	ASSERT(ds->ds_objset != NULL);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1357
	ASSERT(ds->ds_phys->ds_next_snap_obj == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1358
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
  1359
	/*
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
  1360
	 * in case we had to change ds_fsid_guid when we opened it,
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
  1361
	 * sync it out now.
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
  1362
	 */
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
  1363
	dmu_buf_will_dirty(ds->ds_dbuf, tx);
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
  1364
	ds->ds_phys->ds_fsid_guid = ds->ds_fsid_guid;
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
  1365
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
  1366
	dmu_objset_sync(ds->ds_objset, zio, tx);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1367
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1368
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1369
static void
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1370
get_clones_stat(dsl_dataset_t *ds, nvlist_t *nv)
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1371
{
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1372
	uint64_t count = 0;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1373
	objset_t *mos = ds->ds_dir->dd_pool->dp_meta_objset;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1374
	zap_cursor_t zc;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1375
	zap_attribute_t za;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1376
	nvlist_t *propval = fnvlist_alloc();
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1377
	nvlist_t *val = fnvlist_alloc();
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1378
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1379
	ASSERT(dsl_pool_config_held(ds->ds_dir->dd_pool));
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1380
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1381
	/*
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1382
	 * There may be missing entries in ds_next_clones_obj
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1383
	 * due to a bug in a previous version of the code.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1384
	 * Only trust it if it has the right number of entries.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1385
	 */
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1386
	if (ds->ds_phys->ds_next_clones_obj != 0) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1387
		ASSERT0(zap_count(mos, ds->ds_phys->ds_next_clones_obj,
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1388
		    &count));
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1389
	}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1390
	if (count != ds->ds_phys->ds_num_children - 1)
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1391
		goto fail;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1392
	for (zap_cursor_init(&zc, mos, ds->ds_phys->ds_next_clones_obj);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1393
	    zap_cursor_retrieve(&zc, &za) == 0;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1394
	    zap_cursor_advance(&zc)) {
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1395
		dsl_dataset_t *clone;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1396
		char buf[ZFS_MAXNAMELEN];
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1397
		VERIFY0(dsl_dataset_hold_obj(ds->ds_dir->dd_pool,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1398
		    za.za_first_integer, FTAG, &clone));
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1399
		dsl_dir_name(clone->ds_dir, buf);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1400
		fnvlist_add_boolean(val, buf);
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1401
		dsl_dataset_rele(clone, FTAG);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1402
	}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1403
	zap_cursor_fini(&zc);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1404
	fnvlist_add_nvlist(propval, ZPROP_VALUE, val);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1405
	fnvlist_add_nvlist(nv, zfs_prop_to_name(ZFS_PROP_CLONES), propval);
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1406
fail:
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1407
	nvlist_free(val);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1408
	nvlist_free(propval);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1409
}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1410
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1411
void
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1412
dsl_dataset_stats(dsl_dataset_t *ds, nvlist_t *nv)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1413
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1414
	dsl_pool_t *dp = ds->ds_dir->dd_pool;
13387
7dd5626b8b49 1092 zfs refratio property
Matt Ahrens <Matt.Ahrens@delphix.com>
parents: 13055
diff changeset
  1415
	uint64_t refd, avail, uobjs, aobjs, ratio;
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1416
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1417
	ASSERT(dsl_pool_config_held(dp));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1418
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1419
	ratio = ds->ds_phys->ds_compressed_bytes == 0 ? 100 :
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1420
	    (ds->ds_phys->ds_uncompressed_bytes * 100 /
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1421
	    ds->ds_phys->ds_compressed_bytes);
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1422
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1423
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRATIO, ratio);
13969
b2c7608044b7 3588 provide zfs properties for logical (uncompressed) space used and referenced
Matthew Ahrens <mahrens@delphix.com>
parents: 13952
diff changeset
  1424
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_LOGICALREFERENCED,
b2c7608044b7 3588 provide zfs properties for logical (uncompressed) space used and referenced
Matthew Ahrens <mahrens@delphix.com>
parents: 13952
diff changeset
  1425
	    ds->ds_phys->ds_uncompressed_bytes);
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1426
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1427
	if (dsl_dataset_is_snapshot(ds)) {
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1428
		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_COMPRESSRATIO, ratio);
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1429
		dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USED,
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1430
		    ds->ds_phys->ds_unique_bytes);
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1431
		get_clones_stat(ds, nv);
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1432
	} else {
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1433
		dsl_dir_stats(ds->ds_dir, nv);
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1434
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1435
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1436
	dsl_dataset_space(ds, &refd, &avail, &uobjs, &aobjs);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1437
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_AVAILABLE, avail);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1438
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFERENCED, refd);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1439
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1440
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATION,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1441
	    ds->ds_phys->ds_creation_time);
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1442
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_CREATETXG,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1443
	    ds->ds_phys->ds_creation_txg);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1444
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFQUOTA,
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1445
	    ds->ds_quota);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1446
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_REFRESERVATION,
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1447
	    ds->ds_reserved);
6643
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6502
diff changeset
  1448
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_GUID,
3a34b0dbb107 6625086 changing cachefile doesn't remove old cache on last user
eschrock
parents: 6502
diff changeset
  1449
	    ds->ds_phys->ds_guid);
10575
2a8816c5173b 6882196 resource.* events shouldn't be posted during spa_tryimport()
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10385
diff changeset
  1450
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_UNIQUE,
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1451
	    ds->ds_phys->ds_unique_bytes);
10575
2a8816c5173b 6882196 resource.* events shouldn't be posted during spa_tryimport()
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10385
diff changeset
  1452
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_OBJSETID,
2a8816c5173b 6882196 resource.* events shouldn't be posted during spa_tryimport()
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 10385
diff changeset
  1453
	    ds->ds_object);
11022
63ab26072e41 PSARC 2009/510 ZFS received properties
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 10951
diff changeset
  1454
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_USERREFS,
63ab26072e41 PSARC 2009/510 ZFS received properties
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 10951
diff changeset
  1455
	    ds->ds_userrefs);
10242
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
  1456
	dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_DEFER_DESTROY,
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
  1457
	    DS_IS_DEFER_DESTROY(ds) ? 1 : 0);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1458
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1459
	if (ds->ds_phys->ds_prev_snap_obj != 0) {
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1460
		uint64_t written, comp, uncomp;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1461
		dsl_pool_t *dp = ds->ds_dir->dd_pool;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1462
		dsl_dataset_t *prev;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1463
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1464
		int err = dsl_dataset_hold_obj(dp,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1465
		    ds->ds_phys->ds_prev_snap_obj, FTAG, &prev);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1466
		if (err == 0) {
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1467
			err = dsl_dataset_space_written(prev, ds, &written,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1468
			    &comp, &uncomp);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1469
			dsl_dataset_rele(prev, FTAG);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1470
			if (err == 0) {
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1471
				dsl_prop_nvlist_add_uint64(nv, ZFS_PROP_WRITTEN,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1472
				    written);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1473
			}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1474
		}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  1475
	}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1476
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1477
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1478
void
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1479
dsl_dataset_fast_stat(dsl_dataset_t *ds, dmu_objset_stats_t *stat)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1480
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1481
	dsl_pool_t *dp = ds->ds_dir->dd_pool;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1482
	ASSERT(dsl_pool_config_held(dp));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1483
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1484
	stat->dds_creation_txg = ds->ds_phys->ds_creation_txg;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1485
	stat->dds_inconsistent = ds->ds_phys->ds_flags & DS_FLAG_INCONSISTENT;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  1486
	stat->dds_guid = ds->ds_phys->ds_guid;
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1487
	stat->dds_origin[0] = '\0';
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1488
	if (dsl_dataset_is_snapshot(ds)) {
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1489
		stat->dds_is_snapshot = B_TRUE;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1490
		stat->dds_num_clones = ds->ds_phys->ds_num_children - 1;
8228
51e9ca9ee3a5 6572357 libzfs should do more to avoid mnttab lookups
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7837
diff changeset
  1491
	} else {
51e9ca9ee3a5 6572357 libzfs should do more to avoid mnttab lookups
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7837
diff changeset
  1492
		stat->dds_is_snapshot = B_FALSE;
51e9ca9ee3a5 6572357 libzfs should do more to avoid mnttab lookups
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 7837
diff changeset
  1493
		stat->dds_num_clones = 0;
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1494
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1495
		if (dsl_dir_is_clone(ds->ds_dir)) {
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1496
			dsl_dataset_t *ods;
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1497
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1498
			VERIFY0(dsl_dataset_hold_obj(dp,
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1499
			    ds->ds_dir->dd_phys->dd_origin_obj, FTAG, &ods));
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1500
			dsl_dataset_name(ods, stat->dds_origin);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1501
			dsl_dataset_rele(ods, FTAG);
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  1502
		}
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1503
	}
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1504
}
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1505
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1506
uint64_t
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1507
dsl_dataset_fsid_guid(dsl_dataset_t *ds)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1508
{
4787
602d3f97842c 6393351 unique_* could be improved
ahrens
parents: 4603
diff changeset
  1509
	return (ds->ds_fsid_guid);
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1510
}
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1511
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1512
void
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1513
dsl_dataset_space(dsl_dataset_t *ds,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1514
    uint64_t *refdbytesp, uint64_t *availbytesp,
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1515
    uint64_t *usedobjsp, uint64_t *availobjsp)
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1516
{
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  1517
	*refdbytesp = ds->ds_phys->ds_referenced_bytes;
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1518
	*availbytesp = dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1519
	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes)
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1520
		*availbytesp += ds->ds_reserved - ds->ds_phys->ds_unique_bytes;
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1521
	if (ds->ds_quota != 0) {
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1522
		/*
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1523
		 * Adjust available bytes according to refquota
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1524
		 */
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1525
		if (*refdbytesp < ds->ds_quota)
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1526
			*availbytesp = MIN(*availbytesp,
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1527
			    ds->ds_quota - *refdbytesp);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1528
		else
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1529
			*availbytesp = 0;
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  1530
	}
2885
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1531
	*usedobjsp = ds->ds_phys->ds_bp.blk_fill;
c0259887ebbc 6460059 zfs destroy <snapshot> leaves behind kruft
ahrens
parents: 2856
diff changeset
  1532
	*availobjsp = DN_MAX_OBJECT - *usedobjsp;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1533
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1534
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1535
boolean_t
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  1536
dsl_dataset_modified_since_snap(dsl_dataset_t *ds, dsl_dataset_t *snap)
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1537
{
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1538
	dsl_pool_t *dp = ds->ds_dir->dd_pool;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1539
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1540
	ASSERT(dsl_pool_config_held(dp));
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  1541
	if (snap == NULL)
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1542
		return (B_FALSE);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1543
	if (ds->ds_phys->ds_bp.blk_birth >
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  1544
	    snap->ds_phys->ds_creation_txg) {
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  1545
		objset_t *os, *os_snap;
12839
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1546
		/*
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1547
		 * It may be that only the ZIL differs, because it was
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1548
		 * reset in the head.  Don't count that as being
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1549
		 * modified.
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1550
		 */
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1551
		if (dmu_objset_from_ds(ds, &os) != 0)
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1552
			return (B_TRUE);
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  1553
		if (dmu_objset_from_ds(snap, &os_snap) != 0)
12839
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1554
			return (B_TRUE);
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1555
		return (bcmp(&os->os_phys->os_meta_dnode,
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  1556
		    &os_snap->os_phys->os_meta_dnode,
12839
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1557
		    sizeof (os->os_phys->os_meta_dnode)) != 0);
1eab9192da8b 6844896 recursive snapshots take a long time
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12798
diff changeset
  1558
	}
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1559
	return (B_FALSE);
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1560
}
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  1561
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1562
typedef struct dsl_dataset_rename_snapshot_arg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1563
	const char *ddrsa_fsname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1564
	const char *ddrsa_oldsnapname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1565
	const char *ddrsa_newsnapname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1566
	boolean_t ddrsa_recursive;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1567
	dmu_tx_t *ddrsa_tx;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1568
} dsl_dataset_rename_snapshot_arg_t;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1569
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1570
/* ARGSUSED */
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1571
static int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1572
dsl_dataset_rename_snapshot_check_impl(dsl_pool_t *dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1573
    dsl_dataset_t *hds, void *arg)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1574
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1575
	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1576
	int error;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1577
	uint64_t val;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1578
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1579
	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1580
	if (error != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1581
		/* ignore nonexistent snapshots */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1582
		return (error == ENOENT ? 0 : error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1583
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1584
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1585
	/* new name should not exist */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1586
	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_newsnapname, &val);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1587
	if (error == 0)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1588
		error = SET_ERROR(EEXIST);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1589
	else if (error == ENOENT)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1590
		error = 0;
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1591
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1592
	/* dataset name + 1 for the "@" + the new snapshot name must fit */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1593
	if (dsl_dir_namelen(hds->ds_dir) + 1 +
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1594
	    strlen(ddrsa->ddrsa_newsnapname) >= MAXNAMELEN)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1595
		error = SET_ERROR(ENAMETOOLONG);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1596
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1597
	return (error);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1598
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1599
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1600
static int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1601
dsl_dataset_rename_snapshot_check(void *arg, dmu_tx_t *tx)
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1602
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1603
	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1604
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1605
	dsl_dataset_t *hds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1606
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1607
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1608
	error = dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1609
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1610
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1611
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1612
	if (ddrsa->ddrsa_recursive) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1613
		error = dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1614
		    dsl_dataset_rename_snapshot_check_impl, ddrsa,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1615
		    DS_FIND_CHILDREN);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1616
	} else {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1617
		error = dsl_dataset_rename_snapshot_check_impl(dp, hds, ddrsa);
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4009
diff changeset
  1618
	}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1619
	dsl_dataset_rele(hds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1620
	return (error);
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1621
}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1622
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1623
static int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1624
dsl_dataset_rename_snapshot_sync_impl(dsl_pool_t *dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1625
    dsl_dataset_t *hds, void *arg)
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1626
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1627
	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1628
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1629
	uint64_t val;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1630
	dmu_tx_t *tx = ddrsa->ddrsa_tx;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1631
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1632
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1633
	error = dsl_dataset_snap_lookup(hds, ddrsa->ddrsa_oldsnapname, &val);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1634
	ASSERT(error == 0 || error == ENOENT);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1635
	if (error == ENOENT) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1636
		/* ignore nonexistent snapshots */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1637
		return (0);
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1638
	}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1639
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1640
	VERIFY0(dsl_dataset_hold_obj(dp, val, FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1641
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1642
	/* log before we change the name */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1643
	spa_history_log_internal_ds(ds, "rename", tx,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1644
	    "-> @%s", ddrsa->ddrsa_newsnapname);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1645
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1646
	VERIFY0(dsl_dataset_snap_remove(hds, ddrsa->ddrsa_oldsnapname, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1647
	mutex_enter(&ds->ds_lock);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1648
	(void) strcpy(ds->ds_snapname, ddrsa->ddrsa_newsnapname);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1649
	mutex_exit(&ds->ds_lock);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1650
	VERIFY0(zap_add(dp->dp_meta_objset, hds->ds_phys->ds_snapnames_zapobj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1651
	    ds->ds_snapname, 8, 1, &ds->ds_object, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1652
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1653
	dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1654
	return (0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1655
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1656
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1657
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1658
dsl_dataset_rename_snapshot_sync(void *arg, dmu_tx_t *tx)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1659
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1660
	dsl_dataset_rename_snapshot_arg_t *ddrsa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1661
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1662
	dsl_dataset_t *hds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1663
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1664
	VERIFY0(dsl_dataset_hold(dp, ddrsa->ddrsa_fsname, FTAG, &hds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1665
	ddrsa->ddrsa_tx = tx;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1666
	if (ddrsa->ddrsa_recursive) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1667
		VERIFY0(dmu_objset_find_dp(dp, hds->ds_dir->dd_object,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1668
		    dsl_dataset_rename_snapshot_sync_impl, ddrsa,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1669
		    DS_FIND_CHILDREN));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1670
	} else {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1671
		VERIFY0(dsl_dataset_rename_snapshot_sync_impl(dp, hds, ddrsa));
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1672
	}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1673
	dsl_dataset_rele(hds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1674
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1675
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1676
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1677
dsl_dataset_rename_snapshot(const char *fsname,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1678
    const char *oldsnapname, const char *newsnapname, boolean_t recursive)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1679
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1680
	dsl_dataset_rename_snapshot_arg_t ddrsa;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1681
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1682
	ddrsa.ddrsa_fsname = fsname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1683
	ddrsa.ddrsa_oldsnapname = oldsnapname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1684
	ddrsa.ddrsa_newsnapname = newsnapname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1685
	ddrsa.ddrsa_recursive = recursive;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1686
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1687
	return (dsl_sync_task(fsname, dsl_dataset_rename_snapshot_check,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1688
	    dsl_dataset_rename_snapshot_sync, &ddrsa, 1));
4007
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1689
}
c6f5c6753018 PSARC 2007/142 zfs rename -r
mmusante
parents: 3978
diff changeset
  1690
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1691
/*
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1692
 * If we're doing an ownership handoff, we need to make sure that there is
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1693
 * only one long hold on the dataset.  We're not allowed to change anything here
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1694
 * so we don't permanently release the long hold or regular hold here.  We want
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1695
 * to do this only when syncing to avoid the dataset unexpectedly going away
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1696
 * when we release the long hold.
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1697
 */
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1698
static int
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1699
dsl_dataset_handoff_check(dsl_dataset_t *ds, void *owner, dmu_tx_t *tx)
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1700
{
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1701
	boolean_t held;
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1702
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1703
	if (!dmu_tx_is_syncing(tx))
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1704
		return (0);
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1705
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1706
	if (owner != NULL) {
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1707
		VERIFY3P(ds->ds_owner, ==, owner);
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1708
		dsl_dataset_long_rele(ds, owner);
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1709
	}
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1710
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1711
	held = dsl_dataset_long_held(ds);
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1712
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1713
	if (owner != NULL)
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1714
		dsl_dataset_long_hold(ds, owner);
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1715
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1716
	if (held)
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1717
		return (SET_ERROR(EBUSY));
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1718
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1719
	return (0);
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1720
}
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1721
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1722
typedef struct dsl_dataset_rollback_arg {
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1723
	const char *ddra_fsname;
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1724
	void *ddra_owner;
14140
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1725
	nvlist_t *ddra_result;
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1726
} dsl_dataset_rollback_arg_t;
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1727
4569
e80d40447f27 6355623 zfs rename to a valid dataset name, but its snapshot name becomes too long, panics the system
mmusante
parents: 4543
diff changeset
  1728
static int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1729
dsl_dataset_rollback_check(void *arg, dmu_tx_t *tx)
4569
e80d40447f27 6355623 zfs rename to a valid dataset name, but its snapshot name becomes too long, panics the system
mmusante
parents: 4543
diff changeset
  1730
{
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1731
	dsl_dataset_rollback_arg_t *ddra = arg;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1732
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1733
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1734
	int64_t unused_refres_delta;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1735
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1736
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1737
	error = dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1738
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1739
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1740
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1741
	/* must not be a snapshot */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1742
	if (dsl_dataset_is_snapshot(ds)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1743
		dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1744
		return (SET_ERROR(EINVAL));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1745
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1746
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1747
	/* must have a most recent snapshot */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1748
	if (ds->ds_phys->ds_prev_snap_txg < TXG_INITIAL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1749
		dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1750
		return (SET_ERROR(EINVAL));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1751
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1752
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1753
	error = dsl_dataset_handoff_check(ds, ddra->ddra_owner, tx);
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1754
	if (error != 0) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1755
		dsl_dataset_rele(ds, FTAG);
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1756
		return (error);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1757
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1758
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1759
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1760
	 * Check if the snap we are rolling back to uses more than
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1761
	 * the refquota.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1762
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1763
	if (ds->ds_quota != 0 &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1764
	    ds->ds_prev->ds_phys->ds_referenced_bytes > ds->ds_quota) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1765
		dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1766
		return (SET_ERROR(EDQUOT));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1767
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1768
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1769
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1770
	 * When we do the clone swap, we will temporarily use more space
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1771
	 * due to the refreservation (the head will no longer have any
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1772
	 * unique space, so the entire amount of the refreservation will need
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1773
	 * to be free).  We will immediately destroy the clone, freeing
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1774
	 * this space, but the freeing happens over many txg's.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1775
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1776
	unused_refres_delta = (int64_t)MIN(ds->ds_reserved,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1777
	    ds->ds_phys->ds_unique_bytes);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1778
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1779
	if (unused_refres_delta > 0 &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1780
	    unused_refres_delta >
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1781
	    dsl_dir_space_available(ds->ds_dir, NULL, 0, TRUE)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1782
		dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1783
		return (SET_ERROR(ENOSPC));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1784
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1785
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1786
	dsl_dataset_rele(ds, FTAG);
4569
e80d40447f27 6355623 zfs rename to a valid dataset name, but its snapshot name becomes too long, panics the system
mmusante
parents: 4543
diff changeset
  1787
	return (0);
e80d40447f27 6355623 zfs rename to a valid dataset name, but its snapshot name becomes too long, panics the system
mmusante
parents: 4543
diff changeset
  1788
}
e80d40447f27 6355623 zfs rename to a valid dataset name, but its snapshot name becomes too long, panics the system
mmusante
parents: 4543
diff changeset
  1789
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1790
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1791
dsl_dataset_rollback_sync(void *arg, dmu_tx_t *tx)
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1792
{
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1793
	dsl_dataset_rollback_arg_t *ddra = arg;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1794
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1795
	dsl_dataset_t *ds, *clone;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1796
	uint64_t cloneobj;
14140
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1797
	char namebuf[ZFS_MAXNAMELEN];
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1798
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1799
	VERIFY0(dsl_dataset_hold(dp, ddra->ddra_fsname, FTAG, &ds));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1800
14140
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1801
	dsl_dataset_name(ds->ds_prev, namebuf);
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1802
	fnvlist_add_string(ddra->ddra_result, "target", namebuf);
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1803
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1804
	cloneobj = dsl_dataset_create_sync(ds->ds_dir, "%rollback",
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1805
	    ds->ds_prev, DS_CREATE_FLAG_NODIRTY, kcred, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1806
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1807
	VERIFY0(dsl_dataset_hold_obj(dp, cloneobj, FTAG, &clone));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1808
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1809
	dsl_dataset_clone_swap_sync_impl(clone, ds, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1810
	dsl_dataset_zero_zil(ds, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1811
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1812
	dsl_destroy_head_sync_impl(clone, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1813
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1814
	dsl_dataset_rele(clone, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1815
	dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1816
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1817
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1818
/*
14140
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1819
 * Rolls back the given filesystem or volume to the most recent snapshot.
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1820
 * The name of the most recent snapshot will be returned under key "target"
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1821
 * in the result nvlist.
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1822
 *
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1823
 * If owner != NULL:
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1824
 * - The existing dataset MUST be owned by the specified owner at entry
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1825
 * - Upon return, dataset will still be held by the same owner, whether we
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1826
 *   succeed or not.
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1827
 *
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1828
 * This mode is required any time the existing filesystem is mounted.  See
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1829
 * notes above zfs_suspend_fs() for further details.
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1830
 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1831
int
14140
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1832
dsl_dataset_rollback(const char *fsname, void *owner, nvlist_t *result)
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1833
{
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1834
	dsl_dataset_rollback_arg_t ddra;
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1835
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1836
	ddra.ddra_fsname = fsname;
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1837
	ddra.ddra_owner = owner;
14140
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1838
	ddra.ddra_result = result;
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  1839
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1840
	return (dsl_sync_task(fsname, dsl_dataset_rollback_check,
14140
f7523d207d74 3996 want a libzfs_core API to rollback to latest snapshot
Matthew Ahrens <mahrens@delphix.com>
parents: 14097
diff changeset
  1841
	    dsl_dataset_rollback_sync, &ddra, 1));
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1842
}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1843
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  1844
struct promotenode {
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1845
	list_node_t link;
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1846
	dsl_dataset_t *ds;
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1847
};
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1848
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1849
typedef struct dsl_dataset_promote_arg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1850
	const char *ddpa_clonename;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1851
	dsl_dataset_t *ddpa_clone;
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1852
	list_t shared_snaps, origin_snaps, clone_snaps;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1853
	dsl_dataset_t *origin_origin; /* origin of the origin */
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1854
	uint64_t used, comp, uncomp, unique, cloneusedsnap, originusedsnap;
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 10575
diff changeset
  1855
	char *err_ds;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1856
} dsl_dataset_promote_arg_t;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1857
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1858
static int snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1859
static int promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1860
    void *tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1861
static void promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1862
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1863
static int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1864
dsl_dataset_promote_check(void *arg, dmu_tx_t *tx)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1865
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1866
	dsl_dataset_promote_arg_t *ddpa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1867
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1868
	dsl_dataset_t *hds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1869
	struct promotenode *snap;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1870
	dsl_dataset_t *origin_ds;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1871
	int err;
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1872
	uint64_t unused;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1873
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1874
	err = promote_hold(ddpa, dp, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1875
	if (err != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1876
		return (err);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1877
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1878
	hds = ddpa->ddpa_clone;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1879
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1880
	if (hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1881
		promote_rele(ddpa, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1882
		return (SET_ERROR(EXDEV));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1883
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1884
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1885
	/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1886
	 * Compute and check the amount of space to transfer.  Since this is
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1887
	 * so expensive, don't do the preliminary check.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1888
	 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1889
	if (!dmu_tx_is_syncing(tx)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1890
		promote_rele(ddpa, FTAG);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  1891
		return (0);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1892
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1893
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1894
	snap = list_head(&ddpa->shared_snaps);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1895
	origin_ds = snap->ds;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1896
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  1897
	/* compute origin's new unique space */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1898
	snap = list_tail(&ddpa->clone_snaps);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1899
	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1900
	dsl_deadlist_space_range(&snap->ds->ds_deadlist,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1901
	    origin_ds->ds_phys->ds_prev_snap_txg, UINT64_MAX,
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1902
	    &ddpa->unique, &unused, &unused);
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1903
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1904
	/*
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1905
	 * Walk the snapshots that we are moving
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1906
	 *
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1907
	 * Compute space to transfer.  Consider the incremental changes
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1908
	 * to used by each snapshot:
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1909
	 * (my used) = (prev's used) + (blocks born) - (blocks killed)
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1910
	 * So each snapshot gave birth to:
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1911
	 * (blocks born) = (my used) - (prev's used) + (blocks killed)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1912
	 * So a sequence would look like:
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1913
	 * (uN - u(N-1) + kN) + ... + (u1 - u0 + k1) + (u0 - 0 + k0)
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1914
	 * Which simplifies to:
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1915
	 * uN + kN + kN-1 + ... + k1 + k0
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1916
	 * Note however, if we stop before we reach the ORIGIN we get:
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1917
	 * uN + kN + kN-1 + ... + kM - uM-1
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1918
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1919
	ddpa->used = origin_ds->ds_phys->ds_referenced_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1920
	ddpa->comp = origin_ds->ds_phys->ds_compressed_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1921
	ddpa->uncomp = origin_ds->ds_phys->ds_uncompressed_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1922
	for (snap = list_head(&ddpa->shared_snaps); snap;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1923
	    snap = list_next(&ddpa->shared_snaps, snap)) {
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1924
		uint64_t val, dlused, dlcomp, dluncomp;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1925
		dsl_dataset_t *ds = snap->ds;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1926
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1927
		/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1928
		 * If there are long holds, we won't be able to evict
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1929
		 * the objset.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1930
		 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1931
		if (dsl_dataset_long_held(ds)) {
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1932
			err = SET_ERROR(EBUSY);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1933
			goto out;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1934
		}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1935
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1936
		/* Check that the snapshot name does not conflict */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1937
		VERIFY0(dsl_dataset_get_snapname(ds));
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1938
		err = dsl_dataset_snap_lookup(hds, ds->ds_snapname, &val);
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 10575
diff changeset
  1939
		if (err == 0) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1940
			(void) strcpy(ddpa->err_ds, snap->ds->ds_snapname);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  1941
			err = SET_ERROR(EEXIST);
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 10575
diff changeset
  1942
			goto out;
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 10575
diff changeset
  1943
		}
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1944
		if (err != ENOENT)
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 10575
diff changeset
  1945
			goto out;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1946
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1947
		/* The very first snapshot does not have a deadlist */
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1948
		if (ds->ds_phys->ds_prev_snap_obj == 0)
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1949
			continue;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1950
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1951
		dsl_deadlist_space(&ds->ds_deadlist,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1952
		    &dlused, &dlcomp, &dluncomp);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1953
		ddpa->used += dlused;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1954
		ddpa->comp += dlcomp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1955
		ddpa->uncomp += dluncomp;
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1956
	}
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1957
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1958
	/*
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1959
	 * If we are a clone of a clone then we never reached ORIGIN,
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1960
	 * so we need to subtract out the clone origin's used space.
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  1961
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1962
	if (ddpa->origin_origin) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1963
		ddpa->used -= ddpa->origin_origin->ds_phys->ds_referenced_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1964
		ddpa->comp -= ddpa->origin_origin->ds_phys->ds_compressed_bytes;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1965
		ddpa->uncomp -=
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1966
		    ddpa->origin_origin->ds_phys->ds_uncompressed_bytes;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1967
	}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1968
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  1969
	/* Check that there is enough space here */
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1970
	err = dsl_dir_transfer_possible(origin_ds->ds_dir, hds->ds_dir,
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1971
	    ddpa->used);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1972
	if (err != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1973
		goto out;
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1974
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1975
	/*
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1976
	 * Compute the amounts of space that will be used by snapshots
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1977
	 * after the promotion (for both origin and clone).  For each,
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1978
	 * it is the amount of space that will be on all of their
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1979
	 * deadlists (that was not born before their new origin).
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1980
	 */
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1981
	if (hds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1982
		uint64_t space;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1983
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1984
		/*
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1985
		 * Note, typically this will not be a clone of a clone,
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1986
		 * so dd_origin_txg will be < TXG_INITIAL, so
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  1987
		 * these snaplist_space() -> dsl_deadlist_space_range()
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1988
		 * calls will be fast because they do not have to
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1989
		 * iterate over all bps.
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  1990
		 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1991
		snap = list_head(&ddpa->origin_snaps);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1992
		err = snaplist_space(&ddpa->shared_snaps,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1993
		    snap->ds->ds_dir->dd_origin_txg, &ddpa->cloneusedsnap);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1994
		if (err != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1995
			goto out;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1996
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1997
		err = snaplist_space(&ddpa->clone_snaps,
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  1998
		    snap->ds->ds_dir->dd_origin_txg, &space);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  1999
		if (err != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2000
			goto out;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2001
		ddpa->cloneusedsnap += space;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2002
	}
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2003
	if (origin_ds->ds_dir->dd_phys->dd_flags & DD_FLAG_USED_BREAKDOWN) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2004
		err = snaplist_space(&ddpa->origin_snaps,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2005
		    origin_ds->ds_phys->ds_creation_txg, &ddpa->originusedsnap);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2006
		if (err != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2007
			goto out;
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2008
	}
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2009
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 10575
diff changeset
  2010
out:
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2011
	promote_rele(ddpa, FTAG);
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 10575
diff changeset
  2012
	return (err);
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  2013
}
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2014
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  2015
static void
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2016
dsl_dataset_promote_sync(void *arg, dmu_tx_t *tx)
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  2017
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2018
	dsl_dataset_promote_arg_t *ddpa = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2019
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2020
	dsl_dataset_t *hds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2021
	struct promotenode *snap;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2022
	dsl_dataset_t *origin_ds;
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2023
	dsl_dataset_t *origin_head;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2024
	dsl_dir_t *dd;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2025
	dsl_dir_t *odd = NULL;
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  2026
	uint64_t oldnext_obj;
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2027
	int64_t delta;
2199
712a788c2dfd PSARC 2006/388 snapshot -r
ahrens
parents: 2082
diff changeset
  2028
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2029
	VERIFY0(promote_hold(ddpa, dp, FTAG));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2030
	hds = ddpa->ddpa_clone;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2031
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2032
	ASSERT0(hds->ds_phys->ds_flags & DS_FLAG_NOPROMOTE);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2033
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2034
	snap = list_head(&ddpa->shared_snaps);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2035
	origin_ds = snap->ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2036
	dd = hds->ds_dir;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2037
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2038
	snap = list_head(&ddpa->origin_snaps);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2039
	origin_head = snap->ds;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2040
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2207
diff changeset
  2041
	/*
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2042
	 * We need to explicitly open odd, since origin_ds's dd will be
2417
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2207
diff changeset
  2043
	 * changing.
694d5de97348 6444346 zfs promote fails in zone
ahrens
parents: 2207
diff changeset
  2044
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2045
	VERIFY0(dsl_dir_hold_obj(dp, origin_ds->ds_dir->dd_object,
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2046
	    NULL, FTAG, &odd));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2047
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2048
	/* change origin's next snap */
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2049
	dmu_buf_will_dirty(origin_ds->ds_dbuf, tx);
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  2050
	oldnext_obj = origin_ds->ds_phys->ds_next_snap_obj;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2051
	snap = list_tail(&ddpa->clone_snaps);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2052
	ASSERT3U(snap->ds->ds_phys->ds_prev_snap_obj, ==, origin_ds->ds_object);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2053
	origin_ds->ds_phys->ds_next_snap_obj = snap->ds->ds_object;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2054
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  2055
	/* change the origin's next clone */
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  2056
	if (origin_ds->ds_phys->ds_next_clones_obj) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2057
		dsl_dataset_remove_from_next_clones(origin_ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2058
		    snap->ds->ds_object, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2059
		VERIFY0(zap_add_int(dp->dp_meta_objset,
7046
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  2060
		    origin_ds->ds_phys->ds_next_clones_obj,
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  2061
		    oldnext_obj, tx));
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  2062
	}
361307ae060d 6343667 scrub/resilver has to start over when a snapshot is taken
ahrens
parents: 6992
diff changeset
  2063
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2064
	/* change origin */
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2065
	dmu_buf_will_dirty(dd->dd_dbuf, tx);
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2066
	ASSERT3U(dd->dd_phys->dd_origin_obj, ==, origin_ds->ds_object);
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2067
	dd->dd_phys->dd_origin_obj = odd->dd_phys->dd_origin_obj;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  2068
	dd->dd_origin_txg = origin_head->ds_dir->dd_origin_txg;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2069
	dmu_buf_will_dirty(odd->dd_dbuf, tx);
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2070
	odd->dd_phys->dd_origin_obj = origin_ds->ds_object;
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  2071
	origin_head->ds_dir->dd_origin_txg =
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  2072
	    origin_ds->ds_phys->ds_creation_txg;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2073
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2074
	/* change dd_clone entries */
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2075
	if (spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2076
		VERIFY0(zap_remove_int(dp->dp_meta_objset,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2077
		    odd->dd_phys->dd_clones, hds->ds_object, tx));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2078
		VERIFY0(zap_add_int(dp->dp_meta_objset,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2079
		    ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2080
		    hds->ds_object, tx));
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2081
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2082
		VERIFY0(zap_remove_int(dp->dp_meta_objset,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2083
		    ddpa->origin_origin->ds_dir->dd_phys->dd_clones,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2084
		    origin_head->ds_object, tx));
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2085
		if (dd->dd_phys->dd_clones == 0) {
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2086
			dd->dd_phys->dd_clones = zap_create(dp->dp_meta_objset,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2087
			    DMU_OT_DSL_CLONES, DMU_OT_NONE, 0, tx);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2088
		}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2089
		VERIFY0(zap_add_int(dp->dp_meta_objset,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2090
		    dd->dd_phys->dd_clones, origin_head->ds_object, tx));
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2091
	}
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2092
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2093
	/* move snapshots to this dir */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2094
	for (snap = list_head(&ddpa->shared_snaps); snap;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2095
	    snap = list_next(&ddpa->shared_snaps, snap)) {
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2096
		dsl_dataset_t *ds = snap->ds;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2097
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2098
		/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2099
		 * Property callbacks are registered to a particular
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2100
		 * dsl_dir.  Since ours is changing, evict the objset
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2101
		 * so that they will be unregistered from the old dsl_dir.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2102
		 */
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
  2103
		if (ds->ds_objset) {
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
  2104
			dmu_objset_evict(ds->ds_objset);
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
  2105
			ds->ds_objset = NULL;
7237
f47d41541b14 PSARC 2008/393 zfs primarycache and secondarycache properties
ek110237
parents: 7077
diff changeset
  2106
		}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2107
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2108
		/* move snap name entry */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2109
		VERIFY0(dsl_dataset_get_snapname(ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2110
		VERIFY0(dsl_dataset_snap_remove(origin_head,
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2111
		    ds->ds_snapname, tx));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2112
		VERIFY0(zap_add(dp->dp_meta_objset,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2113
		    hds->ds_phys->ds_snapnames_zapobj, ds->ds_snapname,
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2114
		    8, 1, &ds->ds_object, tx));
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2115
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2116
		/* change containing dsl_dir */
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2117
		dmu_buf_will_dirty(ds->ds_dbuf, tx);
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2118
		ASSERT3U(ds->ds_phys->ds_dir_obj, ==, odd->dd_object);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2119
		ds->ds_phys->ds_dir_obj = dd->dd_object;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2120
		ASSERT3P(ds->ds_dir, ==, odd);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2121
		dsl_dir_rele(ds->ds_dir, ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2122
		VERIFY0(dsl_dir_hold_obj(dp, dd->dd_object,
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2123
		    NULL, ds, &ds->ds_dir));
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2124
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2125
		/* move any clone references */
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2126
		if (ds->ds_phys->ds_next_clones_obj &&
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2127
		    spa_version(dp->dp_spa) >= SPA_VERSION_DIR_CLONES) {
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2128
			zap_cursor_t zc;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2129
			zap_attribute_t za;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2130
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2131
			for (zap_cursor_init(&zc, dp->dp_meta_objset,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2132
			    ds->ds_phys->ds_next_clones_obj);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2133
			    zap_cursor_retrieve(&zc, &za) == 0;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2134
			    zap_cursor_advance(&zc)) {
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2135
				dsl_dataset_t *cnds;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2136
				uint64_t o;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2137
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2138
				if (za.za_first_integer == oldnext_obj) {
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2139
					/*
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2140
					 * We've already moved the
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2141
					 * origin's reference.
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2142
					 */
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2143
					continue;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2144
				}
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2145
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2146
				VERIFY0(dsl_dataset_hold_obj(dp,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2147
				    za.za_first_integer, FTAG, &cnds));
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2148
				o = cnds->ds_dir->dd_phys->dd_head_dataset_obj;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2149
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2150
				VERIFY0(zap_remove_int(dp->dp_meta_objset,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2151
				    odd->dd_phys->dd_clones, o, tx));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2152
				VERIFY0(zap_add_int(dp->dp_meta_objset,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2153
				    dd->dd_phys->dd_clones, o, tx));
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2154
				dsl_dataset_rele(cnds, FTAG);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2155
			}
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2156
			zap_cursor_fini(&zc);
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2157
		}
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2158
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2159
		ASSERT(!dsl_prop_hascb(ds));
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2160
	}
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2161
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2162
	/*
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2163
	 * Change space accounting.
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2164
	 * Note, pa->*usedsnap and dd_used_breakdown[SNAP] will either
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2165
	 * both be valid, or both be 0 (resulting in delta == 0).  This
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2166
	 * is true for each of {clone,origin} independently.
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2167
	 */
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2168
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2169
	delta = ddpa->cloneusedsnap -
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2170
	    dd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2171
	ASSERT3S(delta, >=, 0);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2172
	ASSERT3U(ddpa->used, >=, delta);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2173
	dsl_dir_diduse_space(dd, DD_USED_SNAP, delta, 0, 0, tx);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2174
	dsl_dir_diduse_space(dd, DD_USED_HEAD,
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2175
	    ddpa->used - delta, ddpa->comp, ddpa->uncomp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2176
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2177
	delta = ddpa->originusedsnap -
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2178
	    odd->dd_phys->dd_used_breakdown[DD_USED_SNAP];
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2179
	ASSERT3S(delta, <=, 0);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2180
	ASSERT3U(ddpa->used, >=, -delta);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2181
	dsl_dir_diduse_space(odd, DD_USED_SNAP, delta, 0, 0, tx);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2182
	dsl_dir_diduse_space(odd, DD_USED_HEAD,
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2183
	    -ddpa->used - delta, -ddpa->comp, -ddpa->uncomp, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2184
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2185
	origin_ds->ds_phys->ds_unique_bytes = ddpa->unique;
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2186
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4009
diff changeset
  2187
	/* log history record */
13743
95aba6e49b9f 2882 implement libzfs_core
Matthew Ahrens <mahrens@delphix.com>
parents: 13700
diff changeset
  2188
	spa_history_log_internal_ds(hds, "promote", tx, "");
4543
12bb2876a62e PSARC/2006/465 ZFS Delegated Administration
marks
parents: 4009
diff changeset
  2189
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2190
	dsl_dir_rele(odd, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2191
	promote_rele(ddpa, FTAG);
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2192
}
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2193
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2194
/*
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2195
 * Make a list of dsl_dataset_t's for the snapshots between first_obj
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2196
 * (exclusive) and last_obj (inclusive).  The list will be in reverse
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2197
 * order (last_obj will be the list_head()).  If first_obj == 0, do all
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2198
 * snapshots back to this dataset's origin.
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2199
 */
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2200
static int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2201
snaplist_make(dsl_pool_t *dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2202
    uint64_t first_obj, uint64_t last_obj, list_t *l, void *tag)
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2203
{
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2204
	uint64_t obj = last_obj;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2205
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2206
	list_create(l, sizeof (struct promotenode),
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2207
	    offsetof(struct promotenode, link));
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2208
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2209
	while (obj != first_obj) {
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2210
		dsl_dataset_t *ds;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2211
		struct promotenode *snap;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2212
		int err;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2213
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2214
		err = dsl_dataset_hold_obj(dp, obj, tag, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2215
		ASSERT(err != ENOENT);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2216
		if (err != 0)
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2217
			return (err);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2218
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2219
		if (first_obj == 0)
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2220
			first_obj = ds->ds_dir->dd_phys->dd_origin_obj;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2221
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2222
		snap = kmem_alloc(sizeof (*snap), KM_SLEEP);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2223
		snap->ds = ds;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2224
		list_insert_tail(l, snap);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2225
		obj = ds->ds_phys->ds_prev_snap_obj;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2226
	}
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2227
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2228
	return (0);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2229
}
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2230
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2231
static int
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2232
snaplist_space(list_t *l, uint64_t mintxg, uint64_t *spacep)
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2233
{
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2234
	struct promotenode *snap;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2235
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2236
	*spacep = 0;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2237
	for (snap = list_head(l); snap; snap = list_next(l, snap)) {
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2238
		uint64_t used, comp, uncomp;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2239
		dsl_deadlist_space_range(&snap->ds->ds_deadlist,
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2240
		    mintxg, UINT64_MAX, &used, &comp, &uncomp);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2241
		*spacep += used;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2242
	}
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2243
	return (0);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2244
}
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2245
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2246
static void
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2247
snaplist_destroy(list_t *l, void *tag)
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2248
{
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2249
	struct promotenode *snap;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2250
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2251
	if (l == NULL || !list_link_active(&l->list_head))
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2252
		return;
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2253
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2254
	while ((snap = list_tail(l)) != NULL) {
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2255
		list_remove(l, snap);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2256
		dsl_dataset_rele(snap->ds, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2257
		kmem_free(snap, sizeof (*snap));
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2258
	}
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2259
	list_destroy(l);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2260
}
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2261
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2262
static int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2263
promote_hold(dsl_dataset_promote_arg_t *ddpa, dsl_pool_t *dp, void *tag)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2264
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2265
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2266
	dsl_dir_t *dd;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2267
	struct promotenode *snap;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2268
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2269
	error = dsl_dataset_hold(dp, ddpa->ddpa_clonename, tag,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2270
	    &ddpa->ddpa_clone);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2271
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2272
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2273
	dd = ddpa->ddpa_clone->ds_dir;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2274
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2275
	if (dsl_dataset_is_snapshot(ddpa->ddpa_clone) ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2276
	    !dsl_dir_is_clone(dd)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2277
		dsl_dataset_rele(ddpa->ddpa_clone, tag);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2278
		return (SET_ERROR(EINVAL));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2279
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2280
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2281
	error = snaplist_make(dp, 0, dd->dd_phys->dd_origin_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2282
	    &ddpa->shared_snaps, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2283
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2284
		goto out;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2285
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2286
	error = snaplist_make(dp, 0, ddpa->ddpa_clone->ds_object,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2287
	    &ddpa->clone_snaps, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2288
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2289
		goto out;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2290
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2291
	snap = list_head(&ddpa->shared_snaps);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2292
	ASSERT3U(snap->ds->ds_object, ==, dd->dd_phys->dd_origin_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2293
	error = snaplist_make(dp, dd->dd_phys->dd_origin_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2294
	    snap->ds->ds_dir->dd_phys->dd_head_dataset_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2295
	    &ddpa->origin_snaps, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2296
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2297
		goto out;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2298
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2299
	if (snap->ds->ds_dir->dd_phys->dd_origin_obj != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2300
		error = dsl_dataset_hold_obj(dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2301
		    snap->ds->ds_dir->dd_phys->dd_origin_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2302
		    tag, &ddpa->origin_origin);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2303
		if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2304
			goto out;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2305
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2306
out:
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2307
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2308
		promote_rele(ddpa, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2309
	return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2310
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2311
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2312
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2313
promote_rele(dsl_dataset_promote_arg_t *ddpa, void *tag)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2314
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2315
	snaplist_destroy(&ddpa->shared_snaps, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2316
	snaplist_destroy(&ddpa->clone_snaps, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2317
	snaplist_destroy(&ddpa->origin_snaps, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2318
	if (ddpa->origin_origin != NULL)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2319
		dsl_dataset_rele(ddpa->origin_origin, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2320
	dsl_dataset_rele(ddpa->ddpa_clone, tag);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2321
}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2322
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2323
/*
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2324
 * Promote a clone.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2325
 *
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2326
 * If it fails due to a conflicting snapshot name, "conflsnap" will be filled
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2327
 * in with the name.  (It must be at least MAXNAMELEN bytes long.)
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2328
 */
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2329
int
10588
dc03f981ea18 6438937 if 'zfs destroy' fails, it can leave a zvol device link missing
Eric Taylor <Eric.Taylor@Sun.COM>
parents: 10575
diff changeset
  2330
dsl_dataset_promote(const char *name, char *conflsnap)
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2331
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2332
	dsl_dataset_promote_arg_t ddpa = { 0 };
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2333
	uint64_t numsnaps;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2334
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2335
	objset_t *os;
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2336
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2337
	/*
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2338
	 * We will modify space proportional to the number of
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2339
	 * snapshots.  Compute numsnaps.
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2340
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2341
	error = dmu_objset_hold(name, FTAG, &os);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2342
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2343
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2344
	error = zap_count(dmu_objset_pool(os)->dp_meta_objset,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2345
	    dmu_objset_ds(os)->ds_phys->ds_snapnames_zapobj, &numsnaps);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2346
	dmu_objset_rele(os, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2347
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2348
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2349
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2350
	ddpa.ddpa_clonename = name;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2351
	ddpa.err_ds = conflsnap;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2352
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2353
	return (dsl_sync_task(name, dsl_dataset_promote_check,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2354
	    dsl_dataset_promote_sync, &ddpa, 2 + numsnaps));
2082
76b439ec3ac1 PSARC 2006/223 ZFS Hot Spares
eschrock
parents: 1758
diff changeset
  2355
}
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2356
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2357
int
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2358
dsl_dataset_clone_swap_check_impl(dsl_dataset_t *clone,
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  2359
    dsl_dataset_t *origin_head, boolean_t force, void *owner, dmu_tx_t *tx)
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2360
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2361
	int64_t unused_refres_delta;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2362
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2363
	/* they should both be heads */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2364
	if (dsl_dataset_is_snapshot(clone) ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2365
	    dsl_dataset_is_snapshot(origin_head))
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2366
		return (SET_ERROR(EINVAL));
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2367
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  2368
	/* if we are not forcing, the branch point should be just before them */
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  2369
	if (!force && clone->ds_prev != origin_head->ds_prev)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2370
		return (SET_ERROR(EINVAL));
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2371
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2372
	/* clone should be the clone (unless they are unrelated) */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2373
	if (clone->ds_prev != NULL &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2374
	    clone->ds_prev != clone->ds_dir->dd_pool->dp_origin_snap &&
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  2375
	    origin_head->ds_dir != clone->ds_prev->ds_dir)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2376
		return (SET_ERROR(EINVAL));
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2377
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2378
	/* the clone should be a child of the origin */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2379
	if (clone->ds_dir->dd_parent != origin_head->ds_dir)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2380
		return (SET_ERROR(EINVAL));
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2381
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2382
	/* origin_head shouldn't be modified unless 'force' */
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  2383
	if (!force &&
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  2384
	    dsl_dataset_modified_since_snap(origin_head, origin_head->ds_prev))
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2385
		return (SET_ERROR(ETXTBSY));
5481
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5475
diff changeset
  2386
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2387
	/* origin_head should have no long holds (e.g. is not mounted) */
14091
3ca4e9e72fe7 3875 panic in zfs_root() after failed rollback
Keith M Wesolowski <wesolows@foobazco.org>
parents: 14047
diff changeset
  2388
	if (dsl_dataset_handoff_check(origin_head, owner, tx))
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2389
		return (SET_ERROR(EBUSY));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2390
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2391
	/* check amount of any unconsumed refreservation */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2392
	unused_refres_delta =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2393
	    (int64_t)MIN(origin_head->ds_reserved,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2394
	    origin_head->ds_phys->ds_unique_bytes) -
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2395
	    (int64_t)MIN(origin_head->ds_reserved,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2396
	    clone->ds_phys->ds_unique_bytes);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2397
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2398
	if (unused_refres_delta > 0 &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2399
	    unused_refres_delta >
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2400
	    dsl_dir_space_available(origin_head->ds_dir, NULL, 0, TRUE))
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2401
		return (SET_ERROR(ENOSPC));
5481
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5475
diff changeset
  2402
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2403
	/* clone can't be over the head's refquota */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2404
	if (origin_head->ds_quota != 0 &&
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2405
	    clone->ds_phys->ds_referenced_bytes > origin_head->ds_quota)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2406
		return (SET_ERROR(EDQUOT));
10799
395576ddfea9 6881321 zfs test rsend_012_pos panics when running daily.0807 archives
Chris Kirby <Chris.Kirby@sun.com>
parents: 10588
diff changeset
  2407
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2408
	return (0);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2409
}
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2410
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2411
void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2412
dsl_dataset_clone_swap_sync_impl(dsl_dataset_t *clone,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2413
    dsl_dataset_t *origin_head, dmu_tx_t *tx)
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2414
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2415
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2416
	int64_t unused_refres_delta;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2417
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2418
	ASSERT(clone->ds_reserved == 0);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2419
	ASSERT(origin_head->ds_quota == 0 ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2420
	    clone->ds_phys->ds_unique_bytes <= origin_head->ds_quota);
14097
8a2884391fc2 3888 zfs recv -F should destroy any snapshots created since the incremental source
Matthew Ahrens <mahrens@delphix.com>
parents: 14092
diff changeset
  2421
	ASSERT3P(clone->ds_prev, ==, origin_head->ds_prev);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2422
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2423
	dmu_buf_will_dirty(clone->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2424
	dmu_buf_will_dirty(origin_head->ds_dbuf, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2425
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2426
	if (clone->ds_objset != NULL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2427
		dmu_objset_evict(clone->ds_objset);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2428
		clone->ds_objset = NULL;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2429
	}
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2430
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2431
	if (origin_head->ds_objset != NULL) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2432
		dmu_objset_evict(origin_head->ds_objset);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2433
		origin_head->ds_objset = NULL;
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2434
	}
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2435
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2436
	unused_refres_delta =
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2437
	    (int64_t)MIN(origin_head->ds_reserved,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2438
	    origin_head->ds_phys->ds_unique_bytes) -
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2439
	    (int64_t)MIN(origin_head->ds_reserved,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2440
	    clone->ds_phys->ds_unique_bytes);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2441
10272
a0669934e974 6861581 ZFS frees in synching context during rollback
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10268
diff changeset
  2442
	/*
a0669934e974 6861581 ZFS frees in synching context during rollback
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10268
diff changeset
  2443
	 * Reset origin's unique bytes, if it exists.
a0669934e974 6861581 ZFS frees in synching context during rollback
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10268
diff changeset
  2444
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2445
	if (clone->ds_prev) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2446
		dsl_dataset_t *origin = clone->ds_prev;
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2447
		uint64_t comp, uncomp;
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2448
10272
a0669934e974 6861581 ZFS frees in synching context during rollback
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10268
diff changeset
  2449
		dmu_buf_will_dirty(origin->ds_dbuf, tx);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2450
		dsl_deadlist_space_range(&clone->ds_deadlist,
10272
a0669934e974 6861581 ZFS frees in synching context during rollback
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10268
diff changeset
  2451
		    origin->ds_phys->ds_prev_snap_txg, UINT64_MAX,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2452
		    &origin->ds_phys->ds_unique_bytes, &comp, &uncomp);
10272
a0669934e974 6861581 ZFS frees in synching context during rollback
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10268
diff changeset
  2453
	}
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2454
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2455
	/* swap blkptrs */
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2456
	{
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2457
		blkptr_t tmp;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2458
		tmp = origin_head->ds_phys->ds_bp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2459
		origin_head->ds_phys->ds_bp = clone->ds_phys->ds_bp;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2460
		clone->ds_phys->ds_bp = tmp;
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2461
	}
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2462
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2463
	/* set dd_*_bytes */
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2464
	{
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2465
		int64_t dused, dcomp, duncomp;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2466
		uint64_t cdl_used, cdl_comp, cdl_uncomp;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2467
		uint64_t odl_used, odl_comp, odl_uncomp;
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2468
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2469
		ASSERT3U(clone->ds_dir->dd_phys->
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2470
		    dd_used_breakdown[DD_USED_SNAP], ==, 0);
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2471
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2472
		dsl_deadlist_space(&clone->ds_deadlist,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2473
		    &cdl_used, &cdl_comp, &cdl_uncomp);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2474
		dsl_deadlist_space(&origin_head->ds_deadlist,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2475
		    &odl_used, &odl_comp, &odl_uncomp);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2476
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2477
		dused = clone->ds_phys->ds_referenced_bytes + cdl_used -
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2478
		    (origin_head->ds_phys->ds_referenced_bytes + odl_used);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2479
		dcomp = clone->ds_phys->ds_compressed_bytes + cdl_comp -
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2480
		    (origin_head->ds_phys->ds_compressed_bytes + odl_comp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2481
		duncomp = clone->ds_phys->ds_uncompressed_bytes +
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2482
		    cdl_uncomp -
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2483
		    (origin_head->ds_phys->ds_uncompressed_bytes + odl_uncomp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2484
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2485
		dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_HEAD,
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2486
		    dused, dcomp, duncomp, tx);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2487
		dsl_dir_diduse_space(clone->ds_dir, DD_USED_HEAD,
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2488
		    -dused, -dcomp, -duncomp, tx);
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2489
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2490
		/*
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2491
		 * The difference in the space used by snapshots is the
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2492
		 * difference in snapshot space due to the head's
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2493
		 * deadlist (since that's the only thing that's
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2494
		 * changing that affects the snapused).
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2495
		 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2496
		dsl_deadlist_space_range(&clone->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2497
		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2498
		    &cdl_used, &cdl_comp, &cdl_uncomp);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2499
		dsl_deadlist_space_range(&origin_head->ds_deadlist,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2500
		    origin_head->ds_dir->dd_origin_txg, UINT64_MAX,
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2501
		    &odl_used, &odl_comp, &odl_uncomp);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2502
		dsl_dir_transfer_space(origin_head->ds_dir, cdl_used - odl_used,
7390
6d408f0a5fbd PSARC/2008/518 ZFS space accounting enhancements
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7385
diff changeset
  2503
		    DD_USED_HEAD, DD_USED_SNAP, tx);
5367
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2504
	}
c40abbe796be PSARC/2007/574 zfs send -R
ahrens
parents: 5326
diff changeset
  2505
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2506
	/* swap ds_*_bytes */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2507
	SWITCH64(origin_head->ds_phys->ds_referenced_bytes,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2508
	    clone->ds_phys->ds_referenced_bytes);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2509
	SWITCH64(origin_head->ds_phys->ds_compressed_bytes,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2510
	    clone->ds_phys->ds_compressed_bytes);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2511
	SWITCH64(origin_head->ds_phys->ds_uncompressed_bytes,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2512
	    clone->ds_phys->ds_uncompressed_bytes);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2513
	SWITCH64(origin_head->ds_phys->ds_unique_bytes,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2514
	    clone->ds_phys->ds_unique_bytes);
5481
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5475
diff changeset
  2515
1364fb7de75d 6619182 new non-sparse zvols should get refreservations
ck153898
parents: 5475
diff changeset
  2516
	/* apply any parent delta for change in unconsumed refreservation */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2517
	dsl_dir_diduse_space(origin_head->ds_dir, DD_USED_REFRSRV,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2518
	    unused_refres_delta, 0, 0, tx);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2519
12470
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2520
	/*
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2521
	 * Swap deadlists.
54258108784b 6948890 snapshot deletion can induce pathologically long spa_sync() times
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 12450
diff changeset
  2522
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2523
	dsl_deadlist_close(&clone->ds_deadlist);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2524
	dsl_deadlist_close(&origin_head->ds_deadlist);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2525
	SWITCH64(origin_head->ds_phys->ds_deadlist_obj,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2526
	    clone->ds_phys->ds_deadlist_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2527
	dsl_deadlist_open(&clone->ds_deadlist, dp->dp_meta_objset,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2528
	    clone->ds_phys->ds_deadlist_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2529
	dsl_deadlist_open(&origin_head->ds_deadlist, dp->dp_meta_objset,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2530
	    origin_head->ds_phys->ds_deadlist_obj);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2531
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2532
	dsl_scan_ds_clone_swapped(origin_head, clone, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2533
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2534
	spa_history_log_internal_ds(clone, "clone swap", tx,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2535
	    "parent=%s", origin_head->ds_dir->dd_myname);
5326
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2536
}
6752aa2bd5bc 6425096 want online 'zfs recv' (read only and read/write)
ek110237
parents: 5094
diff changeset
  2537
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2538
/*
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2539
 * Given a pool name and a dataset object number in that pool,
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2540
 * return the name of that dataset.
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2541
 */
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2542
int
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2543
dsl_dsobj_to_dsname(char *pname, uint64_t obj, char *buf)
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2544
{
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2545
	dsl_pool_t *dp;
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2546
	dsl_dataset_t *ds;
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2547
	int error;
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2548
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2549
	error = dsl_pool_hold(pname, FTAG, &dp);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2550
	if (error != 0)
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2551
		return (error);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2552
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2553
	error = dsl_dataset_hold_obj(dp, obj, FTAG, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2554
	if (error == 0) {
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2555
		dsl_dataset_name(ds, buf);
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2556
		dsl_dataset_rele(ds, FTAG);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2557
	}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2558
	dsl_pool_rele(dp, FTAG);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2559
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2560
	return (error);
3912
f6891a60bd72 PSARC 2007/083 ZFS bootable datasets
lling
parents: 3547
diff changeset
  2561
}
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2562
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2563
int
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2564
dsl_dataset_check_quota(dsl_dataset_t *ds, boolean_t check_quota,
6689
47572a2f5e73 6610506 Eliminate or improve retry logic from callers of dmu_objset_open()
maybee
parents: 6643
diff changeset
  2565
    uint64_t asize, uint64_t inflight, uint64_t *used, uint64_t *ref_rsrv)
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2566
{
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2567
	int error = 0;
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2568
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2569
	ASSERT3S(asize, >, 0);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2570
5831
48655d6b290b 6630761 In sub-filesystem, available space is less than refreservation space
ck153898
parents: 5712
diff changeset
  2571
	/*
48655d6b290b 6630761 In sub-filesystem, available space is less than refreservation space
ck153898
parents: 5712
diff changeset
  2572
	 * *ref_rsrv is the portion of asize that will come from any
48655d6b290b 6630761 In sub-filesystem, available space is less than refreservation space
ck153898
parents: 5712
diff changeset
  2573
	 * unconsumed refreservation space.
48655d6b290b 6630761 In sub-filesystem, available space is less than refreservation space
ck153898
parents: 5712
diff changeset
  2574
	 */
48655d6b290b 6630761 In sub-filesystem, available space is less than refreservation space
ck153898
parents: 5712
diff changeset
  2575
	*ref_rsrv = 0;
48655d6b290b 6630761 In sub-filesystem, available space is less than refreservation space
ck153898
parents: 5712
diff changeset
  2576
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2577
	mutex_enter(&ds->ds_lock);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2578
	/*
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2579
	 * Make a space adjustment for reserved bytes.
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2580
	 */
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2581
	if (ds->ds_reserved > ds->ds_phys->ds_unique_bytes) {
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2582
		ASSERT3U(*used, >=,
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2583
		    ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2584
		*used -= (ds->ds_reserved - ds->ds_phys->ds_unique_bytes);
5831
48655d6b290b 6630761 In sub-filesystem, available space is less than refreservation space
ck153898
parents: 5712
diff changeset
  2585
		*ref_rsrv =
48655d6b290b 6630761 In sub-filesystem, available space is less than refreservation space
ck153898
parents: 5712
diff changeset
  2586
		    asize - MIN(asize, parent_delta(ds, asize + inflight));
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2587
	}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2588
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2589
	if (!check_quota || ds->ds_quota == 0) {
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2590
		mutex_exit(&ds->ds_lock);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2591
		return (0);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2592
	}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2593
	/*
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2594
	 * If they are requesting more space, and our current estimate
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2595
	 * is over quota, they get to try again unless the actual
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2596
	 * on-disk is over quota and there are no pending changes (which
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2597
	 * may free up space for us).
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2598
	 */
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2599
	if (ds->ds_phys->ds_referenced_bytes + inflight >= ds->ds_quota) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2600
		if (inflight > 0 ||
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2601
		    ds->ds_phys->ds_referenced_bytes < ds->ds_quota)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2602
			error = SET_ERROR(ERESTART);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2603
		else
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2604
			error = SET_ERROR(EDQUOT);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2605
	}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2606
	mutex_exit(&ds->ds_lock);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2607
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2608
	return (error);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2609
}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2610
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2611
typedef struct dsl_dataset_set_qr_arg {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2612
	const char *ddsqra_name;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2613
	zprop_source_t ddsqra_source;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2614
	uint64_t ddsqra_value;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2615
} dsl_dataset_set_qr_arg_t;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2616
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2617
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2618
/* ARGSUSED */
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2619
static int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2620
dsl_dataset_set_refquota_check(void *arg, dmu_tx_t *tx)
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2621
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2622
	dsl_dataset_set_qr_arg_t *ddsqra = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2623
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2624
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2625
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2626
	uint64_t newval;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2627
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2628
	if (spa_version(dp->dp_spa) < SPA_VERSION_REFQUOTA)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2629
		return (SET_ERROR(ENOTSUP));
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2630
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2631
	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2632
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2633
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2634
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2635
	if (dsl_dataset_is_snapshot(ds)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2636
		dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2637
		return (SET_ERROR(EINVAL));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2638
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2639
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2640
	error = dsl_prop_predict(ds->ds_dir,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2641
	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2642
	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2643
	if (error != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2644
		dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2645
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2646
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2647
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2648
	if (newval == 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2649
		dsl_dataset_rele(ds, FTAG);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2650
		return (0);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2651
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2652
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2653
	if (newval < ds->ds_phys->ds_referenced_bytes ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2654
	    newval < ds->ds_reserved) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2655
		dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2656
		return (SET_ERROR(ENOSPC));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2657
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2658
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2659
	dsl_dataset_rele(ds, FTAG);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2660
	return (0);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2661
}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2662
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2663
static void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2664
dsl_dataset_set_refquota_sync(void *arg, dmu_tx_t *tx)
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2665
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2666
	dsl_dataset_set_qr_arg_t *ddsqra = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2667
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2668
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2669
	uint64_t newval;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2670
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2671
	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2672
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2673
	dsl_prop_set_sync_impl(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2674
	    zfs_prop_to_name(ZFS_PROP_REFQUOTA),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2675
	    ddsqra->ddsqra_source, sizeof (ddsqra->ddsqra_value), 1,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2676
	    &ddsqra->ddsqra_value, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2677
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2678
	VERIFY0(dsl_prop_get_int_ds(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2679
	    zfs_prop_to_name(ZFS_PROP_REFQUOTA), &newval));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2680
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2681
	if (ds->ds_quota != newval) {
11022
63ab26072e41 PSARC 2009/510 ZFS received properties
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 10951
diff changeset
  2682
		dmu_buf_will_dirty(ds->ds_dbuf, tx);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2683
		ds->ds_quota = newval;
11022
63ab26072e41 PSARC 2009/510 ZFS received properties
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 10951
diff changeset
  2684
	}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2685
	dsl_dataset_rele(ds, FTAG);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2686
}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2687
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2688
int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2689
dsl_dataset_set_refquota(const char *dsname, zprop_source_t source,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2690
    uint64_t refquota)
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2691
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2692
	dsl_dataset_set_qr_arg_t ddsqra;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2693
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2694
	ddsqra.ddsqra_name = dsname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2695
	ddsqra.ddsqra_source = source;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2696
	ddsqra.ddsqra_value = refquota;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2697
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2698
	return (dsl_sync_task(dsname, dsl_dataset_set_refquota_check,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2699
	    dsl_dataset_set_refquota_sync, &ddsqra, 0));
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2700
}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2701
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2702
static int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2703
dsl_dataset_set_refreservation_check(void *arg, dmu_tx_t *tx)
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2704
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2705
	dsl_dataset_set_qr_arg_t *ddsqra = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2706
	dsl_pool_t *dp = dmu_tx_pool(tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2707
	dsl_dataset_t *ds;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2708
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2709
	uint64_t newval, unique;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2710
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2711
	if (spa_version(dp->dp_spa) < SPA_VERSION_REFRESERVATION)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2712
		return (SET_ERROR(ENOTSUP));
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2713
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2714
	error = dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2715
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2716
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2717
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2718
	if (dsl_dataset_is_snapshot(ds)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2719
		dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2720
		return (SET_ERROR(EINVAL));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2721
	}
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2722
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2723
	error = dsl_prop_predict(ds->ds_dir,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2724
	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2725
	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, &newval);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2726
	if (error != 0) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2727
		dsl_dataset_rele(ds, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2728
		return (error);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2729
	}
11022
63ab26072e41 PSARC 2009/510 ZFS received properties
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 10951
diff changeset
  2730
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2731
	/*
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2732
	 * If we are doing the preliminary check in open context, the
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2733
	 * space estimates may be inaccurate.
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2734
	 */
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2735
	if (!dmu_tx_is_syncing(tx)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2736
		dsl_dataset_rele(ds, FTAG);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2737
		return (0);
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2738
	}
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2739
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2740
	mutex_enter(&ds->ds_lock);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  2741
	if (!DS_UNIQUE_IS_ACCURATE(ds))
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  2742
		dsl_dataset_recalc_head_uniq(ds);
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  2743
	unique = ds->ds_phys->ds_unique_bytes;
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2744
	mutex_exit(&ds->ds_lock);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2745
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2746
	if (MAX(unique, newval) > MAX(unique, ds->ds_reserved)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2747
		uint64_t delta = MAX(unique, newval) -
8525
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8517
diff changeset
  2748
		    MAX(unique, ds->ds_reserved);
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8517
diff changeset
  2749
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2750
		if (delta >
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2751
		    dsl_dir_space_available(ds->ds_dir, NULL, 0, B_TRUE) ||
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2752
		    (ds->ds_quota > 0 && newval > ds->ds_quota)) {
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2753
			dsl_dataset_rele(ds, FTAG);
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2754
			return (SET_ERROR(ENOSPC));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2755
		}
8525
e0e0e525d0f8 6788830 set large value to reservation cause core dump
Eric Schrock <Eric.Schrock@Sun.COM>
parents: 8517
diff changeset
  2756
	}
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2757
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2758
	dsl_dataset_rele(ds, FTAG);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2759
	return (0);
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2760
}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2761
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2762
void
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2763
dsl_dataset_set_refreservation_sync_impl(dsl_dataset_t *ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2764
    zprop_source_t source, uint64_t value, dmu_tx_t *tx)
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2765
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2766
	uint64_t newval;
7595
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2767
	uint64_t unique;
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2768
	int64_t delta;
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2769
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2770
	dsl_prop_set_sync_impl(ds, zfs_prop_to_name(ZFS_PROP_REFRESERVATION),
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2771
	    source, sizeof (value), 1, &value, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2772
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2773
	VERIFY0(dsl_prop_get_int_ds(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2774
	    zfs_prop_to_name(ZFS_PROP_REFRESERVATION), &newval));
11022
63ab26072e41 PSARC 2009/510 ZFS received properties
Tom Erickson <Tom.Erickson@Sun.COM>
parents: 10951
diff changeset
  2775
7595
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2776
	dmu_buf_will_dirty(ds->ds_dbuf, tx);
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2777
	mutex_enter(&ds->ds_dir->dd_lock);
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2778
	mutex_enter(&ds->ds_lock);
12296
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  2779
	ASSERT(DS_UNIQUE_IS_ACCURATE(ds));
7cf402a7f374 6675946 'zpool status' should show the progress of resilvering for individual disk.
Lin Ling <Lin.Ling@Sun.COM>
parents: 12295
diff changeset
  2780
	unique = ds->ds_phys->ds_unique_bytes;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2781
	delta = MAX(0, (int64_t)(newval - unique)) -
7595
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2782
	    MAX(0, (int64_t)(ds->ds_reserved - unique));
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2783
	ds->ds_reserved = newval;
7595
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2784
	mutex_exit(&ds->ds_lock);
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2785
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2786
	dsl_dir_diduse_space(ds->ds_dir, DD_USED_REFRSRV, delta, 0, 0, tx);
2ff5700c7efc 6747355 finish closing race condition when setting refreservation
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 7525
diff changeset
  2787
	mutex_exit(&ds->ds_dir->dd_lock);
5378
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2788
}
111aa1baa84a PSARC 2007/555 zfs fs-only quotas and reservations
ck153898
parents: 5367
diff changeset
  2789
12527
693dd2cad55f 6884007 zfs_send() can leave temporary holds around
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12470
diff changeset
  2790
static void
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2791
dsl_dataset_set_refreservation_sync(void *arg, dmu_tx_t *tx)
10242
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
  2792
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2793
	dsl_dataset_set_qr_arg_t *ddsqra = arg;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2794
	dsl_pool_t *dp = dmu_tx_pool(tx);
10242
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
  2795
	dsl_dataset_t *ds;
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2796
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2797
	VERIFY0(dsl_dataset_hold(dp, ddsqra->ddsqra_name, FTAG, &ds));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2798
	dsl_dataset_set_refreservation_sync_impl(ds,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2799
	    ddsqra->ddsqra_source, ddsqra->ddsqra_value, tx);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2800
	dsl_dataset_rele(ds, FTAG);
12798
f6c8601080b4 6938335 zfs send -R can still miss renamed snapshots
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12711
diff changeset
  2801
}
f6c8601080b4 6938335 zfs send -R can still miss renamed snapshots
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12711
diff changeset
  2802
f6c8601080b4 6938335 zfs send -R can still miss renamed snapshots
Chris Kirby <Chris.Kirby@oracle.com>
parents: 12711
diff changeset
  2803
int
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2804
dsl_dataset_set_refreservation(const char *dsname, zprop_source_t source,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2805
    uint64_t refreservation)
10242
c40d075fbca6 PSARC/2009/297 zfs snapshot holds
Chris Kirby <chris.kirby@sun.com>
parents: 10204
diff changeset
  2806
{
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2807
	dsl_dataset_set_qr_arg_t ddsqra;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2808
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2809
	ddsqra.ddsqra_name = dsname;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2810
	ddsqra.ddsqra_source = source;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2811
	ddsqra.ddsqra_value = refreservation;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2812
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2813
	return (dsl_sync_task(dsname, dsl_dataset_set_refreservation_check,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2814
	    dsl_dataset_set_refreservation_sync, &ddsqra, 0));
10298
a0d52501437c 6860996 %temporary clones are not automatically destroyed on error
Matthew Ahrens <Matthew.Ahrens@Sun.COM>
parents: 10272
diff changeset
  2815
}
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2816
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2817
/*
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2818
 * Return (in *usedp) the amount of space written in new that is not
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2819
 * present in oldsnap.  New may be a snapshot or the head.  Old must be
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2820
 * a snapshot before new, in new's filesystem (or its origin).  If not then
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2821
 * fail and return EINVAL.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2822
 *
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2823
 * The written space is calculated by considering two components:  First, we
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2824
 * ignore any freed space, and calculate the written as new's used space
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2825
 * minus old's used space.  Next, we add in the amount of space that was freed
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2826
 * between the two snapshots, thus reducing new's used space relative to old's.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2827
 * Specifically, this is the space that was born before old->ds_creation_txg,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2828
 * and freed before new (ie. on new's deadlist or a previous deadlist).
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2829
 *
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2830
 * space freed                         [---------------------]
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2831
 * snapshots                       ---O-------O--------O-------O------
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2832
 *                                         oldsnap            new
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2833
 */
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2834
int
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2835
dsl_dataset_space_written(dsl_dataset_t *oldsnap, dsl_dataset_t *new,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2836
    uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2837
{
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2838
	int err = 0;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2839
	uint64_t snapobj;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2840
	dsl_pool_t *dp = new->ds_dir->dd_pool;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2841
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2842
	ASSERT(dsl_pool_config_held(dp));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2843
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2844
	*usedp = 0;
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2845
	*usedp += new->ds_phys->ds_referenced_bytes;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2846
	*usedp -= oldsnap->ds_phys->ds_referenced_bytes;
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2847
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2848
	*compp = 0;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2849
	*compp += new->ds_phys->ds_compressed_bytes;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2850
	*compp -= oldsnap->ds_phys->ds_compressed_bytes;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2851
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2852
	*uncompp = 0;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2853
	*uncompp += new->ds_phys->ds_uncompressed_bytes;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2854
	*uncompp -= oldsnap->ds_phys->ds_uncompressed_bytes;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2855
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2856
	snapobj = new->ds_object;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2857
	while (snapobj != oldsnap->ds_object) {
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2858
		dsl_dataset_t *snap;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2859
		uint64_t used, comp, uncomp;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2860
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2861
		if (snapobj == new->ds_object) {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2862
			snap = new;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2863
		} else {
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2864
			err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &snap);
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2865
			if (err != 0)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2866
				break;
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2867
		}
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2868
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2869
		if (snap->ds_phys->ds_prev_snap_txg ==
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2870
		    oldsnap->ds_phys->ds_creation_txg) {
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2871
			/*
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2872
			 * The blocks in the deadlist can not be born after
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2873
			 * ds_prev_snap_txg, so get the whole deadlist space,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2874
			 * which is more efficient (especially for old-format
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2875
			 * deadlists).  Unfortunately the deadlist code
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2876
			 * doesn't have enough information to make this
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2877
			 * optimization itself.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2878
			 */
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2879
			dsl_deadlist_space(&snap->ds_deadlist,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2880
			    &used, &comp, &uncomp);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2881
		} else {
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2882
			dsl_deadlist_space_range(&snap->ds_deadlist,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2883
			    0, oldsnap->ds_phys->ds_creation_txg,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2884
			    &used, &comp, &uncomp);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2885
		}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2886
		*usedp += used;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2887
		*compp += comp;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2888
		*uncompp += uncomp;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2889
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2890
		/*
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2891
		 * If we get to the beginning of the chain of snapshots
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2892
		 * (ds_prev_snap_obj == 0) before oldsnap, then oldsnap
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2893
		 * was not a snapshot of/before new.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2894
		 */
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2895
		snapobj = snap->ds_phys->ds_prev_snap_obj;
13700
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2896
		if (snap != new)
2889e2596bd6 2619 asynchronous destruction of ZFS file systems
Christopher Siden <chris.siden@delphix.com>
parents: 13686
diff changeset
  2897
			dsl_dataset_rele(snap, FTAG);
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2898
		if (snapobj == 0) {
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2899
			err = SET_ERROR(EINVAL);
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2900
			break;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2901
		}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2902
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2903
	}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2904
	return (err);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2905
}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2906
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2907
/*
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2908
 * Return (in *usedp) the amount of space that will be reclaimed if firstsnap,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2909
 * lastsnap, and all snapshots in between are deleted.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2910
 *
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2911
 * blocks that would be freed            [---------------------------]
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2912
 * snapshots                       ---O-------O--------O-------O--------O
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2913
 *                                        firstsnap        lastsnap
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2914
 *
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2915
 * This is the set of blocks that were born after the snap before firstsnap,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2916
 * (birth > firstsnap->prev_snap_txg) and died before the snap after the
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2917
 * last snap (ie, is on lastsnap->ds_next->ds_deadlist or an earlier deadlist).
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2918
 * We calculate this by iterating over the relevant deadlists (from the snap
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2919
 * after lastsnap, backward to the snap after firstsnap), summing up the
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2920
 * space on the deadlist that was born after the snap before firstsnap.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2921
 */
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2922
int
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2923
dsl_dataset_space_wouldfree(dsl_dataset_t *firstsnap,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2924
    dsl_dataset_t *lastsnap,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2925
    uint64_t *usedp, uint64_t *compp, uint64_t *uncompp)
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2926
{
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2927
	int err = 0;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2928
	uint64_t snapobj;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2929
	dsl_pool_t *dp = firstsnap->ds_dir->dd_pool;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2930
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2931
	ASSERT(dsl_dataset_is_snapshot(firstsnap));
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2932
	ASSERT(dsl_dataset_is_snapshot(lastsnap));
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2933
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2934
	/*
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2935
	 * Check that the snapshots are in the same dsl_dir, and firstsnap
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2936
	 * is before lastsnap.
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2937
	 */
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2938
	if (firstsnap->ds_dir != lastsnap->ds_dir ||
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2939
	    firstsnap->ds_phys->ds_creation_txg >
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2940
	    lastsnap->ds_phys->ds_creation_txg)
13980
d7059eb1884c 3598 want to dtrace when errors are generated in zfs
Matthew Ahrens <mahrens@delphix.com>
parents: 13973
diff changeset
  2941
		return (SET_ERROR(EINVAL));
13524
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2942
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2943
	*usedp = *compp = *uncompp = 0;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2944
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2945
	snapobj = lastsnap->ds_phys->ds_next_snap_obj;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2946
	while (snapobj != firstsnap->ds_object) {
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2947
		dsl_dataset_t *ds;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2948
		uint64_t used, comp, uncomp;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2949
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2950
		err = dsl_dataset_hold_obj(dp, snapobj, FTAG, &ds);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2951
		if (err != 0)
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2952
			break;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2953
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2954
		dsl_deadlist_space_range(&ds->ds_deadlist,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2955
		    firstsnap->ds_phys->ds_prev_snap_txg, UINT64_MAX,
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2956
		    &used, &comp, &uncomp);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2957
		*usedp += used;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2958
		*compp += comp;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2959
		*uncompp += uncomp;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2960
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2961
		snapobj = ds->ds_phys->ds_prev_snap_obj;
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2962
		ASSERT3U(snapobj, !=, 0);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2963
		dsl_dataset_rele(ds, FTAG);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2964
	}
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2965
	return (err);
f0e12b33f77c 1644 add ZFS "clones" property
Matthew Ahrens <matt@delphix.com>
parents: 13512
diff changeset
  2966
}
13973
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2967
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2968
/*
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2969
 * Return TRUE if 'earlier' is an earlier snapshot in 'later's timeline.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2970
 * For example, they could both be snapshots of the same filesystem, and
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2971
 * 'earlier' is before 'later'.  Or 'earlier' could be the origin of
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2972
 * 'later's filesystem.  Or 'earlier' could be an older snapshot in the origin's
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2973
 * filesystem.  Or 'earlier' could be the origin's origin.
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2974
 */
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2975
boolean_t
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2976
dsl_dataset_is_before(dsl_dataset_t *later, dsl_dataset_t *earlier)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2977
{
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2978
	dsl_pool_t *dp = later->ds_dir->dd_pool;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2979
	int error;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2980
	boolean_t ret;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2981
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2982
	ASSERT(dsl_pool_config_held(dp));
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2983
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2984
	if (earlier->ds_phys->ds_creation_txg >=
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2985
	    later->ds_phys->ds_creation_txg)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2986
		return (B_FALSE);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2987
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2988
	if (later->ds_dir == earlier->ds_dir)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2989
		return (B_TRUE);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2990
	if (!dsl_dir_is_clone(later->ds_dir))
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2991
		return (B_FALSE);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2992
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2993
	if (later->ds_dir->dd_phys->dd_origin_obj == earlier->ds_object)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2994
		return (B_TRUE);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2995
	dsl_dataset_t *origin;
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2996
	error = dsl_dataset_hold_obj(dp,
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2997
	    later->ds_dir->dd_phys->dd_origin_obj, FTAG, &origin);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2998
	if (error != 0)
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  2999
		return (B_FALSE);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  3000
	ret = dsl_dataset_is_before(origin, earlier);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  3001
	dsl_dataset_rele(origin, FTAG);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  3002
	return (ret);
4972ab336f54 3464 zfs synctask code needs restructuring
Matthew Ahrens <mahrens@delphix.com>
parents: 13969
diff changeset
  3003
}