HAMMER 59A/Many: Mirroring related work (and one bug fix).
[dragonfly.git] / sys / vfs / hammer / hammer_ioctl.c
blobcf25cbbf29bd739bf489bdc6663cb4ab90dd926c
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.24 2008/06/26 04:06:23 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);
43 static int hammer_ioc_get_pseudofs(hammer_transaction_t trans,
44 hammer_inode_t ip,
45 struct hammer_ioc_get_pseudofs *pfs);
47 int
48 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
49 struct ucred *cred)
51 struct hammer_transaction trans;
52 int error;
54 error = suser_cred(cred, PRISON_ROOT);
56 hammer_start_transaction(&trans, ip->hmp);
58 switch(com) {
59 case HAMMERIOC_PRUNE:
60 if (error == 0) {
61 error = hammer_ioc_prune(&trans, ip,
62 (struct hammer_ioc_prune *)data);
64 break;
65 case HAMMERIOC_GETHISTORY:
66 error = hammer_ioc_gethistory(&trans, ip,
67 (struct hammer_ioc_history *)data);
68 break;
69 case HAMMERIOC_REBLOCK:
70 error = hammer_ioc_reblock(&trans, ip,
71 (struct hammer_ioc_reblock *)data);
72 break;
73 case HAMMERIOC_SYNCTID:
74 error = hammer_ioc_synctid(&trans, ip,
75 (struct hammer_ioc_synctid *)data);
76 break;
77 case HAMMERIOC_GET_PSEUDOFS:
78 error = hammer_ioc_get_pseudofs(&trans, ip,
79 (struct hammer_ioc_get_pseudofs *)data);
80 break;
81 case HAMMERIOC_MIRROR_READ:
82 error = hammer_ioc_mirror_read(&trans, ip,
83 (struct hammer_ioc_mirror_rw *)data);
84 break;
85 case HAMMERIOC_MIRROR_WRITE:
86 error = hammer_ioc_mirror_write(&trans, ip,
87 (struct hammer_ioc_mirror_rw *)data);
88 break;
89 default:
90 error = EOPNOTSUPP;
91 break;
93 hammer_done_transaction(&trans);
94 return (error);
98 * Iterate through an object's inode or an object's records and record
99 * modification TIDs.
101 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
102 hammer_btree_elm_t elm);
104 static
106 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
107 struct hammer_ioc_history *hist)
109 struct hammer_cursor cursor;
110 hammer_btree_elm_t elm;
111 int error;
114 * Validate the structure and initialize for return.
116 if (hist->beg_tid > hist->end_tid)
117 return(EINVAL);
118 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
119 if (hist->key > hist->nxt_key)
120 return(EINVAL);
123 hist->obj_id = ip->obj_id;
124 hist->count = 0;
125 hist->nxt_tid = hist->end_tid;
126 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
127 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
128 hist->head.flags &= ~HAMMER_IOC_HISTORY_EOF;
129 hist->head.flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
130 if ((ip->flags & HAMMER_INODE_MODMASK) &
131 ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
132 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
136 * Setup the cursor. We can't handle undeletable records
137 * (create_tid of 0) at the moment. A create_tid of 0 has
138 * a special meaning and cannot be specified in the cursor.
140 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
141 if (error) {
142 hammer_done_cursor(&cursor);
143 return(error);
146 cursor.key_beg.obj_id = hist->obj_id;
147 cursor.key_beg.create_tid = hist->beg_tid;
148 cursor.key_beg.delete_tid = 0;
149 cursor.key_beg.obj_type = 0;
150 if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
151 cursor.key_beg.create_tid = 1;
153 cursor.key_end.obj_id = hist->obj_id;
154 cursor.key_end.create_tid = hist->end_tid;
155 cursor.key_end.delete_tid = 0;
156 cursor.key_end.obj_type = 0;
158 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
160 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
162 * key-range within the file. For a regular file the
163 * on-disk key represents BASE+LEN, not BASE, so the
164 * first possible record containing the offset 'key'
165 * has an on-disk key of (key + 1).
167 cursor.key_beg.key = hist->key;
168 cursor.key_end.key = HAMMER_MAX_KEY;
169 cursor.key_beg.localization = ip->obj_localization +
170 HAMMER_LOCALIZE_MISC;
171 cursor.key_end.localization = ip->obj_localization +
172 HAMMER_LOCALIZE_MISC;
174 switch(ip->ino_data.obj_type) {
175 case HAMMER_OBJTYPE_REGFILE:
176 ++cursor.key_beg.key;
177 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
178 break;
179 case HAMMER_OBJTYPE_DIRECTORY:
180 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
181 break;
182 case HAMMER_OBJTYPE_DBFILE:
183 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
184 break;
185 default:
186 error = EINVAL;
187 break;
189 cursor.key_end.rec_type = cursor.key_beg.rec_type;
190 } else {
192 * The inode itself.
194 cursor.key_beg.key = 0;
195 cursor.key_end.key = 0;
196 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
197 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
198 cursor.key_beg.localization = ip->obj_localization +
199 HAMMER_LOCALIZE_INODE;
200 cursor.key_end.localization = ip->obj_localization +
201 HAMMER_LOCALIZE_INODE;
204 error = hammer_btree_first(&cursor);
205 while (error == 0) {
206 elm = &cursor.node->ondisk->elms[cursor.index];
208 add_history(ip, hist, elm);
209 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
210 HAMMER_IOC_HISTORY_NEXT_KEY |
211 HAMMER_IOC_HISTORY_EOF)) {
212 break;
214 error = hammer_btree_iterate(&cursor);
216 if (error == ENOENT) {
217 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
218 error = 0;
220 hammer_done_cursor(&cursor);
221 return(error);
225 * Add the scanned element to the ioctl return structure. Some special
226 * casing is required for regular files to accomodate how data ranges are
227 * stored on-disk.
229 static void
230 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
231 hammer_btree_elm_t elm)
233 int i;
235 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
236 return;
237 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
238 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
240 * Adjust nxt_key
242 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
243 hist->key < elm->leaf.base.key - elm->leaf.data_len) {
244 hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
246 if (hist->nxt_key > elm->leaf.base.key)
247 hist->nxt_key = elm->leaf.base.key;
250 * Record is beyond MAXPHYS, there won't be any more records
251 * in the iteration covering the requested offset (key).
253 if (elm->leaf.base.key >= MAXPHYS &&
254 elm->leaf.base.key - MAXPHYS > hist->key) {
255 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
259 * Data-range of record does not cover the key.
261 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
262 return;
264 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
266 * Adjust nxt_key
268 if (hist->nxt_key > elm->leaf.base.key &&
269 hist->key < elm->leaf.base.key) {
270 hist->nxt_key = elm->leaf.base.key;
274 * Record is beyond the requested key.
276 if (elm->leaf.base.key > hist->key)
277 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
281 * Add create_tid if it is in-bounds.
283 i = hist->count;
284 if ((i == 0 ||
285 elm->leaf.base.create_tid != hist->hist_ary[i - 1].tid) &&
286 elm->leaf.base.create_tid >= hist->beg_tid &&
287 elm->leaf.base.create_tid < hist->end_tid) {
288 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
289 hist->nxt_tid = elm->leaf.base.create_tid;
290 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
291 return;
293 hist->hist_ary[i].tid = elm->leaf.base.create_tid;
294 hist->hist_ary[i].time32 = elm->leaf.create_ts;
295 ++hist->count;
299 * Add delete_tid if it is in-bounds. Note that different portions
300 * of the history may have overlapping data ranges with different
301 * delete_tid's. If this case occurs the delete_tid may match the
302 * create_tid of a following record. XXX
304 * [ ]
305 * [ ]
307 i = hist->count;
308 if (elm->leaf.base.delete_tid &&
309 elm->leaf.base.delete_tid >= hist->beg_tid &&
310 elm->leaf.base.delete_tid < hist->end_tid) {
311 if (i == HAMMER_MAX_HISTORY_ELMS) {
312 hist->nxt_tid = elm->leaf.base.delete_tid;
313 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
314 return;
316 hist->hist_ary[i].tid = elm->leaf.base.delete_tid;
317 hist->hist_ary[i].time32 = elm->leaf.delete_ts;
318 ++hist->count;
323 * Acquire synchronization TID
325 static
327 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
328 struct hammer_ioc_synctid *std)
330 hammer_mount_t hmp = ip->hmp;
331 int error = 0;
333 switch(std->op) {
334 case HAMMER_SYNCTID_NONE:
335 std->tid = hmp->flusher.tid; /* inaccurate */
336 break;
337 case HAMMER_SYNCTID_ASYNC:
338 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
339 std->tid = hmp->flusher.tid; /* inaccurate */
340 hammer_flusher_async(hmp);
341 break;
342 case HAMMER_SYNCTID_SYNC1:
343 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
344 hammer_flusher_sync(hmp);
345 std->tid = hmp->flusher.tid;
346 break;
347 case HAMMER_SYNCTID_SYNC2:
348 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
349 hammer_flusher_sync(hmp);
350 std->tid = hmp->flusher.tid;
351 hammer_flusher_sync(hmp);
352 break;
353 default:
354 error = EOPNOTSUPP;
355 break;
357 return(error);
361 * Return the pseudoid for a pseudo-filesystem.
363 static int
364 hammer_ioc_get_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
365 struct hammer_ioc_get_pseudofs *pfs)
367 pfs->pseudoid = ip->obj_localization;
368 return(0);