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