d92696ef99a78e6397c3eb18b68940e380264075
[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.14 2008/07/11 05:44:23 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 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);
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_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
65         if (prune->nelms < 0 || prune->nelms > HAMMER_MAX_PRUNE_ELMS)
66                 return(EINVAL);
67         if ((prune->key_beg.localization | prune->key_end.localization) &
68             HAMMER_LOCALIZE_PSEUDOFS_MASK) {
69                 return(EINVAL);
70         }
71         if (prune->key_beg.localization > prune->key_end.localization)
72                 return(EINVAL);
73         if (prune->key_beg.localization == prune->key_end.localization) {
74                 if (prune->key_beg.obj_id > prune->key_end.obj_id)
75                         return(EINVAL);
76                 /* key-space limitations - no check needed */
77         }
78         if ((prune->head.flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
79                 return(EINVAL);
80
81         prune->key_cur.localization = prune->key_end.localization +
82                                       ip->obj_localization;
83         prune->key_cur.obj_id = prune->key_end.obj_id;
84         prune->key_cur.key = HAMMER_MAX_KEY;
85
86         /*
87          * Copy element array from userland
88          */
89         elm_array_size = sizeof(*copy_elms) * prune->nelms;
90         user_elms = prune->elms;
91         copy_elms = kmalloc(elm_array_size, M_TEMP, M_WAITOK);
92         if ((error = copyin(user_elms, copy_elms, elm_array_size)) != 0)
93                 goto failed;
94         prune->elms = copy_elms;
95
96         seq = trans->hmp->flusher.act;
97
98         /*
99          * Scan backwards.  Retries typically occur if a deadlock is detected.
100          */
101 retry:
102         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
103         if (error) {
104                 hammer_done_cursor(&cursor);
105                 goto failed;
106         }
107         cursor.key_beg.localization = prune->key_beg.localization +
108                                       ip->obj_localization;
109         cursor.key_beg.obj_id = prune->key_beg.obj_id;
110         cursor.key_beg.key = HAMMER_MIN_KEY;
111         cursor.key_beg.create_tid = 1;
112         cursor.key_beg.delete_tid = 0;
113         cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
114         cursor.key_beg.obj_type = 0;
115
116         cursor.key_end.localization = prune->key_cur.localization;
117         cursor.key_end.obj_id = prune->key_cur.obj_id;
118         cursor.key_end.key = prune->key_cur.key;
119         cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
120         cursor.key_end.delete_tid = 0;
121         cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
122         cursor.key_end.obj_type = 0;
123
124         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
125         cursor.flags |= HAMMER_CURSOR_BACKEND;
126
127         /*
128          * This flag allows the B-Tree code to clean up loose ends.
129          */
130         cursor.flags |= HAMMER_CURSOR_PRUNING;
131
132         error = hammer_btree_last(&cursor);
133
134         while (error == 0) {
135                 /*
136                  * Check for work
137                  */
138                 elm = &cursor.node->ondisk->elms[cursor.index].leaf;
139                 prune->key_cur = elm->base;
140
141                 /*
142                  * Yield to more important tasks
143                  */
144                 if ((error = hammer_signal_check(trans->hmp)) != 0)
145                         break;
146
147                 if (prune->stat_oldest_tid > elm->base.create_tid)
148                         prune->stat_oldest_tid = elm->base.create_tid;
149
150                 if (hammer_debug_general & 0x0200) {
151                         kprintf("check %016llx %016llx cre=%016llx del=%016llx\n",
152                                         elm->base.obj_id,
153                                         elm->base.key,
154                                         elm->base.create_tid,
155                                         elm->base.delete_tid);
156                 }
157                                 
158                 if (prune_should_delete(prune, elm)) {
159                         if (hammer_debug_general & 0x0200) {
160                                 kprintf("check %016llx %016llx: DELETE\n",
161                                         elm->base.obj_id, elm->base.key);
162                         }
163
164                         /*
165                          * NOTE: This can return EDEADLK
166                          *
167                          * Acquiring the sync lock guarantees that the
168                          * operation will not cross a synchronization
169                          * boundary (see the flusher).
170                          */
171                         isdir = (elm->base.rec_type == HAMMER_RECTYPE_DIRENTRY);
172
173                         hammer_sync_lock_sh(trans);
174                         error = hammer_delete_at_cursor(&cursor,
175                                                         HAMMER_DELETE_DESTROY,
176                                                         &prune->stat_bytes);
177                         hammer_sync_unlock(trans);
178                         if (error)
179                                 break;
180
181                         if (isdir)
182                                 ++prune->stat_dirrecords;
183                         else
184                                 ++prune->stat_rawrecords;
185
186                         /*
187                          * The current record might now be the one after
188                          * the one we deleted, set ATEDISK to force us
189                          * to skip it (since we are iterating backwards).
190                          */
191                         cursor.flags |= HAMMER_CURSOR_ATEDISK;
192                 } else {
193                         /*
194                          * Nothing to delete, but we may have to check other
195                          * things.
196                          */
197                         prune_check_nlinks(&cursor, elm);
198                         cursor.flags |= HAMMER_CURSOR_ATEDISK;
199                         if (hammer_debug_general & 0x0100) {
200                                 kprintf("check %016llx %016llx: SKIP\n",
201                                         elm->base.obj_id, elm->base.key);
202                         }
203                 }
204                 ++prune->stat_scanrecords;
205
206                 if (hammer_flusher_meta_halflimit(trans->hmp) ||
207                     hammer_flusher_undo_exhausted(trans, 1)) {
208                         hammer_unlock_cursor(&cursor, 0);
209                         hammer_flusher_wait(trans->hmp, seq);
210                         hammer_lock_cursor(&cursor, 0);
211                         seq = hammer_flusher_async(trans->hmp);
212                 }
213                 error = hammer_btree_iterate_reverse(&cursor);
214         }
215         if (error == ENOENT)
216                 error = 0;
217         hammer_done_cursor(&cursor);
218         if (error == EDEADLK)
219                 goto retry;
220         if (error == EINTR) {
221                 prune->head.flags |= HAMMER_IOC_HEAD_INTR;
222                 error = 0;
223         }
224 failed:
225         prune->key_cur.localization &= HAMMER_LOCALIZE_MASK;
226         prune->elms = user_elms;
227         kfree(copy_elms, M_TEMP);
228         return(error);
229 }
230
231 /*
232  * Check pruning list.  The list must be sorted in descending order.
233  *
234  * Return non-zero if the record should be deleted.
235  */
236 static int
237 prune_should_delete(struct hammer_ioc_prune *prune, hammer_btree_leaf_elm_t elm)
238 {
239         struct hammer_ioc_prune_elm *scan;
240         int i;
241
242         /*
243          * If pruning everything remove all records with a non-zero
244          * delete_tid.
245          */
246         if (prune->head.flags & HAMMER_IOC_PRUNE_ALL) {
247                 if (elm->base.delete_tid != 0)
248                         return(1);
249                 return(0);
250         }
251
252         for (i = 0; i < prune->nelms; ++i) {
253                 scan = &prune->elms[i];
254
255                 /*
256                  * Check for loop termination.
257                  */
258                 if (elm->base.create_tid >= scan->end_tid ||
259                     elm->base.delete_tid > scan->end_tid) {
260                         break;
261                 }
262
263                 /*
264                  * Determine if we can delete the record.
265                  */
266                 if (elm->base.delete_tid &&
267                     elm->base.create_tid >= scan->beg_tid &&
268                     elm->base.delete_tid <= scan->end_tid &&
269                     (elm->base.create_tid - scan->beg_tid) / scan->mod_tid ==
270                     (elm->base.delete_tid - scan->beg_tid) / scan->mod_tid) {
271                         return(1);
272                 }
273         }
274         return(0);
275 }
276
277 /*
278  * Dangling inodes can occur if processes are holding open descriptors on
279  * deleted files as-of when a machine crashes.  When we find one simply
280  * acquire the inode and release it.  The inode handling code will then
281  * do the right thing.
282  */
283 static
284 void
285 prune_check_nlinks(hammer_cursor_t cursor, hammer_btree_leaf_elm_t elm)
286 {
287         hammer_inode_t ip;
288         int error;
289
290         if (elm->base.rec_type != HAMMER_RECTYPE_INODE)
291                 return;
292         if (elm->base.delete_tid != 0)
293                 return;
294         if (hammer_btree_extract(cursor, HAMMER_CURSOR_GET_DATA))
295                 return;
296         if (cursor->data->inode.nlinks)
297                 return;
298         hammer_cursor_downgrade(cursor);
299         ip = hammer_get_inode(cursor->trans, NULL, elm->base.obj_id,
300                       HAMMER_MAX_TID,
301                       elm->base.localization & HAMMER_LOCALIZE_PSEUDOFS_MASK,
302                       0, &error);
303         if (ip) {
304                 kprintf("pruning disconnected inode %016llx\n",
305                         elm->base.obj_id);
306                 hammer_rel_inode(ip, 0);
307         } else {
308                 kprintf("unable to prune disconnected inode %016llx\n",
309                         elm->base.obj_id);
310         }
311 }
312
313 #if 0
314
315 /*
316  * NOTE: THIS CODE HAS BEEN REMOVED!  Pruning no longer attempts to realign
317  *       adjacent records because it seriously interferes with every 
318  *       mirroring algorithm I could come up with.
319  *
320  *       This means that historical accesses beyond the first snapshot
321  *       softlink should be on snapshot boundaries only.  Historical
322  *       accesses from "now" to the first snapshot softlink continue to
323  *       be fine-grained.
324  *
325  * NOTE: It also looks like there's a bug in the removed code.  It is believed
326  *       that create_tid can sometimes get set to 0xffffffffffffffff.  Just as
327  *       well we no longer try to do this fancy shit.  Probably the attempt to
328  *       correct the rhb is blowing up the cursor's indexing or addressing mapping.
329  *
330  * Align the record to cover any gaps created through the deletion of
331  * records within the pruning space.  If we were to just delete the records
332  * there would be gaps which in turn would cause a snapshot that is NOT on
333  * a pruning boundary to appear corrupt to the user.  Forcing alignment
334  * of the create_tid and delete_tid for retained records 'reconnects'
335  * the previously contiguous space, making it contiguous again after the
336  * deletions.
337  *
338  * The use of a reverse iteration allows us to safely align the records and
339  * related elements without creating temporary overlaps.  XXX we should
340  * add ordering dependancies for record buffers to guarantee consistency
341  * during recovery.
342  */
343 static int
344 realign_prune(struct hammer_ioc_prune *prune,
345               hammer_cursor_t cursor, int realign_cre, int realign_del)
346 {
347         struct hammer_ioc_prune_elm *scan;
348         hammer_btree_elm_t elm;
349         hammer_tid_t delta;
350         hammer_tid_t tid;
351         int error;
352
353         hammer_cursor_downgrade(cursor);
354
355         elm = &cursor->node->ondisk->elms[cursor->index];
356         ++prune->stat_realignments;
357
358         /*
359          * Align the create_tid.  By doing a reverse iteration we guarantee
360          * that all records after our current record have already been
361          * aligned, allowing us to safely correct the right-hand-boundary
362          * (because no record to our right is otherwise exactly matching
363          * will have a create_tid to the left of our aligned create_tid).
364          */
365         error = 0;
366         if (realign_cre >= 0) {
367                 scan = &prune->elms[realign_cre];
368
369                 delta = (elm->leaf.base.create_tid - scan->beg_tid) % 
370                         scan->mod_tid;
371                 if (delta) {
372                         tid = elm->leaf.base.create_tid - delta + scan->mod_tid;
373
374                         /* can EDEADLK */
375                         error = hammer_btree_correct_rhb(cursor, tid + 1);
376                         if (error == 0) {
377                                 error = hammer_btree_extract(cursor,
378                                                      HAMMER_CURSOR_GET_LEAF);
379                         }
380                         if (error == 0) {
381                                 /* can EDEADLK */
382                                 error = hammer_cursor_upgrade(cursor);
383                         }
384                         if (error == 0) {
385                                 hammer_modify_node(cursor->trans, cursor->node,
386                                             &elm->leaf.base.create_tid,
387                                             sizeof(elm->leaf.base.create_tid));
388                                 elm->leaf.base.create_tid = tid;
389                                 hammer_modify_node_done(cursor->node);
390                         }
391                 }
392         }
393
394         /*
395          * Align the delete_tid.  This only occurs if the record is historical
396          * was deleted at some point.  Realigning the delete_tid does not
397          * move the record within the B-Tree but may cause it to temporarily
398          * overlap a record that has not yet been pruned.
399          */
400         if (error == 0 && realign_del >= 0) {
401                 scan = &prune->elms[realign_del];
402
403                 delta = (elm->leaf.base.delete_tid - scan->beg_tid) % 
404                         scan->mod_tid;
405                 if (delta) {
406                         error = hammer_btree_extract(cursor,
407                                                      HAMMER_CURSOR_GET_LEAF);
408                         if (error == 0) {
409                                 hammer_modify_node(cursor->trans, cursor->node,
410                                             &elm->leaf.base.delete_tid,
411                                             sizeof(elm->leaf.base.delete_tid));
412                                 elm->leaf.base.delete_tid =
413                                             elm->leaf.base.delete_tid -
414                                             delta + scan->mod_tid;
415                                 hammer_modify_node_done(cursor->node);
416                         }
417                 }
418         }
419         return (error);
420 }
421
422 #endif