HAMMER 53G/Many: Performance tuning.
[dragonfly.git] / sys / vfs / hammer / hammer_ioctl.c
blob156bbcdc0741c708c2448004568e7d8d796b50a4
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:
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.
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.
34 * $DragonFly: src/sys/vfs/hammer/hammer_ioctl.c,v 1.20 2008/06/10 08:51:01 dillon Exp $
37 #include "hammer.h"
39 static int hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
40 struct hammer_ioc_history *hist);
41 static int hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
42 struct hammer_ioc_synctid *std);
44 int
45 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
46 struct ucred *cred)
48 struct hammer_transaction trans;
49 int error;
51 error = suser_cred(cred, PRISON_ROOT);
53 hammer_start_transaction(&trans, ip->hmp);
55 switch(com) {
56 case HAMMERIOC_PRUNE:
57 if (error == 0) {
58 error = hammer_ioc_prune(&trans, ip,
59 (struct hammer_ioc_prune *)data);
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 case HAMMERIOC_SYNCTID:
71 error = hammer_ioc_synctid(&trans, ip,
72 (struct hammer_ioc_synctid *)data);
73 break;
74 default:
75 error = EOPNOTSUPP;
76 break;
78 hammer_done_transaction(&trans);
79 return (error);
83 * Iterate through an object's inode or an object's records and record
84 * modification TIDs.
86 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
87 hammer_btree_elm_t elm);
89 static
90 int
91 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
92 struct hammer_ioc_history *hist)
94 struct hammer_cursor cursor;
95 hammer_btree_elm_t elm;
96 int error;
99 * Validate the structure and initialize for return.
101 if (hist->beg_tid > hist->end_tid)
102 return(EINVAL);
103 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
104 if (hist->key > hist->nxt_key)
105 return(EINVAL);
108 hist->obj_id = ip->obj_id;
109 hist->count = 0;
110 hist->nxt_tid = hist->end_tid;
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;
115 if ((ip->flags & HAMMER_INODE_MODMASK) & ~HAMMER_INODE_ITIMES)
116 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
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.
123 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
124 if (error) {
125 hammer_done_cursor(&cursor);
126 return(error);
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;
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;
141 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
143 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
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).
150 cursor.key_beg.key = hist->key;
151 cursor.key_end.key = HAMMER_MAX_KEY;
152 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
153 cursor.key_end.localization = HAMMER_LOCALIZE_MISC;
155 switch(ip->ino_data.obj_type) {
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;
170 cursor.key_end.rec_type = cursor.key_beg.rec_type;
171 } else {
173 * The inode itself.
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;
179 cursor.key_beg.localization = HAMMER_LOCALIZE_INODE;
180 cursor.key_end.localization = HAMMER_LOCALIZE_INODE;
183 error = hammer_btree_first(&cursor);
184 while (error == 0) {
185 elm = &cursor.node->ondisk->elms[cursor.index];
187 add_history(ip, hist, elm);
188 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
189 HAMMER_IOC_HISTORY_NEXT_KEY |
190 HAMMER_IOC_HISTORY_EOF)) {
191 break;
193 error = hammer_btree_iterate(&cursor);
195 if (error == ENOENT) {
196 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
197 error = 0;
199 hammer_done_cursor(&cursor);
200 return(error);
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.
208 static void
209 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
210 hammer_btree_elm_t elm)
212 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
213 return;
214 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
215 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
217 * Adjust nxt_key
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;
223 if (hist->nxt_key > elm->leaf.base.key)
224 hist->nxt_key = elm->leaf.base.key;
227 * Record is beyond MAXPHYS, there won't be any more records
228 * in the iteration covering the requested offset (key).
230 if (elm->leaf.base.key >= MAXPHYS &&
231 elm->leaf.base.key - MAXPHYS > hist->key) {
232 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
236 * Data-range of record does not cover the key.
238 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
239 return;
241 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
243 * Adjust nxt_key
245 if (hist->nxt_key > elm->leaf.base.key &&
246 hist->key < elm->leaf.base.key) {
247 hist->nxt_key = elm->leaf.base.key;
251 * Record is beyond the requested key.
253 if (elm->leaf.base.key > hist->key)
254 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
258 * Add create_tid if it is in-bounds.
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;
266 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
267 return;
269 hist->tid_ary[hist->count++] = elm->leaf.base.create_tid;
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
278 * [ ]
279 * [ ]
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;
286 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
287 return;
289 hist->tid_ary[hist->count++] = elm->leaf.base.delete_tid;
294 * Acquire synchronization TID
296 static
298 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
299 struct hammer_ioc_synctid *std)
301 hammer_mount_t hmp = ip->hmp;
302 int error = 0;
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;
328 return(error);