3dfcc036b6f617f37a57c5063afbe601da30ee50
[dragonfly.git] / sys / vfs / hammer / hammer_ioctl.c
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  * 
34  * $DragonFly: src/sys/vfs/hammer/hammer_ioctl.c,v 1.25 2008/07/02 21:57:54 dillon Exp $
35  */
36
37 #include "hammer.h"
38
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
44 int
45 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
46              struct ucred *cred)
47 {
48         struct hammer_transaction trans;
49         int error;
50
51         error = suser_cred(cred, PRISON_ROOT);
52
53         hammer_start_transaction(&trans, ip->hmp);
54
55         switch(com) {
56         case HAMMERIOC_PRUNE:
57                 if (error == 0) {
58                         error = hammer_ioc_prune(&trans, ip,
59                                         (struct hammer_ioc_prune *)data);
60                 }
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                 if (error == 0) {
68                         error = hammer_ioc_reblock(&trans, ip,
69                                         (struct hammer_ioc_reblock *)data);
70                 }
71                 break;
72         case HAMMERIOC_SYNCTID:
73                 error = hammer_ioc_synctid(&trans, ip,
74                                         (struct hammer_ioc_synctid *)data);
75                 break;
76         case HAMMERIOC_GET_PSEUDOFS:
77                 error = hammer_ioc_get_pseudofs(&trans, ip,
78                                     (struct hammer_ioc_pseudofs_rw *)data);
79                 break;
80         case HAMMERIOC_SET_PSEUDOFS:
81                 if (error == 0) {
82                         error = hammer_ioc_set_pseudofs(&trans, ip,
83                                     (struct hammer_ioc_pseudofs_rw *)data);
84                 }
85                 break;
86         case HAMMERIOC_MIRROR_READ:
87                 if (error == 0) {
88                         error = hammer_ioc_mirror_read(&trans, ip,
89                                     (struct hammer_ioc_mirror_rw *)data);
90                 }
91                 break;
92         case HAMMERIOC_MIRROR_WRITE:
93                 if (error == 0) {
94                         error = hammer_ioc_mirror_write(&trans, ip,
95                                     (struct hammer_ioc_mirror_rw *)data);
96                 }
97                 break;
98         default:
99                 error = EOPNOTSUPP;
100                 break;
101         }
102         hammer_done_transaction(&trans);
103         return (error);
104 }
105
106 /*
107  * Iterate through an object's inode or an object's records and record
108  * modification TIDs.
109  */
110 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
111                         hammer_btree_elm_t elm);
112
113 static
114 int
115 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
116                       struct hammer_ioc_history *hist)
117 {
118         struct hammer_cursor cursor;
119         hammer_btree_elm_t elm;
120         int error;
121
122         /*
123          * Validate the structure and initialize for return.
124          */
125         if (hist->beg_tid > hist->end_tid)
126                 return(EINVAL);
127         if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
128                 if (hist->key > hist->nxt_key)
129                         return(EINVAL);
130         }
131
132         hist->obj_id = ip->obj_id;
133         hist->count = 0;
134         hist->nxt_tid = hist->end_tid;
135         hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
136         hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
137         hist->head.flags &= ~HAMMER_IOC_HISTORY_EOF;
138         hist->head.flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
139         if ((ip->flags & HAMMER_INODE_MODMASK) & 
140             ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
141                 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
142         }
143
144         /*
145          * Setup the cursor.  We can't handle undeletable records
146          * (create_tid of 0) at the moment.  A create_tid of 0 has
147          * a special meaning and cannot be specified in the cursor.
148          */
149         error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
150         if (error) {
151                 hammer_done_cursor(&cursor);
152                 return(error);
153         }
154
155         cursor.key_beg.obj_id = hist->obj_id;
156         cursor.key_beg.create_tid = hist->beg_tid;
157         cursor.key_beg.delete_tid = 0;
158         cursor.key_beg.obj_type = 0;
159         if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
160                 cursor.key_beg.create_tid = 1;
161
162         cursor.key_end.obj_id = hist->obj_id;
163         cursor.key_end.create_tid = hist->end_tid;
164         cursor.key_end.delete_tid = 0;
165         cursor.key_end.obj_type = 0;
166
167         cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
168
169         if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
170                 /*
171                  * key-range within the file.  For a regular file the
172                  * on-disk key represents BASE+LEN, not BASE, so the
173                  * first possible record containing the offset 'key'
174                  * has an on-disk key of (key + 1).
175                  */
176                 cursor.key_beg.key = hist->key;
177                 cursor.key_end.key = HAMMER_MAX_KEY;
178                 cursor.key_beg.localization = ip->obj_localization + 
179                                               HAMMER_LOCALIZE_MISC;
180                 cursor.key_end.localization = ip->obj_localization + 
181                                               HAMMER_LOCALIZE_MISC;
182
183                 switch(ip->ino_data.obj_type) {
184                 case HAMMER_OBJTYPE_REGFILE:
185                         ++cursor.key_beg.key;
186                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
187                         break;
188                 case HAMMER_OBJTYPE_DIRECTORY:
189                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
190                         break;
191                 case HAMMER_OBJTYPE_DBFILE:
192                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
193                         break;
194                 default:
195                         error = EINVAL;
196                         break;
197                 }
198                 cursor.key_end.rec_type = cursor.key_beg.rec_type;
199         } else {
200                 /*
201                  * The inode itself.
202                  */
203                 cursor.key_beg.key = 0;
204                 cursor.key_end.key = 0;
205                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
206                 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
207                 cursor.key_beg.localization = ip->obj_localization +
208                                               HAMMER_LOCALIZE_INODE;
209                 cursor.key_end.localization = ip->obj_localization +
210                                               HAMMER_LOCALIZE_INODE;
211         }
212
213         error = hammer_btree_first(&cursor);
214         while (error == 0) {
215                 elm = &cursor.node->ondisk->elms[cursor.index];
216
217                 add_history(ip, hist, elm);
218                 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
219                                         HAMMER_IOC_HISTORY_NEXT_KEY |
220                                         HAMMER_IOC_HISTORY_EOF)) {
221                         break;
222                 }
223                 error = hammer_btree_iterate(&cursor);
224         }
225         if (error == ENOENT) {
226                 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
227                 error = 0;
228         }
229         hammer_done_cursor(&cursor);
230         return(error);
231 }
232
233 /*
234  * Add the scanned element to the ioctl return structure.  Some special
235  * casing is required for regular files to accomodate how data ranges are
236  * stored on-disk.
237  */
238 static void
239 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
240             hammer_btree_elm_t elm)
241 {
242         int i;
243
244         if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
245                 return;
246         if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
247             ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
248                 /*
249                  * Adjust nxt_key
250                  */
251                 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
252                     hist->key < elm->leaf.base.key - elm->leaf.data_len) {
253                         hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
254                 }
255                 if (hist->nxt_key > elm->leaf.base.key)
256                         hist->nxt_key = elm->leaf.base.key;
257
258                 /*
259                  * Record is beyond MAXPHYS, there won't be any more records
260                  * in the iteration covering the requested offset (key).
261                  */
262                 if (elm->leaf.base.key >= MAXPHYS &&
263                     elm->leaf.base.key - MAXPHYS > hist->key) {
264                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
265                 }
266
267                 /*
268                  * Data-range of record does not cover the key.
269                  */
270                 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
271                         return;
272
273         } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
274                 /*
275                  * Adjust nxt_key
276                  */
277                 if (hist->nxt_key > elm->leaf.base.key &&
278                     hist->key < elm->leaf.base.key) {
279                         hist->nxt_key = elm->leaf.base.key;
280                 }
281
282                 /*
283                  * Record is beyond the requested key.
284                  */
285                 if (elm->leaf.base.key > hist->key)
286                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
287         }
288
289         /*
290          * Add create_tid if it is in-bounds.
291          */
292         i = hist->count;
293         if ((i == 0 ||
294              elm->leaf.base.create_tid != hist->hist_ary[i - 1].tid) &&
295             elm->leaf.base.create_tid >= hist->beg_tid &&
296             elm->leaf.base.create_tid < hist->end_tid) {
297                 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
298                         hist->nxt_tid = elm->leaf.base.create_tid;
299                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
300                         return;
301                 }
302                 hist->hist_ary[i].tid = elm->leaf.base.create_tid;
303                 hist->hist_ary[i].time32 = elm->leaf.create_ts;
304                 ++hist->count;
305         }
306
307         /*
308          * Add delete_tid if it is in-bounds.  Note that different portions
309          * of the history may have overlapping data ranges with different
310          * delete_tid's.  If this case occurs the delete_tid may match the
311          * create_tid of a following record.  XXX
312          *
313          *      [        ]
314          *            [     ]
315          */
316         i = hist->count;
317         if (elm->leaf.base.delete_tid &&
318             elm->leaf.base.delete_tid >= hist->beg_tid &&
319             elm->leaf.base.delete_tid < hist->end_tid) {
320                 if (i == HAMMER_MAX_HISTORY_ELMS) {
321                         hist->nxt_tid = elm->leaf.base.delete_tid;
322                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
323                         return;
324                 }
325                 hist->hist_ary[i].tid = elm->leaf.base.delete_tid;
326                 hist->hist_ary[i].time32 = elm->leaf.delete_ts;
327                 ++hist->count;
328         }
329 }
330
331 /*
332  * Acquire synchronization TID
333  */
334 static
335 int
336 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
337                    struct hammer_ioc_synctid *std)
338 {
339         hammer_mount_t hmp = ip->hmp;
340         int error = 0;
341
342         switch(std->op) {
343         case HAMMER_SYNCTID_NONE:
344                 std->tid = hmp->flusher.tid;    /* inaccurate */
345                 break;
346         case HAMMER_SYNCTID_ASYNC:
347                 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
348                 std->tid = hmp->flusher.tid;    /* inaccurate */
349                 hammer_flusher_async(hmp);
350                 break;
351         case HAMMER_SYNCTID_SYNC1:
352                 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
353                 hammer_flusher_sync(hmp);
354                 std->tid = hmp->flusher.tid;
355                 break;
356         case HAMMER_SYNCTID_SYNC2:
357                 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
358                 hammer_flusher_sync(hmp);
359                 std->tid = hmp->flusher.tid;
360                 hammer_flusher_sync(hmp);
361                 break;
362         default:
363                 error = EOPNOTSUPP;
364                 break;
365         }
366         return(error);
367 }
368