sys/vfs/hammer: Use HAMMER_BUFSIZE_DOALIGN() and variants
[dragonfly.git] / sys / vfs / hammer / hammer_prune.c
blobec61213be4e34a93a58cb87e1b41d8de04449528
1 /*
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
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.19 2008/09/23 21:03:52 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;
63 int seq;
64 int64_t bytes;
65 uint32_t key_beg_localization;
67 if (prune->nelms < 0 || prune->nelms > HAMMER_MAX_PRUNE_ELMS)
68 return(EINVAL);
69 if ((prune->key_beg.localization | prune->key_end.localization) &
70 HAMMER_LOCALIZE_PSEUDOFS_MASK) {
71 return(EINVAL);
73 if (prune->key_beg.localization > prune->key_end.localization)
74 return(EINVAL);
75 if (prune->key_beg.localization == prune->key_end.localization) {
76 if (prune->key_beg.obj_id > prune->key_end.obj_id)
77 return(EINVAL);
78 /* key-space limitations - no check needed */
80 if ((prune->head.flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
81 return(EINVAL);
84 * Ioctl caller has only set localization type to prune.
85 * Initialize cursor key localization with ip localization.
87 key_beg_localization = prune->key_beg.localization;
88 key_beg_localization &= HAMMER_LOCALIZE_MASK;
89 key_beg_localization |= ip->obj_localization;
91 prune->key_cur.localization = prune->key_end.localization;
92 prune->key_cur.localization &= HAMMER_LOCALIZE_MASK;
93 prune->key_cur.localization |= ip->obj_localization;
95 prune->key_cur.obj_id = prune->key_end.obj_id;
96 prune->key_cur.key = HAMMER_MAX_KEY;
99 * Copy element array from userland
101 elm_array_size = sizeof(*copy_elms) * prune->nelms;
102 user_elms = prune->elms;
103 copy_elms = kmalloc(elm_array_size, M_TEMP, M_WAITOK);
104 if ((error = copyin(user_elms, copy_elms, elm_array_size)) != 0)
105 goto failed;
106 prune->elms = copy_elms;
108 seq = trans->hmp->flusher.done;
111 * Scan backwards. Retries typically occur if a deadlock is detected.
113 retry:
114 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
115 if (error) {
116 hammer_done_cursor(&cursor);
117 goto failed;
119 cursor.key_beg.localization = key_beg_localization;
120 cursor.key_beg.obj_id = prune->key_beg.obj_id;
121 cursor.key_beg.key = HAMMER_MIN_KEY;
122 cursor.key_beg.create_tid = 1;
123 cursor.key_beg.delete_tid = 0;
124 cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
125 cursor.key_beg.obj_type = 0;
127 cursor.key_end.localization = prune->key_cur.localization;
128 cursor.key_end.obj_id = prune->key_cur.obj_id;
129 cursor.key_end.key = prune->key_cur.key;
130 cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
131 cursor.key_end.delete_tid = 0;
132 cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
133 cursor.key_end.obj_type = 0;
135 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
136 cursor.flags |= HAMMER_CURSOR_BACKEND;
139 * This flag allows the B-Tree code to clean up loose ends. At
140 * the moment (XXX) it also means we have to hold the sync lock
141 * through the iteration.
143 cursor.flags |= HAMMER_CURSOR_PRUNING;
145 hammer_sync_lock_sh(trans);
146 error = hammer_btree_last(&cursor);
147 hammer_sync_unlock(trans);
149 while (error == 0) {
151 * Check for work
153 elm = &cursor.node->ondisk->elms[cursor.index].leaf;
154 prune->key_cur = elm->base;
157 * Yield to more important tasks
159 if ((error = hammer_signal_check(trans->hmp)) != 0)
160 break;
162 if (prune->stat_oldest_tid > elm->base.create_tid)
163 prune->stat_oldest_tid = elm->base.create_tid;
165 if (hammer_debug_general & 0x0200) {
166 hdkprintf("check %016jx %016jx cre=%016jx del=%016jx\n",
167 (intmax_t)elm->base.obj_id,
168 (intmax_t)elm->base.key,
169 (intmax_t)elm->base.create_tid,
170 (intmax_t)elm->base.delete_tid);
173 if (prune_should_delete(prune, elm)) {
174 if (hammer_debug_general & 0x0200) {
175 hdkprintf("check %016jx %016jx: DELETE\n",
176 (intmax_t)elm->base.obj_id,
177 (intmax_t)elm->base.key);
181 * NOTE: This can return EDEADLK
183 * Acquiring the sync lock guarantees that the
184 * operation will not cross a synchronization
185 * boundary (see the flusher).
187 * We dont need to track inodes or next_tid when
188 * we are destroying deleted records.
190 isdir = (elm->base.rec_type == HAMMER_RECTYPE_DIRENTRY);
192 hammer_sync_lock_sh(trans);
193 error = hammer_delete_at_cursor(&cursor,
194 HAMMER_DELETE_DESTROY,
195 cursor.trans->tid,
196 cursor.trans->time32,
197 0, &bytes);
198 hammer_sync_unlock(trans);
199 if (error)
200 break;
202 if (isdir)
203 ++prune->stat_dirrecords;
204 else
205 ++prune->stat_rawrecords;
206 prune->stat_bytes += bytes;
209 * The current record might now be the one after
210 * the one we deleted, set ATEDISK to force us
211 * to skip it (since we are iterating backwards).
213 cursor.flags |= HAMMER_CURSOR_ATEDISK;
214 } else {
216 * Nothing to delete, but we may have to check other
217 * things.
219 prune_check_nlinks(&cursor, elm);
220 cursor.flags |= HAMMER_CURSOR_ATEDISK;
221 if (hammer_debug_general & 0x0100) {
222 hdkprintf("check %016jx %016jx: SKIP\n",
223 (intmax_t)elm->base.obj_id,
224 (intmax_t)elm->base.key);
227 ++prune->stat_scanrecords;
230 * WARNING: See warnings in hammer_unlock_cursor() function.
232 while (hammer_flusher_meta_halflimit(trans->hmp) ||
233 hammer_flusher_undo_exhausted(trans, 2)) {
234 hammer_unlock_cursor(&cursor);
235 hammer_flusher_wait(trans->hmp, seq);
236 hammer_lock_cursor(&cursor);
237 seq = hammer_flusher_async_one(trans->hmp);
239 hammer_sync_lock_sh(trans);
240 error = hammer_btree_iterate_reverse(&cursor);
241 hammer_sync_unlock(trans);
243 if (error == ENOENT)
244 error = 0;
245 hammer_done_cursor(&cursor);
246 if (error == EDEADLK)
247 goto retry;
248 if (error == EINTR) {
249 prune->head.flags |= HAMMER_IOC_HEAD_INTR;
250 error = 0;
252 failed:
253 prune->key_cur.localization &= HAMMER_LOCALIZE_MASK;
254 prune->elms = user_elms;
255 kfree(copy_elms, M_TEMP);
256 return(error);
260 * Check pruning list. The list must be sorted in descending order.
262 * Return non-zero if the record should be deleted.
264 static int
265 prune_should_delete(struct hammer_ioc_prune *prune, hammer_btree_leaf_elm_t elm)
267 struct hammer_ioc_prune_elm *scan;
268 int i;
271 * If pruning everything remove all records with a non-zero
272 * delete_tid.
274 if (prune->head.flags & HAMMER_IOC_PRUNE_ALL) {
275 if (elm->base.delete_tid != 0)
276 return(1);
277 return(0);
280 for (i = 0; i < prune->nelms; ++i) {
281 scan = &prune->elms[i];
284 * Check for loop termination.
286 if (elm->base.create_tid >= scan->end_tid ||
287 elm->base.delete_tid > scan->end_tid) {
288 break;
292 * Determine if we can delete the record.
294 if (elm->base.delete_tid &&
295 elm->base.create_tid >= scan->beg_tid &&
296 elm->base.delete_tid <= scan->end_tid &&
297 (elm->base.create_tid - scan->beg_tid) / scan->mod_tid ==
298 (elm->base.delete_tid - scan->beg_tid) / scan->mod_tid) {
299 return(1);
302 return(0);
306 * Dangling inodes can occur if processes are holding open descriptors on
307 * deleted files as-of when a machine crashes. When we find one simply
308 * acquire the inode and release it. The inode handling code will then
309 * do the right thing.
311 static
312 void
313 prune_check_nlinks(hammer_cursor_t cursor, hammer_btree_leaf_elm_t elm)
315 hammer_inode_t ip;
316 int error;
318 if (elm->base.rec_type != HAMMER_RECTYPE_INODE)
319 return;
320 if (elm->base.delete_tid != 0)
321 return;
322 if (hammer_btree_extract_data(cursor))
323 return;
324 if (cursor->data->inode.nlinks)
325 return;
326 hammer_cursor_downgrade(cursor);
327 ip = hammer_get_inode(cursor->trans, NULL, elm->base.obj_id,
328 HAMMER_MAX_TID,
329 elm->base.localization & HAMMER_LOCALIZE_PSEUDOFS_MASK,
330 0, &error);
331 if (ip) {
332 if (hammer_debug_general & 0x0001) {
333 hdkprintf("pruning disconnected inode %016jx\n",
334 (intmax_t)elm->base.obj_id);
336 hammer_rel_inode(ip, 0);
337 hammer_inode_waitreclaims(cursor->trans);
338 } else {
339 hkprintf("unable to prune disconnected inode %016jx\n",
340 (intmax_t)elm->base.obj_id);
344 #if 0
347 * NOTE: THIS CODE HAS BEEN REMOVED! Pruning no longer attempts to realign
348 * adjacent records because it seriously interferes with every
349 * mirroring algorithm I could come up with.
351 * This means that historical accesses beyond the first snapshot
352 * softlink should be on snapshot boundaries only. Historical
353 * accesses from "now" to the first snapshot softlink continue to
354 * be fine-grained.
356 * NOTE: It also looks like there's a bug in the removed code. It is believed
357 * that create_tid can sometimes get set to 0xffffffffffffffff. Just as
358 * well we no longer try to do this fancy shit. Probably the attempt to
359 * correct the rhb is blowing up the cursor's indexing or addressing mapping.
361 * Align the record to cover any gaps created through the deletion of
362 * records within the pruning space. If we were to just delete the records
363 * there would be gaps which in turn would cause a snapshot that is NOT on
364 * a pruning boundary to appear corrupt to the user. Forcing alignment
365 * of the create_tid and delete_tid for retained records 'reconnects'
366 * the previously contiguous space, making it contiguous again after the
367 * deletions.
369 * The use of a reverse iteration allows us to safely align the records and
370 * related elements without creating temporary overlaps. XXX we should
371 * add ordering dependancies for record buffers to guarantee consistency
372 * during recovery.
374 static int
375 realign_prune(struct hammer_ioc_prune *prune,
376 hammer_cursor_t cursor, int realign_cre, int realign_del)
378 struct hammer_ioc_prune_elm *scan;
379 hammer_btree_elm_t elm;
380 hammer_tid_t delta;
381 hammer_tid_t tid;
382 int error;
384 hammer_cursor_downgrade(cursor);
386 elm = &cursor->node->ondisk->elms[cursor->index];
387 ++prune->stat_realignments;
390 * Align the create_tid. By doing a reverse iteration we guarantee
391 * that all records after our current record have already been
392 * aligned, allowing us to safely correct the right-hand-boundary
393 * (because no record to our right is otherwise exactly matching
394 * will have a create_tid to the left of our aligned create_tid).
396 error = 0;
397 if (realign_cre >= 0) {
398 scan = &prune->elms[realign_cre];
400 delta = (elm->leaf.base.create_tid - scan->beg_tid) %
401 scan->mod_tid;
402 if (delta) {
403 tid = elm->leaf.base.create_tid - delta + scan->mod_tid;
405 /* can EDEADLK */
406 error = hammer_btree_correct_rhb(cursor, tid + 1);
407 if (error == 0) {
408 error = hammer_btree_extract_leaf(cursor);
410 if (error == 0) {
411 /* can EDEADLK */
412 error = hammer_cursor_upgrade(cursor);
414 if (error == 0) {
415 hammer_modify_node(cursor->trans, cursor->node,
416 &elm->leaf.base.create_tid,
417 sizeof(elm->leaf.base.create_tid));
418 elm->leaf.base.create_tid = tid;
419 hammer_modify_node_done(cursor->node);
425 * Align the delete_tid. This only occurs if the record is historical
426 * was deleted at some point. Realigning the delete_tid does not
427 * move the record within the B-Tree but may cause it to temporarily
428 * overlap a record that has not yet been pruned.
430 if (error == 0 && realign_del >= 0) {
431 scan = &prune->elms[realign_del];
433 delta = (elm->leaf.base.delete_tid - scan->beg_tid) %
434 scan->mod_tid;
435 if (delta) {
436 error = hammer_btree_extract_leaf(cursor);
437 if (error == 0) {
438 hammer_modify_node(cursor->trans, cursor->node,
439 &elm->leaf.base.delete_tid,
440 sizeof(elm->leaf.base.delete_tid));
441 elm->leaf.base.delete_tid =
442 elm->leaf.base.delete_tid -
443 delta + scan->mod_tid;
444 hammer_modify_node_done(cursor->node);
448 return (error);
451 #endif