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.3 2008/05/31 18:37:57 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;
58 struct hammer_ioc_prune_elm *copy_elms;
59 struct hammer_ioc_prune_elm *user_elms;
66 if (prune->nelms < 0 || prune->nelms > HAMMER_MAX_PRUNE_ELMS)
68 if (prune->beg_obj_id >= prune->end_obj_id)
70 if ((prune->head.flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
73 prune->cur_localization = prune->end_localization;
74 prune->cur_obj_id = prune->end_obj_id;
75 prune->cur_key = HAMMER_MAX_KEY;
78 * Copy element array from userland
80 elm_array_size = sizeof(*copy_elms) * prune->nelms;
81 user_elms = prune->elms;
82 copy_elms = kmalloc(elm_array_size, M_TEMP, M_WAITOK);
83 if ((error = copyin(user_elms, copy_elms, elm_array_size)) != 0)
85 prune->elms = copy_elms;
88 * Scan backwards. Retries typically occur if a deadlock is detected.
91 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
93 hammer_done_cursor(&cursor);
96 cursor.key_beg.localization = prune->beg_localization;
97 cursor.key_beg.obj_id = prune->beg_obj_id;
98 cursor.key_beg.key = HAMMER_MIN_KEY;
99 cursor.key_beg.create_tid = 1;
100 cursor.key_beg.delete_tid = 0;
101 cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
102 cursor.key_beg.obj_type = 0;
104 cursor.key_end.localization = prune->cur_localization;
105 cursor.key_end.obj_id = prune->cur_obj_id;
106 cursor.key_end.key = prune->cur_key;
107 cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
108 cursor.key_end.delete_tid = 0;
109 cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
110 cursor.key_end.obj_type = 0;
112 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
113 cursor.flags |= HAMMER_CURSOR_BACKEND;
116 * This flag allows the B-Tree code to clean up loose ends.
118 cursor.flags |= HAMMER_CURSOR_PRUNING;
120 hammer_sync_lock_sh(trans);
121 error = hammer_btree_last(&cursor);
124 * Yield to more important tasks
126 if (trans->hmp->sync_lock.wanted) {
127 hammer_sync_unlock(trans);
128 tsleep(trans, 0, "hmrslo", hz / 10);
129 hammer_sync_lock_sh(trans);
135 elm = &cursor.node->ondisk->elms[cursor.index];
136 prune->cur_localization = elm->base.localization;
137 prune->cur_obj_id = elm->base.obj_id;
138 prune->cur_key = elm->base.key;
140 if (prune->stat_oldest_tid > elm->leaf.base.create_tid)
141 prune->stat_oldest_tid = elm->leaf.base.create_tid;
143 if (check_prune(prune, elm, &realign_cre, &realign_del) == 0) {
144 if (hammer_debug_general & 0x0200) {
145 kprintf("check %016llx %016llx: DELETE\n",
146 elm->base.obj_id, elm->base.key);
150 * NOTE: This can return EDEADLK
152 * Acquiring the sync lock guarantees that the
153 * operation will not cross a synchronization
154 * boundary (see the flusher).
156 isdir = (elm->base.rec_type == HAMMER_RECTYPE_DIRENTRY);
158 error = hammer_delete_at_cursor(&cursor,
164 ++prune->stat_dirrecords;
166 ++prune->stat_rawrecords;
169 * The current record might now be the one after
170 * the one we deleted, set ATEDISK to force us
171 * to skip it (since we are iterating backwards).
173 cursor.flags |= HAMMER_CURSOR_ATEDISK;
174 } else if (realign_cre >= 0 || realign_del >= 0) {
175 error = realign_prune(prune, &cursor,
176 realign_cre, realign_del);
178 cursor.flags |= HAMMER_CURSOR_ATEDISK;
179 if (hammer_debug_general & 0x0200) {
180 kprintf("check %016llx %016llx: "
187 cursor.flags |= HAMMER_CURSOR_ATEDISK;
188 if (hammer_debug_general & 0x0100) {
189 kprintf("check %016llx %016llx: SKIP\n",
190 elm->base.obj_id, elm->base.key);
193 ++prune->stat_scanrecords;
196 * Bad hack for now, don't blow out the kernel's buffer
197 * cache. NOTE: We still hold locks on the cursor, we
198 * cannot call the flusher synchronously.
200 if (trans->hmp->locked_dirty_count +
201 trans->hmp->io_running_count > hammer_limit_dirtybufs) {
202 hammer_flusher_async(trans->hmp);
203 tsleep(trans, 0, "hmrslo", hz / 10);
205 error = hammer_signal_check(trans->hmp);
207 error = hammer_btree_iterate_reverse(&cursor);
209 hammer_sync_unlock(trans);
212 hammer_done_cursor(&cursor);
213 if (error == EDEADLK)
215 if (error == EINTR) {
216 prune->head.flags |= HAMMER_IOC_HEAD_INTR;
220 prune->elms = user_elms;
221 kfree(copy_elms, M_TEMP);
226 * Check pruning list. The list must be sorted in descending order.
229 check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm,
230 int *realign_cre, int *realign_del)
232 struct hammer_ioc_prune_elm *scan;
239 * If pruning everything remove all records with a non-zero
242 if (prune->head.flags & HAMMER_IOC_PRUNE_ALL) {
243 if (elm->base.delete_tid != 0)
248 for (i = 0; i < prune->nelms; ++i) {
249 scan = &prune->elms[i];
252 * Locate the scan index covering the create and delete TIDs.
254 if (*realign_cre < 0 &&
255 elm->base.create_tid >= scan->beg_tid &&
256 elm->base.create_tid < scan->end_tid) {
259 if (*realign_del < 0 && elm->base.delete_tid &&
260 elm->base.delete_tid > scan->beg_tid &&
261 elm->base.delete_tid <= scan->end_tid) {
266 * Now check for loop termination.
268 if (elm->base.create_tid >= scan->end_tid ||
269 elm->base.delete_tid > scan->end_tid) {
274 * Now determine if we can delete the record.
276 if (elm->base.delete_tid &&
277 elm->base.create_tid >= scan->beg_tid &&
278 elm->base.delete_tid <= scan->end_tid &&
279 (elm->base.create_tid - scan->beg_tid) / scan->mod_tid ==
280 (elm->base.delete_tid - scan->beg_tid) / scan->mod_tid) {
288 * Align the record to cover any gaps created through the deletion of
289 * records within the pruning space. If we were to just delete the records
290 * there would be gaps which in turn would cause a snapshot that is NOT on
291 * a pruning boundary to appear corrupt to the user. Forcing alignment
292 * of the create_tid and delete_tid for retained records 'reconnects'
293 * the previously contiguous space, making it contiguous again after the
296 * The use of a reverse iteration allows us to safely align the records and
297 * related elements without creating temporary overlaps. XXX we should
298 * add ordering dependancies for record buffers to guarantee consistency
302 realign_prune(struct hammer_ioc_prune *prune,
303 hammer_cursor_t cursor, int realign_cre, int realign_del)
305 struct hammer_ioc_prune_elm *scan;
306 hammer_btree_elm_t elm;
311 hammer_cursor_downgrade(cursor);
313 elm = &cursor->node->ondisk->elms[cursor->index];
314 ++prune->stat_realignments;
317 * Align the create_tid. By doing a reverse iteration we guarantee
318 * that all records after our current record have already been
319 * aligned, allowing us to safely correct the right-hand-boundary
320 * (because no record to our right is otherwise exactly matching
321 * will have a create_tid to the left of our aligned create_tid).
324 if (realign_cre >= 0) {
325 scan = &prune->elms[realign_cre];
327 delta = (elm->leaf.base.create_tid - scan->beg_tid) %
330 tid = elm->leaf.base.create_tid - delta + scan->mod_tid;
333 error = hammer_btree_correct_rhb(cursor, tid + 1);
335 error = hammer_btree_extract(cursor,
336 HAMMER_CURSOR_GET_LEAF);
340 error = hammer_cursor_upgrade(cursor);
343 hammer_modify_node(cursor->trans, cursor->node,
344 &elm->leaf.base.create_tid,
345 sizeof(elm->leaf.base.create_tid));
346 elm->leaf.base.create_tid = tid;
347 hammer_modify_node_done(cursor->node);
353 * Align the delete_tid. This only occurs if the record is historical
354 * was deleted at some point. Realigning the delete_tid does not
355 * move the record within the B-Tree but may cause it to temporarily
356 * overlap a record that has not yet been pruned.
358 if (error == 0 && realign_del >= 0) {
359 scan = &prune->elms[realign_del];
361 delta = (elm->leaf.base.delete_tid - scan->beg_tid) %
364 error = hammer_btree_extract(cursor,
365 HAMMER_CURSOR_GET_LEAF);
367 hammer_modify_node(cursor->trans, cursor->node,
368 &elm->leaf.base.delete_tid,
369 sizeof(elm->leaf.base.delete_tid));
370 elm->leaf.base.delete_tid =
371 elm->leaf.base.delete_tid -
372 delta + scan->mod_tid;
373 hammer_modify_node_done(cursor->node);