fsdbutil.c revision 122621
1183724Ssos/* $NetBSD: fsdbutil.c,v 1.2 1995/10/08 23:18:12 thorpej Exp $ */ 2230132Suqs 3183724Ssos/* 4183724Ssos * Copyright (c) 1995 John T. Kohl 5183724Ssos * All rights reserved. 6183724Ssos * 7183724Ssos * Redistribution and use in source and binary forms, with or without 8183724Ssos * modification, are permitted provided that the following conditions 9183724Ssos * are met: 10183724Ssos * 1. Redistributions of source code must retain the above copyright 11183724Ssos * notice, this list of conditions and the following disclaimer. 12183724Ssos * 2. Redistributions in binary form must reproduce the above copyright 13183724Ssos * notice, this list of conditions and the following disclaimer in the 14183724Ssos * documentation and/or other materials provided with the distribution. 15183724Ssos * 3. The name of the author may not be used to endorse or promote products 16183724Ssos * derived from this software without specific prior written permission. 17183724Ssos * 18183724Ssos * THIS SOFTWARE IS PROVIDED BY THE AUTHOR `AS IS'' AND ANY EXPRESS OR 19183724Ssos * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 20183724Ssos * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 21183724Ssos * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 22183724Ssos * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 23183724Ssos * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 24183724Ssos * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 25183724Ssos * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 26183724Ssos * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 27183724Ssos * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 28183724Ssos * POSSIBILITY OF SUCH DAMAGE. 29183724Ssos */ 30183724Ssos 31183724Ssos#ifndef lint 32183724Ssosstatic const char rcsid[] = 33183724Ssos "$FreeBSD: head/sbin/fsdb/fsdbutil.c 122621 2003-11-13 19:08:43Z johan $"; 34183724Ssos#endif /* not lint */ 35183724Ssos 36183724Ssos#include <sys/param.h> 37183724Ssos#include <ctype.h> 38183724Ssos#include <err.h> 39183724Ssos#include <grp.h> 40183724Ssos#include <pwd.h> 41183724Ssos#include <stdint.h> 42183724Ssos#include <string.h> 43183724Ssos#include <time.h> 44183724Ssos#include <timeconv.h> 45183724Ssos 46183724Ssos#include <ufs/ufs/dinode.h> 47183724Ssos#include <ufs/ffs/fs.h> 48183724Ssos 49183724Ssos#include <sys/ioctl.h> 50183724Ssos 51183724Ssos#include "fsdb.h" 52183724Ssos#include "fsck.h" 53183724Ssos 54183724Ssosstatic int charsperline(void); 55200171Smavstatic int printindir(ufs2_daddr_t blk, int level, char *bufp); 56183724Ssosstatic void printblocks(ino_t inum, union dinode *dp); 57183724Ssos 58183724Ssoschar ** 59183724Ssoscrack(char *line, int *argc) 60183724Ssos{ 61183724Ssos static char *argv[8]; 62183724Ssos int i; 63183724Ssos char *p, *val; 64183724Ssos for (p = line, i = 0; p != NULL && i < 8; i++) { 65183724Ssos while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0') 66183724Ssos /**/; 67183724Ssos if (val) 68183724Ssos argv[i] = val; 69183724Ssos else 70183724Ssos break; 71183724Ssos } 72183724Ssos *argc = i; 73183724Ssos return argv; 74183724Ssos} 75183724Ssos 76183724Ssoschar ** 77183724Ssosrecrack(char *line, int *argc, int argc_max) 78281140Smav{ 79183724Ssos static char *argv[8]; 80183724Ssos int i; 81183724Ssos char *p, *val; 82183724Ssos for (p = line, i = 0; p != NULL && i < 8 && i < argc_max - 1; i++) { 83183724Ssos while ((val = strsep(&p, " \t\n")) != NULL && *val == '\0') 84183724Ssos /**/; 85183724Ssos if (val) 86183724Ssos argv[i] = val; 87183724Ssos else 88183724Ssos break; 89183724Ssos } 90183724Ssos argv[i] = argv[i - 1] + strlen(argv[i - 1]) + 1; 91183724Ssos argv[i][strcspn(argv[i], "\n")] = '\0'; 92183724Ssos *argc = i + 1; 93183724Ssos return argv; 94183724Ssos} 95200171Smav 96200171Smavint 97183724Ssosargcount(struct cmdtable *cmdp, int argc, char *argv[]) 98200171Smav{ 99200171Smav if (cmdp->minargc == cmdp->maxargc) 100183724Ssos warnx("command `%s' takes %u arguments, got %u", cmdp->cmd, 101200171Smav cmdp->minargc-1, argc); 102183724Ssos else 103200171Smav warnx("command `%s' takes from %u to %u arguments", 104200171Smav cmdp->cmd, cmdp->minargc-1, cmdp->maxargc-1); 105200171Smav 106183724Ssos warnx("usage: %s: %s", cmdp->cmd, cmdp->helptxt); 107200171Smav return 1; 108200171Smav} 109183724Ssos 110183724Ssosvoid 111183724Ssosprintstat(const char *cp, ino_t inum, union dinode *dp) 112{ 113 struct group *grp; 114 struct passwd *pw; 115 ufs2_daddr_t blocks; 116 int64_t gen; 117 char *p; 118 time_t t; 119 120 printf("%s: ", cp); 121 switch (DIP(dp, di_mode) & IFMT) { 122 case IFDIR: 123 puts("directory"); 124 break; 125 case IFREG: 126 puts("regular file"); 127 break; 128 case IFBLK: 129 printf("block special (%d,%d)", 130 major(DIP(dp, di_rdev)), minor(DIP(dp, di_rdev))); 131 break; 132 case IFCHR: 133 printf("character special (%d,%d)", 134 major(DIP(dp, di_rdev)), minor(DIP(dp, di_rdev))); 135 break; 136 case IFLNK: 137 fputs("symlink",stdout); 138 if (DIP(dp, di_size) > 0 && 139 DIP(dp, di_size) < sblock.fs_maxsymlinklen && 140 DIP(dp, di_blocks) == 0) { 141 if (sblock.fs_magic == FS_UFS1_MAGIC) 142 p = (caddr_t)dp->dp1.di_db; 143 else 144 p = (caddr_t)dp->dp2.di_db; 145 printf(" to `%.*s'\n", (int) DIP(dp, di_size), p); 146 } else { 147 putchar('\n'); 148 } 149 break; 150 case IFSOCK: 151 puts("socket"); 152 break; 153 case IFIFO: 154 puts("fifo"); 155 break; 156 } 157 printf("I=%lu MODE=%o SIZE=%ju", (u_long)inum, DIP(dp, di_mode), 158 (uintmax_t)DIP(dp, di_size)); 159 if (sblock.fs_magic == FS_UFS1_MAGIC) 160 t = _time32_to_time(dp->dp1.di_mtime); 161 else 162 t = _time64_to_time(dp->dp2.di_mtime); 163 p = ctime(&t); 164 printf("\n\tMTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 165 DIP(dp, di_mtimensec)); 166 if (sblock.fs_magic == FS_UFS1_MAGIC) 167 t = _time32_to_time(dp->dp1.di_ctime); 168 else 169 t = _time64_to_time(dp->dp2.di_ctime); 170 p = ctime(&t); 171 printf("\n\tCTIME=%15.15s %4.4s [%d nsec]", &p[4], &p[20], 172 DIP(dp, di_ctimensec)); 173 if (sblock.fs_magic == FS_UFS1_MAGIC) 174 t = _time32_to_time(dp->dp1.di_atime); 175 else 176 t = _time64_to_time(dp->dp2.di_atime); 177 p = ctime(&t); 178 printf("\n\tATIME=%15.15s %4.4s [%d nsec]\n", &p[4], &p[20], 179 DIP(dp, di_atimensec)); 180 181 if ((pw = getpwuid(DIP(dp, di_uid)))) 182 printf("OWNER=%s ", pw->pw_name); 183 else 184 printf("OWNUID=%u ", DIP(dp, di_uid)); 185 if ((grp = getgrgid(DIP(dp, di_gid)))) 186 printf("GRP=%s ", grp->gr_name); 187 else 188 printf("GID=%u ", DIP(dp, di_gid)); 189 190 blocks = DIP(dp, di_blocks); 191 gen = DIP(dp, di_gen); 192 printf("LINKCNT=%hd FLAGS=%#x BLKCNT=%jx GEN=%jx\n", DIP(dp, di_nlink), 193 DIP(dp, di_flags), (intmax_t)blocks, (intmax_t)gen); 194} 195 196 197/* 198 * Determine the number of characters in a 199 * single line. 200 */ 201 202static int 203charsperline(void) 204{ 205 int columns; 206 char *cp; 207 struct winsize ws; 208 209 columns = 0; 210 if (ioctl(0, TIOCGWINSZ, &ws) != -1) 211 columns = ws.ws_col; 212 if (columns == 0 && (cp = getenv("COLUMNS"))) 213 columns = atoi(cp); 214 if (columns == 0) 215 columns = 80; /* last resort */ 216 return (columns); 217} 218 219 220/* 221 * Recursively print a list of indirect blocks. 222 */ 223static int 224printindir(ufs2_daddr_t blk, int level, char *bufp) 225{ 226 struct bufarea buf, *bp; 227 char tempbuf[32]; /* enough to print an ufs2_daddr_t */ 228 int i, j, cpl, charssofar; 229 ufs2_daddr_t blkno; 230 231 if (level == 0) { 232 /* for the final indirect level, don't use the cache */ 233 bp = &buf; 234 bp->b_un.b_buf = bufp; 235 bp->b_prev = bp->b_next = bp; 236 initbarea(bp); 237 238 getblk(bp, blk, sblock.fs_bsize); 239 } else 240 bp = getdatablk(blk, sblock.fs_bsize); 241 242 cpl = charsperline(); 243 for (i = charssofar = 0; i < NINDIR(&sblock); i++) { 244 if (sblock.fs_magic == FS_UFS1_MAGIC) 245 blkno = bp->b_un.b_indir1[i]; 246 else 247 blkno = bp->b_un.b_indir2[i]; 248 if (blkno == 0) { 249 if (level == 0) 250 putchar('\n'); 251 return 0; 252 } 253 j = sprintf(tempbuf, "%jd", (intmax_t)blkno); 254 if (level == 0) { 255 charssofar += j; 256 if (charssofar >= cpl - 2) { 257 putchar('\n'); 258 charssofar = j; 259 } 260 } 261 fputs(tempbuf, stdout); 262 if (level == 0) { 263 printf(", "); 264 charssofar += 2; 265 } else { 266 printf(" =>\n"); 267 if (printindir(blkno, level - 1, bufp) == 0) 268 return 0; 269 } 270 } 271 if (level == 0) 272 putchar('\n'); 273 return 1; 274} 275 276 277/* 278 * Print the block pointers for one inode. 279 */ 280static void 281printblocks(ino_t inum, union dinode *dp) 282{ 283 char *bufp; 284 int i, nfrags; 285 long ndb, offset; 286 ufs2_daddr_t blkno; 287 288 printf("Blocks for inode %d:\n", inum); 289 printf("Direct blocks:\n"); 290 ndb = howmany(DIP(dp, di_size), sblock.fs_bsize); 291 for (i = 0; i < NDADDR; i++) { 292 if (DIP(dp, di_db[i]) == 0) { 293 putchar('\n'); 294 return; 295 } 296 if (i > 0) 297 printf(", "); 298 blkno = DIP(dp, di_db[i]); 299 printf("%jd", (intmax_t)blkno); 300 if (--ndb == 0 && (offset = blkoff(&sblock, DIP(dp, di_size))) != 0) { 301 nfrags = numfrags(&sblock, fragroundup(&sblock, offset)); 302 printf(" (%d frag%s)", nfrags, nfrags > 1? "s": ""); 303 } 304 } 305 putchar('\n'); 306 if (DIP(dp, di_ib[0]) == 0) 307 return; 308 309 bufp = malloc((unsigned int)sblock.fs_bsize); 310 if (bufp == 0) 311 errx(EEXIT, "cannot allocate indirect block buffer"); 312 printf("Indirect blocks:\n"); 313 for (i = 0; i < NIADDR; i++) 314 if (printindir(DIP(dp, di_ib[i]), i, bufp) == 0) 315 break; 316 free(bufp); 317} 318 319 320int 321checkactive(void) 322{ 323 if (!curinode) { 324 warnx("no current inode\n"); 325 return 0; 326 } 327 return 1; 328} 329 330int 331checkactivedir(void) 332{ 333 if (!curinode) { 334 warnx("no current inode\n"); 335 return 0; 336 } 337 if ((DIP(curinode, di_mode) & IFMT) != IFDIR) { 338 warnx("inode %d not a directory", curinum); 339 return 0; 340 } 341 return 1; 342} 343 344int 345printactive(int doblocks) 346{ 347 if (!checkactive()) 348 return 1; 349 switch (DIP(curinode, di_mode) & IFMT) { 350 case IFDIR: 351 case IFREG: 352 case IFBLK: 353 case IFCHR: 354 case IFLNK: 355 case IFSOCK: 356 case IFIFO: 357 if (doblocks) 358 printblocks(curinum, curinode); 359 else 360 printstat("current inode", curinum, curinode); 361 break; 362 case 0: 363 printf("current inode %d: unallocated inode\n", curinum); 364 break; 365 default: 366 printf("current inode %d: screwy itype 0%o (mode 0%o)?\n", 367 curinum, DIP(curinode, di_mode) & IFMT, DIP(curinode, di_mode)); 368 break; 369 } 370 return 0; 371} 372