usr/src/uts/common/fs/zfs/dbuf.c
author maybee
Wed, 23 Nov 2005 07:16:54 -0800
changeset 982 9bc5c1db9740
parent 873 adefbfa5f42d
child 1163 4ba797920cc2
permissions -rw-r--r--
6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename 6354299 Disable metadata compression, at least temporarily
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
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     5
 * Common Development and Distribution License, Version 1.0 only
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     6
 * (the "License").  You may not use this file except in compliance
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     7
 * with the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     8
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
     9
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    10
 * or http://www.opensolaris.org/os/licensing.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    11
 * See the License for the specific language governing permissions
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    12
 * and limitations under the License.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    13
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    14
 * When distributing Covered Code, include this CDDL HEADER in each
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    15
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    16
 * If applicable, add the following below this CDDL HEADER, with the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    17
 * fields enclosed by brackets "[]" replaced with your own identifying
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    18
 * information: Portions Copyright [yyyy] [name of copyright owner]
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    19
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    20
 * CDDL HEADER END
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    21
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    22
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    23
 * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    24
 * Use is subject to license terms.
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
#pragma ident	"%Z%%M%	%I%	%E% SMI"
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    28
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    29
#include <sys/zfs_context.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    30
#include <sys/dmu.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    31
#include <sys/dmu_impl.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    32
#include <sys/dbuf.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    33
#include <sys/dmu_objset.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    34
#include <sys/dsl_dataset.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    35
#include <sys/dsl_dir.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    36
#include <sys/dmu_tx.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    37
#include <sys/spa.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    38
#include <sys/zio.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    39
#include <sys/dmu_zfetch.h>
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    40
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    41
static void dbuf_destroy(dmu_buf_impl_t *db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    42
static int dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    43
static arc_done_func_t dbuf_write_done;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    44
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    45
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    46
 * Global data structures and functions for the dbuf cache.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    47
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    48
taskq_t *dbuf_tq;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    49
static kmem_cache_t *dbuf_cache;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    50
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    51
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    52
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    53
dbuf_cons(void *vdb, void *unused, int kmflag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    54
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    55
	dmu_buf_impl_t *db = vdb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    56
	bzero(db, sizeof (dmu_buf_impl_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    57
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    58
	mutex_init(&db->db_mtx, NULL, MUTEX_DEFAULT, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    59
	cv_init(&db->db_changed, NULL, CV_DEFAULT, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    60
	refcount_create(&db->db_holds);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    61
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    62
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    63
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    64
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    65
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    66
dbuf_dest(void *vdb, void *unused)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    67
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    68
	dmu_buf_impl_t *db = vdb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    69
	mutex_destroy(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    70
	cv_destroy(&db->db_changed);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    71
	refcount_destroy(&db->db_holds);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    72
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    73
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    74
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    75
 * dbuf hash table routines
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    76
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    77
static dbuf_hash_table_t dbuf_hash_table;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    78
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    79
static uint64_t dbuf_hash_count;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    80
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    81
static uint64_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    82
dbuf_hash(void *os, uint64_t obj, uint8_t lvl, uint64_t blkid)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    83
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    84
	uintptr_t osv = (uintptr_t)os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    85
	uint64_t crc = -1ULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    86
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    87
	ASSERT(zfs_crc64_table[128] == ZFS_CRC64_POLY);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    88
	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (lvl)) & 0xFF];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    89
	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (osv >> 6)) & 0xFF];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    90
	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 0)) & 0xFF];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    91
	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (obj >> 8)) & 0xFF];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    92
	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 0)) & 0xFF];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    93
	crc = (crc >> 8) ^ zfs_crc64_table[(crc ^ (blkid >> 8)) & 0xFF];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    94
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    95
	crc ^= (osv>>14) ^ (obj>>16) ^ (blkid>>16);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    96
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    97
	return (crc);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    98
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
    99
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   100
#define	DBUF_HASH(os, obj, level, blkid) dbuf_hash(os, obj, level, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   101
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   102
#define	DBUF_EQUAL(dbuf, os, obj, level, blkid)		\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   103
	((dbuf)->db.db_object == (obj) &&		\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   104
	(dbuf)->db_objset == (os) &&			\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   105
	(dbuf)->db_level == (level) &&			\
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   106
	(dbuf)->db_blkid == (blkid))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   107
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   108
dmu_buf_impl_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   109
dbuf_find(dnode_t *dn, uint8_t level, uint64_t blkid)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   110
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   111
	dbuf_hash_table_t *h = &dbuf_hash_table;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   112
	objset_impl_t *os = dn->dn_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   113
	uint64_t obj = dn->dn_object;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   114
	uint64_t hv = DBUF_HASH(os, obj, level, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   115
	uint64_t idx = hv & h->hash_table_mask;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   116
	dmu_buf_impl_t *db;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   117
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   118
	mutex_enter(DBUF_HASH_MUTEX(h, idx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   119
	for (db = h->hash_table[idx]; db != NULL; db = db->db_hash_next) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   120
		if (DBUF_EQUAL(db, os, obj, level, blkid)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   121
			mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   122
			if (!refcount_is_zero(&db->db_holds)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   123
				mutex_exit(DBUF_HASH_MUTEX(h, idx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   124
				return (db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   125
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   126
			mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   127
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   128
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   129
	mutex_exit(DBUF_HASH_MUTEX(h, idx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   130
	return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   131
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   132
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   133
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   134
 * Insert an entry into the hash table.  If there is already an element
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   135
 * equal to elem in the hash table, then the already existing element
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   136
 * will be returned and the new element will not be inserted.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   137
 * Otherwise returns NULL.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   138
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   139
static dmu_buf_impl_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   140
dbuf_hash_insert(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   141
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   142
	dbuf_hash_table_t *h = &dbuf_hash_table;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   143
	objset_impl_t *os = db->db_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   144
	uint64_t obj = db->db.db_object;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   145
	int level = db->db_level;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   146
	uint64_t blkid = db->db_blkid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   147
	uint64_t hv = DBUF_HASH(os, obj, level, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   148
	uint64_t idx = hv & h->hash_table_mask;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   149
	dmu_buf_impl_t *dbf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   150
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   151
	mutex_enter(DBUF_HASH_MUTEX(h, idx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   152
	for (dbf = h->hash_table[idx]; dbf != NULL; dbf = dbf->db_hash_next) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   153
		if (DBUF_EQUAL(dbf, os, obj, level, blkid)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   154
			mutex_enter(&dbf->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   155
			if (!refcount_is_zero(&dbf->db_holds)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   156
				mutex_exit(DBUF_HASH_MUTEX(h, idx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   157
				return (dbf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   158
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   159
			mutex_exit(&dbf->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   160
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   161
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   162
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   163
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   164
	db->db_hash_next = h->hash_table[idx];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   165
	h->hash_table[idx] = db;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   166
	mutex_exit(DBUF_HASH_MUTEX(h, idx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   167
	atomic_add_64(&dbuf_hash_count, 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   168
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   169
	return (NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   170
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   171
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   172
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   173
 * Remove an entry from the hash table.  This operation will
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   174
 * fail if there are any existing holds on the db.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   175
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   176
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   177
dbuf_hash_remove(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   178
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   179
	dbuf_hash_table_t *h = &dbuf_hash_table;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   180
	uint64_t hv = DBUF_HASH(db->db_objset, db->db.db_object,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   181
	    db->db_level, db->db_blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   182
	uint64_t idx = hv & h->hash_table_mask;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   183
	dmu_buf_impl_t *dbf, **dbp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   184
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   185
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   186
	 * We musn't hold db_mtx to maintin lock ordering:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   187
	 * DBUF_HASH_MUTEX > db_mtx.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   188
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   189
	ASSERT(refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   190
	ASSERT(db->db_dnode != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   191
	ASSERT(!MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   192
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   193
	mutex_enter(DBUF_HASH_MUTEX(h, idx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   194
	dbp = &h->hash_table[idx];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   195
	while ((dbf = *dbp) != db) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   196
		dbp = &dbf->db_hash_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   197
		ASSERT(dbf != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   198
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   199
	*dbp = db->db_hash_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   200
	db->db_hash_next = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   201
	mutex_exit(DBUF_HASH_MUTEX(h, idx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   202
	atomic_add_64(&dbuf_hash_count, -1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   203
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   204
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   205
static int dbuf_evictable(dmu_buf_impl_t *db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   206
static void dbuf_clear(dmu_buf_impl_t *db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   207
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   208
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   209
dbuf_evict(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   210
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   211
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   212
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   213
	ASSERT(MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   214
	err = dbuf_evictable(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   215
	ASSERT(err == TRUE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   216
	dbuf_clear(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   217
	dbuf_destroy(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   218
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   219
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   220
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   221
dbuf_evict_user(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   222
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   223
	ASSERT(MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   224
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   225
	if (db->db_level != 0 || db->db_d.db_evict_func == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   226
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   227
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   228
	if (db->db_d.db_user_data_ptr_ptr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   229
		*db->db_d.db_user_data_ptr_ptr = db->db.db_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   230
	db->db_d.db_evict_func(&db->db, db->db_d.db_user_ptr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   231
	db->db_d.db_user_ptr = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   232
	db->db_d.db_user_data_ptr_ptr = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   233
	db->db_d.db_evict_func = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   234
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   235
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   236
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   237
dbuf_init(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   238
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   239
	uint64_t hsize = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   240
	dbuf_hash_table_t *h = &dbuf_hash_table;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   241
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   242
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   243
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   244
	 * The hash table is big enough to fill all of physical memory
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   245
	 * with an average 64k block size.  The table will take up
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   246
	 * totalmem*sizeof(void*)/64k bytes (i.e. 128KB/GB with 8-byte
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   247
	 * pointers).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   248
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   249
	while (hsize * 65536 < physmem * PAGESIZE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   250
		hsize <<= 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   251
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   252
	h->hash_table_mask = hsize - 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   253
	h->hash_table = kmem_zalloc(hsize * sizeof (void *), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   254
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   255
	dbuf_cache = kmem_cache_create("dmu_buf_impl_t",
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   256
	    sizeof (dmu_buf_impl_t),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   257
	    0, dbuf_cons, dbuf_dest, NULL, NULL, NULL, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   258
	dbuf_tq = taskq_create("dbuf_tq", 8, maxclsyspri, 50, INT_MAX,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   259
	    TASKQ_PREPOPULATE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   260
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   261
	for (i = 0; i < DBUF_MUTEXES; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   262
		mutex_init(&h->hash_mutexes[i], NULL, MUTEX_DEFAULT, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   263
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   264
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   265
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   266
dbuf_fini(void)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   267
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   268
	dbuf_hash_table_t *h = &dbuf_hash_table;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   269
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   270
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   271
	taskq_destroy(dbuf_tq);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   272
	dbuf_tq = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   273
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   274
	for (i = 0; i < DBUF_MUTEXES; i++)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   275
		mutex_destroy(&h->hash_mutexes[i]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   276
	kmem_free(h->hash_table, (h->hash_table_mask + 1) * sizeof (void *));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   277
	kmem_cache_destroy(dbuf_cache);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   278
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   279
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   280
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   281
 * Other stuff.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   282
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   283
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
   284
#ifdef ZFS_DEBUG
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   285
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   286
dbuf_verify(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   287
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   288
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   289
	dnode_t *dn = db->db_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   290
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   291
	ASSERT(MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   292
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   293
	if (!(zfs_flags & ZFS_DEBUG_DBUF_VERIFY))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   294
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   295
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   296
	ASSERT(db->db_objset != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   297
	if (dn == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   298
		ASSERT(db->db_parent == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   299
		ASSERT(db->db_blkptr == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   300
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   301
		ASSERT3U(db->db.db_object, ==, dn->dn_object);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   302
		ASSERT3P(db->db_objset, ==, dn->dn_objset);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   303
		ASSERT(list_head(&dn->dn_dbufs));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   304
		ASSERT3U(db->db_level, <, dn->dn_nlevels);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   305
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   306
	if (db->db_blkid == DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   307
		ASSERT(dn != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   308
		ASSERT3U(db->db.db_size, ==, dn->dn_bonuslen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   309
		ASSERT3U(db->db.db_offset, ==, DB_BONUS_BLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   310
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   311
		ASSERT3U(db->db.db_offset, ==, db->db_blkid * db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   312
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   313
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   314
	if (db->db_level == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   315
		void **udpp = db->db_d.db_user_data_ptr_ptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   316
		/* we can be momentarily larger in dnode_set_blksz() */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   317
		if (db->db_blkid != DB_BONUS_BLKID && dn) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   318
			ASSERT3U(db->db.db_size, >=, dn->dn_datablksz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   319
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   320
		if (udpp) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   321
			ASSERT((refcount_is_zero(&db->db_holds) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   322
			    *udpp == NULL) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   323
			    (!refcount_is_zero(&db->db_holds) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   324
			    *udpp == db->db.db_data));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   325
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   326
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   327
		if (IS_DNODE_DNODE(db->db.db_object)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   328
			for (i = 0; i < TXG_SIZE; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   329
				/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   330
				 * it should only be modified in syncing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   331
				 * context, so make sure we only have
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   332
				 * one copy of the data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   333
				 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   334
				ASSERT(db->db_d.db_data_old[i] == NULL ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   335
				    db->db_d.db_data_old[i] == db->db_buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   336
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   337
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   338
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   339
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   340
	/* verify db->db_blkptr */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   341
	if (db->db_blkptr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   342
		if (db->db_parent == dn->dn_dbuf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   343
			/* db is pointed to by the dnode */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   344
			/* ASSERT3U(db->db_blkid, <, dn->dn_nblkptr); */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   345
			if (IS_DNODE_DNODE(db->db.db_object))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   346
				ASSERT(db->db_parent == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   347
			else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   348
				ASSERT(db->db_parent != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   349
			ASSERT3P(db->db_blkptr, ==,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   350
			    &dn->dn_phys->dn_blkptr[db->db_blkid]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   351
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   352
			/* db is pointed to by an indirect block */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   353
			int epb = db->db_parent->db.db_size >> SPA_BLKPTRSHIFT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   354
			ASSERT3U(db->db_parent->db_level, ==, db->db_level+1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   355
			ASSERT3U(db->db_parent->db.db_object, ==,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   356
			    db->db.db_object);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   357
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   358
			 * dnode_grow_indblksz() can make this fail if we don't
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   359
			 * have the struct_rwlock.  XXX indblksz no longer
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   360
			 * grows.  safe to do this now?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   361
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   362
			if (RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   363
				ASSERT3P(db->db_blkptr, ==,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   364
				    ((blkptr_t *)db->db_parent->db.db_data +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   365
				    db->db_blkid % epb));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   366
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   367
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   368
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   369
	if ((db->db_blkptr == NULL || BP_IS_HOLE(db->db_blkptr)) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   370
	    db->db.db_data && db->db_blkid != DB_BONUS_BLKID &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   371
	    db->db_state != DB_FILL && !dn->dn_free_txg) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   372
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   373
		 * If the blkptr isn't set but they have nonzero data,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   374
		 * it had better be dirty, otherwise we'll lose that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   375
		 * data when we evict this buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   376
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   377
		if (db->db_dirtycnt == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   378
			uint64_t *buf = db->db.db_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   379
			int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   380
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   381
			for (i = 0; i < db->db.db_size >> 3; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   382
				ASSERT(buf[i] == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   383
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   384
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   385
	}
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
   386
}
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   387
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   388
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   389
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   390
dbuf_update_data(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   391
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   392
	ASSERT(MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   393
	if (db->db_level == 0 && db->db_d.db_user_data_ptr_ptr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   394
		ASSERT(!refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   395
		*db->db_d.db_user_data_ptr_ptr = db->db.db_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   396
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   397
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   398
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   399
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   400
dbuf_set_data(dmu_buf_impl_t *db, arc_buf_t *buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   401
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   402
	ASSERT(MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   403
	ASSERT(buf->b_data != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   404
	db->db_buf = buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   405
	db->db.db_data = buf->b_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   406
	dbuf_update_data(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   407
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   408
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   409
uint64_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   410
dbuf_whichblock(dnode_t *dn, uint64_t offset)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   411
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   412
	if (dn->dn_datablkshift) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   413
		return (offset >> dn->dn_datablkshift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   414
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   415
		ASSERT3U(offset, <, dn->dn_datablksz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   416
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   417
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   418
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   419
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   420
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   421
dbuf_read_done(zio_t *zio, arc_buf_t *buf, void *vdb)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   422
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   423
	dmu_buf_impl_t *db = vdb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   424
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   425
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   426
	ASSERT3U(db->db_state, ==, DB_READ);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   427
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   428
	 * All reads are synchronous, so we must have a hold on the dbuf
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   429
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   430
	ASSERT(refcount_count(&db->db_holds) > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   431
	ASSERT(db->db.db_data == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   432
	if (db->db_level == 0 && db->db_d.db_freed_in_flight) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   433
		/* we were freed in flight; disregard any error */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   434
		arc_release(buf, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   435
		bzero(buf->b_data, db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   436
		db->db_d.db_freed_in_flight = FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   437
		dbuf_set_data(db, buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   438
		db->db_state = DB_CACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   439
	} else if (zio == NULL || zio->io_error == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   440
		dbuf_set_data(db, buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   441
		db->db_state = DB_CACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   442
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   443
		ASSERT(db->db_blkid != DB_BONUS_BLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   444
		arc_buf_free(buf, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   445
		db->db_state = DB_UNCACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   446
		ASSERT3P(db->db_buf, ==, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   447
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   448
	cv_broadcast(&db->db_changed);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   449
	mutex_exit(&db->db_mtx);
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
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   453
dbuf_read_impl(dmu_buf_impl_t *db, zio_t *zio, uint32_t flags)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   454
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   455
	arc_buf_t *buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   456
	blkptr_t *bp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   457
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   458
	ASSERT(!refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   459
	/* We need the struct_rwlock to prevent db_blkptr from changing. */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   460
	ASSERT(RW_LOCK_HELD(&db->db_dnode->dn_struct_rwlock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   461
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   462
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   463
	 * prefetch only data blocks (level 0) -- don't prefetch indirect
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   464
	 * blocks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   465
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   466
	if ((db->db_level > 0) || (db->db_blkid == DB_BONUS_BLKID)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   467
		flags |= DB_RF_NOPREFETCH;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   468
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   469
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   470
	if (((flags & DB_RF_NOPREFETCH) == 0) && (db->db_dnode != NULL)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   471
		dmu_zfetch(&db->db_dnode->dn_zfetch, db->db.db_offset,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   472
		    db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   473
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   474
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   475
	if (db->db_state == DB_CACHED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   476
		ASSERT(db->db.db_data != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   477
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   478
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   479
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   480
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   481
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   482
	if (db->db_state != DB_UNCACHED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   483
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   484
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   485
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   486
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   487
	ASSERT3U(db->db_state, ==, DB_UNCACHED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   488
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   489
	if (db->db_blkid == DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   490
		ASSERT3U(db->db_dnode->dn_bonuslen, ==, db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   491
		buf = arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   492
		    DN_MAX_BONUSLEN, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   493
		if (db->db.db_size < DN_MAX_BONUSLEN)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   494
			bzero(buf->b_data, DN_MAX_BONUSLEN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   495
		bcopy(DN_BONUS(db->db_dnode->dn_phys), buf->b_data,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   496
		    db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   497
		dbuf_set_data(db, buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   498
		db->db_state = DB_CACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   499
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   500
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   501
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   502
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   503
	if (db->db_level == 0 && dnode_block_freed(db->db_dnode, db->db_blkid))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   504
		bp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   505
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   506
		bp = db->db_blkptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   507
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   508
	if (bp == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   509
		dprintf_dbuf(db, "blkptr: %s\n", "NULL");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   510
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   511
		dprintf_dbuf_bp(db, bp, "%s", "blkptr:");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   512
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   513
	if (bp == NULL || BP_IS_HOLE(bp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   514
		ASSERT(bp == NULL || BP_IS_HOLE(bp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   515
		dbuf_set_data(db, arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   516
		    db->db.db_size, db));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   517
		bzero(db->db.db_data, db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   518
		db->db_state = DB_CACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   519
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   520
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   521
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   522
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   523
	db->db_state = DB_READ;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   524
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   525
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   526
	/* ZIO_FLAG_CANFAIL callers have to check the parent zio's error */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   527
	(void) arc_read(zio, db->db_dnode->dn_objset->os_spa, bp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   528
	    db->db_level > 0 ? byteswap_uint64_array :
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   529
	    dmu_ot[db->db_dnode->dn_type].ot_byteswap,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   530
	    dbuf_read_done, db, ZIO_PRIORITY_SYNC_READ,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   531
	    (flags & DB_RF_CANFAIL) ? ZIO_FLAG_CANFAIL : ZIO_FLAG_MUSTSUCCEED,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   532
	    ARC_NOWAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   533
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   534
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   535
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   536
dbuf_read_generic(dmu_buf_impl_t *db, uint32_t flags)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   537
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   538
	zio_t *zio;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   539
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   540
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   541
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   542
	 * We don't have to hold the mutex to check db_state because it
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   543
	 * can't be freed while we have a hold on the buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   544
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   545
	ASSERT(!refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   546
	if (db->db_state == DB_CACHED)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   547
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   548
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   549
	if (db->db_state == DB_UNCACHED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   550
		zio = zio_root(db->db_dnode->dn_objset->os_spa, NULL, NULL,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   551
		    ZIO_FLAG_CANFAIL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   552
		if ((flags & DB_RF_HAVESTRUCT) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   553
			rw_enter(&db->db_dnode->dn_struct_rwlock, RW_READER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   554
		dbuf_read_impl(db, zio, flags);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   555
		if ((flags & DB_RF_HAVESTRUCT) == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   556
			rw_exit(&db->db_dnode->dn_struct_rwlock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   557
		err = zio_wait(zio);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   558
		if (err)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   559
			return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   560
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   561
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   562
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   563
	while (db->db_state == DB_READ || db->db_state == DB_FILL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   564
		ASSERT(db->db_state == DB_READ ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   565
		    (flags & DB_RF_HAVESTRUCT) == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   566
		cv_wait(&db->db_changed, &db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   567
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   568
	ASSERT3U(db->db_state, ==, DB_CACHED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   569
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   570
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   571
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   572
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   573
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   574
#pragma weak dmu_buf_read = dbuf_read
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   575
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   576
dbuf_read(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   577
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   578
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   579
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   580
	err = dbuf_read_generic(db, DB_RF_MUST_SUCCEED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   581
	ASSERT(err == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   582
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   583
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   584
#pragma weak dmu_buf_read_canfail = dbuf_read_canfail
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   585
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   586
dbuf_read_canfail(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   587
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   588
	return (dbuf_read_generic(db, DB_RF_CANFAIL));
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
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   592
dbuf_read_havestruct(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   593
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   594
	int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   595
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   596
	ASSERT(RW_LOCK_HELD(&db->db_dnode->dn_struct_rwlock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   597
	err = dbuf_read_generic(db, (DB_RF_HAVESTRUCT | DB_RF_NOPREFETCH));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   598
	ASSERT(err == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   599
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   600
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   601
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   602
dbuf_noread(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   603
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   604
	ASSERT(!refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   605
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   606
	while (db->db_state == DB_READ || db->db_state == DB_FILL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   607
		cv_wait(&db->db_changed, &db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   608
	if (db->db_state == DB_UNCACHED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   609
		int blksz = (db->db_blkid == DB_BONUS_BLKID) ?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   610
		    DN_MAX_BONUSLEN : db->db.db_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   611
		ASSERT(db->db.db_data == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   612
		dbuf_set_data(db, arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   613
		    blksz, db));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   614
		db->db_state = DB_FILL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   615
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   616
		ASSERT3U(db->db_state, ==, DB_CACHED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   617
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   618
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   619
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   620
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   621
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   622
 * This is our just-in-time copy function.  It makes a copy of
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   623
 * buffers, that have been modified in a previous transaction
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   624
 * group, before we modify them in the current active group.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   625
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   626
 * This function is used in two places: when we are dirtying a
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   627
 * buffer for the first time in a txg, and when we are freeing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   628
 * a range in a dnode that includes this buffer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   629
 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   630
 * Note that when we are called from dbuf_free_range() we do
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   631
 * not put a hold on the buffer, we just traverse the active
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   632
 * dbuf list for the dnode.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   633
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   634
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   635
dbuf_fix_old_data(dmu_buf_impl_t *db, uint64_t txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   636
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   637
	arc_buf_t **quiescing, **syncing;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   638
	int size = (db->db_blkid == DB_BONUS_BLKID) ?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   639
	    DN_MAX_BONUSLEN : db->db.db_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   640
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   641
	ASSERT(MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   642
	ASSERT(db->db.db_data != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   643
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   644
	quiescing = &db->db_d.db_data_old[(txg-1)&TXG_MASK];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   645
	syncing = &db->db_d.db_data_old[(txg-2)&TXG_MASK];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   646
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   647
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   648
	 * If this buffer is referenced from the current quiescing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   649
	 * transaction group: either make a copy and reset the reference
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   650
	 * to point to the copy, or (if there a no active holders) just
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   651
	 * null out the current db_data pointer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   652
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   653
	if (*quiescing == db->db_buf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   654
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   655
		 * If the quiescing txg is "dirty", then we better not
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   656
		 * be referencing the same buffer from the syncing txg.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   657
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   658
		ASSERT(*syncing != db->db_buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   659
		if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   660
			*quiescing = arc_buf_alloc(
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   661
			    db->db_dnode->dn_objset->os_spa, size, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   662
			bcopy(db->db.db_data, (*quiescing)->b_data, size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   663
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   664
			db->db.db_data = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   665
			db->db_buf = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   666
			db->db_state = DB_UNCACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   667
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   668
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   669
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   670
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   671
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   672
	 * If this buffer is referenced from the current syncing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   673
	 * transaction group: either
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   674
	 *	1 - make a copy and reset the reference, or
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   675
	 *	2 - if there are no holders, just null the current db_data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   676
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   677
	if (*syncing == db->db_buf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   678
		ASSERT3P(*quiescing, ==, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   679
		ASSERT3U(db->db_dirtycnt, ==, 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   680
		if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   681
			/* we can't copy if we have already started a write */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   682
			ASSERT(*syncing != db->db_data_pending);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   683
			*syncing = arc_buf_alloc(
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   684
			    db->db_dnode->dn_objset->os_spa, size, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   685
			bcopy(db->db.db_data, (*syncing)->b_data, size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   686
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   687
			db->db.db_data = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   688
			db->db_buf = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   689
			db->db_state = DB_UNCACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   690
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   691
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   692
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   693
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   694
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   695
dbuf_unoverride(dmu_buf_impl_t *db, uint64_t txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   696
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   697
	ASSERT(MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   698
	if (db->db_d.db_overridden_by[txg&TXG_MASK] == IN_DMU_SYNC) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   699
		db->db_d.db_overridden_by[txg&TXG_MASK] = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   700
	} else if (db->db_d.db_overridden_by[txg&TXG_MASK] != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   701
		/* free this block */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   702
		ASSERT(list_link_active(&db->db_dirty_node[txg&TXG_MASK]) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   703
		    db->db_dnode->dn_free_txg == txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   704
		if (!BP_IS_HOLE(db->db_d.db_overridden_by[txg&TXG_MASK])) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   705
			/* XXX can get silent EIO here */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   706
			(void) arc_free(NULL, db->db_dnode->dn_objset->os_spa,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   707
			    txg, db->db_d.db_overridden_by[txg&TXG_MASK],
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   708
			    NULL, NULL, ARC_WAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   709
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   710
		kmem_free(db->db_d.db_overridden_by[txg&TXG_MASK],
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   711
		    sizeof (blkptr_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   712
		db->db_d.db_overridden_by[txg&TXG_MASK] = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   713
		/* release the already-written buffer */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   714
		arc_release(db->db_d.db_data_old[txg&TXG_MASK], db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   715
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   716
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   717
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   718
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   719
dbuf_free_range(dnode_t *dn, uint64_t blkid, uint64_t nblks, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   720
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   721
	dmu_buf_impl_t *db, *db_next;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   722
	uint64_t txg = tx->tx_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   723
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   724
	dprintf_dnode(dn, "blkid=%llu nblks=%llu\n", blkid, nblks);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   725
	mutex_enter(&dn->dn_dbufs_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   726
	for (db = list_head(&dn->dn_dbufs); db; db = db_next) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   727
		db_next = list_next(&dn->dn_dbufs, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   728
		if ((db->db_level != 0) || (db->db_blkid == DB_BONUS_BLKID))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   729
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   730
		dprintf_dbuf(db, "found buf %s\n", "");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   731
		if (db->db_blkid < blkid ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   732
		    db->db_blkid >= blkid+nblks)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   733
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   734
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   735
		/* found a level 0 buffer in the range */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   736
		if (dbuf_undirty(db, tx))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   737
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   738
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   739
		mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   740
		if (db->db_state == DB_UNCACHED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   741
			ASSERT(db->db.db_data == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   742
			mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   743
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   744
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   745
		if (db->db_state == DB_READ) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   746
			/* this will be handled in dbuf_read_done() */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   747
			db->db_d.db_freed_in_flight = TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   748
			mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   749
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   750
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   751
		if (db->db_state == DB_FILL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   752
			/* this will be handled in dbuf_rele() */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   753
			db->db_d.db_freed_in_flight = TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   754
			mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   755
			continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   756
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   757
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   758
		/* make a copy of the data if necessary */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   759
		dbuf_fix_old_data(db, txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   760
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   761
		if (db->db.db_data) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   762
			/* fill in with appropriate data */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   763
			arc_release(db->db_buf, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   764
			bzero(db->db.db_data, db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   765
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   766
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   767
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   768
	mutex_exit(&dn->dn_dbufs_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   769
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   770
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   771
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   772
dbuf_new_block(dmu_buf_impl_t *db, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   773
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   774
	dsl_dataset_t *ds = db->db_objset->os_dsl_dataset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   775
	uint64_t birth_txg = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   776
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   777
	/* Don't count meta-objects */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   778
	if (ds == NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   779
		return (FALSE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   780
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   781
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   782
	 * We don't need any locking to protect db_blkptr:
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   783
	 * If it's syncing, then db_dirtied will be set so we'll
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   784
	 * ignore db_blkptr.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   785
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   786
	ASSERT(MUTEX_HELD(&db->db_mtx)); /* XXX strictly necessary? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   787
	/* If we have been dirtied since the last snapshot, its not new */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   788
	if (db->db_dirtied)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   789
		birth_txg = db->db_dirtied;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   790
	else if (db->db_blkptr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   791
		birth_txg = db->db_blkptr->blk_birth;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   792
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   793
	if (birth_txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   794
		return (!dsl_dataset_block_freeable(ds, birth_txg, tx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   795
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   796
		return (TRUE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   797
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   798
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   799
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   800
dbuf_new_size(dmu_buf_impl_t *db, int size, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   801
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   802
	arc_buf_t *buf, *obuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   803
	int osize = db->db.db_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   804
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   805
	/* XXX does *this* func really need the lock? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   806
	ASSERT(RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   807
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   808
	ASSERT3U(osize, <=, size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   809
	if (osize == size)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   810
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   811
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   812
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   813
	 * This call to dbuf_will_dirty() with the dn_struct_rwlock held
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   814
	 * is OK, because there can be no other references to the db
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   815
	 * when we are changing its size, so no concurrent DB_FILL can
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   816
	 * be happening.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   817
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   818
	/* Make a copy of the data if necessary */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   819
	dbuf_will_dirty(db, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   820
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   821
	/* create the data buffer for the new block */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   822
	buf = arc_buf_alloc(db->db_dnode->dn_objset->os_spa, size, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   823
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   824
	/* copy old block data to the new block */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   825
	obuf = db->db_buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   826
	bcopy(obuf->b_data, buf->b_data, osize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   827
	/* zero the remainder */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   828
	bzero((uint8_t *)buf->b_data + osize, size - osize);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   829
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   830
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   831
	/* ASSERT3U(refcount_count(&db->db_holds), ==, 1); */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   832
	dbuf_set_data(db, buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   833
	arc_buf_free(obuf, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   834
	db->db.db_size = size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   835
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   836
	/* fix up the dirty info */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   837
	if (db->db_level == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   838
		db->db_d.db_data_old[tx->tx_txg&TXG_MASK] = buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   839
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   840
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   841
	dnode_willuse_space(db->db_dnode, size-osize, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   842
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   843
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   844
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   845
dbuf_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   846
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   847
	dnode_t *dn = db->db_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   848
	objset_impl_t *os = dn->dn_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   849
	int drop_struct_lock = FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   850
	int txgoff = tx->tx_txg & TXG_MASK;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   851
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   852
	ASSERT(tx->tx_txg != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   853
	ASSERT(!refcount_is_zero(&db->db_holds));
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
   854
	DMU_TX_DIRTY_BUF(tx, db);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   855
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   856
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   857
	 * Shouldn't dirty a regular buffer in syncing context.  Private
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   858
	 * objects may be dirtied in syncing context, but only if they
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   859
	 * were already pre-dirtied in open context.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   860
	 * XXX We may want to prohibit dirtying in syncing context even
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   861
	 * if they did pre-dirty.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   862
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   863
	ASSERT(!(dmu_tx_is_syncing(tx) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   864
	    !BP_IS_HOLE(&dn->dn_objset->os_rootbp) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   865
	    !(dn->dn_object & DMU_PRIVATE_OBJECT) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   866
	    dn->dn_objset->os_dsl_dataset != NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   867
	    !dsl_dir_is_private(
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   868
	    dn->dn_objset->os_dsl_dataset->ds_dir)));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   869
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   870
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   871
	 * We make this assert for private objects as well, but after we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   872
	 * check if we're already dirty.  They are allowed to re-dirty
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   873
	 * in syncing context.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   874
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   875
	ASSERT(dn->dn_object & DMU_PRIVATE_OBJECT ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   876
	    dn->dn_dirtyctx == DN_UNDIRTIED ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   877
	    dn->dn_dirtyctx ==
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   878
	    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   879
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   880
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   881
	/* XXX make this true for indirects too? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   882
	ASSERT(db->db_level != 0 || db->db_state == DB_CACHED ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   883
	    db->db_state == DB_FILL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   884
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   885
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   886
	 * If this buffer is currently part of an "overridden" region,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   887
	 * we now need to remove it from that region.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   888
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   889
	if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   890
	    db->db_d.db_overridden_by[txgoff] != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   891
		dbuf_unoverride(db, tx->tx_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   892
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   893
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   894
	mutex_enter(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   895
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   896
	 * Don't set dirtyctx to SYNC if we're just modifying this as we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   897
	 * initialize the objset.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   898
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   899
	if (dn->dn_dirtyctx == DN_UNDIRTIED &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   900
	    !BP_IS_HOLE(&dn->dn_objset->os_rootbp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   901
		dn->dn_dirtyctx =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   902
		    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   903
		ASSERT(dn->dn_dirtyctx_firstset == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   904
		dn->dn_dirtyctx_firstset = kmem_alloc(1, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   905
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   906
	mutex_exit(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   907
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   908
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   909
	 * If this buffer is already dirty, we're done.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   910
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   911
	if (list_link_active(&db->db_dirty_node[txgoff])) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   912
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   913
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   914
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   915
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   916
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   917
	 * Only valid if not already dirty.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   918
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   919
	ASSERT(dn->dn_dirtyctx == DN_UNDIRTIED || dn->dn_dirtyctx ==
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   920
	    (dmu_tx_is_syncing(tx) ? DN_DIRTY_SYNC : DN_DIRTY_OPEN));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   921
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   922
	ASSERT3U(dn->dn_nlevels, >, db->db_level);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   923
	ASSERT((dn->dn_phys->dn_nlevels == 0 && db->db_level == 0) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   924
	    dn->dn_phys->dn_nlevels > db->db_level ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   925
	    dn->dn_next_nlevels[txgoff] > db->db_level ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   926
	    dn->dn_next_nlevels[(tx->tx_txg-1) & TXG_MASK] > db->db_level ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   927
	    dn->dn_next_nlevels[(tx->tx_txg-2) & TXG_MASK] > db->db_level);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   928
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   929
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   930
	 * We should only be dirtying in syncing context if it's the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   931
	 * mos, a spa os, or we're initializing the os.  However, we are
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   932
	 * allowed to dirty in syncing context provided we already
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   933
	 * dirtied it in open context.  Hence we must make this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   934
	 * assertion only if we're not already dirty.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   935
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   936
	ASSERT(!dmu_tx_is_syncing(tx) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   937
	    os->os_dsl_dataset == NULL ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   938
	    !dsl_dir_is_private(os->os_dsl_dataset->ds_dir) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   939
	    !BP_IS_HOLE(&os->os_rootbp));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   940
	ASSERT(db->db.db_size != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   941
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   942
	dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   943
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   944
	if (db->db_level == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   945
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   946
		 * Release the data buffer from the cache so that we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   947
		 * can modify it without impacting possible other users
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   948
		 * of this cached data block.  Note that indirect blocks
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   949
		 * and private objects are not released until the syncing
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   950
		 * state (since they are only modified then).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   951
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   952
		 * If this buffer is dirty in an old transaction group we need
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   953
		 * to make a copy of it so that the changes we make in this
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   954
		 * transaction group won't leak out when we sync the older txg.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   955
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   956
		ASSERT(db->db_buf != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   957
		ASSERT(db->db.db_data != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   958
		ASSERT(db->db_d.db_data_old[txgoff] == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   959
		if (!(db->db.db_object & DMU_PRIVATE_OBJECT)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   960
			arc_release(db->db_buf, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   961
			dbuf_fix_old_data(db, tx->tx_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   962
			ASSERT(db->db_buf != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   963
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   964
		db->db_d.db_data_old[txgoff] = db->db_buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   965
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   966
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   967
	mutex_enter(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   968
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   969
	 * We could have been freed_in_flight between the dbuf_noread
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   970
	 * and dbuf_dirty.  We win, as though the dbuf_noread() had
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   971
	 * happened after the free.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   972
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   973
	if (db->db_level == 0 && db->db_blkid != DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   974
		dnode_clear_range(dn, db->db_blkid, 1, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   975
		db->db_d.db_freed_in_flight = FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   976
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   977
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   978
	db->db_dirtied = tx->tx_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   979
	list_insert_tail(&dn->dn_dirty_dbufs[txgoff], db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   980
	mutex_exit(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   981
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   982
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   983
	 * If writting this buffer will consume a new block on disk,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   984
	 * then update the accounting.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   985
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   986
	if (db->db_blkid != DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   987
		if (!dbuf_new_block(db, tx) && db->db_blkptr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   988
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   989
			 * This is only a guess -- if the dbuf is dirty
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   990
			 * in a previous txg, we don't know how much
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   991
			 * space it will use on disk yet.  We should
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   992
			 * really have the struct_rwlock to access
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   993
			 * db_blkptr, but since this is just a guess,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   994
			 * it's OK if we get an odd answer.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   995
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   996
			dnode_willuse_space(dn,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   997
			    -BP_GET_ASIZE(db->db_blkptr), tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   998
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
   999
		dnode_willuse_space(dn, db->db.db_size, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1000
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1001
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1002
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1003
	 * This buffer is now part of this txg
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1004
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1005
	dbuf_add_ref(db, (void *)(uintptr_t)tx->tx_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1006
	db->db_dirtycnt += 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1007
	ASSERT3U(db->db_dirtycnt, <=, 3);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1008
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1009
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1010
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1011
	if (db->db_blkid == DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1012
		dnode_setdirty(dn, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1013
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1014
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1015
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1016
	if (db->db_level == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1017
		dnode_new_blkid(dn, db->db_blkid, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1018
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1019
	if (!RW_WRITE_HELD(&dn->dn_struct_rwlock)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1020
		rw_enter(&dn->dn_struct_rwlock, RW_READER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1021
		drop_struct_lock = TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1022
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1023
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1024
	if (db->db_level < dn->dn_nlevels-1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1025
		int epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1026
		dmu_buf_impl_t *parent;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1027
		parent = dbuf_hold_level(dn, db->db_level+1,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1028
		    db->db_blkid >> epbs, FTAG);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1029
		if (drop_struct_lock)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1030
			rw_exit(&dn->dn_struct_rwlock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1031
		dbuf_dirty(parent, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1032
		dbuf_remove_ref(parent, FTAG);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1033
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1034
		if (drop_struct_lock)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1035
			rw_exit(&dn->dn_struct_rwlock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1036
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1037
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1038
	dnode_setdirty(dn, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1039
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1040
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1041
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1042
dbuf_undirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1043
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1044
	dnode_t *dn = db->db_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1045
	int txgoff = tx->tx_txg & TXG_MASK;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1046
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1047
	ASSERT(tx->tx_txg != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1048
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1049
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1050
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1051
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1052
	 * If this buffer is not dirty, we're done.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1053
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1054
	if (!list_link_active(&db->db_dirty_node[txgoff])) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1055
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1056
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1057
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1058
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1059
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1060
	 * If this buffer is currently held, we cannot undirty
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1061
	 * it, since one of the current holders may be in the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1062
	 * middle of an update.  Note that users of dbuf_undirty()
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1063
	 * should not place a hold on the dbuf before the call.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1064
	 * XXX - this check assumes we are being called from
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1065
	 * dbuf_free_range(), perhaps we should move it there?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1066
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1067
	if (refcount_count(&db->db_holds) > db->db_dirtycnt) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1068
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1069
		mutex_enter(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1070
		dnode_clear_range(dn, db->db_blkid, 1, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1071
		mutex_exit(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1072
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1073
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1074
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1075
	dprintf_dbuf(db, "size=%llx\n", (u_longlong_t)db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1076
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1077
	dbuf_unoverride(db, tx->tx_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1078
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1079
	ASSERT(db->db.db_size != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1080
	if (db->db_level == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1081
		ASSERT(db->db_buf != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1082
		ASSERT(db->db_d.db_data_old[txgoff] != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1083
		if (db->db_d.db_data_old[txgoff] != db->db_buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1084
			arc_buf_free(db->db_d.db_data_old[txgoff], db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1085
		db->db_d.db_data_old[txgoff] = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1086
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1087
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1088
	/* XXX would be nice to fix up dn_towrite_space[] */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1089
	/* XXX undo db_dirtied? but how? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1090
	/* db->db_dirtied = tx->tx_txg; */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1091
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1092
	mutex_enter(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1093
	list_remove(&dn->dn_dirty_dbufs[txgoff], db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1094
	mutex_exit(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1095
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1096
	ASSERT(db->db_dirtycnt > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1097
	db->db_dirtycnt -= 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1098
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1099
	if (refcount_remove(&db->db_holds,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1100
	    (void *)(uintptr_t)tx->tx_txg) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1101
		/* make duf_verify() happy */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1102
		if (db->db.db_data)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1103
			bzero(db->db.db_data, db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1104
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1105
		dbuf_evict(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1106
		return (1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1107
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1108
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1109
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1110
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1111
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1112
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1113
#pragma weak dmu_buf_will_dirty = dbuf_will_dirty
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1114
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1115
dbuf_will_dirty(dmu_buf_impl_t *db, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1116
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1117
	int rf = DB_RF_MUST_SUCCEED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1118
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1119
	ASSERT(tx->tx_txg != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1120
	ASSERT(!refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1121
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1122
	if (RW_WRITE_HELD(&db->db_dnode->dn_struct_rwlock))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1123
		rf |= DB_RF_HAVESTRUCT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1124
	(void) dbuf_read_generic(db, rf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1125
	dbuf_dirty(db, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1126
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1127
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1128
#pragma weak dmu_buf_will_fill = dbuf_will_fill
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1129
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1130
dbuf_will_fill(dmu_buf_impl_t *db, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1131
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1132
	ASSERT(tx->tx_txg != 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1133
	ASSERT(db->db_level == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1134
	ASSERT(!refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1135
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1136
	ASSERT(!(db->db.db_object & DMU_PRIVATE_OBJECT) ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1137
	    dmu_tx_private_ok(tx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1138
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1139
	dbuf_noread(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1140
	dbuf_dirty(db, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1141
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1142
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1143
#pragma weak dmu_buf_fill_done = dbuf_fill_done
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1144
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1145
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1146
dbuf_fill_done(dmu_buf_impl_t *db, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1147
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1148
	mutex_enter(&db->db_mtx);
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
  1149
	DBUF_VERIFY(db);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1150
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1151
	if (db->db_state == DB_FILL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1152
		if (db->db_level == 0 && db->db_d.db_freed_in_flight) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1153
			/* we were freed while filling */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1154
			/* XXX dbuf_undirty? */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1155
			bzero(db->db.db_data, db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1156
			db->db_d.db_freed_in_flight = FALSE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1157
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1158
		db->db_state = DB_CACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1159
		cv_broadcast(&db->db_changed);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1160
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1161
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1162
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1163
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1164
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1165
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1166
dbuf_clear(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1167
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1168
	dnode_t *dn = db->db_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1169
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1170
	ASSERT(MUTEX_HELD(&dn->dn_dbufs_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1171
	ASSERT(MUTEX_HELD(&db->db_mtx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1172
	ASSERT(refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1173
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1174
	if (db->db_state == DB_CACHED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1175
		ASSERT(db->db_buf != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1176
		arc_buf_free(db->db_buf, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1177
		db->db.db_data = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1178
		db->db_buf = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1179
		db->db_state = DB_UNCACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1180
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1181
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1182
	ASSERT3U(db->db_state, ==, DB_UNCACHED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1183
	ASSERT(db->db_buf == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1184
	ASSERT(db->db_data_pending == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1185
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1186
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1187
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1188
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1189
	 * If this dbuf is referened from an indirect dbuf,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1190
	 * decrement the ref count on the indirect dbuf.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1191
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1192
	if (db->db_parent && db->db_parent != dn->dn_dbuf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1193
		dbuf_remove_ref(db->db_parent, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1194
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1195
	/* remove from dn_dbufs */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1196
	list_remove(&dn->dn_dbufs, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1197
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1198
	dnode_rele(dn, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1199
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1200
	dbuf_hash_remove(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1201
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1202
	db->db_dnode = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1203
	db->db_parent = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1204
	db->db_blkptr = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1205
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1206
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1207
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1208
dbuf_findbp(dnode_t *dn, int level, uint64_t blkid, int fail_sparse,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1209
    dmu_buf_impl_t **parentp, blkptr_t **bpp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1210
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1211
	int nlevels, epbs;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1212
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1213
	if (dn->dn_phys->dn_nlevels == 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1214
		nlevels = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1215
	else
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1216
		nlevels = dn->dn_phys->dn_nlevels;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1217
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1218
	epbs = dn->dn_indblkshift - SPA_BLKPTRSHIFT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1219
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1220
	ASSERT3U(level * epbs, <, 64);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1221
	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1222
	if (blkid == DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1223
		/* this is the bonus buffer */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1224
		*parentp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1225
		*bpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1226
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1227
	} else if (level >= nlevels ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1228
	    (blkid > (dn->dn_phys->dn_maxblkid >> (level * epbs)))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1229
		/* the buffer has no parent yet */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1230
		*parentp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1231
		*bpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1232
		return (ENOENT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1233
	} else if (level < nlevels-1) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1234
		/* this block is referenced from an indirect block */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1235
		int err = dbuf_hold_impl(dn, level+1,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1236
		    blkid >> epbs, fail_sparse, NULL, parentp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1237
		if (err)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1238
			return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1239
		dbuf_read_havestruct(*parentp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1240
		*bpp = ((blkptr_t *)(*parentp)->db.db_data) +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1241
		    (blkid & ((1ULL << epbs) - 1));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1242
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1243
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1244
		/* the block is referenced from the dnode */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1245
		ASSERT3U(level, ==, nlevels-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1246
		ASSERT(dn->dn_phys->dn_nblkptr == 0 ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1247
		    blkid < dn->dn_phys->dn_nblkptr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1248
		*parentp = dn->dn_dbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1249
		*bpp = &dn->dn_phys->dn_blkptr[blkid];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1250
		return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1251
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1252
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1253
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1254
static dmu_buf_impl_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1255
dbuf_create(dnode_t *dn, uint8_t level, uint64_t blkid,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1256
    dmu_buf_impl_t *parent, blkptr_t *blkptr)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1257
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1258
	objset_impl_t *os = dn->dn_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1259
	dmu_buf_impl_t *db, *odb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1260
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1261
	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1262
	ASSERT(dn->dn_type != DMU_OT_NONE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1263
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1264
	db = kmem_cache_alloc(dbuf_cache, KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1265
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1266
	db->db_objset = os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1267
	db->db.db_object = dn->dn_object;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1268
	db->db_level = level;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1269
	db->db_blkid = blkid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1270
	db->db_state = DB_UNCACHED;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1271
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1272
	if (db->db_blkid == DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1273
		db->db.db_size = dn->dn_bonuslen;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1274
		db->db.db_offset = DB_BONUS_BLKID;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1275
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1276
		int blocksize =
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1277
		    db->db_level ? 1<<dn->dn_indblkshift :  dn->dn_datablksz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1278
		db->db.db_size = blocksize;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1279
		db->db.db_offset = db->db_blkid * blocksize;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1280
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1281
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1282
	db->db_dirtied = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1283
	db->db_dirtycnt = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1284
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1285
	bzero(&db->db_d, sizeof (db->db_d));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1286
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1287
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1288
	 * Hold the dn_dbufs_mtx while we get the new dbuf
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1289
	 * in the hash table *and* added to the dbufs list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1290
	 * This prevents a possible deadlock with someone
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1291
	 * trying to look up this dbuf before its added to the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1292
	 * dn_dbufs list.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1293
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1294
	mutex_enter(&dn->dn_dbufs_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1295
	if ((odb = dbuf_hash_insert(db)) != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1296
		/* someone else inserted it first */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1297
		kmem_cache_free(dbuf_cache, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1298
		mutex_exit(&dn->dn_dbufs_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1299
		return (odb);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1300
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1301
	list_insert_head(&dn->dn_dbufs, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1302
	mutex_exit(&dn->dn_dbufs_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1303
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1304
	if (parent && parent != dn->dn_dbuf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1305
		dbuf_add_ref(parent, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1306
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1307
	(void) refcount_add(&dn->dn_holds, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1308
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1309
	db->db_dnode = dn;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1310
	db->db_parent = parent;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1311
	db->db_blkptr = blkptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1312
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1313
	dprintf_dbuf(db, "db=%p\n", db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1314
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1315
	return (db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1316
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1317
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1318
static int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1319
dbuf_evictable(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1320
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1321
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1322
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1323
	ASSERT(MUTEX_HELD(&db->db_mtx));
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
  1324
	DBUF_VERIFY(db);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1325
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1326
	if (db->db_state != DB_UNCACHED && db->db_state != DB_CACHED)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1327
		return (FALSE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1328
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1329
	if (!refcount_is_zero(&db->db_holds))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1330
		return (FALSE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1331
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1332
#ifdef ZFS_DEBUG
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1333
	for (i = 0; i < TXG_SIZE; i++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1334
		ASSERT(!list_link_active(&db->db_dirty_node[i]));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1335
		ASSERT(db->db_level != 0 || db->db_d.db_data_old[i] == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1336
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1337
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1338
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1339
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1340
	 * Now we know we want to free it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1341
	 * This call must be done last, since it has side effects -
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1342
	 * calling the db_evict_func().
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1343
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1344
	dbuf_evict_user(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1345
	return (TRUE);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1346
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1347
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1348
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1349
dbuf_destroy(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1350
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1351
	ASSERT(refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1352
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1353
	ASSERT(db->db.db_data == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1354
	ASSERT(db->db_dnode == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1355
	ASSERT(db->db_parent == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1356
	ASSERT(db->db_hash_next == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1357
	ASSERT(db->db_blkptr == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1358
	ASSERT(db->db_data_pending == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1359
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1360
	kmem_cache_free(dbuf_cache, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1361
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1362
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1363
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1364
dbuf_prefetch(dnode_t *dn, uint64_t blkid)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1365
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1366
	dmu_buf_impl_t *db, *parent = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1367
	blkptr_t *bp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1368
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1369
	ASSERT(blkid != DB_BONUS_BLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1370
	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1371
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1372
	if (dnode_block_freed(dn, blkid))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1373
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1374
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1375
	/* dbuf_find() returns with db_mtx held */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1376
	if (db = dbuf_find(dn, 0, blkid)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1377
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1378
		 * This dbuf is already in the cache.  We assume that
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1379
		 * it is already CACHED, or else about to be either
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1380
		 * read or filled.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1381
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1382
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1383
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1384
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1385
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1386
	if (dbuf_findbp(dn, 0, blkid, TRUE, &parent, &bp) == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1387
		if (bp && !BP_IS_HOLE(bp)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1388
			(void) arc_read(NULL, dn->dn_objset->os_spa, bp,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1389
			    dmu_ot[dn->dn_type].ot_byteswap,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1390
			    NULL, NULL, ZIO_PRIORITY_ASYNC_READ,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1391
			    ZIO_FLAG_CANFAIL | ZIO_FLAG_SPECULATIVE,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1392
			    (ARC_NOWAIT | ARC_PREFETCH));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1393
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1394
		if (parent && parent != dn->dn_dbuf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1395
			dbuf_rele(parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1396
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1397
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1398
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1399
/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1400
 * Returns with db_holds incremented, and db_mtx not held.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1401
 * Note: dn_struct_rwlock must be held.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1402
 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1403
int
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1404
dbuf_hold_impl(dnode_t *dn, uint8_t level, uint64_t blkid, int fail_sparse,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1405
    void *tag, dmu_buf_impl_t **dbp)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1406
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1407
	dmu_buf_impl_t *db, *parent = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1408
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1409
	ASSERT(RW_LOCK_HELD(&dn->dn_struct_rwlock));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1410
	ASSERT3U(dn->dn_nlevels, >, level);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1411
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1412
	*dbp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1413
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1414
	/* dbuf_find() returns with db_mtx held */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1415
	db = dbuf_find(dn, level, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1416
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1417
	if (db == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1418
		blkptr_t *bp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1419
		int err;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1420
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1421
		err = dbuf_findbp(dn, level, blkid, fail_sparse, &parent, &bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1422
		if (fail_sparse) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1423
			if (err == 0 && bp && BP_IS_HOLE(bp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1424
				err = ENOENT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1425
			if (err) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1426
				if (parent && parent != dn->dn_dbuf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1427
					dbuf_rele(parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1428
				return (err);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1429
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1430
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1431
		db = dbuf_create(dn, level, blkid, parent, bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1432
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1433
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1434
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1435
	 * If this buffer is currently syncing out, and we are
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1436
	 * are still referencing it from db_data, we need to make
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1437
	 * a copy of it in case we decide we want to dirty it
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1438
	 * again in this txg.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1439
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1440
	if (db->db_level == 0 && db->db_state == DB_CACHED &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1441
	    !(dn->dn_object & DMU_PRIVATE_OBJECT) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1442
	    db->db_data_pending == db->db_buf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1443
		int size = (db->db_blkid == DB_BONUS_BLKID) ?
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1444
		    DN_MAX_BONUSLEN : db->db.db_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1445
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1446
		dbuf_set_data(db, arc_buf_alloc(db->db_dnode->dn_objset->os_spa,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1447
		    size, db));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1448
		bcopy(db->db_data_pending->b_data, db->db.db_data,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1449
		    db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1450
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1451
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1452
	dbuf_add_ref(db, tag);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1453
	dbuf_update_data(db);
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
  1454
	DBUF_VERIFY(db);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1455
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1456
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1457
	/* NOTE: we can't rele the parent until after we drop the db_mtx */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1458
	if (parent && parent != dn->dn_dbuf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1459
		dbuf_rele(parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1460
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1461
	ASSERT3P(db->db_dnode, ==, dn);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1462
	ASSERT3U(db->db_blkid, ==, blkid);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1463
	ASSERT3U(db->db_level, ==, level);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1464
	*dbp = db;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1465
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1466
	return (0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1467
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1468
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1469
dmu_buf_impl_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1470
dbuf_hold(dnode_t *dn, uint64_t blkid)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1471
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1472
	dmu_buf_impl_t *db;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1473
	(void) dbuf_hold_impl(dn, 0, blkid, FALSE, NULL, &db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1474
	return (db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1475
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1476
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1477
dmu_buf_impl_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1478
dbuf_hold_level(dnode_t *dn, int level, uint64_t blkid, void *tag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1479
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1480
	dmu_buf_impl_t *db;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1481
	(void) dbuf_hold_impl(dn, level, blkid, FALSE, tag, &db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1482
	return (db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1483
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1484
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1485
dmu_buf_impl_t *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1486
dbuf_hold_bonus(dnode_t *dn, void *tag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1487
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1488
	dmu_buf_impl_t *db;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1489
	rw_enter(&dn->dn_struct_rwlock, RW_READER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1490
	(void) dbuf_hold_impl(dn, 0, DB_BONUS_BLKID, FALSE, tag, &db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1491
	rw_exit(&dn->dn_struct_rwlock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1492
	return (db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1493
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1494
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1495
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1496
dbuf_add_ref(dmu_buf_impl_t *db, void *tag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1497
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1498
	(void) refcount_add(&db->db_holds, tag);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1499
	/* dprintf_dbuf(db, "adding ref %p; holds up to %lld\n", tag, holds); */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1500
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1501
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1502
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1503
dbuf_remove_ref(dmu_buf_impl_t *db, void *tag)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1504
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1505
	int64_t holds;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1506
	dnode_t *dn = db->db_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1507
	int need_mutex;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1508
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1509
	ASSERT(dn != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1510
	need_mutex = !MUTEX_HELD(&dn->dn_dbufs_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1511
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1512
	if (need_mutex) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1513
		dnode_add_ref(dn, FTAG);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1514
		mutex_enter(&dn->dn_dbufs_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1515
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1516
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1517
	mutex_enter(&db->db_mtx);
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
  1518
	DBUF_VERIFY(db);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1519
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1520
	holds = refcount_remove(&db->db_holds, tag);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1521
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1522
	if (holds == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1523
		ASSERT3U(db->db_state, !=, DB_FILL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1524
		if (db->db_level == 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1525
		    db->db_d.db_user_data_ptr_ptr != NULL)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1526
			*db->db_d.db_user_data_ptr_ptr = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1527
		dbuf_evict(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1528
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1529
		if (holds == db->db_dirtycnt &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1530
		    db->db_level == 0 && db->db_d.db_immediate_evict)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1531
			dbuf_evict_user(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1532
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1533
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1534
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1535
	if (need_mutex) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1536
		mutex_exit(&dn->dn_dbufs_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1537
		dnode_rele(dn, FTAG);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1538
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1539
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1540
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1541
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1542
dbuf_rele(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1543
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1544
	dbuf_remove_ref(db, NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1545
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1546
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1547
#pragma weak dmu_buf_refcount = dbuf_refcount
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1548
uint64_t
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1549
dbuf_refcount(dmu_buf_impl_t *db)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1550
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1551
	return (refcount_count(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1552
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1553
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1554
void *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1555
dmu_buf_set_user(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1556
    dmu_buf_evict_func_t *evict_func)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1557
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1558
	return (dmu_buf_update_user(db_fake, NULL, user_ptr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1559
	    user_data_ptr_ptr, evict_func));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1560
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1561
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1562
void *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1563
dmu_buf_set_user_ie(dmu_buf_t *db_fake, void *user_ptr, void *user_data_ptr_ptr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1564
    dmu_buf_evict_func_t *evict_func)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1565
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1566
	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1567
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1568
	db->db_d.db_immediate_evict = TRUE;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1569
	return (dmu_buf_update_user(db_fake, NULL, user_ptr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1570
	    user_data_ptr_ptr, evict_func));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1571
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1572
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1573
void *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1574
dmu_buf_update_user(dmu_buf_t *db_fake, void *old_user_ptr, void *user_ptr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1575
    void *user_data_ptr_ptr, dmu_buf_evict_func_t *evict_func)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1576
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1577
	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1578
	ASSERT(db->db_level == 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1579
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1580
	ASSERT((user_ptr == NULL) == (evict_func == NULL));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1581
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1582
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1583
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1584
	if (db->db_d.db_user_ptr == old_user_ptr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1585
		db->db_d.db_user_ptr = user_ptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1586
		db->db_d.db_user_data_ptr_ptr = user_data_ptr_ptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1587
		db->db_d.db_evict_func = evict_func;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1588
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1589
		dbuf_update_data(db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1590
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1591
		old_user_ptr = db->db_d.db_user_ptr;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1592
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1593
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1594
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1595
	return (old_user_ptr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1596
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1597
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1598
void *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1599
dmu_buf_get_user(dmu_buf_t *db_fake)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1600
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1601
	dmu_buf_impl_t *db = (dmu_buf_impl_t *)db_fake;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1602
	ASSERT(!refcount_is_zero(&db->db_holds));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1603
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1604
	return (db->db_d.db_user_ptr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1605
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1606
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1607
void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1608
dbuf_sync(dmu_buf_impl_t *db, zio_t *zio, dmu_tx_t *tx)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1609
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1610
	arc_buf_t **data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1611
	uint64_t txg = tx->tx_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1612
	dnode_t *dn = db->db_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1613
	objset_impl_t *os = dn->dn_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1614
	int blksz;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1615
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1616
	ASSERT(dmu_tx_is_syncing(tx));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1617
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1618
	dprintf_dbuf_bp(db, db->db_blkptr, "blkptr=%p", db->db_blkptr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1619
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1620
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1621
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1622
	 * To be synced, we must be dirtied.  But we
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1623
	 * might have been freed after the dirty.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1624
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1625
	if (db->db_state == DB_UNCACHED) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1626
		/* This buffer has been freed since it was dirtied */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1627
		ASSERT(db->db.db_data == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1628
	} else if (db->db_state == DB_FILL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1629
		/* This buffer was freed and is now being re-filled */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1630
		ASSERT(db->db.db_data != db->db_d.db_data_old[txg&TXG_MASK]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1631
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1632
		ASSERT3U(db->db_state, ==, DB_CACHED);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1633
	}
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
  1634
	DBUF_VERIFY(db);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1635
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1636
	/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1637
	 * Don't need a lock on db_dirty (dn_mtx), because it can't
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1638
	 * be modified yet.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1639
	 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1640
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1641
	if (db->db_level == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1642
		data = &db->db_d.db_data_old[txg&TXG_MASK];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1643
		blksz = arc_buf_size(*data);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1644
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1645
		 * If this buffer is currently "in use" (i.e., there are
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1646
		 * active holds and db_data still references it), then make
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1647
		 * a copy before we start the write so that any modifications
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1648
		 * from the open txg will not leak into this write.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1649
		 *
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1650
		 * NOTE: this copy does not need to be made for objects only
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1651
		 * modified in the syncing context (e.g. DNONE_DNODE blocks)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1652
		 * or if there is no actual write involved (bonus blocks).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1653
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1654
		if (!(dn->dn_object & DMU_PRIVATE_OBJECT) &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1655
		    db->db_d.db_overridden_by[txg&TXG_MASK] == NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1656
		    db->db_blkid != DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1657
			if (refcount_count(&db->db_holds) > 1 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1658
			    *data == db->db_buf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1659
				*data = arc_buf_alloc(
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1660
				    db->db_dnode->dn_objset->os_spa, blksz, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1661
				bcopy(db->db.db_data, (*data)->b_data, blksz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1662
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1663
			db->db_data_pending = *data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1664
		} else if (dn->dn_object & DMU_PRIVATE_OBJECT) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1665
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1666
			 * Private object buffers are released here rather
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1667
			 * than in dbuf_dirty() since they are only modified
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1668
			 * in the syncing context and we don't want the
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1669
			 * overhead of making multiple copies of the data.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1670
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1671
			arc_release(db->db_buf, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1672
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1673
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1674
		data = &db->db_buf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1675
		if (*data == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1676
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1677
			 * This can happen if we dirty and then free
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1678
			 * the level-0 data blocks in the same txg. So
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1679
			 * this indirect remains unchanged.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1680
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1681
			if (db->db_dirtied == txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1682
				db->db_dirtied = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1683
			ASSERT(db->db_dirtycnt > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1684
			db->db_dirtycnt -= 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1685
			mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1686
			dbuf_remove_ref(db, (void *)(uintptr_t)txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1687
			return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1688
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1689
		blksz = db->db.db_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1690
		ASSERT3U(blksz, ==, 1<<dn->dn_phys->dn_indblkshift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1691
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1692
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1693
	ASSERT(*data != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1694
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1695
	if (db->db_blkid == DB_BONUS_BLKID) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1696
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1697
		 * Simply copy the bonus data into the dnode.  It will
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1698
		 * be written out when the dnode is synced (and it will
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1699
		 * be synced, since it must have been dirty for dbuf_sync
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1700
		 * to be called).  The bonus data will be byte swapped
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1701
		 * in dnode_byteswap.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1702
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1703
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1704
		 * Use dn_phys->dn_bonuslen since db.db_size is the length
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1705
		 * of the bonus buffer in the open transaction rather than
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1706
		 * the syncing transaction.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1707
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1708
		ASSERT3U(db->db_level, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1709
		ASSERT3U(dn->dn_phys->dn_bonuslen, <=, blksz);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1710
		bcopy((*data)->b_data, DN_BONUS(dn->dn_phys),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1711
		    dn->dn_phys->dn_bonuslen);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1712
		if (*data != db->db_buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1713
			arc_buf_free(*data, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1714
		db->db_d.db_data_old[txg&TXG_MASK] = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1715
		db->db_data_pending = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1716
		if (db->db_dirtied == txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1717
			db->db_dirtied = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1718
		ASSERT(db->db_dirtycnt > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1719
		db->db_dirtycnt -= 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1720
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1721
		dbuf_remove_ref(db, (void *)(uintptr_t)txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1722
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1723
	} else if (db->db_level > 0 && !arc_released(db->db_buf)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1724
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1725
		 * This indirect buffer was marked dirty, but
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1726
		 * never modified (if it had been modified, then
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1727
		 * we would have released the buffer).  There is
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1728
		 * no reason to write anything.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1729
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1730
		db->db_data_pending = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1731
		if (db->db_dirtied == txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1732
			db->db_dirtied = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1733
		ASSERT(db->db_dirtycnt > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1734
		db->db_dirtycnt -= 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1735
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1736
		dbuf_remove_ref(db, (void *)(uintptr_t)txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1737
		return;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1738
	} else if (db->db_blkptr == NULL &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1739
	    db->db_level == dn->dn_phys->dn_nlevels-1 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1740
	    db->db_blkid < dn->dn_phys->dn_nblkptr) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1741
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1742
		 * This buffer was allocated at a time when there was
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1743
		 * no available blkptrs from the dnode, or it was
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1744
		 * inappropriate to hook it in (i.e., nlevels mis-match).
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1745
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1746
		ASSERT(db->db_blkptr == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1747
		ASSERT(db->db_parent == NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1748
		db->db_parent = dn->dn_dbuf;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1749
		db->db_blkptr = &dn->dn_phys->dn_blkptr[db->db_blkid];
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
  1750
		DBUF_VERIFY(db);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1751
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1752
	} else if (db->db_blkptr == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1753
		dmu_buf_impl_t *parent = db->db_parent;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1754
		int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1755
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1756
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1757
		ASSERT(dn->dn_phys->dn_nlevels > 1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1758
		if (parent == NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1759
			rw_enter(&dn->dn_struct_rwlock, RW_READER);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1760
			(void) dbuf_hold_impl(dn, db->db_level+1,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1761
			    db->db_blkid >> epbs, FALSE, NULL, &parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1762
			rw_exit(&dn->dn_struct_rwlock);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1763
			dbuf_add_ref(parent, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1764
			db->db_parent = parent;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1765
			dbuf_rele(parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1766
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1767
		dbuf_read(parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1768
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1769
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1770
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1771
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1772
	ASSERT(IS_DNODE_DNODE(dn->dn_object) || db->db_parent != NULL);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1773
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1774
	if (db->db_parent != dn->dn_dbuf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1775
		dmu_buf_impl_t *parent = db->db_parent;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1776
		int epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1777
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1778
		mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1779
		ASSERT(db->db_level == parent->db_level-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1780
		ASSERT(list_link_active(&parent->db_dirty_node[txg&TXG_MASK]));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1781
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1782
		 * We may have read this block after we dirtied it,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1783
		 * so never released it from the cache.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1784
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1785
		arc_release(parent->db_buf, parent);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1786
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1787
		db->db_blkptr = (blkptr_t *)parent->db.db_data +
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1788
		    (db->db_blkid & ((1ULL << epbs) - 1));
873
adefbfa5f42d 6347448 non ZFS_DEBUG kernels shouldn't call empty verify functions
ek110237
parents: 789
diff changeset
  1789
		DBUF_VERIFY(db);
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1790
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1791
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1792
	ASSERT(db->db_parent == NULL || arc_released(db->db_parent->db_buf));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1793
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1794
#ifdef ZFS_DEBUG
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1795
	if (db->db_parent == dn->dn_dbuf) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1796
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1797
		 * We don't need to dnode_setdirty(dn) because if we got
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1798
		 * here then the parent is already dirty.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1799
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1800
		ASSERT(db->db_level == dn->dn_phys->dn_nlevels-1);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1801
		ASSERT3P(db->db_blkptr, ==,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1802
		    &dn->dn_phys->dn_blkptr[db->db_blkid]);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1803
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1804
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1805
	if (db->db_level == 0 &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1806
	    db->db_d.db_overridden_by[txg&TXG_MASK] != NULL) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1807
		arc_buf_t **old = &db->db_d.db_data_old[txg&TXG_MASK];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1808
		blkptr_t **bpp = &db->db_d.db_overridden_by[txg&TXG_MASK];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1809
		int old_size = BP_GET_ASIZE(db->db_blkptr);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1810
		int new_size = BP_GET_ASIZE(*bpp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1811
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1812
		ASSERT(db->db_blkid != DB_BONUS_BLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1813
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1814
		dnode_diduse_space(dn, new_size-old_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1815
		mutex_enter(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1816
		if (db->db_blkid > dn->dn_phys->dn_maxblkid)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1817
			dn->dn_phys->dn_maxblkid = db->db_blkid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1818
		mutex_exit(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1819
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1820
		dsl_dataset_block_born(os->os_dsl_dataset, *bpp, tx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1821
		if (!BP_IS_HOLE(db->db_blkptr))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1822
			dsl_dataset_block_kill(os->os_dsl_dataset,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1823
			    db->db_blkptr, os->os_synctx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1824
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1825
		mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1826
		*db->db_blkptr = **bpp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1827
		kmem_free(*bpp, sizeof (blkptr_t));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1828
		*bpp = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1829
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1830
		if (*old != db->db_buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1831
			arc_buf_free(*old, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1832
		*old = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1833
		db->db_data_pending = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1834
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1835
		cv_broadcast(&db->db_changed);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1836
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1837
		ASSERT(db->db_dirtycnt > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1838
		db->db_dirtycnt -= 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1839
		mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1840
		dbuf_remove_ref(db, (void *)(uintptr_t)txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1841
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1842
		int checksum, compress;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1843
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1844
		if (db->db_level > 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1845
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1846
			 * XXX -- we should design a compression algorithm
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1847
			 * that specializes in arrays of bps.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1848
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1849
			checksum = ZIO_CHECKSUM_FLETCHER_4;
982
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 873
diff changeset
  1850
			/* XXX - disable compresssion for now */
9bc5c1db9740 6345547 assertion failed: tempreserve < arc.c/4 from zfs_rename
maybee
parents: 873
diff changeset
  1851
			compress = ZIO_COMPRESS_OFF;
789
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1852
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1853
			/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1854
			 * Allow dnode settings to override objset settings,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1855
			 * except for metadata checksums.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1856
			 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1857
			if (dmu_ot[dn->dn_type].ot_metadata) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1858
				checksum = os->os_md_checksum;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1859
				compress = zio_compress_select(dn->dn_compress,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1860
				    os->os_md_compress);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1861
			} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1862
				checksum = zio_checksum_select(dn->dn_checksum,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1863
				    os->os_checksum);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1864
				compress = zio_compress_select(dn->dn_compress,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1865
				    os->os_compress);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1866
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1867
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1868
#ifdef ZFS_DEBUG
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1869
		if (db->db_parent) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1870
			ASSERT(list_link_active(
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1871
			    &db->db_parent->db_dirty_node[txg&TXG_MASK]));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1872
			ASSERT(db->db_parent == dn->dn_dbuf ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1873
			    db->db_parent->db_level > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1874
			if (dn->dn_object & DMU_PRIVATE_OBJECT ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1875
			    db->db_level > 0)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1876
				ASSERT(*data == db->db_buf);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1877
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1878
#endif
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1879
		ASSERT3U(db->db_blkptr->blk_birth, <=, tx->tx_txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1880
		(void) arc_write(zio, os->os_spa, checksum, compress, txg,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1881
		    db->db_blkptr, *data, dbuf_write_done, db,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1882
		    ZIO_PRIORITY_ASYNC_WRITE, ZIO_FLAG_MUSTSUCCEED, ARC_NOWAIT);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1883
		/*
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1884
		 * We can't access db after arc_write, since it could finish
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1885
		 * and be freed, and we have no locks on it.
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1886
		 */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1887
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1888
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1889
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1890
struct dbuf_arg {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1891
	objset_impl_t *os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1892
	blkptr_t bp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1893
};
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1894
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1895
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1896
dbuf_do_born(void *arg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1897
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1898
	struct dbuf_arg *da = arg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1899
	dsl_dataset_block_born(da->os->os_dsl_dataset,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1900
	    &da->bp, da->os->os_synctx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1901
	kmem_free(da, sizeof (struct dbuf_arg));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1902
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1903
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1904
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1905
dbuf_do_kill(void *arg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1906
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1907
	struct dbuf_arg *da = arg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1908
	dsl_dataset_block_kill(da->os->os_dsl_dataset,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1909
	    &da->bp, da->os->os_synctx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1910
	kmem_free(da, sizeof (struct dbuf_arg));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1911
}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1912
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1913
/* ARGSUSED */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1914
static void
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1915
dbuf_write_done(zio_t *zio, arc_buf_t *buf, void *vdb)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1916
{
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1917
	dmu_buf_impl_t *db = vdb;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1918
	dnode_t *dn = db->db_dnode;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1919
	objset_impl_t *os = dn->dn_objset;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1920
	uint64_t txg = zio->io_txg;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1921
	uint64_t fill = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1922
	int i;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1923
	int old_size, new_size;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1924
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1925
	ASSERT3U(zio->io_error, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1926
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1927
	dprintf_dbuf_bp(db, &zio->io_bp_orig, "bp_orig: %s", "");
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1928
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1929
	old_size = BP_GET_ASIZE(&zio->io_bp_orig);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1930
	new_size = BP_GET_ASIZE(zio->io_bp);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1931
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1932
	dnode_diduse_space(dn, new_size-old_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1933
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1934
	mutex_enter(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1935
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1936
	if (db->db_dirtied == txg)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1937
		db->db_dirtied = 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1938
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1939
	if (db->db_level == 0) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1940
		arc_buf_t **old = &db->db_d.db_data_old[txg&TXG_MASK];
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1941
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1942
		ASSERT(db->db_blkid != DB_BONUS_BLKID);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1943
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1944
		if (*old != db->db_buf)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1945
			arc_buf_free(*old, db);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1946
		*old = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1947
		db->db_data_pending = NULL;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1948
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1949
		mutex_enter(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1950
		if (db->db_blkid > dn->dn_phys->dn_maxblkid &&
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1951
		    !BP_IS_HOLE(db->db_blkptr))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1952
			dn->dn_phys->dn_maxblkid = db->db_blkid;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1953
		mutex_exit(&dn->dn_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1954
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1955
		if (dn->dn_type == DMU_OT_DNODE) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1956
			dnode_phys_t *dnp = db->db.db_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1957
			for (i = db->db.db_size >> DNODE_SHIFT; i > 0;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1958
			    i--, dnp++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1959
				if (dnp->dn_type != DMU_OT_NONE)
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1960
					fill++;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1961
			}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1962
		} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1963
			if (!BP_IS_HOLE(db->db_blkptr))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1964
				fill = 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1965
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1966
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1967
		blkptr_t *bp = db->db.db_data;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1968
		ASSERT3U(db->db.db_size, ==, 1<<dn->dn_phys->dn_indblkshift);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1969
		if (!BP_IS_HOLE(db->db_blkptr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1970
			ASSERT3U(BP_GET_LSIZE(zio->io_bp), ==, db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1971
			ASSERT3U(BP_GET_LSIZE(db->db_blkptr), ==,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1972
			    db->db.db_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1973
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1974
		for (i = db->db.db_size >> SPA_BLKPTRSHIFT; i > 0; i--, bp++) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1975
			if (BP_IS_HOLE(bp))
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1976
				continue;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1977
			ASSERT3U(BP_GET_LSIZE(bp), ==,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1978
			    db->db_level == 1 ? dn->dn_datablksz :
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1979
			    (1<<dn->dn_phys->dn_indblkshift));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1980
			fill += bp->blk_fill;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1981
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1982
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1983
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1984
	if (!BP_IS_HOLE(db->db_blkptr)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1985
		db->db_blkptr->blk_fill = fill;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1986
		BP_SET_TYPE(db->db_blkptr, dn->dn_type);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1987
		BP_SET_LEVEL(db->db_blkptr, db->db_level);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1988
	} else {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1989
		ASSERT3U(fill, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1990
		ASSERT3U(db->db_blkptr->blk_fill, ==, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1991
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1992
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1993
	dprintf_dbuf_bp(db, db->db_blkptr,
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1994
	    "wrote %llu bytes to blkptr:", zio->io_size);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1995
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1996
	ASSERT(db->db_parent == NULL ||
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1997
	    list_link_active(&db->db_parent->db_dirty_node[txg&TXG_MASK]));
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1998
	cv_broadcast(&db->db_changed);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  1999
	ASSERT(db->db_dirtycnt > 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2000
	db->db_dirtycnt -= 1;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2001
	mutex_exit(&db->db_mtx);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2002
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2003
	/* We must do this after we've set the bp's type and level */
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2004
	if (!DVA_EQUAL(BP_IDENTITY(zio->io_bp),
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2005
	    BP_IDENTITY(&zio->io_bp_orig))) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2006
		struct dbuf_arg *da;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2007
		da = kmem_alloc(sizeof (struct dbuf_arg), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2008
		da->os = os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2009
		da->bp = *zio->io_bp;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2010
		(void) taskq_dispatch(dbuf_tq, dbuf_do_born, da, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2011
		if (!BP_IS_HOLE(&zio->io_bp_orig)) {
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2012
			da = kmem_alloc(sizeof (struct dbuf_arg), KM_SLEEP);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2013
			da->os = os;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2014
			da->bp = zio->io_bp_orig;
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2015
			(void) taskq_dispatch(dbuf_tq, dbuf_do_kill, da, 0);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2016
		}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2017
	}
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2018
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2019
	dbuf_remove_ref(db, (void *)(uintptr_t)txg);
b348f31ed315 PSARC 2002/240 ZFS
ahrens
parents:
diff changeset
  2020
}