HAMMER 59B/Many: Stabilization pass - fixes for large file issues
[dragonfly.git] / sys / vfs / hammer / hammer_prune.c
blob47f86dece4382283930a41b505368e799d42fa98
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.9 2008/06/27 20:56:59 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 prune_should_delete(struct hammer_ioc_prune *prune,
48 hammer_btree_leaf_elm_t elm);
49 static void prune_check_nlinks(hammer_cursor_t cursor,
50 hammer_btree_leaf_elm_t elm);
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_leaf_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 elm_array_size;
64 if (prune->nelms < 0 || prune->nelms > HAMMER_MAX_PRUNE_ELMS)
65 return(EINVAL);
66 if ((prune->key_beg.localization | prune->key_end.localization) &
67 HAMMER_LOCALIZE_PSEUDOFS_MASK) {
68 return(EINVAL);
70 if (prune->key_beg.localization > prune->key_end.localization)
71 return(EINVAL);
72 if (prune->key_beg.localization == prune->key_end.localization) {
73 if (prune->key_beg.obj_id > prune->key_end.obj_id)
74 return(EINVAL);
75 /* key-space limitations - no check needed */
77 if ((prune->head.flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
78 return(EINVAL);
80 prune->key_cur.localization = prune->key_end.localization +
81 ip->obj_localization;
82 prune->key_cur.obj_id = prune->key_end.obj_id;
83 prune->key_cur.key = HAMMER_MAX_KEY;
86 * Copy element array from userland
88 elm_array_size = sizeof(*copy_elms) * prune->nelms;
89 user_elms = prune->elms;
90 copy_elms = kmalloc(elm_array_size, M_TEMP, M_WAITOK);
91 if ((error = copyin(user_elms, copy_elms, elm_array_size)) != 0)
92 goto failed;
93 prune->elms = copy_elms;
96 * Scan backwards. Retries typically occur if a deadlock is detected.
98 retry:
99 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
100 if (error) {
101 hammer_done_cursor(&cursor);
102 goto failed;
104 cursor.key_beg.localization = prune->key_beg.localization +
105 ip->obj_localization;
106 cursor.key_beg.obj_id = prune->key_beg.obj_id;
107 cursor.key_beg.key = HAMMER_MIN_KEY;
108 cursor.key_beg.create_tid = 1;
109 cursor.key_beg.delete_tid = 0;
110 cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
111 cursor.key_beg.obj_type = 0;
113 cursor.key_end.localization = prune->key_cur.localization;
114 cursor.key_end.obj_id = prune->key_cur.obj_id;
115 cursor.key_end.key = prune->key_cur.key;
116 cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
117 cursor.key_end.delete_tid = 0;
118 cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
119 cursor.key_end.obj_type = 0;
121 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
122 cursor.flags |= HAMMER_CURSOR_BACKEND;
125 * This flag allows the B-Tree code to clean up loose ends.
127 cursor.flags |= HAMMER_CURSOR_PRUNING;
129 hammer_sync_lock_sh(trans);
130 error = hammer_btree_last(&cursor);
132 while (error == 0) {
134 * Check for work
136 elm = &cursor.node->ondisk->elms[cursor.index].leaf;
137 prune->key_cur = elm->base;
140 * Yield to more important tasks
142 if ((error = hammer_signal_check(trans->hmp)) != 0)
143 break;
144 if (trans->hmp->sync_lock.wanted) {
145 hammer_sync_unlock(trans);
146 tsleep(trans, 0, "hmrslo", hz / 10);
147 hammer_sync_lock_sh(trans);
149 if (hammer_flusher_meta_limit(trans->hmp) ||
150 hammer_flusher_undo_exhausted(trans, 2)) {
151 error = EWOULDBLOCK;
152 break;
155 if (prune->stat_oldest_tid > elm->base.create_tid)
156 prune->stat_oldest_tid = elm->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 (prune_should_delete(prune, elm)) {
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 {
199 * Nothing to delete, but we may have to check other
200 * things.
202 prune_check_nlinks(&cursor, elm);
203 cursor.flags |= HAMMER_CURSOR_ATEDISK;
204 if (hammer_debug_general & 0x0100) {
205 kprintf("check %016llx %016llx: SKIP\n",
206 elm->base.obj_id, elm->base.key);
209 ++prune->stat_scanrecords;
210 error = hammer_btree_iterate_reverse(&cursor);
212 hammer_sync_unlock(trans);
213 if (error == ENOENT)
214 error = 0;
215 hammer_done_cursor(&cursor);
216 if (error == EWOULDBLOCK) {
217 hammer_flusher_sync(trans->hmp);
218 goto retry;
220 if (error == EDEADLK)
221 goto retry;
222 if (error == EINTR) {
223 prune->head.flags |= HAMMER_IOC_HEAD_INTR;
224 error = 0;
226 failed:
227 prune->key_cur.localization &= HAMMER_LOCALIZE_MASK;
228 prune->elms = user_elms;
229 kfree(copy_elms, M_TEMP);
230 return(error);
234 * Check pruning list. The list must be sorted in descending order.
236 * Return non-zero if the record should be deleted.
238 static int
239 prune_should_delete(struct hammer_ioc_prune *prune, hammer_btree_leaf_elm_t elm)
241 struct hammer_ioc_prune_elm *scan;
242 int i;
245 * If pruning everything remove all records with a non-zero
246 * delete_tid.
248 if (prune->head.flags & HAMMER_IOC_PRUNE_ALL) {
249 if (elm->base.delete_tid != 0)
250 return(1);
251 return(0);
254 for (i = 0; i < prune->nelms; ++i) {
255 scan = &prune->elms[i];
258 * Check for loop termination.
260 if (elm->base.create_tid >= scan->end_tid ||
261 elm->base.delete_tid > scan->end_tid) {
262 break;
266 * Determine if we can delete the record.
268 if (elm->base.delete_tid &&
269 elm->base.create_tid >= scan->beg_tid &&
270 elm->base.delete_tid <= scan->end_tid &&
271 (elm->base.create_tid - scan->beg_tid) / scan->mod_tid ==
272 (elm->base.delete_tid - scan->beg_tid) / scan->mod_tid) {
273 return(1);
276 return(0);
279 static
280 void
281 prune_check_nlinks(hammer_cursor_t cursor, hammer_btree_leaf_elm_t elm)
283 if (elm->base.rec_type != HAMMER_RECTYPE_INODE)
284 return;
285 if (elm->base.delete_tid != 0)
286 return;
287 if (hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA))
288 return;
289 if (cursor->data->inode.nlinks)
290 return;
291 kprintf("found disconnected inode %016llx\n", elm->base.obj_id);
294 #if 0
297 * NOTE: THIS CODE HAS BEEN REMOVED! Pruning no longer attempts to realign
298 * adjacent records because it seriously interferes with every
299 * mirroring algorithm I could come up with.
301 * This means that historical accesses beyond the first snapshot
302 * softlink should be on snapshot boundaries only. Historical
303 * accesses from "now" to the first snapshot softlink continue to
304 * be fine-grained.
306 * Align the record to cover any gaps created through the deletion of
307 * records within the pruning space. If we were to just delete the records
308 * there would be gaps which in turn would cause a snapshot that is NOT on
309 * a pruning boundary to appear corrupt to the user. Forcing alignment
310 * of the create_tid and delete_tid for retained records 'reconnects'
311 * the previously contiguous space, making it contiguous again after the
312 * deletions.
314 * The use of a reverse iteration allows us to safely align the records and
315 * related elements without creating temporary overlaps. XXX we should
316 * add ordering dependancies for record buffers to guarantee consistency
317 * during recovery.
319 static int
320 realign_prune(struct hammer_ioc_prune *prune,
321 hammer_cursor_t cursor, int realign_cre, int realign_del)
323 struct hammer_ioc_prune_elm *scan;
324 hammer_btree_elm_t elm;
325 hammer_tid_t delta;
326 hammer_tid_t tid;
327 int error;
329 hammer_cursor_downgrade(cursor);
331 elm = &cursor->node->ondisk->elms[cursor->index];
332 ++prune->stat_realignments;
335 * Align the create_tid. By doing a reverse iteration we guarantee
336 * that all records after our current record have already been
337 * aligned, allowing us to safely correct the right-hand-boundary
338 * (because no record to our right is otherwise exactly matching
339 * will have a create_tid to the left of our aligned create_tid).
341 error = 0;
342 if (realign_cre >= 0) {
343 scan = &prune->elms[realign_cre];
345 delta = (elm->leaf.base.create_tid - scan->beg_tid) %
346 scan->mod_tid;
347 if (delta) {
348 tid = elm->leaf.base.create_tid - delta + scan->mod_tid;
350 /* can EDEADLK */
351 error = hammer_btree_correct_rhb(cursor, tid + 1);
352 if (error == 0) {
353 error = hammer_btree_extract(cursor,
354 HAMMER_CURSOR_GET_LEAF);
356 if (error == 0) {
357 /* can EDEADLK */
358 error = hammer_cursor_upgrade(cursor);
360 if (error == 0) {
361 hammer_modify_node(cursor->trans, cursor->node,
362 &elm->leaf.base.create_tid,
363 sizeof(elm->leaf.base.create_tid));
364 elm->leaf.base.create_tid = tid;
365 hammer_modify_node_done(cursor->node);
371 * Align the delete_tid. This only occurs if the record is historical
372 * was deleted at some point. Realigning the delete_tid does not
373 * move the record within the B-Tree but may cause it to temporarily
374 * overlap a record that has not yet been pruned.
376 if (error == 0 && realign_del >= 0) {
377 scan = &prune->elms[realign_del];
379 delta = (elm->leaf.base.delete_tid - scan->beg_tid) %
380 scan->mod_tid;
381 if (delta) {
382 error = hammer_btree_extract(cursor,
383 HAMMER_CURSOR_GET_LEAF);
384 if (error == 0) {
385 hammer_modify_node(cursor->trans, cursor->node,
386 &elm->leaf.base.delete_tid,
387 sizeof(elm->leaf.base.delete_tid));
388 elm->leaf.base.delete_tid =
389 elm->leaf.base.delete_tid -
390 delta + scan->mod_tid;
391 hammer_modify_node_done(cursor->node);
395 return (error);
398 #endif