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.32 2008/11/13 02:23:29 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_version(hammer_transaction_t trans,
45 struct hammer_ioc_version *ver);
46 static int hammer_ioc_set_version(hammer_transaction_t trans,
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_pfs_iterate(hammer_transaction_t trans,
52 struct hammer_ioc_pfs_iterate *pi);
53 static int hammer_ioc_add_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
54 struct hammer_ioc_snapshot *snap);
55 static int hammer_ioc_del_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
56 struct hammer_ioc_snapshot *snap);
57 static int hammer_ioc_get_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
58 struct hammer_ioc_snapshot *snap);
59 static int hammer_ioc_get_config(hammer_transaction_t trans, hammer_inode_t ip,
60 struct hammer_ioc_config *snap);
61 static int hammer_ioc_set_config(hammer_transaction_t trans, hammer_inode_t ip,
62 struct hammer_ioc_config *snap);
63 static int hammer_ioc_get_data(hammer_transaction_t trans, hammer_inode_t ip,
64 struct hammer_ioc_data *data);
67 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
70 struct hammer_transaction trans;
73 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
75 hammer_start_transaction(&trans, ip->hmp);
80 error = hammer_ioc_prune(&trans, ip,
81 (struct hammer_ioc_prune *)data);
84 case HAMMERIOC_GETHISTORY:
85 error = hammer_ioc_gethistory(&trans, ip,
86 (struct hammer_ioc_history *)data);
88 case HAMMERIOC_REBLOCK:
90 error = hammer_ioc_reblock(&trans, ip,
91 (struct hammer_ioc_reblock *)data);
94 case HAMMERIOC_REBALANCE:
96 * Rebalancing needs to lock a lot of B-Tree nodes. The
97 * children and children's children. Systems with very
98 * little memory will not be able to do it.
100 if (error == 0 && nbuf < HAMMER_REBALANCE_MIN_BUFS) {
101 kprintf("hammer: System has insufficient buffers "
102 "to rebalance the tree. nbuf < %d\n",
103 HAMMER_REBALANCE_MIN_BUFS);
107 error = hammer_ioc_rebalance(&trans, ip,
108 (struct hammer_ioc_rebalance *)data);
111 case HAMMERIOC_SYNCTID:
112 error = hammer_ioc_synctid(&trans, ip,
113 (struct hammer_ioc_synctid *)data);
115 case HAMMERIOC_GET_PSEUDOFS:
116 error = hammer_ioc_get_pseudofs(&trans, ip,
117 (struct hammer_ioc_pseudofs_rw *)data);
119 case HAMMERIOC_SET_PSEUDOFS:
121 error = hammer_ioc_set_pseudofs(&trans, ip, cred,
122 (struct hammer_ioc_pseudofs_rw *)data);
125 case HAMMERIOC_UPG_PSEUDOFS:
127 error = hammer_ioc_upgrade_pseudofs(&trans, ip,
128 (struct hammer_ioc_pseudofs_rw *)data);
131 case HAMMERIOC_DGD_PSEUDOFS:
133 error = hammer_ioc_downgrade_pseudofs(&trans, ip,
134 (struct hammer_ioc_pseudofs_rw *)data);
137 case HAMMERIOC_RMR_PSEUDOFS:
139 error = hammer_ioc_destroy_pseudofs(&trans, ip,
140 (struct hammer_ioc_pseudofs_rw *)data);
143 case HAMMERIOC_WAI_PSEUDOFS:
145 error = hammer_ioc_wait_pseudofs(&trans, ip,
146 (struct hammer_ioc_pseudofs_rw *)data);
149 case HAMMERIOC_MIRROR_READ:
151 error = hammer_ioc_mirror_read(&trans, ip,
152 (struct hammer_ioc_mirror_rw *)data);
155 case HAMMERIOC_MIRROR_WRITE:
157 error = hammer_ioc_mirror_write(&trans, ip,
158 (struct hammer_ioc_mirror_rw *)data);
161 case HAMMERIOC_GET_VERSION:
162 error = hammer_ioc_get_version(&trans, ip,
163 (struct hammer_ioc_version *)data);
165 case HAMMERIOC_GET_INFO:
166 error = hammer_ioc_get_info(&trans,
167 (struct hammer_ioc_info *)data);
169 case HAMMERIOC_SET_VERSION:
171 error = hammer_ioc_set_version(&trans, ip,
172 (struct hammer_ioc_version *)data);
175 case HAMMERIOC_ADD_VOLUME:
177 error = priv_check_cred(cred, PRIV_HAMMER_VOLUME, 0);
179 error = hammer_ioc_volume_add(&trans, ip,
180 (struct hammer_ioc_volume *)data);
183 case HAMMERIOC_DEL_VOLUME:
185 error = priv_check_cred(cred, PRIV_HAMMER_VOLUME, 0);
187 error = hammer_ioc_volume_del(&trans, ip,
188 (struct hammer_ioc_volume *)data);
191 case HAMMERIOC_LIST_VOLUMES:
192 error = hammer_ioc_volume_list(&trans, ip,
193 (struct hammer_ioc_volume_list *)data);
195 case HAMMERIOC_ADD_SNAPSHOT:
197 error = hammer_ioc_add_snapshot(
198 &trans, ip, (struct hammer_ioc_snapshot *)data);
201 case HAMMERIOC_DEL_SNAPSHOT:
203 error = hammer_ioc_del_snapshot(
204 &trans, ip, (struct hammer_ioc_snapshot *)data);
207 case HAMMERIOC_GET_SNAPSHOT:
208 error = hammer_ioc_get_snapshot(
209 &trans, ip, (struct hammer_ioc_snapshot *)data);
211 case HAMMERIOC_GET_CONFIG:
212 error = hammer_ioc_get_config(
213 &trans, ip, (struct hammer_ioc_config *)data);
215 case HAMMERIOC_SET_CONFIG:
217 error = hammer_ioc_set_config(
218 &trans, ip, (struct hammer_ioc_config *)data);
221 case HAMMERIOC_DEDUP:
223 error = hammer_ioc_dedup(
224 &trans, ip, (struct hammer_ioc_dedup *)data);
227 case HAMMERIOC_GET_DATA:
229 error = hammer_ioc_get_data(
230 &trans, ip, (struct hammer_ioc_data *)data);
233 case HAMMERIOC_PFS_ITERATE:
234 error = hammer_ioc_pfs_iterate(
235 &trans, (struct hammer_ioc_pfs_iterate *)data);
241 hammer_done_transaction(&trans);
246 * Iterate through an object's inode or an object's records and record
249 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
250 hammer_btree_elm_t elm);
254 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
255 struct hammer_ioc_history *hist)
257 struct hammer_cursor cursor;
258 hammer_btree_elm_t elm;
262 * Validate the structure and initialize for return.
264 if (hist->beg_tid > hist->end_tid)
266 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
267 if (hist->key > hist->nxt_key)
271 hist->obj_id = ip->obj_id;
273 hist->nxt_tid = hist->end_tid;
274 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
275 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
276 hist->head.flags &= ~HAMMER_IOC_HISTORY_EOF;
277 hist->head.flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
278 if ((ip->flags & HAMMER_INODE_MODMASK) &
279 ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
280 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
284 * Setup the cursor. We can't handle undeletable records
285 * (create_tid of 0) at the moment. A create_tid of 0 has
286 * a special meaning and cannot be specified in the cursor.
288 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
290 hammer_done_cursor(&cursor);
294 cursor.key_beg.obj_id = hist->obj_id;
295 cursor.key_beg.create_tid = hist->beg_tid;
296 cursor.key_beg.delete_tid = 0;
297 cursor.key_beg.obj_type = 0;
298 if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
299 cursor.key_beg.create_tid = 1;
301 cursor.key_end.obj_id = hist->obj_id;
302 cursor.key_end.create_tid = hist->end_tid;
303 cursor.key_end.delete_tid = 0;
304 cursor.key_end.obj_type = 0;
306 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
308 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
310 * key-range within the file. For a regular file the
311 * on-disk key represents BASE+LEN, not BASE, so the
312 * first possible record containing the offset 'key'
313 * has an on-disk key of (key + 1).
315 cursor.key_beg.key = hist->key;
316 cursor.key_end.key = HAMMER_MAX_KEY;
317 cursor.key_beg.localization = ip->obj_localization +
318 HAMMER_LOCALIZE_MISC;
319 cursor.key_end.localization = ip->obj_localization +
320 HAMMER_LOCALIZE_MISC;
322 switch(ip->ino_data.obj_type) {
323 case HAMMER_OBJTYPE_REGFILE:
324 ++cursor.key_beg.key;
325 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
327 case HAMMER_OBJTYPE_DIRECTORY:
328 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
329 cursor.key_beg.localization = ip->obj_localization +
330 hammer_dir_localization(ip);
331 cursor.key_end.localization = ip->obj_localization +
332 hammer_dir_localization(ip);
334 case HAMMER_OBJTYPE_DBFILE:
335 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
341 cursor.key_end.rec_type = cursor.key_beg.rec_type;
346 cursor.key_beg.key = 0;
347 cursor.key_end.key = 0;
348 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
349 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
350 cursor.key_beg.localization = ip->obj_localization +
351 HAMMER_LOCALIZE_INODE;
352 cursor.key_end.localization = ip->obj_localization +
353 HAMMER_LOCALIZE_INODE;
356 error = hammer_btree_first(&cursor);
358 elm = &cursor.node->ondisk->elms[cursor.index];
360 add_history(ip, hist, elm);
361 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
362 HAMMER_IOC_HISTORY_NEXT_KEY |
363 HAMMER_IOC_HISTORY_EOF)) {
366 error = hammer_btree_iterate(&cursor);
368 if (error == ENOENT) {
369 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
372 hammer_done_cursor(&cursor);
377 * Add the scanned element to the ioctl return structure. Some special
378 * casing is required for regular files to accomodate how data ranges are
382 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
383 hammer_btree_elm_t elm)
387 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
389 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
390 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
394 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
395 hist->key < elm->leaf.base.key - elm->leaf.data_len) {
396 hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
398 if (hist->nxt_key > elm->leaf.base.key)
399 hist->nxt_key = elm->leaf.base.key;
402 * Record is beyond MAXPHYS, there won't be any more records
403 * in the iteration covering the requested offset (key).
405 if (elm->leaf.base.key >= MAXPHYS &&
406 elm->leaf.base.key - MAXPHYS > hist->key) {
407 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
411 * Data-range of record does not cover the key.
413 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
416 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
420 if (hist->nxt_key > elm->leaf.base.key &&
421 hist->key < elm->leaf.base.key) {
422 hist->nxt_key = elm->leaf.base.key;
426 * Record is beyond the requested key.
428 if (elm->leaf.base.key > hist->key)
429 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
433 * Add create_tid if it is in-bounds.
437 elm->leaf.base.create_tid != hist->hist_ary[i - 1].tid) &&
438 elm->leaf.base.create_tid >= hist->beg_tid &&
439 elm->leaf.base.create_tid < hist->end_tid) {
440 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
441 hist->nxt_tid = elm->leaf.base.create_tid;
442 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
445 hist->hist_ary[i].tid = elm->leaf.base.create_tid;
446 hist->hist_ary[i].time32 = elm->leaf.create_ts;
451 * Add delete_tid if it is in-bounds. Note that different portions
452 * of the history may have overlapping data ranges with different
453 * delete_tid's. If this case occurs the delete_tid may match the
454 * create_tid of a following record. XXX
460 if (elm->leaf.base.delete_tid &&
461 elm->leaf.base.delete_tid >= hist->beg_tid &&
462 elm->leaf.base.delete_tid < hist->end_tid) {
463 if (i == HAMMER_MAX_HISTORY_ELMS) {
464 hist->nxt_tid = elm->leaf.base.delete_tid;
465 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
468 hist->hist_ary[i].tid = elm->leaf.base.delete_tid;
469 hist->hist_ary[i].time32 = elm->leaf.delete_ts;
475 * Acquire synchronization TID
479 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
480 struct hammer_ioc_synctid *std)
482 hammer_mount_t hmp = ip->hmp;
486 case HAMMER_SYNCTID_NONE:
487 std->tid = hmp->flusher.tid; /* inaccurate */
489 case HAMMER_SYNCTID_ASYNC:
490 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
491 hammer_flusher_async(hmp, NULL);
492 std->tid = hmp->flusher.tid; /* inaccurate */
494 case HAMMER_SYNCTID_SYNC1:
495 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
496 hammer_flusher_sync(hmp);
497 std->tid = hmp->flusher.tid;
499 case HAMMER_SYNCTID_SYNC2:
500 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
501 hammer_flusher_sync(hmp);
502 std->tid = hmp->flusher.tid;
503 hammer_flusher_sync(hmp);
513 * Retrieve version info.
515 * Load min_version, wip_version, and max_versino. If cur_version is passed
516 * as 0 then load the current version into cur_version. Load the description
517 * for cur_version into the description array.
519 * Returns 0 on success, EINVAL if cur_version is non-zero and set to an
524 hammer_ioc_get_version(hammer_transaction_t trans, hammer_inode_t ip,
525 struct hammer_ioc_version *ver)
529 ver->min_version = HAMMER_VOL_VERSION_MIN;
530 ver->wip_version = HAMMER_VOL_VERSION_WIP;
531 ver->max_version = HAMMER_VOL_VERSION_MAX;
532 if (ver->cur_version == 0)
533 ver->cur_version = trans->hmp->version;
534 switch(ver->cur_version) {
536 ksnprintf(ver->description, sizeof(ver->description),
537 "First HAMMER release (DragonFly 2.0+)");
540 ksnprintf(ver->description, sizeof(ver->description),
541 "New directory entry layout (DragonFly 2.3+)");
544 ksnprintf(ver->description, sizeof(ver->description),
545 "New snapshot management (DragonFly 2.5+)");
548 ksnprintf(ver->description, sizeof(ver->description),
549 "New undo/flush, faster flush/sync (DragonFly 2.5+)");
552 ksnprintf(ver->description, sizeof(ver->description),
553 "Adjustments for dedup support (DragonFly 2.9+)");
556 ksnprintf(ver->description, sizeof(ver->description),
557 "Directory Hash ALG1 (tmp/rename resistance)");
560 ksnprintf(ver->description, sizeof(ver->description),
573 hammer_ioc_set_version(hammer_transaction_t trans, hammer_inode_t ip,
574 struct hammer_ioc_version *ver)
576 hammer_mount_t hmp = trans->hmp;
577 struct hammer_cursor cursor;
578 hammer_volume_t volume;
580 int over = hmp->version;
583 * Generally do not allow downgrades. However, version 4 can
584 * be downgraded to version 3.
586 if (ver->cur_version < hmp->version) {
587 if (!(ver->cur_version == 3 && hmp->version == 4))
590 if (ver->cur_version == hmp->version)
592 if (ver->cur_version > HAMMER_VOL_VERSION_MAX)
598 * Update the root volume header and the version cached in
599 * the hammer_mount structure.
601 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
604 hammer_lock_ex(&hmp->flusher.finalize_lock);
605 hammer_sync_lock_ex(trans);
606 hmp->version = ver->cur_version;
609 * If upgrading from version < 4 to version >= 4 the UNDO FIFO
610 * must be reinitialized.
612 if (over < HAMMER_VOL_VERSION_FOUR &&
613 ver->cur_version >= HAMMER_VOL_VERSION_FOUR) {
614 kprintf("upgrade undo to version 4\n");
615 error = hammer_upgrade_undo_4(trans);
621 * Adjust the version in the volume header
623 volume = hammer_get_root_volume(hmp, &error);
624 KKASSERT(error == 0);
625 hammer_modify_volume_field(cursor.trans, volume, vol_version);
626 volume->ondisk->vol_version = ver->cur_version;
627 hammer_modify_volume_done(volume);
628 hammer_rel_volume(volume, 0);
630 hammer_sync_unlock(trans);
631 hammer_unlock(&hmp->flusher.finalize_lock);
633 ver->head.error = error;
634 hammer_done_cursor(&cursor);
643 hammer_ioc_get_info(hammer_transaction_t trans, struct hammer_ioc_info *info) {
645 struct hammer_volume_ondisk *od = trans->hmp->rootvol->ondisk;
646 struct hammer_mount *hm = trans->hmp;
648 /* Fill the structure with the necessary information */
649 _hammer_checkspace(hm, HAMMER_CHKSPC_WRITE, &info->rsvbigblocks);
650 info->rsvbigblocks = info->rsvbigblocks >> HAMMER_BIGBLOCK_BITS;
651 strlcpy(info->vol_name, od->vol_name, sizeof(od->vol_name));
653 info->vol_fsid = hm->fsid;
654 info->vol_fstype = od->vol_fstype;
655 info->version = hm->version;
657 info->inodes = od->vol0_stat_inodes;
658 info->bigblocks = od->vol0_stat_bigblocks;
659 info->freebigblocks = od->vol0_stat_freebigblocks;
660 info->nvolumes = hm->nvolumes;
666 * Add a snapshot transction id(s) to the list of snapshots.
668 * NOTE: Records are created with an allocated TID. If a flush cycle
669 * is in progress the record may be synced in the current flush
670 * cycle and the volume header will reflect the allocation of the
671 * TID, but the synchronization point may not catch up to the
672 * TID until the next flush cycle.
676 hammer_ioc_add_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
677 struct hammer_ioc_snapshot *snap)
679 hammer_mount_t hmp = ip->hmp;
680 struct hammer_btree_leaf_elm leaf;
681 struct hammer_cursor cursor;
687 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
689 if (snap->index > snap->count)
692 hammer_lock_ex(&hmp->snapshot_lock);
695 * Look for keys starting after the previous iteration, or at
696 * the beginning if snap->count is 0.
698 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
700 hammer_done_cursor(&cursor);
704 cursor.asof = HAMMER_MAX_TID;
705 cursor.flags |= HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF;
707 bzero(&leaf, sizeof(leaf));
708 leaf.base.obj_id = HAMMER_OBJID_ROOT;
709 leaf.base.rec_type = HAMMER_RECTYPE_SNAPSHOT;
710 leaf.base.create_tid = hammer_alloc_tid(hmp, 1);
711 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
712 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
713 leaf.data_len = sizeof(struct hammer_snapshot_data);
715 while (snap->index < snap->count) {
716 leaf.base.key = (int64_t)snap->snaps[snap->index].tid;
717 cursor.key_beg = leaf.base;
718 error = hammer_btree_lookup(&cursor);
725 * NOTE: Must reload key_beg after an ASOF search because
726 * the create_tid may have been modified during the
729 cursor.flags &= ~HAMMER_CURSOR_ASOF;
730 cursor.key_beg = leaf.base;
731 error = hammer_create_at_cursor(&cursor, &leaf,
732 &snap->snaps[snap->index],
733 HAMMER_CREATE_MODE_SYS);
734 if (error == EDEADLK) {
735 hammer_done_cursor(&cursor);
738 cursor.flags |= HAMMER_CURSOR_ASOF;
743 snap->head.error = error;
744 hammer_done_cursor(&cursor);
745 hammer_unlock(&hmp->snapshot_lock);
750 * Delete snapshot transaction id(s) from the list of snapshots.
754 hammer_ioc_del_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
755 struct hammer_ioc_snapshot *snap)
757 hammer_mount_t hmp = ip->hmp;
758 struct hammer_cursor cursor;
764 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
766 if (snap->index > snap->count)
769 hammer_lock_ex(&hmp->snapshot_lock);
772 * Look for keys starting after the previous iteration, or at
773 * the beginning if snap->count is 0.
775 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
777 hammer_done_cursor(&cursor);
781 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
782 cursor.key_beg.create_tid = 0;
783 cursor.key_beg.delete_tid = 0;
784 cursor.key_beg.obj_type = 0;
785 cursor.key_beg.rec_type = HAMMER_RECTYPE_SNAPSHOT;
786 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
787 cursor.asof = HAMMER_MAX_TID;
788 cursor.flags |= HAMMER_CURSOR_ASOF;
790 while (snap->index < snap->count) {
791 cursor.key_beg.key = (int64_t)snap->snaps[snap->index].tid;
792 error = hammer_btree_lookup(&cursor);
795 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF);
798 error = hammer_delete_at_cursor(&cursor, HAMMER_DELETE_DESTROY,
800 if (error == EDEADLK) {
801 hammer_done_cursor(&cursor);
808 snap->head.error = error;
809 hammer_done_cursor(&cursor);
810 hammer_unlock(&hmp->snapshot_lock);
815 * Retrieve as many snapshot ids as possible or until the array is
816 * full, starting after the last transction id passed in. If count
817 * is 0 we retrieve starting at the beginning.
819 * NOTE: Because the b-tree key field is signed but transaction ids
820 * are unsigned the returned list will be signed-sorted instead
821 * of unsigned sorted. The Caller must still sort the aggregate
826 hammer_ioc_get_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
827 struct hammer_ioc_snapshot *snap)
829 struct hammer_cursor cursor;
835 if (snap->index != 0)
837 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
841 * Look for keys starting after the previous iteration, or at
842 * the beginning if snap->count is 0.
844 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
846 hammer_done_cursor(&cursor);
850 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
851 cursor.key_beg.create_tid = 0;
852 cursor.key_beg.delete_tid = 0;
853 cursor.key_beg.obj_type = 0;
854 cursor.key_beg.rec_type = HAMMER_RECTYPE_SNAPSHOT;
855 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
856 if (snap->count == 0)
857 cursor.key_beg.key = HAMMER_MIN_KEY;
859 cursor.key_beg.key = (int64_t)snap->snaps[snap->count - 1].tid + 1;
861 cursor.key_end = cursor.key_beg;
862 cursor.key_end.key = HAMMER_MAX_KEY;
863 cursor.asof = HAMMER_MAX_TID;
864 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE | HAMMER_CURSOR_ASOF;
868 error = hammer_btree_first(&cursor);
869 while (error == 0 && snap->count < HAMMER_SNAPS_PER_IOCTL) {
870 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF);
873 if (cursor.leaf->base.rec_type == HAMMER_RECTYPE_SNAPSHOT) {
874 error = hammer_btree_extract(
875 &cursor, HAMMER_CURSOR_GET_LEAF |
876 HAMMER_CURSOR_GET_DATA);
877 snap->snaps[snap->count] = cursor.data->snap;
880 * The snap data tid should match the key but might
881 * not due to a bug in the HAMMER v3 conversion code.
883 * This error will work itself out over time but we
884 * have to force a match or the snapshot will not
887 if (cursor.data->snap.tid !=
888 (hammer_tid_t)cursor.leaf->base.key) {
889 kprintf("HAMMER: lo=%08x snapshot key "
890 "0x%016jx data mismatch 0x%016jx\n",
891 cursor.key_beg.localization,
892 (uintmax_t)cursor.data->snap.tid,
893 cursor.leaf->base.key);
894 kprintf("HAMMER: Probably left over from the "
895 "original v3 conversion, hammer "
896 "cleanup should get it eventually\n");
897 snap->snaps[snap->count].tid =
898 cursor.leaf->base.key;
902 error = hammer_btree_iterate(&cursor);
905 if (error == ENOENT) {
906 snap->head.flags |= HAMMER_IOC_SNAPSHOT_EOF;
909 snap->head.error = error;
910 hammer_done_cursor(&cursor);
915 * Retrieve the PFS hammer cleanup utility config record. This is
916 * different (newer than) the PFS config.
920 hammer_ioc_get_config(hammer_transaction_t trans, hammer_inode_t ip,
921 struct hammer_ioc_config *config)
923 struct hammer_cursor cursor;
926 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
928 hammer_done_cursor(&cursor);
932 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
933 cursor.key_beg.create_tid = 0;
934 cursor.key_beg.delete_tid = 0;
935 cursor.key_beg.obj_type = 0;
936 cursor.key_beg.rec_type = HAMMER_RECTYPE_CONFIG;
937 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
938 cursor.key_beg.key = 0; /* config space page 0 */
940 cursor.asof = HAMMER_MAX_TID;
941 cursor.flags |= HAMMER_CURSOR_ASOF;
943 error = hammer_btree_lookup(&cursor);
945 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
946 HAMMER_CURSOR_GET_DATA);
948 config->config = cursor.data->config;
950 /* error can be ENOENT */
951 config->head.error = error;
952 hammer_done_cursor(&cursor);
957 * Retrieve the PFS hammer cleanup utility config record. This is
958 * different (newer than) the PFS config.
960 * This is kinda a hack.
964 hammer_ioc_set_config(hammer_transaction_t trans, hammer_inode_t ip,
965 struct hammer_ioc_config *config)
967 struct hammer_btree_leaf_elm leaf;
968 struct hammer_cursor cursor;
969 hammer_mount_t hmp = ip->hmp;
973 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
975 hammer_done_cursor(&cursor);
979 bzero(&leaf, sizeof(leaf));
980 leaf.base.obj_id = HAMMER_OBJID_ROOT;
981 leaf.base.rec_type = HAMMER_RECTYPE_CONFIG;
982 leaf.base.create_tid = hammer_alloc_tid(hmp, 1);
983 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
984 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
985 leaf.base.key = 0; /* page 0 */
986 leaf.data_len = sizeof(struct hammer_config_data);
988 cursor.key_beg = leaf.base;
990 cursor.asof = HAMMER_MAX_TID;
991 cursor.flags |= HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF;
993 error = hammer_btree_lookup(&cursor);
995 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
996 HAMMER_CURSOR_GET_DATA);
997 error = hammer_delete_at_cursor(&cursor, HAMMER_DELETE_DESTROY,
999 if (error == EDEADLK) {
1000 hammer_done_cursor(&cursor);
1004 if (error == ENOENT)
1008 * NOTE: Must reload key_beg after an ASOF search because
1009 * the create_tid may have been modified during the
1012 cursor.flags &= ~HAMMER_CURSOR_ASOF;
1013 cursor.key_beg = leaf.base;
1014 error = hammer_create_at_cursor(&cursor, &leaf,
1016 HAMMER_CREATE_MODE_SYS);
1017 if (error == EDEADLK) {
1018 hammer_done_cursor(&cursor);
1022 config->head.error = error;
1023 hammer_done_cursor(&cursor);
1029 hammer_ioc_pfs_iterate(hammer_transaction_t trans,
1030 struct hammer_ioc_pfs_iterate *pi)
1032 struct hammer_cursor cursor;
1036 ip = hammer_get_inode(trans, NULL, HAMMER_OBJID_ROOT, HAMMER_MAX_TID,
1037 HAMMER_DEF_LOCALIZATION, 0, &error);
1039 error = hammer_init_cursor(trans, &cursor,
1040 (ip ? &ip->cache[1] : NULL), ip);
1044 pi->head.flags &= ~HAMMER_PFSD_DELETED;
1046 cursor.key_beg.localization = HAMMER_DEF_LOCALIZATION +
1047 HAMMER_LOCALIZE_MISC;
1048 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
1049 cursor.key_beg.create_tid = 0;
1050 cursor.key_beg.delete_tid = 0;
1051 cursor.key_beg.rec_type = HAMMER_RECTYPE_PFS;
1052 cursor.key_beg.obj_type = 0;
1053 cursor.key_end = cursor.key_beg;
1054 cursor.key_end.key = HAMMER_MAX_KEY;
1055 cursor.asof = HAMMER_MAX_TID;
1056 cursor.flags |= HAMMER_CURSOR_ASOF;
1058 if (pi->pos < 0) /* Sanity check */
1062 cursor.key_beg.key = pi->pos;
1063 error = hammer_ip_lookup(&cursor);
1066 error = hammer_ip_resolve_data(&cursor);
1069 if (cursor.data->pfsd.mirror_flags & HAMMER_PFSD_DELETED)
1070 pi->head.flags |= HAMMER_PFSD_DELETED;
1072 copyout(cursor.data, pi->ondisk, cursor.leaf->data_len);
1073 pi->pos = (u_int32_t)(cursor.leaf->base.key >> 16);
1077 hammer_done_cursor(&cursor);
1079 hammer_rel_inode(ip, 0);
1086 hammer_ioc_get_data(hammer_transaction_t trans, hammer_inode_t ip,
1087 struct hammer_ioc_data *data)
1089 struct hammer_cursor cursor;
1093 /* XXX cached inode ? */
1094 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
1098 cursor.key_beg = data->elm;
1099 cursor.flags |= HAMMER_CURSOR_BACKEND;
1101 error = hammer_btree_lookup(&cursor);
1103 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
1104 HAMMER_CURSOR_GET_DATA);
1106 data->leaf = *cursor.leaf;
1107 bytes = cursor.leaf->data_len;
1108 if (bytes > data->size)
1110 error = copyout(cursor.data, data->ubuf, bytes);
1115 hammer_done_cursor(&cursor);