HAMMER 53D/Many: Stabilization
[dragonfly.git] / sys / vfs / hammer / hammer_prune.c
blobfb5ccf8eebeaedd27c78aa7ed1fc3342b5d9faa4
1 /*
2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
3 *
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
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
16 * distribution.
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
32 * SUCH DAMAGE.
34 * $DragonFly: src/sys/vfs/hammer/hammer_prune.c,v 1.6 2008/06/10 00:40:31 dillon Exp $
37 #include "hammer.h"
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);
52 int
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;
60 int error;
61 int isdir;
62 int realign_cre;
63 int realign_del;
64 int elm_array_size;
66 if (prune->nelms < 0 || prune->nelms > HAMMER_MAX_PRUNE_ELMS)
67 return(EINVAL);
68 if (prune->beg_localization > prune->end_localization)
69 return(EINVAL);
70 if (prune->beg_localization == prune->end_localization) {
71 if (prune->beg_obj_id > prune->end_obj_id)
72 return(EINVAL);
73 /* key-space limitations - no check needed */
75 if ((prune->head.flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
76 return(EINVAL);
78 prune->cur_localization = prune->end_localization;
79 prune->cur_obj_id = prune->end_obj_id;
80 prune->cur_key = HAMMER_MAX_KEY;
83 * Copy element array from userland
85 elm_array_size = sizeof(*copy_elms) * prune->nelms;
86 user_elms = prune->elms;
87 copy_elms = kmalloc(elm_array_size, M_TEMP, M_WAITOK);
88 if ((error = copyin(user_elms, copy_elms, elm_array_size)) != 0)
89 goto failed;
90 prune->elms = copy_elms;
93 * Scan backwards. Retries typically occur if a deadlock is detected.
95 retry:
96 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
97 if (error) {
98 hammer_done_cursor(&cursor);
99 goto failed;
101 cursor.key_beg.localization = prune->beg_localization;
102 cursor.key_beg.obj_id = prune->beg_obj_id;
103 cursor.key_beg.key = HAMMER_MIN_KEY;
104 cursor.key_beg.create_tid = 1;
105 cursor.key_beg.delete_tid = 0;
106 cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
107 cursor.key_beg.obj_type = 0;
109 cursor.key_end.localization = prune->cur_localization;
110 cursor.key_end.obj_id = prune->cur_obj_id;
111 cursor.key_end.key = prune->cur_key;
112 cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
113 cursor.key_end.delete_tid = 0;
114 cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
115 cursor.key_end.obj_type = 0;
117 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
118 cursor.flags |= HAMMER_CURSOR_BACKEND;
121 * This flag allows the B-Tree code to clean up loose ends.
123 cursor.flags |= HAMMER_CURSOR_PRUNING;
125 hammer_sync_lock_sh(trans);
126 error = hammer_btree_last(&cursor);
128 while (error == 0) {
130 * Yield to more important tasks
132 if ((error = hammer_signal_check(trans->hmp)) != 0)
133 break;
134 if (trans->hmp->sync_lock.wanted) {
135 hammer_sync_unlock(trans);
136 tsleep(trans, 0, "hmrslo", hz / 10);
137 hammer_sync_lock_sh(trans);
139 if (trans->hmp->locked_dirty_count +
140 trans->hmp->io_running_count > hammer_limit_dirtybufs) {
141 hammer_sync_unlock(trans);
142 hammer_flusher_async(trans->hmp);
143 tsleep(trans, 0, "hmrslo", hz / 10);
144 hammer_sync_lock_sh(trans);
148 * Check for work
150 elm = &cursor.node->ondisk->elms[cursor.index];
151 prune->cur_localization = elm->base.localization;
152 prune->cur_obj_id = elm->base.obj_id;
153 prune->cur_key = elm->base.key;
155 if (prune->stat_oldest_tid > elm->leaf.base.create_tid)
156 prune->stat_oldest_tid = elm->leaf.base.create_tid;
158 if (hammer_debug_general & 0x0200) {
159 kprintf("check %016llx %016llx cre=%016llx del=%016llx\n",
160 elm->base.obj_id,
161 elm->base.key,
162 elm->base.create_tid,
163 elm->base.delete_tid);
166 if (check_prune(prune, elm, &realign_cre, &realign_del) == 0) {
167 if (hammer_debug_general & 0x0200) {
168 kprintf("check %016llx %016llx: DELETE\n",
169 elm->base.obj_id, elm->base.key);
173 * NOTE: This can return EDEADLK
175 * Acquiring the sync lock guarantees that the
176 * operation will not cross a synchronization
177 * boundary (see the flusher).
179 isdir = (elm->base.rec_type == HAMMER_RECTYPE_DIRENTRY);
181 error = hammer_delete_at_cursor(&cursor,
182 &prune->stat_bytes);
183 if (error)
184 break;
186 if (isdir)
187 ++prune->stat_dirrecords;
188 else
189 ++prune->stat_rawrecords;
192 * The current record might now be the one after
193 * the one we deleted, set ATEDISK to force us
194 * to skip it (since we are iterating backwards).
196 cursor.flags |= HAMMER_CURSOR_ATEDISK;
197 } else if (realign_cre >= 0 || realign_del >= 0) {
198 error = realign_prune(prune, &cursor,
199 realign_cre, realign_del);
200 if (error == 0) {
201 cursor.flags |= HAMMER_CURSOR_ATEDISK;
202 if (hammer_debug_general & 0x0200) {
203 kprintf("check %016llx %016llx: "
204 "REALIGN\n",
205 elm->base.obj_id,
206 elm->base.key);
209 } else {
210 cursor.flags |= HAMMER_CURSOR_ATEDISK;
211 if (hammer_debug_general & 0x0100) {
212 kprintf("check %016llx %016llx: SKIP\n",
213 elm->base.obj_id, elm->base.key);
216 ++prune->stat_scanrecords;
217 error = hammer_btree_iterate_reverse(&cursor);
219 hammer_sync_unlock(trans);
220 if (error == ENOENT)
221 error = 0;
222 hammer_done_cursor(&cursor);
223 if (error == EDEADLK)
224 goto retry;
225 if (error == EINTR) {
226 prune->head.flags |= HAMMER_IOC_HEAD_INTR;
227 error = 0;
229 failed:
230 prune->elms = user_elms;
231 kfree(copy_elms, M_TEMP);
232 return(error);
236 * Check pruning list. The list must be sorted in descending order.
238 static int
239 check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm,
240 int *realign_cre, int *realign_del)
242 struct hammer_ioc_prune_elm *scan;
243 int i;
245 *realign_cre = -1;
246 *realign_del = -1;
249 * If pruning everything remove all records with a non-zero
250 * delete_tid.
252 if (prune->head.flags & HAMMER_IOC_PRUNE_ALL) {
253 if (elm->base.delete_tid != 0)
254 return(0);
255 return(-1);
258 for (i = 0; i < prune->nelms; ++i) {
259 scan = &prune->elms[i];
262 * Locate the scan index covering the create and delete TIDs.
264 if (*realign_cre < 0 &&
265 elm->base.create_tid >= scan->beg_tid &&
266 elm->base.create_tid < scan->end_tid) {
267 *realign_cre = i;
269 if (*realign_del < 0 && elm->base.delete_tid &&
270 elm->base.delete_tid > scan->beg_tid &&
271 elm->base.delete_tid <= scan->end_tid) {
272 *realign_del = i;
276 * Now check for loop termination.
278 if (elm->base.create_tid >= scan->end_tid ||
279 elm->base.delete_tid > scan->end_tid) {
280 break;
284 * Now determine if we can delete the record.
286 if (elm->base.delete_tid &&
287 elm->base.create_tid >= scan->beg_tid &&
288 elm->base.delete_tid <= scan->end_tid &&
289 (elm->base.create_tid - scan->beg_tid) / scan->mod_tid ==
290 (elm->base.delete_tid - scan->beg_tid) / scan->mod_tid) {
291 return(0);
294 return(-1);
298 * Align the record to cover any gaps created through the deletion of
299 * records within the pruning space. If we were to just delete the records
300 * there would be gaps which in turn would cause a snapshot that is NOT on
301 * a pruning boundary to appear corrupt to the user. Forcing alignment
302 * of the create_tid and delete_tid for retained records 'reconnects'
303 * the previously contiguous space, making it contiguous again after the
304 * deletions.
306 * The use of a reverse iteration allows us to safely align the records and
307 * related elements without creating temporary overlaps. XXX we should
308 * add ordering dependancies for record buffers to guarantee consistency
309 * during recovery.
311 static int
312 realign_prune(struct hammer_ioc_prune *prune,
313 hammer_cursor_t cursor, int realign_cre, int realign_del)
315 struct hammer_ioc_prune_elm *scan;
316 hammer_btree_elm_t elm;
317 hammer_tid_t delta;
318 hammer_tid_t tid;
319 int error;
321 hammer_cursor_downgrade(cursor);
323 elm = &cursor->node->ondisk->elms[cursor->index];
324 ++prune->stat_realignments;
327 * Align the create_tid. By doing a reverse iteration we guarantee
328 * that all records after our current record have already been
329 * aligned, allowing us to safely correct the right-hand-boundary
330 * (because no record to our right is otherwise exactly matching
331 * will have a create_tid to the left of our aligned create_tid).
333 error = 0;
334 if (realign_cre >= 0) {
335 scan = &prune->elms[realign_cre];
337 delta = (elm->leaf.base.create_tid - scan->beg_tid) %
338 scan->mod_tid;
339 if (delta) {
340 tid = elm->leaf.base.create_tid - delta + scan->mod_tid;
342 /* can EDEADLK */
343 error = hammer_btree_correct_rhb(cursor, tid + 1);
344 if (error == 0) {
345 error = hammer_btree_extract(cursor,
346 HAMMER_CURSOR_GET_LEAF);
348 if (error == 0) {
349 /* can EDEADLK */
350 error = hammer_cursor_upgrade(cursor);
352 if (error == 0) {
353 hammer_modify_node(cursor->trans, cursor->node,
354 &elm->leaf.base.create_tid,
355 sizeof(elm->leaf.base.create_tid));
356 elm->leaf.base.create_tid = tid;
357 hammer_modify_node_done(cursor->node);
363 * Align the delete_tid. This only occurs if the record is historical
364 * was deleted at some point. Realigning the delete_tid does not
365 * move the record within the B-Tree but may cause it to temporarily
366 * overlap a record that has not yet been pruned.
368 if (error == 0 && realign_del >= 0) {
369 scan = &prune->elms[realign_del];
371 delta = (elm->leaf.base.delete_tid - scan->beg_tid) %
372 scan->mod_tid;
373 if (delta) {
374 error = hammer_btree_extract(cursor,
375 HAMMER_CURSOR_GET_LEAF);
376 if (error == 0) {
377 hammer_modify_node(cursor->trans, cursor->node,
378 &elm->leaf.base.delete_tid,
379 sizeof(elm->leaf.base.delete_tid));
380 elm->leaf.base.delete_tid =
381 elm->leaf.base.delete_tid -
382 delta + scan->mod_tid;
383 hammer_modify_node_done(cursor->node);
387 return (error);