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.17 2008/05/12 21:17:18 dillon Exp $
39 static int hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
40 struct hammer_ioc_history *hist);
43 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
46 struct hammer_transaction trans;
49 error = suser_cred(cred, PRISON_ROOT);
51 hammer_start_transaction(&trans, ip->hmp);
56 error = hammer_ioc_prune(&trans, ip,
57 (struct hammer_ioc_prune *)data);
60 case HAMMERIOC_GETHISTORY:
61 error = hammer_ioc_gethistory(&trans, ip,
62 (struct hammer_ioc_history *)data);
64 case HAMMERIOC_REBLOCK:
65 error = hammer_ioc_reblock(&trans, ip,
66 (struct hammer_ioc_reblock *)data);
72 hammer_done_transaction(&trans);
77 * Iterate through an object's inode or an object's records and record
80 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
81 hammer_btree_elm_t elm);
85 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
86 struct hammer_ioc_history *hist)
88 struct hammer_cursor cursor;
89 hammer_btree_elm_t elm;
93 * Validate the structure and initialize for return.
95 if (hist->beg_tid > hist->end_tid)
97 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
98 if (hist->key > hist->nxt_key)
102 hist->obj_id = ip->obj_id;
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;
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.
117 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
119 hammer_done_cursor(&cursor);
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;
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;
135 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
137 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
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).
144 cursor.key_beg.key = hist->key;
145 cursor.key_end.key = HAMMER_MAX_KEY;
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;
152 case HAMMER_OBJTYPE_DIRECTORY:
153 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
155 case HAMMER_OBJTYPE_DBFILE:
156 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
162 cursor.key_end.rec_type = cursor.key_beg.rec_type;
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;
173 error = hammer_btree_first(&cursor);
175 elm = &cursor.node->ondisk->elms[cursor.index];
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)) {
183 error = hammer_btree_iterate(&cursor);
185 if (error == ENOENT) {
186 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
189 hammer_done_cursor(&cursor);
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
199 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
200 hammer_btree_elm_t elm)
202 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
204 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
205 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
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;
213 if (hist->nxt_key > elm->leaf.base.key)
214 hist->nxt_key = elm->leaf.base.key;
217 * Record is beyond MAXPHYS, there won't be any more records
218 * in the iteration covering the requested offset (key).
220 if (elm->leaf.base.key >= MAXPHYS &&
221 elm->leaf.base.key - MAXPHYS > hist->key) {
222 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
226 * Data-range of record does not cover the key.
228 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
231 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
235 if (hist->nxt_key > elm->leaf.base.key &&
236 hist->key < elm->leaf.base.key) {
237 hist->nxt_key = elm->leaf.base.key;
241 * Record is beyond the requested key.
243 if (elm->leaf.base.key > hist->key)
244 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
248 * Add create_tid if it is in-bounds.
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;
259 hist->tid_ary[hist->count++] = elm->leaf.base.create_tid;
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
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;
279 hist->tid_ary[hist->count++] = elm->leaf.base.delete_tid;