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_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);
63 hammer_ioctl(hammer_inode_t ip, u_long com, caddr_t data, int fflag,
66 struct hammer_transaction trans;
69 error = priv_check_cred(cred, PRIV_HAMMER_IOCTL, 0);
71 hammer_start_transaction(&trans, ip->hmp);
76 error = hammer_ioc_prune(&trans, ip,
77 (struct hammer_ioc_prune *)data);
80 case HAMMERIOC_GETHISTORY:
81 error = hammer_ioc_gethistory(&trans, ip,
82 (struct hammer_ioc_history *)data);
84 case HAMMERIOC_REBLOCK:
86 error = hammer_ioc_reblock(&trans, ip,
87 (struct hammer_ioc_reblock *)data);
90 case HAMMERIOC_REBALANCE:
92 error = hammer_ioc_rebalance(&trans, ip,
93 (struct hammer_ioc_rebalance *)data);
96 case HAMMERIOC_SYNCTID:
97 error = hammer_ioc_synctid(&trans, ip,
98 (struct hammer_ioc_synctid *)data);
100 case HAMMERIOC_GET_PSEUDOFS:
101 error = hammer_ioc_get_pseudofs(&trans, ip,
102 (struct hammer_ioc_pseudofs_rw *)data);
104 case HAMMERIOC_SET_PSEUDOFS:
106 error = hammer_ioc_set_pseudofs(&trans, ip, cred,
107 (struct hammer_ioc_pseudofs_rw *)data);
110 case HAMMERIOC_UPG_PSEUDOFS:
112 error = hammer_ioc_upgrade_pseudofs(&trans, ip,
113 (struct hammer_ioc_pseudofs_rw *)data);
116 case HAMMERIOC_DGD_PSEUDOFS:
118 error = hammer_ioc_downgrade_pseudofs(&trans, ip,
119 (struct hammer_ioc_pseudofs_rw *)data);
122 case HAMMERIOC_RMR_PSEUDOFS:
124 error = hammer_ioc_destroy_pseudofs(&trans, ip,
125 (struct hammer_ioc_pseudofs_rw *)data);
128 case HAMMERIOC_WAI_PSEUDOFS:
130 error = hammer_ioc_wait_pseudofs(&trans, ip,
131 (struct hammer_ioc_pseudofs_rw *)data);
134 case HAMMERIOC_MIRROR_READ:
136 error = hammer_ioc_mirror_read(&trans, ip,
137 (struct hammer_ioc_mirror_rw *)data);
140 case HAMMERIOC_MIRROR_WRITE:
142 error = hammer_ioc_mirror_write(&trans, ip,
143 (struct hammer_ioc_mirror_rw *)data);
146 case HAMMERIOC_GET_VERSION:
147 error = hammer_ioc_get_version(&trans, ip,
148 (struct hammer_ioc_version *)data);
150 case HAMMERIOC_GET_INFO:
151 error = hammer_ioc_get_info(&trans,
152 (struct hammer_ioc_info *)data);
154 case HAMMERIOC_SET_VERSION:
156 error = hammer_ioc_set_version(&trans, ip,
157 (struct hammer_ioc_version *)data);
160 case HAMMERIOC_ADD_VOLUME:
162 error = priv_check_cred(cred, PRIV_HAMMER_VOLUME, 0);
164 error = hammer_ioc_volume_add(&trans, ip,
165 (struct hammer_ioc_volume_add *)data);
168 case HAMMERIOC_ADD_SNAPSHOT:
170 error = hammer_ioc_add_snapshot(
171 &trans, ip, (struct hammer_ioc_snapshot *)data);
174 case HAMMERIOC_DEL_SNAPSHOT:
176 error = hammer_ioc_del_snapshot(
177 &trans, ip, (struct hammer_ioc_snapshot *)data);
180 case HAMMERIOC_GET_SNAPSHOT:
181 error = hammer_ioc_get_snapshot(
182 &trans, ip, (struct hammer_ioc_snapshot *)data);
184 case HAMMERIOC_GET_CONFIG:
185 error = hammer_ioc_get_config(
186 &trans, ip, (struct hammer_ioc_config *)data);
188 case HAMMERIOC_SET_CONFIG:
190 error = hammer_ioc_set_config(
191 &trans, ip, (struct hammer_ioc_config *)data);
198 hammer_done_transaction(&trans);
203 * Iterate through an object's inode or an object's records and record
206 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
207 hammer_btree_elm_t elm);
211 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
212 struct hammer_ioc_history *hist)
214 struct hammer_cursor cursor;
215 hammer_btree_elm_t elm;
219 * Validate the structure and initialize for return.
221 if (hist->beg_tid > hist->end_tid)
223 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
224 if (hist->key > hist->nxt_key)
228 hist->obj_id = ip->obj_id;
230 hist->nxt_tid = hist->end_tid;
231 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
232 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
233 hist->head.flags &= ~HAMMER_IOC_HISTORY_EOF;
234 hist->head.flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
235 if ((ip->flags & HAMMER_INODE_MODMASK) &
236 ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
237 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
241 * Setup the cursor. We can't handle undeletable records
242 * (create_tid of 0) at the moment. A create_tid of 0 has
243 * a special meaning and cannot be specified in the cursor.
245 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
247 hammer_done_cursor(&cursor);
251 cursor.key_beg.obj_id = hist->obj_id;
252 cursor.key_beg.create_tid = hist->beg_tid;
253 cursor.key_beg.delete_tid = 0;
254 cursor.key_beg.obj_type = 0;
255 if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
256 cursor.key_beg.create_tid = 1;
258 cursor.key_end.obj_id = hist->obj_id;
259 cursor.key_end.create_tid = hist->end_tid;
260 cursor.key_end.delete_tid = 0;
261 cursor.key_end.obj_type = 0;
263 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
265 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
267 * key-range within the file. For a regular file the
268 * on-disk key represents BASE+LEN, not BASE, so the
269 * first possible record containing the offset 'key'
270 * has an on-disk key of (key + 1).
272 cursor.key_beg.key = hist->key;
273 cursor.key_end.key = HAMMER_MAX_KEY;
274 cursor.key_beg.localization = ip->obj_localization +
275 HAMMER_LOCALIZE_MISC;
276 cursor.key_end.localization = ip->obj_localization +
277 HAMMER_LOCALIZE_MISC;
279 switch(ip->ino_data.obj_type) {
280 case HAMMER_OBJTYPE_REGFILE:
281 ++cursor.key_beg.key;
282 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
284 case HAMMER_OBJTYPE_DIRECTORY:
285 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
286 cursor.key_beg.localization = ip->obj_localization +
287 hammer_dir_localization(ip);
288 cursor.key_end.localization = ip->obj_localization +
289 hammer_dir_localization(ip);
291 case HAMMER_OBJTYPE_DBFILE:
292 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
298 cursor.key_end.rec_type = cursor.key_beg.rec_type;
303 cursor.key_beg.key = 0;
304 cursor.key_end.key = 0;
305 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
306 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
307 cursor.key_beg.localization = ip->obj_localization +
308 HAMMER_LOCALIZE_INODE;
309 cursor.key_end.localization = ip->obj_localization +
310 HAMMER_LOCALIZE_INODE;
313 error = hammer_btree_first(&cursor);
315 elm = &cursor.node->ondisk->elms[cursor.index];
317 add_history(ip, hist, elm);
318 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
319 HAMMER_IOC_HISTORY_NEXT_KEY |
320 HAMMER_IOC_HISTORY_EOF)) {
323 error = hammer_btree_iterate(&cursor);
325 if (error == ENOENT) {
326 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
329 hammer_done_cursor(&cursor);
334 * Add the scanned element to the ioctl return structure. Some special
335 * casing is required for regular files to accomodate how data ranges are
339 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
340 hammer_btree_elm_t elm)
344 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
346 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
347 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
351 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
352 hist->key < elm->leaf.base.key - elm->leaf.data_len) {
353 hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
355 if (hist->nxt_key > elm->leaf.base.key)
356 hist->nxt_key = elm->leaf.base.key;
359 * Record is beyond MAXPHYS, there won't be any more records
360 * in the iteration covering the requested offset (key).
362 if (elm->leaf.base.key >= MAXPHYS &&
363 elm->leaf.base.key - MAXPHYS > hist->key) {
364 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
368 * Data-range of record does not cover the key.
370 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
373 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
377 if (hist->nxt_key > elm->leaf.base.key &&
378 hist->key < elm->leaf.base.key) {
379 hist->nxt_key = elm->leaf.base.key;
383 * Record is beyond the requested key.
385 if (elm->leaf.base.key > hist->key)
386 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
390 * Add create_tid if it is in-bounds.
394 elm->leaf.base.create_tid != hist->hist_ary[i - 1].tid) &&
395 elm->leaf.base.create_tid >= hist->beg_tid &&
396 elm->leaf.base.create_tid < hist->end_tid) {
397 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
398 hist->nxt_tid = elm->leaf.base.create_tid;
399 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
402 hist->hist_ary[i].tid = elm->leaf.base.create_tid;
403 hist->hist_ary[i].time32 = elm->leaf.create_ts;
408 * Add delete_tid if it is in-bounds. Note that different portions
409 * of the history may have overlapping data ranges with different
410 * delete_tid's. If this case occurs the delete_tid may match the
411 * create_tid of a following record. XXX
417 if (elm->leaf.base.delete_tid &&
418 elm->leaf.base.delete_tid >= hist->beg_tid &&
419 elm->leaf.base.delete_tid < hist->end_tid) {
420 if (i == HAMMER_MAX_HISTORY_ELMS) {
421 hist->nxt_tid = elm->leaf.base.delete_tid;
422 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
425 hist->hist_ary[i].tid = elm->leaf.base.delete_tid;
426 hist->hist_ary[i].time32 = elm->leaf.delete_ts;
432 * Acquire synchronization TID
436 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
437 struct hammer_ioc_synctid *std)
439 hammer_mount_t hmp = ip->hmp;
443 case HAMMER_SYNCTID_NONE:
444 std->tid = hmp->flusher.tid; /* inaccurate */
446 case HAMMER_SYNCTID_ASYNC:
447 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
448 hammer_flusher_async(hmp, NULL);
449 std->tid = hmp->flusher.tid; /* inaccurate */
451 case HAMMER_SYNCTID_SYNC1:
452 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
453 hammer_flusher_sync(hmp);
454 std->tid = hmp->flusher.tid;
456 case HAMMER_SYNCTID_SYNC2:
457 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
458 hammer_flusher_sync(hmp);
459 std->tid = hmp->flusher.tid;
460 hammer_flusher_sync(hmp);
470 * Retrieve version info.
472 * Load min_version, wip_version, and max_versino. If cur_version is passed
473 * as 0 then load the current version into cur_version. Load the description
474 * for cur_version into the description array.
476 * Returns 0 on success, EINVAL if cur_version is non-zero and set to an
481 hammer_ioc_get_version(hammer_transaction_t trans, hammer_inode_t ip,
482 struct hammer_ioc_version *ver)
486 ver->min_version = HAMMER_VOL_VERSION_MIN;
487 ver->wip_version = HAMMER_VOL_VERSION_WIP;
488 ver->max_version = HAMMER_VOL_VERSION_MAX;
489 if (ver->cur_version == 0)
490 ver->cur_version = trans->hmp->version;
491 switch(ver->cur_version) {
493 ksnprintf(ver->description, sizeof(ver->description),
494 "First HAMMER release (DragonFly 2.0+)");
497 ksnprintf(ver->description, sizeof(ver->description),
498 "New directory entry layout (DragonFly 2.3+)");
501 ksnprintf(ver->description, sizeof(ver->description),
502 "New snapshot management (DragonFly 2.5+)");
505 ksnprintf(ver->description, sizeof(ver->description),
506 "New undo/flush, faster flush/sync (DragonFly 2.5+)");
509 ksnprintf(ver->description, sizeof(ver->description),
522 hammer_ioc_set_version(hammer_transaction_t trans, hammer_inode_t ip,
523 struct hammer_ioc_version *ver)
525 hammer_mount_t hmp = trans->hmp;
526 struct hammer_cursor cursor;
527 hammer_volume_t volume;
529 int over = hmp->version;
532 * Generally do not allow downgrades. However, version 4 can
533 * be downgraded to version 3.
535 if (ver->cur_version < hmp->version) {
536 if (!(ver->cur_version == 3 && hmp->version == 4))
539 if (ver->cur_version == hmp->version)
541 if (ver->cur_version > HAMMER_VOL_VERSION_MAX)
547 * Update the root volume header and the version cached in
548 * the hammer_mount structure.
550 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
553 hammer_lock_ex(&hmp->flusher.finalize_lock);
554 hammer_sync_lock_ex(trans);
555 hmp->version = ver->cur_version;
558 * If upgrading from version < 4 to version >= 4 the UNDO FIFO
559 * must be reinitialized.
561 if (over < HAMMER_VOL_VERSION_FOUR &&
562 ver->cur_version >= HAMMER_VOL_VERSION_FOUR) {
563 kprintf("upgrade undo to version 4\n");
564 error = hammer_upgrade_undo_4(trans);
570 * Adjust the version in the volume header
572 volume = hammer_get_root_volume(hmp, &error);
573 KKASSERT(error == 0);
574 hammer_modify_volume_field(cursor.trans, volume, vol_version);
575 volume->ondisk->vol_version = ver->cur_version;
576 hammer_modify_volume_done(volume);
577 hammer_rel_volume(volume, 0);
579 hammer_sync_unlock(trans);
580 hammer_unlock(&hmp->flusher.finalize_lock);
582 ver->head.error = error;
583 hammer_done_cursor(&cursor);
592 hammer_ioc_get_info(hammer_transaction_t trans, struct hammer_ioc_info *info) {
594 struct hammer_volume_ondisk *od = trans->hmp->rootvol->ondisk;
595 struct hammer_mount *hm = trans->hmp;
597 /* Fill the structure with the necessary information */
598 _hammer_checkspace(hm, HAMMER_CHKSPC_WRITE, &info->rsvbigblocks);
599 info->rsvbigblocks = info->rsvbigblocks >> HAMMER_LARGEBLOCK_BITS;
600 strlcpy(info->vol_name, od->vol_name, sizeof(od->vol_name));
602 info->vol_fsid = hm->fsid;
603 info->vol_fstype = od->vol_fstype;
604 info->version = hm->version;
606 info->inodes = od->vol0_stat_inodes;
607 info->bigblocks = od->vol0_stat_bigblocks;
608 info->freebigblocks = od->vol0_stat_freebigblocks;
609 info->nvolumes = hm->nvolumes;
615 * Add a snapshot transction id(s) to the list of snapshots.
617 * NOTE: Records are created with an allocated TID. If a flush cycle
618 * is in progress the record may be synced in the current flush
619 * cycle and the volume header will reflect the allocation of the
620 * TID, but the synchronization point may not catch up to the
621 * TID until the next flush cycle.
625 hammer_ioc_add_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
626 struct hammer_ioc_snapshot *snap)
628 hammer_mount_t hmp = ip->hmp;
629 struct hammer_btree_leaf_elm leaf;
630 struct hammer_cursor cursor;
636 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
638 if (snap->index > snap->count)
641 hammer_lock_ex(&hmp->snapshot_lock);
644 * Look for keys starting after the previous iteration, or at
645 * the beginning if snap->count is 0.
647 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
649 hammer_done_cursor(&cursor);
653 cursor.asof = HAMMER_MAX_TID;
654 cursor.flags |= HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF;
656 bzero(&leaf, sizeof(leaf));
657 leaf.base.obj_id = HAMMER_OBJID_ROOT;
658 leaf.base.rec_type = HAMMER_RECTYPE_SNAPSHOT;
659 leaf.base.create_tid = hammer_alloc_tid(hmp, 1);
660 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
661 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
662 leaf.data_len = sizeof(struct hammer_snapshot_data);
664 while (snap->index < snap->count) {
665 leaf.base.key = (int64_t)snap->snaps[snap->index].tid;
666 cursor.key_beg = leaf.base;
667 error = hammer_btree_lookup(&cursor);
673 cursor.flags &= ~HAMMER_CURSOR_ASOF;
674 error = hammer_create_at_cursor(&cursor, &leaf,
675 &snap->snaps[snap->index],
676 HAMMER_CREATE_MODE_SYS);
677 if (error == EDEADLK) {
678 hammer_done_cursor(&cursor);
681 cursor.flags |= HAMMER_CURSOR_ASOF;
686 snap->head.error = error;
687 hammer_done_cursor(&cursor);
688 hammer_unlock(&hmp->snapshot_lock);
693 * Delete snapshot transaction id(s) from the list of snapshots.
697 hammer_ioc_del_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
698 struct hammer_ioc_snapshot *snap)
700 hammer_mount_t hmp = ip->hmp;
701 struct hammer_cursor cursor;
707 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
709 if (snap->index > snap->count)
712 hammer_lock_ex(&hmp->snapshot_lock);
715 * Look for keys starting after the previous iteration, or at
716 * the beginning if snap->count is 0.
718 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
720 hammer_done_cursor(&cursor);
724 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
725 cursor.key_beg.create_tid = 0;
726 cursor.key_beg.delete_tid = 0;
727 cursor.key_beg.obj_type = 0;
728 cursor.key_beg.rec_type = HAMMER_RECTYPE_SNAPSHOT;
729 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
730 cursor.asof = HAMMER_MAX_TID;
731 cursor.flags |= HAMMER_CURSOR_ASOF;
733 while (snap->index < snap->count) {
734 cursor.key_beg.key = (int64_t)snap->snaps[snap->index].tid;
735 error = hammer_btree_lookup(&cursor);
738 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF);
741 error = hammer_delete_at_cursor(&cursor, HAMMER_DELETE_DESTROY,
743 if (error == EDEADLK) {
744 hammer_done_cursor(&cursor);
751 snap->head.error = error;
752 hammer_done_cursor(&cursor);
753 hammer_unlock(&hmp->snapshot_lock);
758 * Retrieve as many snapshot ids as possible or until the array is
759 * full, starting after the last transction id passed in. If count
760 * is 0 we retrieve starting at the beginning.
762 * NOTE: Because the b-tree key field is signed but transaction ids
763 * are unsigned the returned list will be signed-sorted instead
764 * of unsigned sorted. The Caller must still sort the aggregate
769 hammer_ioc_get_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
770 struct hammer_ioc_snapshot *snap)
772 struct hammer_cursor cursor;
778 if (snap->index != 0)
780 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
784 * Look for keys starting after the previous iteration, or at
785 * the beginning if snap->count is 0.
787 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
789 hammer_done_cursor(&cursor);
793 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
794 cursor.key_beg.create_tid = 0;
795 cursor.key_beg.delete_tid = 0;
796 cursor.key_beg.obj_type = 0;
797 cursor.key_beg.rec_type = HAMMER_RECTYPE_SNAPSHOT;
798 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
799 if (snap->count == 0)
800 cursor.key_beg.key = HAMMER_MIN_KEY;
802 cursor.key_beg.key = (int64_t)snap->snaps[snap->count - 1].tid + 1;
804 cursor.key_end = cursor.key_beg;
805 cursor.key_end.key = HAMMER_MAX_KEY;
806 cursor.asof = HAMMER_MAX_TID;
807 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE | HAMMER_CURSOR_ASOF;
811 error = hammer_btree_first(&cursor);
812 while (error == 0 && snap->count < HAMMER_SNAPS_PER_IOCTL) {
813 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF);
816 if (cursor.leaf->base.rec_type == HAMMER_RECTYPE_SNAPSHOT) {
817 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
818 HAMMER_CURSOR_GET_DATA);
819 snap->snaps[snap->count] = cursor.data->snap;
822 error = hammer_btree_iterate(&cursor);
825 if (error == ENOENT) {
826 snap->head.flags |= HAMMER_IOC_SNAPSHOT_EOF;
829 snap->head.error = error;
830 hammer_done_cursor(&cursor);
835 * Retrieve the PFS hammer cleanup utility config record. This is
836 * different (newer than) the PFS config.
840 hammer_ioc_get_config(hammer_transaction_t trans, hammer_inode_t ip,
841 struct hammer_ioc_config *config)
843 struct hammer_cursor cursor;
846 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
848 hammer_done_cursor(&cursor);
852 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
853 cursor.key_beg.create_tid = 0;
854 cursor.key_beg.delete_tid = 0;
855 cursor.key_beg.obj_type = 0;
856 cursor.key_beg.rec_type = HAMMER_RECTYPE_CONFIG;
857 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
858 cursor.key_beg.key = 0; /* config space page 0 */
860 cursor.asof = HAMMER_MAX_TID;
861 cursor.flags |= HAMMER_CURSOR_ASOF;
863 error = hammer_btree_lookup(&cursor);
865 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
866 HAMMER_CURSOR_GET_DATA);
868 config->config = cursor.data->config;
870 /* error can be ENOENT */
871 config->head.error = error;
872 hammer_done_cursor(&cursor);
877 * Retrieve the PFS hammer cleanup utility config record. This is
878 * different (newer than) the PFS config.
880 * This is kinda a hack.
884 hammer_ioc_set_config(hammer_transaction_t trans, hammer_inode_t ip,
885 struct hammer_ioc_config *config)
887 struct hammer_btree_leaf_elm leaf;
888 struct hammer_cursor cursor;
889 hammer_mount_t hmp = ip->hmp;
893 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
895 hammer_done_cursor(&cursor);
899 bzero(&leaf, sizeof(leaf));
900 leaf.base.obj_id = HAMMER_OBJID_ROOT;
901 leaf.base.rec_type = HAMMER_RECTYPE_CONFIG;
902 leaf.base.create_tid = hammer_alloc_tid(hmp, 1);
903 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
904 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
905 leaf.base.key = 0; /* page 0 */
906 leaf.data_len = sizeof(struct hammer_config_data);
908 cursor.key_beg = leaf.base;
910 cursor.asof = HAMMER_MAX_TID;
911 cursor.flags |= HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF;
913 error = hammer_btree_lookup(&cursor);
915 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
916 HAMMER_CURSOR_GET_DATA);
917 error = hammer_delete_at_cursor(&cursor, HAMMER_DELETE_DESTROY,
919 if (error == EDEADLK) {
920 hammer_done_cursor(&cursor);
927 cursor.flags &= ~HAMMER_CURSOR_ASOF;
928 cursor.key_beg = leaf.base;
929 error = hammer_create_at_cursor(&cursor, &leaf,
931 HAMMER_CREATE_MODE_SYS);
932 if (error == EDEADLK) {
933 hammer_done_cursor(&cursor);
937 config->head.error = error;
938 hammer_done_cursor(&cursor);