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