]>
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
);
81 if (iocur_top
== NULL
|| off
+ len
> BBTOB(iocur_top
->blen
))
82 dbprintf(_("can't set block offset to %d\n"), off
);
84 iocur_top
->boff
= off
;
85 iocur_top
->off
= ((xfs_off_t
)iocur_top
->bb
<< BBSHIFT
) + off
;
87 iocur_top
->data
= (void *)((char *)iocur_top
->buf
+ off
);
95 dbprintf(_("can't pop anything from I/O stack\n"));
99 libxfs_putbuf(iocur_top
->bp
);
100 iocur_top
->bp
= NULL
;
102 if (iocur_top
->bbmap
) {
103 free(iocur_top
->bbmap
);
104 iocur_top
->bbmap
= NULL
;
106 if (--iocur_sp
>= 0) {
107 iocur_top
= iocur_base
+ iocur_sp
;
108 cur_typ
= iocur_top
->typ
;
110 iocur_top
= iocur_base
;
130 " Changes the address and data type to the first entry on the stack.\n"
142 dbprintf("%s\n", tag
);
143 dbprintf(_("\tbyte offset %lld, length %d\n"), ioc
->off
, ioc
->len
);
144 dbprintf(_("\tbuffer block %lld (fsbno %lld), %d bb%s\n"), ioc
->bb
,
145 (xfs_fsblock_t
)XFS_DADDR_TO_FSB(mp
, ioc
->bb
), ioc
->blen
,
146 ioc
->blen
== 1 ? "" : "s");
148 dbprintf(_("\tblock map"));
149 for (i
= 0; i
< ioc
->bbmap
->nmaps
; i
++)
150 dbprintf(" %lld:%d", ioc
->bbmap
->b
[i
].bm_bn
,
151 ioc
->bbmap
->b
[i
].bm_len
);
154 dbprintf(_("\tinode %lld, dir inode %lld, type %s\n"), ioc
->ino
,
155 ioc
->dirino
, ioc
->typ
== NULL
? _("none") : ioc
->typ
->name
);
164 if (ring_current
== -1) {
165 dbprintf(_("no entries in location ring.\n"));
169 dbprintf(_(" type bblock bblen fsbno inode\n"));
173 ioc
= &iocur_ring
[i
];
174 if (i
== ring_current
)
179 dbprintf("%-7.7s %8lld %5d %8lld %9lld\n",
180 ioc
->typ
== NULL
? "none" : ioc
->typ
->name
,
183 (xfs_fsblock_t
)XFS_DADDR_TO_FSB(mp
, ioc
->bb
),
190 i
= (i
+(RING_ENTRIES
-1))%RING_ENTRIES
;
198 if (iocur_sp
+ 1 >= iocur_len
) {
199 iocur_base
= xrealloc(iocur_base
,
200 sizeof(*iocur_base
) * (iocur_len
+ 1));
204 iocur_top
= iocur_base
+ iocur_sp
;
205 memset(iocur_top
, 0, sizeof(*iocur_base
));
206 iocur_top
->ino
= iocur_sp
> 0 ? iocur_top
[-1].ino
: NULLFSINO
;
207 iocur_top
->dirino
= iocur_sp
> 0 ? iocur_top
[-1].dirino
: NULLFSINO
;
208 iocur_top
->mode
= iocur_sp
> 0 ? iocur_top
[-1].mode
: 0;
213 push_cur_and_set_type(void)
215 /* save current state */
217 if (iocur_top
[-1].typ
&& iocur_top
[-1].typ
->typnm
== TYP_INODE
)
218 set_cur_inode(iocur_top
[-1].ino
);
220 set_cur(iocur_top
[-1].typ
, iocur_top
[-1].bb
,
221 iocur_top
[-1].blen
, DB_RING_IGN
,
222 iocur_top
[-1].bbmap
);
233 /* check we can execute command */
234 ct
= find_command(argv
[1]);
236 dbprintf(_("no such command %s\n"), argv
[1]);
240 dbprintf(_("no push form allowed for %s\n"), argv
[1]);
245 push_cur_and_set_type();
247 /* run requested command */
249 (void)command(argc
-1, argv
+1);
258 " Allows you to push the current address and data type on the stack for\n"
259 " later return. 'push' also accepts an additional command to execute after\n"
260 " storing the current address (ex: 'push a rootino' from the superblock).\n"
265 /* move forward through the ring */
272 if (ring_current
== -1) {
273 dbprintf(_("ring is empty\n"));
276 if (ring_current
== ring_head
) {
277 dbprintf(_("no further entries\n"));
281 ring_current
= (ring_current
+1)%RING_ENTRIES
;
283 set_cur(iocur_ring
[ring_current
].typ
,
284 iocur_ring
[ring_current
].bb
,
285 iocur_ring
[ring_current
].blen
,
287 iocur_ring
[ring_current
].bbmap
);
297 " The 'forward' ('f') command moves to the next location in the position\n"
298 " ring, updating the current position and data type. If the current location\n"
299 " is the top entry in the ring, then the 'forward' command will have\n"
305 /* move backwards through the ring */
312 if (ring_current
== -1) {
313 dbprintf(_("ring is empty\n"));
316 if (ring_current
== ring_tail
) {
317 dbprintf(_("no previous entries\n"));
321 ring_current
= (ring_current
+(RING_ENTRIES
-1))%RING_ENTRIES
;
323 set_cur(iocur_ring
[ring_current
].typ
,
324 iocur_ring
[ring_current
].bb
,
325 iocur_ring
[ring_current
].blen
,
327 iocur_ring
[ring_current
].bbmap
);
337 " The 'back' ('b') command moves to the previous location in the position\n"
338 " ring, updating the current position and data type. If the current location\n"
339 " is the last entry in the ring, then the 'back' command will have no effect.\n"
344 /* show or go to specific point in ring */
357 index
= (int)strtoul(argv
[1], NULL
, 0);
358 if (index
< 0 || index
>= RING_ENTRIES
) {
359 dbprintf(_("invalid entry: %d\n"), index
);
363 ring_current
= index
;
365 set_cur(iocur_ring
[index
].typ
,
366 iocur_ring
[index
].bb
,
367 iocur_ring
[index
].blen
,
369 iocur_ring
[index
].bbmap
);
379 " The position ring automatically keeps track of each disk location and\n"
380 " structure type for each change of position you make during your xfs_db\n"
381 " session. The last %d most recent entries are kept in the ring.\n"
383 " To display the current list of ring entries type 'ring' by itself on\n"
384 " the command line. The entry highlighted by an asterisk ('*') is the\n"
387 " To move to another entry in the ring type 'ring <num>' where <num> is\n"
388 " your desired entry from the ring position list.\n"
390 " You may also use the 'forward' ('f') or 'back' ('b') commands to move\n"
391 " to the previous or next entry in the ring, respectively.\n"
393 " Note: Unlike the 'stack', 'push' and 'pop' commands, the ring tracks your\n"
394 " location implicitly. Use the 'push' and 'pop' commands if you wish to\n"
395 " store a specific location explicitly for later return.\n"
404 if (ring_head
== -1) {
405 /* only get here right after startup */
409 iocur_ring
[0] = *iocur_top
;
411 if (ring_current
== ring_head
) {
412 ring_head
= (ring_head
+1)%RING_ENTRIES
;
413 iocur_ring
[ring_head
] = *iocur_top
;
414 if (ring_head
== ring_tail
)
415 ring_tail
= (ring_tail
+1)%RING_ENTRIES
;
416 ring_current
= ring_head
;
418 ring_current
= (ring_current
+1)%RING_ENTRIES
;
419 iocur_ring
[ring_current
] = *iocur_top
;
429 ret
= -libxfs_writebufr(iocur_top
->bp
);
431 dbprintf(_("write error: %s\n"), strerror(ret
));
433 /* re-read buffer from disk */
434 ret
= -libxfs_readbufr(mp
->m_ddev_targp
, iocur_top
->bb
, iocur_top
->bp
,
437 dbprintf(_("read error: %s\n"), strerror(ret
));
445 ret
= -libxfs_writebufr(iocur_top
->bp
);
447 dbprintf(_("write error: %s\n"), strerror(ret
));
450 /* re-read buffer from disk */
451 ret
= -libxfs_readbufr_map(mp
->m_ddev_targp
, iocur_top
->bp
, 0);
453 dbprintf(_("read error: %s\n"), strerror(ret
));
464 xfs_verify_recalc_crc(
467 xfs_buf_update_cksum(bp
, iocur_top
->typ
->crc_off
);
473 bool skip_crc
= false;
476 dbprintf(_("nothing to write\n"));
480 if (!xfs_sb_version_hascrc(&mp
->m_sb
) ||
481 !iocur_top
->bp
->b_ops
||
482 iocur_top
->bp
->b_ops
->verify_write
== xfs_dummy_verify
)
486 if (iocur_top
->ino_buf
)
487 xfs_inode_set_crc(iocur_top
->bp
);
488 else if (iocur_top
->dquot_buf
)
489 xfs_dquot_set_crc(iocur_top
->bp
);
491 if (iocur_top
->bbmap
)
496 /* If we didn't write the crc automatically, re-check inode validity */
497 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
498 skip_crc
&& iocur_top
->ino_buf
) {
499 iocur_top
->ino_crc_ok
= libxfs_verify_cksum(iocur_top
->data
,
500 mp
->m_sb
.sb_inodesize
,
518 const struct xfs_buf_ops
*ops
= type
? type
->bops
: NULL
;
521 dbprintf(_("set_cur no stack element to set\n"));
525 ino
= iocur_top
->ino
;
526 dirino
= iocur_top
->dirino
;
527 mode
= iocur_top
->mode
;
534 printf(_("xfs_db got a bbmap for %lld\n"), (long long)blknum
);
535 printf(_("\tblock map"));
536 for (i
= 0; i
< bbmap
->nmaps
; i
++)
537 printf(" %lld:%d", (long long)bbmap
->b
[i
].bm_bn
,
541 iocur_top
->bbmap
= malloc(sizeof(struct bbmap
));
542 if (!iocur_top
->bbmap
)
544 memcpy(iocur_top
->bbmap
, bbmap
, sizeof(struct bbmap
));
545 bp
= libxfs_readbuf_map(mp
->m_ddev_targp
, bbmap
->b
,
546 bbmap
->nmaps
, 0, ops
);
548 bp
= libxfs_readbuf(mp
->m_ddev_targp
, blknum
, len
, 0, ops
);
549 iocur_top
->bbmap
= NULL
;
553 * Keep the buffer even if the verifier says it is corrupted.
554 * We're a diagnostic tool, after all.
556 if (!bp
|| (bp
->b_error
&& bp
->b_error
!= -EFSCORRUPTED
&&
557 bp
->b_error
!= -EFSBADCRC
))
559 iocur_top
->buf
= bp
->b_addr
;
562 bp
->b_flags
|= LIBXFS_B_UNCHECKED
;
564 iocur_top
->bb
= blknum
;
565 iocur_top
->blen
= len
;
567 iocur_top
->data
= iocur_top
->buf
;
568 iocur_top
->len
= BBTOB(len
);
569 iocur_top
->off
= blknum
<< BBSHIFT
;
570 iocur_top
->typ
= cur_typ
= type
;
571 iocur_top
->ino
= ino
;
572 iocur_top
->dirino
= dirino
;
573 iocur_top
->mode
= mode
;
574 iocur_top
->ino_buf
= 0;
575 iocur_top
->dquot_buf
= 0;
577 /* store location in ring */
586 struct xfs_buf
*bp
= iocur_top
->bp
;
588 /* Inodes are special; verifier checks all inodes in the chunk */
589 if (type
->typnm
== TYP_INODE
) {
590 xfs_daddr_t b
= iocur_top
->bb
;
594 * Note that this will back up to the beginning of the inode
595 * which contains the current disk location; daddr may change.
597 ino
= XFS_AGINO_TO_INO(mp
, xfs_daddr_to_agno(mp
, b
),
598 ((b
<< BBSHIFT
) >> mp
->m_sb
.sb_inodelog
) %
599 (mp
->m_sb
.sb_agblocks
<< mp
->m_sb
.sb_inopblog
));
604 /* adjust buffer size for types with fields & hence fsize() */
606 int bb_count
; /* type's size in basic blocks */
608 bb_count
= BTOBB(byteize(fsize(type
->fields
,
609 iocur_top
->data
, 0, 0)));
610 set_cur(type
, iocur_top
->bb
, bb_count
, DB_RING_IGN
, NULL
);
612 iocur_top
->typ
= type
;
614 /* verify the buffer if the type has one. */
619 bp
->b_flags
|= LIBXFS_B_UNCHECKED
;
622 if (!(bp
->b_flags
& LIBXFS_B_UPTODATE
))
625 bp
->b_ops
= type
->bops
;
626 bp
->b_ops
->verify_read(bp
);
627 bp
->b_flags
&= ~LIBXFS_B_UNCHECKED
;
635 " The stack is used to explicitly store your location and data type\n"
636 " for later return. The 'push' operation stores the current address\n"
637 " and type on the stack, the 'pop' operation returns you to the\n"
638 " position and datatype of the top entry on the stack.\n"
640 " The 'stack' allows explicit location saves, see 'ring' for implicit\n"
641 " position tracking.\n"
655 for (i
= iocur_sp
; i
> 0; i
--) {
656 snprintf(tagbuf
, sizeof(tagbuf
), "%d: ", i
);
657 print_iocur(tagbuf
, &iocur_base
[i
]);