2 * Copyright (c) 2008 The DragonFly Project. All rights reserved.
4 * This code is derived from software contributed to The DragonFly Project
5 * by Matthew Dillon <dillon@backplane.com>
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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
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
34 * $DragonFly: src/sys/vfs/hammer/hammer_ioctl.c,v 1.22 2008/06/23 07:31:14 dillon Exp $
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);
43 static int hammer_ioc_get_pseudofs(hammer_transaction_t trans,
45 struct hammer_ioc_get_pseudofs *pfs);
48 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
51 struct hammer_transaction trans;
54 error = suser_cred(cred, PRISON_ROOT);
56 hammer_start_transaction(&trans, ip->hmp);
61 error = hammer_ioc_prune(&trans, ip,
62 (struct hammer_ioc_prune *)data);
65 case HAMMERIOC_GETHISTORY:
66 error = hammer_ioc_gethistory(&trans, ip,
67 (struct hammer_ioc_history *)data);
69 case HAMMERIOC_REBLOCK:
70 error = hammer_ioc_reblock(&trans, ip,
71 (struct hammer_ioc_reblock *)data);
73 case HAMMERIOC_SYNCTID:
74 error = hammer_ioc_synctid(&trans, ip,
75 (struct hammer_ioc_synctid *)data);
77 case HAMMERIOC_GET_PSEUDOFS:
78 error = hammer_ioc_get_pseudofs(&trans, ip,
79 (struct hammer_ioc_get_pseudofs *)data);
85 hammer_done_transaction(&trans);
90 * Iterate through an object's inode or an object's records and record
93 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
94 hammer_btree_elm_t elm);
98 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
99 struct hammer_ioc_history *hist)
101 struct hammer_cursor cursor;
102 hammer_btree_elm_t elm;
106 * Validate the structure and initialize for return.
108 if (hist->beg_tid > hist->end_tid)
110 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
111 if (hist->key > hist->nxt_key)
115 hist->obj_id = ip->obj_id;
117 hist->nxt_tid = hist->end_tid;
118 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
119 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
120 hist->head.flags &= ~HAMMER_IOC_HISTORY_EOF;
121 hist->head.flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
122 if ((ip->flags & HAMMER_INODE_MODMASK) &
123 ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
124 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
128 * Setup the cursor. We can't handle undeletable records
129 * (create_tid of 0) at the moment. A create_tid of 0 has
130 * a special meaning and cannot be specified in the cursor.
132 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
134 hammer_done_cursor(&cursor);
138 cursor.key_beg.obj_id = hist->obj_id;
139 cursor.key_beg.create_tid = hist->beg_tid;
140 cursor.key_beg.delete_tid = 0;
141 cursor.key_beg.obj_type = 0;
142 if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
143 cursor.key_beg.create_tid = 1;
145 cursor.key_end.obj_id = hist->obj_id;
146 cursor.key_end.create_tid = hist->end_tid;
147 cursor.key_end.delete_tid = 0;
148 cursor.key_end.obj_type = 0;
150 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
152 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
154 * key-range within the file. For a regular file the
155 * on-disk key represents BASE+LEN, not BASE, so the
156 * first possible record containing the offset 'key'
157 * has an on-disk key of (key + 1).
159 cursor.key_beg.key = hist->key;
160 cursor.key_end.key = HAMMER_MAX_KEY;
161 cursor.key_beg.localization = ip->obj_localization +
162 HAMMER_LOCALIZE_MISC;
163 cursor.key_end.localization = ip->obj_localization +
164 HAMMER_LOCALIZE_MISC;
166 switch(ip->ino_data.obj_type) {
167 case HAMMER_OBJTYPE_REGFILE:
168 ++cursor.key_beg.key;
169 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
171 case HAMMER_OBJTYPE_DIRECTORY:
172 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
174 case HAMMER_OBJTYPE_DBFILE:
175 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
181 cursor.key_end.rec_type = cursor.key_beg.rec_type;
186 cursor.key_beg.key = 0;
187 cursor.key_end.key = 0;
188 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
189 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
190 cursor.key_beg.localization = ip->obj_localization +
191 HAMMER_LOCALIZE_INODE;
192 cursor.key_end.localization = ip->obj_localization +
193 HAMMER_LOCALIZE_INODE;
196 error = hammer_btree_first(&cursor);
198 elm = &cursor.node->ondisk->elms[cursor.index];
200 add_history(ip, hist, elm);
201 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
202 HAMMER_IOC_HISTORY_NEXT_KEY |
203 HAMMER_IOC_HISTORY_EOF)) {
206 error = hammer_btree_iterate(&cursor);
208 if (error == ENOENT) {
209 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
212 hammer_done_cursor(&cursor);
217 * Add the scanned element to the ioctl return structure. Some special
218 * casing is required for regular files to accomodate how data ranges are
222 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
223 hammer_btree_elm_t elm)
225 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
227 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
228 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
232 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
233 hist->key < elm->leaf.base.key - elm->leaf.data_len) {
234 hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
236 if (hist->nxt_key > elm->leaf.base.key)
237 hist->nxt_key = elm->leaf.base.key;
240 * Record is beyond MAXPHYS, there won't be any more records
241 * in the iteration covering the requested offset (key).
243 if (elm->leaf.base.key >= MAXPHYS &&
244 elm->leaf.base.key - MAXPHYS > hist->key) {
245 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
249 * Data-range of record does not cover the key.
251 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
254 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
258 if (hist->nxt_key > elm->leaf.base.key &&
259 hist->key < elm->leaf.base.key) {
260 hist->nxt_key = elm->leaf.base.key;
264 * Record is beyond the requested key.
266 if (elm->leaf.base.key > hist->key)
267 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
271 * Add create_tid if it is in-bounds.
273 if ((hist->count == 0 ||
274 elm->leaf.base.create_tid != hist->tid_ary[hist->count - 1]) &&
275 elm->leaf.base.create_tid >= hist->beg_tid &&
276 elm->leaf.base.create_tid < hist->end_tid) {
277 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
278 hist->nxt_tid = elm->leaf.base.create_tid;
279 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
282 hist->tid_ary[hist->count++] = elm->leaf.base.create_tid;
286 * Add delete_tid if it is in-bounds. Note that different portions
287 * of the history may have overlapping data ranges with different
288 * delete_tid's. If this case occurs the delete_tid may match the
289 * create_tid of a following record. XXX
294 if (elm->leaf.base.delete_tid &&
295 elm->leaf.base.delete_tid >= hist->beg_tid &&
296 elm->leaf.base.delete_tid < hist->end_tid) {
297 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
298 hist->nxt_tid = elm->leaf.base.delete_tid;
299 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
302 hist->tid_ary[hist->count++] = elm->leaf.base.delete_tid;
307 * Acquire synchronization TID
311 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
312 struct hammer_ioc_synctid *std)
314 hammer_mount_t hmp = ip->hmp;
318 case HAMMER_SYNCTID_NONE:
319 std->tid = hmp->flusher.tid; /* inaccurate */
321 case HAMMER_SYNCTID_ASYNC:
322 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
323 std->tid = hmp->flusher.tid; /* inaccurate */
324 hammer_flusher_async(hmp);
326 case HAMMER_SYNCTID_SYNC1:
327 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
328 hammer_flusher_sync(hmp);
329 std->tid = hmp->flusher.tid;
331 case HAMMER_SYNCTID_SYNC2:
332 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
333 hammer_flusher_sync(hmp);
334 std->tid = hmp->flusher.tid;
335 hammer_flusher_sync(hmp);
345 * Return the pseudoid for a pseudo-filesystem.
348 hammer_ioc_get_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
349 struct hammer_ioc_get_pseudofs *pfs)
351 pfs->pseudoid = ip->obj_localization;