xref: /dflybsd-src/sys/vfs/hammer/hammer_reblock.c (revision 982be4bfef38fc8be86f896586cd899dd9994fc8)
1bf686dbeSMatthew Dillon /*
2bf686dbeSMatthew Dillon  * Copyright (c) 2008 The DragonFly Project.  All rights reserved.
3bf686dbeSMatthew Dillon  *
4bf686dbeSMatthew Dillon  * This code is derived from software contributed to The DragonFly Project
5bf686dbeSMatthew Dillon  * by Matthew Dillon <dillon@backplane.com>
6bf686dbeSMatthew Dillon  *
7bf686dbeSMatthew Dillon  * Redistribution and use in source and binary forms, with or without
8bf686dbeSMatthew Dillon  * modification, are permitted provided that the following conditions
9bf686dbeSMatthew Dillon  * are met:
10bf686dbeSMatthew Dillon  *
11bf686dbeSMatthew Dillon  * 1. Redistributions of source code must retain the above copyright
12bf686dbeSMatthew Dillon  *    notice, this list of conditions and the following disclaimer.
13bf686dbeSMatthew Dillon  * 2. Redistributions in binary form must reproduce the above copyright
14bf686dbeSMatthew Dillon  *    notice, this list of conditions and the following disclaimer in
15bf686dbeSMatthew Dillon  *    the documentation and/or other materials provided with the
16bf686dbeSMatthew Dillon  *    distribution.
17bf686dbeSMatthew Dillon  * 3. Neither the name of The DragonFly Project nor the names of its
18bf686dbeSMatthew Dillon  *    contributors may be used to endorse or promote products derived
19bf686dbeSMatthew Dillon  *    from this software without specific, prior written permission.
20bf686dbeSMatthew Dillon  *
21bf686dbeSMatthew Dillon  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22bf686dbeSMatthew Dillon  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23bf686dbeSMatthew Dillon  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24bf686dbeSMatthew Dillon  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
25bf686dbeSMatthew Dillon  * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26bf686dbeSMatthew Dillon  * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27bf686dbeSMatthew Dillon  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28bf686dbeSMatthew Dillon  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29bf686dbeSMatthew Dillon  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30bf686dbeSMatthew Dillon  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31bf686dbeSMatthew Dillon  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32bf686dbeSMatthew Dillon  * SUCH DAMAGE.
33bf686dbeSMatthew Dillon  *
3444a83111SMatthew Dillon  * $DragonFly: src/sys/vfs/hammer/hammer_reblock.c,v 1.34 2008/11/13 02:18:43 dillon Exp $
35bf686dbeSMatthew Dillon  */
36bf686dbeSMatthew Dillon /*
37bf686dbeSMatthew Dillon  * HAMMER reblocker - This code frees up fragmented physical space
38bf686dbeSMatthew Dillon  *
39bf686dbeSMatthew Dillon  * HAMMER only keeps track of free space on a big-block basis.  A big-block
40bf686dbeSMatthew Dillon  * containing holes can only be freed by migrating the remaining data in
41bf686dbeSMatthew Dillon  * that big-block into a new big-block, then freeing the big-block.
42bf686dbeSMatthew Dillon  *
43bf686dbeSMatthew Dillon  * This function is called from an ioctl or via the hammer support thread.
44bf686dbeSMatthew Dillon  */
45bf686dbeSMatthew Dillon 
46bf686dbeSMatthew Dillon #include "hammer.h"
47bf686dbeSMatthew Dillon 
4836f82b23SMatthew Dillon static int hammer_reblock_helper(struct hammer_ioc_reblock *reblock,
49bf686dbeSMatthew Dillon 				 hammer_cursor_t cursor,
50bf686dbeSMatthew Dillon 				 hammer_btree_elm_t elm);
5136f82b23SMatthew Dillon static int hammer_reblock_data(struct hammer_ioc_reblock *reblock,
52bf686dbeSMatthew Dillon 				hammer_cursor_t cursor, hammer_btree_elm_t elm);
532f85fa4dSMatthew Dillon static int hammer_reblock_leaf_node(struct hammer_ioc_reblock *reblock,
542f85fa4dSMatthew Dillon 				hammer_cursor_t cursor, hammer_btree_elm_t elm);
552f85fa4dSMatthew Dillon static int hammer_reblock_int_node(struct hammer_ioc_reblock *reblock,
56bf686dbeSMatthew Dillon 				hammer_cursor_t cursor, hammer_btree_elm_t elm);
57bf686dbeSMatthew Dillon 
58bf686dbeSMatthew Dillon int
5936f82b23SMatthew Dillon hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
6036f82b23SMatthew Dillon 	       struct hammer_ioc_reblock *reblock)
61bf686dbeSMatthew Dillon {
62bf686dbeSMatthew Dillon 	struct hammer_cursor cursor;
63bf686dbeSMatthew Dillon 	hammer_btree_elm_t elm;
64a7e9bef1SMatthew Dillon 	int checkspace_count;
6593291532SMatthew Dillon 	int error;
6693291532SMatthew Dillon 	int seq;
677b6ccb11SMatthew Dillon 	int slop;
687b6ccb11SMatthew Dillon 
697b6ccb11SMatthew Dillon 	/*
707b6ccb11SMatthew Dillon 	 * A fill level <= 20% is considered an emergency.  free_level is
717b6ccb11SMatthew Dillon 	 * inverted from fill_level.
727b6ccb11SMatthew Dillon 	 */
737b6ccb11SMatthew Dillon 	if (reblock->free_level >= HAMMER_LARGEBLOCK_SIZE * 8 / 10)
747b6ccb11SMatthew Dillon 		slop = HAMMER_CHKSPC_EMERGENCY;
757b6ccb11SMatthew Dillon 	else
767b6ccb11SMatthew Dillon 		slop = HAMMER_CHKSPC_REBLOCK;
77bf686dbeSMatthew Dillon 
78dd94f1b1SMatthew Dillon 	if ((reblock->key_beg.localization | reblock->key_end.localization) &
79dd94f1b1SMatthew Dillon 	    HAMMER_LOCALIZE_PSEUDOFS_MASK) {
80dd94f1b1SMatthew Dillon 		return(EINVAL);
81dd94f1b1SMatthew Dillon 	}
82dd94f1b1SMatthew Dillon 	if (reblock->key_beg.obj_id >= reblock->key_end.obj_id)
83bf686dbeSMatthew Dillon 		return(EINVAL);
84bf686dbeSMatthew Dillon 	if (reblock->free_level < 0)
85bf686dbeSMatthew Dillon 		return(EINVAL);
86bf686dbeSMatthew Dillon 
87dd94f1b1SMatthew Dillon 	reblock->key_cur = reblock->key_beg;
88842e7a70SMatthew Dillon 	reblock->key_cur.localization &= HAMMER_LOCALIZE_MASK;
89dd94f1b1SMatthew Dillon 	reblock->key_cur.localization += ip->obj_localization;
90814387f6SMatthew Dillon 
91a7e9bef1SMatthew Dillon 	checkspace_count = 0;
9293291532SMatthew Dillon 	seq = trans->hmp->flusher.act;
93bf686dbeSMatthew Dillon retry:
944e17f465SMatthew Dillon 	error = hammer_init_cursor(trans, &cursor, NULL, NULL);
95bf686dbeSMatthew Dillon 	if (error) {
96bf686dbeSMatthew Dillon 		hammer_done_cursor(&cursor);
97dd94f1b1SMatthew Dillon 		goto failed;
98bf686dbeSMatthew Dillon 	}
99dd94f1b1SMatthew Dillon 	cursor.key_beg.localization = reblock->key_cur.localization;
100dd94f1b1SMatthew Dillon 	cursor.key_beg.obj_id = reblock->key_cur.obj_id;
101bf686dbeSMatthew Dillon 	cursor.key_beg.key = HAMMER_MIN_KEY;
102bf686dbeSMatthew Dillon 	cursor.key_beg.create_tid = 1;
103bf686dbeSMatthew Dillon 	cursor.key_beg.delete_tid = 0;
104bf686dbeSMatthew Dillon 	cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
105bf686dbeSMatthew Dillon 	cursor.key_beg.obj_type = 0;
106bf686dbeSMatthew Dillon 
107842e7a70SMatthew Dillon 	cursor.key_end.localization = (reblock->key_end.localization &
108842e7a70SMatthew Dillon 					HAMMER_LOCALIZE_MASK) +
109dd94f1b1SMatthew Dillon 				      ip->obj_localization;
110dd94f1b1SMatthew Dillon 	cursor.key_end.obj_id = reblock->key_end.obj_id;
111bf686dbeSMatthew Dillon 	cursor.key_end.key = HAMMER_MAX_KEY;
112bf686dbeSMatthew Dillon 	cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
113bf686dbeSMatthew Dillon 	cursor.key_end.delete_tid = 0;
114bf686dbeSMatthew Dillon 	cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
115bf686dbeSMatthew Dillon 	cursor.key_end.obj_type = 0;
116bf686dbeSMatthew Dillon 
117bf686dbeSMatthew Dillon 	cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1189480ff55SMatthew Dillon 	cursor.flags |= HAMMER_CURSOR_BACKEND;
119bf686dbeSMatthew Dillon 
1202f85fa4dSMatthew Dillon 	/*
1212f85fa4dSMatthew Dillon 	 * This flag allows the btree scan code to return internal nodes,
1222f85fa4dSMatthew Dillon 	 * so we can reblock them in addition to the leafs.  Only specify it
1232f85fa4dSMatthew Dillon 	 * if we intend to reblock B-Tree nodes.
1242f85fa4dSMatthew Dillon 	 */
1252f85fa4dSMatthew Dillon 	if (reblock->head.flags & HAMMER_IOC_DO_BTREE)
1262f85fa4dSMatthew Dillon 		cursor.flags |= HAMMER_CURSOR_REBLOCKING;
1272f85fa4dSMatthew Dillon 
128bf686dbeSMatthew Dillon 	error = hammer_btree_first(&cursor);
129bf686dbeSMatthew Dillon 	while (error == 0) {
1302f85fa4dSMatthew Dillon 		/*
1312f85fa4dSMatthew Dillon 		 * Internal or Leaf node
1322f85fa4dSMatthew Dillon 		 */
133bf686dbeSMatthew Dillon 		elm = &cursor.node->ondisk->elms[cursor.index];
134dd94f1b1SMatthew Dillon 		reblock->key_cur.obj_id = elm->base.obj_id;
135dd94f1b1SMatthew Dillon 		reblock->key_cur.localization = elm->base.localization;
136bf686dbeSMatthew Dillon 
1379480ff55SMatthew Dillon 		/*
1389f5097dcSMatthew Dillon 		 * Yield to more important tasks
1399f5097dcSMatthew Dillon 		 */
1409f5097dcSMatthew Dillon 		if ((error = hammer_signal_check(trans->hmp)) != 0)
1419f5097dcSMatthew Dillon 			break;
142a7e9bef1SMatthew Dillon 
143a7e9bef1SMatthew Dillon 		/*
144a7e9bef1SMatthew Dillon 		 * If there is insufficient free space it may be due to
145a7e9bef1SMatthew Dillon 		 * reserved bigblocks, which flushing might fix.
146a7e9bef1SMatthew Dillon 		 */
1477b6ccb11SMatthew Dillon 		if (hammer_checkspace(trans->hmp, slop)) {
148a7e9bef1SMatthew Dillon 			if (++checkspace_count == 10) {
149a7e9bef1SMatthew Dillon 				error = ENOSPC;
150a7e9bef1SMatthew Dillon 				break;
151a7e9bef1SMatthew Dillon 			}
152*982be4bfSMatthew Dillon 			hammer_unlock_cursor(&cursor);
15393291532SMatthew Dillon 			hammer_flusher_wait(trans->hmp, seq);
154*982be4bfSMatthew Dillon 			hammer_lock_cursor(&cursor);
1557a61b85dSMatthew Dillon 			seq = hammer_flusher_async(trans->hmp, NULL);
1567b6ccb11SMatthew Dillon 			continue;
15793291532SMatthew Dillon 		}
158a7e9bef1SMatthew Dillon 
159a7e9bef1SMatthew Dillon 		/*
1609480ff55SMatthew Dillon 		 * Acquiring the sync_lock prevents the operation from
1619480ff55SMatthew Dillon 		 * crossing a synchronization boundary.
16209ac686bSMatthew Dillon 		 *
16309ac686bSMatthew Dillon 		 * NOTE: cursor.node may have changed on return.
1649480ff55SMatthew Dillon 		 */
1652f85fa4dSMatthew Dillon 		hammer_sync_lock_sh(trans);
16636f82b23SMatthew Dillon 		error = hammer_reblock_helper(reblock, &cursor, elm);
1672f85fa4dSMatthew Dillon 		hammer_sync_unlock(trans);
16893291532SMatthew Dillon 
16915e75dabSMatthew Dillon 		while (hammer_flusher_meta_halflimit(trans->hmp) ||
1707a61b85dSMatthew Dillon 		       hammer_flusher_undo_exhausted(trans, 2)) {
171*982be4bfSMatthew Dillon 			hammer_unlock_cursor(&cursor);
17293291532SMatthew Dillon 			hammer_flusher_wait(trans->hmp, seq);
173*982be4bfSMatthew Dillon 			hammer_lock_cursor(&cursor);
17415e75dabSMatthew Dillon 			seq = hammer_flusher_async_one(trans->hmp);
17593291532SMatthew Dillon 		}
1761b0ab2c3SMatthew Dillon 
1771b0ab2c3SMatthew Dillon 		/*
1781b0ab2c3SMatthew Dillon 		 * Setup for iteration, our cursor flags may be modified by
1791b0ab2c3SMatthew Dillon 		 * other threads while we are unlocked.
1801b0ab2c3SMatthew Dillon 		 */
181bf686dbeSMatthew Dillon 		cursor.flags |= HAMMER_CURSOR_ATEDISK;
1821b0ab2c3SMatthew Dillon 
1831b0ab2c3SMatthew Dillon 		/*
1841b0ab2c3SMatthew Dillon 		 * We allocate data buffers, which atm we don't track
1851b0ab2c3SMatthew Dillon 		 * dirty levels for because we allow the kernel to write
1861b0ab2c3SMatthew Dillon 		 * them.  But if we allocate too many we can still deadlock
1871b0ab2c3SMatthew Dillon 		 * the buffer cache.
1881b0ab2c3SMatthew Dillon 		 *
1891b0ab2c3SMatthew Dillon 		 * (The cursor's node and element may change!)
1901b0ab2c3SMatthew Dillon 		 */
1911b0ab2c3SMatthew Dillon 		if (bd_heatup()) {
192*982be4bfSMatthew Dillon 			hammer_unlock_cursor(&cursor);
1931b0ab2c3SMatthew Dillon 			bwillwrite(HAMMER_XBUFSIZE);
194*982be4bfSMatthew Dillon 			hammer_lock_cursor(&cursor);
1951b0ab2c3SMatthew Dillon 		}
1961b0ab2c3SMatthew Dillon 
1971b0ab2c3SMatthew Dillon 		if (error == 0) {
198bf686dbeSMatthew Dillon 			error = hammer_btree_iterate(&cursor);
199bf686dbeSMatthew Dillon 		}
200a7e9bef1SMatthew Dillon 
201bf686dbeSMatthew Dillon 	}
202bf686dbeSMatthew Dillon 	if (error == ENOENT)
203bf686dbeSMatthew Dillon 		error = 0;
204bf686dbeSMatthew Dillon 	hammer_done_cursor(&cursor);
20506ad81ffSMatthew Dillon 	if (error == EWOULDBLOCK) {
20606ad81ffSMatthew Dillon 		hammer_flusher_sync(trans->hmp);
20706ad81ffSMatthew Dillon 		goto retry;
20806ad81ffSMatthew Dillon 	}
209bf686dbeSMatthew Dillon 	if (error == EDEADLK)
210bf686dbeSMatthew Dillon 		goto retry;
21119619882SMatthew Dillon 	if (error == EINTR) {
21219619882SMatthew Dillon 		reblock->head.flags |= HAMMER_IOC_HEAD_INTR;
21319619882SMatthew Dillon 		error = 0;
21419619882SMatthew Dillon 	}
215dd94f1b1SMatthew Dillon failed:
216dd94f1b1SMatthew Dillon 	reblock->key_cur.localization &= HAMMER_LOCALIZE_MASK;
217bf686dbeSMatthew Dillon 	return(error);
218bf686dbeSMatthew Dillon }
219bf686dbeSMatthew Dillon 
220bf686dbeSMatthew Dillon /*
221bf686dbeSMatthew Dillon  * Reblock the B-Tree (leaf) node, record, and/or data if necessary.
222bf686dbeSMatthew Dillon  *
2239480ff55SMatthew Dillon  * XXX We have no visibility into internal B-Tree nodes at the moment,
2249480ff55SMatthew Dillon  * only leaf nodes.
225bf686dbeSMatthew Dillon  */
226bf686dbeSMatthew Dillon static int
22736f82b23SMatthew Dillon hammer_reblock_helper(struct hammer_ioc_reblock *reblock,
228bf686dbeSMatthew Dillon 		      hammer_cursor_t cursor, hammer_btree_elm_t elm)
229bf686dbeSMatthew Dillon {
23043c665aeSMatthew Dillon 	hammer_mount_t hmp;
231bf686dbeSMatthew Dillon 	hammer_off_t tmp_offset;
23244a83111SMatthew Dillon 	struct hammer_btree_leaf_elm leaf;
233bf686dbeSMatthew Dillon 	int error;
234bf686dbeSMatthew Dillon 	int bytes;
235bf686dbeSMatthew Dillon 	int cur;
236bf3b416bSMatthew Dillon 	int iocflags;
237bf686dbeSMatthew Dillon 
238bf686dbeSMatthew Dillon 	error = 0;
23943c665aeSMatthew Dillon 	hmp = cursor->trans->hmp;
240bf686dbeSMatthew Dillon 
241bf686dbeSMatthew Dillon 	/*
242bf686dbeSMatthew Dillon 	 * Reblock data.  Note that data embedded in a record is reblocked
2432f85fa4dSMatthew Dillon 	 * by the record reblock code.  Data processing only occurs at leaf
2442f85fa4dSMatthew Dillon 	 * nodes and for RECORD element types.
245bf686dbeSMatthew Dillon 	 */
2462f85fa4dSMatthew Dillon 	if (cursor->node->ondisk->type != HAMMER_BTREE_TYPE_LEAF)
2472f85fa4dSMatthew Dillon 		goto skip;
2482f85fa4dSMatthew Dillon 	if (elm->leaf.base.btype != HAMMER_BTREE_TYPE_RECORD)
2492f85fa4dSMatthew Dillon 		return(0);
250bf686dbeSMatthew Dillon 	tmp_offset = elm->leaf.data_offset;
251bf3b416bSMatthew Dillon 	if (tmp_offset == 0)
252bf3b416bSMatthew Dillon 		goto skip;
253bf3b416bSMatthew Dillon 	if (error)
254bf3b416bSMatthew Dillon 		goto skip;
255bf3b416bSMatthew Dillon 
256bf3b416bSMatthew Dillon 	/*
257bf3b416bSMatthew Dillon 	 * NOTE: Localization restrictions may also have been set-up, we can't
258bf3b416bSMatthew Dillon 	 * just set the match flags willy-nilly here.
259bf3b416bSMatthew Dillon 	 */
260bf3b416bSMatthew Dillon 	switch(elm->leaf.base.rec_type) {
261bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_INODE:
262bf3b416bSMatthew Dillon 		iocflags = HAMMER_IOC_DO_INODES;
263bf3b416bSMatthew Dillon 		break;
264bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_EXT:
265bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_FIX:
266ea434b6fSMatthew Dillon 	case HAMMER_RECTYPE_PFS:
267bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_DIRENTRY:
268bf3b416bSMatthew Dillon 		iocflags = HAMMER_IOC_DO_DIRS;
269bf3b416bSMatthew Dillon 		break;
270bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_DATA:
271bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_DB:
272bf3b416bSMatthew Dillon 		iocflags = HAMMER_IOC_DO_DATA;
273bf3b416bSMatthew Dillon 		break;
274bf3b416bSMatthew Dillon 	default:
275bf3b416bSMatthew Dillon 		iocflags = 0;
276bf3b416bSMatthew Dillon 		break;
277bf3b416bSMatthew Dillon 	}
278bf3b416bSMatthew Dillon 	if (reblock->head.flags & iocflags) {
279bf686dbeSMatthew Dillon 		++reblock->data_count;
280bf686dbeSMatthew Dillon 		reblock->data_byte_count += elm->leaf.data_len;
28143c665aeSMatthew Dillon 		bytes = hammer_blockmap_getfree(hmp, tmp_offset, &cur, &error);
2826e1e8b6dSMatthew Dillon 		if (hammer_debug_general & 0x4000)
2832f85fa4dSMatthew Dillon 			kprintf("D %6d/%d\n", bytes, reblock->free_level);
284bf3b416bSMatthew Dillon 		if (error == 0 && (cur == 0 || reblock->free_level == 0) &&
285bf3b416bSMatthew Dillon 		    bytes >= reblock->free_level) {
28644a83111SMatthew Dillon 			/*
28744a83111SMatthew Dillon 			 * This is nasty, the uncache code may have to get
28844a83111SMatthew Dillon 			 * vnode locks and because of that we can't hold
28944a83111SMatthew Dillon 			 * the cursor locked.
29044a83111SMatthew Dillon 			 */
29144a83111SMatthew Dillon 			leaf = elm->leaf;
292*982be4bfSMatthew Dillon 			hammer_unlock_cursor(cursor);
29344a83111SMatthew Dillon 			hammer_io_direct_uncache(hmp, &leaf);
294*982be4bfSMatthew Dillon 			hammer_lock_cursor(cursor);
29544a83111SMatthew Dillon 			if (cursor->flags & HAMMER_CURSOR_RETEST) {
29644a83111SMatthew Dillon 				kprintf("hammer: retest after uncache\n");
29744a83111SMatthew Dillon 				error = EDEADLK;
29844a83111SMatthew Dillon 			} else {
29944a83111SMatthew Dillon 				KKASSERT(bcmp(&elm->leaf, &leaf, sizeof(leaf)) == 0);
30044a83111SMatthew Dillon 			}
30144a83111SMatthew Dillon 			if (error == 0)
302bf686dbeSMatthew Dillon 				error = hammer_cursor_upgrade(cursor);
303bf686dbeSMatthew Dillon 			if (error == 0) {
30436f82b23SMatthew Dillon 				error = hammer_reblock_data(reblock,
305bf686dbeSMatthew Dillon 							    cursor, elm);
306bf686dbeSMatthew Dillon 			}
307bf686dbeSMatthew Dillon 			if (error == 0) {
308bf686dbeSMatthew Dillon 				++reblock->data_moves;
309bf686dbeSMatthew Dillon 				reblock->data_byte_moves += elm->leaf.data_len;
310bf686dbeSMatthew Dillon 			}
311bf686dbeSMatthew Dillon 		}
312bf686dbeSMatthew Dillon 	}
313bf686dbeSMatthew Dillon 
3142f85fa4dSMatthew Dillon skip:
315bf686dbeSMatthew Dillon 	/*
3162f85fa4dSMatthew Dillon 	 * Reblock a B-Tree internal or leaf node.
317bf686dbeSMatthew Dillon 	 */
318bf686dbeSMatthew Dillon 	tmp_offset = cursor->node->node_offset;
319bf3b416bSMatthew Dillon 	if (cursor->index == 0 &&
320814387f6SMatthew Dillon 	    error == 0 && (reblock->head.flags & HAMMER_IOC_DO_BTREE)) {
321bf686dbeSMatthew Dillon 		++reblock->btree_count;
32243c665aeSMatthew Dillon 		bytes = hammer_blockmap_getfree(hmp, tmp_offset, &cur, &error);
3236e1e8b6dSMatthew Dillon 		if (hammer_debug_general & 0x4000)
3242f85fa4dSMatthew Dillon 			kprintf("B %6d/%d\n", bytes, reblock->free_level);
325bf3b416bSMatthew Dillon 		if (error == 0 && (cur == 0 || reblock->free_level == 0) &&
326bf3b416bSMatthew Dillon 		    bytes >= reblock->free_level) {
327bf686dbeSMatthew Dillon 			error = hammer_cursor_upgrade(cursor);
328bf686dbeSMatthew Dillon 			if (error == 0) {
329bf686dbeSMatthew Dillon 				if (cursor->parent)
330bf686dbeSMatthew Dillon 					elm = &cursor->parent->ondisk->elms[cursor->parent_index];
331bf686dbeSMatthew Dillon 				else
332bf686dbeSMatthew Dillon 					elm = NULL;
3332f85fa4dSMatthew Dillon 				switch(cursor->node->ondisk->type) {
3342f85fa4dSMatthew Dillon 				case HAMMER_BTREE_TYPE_LEAF:
3352f85fa4dSMatthew Dillon 					error = hammer_reblock_leaf_node(
3362f85fa4dSMatthew Dillon 							reblock, cursor, elm);
3372f85fa4dSMatthew Dillon 					break;
3382f85fa4dSMatthew Dillon 				case HAMMER_BTREE_TYPE_INTERNAL:
3392f85fa4dSMatthew Dillon 					error = hammer_reblock_int_node(
3402f85fa4dSMatthew Dillon 							reblock, cursor, elm);
3412f85fa4dSMatthew Dillon 					break;
3422f85fa4dSMatthew Dillon 				default:
3432f85fa4dSMatthew Dillon 					panic("Illegal B-Tree node type");
3442f85fa4dSMatthew Dillon 				}
345bf686dbeSMatthew Dillon 			}
346bf686dbeSMatthew Dillon 			if (error == 0) {
347bf686dbeSMatthew Dillon 				++reblock->btree_moves;
348bf686dbeSMatthew Dillon 			}
349bf686dbeSMatthew Dillon 		}
350bf686dbeSMatthew Dillon 	}
351bf686dbeSMatthew Dillon 
352bf686dbeSMatthew Dillon 	hammer_cursor_downgrade(cursor);
353bf686dbeSMatthew Dillon 	return(error);
354bf686dbeSMatthew Dillon }
355bf686dbeSMatthew Dillon 
356bf686dbeSMatthew Dillon /*
357bf686dbeSMatthew Dillon  * Reblock a record's data.  Both the B-Tree element and record pointers
358bf686dbeSMatthew Dillon  * to the data must be adjusted.
359bf686dbeSMatthew Dillon  */
360bf686dbeSMatthew Dillon static int
36136f82b23SMatthew Dillon hammer_reblock_data(struct hammer_ioc_reblock *reblock,
362bf686dbeSMatthew Dillon 		    hammer_cursor_t cursor, hammer_btree_elm_t elm)
363bf686dbeSMatthew Dillon {
364bf686dbeSMatthew Dillon 	struct hammer_buffer *data_buffer = NULL;
365bf686dbeSMatthew Dillon 	hammer_off_t ndata_offset;
366bf686dbeSMatthew Dillon 	int error;
367bf686dbeSMatthew Dillon 	void *ndata;
368bf686dbeSMatthew Dillon 
369bf686dbeSMatthew Dillon 	error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA |
37011ad5adeSMatthew Dillon 					     HAMMER_CURSOR_GET_LEAF);
371bf686dbeSMatthew Dillon 	if (error)
372bf686dbeSMatthew Dillon 		return (error);
37336f82b23SMatthew Dillon 	ndata = hammer_alloc_data(cursor->trans, elm->leaf.data_len,
374bf3b416bSMatthew Dillon 				  elm->leaf.base.rec_type,
37536f82b23SMatthew Dillon 				  &ndata_offset, &data_buffer, &error);
376bf686dbeSMatthew Dillon 	if (error)
377bf686dbeSMatthew Dillon 		goto done;
378bf686dbeSMatthew Dillon 
379bf686dbeSMatthew Dillon 	/*
380bf686dbeSMatthew Dillon 	 * Move the data
381bf686dbeSMatthew Dillon 	 */
38210a5d1baSMatthew Dillon 	hammer_modify_buffer(cursor->trans, data_buffer, NULL, 0);
383bf686dbeSMatthew Dillon 	bcopy(cursor->data, ndata, elm->leaf.data_len);
38410a5d1baSMatthew Dillon 	hammer_modify_buffer_done(data_buffer);
385bf686dbeSMatthew Dillon 
38636f82b23SMatthew Dillon 	hammer_blockmap_free(cursor->trans,
38736f82b23SMatthew Dillon 			     elm->leaf.data_offset, elm->leaf.data_len);
388bf686dbeSMatthew Dillon 
38910a5d1baSMatthew Dillon 	hammer_modify_node(cursor->trans, cursor->node,
39010a5d1baSMatthew Dillon 			   &elm->leaf.data_offset, sizeof(hammer_off_t));
391bf686dbeSMatthew Dillon 	elm->leaf.data_offset = ndata_offset;
39210a5d1baSMatthew Dillon 	hammer_modify_node_done(cursor->node);
393bf686dbeSMatthew Dillon 
394bf686dbeSMatthew Dillon done:
395bf686dbeSMatthew Dillon 	if (data_buffer)
396bf686dbeSMatthew Dillon 		hammer_rel_buffer(data_buffer, 0);
397bf686dbeSMatthew Dillon 	return (error);
398bf686dbeSMatthew Dillon }
399bf686dbeSMatthew Dillon 
400bf686dbeSMatthew Dillon /*
4012f85fa4dSMatthew Dillon  * Reblock a B-Tree leaf node.  The parent must be adjusted to point to
4022f85fa4dSMatthew Dillon  * the new copy of the leaf node.
403bf686dbeSMatthew Dillon  *
4042f85fa4dSMatthew Dillon  * elm is a pointer to the parent element pointing at cursor.node.
405bf686dbeSMatthew Dillon  */
406bf686dbeSMatthew Dillon static int
4072f85fa4dSMatthew Dillon hammer_reblock_leaf_node(struct hammer_ioc_reblock *reblock,
408bf686dbeSMatthew Dillon 			 hammer_cursor_t cursor, hammer_btree_elm_t elm)
409bf686dbeSMatthew Dillon {
410bf686dbeSMatthew Dillon 	hammer_node_t onode;
411bf686dbeSMatthew Dillon 	hammer_node_t nnode;
412bf686dbeSMatthew Dillon 	int error;
413bf686dbeSMatthew Dillon 
414bf686dbeSMatthew Dillon 	onode = cursor->node;
41536f82b23SMatthew Dillon 	nnode = hammer_alloc_btree(cursor->trans, &error);
4168d0efe43SMatthew Dillon 
417bf686dbeSMatthew Dillon 	if (nnode == NULL)
418bf686dbeSMatthew Dillon 		return (error);
419bf686dbeSMatthew Dillon 
420bf686dbeSMatthew Dillon 	/*
421bf686dbeSMatthew Dillon 	 * Move the node
422bf686dbeSMatthew Dillon 	 */
42309ac686bSMatthew Dillon 	hammer_lock_ex(&nnode->lock);
42409ac686bSMatthew Dillon 	hammer_modify_node_noundo(cursor->trans, nnode);
425bf686dbeSMatthew Dillon 	bcopy(onode->ondisk, nnode->ondisk, sizeof(*nnode->ondisk));
426bf686dbeSMatthew Dillon 
427bf686dbeSMatthew Dillon 	if (elm) {
428bf686dbeSMatthew Dillon 		/*
429bf686dbeSMatthew Dillon 		 * We are not the root of the B-Tree
430bf686dbeSMatthew Dillon 		 */
43136f82b23SMatthew Dillon 		hammer_modify_node(cursor->trans, cursor->parent,
432bf686dbeSMatthew Dillon 				   &elm->internal.subtree_offset,
433bf686dbeSMatthew Dillon 				   sizeof(elm->internal.subtree_offset));
434bf686dbeSMatthew Dillon 		elm->internal.subtree_offset = nnode->node_offset;
43510a5d1baSMatthew Dillon 		hammer_modify_node_done(cursor->parent);
436bf686dbeSMatthew Dillon 	} else {
437bf686dbeSMatthew Dillon 		/*
438bf686dbeSMatthew Dillon 		 * We are the root of the B-Tree
439bf686dbeSMatthew Dillon 		 */
440bf686dbeSMatthew Dillon                 hammer_volume_t volume;
441bf686dbeSMatthew Dillon 
44236f82b23SMatthew Dillon                 volume = hammer_get_root_volume(cursor->trans->hmp, &error);
443bf686dbeSMatthew Dillon                 KKASSERT(error == 0);
444bf686dbeSMatthew Dillon 
445e8599db1SMatthew Dillon                 hammer_modify_volume_field(cursor->trans, volume,
446e8599db1SMatthew Dillon 					   vol0_btree_root);
447bf686dbeSMatthew Dillon                 volume->ondisk->vol0_btree_root = nnode->node_offset;
44810a5d1baSMatthew Dillon                 hammer_modify_volume_done(volume);
449bf686dbeSMatthew Dillon                 hammer_rel_volume(volume, 0);
450bf686dbeSMatthew Dillon         }
451bf686dbeSMatthew Dillon 
452b3bad96fSMatthew Dillon 	hammer_cursor_replaced_node(onode, nnode);
45336f82b23SMatthew Dillon 	hammer_delete_node(cursor->trans, onode);
454bf686dbeSMatthew Dillon 
455b58c6388SMatthew Dillon 	if (hammer_debug_general & 0x4000) {
4562f85fa4dSMatthew Dillon 		kprintf("REBLOCK LNODE %016llx -> %016llx\n",
457bf686dbeSMatthew Dillon 			onode->node_offset, nnode->node_offset);
458b58c6388SMatthew Dillon 	}
4598d0efe43SMatthew Dillon 	hammer_modify_node_done(nnode);
460bf686dbeSMatthew Dillon 	cursor->node = nnode;
46109ac686bSMatthew Dillon 
46209ac686bSMatthew Dillon 	hammer_unlock(&onode->lock);
463bf686dbeSMatthew Dillon 	hammer_rel_node(onode);
464bf686dbeSMatthew Dillon 
465bf686dbeSMatthew Dillon 	return (error);
466bf686dbeSMatthew Dillon }
467bf686dbeSMatthew Dillon 
4682f85fa4dSMatthew Dillon /*
4692f85fa4dSMatthew Dillon  * Reblock a B-Tree internal node.  The parent must be adjusted to point to
4702f85fa4dSMatthew Dillon  * the new copy of the internal node, and the node's children's parent
4712f85fa4dSMatthew Dillon  * pointers must also be adjusted to point to the new copy.
4722f85fa4dSMatthew Dillon  *
4732f85fa4dSMatthew Dillon  * elm is a pointer to the parent element pointing at cursor.node.
4742f85fa4dSMatthew Dillon  */
4752f85fa4dSMatthew Dillon static int
4762f85fa4dSMatthew Dillon hammer_reblock_int_node(struct hammer_ioc_reblock *reblock,
4772f85fa4dSMatthew Dillon 			 hammer_cursor_t cursor, hammer_btree_elm_t elm)
4782f85fa4dSMatthew Dillon {
4792f85fa4dSMatthew Dillon 	hammer_node_locklist_t locklist = NULL;
4802f85fa4dSMatthew Dillon 	hammer_node_t onode;
4812f85fa4dSMatthew Dillon 	hammer_node_t nnode;
4822f85fa4dSMatthew Dillon 	int error;
4832f85fa4dSMatthew Dillon 	int i;
4842f85fa4dSMatthew Dillon 
4852f85fa4dSMatthew Dillon 	error = hammer_btree_lock_children(cursor, &locklist);
4862f85fa4dSMatthew Dillon 	if (error)
4872f85fa4dSMatthew Dillon 		goto done;
4882f85fa4dSMatthew Dillon 
4892f85fa4dSMatthew Dillon 	onode = cursor->node;
4902f85fa4dSMatthew Dillon 	nnode = hammer_alloc_btree(cursor->trans, &error);
4912f85fa4dSMatthew Dillon 
4922f85fa4dSMatthew Dillon 	if (nnode == NULL)
4932f85fa4dSMatthew Dillon 		goto done;
4942f85fa4dSMatthew Dillon 
4952f85fa4dSMatthew Dillon 	/*
4962f85fa4dSMatthew Dillon 	 * Move the node.  Adjust the parent's pointer to us first.
4972f85fa4dSMatthew Dillon 	 */
4982f85fa4dSMatthew Dillon 	hammer_lock_ex(&nnode->lock);
4992f85fa4dSMatthew Dillon 	hammer_modify_node_noundo(cursor->trans, nnode);
5002f85fa4dSMatthew Dillon 	bcopy(onode->ondisk, nnode->ondisk, sizeof(*nnode->ondisk));
5012f85fa4dSMatthew Dillon 
5022f85fa4dSMatthew Dillon 	if (elm) {
5032f85fa4dSMatthew Dillon 		/*
5042f85fa4dSMatthew Dillon 		 * We are not the root of the B-Tree
5052f85fa4dSMatthew Dillon 		 */
5062f85fa4dSMatthew Dillon 		hammer_modify_node(cursor->trans, cursor->parent,
5072f85fa4dSMatthew Dillon 				   &elm->internal.subtree_offset,
5082f85fa4dSMatthew Dillon 				   sizeof(elm->internal.subtree_offset));
5092f85fa4dSMatthew Dillon 		elm->internal.subtree_offset = nnode->node_offset;
5102f85fa4dSMatthew Dillon 		hammer_modify_node_done(cursor->parent);
5112f85fa4dSMatthew Dillon 	} else {
5122f85fa4dSMatthew Dillon 		/*
5132f85fa4dSMatthew Dillon 		 * We are the root of the B-Tree
5142f85fa4dSMatthew Dillon 		 */
5152f85fa4dSMatthew Dillon                 hammer_volume_t volume;
5162f85fa4dSMatthew Dillon 
5172f85fa4dSMatthew Dillon                 volume = hammer_get_root_volume(cursor->trans->hmp, &error);
5182f85fa4dSMatthew Dillon                 KKASSERT(error == 0);
5192f85fa4dSMatthew Dillon 
5202f85fa4dSMatthew Dillon                 hammer_modify_volume_field(cursor->trans, volume,
5212f85fa4dSMatthew Dillon 					   vol0_btree_root);
5222f85fa4dSMatthew Dillon                 volume->ondisk->vol0_btree_root = nnode->node_offset;
5232f85fa4dSMatthew Dillon                 hammer_modify_volume_done(volume);
5242f85fa4dSMatthew Dillon                 hammer_rel_volume(volume, 0);
5252f85fa4dSMatthew Dillon         }
5262f85fa4dSMatthew Dillon 
5272f85fa4dSMatthew Dillon 	/*
5282f85fa4dSMatthew Dillon 	 * Now adjust our children's pointers to us.
5292f85fa4dSMatthew Dillon 	 */
5302f85fa4dSMatthew Dillon 	for (i = 0; i < nnode->ondisk->count; ++i) {
5312f85fa4dSMatthew Dillon 		elm = &nnode->ondisk->elms[i];
5322f85fa4dSMatthew Dillon 		error = btree_set_parent(cursor->trans, nnode, elm);
5332f85fa4dSMatthew Dillon 		if (error)
5342f85fa4dSMatthew Dillon 			panic("reblock internal node: fixup problem");
5352f85fa4dSMatthew Dillon 	}
5362f85fa4dSMatthew Dillon 
5372f85fa4dSMatthew Dillon 	/*
5382f85fa4dSMatthew Dillon 	 * Clean up.
5392f85fa4dSMatthew Dillon 	 *
5402f85fa4dSMatthew Dillon 	 * The new node replaces the current node in the cursor.  The cursor
5412f85fa4dSMatthew Dillon 	 * expects it to be locked so leave it locked.  Discard onode.
5422f85fa4dSMatthew Dillon 	 */
543b3bad96fSMatthew Dillon 	hammer_cursor_replaced_node(onode, nnode);
5442f85fa4dSMatthew Dillon 	hammer_delete_node(cursor->trans, onode);
5452f85fa4dSMatthew Dillon 
5462f85fa4dSMatthew Dillon 	if (hammer_debug_general & 0x4000) {
5472f85fa4dSMatthew Dillon 		kprintf("REBLOCK INODE %016llx -> %016llx\n",
5482f85fa4dSMatthew Dillon 			onode->node_offset, nnode->node_offset);
5492f85fa4dSMatthew Dillon 	}
5502f85fa4dSMatthew Dillon 	hammer_modify_node_done(nnode);
5512f85fa4dSMatthew Dillon 	cursor->node = nnode;
5522f85fa4dSMatthew Dillon 
5532f85fa4dSMatthew Dillon 	hammer_unlock(&onode->lock);
5542f85fa4dSMatthew Dillon 	hammer_rel_node(onode);
5552f85fa4dSMatthew Dillon 
5562f85fa4dSMatthew Dillon done:
557bac808feSMatthew Dillon 	hammer_btree_unlock_children(cursor, &locklist);
5582f85fa4dSMatthew Dillon 	return (error);
5592f85fa4dSMatthew Dillon }
5602f85fa4dSMatthew Dillon 
561