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"));
141 iocur_top
->typ
->crc_off
== TYP_F_NO_CRC_OFF
&&
142 !iocur_top
->ino_buf
) {
143 dbprintf(_("Cannot recalculate CRCs on this type of object\n"));
151 * If the buffer has no verifier or we are using standard verifier
152 * paths, then just write it out and return
154 if (!iocur_top
->bp
->b_ops
||
155 !(corrupt
|| invalid_data
)) {
156 (*pf
)(DB_WRITE
, cur_typ
->fields
, argc
, argv
);
161 /* Temporarily remove write verifier to write bad data */
162 stashed_ops
= iocur_top
->bp
->b_ops
;
163 local_ops
.verify_read
= stashed_ops
->verify_read
;
164 iocur_top
->bp
->b_ops
= &local_ops
;
167 local_ops
.verify_write
= xfs_dummy_verify
;
168 dbprintf(_("Allowing write of corrupted data and bad CRC\n"));
169 } else if (iocur_top
->ino_buf
) {
170 local_ops
.verify_write
= xfs_verify_recalc_inode_crc
;
171 dbprintf(_("Allowing write of corrupted inode with good CRC\n"));
172 } else { /* invalid data */
173 local_ops
.verify_write
= xfs_verify_recalc_crc
;
174 dbprintf(_("Allowing write of corrupted data with good CRC\n"));
177 (*pf
)(DB_WRITE
, cur_typ
->fields
, argc
, argv
);
179 iocur_top
->bp
->b_ops
= stashed_ops
;
184 /* compare significant portions of commands */
197 for (sigcnt
= 0; *s1
== *s2
; s1
++, s2
++) {
205 if (sig
&& (sigcnt
>= sig
))
227 len
= iocur_top
->len
- start
;
229 if (len
+start
> iocur_top
->len
) {
230 dbprintf(_("length (%d) too large for data block size (%d)"),
231 len
, iocur_top
->len
);
234 base
= (char *)iocur_top
->data
+ start
;
236 memcpy(base
, base
+shift
, len
-shift
);
237 memset(base
+(len
-shift
), 0, shift
);
256 len
= iocur_top
->len
- start
;
258 if (len
+start
> iocur_top
->len
) {
259 dbprintf(_("length (%d) too large for data block size (%d)"),
260 len
, iocur_top
->len
);
263 base
= (char *)iocur_top
->data
+ start
;
265 memcpy(base
+shift
, base
, len
-shift
);
266 memset(base
, 0, shift
);
286 len
= iocur_top
->len
- start
;
288 if (len
+start
> iocur_top
->len
) {
289 dbprintf(_("length (%d) too large for data block size (%d)"),
290 len
, iocur_top
->len
);
293 base
= (char *)iocur_top
->data
+ start
;
295 hold_region
= xmalloc(shift
);
296 memcpy(hold_region
, base
, shift
);
297 memcpy(base
, base
+shift
, len
-shift
);
298 memcpy(base
+(len
-shift
), hold_region
, shift
);
319 len
= iocur_top
->len
- start
;
321 if (len
+start
> iocur_top
->len
) {
322 dbprintf(_("length (%d) too large for data block size (%d)"),
323 len
, iocur_top
->len
);
326 base
= (char *)iocur_top
->data
+ start
;
328 hold_region
= xmalloc(shift
);
329 memcpy(hold_region
, base
+(len
-shift
), shift
);
330 memmove(base
+shift
, base
, len
-shift
);
331 memcpy(base
, hold_region
, shift
);
355 len
= iocur_top
->len
- start
;
357 if (len
+start
> iocur_top
->len
) {
358 dbprintf(_("length (%d) too large for data block size (%d)"),
359 len
, iocur_top
->len
);
362 if (from
== -1 || from
> 255)
364 if (to
== -1 || to
> 255)
379 buf
= (char *)iocur_top
->data
+ start
;
382 for (i
= start
; i
< start
+len
; i
++) {
384 tmp
= (tmp
+ step
)%(range
+1);
404 len
= iocur_top
->len
- start
;
406 if (len
+start
> iocur_top
->len
) {
407 dbprintf(_("length (%d) too large for data block size (%d)"),
408 len
, iocur_top
->len
);
411 buf
= (char *)iocur_top
->data
+ start
;
413 for (i
= start
; i
< start
+len
; i
++)
414 *buf
++ = (char)lrand48();
433 len
= iocur_top
->len
- start
;
435 if (len
+start
> iocur_top
->len
) {
436 dbprintf(_("length (%d) too large for data block size (%d)"),
437 len
, iocur_top
->len
);
440 base
= (char *)iocur_top
->data
+ start
;
442 memset(base
, value
, len
);
445 static struct bw_cmd
{
446 void (*cmdfunc
)(int,int,int,int,int);
458 /* cmd sig min max sh frm to start len */
459 { bwrite_lshift
, "lshift", 2, 0, 3, 1, 0, 0, 2, 3,
460 "[shiftcount] [start] [len]", },
461 { bwrite_rshift
, "rshift", 2, 0, 3, 1, 0, 0, 2, 3,
462 "[shiftcount] [start] [len]", },
463 { bwrite_lrot
, "lrot", 2, 0, 3, 1, 0, 0, 2, 3,
464 "[shiftcount] [start] [len]", },
465 { bwrite_rrot
, "rrot", 2, 0, 3, 1, 0, 0, 2, 3,
466 "[shiftcount] [start] [len]", },
467 { bwrite_seq
, "sequence", 3, 0, 4, 0, 1, 2, 3, 4,
468 "[from] [to] [start] [len]", },
469 { bwrite_random
, "random", 3, 0, 2, 0, 0, 0, 1, 2,
471 { bwrite_fill
, "fill", 1, 1, 3, 1, 0, 0, 2, 3,
472 "num [start] [len]" }
475 #define BWRITE_CMD_MAX (sizeof(bw_cmdtab)/sizeof(bw_cmdtab[0]))
486 /* only allow 1 case, '\' and 3 octal digits (or less) */
488 for (count
= 0; count
< 3; count
++) {
489 if (arg
[count
] == '\0')
492 if ((arg
[count
] < '0') && (arg
[count
] > '7'))
496 for (i
= 0; i
< count
; i
++) {
497 val
|= ((arg
[(count
-1)-i
]-'0')&0x07)<<(i
*3);
505 #define NYBBLE(x) (isdigit(x)?(x-'0'):(tolower(x)-'a'+0xa))
508 * convert_arg allows input in the following forms:
510 * - A string ("ABTB") whose ASCII value is placed in an array in the order
511 * matching the input.
513 * - An even number of hex numbers. If the length is greater than 64 bits,
514 * then the output is an array of bytes whose top nibble is the first hex
515 * digit in the input, the lower nibble is the second hex digit in the
516 * input. UUID entries are entered in this manner.
518 * - A decimal or hexadecimal integer to be used with setbitval().
520 * Numbers that are passed to setbitval() need to be in big endian format and
521 * are adjusted in the buffer so that the first input bit is to be be written to
522 * the first bit in the output.
534 static char *buf
= NULL
;
541 if (bit_length
<= 64)
544 alloc_size
= (bit_length
+ 7) / 8;
546 buf
= xrealloc(buf
, alloc_size
);
547 memset(buf
, 0, alloc_size
);
548 value
= (__u64
*)buf
;
552 /* input a string and output ASCII array of characters */
554 /* zap closing quote if there is one */
555 ostr
= strrchr(arg
+ 1, '\"');
560 for (i
= 0; i
< alloc_size
; i
++) {
564 /* do octal conversion */
566 if (*(ostr
+ 1) >= '0' || *(ostr
+ 1) <= '7') {
567 ret
= convert_oct(ostr
+ 1, &octval
);
578 if (arg
[0] == '#' || ((arg
[0] != '-') && strchr(arg
,'-'))) {
580 * handle hex blocks ie
581 * #00112233445566778899aabbccddeeff
583 * 1122334455667788-99aa-bbcc-ddee-ff00112233445566778899
585 * (but if it starts with "-" assume it's just an integer)
587 int bytes
= bit_length
/ NBBY
;
589 /* is this an array of hec numbers? */
590 if (bit_length
% NBBY
)
593 /* skip leading hash */
597 while (*arg
&& bytes
--) {
602 /* get first nybble */
603 if (!isxdigit((int)*arg
))
605 *rbuf
= NYBBLE((int)*arg
) << 4;
608 /* skip more hyphens */
612 /* get second nybble */
613 if (!isxdigit((int)*arg
))
615 *rbuf
++ |= NYBBLE((int)*arg
);
618 if (bytes
< 0 && *arg
)
624 /* handle decimal / hexadecimal integers */
625 val
= strtoll(arg
, &endp
, 0);
626 /* return if not a clean number */
630 /* Does the value fit into the range of the destination bitfield? */
631 if (bit_length
< 64 && (val
>> bit_length
) > 0)
634 * If the length of the field is not a multiple of a byte, push
635 * the bits up in the field, so the most signicant field bit is
636 * the most significant bit in the byte:
639 * val |----|----|----|----|----|--MM|mmmm|llll|
641 * val |----|----|----|----|----|MMmm|mmll|ll00|
643 offset
= bit_length
% NBBY
;
645 val
<<= (NBBY
- offset
);
648 * convert to big endian and copy into the array
649 * rbuf |----|----|----|----|----|MMmm|mmll|ll00|
651 *value
= cpu_to_be64(val
);
654 * Align the array to point to the field in the array.
655 * rbuf = |MMmm|mmll|ll00|
657 offset
= sizeof(__be64
) - 1 - ((bit_length
- 1) / sizeof(__be64
));
666 const field_t
*fields
,
678 dbprintf(_("usage: write fieldname value\n"));
682 fl
= flist_scan(argv
[0]);
684 dbprintf(_("unable to parse '%s'.\n"), argv
[0]);
688 /* if we're a root field type, go down 1 layer to get field list */
689 if (fields
->name
[0] == '\0') {
690 fa
= &ftattrtab
[fields
->ftyp
];
691 ASSERT(fa
->ftyp
== fields
->ftyp
);
695 /* run down the field list and set offsets into the data */
696 if (!flist_parse(fields
, fl
, iocur_top
->data
, 0)) {
698 dbprintf(_("parsing error\n"));
705 parentoffset
= sfl
->offset
;
710 * For structures, fsize * fcount tells us the size of the region we are
711 * modifying, which is usually a single structure member and is pointed
712 * to by the last child in the list.
714 * However, if the base structure is an array and we have a direct index
715 * into the array (e.g. write bno[5]) then we are returned a single
716 * flist object with the offset pointing directly at the location we
717 * need to modify. The length of the object we are modifying is then
718 * determined by the size of the individual array entry (fsize) and the
719 * indexes defined in the object, not the overall size of the array
720 * (which is what fcount returns).
722 bit_length
= fsize(sfl
->fld
, iocur_top
->data
, parentoffset
, 0);
723 if (sfl
->fld
->flags
& FLD_ARRAY
)
724 bit_length
*= sfl
->high
- sfl
->low
+ 1;
726 bit_length
*= fcount(sfl
->fld
, iocur_top
->data
, parentoffset
);
728 /* convert this to a generic conversion routine */
729 /* should be able to handle str, num, or even labels */
731 buf
= convert_arg(argv
[1], bit_length
);
733 dbprintf(_("unable to convert value '%s'.\n"), argv
[1]);
738 setbitval(iocur_top
->data
, sfl
->offset
, bit_length
, buf
);
749 const field_t
*fields
,
757 dbprintf(_("usage (in string mode): write \"string...\"\n"));
761 buf
= convert_arg(argv
[0], (int)((strlen(argv
[0])+1)*8));
762 for (i
= 0; i
< iocur_top
->len
; i
++) {
763 ((char *)iocur_top
->data
)[i
] = *buf
;
768 /* write back to disk */
775 const field_t
*fields
,
785 struct bw_cmd
*cmd
= NULL
;
787 if (argc
<= 1 || argc
> 5)
790 for (i
= 0; i
< BWRITE_CMD_MAX
; i
++) {
791 if (sigcmp(argv
[0], bw_cmdtab
[i
].cmdstr
,
792 bw_cmdtab
[i
].sig_chars
)) {
799 dbprintf(_("write: invalid subcommand\n"));
803 if ((argc
< cmd
->argmin
+ 1) || (argc
> cmd
->argmax
+ 1)) {
804 dbprintf(_("write %s: invalid number of arguments\n"),
809 if (cmd
->shiftcount_arg
&& (cmd
->shiftcount_arg
< argc
))
810 shiftcount
= (int)strtoul(argv
[cmd
->shiftcount_arg
], NULL
, 0);
811 if (cmd
->start_arg
&& (cmd
->start_arg
< argc
))
812 start
= (int)strtoul(argv
[cmd
->start_arg
], NULL
, 0);
813 if (cmd
->len_arg
&& (cmd
->len_arg
< argc
))
814 len
= (int)strtoul(argv
[cmd
->len_arg
], NULL
, 0);
815 if (cmd
->from_arg
&& (cmd
->len_arg
< argc
))
816 from
= (int)strtoul(argv
[cmd
->from_arg
], NULL
, 0);
817 if (cmd
->to_arg
&& (cmd
->len_arg
< argc
))
818 to
= (int)strtoul(argv
[cmd
->to_arg
], NULL
, 0);
820 cmd
->cmdfunc(start
, len
, shiftcount
, from
, to
);
822 /* write back to disk */
828 dbprintf(_("usage: write (in data mode)\n"));
829 for (i
= 0; i
< BWRITE_CMD_MAX
; i
++) {
830 dbprintf(" %-9.9s %s\n",
831 bw_cmdtab
[i
].cmdstr
, bw_cmdtab
[i
].usage
);