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.28 2008/07/12 23:04:50 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
);
45 hammer_ioctl(hammer_inode_t ip
, u_long com
, caddr_t data
, int fflag
,
48 struct hammer_transaction trans
;
51 error
= suser_cred(cred
, PRISON_ROOT
);
53 hammer_start_transaction(&trans
, ip
->hmp
);
58 error
= hammer_ioc_prune(&trans
, ip
,
59 (struct hammer_ioc_prune
*)data
);
62 case HAMMERIOC_GETHISTORY
:
63 error
= hammer_ioc_gethistory(&trans
, ip
,
64 (struct hammer_ioc_history
*)data
);
66 case HAMMERIOC_REBLOCK
:
68 error
= hammer_ioc_reblock(&trans
, ip
,
69 (struct hammer_ioc_reblock
*)data
);
72 case HAMMERIOC_SYNCTID
:
73 error
= hammer_ioc_synctid(&trans
, ip
,
74 (struct hammer_ioc_synctid
*)data
);
76 case HAMMERIOC_GET_PSEUDOFS
:
77 error
= hammer_ioc_get_pseudofs(&trans
, ip
,
78 (struct hammer_ioc_pseudofs_rw
*)data
);
80 case HAMMERIOC_SET_PSEUDOFS
:
82 error
= hammer_ioc_set_pseudofs(&trans
, ip
, cred
,
83 (struct hammer_ioc_pseudofs_rw
*)data
);
86 case HAMMERIOC_UPG_PSEUDOFS
:
88 error
= hammer_ioc_upgrade_pseudofs(&trans
, ip
,
89 (struct hammer_ioc_pseudofs_rw
*)data
);
92 case HAMMERIOC_DGD_PSEUDOFS
:
94 error
= hammer_ioc_downgrade_pseudofs(&trans
, ip
,
95 (struct hammer_ioc_pseudofs_rw
*)data
);
98 case HAMMERIOC_RMR_PSEUDOFS
:
100 error
= hammer_ioc_destroy_pseudofs(&trans
, ip
,
101 (struct hammer_ioc_pseudofs_rw
*)data
);
104 case HAMMERIOC_MIRROR_READ
:
106 error
= hammer_ioc_mirror_read(&trans
, ip
,
107 (struct hammer_ioc_mirror_rw
*)data
);
110 case HAMMERIOC_MIRROR_WRITE
:
112 error
= hammer_ioc_mirror_write(&trans
, ip
,
113 (struct hammer_ioc_mirror_rw
*)data
);
120 hammer_done_transaction(&trans
);
125 * Iterate through an object's inode or an object's records and record
128 static void add_history(hammer_inode_t ip
, struct hammer_ioc_history
*hist
,
129 hammer_btree_elm_t elm
);
133 hammer_ioc_gethistory(hammer_transaction_t trans
, hammer_inode_t ip
,
134 struct hammer_ioc_history
*hist
)
136 struct hammer_cursor cursor
;
137 hammer_btree_elm_t elm
;
141 * Validate the structure and initialize for return.
143 if (hist
->beg_tid
> hist
->end_tid
)
145 if (hist
->head
.flags
& HAMMER_IOC_HISTORY_ATKEY
) {
146 if (hist
->key
> hist
->nxt_key
)
150 hist
->obj_id
= ip
->obj_id
;
152 hist
->nxt_tid
= hist
->end_tid
;
153 hist
->head
.flags
&= ~HAMMER_IOC_HISTORY_NEXT_TID
;
154 hist
->head
.flags
&= ~HAMMER_IOC_HISTORY_NEXT_KEY
;
155 hist
->head
.flags
&= ~HAMMER_IOC_HISTORY_EOF
;
156 hist
->head
.flags
&= ~HAMMER_IOC_HISTORY_UNSYNCED
;
157 if ((ip
->flags
& HAMMER_INODE_MODMASK
) &
158 ~(HAMMER_INODE_ATIME
| HAMMER_INODE_MTIME
)) {
159 hist
->head
.flags
|= HAMMER_IOC_HISTORY_UNSYNCED
;
163 * Setup the cursor. We can't handle undeletable records
164 * (create_tid of 0) at the moment. A create_tid of 0 has
165 * a special meaning and cannot be specified in the cursor.
167 error
= hammer_init_cursor(trans
, &cursor
, &ip
->cache
[0], NULL
);
169 hammer_done_cursor(&cursor
);
173 cursor
.key_beg
.obj_id
= hist
->obj_id
;
174 cursor
.key_beg
.create_tid
= hist
->beg_tid
;
175 cursor
.key_beg
.delete_tid
= 0;
176 cursor
.key_beg
.obj_type
= 0;
177 if (cursor
.key_beg
.create_tid
== HAMMER_MIN_TID
)
178 cursor
.key_beg
.create_tid
= 1;
180 cursor
.key_end
.obj_id
= hist
->obj_id
;
181 cursor
.key_end
.create_tid
= hist
->end_tid
;
182 cursor
.key_end
.delete_tid
= 0;
183 cursor
.key_end
.obj_type
= 0;
185 cursor
.flags
|= HAMMER_CURSOR_END_EXCLUSIVE
;
187 if (hist
->head
.flags
& HAMMER_IOC_HISTORY_ATKEY
) {
189 * key-range within the file. For a regular file the
190 * on-disk key represents BASE+LEN, not BASE, so the
191 * first possible record containing the offset 'key'
192 * has an on-disk key of (key + 1).
194 cursor
.key_beg
.key
= hist
->key
;
195 cursor
.key_end
.key
= HAMMER_MAX_KEY
;
196 cursor
.key_beg
.localization
= ip
->obj_localization
+
197 HAMMER_LOCALIZE_MISC
;
198 cursor
.key_end
.localization
= ip
->obj_localization
+
199 HAMMER_LOCALIZE_MISC
;
201 switch(ip
->ino_data
.obj_type
) {
202 case HAMMER_OBJTYPE_REGFILE
:
203 ++cursor
.key_beg
.key
;
204 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DATA
;
206 case HAMMER_OBJTYPE_DIRECTORY
:
207 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
209 case HAMMER_OBJTYPE_DBFILE
:
210 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DB
;
216 cursor
.key_end
.rec_type
= cursor
.key_beg
.rec_type
;
221 cursor
.key_beg
.key
= 0;
222 cursor
.key_end
.key
= 0;
223 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_INODE
;
224 cursor
.key_end
.rec_type
= HAMMER_RECTYPE_INODE
;
225 cursor
.key_beg
.localization
= ip
->obj_localization
+
226 HAMMER_LOCALIZE_INODE
;
227 cursor
.key_end
.localization
= ip
->obj_localization
+
228 HAMMER_LOCALIZE_INODE
;
231 error
= hammer_btree_first(&cursor
);
233 elm
= &cursor
.node
->ondisk
->elms
[cursor
.index
];
235 add_history(ip
, hist
, elm
);
236 if (hist
->head
.flags
& (HAMMER_IOC_HISTORY_NEXT_TID
|
237 HAMMER_IOC_HISTORY_NEXT_KEY
|
238 HAMMER_IOC_HISTORY_EOF
)) {
241 error
= hammer_btree_iterate(&cursor
);
243 if (error
== ENOENT
) {
244 hist
->head
.flags
|= HAMMER_IOC_HISTORY_EOF
;
247 hammer_done_cursor(&cursor
);
252 * Add the scanned element to the ioctl return structure. Some special
253 * casing is required for regular files to accomodate how data ranges are
257 add_history(hammer_inode_t ip
, struct hammer_ioc_history
*hist
,
258 hammer_btree_elm_t elm
)
262 if (elm
->base
.btype
!= HAMMER_BTREE_TYPE_RECORD
)
264 if ((hist
->head
.flags
& HAMMER_IOC_HISTORY_ATKEY
) &&
265 ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_REGFILE
) {
269 if (hist
->nxt_key
> elm
->leaf
.base
.key
- elm
->leaf
.data_len
&&
270 hist
->key
< elm
->leaf
.base
.key
- elm
->leaf
.data_len
) {
271 hist
->nxt_key
= elm
->leaf
.base
.key
- elm
->leaf
.data_len
;
273 if (hist
->nxt_key
> elm
->leaf
.base
.key
)
274 hist
->nxt_key
= elm
->leaf
.base
.key
;
277 * Record is beyond MAXPHYS, there won't be any more records
278 * in the iteration covering the requested offset (key).
280 if (elm
->leaf
.base
.key
>= MAXPHYS
&&
281 elm
->leaf
.base
.key
- MAXPHYS
> hist
->key
) {
282 hist
->head
.flags
|= HAMMER_IOC_HISTORY_NEXT_KEY
;
286 * Data-range of record does not cover the key.
288 if (elm
->leaf
.base
.key
- elm
->leaf
.data_len
> hist
->key
)
291 } else if (hist
->head
.flags
& HAMMER_IOC_HISTORY_ATKEY
) {
295 if (hist
->nxt_key
> elm
->leaf
.base
.key
&&
296 hist
->key
< elm
->leaf
.base
.key
) {
297 hist
->nxt_key
= elm
->leaf
.base
.key
;
301 * Record is beyond the requested key.
303 if (elm
->leaf
.base
.key
> hist
->key
)
304 hist
->head
.flags
|= HAMMER_IOC_HISTORY_NEXT_KEY
;
308 * Add create_tid if it is in-bounds.
312 elm
->leaf
.base
.create_tid
!= hist
->hist_ary
[i
- 1].tid
) &&
313 elm
->leaf
.base
.create_tid
>= hist
->beg_tid
&&
314 elm
->leaf
.base
.create_tid
< hist
->end_tid
) {
315 if (hist
->count
== HAMMER_MAX_HISTORY_ELMS
) {
316 hist
->nxt_tid
= elm
->leaf
.base
.create_tid
;
317 hist
->head
.flags
|= HAMMER_IOC_HISTORY_NEXT_TID
;
320 hist
->hist_ary
[i
].tid
= elm
->leaf
.base
.create_tid
;
321 hist
->hist_ary
[i
].time32
= elm
->leaf
.create_ts
;
326 * Add delete_tid if it is in-bounds. Note that different portions
327 * of the history may have overlapping data ranges with different
328 * delete_tid's. If this case occurs the delete_tid may match the
329 * create_tid of a following record. XXX
335 if (elm
->leaf
.base
.delete_tid
&&
336 elm
->leaf
.base
.delete_tid
>= hist
->beg_tid
&&
337 elm
->leaf
.base
.delete_tid
< hist
->end_tid
) {
338 if (i
== HAMMER_MAX_HISTORY_ELMS
) {
339 hist
->nxt_tid
= elm
->leaf
.base
.delete_tid
;
340 hist
->head
.flags
|= HAMMER_IOC_HISTORY_NEXT_TID
;
343 hist
->hist_ary
[i
].tid
= elm
->leaf
.base
.delete_tid
;
344 hist
->hist_ary
[i
].time32
= elm
->leaf
.delete_ts
;
350 * Acquire synchronization TID
354 hammer_ioc_synctid(hammer_transaction_t trans
, hammer_inode_t ip
,
355 struct hammer_ioc_synctid
*std
)
357 hammer_mount_t hmp
= ip
->hmp
;
361 case HAMMER_SYNCTID_NONE
:
362 std
->tid
= hmp
->flusher
.tid
; /* inaccurate */
364 case HAMMER_SYNCTID_ASYNC
:
365 hammer_queue_inodes_flusher(hmp
, MNT_NOWAIT
);
366 std
->tid
= hmp
->flusher
.tid
; /* inaccurate */
367 hammer_flusher_async(hmp
, NULL
);
369 case HAMMER_SYNCTID_SYNC1
:
370 hammer_queue_inodes_flusher(hmp
, MNT_WAIT
);
371 hammer_flusher_sync(hmp
);
372 std
->tid
= hmp
->flusher
.tid
;
374 case HAMMER_SYNCTID_SYNC2
:
375 hammer_queue_inodes_flusher(hmp
, MNT_WAIT
);
376 hammer_flusher_sync(hmp
);
377 std
->tid
= hmp
->flusher
.tid
;
378 hammer_flusher_sync(hmp
);