]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/crc.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2016 Red Hat, Inc.
22 static int crc_f(int argc
, char **argv
);
23 static void crc_help(void);
25 static const cmdinfo_t crc_cmd
=
26 { "crc", NULL
, crc_f
, 0, 1, 0, "[-i|-r|-v]",
27 N_("manipulate crc values for V5 filesystem structures"), crc_help
};
32 if (xfs_sb_version_hascrc(&mp
->m_sb
))
33 add_command(&crc_cmd
);
41 " 'crc' validates, invalidates, or recalculates the crc value for\n"
42 " the current on-disk metadata structures in Version 5 filesystems.\n"
44 " Usage: \"crc [-i|-r|-v]\"\n"
55 const struct xfs_buf_ops
*stashed_ops
= NULL
;
56 extern char *progname
;
57 const field_t
*fields
;
65 if (cur_typ
== NULL
) {
66 dbprintf(_("no current type\n"));
70 if (cur_typ
->fields
== NULL
) {
71 dbprintf(_("current type (%s) is not a structure\n"),
76 if (argc
) while ((c
= getopt(argc
, argv
, "irv")) != EOF
) {
88 dbprintf(_("bad option for crc command\n"));
94 if (invalidate
+ recalculate
+ validate
> 1) {
95 dbprintf(_("crc command accepts only one option\n"));
99 if ((invalidate
|| recalculate
) &&
100 ((x
.isreadonly
& LIBXFS_ISREADONLY
) || !expert_mode
)) {
101 dbprintf(_("%s not in expert mode, writing disabled\n"),
106 fields
= cur_typ
->fields
;
108 /* if we're a root field type, go down 1 layer to get field list */
109 if (fields
->name
[0] == '\0') {
110 fa
= &ftattrtab
[fields
->ftyp
];
111 ASSERT(fa
->ftyp
== fields
->ftyp
);
115 /* Search for a CRC field */
116 fl
= flist_find_ftyp(fields
, FLDT_CRC
);
118 dbprintf(_("No CRC field found for type %s\n"), cur_typ
->name
);
122 /* run down the field list and set offsets into the data */
123 if (!flist_parse(fields
, fl
, iocur_top
->data
, 0)) {
125 dbprintf(_("parsing error\n"));
130 struct xfs_buf_ops nowrite_ops
;
139 parentoffset
= sfl
->offset
;
142 ASSERT(sfl
->fld
->ftyp
== FLDT_CRC
);
144 bit_length
= fsize(sfl
->fld
, iocur_top
->data
, parentoffset
, 0);
145 bit_length
*= fcount(sfl
->fld
, iocur_top
->data
, parentoffset
);
146 crc
= getbitval(iocur_top
->data
, sfl
->offset
, bit_length
,
148 /* Off by one, ignore endianness - we're just corrupting it. */
150 setbitval(iocur_top
->data
, sfl
->offset
, bit_length
, &crc
);
152 /* Temporarily remove write verifier to write a bad CRC */
153 stashed_ops
= iocur_top
->bp
->b_ops
;
154 nowrite_ops
.verify_read
= stashed_ops
->verify_read
;
155 nowrite_ops
.verify_write
= xfs_dummy_verify
;
156 iocur_top
->bp
->b_ops
= &nowrite_ops
;
159 if (invalidate
|| recalculate
) {
161 dbprintf(_("Invalidating CRC:\n"));
163 dbprintf(_("Recalculating CRC:\n"));
167 iocur_top
->bp
->b_ops
= stashed_ops
;
168 /* re-verify to get proper b_error state */
169 iocur_top
->bp
->b_ops
->verify_read(iocur_top
->bp
);
171 dbprintf(_("Verifying CRC:\n"));
173 /* And show us what we've got! */