]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/io.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
22 static int pop_f(int argc
, char **argv
);
23 static void pop_help(void);
24 static int push_f(int argc
, char **argv
);
25 static void push_help(void);
26 static int stack_f(int argc
, char **argv
);
27 static void stack_help(void);
28 static int forward_f(int argc
, char **argv
);
29 static void forward_help(void);
30 static int back_f(int argc
, char **argv
);
31 static void back_help(void);
32 static int ring_f(int argc
, char **argv
);
33 static void ring_help(void);
35 static const cmdinfo_t pop_cmd
=
36 { "pop", NULL
, pop_f
, 0, 0, 0, NULL
,
37 N_("pop location from the stack"), pop_help
};
38 static const cmdinfo_t push_cmd
=
39 { "push", NULL
, push_f
, 0, 2, 0, N_("[command]"),
40 N_("push location to the stack"), push_help
};
41 static const cmdinfo_t stack_cmd
=
42 { "stack", NULL
, stack_f
, 0, 0, 0, NULL
,
43 N_("view the location stack"), stack_help
};
44 static const cmdinfo_t forward_cmd
=
45 { "forward", "f", forward_f
, 0, 0, 0, NULL
,
46 N_("move forward to next entry in the position ring"), forward_help
};
47 static const cmdinfo_t back_cmd
=
48 { "back", "b", back_f
, 0, 0, 0, NULL
,
49 N_("move to the previous location in the position ring"), back_help
};
50 static const cmdinfo_t ring_cmd
=
51 { "ring", NULL
, ring_f
, 0, 1, 0, NULL
,
52 N_("show position ring or move to a specific entry"), ring_help
};
59 #define RING_ENTRIES 20
60 static iocur_t iocur_ring
[RING_ENTRIES
];
61 static int ring_head
= -1;
62 static int ring_tail
= -1;
63 static int ring_current
= -1;
68 add_command(&pop_cmd
);
69 add_command(&push_cmd
);
70 add_command(&stack_cmd
);
71 add_command(&forward_cmd
);
72 add_command(&back_cmd
);
73 add_command(&ring_cmd
);
76 static inline void set_cur_boff(int off
)
78 iocur_top
->boff
= off
;
79 iocur_top
->off
= ((xfs_off_t
)iocur_top
->bb
<< BBSHIFT
) + off
;
80 iocur_top
->data
= (void *)((char *)iocur_top
->buf
+ off
);
88 if (iocur_top
== NULL
|| off
+ len
> BBTOB(iocur_top
->blen
))
89 dbprintf(_("can't set block offset to %d\n"), off
);
100 dbprintf(_("can't pop anything from I/O stack\n"));
104 libxfs_buf_relse(iocur_top
->bp
);
105 iocur_top
->bp
= NULL
;
107 if (iocur_top
->bbmap
) {
108 free(iocur_top
->bbmap
);
109 iocur_top
->bbmap
= NULL
;
111 if (--iocur_sp
>= 0) {
112 iocur_top
= iocur_base
+ iocur_sp
;
113 cur_typ
= iocur_top
->typ
;
115 iocur_top
= iocur_base
;
135 " Changes the address and data type to the first entry on the stack.\n"
141 iocur_is_ddev(const struct iocur
*ioc
)
146 return ioc
->bp
->b_target
== ioc
->bp
->b_mount
->m_ddev_targp
;
150 iocur_is_extlogdev(const struct iocur
*ioc
)
152 struct xfs_buf
*bp
= ioc
->bp
;
156 if (bp
->b_mount
->m_logdev_targp
== bp
->b_mount
->m_ddev_targp
)
159 return bp
->b_target
== bp
->b_mount
->m_logdev_targp
;
167 const char *block_unit
= "fsbno?";
170 if (iocur_is_ddev(ioc
))
171 block_unit
= "fsbno";
172 else if (iocur_is_extlogdev(ioc
))
173 block_unit
= "logbno";
175 dbprintf("%s\n", tag
);
176 dbprintf(_("\tbyte offset %lld, length %d\n"), ioc
->off
, ioc
->len
);
177 dbprintf(_("\tbuffer block %lld (%s %lld), %d bb%s\n"), ioc
->bb
,
179 (xfs_fsblock_t
)XFS_DADDR_TO_FSB(mp
, ioc
->bb
),
180 ioc
->blen
, ioc
->blen
== 1 ? "" : "s");
182 dbprintf(_("\tblock map"));
183 for (i
= 0; i
< ioc
->bbmap
->nmaps
; i
++)
184 dbprintf(" %lld:%d", ioc
->bbmap
->b
[i
].bm_bn
,
185 ioc
->bbmap
->b
[i
].bm_len
);
188 dbprintf(_("\tinode %lld, dir inode %lld, type %s\n"), ioc
->ino
,
189 ioc
->dirino
, ioc
->typ
== NULL
? _("none") : ioc
->typ
->name
);
198 if (ring_current
== -1) {
199 dbprintf(_("no entries in location ring.\n"));
203 dbprintf(_(" type bblock bblen fsbno inode\n"));
207 ioc
= &iocur_ring
[i
];
208 if (i
== ring_current
)
213 dbprintf("%-7.7s %8lld %5d %8lld %9lld\n",
214 ioc
->typ
== NULL
? "none" : ioc
->typ
->name
,
217 (xfs_fsblock_t
)XFS_DADDR_TO_FSB(mp
, ioc
->bb
),
224 i
= (i
+(RING_ENTRIES
-1))%RING_ENTRIES
;
232 if (iocur_sp
+ 1 >= iocur_len
) {
233 iocur_base
= xrealloc(iocur_base
,
234 sizeof(*iocur_base
) * (iocur_len
+ 1));
238 iocur_top
= iocur_base
+ iocur_sp
;
239 memset(iocur_top
, 0, sizeof(*iocur_base
));
240 iocur_top
->ino
= iocur_sp
> 0 ? iocur_top
[-1].ino
: NULLFSINO
;
241 iocur_top
->dirino
= iocur_sp
> 0 ? iocur_top
[-1].dirino
: NULLFSINO
;
242 iocur_top
->mode
= iocur_sp
> 0 ? iocur_top
[-1].mode
: 0;
247 push_cur_and_set_type(void)
249 /* save current state */
251 if (iocur_top
[-1].typ
&& iocur_top
[-1].typ
->typnm
== TYP_INODE
)
252 set_cur_inode(iocur_top
[-1].ino
);
254 set_cur(iocur_top
[-1].typ
, iocur_top
[-1].bb
,
255 iocur_top
[-1].blen
, DB_RING_IGN
,
256 iocur_top
[-1].bbmap
);
267 /* check we can execute command */
268 ct
= find_command(argv
[1]);
270 dbprintf(_("no such command %s\n"), argv
[1]);
274 dbprintf(_("no push form allowed for %s\n"), argv
[1]);
279 push_cur_and_set_type();
281 /* run requested command */
283 (void)command(argc
-1, argv
+1);
292 " Allows you to push the current address and data type on the stack for\n"
293 " later return. 'push' also accepts an additional command to execute after\n"
294 " storing the current address (ex: 'push a rootino' from the superblock).\n"
299 /* move forward through the ring */
306 if (ring_current
== -1) {
307 dbprintf(_("ring is empty\n"));
310 if (ring_current
== ring_head
) {
311 dbprintf(_("no further entries\n"));
315 ring_current
= (ring_current
+1)%RING_ENTRIES
;
317 set_cur(iocur_ring
[ring_current
].typ
,
318 iocur_ring
[ring_current
].bb
,
319 iocur_ring
[ring_current
].blen
,
321 iocur_ring
[ring_current
].bbmap
);
331 " The 'forward' ('f') command moves to the next location in the position\n"
332 " ring, updating the current position and data type. If the current location\n"
333 " is the top entry in the ring, then the 'forward' command will have\n"
339 /* move backwards through the ring */
346 if (ring_current
== -1) {
347 dbprintf(_("ring is empty\n"));
350 if (ring_current
== ring_tail
) {
351 dbprintf(_("no previous entries\n"));
355 ring_current
= (ring_current
+(RING_ENTRIES
-1))%RING_ENTRIES
;
357 set_cur(iocur_ring
[ring_current
].typ
,
358 iocur_ring
[ring_current
].bb
,
359 iocur_ring
[ring_current
].blen
,
361 iocur_ring
[ring_current
].bbmap
);
371 " The 'back' ('b') command moves to the previous location in the position\n"
372 " ring, updating the current position and data type. If the current location\n"
373 " is the last entry in the ring, then the 'back' command will have no effect.\n"
378 /* show or go to specific point in ring */
391 index
= (int)strtoul(argv
[1], NULL
, 0);
392 if (index
< 0 || index
>= RING_ENTRIES
) {
393 dbprintf(_("invalid entry: %d\n"), index
);
397 ring_current
= index
;
399 set_cur(iocur_ring
[index
].typ
,
400 iocur_ring
[index
].bb
,
401 iocur_ring
[index
].blen
,
403 iocur_ring
[index
].bbmap
);
413 " The position ring automatically keeps track of each disk location and\n"
414 " structure type for each change of position you make during your xfs_db\n"
415 " session. The last %d most recent entries are kept in the ring.\n"
417 " To display the current list of ring entries type 'ring' by itself on\n"
418 " the command line. The entry highlighted by an asterisk ('*') is the\n"
421 " To move to another entry in the ring type 'ring <num>' where <num> is\n"
422 " your desired entry from the ring position list.\n"
424 " You may also use the 'forward' ('f') or 'back' ('b') commands to move\n"
425 " to the previous or next entry in the ring, respectively.\n"
427 " Note: Unlike the 'stack', 'push' and 'pop' commands, the ring tracks your\n"
428 " location implicitly. Use the 'push' and 'pop' commands if you wish to\n"
429 " store a specific location explicitly for later return.\n"
438 if (ring_head
== -1) {
439 /* only get here right after startup */
443 iocur_ring
[0] = *iocur_top
;
445 if (ring_current
== ring_head
) {
446 ring_head
= (ring_head
+1)%RING_ENTRIES
;
447 iocur_ring
[ring_head
] = *iocur_top
;
448 if (ring_head
== ring_tail
)
449 ring_tail
= (ring_tail
+1)%RING_ENTRIES
;
450 ring_current
= ring_head
;
452 ring_current
= (ring_current
+1)%RING_ENTRIES
;
453 iocur_ring
[ring_current
] = *iocur_top
;
463 ret
= -libxfs_bwrite(iocur_top
->bp
);
465 dbprintf(_("write error: %s\n"), strerror(ret
));
467 /* re-read buffer from disk */
468 ret
= -libxfs_readbufr(mp
->m_ddev_targp
, iocur_top
->bb
, iocur_top
->bp
,
471 dbprintf(_("read error: %s\n"), strerror(ret
));
479 ret
= -libxfs_bwrite(iocur_top
->bp
);
481 dbprintf(_("write error: %s\n"), strerror(ret
));
484 /* re-read buffer from disk */
485 ret
= -libxfs_readbufr_map(mp
->m_ddev_targp
, iocur_top
->bp
, 0);
487 dbprintf(_("read error: %s\n"), strerror(ret
));
498 xfs_verify_recalc_crc(
501 xfs_buf_update_cksum(bp
, iocur_top
->typ
->crc_off
);
507 bool skip_crc
= false;
510 dbprintf(_("nothing to write\n"));
514 if (!xfs_has_crc(mp
) ||
515 !iocur_top
->bp
->b_ops
||
516 iocur_top
->bp
->b_ops
->verify_write
== xfs_dummy_verify
)
520 if (iocur_top
->ino_buf
)
521 xfs_inode_set_crc(iocur_top
->bp
);
522 else if (iocur_top
->dquot_buf
)
523 xfs_dquot_set_crc(iocur_top
->bp
);
525 if (iocur_top
->bbmap
)
530 /* If we didn't write the crc automatically, re-check inode validity */
531 if (xfs_has_crc(mp
) &&
532 skip_crc
&& iocur_top
->ino_buf
) {
533 iocur_top
->ino_crc_ok
= libxfs_verify_cksum(iocur_top
->data
,
534 mp
->m_sb
.sb_inodesize
,
542 struct xfs_buftarg
*btargp
,
553 const struct xfs_buf_ops
*ops
= type
? type
->bops
: NULL
;
557 dbprintf(_("set_cur no stack element to set\n"));
561 ino
= iocur_top
->ino
;
562 dirino
= iocur_top
->dirino
;
563 mode
= iocur_top
->mode
;
570 printf(_("xfs_db got a bbmap for %lld\n"), (long long)blknum
);
571 printf(_("\tblock map"));
572 for (i
= 0; i
< bbmap
->nmaps
; i
++)
573 printf(" %lld:%d", (long long)bbmap
->b
[i
].bm_bn
,
577 iocur_top
->bbmap
= malloc(sizeof(struct bbmap
));
578 if (!iocur_top
->bbmap
)
580 memcpy(iocur_top
->bbmap
, bbmap
, sizeof(struct bbmap
));
581 error
= -libxfs_buf_read_map(btargp
, bbmap
->b
,
582 bbmap
->nmaps
, LIBXFS_READBUF_SALVAGE
, &bp
,
585 error
= -libxfs_buf_read(btargp
, blknum
, len
,
586 LIBXFS_READBUF_SALVAGE
, &bp
, ops
);
587 iocur_top
->bbmap
= NULL
;
591 * Salvage mode means that we still get a buffer even if the verifier
592 * says the metadata is corrupt. Therefore, the only errors we should
593 * get are for IO errors or runtime errors.
597 iocur_top
->buf
= bp
->b_addr
;
601 bp
->b_flags
|= LIBXFS_B_UNCHECKED
;
604 iocur_top
->bb
= blknum
;
605 iocur_top
->blen
= len
;
607 iocur_top
->data
= iocur_top
->buf
;
608 iocur_top
->len
= BBTOB(len
);
609 iocur_top
->off
= blknum
<< BBSHIFT
;
610 iocur_top
->typ
= cur_typ
= type
;
611 iocur_top
->ino
= ino
;
612 iocur_top
->dirino
= dirino
;
613 iocur_top
->mode
= mode
;
614 iocur_top
->ino_buf
= 0;
615 iocur_top
->dquot_buf
= 0;
617 /* store location in ring */
630 __set_cur(mp
->m_ddev_targp
, type
, blknum
, len
, ring_flag
, bbmap
);
641 if (mp
->m_logdev_targp
->bt_bdev
== mp
->m_ddev_targp
->bt_bdev
) {
642 fprintf(stderr
, "no external log specified\n");
647 __set_cur(mp
->m_logdev_targp
, type
, blknum
, len
, ring_flag
, bbmap
);
655 int bb_count
= 1; /* type's size in basic blocks */
656 int boff
= iocur_top
->boff
;
659 * Inodes are special; verifier checks all inodes in the chunk, the
660 * set_cur_inode() will help that
662 if (type
->typnm
== TYP_INODE
) {
663 xfs_daddr_t b
= iocur_top
->bb
;
668 agbno
= xfs_daddr_to_agbno(mp
, b
);
669 agino
= XFS_OFFBNO_TO_AGINO(mp
, agbno
,
670 iocur_top
->boff
/ mp
->m_sb
.sb_inodesize
);
671 ino
= XFS_AGINO_TO_INO(mp
, xfs_daddr_to_agno(mp
, b
), agino
);
676 /* adjust buffer size for types with fields & hence fsize() */
678 bb_count
= BTOBB(byteize(fsize(type
->fields
,
679 iocur_top
->data
, 0, 0)));
680 set_cur(type
, iocur_top
->bb
, bb_count
, DB_RING_IGN
, NULL
);
689 " The stack is used to explicitly store your location and data type\n"
690 " for later return. The 'push' operation stores the current address\n"
691 " and type on the stack, the 'pop' operation returns you to the\n"
692 " position and datatype of the top entry on the stack.\n"
694 " The 'stack' allows explicit location saves, see 'ring' for implicit\n"
695 " position tracking.\n"
709 for (i
= iocur_sp
; i
> 0; i
--) {
710 snprintf(tagbuf
, sizeof(tagbuf
), "%d: ", i
);
711 print_iocur(tagbuf
, &iocur_base
[i
]);