HAMMER 49B/Many: Stabilization pass
[dragonfly.git] / sys / vfs / hammer / hammer_prune.c
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  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_prune.c,v 1.4 2008/06/01 01:33:25 dillon Exp $
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  */
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);
51
52 int
53 hammer_ioc_prune(hammer_transaction_t trans, hammer_inode_t ip,
54                  struct hammer_ioc_prune *prune)
55 {
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;
65
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 */
74         }
75         if ((prune->head.flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
76                 return(EINVAL);
77
78         prune->cur_localization = prune->end_localization;
79         prune->cur_obj_id = prune->end_obj_id;
80         prune->cur_key = HAMMER_MAX_KEY;
81
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          */
95 retry:
96         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
97         if (error) {
98                 hammer_done_cursor(&cursor);
99                 goto failed;
100         }
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;
108
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;
116
117         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
118         cursor.flags |= HAMMER_CURSOR_BACKEND;
119
120         /*
121          * This flag allows the B-Tree code to clean up loose ends.
122          */
123         cursor.flags |= HAMMER_CURSOR_PRUNING;
124
125         hammer_sync_lock_sh(trans);
126         error = hammer_btree_last(&cursor);
127         while (error == 0) {
128                 /*
129                  * Yield to more important tasks
130                  */
131                 if (trans->hmp->sync_lock.wanted) {
132                         hammer_sync_unlock(trans);
133                         tsleep(trans, 0, "hmrslo", hz / 10);
134                         hammer_sync_lock_sh(trans);
135                 }
136
137                 /*
138                  * Check for work
139                  */
140                 elm = &cursor.node->ondisk->elms[cursor.index];
141                 prune->cur_localization = elm->base.localization;
142                 prune->cur_obj_id = elm->base.obj_id;
143                 prune->cur_key = elm->base.key;
144
145                 if (prune->stat_oldest_tid > elm->leaf.base.create_tid)
146                         prune->stat_oldest_tid = elm->leaf.base.create_tid;
147
148                 if (hammer_debug_general & 0x0200) {
149                         kprintf("check %016llx %016llx cre=%016llx del=%016llx\n",
150                                         elm->base.obj_id,
151                                         elm->base.key,
152                                         elm->base.create_tid,
153                                         elm->base.delete_tid);
154                 }
155                                 
156                 if (check_prune(prune, elm, &realign_cre, &realign_del) == 0) {
157                         if (hammer_debug_general & 0x0200) {
158                                 kprintf("check %016llx %016llx: DELETE\n",
159                                         elm->base.obj_id, elm->base.key);
160                         }
161
162                         /*
163                          * NOTE: This can return EDEADLK
164                          *
165                          * Acquiring the sync lock guarantees that the
166                          * operation will not cross a synchronization
167                          * boundary (see the flusher).
168                          */
169                         isdir = (elm->base.rec_type == HAMMER_RECTYPE_DIRENTRY);
170
171                         error = hammer_delete_at_cursor(&cursor,
172                                                         &prune->stat_bytes);
173                         if (error)
174                                 break;
175
176                         if (isdir)
177                                 ++prune->stat_dirrecords;
178                         else
179                                 ++prune->stat_rawrecords;
180
181                         /*
182                          * The current record might now be the one after
183                          * the one we deleted, set ATEDISK to force us
184                          * to skip it (since we are iterating backwards).
185                          */
186                         cursor.flags |= HAMMER_CURSOR_ATEDISK;
187                 } else if (realign_cre >= 0 || realign_del >= 0) {
188                         error = realign_prune(prune, &cursor,
189                                               realign_cre, realign_del);
190                         if (error == 0) {
191                                 cursor.flags |= HAMMER_CURSOR_ATEDISK;
192                                 if (hammer_debug_general & 0x0200) {
193                                         kprintf("check %016llx %016llx: "
194                                                 "REALIGN\n",
195                                                 elm->base.obj_id,
196                                                 elm->base.key);
197                                 }
198                         }
199                 } else {
200                         cursor.flags |= HAMMER_CURSOR_ATEDISK;
201                         if (hammer_debug_general & 0x0100) {
202                                 kprintf("check %016llx %016llx: SKIP\n",
203                                         elm->base.obj_id, elm->base.key);
204                         }
205                 }
206                 ++prune->stat_scanrecords;
207
208                 /*
209                  * Bad hack for now, don't blow out the kernel's buffer
210                  * cache.  NOTE: We still hold locks on the cursor, we
211                  * cannot call the flusher synchronously.
212                  */
213                 if (trans->hmp->locked_dirty_count +
214                     trans->hmp->io_running_count > hammer_limit_dirtybufs) {
215                         hammer_flusher_async(trans->hmp);
216                         tsleep(trans, 0, "hmrslo", hz / 10);
217                 }
218                 error = hammer_signal_check(trans->hmp);
219                 if (error == 0)
220                         error = hammer_btree_iterate_reverse(&cursor);
221         }
222         hammer_sync_unlock(trans);
223         if (error == ENOENT)
224                 error = 0;
225         hammer_done_cursor(&cursor);
226         if (error == EDEADLK)
227                 goto retry;
228         if (error == EINTR) {
229                 prune->head.flags |= HAMMER_IOC_HEAD_INTR;
230                 error = 0;
231         }
232 failed:
233         prune->elms = user_elms;
234         kfree(copy_elms, M_TEMP);
235         return(error);
236 }
237
238 /*
239  * Check pruning list.  The list must be sorted in descending order.
240  */
241 static int
242 check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm,
243             int *realign_cre, int *realign_del)
244 {
245         struct hammer_ioc_prune_elm *scan;
246         int i;
247
248         *realign_cre = -1;
249         *realign_del = -1;
250
251         /*
252          * If pruning everything remove all records with a non-zero
253          * delete_tid.
254          */
255         if (prune->head.flags & HAMMER_IOC_PRUNE_ALL) {
256                 if (elm->base.delete_tid != 0)
257                         return(0);
258                 return(-1);
259         }
260
261         for (i = 0; i < prune->nelms; ++i) {
262                 scan = &prune->elms[i];
263
264                 /*
265                  * Locate the scan index covering the create and delete TIDs.
266                  */
267                 if (*realign_cre < 0 &&
268                     elm->base.create_tid >= scan->beg_tid &&
269                     elm->base.create_tid < scan->end_tid) {
270                         *realign_cre = i;
271                 }
272                 if (*realign_del < 0 && elm->base.delete_tid &&
273                     elm->base.delete_tid > scan->beg_tid &&
274                     elm->base.delete_tid <= scan->end_tid) {
275                         *realign_del = i;
276                 }
277
278                 /*
279                  * Now check for loop termination.
280                  */
281                 if (elm->base.create_tid >= scan->end_tid ||
282                     elm->base.delete_tid > scan->end_tid) {
283                         break;
284                 }
285
286                 /*
287                  * Now determine if we can delete the record.
288                  */
289                 if (elm->base.delete_tid &&
290                     elm->base.create_tid >= scan->beg_tid &&
291                     elm->base.delete_tid <= scan->end_tid &&
292                     (elm->base.create_tid - scan->beg_tid) / scan->mod_tid ==
293                     (elm->base.delete_tid - scan->beg_tid) / scan->mod_tid) {
294                         return(0);
295                 }
296         }
297         return(-1);
298 }
299
300 /*
301  * Align the record to cover any gaps created through the deletion of
302  * records within the pruning space.  If we were to just delete the records
303  * there would be gaps which in turn would cause a snapshot that is NOT on
304  * a pruning boundary to appear corrupt to the user.  Forcing alignment
305  * of the create_tid and delete_tid for retained records 'reconnects'
306  * the previously contiguous space, making it contiguous again after the
307  * deletions.
308  *
309  * The use of a reverse iteration allows us to safely align the records and
310  * related elements without creating temporary overlaps.  XXX we should
311  * add ordering dependancies for record buffers to guarantee consistency
312  * during recovery.
313  */
314 static int
315 realign_prune(struct hammer_ioc_prune *prune,
316               hammer_cursor_t cursor, int realign_cre, int realign_del)
317 {
318         struct hammer_ioc_prune_elm *scan;
319         hammer_btree_elm_t elm;
320         hammer_tid_t delta;
321         hammer_tid_t tid;
322         int error;
323
324         hammer_cursor_downgrade(cursor);
325
326         elm = &cursor->node->ondisk->elms[cursor->index];
327         ++prune->stat_realignments;
328
329         /*
330          * Align the create_tid.  By doing a reverse iteration we guarantee
331          * that all records after our current record have already been
332          * aligned, allowing us to safely correct the right-hand-boundary
333          * (because no record to our right is otherwise exactly matching
334          * will have a create_tid to the left of our aligned create_tid).
335          */
336         error = 0;
337         if (realign_cre >= 0) {
338                 scan = &prune->elms[realign_cre];
339
340                 delta = (elm->leaf.base.create_tid - scan->beg_tid) % 
341                         scan->mod_tid;
342                 if (delta) {
343                         tid = elm->leaf.base.create_tid - delta + scan->mod_tid;
344
345                         /* can EDEADLK */
346                         error = hammer_btree_correct_rhb(cursor, tid + 1);
347                         if (error == 0) {
348                                 error = hammer_btree_extract(cursor,
349                                                      HAMMER_CURSOR_GET_LEAF);
350                         }
351                         if (error == 0) {
352                                 /* can EDEADLK */
353                                 error = hammer_cursor_upgrade(cursor);
354                         }
355                         if (error == 0) {
356                                 hammer_modify_node(cursor->trans, cursor->node,
357                                             &elm->leaf.base.create_tid,
358                                             sizeof(elm->leaf.base.create_tid));
359                                 elm->leaf.base.create_tid = tid;
360                                 hammer_modify_node_done(cursor->node);
361                         }
362                 }
363         }
364
365         /*
366          * Align the delete_tid.  This only occurs if the record is historical
367          * was deleted at some point.  Realigning the delete_tid does not
368          * move the record within the B-Tree but may cause it to temporarily
369          * overlap a record that has not yet been pruned.
370          */
371         if (error == 0 && realign_del >= 0) {
372                 scan = &prune->elms[realign_del];
373
374                 delta = (elm->leaf.base.delete_tid - scan->beg_tid) % 
375                         scan->mod_tid;
376                 if (delta) {
377                         error = hammer_btree_extract(cursor,
378                                                      HAMMER_CURSOR_GET_LEAF);
379                         if (error == 0) {
380                                 hammer_modify_node(cursor->trans, cursor->node,
381                                             &elm->leaf.base.delete_tid,
382                                             sizeof(elm->leaf.base.delete_tid));
383                                 elm->leaf.base.delete_tid =
384                                             elm->leaf.base.delete_tid -
385                                             delta + scan->mod_tid;
386                                 hammer_modify_node_done(cursor->node);
387                         }
388                 }
389         }
390         return (error);
391 }
392