]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/io.c
2 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
7 * published by the Free Software Foundation.
9 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 static int pop_f(int argc
, char **argv
);
33 static void pop_help(void);
34 static int push_f(int argc
, char **argv
);
35 static void push_help(void);
36 static int stack_f(int argc
, char **argv
);
37 static void stack_help(void);
38 static int forward_f(int argc
, char **argv
);
39 static void forward_help(void);
40 static int back_f(int argc
, char **argv
);
41 static void back_help(void);
42 static int ring_f(int argc
, char **argv
);
43 static void ring_help(void);
45 static const cmdinfo_t pop_cmd
=
46 { "pop", NULL
, pop_f
, 0, 0, 0, NULL
,
47 N_("pop location from the stack"), pop_help
};
48 static const cmdinfo_t push_cmd
=
49 { "push", NULL
, push_f
, 0, 2, 0, N_("[command]"),
50 N_("push location to the stack"), push_help
};
51 static const cmdinfo_t stack_cmd
=
52 { "stack", NULL
, stack_f
, 0, 0, 0, NULL
,
53 N_("view the location stack"), stack_help
};
54 static const cmdinfo_t forward_cmd
=
55 { "forward", "f", forward_f
, 0, 0, 0, NULL
,
56 N_("move forward to next entry in the position ring"), forward_help
};
57 static const cmdinfo_t back_cmd
=
58 { "back", "b", back_f
, 0, 0, 0, NULL
,
59 N_("move to the previous location in the position ring"), back_help
};
60 static const cmdinfo_t ring_cmd
=
61 { "ring", NULL
, ring_f
, 0, 1, 0, NULL
,
62 N_("show position ring or move to a specific entry"), ring_help
};
69 #define RING_ENTRIES 20
70 static iocur_t iocur_ring
[RING_ENTRIES
];
71 static int ring_head
= -1;
72 static int ring_tail
= -1;
73 static int ring_current
= -1;
78 add_command(&pop_cmd
);
79 add_command(&push_cmd
);
80 add_command(&stack_cmd
);
81 add_command(&forward_cmd
);
82 add_command(&back_cmd
);
83 add_command(&ring_cmd
);
91 if (iocur_top
== NULL
|| off
+ len
> BBTOB(iocur_top
->blen
))
92 dbprintf(_("can't set block offset to %d\n"), off
);
94 iocur_top
->boff
= off
;
95 iocur_top
->off
= ((xfs_off_t
)iocur_top
->bb
<< BBSHIFT
) + off
;
97 iocur_top
->data
= (void *)((char *)iocur_top
->buf
+ off
);
105 dbprintf(_("can't pop anything from I/O stack\n"));
109 libxfs_putbuf(iocur_top
->bp
);
110 iocur_top
->bp
= NULL
;
112 if (iocur_top
->bbmap
) {
113 free(iocur_top
->bbmap
);
114 iocur_top
->bbmap
= NULL
;
116 if (--iocur_sp
>= 0) {
117 iocur_top
= iocur_base
+ iocur_sp
;
118 cur_typ
= iocur_top
->typ
;
120 iocur_top
= iocur_base
;
140 " Changes the address and data type to the first entry on the stack.\n"
152 dbprintf("%s\n", tag
);
153 dbprintf(_("\tbyte offset %lld, length %d\n"), ioc
->off
, ioc
->len
);
154 dbprintf(_("\tbuffer block %lld (fsbno %lld), %d bb%s\n"), ioc
->bb
,
155 (xfs_fsblock_t
)XFS_DADDR_TO_FSB(mp
, ioc
->bb
), ioc
->blen
,
156 ioc
->blen
== 1 ? "" : "s");
158 dbprintf(_("\tblock map"));
159 for (i
= 0; i
< ioc
->bbmap
->nmaps
; i
++)
160 dbprintf(" %lld:%d", ioc
->bbmap
->b
[i
].bm_bn
,
161 ioc
->bbmap
->b
[i
].bm_len
);
164 dbprintf(_("\tinode %lld, dir inode %lld, type %s\n"), ioc
->ino
,
165 ioc
->dirino
, ioc
->typ
== NULL
? _("none") : ioc
->typ
->name
);
174 if (ring_current
== -1) {
175 dbprintf(_("no entries in location ring.\n"));
179 dbprintf(_(" type bblock bblen fsbno inode\n"));
183 ioc
= &iocur_ring
[i
];
184 if (i
== ring_current
)
189 dbprintf("%-7.7s %8lld %5d %8lld %9lld\n",
190 ioc
->typ
== NULL
? "none" : ioc
->typ
->name
,
193 (xfs_fsblock_t
)XFS_DADDR_TO_FSB(mp
, ioc
->bb
),
200 i
= (i
+(RING_ENTRIES
-1))%RING_ENTRIES
;
208 if (iocur_sp
+ 1 >= iocur_len
) {
209 iocur_base
= xrealloc(iocur_base
,
210 sizeof(*iocur_base
) * (iocur_len
+ 1));
214 iocur_top
= iocur_base
+ iocur_sp
;
215 memset(iocur_top
, 0, sizeof(*iocur_base
));
216 iocur_top
->ino
= iocur_sp
> 0 ? iocur_top
[-1].ino
: NULLFSINO
;
217 iocur_top
->dirino
= iocur_sp
> 0 ? iocur_top
[-1].dirino
: NULLFSINO
;
218 iocur_top
->mode
= iocur_sp
> 0 ? iocur_top
[-1].mode
: 0;
230 /* check we can execute command */
231 ct
= find_command(argv
[1]);
233 dbprintf(_("no such command %s\n"), argv
[1]);
237 dbprintf(_("no push form allowed for %s\n"), argv
[1]);
242 /* save current state */
244 if (iocur_top
[-1].typ
&& iocur_top
[-1].typ
->typnm
== TYP_INODE
)
245 set_cur_inode(iocur_top
[-1].ino
);
247 set_cur(iocur_top
[-1].typ
, iocur_top
[-1].bb
,
248 iocur_top
[-1].blen
, DB_RING_IGN
,
249 iocur_top
[-1].bbmap
);
251 /* run requested command */
253 (void)command(argc
-1, argv
+1);
262 " Allows you to push the current address and data type on the stack for\n"
263 " later return. 'push' also accepts an additional command to execute after\n"
264 " storing the current address (ex: 'push a rootino' from the superblock).\n"
269 /* move forward through the ring */
276 if (ring_current
== -1) {
277 dbprintf(_("ring is empty\n"));
280 if (ring_current
== ring_head
) {
281 dbprintf(_("no further entries\n"));
285 ring_current
= (ring_current
+1)%RING_ENTRIES
;
287 set_cur(iocur_ring
[ring_current
].typ
,
288 iocur_ring
[ring_current
].bb
,
289 iocur_ring
[ring_current
].blen
,
291 iocur_ring
[ring_current
].bbmap
);
301 " The 'forward' ('f') command moves to the next location in the position\n"
302 " ring, updating the current position and data type. If the current location\n"
303 " is the top entry in the ring, then the 'forward' command will have\n"
309 /* move backwards through the ring */
316 if (ring_current
== -1) {
317 dbprintf(_("ring is empty\n"));
320 if (ring_current
== ring_tail
) {
321 dbprintf(_("no previous entries\n"));
325 ring_current
= (ring_current
+(RING_ENTRIES
-1))%RING_ENTRIES
;
327 set_cur(iocur_ring
[ring_current
].typ
,
328 iocur_ring
[ring_current
].bb
,
329 iocur_ring
[ring_current
].blen
,
331 iocur_ring
[ring_current
].bbmap
);
341 " The 'back' ('b') command moves to the previous location in the position\n"
342 " ring, updating the current position and data type. If the current location\n"
343 " is the last entry in the ring, then the 'back' command will have no effect.\n"
348 /* show or go to specific point in ring */
361 index
= (int)strtoul(argv
[1], NULL
, 0);
362 if (index
< 0 || index
>= RING_ENTRIES
) {
363 dbprintf(_("invalid entry: %d\n"), index
);
367 ring_current
= index
;
369 set_cur(iocur_ring
[index
].typ
,
370 iocur_ring
[index
].bb
,
371 iocur_ring
[index
].blen
,
373 iocur_ring
[index
].bbmap
);
383 " The position ring automatically keeps track of each disk location and\n"
384 " structure type for each change of position you make during your xfs_db\n"
385 " session. The last %d most recent entries are kept in the ring.\n"
387 " To display the current list of ring entries type 'ring' by itself on\n"
388 " the command line. The entry highlighted by an asterisk ('*') is the\n"
391 " To move to another entry in the ring type 'ring <num>' where <num> is\n"
392 " your desired entry from the ring position list.\n"
394 " You may also use the 'forward' ('f') or 'back' ('b') commands to move\n"
395 " to the previous or next entry in the ring, respectively.\n"
397 " Note: Unlike the 'stack', 'push' and 'pop' commands, the ring tracks your\n"
398 " location implicitly. Use the 'push' and 'pop' commands if you wish to\n"
399 " store a specific location explicitly for later return.\n"
408 if (ring_head
== -1) {
409 /* only get here right after startup */
413 iocur_ring
[0] = *iocur_top
;
415 if (ring_current
== ring_head
) {
416 ring_head
= (ring_head
+1)%RING_ENTRIES
;
417 iocur_ring
[ring_head
] = *iocur_top
;
418 if (ring_head
== ring_tail
)
419 ring_tail
= (ring_tail
+1)%RING_ENTRIES
;
420 ring_current
= ring_head
;
422 ring_current
= (ring_current
+1)%RING_ENTRIES
;
423 iocur_ring
[ring_current
] = *iocur_top
;
433 ret
= -libxfs_writebufr(iocur_top
->bp
);
435 dbprintf(_("write error: %s\n"), strerror(ret
));
437 /* re-read buffer from disk */
438 ret
= -libxfs_readbufr(mp
->m_ddev_targp
, iocur_top
->bb
, iocur_top
->bp
,
441 dbprintf(_("read error: %s\n"), strerror(ret
));
449 ret
= -libxfs_writebufr(iocur_top
->bp
);
451 dbprintf(_("write error: %s\n"), strerror(ret
));
454 /* re-read buffer from disk */
455 ret
= -libxfs_readbufr_map(mp
->m_ddev_targp
, iocur_top
->bp
, 0);
457 dbprintf(_("read error: %s\n"), strerror(ret
));
468 xfs_verify_recalc_inode_crc(
471 ASSERT(iocur_top
->ino_buf
);
472 ASSERT(iocur_top
->bp
== bp
);
474 libxfs_dinode_calc_crc(mp
, iocur_top
->data
);
475 iocur_top
->ino_crc_ok
= 1;
479 xfs_verify_recalc_dquot_crc(
482 ASSERT((iocur_top
->dquot_buf
));
483 ASSERT(iocur_top
->bp
== bp
);
485 xfs_update_cksum(iocur_top
->data
, sizeof(struct xfs_dqblk
),
490 xfs_verify_recalc_crc(
493 xfs_buf_update_cksum(bp
, iocur_top
->typ
->crc_off
);
499 bool skip_crc
= false;
502 dbprintf(_("nothing to write\n"));
506 if (!xfs_sb_version_hascrc(&mp
->m_sb
) ||
507 !iocur_top
->bp
->b_ops
||
508 iocur_top
->bp
->b_ops
->verify_write
== xfs_dummy_verify
)
512 if (iocur_top
->ino_buf
) {
513 libxfs_dinode_calc_crc(mp
, iocur_top
->data
);
514 iocur_top
->ino_crc_ok
= 1;
515 } else if (iocur_top
->dquot_buf
) {
516 xfs_update_cksum(iocur_top
->data
,
517 sizeof(struct xfs_dqblk
),
521 if (iocur_top
->bbmap
)
526 /* If we didn't write the crc automatically, re-check inode validity */
527 if (xfs_sb_version_hascrc(&mp
->m_sb
) &&
528 skip_crc
&& iocur_top
->ino_buf
) {
529 iocur_top
->ino_crc_ok
= libxfs_verify_cksum(iocur_top
->data
,
530 mp
->m_sb
.sb_inodesize
,
548 const struct xfs_buf_ops
*ops
= t
? t
->bops
: NULL
;
551 dbprintf(_("set_cur no stack element to set\n"));
556 ino
= iocur_top
->ino
;
557 dirino
= iocur_top
->dirino
;
558 mode
= iocur_top
->mode
;
565 printf(_("xfs_db got a bbmap for %lld\n"), (long long)d
);
566 printf(_("\tblock map"));
567 for (i
= 0; i
< bbmap
->nmaps
; i
++)
568 printf(" %lld:%d", (long long)bbmap
->b
[i
].bm_bn
,
572 iocur_top
->bbmap
= malloc(sizeof(struct bbmap
));
573 if (!iocur_top
->bbmap
)
575 memcpy(iocur_top
->bbmap
, bbmap
, sizeof(struct bbmap
));
576 bp
= libxfs_readbuf_map(mp
->m_ddev_targp
, bbmap
->b
,
577 bbmap
->nmaps
, 0, ops
);
579 bp
= libxfs_readbuf(mp
->m_ddev_targp
, d
, c
, 0, ops
);
580 iocur_top
->bbmap
= NULL
;
584 * Keep the buffer even if the verifier says it is corrupted.
585 * We're a diagnostic tool, after all.
587 if (!bp
|| (bp
->b_error
&& bp
->b_error
!= -EFSCORRUPTED
&&
588 bp
->b_error
!= -EFSBADCRC
))
590 iocur_top
->buf
= bp
->b_addr
;
593 bp
->b_flags
|= LIBXFS_B_UNCHECKED
;
598 iocur_top
->data
= iocur_top
->buf
;
599 iocur_top
->len
= BBTOB(c
);
600 iocur_top
->off
= d
<< BBSHIFT
;
601 iocur_top
->typ
= cur_typ
= t
;
602 iocur_top
->ino
= ino
;
603 iocur_top
->dirino
= dirino
;
604 iocur_top
->mode
= mode
;
605 iocur_top
->ino_buf
= 0;
606 iocur_top
->dquot_buf
= 0;
608 /* store location in ring */
617 struct xfs_buf
*bp
= iocur_top
->bp
;
621 /* verify the buffer if the type has one. */
626 bp
->b_flags
|= LIBXFS_B_UNCHECKED
;
629 if (!(bp
->b_flags
& LIBXFS_B_UPTODATE
))
633 bp
->b_ops
->verify_read(bp
);
634 bp
->b_flags
&= ~LIBXFS_B_UNCHECKED
;
642 " The stack is used to explicitly store your location and data type\n"
643 " for later return. The 'push' operation stores the current address\n"
644 " and type on the stack, the 'pop' operation returns you to the\n"
645 " position and datatype of the top entry on the stack.\n"
647 " The 'stack' allows explicit location saves, see 'ring' for implicit\n"
648 " position tracking.\n"
662 for (i
= iocur_sp
; i
> 0; i
--) {
663 snprintf(tagbuf
, sizeof(tagbuf
), "%d: ", i
);
664 print_iocur(tagbuf
, &iocur_base
[i
]);