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 *)data);
168 case HAMMERIOC_DEL_VOLUME:
170 error = priv_check_cred(cred, PRIV_HAMMER_VOLUME, 0);
172 error = hammer_ioc_volume_del(&trans, ip,
173 (struct hammer_ioc_volume *)data);
176 case HAMMERIOC_ADD_SNAPSHOT:
178 error = hammer_ioc_add_snapshot(
179 &trans, ip, (struct hammer_ioc_snapshot *)data);
182 case HAMMERIOC_DEL_SNAPSHOT:
184 error = hammer_ioc_del_snapshot(
185 &trans, ip, (struct hammer_ioc_snapshot *)data);
188 case HAMMERIOC_GET_SNAPSHOT:
189 error = hammer_ioc_get_snapshot(
190 &trans, ip, (struct hammer_ioc_snapshot *)data);
192 case HAMMERIOC_GET_CONFIG:
193 error = hammer_ioc_get_config(
194 &trans, ip, (struct hammer_ioc_config *)data);
196 case HAMMERIOC_SET_CONFIG:
198 error = hammer_ioc_set_config(
199 &trans, ip, (struct hammer_ioc_config *)data);
206 hammer_done_transaction(&trans);
211 * Iterate through an object's inode or an object's records and record
214 static void add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
215 hammer_btree_elm_t elm);
219 hammer_ioc_gethistory(hammer_transaction_t trans, hammer_inode_t ip,
220 struct hammer_ioc_history *hist)
222 struct hammer_cursor cursor;
223 hammer_btree_elm_t elm;
227 * Validate the structure and initialize for return.
229 if (hist->beg_tid > hist->end_tid)
231 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
232 if (hist->key > hist->nxt_key)
236 hist->obj_id = ip->obj_id;
238 hist->nxt_tid = hist->end_tid;
239 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_TID;
240 hist->head.flags &= ~HAMMER_IOC_HISTORY_NEXT_KEY;
241 hist->head.flags &= ~HAMMER_IOC_HISTORY_EOF;
242 hist->head.flags &= ~HAMMER_IOC_HISTORY_UNSYNCED;
243 if ((ip->flags & HAMMER_INODE_MODMASK) &
244 ~(HAMMER_INODE_ATIME | HAMMER_INODE_MTIME)) {
245 hist->head.flags |= HAMMER_IOC_HISTORY_UNSYNCED;
249 * Setup the cursor. We can't handle undeletable records
250 * (create_tid of 0) at the moment. A create_tid of 0 has
251 * a special meaning and cannot be specified in the cursor.
253 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
255 hammer_done_cursor(&cursor);
259 cursor.key_beg.obj_id = hist->obj_id;
260 cursor.key_beg.create_tid = hist->beg_tid;
261 cursor.key_beg.delete_tid = 0;
262 cursor.key_beg.obj_type = 0;
263 if (cursor.key_beg.create_tid == HAMMER_MIN_TID)
264 cursor.key_beg.create_tid = 1;
266 cursor.key_end.obj_id = hist->obj_id;
267 cursor.key_end.create_tid = hist->end_tid;
268 cursor.key_end.delete_tid = 0;
269 cursor.key_end.obj_type = 0;
271 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE;
273 if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
275 * key-range within the file. For a regular file the
276 * on-disk key represents BASE+LEN, not BASE, so the
277 * first possible record containing the offset 'key'
278 * has an on-disk key of (key + 1).
280 cursor.key_beg.key = hist->key;
281 cursor.key_end.key = HAMMER_MAX_KEY;
282 cursor.key_beg.localization = ip->obj_localization +
283 HAMMER_LOCALIZE_MISC;
284 cursor.key_end.localization = ip->obj_localization +
285 HAMMER_LOCALIZE_MISC;
287 switch(ip->ino_data.obj_type) {
288 case HAMMER_OBJTYPE_REGFILE:
289 ++cursor.key_beg.key;
290 cursor.key_beg.rec_type = HAMMER_RECTYPE_DATA;
292 case HAMMER_OBJTYPE_DIRECTORY:
293 cursor.key_beg.rec_type = HAMMER_RECTYPE_DIRENTRY;
294 cursor.key_beg.localization = ip->obj_localization +
295 hammer_dir_localization(ip);
296 cursor.key_end.localization = ip->obj_localization +
297 hammer_dir_localization(ip);
299 case HAMMER_OBJTYPE_DBFILE:
300 cursor.key_beg.rec_type = HAMMER_RECTYPE_DB;
306 cursor.key_end.rec_type = cursor.key_beg.rec_type;
311 cursor.key_beg.key = 0;
312 cursor.key_end.key = 0;
313 cursor.key_beg.rec_type = HAMMER_RECTYPE_INODE;
314 cursor.key_end.rec_type = HAMMER_RECTYPE_INODE;
315 cursor.key_beg.localization = ip->obj_localization +
316 HAMMER_LOCALIZE_INODE;
317 cursor.key_end.localization = ip->obj_localization +
318 HAMMER_LOCALIZE_INODE;
321 error = hammer_btree_first(&cursor);
323 elm = &cursor.node->ondisk->elms[cursor.index];
325 add_history(ip, hist, elm);
326 if (hist->head.flags & (HAMMER_IOC_HISTORY_NEXT_TID |
327 HAMMER_IOC_HISTORY_NEXT_KEY |
328 HAMMER_IOC_HISTORY_EOF)) {
331 error = hammer_btree_iterate(&cursor);
333 if (error == ENOENT) {
334 hist->head.flags |= HAMMER_IOC_HISTORY_EOF;
337 hammer_done_cursor(&cursor);
342 * Add the scanned element to the ioctl return structure. Some special
343 * casing is required for regular files to accomodate how data ranges are
347 add_history(hammer_inode_t ip, struct hammer_ioc_history *hist,
348 hammer_btree_elm_t elm)
352 if (elm->base.btype != HAMMER_BTREE_TYPE_RECORD)
354 if ((hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) &&
355 ip->ino_data.obj_type == HAMMER_OBJTYPE_REGFILE) {
359 if (hist->nxt_key > elm->leaf.base.key - elm->leaf.data_len &&
360 hist->key < elm->leaf.base.key - elm->leaf.data_len) {
361 hist->nxt_key = elm->leaf.base.key - elm->leaf.data_len;
363 if (hist->nxt_key > elm->leaf.base.key)
364 hist->nxt_key = elm->leaf.base.key;
367 * Record is beyond MAXPHYS, there won't be any more records
368 * in the iteration covering the requested offset (key).
370 if (elm->leaf.base.key >= MAXPHYS &&
371 elm->leaf.base.key - MAXPHYS > hist->key) {
372 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
376 * Data-range of record does not cover the key.
378 if (elm->leaf.base.key - elm->leaf.data_len > hist->key)
381 } else if (hist->head.flags & HAMMER_IOC_HISTORY_ATKEY) {
385 if (hist->nxt_key > elm->leaf.base.key &&
386 hist->key < elm->leaf.base.key) {
387 hist->nxt_key = elm->leaf.base.key;
391 * Record is beyond the requested key.
393 if (elm->leaf.base.key > hist->key)
394 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_KEY;
398 * Add create_tid if it is in-bounds.
402 elm->leaf.base.create_tid != hist->hist_ary[i - 1].tid) &&
403 elm->leaf.base.create_tid >= hist->beg_tid &&
404 elm->leaf.base.create_tid < hist->end_tid) {
405 if (hist->count == HAMMER_MAX_HISTORY_ELMS) {
406 hist->nxt_tid = elm->leaf.base.create_tid;
407 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
410 hist->hist_ary[i].tid = elm->leaf.base.create_tid;
411 hist->hist_ary[i].time32 = elm->leaf.create_ts;
416 * Add delete_tid if it is in-bounds. Note that different portions
417 * of the history may have overlapping data ranges with different
418 * delete_tid's. If this case occurs the delete_tid may match the
419 * create_tid of a following record. XXX
425 if (elm->leaf.base.delete_tid &&
426 elm->leaf.base.delete_tid >= hist->beg_tid &&
427 elm->leaf.base.delete_tid < hist->end_tid) {
428 if (i == HAMMER_MAX_HISTORY_ELMS) {
429 hist->nxt_tid = elm->leaf.base.delete_tid;
430 hist->head.flags |= HAMMER_IOC_HISTORY_NEXT_TID;
433 hist->hist_ary[i].tid = elm->leaf.base.delete_tid;
434 hist->hist_ary[i].time32 = elm->leaf.delete_ts;
440 * Acquire synchronization TID
444 hammer_ioc_synctid(hammer_transaction_t trans, hammer_inode_t ip,
445 struct hammer_ioc_synctid *std)
447 hammer_mount_t hmp = ip->hmp;
451 case HAMMER_SYNCTID_NONE:
452 std->tid = hmp->flusher.tid; /* inaccurate */
454 case HAMMER_SYNCTID_ASYNC:
455 hammer_queue_inodes_flusher(hmp, MNT_NOWAIT);
456 hammer_flusher_async(hmp, NULL);
457 std->tid = hmp->flusher.tid; /* inaccurate */
459 case HAMMER_SYNCTID_SYNC1:
460 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
461 hammer_flusher_sync(hmp);
462 std->tid = hmp->flusher.tid;
464 case HAMMER_SYNCTID_SYNC2:
465 hammer_queue_inodes_flusher(hmp, MNT_WAIT);
466 hammer_flusher_sync(hmp);
467 std->tid = hmp->flusher.tid;
468 hammer_flusher_sync(hmp);
478 * Retrieve version info.
480 * Load min_version, wip_version, and max_versino. If cur_version is passed
481 * as 0 then load the current version into cur_version. Load the description
482 * for cur_version into the description array.
484 * Returns 0 on success, EINVAL if cur_version is non-zero and set to an
489 hammer_ioc_get_version(hammer_transaction_t trans, hammer_inode_t ip,
490 struct hammer_ioc_version *ver)
494 ver->min_version = HAMMER_VOL_VERSION_MIN;
495 ver->wip_version = HAMMER_VOL_VERSION_WIP;
496 ver->max_version = HAMMER_VOL_VERSION_MAX;
497 if (ver->cur_version == 0)
498 ver->cur_version = trans->hmp->version;
499 switch(ver->cur_version) {
501 ksnprintf(ver->description, sizeof(ver->description),
502 "First HAMMER release (DragonFly 2.0+)");
505 ksnprintf(ver->description, sizeof(ver->description),
506 "New directory entry layout (DragonFly 2.3+)");
509 ksnprintf(ver->description, sizeof(ver->description),
510 "New snapshot management (DragonFly 2.5+)");
513 ksnprintf(ver->description, sizeof(ver->description),
514 "New undo/flush, faster flush/sync (DragonFly 2.5+)");
517 ksnprintf(ver->description, sizeof(ver->description),
530 hammer_ioc_set_version(hammer_transaction_t trans, hammer_inode_t ip,
531 struct hammer_ioc_version *ver)
533 hammer_mount_t hmp = trans->hmp;
534 struct hammer_cursor cursor;
535 hammer_volume_t volume;
537 int over = hmp->version;
540 * Generally do not allow downgrades. However, version 4 can
541 * be downgraded to version 3.
543 if (ver->cur_version < hmp->version) {
544 if (!(ver->cur_version == 3 && hmp->version == 4))
547 if (ver->cur_version == hmp->version)
549 if (ver->cur_version > HAMMER_VOL_VERSION_MAX)
555 * Update the root volume header and the version cached in
556 * the hammer_mount structure.
558 error = hammer_init_cursor(trans, &cursor, NULL, NULL);
561 hammer_lock_ex(&hmp->flusher.finalize_lock);
562 hammer_sync_lock_ex(trans);
563 hmp->version = ver->cur_version;
566 * If upgrading from version < 4 to version >= 4 the UNDO FIFO
567 * must be reinitialized.
569 if (over < HAMMER_VOL_VERSION_FOUR &&
570 ver->cur_version >= HAMMER_VOL_VERSION_FOUR) {
571 kprintf("upgrade undo to version 4\n");
572 error = hammer_upgrade_undo_4(trans);
578 * Adjust the version in the volume header
580 volume = hammer_get_root_volume(hmp, &error);
581 KKASSERT(error == 0);
582 hammer_modify_volume_field(cursor.trans, volume, vol_version);
583 volume->ondisk->vol_version = ver->cur_version;
584 hammer_modify_volume_done(volume);
585 hammer_rel_volume(volume, 0);
587 hammer_sync_unlock(trans);
588 hammer_unlock(&hmp->flusher.finalize_lock);
590 ver->head.error = error;
591 hammer_done_cursor(&cursor);
600 hammer_ioc_get_info(hammer_transaction_t trans, struct hammer_ioc_info *info) {
602 struct hammer_volume_ondisk *od = trans->hmp->rootvol->ondisk;
603 struct hammer_mount *hm = trans->hmp;
605 /* Fill the structure with the necessary information */
606 _hammer_checkspace(hm, HAMMER_CHKSPC_WRITE, &info->rsvbigblocks);
607 info->rsvbigblocks = info->rsvbigblocks >> HAMMER_LARGEBLOCK_BITS;
608 strlcpy(info->vol_name, od->vol_name, sizeof(od->vol_name));
610 info->vol_fsid = hm->fsid;
611 info->vol_fstype = od->vol_fstype;
612 info->version = hm->version;
614 info->inodes = od->vol0_stat_inodes;
615 info->bigblocks = od->vol0_stat_bigblocks;
616 info->freebigblocks = od->vol0_stat_freebigblocks;
617 info->nvolumes = hm->nvolumes;
623 * Add a snapshot transction id(s) to the list of snapshots.
625 * NOTE: Records are created with an allocated TID. If a flush cycle
626 * is in progress the record may be synced in the current flush
627 * cycle and the volume header will reflect the allocation of the
628 * TID, but the synchronization point may not catch up to the
629 * TID until the next flush cycle.
633 hammer_ioc_add_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
634 struct hammer_ioc_snapshot *snap)
636 hammer_mount_t hmp = ip->hmp;
637 struct hammer_btree_leaf_elm leaf;
638 struct hammer_cursor cursor;
644 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
646 if (snap->index > snap->count)
649 hammer_lock_ex(&hmp->snapshot_lock);
652 * Look for keys starting after the previous iteration, or at
653 * the beginning if snap->count is 0.
655 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
657 hammer_done_cursor(&cursor);
661 cursor.asof = HAMMER_MAX_TID;
662 cursor.flags |= HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF;
664 bzero(&leaf, sizeof(leaf));
665 leaf.base.obj_id = HAMMER_OBJID_ROOT;
666 leaf.base.rec_type = HAMMER_RECTYPE_SNAPSHOT;
667 leaf.base.create_tid = hammer_alloc_tid(hmp, 1);
668 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
669 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
670 leaf.data_len = sizeof(struct hammer_snapshot_data);
672 while (snap->index < snap->count) {
673 leaf.base.key = (int64_t)snap->snaps[snap->index].tid;
674 cursor.key_beg = leaf.base;
675 error = hammer_btree_lookup(&cursor);
681 cursor.flags &= ~HAMMER_CURSOR_ASOF;
682 error = hammer_create_at_cursor(&cursor, &leaf,
683 &snap->snaps[snap->index],
684 HAMMER_CREATE_MODE_SYS);
685 if (error == EDEADLK) {
686 hammer_done_cursor(&cursor);
689 cursor.flags |= HAMMER_CURSOR_ASOF;
694 snap->head.error = error;
695 hammer_done_cursor(&cursor);
696 hammer_unlock(&hmp->snapshot_lock);
701 * Delete snapshot transaction id(s) from the list of snapshots.
705 hammer_ioc_del_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
706 struct hammer_ioc_snapshot *snap)
708 hammer_mount_t hmp = ip->hmp;
709 struct hammer_cursor cursor;
715 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
717 if (snap->index > snap->count)
720 hammer_lock_ex(&hmp->snapshot_lock);
723 * Look for keys starting after the previous iteration, or at
724 * the beginning if snap->count is 0.
726 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
728 hammer_done_cursor(&cursor);
732 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
733 cursor.key_beg.create_tid = 0;
734 cursor.key_beg.delete_tid = 0;
735 cursor.key_beg.obj_type = 0;
736 cursor.key_beg.rec_type = HAMMER_RECTYPE_SNAPSHOT;
737 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
738 cursor.asof = HAMMER_MAX_TID;
739 cursor.flags |= HAMMER_CURSOR_ASOF;
741 while (snap->index < snap->count) {
742 cursor.key_beg.key = (int64_t)snap->snaps[snap->index].tid;
743 error = hammer_btree_lookup(&cursor);
746 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF);
749 error = hammer_delete_at_cursor(&cursor, HAMMER_DELETE_DESTROY,
751 if (error == EDEADLK) {
752 hammer_done_cursor(&cursor);
759 snap->head.error = error;
760 hammer_done_cursor(&cursor);
761 hammer_unlock(&hmp->snapshot_lock);
766 * Retrieve as many snapshot ids as possible or until the array is
767 * full, starting after the last transction id passed in. If count
768 * is 0 we retrieve starting at the beginning.
770 * NOTE: Because the b-tree key field is signed but transaction ids
771 * are unsigned the returned list will be signed-sorted instead
772 * of unsigned sorted. The Caller must still sort the aggregate
777 hammer_ioc_get_snapshot(hammer_transaction_t trans, hammer_inode_t ip,
778 struct hammer_ioc_snapshot *snap)
780 struct hammer_cursor cursor;
786 if (snap->index != 0)
788 if (snap->count > HAMMER_SNAPS_PER_IOCTL)
792 * Look for keys starting after the previous iteration, or at
793 * the beginning if snap->count is 0.
795 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
797 hammer_done_cursor(&cursor);
801 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
802 cursor.key_beg.create_tid = 0;
803 cursor.key_beg.delete_tid = 0;
804 cursor.key_beg.obj_type = 0;
805 cursor.key_beg.rec_type = HAMMER_RECTYPE_SNAPSHOT;
806 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
807 if (snap->count == 0)
808 cursor.key_beg.key = HAMMER_MIN_KEY;
810 cursor.key_beg.key = (int64_t)snap->snaps[snap->count - 1].tid + 1;
812 cursor.key_end = cursor.key_beg;
813 cursor.key_end.key = HAMMER_MAX_KEY;
814 cursor.asof = HAMMER_MAX_TID;
815 cursor.flags |= HAMMER_CURSOR_END_EXCLUSIVE | HAMMER_CURSOR_ASOF;
819 error = hammer_btree_first(&cursor);
820 while (error == 0 && snap->count < HAMMER_SNAPS_PER_IOCTL) {
821 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF);
824 if (cursor.leaf->base.rec_type == HAMMER_RECTYPE_SNAPSHOT) {
825 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
826 HAMMER_CURSOR_GET_DATA);
827 snap->snaps[snap->count] = cursor.data->snap;
830 error = hammer_btree_iterate(&cursor);
833 if (error == ENOENT) {
834 snap->head.flags |= HAMMER_IOC_SNAPSHOT_EOF;
837 snap->head.error = error;
838 hammer_done_cursor(&cursor);
843 * Retrieve the PFS hammer cleanup utility config record. This is
844 * different (newer than) the PFS config.
848 hammer_ioc_get_config(hammer_transaction_t trans, hammer_inode_t ip,
849 struct hammer_ioc_config *config)
851 struct hammer_cursor cursor;
854 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
856 hammer_done_cursor(&cursor);
860 cursor.key_beg.obj_id = HAMMER_OBJID_ROOT;
861 cursor.key_beg.create_tid = 0;
862 cursor.key_beg.delete_tid = 0;
863 cursor.key_beg.obj_type = 0;
864 cursor.key_beg.rec_type = HAMMER_RECTYPE_CONFIG;
865 cursor.key_beg.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
866 cursor.key_beg.key = 0; /* config space page 0 */
868 cursor.asof = HAMMER_MAX_TID;
869 cursor.flags |= HAMMER_CURSOR_ASOF;
871 error = hammer_btree_lookup(&cursor);
873 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
874 HAMMER_CURSOR_GET_DATA);
876 config->config = cursor.data->config;
878 /* error can be ENOENT */
879 config->head.error = error;
880 hammer_done_cursor(&cursor);
885 * Retrieve the PFS hammer cleanup utility config record. This is
886 * different (newer than) the PFS config.
888 * This is kinda a hack.
892 hammer_ioc_set_config(hammer_transaction_t trans, hammer_inode_t ip,
893 struct hammer_ioc_config *config)
895 struct hammer_btree_leaf_elm leaf;
896 struct hammer_cursor cursor;
897 hammer_mount_t hmp = ip->hmp;
901 error = hammer_init_cursor(trans, &cursor, &ip->cache[0], NULL);
903 hammer_done_cursor(&cursor);
907 bzero(&leaf, sizeof(leaf));
908 leaf.base.obj_id = HAMMER_OBJID_ROOT;
909 leaf.base.rec_type = HAMMER_RECTYPE_CONFIG;
910 leaf.base.create_tid = hammer_alloc_tid(hmp, 1);
911 leaf.base.btype = HAMMER_BTREE_TYPE_RECORD;
912 leaf.base.localization = ip->obj_localization + HAMMER_LOCALIZE_INODE;
913 leaf.base.key = 0; /* page 0 */
914 leaf.data_len = sizeof(struct hammer_config_data);
916 cursor.key_beg = leaf.base;
918 cursor.asof = HAMMER_MAX_TID;
919 cursor.flags |= HAMMER_CURSOR_BACKEND | HAMMER_CURSOR_ASOF;
921 error = hammer_btree_lookup(&cursor);
923 error = hammer_btree_extract(&cursor, HAMMER_CURSOR_GET_LEAF |
924 HAMMER_CURSOR_GET_DATA);
925 error = hammer_delete_at_cursor(&cursor, HAMMER_DELETE_DESTROY,
927 if (error == EDEADLK) {
928 hammer_done_cursor(&cursor);
935 cursor.flags &= ~HAMMER_CURSOR_ASOF;
936 cursor.key_beg = leaf.base;
937 error = hammer_create_at_cursor(&cursor, &leaf,
939 HAMMER_CREATE_MODE_SYS);
940 if (error == EDEADLK) {
941 hammer_done_cursor(&cursor);
945 config->head.error = error;
946 hammer_done_cursor(&cursor);