NFE - Change default RX ring size from 128 -> 256, Adjust moderation timer.
[dragonfly.git] / sbin / fsdb / fsdbutil.c
blob80abcb4bc34a8dadc923e28e4a2b90bf5a8699e8
1 /* $NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $ */
3 /*
4 * Copyright (c) 1995 John T. Kohl
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
20 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
22 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
23 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28 * POSSIBILITY OF SUCH DAMAGE.
30 * $FreeBSD: src/sbin/fsdb/fsdbutil.c,v 1.9.2.2 2002/03/20 13:39:02 joerg Exp $
31 * $DragonFly: src/sbin/fsdb/fsdbutil.c,v 1.7 2006/04/03 01:58:49 dillon Exp $
34 #include <sys/param.h>
35 #include <ctype.h>
36 #include <err.h>
37 #include <grp.h>
38 #include <pwd.h>
39 #include <string.h>
40 #include <time.h>
42 #include <vfs/ufs/dinode.h>
43 #include <vfs/ufs/fs.h>
45 #include <sys/ioctl.h>
47 #include "fsdb.h"
48 #include "fsck.h"
50 static int charsperline(void);
51 static int printindir(ufs_daddr_t blk, int level, char *bufp);
52 static void printblocks(ino_t inum, struct ufs1_dinode *dp);
54 char **
55 crack(char *line, int *argc)
57 static char *argv[8];
58 int i;
59 char *p, *val;
60 for (p = line, i = 0; p != NULL && i < 8; i++) {
61 while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0')
62 /**/;
63 if (val)
64 argv[i] = val;
65 else
66 break;
68 *argc = i;
69 return argv;
72 int
73 argcount(struct cmdtable *cmdp, int argc, char **argv)
75 if (cmdp->minargc == cmdp->maxargc)
76 warnx("command `%s' takes %u arguments", cmdp->cmd, cmdp->minargc-1);
77 else
78 warnx("command `%s' takes from %u to %u arguments",
79 cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1);
81 warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt);
82 return 1;
85 void
86 printstat(const char *cp, ino_t inum, struct ufs1_dinode *dp)
88 struct group *grp;
89 struct passwd *pw;
90 char *p;
91 time_t t;
93 printf("%s: ", cp);
94 switch (dp->di_mode & IFMT) {
95 case IFDIR:
96 puts("directory");
97 break;
98 case IFREG:
99 puts("regular file");
100 break;
101 case IFBLK:
102 printf("block special (%d,%d)",
103 major(dp->di_rdev), minor(dp->di_rdev));
104 break;
105 case IFCHR:
106 printf("character special (%d,%d)",
107 major(dp->di_rdev), minor(dp->di_rdev));
108 break;
109 case IFLNK:
110 fputs("symlink",stdout);
111 if (dp->di_size > 0 && dp->di_size < MAXSYMLINKLEN &&
112 dp->di_blocks == 0)
113 printf(" to `%.*s'\n", (int) dp->di_size, (char *)dp->di_shortlink);
114 else
115 putchar('\n');
116 break;
117 case IFSOCK:
118 puts("socket");
119 break;
120 case IFIFO:
121 puts("fifo");
122 break;
124 printf("I=%lu MODE=%o SIZE=%qu", (u_long)inum, dp->di_mode, dp->di_size);
125 t = dp->di_mtime;
126 p = ctime(&t);
127 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
128 dp->di_mtimensec);
129 t = dp->di_ctime;
130 p = ctime(&t);
131 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20],
132 dp->di_ctimensec);
133 t = dp->di_atime;
134 p = ctime(&t);
135 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20],
136 dp->di_atimensec);
138 if ((pw = getpwuid(dp->di_uid)))
139 printf("OWNER=%s ", pw->pw_name);
140 else
141 printf("OWNUID=%u ", dp->di_uid);
142 if ((grp = getgrgid(dp->di_gid)))
143 printf("GRP=%s ", grp->gr_name);
144 else
145 printf("GID=%u ", dp->di_gid);
147 printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%x GEN=%x\n", dp->di_nlink, dp->di_flags,
148 dp->di_blocks, dp->di_gen);
153 * Determine the number of characters in a
154 * single line.
157 static int
158 charsperline(void)
160 int columns;
161 char *cp;
162 struct winsize ws;
164 columns = 0;
165 if (ioctl(0, TIOCGWINSZ, &ws) != -1)
166 columns = ws.ws_col;
167 if (columns == 0 && (cp = getenv("COLUMNS")))
168 columns = atoi(cp);
169 if (columns == 0)
170 columns = 80; /* last resort */
171 return (columns);
176 * Recursively print a list of indirect blocks.
178 static int
179 printindir(ufs_daddr_t blk, int level, char *bufp)
181 struct bufarea buf, *bp;
182 char tempbuf[32]; /* enough to print an ufs_daddr_t */
183 int i, j, cpl, charssofar;
184 ufs_daddr_t blkno;
186 if (level == 0) {
187 /* for the final indirect level, don't use the cache */
188 bp = &buf;
189 bp->b_un.b_buf = bufp;
190 bp->b_prev = bp->b_next = bp;
191 initbarea(bp);
193 getblk(bp, blk, sblock.fs_bsize);
194 } else
195 bp = getdatablk(blk, sblock.fs_bsize);
197 cpl = charsperline();
198 for (i = charssofar = 0; i < NINDIR(&sblock); i++) {
199 blkno = bp->b_un.b_indir[i];
200 if (blkno == 0) {
201 if (level == 0)
202 putchar('\n');
203 return 0;
205 j = sprintf(tempbuf, "%d", blkno);
206 if (level == 0) {
207 charssofar += j;
208 if (charssofar >= cpl - 2) {
209 putchar('\n');
210 charssofar = j;
213 fputs(tempbuf, stdout);
214 if (level == 0) {
215 printf(", ");
216 charssofar += 2;
217 } else {
218 printf(" =>\n");
219 if (printindir(blkno, level - 1, bufp) == 0)
220 return 0;
223 if (level == 0)
224 putchar('\n');
225 return 1;
230 * Print the block pointers for one inode.
232 static void
233 printblocks(ino_t inum, struct ufs1_dinode *dp)
235 char *bufp;
236 int i, nfrags;
237 long ndb, offset;
239 printf("Blocks for inode %d:\n", inum);
240 printf("Direct blocks:\n");
241 ndb = howmany(dp->di_size, sblock.fs_bsize);
242 for (i = 0; i < NDADDR; i++) {
243 if (dp->di_db[i] == 0) {
244 putchar('\n');
245 return;
247 if (i > 0)
248 printf(", ");
249 printf("%d", dp->di_db[i]);
250 if (--ndb == 0 && (offset = blkoff(&sblock, dp->di_size)) != 0) {
251 nfrags = numfrags(&sblock, fragroundup(&sblock, offset));
252 printf(" (%d frag%s)", nfrags, nfrags > 1? "s": "");
255 putchar('\n');
256 if (dp->di_ib[0] == 0)
257 return;
259 bufp = malloc((unsigned int)sblock.fs_bsize);
260 if (bufp == 0)
261 errx(EEXIT, "cannot allocate indirect block buffer");
262 printf("Indirect blocks:\n");
263 for (i = 0; i < NIADDR; i++)
264 if (printindir(dp->di_ib[i], i, bufp) == 0)
265 break;
266 free(bufp);
271 checkactive(void)
273 if (!curinode) {
274 warnx("no current inode\n");
275 return 0;
277 return 1;
281 checkactivedir(void)
283 if (!curinode) {
284 warnx("no current inode\n");
285 return 0;
287 if ((curinode->di_mode & IFMT) != IFDIR) {
288 warnx("inode %d not a directory", curinum);
289 return 0;
291 return 1;
295 printactive(int doblocks)
297 if (!checkactive())
298 return 1;
299 switch (curinode->di_mode & IFMT) {
300 case IFDIR:
301 case IFREG:
302 case IFBLK:
303 case IFCHR:
304 case IFLNK:
305 case IFSOCK:
306 case IFIFO:
307 if (doblocks)
308 printblocks(curinum, curinode);
309 else
310 printstat("current inode", curinum, curinode);
311 break;
312 case 0:
313 printf("current inode %d: unallocated inode\n", curinum);
314 break;
315 default:
316 printf("current inode %d: screwy itype 0%o (mode 0%o)?\n",
317 curinum, curinode->di_mode & IFMT, curinode->di_mode);
318 break;
320 return 0;