]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/write.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
37 static int write_f(int argc
, char **argv
);
38 static void write_help(void);
40 static const cmdinfo_t write_cmd
=
41 { "write", NULL
, write_f
, 0, -1, 0, N_("[-c] [field or value]..."),
42 N_("write value to disk"), write_help
};
50 add_command(&write_cmd
);
59 " The 'write' command takes on different personalities depending on the\n"
60 " type of object being worked with.\n\n"
61 " Write has 3 modes:\n"
62 " 'struct mode' - is active anytime you're looking at a filesystem object\n"
63 " which contains individual fields (ex: an inode).\n"
64 " 'data mode' - is active anytime you set a disk address directly or set\n"
65 " the type to 'data'.\n"
66 " 'string mode' - only used for writing symlink blocks.\n"
69 " Struct mode: 'write core.uid 23' - set an inode uid field to 23.\n"
70 " 'write fname \"hello\\000\"' - write superblock fname.\n"
71 " (note: in struct mode strings are not null terminated)\n"
72 " 'write fname #6669736800' - write superblock fname with hex.\n"
73 " 'write uuid 00112233-4455-6677-8899-aabbccddeeff'\n"
74 " - write superblock uuid.\n"
75 " Data mode: 'write fill 0xff' - fill the entire block with 0xff's\n"
76 " 'write lshift 3' - shift the block 3 bytes to the left\n"
77 " 'write sequence 1 5' - write a cycle of number [1-5] through\n"
78 " the entire block.\n"
79 " String mode: 'write \"This_is_a_filename\" - write null terminated string.\n"
81 " In data mode type 'write' by itself for a list of specific commands.\n\n"
82 " Specifying the -c option will allow writes of invalid (corrupt) data with\n"
83 " an invalid CRC. Specifying the -d option will allow writes of invalid data,\n"
84 " but still recalculate the CRC so we are forced to check and detect the\n"
85 " invalid data appropriately.\n\n"
96 extern char *progname
;
98 bool corrupt
= false; /* Allow write of bad data w/ invalid CRC */
99 bool invalid_data
= false; /* Allow write of bad data w/ valid CRC */
100 struct xfs_buf_ops local_ops
;
101 const struct xfs_buf_ops
*stashed_ops
= NULL
;
103 if (x
.isreadonly
& LIBXFS_ISREADONLY
) {
104 dbprintf(_("%s started in read only mode, writing disabled\n"),
109 if (cur_typ
== NULL
) {
110 dbprintf(_("no current type\n"));
116 dbprintf(_("no handler function for type %s, write unsupported.\n"),
121 while ((c
= getopt(argc
, argv
, "cd")) != EOF
) {
130 dbprintf(_("bad option for write command\n"));
135 if (corrupt
&& invalid_data
) {
136 dbprintf(_("Cannot specify both -c and -d options\n"));
140 if (invalid_data
&& iocur_top
->typ
->crc_off
== TYP_F_NO_CRC_OFF
) {
141 dbprintf(_("Cannot recalculate CRCs on this type of object\n"));
149 * If the buffer has no verifier or we are using standard verifier
150 * paths, then just write it out and return
152 if (!iocur_top
->bp
->b_ops
||
153 !(corrupt
|| invalid_data
)) {
154 (*pf
)(DB_WRITE
, cur_typ
->fields
, argc
, argv
);
159 /* Temporarily remove write verifier to write bad data */
160 stashed_ops
= iocur_top
->bp
->b_ops
;
161 local_ops
.verify_read
= stashed_ops
->verify_read
;
162 iocur_top
->bp
->b_ops
= &local_ops
;
165 local_ops
.verify_write
= xfs_dummy_verify
;
166 dbprintf(_("Allowing write of corrupted data and bad CRC\n"));
167 } else { /* invalid data */
168 local_ops
.verify_write
= xfs_verify_recalc_crc
;
169 dbprintf(_("Allowing write of corrupted data with good CRC\n"));
172 (*pf
)(DB_WRITE
, cur_typ
->fields
, argc
, argv
);
174 iocur_top
->bp
->b_ops
= stashed_ops
;
179 /* compare significant portions of commands */
192 for (sigcnt
= 0; *s1
== *s2
; s1
++, s2
++) {
200 if (sig
&& (sigcnt
>= sig
))
222 len
= iocur_top
->len
- start
;
224 if (len
+start
> iocur_top
->len
) {
225 dbprintf(_("length (%d) too large for data block size (%d)"),
226 len
, iocur_top
->len
);
229 base
= (char *)iocur_top
->data
+ start
;
231 memcpy(base
, base
+shift
, len
-shift
);
232 memset(base
+(len
-shift
), 0, shift
);
251 len
= iocur_top
->len
- start
;
253 if (len
+start
> iocur_top
->len
) {
254 dbprintf(_("length (%d) too large for data block size (%d)"),
255 len
, iocur_top
->len
);
258 base
= (char *)iocur_top
->data
+ start
;
260 memcpy(base
+shift
, base
, len
-shift
);
261 memset(base
, 0, shift
);
281 len
= iocur_top
->len
- start
;
283 if (len
+start
> iocur_top
->len
) {
284 dbprintf(_("length (%d) too large for data block size (%d)"),
285 len
, iocur_top
->len
);
288 base
= (char *)iocur_top
->data
+ start
;
290 hold_region
= xmalloc(shift
);
291 memcpy(hold_region
, base
, shift
);
292 memcpy(base
, base
+shift
, len
-shift
);
293 memcpy(base
+(len
-shift
), hold_region
, shift
);
314 len
= iocur_top
->len
- start
;
316 if (len
+start
> iocur_top
->len
) {
317 dbprintf(_("length (%d) too large for data block size (%d)"),
318 len
, iocur_top
->len
);
321 base
= (char *)iocur_top
->data
+ start
;
323 hold_region
= xmalloc(shift
);
324 memcpy(hold_region
, base
+(len
-shift
), shift
);
325 memmove(base
+shift
, base
, len
-shift
);
326 memcpy(base
, hold_region
, shift
);
350 len
= iocur_top
->len
- start
;
352 if (len
+start
> iocur_top
->len
) {
353 dbprintf(_("length (%d) too large for data block size (%d)"),
354 len
, iocur_top
->len
);
357 if (from
== -1 || from
> 255)
359 if (to
== -1 || to
> 255)
374 buf
= (char *)iocur_top
->data
+ start
;
377 for (i
= start
; i
< start
+len
; i
++) {
379 tmp
= (tmp
+ step
)%(range
+1);
399 len
= iocur_top
->len
- start
;
401 if (len
+start
> iocur_top
->len
) {
402 dbprintf(_("length (%d) too large for data block size (%d)"),
403 len
, iocur_top
->len
);
406 buf
= (char *)iocur_top
->data
+ start
;
408 for (i
= start
; i
< start
+len
; i
++)
409 *buf
++ = (char)lrand48();
428 len
= iocur_top
->len
- start
;
430 if (len
+start
> iocur_top
->len
) {
431 dbprintf(_("length (%d) too large for data block size (%d)"),
432 len
, iocur_top
->len
);
435 base
= (char *)iocur_top
->data
+ start
;
437 memset(base
, value
, len
);
440 static struct bw_cmd
{
441 void (*cmdfunc
)(int,int,int,int,int);
453 /* cmd sig min max sh frm to start len */
454 { bwrite_lshift
, "lshift", 2, 0, 3, 1, 0, 0, 2, 3,
455 "[shiftcount] [start] [len]", },
456 { bwrite_rshift
, "rshift", 2, 0, 3, 1, 0, 0, 2, 3,
457 "[shiftcount] [start] [len]", },
458 { bwrite_lrot
, "lrot", 2, 0, 3, 1, 0, 0, 2, 3,
459 "[shiftcount] [start] [len]", },
460 { bwrite_rrot
, "rrot", 2, 0, 3, 1, 0, 0, 2, 3,
461 "[shiftcount] [start] [len]", },
462 { bwrite_seq
, "sequence", 3, 0, 4, 0, 1, 2, 3, 4,
463 "[from] [to] [start] [len]", },
464 { bwrite_random
, "random", 3, 0, 2, 0, 0, 0, 1, 2,
466 { bwrite_fill
, "fill", 1, 1, 3, 1, 0, 0, 2, 3,
467 "num [start] [len]" }
470 #define BWRITE_CMD_MAX (sizeof(bw_cmdtab)/sizeof(bw_cmdtab[0]))
481 /* only allow 1 case, '\' and 3 octal digits (or less) */
483 for (count
= 0; count
< 3; count
++) {
484 if (arg
[count
] == '\0')
487 if ((arg
[count
] < '0') && (arg
[count
] > '7'))
491 for (i
= 0; i
< count
; i
++) {
492 val
|= ((arg
[(count
-1)-i
]-'0')&0x07)<<(i
*3);
500 #define NYBBLE(x) (isdigit(x)?(x-'0'):(tolower(x)-'a'+0xa))
503 * convert_arg allows input in the following forms:
505 * - A string ("ABTB") whose ASCII value is placed in an array in the order
506 * matching the input.
508 * - An even number of hex numbers. If the length is greater than 64 bits,
509 * then the output is an array of bytes whose top nibble is the first hex
510 * digit in the input, the lower nibble is the second hex digit in the
511 * input. UUID entries are entered in this manner.
513 * - A decimal or hexadecimal integer to be used with setbitval().
515 * Numbers that are passed to setbitval() need to be in big endian format and
516 * are adjusted in the buffer so that the first input bit is to be be written to
517 * the first bit in the output.
529 static char *buf
= NULL
;
536 if (bit_length
<= 64)
539 alloc_size
= (bit_length
+ 7) / 8;
541 buf
= xrealloc(buf
, alloc_size
);
542 memset(buf
, 0, alloc_size
);
543 value
= (__u64
*)buf
;
547 /* input a string and output ASCII array of characters */
549 /* zap closing quote if there is one */
550 ostr
= strrchr(arg
+ 1, '\"');
555 for (i
= 0; i
< alloc_size
; i
++) {
559 /* do octal conversion */
561 if (*(ostr
+ 1) >= '0' || *(ostr
+ 1) <= '7') {
562 ret
= convert_oct(ostr
+ 1, &octval
);
573 if (arg
[0] == '#' || ((arg
[0] != '-') && strchr(arg
,'-'))) {
575 * handle hex blocks ie
576 * #00112233445566778899aabbccddeeff
578 * 1122334455667788-99aa-bbcc-ddee-ff00112233445566778899
580 * (but if it starts with "-" assume it's just an integer)
582 int bytes
= bit_length
/ NBBY
;
584 /* is this an array of hec numbers? */
585 if (bit_length
% NBBY
)
588 /* skip leading hash */
592 while (*arg
&& bytes
--) {
597 /* get first nybble */
598 if (!isxdigit((int)*arg
))
600 *rbuf
= NYBBLE((int)*arg
) << 4;
603 /* skip more hyphens */
607 /* get second nybble */
608 if (!isxdigit((int)*arg
))
610 *rbuf
++ |= NYBBLE((int)*arg
);
613 if (bytes
< 0 && *arg
)
619 /* handle decimal / hexadecimal integers */
620 val
= strtoll(arg
, &endp
, 0);
621 /* return if not a clean number */
625 /* Does the value fit into the range of the destination bitfield? */
626 if (bit_length
< 64 && (val
>> bit_length
) > 0)
629 * If the length of the field is not a multiple of a byte, push
630 * the bits up in the field, so the most signicant field bit is
631 * the most significant bit in the byte:
634 * val |----|----|----|----|----|--MM|mmmm|llll|
636 * val |----|----|----|----|----|MMmm|mmll|ll00|
638 offset
= bit_length
% NBBY
;
640 val
<<= (NBBY
- offset
);
643 * convert to big endian and copy into the array
644 * rbuf |----|----|----|----|----|MMmm|mmll|ll00|
646 *value
= cpu_to_be64(val
);
649 * Align the array to point to the field in the array.
650 * rbuf = |MMmm|mmll|ll00|
652 offset
= sizeof(__be64
) - 1 - ((bit_length
- 1) / sizeof(__be64
));
661 const field_t
*fields
,
673 dbprintf(_("usage: write fieldname value\n"));
677 fl
= flist_scan(argv
[0]);
679 dbprintf(_("unable to parse '%s'.\n"), argv
[0]);
683 /* if we're a root field type, go down 1 layer to get field list */
684 if (fields
->name
[0] == '\0') {
685 fa
= &ftattrtab
[fields
->ftyp
];
686 ASSERT(fa
->ftyp
== fields
->ftyp
);
690 /* run down the field list and set offsets into the data */
691 if (!flist_parse(fields
, fl
, iocur_top
->data
, 0)) {
693 dbprintf(_("parsing error\n"));
700 parentoffset
= sfl
->offset
;
705 * For structures, fsize * fcount tells us the size of the region we are
706 * modifying, which is usually a single structure member and is pointed
707 * to by the last child in the list.
709 * However, if the base structure is an array and we have a direct index
710 * into the array (e.g. write bno[5]) then we are returned a single
711 * flist object with the offset pointing directly at the location we
712 * need to modify. The length of the object we are modifying is then
713 * determined by the size of the individual array entry (fsize) and the
714 * indexes defined in the object, not the overall size of the array
715 * (which is what fcount returns).
717 bit_length
= fsize(sfl
->fld
, iocur_top
->data
, parentoffset
, 0);
718 if (sfl
->fld
->flags
& FLD_ARRAY
)
719 bit_length
*= sfl
->high
- sfl
->low
+ 1;
721 bit_length
*= fcount(sfl
->fld
, iocur_top
->data
, parentoffset
);
723 /* convert this to a generic conversion routine */
724 /* should be able to handle str, num, or even labels */
726 buf
= convert_arg(argv
[1], bit_length
);
728 dbprintf(_("unable to convert value '%s'.\n"), argv
[1]);
733 setbitval(iocur_top
->data
, sfl
->offset
, bit_length
, buf
);
744 const field_t
*fields
,
752 dbprintf(_("usage (in string mode): write \"string...\"\n"));
756 buf
= convert_arg(argv
[0], (int)((strlen(argv
[0])+1)*8));
757 for (i
= 0; i
< iocur_top
->len
; i
++) {
758 ((char *)iocur_top
->data
)[i
] = *buf
;
763 /* write back to disk */
770 const field_t
*fields
,
780 struct bw_cmd
*cmd
= NULL
;
782 if (argc
<= 1 || argc
> 5)
785 for (i
= 0; i
< BWRITE_CMD_MAX
; i
++) {
786 if (sigcmp(argv
[0], bw_cmdtab
[i
].cmdstr
,
787 bw_cmdtab
[i
].sig_chars
)) {
794 dbprintf(_("write: invalid subcommand\n"));
798 if ((argc
< cmd
->argmin
+ 1) || (argc
> cmd
->argmax
+ 1)) {
799 dbprintf(_("write %s: invalid number of arguments\n"),
804 if (cmd
->shiftcount_arg
&& (cmd
->shiftcount_arg
< argc
))
805 shiftcount
= (int)strtoul(argv
[cmd
->shiftcount_arg
], NULL
, 0);
806 if (cmd
->start_arg
&& (cmd
->start_arg
< argc
))
807 start
= (int)strtoul(argv
[cmd
->start_arg
], NULL
, 0);
808 if (cmd
->len_arg
&& (cmd
->len_arg
< argc
))
809 len
= (int)strtoul(argv
[cmd
->len_arg
], NULL
, 0);
810 if (cmd
->from_arg
&& (cmd
->len_arg
< argc
))
811 from
= (int)strtoul(argv
[cmd
->from_arg
], NULL
, 0);
812 if (cmd
->to_arg
&& (cmd
->len_arg
< argc
))
813 to
= (int)strtoul(argv
[cmd
->to_arg
], NULL
, 0);
815 cmd
->cmdfunc(start
, len
, shiftcount
, from
, to
);
817 /* write back to disk */
823 dbprintf(_("usage: write (in data mode)\n"));
824 for (i
= 0; i
< BWRITE_CMD_MAX
; i
++) {
825 dbprintf(" %-9.9s %s\n",
826 bw_cmdtab
[i
].cmdstr
, bw_cmdtab
[i
].usage
);