HAMMER 38C/Many: Undo/Synchronization and crash recovery
[dragonfly.git] / sys / vfs / hammer / hammer_ioctl.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_ioctl.c,v 1.9 2008/04/25 21:49:49 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 static int hammer_ioc_prune(hammer_transaction_t trans, hammer_inode_t ip,
40                                 struct hammer_ioc_prune *prune);
41 static int hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
42                                 struct hammer_ioc_history *hist);
43
44 int
45 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
46              struct ucred *cred)
47 {
48         struct hammer_transaction trans;
49         int error;
50
51         error = suser_cred(cred, PRISON_ROOT);
52
53         hammer_start_transaction(&trans, ip->hmp);
54
55         switch(com) {
56         case HAMMERIOC_PRUNE:
57                 if (error == 0) {
58                         error = hammer_ioc_prune(&trans, ip,
59                                         (struct hammer_ioc_prune *)data);
60                 }
61                 break;
62         case HAMMERIOC_GETHISTORY:
63                 error = hammer_ioc_gethistory(&trans, ip,
64                                         (struct hammer_ioc_history *)data);
65                 break;
66         case HAMMERIOC_REBLOCK:
67                 error = hammer_ioc_reblock(&trans, ip,
68                                         (struct hammer_ioc_reblock *)data);
69                 break;
70         default:
71                 error = EOPNOTSUPP;
72                 break;
73         }
74         hammer_done_transaction(&trans);
75         return (error);
76 }
77
78 /*
79  * Iterate through the specified range of object ids and remove any
80  * deleted records that fall entirely within a prune modulo.
81  *
82  * A reverse iteration is used to prevent overlapping records from being
83  * created during the iteration due to alignments.  This also allows us
84  * to adjust alignments without blowing up the B-Tree.
85  */
86 static int check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm,
87                         int *realign_cre, int *realign_del);
88 static int realign_prune(struct hammer_ioc_prune *prune, hammer_cursor_t cursor,
89                         int realign_cre, int realign_del);
90
91 static int
92 hammer_ioc_prune(hammer_transaction_t trans, hammer_inode_t ip,
93                  struct hammer_ioc_prune *prune)
94 {
95         struct hammer_cursor cursor;
96         hammer_btree_elm_t elm;
97         int error;
98         int isdir;
99         int realign_cre;
100         int realign_del;
101
102         if (prune->nelms < 0 || prune->nelms > HAMMER_MAX_PRUNE_ELMS)
103                 return(EINVAL);
104         if (prune->beg_obj_id >= prune->end_obj_id)
105                 return(EINVAL);
106         if ((prune->flags & HAMMER_IOC_PRUNE_ALL) && prune->nelms)
107                 return(EINVAL);
108
109 retry:
110         error = hammer_init_cursor(trans, &cursor, NULL);
111         if (error) {
112                 hammer_done_cursor(&cursor);
113                 return(error);
114         }
115         cursor.key_beg.obj_id = prune->beg_obj_id;
116         cursor.key_beg.key = HAMMER_MIN_KEY;
117         cursor.key_beg.create_tid = 1;
118         cursor.key_beg.delete_tid = 0;
119         cursor.key_beg.rec_type = HAMMER_MIN_RECTYPE;
120         cursor.key_beg.obj_type = 0;
121
122         cursor.key_end.obj_id = prune->cur_obj_id;
123         cursor.key_end.key = prune->cur_key;
124         cursor.key_end.create_tid = HAMMER_MAX_TID - 1;
125         cursor.key_end.delete_tid = 0;
126         cursor.key_end.rec_type = HAMMER_MAX_RECTYPE;
127         cursor.key_end.obj_type = 0;
128
129         cursor.flags |= HAMMER_CURSOR_END_INCLUSIVE;
130
131         error = hammer_btree_last(&cursor);
132         while (error == 0) {
133                 elm = &cursor.node->ondisk->elms[cursor.index];
134                 prune->cur_obj_id = elm->base.obj_id;
135                 prune->cur_key = elm->base.key;
136
137                 if (prune->stat_oldest_tid > elm->leaf.base.create_tid)
138                         prune->stat_oldest_tid = elm->leaf.base.create_tid;
139
140                 if (check_prune(prune, elm, &realign_cre, &realign_del) == 0) {
141                         if (hammer_debug_general & 0x0200) {
142                                 kprintf("check %016llx %016llx: DELETE\n",
143                                         elm->base.obj_id, elm->base.key);
144                         }
145
146                         /*
147                          * NOTE: This can return EDEADLK
148                          */
149                         isdir = (elm->base.rec_type == HAMMER_RECTYPE_DIRENTRY);
150
151                         error = hammer_delete_at_cursor(&cursor,
152                                                         &prune->stat_bytes);
153                         if (error)
154                                 break;
155
156                         if (isdir)
157                                 ++prune->stat_dirrecords;
158                         else
159                                 ++prune->stat_rawrecords;
160                 } else if (realign_cre >= 0 || realign_del >= 0) {
161                         error = realign_prune(prune, &cursor,
162                                               realign_cre, realign_del);
163                         if (error == 0) {
164                                 cursor.flags |= HAMMER_CURSOR_ATEDISK;
165                                 if (hammer_debug_general & 0x0200) {
166                                         kprintf("check %016llx %016llx: "
167                                                 "REALIGN\n",
168                                                 elm->base.obj_id,
169                                                 elm->base.key);
170                                 }
171                         }
172                 } else {
173                         cursor.flags |= HAMMER_CURSOR_ATEDISK;
174                         if (hammer_debug_general & 0x0100) {
175                                 kprintf("check %016llx %016llx: SKIP\n",
176                                         elm->base.obj_id, elm->base.key);
177                         }
178                 }
179                 error = hammer_signal_check(trans->hmp);
180                 if (error == 0)
181                         error = hammer_btree_iterate_reverse(&cursor);
182         }
183         if (error == ENOENT)
184                 error = 0;
185         hammer_done_cursor(&cursor);
186         if (error == EDEADLK)
187                 goto retry;
188         return(error);
189 }
190
191 /*
192  * Check pruning list.  The list must be sorted in descending order.
193  */
194 static int
195 check_prune(struct hammer_ioc_prune *prune, hammer_btree_elm_t elm,
196             int *realign_cre, int *realign_del)
197 {
198         struct hammer_ioc_prune_elm *scan;
199         int i;
200
201         *realign_cre = -1;
202         *realign_del = -1;
203
204         /*
205          * If pruning everything remove all records with a non-zero
206          * delete_tid.
207          */
208         if (prune->flags & HAMMER_IOC_PRUNE_ALL) {
209                 if (elm->base.delete_tid != 0)
210                         return(0);
211                 return(-1);
212         }
213
214         for (i = 0; i < prune->nelms; ++i) {
215                 scan = &prune->elms[i];
216
217                 /*
218                  * Locate the scan index covering the create and delete TIDs.
219                  */
220                 if (*realign_cre < 0 &&
221                     elm->base.create_tid >= scan->beg_tid &&
222                     elm->base.create_tid < scan->end_tid) {
223                         *realign_cre = i;
224                 }
225                 if (*realign_del < 0 && elm->base.delete_tid &&
226                     elm->base.delete_tid > scan->beg_tid &&
227                     elm->base.delete_tid <= scan->end_tid) {
228                         *realign_del = i;
229                 }
230
231                 /*
232                  * Now check for loop termination.
233                  */
234                 if (elm->base.create_tid >= scan->end_tid ||
235                     elm->base.delete_tid > scan->end_tid) {
236                         break;
237                 }
238
239                 /*
240                  * Now determine if we can delete the record.
241                  */
242                 if (elm->base.delete_tid &&
243                     elm->base.create_tid >= scan->beg_tid &&
244                     elm->base.delete_tid <= scan->end_tid &&
245                     elm->base.create_tid / scan->mod_tid ==
246                     elm->base.delete_tid / scan->mod_tid) {
247                         return(0);
248                 }
249         }
250         return(-1);
251 }
252
253 /*
254  * Align the record to cover any gaps created through the deletion of
255  * records within the pruning space.  If we were to just delete the records
256  * there would be gaps which in turn would cause a snapshot that is NOT on
257  * a pruning boundary to appear corrupt to the user.  Forcing alignment
258  * of the create_tid and delete_tid for retained records 'reconnects'
259  * the previously contiguous space, making it contiguous again after the
260  * deletions.
261  *
262  * The use of a reverse iteration allows us to safely align the records and
263  * related elements without creating temporary overlaps.  XXX we should
264  * add ordering dependancies for record buffers to guarantee consistency
265  * during recovery.
266  */
267 static int
268 realign_prune(struct hammer_ioc_prune *prune,
269               hammer_cursor_t cursor, int realign_cre, int realign_del)
270 {
271         hammer_btree_elm_t elm;
272         hammer_tid_t delta;
273         hammer_tid_t mod;
274         hammer_tid_t tid;
275         int error;
276
277         hammer_cursor_downgrade(cursor);
278
279         elm = &cursor->node->ondisk->elms[cursor->index];
280         ++prune->stat_realignments;
281
282         /*
283          * Align the create_tid.  By doing a reverse iteration we guarantee
284          * that all records after our current record have already been
285          * aligned, allowing us to safely correct the right-hand-boundary
286          * (because no record to our right if otherwise exactly matching
287          * will have a create_tid to the left of our aligned create_tid).
288          *
289          * Ordering is important here XXX but disk write ordering for
290          * inter-cluster corrections is not currently guaranteed.
291          */
292         error = 0;
293         if (realign_cre >= 0) {
294                 mod = prune->elms[realign_cre].mod_tid;
295                 delta = elm->leaf.base.create_tid % mod;
296                 if (delta) {
297                         tid = elm->leaf.base.create_tid - delta + mod;
298
299                         /* can EDEADLK */
300                         error = hammer_btree_correct_rhb(cursor, tid + 1);
301                         if (error == 0) {
302                                 error = hammer_btree_extract(cursor,
303                                                      HAMMER_CURSOR_GET_RECORD);
304                         }
305                         if (error == 0) {
306                                 /* can EDEADLK */
307                                 error = hammer_cursor_upgrade(cursor);
308                         }
309                         if (error == 0) {
310                                 hammer_modify_buffer(cursor->trans,
311                                                      cursor->record_buffer,
312                                                      NULL, 0);
313                                 cursor->record->base.base.create_tid = tid;
314                                 hammer_modify_buffer_done(cursor->record_buffer);
315                                 hammer_modify_node(cursor->trans, cursor->node,
316                                                    &elm->leaf.base.create_tid,
317                                                    sizeof(elm->leaf.base.create_tid));
318                                 elm->leaf.base.create_tid = tid;
319                                 hammer_modify_node_done(cursor->node);
320                         }
321                 }
322         }
323
324         /*
325          * Align the delete_tid.  This only occurs if the record is historical
326          * was deleted at some point.  Realigning the delete_tid does not
327          * move the record within the B-Tree but may cause it to temporarily
328          * overlap a record that has not yet been pruned.
329          */
330         if (error == 0 && realign_del >= 0) {
331                 mod = prune->elms[realign_del].mod_tid;
332                 delta = elm->leaf.base.delete_tid % mod;
333                 if (delta) {
334                         error = hammer_btree_extract(cursor,
335                                                      HAMMER_CURSOR_GET_RECORD);
336                         if (error == 0) {
337                                 hammer_modify_node(cursor->trans, cursor->node,
338                                                    &elm->leaf.base.delete_tid,
339                                                    sizeof(elm->leaf.base.delete_tid));
340                                 elm->leaf.base.delete_tid =
341                                                 elm->leaf.base.delete_tid -
342                                                 delta + mod;
343                                 hammer_modify_node_done(cursor->node);
344                                 hammer_modify_buffer(cursor->trans, cursor->record_buffer, &cursor->record->base.base.delete_tid, sizeof(hammer_tid_t));
345                                 cursor->record->base.base.delete_tid =
346                                                 elm->leaf.base.delete_tid;
347                                 hammer_modify_buffer_done(cursor->record_buffer);
348                         }
349                 }
350         }
351         return (error);
352 }
353
354 /*
355  * Iterate through an object's inode or an object's records and record
356  * modification TIDs.
357  */
358 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
359                         hammer_btree_elm_t elm);
360
361 static
362 int
363 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
364                       struct hammer_ioc_history *hist)
365 {
366         struct hammer_cursor cursor;
367         hammer_btree_elm_t elm;
368         int error;
369
370         /*
371          * Validate the structure and initialize for return.
372          */
373         if (hist->beg_tid > hist->end_tid)
374                 return(EINVAL);
375         if (hist->flags & HAMMER_IOC_HISTORY_ATKEY) {
376                 if (hist->key > hist->nxt_key)
377                         return(EINVAL);
378         }
379
380         hist->obj_id = ip->obj_id;
381         hist->count = 0;
382         hist->nxt_tid = hist->end_tid;
383         hist->flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
384         hist->flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
385         hist->flags &= ~HAMMER_IOC_HISTORY_EOF;
386         hist->flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
387         if ((ip->flags & HAMMER_INODE_MODMASK) & ~HAMMER_INODE_ITIMES)
388                 hist->flags |= HAMMER_IOC_HISTORY_UNSYNCED;
389
390         /*
391          * Setup the cursor.  We can't handle undeletable records
392          * (create_tid of 0) at the moment.  A create_tid of 0 has
393          * a special meaning and cannot be specified in the cursor.
394          */
395         error = hammer_init_cursor(trans, &cursor, &ip->cache[0]);
396         if (error) {
397                 hammer_done_cursor(&cursor);
398                 return(error);
399         }
400
401         cursor.key_beg.obj_id = hist->obj_id;
402         cursor.key_beg.create_tid = hist->beg_tid;
403         cursor.key_beg.delete_tid = 0;
404         cursor.key_beg.obj_type = 0;
405         if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
406                 cursor.key_beg.create_tid = 1;
407
408         cursor.key_end.obj_id = hist->obj_id;
409         cursor.key_end.create_tid = hist->end_tid;
410         cursor.key_end.delete_tid = 0;
411         cursor.key_end.obj_type = 0;
412
413         cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
414
415         if (hist->flags & HAMMER_IOC_HISTORY_ATKEY) {
416                 /*
417                  * key-range within the file.  For a regular file the
418                  * on-disk key represents BASE+LEN, not BASE, so the
419                  * first possible record containing the offset 'key'
420                  * has an on-disk key of (key + 1).
421                  */
422                 cursor.key_beg.key = hist->key;
423                 cursor.key_end.key = HAMMER_MAX_KEY;
424
425                 switch(ip->ino_rec.base.base.obj_type) {
426                 case HAMMER_OBJTYPE_REGFILE:
427                         ++cursor.key_beg.key;
428                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
429                         break;
430                 case HAMMER_OBJTYPE_DIRECTORY:
431                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
432                         break;
433                 case HAMMER_OBJTYPE_DBFILE:
434                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
435                         break;
436                 default:
437                         error = EINVAL;
438                         break;
439                 }
440                 cursor.key_end.rec_type = cursor.key_beg.rec_type;
441         } else {
442                 /*
443                  * The inode itself.
444                  */
445                 cursor.key_beg.key = 0;
446                 cursor.key_end.key = 0;
447                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
448                 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
449         }
450
451         error = hammer_btree_first(&cursor);
452         while (error == 0) {
453                 elm = &cursor.node->ondisk->elms[cursor.index];
454
455                 add_history(ip, hist, elm);
456                 if (hist->flags & (HAMMER_IOC_HISTORY_NEXT_TID |
457                                   HAMMER_IOC_HISTORY_NEXT_KEY |
458                                   HAMMER_IOC_HISTORY_EOF)) {
459                         break;
460                 }
461                 error = hammer_btree_iterate(&cursor);
462         }
463         if (error == ENOENT) {
464                 hist->flags |= HAMMER_IOC_HISTORY_EOF;
465                 error = 0;
466         }
467         hammer_done_cursor(&cursor);
468         return(error);
469 }
470
471 /*
472  * Add the scanned element to the ioctl return structure.  Some special
473  * casing is required for regular files to accomodate how data ranges are
474  * stored on-disk.
475  */
476 static void
477 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
478             hammer_btree_elm_t elm)
479 {
480         if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
481                 return;
482         if ((hist->flags & HAMMER_IOC_HISTORY_ATKEY) &&
483             ip->ino_rec.base.base.obj_type == HAMMER_OBJTYPE_REGFILE) {
484                 /*
485                  * Adjust nxt_key
486                  */
487                 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
488                     hist->key < elm->leaf.base.key - elm->leaf.data_len) {
489                         hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
490                 }
491                 if (hist->nxt_key > elm->leaf.base.key)
492                         hist->nxt_key = elm->leaf.base.key;
493
494                 /*
495                  * Record is beyond MAXPHYS, there won't be any more records
496                  * in the iteration covering the requested offset (key).
497                  */
498                 if (elm->leaf.base.key >= MAXPHYS &&
499                     elm->leaf.base.key - MAXPHYS > hist->key) {
500                         hist->flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
501                 }
502
503                 /*
504                  * Data-range of record does not cover the key.
505                  */
506                 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
507                         return;
508
509         } else if (hist->flags & HAMMER_IOC_HISTORY_ATKEY) {
510                 /*
511                  * Adjust nxt_key
512                  */
513                 if (hist->nxt_key > elm->leaf.base.key &&
514                     hist->key < elm->leaf.base.key) {
515                         hist->nxt_key = elm->leaf.base.key;
516                 }
517
518                 /*
519                  * Record is beyond the requested key.
520                  */
521                 if (elm->leaf.base.key > hist->key)
522                         hist->flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
523         }
524
525         /*
526          * Add create_tid if it is in-bounds.
527          */
528         if ((hist->count == 0 ||
529              elm->leaf.base.create_tid != hist->tid_ary[hist->count - 1]) &&
530             elm->leaf.base.create_tid >= hist->beg_tid &&
531             elm->leaf.base.create_tid < hist->end_tid) {
532                 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
533                         hist->nxt_tid = elm->leaf.base.create_tid;
534                         hist->flags |= HAMMER_IOC_HISTORY_NEXT_TID;
535                         return;
536                 }
537                 hist->tid_ary[hist->count++] = elm->leaf.base.create_tid;
538         }
539
540         /*
541          * Add delete_tid if it is in-bounds.  Note that different portions
542          * of the history may have overlapping data ranges with different
543          * delete_tid's.  If this case occurs the delete_tid may match the
544          * create_tid of a following record.  XXX
545          *
546          *      [        ]
547          *            [     ]
548          */
549         if (elm->leaf.base.delete_tid &&
550             elm->leaf.base.delete_tid >= hist->beg_tid &&
551             elm->leaf.base.delete_tid < hist->end_tid) {
552                 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
553                         hist->nxt_tid = elm->leaf.base.delete_tid;
554                         hist->flags |= HAMMER_IOC_HISTORY_NEXT_TID;
555                         return;
556                 }
557                 hist->tid_ary[hist->count++] = elm->leaf.base.delete_tid;
558         }
559 }
560