]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/write.c
1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (c) 2000-2002,2005 Silicon Graphics, Inc.
25 static int write_f(int argc
, char **argv
);
26 static void write_help(void);
28 static const cmdinfo_t write_cmd
=
29 { "write", NULL
, write_f
, 0, -1, 0, N_("[-c|-d] [field or value]..."),
30 N_("write value to disk"), write_help
};
38 add_command(&write_cmd
);
47 " The 'write' command takes on different personalities depending on the\n"
48 " type of object being worked with.\n\n"
49 " Write has 3 modes:\n"
50 " 'struct mode' - is active anytime you're looking at a filesystem object\n"
51 " which contains individual fields (ex: an inode).\n"
52 " 'data mode' - is active anytime you set a disk address directly or set\n"
53 " the type to 'data'.\n"
54 " 'string mode' - only used for writing symlink blocks.\n"
57 " Struct mode: 'write core.uid 23' - set an inode uid field to 23.\n"
58 " 'write fname \"hello\\000\"' - write superblock fname.\n"
59 " (note: in struct mode strings are not null terminated)\n"
60 " 'write fname #6669736800' - write superblock fname with hex.\n"
61 " 'write uuid 00112233-4455-6677-8899-aabbccddeeff'\n"
62 " - write superblock uuid.\n"
63 " Data mode: 'write fill 0xff' - fill the entire block with 0xff's\n"
64 " 'write lshift 3' - shift the block 3 bytes to the left\n"
65 " 'write sequence 1 5' - write a cycle of number [1-5] through\n"
66 " the entire block.\n"
67 " String mode: 'write \"This_is_a_filename\" - write null terminated string.\n"
69 " In data mode type 'write' by itself for a list of specific commands.\n\n"
70 " Specifying the -c option will allow writes of invalid (corrupt) data with\n"
71 " an invalid CRC. Specifying the -d option will allow writes of invalid data,\n"
72 " but still recalculate the CRC so we are forced to check and detect the\n"
73 " invalid data appropriately.\n\n"
84 extern char *progname
;
86 bool corrupt
= false; /* Allow write of bad data w/ invalid CRC */
87 bool invalid_data
= false; /* Allow write of bad data w/ valid CRC */
88 struct xfs_buf_ops local_ops
;
89 const struct xfs_buf_ops
*stashed_ops
= NULL
;
91 if (x
.flags
& LIBXFS_ISREADONLY
) {
92 dbprintf(_("%s started in read only mode, writing disabled\n"),
97 if (cur_typ
== NULL
) {
98 dbprintf(_("no current type\n"));
104 dbprintf(_("no handler function for type %s, write unsupported.\n"),
109 while ((c
= getopt(argc
, argv
, "cd")) != EOF
) {
118 dbprintf(_("bad option for write command\n"));
123 if (corrupt
&& invalid_data
) {
124 dbprintf(_("Cannot specify both -c and -d options\n"));
129 iocur_top
->typ
->crc_off
== TYP_F_NO_CRC_OFF
&&
131 dbprintf(_("Cannot recalculate CRCs on this type of object\n"));
139 * If the buffer has no verifier or we are using standard verifier
140 * paths, then just write it out and return
142 if (!iocur_top
->bp
->b_ops
||
143 !(corrupt
|| invalid_data
)) {
144 (*pf
)(DB_WRITE
, cur_typ
->fields
, argc
, argv
);
149 /* Temporarily remove write verifier to write bad data */
150 stashed_ops
= iocur_top
->bp
->b_ops
;
151 local_ops
.verify_read
= stashed_ops
->verify_read
;
152 iocur_top
->bp
->b_ops
= &local_ops
;
154 if (!xfs_has_crc(mp
)) {
155 local_ops
.verify_write
= xfs_dummy_verify
;
156 } else if (corrupt
) {
157 local_ops
.verify_write
= xfs_dummy_verify
;
158 dbprintf(_("Allowing write of corrupted data and bad CRC\n"));
159 } else if (iocur_top
->typ
->crc_off
== TYP_F_CRC_FUNC
) {
160 local_ops
.verify_write
= iocur_top
->typ
->set_crc
;
161 dbprintf(_("Allowing write of corrupted data with good CRC\n"));
162 } else { /* invalid data */
163 local_ops
.verify_write
= xfs_verify_recalc_crc
;
164 dbprintf(_("Allowing write of corrupted data with good CRC\n"));
167 (*pf
)(DB_WRITE
, cur_typ
->fields
, argc
, argv
);
169 iocur_top
->bp
->b_ops
= stashed_ops
;
174 /* compare significant portions of commands */
187 for (sigcnt
= 0; *s1
== *s2
; s1
++, s2
++) {
195 if (sig
&& (sigcnt
>= sig
))
217 len
= iocur_top
->len
- start
;
219 if (len
+start
> iocur_top
->len
) {
220 dbprintf(_("length (%d) too large for data block size (%d)"),
221 len
, iocur_top
->len
);
224 base
= (char *)iocur_top
->data
+ start
;
226 memcpy(base
, base
+shift
, len
-shift
);
227 memset(base
+(len
-shift
), 0, shift
);
246 len
= iocur_top
->len
- start
;
248 if (len
+start
> iocur_top
->len
) {
249 dbprintf(_("length (%d) too large for data block size (%d)"),
250 len
, iocur_top
->len
);
253 base
= (char *)iocur_top
->data
+ start
;
255 memcpy(base
+shift
, base
, len
-shift
);
256 memset(base
, 0, shift
);
276 len
= iocur_top
->len
- start
;
278 if (len
+start
> iocur_top
->len
) {
279 dbprintf(_("length (%d) too large for data block size (%d)"),
280 len
, iocur_top
->len
);
283 base
= (char *)iocur_top
->data
+ start
;
285 hold_region
= xmalloc(shift
);
286 memcpy(hold_region
, base
, shift
);
287 memcpy(base
, base
+shift
, len
-shift
);
288 memcpy(base
+(len
-shift
), hold_region
, shift
);
309 len
= iocur_top
->len
- start
;
311 if (len
+start
> iocur_top
->len
) {
312 dbprintf(_("length (%d) too large for data block size (%d)"),
313 len
, iocur_top
->len
);
316 base
= (char *)iocur_top
->data
+ start
;
318 hold_region
= xmalloc(shift
);
319 memcpy(hold_region
, base
+(len
-shift
), shift
);
320 memmove(base
+shift
, base
, len
-shift
);
321 memcpy(base
, hold_region
, shift
);
345 len
= iocur_top
->len
- start
;
347 if (len
+start
> iocur_top
->len
) {
348 dbprintf(_("length (%d) too large for data block size (%d)"),
349 len
, iocur_top
->len
);
352 if (from
== -1 || from
> 255)
354 if (to
== -1 || to
> 255)
369 buf
= (char *)iocur_top
->data
+ start
;
372 for (i
= start
; i
< start
+len
; i
++) {
374 tmp
= (tmp
+ step
)%(range
+1);
394 len
= iocur_top
->len
- start
;
396 if (len
+start
> iocur_top
->len
) {
397 dbprintf(_("length (%d) too large for data block size (%d)"),
398 len
, iocur_top
->len
);
401 buf
= (char *)iocur_top
->data
+ start
;
403 for (i
= start
; i
< start
+len
; i
++)
404 *buf
++ = (char)lrand48();
423 len
= iocur_top
->len
- start
;
425 if (len
+start
> iocur_top
->len
) {
426 dbprintf(_("length (%d) too large for data block size (%d)"),
427 len
, iocur_top
->len
);
430 base
= (char *)iocur_top
->data
+ start
;
432 memset(base
, value
, len
);
435 static struct bw_cmd
{
436 void (*cmdfunc
)(int,int,int,int,int);
448 /* cmd sig min max sh frm to start len */
449 { bwrite_lshift
, "lshift", 2, 0, 3, 1, 0, 0, 2, 3,
450 "[shiftcount] [start] [len]", },
451 { bwrite_rshift
, "rshift", 2, 0, 3, 1, 0, 0, 2, 3,
452 "[shiftcount] [start] [len]", },
453 { bwrite_lrot
, "lrot", 2, 0, 3, 1, 0, 0, 2, 3,
454 "[shiftcount] [start] [len]", },
455 { bwrite_rrot
, "rrot", 2, 0, 3, 1, 0, 0, 2, 3,
456 "[shiftcount] [start] [len]", },
457 { bwrite_seq
, "sequence", 3, 0, 4, 0, 1, 2, 3, 4,
458 "[from] [to] [start] [len]", },
459 { bwrite_random
, "random", 3, 0, 2, 0, 0, 0, 1, 2,
461 { bwrite_fill
, "fill", 1, 1, 3, 1, 0, 0, 2, 3,
462 "num [start] [len]" }
465 #define BWRITE_CMD_MAX (sizeof(bw_cmdtab)/sizeof(bw_cmdtab[0]))
476 /* only allow 1 case, '\' and 3 octal digits (or less) */
478 for (count
= 0; count
< 3; count
++) {
479 if (arg
[count
] == '\0')
482 if ((arg
[count
] < '0') || (arg
[count
] > '7'))
486 for (i
= 0; i
< count
; i
++) {
487 val
|= ((arg
[(count
-1)-i
]-'0')&0x07)<<(i
*3);
495 #define NYBBLE(x) (isdigit(x)?(x-'0'):(tolower(x)-'a'+0xa))
498 * convert_arg allows input in the following forms:
500 * - A string ("ABTB") whose ASCII value is placed in an array in the order
501 * matching the input.
503 * - An even number of hex numbers. If the length is greater than 64 bits,
504 * then the output is an array of bytes whose top nibble is the first hex
505 * digit in the input, the lower nibble is the second hex digit in the
506 * input. UUID entries are entered in this manner.
508 * - A decimal or hexadecimal integer to be used with setbitval().
510 * Numbers that are passed to setbitval() need to be in big endian format and
511 * are adjusted in the buffer so that the first input bit is to be be written to
512 * the first bit in the output.
524 static char *buf
= NULL
;
531 if (bit_length
<= 64)
534 alloc_size
= (bit_length
+ 7) / 8;
536 buf
= xrealloc(buf
, alloc_size
);
537 memset(buf
, 0, alloc_size
);
538 value
= (__be64
*)buf
;
542 /* input a string and output ASCII array of characters */
544 /* zap closing quote if there is one */
545 ostr
= strrchr(arg
+ 1, '\"');
550 for (i
= 0; i
< alloc_size
; i
++) {
554 /* do octal conversion */
556 if (*(ostr
+ 1) >= '0' && *(ostr
+ 1) <= '7') {
557 ret
= convert_oct(ostr
+ 1, &octval
);
568 if (arg
[0] == '#' || ((arg
[0] != '-') && strchr(arg
,'-'))) {
570 * handle hex blocks ie
571 * #00112233445566778899aabbccddeeff
573 * 1122334455667788-99aa-bbcc-ddee-ff00112233445566778899
575 * (but if it starts with "-" assume it's just an integer)
577 int bytes
= bit_length
/ NBBY
;
579 /* is this an array of hec numbers? */
580 if (bit_length
% NBBY
)
583 /* skip leading hash */
587 while (*arg
&& bytes
--) {
592 /* get first nybble */
593 if (!isxdigit((int)*arg
))
595 *rbuf
= NYBBLE((int)*arg
) << 4;
598 /* skip more hyphens */
602 /* get second nybble */
603 if (!isxdigit((int)*arg
))
605 *rbuf
++ |= NYBBLE((int)*arg
);
608 if (bytes
< 0 && *arg
)
614 /* handle decimal / hexadecimal integers */
615 val
= strtoll(arg
, &endp
, 0);
616 /* return if not a clean number */
620 /* Does the value fit into the range of the destination bitfield? */
621 if (bit_length
< 64 && (val
>> bit_length
) > 0)
624 * If the length of the field is not a multiple of a byte, push
625 * the bits up in the field, so the most signicant field bit is
626 * the most significant bit in the byte:
629 * val |----|----|----|----|----|--MM|mmmm|llll|
631 * val |----|----|----|----|----|MMmm|mmll|ll00|
633 offset
= bit_length
% NBBY
;
635 val
<<= (NBBY
- offset
);
638 * convert to big endian and copy into the array
639 * rbuf |----|----|----|----|----|MMmm|mmll|ll00|
641 *value
= cpu_to_be64(val
);
644 * Align the array to point to the field in the array.
645 * rbuf = |MMmm|mmll|ll00|
647 offset
= sizeof(__be64
) - 1 - ((bit_length
- 1) / sizeof(__be64
));
656 const field_t
*fields
,
668 dbprintf(_("usage: write fieldname value\n"));
672 fl
= flist_scan(argv
[0]);
674 dbprintf(_("unable to parse '%s'.\n"), argv
[0]);
678 /* if we're a root field type, go down 1 layer to get field list */
679 if (fields
->name
[0] == '\0') {
680 fa
= &ftattrtab
[fields
->ftyp
];
681 ASSERT(fa
->ftyp
== fields
->ftyp
);
685 /* run down the field list and set offsets into the data */
686 if (!flist_parse(fields
, fl
, iocur_top
->data
, 0)) {
688 dbprintf(_("parsing error\n"));
695 parentoffset
= sfl
->offset
;
700 * For structures, fsize * fcount tells us the size of the region we are
701 * modifying, which is usually a single structure member and is pointed
702 * to by the last child in the list.
704 * However, if the base structure is an array and we have a direct index
705 * into the array (e.g. write bno[5]) then we are returned a single
706 * flist object with the offset pointing directly at the location we
707 * need to modify. The length of the object we are modifying is then
708 * determined by the size of the individual array entry (fsize) and the
709 * indexes defined in the object, not the overall size of the array
710 * (which is what fcount returns).
712 bit_length
= fsize(sfl
->fld
, iocur_top
->data
, parentoffset
, 0);
713 if (sfl
->fld
->flags
& FLD_ARRAY
)
714 bit_length
*= sfl
->high
- sfl
->low
+ 1;
716 bit_length
*= fcount(sfl
->fld
, iocur_top
->data
, parentoffset
);
718 /* convert this to a generic conversion routine */
719 /* should be able to handle str, num, or even labels */
721 buf
= convert_arg(argv
[1], bit_length
);
723 dbprintf(_("unable to convert value '%s'.\n"), argv
[1]);
728 setbitval(iocur_top
->data
, sfl
->offset
, bit_length
, buf
);
739 const field_t
*fields
,
747 dbprintf(_("usage (in string mode): write \"string...\"\n"));
751 buf
= convert_arg(argv
[0], (int)((strlen(argv
[0])+1)*8));
752 for (i
= 0; i
< iocur_top
->len
; i
++) {
753 ((char *)iocur_top
->data
)[i
] = *buf
;
758 /* write back to disk */
765 const field_t
*fields
,
775 struct bw_cmd
*cmd
= NULL
;
777 if (argc
<= 1 || argc
> 5)
780 for (i
= 0; i
< BWRITE_CMD_MAX
; i
++) {
781 if (sigcmp(argv
[0], bw_cmdtab
[i
].cmdstr
,
782 bw_cmdtab
[i
].sig_chars
)) {
789 dbprintf(_("write: invalid subcommand\n"));
793 if ((argc
< cmd
->argmin
+ 1) || (argc
> cmd
->argmax
+ 1)) {
794 dbprintf(_("write %s: invalid number of arguments\n"),
799 if (cmd
->shiftcount_arg
&& (cmd
->shiftcount_arg
< argc
))
800 shiftcount
= (int)strtoul(argv
[cmd
->shiftcount_arg
], NULL
, 0);
801 if (cmd
->start_arg
&& (cmd
->start_arg
< argc
))
802 start
= (int)strtoul(argv
[cmd
->start_arg
], NULL
, 0);
803 if (cmd
->len_arg
&& (cmd
->len_arg
< argc
))
804 len
= (int)strtoul(argv
[cmd
->len_arg
], NULL
, 0);
805 if (cmd
->from_arg
&& (cmd
->len_arg
< argc
))
806 from
= (int)strtoul(argv
[cmd
->from_arg
], NULL
, 0);
807 if (cmd
->to_arg
&& (cmd
->len_arg
< argc
))
808 to
= (int)strtoul(argv
[cmd
->to_arg
], NULL
, 0);
810 cmd
->cmdfunc(start
, len
, shiftcount
, from
, to
);
812 /* write back to disk */
818 dbprintf(_("usage: write (in data mode)\n"));
819 for (i
= 0; i
< BWRITE_CMD_MAX
; i
++) {
820 dbprintf(" %-9.9s %s\n",
821 bw_cmdtab
[i
].cmdstr
, bw_cmdtab
[i
].usage
);