]>
git.ipfire.org Git - thirdparty/xfsprogs-dev.git/blob - db/io.c
2 * Copyright (c) 2000 Silicon Graphics, Inc. All Rights Reserved.
4 * This program is free software; you can redistribute it and/or modify it
5 * under the terms of version 2 of the GNU General Public License as
6 * published by the Free Software Foundation.
8 * This program is distributed in the hope that it would be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
12 * Further, this software is distributed without any warranty that it is
13 * free of the rightful claim of any third person regarding infringement
14 * or the like. Any license provided herein, whether implied or
15 * otherwise, applies only to this software file. Patent licenses, if
16 * any, provided herein do not apply to combinations of this program with
17 * other software, or any other product whatsoever.
19 * You should have received a copy of the GNU General Public License along
20 * with this program; if not, write the Free Software Foundation, Inc., 59
21 * Temple Place - Suite 330, Boston MA 02111-1307, USA.
23 * Contact information: Silicon Graphics, Inc., 1600 Amphitheatre Pkwy,
24 * Mountain View, CA 94043, or:
28 * For further information regarding this notice, see:
30 * http://oss.sgi.com/projects/GenInfo/SGIGPLNoticeExplan/
47 static int pop_f(int argc
, char **argv
);
48 static void pop_help(void);
49 static int push_f(int argc
, char **argv
);
50 static void push_help(void);
51 static int stack_f(int argc
, char **argv
);
52 static void stack_help(void);
53 static int forward_f(int argc
, char **argv
);
54 static void forward_help(void);
55 static int back_f(int argc
, char **argv
);
56 static void back_help(void);
57 static int ring_f(int argc
, char **argv
);
58 static void ring_help(void);
60 static const cmdinfo_t pop_cmd
=
61 { "pop", NULL
, pop_f
, 0, 0, 0, NULL
,
62 "pop location from the stack", pop_help
};
63 static const cmdinfo_t push_cmd
=
64 { "push", NULL
, push_f
, 0, 2, 0, "[command]",
65 "push location to the stack", push_help
};
66 static const cmdinfo_t stack_cmd
=
67 { "stack", NULL
, stack_f
, 0, 0, 0, NULL
,
68 "view the location stack", stack_help
};
69 static const cmdinfo_t forward_cmd
=
70 { "forward", "f", forward_f
, 0, 0, 0, NULL
,
71 "move forward to next entry in the position ring", forward_help
};
72 static const cmdinfo_t back_cmd
=
73 { "back", "b", back_f
, 0, 0, 0, NULL
,
74 "move to the previous location in the position ring", back_help
};
75 static const cmdinfo_t ring_cmd
=
76 { "ring", NULL
, ring_f
, 0, 1, 0, NULL
,
77 "show position ring or move to a specific entry", ring_help
};
84 #define RING_ENTRIES 20
85 static iocur_t iocur_ring
[RING_ENTRIES
];
86 static int ring_head
= -1;
87 static int ring_tail
= -1;
88 static int ring_current
= -1;
93 add_command(&pop_cmd
);
94 add_command(&push_cmd
);
95 add_command(&stack_cmd
);
96 add_command(&forward_cmd
);
97 add_command(&back_cmd
);
98 add_command(&ring_cmd
);
106 if (iocur_top
== NULL
|| off
+ len
> BBTOB(iocur_top
->blen
))
107 dbprintf("can't set block offset to %d\n", off
);
109 iocur_top
->boff
= off
;
110 iocur_top
->off
= ((xfs_off_t
)iocur_top
->bb
<< BBSHIFT
) + off
;
111 iocur_top
->len
= len
;
112 iocur_top
->data
= (void *)((char *)iocur_top
->buf
+ off
);
120 dbprintf("can't pop anything from I/O stack\n");
124 xfree(iocur_top
->buf
);
125 if (--iocur_sp
>= 0) {
126 iocur_top
= iocur_base
+ iocur_sp
;
127 cur_typ
= iocur_top
->typ
;
129 iocur_top
= iocur_base
;
149 " Changes the address and data type to the first entry on the stack.\n"
161 dbprintf("%s\n", tag
);
162 dbprintf("\tbyte offset %lld, length %d\n", ioc
->off
, ioc
->len
);
163 dbprintf("\tbuffer block %lld (fsbno %lld), %d bb%s\n", ioc
->bb
,
164 (xfs_dfsbno_t
)XFS_DADDR_TO_FSB(mp
, ioc
->bb
), ioc
->blen
,
165 ioc
->blen
== 1 ? "" : "s");
166 if (ioc
->use_bbmap
) {
167 dbprintf("\tblock map");
168 for (i
= 0; i
< ioc
->blen
; i
++)
169 dbprintf(" %d:%lld", i
, ioc
->bbmap
.b
[i
]);
172 dbprintf("\tinode %lld, dir inode %lld, type %s\n", ioc
->ino
,
173 ioc
->dirino
, ioc
->typ
== NULL
? "none" : ioc
->typ
->name
);
182 if (ring_current
== -1) {
183 dbprintf("no entries in location ring.\n");
187 dbprintf(" type bblock bblen fsbno inode\n");
191 ioc
= &iocur_ring
[i
];
192 if (i
== ring_current
)
197 dbprintf("%-7.7s %8lld %5d %8lld %9lld\n",
198 ioc
->typ
== NULL
? "none" : ioc
->typ
->name
,
201 (xfs_dfsbno_t
)XFS_DADDR_TO_FSB(mp
, ioc
->bb
),
208 i
= (i
+(RING_ENTRIES
-1))%RING_ENTRIES
;
216 if (iocur_sp
+ 1 >= iocur_len
) {
217 iocur_base
= xrealloc(iocur_base
,
218 sizeof(*iocur_base
) * (iocur_len
+ 1));
222 iocur_top
= iocur_base
+ iocur_sp
;
223 memset(iocur_top
, 0, sizeof(*iocur_base
));
224 iocur_top
->ino
= iocur_sp
> 0 ? iocur_top
[-1].ino
: NULLFSINO
;
225 iocur_top
->dirino
= iocur_sp
> 0 ? iocur_top
[-1].dirino
: NULLFSINO
;
226 iocur_top
->mode
= iocur_sp
> 0 ? iocur_top
[-1].mode
: 0;
238 /* check we can execute command */
239 ct
= find_command(argv
[1]);
241 dbprintf("no such command %s\n", argv
[1]);
245 dbprintf("no push form allowed for %s\n", argv
[1]);
250 /* save current state */
252 if (iocur_top
[-1].typ
&& iocur_top
[-1].typ
->typnm
== TYP_INODE
)
253 set_cur_inode(iocur_top
[-1].ino
);
255 set_cur(iocur_top
[-1].typ
, iocur_top
[-1].bb
,
256 iocur_top
[-1].blen
, DB_RING_IGN
,
257 iocur_top
[-1].use_bbmap
? &iocur_top
[-1].bbmap
: NULL
);
259 /* run requested command */
261 (void)command(argc
-1, argv
+1);
270 " Allows you to push the current address and data type on the stack for\n"
271 " later return. 'push' also accepts an additional command to execute after\n"
272 " storing the current address (ex: 'push a rootino' from the superblock).\n"
277 /* move forward through the ring */
284 if (ring_current
== -1) {
285 dbprintf("ring is empty\n");
288 if (ring_current
== ring_head
) {
289 dbprintf("no further entries\n");
293 ring_current
= (ring_current
+1)%RING_ENTRIES
;
295 set_cur(iocur_ring
[ring_current
].typ
,
296 iocur_ring
[ring_current
].bb
,
297 iocur_ring
[ring_current
].blen
,
299 iocur_ring
[ring_current
].use_bbmap
?
300 &iocur_ring
[ring_current
].bbmap
: NULL
);
310 " The 'forward' ('f') command moves to the next location in the position\n"
311 " ring, updating the current position and data type. If the current location\n"
312 " is the top entry in the ring, then the 'forward' command will have\n"
318 /* move backwards through the ring */
325 if (ring_current
== -1) {
326 dbprintf("ring is empty\n");
329 if (ring_current
== ring_tail
) {
330 dbprintf("no previous entries\n");
334 ring_current
= (ring_current
+(RING_ENTRIES
-1))%RING_ENTRIES
;
336 set_cur(iocur_ring
[ring_current
].typ
,
337 iocur_ring
[ring_current
].bb
,
338 iocur_ring
[ring_current
].blen
,
340 iocur_ring
[ring_current
].use_bbmap
?
341 &iocur_ring
[ring_current
].bbmap
: NULL
);
351 " The 'back' ('b') command moves to the previous location in the position\n"
352 " ring, updating the current position and data type. If the current location\n"
353 " is the last entry in the ring, then the 'back' command will have no effect.\n"
358 /* show or go to specific point in ring */
371 index
= (int)strtoul(argv
[0], NULL
, 0);
372 if (index
< 0 || index
>= RING_ENTRIES
)
373 dbprintf("invalid entry: %d\n", index
);
375 ring_current
= index
;
377 set_cur(iocur_ring
[index
].typ
,
378 iocur_ring
[index
].bb
,
379 iocur_ring
[index
].blen
,
381 iocur_ring
[index
].use_bbmap
? &iocur_ring
[index
].bbmap
: NULL
);
391 " The position ring automatically keeps track of each disk location and\n"
392 " structure type for each change of position you make during your xfs_db\n"
393 " session. The last %d most recent entries are kept in the ring.\n"
395 " To display the current list of ring entries type 'ring' by itself on\n"
396 " the command line. The entry highlighted by an asterisk ('*') is the\n"
399 " To move to another entry in the ring type 'ring <num>' where <num> is\n"
400 " your desired entry from the ring position list.\n"
402 " You may also use the 'forward' ('f') or 'back' ('b') commands to move\n"
403 " to the previous or next entry in the ring, respectively.\n"
405 " Note: Unlike the 'stack', 'push' and 'pop' commands, the ring tracks your\n"
406 " location implicitly. Use the 'push' and 'pop' commands if you wish to\n"
407 " store a specific location explicitly for later return.\n"
416 if (ring_head
== -1) {
417 /* only get here right after startup */
421 iocur_ring
[0] = *iocur_top
;
423 if (ring_current
== ring_head
) {
424 ring_head
= (ring_head
+1)%RING_ENTRIES
;
425 iocur_ring
[ring_head
] = *iocur_top
;
426 if (ring_head
== ring_tail
)
427 ring_tail
= (ring_tail
+1)%RING_ENTRIES
;
428 ring_current
= ring_head
;
430 ring_current
= (ring_current
+1)%RING_ENTRIES
;
431 iocur_ring
[ring_current
] = *iocur_top
;
447 int rval
= EINVAL
; /* initialize for zero `count' case */
449 for (j
= 0; j
< count
; j
+= bbmap
? 1 : count
) {
452 if (lseek64(xfsargs
.dfd
, bbno
<< BBSHIFT
, SEEK_SET
) < 0) {
454 dbprintf("can't seek in filesystem at bb %lld\n", bbno
);
457 c
= BBTOB(bbmap
? 1 : count
);
458 i
= (int)write(xfsargs
.dfd
, (char *)bufp
+ BBTOB(j
), c
);
492 for (j
= 0; j
< count
; j
+= bbmap
? 1 : count
) {
495 if (lseek64(xfsargs
.dfd
, bbno
<< BBSHIFT
, SEEK_SET
) < 0) {
497 dbprintf("can't seek in filesystem at bb %lld\n", bbno
);
502 c
= BBTOB(bbmap
? 1 : count
);
503 i
= (int)read(xfsargs
.dfd
, (char *)buf
+ BBTOB(j
), c
);
531 dbprintf("nothing to write\n");
534 ret
= write_bbs(iocur_top
->bb
, iocur_top
->blen
, iocur_top
->buf
,
535 iocur_top
->use_bbmap
? &iocur_top
->bbmap
: NULL
);
537 dbprintf("incomplete write, block: %lld\n",
538 (iocur_base
+ iocur_sp
)->bb
);
540 dbprintf("write error: %s\n", strerror(ret
));
541 /* re-read buffer from disk */
542 ret
= read_bbs(iocur_top
->bb
, iocur_top
->blen
, &iocur_top
->buf
,
543 iocur_top
->use_bbmap
? &iocur_top
->bbmap
: NULL
);
545 dbprintf("incomplete read, block: %lld\n",
546 (iocur_base
+ iocur_sp
)->bb
);
548 dbprintf("read error: %s\n", strerror(ret
));
564 dbprintf("set_cur no stack element to set\n");
570 printf("xfs_db got a bbmap for %lld\n", (long long)d
);
572 ino
= iocur_top
->ino
;
573 dirino
= iocur_top
->dirino
;
574 mode
= iocur_top
->mode
;
577 if (read_bbs(d
, c
, &iocur_top
->buf
, bbmap
))
582 iocur_top
->data
= iocur_top
->buf
;
583 iocur_top
->len
= BBTOB(c
);
584 iocur_top
->off
= d
<< BBSHIFT
;
585 iocur_top
->typ
= cur_typ
= t
;
586 iocur_top
->ino
= ino
;
587 iocur_top
->dirino
= dirino
;
588 iocur_top
->mode
= mode
;
589 if (iocur_top
->use_bbmap
= (bbmap
!= NULL
))
590 iocur_top
->bbmap
= *bbmap
;
592 /* store location in ring */
602 " The stack is used to explicitly store your location and data type\n"
603 " for later return. The 'push' operation stores the current address\n"
604 " and type on the stack, the 'pop' operation returns you to the\n"
605 " position and datatype of the top entry on the stack.\n"
607 " The 'stack' allows explicit location saves, see 'ring' for implicit\n"
608 " position tracking.\n"
622 for (i
= iocur_sp
; i
>= 0; i
--) {
623 sprintf(tagbuf
, "%d: ", i
);
624 print_iocur(tagbuf
, &iocur_base
[i
]);