xref: /dflybsd-src/sys/vfs/hammer/hammer_reblock.c (revision 4fa5fb92edf94e3ddbbeecd94be13d3faf834b74)
1bf686dbeSMatthew Dillon /*
255b50bd5SMatthew Dillon  * Copyright (c) 2008-2012 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  */
34bf686dbeSMatthew Dillon /*
35bf686dbeSMatthew Dillon  * HAMMER reblocker - This code frees up fragmented physical space
36bf686dbeSMatthew Dillon  *
37bf686dbeSMatthew Dillon  * HAMMER only keeps track of free space on a big-block basis.  A big-block
38bf686dbeSMatthew Dillon  * containing holes can only be freed by migrating the remaining data in
39bf686dbeSMatthew Dillon  * that big-block into a new big-block, then freeing the big-block.
40bf686dbeSMatthew Dillon  *
41bf686dbeSMatthew Dillon  * This function is called from an ioctl or via the hammer support thread.
42bf686dbeSMatthew Dillon  */
43bf686dbeSMatthew Dillon 
44bf686dbeSMatthew Dillon #include "hammer.h"
45bf686dbeSMatthew Dillon 
4636f82b23SMatthew Dillon static int hammer_reblock_helper(struct hammer_ioc_reblock *reblock,
47bf686dbeSMatthew Dillon 				 hammer_cursor_t cursor,
48bf686dbeSMatthew Dillon 				 hammer_btree_elm_t elm);
4936f82b23SMatthew Dillon static int hammer_reblock_data(struct hammer_ioc_reblock *reblock,
50bf686dbeSMatthew Dillon 				hammer_cursor_t cursor, hammer_btree_elm_t elm);
512f85fa4dSMatthew Dillon static int hammer_reblock_leaf_node(struct hammer_ioc_reblock *reblock,
522f85fa4dSMatthew Dillon 				hammer_cursor_t cursor, hammer_btree_elm_t elm);
532f85fa4dSMatthew Dillon static int hammer_reblock_int_node(struct hammer_ioc_reblock *reblock,
54bf686dbeSMatthew Dillon 				hammer_cursor_t cursor, hammer_btree_elm_t elm);
55bf686dbeSMatthew Dillon 
56bf686dbeSMatthew Dillon int
5736f82b23SMatthew Dillon hammer_ioc_reblock(hammer_transaction_t trans, hammer_inode_t ip,
5836f82b23SMatthew Dillon 		   struct hammer_ioc_reblock *reblock)
59bf686dbeSMatthew Dillon {
60bf686dbeSMatthew Dillon 	struct hammer_cursor cursor;
61bf686dbeSMatthew Dillon 	hammer_btree_elm_t elm;
62a7e9bef1SMatthew Dillon 	int checkspace_count;
6393291532SMatthew Dillon 	int error;
6493291532SMatthew Dillon 	int seq;
657b6ccb11SMatthew Dillon 	int slop;
667b6ccb11SMatthew Dillon 
67*4fa5fb92STomohiro Kusumi 	if ((reblock->key_beg.localization | reblock->key_end.localization) &
68*4fa5fb92STomohiro Kusumi 	    HAMMER_LOCALIZE_PSEUDOFS_MASK) {
69*4fa5fb92STomohiro Kusumi 		return(EINVAL);
70*4fa5fb92STomohiro Kusumi 	}
71*4fa5fb92STomohiro Kusumi 	if (reblock->key_beg.obj_id >= reblock->key_end.obj_id)
72*4fa5fb92STomohiro Kusumi 		return(EINVAL);
73*4fa5fb92STomohiro Kusumi 	if (reblock->free_level < 0 ||
74*4fa5fb92STomohiro Kusumi 	    reblock->free_level > HAMMER_BIGBLOCK_SIZE)
75*4fa5fb92STomohiro Kusumi 		return(EINVAL);
76*4fa5fb92STomohiro Kusumi 
777b6ccb11SMatthew Dillon 	/*
787b6ccb11SMatthew Dillon 	 * A fill level <= 20% is considered an emergency.  free_level is
797b6ccb11SMatthew Dillon 	 * inverted from fill_level.
807b6ccb11SMatthew Dillon 	 */
81e04ee2deSTomohiro Kusumi 	if (reblock->free_level >= HAMMER_BIGBLOCK_SIZE * 8 / 10)
827b6ccb11SMatthew Dillon 		slop = HAMMER_CHKSPC_EMERGENCY;
837b6ccb11SMatthew Dillon 	else
847b6ccb11SMatthew Dillon 		slop = HAMMER_CHKSPC_REBLOCK;
85bf686dbeSMatthew Dillon 
86dd94f1b1SMatthew Dillon 	reblock->key_cur = reblock->key_beg;
87842e7a70SMatthew Dillon 	reblock->key_cur.localization &= HAMMER_LOCALIZE_MASK;
88dd94f1b1SMatthew Dillon 	reblock->key_cur.localization += ip->obj_localization;
89814387f6SMatthew Dillon 
90a7e9bef1SMatthew Dillon 	checkspace_count = 0;
91e86903d8SMatthew Dillon 	seq = trans->hmp->flusher.done;
92bf686dbeSMatthew Dillon retry:
934e17f465SMatthew Dillon 	error = hammer_init_cursor(trans, &cursor, NULL, NULL);
94bf686dbeSMatthew Dillon 	if (error) {
95bf686dbeSMatthew Dillon 		hammer_done_cursor(&cursor);
96dd94f1b1SMatthew Dillon 		goto failed;
97bf686dbeSMatthew Dillon 	}
98dd94f1b1SMatthew Dillon 	cursor.key_beg.localization = reblock->key_cur.localization;
99dd94f1b1SMatthew Dillon 	cursor.key_beg.obj_id = reblock->key_cur.obj_id;
100bf686dbeSMatthew Dillon 	cursor.key_beg.key = HAMMER_MIN_KEY;
101bf686dbeSMatthew Dillon 	cursor.key_beg.create_tid = 1;
102bf686dbeSMatthew Dillon 	cursor.key_beg.delete_tid = 0;
103bf686dbeSMatthew Dillon 	cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
104bf686dbeSMatthew Dillon 	cursor.key_beg.obj_type = 0;
105bf686dbeSMatthew Dillon 
106842e7a70SMatthew Dillon 	cursor.key_end.localization = (reblock->key_end.localization &
107842e7a70SMatthew Dillon 					HAMMER_LOCALIZE_MASK) +
108dd94f1b1SMatthew Dillon 				      ip->obj_localization;
109dd94f1b1SMatthew Dillon 	cursor.key_end.obj_id = reblock->key_end.obj_id;
110bf686dbeSMatthew Dillon 	cursor.key_end.key = HAMMER_MAX_KEY;
111bf686dbeSMatthew Dillon 	cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
112bf686dbeSMatthew Dillon 	cursor.key_end.delete_tid = 0;
113bf686dbeSMatthew Dillon 	cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
114bf686dbeSMatthew Dillon 	cursor.key_end.obj_type = 0;
115bf686dbeSMatthew Dillon 
116bf686dbeSMatthew Dillon 	cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
1179480ff55SMatthew Dillon 	cursor.flags |= HAMMER_CURSOR_BACKEND;
11818bee4a2SMatthew Dillon 	cursor.flags |= HAMMER_CURSOR_NOSWAPCACHE;
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 		 */
13307ed04b5SMatthew Dillon 		KKASSERT(cursor.index < cursor.node->ondisk->count);
134bf686dbeSMatthew Dillon 		elm = &cursor.node->ondisk->elms[cursor.index];
135dd94f1b1SMatthew Dillon 		reblock->key_cur.obj_id = elm->base.obj_id;
136dd94f1b1SMatthew Dillon 		reblock->key_cur.localization = elm->base.localization;
137bf686dbeSMatthew Dillon 
1389480ff55SMatthew Dillon 		/*
1399f5097dcSMatthew Dillon 		 * Yield to more important tasks
1409f5097dcSMatthew Dillon 		 */
1419f5097dcSMatthew Dillon 		if ((error = hammer_signal_check(trans->hmp)) != 0)
1429f5097dcSMatthew Dillon 			break;
143a7e9bef1SMatthew Dillon 
144a7e9bef1SMatthew Dillon 		/*
145a7e9bef1SMatthew Dillon 		 * If there is insufficient free space it may be due to
146a7e9bef1SMatthew Dillon 		 * reserved bigblocks, which flushing might fix.
147c9ce54d6SMatthew Dillon 		 *
14807ed04b5SMatthew Dillon 		 * We must force a retest in case the unlocked cursor is
14907ed04b5SMatthew Dillon 		 * moved to the end of the leaf, or moved to an internal
15007ed04b5SMatthew Dillon 		 * node.
15107ed04b5SMatthew Dillon 		 *
152c9ce54d6SMatthew Dillon 		 * WARNING: See warnings in hammer_unlock_cursor() function.
153a7e9bef1SMatthew Dillon 		 */
1547b6ccb11SMatthew Dillon 		if (hammer_checkspace(trans->hmp, slop)) {
155a7e9bef1SMatthew Dillon 			if (++checkspace_count == 10) {
156a7e9bef1SMatthew Dillon 				error = ENOSPC;
157a7e9bef1SMatthew Dillon 				break;
158a7e9bef1SMatthew Dillon 			}
159982be4bfSMatthew Dillon 			hammer_unlock_cursor(&cursor);
16007ed04b5SMatthew Dillon 			cursor.flags |= HAMMER_CURSOR_RETEST;
16193291532SMatthew Dillon 			hammer_flusher_wait(trans->hmp, seq);
162982be4bfSMatthew Dillon 			hammer_lock_cursor(&cursor);
1637a61b85dSMatthew Dillon 			seq = hammer_flusher_async(trans->hmp, NULL);
16407ed04b5SMatthew Dillon 			goto skip;
16593291532SMatthew Dillon 		}
166a7e9bef1SMatthew Dillon 
167a7e9bef1SMatthew Dillon 		/*
1689480ff55SMatthew Dillon 		 * Acquiring the sync_lock prevents the operation from
1699480ff55SMatthew Dillon 		 * crossing a synchronization boundary.
17009ac686bSMatthew Dillon 		 *
17109ac686bSMatthew Dillon 		 * NOTE: cursor.node may have changed on return.
172c9ce54d6SMatthew Dillon 		 *
173c9ce54d6SMatthew Dillon 		 * WARNING: See warnings in hammer_unlock_cursor() function.
1749480ff55SMatthew Dillon 		 */
1752f85fa4dSMatthew Dillon 		hammer_sync_lock_sh(trans);
17636f82b23SMatthew Dillon 		error = hammer_reblock_helper(reblock, &cursor, elm);
1772f85fa4dSMatthew Dillon 		hammer_sync_unlock(trans);
17893291532SMatthew Dillon 
17915e75dabSMatthew Dillon 		while (hammer_flusher_meta_halflimit(trans->hmp) ||
1807a61b85dSMatthew Dillon 		       hammer_flusher_undo_exhausted(trans, 2)) {
181982be4bfSMatthew Dillon 			hammer_unlock_cursor(&cursor);
18293291532SMatthew Dillon 			hammer_flusher_wait(trans->hmp, seq);
183982be4bfSMatthew Dillon 			hammer_lock_cursor(&cursor);
18415e75dabSMatthew Dillon 			seq = hammer_flusher_async_one(trans->hmp);
18593291532SMatthew Dillon 		}
1861b0ab2c3SMatthew Dillon 
1871b0ab2c3SMatthew Dillon 		/*
1881b0ab2c3SMatthew Dillon 		 * Setup for iteration, our cursor flags may be modified by
1891b0ab2c3SMatthew Dillon 		 * other threads while we are unlocked.
1901b0ab2c3SMatthew Dillon 		 */
191bf686dbeSMatthew Dillon 		cursor.flags |= HAMMER_CURSOR_ATEDISK;
1921b0ab2c3SMatthew Dillon 
1931b0ab2c3SMatthew Dillon 		/*
1941b0ab2c3SMatthew Dillon 		 * We allocate data buffers, which atm we don't track
1951b0ab2c3SMatthew Dillon 		 * dirty levels for because we allow the kernel to write
1961b0ab2c3SMatthew Dillon 		 * them.  But if we allocate too many we can still deadlock
1971b0ab2c3SMatthew Dillon 		 * the buffer cache.
1981b0ab2c3SMatthew Dillon 		 *
199c9ce54d6SMatthew Dillon 		 * WARNING: See warnings in hammer_unlock_cursor() function.
2001b0ab2c3SMatthew Dillon 		 *	    (The cursor's node and element may change!)
2011b0ab2c3SMatthew Dillon 		 */
2021b0ab2c3SMatthew Dillon 		if (bd_heatup()) {
203982be4bfSMatthew Dillon 			hammer_unlock_cursor(&cursor);
2041b0ab2c3SMatthew Dillon 			bwillwrite(HAMMER_XBUFSIZE);
205982be4bfSMatthew Dillon 			hammer_lock_cursor(&cursor);
2061b0ab2c3SMatthew Dillon 		}
20755b50bd5SMatthew Dillon 		vm_wait_nominal();
20807ed04b5SMatthew Dillon skip:
2091b0ab2c3SMatthew Dillon 		if (error == 0) {
210bf686dbeSMatthew Dillon 			error = hammer_btree_iterate(&cursor);
211bf686dbeSMatthew Dillon 		}
212bf686dbeSMatthew Dillon 	}
213bf686dbeSMatthew Dillon 	if (error == ENOENT)
214bf686dbeSMatthew Dillon 		error = 0;
215bf686dbeSMatthew Dillon 	hammer_done_cursor(&cursor);
21606ad81ffSMatthew Dillon 	if (error == EWOULDBLOCK) {
21706ad81ffSMatthew Dillon 		hammer_flusher_sync(trans->hmp);
21806ad81ffSMatthew Dillon 		goto retry;
21906ad81ffSMatthew Dillon 	}
220bf686dbeSMatthew Dillon 	if (error == EDEADLK)
221bf686dbeSMatthew Dillon 		goto retry;
22219619882SMatthew Dillon 	if (error == EINTR) {
22319619882SMatthew Dillon 		reblock->head.flags |= HAMMER_IOC_HEAD_INTR;
22419619882SMatthew Dillon 		error = 0;
22519619882SMatthew Dillon 	}
226dd94f1b1SMatthew Dillon failed:
227dd94f1b1SMatthew Dillon 	reblock->key_cur.localization &= HAMMER_LOCALIZE_MASK;
228bf686dbeSMatthew Dillon 	return(error);
229bf686dbeSMatthew Dillon }
230bf686dbeSMatthew Dillon 
231bf686dbeSMatthew Dillon /*
232bf686dbeSMatthew Dillon  * Reblock the B-Tree (leaf) node, record, and/or data if necessary.
233bf686dbeSMatthew Dillon  *
2349480ff55SMatthew Dillon  * XXX We have no visibility into internal B-Tree nodes at the moment,
2359480ff55SMatthew Dillon  * only leaf nodes.
236bf686dbeSMatthew Dillon  */
237bf686dbeSMatthew Dillon static int
23836f82b23SMatthew Dillon hammer_reblock_helper(struct hammer_ioc_reblock *reblock,
239bf686dbeSMatthew Dillon 		      hammer_cursor_t cursor, hammer_btree_elm_t elm)
240bf686dbeSMatthew Dillon {
24143c665aeSMatthew Dillon 	hammer_mount_t hmp;
242bf686dbeSMatthew Dillon 	hammer_off_t tmp_offset;
243ebbcfba9SMatthew Dillon 	hammer_node_ondisk_t ondisk;
24444a83111SMatthew Dillon 	struct hammer_btree_leaf_elm leaf;
245bf686dbeSMatthew Dillon 	int error;
246bf686dbeSMatthew Dillon 	int bytes;
247bf686dbeSMatthew Dillon 	int cur;
248bf3b416bSMatthew Dillon 	int iocflags;
249bf686dbeSMatthew Dillon 
250bf686dbeSMatthew Dillon 	error = 0;
25143c665aeSMatthew Dillon 	hmp = cursor->trans->hmp;
252bf686dbeSMatthew Dillon 
253bf686dbeSMatthew Dillon 	/*
254bf686dbeSMatthew Dillon 	 * Reblock data.  Note that data embedded in a record is reblocked
2552f85fa4dSMatthew Dillon 	 * by the record reblock code.  Data processing only occurs at leaf
2562f85fa4dSMatthew Dillon 	 * nodes and for RECORD element types.
257bf686dbeSMatthew Dillon 	 */
2582f85fa4dSMatthew Dillon 	if (cursor->node->ondisk->type != HAMMER_BTREE_TYPE_LEAF)
2592f85fa4dSMatthew Dillon 		goto skip;
2602f85fa4dSMatthew Dillon 	if (elm->leaf.base.btype != HAMMER_BTREE_TYPE_RECORD)
2612f85fa4dSMatthew Dillon 		return(0);
262bf686dbeSMatthew Dillon 	tmp_offset = elm->leaf.data_offset;
263bf3b416bSMatthew Dillon 	if (tmp_offset == 0)
264bf3b416bSMatthew Dillon 		goto skip;
265bf3b416bSMatthew Dillon 	if (error)
266bf3b416bSMatthew Dillon 		goto skip;
267bf3b416bSMatthew Dillon 
268bf3b416bSMatthew Dillon 	/*
269bf3b416bSMatthew Dillon 	 * NOTE: Localization restrictions may also have been set-up, we can't
270bf3b416bSMatthew Dillon 	 *	 just set the match flags willy-nilly here.
271bf3b416bSMatthew Dillon 	 */
272bf3b416bSMatthew Dillon 	switch(elm->leaf.base.rec_type) {
273bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_INODE:
27483f2a3aaSMatthew Dillon 	case HAMMER_RECTYPE_SNAPSHOT:
27583f2a3aaSMatthew Dillon 	case HAMMER_RECTYPE_CONFIG:
276bf3b416bSMatthew Dillon 		iocflags = HAMMER_IOC_DO_INODES;
277bf3b416bSMatthew Dillon 		break;
278bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_EXT:
279bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_FIX:
280ea434b6fSMatthew Dillon 	case HAMMER_RECTYPE_PFS:
281bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_DIRENTRY:
282bf3b416bSMatthew Dillon 		iocflags = HAMMER_IOC_DO_DIRS;
283bf3b416bSMatthew Dillon 		break;
284bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_DATA:
285bf3b416bSMatthew Dillon 	case HAMMER_RECTYPE_DB:
286bf3b416bSMatthew Dillon 		iocflags = HAMMER_IOC_DO_DATA;
287bf3b416bSMatthew Dillon 		break;
288bf3b416bSMatthew Dillon 	default:
289bf3b416bSMatthew Dillon 		iocflags = 0;
290bf3b416bSMatthew Dillon 		break;
291bf3b416bSMatthew Dillon 	}
292bf3b416bSMatthew Dillon 	if (reblock->head.flags & iocflags) {
293bf686dbeSMatthew Dillon 		++reblock->data_count;
294bf686dbeSMatthew Dillon 		reblock->data_byte_count += elm->leaf.data_len;
29543c665aeSMatthew Dillon 		bytes = hammer_blockmap_getfree(hmp, tmp_offset, &cur, &error);
2966e1e8b6dSMatthew Dillon 		if (hammer_debug_general & 0x4000)
2972f85fa4dSMatthew Dillon 			kprintf("D %6d/%d\n", bytes, reblock->free_level);
298bf3b416bSMatthew Dillon 		if (error == 0 && (cur == 0 || reblock->free_level == 0) &&
299bf3b416bSMatthew Dillon 		    bytes >= reblock->free_level) {
30044a83111SMatthew Dillon 			/*
30144a83111SMatthew Dillon 			 * This is nasty, the uncache code may have to get
30244a83111SMatthew Dillon 			 * vnode locks and because of that we can't hold
30344a83111SMatthew Dillon 			 * the cursor locked.
304c9ce54d6SMatthew Dillon 			 *
305c9ce54d6SMatthew Dillon 			 * WARNING: See warnings in hammer_unlock_cursor()
306c9ce54d6SMatthew Dillon 			 *	    function.
30744a83111SMatthew Dillon 			 */
30844a83111SMatthew Dillon 			leaf = elm->leaf;
309982be4bfSMatthew Dillon 			hammer_unlock_cursor(cursor);
31044a83111SMatthew Dillon 			hammer_io_direct_uncache(hmp, &leaf);
311982be4bfSMatthew Dillon 			hammer_lock_cursor(cursor);
312ebbcfba9SMatthew Dillon 
313ebbcfba9SMatthew Dillon 			/*
314ebbcfba9SMatthew Dillon 			 * elm may have become stale or invalid, reload it.
315ebbcfba9SMatthew Dillon 			 * ondisk variable is temporary only.  Note that
316ebbcfba9SMatthew Dillon 			 * cursor->node and thus cursor->node->ondisk may
317ebbcfba9SMatthew Dillon 			 * also changed.
318ebbcfba9SMatthew Dillon 			 */
319ebbcfba9SMatthew Dillon 			ondisk = cursor->node->ondisk;
320ebbcfba9SMatthew Dillon 			elm = &ondisk->elms[cursor->index];
32144a83111SMatthew Dillon 			if (cursor->flags & HAMMER_CURSOR_RETEST) {
322ebbcfba9SMatthew Dillon 				kprintf("hammer: debug: retest on "
323ebbcfba9SMatthew Dillon 					"reblocker uncache\n");
32444a83111SMatthew Dillon 				error = EDEADLK;
325ebbcfba9SMatthew Dillon 			} else if (ondisk->type != HAMMER_BTREE_TYPE_LEAF ||
326ebbcfba9SMatthew Dillon 				   cursor->index >= ondisk->count) {
327ebbcfba9SMatthew Dillon 				kprintf("hammer: debug: shifted on "
328ebbcfba9SMatthew Dillon 					"reblocker uncache\n");
329ebbcfba9SMatthew Dillon 				error = EDEADLK;
330ebbcfba9SMatthew Dillon 			} else if (bcmp(&elm->leaf, &leaf, sizeof(leaf))) {
331ebbcfba9SMatthew Dillon 				kprintf("hammer: debug: changed on "
332ebbcfba9SMatthew Dillon 					"reblocker uncache\n");
333ebbcfba9SMatthew Dillon 				error = EDEADLK;
33444a83111SMatthew Dillon 			}
33544a83111SMatthew Dillon 			if (error == 0)
336bf686dbeSMatthew Dillon 				error = hammer_cursor_upgrade(cursor);
337bf686dbeSMatthew Dillon 			if (error == 0) {
33807ed04b5SMatthew Dillon 				KKASSERT(cursor->index < ondisk->count);
33936f82b23SMatthew Dillon 				error = hammer_reblock_data(reblock,
340bf686dbeSMatthew Dillon 							    cursor, elm);
341bf686dbeSMatthew Dillon 			}
342bf686dbeSMatthew Dillon 			if (error == 0) {
343bf686dbeSMatthew Dillon 				++reblock->data_moves;
344bf686dbeSMatthew Dillon 				reblock->data_byte_moves += elm->leaf.data_len;
345bf686dbeSMatthew Dillon 			}
346bf686dbeSMatthew Dillon 		}
347bf686dbeSMatthew Dillon 	}
348bf686dbeSMatthew Dillon 
3492f85fa4dSMatthew Dillon skip:
350bf686dbeSMatthew Dillon 	/*
3511775b6a0SMatthew Dillon 	 * Reblock a B-Tree internal or leaf node.  A leaf node is reblocked
3521775b6a0SMatthew Dillon 	 * on initial entry only (element 0).  An internal node is reblocked
3531775b6a0SMatthew Dillon 	 * when entered upward from its first leaf node only (also element 0).
3541775b6a0SMatthew Dillon 	 * Further revisits of the internal node (index > 0) are ignored.
355bf686dbeSMatthew Dillon 	 */
356bf686dbeSMatthew Dillon 	tmp_offset = cursor->node->node_offset;
357bf3b416bSMatthew Dillon 	if (cursor->index == 0 &&
358814387f6SMatthew Dillon 	    error == 0 && (reblock->head.flags & HAMMER_IOC_DO_BTREE)) {
359bf686dbeSMatthew Dillon 		++reblock->btree_count;
36043c665aeSMatthew Dillon 		bytes = hammer_blockmap_getfree(hmp, tmp_offset, &cur, &error);
3616e1e8b6dSMatthew Dillon 		if (hammer_debug_general & 0x4000)
3622f85fa4dSMatthew Dillon 			kprintf("B %6d/%d\n", bytes, reblock->free_level);
363bf3b416bSMatthew Dillon 		if (error == 0 && (cur == 0 || reblock->free_level == 0) &&
364bf3b416bSMatthew Dillon 		    bytes >= reblock->free_level) {
365bf686dbeSMatthew Dillon 			error = hammer_cursor_upgrade(cursor);
366bf686dbeSMatthew Dillon 			if (error == 0) {
36707ed04b5SMatthew Dillon 				if (cursor->parent) {
36807ed04b5SMatthew Dillon 					KKASSERT(cursor->parent_index <
36907ed04b5SMatthew Dillon 						 cursor->parent->ondisk->count);
370bf686dbeSMatthew Dillon 					elm = &cursor->parent->ondisk->elms[cursor->parent_index];
37107ed04b5SMatthew Dillon 				} else {
372bf686dbeSMatthew Dillon 					elm = NULL;
37307ed04b5SMatthew Dillon 				}
3742f85fa4dSMatthew Dillon 				switch(cursor->node->ondisk->type) {
3752f85fa4dSMatthew Dillon 				case HAMMER_BTREE_TYPE_LEAF:
3762f85fa4dSMatthew Dillon 					error = hammer_reblock_leaf_node(
3772f85fa4dSMatthew Dillon 							reblock, cursor, elm);
3782f85fa4dSMatthew Dillon 					break;
3792f85fa4dSMatthew Dillon 				case HAMMER_BTREE_TYPE_INTERNAL:
3802f85fa4dSMatthew Dillon 					error = hammer_reblock_int_node(
3812f85fa4dSMatthew Dillon 							reblock, cursor, elm);
3822f85fa4dSMatthew Dillon 					break;
3832f85fa4dSMatthew Dillon 				default:
3842f85fa4dSMatthew Dillon 					panic("Illegal B-Tree node type");
3852f85fa4dSMatthew Dillon 				}
386bf686dbeSMatthew Dillon 			}
387bf686dbeSMatthew Dillon 			if (error == 0) {
388bf686dbeSMatthew Dillon 				++reblock->btree_moves;
389bf686dbeSMatthew Dillon 			}
390bf686dbeSMatthew Dillon 		}
391bf686dbeSMatthew Dillon 	}
392bf686dbeSMatthew Dillon 
393bf686dbeSMatthew Dillon 	hammer_cursor_downgrade(cursor);
394bf686dbeSMatthew Dillon 	return(error);
395bf686dbeSMatthew Dillon }
396bf686dbeSMatthew Dillon 
397bf686dbeSMatthew Dillon /*
398bf686dbeSMatthew Dillon  * Reblock a record's data.  Both the B-Tree element and record pointers
399bf686dbeSMatthew Dillon  * to the data must be adjusted.
400bf686dbeSMatthew Dillon  */
401bf686dbeSMatthew Dillon static int
40236f82b23SMatthew Dillon hammer_reblock_data(struct hammer_ioc_reblock *reblock,
403bf686dbeSMatthew Dillon 		    hammer_cursor_t cursor, hammer_btree_elm_t elm)
404bf686dbeSMatthew Dillon {
405bf686dbeSMatthew Dillon 	struct hammer_buffer *data_buffer = NULL;
406bf686dbeSMatthew Dillon 	hammer_off_t ndata_offset;
407bf686dbeSMatthew Dillon 	int error;
408bf686dbeSMatthew Dillon 	void *ndata;
409bf686dbeSMatthew Dillon 
410bf686dbeSMatthew Dillon 	error = hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA |
41111ad5adeSMatthew Dillon 					     HAMMER_CURSOR_GET_LEAF);
412bf686dbeSMatthew Dillon 	if (error)
413bf686dbeSMatthew Dillon 		return (error);
41436f82b23SMatthew Dillon 	ndata = hammer_alloc_data(cursor->trans, elm->leaf.data_len,
415bf3b416bSMatthew Dillon 				  elm->leaf.base.rec_type,
416df2ccbacSMatthew Dillon 				  &ndata_offset, &data_buffer,
417df2ccbacSMatthew Dillon 				  0, &error);
418bf686dbeSMatthew Dillon 	if (error)
419bf686dbeSMatthew Dillon 		goto done;
420b8a41159SMatthew Dillon 	hammer_io_notmeta(data_buffer);
421bf686dbeSMatthew Dillon 
422bf686dbeSMatthew Dillon 	/*
423b9107f58SMatthew Dillon 	 * Move the data.  Note that we must invalidate any cached
424b9107f58SMatthew Dillon 	 * data buffer in the cursor before calling blockmap_free.
425e04ee2deSTomohiro Kusumi 	 * The blockmap_free may free up the entire big-block and
426b9107f58SMatthew Dillon 	 * will not be able to invalidate it if the cursor is holding
427e04ee2deSTomohiro Kusumi 	 * a data buffer cached in that big block.
428bf686dbeSMatthew Dillon 	 */
42910a5d1baSMatthew Dillon 	hammer_modify_buffer(cursor->trans, data_buffer, NULL, 0);
430bf686dbeSMatthew Dillon 	bcopy(cursor->data, ndata, elm->leaf.data_len);
43110a5d1baSMatthew Dillon 	hammer_modify_buffer_done(data_buffer);
432b9107f58SMatthew Dillon 	hammer_cursor_invalidate_cache(cursor);
433bf686dbeSMatthew Dillon 
43436f82b23SMatthew Dillon 	hammer_blockmap_free(cursor->trans,
43536f82b23SMatthew Dillon 			     elm->leaf.data_offset, elm->leaf.data_len);
436bf686dbeSMatthew Dillon 
43710a5d1baSMatthew Dillon 	hammer_modify_node(cursor->trans, cursor->node,
43810a5d1baSMatthew Dillon 			   &elm->leaf.data_offset, sizeof(hammer_off_t));
439bf686dbeSMatthew Dillon 	elm->leaf.data_offset = ndata_offset;
44010a5d1baSMatthew Dillon 	hammer_modify_node_done(cursor->node);
441bf686dbeSMatthew Dillon 
442bf686dbeSMatthew Dillon done:
443bf686dbeSMatthew Dillon 	if (data_buffer)
444bf686dbeSMatthew Dillon 		hammer_rel_buffer(data_buffer, 0);
445bf686dbeSMatthew Dillon 	return (error);
446bf686dbeSMatthew Dillon }
447bf686dbeSMatthew Dillon 
448bf686dbeSMatthew Dillon /*
4492f85fa4dSMatthew Dillon  * Reblock a B-Tree leaf node.  The parent must be adjusted to point to
4502f85fa4dSMatthew Dillon  * the new copy of the leaf node.
451bf686dbeSMatthew Dillon  *
4522f85fa4dSMatthew Dillon  * elm is a pointer to the parent element pointing at cursor.node.
453bf686dbeSMatthew Dillon  */
454bf686dbeSMatthew Dillon static int
4552f85fa4dSMatthew Dillon hammer_reblock_leaf_node(struct hammer_ioc_reblock *reblock,
456bf686dbeSMatthew Dillon 			 hammer_cursor_t cursor, hammer_btree_elm_t elm)
457bf686dbeSMatthew Dillon {
458bf686dbeSMatthew Dillon 	hammer_node_t onode;
459bf686dbeSMatthew Dillon 	hammer_node_t nnode;
460bf686dbeSMatthew Dillon 	int error;
461bf686dbeSMatthew Dillon 
462df2ccbacSMatthew Dillon 	/*
463df2ccbacSMatthew Dillon 	 * Don't supply a hint when allocating the leaf.  Fills are done
464df2ccbacSMatthew Dillon 	 * from the leaf upwards.
465df2ccbacSMatthew Dillon 	 */
466bf686dbeSMatthew Dillon 	onode = cursor->node;
467df2ccbacSMatthew Dillon 	nnode = hammer_alloc_btree(cursor->trans, 0, &error);
4688d0efe43SMatthew Dillon 
469bf686dbeSMatthew Dillon 	if (nnode == NULL)
470bf686dbeSMatthew Dillon 		return (error);
471bf686dbeSMatthew Dillon 
472bf686dbeSMatthew Dillon 	/*
473bf686dbeSMatthew Dillon 	 * Move the node
474bf686dbeSMatthew Dillon 	 */
47509ac686bSMatthew Dillon 	hammer_lock_ex(&nnode->lock);
47609ac686bSMatthew Dillon 	hammer_modify_node_noundo(cursor->trans, nnode);
477bf686dbeSMatthew Dillon 	bcopy(onode->ondisk, nnode->ondisk, sizeof(*nnode->ondisk));
478bf686dbeSMatthew Dillon 
479bf686dbeSMatthew Dillon 	if (elm) {
480bf686dbeSMatthew Dillon 		/*
481bf686dbeSMatthew Dillon 		 * We are not the root of the B-Tree
482bf686dbeSMatthew Dillon 		 */
48336f82b23SMatthew Dillon 		hammer_modify_node(cursor->trans, cursor->parent,
484bf686dbeSMatthew Dillon 				   &elm->internal.subtree_offset,
485bf686dbeSMatthew Dillon 				   sizeof(elm->internal.subtree_offset));
486bf686dbeSMatthew Dillon 		elm->internal.subtree_offset = nnode->node_offset;
48710a5d1baSMatthew Dillon 		hammer_modify_node_done(cursor->parent);
488bf686dbeSMatthew Dillon 	} else {
489bf686dbeSMatthew Dillon 		/*
490bf686dbeSMatthew Dillon 		 * We are the root of the B-Tree
491bf686dbeSMatthew Dillon 		 */
492bf686dbeSMatthew Dillon                 hammer_volume_t volume;
493bf686dbeSMatthew Dillon 
49436f82b23SMatthew Dillon                 volume = hammer_get_root_volume(cursor->trans->hmp, &error);
495bf686dbeSMatthew Dillon                 KKASSERT(error == 0);
496bf686dbeSMatthew Dillon 
497e8599db1SMatthew Dillon                 hammer_modify_volume_field(cursor->trans, volume,
498e8599db1SMatthew Dillon 					   vol0_btree_root);
499bf686dbeSMatthew Dillon                 volume->ondisk->vol0_btree_root = nnode->node_offset;
50010a5d1baSMatthew Dillon                 hammer_modify_volume_done(volume);
501bf686dbeSMatthew Dillon                 hammer_rel_volume(volume, 0);
502bf686dbeSMatthew Dillon         }
503bf686dbeSMatthew Dillon 
504b3bad96fSMatthew Dillon 	hammer_cursor_replaced_node(onode, nnode);
50536f82b23SMatthew Dillon 	hammer_delete_node(cursor->trans, onode);
506bf686dbeSMatthew Dillon 
507b58c6388SMatthew Dillon 	if (hammer_debug_general & 0x4000) {
5082f85fa4dSMatthew Dillon 		kprintf("REBLOCK LNODE %016llx -> %016llx\n",
509973c11b9SMatthew Dillon 			(long long)onode->node_offset,
510973c11b9SMatthew Dillon 			(long long)nnode->node_offset);
511b58c6388SMatthew Dillon 	}
5128d0efe43SMatthew Dillon 	hammer_modify_node_done(nnode);
513bf686dbeSMatthew Dillon 	cursor->node = nnode;
51409ac686bSMatthew Dillon 
51509ac686bSMatthew Dillon 	hammer_unlock(&onode->lock);
516bf686dbeSMatthew Dillon 	hammer_rel_node(onode);
517bf686dbeSMatthew Dillon 
518bf686dbeSMatthew Dillon 	return (error);
519bf686dbeSMatthew Dillon }
520bf686dbeSMatthew Dillon 
5212f85fa4dSMatthew Dillon /*
5222f85fa4dSMatthew Dillon  * Reblock a B-Tree internal node.  The parent must be adjusted to point to
5232f85fa4dSMatthew Dillon  * the new copy of the internal node, and the node's children's parent
5242f85fa4dSMatthew Dillon  * pointers must also be adjusted to point to the new copy.
5252f85fa4dSMatthew Dillon  *
5262f85fa4dSMatthew Dillon  * elm is a pointer to the parent element pointing at cursor.node.
5272f85fa4dSMatthew Dillon  */
5282f85fa4dSMatthew Dillon static int
5292f85fa4dSMatthew Dillon hammer_reblock_int_node(struct hammer_ioc_reblock *reblock,
5302f85fa4dSMatthew Dillon 			 hammer_cursor_t cursor, hammer_btree_elm_t elm)
5312f85fa4dSMatthew Dillon {
5321775b6a0SMatthew Dillon 	struct hammer_node_lock lockroot;
5332f85fa4dSMatthew Dillon 	hammer_node_t onode;
5342f85fa4dSMatthew Dillon 	hammer_node_t nnode;
5352f85fa4dSMatthew Dillon 	int error;
5362f85fa4dSMatthew Dillon 	int i;
5372f85fa4dSMatthew Dillon 
5381775b6a0SMatthew Dillon 	hammer_node_lock_init(&lockroot, cursor->node);
53924cf83d2SMatthew Dillon 	error = hammer_btree_lock_children(cursor, 1, &lockroot, NULL);
5402f85fa4dSMatthew Dillon 	if (error)
5412f85fa4dSMatthew Dillon 		goto done;
5422f85fa4dSMatthew Dillon 
5432f85fa4dSMatthew Dillon 	onode = cursor->node;
544b4f86ea3SMatthew Dillon 	nnode = hammer_alloc_btree(cursor->trans, 0, &error);
5452f85fa4dSMatthew Dillon 
5462f85fa4dSMatthew Dillon 	if (nnode == NULL)
5472f85fa4dSMatthew Dillon 		goto done;
5482f85fa4dSMatthew Dillon 
5492f85fa4dSMatthew Dillon 	/*
5502f85fa4dSMatthew Dillon 	 * Move the node.  Adjust the parent's pointer to us first.
5512f85fa4dSMatthew Dillon 	 */
5522f85fa4dSMatthew Dillon 	hammer_lock_ex(&nnode->lock);
5532f85fa4dSMatthew Dillon 	hammer_modify_node_noundo(cursor->trans, nnode);
5542f85fa4dSMatthew Dillon 	bcopy(onode->ondisk, nnode->ondisk, sizeof(*nnode->ondisk));
5552f85fa4dSMatthew Dillon 
5562f85fa4dSMatthew Dillon 	if (elm) {
5572f85fa4dSMatthew Dillon 		/*
5582f85fa4dSMatthew Dillon 		 * We are not the root of the B-Tree
5592f85fa4dSMatthew Dillon 		 */
5602f85fa4dSMatthew Dillon 		hammer_modify_node(cursor->trans, cursor->parent,
5612f85fa4dSMatthew Dillon 				   &elm->internal.subtree_offset,
5622f85fa4dSMatthew Dillon 				   sizeof(elm->internal.subtree_offset));
5632f85fa4dSMatthew Dillon 		elm->internal.subtree_offset = nnode->node_offset;
5642f85fa4dSMatthew Dillon 		hammer_modify_node_done(cursor->parent);
5652f85fa4dSMatthew Dillon 	} else {
5662f85fa4dSMatthew Dillon 		/*
5672f85fa4dSMatthew Dillon 		 * We are the root of the B-Tree
5682f85fa4dSMatthew Dillon 		 */
5692f85fa4dSMatthew Dillon                 hammer_volume_t volume;
5702f85fa4dSMatthew Dillon 
5712f85fa4dSMatthew Dillon                 volume = hammer_get_root_volume(cursor->trans->hmp, &error);
5722f85fa4dSMatthew Dillon                 KKASSERT(error == 0);
5732f85fa4dSMatthew Dillon 
5742f85fa4dSMatthew Dillon                 hammer_modify_volume_field(cursor->trans, volume,
5752f85fa4dSMatthew Dillon 					   vol0_btree_root);
5762f85fa4dSMatthew Dillon                 volume->ondisk->vol0_btree_root = nnode->node_offset;
5772f85fa4dSMatthew Dillon                 hammer_modify_volume_done(volume);
5782f85fa4dSMatthew Dillon                 hammer_rel_volume(volume, 0);
5792f85fa4dSMatthew Dillon         }
5802f85fa4dSMatthew Dillon 
5812f85fa4dSMatthew Dillon 	/*
5822f85fa4dSMatthew Dillon 	 * Now adjust our children's pointers to us.
5832f85fa4dSMatthew Dillon 	 */
5842f85fa4dSMatthew Dillon 	for (i = 0; i < nnode->ondisk->count; ++i) {
5852f85fa4dSMatthew Dillon 		elm = &nnode->ondisk->elms[i];
5862f85fa4dSMatthew Dillon 		error = btree_set_parent(cursor->trans, nnode, elm);
5872f85fa4dSMatthew Dillon 		if (error)
5882f85fa4dSMatthew Dillon 			panic("reblock internal node: fixup problem");
5892f85fa4dSMatthew Dillon 	}
5902f85fa4dSMatthew Dillon 
5912f85fa4dSMatthew Dillon 	/*
5922f85fa4dSMatthew Dillon 	 * Clean up.
5932f85fa4dSMatthew Dillon 	 *
5942f85fa4dSMatthew Dillon 	 * The new node replaces the current node in the cursor.  The cursor
5952f85fa4dSMatthew Dillon 	 * expects it to be locked so leave it locked.  Discard onode.
5962f85fa4dSMatthew Dillon 	 */
597b3bad96fSMatthew Dillon 	hammer_cursor_replaced_node(onode, nnode);
5982f85fa4dSMatthew Dillon 	hammer_delete_node(cursor->trans, onode);
5992f85fa4dSMatthew Dillon 
6002f85fa4dSMatthew Dillon 	if (hammer_debug_general & 0x4000) {
6012f85fa4dSMatthew Dillon 		kprintf("REBLOCK INODE %016llx -> %016llx\n",
602973c11b9SMatthew Dillon 			(long long)onode->node_offset,
603973c11b9SMatthew Dillon 			(long long)nnode->node_offset);
6042f85fa4dSMatthew Dillon 	}
6052f85fa4dSMatthew Dillon 	hammer_modify_node_done(nnode);
6062f85fa4dSMatthew Dillon 	cursor->node = nnode;
6072f85fa4dSMatthew Dillon 
6082f85fa4dSMatthew Dillon 	hammer_unlock(&onode->lock);
6092f85fa4dSMatthew Dillon 	hammer_rel_node(onode);
6102f85fa4dSMatthew Dillon 
6112f85fa4dSMatthew Dillon done:
61224cf83d2SMatthew Dillon 	hammer_btree_unlock_children(cursor->trans->hmp, &lockroot, NULL);
6132f85fa4dSMatthew Dillon 	return (error);
6142f85fa4dSMatthew Dillon }
6152f85fa4dSMatthew Dillon 
616