HAMMER 43/Many: Remove records from the media format, plus other stuff
[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.17 2008/05/12 21:17:18 dillon Exp $
35  */
36
37 #include "hammer.h"
38
39 static int hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
40                                 struct hammer_ioc_history *hist);
41
42 int
43 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
44              struct ucred *cred)
45 {
46         struct hammer_transaction trans;
47         int error;
48
49         error = suser_cred(cred, PRISON_ROOT);
50
51         hammer_start_transaction(&trans, ip->hmp);
52
53         switch(com) {
54         case HAMMERIOC_PRUNE:
55                 if (error == 0) {
56                         error = hammer_ioc_prune(&trans, ip,
57                                         (struct hammer_ioc_prune *)data);
58                 }
59                 break;
60         case HAMMERIOC_GETHISTORY:
61                 error = hammer_ioc_gethistory(&trans, ip,
62                                         (struct hammer_ioc_history *)data);
63                 break;
64         case HAMMERIOC_REBLOCK:
65                 error = hammer_ioc_reblock(&trans, ip,
66                                         (struct hammer_ioc_reblock *)data);
67                 break;
68         default:
69                 error = EOPNOTSUPP;
70                 break;
71         }
72         hammer_done_transaction(&trans);
73         return (error);
74 }
75
76 /*
77  * Iterate through an object's inode or an object's records and record
78  * modification TIDs.
79  */
80 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
81                         hammer_btree_elm_t elm);
82
83 static
84 int
85 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
86                       struct hammer_ioc_history *hist)
87 {
88         struct hammer_cursor cursor;
89         hammer_btree_elm_t elm;
90         int error;
91
92         /*
93          * Validate the structure and initialize for return.
94          */
95         if (hist->beg_tid > hist->end_tid)
96                 return(EINVAL);
97         if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
98                 if (hist->key > hist->nxt_key)
99                         return(EINVAL);
100         }
101
102         hist->obj_id = ip->obj_id;
103         hist->count = 0;
104         hist->nxt_tid = hist->end_tid;
105         hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
106         hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
107         hist->head.flags &= ~HAMMER_IOC_HISTORY_EOF;
108         hist->head.flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
109         if ((ip->flags & HAMMER_INODE_MODMASK) & ~HAMMER_INODE_ITIMES)
110                 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
111
112         /*
113          * Setup the cursor.  We can't handle undeletable records
114          * (create_tid of 0) at the moment.  A create_tid of 0 has
115          * a special meaning and cannot be specified in the cursor.
116          */
117         error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
118         if (error) {
119                 hammer_done_cursor(&cursor);
120                 return(error);
121         }
122
123         cursor.key_beg.obj_id = hist->obj_id;
124         cursor.key_beg.create_tid = hist->beg_tid;
125         cursor.key_beg.delete_tid = 0;
126         cursor.key_beg.obj_type = 0;
127         if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
128                 cursor.key_beg.create_tid = 1;
129
130         cursor.key_end.obj_id = hist->obj_id;
131         cursor.key_end.create_tid = hist->end_tid;
132         cursor.key_end.delete_tid = 0;
133         cursor.key_end.obj_type = 0;
134
135         cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
136
137         if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
138                 /*
139                  * key-range within the file.  For a regular file the
140                  * on-disk key represents BASE+LEN, not BASE, so the
141                  * first possible record containing the offset 'key'
142                  * has an on-disk key of (key + 1).
143                  */
144                 cursor.key_beg.key = hist->key;
145                 cursor.key_end.key = HAMMER_MAX_KEY;
146
147                 switch(ip->ino_data.obj_type) {
148                 case HAMMER_OBJTYPE_REGFILE:
149                         ++cursor.key_beg.key;
150                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
151                         break;
152                 case HAMMER_OBJTYPE_DIRECTORY:
153                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
154                         break;
155                 case HAMMER_OBJTYPE_DBFILE:
156                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
157                         break;
158                 default:
159                         error = EINVAL;
160                         break;
161                 }
162                 cursor.key_end.rec_type = cursor.key_beg.rec_type;
163         } else {
164                 /*
165                  * The inode itself.
166                  */
167                 cursor.key_beg.key = 0;
168                 cursor.key_end.key = 0;
169                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
170                 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
171         }
172
173         error = hammer_btree_first(&cursor);
174         while (error == 0) {
175                 elm = &cursor.node->ondisk->elms[cursor.index];
176
177                 add_history(ip, hist, elm);
178                 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
179                                         HAMMER_IOC_HISTORY_NEXT_KEY |
180                                         HAMMER_IOC_HISTORY_EOF)) {
181                         break;
182                 }
183                 error = hammer_btree_iterate(&cursor);
184         }
185         if (error == ENOENT) {
186                 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
187                 error = 0;
188         }
189         hammer_done_cursor(&cursor);
190         return(error);
191 }
192
193 /*
194  * Add the scanned element to the ioctl return structure.  Some special
195  * casing is required for regular files to accomodate how data ranges are
196  * stored on-disk.
197  */
198 static void
199 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
200             hammer_btree_elm_t elm)
201 {
202         if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
203                 return;
204         if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
205             ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
206                 /*
207                  * Adjust nxt_key
208                  */
209                 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
210                     hist->key < elm->leaf.base.key - elm->leaf.data_len) {
211                         hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
212                 }
213                 if (hist->nxt_key > elm->leaf.base.key)
214                         hist->nxt_key = elm->leaf.base.key;
215
216                 /*
217                  * Record is beyond MAXPHYS, there won't be any more records
218                  * in the iteration covering the requested offset (key).
219                  */
220                 if (elm->leaf.base.key >= MAXPHYS &&
221                     elm->leaf.base.key - MAXPHYS > hist->key) {
222                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
223                 }
224
225                 /*
226                  * Data-range of record does not cover the key.
227                  */
228                 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
229                         return;
230
231         } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
232                 /*
233                  * Adjust nxt_key
234                  */
235                 if (hist->nxt_key > elm->leaf.base.key &&
236                     hist->key < elm->leaf.base.key) {
237                         hist->nxt_key = elm->leaf.base.key;
238                 }
239
240                 /*
241                  * Record is beyond the requested key.
242                  */
243                 if (elm->leaf.base.key > hist->key)
244                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
245         }
246
247         /*
248          * Add create_tid if it is in-bounds.
249          */
250         if ((hist->count == 0 ||
251              elm->leaf.base.create_tid != hist->tid_ary[hist->count - 1]) &&
252             elm->leaf.base.create_tid >= hist->beg_tid &&
253             elm->leaf.base.create_tid < hist->end_tid) {
254                 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
255                         hist->nxt_tid = elm->leaf.base.create_tid;
256                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
257                         return;
258                 }
259                 hist->tid_ary[hist->count++] = elm->leaf.base.create_tid;
260         }
261
262         /*
263          * Add delete_tid if it is in-bounds.  Note that different portions
264          * of the history may have overlapping data ranges with different
265          * delete_tid's.  If this case occurs the delete_tid may match the
266          * create_tid of a following record.  XXX
267          *
268          *      [        ]
269          *            [     ]
270          */
271         if (elm->leaf.base.delete_tid &&
272             elm->leaf.base.delete_tid >= hist->beg_tid &&
273             elm->leaf.base.delete_tid < hist->end_tid) {
274                 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
275                         hist->nxt_tid = elm->leaf.base.delete_tid;
276                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
277                         return;
278                 }
279                 hist->tid_ary[hist->count++] = elm->leaf.base.delete_tid;
280         }
281 }
282