Merge from vendor branch NETGRAPH:
[dragonfly.git] / sys / vfs / hammer / hammer_prune.c
CommitLineData
11ad5ade
MD
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:
10 *
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.
20 *
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.
33 *
c82af904 34 * $DragonFly: src/sys/vfs/hammer/hammer_prune.c,v 1.8 2008/06/26 04:06:23 dillon Exp $
11ad5ade
MD
35 */
36
37#include "hammer.h"
38
39/*
40 * Iterate through the specified range of object ids and remove any
41 * deleted records that fall entirely within a prune modulo.
42 *
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.
46 */
c82af904 47static int check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm);
11ad5ade
MD
48
49int
50hammer_ioc_prune(hammer_transaction_t trans, hammer_inode_t ip,
51 struct hammer_ioc_prune *prune)
52{
53 struct hammer_cursor cursor;
54 hammer_btree_elm_t elm;
f1785283
MD
55 struct hammer_ioc_prune_elm *copy_elms;
56 struct hammer_ioc_prune_elm *user_elms;
11ad5ade
MD
57 int error;
58 int isdir;
f1785283 59 int elm_array_size;
11ad5ade
MD
60
61 if (prune->nelms < 0 || prune->nelms > HAMMER_MAX_PRUNE_ELMS)
62 return(EINVAL);
dd94f1b1
MD
63 if ((prune->key_beg.localization | prune->key_end.localization) &
64 HAMMER_LOCALIZE_PSEUDOFS_MASK) {
11ad5ade 65 return(EINVAL);
dd94f1b1
MD
66 }
67 if (prune->key_beg.localization > prune->key_end.localization)
68 return(EINVAL);
69 if (prune->key_beg.localization == prune->key_end.localization) {
70 if (prune->key_beg.obj_id > prune->key_end.obj_id)
1fef775e
MD
71 return(EINVAL);
72 /* key-space limitations - no check needed */
73 }
11ad5ade
MD
74 if ((prune->head.flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
75 return(EINVAL);
76
dd94f1b1
MD
77 prune->key_cur.localization = prune->key_end.localization +
78 ip->obj_localization;
79 prune->key_cur.obj_id = prune->key_end.obj_id;
80 prune->key_cur.key = HAMMER_MAX_KEY;
11ad5ade 81
f1785283
MD
82 /*
83 * Copy element array from userland
84 */
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;
91
92 /*
93 * Scan backwards. Retries typically occur if a deadlock is detected.
94 */
11ad5ade
MD
95retry:
96 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
97 if (error) {
98 hammer_done_cursor(&cursor);
f1785283 99 goto failed;
11ad5ade 100 }
dd94f1b1
MD
101 cursor.key_beg.localization = prune->key_beg.localization +
102 ip->obj_localization;
103 cursor.key_beg.obj_id = prune->key_beg.obj_id;
11ad5ade
MD
104 cursor.key_beg.key = HAMMER_MIN_KEY;
105 cursor.key_beg.create_tid = 1;
106 cursor.key_beg.delete_tid = 0;
107 cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
108 cursor.key_beg.obj_type = 0;
109
dd94f1b1
MD
110 cursor.key_end.localization = prune->key_cur.localization;
111 cursor.key_end.obj_id = prune->key_cur.obj_id;
112 cursor.key_end.key = prune->key_cur.key;
11ad5ade
MD
113 cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
114 cursor.key_end.delete_tid = 0;
115 cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
116 cursor.key_end.obj_type = 0;
117
118 cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
119 cursor.flags |= HAMMER_CURSOR_BACKEND;
120
121 /*
2f85fa4d 122 * This flag allows the B-Tree code to clean up loose ends.
11ad5ade
MD
123 */
124 cursor.flags |= HAMMER_CURSOR_PRUNING;
125
9f5097dc 126 hammer_sync_lock_sh(trans);
cebe9493 127 error = hammer_btree_last(&cursor);
9f5097dc 128
11ad5ade 129 while (error == 0) {
2f85fa4d
MD
130 /*
131 * Yield to more important tasks
132 */
9f5097dc
MD
133 if ((error = hammer_signal_check(trans->hmp)) != 0)
134 break;
2f85fa4d
MD
135 if (trans->hmp->sync_lock.wanted) {
136 hammer_sync_unlock(trans);
137 tsleep(trans, 0, "hmrslo", hz / 10);
138 hammer_sync_lock_sh(trans);
139 }
9f5097dc
MD
140 if (trans->hmp->locked_dirty_count +
141 trans->hmp->io_running_count > hammer_limit_dirtybufs) {
142 hammer_sync_unlock(trans);
143 hammer_flusher_async(trans->hmp);
144 tsleep(trans, 0, "hmrslo", hz / 10);
145 hammer_sync_lock_sh(trans);
146 }
2f85fa4d
MD
147
148 /*
149 * Check for work
150 */
11ad5ade 151 elm = &cursor.node->ondisk->elms[cursor.index];
dd94f1b1 152 prune->key_cur = elm->base;
11ad5ade
MD
153
154 if (prune->stat_oldest_tid > elm->leaf.base.create_tid)
155 prune->stat_oldest_tid = elm->leaf.base.create_tid;
156
1fef775e
MD
157 if (hammer_debug_general & 0x0200) {
158 kprintf("check %016llx %016llx cre=%016llx del=%016llx\n",
159 elm->base.obj_id,
160 elm->base.key,
161 elm->base.create_tid,
162 elm->base.delete_tid);
163 }
164
c82af904 165 if (check_prune(prune, elm) == 0) {
11ad5ade
MD
166 if (hammer_debug_general & 0x0200) {
167 kprintf("check %016llx %016llx: DELETE\n",
168 elm->base.obj_id, elm->base.key);
169 }
170
171 /*
172 * NOTE: This can return EDEADLK
173 *
174 * Acquiring the sync lock guarantees that the
175 * operation will not cross a synchronization
176 * boundary (see the flusher).
177 */
178 isdir = (elm->base.rec_type == HAMMER_RECTYPE_DIRENTRY);
179
11ad5ade
MD
180 error = hammer_delete_at_cursor(&cursor,
181 &prune->stat_bytes);
11ad5ade
MD
182 if (error)
183 break;
184
185 if (isdir)
186 ++prune->stat_dirrecords;
187 else
188 ++prune->stat_rawrecords;
189
190 /*
191 * The current record might now be the one after
192 * the one we deleted, set ATEDISK to force us
193 * to skip it (since we are iterating backwards).
194 */
195 cursor.flags |= HAMMER_CURSOR_ATEDISK;
11ad5ade
MD
196 } else {
197 cursor.flags |= HAMMER_CURSOR_ATEDISK;
198 if (hammer_debug_general & 0x0100) {
199 kprintf("check %016llx %016llx: SKIP\n",
200 elm->base.obj_id, elm->base.key);
201 }
202 }
203 ++prune->stat_scanrecords;
9f5097dc 204 error = hammer_btree_iterate_reverse(&cursor);
11ad5ade 205 }
2f85fa4d 206 hammer_sync_unlock(trans);
11ad5ade
MD
207 if (error == ENOENT)
208 error = 0;
209 hammer_done_cursor(&cursor);
210 if (error == EDEADLK)
211 goto retry;
212 if (error == EINTR) {
213 prune->head.flags |= HAMMER_IOC_HEAD_INTR;
214 error = 0;
215 }
f1785283 216failed:
dd94f1b1 217 prune->key_cur.localization &= HAMMER_LOCALIZE_MASK;
f1785283
MD
218 prune->elms = user_elms;
219 kfree(copy_elms, M_TEMP);
11ad5ade
MD
220 return(error);
221}
222
223/*
224 * Check pruning list. The list must be sorted in descending order.
225 */
226static int
c82af904 227check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm)
11ad5ade
MD
228{
229 struct hammer_ioc_prune_elm *scan;
230 int i;
231
11ad5ade
MD
232 /*
233 * If pruning everything remove all records with a non-zero
234 * delete_tid.
235 */
236 if (prune->head.flags & HAMMER_IOC_PRUNE_ALL) {
237 if (elm->base.delete_tid != 0)
238 return(0);
239 return(-1);
240 }
241
242 for (i = 0; i < prune->nelms; ++i) {
243 scan = &prune->elms[i];
244
c82af904 245#if 0
11ad5ade
MD
246 /*
247 * Locate the scan index covering the create and delete TIDs.
248 */
249 if (*realign_cre < 0 &&
250 elm->base.create_tid >= scan->beg_tid &&
251 elm->base.create_tid < scan->end_tid) {
252 *realign_cre = i;
253 }
254 if (*realign_del < 0 && elm->base.delete_tid &&
255 elm->base.delete_tid > scan->beg_tid &&
256 elm->base.delete_tid <= scan->end_tid) {
257 *realign_del = i;
258 }
c82af904 259#endif
11ad5ade
MD
260
261 /*
262 * Now check for loop termination.
263 */
264 if (elm->base.create_tid >= scan->end_tid ||
265 elm->base.delete_tid > scan->end_tid) {
266 break;
267 }
268
269 /*
270 * Now determine if we can delete the record.
271 */
272 if (elm->base.delete_tid &&
273 elm->base.create_tid >= scan->beg_tid &&
274 elm->base.delete_tid <= scan->end_tid &&
f1785283
MD
275 (elm->base.create_tid - scan->beg_tid) / scan->mod_tid ==
276 (elm->base.delete_tid - scan->beg_tid) / scan->mod_tid) {
11ad5ade
MD
277 return(0);
278 }
279 }
280 return(-1);
281}
282
c82af904
MD
283#if 0
284
11ad5ade 285/*
c82af904
MD
286 * NOTE: THIS CODE HAS BEEN REMOVED! Pruning no longer attempts to realign
287 * adjacent records because it seriously interferes with every
288 * mirroring algorithm I could come up with.
289 *
290 * This means that historical accesses beyond the first snapshot
291 * softlink should be on snapshot boundaries only. Historical
292 * accesses from "now" to the first snapshot softlink continue to
293 * be fine-grained.
294 *
11ad5ade
MD
295 * Align the record to cover any gaps created through the deletion of
296 * records within the pruning space. If we were to just delete the records
297 * there would be gaps which in turn would cause a snapshot that is NOT on
298 * a pruning boundary to appear corrupt to the user. Forcing alignment
299 * of the create_tid and delete_tid for retained records 'reconnects'
300 * the previously contiguous space, making it contiguous again after the
301 * deletions.
302 *
303 * The use of a reverse iteration allows us to safely align the records and
304 * related elements without creating temporary overlaps. XXX we should
305 * add ordering dependancies for record buffers to guarantee consistency
306 * during recovery.
307 */
308static int
309realign_prune(struct hammer_ioc_prune *prune,
310 hammer_cursor_t cursor, int realign_cre, int realign_del)
311{
f1785283 312 struct hammer_ioc_prune_elm *scan;
11ad5ade
MD
313 hammer_btree_elm_t elm;
314 hammer_tid_t delta;
11ad5ade
MD
315 hammer_tid_t tid;
316 int error;
317
318 hammer_cursor_downgrade(cursor);
319
320 elm = &cursor->node->ondisk->elms[cursor->index];
321 ++prune->stat_realignments;
322
323 /*
324 * Align the create_tid. By doing a reverse iteration we guarantee
325 * that all records after our current record have already been
326 * aligned, allowing us to safely correct the right-hand-boundary
f1785283 327 * (because no record to our right is otherwise exactly matching
11ad5ade 328 * will have a create_tid to the left of our aligned create_tid).
11ad5ade
MD
329 */
330 error = 0;
331 if (realign_cre >= 0) {
f1785283
MD
332 scan = &prune->elms[realign_cre];
333
334 delta = (elm->leaf.base.create_tid - scan->beg_tid) %
335 scan->mod_tid;
11ad5ade 336 if (delta) {
f1785283 337 tid = elm->leaf.base.create_tid - delta + scan->mod_tid;
11ad5ade
MD
338
339 /* can EDEADLK */
340 error = hammer_btree_correct_rhb(cursor, tid + 1);
341 if (error == 0) {
342 error = hammer_btree_extract(cursor,
343 HAMMER_CURSOR_GET_LEAF);
344 }
345 if (error == 0) {
346 /* can EDEADLK */
347 error = hammer_cursor_upgrade(cursor);
348 }
349 if (error == 0) {
350 hammer_modify_node(cursor->trans, cursor->node,
351 &elm->leaf.base.create_tid,
352 sizeof(elm->leaf.base.create_tid));
353 elm->leaf.base.create_tid = tid;
354 hammer_modify_node_done(cursor->node);
355 }
356 }
357 }
358
359 /*
360 * Align the delete_tid. This only occurs if the record is historical
361 * was deleted at some point. Realigning the delete_tid does not
362 * move the record within the B-Tree but may cause it to temporarily
363 * overlap a record that has not yet been pruned.
364 */
365 if (error == 0 && realign_del >= 0) {
f1785283
MD
366 scan = &prune->elms[realign_del];
367
368 delta = (elm->leaf.base.delete_tid - scan->beg_tid) %
369 scan->mod_tid;
11ad5ade
MD
370 if (delta) {
371 error = hammer_btree_extract(cursor,
372 HAMMER_CURSOR_GET_LEAF);
373 if (error == 0) {
374 hammer_modify_node(cursor->trans, cursor->node,
375 &elm->leaf.base.delete_tid,
376 sizeof(elm->leaf.base.delete_tid));
377 elm->leaf.base.delete_tid =
378 elm->leaf.base.delete_tid -
f1785283 379 delta + scan->mod_tid;
11ad5ade
MD
380 hammer_modify_node_done(cursor->node);
381 }
382 }
383 }
384 return (error);
385}
386
c82af904 387#endif