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