HAMMER 56D/Many: Media structure finalization, atime/mtime, etc.
[dragonfly.git] / sys / vfs / hammer / hammer_ioctl.c
blob80b411f99e90acc9f21261487418b24335f2f45b
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.21 2008/06/20 21:24:53 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) &
116 ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
117 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
121 * Setup the cursor. We can't handle undeletable records
122 * (create_tid of 0) at the moment. A create_tid of 0 has
123 * a special meaning and cannot be specified in the cursor.
125 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
126 if (error) {
127 hammer_done_cursor(&cursor);
128 return(error);
131 cursor.key_beg.obj_id = hist->obj_id;
132 cursor.key_beg.create_tid = hist->beg_tid;
133 cursor.key_beg.delete_tid = 0;
134 cursor.key_beg.obj_type = 0;
135 if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
136 cursor.key_beg.create_tid = 1;
138 cursor.key_end.obj_id = hist->obj_id;
139 cursor.key_end.create_tid = hist->end_tid;
140 cursor.key_end.delete_tid = 0;
141 cursor.key_end.obj_type = 0;
143 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
145 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
147 * key-range within the file. For a regular file the
148 * on-disk key represents BASE+LEN, not BASE, so the
149 * first possible record containing the offset 'key'
150 * has an on-disk key of (key + 1).
152 cursor.key_beg.key = hist->key;
153 cursor.key_end.key = HAMMER_MAX_KEY;
154 cursor.key_beg.localization = HAMMER_LOCALIZE_MISC;
155 cursor.key_end.localization = HAMMER_LOCALIZE_MISC;
157 switch(ip->ino_data.obj_type) {
158 case HAMMER_OBJTYPE_REGFILE:
159 ++cursor.key_beg.key;
160 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
161 break;
162 case HAMMER_OBJTYPE_DIRECTORY:
163 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
164 break;
165 case HAMMER_OBJTYPE_DBFILE:
166 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
167 break;
168 default:
169 error = EINVAL;
170 break;
172 cursor.key_end.rec_type = cursor.key_beg.rec_type;
173 } else {
175 * The inode itself.
177 cursor.key_beg.key = 0;
178 cursor.key_end.key = 0;
179 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
180 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
181 cursor.key_beg.localization = HAMMER_LOCALIZE_INODE;
182 cursor.key_end.localization = HAMMER_LOCALIZE_INODE;
185 error = hammer_btree_first(&cursor);
186 while (error == 0) {
187 elm = &cursor.node->ondisk->elms[cursor.index];
189 add_history(ip, hist, elm);
190 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
191 HAMMER_IOC_HISTORY_NEXT_KEY |
192 HAMMER_IOC_HISTORY_EOF)) {
193 break;
195 error = hammer_btree_iterate(&cursor);
197 if (error == ENOENT) {
198 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
199 error = 0;
201 hammer_done_cursor(&cursor);
202 return(error);
206 * Add the scanned element to the ioctl return structure. Some special
207 * casing is required for regular files to accomodate how data ranges are
208 * stored on-disk.
210 static void
211 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
212 hammer_btree_elm_t elm)
214 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
215 return;
216 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
217 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
219 * Adjust nxt_key
221 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
222 hist->key < elm->leaf.base.key - elm->leaf.data_len) {
223 hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
225 if (hist->nxt_key > elm->leaf.base.key)
226 hist->nxt_key = elm->leaf.base.key;
229 * Record is beyond MAXPHYS, there won't be any more records
230 * in the iteration covering the requested offset (key).
232 if (elm->leaf.base.key >= MAXPHYS &&
233 elm->leaf.base.key - MAXPHYS > hist->key) {
234 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
238 * Data-range of record does not cover the key.
240 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
241 return;
243 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
245 * Adjust nxt_key
247 if (hist->nxt_key > elm->leaf.base.key &&
248 hist->key < elm->leaf.base.key) {
249 hist->nxt_key = elm->leaf.base.key;
253 * Record is beyond the requested key.
255 if (elm->leaf.base.key > hist->key)
256 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
260 * Add create_tid if it is in-bounds.
262 if ((hist->count == 0 ||
263 elm->leaf.base.create_tid != hist->tid_ary[hist->count - 1]) &&
264 elm->leaf.base.create_tid >= hist->beg_tid &&
265 elm->leaf.base.create_tid < hist->end_tid) {
266 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
267 hist->nxt_tid = elm->leaf.base.create_tid;
268 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
269 return;
271 hist->tid_ary[hist->count++] = elm->leaf.base.create_tid;
275 * Add delete_tid if it is in-bounds. Note that different portions
276 * of the history may have overlapping data ranges with different
277 * delete_tid's. If this case occurs the delete_tid may match the
278 * create_tid of a following record. XXX
280 * [ ]
281 * [ ]
283 if (elm->leaf.base.delete_tid &&
284 elm->leaf.base.delete_tid >= hist->beg_tid &&
285 elm->leaf.base.delete_tid < hist->end_tid) {
286 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
287 hist->nxt_tid = elm->leaf.base.delete_tid;
288 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
289 return;
291 hist->tid_ary[hist->count++] = elm->leaf.base.delete_tid;
296 * Acquire synchronization TID
298 static
300 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
301 struct hammer_ioc_synctid *std)
303 hammer_mount_t hmp = ip->hmp;
304 int error = 0;
306 switch(std->op) {
307 case HAMMER_SYNCTID_NONE:
308 std->tid = hmp->flusher.tid; /* inaccurate */
309 break;
310 case HAMMER_SYNCTID_ASYNC:
311 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
312 std->tid = hmp->flusher.tid; /* inaccurate */
313 hammer_flusher_async(hmp);
314 break;
315 case HAMMER_SYNCTID_SYNC1:
316 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
317 hammer_flusher_sync(hmp);
318 std->tid = hmp->flusher.tid;
319 break;
320 case HAMMER_SYNCTID_SYNC2:
321 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
322 hammer_flusher_sync(hmp);
323 std->tid = hmp->flusher.tid;
324 hammer_flusher_sync(hmp);
325 break;
326 default:
327 error = EOPNOTSUPP;
328 break;
330 return(error);