| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | /*
 | 
					
						
							|  |  |  |  * CDDL HEADER START | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * The contents of this file are subject to the terms of the | 
					
						
							|  |  |  |  * Common Development and Distribution License (the "License"). | 
					
						
							|  |  |  |  * You may not use this file except in compliance with the License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE | 
					
						
							|  |  |  |  * or http://www.opensolaris.org/os/licensing.
 | 
					
						
							|  |  |  |  * See the License for the specific language governing permissions | 
					
						
							|  |  |  |  * and limitations under the License. | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * When distributing Covered Code, include this CDDL HEADER in each | 
					
						
							|  |  |  |  * file and include the License file at usr/src/OPENSOLARIS.LICENSE. | 
					
						
							|  |  |  |  * If applicable, add the following below this CDDL HEADER, with the | 
					
						
							|  |  |  |  * fields enclosed by brackets "[]" replaced with your own identifying | 
					
						
							|  |  |  |  * information: Portions Copyright [yyyy] [name of copyright owner] | 
					
						
							|  |  |  |  * | 
					
						
							|  |  |  |  * CDDL HEADER END | 
					
						
							|  |  |  |  */ | 
					
						
							| 
									
										
										
										
											2012-12-14 03:24:15 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  |  * Copyright (c) 2005, 2010, Oracle and/or its affiliates. All rights reserved. | 
					
						
							| 
									
										
										
										
											2016-05-15 18:02:28 +03:00
										 |  |  |  * Copyright (c) 2012, 2016 by Delphix. All rights reserved. | 
					
						
							| 
									
										
										
										
											2015-04-02 06:44:32 +03:00
										 |  |  |  * Copyright (c) 2014 Spectra Logic Corporation, All rights reserved. | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  |  */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #include <sys/zfs_context.h>
 | 
					
						
							|  |  |  | #include <sys/dbuf.h>
 | 
					
						
							|  |  |  | #include <sys/dnode.h>
 | 
					
						
							|  |  |  | #include <sys/dmu.h>
 | 
					
						
							|  |  |  | #include <sys/dmu_tx.h>
 | 
					
						
							|  |  |  | #include <sys/dmu_objset.h>
 | 
					
						
							|  |  |  | #include <sys/dsl_dataset.h>
 | 
					
						
							|  |  |  | #include <sys/spa.h>
 | 
					
						
							| 
									
										
										
										
											2014-04-16 07:40:22 +04:00
										 |  |  | #include <sys/range_tree.h>
 | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | #include <sys/zfeature.h>
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | dnode_increase_indirection(dnode_t *dn, dmu_tx_t *tx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	dmu_buf_impl_t *db; | 
					
						
							|  |  |  | 	int txgoff = tx->tx_txg & TXG_MASK; | 
					
						
							|  |  |  | 	int nblkptr = dn->dn_phys->dn_nblkptr; | 
					
						
							|  |  |  | 	int old_toplvl = dn->dn_phys->dn_nlevels - 1; | 
					
						
							|  |  |  | 	int new_level = dn->dn_next_nlevels[txgoff]; | 
					
						
							|  |  |  | 	int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* this dnode can't be paged out because it's dirty */ | 
					
						
							|  |  |  | 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); | 
					
						
							|  |  |  | 	ASSERT(RW_WRITE_HELD(&dn->dn_struct_rwlock)); | 
					
						
							|  |  |  | 	ASSERT(new_level > 1 && dn->dn_phys->dn_nlevels > 0); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	db = dbuf_hold_level(dn, dn->dn_phys->dn_nlevels, 0, FTAG); | 
					
						
							|  |  |  | 	ASSERT(db != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dn->dn_phys->dn_nlevels = new_level; | 
					
						
							|  |  |  | 	dprintf("os=%p obj=%llu, increase to %d\n", dn->dn_objset, | 
					
						
							|  |  |  | 	    dn->dn_object, dn->dn_phys->dn_nlevels); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2016-05-15 18:02:28 +03:00
										 |  |  | 	/* transfer dnode's block pointers to new indirect block */ | 
					
						
							|  |  |  | 	(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED|DB_RF_HAVESTRUCT); | 
					
						
							|  |  |  | 	ASSERT(db->db.db_data); | 
					
						
							|  |  |  | 	ASSERT(arc_released(db->db_buf)); | 
					
						
							|  |  |  | 	ASSERT3U(sizeof (blkptr_t) * nblkptr, <=, db->db.db_size); | 
					
						
							|  |  |  | 	bcopy(dn->dn_phys->dn_blkptr, db->db.db_data, | 
					
						
							|  |  |  | 	    sizeof (blkptr_t) * nblkptr); | 
					
						
							|  |  |  | 	arc_buf_freeze(db->db_buf); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* set dbuf's parent pointers to new indirect buf */ | 
					
						
							|  |  |  | 	for (i = 0; i < nblkptr; i++) { | 
					
						
							| 
									
										
										
										
											2015-04-02 14:59:15 +03:00
										 |  |  | 		dmu_buf_impl_t *child = | 
					
						
							|  |  |  | 		    dbuf_find(dn->dn_objset, dn->dn_object, old_toplvl, i); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (child == NULL) | 
					
						
							|  |  |  | 			continue; | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | #ifdef	DEBUG
 | 
					
						
							|  |  |  | 		DB_DNODE_ENTER(child); | 
					
						
							|  |  |  | 		ASSERT3P(DB_DNODE(child), ==, dn); | 
					
						
							|  |  |  | 		DB_DNODE_EXIT(child); | 
					
						
							|  |  |  | #endif	/* DEBUG */
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		if (child->db_parent && child->db_parent != dn->dn_dbuf) { | 
					
						
							|  |  |  | 			ASSERT(child->db_parent->db_level == db->db_level); | 
					
						
							|  |  |  | 			ASSERT(child->db_blkptr != | 
					
						
							|  |  |  | 			    &dn->dn_phys->dn_blkptr[child->db_blkid]); | 
					
						
							|  |  |  | 			mutex_exit(&child->db_mtx); | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ASSERT(child->db_parent == NULL || | 
					
						
							|  |  |  | 		    child->db_parent == dn->dn_dbuf); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		child->db_parent = db; | 
					
						
							|  |  |  | 		dbuf_add_ref(db, child); | 
					
						
							|  |  |  | 		if (db->db.db_data) | 
					
						
							|  |  |  | 			child->db_blkptr = (blkptr_t *)db->db.db_data + i; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			child->db_blkptr = NULL; | 
					
						
							|  |  |  | 		dprintf_dbuf_bp(child, child->db_blkptr, | 
					
						
							|  |  |  | 		    "changed db_blkptr to new indirect %s", ""); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		mutex_exit(&child->db_mtx); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	bzero(dn->dn_phys->dn_blkptr, sizeof (blkptr_t) * nblkptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dbuf_rele(db, FTAG); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	rw_exit(&dn->dn_struct_rwlock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | static void | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | free_blocks(dnode_t *dn, blkptr_t *bp, int num, dmu_tx_t *tx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 	dsl_dataset_t *ds = dn->dn_objset->os_dsl_dataset; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	uint64_t bytesfreed = 0; | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	int i; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 	dprintf("ds=%p obj=%llx num=%d\n", ds, dn->dn_object, num); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	for (i = 0; i < num; i++, bp++) { | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 		uint64_t lsize, lvl; | 
					
						
							|  |  |  | 		dmu_object_type_t type; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		if (BP_IS_HOLE(bp)) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 		bytesfreed += dsl_dataset_block_kill(ds, bp, tx, B_FALSE); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		ASSERT3U(bytesfreed, <=, DN_USED_BYTES(dn->dn_phys)); | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * Save some useful information on the holes being | 
					
						
							|  |  |  | 		 * punched, including logical size, type, and indirection | 
					
						
							|  |  |  | 		 * level. Retaining birth time enables detection of when | 
					
						
							|  |  |  | 		 * holes are punched for reducing the number of free | 
					
						
							|  |  |  | 		 * records transmitted during a zfs send. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		lsize = BP_GET_LSIZE(bp); | 
					
						
							|  |  |  | 		type = BP_GET_TYPE(bp); | 
					
						
							|  |  |  | 		lvl = BP_GET_LEVEL(bp); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		bzero(bp, sizeof (blkptr_t)); | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 
 | 
					
						
							|  |  |  | 		if (spa_feature_is_active(dn->dn_objset->os_spa, | 
					
						
							|  |  |  | 		    SPA_FEATURE_HOLE_BIRTH)) { | 
					
						
							|  |  |  | 			BP_SET_LSIZE(bp, lsize); | 
					
						
							|  |  |  | 			BP_SET_TYPE(bp, type); | 
					
						
							|  |  |  | 			BP_SET_LEVEL(bp, lvl); | 
					
						
							|  |  |  | 			BP_SET_BIRTH(bp, dmu_tx_get_txg(tx), 0); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	dnode_diduse_space(dn, -bytesfreed); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | #ifdef ZFS_DEBUG
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | free_verify(dmu_buf_impl_t *db, uint64_t start, uint64_t end, dmu_tx_t *tx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int off, num; | 
					
						
							|  |  |  | 	int i, err, epbs; | 
					
						
							|  |  |  | 	uint64_t txg = tx->tx_txg; | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 	dnode_t *dn; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 	DB_DNODE_ENTER(db); | 
					
						
							|  |  |  | 	dn = DB_DNODE(db); | 
					
						
							|  |  |  | 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	off = start - (db->db_blkid * 1<<epbs); | 
					
						
							|  |  |  | 	num = end - start + 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ASSERT3U(off, >=, 0); | 
					
						
							|  |  |  | 	ASSERT3U(num, >=, 0); | 
					
						
							|  |  |  | 	ASSERT3U(db->db_level, >, 0); | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 	ASSERT3U(db->db.db_size, ==, 1 << dn->dn_phys->dn_indblkshift); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	ASSERT3U(off+num, <=, db->db.db_size >> SPA_BLKPTRSHIFT); | 
					
						
							|  |  |  | 	ASSERT(db->db_blkptr != NULL); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	for (i = off; i < off+num; i++) { | 
					
						
							|  |  |  | 		uint64_t *buf; | 
					
						
							|  |  |  | 		dmu_buf_impl_t *child; | 
					
						
							|  |  |  | 		dbuf_dirty_record_t *dr; | 
					
						
							|  |  |  | 		int j; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ASSERT(db->db_level == 1); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 		rw_enter(&dn->dn_struct_rwlock, RW_READER); | 
					
						
							|  |  |  | 		err = dbuf_hold_impl(dn, db->db_level-1, | 
					
						
							| 
									
										
										
										
											2015-12-22 04:31:57 +03:00
										 |  |  | 		    (db->db_blkid << epbs) + i, TRUE, FALSE, FTAG, &child); | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 		rw_exit(&dn->dn_struct_rwlock); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		if (err == ENOENT) | 
					
						
							|  |  |  | 			continue; | 
					
						
							|  |  |  | 		ASSERT(err == 0); | 
					
						
							|  |  |  | 		ASSERT(child->db_level == 0); | 
					
						
							|  |  |  | 		dr = child->db_last_dirty; | 
					
						
							|  |  |  | 		while (dr && dr->dr_txg > txg) | 
					
						
							|  |  |  | 			dr = dr->dr_next; | 
					
						
							|  |  |  | 		ASSERT(dr == NULL || dr->dr_txg == txg); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/* data_old better be zeroed */ | 
					
						
							|  |  |  | 		if (dr) { | 
					
						
							|  |  |  | 			buf = dr->dt.dl.dr_data->b_data; | 
					
						
							|  |  |  | 			for (j = 0; j < child->db.db_size >> 3; j++) { | 
					
						
							|  |  |  | 				if (buf[j] != 0) { | 
					
						
							|  |  |  | 					panic("freed data not zero: " | 
					
						
							|  |  |  | 					    "child=%p i=%d off=%d num=%d\n", | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 					    (void *)child, i, off, num); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * db_data better be zeroed unless it's dirty in a | 
					
						
							|  |  |  | 		 * future txg. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		mutex_enter(&child->db_mtx); | 
					
						
							|  |  |  | 		buf = child->db.db_data; | 
					
						
							|  |  |  | 		if (buf != NULL && child->db_state != DB_FILL && | 
					
						
							|  |  |  | 		    child->db_last_dirty == NULL) { | 
					
						
							|  |  |  | 			for (j = 0; j < child->db.db_size >> 3; j++) { | 
					
						
							|  |  |  | 				if (buf[j] != 0) { | 
					
						
							|  |  |  | 					panic("freed data not zero: " | 
					
						
							|  |  |  | 					    "child=%p i=%d off=%d num=%d\n", | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 					    (void *)child, i, off, num); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 				} | 
					
						
							|  |  |  | 			} | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mutex_exit(&child->db_mtx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		dbuf_rele(child, FTAG); | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 	DB_DNODE_EXIT(db); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | } | 
					
						
							|  |  |  | #endif
 | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | static void | 
					
						
							|  |  |  | free_children(dmu_buf_impl_t *db, uint64_t blkid, uint64_t nblks, | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  |     dmu_tx_t *tx) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 	dnode_t *dn; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	blkptr_t *bp; | 
					
						
							|  |  |  | 	dmu_buf_impl_t *subdb; | 
					
						
							|  |  |  | 	uint64_t start, end, dbstart, dbend, i; | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	int epbs, shift; | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * There is a small possibility that this block will not be cached: | 
					
						
							|  |  |  | 	 *   1 - if level > 1 and there are no children with level <= 1 | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	 *   2 - if this block was evicted since we read it from | 
					
						
							|  |  |  | 	 *	 dmu_tx_hold_free(). | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 	 */ | 
					
						
							|  |  |  | 	if (db->db_state != DB_CACHED) | 
					
						
							|  |  |  | 		(void) dbuf_read(db, NULL, DB_RF_MUST_SUCCEED); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 	dbuf_release_bp(db); | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	bp = db->db.db_data; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 	DB_DNODE_ENTER(db); | 
					
						
							|  |  |  | 	dn = DB_DNODE(db); | 
					
						
							|  |  |  | 	epbs = dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	shift = (db->db_level - 1) * epbs; | 
					
						
							|  |  |  | 	dbstart = db->db_blkid << epbs; | 
					
						
							|  |  |  | 	start = blkid >> shift; | 
					
						
							|  |  |  | 	if (dbstart < start) { | 
					
						
							|  |  |  | 		bp += start - dbstart; | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		start = dbstart; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	dbend = ((db->db_blkid + 1) << epbs) - 1; | 
					
						
							|  |  |  | 	end = (blkid + nblks - 1) >> shift; | 
					
						
							|  |  |  | 	if (dbend <= end) | 
					
						
							|  |  |  | 		end = dbend; | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	ASSERT3U(start, <=, end); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (db->db_level == 1) { | 
					
						
							|  |  |  | 		FREE_VERIFY(db, start, end, tx); | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 		free_blocks(dn, bp, end-start+1, tx); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		for (i = start; i <= end; i++, bp++) { | 
					
						
							|  |  |  | 			if (BP_IS_HOLE(bp)) | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			rw_enter(&dn->dn_struct_rwlock, RW_READER); | 
					
						
							|  |  |  | 			VERIFY0(dbuf_hold_impl(dn, db->db_level - 1, | 
					
						
							| 
									
										
										
										
											2015-12-22 04:31:57 +03:00
										 |  |  | 			    i, TRUE, FALSE, FTAG, &subdb)); | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 			rw_exit(&dn->dn_struct_rwlock); | 
					
						
							|  |  |  | 			ASSERT3P(bp, ==, subdb->db_blkptr); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			free_children(subdb, blkid, nblks, tx); | 
					
						
							|  |  |  | 			dbuf_rele(subdb, FTAG); | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	/* If this whole block is free, free ourself too. */ | 
					
						
							|  |  |  | 	for (i = 0, bp = db->db.db_data; i < 1 << epbs; i++, bp++) { | 
					
						
							|  |  |  | 		if (!BP_IS_HOLE(bp)) | 
					
						
							|  |  |  | 			break; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	if (i == 1 << epbs) { | 
					
						
							|  |  |  | 		/* didn't find any non-holes */ | 
					
						
							|  |  |  | 		bzero(db->db.db_data, db->db.db_size); | 
					
						
							|  |  |  | 		free_blocks(dn, db->db_blkptr, 1, tx); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		/*
 | 
					
						
							|  |  |  | 		 * Partial block free; must be marked dirty so that it | 
					
						
							|  |  |  | 		 * will be written out. | 
					
						
							|  |  |  | 		 */ | 
					
						
							|  |  |  | 		ASSERT(db->db_dirtycnt > 0); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	} | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | 	DB_DNODE_EXIT(db); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	arc_buf_freeze(db->db_buf); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2013-06-11 21:12:34 +04:00
										 |  |  |  * Traverse the indicated range of the provided file | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  |  * and "free" all the blocks contained there. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | static void | 
					
						
							| 
									
										
										
										
											2014-04-16 07:40:22 +04:00
										 |  |  | dnode_sync_free_range_impl(dnode_t *dn, uint64_t blkid, uint64_t nblks, | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  |     dmu_tx_t *tx) | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | { | 
					
						
							|  |  |  | 	blkptr_t *bp = dn->dn_phys->dn_blkptr; | 
					
						
							|  |  |  | 	int dnlevel = dn->dn_phys->dn_nlevels; | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	boolean_t trunc = B_FALSE; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	if (blkid > dn->dn_phys->dn_maxblkid) | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ASSERT(dn->dn_phys->dn_maxblkid < UINT64_MAX); | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	if (blkid + nblks > dn->dn_phys->dn_maxblkid) { | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		nblks = dn->dn_phys->dn_maxblkid - blkid + 1; | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 		trunc = B_TRUE; | 
					
						
							|  |  |  | 	} | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	/* There are no indirect blocks in the object */ | 
					
						
							|  |  |  | 	if (dnlevel == 1) { | 
					
						
							|  |  |  | 		if (blkid >= dn->dn_phys->dn_nblkptr) { | 
					
						
							|  |  |  | 			/* this range was never made persistent */ | 
					
						
							|  |  |  | 			return; | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		ASSERT3U(blkid + nblks, <=, dn->dn_phys->dn_nblkptr); | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 		free_blocks(dn, bp + blkid, nblks, tx); | 
					
						
							|  |  |  | 	} else { | 
					
						
							|  |  |  | 		int shift = (dnlevel - 1) * | 
					
						
							|  |  |  | 		    (dn->dn_phys->dn_indblkshift - SPA_BLKPTRSHIFT); | 
					
						
							|  |  |  | 		int start = blkid >> shift; | 
					
						
							|  |  |  | 		int end = (blkid + nblks - 1) >> shift; | 
					
						
							|  |  |  | 		dmu_buf_impl_t *db; | 
					
						
							|  |  |  | 		int i; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ASSERT(start < dn->dn_phys->dn_nblkptr); | 
					
						
							|  |  |  | 		bp += start; | 
					
						
							|  |  |  | 		for (i = start; i <= end; i++, bp++) { | 
					
						
							|  |  |  | 			if (BP_IS_HOLE(bp)) | 
					
						
							|  |  |  | 				continue; | 
					
						
							|  |  |  | 			rw_enter(&dn->dn_struct_rwlock, RW_READER); | 
					
						
							|  |  |  | 			VERIFY0(dbuf_hold_impl(dn, dnlevel - 1, i, | 
					
						
							| 
									
										
										
										
											2015-12-22 04:31:57 +03:00
										 |  |  | 			    TRUE, FALSE, FTAG, &db)); | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 			rw_exit(&dn->dn_struct_rwlock); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			free_children(db, blkid, nblks, tx); | 
					
						
							|  |  |  | 			dbuf_rele(db, FTAG); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (trunc) { | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 		ASSERTV(uint64_t off); | 
					
						
							|  |  |  | 		dn->dn_phys->dn_maxblkid = blkid == 0 ? 0 : blkid - 1; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		ASSERTV(off = (dn->dn_phys->dn_maxblkid + 1) * | 
					
						
							| 
									
										
										
										
											2010-08-26 20:53:00 +04:00
										 |  |  | 		    (dn->dn_phys->dn_datablkszsec << SPA_MINBLOCKSHIFT)); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		ASSERT(off < dn->dn_phys->dn_maxblkid || | 
					
						
							|  |  |  | 		    dn->dn_phys->dn_maxblkid == 0 || | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 		    dnode_next_offset(dn, 0, &off, 1, 1, 0) != 0); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-04-16 07:40:22 +04:00
										 |  |  | typedef struct dnode_sync_free_range_arg { | 
					
						
							|  |  |  | 	dnode_t *dsfra_dnode; | 
					
						
							|  |  |  | 	dmu_tx_t *dsfra_tx; | 
					
						
							|  |  |  | } dnode_sync_free_range_arg_t; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | dnode_sync_free_range(void *arg, uint64_t blkid, uint64_t nblks) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	dnode_sync_free_range_arg_t *dsfra = arg; | 
					
						
							|  |  |  | 	dnode_t *dn = dsfra->dsfra_dnode; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex_exit(&dn->dn_mtx); | 
					
						
							|  |  |  | 	dnode_sync_free_range_impl(dn, blkid, nblks, dsfra->dsfra_tx); | 
					
						
							|  |  |  | 	mutex_enter(&dn->dn_mtx); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | /*
 | 
					
						
							| 
									
										
										
										
											2013-06-11 21:12:34 +04:00
										 |  |  |  * Try to kick all the dnode's dbufs out of the cache... | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  |  */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | dnode_evict_dbufs(dnode_t *dn) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2015-04-02 06:44:32 +03:00
										 |  |  | 	dmu_buf_impl_t *db_marker; | 
					
						
							|  |  |  | 	dmu_buf_impl_t *db, *db_next; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 06:44:32 +03:00
										 |  |  | 	db_marker = kmem_alloc(sizeof (dmu_buf_impl_t), KM_SLEEP); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex_enter(&dn->dn_dbufs_mtx); | 
					
						
							|  |  |  | 	for (db = avl_first(&dn->dn_dbufs); db != NULL; db = db_next) { | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | #ifdef	DEBUG
 | 
					
						
							| 
									
										
										
										
											2015-04-02 06:44:32 +03:00
										 |  |  | 		DB_DNODE_ENTER(db); | 
					
						
							|  |  |  | 		ASSERT3P(DB_DNODE(db), ==, dn); | 
					
						
							|  |  |  | 		DB_DNODE_EXIT(db); | 
					
						
							| 
									
										
										
										
											2010-08-27 01:24:34 +04:00
										 |  |  | #endif	/* DEBUG */
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 06:44:32 +03:00
										 |  |  | 		mutex_enter(&db->db_mtx); | 
					
						
							|  |  |  | 		if (db->db_state != DB_EVICTING && | 
					
						
							|  |  |  | 		    refcount_is_zero(&db->db_holds)) { | 
					
						
							|  |  |  | 			db_marker->db_level = db->db_level; | 
					
						
							|  |  |  | 			db_marker->db_blkid = db->db_blkid; | 
					
						
							|  |  |  | 			db_marker->db_state = DB_SEARCH; | 
					
						
							|  |  |  | 			avl_insert_here(&dn->dn_dbufs, db_marker, db, | 
					
						
							|  |  |  | 			    AVL_BEFORE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			dbuf_clear(db); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 			db_next = AVL_NEXT(&dn->dn_dbufs, db_marker); | 
					
						
							|  |  |  | 			avl_remove(&dn->dn_dbufs, db_marker); | 
					
						
							|  |  |  | 		} else { | 
					
						
							| 
									
										
										
										
											2015-10-14 00:09:45 +03:00
										 |  |  | 			db->db_pending_evict = TRUE; | 
					
						
							| 
									
										
										
										
											2015-04-02 06:44:32 +03:00
										 |  |  | 			mutex_exit(&db->db_mtx); | 
					
						
							|  |  |  | 			db_next = AVL_NEXT(&dn->dn_dbufs, db); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		} | 
					
						
							| 
									
										
										
										
											2015-04-02 06:44:32 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	mutex_exit(&dn->dn_dbufs_mtx); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-04-02 06:44:32 +03:00
										 |  |  | 	kmem_free(db_marker, sizeof (dmu_buf_impl_t)); | 
					
						
							| 
									
										
										
										
											2010-08-26 21:24:44 +04:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-03-12 03:10:35 +03:00
										 |  |  | 	dnode_evict_bonus(dn); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | dnode_evict_bonus(dnode_t *dn) | 
					
						
							|  |  |  | { | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	rw_enter(&dn->dn_struct_rwlock, RW_WRITER); | 
					
						
							| 
									
										
										
										
											2015-10-14 00:09:45 +03:00
										 |  |  | 	if (dn->dn_bonus != NULL) { | 
					
						
							|  |  |  | 		if (refcount_is_zero(&dn->dn_bonus->db_holds)) { | 
					
						
							|  |  |  | 			mutex_enter(&dn->dn_bonus->db_mtx); | 
					
						
							|  |  |  | 			dbuf_evict(dn->dn_bonus); | 
					
						
							|  |  |  | 			dn->dn_bonus = NULL; | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			dn->dn_bonus->db_pending_evict = TRUE; | 
					
						
							|  |  |  | 		} | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 	rw_exit(&dn->dn_struct_rwlock); | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | dnode_undirty_dbufs(list_t *list) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	dbuf_dirty_record_t *dr; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-08-26 20:52:42 +04:00
										 |  |  | 	while ((dr = list_head(list))) { | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		dmu_buf_impl_t *db = dr->dr_dbuf; | 
					
						
							|  |  |  | 		uint64_t txg = dr->dr_txg; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 		if (db->db_level != 0) | 
					
						
							|  |  |  | 			dnode_undirty_dbufs(&dr->dt.di.dr_children); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		mutex_enter(&db->db_mtx); | 
					
						
							|  |  |  | 		/* XXX - use dbuf_undirty()? */ | 
					
						
							|  |  |  | 		list_remove(list, dr); | 
					
						
							|  |  |  | 		ASSERT(db->db_last_dirty == dr); | 
					
						
							|  |  |  | 		db->db_last_dirty = NULL; | 
					
						
							|  |  |  | 		db->db_dirtycnt -= 1; | 
					
						
							|  |  |  | 		if (db->db_level == 0) { | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 			ASSERT(db->db_blkid == DMU_BONUS_BLKID || | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 			    dr->dt.dl.dr_data == db->db_buf); | 
					
						
							|  |  |  | 			dbuf_unoverride(dr); | 
					
						
							| 
									
										
										
										
											2015-04-01 16:49:14 +03:00
										 |  |  | 		} else { | 
					
						
							|  |  |  | 			mutex_destroy(&dr->dt.di.dr_mtx); | 
					
						
							|  |  |  | 			list_destroy(&dr->dt.di.dr_children); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 		kmem_free(dr, sizeof (dbuf_dirty_record_t)); | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 		dbuf_rele_and_unlock(db, (void *)(uintptr_t)txg); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | static void | 
					
						
							|  |  |  | dnode_sync_free(dnode_t *dn, dmu_tx_t *tx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	int txgoff = tx->tx_txg & TXG_MASK; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ASSERT(dmu_tx_is_syncing(tx)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Our contents should have been freed in dnode_sync() by the | 
					
						
							|  |  |  | 	 * free range record inserted by the caller of dnode_free(). | 
					
						
							|  |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2013-05-11 01:17:03 +04:00
										 |  |  | 	ASSERT0(DN_USED_BYTES(dn->dn_phys)); | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 	ASSERT(BP_IS_HOLE(dn->dn_phys->dn_blkptr)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	dnode_undirty_dbufs(&dn->dn_dirty_records[txgoff]); | 
					
						
							|  |  |  | 	dnode_evict_dbufs(dn); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * XXX - It would be nice to assert this, but we may still | 
					
						
							|  |  |  | 	 * have residual holds from async evictions from the arc... | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * zfs_obj_to_path() also depends on this being | 
					
						
							|  |  |  | 	 * commented out. | 
					
						
							|  |  |  | 	 * | 
					
						
							|  |  |  | 	 * ASSERT3U(refcount_count(&dn->dn_holds), ==, 1); | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* Undirty next bits */ | 
					
						
							|  |  |  | 	dn->dn_next_nlevels[txgoff] = 0; | 
					
						
							|  |  |  | 	dn->dn_next_indblkshift[txgoff] = 0; | 
					
						
							|  |  |  | 	dn->dn_next_blksz[txgoff] = 0; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/* ASSERT(blkptrs are zero); */ | 
					
						
							|  |  |  | 	ASSERT(dn->dn_phys->dn_type != DMU_OT_NONE); | 
					
						
							|  |  |  | 	ASSERT(dn->dn_type != DMU_OT_NONE); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ASSERT(dn->dn_free_txg > 0); | 
					
						
							|  |  |  | 	if (dn->dn_allocated_txg != dn->dn_free_txg) | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 		dmu_buf_will_dirty(&dn->dn_dbuf->db, tx); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	bzero(dn->dn_phys, sizeof (dnode_phys_t)); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex_enter(&dn->dn_mtx); | 
					
						
							|  |  |  | 	dn->dn_type = DMU_OT_NONE; | 
					
						
							|  |  |  | 	dn->dn_maxblkid = 0; | 
					
						
							|  |  |  | 	dn->dn_allocated_txg = 0; | 
					
						
							|  |  |  | 	dn->dn_free_txg = 0; | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 	dn->dn_have_spill = B_FALSE; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	mutex_exit(&dn->dn_mtx); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ASSERT(dn->dn_object != DMU_META_DNODE_OBJECT); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Now that we've released our hold, the dnode may | 
					
						
							|  |  |  | 	 * be evicted, so we musn't access it. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | /*
 | 
					
						
							|  |  |  |  * Write out the dnode's dirty buffers. | 
					
						
							|  |  |  |  */ | 
					
						
							|  |  |  | void | 
					
						
							|  |  |  | dnode_sync(dnode_t *dn, dmu_tx_t *tx) | 
					
						
							|  |  |  | { | 
					
						
							|  |  |  | 	dnode_phys_t *dnp = dn->dn_phys; | 
					
						
							|  |  |  | 	int txgoff = tx->tx_txg & TXG_MASK; | 
					
						
							|  |  |  | 	list_t *list = &dn->dn_dirty_records[txgoff]; | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 	boolean_t kill_spill = B_FALSE; | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	boolean_t freeing_dnode; | 
					
						
							| 
									
										
										
										
											2010-08-26 20:53:00 +04:00
										 |  |  | 	ASSERTV(static const dnode_phys_t zerodn = { 0 }); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							|  |  |  | 	ASSERT(dmu_tx_is_syncing(tx)); | 
					
						
							|  |  |  | 	ASSERT(dnp->dn_type != DMU_OT_NONE || dn->dn_allocated_txg); | 
					
						
							| 
									
										
										
										
											2009-07-03 02:44:48 +04:00
										 |  |  | 	ASSERT(dnp->dn_type != DMU_OT_NONE || | 
					
						
							|  |  |  | 	    bcmp(dnp, &zerodn, DNODE_SIZE) == 0); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	DNODE_VERIFY(dn); | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	ASSERT(dn->dn_dbuf == NULL || arc_released(dn->dn_dbuf->db_buf)); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 02:44:48 +04:00
										 |  |  | 	if (dmu_objset_userused_enabled(dn->dn_objset) && | 
					
						
							|  |  |  | 	    !DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 		mutex_enter(&dn->dn_mtx); | 
					
						
							|  |  |  | 		dn->dn_oldused = DN_USED_BYTES(dn->dn_phys); | 
					
						
							|  |  |  | 		dn->dn_oldflags = dn->dn_phys->dn_flags; | 
					
						
							| 
									
										
										
										
											2009-07-03 02:44:48 +04:00
										 |  |  | 		dn->dn_phys->dn_flags |= DNODE_FLAG_USERUSED_ACCOUNTED; | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 		mutex_exit(&dn->dn_mtx); | 
					
						
							|  |  |  | 		dmu_objset_userquota_get_ids(dn, B_FALSE, tx); | 
					
						
							| 
									
										
										
										
											2009-07-03 02:44:48 +04:00
										 |  |  | 	} else { | 
					
						
							|  |  |  | 		/* Once we account for it, we should always account for it. */ | 
					
						
							|  |  |  | 		ASSERT(!(dn->dn_phys->dn_flags & | 
					
						
							|  |  |  | 		    DNODE_FLAG_USERUSED_ACCOUNTED)); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	mutex_enter(&dn->dn_mtx); | 
					
						
							|  |  |  | 	if (dn->dn_allocated_txg == tx->tx_txg) { | 
					
						
							|  |  |  | 		/* The dnode is newly allocated or reallocated */ | 
					
						
							|  |  |  | 		if (dnp->dn_type == DMU_OT_NONE) { | 
					
						
							|  |  |  | 			/* this is a first alloc, not a realloc */ | 
					
						
							|  |  |  | 			dnp->dn_nlevels = 1; | 
					
						
							| 
									
										
										
										
											2009-02-18 23:51:31 +03:00
										 |  |  | 			dnp->dn_nblkptr = dn->dn_nblkptr; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		dnp->dn_type = dn->dn_type; | 
					
						
							|  |  |  | 		dnp->dn_bonustype = dn->dn_bonustype; | 
					
						
							|  |  |  | 		dnp->dn_bonuslen = dn->dn_bonuslen; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 	ASSERT(dnp->dn_nlevels > 1 || | 
					
						
							|  |  |  | 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) || | 
					
						
							| 
									
										
										
										
											2014-06-06 01:19:08 +04:00
										 |  |  | 	    BP_IS_EMBEDDED(&dnp->dn_blkptr[0]) || | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) == | 
					
						
							|  |  |  | 	    dnp->dn_datablkszsec << SPA_MINBLOCKSHIFT); | 
					
						
							| 
									
										
										
										
											2014-06-06 01:19:08 +04:00
										 |  |  | 	ASSERT(dnp->dn_nlevels < 2 || | 
					
						
							|  |  |  | 	    BP_IS_HOLE(&dnp->dn_blkptr[0]) || | 
					
						
							|  |  |  | 	    BP_GET_LSIZE(&dnp->dn_blkptr[0]) == 1 << dnp->dn_indblkshift); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-08 21:13:05 +04:00
										 |  |  | 	if (dn->dn_next_type[txgoff] != 0) { | 
					
						
							|  |  |  | 		dnp->dn_type = dn->dn_type; | 
					
						
							|  |  |  | 		dn->dn_next_type[txgoff] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	if (dn->dn_next_blksz[txgoff] != 0) { | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		ASSERT(P2PHASE(dn->dn_next_blksz[txgoff], | 
					
						
							|  |  |  | 		    SPA_MINBLOCKSIZE) == 0); | 
					
						
							|  |  |  | 		ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[0]) || | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 		    dn->dn_maxblkid == 0 || list_head(list) != NULL || | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT == | 
					
						
							| 
									
										
										
										
											2014-04-16 07:40:22 +04:00
										 |  |  | 		    dnp->dn_datablkszsec || | 
					
						
							|  |  |  | 		    range_tree_space(dn->dn_free_ranges[txgoff]) != 0); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		dnp->dn_datablkszsec = | 
					
						
							|  |  |  | 		    dn->dn_next_blksz[txgoff] >> SPA_MINBLOCKSHIFT; | 
					
						
							|  |  |  | 		dn->dn_next_blksz[txgoff] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-08 21:13:05 +04:00
										 |  |  | 	if (dn->dn_next_bonuslen[txgoff] != 0) { | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		if (dn->dn_next_bonuslen[txgoff] == DN_ZERO_BONUSLEN) | 
					
						
							|  |  |  | 			dnp->dn_bonuslen = 0; | 
					
						
							|  |  |  | 		else | 
					
						
							|  |  |  | 			dnp->dn_bonuslen = dn->dn_next_bonuslen[txgoff]; | 
					
						
							|  |  |  | 		ASSERT(dnp->dn_bonuslen <= DN_MAX_BONUSLEN); | 
					
						
							|  |  |  | 		dn->dn_next_bonuslen[txgoff] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-08 21:13:05 +04:00
										 |  |  | 	if (dn->dn_next_bonustype[txgoff] != 0) { | 
					
						
							| 
									
										
										
										
											2012-12-14 03:24:15 +04:00
										 |  |  | 		ASSERT(DMU_OT_IS_VALID(dn->dn_next_bonustype[txgoff])); | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 		dnp->dn_bonustype = dn->dn_next_bonustype[txgoff]; | 
					
						
							|  |  |  | 		dn->dn_next_bonustype[txgoff] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	freeing_dnode = dn->dn_free_txg > 0 && dn->dn_free_txg <= tx->tx_txg; | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 	/*
 | 
					
						
							| 
									
										
										
										
											2015-05-06 20:24:09 +03:00
										 |  |  | 	 * Remove the spill block if we have been explicitly asked to | 
					
						
							|  |  |  | 	 * remove it, or if the object is being removed. | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 	 */ | 
					
						
							| 
									
										
										
										
											2015-05-06 20:24:09 +03:00
										 |  |  | 	if (dn->dn_rm_spillblk[txgoff] || freeing_dnode) { | 
					
						
							|  |  |  | 		if (dnp->dn_flags & DNODE_FLAG_SPILL_BLKPTR) | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 			kill_spill = B_TRUE; | 
					
						
							|  |  |  | 		dn->dn_rm_spillblk[txgoff] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-08 21:13:05 +04:00
										 |  |  | 	if (dn->dn_next_indblkshift[txgoff] != 0) { | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		ASSERT(dnp->dn_nlevels == 1); | 
					
						
							|  |  |  | 		dnp->dn_indblkshift = dn->dn_next_indblkshift[txgoff]; | 
					
						
							|  |  |  | 		dn->dn_next_indblkshift[txgoff] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Just take the live (open-context) values for checksum and compress. | 
					
						
							|  |  |  | 	 * Strictly speaking it's a future leak, but nothing bad happens if we | 
					
						
							|  |  |  | 	 * start using the new checksum or compress algorithm a little early. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | 	dnp->dn_checksum = dn->dn_checksum; | 
					
						
							|  |  |  | 	dnp->dn_compress = dn->dn_compress; | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	mutex_exit(&dn->dn_mtx); | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 	if (kill_spill) { | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 		free_blocks(dn, &dn->dn_phys->dn_spill, 1, tx); | 
					
						
							| 
									
										
										
										
											2010-05-29 00:45:14 +04:00
										 |  |  | 		mutex_enter(&dn->dn_mtx); | 
					
						
							|  |  |  | 		dnp->dn_flags &= ~DNODE_FLAG_SPILL_BLKPTR; | 
					
						
							|  |  |  | 		mutex_exit(&dn->dn_mtx); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	/* process all the "freed" ranges in the file */ | 
					
						
							| 
									
										
										
										
											2014-04-16 07:40:22 +04:00
										 |  |  | 	if (dn->dn_free_ranges[txgoff] != NULL) { | 
					
						
							|  |  |  | 		dnode_sync_free_range_arg_t dsfra; | 
					
						
							|  |  |  | 		dsfra.dsfra_dnode = dn; | 
					
						
							|  |  |  | 		dsfra.dsfra_tx = tx; | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 		mutex_enter(&dn->dn_mtx); | 
					
						
							| 
									
										
										
										
											2014-04-16 07:40:22 +04:00
										 |  |  | 		range_tree_vacate(dn->dn_free_ranges[txgoff], | 
					
						
							|  |  |  | 		    dnode_sync_free_range, &dsfra); | 
					
						
							|  |  |  | 		range_tree_destroy(dn->dn_free_ranges[txgoff]); | 
					
						
							|  |  |  | 		dn->dn_free_ranges[txgoff] = NULL; | 
					
						
							| 
									
										
										
										
											2008-12-03 23:09:06 +03:00
										 |  |  | 		mutex_exit(&dn->dn_mtx); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-12-09 22:37:51 +04:00
										 |  |  | 	if (freeing_dnode) { | 
					
						
							| 
									
										
										
										
											2016-05-17 04:02:29 +03:00
										 |  |  | 		dn->dn_objset->os_freed_dnodes++; | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		dnode_sync_free(dn, tx); | 
					
						
							|  |  |  | 		return; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2014-09-05 04:50:36 +04:00
										 |  |  | 	if (dn->dn_next_nlevels[txgoff]) { | 
					
						
							|  |  |  | 		dnode_increase_indirection(dn, tx); | 
					
						
							|  |  |  | 		dn->dn_next_nlevels[txgoff] = 0; | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-02-18 23:51:31 +03:00
										 |  |  | 	if (dn->dn_next_nblkptr[txgoff]) { | 
					
						
							|  |  |  | 		/* this should only happen on a realloc */ | 
					
						
							|  |  |  | 		ASSERT(dn->dn_allocated_txg == tx->tx_txg); | 
					
						
							|  |  |  | 		if (dn->dn_next_nblkptr[txgoff] > dnp->dn_nblkptr) { | 
					
						
							|  |  |  | 			/* zero the new blkptrs we are gaining */ | 
					
						
							|  |  |  | 			bzero(dnp->dn_blkptr + dnp->dn_nblkptr, | 
					
						
							|  |  |  | 			    sizeof (blkptr_t) * | 
					
						
							|  |  |  | 			    (dn->dn_next_nblkptr[txgoff] - dnp->dn_nblkptr)); | 
					
						
							|  |  |  | #ifdef ZFS_DEBUG
 | 
					
						
							|  |  |  | 		} else { | 
					
						
							|  |  |  | 			int i; | 
					
						
							|  |  |  | 			ASSERT(dn->dn_next_nblkptr[txgoff] < dnp->dn_nblkptr); | 
					
						
							|  |  |  | 			/* the blkptrs we are losing better be unallocated */ | 
					
						
							| 
									
										
										
										
											2012-12-27 02:56:41 +04:00
										 |  |  | 			for (i = 0; i < dnp->dn_nblkptr; i++) { | 
					
						
							|  |  |  | 				if (i >= dn->dn_next_nblkptr[txgoff]) | 
					
						
							|  |  |  | 					ASSERT(BP_IS_HOLE(&dnp->dn_blkptr[i])); | 
					
						
							|  |  |  | 			} | 
					
						
							| 
									
										
										
										
											2009-02-18 23:51:31 +03:00
										 |  |  | #endif
 | 
					
						
							|  |  |  | 		} | 
					
						
							|  |  |  | 		mutex_enter(&dn->dn_mtx); | 
					
						
							|  |  |  | 		dnp->dn_nblkptr = dn->dn_next_nblkptr[txgoff]; | 
					
						
							|  |  |  | 		dn->dn_next_nblkptr[txgoff] = 0; | 
					
						
							|  |  |  | 		mutex_exit(&dn->dn_mtx); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2015-07-02 19:23:20 +03:00
										 |  |  | 	dbuf_sync_list(list, dn->dn_phys->dn_nlevels - 1, tx); | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 
 | 
					
						
							| 
									
										
										
										
											2009-07-03 02:44:48 +04:00
										 |  |  | 	if (!DMU_OBJECT_IS_SPECIAL(dn->dn_object)) { | 
					
						
							| 
									
										
										
										
											2008-11-20 23:01:55 +03:00
										 |  |  | 		ASSERT3P(list_head(list), ==, NULL); | 
					
						
							|  |  |  | 		dnode_rele(dn, (void *)(uintptr_t)tx->tx_txg); | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	/*
 | 
					
						
							|  |  |  | 	 * Although we have dropped our reference to the dnode, it | 
					
						
							|  |  |  | 	 * can't be evicted until its written, and we haven't yet | 
					
						
							|  |  |  | 	 * initiated the IO for the dnode's dbuf. | 
					
						
							|  |  |  | 	 */ | 
					
						
							|  |  |  | } |