Merge branch 'master' of ssh://crater.dragonflybsd.org/repository/git/dragonfly
[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.32 2008/11/13 02:23:29 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_version(hammer_transaction_t trans,
44                                 hammer_inode_t ip,
45                                 struct hammer_ioc_version *ver);
46 static int hammer_ioc_set_version(hammer_transaction_t trans,
47                                 hammer_inode_t ip,
48                                 struct hammer_ioc_version *ver);
49 static int hammer_ioc_get_info(hammer_transaction_t trans,
50                                 struct hammer_ioc_info *info);
51 static int hammer_ioc_add_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
52                                 struct hammer_ioc_snapshot *snap);
53 static int hammer_ioc_del_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
54                                 struct hammer_ioc_snapshot *snap);
55 static int hammer_ioc_get_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
56                                 struct hammer_ioc_snapshot *snap);
57 static int hammer_ioc_get_config(hammer_transaction_t trans, hammer_inode_t ip,
58                                 struct hammer_ioc_config *snap);
59 static int hammer_ioc_set_config(hammer_transaction_t trans, hammer_inode_t ip,
60                                 struct hammer_ioc_config *snap);
61 static int hammer_ioc_get_data(hammer_transaction_t trans, hammer_inode_t ip,
62                                 struct hammer_ioc_data *data);
63
64 int
65 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
66              struct ucred *cred)
67 {
68         struct hammer_transaction trans;
69         int error;
70
71         error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
72
73         hammer_start_transaction(&trans, ip->hmp);
74
75         switch(com) {
76         case HAMMERIOC_PRUNE:
77                 if (error == 0) {
78                         error = hammer_ioc_prune(&trans, ip,
79                                         (struct hammer_ioc_prune *)data);
80                 }
81                 break;
82         case HAMMERIOC_GETHISTORY:
83                 error = hammer_ioc_gethistory(&trans, ip,
84                                         (struct hammer_ioc_history *)data);
85                 break;
86         case HAMMERIOC_REBLOCK:
87                 if (error == 0) {
88                         error = hammer_ioc_reblock(&trans, ip,
89                                         (struct hammer_ioc_reblock *)data);
90                 }
91                 break;
92         case HAMMERIOC_REBALANCE:
93                 /*
94                  * Rebalancing needs to lock a lot of B-Tree nodes.  The
95                  * children and children's children.  Systems with very
96                  * little memory will not be able to do it.
97                  */
98                 if (error == 0 && nbuf < HAMMER_REBALANCE_MIN_BUFS) {
99                         kprintf("hammer: System has insufficient buffers "
100                                 "to rebalance the tree.  nbuf < %d\n",
101                                 HAMMER_REBALANCE_MIN_BUFS);
102                         error = ENOSPC;
103                 }
104                 if (error == 0) {
105                         error = hammer_ioc_rebalance(&trans, ip,
106                                         (struct hammer_ioc_rebalance *)data);
107                 }
108                 break;
109         case HAMMERIOC_SYNCTID:
110                 error = hammer_ioc_synctid(&trans, ip,
111                                         (struct hammer_ioc_synctid *)data);
112                 break;
113         case HAMMERIOC_GET_PSEUDOFS:
114                 error = hammer_ioc_get_pseudofs(&trans, ip,
115                                     (struct hammer_ioc_pseudofs_rw *)data);
116                 break;
117         case HAMMERIOC_SET_PSEUDOFS:
118                 if (error == 0) {
119                         error = hammer_ioc_set_pseudofs(&trans, ip, cred,
120                                     (struct hammer_ioc_pseudofs_rw *)data);
121                 }
122                 break;
123         case HAMMERIOC_UPG_PSEUDOFS:
124                 if (error == 0) {
125                         error = hammer_ioc_upgrade_pseudofs(&trans, ip, 
126                                     (struct hammer_ioc_pseudofs_rw *)data);
127                 }
128                 break;
129         case HAMMERIOC_DGD_PSEUDOFS:
130                 if (error == 0) {
131                         error = hammer_ioc_downgrade_pseudofs(&trans, ip,
132                                     (struct hammer_ioc_pseudofs_rw *)data);
133                 }
134                 break;
135         case HAMMERIOC_RMR_PSEUDOFS:
136                 if (error == 0) {
137                         error = hammer_ioc_destroy_pseudofs(&trans, ip,
138                                     (struct hammer_ioc_pseudofs_rw *)data);
139                 }
140                 break;
141         case HAMMERIOC_WAI_PSEUDOFS:
142                 if (error == 0) {
143                         error = hammer_ioc_wait_pseudofs(&trans, ip,
144                                     (struct hammer_ioc_pseudofs_rw *)data);
145                 }
146                 break;
147         case HAMMERIOC_MIRROR_READ:
148                 if (error == 0) {
149                         error = hammer_ioc_mirror_read(&trans, ip,
150                                     (struct hammer_ioc_mirror_rw *)data);
151                 }
152                 break;
153         case HAMMERIOC_MIRROR_WRITE:
154                 if (error == 0) {
155                         error = hammer_ioc_mirror_write(&trans, ip,
156                                     (struct hammer_ioc_mirror_rw *)data);
157                 }
158                 break;
159         case HAMMERIOC_GET_VERSION:
160                 error = hammer_ioc_get_version(&trans, ip, 
161                                     (struct hammer_ioc_version *)data);
162                 break;
163         case HAMMERIOC_GET_INFO:
164                 error = hammer_ioc_get_info(&trans,
165                                     (struct hammer_ioc_info *)data);
166                 break;
167         case HAMMERIOC_SET_VERSION:
168                 if (error == 0) {
169                         error = hammer_ioc_set_version(&trans, ip, 
170                                             (struct hammer_ioc_version *)data);
171                 }
172                 break;
173         case HAMMERIOC_ADD_VOLUME:
174                 if (error == 0) {
175                         error = priv_check_cred(cred, PRIV_HAMMER_VOLUME, 0);
176                         if (error == 0)
177                                 error = hammer_ioc_volume_add(&trans, ip,
178                                             (struct hammer_ioc_volume *)data);
179                 }
180                 break;
181         case HAMMERIOC_DEL_VOLUME:
182                 if (error == 0) {
183                         error = priv_check_cred(cred, PRIV_HAMMER_VOLUME, 0);
184                         if (error == 0)
185                                 error = hammer_ioc_volume_del(&trans, ip,
186                                             (struct hammer_ioc_volume *)data);
187                 }
188                 break;
189         case HAMMERIOC_ADD_SNAPSHOT:
190                 if (error == 0) {
191                         error = hammer_ioc_add_snapshot(
192                                         &trans, ip, (struct hammer_ioc_snapshot *)data);
193                 }
194                 break;
195         case HAMMERIOC_DEL_SNAPSHOT:
196                 if (error == 0) {
197                         error = hammer_ioc_del_snapshot(
198                                         &trans, ip, (struct hammer_ioc_snapshot *)data);
199                 }
200                 break;
201         case HAMMERIOC_GET_SNAPSHOT:
202                 error = hammer_ioc_get_snapshot(
203                                         &trans, ip, (struct hammer_ioc_snapshot *)data);
204                 break;
205         case HAMMERIOC_GET_CONFIG:
206                 error = hammer_ioc_get_config(
207                                         &trans, ip, (struct hammer_ioc_config *)data);
208                 break;
209         case HAMMERIOC_SET_CONFIG:
210                 if (error == 0) {
211                         error = hammer_ioc_set_config(
212                                         &trans, ip, (struct hammer_ioc_config *)data);
213                 }
214                 break;
215         case HAMMERIOC_DEDUP:
216                 if (error == 0) {
217                         error = hammer_ioc_dedup(
218                                         &trans, ip, (struct hammer_ioc_dedup *)data);
219                 }
220                 break;
221         case HAMMERIOC_GET_DATA:
222                 if (error == 0) {
223                         error = hammer_ioc_get_data(
224                                         &trans, ip, (struct hammer_ioc_data *)data);
225                 }
226                 break;
227         default:
228                 error = EOPNOTSUPP;
229                 break;
230         }
231         hammer_done_transaction(&trans);
232         return (error);
233 }
234
235 /*
236  * Iterate through an object's inode or an object's records and record
237  * modification TIDs.
238  */
239 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
240                         hammer_btree_elm_t elm);
241
242 static
243 int
244 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
245                       struct hammer_ioc_history *hist)
246 {
247         struct hammer_cursor cursor;
248         hammer_btree_elm_t elm;
249         int error;
250
251         /*
252          * Validate the structure and initialize for return.
253          */
254         if (hist->beg_tid > hist->end_tid)
255                 return(EINVAL);
256         if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
257                 if (hist->key > hist->nxt_key)
258                         return(EINVAL);
259         }
260
261         hist->obj_id = ip->obj_id;
262         hist->count = 0;
263         hist->nxt_tid = hist->end_tid;
264         hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
265         hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
266         hist->head.flags &= ~HAMMER_IOC_HISTORY_EOF;
267         hist->head.flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
268         if ((ip->flags & HAMMER_INODE_MODMASK) & 
269             ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
270                 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
271         }
272
273         /*
274          * Setup the cursor.  We can't handle undeletable records
275          * (create_tid of 0) at the moment.  A create_tid of 0 has
276          * a special meaning and cannot be specified in the cursor.
277          */
278         error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
279         if (error) {
280                 hammer_done_cursor(&cursor);
281                 return(error);
282         }
283
284         cursor.key_beg.obj_id = hist->obj_id;
285         cursor.key_beg.create_tid = hist->beg_tid;
286         cursor.key_beg.delete_tid = 0;
287         cursor.key_beg.obj_type = 0;
288         if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
289                 cursor.key_beg.create_tid = 1;
290
291         cursor.key_end.obj_id = hist->obj_id;
292         cursor.key_end.create_tid = hist->end_tid;
293         cursor.key_end.delete_tid = 0;
294         cursor.key_end.obj_type = 0;
295
296         cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
297
298         if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
299                 /*
300                  * key-range within the file.  For a regular file the
301                  * on-disk key represents BASE+LEN, not BASE, so the
302                  * first possible record containing the offset 'key'
303                  * has an on-disk key of (key + 1).
304                  */
305                 cursor.key_beg.key = hist->key;
306                 cursor.key_end.key = HAMMER_MAX_KEY;
307                 cursor.key_beg.localization = ip->obj_localization + 
308                                               HAMMER_LOCALIZE_MISC;
309                 cursor.key_end.localization = ip->obj_localization + 
310                                               HAMMER_LOCALIZE_MISC;
311
312                 switch(ip->ino_data.obj_type) {
313                 case HAMMER_OBJTYPE_REGFILE:
314                         ++cursor.key_beg.key;
315                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
316                         break;
317                 case HAMMER_OBJTYPE_DIRECTORY:
318                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
319                         cursor.key_beg.localization = ip->obj_localization +
320                                                 hammer_dir_localization(ip);
321                         cursor.key_end.localization = ip->obj_localization +
322                                                 hammer_dir_localization(ip);
323                         break;
324                 case HAMMER_OBJTYPE_DBFILE:
325                         cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
326                         break;
327                 default:
328                         error = EINVAL;
329                         break;
330                 }
331                 cursor.key_end.rec_type = cursor.key_beg.rec_type;
332         } else {
333                 /*
334                  * The inode itself.
335                  */
336                 cursor.key_beg.key = 0;
337                 cursor.key_end.key = 0;
338                 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
339                 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
340                 cursor.key_beg.localization = ip->obj_localization +
341                                               HAMMER_LOCALIZE_INODE;
342                 cursor.key_end.localization = ip->obj_localization +
343                                               HAMMER_LOCALIZE_INODE;
344         }
345
346         error = hammer_btree_first(&cursor);
347         while (error == 0) {
348                 elm = &cursor.node->ondisk->elms[cursor.index];
349
350                 add_history(ip, hist, elm);
351                 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
352                                         HAMMER_IOC_HISTORY_NEXT_KEY |
353                                         HAMMER_IOC_HISTORY_EOF)) {
354                         break;
355                 }
356                 error = hammer_btree_iterate(&cursor);
357         }
358         if (error == ENOENT) {
359                 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
360                 error = 0;
361         }
362         hammer_done_cursor(&cursor);
363         return(error);
364 }
365
366 /*
367  * Add the scanned element to the ioctl return structure.  Some special
368  * casing is required for regular files to accomodate how data ranges are
369  * stored on-disk.
370  */
371 static void
372 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
373             hammer_btree_elm_t elm)
374 {
375         int i;
376
377         if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
378                 return;
379         if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
380             ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
381                 /*
382                  * Adjust nxt_key
383                  */
384                 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
385                     hist->key < elm->leaf.base.key - elm->leaf.data_len) {
386                         hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
387                 }
388                 if (hist->nxt_key > elm->leaf.base.key)
389                         hist->nxt_key = elm->leaf.base.key;
390
391                 /*
392                  * Record is beyond MAXPHYS, there won't be any more records
393                  * in the iteration covering the requested offset (key).
394                  */
395                 if (elm->leaf.base.key >= MAXPHYS &&
396                     elm->leaf.base.key - MAXPHYS > hist->key) {
397                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
398                 }
399
400                 /*
401                  * Data-range of record does not cover the key.
402                  */
403                 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
404                         return;
405
406         } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
407                 /*
408                  * Adjust nxt_key
409                  */
410                 if (hist->nxt_key > elm->leaf.base.key &&
411                     hist->key < elm->leaf.base.key) {
412                         hist->nxt_key = elm->leaf.base.key;
413                 }
414
415                 /*
416                  * Record is beyond the requested key.
417                  */
418                 if (elm->leaf.base.key > hist->key)
419                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
420         }
421
422         /*
423          * Add create_tid if it is in-bounds.
424          */
425         i = hist->count;
426         if ((i == 0 ||
427              elm->leaf.base.create_tid != hist->hist_ary[i - 1].tid) &&
428             elm->leaf.base.create_tid >= hist->beg_tid &&
429             elm->leaf.base.create_tid < hist->end_tid) {
430                 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
431                         hist->nxt_tid = elm->leaf.base.create_tid;
432                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
433                         return;
434                 }
435                 hist->hist_ary[i].tid = elm->leaf.base.create_tid;
436                 hist->hist_ary[i].time32 = elm->leaf.create_ts;
437                 ++hist->count;
438         }
439
440         /*
441          * Add delete_tid if it is in-bounds.  Note that different portions
442          * of the history may have overlapping data ranges with different
443          * delete_tid's.  If this case occurs the delete_tid may match the
444          * create_tid of a following record.  XXX
445          *
446          *      [        ]
447          *            [     ]
448          */
449         i = hist->count;
450         if (elm->leaf.base.delete_tid &&
451             elm->leaf.base.delete_tid >= hist->beg_tid &&
452             elm->leaf.base.delete_tid < hist->end_tid) {
453                 if (i == HAMMER_MAX_HISTORY_ELMS) {
454                         hist->nxt_tid = elm->leaf.base.delete_tid;
455                         hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
456                         return;
457                 }
458                 hist->hist_ary[i].tid = elm->leaf.base.delete_tid;
459                 hist->hist_ary[i].time32 = elm->leaf.delete_ts;
460                 ++hist->count;
461         }
462 }
463
464 /*
465  * Acquire synchronization TID
466  */
467 static
468 int
469 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
470                    struct hammer_ioc_synctid *std)
471 {
472         hammer_mount_t hmp = ip->hmp;
473         int error = 0;
474
475         switch(std->op) {
476         case HAMMER_SYNCTID_NONE:
477                 std->tid = hmp->flusher.tid;    /* inaccurate */
478                 break;
479         case HAMMER_SYNCTID_ASYNC:
480                 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
481                 hammer_flusher_async(hmp, NULL);
482                 std->tid = hmp->flusher.tid;    /* inaccurate */
483                 break;
484         case HAMMER_SYNCTID_SYNC1:
485                 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
486                 hammer_flusher_sync(hmp);
487                 std->tid = hmp->flusher.tid;
488                 break;
489         case HAMMER_SYNCTID_SYNC2:
490                 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
491                 hammer_flusher_sync(hmp);
492                 std->tid = hmp->flusher.tid;
493                 hammer_flusher_sync(hmp);
494                 break;
495         default:
496                 error = EOPNOTSUPP;
497                 break;
498         }
499         return(error);
500 }
501
502 /*
503  * Retrieve version info.
504  *
505  * Load min_version, wip_version, and max_versino.  If cur_version is passed
506  * as 0 then load the current version into cur_version.  Load the description
507  * for cur_version into the description array.
508  *
509  * Returns 0 on success, EINVAL if cur_version is non-zero and set to an
510  * unsupported value.
511  */
512 static
513 int
514 hammer_ioc_get_version(hammer_transaction_t trans, hammer_inode_t ip,
515                    struct hammer_ioc_version *ver)
516 {
517         int error = 0;
518
519         ver->min_version = HAMMER_VOL_VERSION_MIN;
520         ver->wip_version = HAMMER_VOL_VERSION_WIP;
521         ver->max_version = HAMMER_VOL_VERSION_MAX;
522         if (ver->cur_version == 0)
523                 ver->cur_version = trans->hmp->version;
524         switch(ver->cur_version) {
525         case 1:
526                 ksnprintf(ver->description, sizeof(ver->description),
527                          "First HAMMER release (DragonFly 2.0+)");
528                 break;
529         case 2:
530                 ksnprintf(ver->description, sizeof(ver->description),
531                          "New directory entry layout (DragonFly 2.3+)");
532                 break;
533         case 3:
534                 ksnprintf(ver->description, sizeof(ver->description),
535                          "New snapshot management (DragonFly 2.5+)");
536                 break;
537         case 4:
538                 ksnprintf(ver->description, sizeof(ver->description),
539                          "New undo/flush, faster flush/sync (DragonFly 2.5+)");
540                 break;
541         case 5:
542                 ksnprintf(ver->description, sizeof(ver->description),
543                          "Adjustments for dedup support (DragonFly 2.9+)");
544                 break;
545         default:
546                 ksnprintf(ver->description, sizeof(ver->description),
547                          "Unknown");
548                 error = EINVAL;
549                 break;
550         }
551         return(error);
552 };
553
554 /*
555  * Set version info
556  */
557 static
558 int
559 hammer_ioc_set_version(hammer_transaction_t trans, hammer_inode_t ip,
560                    struct hammer_ioc_version *ver)
561 {
562         hammer_mount_t hmp = trans->hmp;
563         struct hammer_cursor cursor;
564         hammer_volume_t volume;
565         int error;
566         int over = hmp->version;
567
568         /*
569          * Generally do not allow downgrades.  However, version 4 can
570          * be downgraded to version 3.
571          */
572         if (ver->cur_version < hmp->version) {
573                 if (!(ver->cur_version == 3 && hmp->version == 4))
574                         return(EINVAL);
575         }
576         if (ver->cur_version == hmp->version)
577                 return(0);
578         if (ver->cur_version > HAMMER_VOL_VERSION_MAX)
579                 return(EINVAL);
580         if (hmp->ronly)
581                 return(EROFS);
582
583         /*
584          * Update the root volume header and the version cached in
585          * the hammer_mount structure.
586          */
587         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
588         if (error)
589                 goto failed;
590         hammer_lock_ex(&hmp->flusher.finalize_lock);
591         hammer_sync_lock_ex(trans);
592         hmp->version = ver->cur_version;
593
594         /*
595          * If upgrading from version < 4 to version >= 4 the UNDO FIFO
596          * must be reinitialized.
597          */
598         if (over < HAMMER_VOL_VERSION_FOUR &&
599             ver->cur_version >= HAMMER_VOL_VERSION_FOUR) {
600                 kprintf("upgrade undo to version 4\n");
601                 error = hammer_upgrade_undo_4(trans);
602                 if (error)
603                         goto failed;
604         }
605
606         /*
607          * Adjust the version in the volume header
608          */
609         volume = hammer_get_root_volume(hmp, &error);
610         KKASSERT(error == 0);
611         hammer_modify_volume_field(cursor.trans, volume, vol_version);
612         volume->ondisk->vol_version = ver->cur_version;
613         hammer_modify_volume_done(volume);
614         hammer_rel_volume(volume, 0);
615
616         hammer_sync_unlock(trans);
617         hammer_unlock(&hmp->flusher.finalize_lock);
618 failed:
619         ver->head.error = error;
620         hammer_done_cursor(&cursor);
621         return(0);
622 }
623
624 /*
625  * Get information
626  */
627 static
628 int
629 hammer_ioc_get_info(hammer_transaction_t trans, struct hammer_ioc_info *info) {
630
631         struct hammer_volume_ondisk     *od = trans->hmp->rootvol->ondisk;
632         struct hammer_mount             *hm = trans->hmp;
633
634         /* Fill the structure with the necessary information */
635         _hammer_checkspace(hm, HAMMER_CHKSPC_WRITE, &info->rsvbigblocks);
636         info->rsvbigblocks = info->rsvbigblocks >> HAMMER_LARGEBLOCK_BITS;
637         strlcpy(info->vol_name, od->vol_name, sizeof(od->vol_name));
638
639         info->vol_fsid = hm->fsid;
640         info->vol_fstype = od->vol_fstype;
641         info->version = hm->version;
642
643         info->inodes = od->vol0_stat_inodes;
644         info->bigblocks = od->vol0_stat_bigblocks;
645         info->freebigblocks = od->vol0_stat_freebigblocks;
646         info->nvolumes = hm->nvolumes;
647
648         return 0;
649 }
650
651 /*
652  * Add a snapshot transction id(s) to the list of snapshots.
653  *
654  * NOTE: Records are created with an allocated TID.  If a flush cycle
655  *       is in progress the record may be synced in the current flush
656  *       cycle and the volume header will reflect the allocation of the
657  *       TID, but the synchronization point may not catch up to the
658  *       TID until the next flush cycle.
659  */
660 static
661 int
662 hammer_ioc_add_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
663                         struct hammer_ioc_snapshot *snap)
664 {
665         hammer_mount_t hmp = ip->hmp;
666         struct hammer_btree_leaf_elm leaf;
667         struct hammer_cursor cursor;
668         int error;
669
670         /*
671          * Validate structure
672          */
673         if (snap->count > HAMMER_SNAPS_PER_IOCTL)
674                 return (EINVAL);
675         if (snap->index > snap->count)
676                 return (EINVAL);
677
678         hammer_lock_ex(&hmp->snapshot_lock);
679 again:
680         /*
681          * Look for keys starting after the previous iteration, or at
682          * the beginning if snap->count is 0.
683          */
684         error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
685         if (error) {
686                 hammer_done_cursor(&cursor);
687                 return(error);
688         }
689
690         cursor.asof = HAMMER_MAX_TID;
691         cursor.flags |= HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF;
692
693         bzero(&leaf, sizeof(leaf));
694         leaf.base.obj_id = HAMMER_OBJID_ROOT;
695         leaf.base.rec_type = HAMMER_RECTYPE_SNAPSHOT;
696         leaf.base.create_tid = hammer_alloc_tid(hmp, 1);
697         leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
698         leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
699         leaf.data_len = sizeof(struct hammer_snapshot_data);
700
701         while (snap->index < snap->count) {
702                 leaf.base.key = (int64_t)snap->snaps[snap->index].tid;
703                 cursor.key_beg = leaf.base;
704                 error = hammer_btree_lookup(&cursor);
705                 if (error == 0) {
706                         error = EEXIST;
707                         break;
708                 }
709
710                 /*
711                  * NOTE: Must reload key_beg after an ASOF search because
712                  *       the create_tid may have been modified during the
713                  *       search.
714                  */
715                 cursor.flags &= ~HAMMER_CURSOR_ASOF;
716                 cursor.key_beg = leaf.base;
717                 error = hammer_create_at_cursor(&cursor, &leaf,
718                                                 &snap->snaps[snap->index],
719                                                 HAMMER_CREATE_MODE_SYS);
720                 if (error == EDEADLK) {
721                         hammer_done_cursor(&cursor);
722                         goto again;
723                 }
724                 cursor.flags |= HAMMER_CURSOR_ASOF;
725                 if (error)
726                         break;
727                 ++snap->index;
728         }
729         snap->head.error = error;
730         hammer_done_cursor(&cursor);
731         hammer_unlock(&hmp->snapshot_lock);
732         return(0);
733 }
734
735 /*
736  * Delete snapshot transaction id(s) from the list of snapshots.
737  */
738 static
739 int
740 hammer_ioc_del_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
741                         struct hammer_ioc_snapshot *snap)
742 {
743         hammer_mount_t hmp = ip->hmp;
744         struct hammer_cursor cursor;
745         int error;
746
747         /*
748          * Validate structure
749          */
750         if (snap->count > HAMMER_SNAPS_PER_IOCTL)
751                 return (EINVAL);
752         if (snap->index > snap->count)
753                 return (EINVAL);
754
755         hammer_lock_ex(&hmp->snapshot_lock);
756 again:
757         /*
758          * Look for keys starting after the previous iteration, or at
759          * the beginning if snap->count is 0.
760          */
761         error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
762         if (error) {
763                 hammer_done_cursor(&cursor);
764                 return(error);
765         }
766
767         cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
768         cursor.key_beg.create_tid = 0;
769         cursor.key_beg.delete_tid = 0;
770         cursor.key_beg.obj_type = 0;
771         cursor.key_beg.rec_type = HAMMER_RECTYPE_SNAPSHOT;
772         cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
773         cursor.asof = HAMMER_MAX_TID;
774         cursor.flags |= HAMMER_CURSOR_ASOF;
775
776         while (snap->index < snap->count) {
777                 cursor.key_beg.key = (int64_t)snap->snaps[snap->index].tid;
778                 error = hammer_btree_lookup(&cursor);
779                 if (error)
780                         break;
781                 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF);
782                 if (error)
783                         break;
784                 error = hammer_delete_at_cursor(&cursor, HAMMER_DELETE_DESTROY,
785                                                 0, 0, 0, NULL);
786                 if (error == EDEADLK) {
787                         hammer_done_cursor(&cursor);
788                         goto again;
789                 }
790                 if (error)
791                         break;
792                 ++snap->index;
793         }
794         snap->head.error = error;
795         hammer_done_cursor(&cursor);
796         hammer_unlock(&hmp->snapshot_lock);
797         return(0);
798 }
799
800 /*
801  * Retrieve as many snapshot ids as possible or until the array is
802  * full, starting after the last transction id passed in.  If count
803  * is 0 we retrieve starting at the beginning.
804  *
805  * NOTE: Because the b-tree key field is signed but transaction ids
806  *       are unsigned the returned list will be signed-sorted instead
807  *       of unsigned sorted.  The Caller must still sort the aggregate
808  *       results.
809  */
810 static
811 int
812 hammer_ioc_get_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
813                         struct hammer_ioc_snapshot *snap)
814 {
815         struct hammer_cursor cursor;
816         int error;
817
818         /*
819          * Validate structure
820          */
821         if (snap->index != 0)
822                 return (EINVAL);
823         if (snap->count > HAMMER_SNAPS_PER_IOCTL)
824                 return (EINVAL);
825
826         /*
827          * Look for keys starting after the previous iteration, or at
828          * the beginning if snap->count is 0.
829          */
830         error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
831         if (error) {
832                 hammer_done_cursor(&cursor);
833                 return(error);
834         }
835
836         cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
837         cursor.key_beg.create_tid = 0;
838         cursor.key_beg.delete_tid = 0;
839         cursor.key_beg.obj_type = 0;
840         cursor.key_beg.rec_type = HAMMER_RECTYPE_SNAPSHOT;
841         cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
842         if (snap->count == 0)
843                 cursor.key_beg.key = HAMMER_MIN_KEY;
844         else
845                 cursor.key_beg.key = (int64_t)snap->snaps[snap->count - 1].tid + 1;
846
847         cursor.key_end = cursor.key_beg;
848         cursor.key_end.key = HAMMER_MAX_KEY;
849         cursor.asof = HAMMER_MAX_TID;
850         cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE | HAMMER_CURSOR_ASOF;
851
852         snap->count = 0;
853
854         error = hammer_btree_first(&cursor);
855         while (error == 0 && snap->count < HAMMER_SNAPS_PER_IOCTL) {
856                 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF);
857                 if (error)
858                         break;
859                 if (cursor.leaf->base.rec_type == HAMMER_RECTYPE_SNAPSHOT) {
860                         error = hammer_btree_extract(
861                                              &cursor, HAMMER_CURSOR_GET_LEAF |
862                                                       HAMMER_CURSOR_GET_DATA);
863                         snap->snaps[snap->count] = cursor.data->snap;
864
865                         /*
866                          * The snap data tid should match the key but might
867                          * not due to a bug in the HAMMER v3 conversion code.
868                          *
869                          * This error will work itself out over time but we
870                          * have to force a match or the snapshot will not
871                          * be deletable.
872                          */
873                         if (cursor.data->snap.tid !=
874                             (hammer_tid_t)cursor.leaf->base.key) {
875                                 kprintf("HAMMER: lo=%08x snapshot key "
876                                         "0x%016jx data mismatch 0x%016jx\n",
877                                         cursor.key_beg.localization,
878                                         (uintmax_t)cursor.data->snap.tid,
879                                         cursor.leaf->base.key);
880                                 kprintf("HAMMER: Probably left over from the "
881                                         "original v3 conversion, hammer "
882                                         "cleanup should get it eventually\n");
883                                 snap->snaps[snap->count].tid =
884                                         cursor.leaf->base.key;
885                         }
886                         ++snap->count;
887                 }
888                 error = hammer_btree_iterate(&cursor);
889         }
890
891         if (error == ENOENT) {
892                 snap->head.flags |= HAMMER_IOC_SNAPSHOT_EOF;
893                 error = 0;
894         }
895         snap->head.error = error;
896         hammer_done_cursor(&cursor);
897         return(0);
898 }
899
900 /*
901  * Retrieve the PFS hammer cleanup utility config record.  This is
902  * different (newer than) the PFS config.
903  */
904 static
905 int
906 hammer_ioc_get_config(hammer_transaction_t trans, hammer_inode_t ip,
907                         struct hammer_ioc_config *config)
908 {
909         struct hammer_cursor cursor;
910         int error;
911
912         error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
913         if (error) {
914                 hammer_done_cursor(&cursor);
915                 return(error);
916         }
917
918         cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
919         cursor.key_beg.create_tid = 0;
920         cursor.key_beg.delete_tid = 0;
921         cursor.key_beg.obj_type = 0;
922         cursor.key_beg.rec_type = HAMMER_RECTYPE_CONFIG;
923         cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
924         cursor.key_beg.key = 0;         /* config space page 0 */
925
926         cursor.asof = HAMMER_MAX_TID;
927         cursor.flags |= HAMMER_CURSOR_ASOF;
928
929         error = hammer_btree_lookup(&cursor);
930         if (error == 0) {
931                 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
932                                                       HAMMER_CURSOR_GET_DATA);
933                 if (error == 0)
934                         config->config = cursor.data->config;
935         }
936         /* error can be ENOENT */
937         config->head.error = error;
938         hammer_done_cursor(&cursor);
939         return(0);
940 }
941
942 /*
943  * Retrieve the PFS hammer cleanup utility config record.  This is
944  * different (newer than) the PFS config.
945  *
946  * This is kinda a hack.
947  */
948 static
949 int
950 hammer_ioc_set_config(hammer_transaction_t trans, hammer_inode_t ip,
951                         struct hammer_ioc_config *config)
952 {
953         struct hammer_btree_leaf_elm leaf;
954         struct hammer_cursor cursor;
955         hammer_mount_t hmp = ip->hmp;
956         int error;
957
958 again:
959         error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
960         if (error) {
961                 hammer_done_cursor(&cursor);
962                 return(error);
963         }
964
965         bzero(&leaf, sizeof(leaf));
966         leaf.base.obj_id = HAMMER_OBJID_ROOT;
967         leaf.base.rec_type = HAMMER_RECTYPE_CONFIG;
968         leaf.base.create_tid = hammer_alloc_tid(hmp, 1);
969         leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
970         leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
971         leaf.base.key = 0;      /* page 0 */
972         leaf.data_len = sizeof(struct hammer_config_data);
973
974         cursor.key_beg = leaf.base;
975
976         cursor.asof = HAMMER_MAX_TID;
977         cursor.flags |= HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF;
978
979         error = hammer_btree_lookup(&cursor);
980         if (error == 0) {
981                 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
982                                                       HAMMER_CURSOR_GET_DATA);
983                 error = hammer_delete_at_cursor(&cursor, HAMMER_DELETE_DESTROY,
984                                                 0, 0, 0, NULL);
985                 if (error == EDEADLK) {
986                         hammer_done_cursor(&cursor);
987                         goto again;
988                 }
989         }
990         if (error == ENOENT)
991                 error = 0;
992         if (error == 0) {
993                 /*
994                  * NOTE: Must reload key_beg after an ASOF search because
995                  *       the create_tid may have been modified during the
996                  *       search.
997                  */
998                 cursor.flags &= ~HAMMER_CURSOR_ASOF;
999                 cursor.key_beg = leaf.base;
1000                 error = hammer_create_at_cursor(&cursor, &leaf,
1001                                                 &config->config,
1002                                                 HAMMER_CREATE_MODE_SYS);
1003                 if (error == EDEADLK) {
1004                         hammer_done_cursor(&cursor);
1005                         goto again;
1006                 }
1007         }
1008         config->head.error = error;
1009         hammer_done_cursor(&cursor);
1010         return(0);
1011 }
1012
1013 static
1014 int
1015 hammer_ioc_get_data(hammer_transaction_t trans, hammer_inode_t ip,
1016                         struct hammer_ioc_data *data)
1017 {
1018         struct hammer_cursor cursor;
1019         int bytes;
1020         int error;
1021
1022         /* XXX cached inode ? */
1023         error = hammer_init_cursor(trans, &cursor, NULL, NULL);
1024         if (error)
1025                 goto failed;
1026
1027         cursor.key_beg = data->elm;
1028         cursor.flags |= HAMMER_CURSOR_BACKEND;
1029
1030         error = hammer_btree_lookup(&cursor);
1031         if (error == 0) {
1032                 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
1033                                                       HAMMER_CURSOR_GET_DATA);
1034                 if (error == 0) {
1035                         data->leaf = *cursor.leaf;
1036                         bytes = cursor.leaf->data_len;
1037                         if (bytes > data->size)
1038                                 bytes = data->size;
1039                         error = copyout(cursor.data, data->ubuf, bytes);
1040                 }
1041         }
1042
1043 failed:
1044         hammer_done_cursor(&cursor);
1045         return (error);
1046 }