2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in
15 * the documentation and/or other materials provided with the
17 * 3. Neither the name of The DragonFly Project nor the names of its
18 * contributors may be used to endorse or promote products derived
19 * from this software without specific, prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
26 * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING,
27 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
28 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
29 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
30 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
31 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * $DragonFly: src/sys/vfs/hammer/hammer_prune.c,v 1.1 2008/05/12 21:17:18 dillon Exp $
40 * Iterate through the specified range of object ids and remove any
41 * deleted records that fall entirely within a prune modulo.
43 * A reverse iteration is used to prevent overlapping records from being
44 * created during the iteration due to alignments. This also allows us
45 * to adjust alignments without blowing up the B-Tree.
47 static int check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm,
48 int *realign_cre, int *realign_del);
49 static int realign_prune(struct hammer_ioc_prune *prune, hammer_cursor_t cursor,
50 int realign_cre, int realign_del);
53 hammer_ioc_prune(hammer_transaction_t trans, hammer_inode_t ip,
54 struct hammer_ioc_prune *prune)
56 struct hammer_cursor cursor;
57 hammer_btree_elm_t elm;
63 if (prune->nelms < 0 || prune->nelms > HAMMER_MAX_PRUNE_ELMS)
65 if (prune->beg_obj_id >= prune->end_obj_id)
67 if ((prune->head.flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
70 prune->cur_obj_id = prune->end_obj_id;
71 prune->cur_key = HAMMER_MAX_KEY;
74 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
76 hammer_done_cursor(&cursor);
79 cursor.key_beg.obj_id = prune->beg_obj_id;
80 cursor.key_beg.key = HAMMER_MIN_KEY;
81 cursor.key_beg.create_tid = 1;
82 cursor.key_beg.delete_tid = 0;
83 cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
84 cursor.key_beg.obj_type = 0;
86 cursor.key_end.obj_id = prune->cur_obj_id;
87 cursor.key_end.key = prune->cur_key;
88 cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
89 cursor.key_end.delete_tid = 0;
90 cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
91 cursor.key_end.obj_type = 0;
93 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
94 cursor.flags |= HAMMER_CURSOR_BACKEND;
97 * This flag allows the B-Tree code to clean up loose ends while
100 cursor.flags |= HAMMER_CURSOR_PRUNING;
102 error = hammer_btree_last(&cursor);
104 elm = &cursor.node->ondisk->elms[cursor.index];
105 prune->cur_obj_id = elm->base.obj_id;
106 prune->cur_key = elm->base.key;
108 if (prune->stat_oldest_tid > elm->leaf.base.create_tid)
109 prune->stat_oldest_tid = elm->leaf.base.create_tid;
111 if (check_prune(prune, elm, &realign_cre, &realign_del) == 0) {
112 if (hammer_debug_general & 0x0200) {
113 kprintf("check %016llx %016llx: DELETE\n",
114 elm->base.obj_id, elm->base.key);
118 * NOTE: This can return EDEADLK
120 * Acquiring the sync lock guarantees that the
121 * operation will not cross a synchronization
122 * boundary (see the flusher).
124 isdir = (elm->base.rec_type == HAMMER_RECTYPE_DIRENTRY);
126 hammer_lock_ex(&trans->hmp->sync_lock);
127 error = hammer_delete_at_cursor(&cursor,
129 hammer_unlock(&trans->hmp->sync_lock);
134 ++prune->stat_dirrecords;
136 ++prune->stat_rawrecords;
139 * The current record might now be the one after
140 * the one we deleted, set ATEDISK to force us
141 * to skip it (since we are iterating backwards).
143 cursor.flags |= HAMMER_CURSOR_ATEDISK;
144 } else if (realign_cre >= 0 || realign_del >= 0) {
145 hammer_lock_ex(&trans->hmp->sync_lock);
146 error = realign_prune(prune, &cursor,
147 realign_cre, realign_del);
148 hammer_unlock(&trans->hmp->sync_lock);
150 cursor.flags |= HAMMER_CURSOR_ATEDISK;
151 if (hammer_debug_general & 0x0200) {
152 kprintf("check %016llx %016llx: "
159 cursor.flags |= HAMMER_CURSOR_ATEDISK;
160 if (hammer_debug_general & 0x0100) {
161 kprintf("check %016llx %016llx: SKIP\n",
162 elm->base.obj_id, elm->base.key);
165 ++prune->stat_scanrecords;
168 * Bad hack for now, don't blow out the kernel's buffer
169 * cache. NOTE: We still hold locks on the cursor, we
170 * cannot call the flusher synchronously.
172 if (trans->hmp->locked_dirty_count > hammer_limit_dirtybufs) {
173 hammer_flusher_async(trans->hmp);
174 tsleep(trans, 0, "hmrslo", hz / 10);
176 error = hammer_signal_check(trans->hmp);
178 error = hammer_btree_iterate_reverse(&cursor);
182 hammer_done_cursor(&cursor);
183 if (error == EDEADLK)
185 if (error == EINTR) {
186 prune->head.flags |= HAMMER_IOC_HEAD_INTR;
193 * Check pruning list. The list must be sorted in descending order.
196 check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm,
197 int *realign_cre, int *realign_del)
199 struct hammer_ioc_prune_elm *scan;
206 * If pruning everything remove all records with a non-zero
209 if (prune->head.flags & HAMMER_IOC_PRUNE_ALL) {
210 if (elm->base.delete_tid != 0)
215 for (i = 0; i < prune->nelms; ++i) {
216 scan = &prune->elms[i];
219 * Locate the scan index covering the create and delete TIDs.
221 if (*realign_cre < 0 &&
222 elm->base.create_tid >= scan->beg_tid &&
223 elm->base.create_tid < scan->end_tid) {
226 if (*realign_del < 0 && elm->base.delete_tid &&
227 elm->base.delete_tid > scan->beg_tid &&
228 elm->base.delete_tid <= scan->end_tid) {
233 * Now check for loop termination.
235 if (elm->base.create_tid >= scan->end_tid ||
236 elm->base.delete_tid > scan->end_tid) {
241 * Now determine if we can delete the record.
243 if (elm->base.delete_tid &&
244 elm->base.create_tid >= scan->beg_tid &&
245 elm->base.delete_tid <= scan->end_tid &&
246 elm->base.create_tid / scan->mod_tid ==
247 elm->base.delete_tid / scan->mod_tid) {
255 * Align the record to cover any gaps created through the deletion of
256 * records within the pruning space. If we were to just delete the records
257 * there would be gaps which in turn would cause a snapshot that is NOT on
258 * a pruning boundary to appear corrupt to the user. Forcing alignment
259 * of the create_tid and delete_tid for retained records 'reconnects'
260 * the previously contiguous space, making it contiguous again after the
263 * The use of a reverse iteration allows us to safely align the records and
264 * related elements without creating temporary overlaps. XXX we should
265 * add ordering dependancies for record buffers to guarantee consistency
269 realign_prune(struct hammer_ioc_prune *prune,
270 hammer_cursor_t cursor, int realign_cre, int realign_del)
272 hammer_btree_elm_t elm;
278 hammer_cursor_downgrade(cursor);
280 elm = &cursor->node->ondisk->elms[cursor->index];
281 ++prune->stat_realignments;
284 * Align the create_tid. By doing a reverse iteration we guarantee
285 * that all records after our current record have already been
286 * aligned, allowing us to safely correct the right-hand-boundary
287 * (because no record to our right if otherwise exactly matching
288 * will have a create_tid to the left of our aligned create_tid).
290 * Ordering is important here XXX but disk write ordering for
291 * inter-cluster corrections is not currently guaranteed.
294 if (realign_cre >= 0) {
295 mod = prune->elms[realign_cre].mod_tid;
296 delta = elm->leaf.base.create_tid % mod;
298 tid = elm->leaf.base.create_tid - delta + mod;
301 error = hammer_btree_correct_rhb(cursor, tid + 1);
303 error = hammer_btree_extract(cursor,
304 HAMMER_CURSOR_GET_LEAF);
308 error = hammer_cursor_upgrade(cursor);
311 hammer_modify_node(cursor->trans, cursor->node,
312 &elm->leaf.base.create_tid,
313 sizeof(elm->leaf.base.create_tid));
314 elm->leaf.base.create_tid = tid;
315 hammer_modify_node_done(cursor->node);
321 * Align the delete_tid. This only occurs if the record is historical
322 * was deleted at some point. Realigning the delete_tid does not
323 * move the record within the B-Tree but may cause it to temporarily
324 * overlap a record that has not yet been pruned.
326 if (error == 0 && realign_del >= 0) {
327 mod = prune->elms[realign_del].mod_tid;
328 delta = elm->leaf.base.delete_tid % mod;
330 error = hammer_btree_extract(cursor,
331 HAMMER_CURSOR_GET_LEAF);
333 hammer_modify_node(cursor->trans, cursor->node,
334 &elm->leaf.base.delete_tid,
335 sizeof(elm->leaf.base.delete_tid));
336 elm->leaf.base.delete_tid =
337 elm->leaf.base.delete_tid -
339 hammer_modify_node_done(cursor->node);