]>
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.\n\n"
93 extern char *progname
;
95 int corrupt
= 0; /* Allow write of corrupt data; skip verification */
96 struct xfs_buf_ops nowrite_ops
;
97 const struct xfs_buf_ops
*stashed_ops
= NULL
;
99 if (x
.isreadonly
& LIBXFS_ISREADONLY
) {
100 dbprintf(_("%s started in read only mode, writing disabled\n"),
105 if (cur_typ
== NULL
) {
106 dbprintf(_("no current type\n"));
112 dbprintf(_("no handler function for type %s, write unsupported.\n"),
117 while ((c
= getopt(argc
, argv
, "c")) != EOF
) {
123 dbprintf(_("bad option for write command\n"));
131 if (iocur_top
->bp
->b_ops
&& corrupt
) {
132 /* Temporarily remove write verifier to write bad data */
133 stashed_ops
= iocur_top
->bp
->b_ops
;
134 nowrite_ops
.verify_read
= stashed_ops
->verify_read
;
135 nowrite_ops
.verify_write
= xfs_dummy_verify
;
136 iocur_top
->bp
->b_ops
= &nowrite_ops
;
137 dbprintf(_("Allowing write of corrupted data\n"));
140 (*pf
)(DB_WRITE
, cur_typ
->fields
, argc
, argv
);
143 iocur_top
->bp
->b_ops
= stashed_ops
;
148 /* compare significant portions of commands */
161 for (sigcnt
= 0; *s1
== *s2
; s1
++, s2
++) {
169 if (sig
&& (sigcnt
>= sig
))
191 len
= iocur_top
->len
- start
;
193 if (len
+start
> iocur_top
->len
) {
194 dbprintf(_("length (%d) too large for data block size (%d)"),
195 len
, iocur_top
->len
);
198 base
= (char *)iocur_top
->data
+ start
;
200 memcpy(base
, base
+shift
, len
-shift
);
201 memset(base
+(len
-shift
), 0, shift
);
220 len
= iocur_top
->len
- start
;
222 if (len
+start
> iocur_top
->len
) {
223 dbprintf(_("length (%d) too large for data block size (%d)"),
224 len
, iocur_top
->len
);
227 base
= (char *)iocur_top
->data
+ start
;
229 memcpy(base
+shift
, base
, len
-shift
);
230 memset(base
, 0, shift
);
250 len
= iocur_top
->len
- start
;
252 if (len
+start
> iocur_top
->len
) {
253 dbprintf(_("length (%d) too large for data block size (%d)"),
254 len
, iocur_top
->len
);
257 base
= (char *)iocur_top
->data
+ start
;
259 hold_region
= xmalloc(shift
);
260 memcpy(hold_region
, base
, shift
);
261 memcpy(base
, base
+shift
, len
-shift
);
262 memcpy(base
+(len
-shift
), hold_region
, shift
);
283 len
= iocur_top
->len
- start
;
285 if (len
+start
> iocur_top
->len
) {
286 dbprintf(_("length (%d) too large for data block size (%d)"),
287 len
, iocur_top
->len
);
290 base
= (char *)iocur_top
->data
+ start
;
292 hold_region
= xmalloc(shift
);
293 memcpy(hold_region
, base
+(len
-shift
), shift
);
294 memmove(base
+shift
, base
, len
-shift
);
295 memcpy(base
, 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 if (from
== -1 || from
> 255)
328 if (to
== -1 || to
> 255)
343 buf
= (char *)iocur_top
->data
+ start
;
346 for (i
= start
; i
< start
+len
; i
++) {
348 tmp
= (tmp
+ step
)%(range
+1);
368 len
= iocur_top
->len
- start
;
370 if (len
+start
> iocur_top
->len
) {
371 dbprintf(_("length (%d) too large for data block size (%d)"),
372 len
, iocur_top
->len
);
375 buf
= (char *)iocur_top
->data
+ start
;
377 for (i
= start
; i
< start
+len
; i
++)
378 *buf
++ = (char)lrand48();
397 len
= iocur_top
->len
- start
;
399 if (len
+start
> iocur_top
->len
) {
400 dbprintf(_("length (%d) too large for data block size (%d)"),
401 len
, iocur_top
->len
);
404 base
= (char *)iocur_top
->data
+ start
;
406 memset(base
, value
, len
);
409 static struct bw_cmd
{
410 void (*cmdfunc
)(int,int,int,int,int);
422 /* cmd sig min max sh frm to start len */
423 { bwrite_lshift
, "lshift", 2, 0, 3, 1, 0, 0, 2, 3,
424 "[shiftcount] [start] [len]", },
425 { bwrite_rshift
, "rshift", 2, 0, 3, 1, 0, 0, 2, 3,
426 "[shiftcount] [start] [len]", },
427 { bwrite_lrot
, "lrot", 2, 0, 3, 1, 0, 0, 2, 3,
428 "[shiftcount] [start] [len]", },
429 { bwrite_rrot
, "rrot", 2, 0, 3, 1, 0, 0, 2, 3,
430 "[shiftcount] [start] [len]", },
431 { bwrite_seq
, "sequence", 3, 0, 4, 0, 1, 2, 3, 4,
432 "[from] [to] [start] [len]", },
433 { bwrite_random
, "random", 3, 0, 2, 0, 0, 0, 1, 2,
435 { bwrite_fill
, "fill", 1, 1, 3, 1, 0, 0, 2, 3,
436 "num [start] [len]" }
439 #define BWRITE_CMD_MAX (sizeof(bw_cmdtab)/sizeof(bw_cmdtab[0]))
450 /* only allow 1 case, '\' and 3 octal digits (or less) */
452 for (count
= 0; count
< 3; count
++) {
453 if (arg
[count
] == '\0')
456 if ((arg
[count
] < '0') && (arg
[count
] > '7'))
460 for (i
= 0; i
< count
; i
++) {
461 val
|= ((arg
[(count
-1)-i
]-'0')&0x07)<<(i
*3);
469 #define NYBBLE(x) (isdigit(x)?(x-'0'):(tolower(x)-'a'+0xa))
472 * convert_arg allows input in the following forms:
474 * - A string ("ABTB") whose ASCII value is placed in an array in the order
475 * matching the input.
477 * - An even number of hex numbers. If the length is greater than 64 bits,
478 * then the output is an array of bytes whose top nibble is the first hex
479 * digit in the input, the lower nibble is the second hex digit in the
480 * input. UUID entries are entered in this manner.
482 * - A decimal or hexadecimal integer to be used with setbitval().
484 * Numbers that are passed to setbitval() need to be in big endian format and
485 * are adjusted in the buffer so that the first input bit is to be be written to
486 * the first bit in the output.
498 static char *buf
= NULL
;
505 if (bit_length
<= 64)
508 alloc_size
= (bit_length
+ 7) / 8;
510 buf
= xrealloc(buf
, alloc_size
);
511 memset(buf
, 0, alloc_size
);
512 value
= (__u64
*)buf
;
516 /* input a string and output ASCII array of characters */
518 /* zap closing quote if there is one */
519 ostr
= strrchr(arg
+ 1, '\"');
524 for (i
= 0; i
< alloc_size
; i
++) {
528 /* do octal conversion */
530 if (*(ostr
+ 1) >= '0' || *(ostr
+ 1) <= '7') {
531 ret
= convert_oct(ostr
+ 1, &octval
);
542 if (arg
[0] == '#' || ((arg
[0] != '-') && strchr(arg
,'-'))) {
544 * handle hex blocks ie
545 * #00112233445566778899aabbccddeeff
547 * 1122334455667788-99aa-bbcc-ddee-ff00112233445566778899
549 * (but if it starts with "-" assume it's just an integer)
551 int bytes
= bit_length
/ NBBY
;
553 /* is this an array of hec numbers? */
554 if (bit_length
% NBBY
)
557 /* skip leading hash */
561 while (*arg
&& bytes
--) {
566 /* get first nybble */
567 if (!isxdigit((int)*arg
))
569 *rbuf
= NYBBLE((int)*arg
) << 4;
572 /* skip more hyphens */
576 /* get second nybble */
577 if (!isxdigit((int)*arg
))
579 *rbuf
++ |= NYBBLE((int)*arg
);
582 if (bytes
< 0 && *arg
)
588 /* handle decimal / hexadecimal integers */
589 val
= strtoll(arg
, &endp
, 0);
590 /* return if not a clean number */
594 /* Does the value fit into the range of the destination bitfield? */
595 if (bit_length
< 64 && (val
>> bit_length
) > 0)
598 * If the length of the field is not a multiple of a byte, push
599 * the bits up in the field, so the most signicant field bit is
600 * the most significant bit in the byte:
603 * val |----|----|----|----|----|--MM|mmmm|llll|
605 * val |----|----|----|----|----|MMmm|mmll|ll00|
607 offset
= bit_length
% NBBY
;
609 val
<<= (NBBY
- offset
);
612 * convert to big endian and copy into the array
613 * rbuf |----|----|----|----|----|MMmm|mmll|ll00|
615 *value
= cpu_to_be64(val
);
618 * Align the array to point to the field in the array.
619 * rbuf = |MMmm|mmll|ll00|
621 offset
= sizeof(__be64
) - 1 - ((bit_length
- 1) / sizeof(__be64
));
630 const field_t
*fields
,
642 dbprintf(_("usage: write fieldname value\n"));
646 fl
= flist_scan(argv
[0]);
648 dbprintf(_("unable to parse '%s'.\n"), argv
[0]);
652 /* if we're a root field type, go down 1 layer to get field list */
653 if (fields
->name
[0] == '\0') {
654 fa
= &ftattrtab
[fields
->ftyp
];
655 ASSERT(fa
->ftyp
== fields
->ftyp
);
659 /* run down the field list and set offsets into the data */
660 if (!flist_parse(fields
, fl
, iocur_top
->data
, 0)) {
662 dbprintf(_("parsing error\n"));
669 parentoffset
= sfl
->offset
;
673 bit_length
= fsize(sfl
->fld
, iocur_top
->data
, parentoffset
, 0);
674 bit_length
*= fcount(sfl
->fld
, iocur_top
->data
, parentoffset
);
676 /* convert this to a generic conversion routine */
677 /* should be able to handle str, num, or even labels */
679 buf
= convert_arg(argv
[1], bit_length
);
681 dbprintf(_("unable to convert value '%s'.\n"), argv
[1]);
686 setbitval(iocur_top
->data
, sfl
->offset
, bit_length
, buf
);
697 const field_t
*fields
,
705 dbprintf(_("usage (in string mode): write \"string...\"\n"));
709 buf
= convert_arg(argv
[0], (int)((strlen(argv
[0])+1)*8));
710 for (i
= 0; i
< iocur_top
->len
; i
++) {
711 ((char *)iocur_top
->data
)[i
] = *buf
;
716 /* write back to disk */
723 const field_t
*fields
,
733 struct bw_cmd
*cmd
= NULL
;
735 if (argc
<= 1 || argc
> 5)
738 for (i
= 0; i
< BWRITE_CMD_MAX
; i
++) {
739 if (sigcmp(argv
[0], bw_cmdtab
[i
].cmdstr
,
740 bw_cmdtab
[i
].sig_chars
)) {
747 dbprintf(_("write: invalid subcommand\n"));
751 if ((argc
< cmd
->argmin
+ 1) || (argc
> cmd
->argmax
+ 1)) {
752 dbprintf(_("write %s: invalid number of arguments\n"),
757 if (cmd
->shiftcount_arg
&& (cmd
->shiftcount_arg
< argc
))
758 shiftcount
= (int)strtoul(argv
[cmd
->shiftcount_arg
], NULL
, 0);
759 if (cmd
->start_arg
&& (cmd
->start_arg
< argc
))
760 start
= (int)strtoul(argv
[cmd
->start_arg
], NULL
, 0);
761 if (cmd
->len_arg
&& (cmd
->len_arg
< argc
))
762 len
= (int)strtoul(argv
[cmd
->len_arg
], NULL
, 0);
763 if (cmd
->from_arg
&& (cmd
->len_arg
< argc
))
764 from
= (int)strtoul(argv
[cmd
->from_arg
], NULL
, 0);
765 if (cmd
->to_arg
&& (cmd
->len_arg
< argc
))
766 to
= (int)strtoul(argv
[cmd
->to_arg
], NULL
, 0);
768 cmd
->cmdfunc(start
, len
, shiftcount
, from
, to
);
770 /* write back to disk */
776 dbprintf(_("usage: write (in data mode)\n"));
777 for (i
= 0; i
< BWRITE_CMD_MAX
; i
++) {
778 dbprintf(" %-9.9s %s\n",
779 bw_cmdtab
[i
].cmdstr
, bw_cmdtab
[i
].usage
);