Merge from vendor branch NETGRAPH:
[dragonfly.git] / sys / vfs / hammer / hammer_ioctl.c
CommitLineData
7dc57964
MD
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:
10 *
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.
20 *
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.
33 *
c82af904 34 * $DragonFly: src/sys/vfs/hammer/hammer_ioctl.c,v 1.24 2008/06/26 04:06:23 dillon Exp $
7dc57964
MD
35 */
36
37#include "hammer.h"
38
36f82b23 39static int hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
7dc57964 40 struct hammer_ioc_history *hist);
f36a9737
MD
41static int hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
42 struct hammer_ioc_synctid *std);
5a930e66
MD
43static int hammer_ioc_get_pseudofs(hammer_transaction_t trans,
44 hammer_inode_t ip,
45 struct hammer_ioc_get_pseudofs *pfs);
7dc57964
MD
46
47int
48hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
49 struct ucred *cred)
50{
36f82b23 51 struct hammer_transaction trans;
7dc57964
MD
52 int error;
53
54 error = suser_cred(cred, PRISON_ROOT);
55
36f82b23
MD
56 hammer_start_transaction(&trans, ip->hmp);
57
7dc57964
MD
58 switch(com) {
59 case HAMMERIOC_PRUNE:
60 if (error == 0) {
36f82b23 61 error = hammer_ioc_prune(&trans, ip,
7dc57964
MD
62 (struct hammer_ioc_prune *)data);
63 }
64 break;
65 case HAMMERIOC_GETHISTORY:
36f82b23 66 error = hammer_ioc_gethistory(&trans, ip,
7dc57964
MD
67 (struct hammer_ioc_history *)data);
68 break;
bf686dbe 69 case HAMMERIOC_REBLOCK:
36f82b23
MD
70 error = hammer_ioc_reblock(&trans, ip,
71 (struct hammer_ioc_reblock *)data);
bf686dbe 72 break;
f36a9737
MD
73 case HAMMERIOC_SYNCTID:
74 error = hammer_ioc_synctid(&trans, ip,
75 (struct hammer_ioc_synctid *)data);
76 break;
5a930e66
MD
77 case HAMMERIOC_GET_PSEUDOFS:
78 error = hammer_ioc_get_pseudofs(&trans, ip,
79 (struct hammer_ioc_get_pseudofs *)data);
80 break;
c82af904
MD
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;
7dc57964
MD
89 default:
90 error = EOPNOTSUPP;
91 break;
92 }
b84de5af 93 hammer_done_transaction(&trans);
7dc57964
MD
94 return (error);
95}
96
7dc57964
MD
97/*
98 * Iterate through an object's inode or an object's records and record
99 * modification TIDs.
100 */
101static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
102 hammer_btree_elm_t elm);
103
104static
105int
36f82b23
MD
106hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
107 struct hammer_ioc_history *hist)
7dc57964
MD
108{
109 struct hammer_cursor cursor;
110 hammer_btree_elm_t elm;
111 int error;
112
113 /*
114 * Validate the structure and initialize for return.
115 */
116 if (hist->beg_tid > hist->end_tid)
117 return(EINVAL);
19619882 118 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
7dc57964
MD
119 if (hist->key > hist->nxt_key)
120 return(EINVAL);
121 }
122
123 hist->obj_id = ip->obj_id;
124 hist->count = 0;
125 hist->nxt_tid = hist->end_tid;
19619882
MD
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;
ddfdf542
MD
130 if ((ip->flags & HAMMER_INODE_MODMASK) &
131 ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
19619882 132 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
ddfdf542 133 }
7dc57964
MD
134
135 /*
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.
139 */
4e17f465 140 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
7dc57964
MD
141 if (error) {
142 hammer_done_cursor(&cursor);
143 return(error);
144 }
145
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;
152
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;
157
158 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
159
19619882 160 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
7dc57964
MD
161 /*
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).
166 */
167 cursor.key_beg.key = hist->key;
168 cursor.key_end.key = HAMMER_MAX_KEY;
5a930e66
MD
169 cursor.key_beg.localization = ip->obj_localization +
170 HAMMER_LOCALIZE_MISC;
171 cursor.key_end.localization = ip->obj_localization +
172 HAMMER_LOCALIZE_MISC;
7dc57964 173
11ad5ade 174 switch(ip->ino_data.obj_type) {
7dc57964
MD
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;
188 }
189 cursor.key_end.rec_type = cursor.key_beg.rec_type;
190 } else {
191 /*
192 * The inode itself.
193 */
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;
5a930e66
MD
198 cursor.key_beg.localization = ip->obj_localization +
199 HAMMER_LOCALIZE_INODE;
200 cursor.key_end.localization = ip->obj_localization +
201 HAMMER_LOCALIZE_INODE;
7dc57964
MD
202 }
203
204 error = hammer_btree_first(&cursor);
205 while (error == 0) {
206 elm = &cursor.node->ondisk->elms[cursor.index];
207
208 add_history(ip, hist, elm);
19619882
MD
209 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
210 HAMMER_IOC_HISTORY_NEXT_KEY |
211 HAMMER_IOC_HISTORY_EOF)) {
7dc57964
MD
212 break;
213 }
214 error = hammer_btree_iterate(&cursor);
215 }
216 if (error == ENOENT) {
19619882 217 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
7dc57964
MD
218 error = 0;
219 }
220 hammer_done_cursor(&cursor);
221 return(error);
222}
223
224/*
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.
228 */
229static void
230add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
231 hammer_btree_elm_t elm)
232{
dd94f1b1
MD
233 int i;
234
7dc57964
MD
235 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
236 return;
19619882 237 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
11ad5ade 238 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
7dc57964
MD
239 /*
240 * Adjust nxt_key
241 */
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;
245 }
246 if (hist->nxt_key > elm->leaf.base.key)
247 hist->nxt_key = elm->leaf.base.key;
248
249 /*
250 * Record is beyond MAXPHYS, there won't be any more records
251 * in the iteration covering the requested offset (key).
252 */
253 if (elm->leaf.base.key >= MAXPHYS &&
254 elm->leaf.base.key - MAXPHYS > hist->key) {
19619882 255 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
7dc57964
MD
256 }
257
258 /*
259 * Data-range of record does not cover the key.
260 */
261 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
262 return;
263
19619882 264 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
7dc57964
MD
265 /*
266 * Adjust nxt_key
267 */
268 if (hist->nxt_key > elm->leaf.base.key &&
269 hist->key < elm->leaf.base.key) {
270 hist->nxt_key = elm->leaf.base.key;
271 }
272
273 /*
274 * Record is beyond the requested key.
275 */
276 if (elm->leaf.base.key > hist->key)
19619882 277 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
7dc57964
MD
278 }
279
280 /*
281 * Add create_tid if it is in-bounds.
282 */
dd94f1b1
MD
283 i = hist->count;
284 if ((i == 0 ||
285 elm->leaf.base.create_tid != hist->hist_ary[i - 1].tid) &&
7dc57964
MD
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;
19619882 290 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
7dc57964
MD
291 return;
292 }
dd94f1b1
MD
293 hist->hist_ary[i].tid = elm->leaf.base.create_tid;
294 hist->hist_ary[i].time32 = elm->leaf.create_ts;
295 ++hist->count;
7dc57964
MD
296 }
297
298 /*
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
303 *
304 * [ ]
305 * [ ]
306 */
dd94f1b1 307 i = hist->count;
7dc57964
MD
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) {
dd94f1b1 311 if (i == HAMMER_MAX_HISTORY_ELMS) {
7dc57964 312 hist->nxt_tid = elm->leaf.base.delete_tid;
19619882 313 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
7dc57964
MD
314 return;
315 }
dd94f1b1
MD
316 hist->hist_ary[i].tid = elm->leaf.base.delete_tid;
317 hist->hist_ary[i].time32 = elm->leaf.delete_ts;
318 ++hist->count;
7dc57964
MD
319 }
320}
321
f36a9737
MD
322/*
323 * Acquire synchronization TID
324 */
325static
326int
327hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
328 struct hammer_ioc_synctid *std)
329{
330 hammer_mount_t hmp = ip->hmp;
331 int error = 0;
332
333 switch(std->op) {
334 case HAMMER_SYNCTID_NONE:
da2da375 335 std->tid = hmp->flusher.tid; /* inaccurate */
f36a9737
MD
336 break;
337 case HAMMER_SYNCTID_ASYNC:
338 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
da2da375 339 std->tid = hmp->flusher.tid; /* inaccurate */
f36a9737
MD
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);
da2da375 345 std->tid = hmp->flusher.tid;
f36a9737
MD
346 break;
347 case HAMMER_SYNCTID_SYNC2:
348 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
349 hammer_flusher_sync(hmp);
da2da375 350 std->tid = hmp->flusher.tid;
f36a9737
MD
351 hammer_flusher_sync(hmp);
352 break;
353 default:
354 error = EOPNOTSUPP;
355 break;
356 }
357 return(error);
358}
359
5a930e66
MD
360/*
361 * Return the pseudoid for a pseudo-filesystem.
362 */
363static int
364hammer_ioc_get_pseudofs(hammer_transaction_t trans, hammer_inode_t ip,
365 struct hammer_ioc_get_pseudofs *pfs)
366{
367 pfs->pseudoid = ip->obj_localization;
368 return(0);
369}
370