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.24 2008/06/26 04:06:23 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
);
43 static int hammer_ioc_get_pseudofs(hammer_transaction_t trans
,
45 struct hammer_ioc_get_pseudofs
*pfs
);
48 hammer_ioctl(hammer_inode_t ip
, u_long com
, caddr_t data
, int fflag
,
51 struct hammer_transaction trans
;
54 error
= suser_cred(cred
, PRISON_ROOT
);
56 hammer_start_transaction(&trans
, ip
->hmp
);
61 error
= hammer_ioc_prune(&trans
, ip
,
62 (struct hammer_ioc_prune
*)data
);
65 case HAMMERIOC_GETHISTORY
:
66 error
= hammer_ioc_gethistory(&trans
, ip
,
67 (struct hammer_ioc_history
*)data
);
69 case HAMMERIOC_REBLOCK
:
70 error
= hammer_ioc_reblock(&trans
, ip
,
71 (struct hammer_ioc_reblock
*)data
);
73 case HAMMERIOC_SYNCTID
:
74 error
= hammer_ioc_synctid(&trans
, ip
,
75 (struct hammer_ioc_synctid
*)data
);
77 case HAMMERIOC_GET_PSEUDOFS
:
78 error
= hammer_ioc_get_pseudofs(&trans
, ip
,
79 (struct hammer_ioc_get_pseudofs
*)data
);
81 case HAMMERIOC_MIRROR_READ
:
82 error
= hammer_ioc_mirror_read(&trans
, ip
,
83 (struct hammer_ioc_mirror_rw
*)data
);
85 case HAMMERIOC_MIRROR_WRITE
:
86 error
= hammer_ioc_mirror_write(&trans
, ip
,
87 (struct hammer_ioc_mirror_rw
*)data
);
93 hammer_done_transaction(&trans
);
98 * Iterate through an object's inode or an object's records and record
101 static void add_history(hammer_inode_t ip
, struct hammer_ioc_history
*hist
,
102 hammer_btree_elm_t elm
);
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
;
114 * Validate the structure and initialize for return.
116 if (hist
->beg_tid
> hist
->end_tid
)
118 if (hist
->head
.flags
& HAMMER_IOC_HISTORY_ATKEY
) {
119 if (hist
->key
> hist
->nxt_key
)
123 hist
->obj_id
= ip
->obj_id
;
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
);
142 hammer_done_cursor(&cursor
);
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
;
179 case HAMMER_OBJTYPE_DIRECTORY
:
180 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DIRENTRY
;
182 case HAMMER_OBJTYPE_DBFILE
:
183 cursor
.key_beg
.rec_type
= HAMMER_RECTYPE_DB
;
189 cursor
.key_end
.rec_type
= cursor
.key_beg
.rec_type
;
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
);
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
)) {
214 error
= hammer_btree_iterate(&cursor
);
216 if (error
== ENOENT
) {
217 hist
->head
.flags
|= HAMMER_IOC_HISTORY_EOF
;
220 hammer_done_cursor(&cursor
);
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
230 add_history(hammer_inode_t ip
, struct hammer_ioc_history
*hist
,
231 hammer_btree_elm_t elm
)
235 if (elm
->base
.btype
!= HAMMER_BTREE_TYPE_RECORD
)
237 if ((hist
->head
.flags
& HAMMER_IOC_HISTORY_ATKEY
) &&
238 ip
->ino_data
.obj_type
== HAMMER_OBJTYPE_REGFILE
) {
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
)
264 } else if (hist
->head
.flags
& HAMMER_IOC_HISTORY_ATKEY
) {
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.
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
;
293 hist
->hist_ary
[i
].tid
= elm
->leaf
.base
.create_tid
;
294 hist
->hist_ary
[i
].time32
= elm
->leaf
.create_ts
;
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
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
;
316 hist
->hist_ary
[i
].tid
= elm
->leaf
.base
.delete_tid
;
317 hist
->hist_ary
[i
].time32
= elm
->leaf
.delete_ts
;
323 * Acquire synchronization TID
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
;
334 case HAMMER_SYNCTID_NONE
:
335 std
->tid
= hmp
->flusher
.tid
; /* inaccurate */
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
);
342 case HAMMER_SYNCTID_SYNC1
:
343 hammer_queue_inodes_flusher(hmp
, MNT_WAIT
);
344 hammer_flusher_sync(hmp
);
345 std
->tid
= hmp
->flusher
.tid
;
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
);
361 * Return the pseudoid for a pseudo-filesystem.
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
;