//PAGEBREAK! // Allocate a new inode with the given type on device dev. // A free inode has a type of zero. struct inode* ialloc(uint dev, short type) { int inum; struct buf *bp; struct dinode *dip; struct superblock sb; readsb(dev, &sb); for(inum = 1; inum < sb.ninodes; inum++){ bp = bread(dev, IBLOCK(inum)); dip = (struct dinode*)bp->data + inum%IPB; if(dip->type == 0){ // a free inode memset(dip, 0, sizeof(*dip)); dip->type = type; log_write(bp); // mark it allocated on the disk brelse(bp); return iget(dev, inum); } brelse(bp); } panic("ialloc: no inodes"); return 0; }
// Allocate a disk block. static uint balloc(uint dev) { int b, bi, m, bound; struct buf *bp; struct superblock sb; bp = 0; readsb(dev, &sb); for(b = 0; b < sb.size; b += BPB){ bp = bread(dev, BBLOCK(b, sb.ninodes)); if(b+BPB > sb.size){ //last bitmap block bound = sb.size % BPB; } else { bound = BPB; } for(bi = 0; bi < bound; bi++){ m = 1 << (bi % 8); if((bp->data[bi/8] & m) == 0){ // Is block free? bp->data[bi/8] |= m; // Mark block in use on disk. bwrite(bp); brelse(bp); return b + bi; } } brelse(bp); } //panic("balloc: out of blocks"); return 0; }
static uint log_balloc(uint dev) { int b, bi, m, i; struct superblock sb; readsb(dev, &sb); for(b = 0; b < sb.size; b += BPB){ for(i = 0; i < b_index; i++) if(bp[i]->sector == BBLOCK(b, sb.ninodes)) { for(bi = 0; bi < BPB; bi++){ m = 1 << (bi % 8); if((bp[i]->data[bi/8] & m) == 0){ bp[i]->data[bi/8] |= m; return b + bi; } } } bp[b_index] = bread(dev, BBLOCK(b, sb.ninodes)); for(bi = 0; bi < BPB; bi++){ m = 1 << (bi % 8); if((bp[b_index]->data[bi/8] & m) == 0){ bp[b_index]->data[bi/8] |= m; b_index++; return b + bi; } } brelse(bp[b_index]); } panic("balloc: out of blocks"); }
// Allocate a zeroed disk block. static uint balloc(uint dev) { int b, bi, m; struct buf *bp; struct superblock sb; bp = 0; readsb(dev, &sb); for(b = 0; b < sb.size; b += BPB){ bp = bread(dev, BBLOCK(b, sb.ninodes)); for(bi = 0; bi < BPB && b + bi < sb.size; bi++){ m = 1 << (bi % 8); if((bp->data[bi/8] & m) == 0){ // Is block free? bp->data[bi/8] |= m; // Mark block in use. log_write(bp); brelse(bp); bzero(dev, b + bi); return b + bi; } } brelse(bp); } panic("balloc: out of blocks"); }
// Allocate a disk block. static uint balloc(uint dev) { int b, bi, m, bound; struct buf *bp; struct superblock sb; bp = 0; readsb(dev, &sb); // read superblock into sb for(b = 0; b < sb.size; b += BPB){ // loop through all available blocks // Return a B_BUSY buf with the contents of the indicated disk sector. bp = bread(dev, BBLOCK(b, sb.ninodes)); // get inode bitmap if(b+BPB > sb.size){ //last bitmap block bound = sb.size % BPB; } else { bound = BPB; } for(bi = 0; bi < bound; bi++){ // loop through all inode bitmap m = 1 << (bi % 8); if((bp->data[bi/8] & m) == 0){ // Is block free? bp->data[bi/8] |= m; // Mark block in use on disk. bwrite(bp); brelse(bp); return b + bi; } } brelse(bp); } //panic("balloc: out of blocks"); return 0; }
static void nand_read_buf(struct mtd_info *mtd, uint8_t *buf, int len) { struct nand_chip *chip = mtd->priv; readsl(chip->IO_ADDR_R, buf, (len >> 2)); if (len & 3) readsb(chip->IO_ADDR_R, buf + (len & ~0x3), (len & 3)); }
void changePartition(int partition){ //TODO: CHECK! currentPartition = partition; superBlockOffset = mbr.partitions[partition].offset; //??? cprintf("off:%d\n",mbr.partitions[partition].offset); readsb(ROOTDEV, &sb); //??? }
void iinit(int dev) { initlock(&icache.lock, "icache"); readsb(dev, &sb); cprintf("sb: size %d nblocks %d ninodes %d nlog %d logstart %d inodestart %d bmap start %d\n", sb.size, sb.nblocks, sb.ninodes, sb.nlog, sb.logstart, sb.inodestart, sb.bmapstart); }
int cadence_qspi_apb_indirect_read_execute(struct cadence_spi_platdata *plat, unsigned int n_rx, u8 *rxbuf) { unsigned int remaining = n_rx; unsigned int bytes_to_read = 0; int ret; writel(n_rx, plat->regbase + CQSPI_REG_INDIRECTRDBYTES); /* Start the indirect read transfer */ writel(CQSPI_REG_INDIRECTRD_START, plat->regbase + CQSPI_REG_INDIRECTRD); while (remaining > 0) { ret = cadence_qspi_wait_for_data(plat); if (ret < 0) { printf("Indirect write timed out (%i)\n", ret); goto failrd; } bytes_to_read = ret; while (bytes_to_read != 0) { bytes_to_read *= CQSPI_FIFO_WIDTH; bytes_to_read = bytes_to_read > remaining ? remaining : bytes_to_read; /* Handle non-4-byte aligned access to avoid data abort. */ if (((uintptr_t)rxbuf % 4) || (bytes_to_read % 4)) readsb(plat->ahbbase, rxbuf, bytes_to_read); else readsl(plat->ahbbase, rxbuf, bytes_to_read >> 2); rxbuf += bytes_to_read; remaining -= bytes_to_read; bytes_to_read = cadence_qspi_get_rd_sram_level(plat); } } /* Check indirect done status */ ret = wait_for_bit("QSPI", plat->regbase + CQSPI_REG_INDIRECTRD, CQSPI_REG_INDIRECTRD_DONE, 1, 10, 0); if (ret) { printf("Indirect read completion error (%i)\n", ret); goto failrd; } /* Clear indirect completion status */ writel(CQSPI_REG_INDIRECTRD_DONE, plat->regbase + CQSPI_REG_INDIRECTRD); return 0; failrd: /* Cancel the indirect read */ writel(CQSPI_REG_INDIRECTRD_CANCEL, plat->regbase + CQSPI_REG_INDIRECTRD); return ret; }
static inline int s3c2410_udc_read_packet(int fifo, u8 *buf, struct s3c2410_request *req, unsigned avail) { unsigned len; len = min(req->req.length - req->req.actual, avail); req->req.actual += len; readsb(fifo + base_addr, buf, len); return len; }
void initlog(void) { if (sizeof(struct logheader) >= BSIZE) panic("initlog: too big logheader"); struct superblock sb; initlock(&log.lock, "log"); readsb(ROOTDEV, &sb); log.start = sb.size - sb.nlog; log.size = sb.nlog; log.dev = ROOTDEV; recover_from_log(); }
void initlog(int dev) { if (sizeof(struct logheader) >= BSIZE) panic("initlog: too big logheader"); struct superblock sb; initlock(&log.lock, "log"); readsb(dev, &sb); log.start = sb.logstart; log.size = sb.nlog; log.dev = dev; recover_from_log(); }
// Free a disk block. static void bfree(int dev, uint b) { struct buf *bp; int bi, m; readsb(dev, &sb); bp = bread(dev, BBLOCK(b, sb)); bi = b % BPB; m = 1 << (bi % 8); if((bp->data[bi/8] & m) == 0) panic("freeing free block"); bp->data[bi/8] &= ~m; log_write(bp); brelse(bp); }
// Free a disk block. static void bfree(int dev, uint b) { struct buf *bp; struct superblock sb; int bi, m; bzero(dev, b); readsb(dev, &sb); bp = bread(dev, BBLOCK(b, sb.ninodes)); bi = b % BPB; m = 1 << (bi % 8); if((bp->data[bi/8] & m) == 0) panic("freeing free block"); bp->data[bi/8] &= ~m; // Mark block free on disk. bwrite(bp); brelse(bp); }
int iinit(struct proc* p, int dev) { struct inode* rootNode; struct superblock sb; // TODO: change ot iterate over all partitions cprintf("kernal by Asaf and Ilana \n"); initlock(&icache.lock, "icache"); rootNode = p->cwd; // acquire(&icache.lock); initMbr(dev); printMBR(&mbrI); cprintf("booting from %d \n", bootfrom); if (bootfrom == -1) { panic("no bootable partition"); } rootNode->part = &(partitions[bootfrom]); int i; for (i = 0; i < NPARTITIONS; i++) { readsb(dev, i); sb = sbs[i]; cprintf("sb: offset %d size %d nblocks %d ninodes %d nlog %d logstart %d inodestart %d bmap start %d\n", sb.offset, sb.size, sb.nblocks, sb.ninodes, sb.nlog, sb.logstart, sb.inodestart, sb.bmapstart); } // set root inode // release(&icache.lock); // cprintf("root node init %d \n",rootNode->part->offset); return bootfrom; }
static int s3c2410_udc_read_fifo_crq(struct usb_ctrlrequest *crq) { unsigned char *outbuf = (unsigned char *)crq; int bytes_read = 0; udc_write(0, S3C2410_UDC_INDEX_REG); bytes_read = s3c2410_udc_fifo_count_out(); dprintk(DEBUG_NORMAL, "%s: fifo_count=%d\n", __func__, bytes_read); if (bytes_read > sizeof(struct usb_ctrlrequest)) bytes_read = sizeof(struct usb_ctrlrequest); readsb(S3C2410_UDC_EP0_FIFO_REG + base_addr, outbuf, bytes_read); dprintk(DEBUG_VERBOSE, "%s: len=%d %02x:%02x {%x,%x,%x}\n", __func__, bytes_read, crq->bRequest, crq->bRequestType, crq->wValue, crq->wIndex, crq->wLength); return bytes_read; }
// Allocate a disk block. static uint j_balloc(uint dev) { int b, bi, m, i; //struct buf *bp; struct superblock sb; // bp = 0; readsb(dev, &sb); for(b = 0; b < sb.size; b += BPB){ /* check in dirty blocks */ for(i = 0; i < b_index; i++) if(bp[i]->sector == BBLOCK(b, sb.ninodes)) { for(bi = 0; bi < BPB; bi++){ m = 1 << (bi % 8); if((bp[i]->data[bi/8] & m) == 0){ // Is block free? bp[i]->data[bi/8] |= m; // Mark block in use on disk. return b + bi; } } } /* load new block out of mem */ bp[b_index] = bread(dev, BBLOCK(b, sb.ninodes)); for(bi = 0; bi < BPB; bi++){ m = 1 << (bi % 8); if((bp[b_index]->data[bi/8] & m) == 0){ // Is block free? bp[b_index]->data[bi/8] |= m; // Mark block in use on disk. /* keep dirty around, move index to next*/ b_index++; return b + bi; } } // panic("eh"); brelse(bp[b_index]); } panic("balloc: out of blocks"); }
int setup(const char *dev) { long cg, asked, i; long bmapsize; struct disklabel *lp; off_t sizepb; struct stat statb; struct m_ext2fs proto; int doskipclean; u_int64_t maxfilesize; havesb = 0; fswritefd = -1; doskipclean = skipclean; if (stat(dev, &statb) < 0) { printf("Can't stat %s: %s\n", dev, strerror(errno)); return 0; } if (!S_ISCHR(statb.st_mode)) { pfatal("%s is not a character device", dev); if (reply("CONTINUE") == 0) return 0; } if ((fsreadfd = open(dev, O_RDONLY)) < 0) { printf("Can't open %s: %s\n", dev, strerror(errno)); return 0; } if (preen == 0) printf("** %s", dev); if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { fswritefd = -1; if (preen) pfatal("NO WRITE ACCESS"); printf(" (NO WRITE)"); } if (preen == 0) printf("\n"); fsmodified = 0; lfdir = 0; initbarea(&sblk); initbarea(&asblk); sblk.b_un.b_buf = malloc(SBSIZE); asblk.b_un.b_buf = malloc(SBSIZE); if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) errexit("cannot allocate space for superblock"); if ((lp = getdisklabel(NULL, fsreadfd)) != NULL) dev_bsize = secsize = lp->d_secsize; else dev_bsize = secsize = DEV_BSIZE; /* * Read in the superblock, looking for alternates if necessary */ if (readsb(1) == 0) { if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) return 0; if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) return 0; for (cg = 1; cg < proto.e2fs_ncg; cg++) { bflag = EXT2_FSBTODB(&proto, cg * proto.e2fs.e2fs_bpg + proto.e2fs.e2fs_first_dblock); if (readsb(0) != 0) break; } if (cg >= proto.e2fs_ncg) { printf("%s %s\n%s %s\n%s %s\n", "SEARCH FOR ALTERNATE SUPER-BLOCK", "FAILED. YOU MUST USE THE", "-b OPTION TO FSCK_FFS TO SPECIFY THE", "LOCATION OF AN ALTERNATE", "SUPER-BLOCK TO SUPPLY NEEDED", "INFORMATION; SEE fsck_ext2fs(8)."); return 0; } doskipclean = 0; pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); } if (debug) printf("state = %d\n", sblock.e2fs.e2fs_state); if (sblock.e2fs.e2fs_state == E2FS_ISCLEAN) { if (doskipclean) { pwarn("%sile system is clean; not checking\n", preen ? "f" : "** F"); return -1; } if (!preen) pwarn("** File system is already clean\n"); } maxfsblock = sblock.e2fs.e2fs_bcount; maxino = sblock.e2fs_ncg * sblock.e2fs.e2fs_ipg; sizepb = sblock.e2fs_bsize; maxfilesize = sblock.e2fs_bsize * EXT2FS_NDADDR - 1; for (i = 0; i < EXT2FS_NIADDR; i++) { sizepb *= EXT2_NINDIR(&sblock); maxfilesize += sizepb; } /* * Check and potentially fix certain fields in the super block. */ if (/* (sblock.e2fs.e2fs_rbcount < 0) || */ (sblock.e2fs.e2fs_rbcount > sblock.e2fs.e2fs_bcount)) { pfatal("IMPOSSIBLE RESERVED BLOCK COUNT=%d IN SUPERBLOCK", sblock.e2fs.e2fs_rbcount); if (reply("SET TO DEFAULT") == 1) { sblock.e2fs.e2fs_rbcount = sblock.e2fs.e2fs_bcount * MINFREE / 100; sbdirty(); dirty(&asblk); } } if (sblock.e2fs.e2fs_bpg != sblock.e2fs.e2fs_fpg) { pfatal("WRONG FPG=%d (BPG=%d) IN SUPERBLOCK", sblock.e2fs.e2fs_fpg, sblock.e2fs.e2fs_bpg); return 0; } if (asblk.b_dirty && !bflag) { copyback_sb(&asblk); flush(fswritefd, &asblk); } /* * read in the summary info. */ sblock.e2fs_gd = malloc(sblock.e2fs_ngdb * sblock.e2fs_bsize); if (sblock.e2fs_gd == NULL) errexit("out of memory"); asked = 0; for (i = 0; i < sblock.e2fs_ngdb; i++) { if (bread(fsreadfd, (char *)&sblock.e2fs_gd[i * sblock.e2fs_bsize / sizeof(struct ext2_gd)], EXT2_FSBTODB(&sblock, ((sblock.e2fs_bsize > 1024) ? 0 : 1) + i + 1), sblock.e2fs_bsize) != 0 && !asked) { pfatal("BAD SUMMARY INFORMATION"); if (reply("CONTINUE") == 0) exit(FSCK_EXIT_CHECK_FAILED); asked++; } } /* * allocate and initialize the necessary maps */ bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t)); blockmap = calloc((unsigned int)bmapsize, sizeof(char)); if (blockmap == NULL) { printf("cannot alloc %u bytes for blockmap\n", (unsigned int)bmapsize); goto badsblabel; } statemap = calloc((unsigned int)(maxino + 2), sizeof(char)); if (statemap == NULL) { printf("cannot alloc %u bytes for statemap\n", (unsigned int)(maxino + 1)); goto badsblabel; } typemap = calloc((unsigned int)(maxino + 1), sizeof(char)); if (typemap == NULL) { printf("cannot alloc %u bytes for typemap\n", (unsigned int)(maxino + 1)); goto badsblabel; } lncntp = calloc((unsigned)(maxino + 1), sizeof(int16_t)); if (lncntp == NULL) { printf("cannot alloc %u bytes for lncntp\n", (unsigned int)((maxino + 1) * sizeof(int16_t))); goto badsblabel; } for (numdirs = 0, cg = 0; cg < sblock.e2fs_ncg; cg++) { numdirs += fs2h16(sblock.e2fs_gd[cg].ext2bgd_ndirs); } inplast = 0; listmax = numdirs + 10; inpsort = calloc((unsigned int)listmax, sizeof(struct inoinfo *)); inphead = calloc((unsigned int)numdirs, sizeof(struct inoinfo *)); if (inpsort == NULL || inphead == NULL) { printf("cannot alloc %u bytes for inphead\n", (unsigned int)(numdirs * sizeof(struct inoinfo *))); goto badsblabel; } bufinit(); return 1; badsblabel: ckfini(0); return 0; }
/* * Read in a superblock finding an alternate if necessary. * Return 1 if successful, 0 if unsuccessful, -1 if filesystem * is already clean (preen mode only). */ int setup(char *dev) { long size, asked, i, j; long skipclean, bmapsize; off_t sizepb; struct stat statb; havesb = 0; fswritefd = -1; skipclean = fflag ? 0 : preen; if (stat(dev, &statb) < 0) { printf("Can't stat %s: %s\n", dev, strerror(errno)); return (0); } if ((statb.st_mode & S_IFMT) != S_IFCHR && (statb.st_mode & S_IFMT) != S_IFBLK) { pfatal("%s is not a disk device", dev); if (reply("CONTINUE") == 0) return (0); } if ((fsreadfd = open(dev, O_RDONLY)) < 0) { printf("Can't open %s: %s\n", dev, strerror(errno)); return (0); } if (preen == 0) printf("** %s", dev); if (nflag || (fswritefd = open(dev, O_WRONLY)) < 0) { fswritefd = -1; if (preen) pfatal("NO WRITE ACCESS"); printf(" (NO WRITE)"); } if (preen == 0) printf("\n"); fsmodified = 0; lfdir = 0; initbarea(&sblk); initbarea(&asblk); sblk.b_un.b_buf = malloc(SBSIZE); asblk.b_un.b_buf = malloc(SBSIZE); if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) errx(EEXIT, "cannot allocate space for superblock"); /* * Figure out the device block size and the sector size. The * block size is updated by readsb() later on. */ { struct partinfo pinfo; if (ioctl(fsreadfd, DIOCGPART, &pinfo) == 0) { dev_bsize = secsize = pinfo.media_blksize; } else { dev_bsize = secsize = DEV_BSIZE; } } /* * Read in the superblock, looking for alternates if necessary */ if (readsb(1) == 0) { skipclean = 0; if (bflag || preen) return(0); if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) return (0); bflag = 32; if (readsb(0) == 0) { printf( "YOU MUST USE THE -b OPTION TO FSCK TO SPECIFY\n" "THE LOCATION OF AN ALTERNATE SUPER-BLOCK TO\n" "SUPPLY NEEDED INFORMATION; SEE fsck(8)."); bflag = 0; return(0); } pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); bflag = 0; } if (skipclean && sblock.fs_clean) { pwarn("FILESYSTEM CLEAN; SKIPPING CHECKS\n"); return (-1); } maxfsblock = sblock.fs_size; maxino = sblock.fs_ncg * sblock.fs_ipg; /* * Check and potentially fix certain fields in the super block. */ if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); if (reply("SET TO DEFAULT") == 1) { sblock.fs_optim = FS_OPTTIME; sbdirty(); } } if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", sblock.fs_minfree); if (reply("SET TO DEFAULT") == 1) { sblock.fs_minfree = 10; sbdirty(); } } if (sblock.fs_interleave < 1 || sblock.fs_interleave > sblock.fs_nsect) { pwarn("IMPOSSIBLE INTERLEAVE=%d IN SUPERBLOCK", sblock.fs_interleave); sblock.fs_interleave = 1; if (preen) printf(" (FIXED)\n"); if (preen || reply("SET TO DEFAULT") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_npsect < sblock.fs_nsect || sblock.fs_npsect > sblock.fs_nsect*2) { pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", sblock.fs_npsect); sblock.fs_npsect = sblock.fs_nsect; if (preen) printf(" (FIXED)\n"); if (preen || reply("SET TO DEFAULT") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_inodefmt >= FS_44INODEFMT) { newinofmt = 1; } else { sblock.fs_qbmask = ~sblock.fs_bmask; sblock.fs_qfmask = ~sblock.fs_fmask; /* This should match the kernel limit in ffs_oldfscompat(). */ sblock.fs_maxfilesize = (u_int64_t)1 << 39; newinofmt = 0; } /* * Convert to new inode format. */ if (cvtlevel >= 2 && sblock.fs_inodefmt < FS_44INODEFMT) { if (preen) pwarn("CONVERTING TO NEW INODE FORMAT\n"); else if (!reply("CONVERT TO NEW INODE FORMAT")) return(0); doinglevel2++; sblock.fs_inodefmt = FS_44INODEFMT; sizepb = sblock.fs_bsize; sblock.fs_maxfilesize = sblock.fs_bsize * NDADDR - 1; for (i = 0; i < NIADDR; i++) { sizepb *= NINDIR(&sblock); sblock.fs_maxfilesize += sizepb; } sblock.fs_maxsymlinklen = MAXSYMLINKLEN; sblock.fs_qbmask = ~sblock.fs_bmask; sblock.fs_qfmask = ~sblock.fs_fmask; sbdirty(); dirty(&asblk); } /* * Convert to new cylinder group format. */ if (cvtlevel >= 1 && sblock.fs_postblformat == FS_42POSTBLFMT) { if (preen) pwarn("CONVERTING TO NEW CYLINDER GROUP FORMAT\n"); else if (!reply("CONVERT TO NEW CYLINDER GROUP FORMAT")) return(0); doinglevel1++; sblock.fs_postblformat = FS_DYNAMICPOSTBLFMT; sblock.fs_nrpos = 8; sblock.fs_postbloff = (char *)(&sblock.fs_opostbl[0][0]) - (char *)(&sblock.fs_firstfield); sblock.fs_rotbloff = &sblock.fs_space[0] - (u_char *)(&sblock.fs_firstfield); sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); sbdirty(); dirty(&asblk); } if (asblk.b_dirty && !bflag) { memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); flush(fswritefd, &asblk); } /* * read in the summary info. */ asked = 0; sblock.fs_csp = calloc(1, sblock.fs_cssize); for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { size = sblock.fs_cssize - i < sblock.fs_bsize ? sblock.fs_cssize - i : sblock.fs_bsize; if (bread(fsreadfd, (char *)sblock.fs_csp + i, fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), size) != 0 && !asked) { pfatal("BAD SUMMARY INFORMATION"); if (reply("CONTINUE") == 0) { ckfini(0); exit(EEXIT); } asked++; } } /* * allocate and initialize the necessary maps */ bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(short)); blockmap = calloc((unsigned)bmapsize, sizeof (char)); if (blockmap == NULL) { printf("cannot alloc %u bytes for blockmap\n", (unsigned)bmapsize); goto badsb; } inostathead = calloc((unsigned)(sblock.fs_ncg), sizeof(struct inostatlist)); if (inostathead == NULL) { printf("cannot alloc %u bytes for inostathead\n", (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); goto badsb; } numdirs = sblock.fs_cstotal.cs_ndir; /* * Calculate the directory hash table size. Do not allocate * a ridiculous amount of memory if we have a lot of directories. */ for (dirhash = 16; dirhash < numdirs; dirhash <<= 1) ; if (dirhash > 1024*1024) dirhash /= 8; dirhashmask = dirhash - 1; if (numdirs == 0) { printf("numdirs is zero, try using an alternate superblock\n"); goto badsb; } inplast = 0; listmax = numdirs + 10; inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *)); inphead = calloc((unsigned)dirhash, sizeof(struct inoinfo *)); if (inpsort == NULL || inphead == NULL) { printf("cannot allocate base structures for %ld directories\n", numdirs); goto badsb; } bufinit(); if (sblock.fs_flags & FS_DOSOFTDEP) usedsoftdep = 1; else usedsoftdep = 0; return (1); badsb: ckfini(0); return (0); }
/* * Read in a superblock finding an alternate if necessary. * Return 1 if successful, 0 if unsuccessful, -1 if file system * is already clean (ckclean and preen mode only). */ int setup(char *dev) { long cg, asked, i, j; long bmapsize; struct stat statb; struct fs proto; size_t size; havesb = 0; fswritefd = -1; cursnapshot = 0; if (stat(dev, &statb) < 0) { printf("Can't stat %s: %s\n", dev, strerror(errno)); if (bkgrdflag) { unlink(snapname); bkgrdflag = 0; } return (0); } if ((statb.st_mode & S_IFMT) != S_IFCHR && (statb.st_mode & S_IFMT) != S_IFBLK) { if (bkgrdflag != 0 && (statb.st_flags & SF_SNAPSHOT) == 0) { unlink(snapname); printf("background fsck lacks a snapshot\n"); exit(EEXIT); } if ((statb.st_flags & SF_SNAPSHOT) != 0 && cvtlevel == 0) { cursnapshot = statb.st_ino; } else { if (cvtlevel == 0 || (statb.st_flags & SF_SNAPSHOT) == 0) { if (preen && bkgrdflag) { unlink(snapname); bkgrdflag = 0; } pfatal("%s is not a disk device", dev); if (reply("CONTINUE") == 0) { if (bkgrdflag) { unlink(snapname); bkgrdflag = 0; } return (0); } } else { if (bkgrdflag) { unlink(snapname); bkgrdflag = 0; } pfatal("cannot convert a snapshot"); exit(EEXIT); } } } if ((fsreadfd = open(dev, O_RDONLY)) < 0) { if (bkgrdflag) { unlink(snapname); bkgrdflag = 0; } printf("Can't open %s: %s\n", dev, strerror(errno)); return (0); } if (bkgrdflag) { unlink(snapname); size = MIBSIZE; if (sysctlnametomib("vfs.ffs.adjrefcnt", adjrefcnt, &size) < 0|| sysctlnametomib("vfs.ffs.adjblkcnt", adjblkcnt, &size) < 0|| sysctlnametomib("vfs.ffs.freefiles", freefiles, &size) < 0|| sysctlnametomib("vfs.ffs.freedirs", freedirs, &size) < 0 || sysctlnametomib("vfs.ffs.freeblks", freeblks, &size) < 0) { pfatal("kernel lacks background fsck support\n"); exit(EEXIT); } /* * When kernel is lack of runtime bgfsck superblock summary * adjustment functionality, it does not mean we can not * continue, as old kernels will recompute the summary at * mount time. However, it will be an unexpected softupdates * inconsistency if it turns out that the summary is still * incorrect. Set a flag so subsequent operation can know * this. */ bkgrdsumadj = 1; if (sysctlnametomib("vfs.ffs.adjndir", adjndir, &size) < 0 || sysctlnametomib("vfs.ffs.adjnbfree", adjnbfree, &size) < 0 || sysctlnametomib("vfs.ffs.adjnifree", adjnifree, &size) < 0 || sysctlnametomib("vfs.ffs.adjnffree", adjnffree, &size) < 0 || sysctlnametomib("vfs.ffs.adjnumclusters", adjnumclusters, &size) < 0) { bkgrdsumadj = 0; pwarn("kernel lacks runtime superblock summary adjustment support"); } cmd.version = FFS_CMD_VERSION; cmd.handle = fsreadfd; fswritefd = -1; } if (preen == 0) printf("** %s", dev); if (bkgrdflag == 0 && (nflag || (fswritefd = open(dev, O_WRONLY)) < 0)) { fswritefd = -1; if (preen) pfatal("NO WRITE ACCESS"); printf(" (NO WRITE)"); } if (preen == 0) printf("\n"); /* * Read in the superblock, looking for alternates if necessary */ if (readsb(1) == 0) { skipclean = 0; if (bflag || preen || calcsb(dev, fsreadfd, &proto) == 0) return(0); if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) return (0); for (cg = 0; cg < proto.fs_ncg; cg++) { bflag = fsbtodb(&proto, cgsblock(&proto, cg)); if (readsb(0) != 0) break; } if (cg >= proto.fs_ncg) { printf("%s %s\n%s %s\n%s %s\n", "SEARCH FOR ALTERNATE SUPER-BLOCK", "FAILED. YOU MUST USE THE", "-b OPTION TO FSCK TO SPECIFY THE", "LOCATION OF AN ALTERNATE", "SUPER-BLOCK TO SUPPLY NEEDED", "INFORMATION; SEE fsck_ffs(8)."); bflag = 0; return(0); } pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); bflag = 0; } if (skipclean && ckclean && sblock.fs_clean) { pwarn("FILE SYSTEM CLEAN; SKIPPING CHECKS\n"); return (-1); } maxfsblock = sblock.fs_size; maxino = sblock.fs_ncg * sblock.fs_ipg; /* * Check and potentially fix certain fields in the super block. */ if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); if (reply("SET TO DEFAULT") == 1) { sblock.fs_optim = FS_OPTTIME; sbdirty(); } } if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", sblock.fs_minfree); if (reply("SET TO DEFAULT") == 1) { sblock.fs_minfree = 10; sbdirty(); } } if (sblock.fs_magic == FS_UFS1_MAGIC && sblock.fs_old_inodefmt < FS_44INODEFMT) { pwarn("Format of file system is too old.\n"); pwarn("Must update to modern format using a version of fsck\n"); pfatal("from before 2002 with the command ``fsck -c 2''\n"); exit(EEXIT); } if (asblk.b_dirty && !bflag) { memmove(&altsblock, &sblock, (size_t)sblock.fs_sbsize); flush(fswritefd, &asblk); } /* * read in the summary info. */ asked = 0; sblock.fs_csp = calloc(1, sblock.fs_cssize); if (sblock.fs_csp == NULL) { printf("cannot alloc %u bytes for cg summary info\n", (unsigned)sblock.fs_cssize); goto badsb; } for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { size = sblock.fs_cssize - i < sblock.fs_bsize ? sblock.fs_cssize - i : sblock.fs_bsize; readcnt[sblk.b_type]++; if (blread(fsreadfd, (char *)sblock.fs_csp + i, fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), size) != 0 && !asked) { pfatal("BAD SUMMARY INFORMATION"); if (reply("CONTINUE") == 0) { ckfini(0); exit(EEXIT); } asked++; } } /* * allocate and initialize the necessary maps */ bmapsize = roundup(howmany(maxfsblock, CHAR_BIT), sizeof(short)); blockmap = calloc((unsigned)bmapsize, sizeof (char)); if (blockmap == NULL) { printf("cannot alloc %u bytes for blockmap\n", (unsigned)bmapsize); goto badsb; } inostathead = calloc((unsigned)(sblock.fs_ncg), sizeof(struct inostatlist)); if (inostathead == NULL) { printf("cannot alloc %u bytes for inostathead\n", (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); goto badsb; } numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128); dirhash = numdirs; inplast = 0; listmax = numdirs + 10; inpsort = (struct inoinfo **)calloc((unsigned)listmax, sizeof(struct inoinfo *)); inphead = (struct inoinfo **)calloc((unsigned)numdirs, sizeof(struct inoinfo *)); if (inpsort == NULL || inphead == NULL) { printf("cannot alloc %ju bytes for inphead\n", (uintmax_t)numdirs * sizeof(struct inoinfo *)); goto badsb; } bufinit(); if (sblock.fs_flags & FS_DOSOFTDEP) usedsoftdep = 1; else usedsoftdep = 0; return (1); badsb: ckfini(0); return (0); }
int setup(char *dev) { long cg, size, asked, i, j, bmapsize; struct disklabel *lp; off_t sizepb; struct stat statb; struct fs proto; int doskipclean; int32_t maxsymlinklen, nindir, inopb; u_int64_t maxfilesize; char *realdev; havesb = 0; fswritefd = fsreadfd = -1; doskipclean = skipclean; if ((fsreadfd = opendev(dev, O_RDONLY, 0, &realdev)) < 0) { printf("Can't open %s: %s\n", dev, strerror(errno)); return (0); } if (strncmp(dev, realdev, PATH_MAX) != 0) { blockcheck(unrawname(realdev)); strlcpy(rdevname, realdev, sizeof(rdevname)); setcdevname(rdevname, dev, preen); } if (fstat(fsreadfd, &statb) < 0) { printf("Can't stat %s: %s\n", realdev, strerror(errno)); close(fsreadfd); return (0); } if (!S_ISCHR(statb.st_mode)) { pfatal("%s is not a character device", realdev); if (reply("CONTINUE") == 0) { close(fsreadfd); return (0); } } if (preen == 0) { printf("** %s", realdev); if (strncmp(dev, realdev, PATH_MAX) != 0) printf(" (%s)", dev); } if (nflag || (fswritefd = opendev(dev, O_WRONLY, 0, NULL)) < 0) { fswritefd = -1; if (preen) pfatal("NO WRITE ACCESS"); printf(" (NO WRITE)"); } if (preen == 0) printf("\n"); fsmodified = 0; lfdir = 0; initbarea(&sblk); initbarea(&asblk); sblk.b_un.b_buf = malloc(SBSIZE); asblk.b_un.b_buf = malloc(SBSIZE); if (sblk.b_un.b_buf == NULL || asblk.b_un.b_buf == NULL) errexit("cannot allocate space for superblock\n"); if ((lp = getdisklabel(NULL, fsreadfd)) != NULL) dev_bsize = secsize = lp->d_secsize; else dev_bsize = secsize = DEV_BSIZE; /* * Read in the superblock, looking for alternates if necessary */ if (readsb(1) == 0) { if (bflag || preen || calcsb(realdev, fsreadfd, &proto) == 0) return(0); if (reply("LOOK FOR ALTERNATE SUPERBLOCKS") == 0) return (0); for (i = 0; i < sizeof(altsbtry) / sizeof(altsbtry[0]); i++) { bflag = altsbtry[i]; /* proto partially setup by calcsb */ if (readsb(0) != 0 && proto.fs_fsize == sblock.fs_fsize && proto.fs_bsize == sblock.fs_bsize) goto found; } for (cg = 0; cg < proto.fs_ncg; cg++) { bflag = fsbtodb(&proto, cgsblock(&proto, cg)); if (readsb(0) != 0) break; } if (cg >= proto.fs_ncg) { printf("%s %s\n%s %s\n%s %s\n", "SEARCH FOR ALTERNATE SUPER-BLOCK", "FAILED. YOU MUST USE THE", "-b OPTION TO FSCK_FFS TO SPECIFY THE", "LOCATION OF AN ALTERNATE", "SUPER-BLOCK TO SUPPLY NEEDED", "INFORMATION; SEE fsck_ffs(8)."); return(0); } found: doskipclean = 0; pwarn("USING ALTERNATE SUPERBLOCK AT %d\n", bflag); } if (debug) printf("clean = %d\n", sblock.fs_clean); if (sblock.fs_clean & FS_ISCLEAN) { if (doskipclean) { pwarn("%sile system is clean; not checking\n", preen ? "f" : "** F"); return (-1); } if (!preen) pwarn("** File system is already clean\n"); } maxfsblock = sblock.fs_size; maxino = sblock.fs_ncg * sblock.fs_ipg; sizepb = sblock.fs_bsize; maxfilesize = sblock.fs_bsize * NDADDR - 1; for (i = 0; i < NIADDR; i++) { sizepb *= NINDIR(&sblock); maxfilesize += sizepb; } /* * Check and potentially fix certain fields in the super block. */ if (sblock.fs_optim != FS_OPTTIME && sblock.fs_optim != FS_OPTSPACE) { pfatal("UNDEFINED OPTIMIZATION IN SUPERBLOCK"); if (reply("SET TO DEFAULT") == 1) { sblock.fs_optim = FS_OPTTIME; sbdirty(); } } if ((sblock.fs_minfree < 0 || sblock.fs_minfree > 99)) { pfatal("IMPOSSIBLE MINFREE=%d IN SUPERBLOCK", sblock.fs_minfree); if (reply("SET TO DEFAULT") == 1) { sblock.fs_minfree = 10; sbdirty(); } } if (sblock.fs_npsect < sblock.fs_nsect || sblock.fs_npsect > sblock.fs_nsect*2) { pwarn("IMPOSSIBLE NPSECT=%d IN SUPERBLOCK", sblock.fs_npsect); sblock.fs_npsect = sblock.fs_nsect; if (preen) printf(" (FIXED)\n"); if (preen || reply("SET TO DEFAULT") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_bmask != ~(sblock.fs_bsize - 1)) { pwarn("INCORRECT BMASK=%x IN SUPERBLOCK", sblock.fs_bmask); sblock.fs_bmask = ~(sblock.fs_bsize - 1); if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_fmask != ~(sblock.fs_fsize - 1)) { pwarn("INCORRECT FMASK=%x IN SUPERBLOCK", sblock.fs_fmask); sblock.fs_fmask = ~(sblock.fs_fsize - 1); if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (1 << sblock.fs_bshift != sblock.fs_bsize) { pwarn("INCORRECT BSHIFT=%d IN SUPERBLOCK", sblock.fs_bshift); sblock.fs_bshift = ffs(sblock.fs_bsize) - 1; if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (1 << sblock.fs_fshift != sblock.fs_fsize) { pwarn("INCORRECT FSHIFT=%d IN SUPERBLOCK", sblock.fs_fshift); sblock.fs_fshift = ffs(sblock.fs_fsize) - 1; if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_inodefmt < FS_44INODEFMT) { pwarn("Format of filesystem is too old.\n"); pwarn("Must update to modern format using a version of fsck\n"); pfatal("from before release 5.0 with the command ``fsck -c 2''\n"); exit(8); } if (sblock.fs_maxfilesize != maxfilesize) { pwarn("INCORRECT MAXFILESIZE=%llu IN SUPERBLOCK", (unsigned long long)sblock.fs_maxfilesize); sblock.fs_maxfilesize = maxfilesize; if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } maxsymlinklen = sblock.fs_magic == FS_UFS1_MAGIC ? MAXSYMLINKLEN_UFS1 : MAXSYMLINKLEN_UFS2; if (sblock.fs_maxsymlinklen != maxsymlinklen) { pwarn("INCORRECT MAXSYMLINKLEN=%d IN SUPERBLOCK", sblock.fs_maxsymlinklen); sblock.fs_maxsymlinklen = maxsymlinklen; if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_qbmask != ~sblock.fs_bmask) { pwarn("INCORRECT QBMASK=%lx IN SUPERBLOCK", (unsigned long)sblock.fs_qbmask); sblock.fs_qbmask = ~sblock.fs_bmask; if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_qfmask != ~sblock.fs_fmask) { pwarn("INCORRECT QFMASK=%lx IN SUPERBLOCK", (unsigned long)sblock.fs_qfmask); sblock.fs_qfmask = ~sblock.fs_fmask; if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_cgsize != fragroundup(&sblock, CGSIZE(&sblock))) { pwarn("INCONSISTENT CGSIZE=%d\n", sblock.fs_cgsize); sblock.fs_cgsize = fragroundup(&sblock, CGSIZE(&sblock)); if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_magic == FS_UFS2_MAGIC) inopb = sblock.fs_bsize / sizeof(struct ufs2_dinode); else inopb = sblock.fs_bsize / sizeof(struct ufs1_dinode); if (INOPB(&sblock) != inopb) { pwarn("INCONSISTENT INOPB=%d\n", INOPB(&sblock)); sblock.fs_inopb = inopb; if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (sblock.fs_magic == FS_UFS2_MAGIC) nindir = sblock.fs_bsize / sizeof(int64_t); else nindir = sblock.fs_bsize / sizeof(int32_t); if (NINDIR(&sblock) != nindir) { pwarn("INCONSISTENT NINDIR=%d\n", NINDIR(&sblock)); sblock.fs_nindir = nindir; if (preen) printf(" (FIXED)\n"); if (preen || reply("FIX") == 1) { sbdirty(); dirty(&asblk); } } if (asblk.b_dirty && !bflag) { memcpy(&altsblock, &sblock, (size_t)sblock.fs_sbsize); flush(fswritefd, &asblk); } /* * read in the summary info. */ asked = 0; sblock.fs_csp = calloc(1, sblock.fs_cssize); if (sblock.fs_csp == NULL) { printf("cannot alloc %u bytes for cylinder group summary area\n", (unsigned)sblock.fs_cssize); goto badsblabel; } for (i = 0, j = 0; i < sblock.fs_cssize; i += sblock.fs_bsize, j++) { size = sblock.fs_cssize - i < sblock.fs_bsize ? sblock.fs_cssize - i : sblock.fs_bsize; if (bread(fsreadfd, (char *)sblock.fs_csp + i, fsbtodb(&sblock, sblock.fs_csaddr + j * sblock.fs_frag), size) != 0 && !asked) { pfatal("BAD SUMMARY INFORMATION"); if (reply("CONTINUE") == 0) { ckfini(0); errexit("%s", ""); } asked++; } } /* * allocate and initialize the necessary maps */ bmapsize = roundup(howmany(maxfsblock, NBBY), sizeof(int16_t)); blockmap = calloc((unsigned)bmapsize, sizeof(char)); if (blockmap == NULL) { printf("cannot alloc %u bytes for blockmap\n", (unsigned)bmapsize); goto badsblabel; } inostathead = calloc((unsigned)(sblock.fs_ncg), sizeof(struct inostatlist)); if (inostathead == NULL) { printf("cannot alloc %u bytes for inostathead\n", (unsigned)(sizeof(struct inostatlist) * (sblock.fs_ncg))); goto badsblabel; } numdirs = MAX(sblock.fs_cstotal.cs_ndir, 128); inplast = 0; listmax = numdirs + 10; inpsort = calloc((unsigned)listmax, sizeof(struct inoinfo *)); if (inpsort == NULL) { printf("cannot alloc %zu bytes for inpsort\n", (unsigned)listmax * sizeof(struct inoinfo *)); goto badsblabel; } inphead = calloc((unsigned)numdirs, sizeof(struct inoinfo *)); if (inphead == NULL) { printf("cannot alloc %zu bytes for inphead\n", (unsigned)numdirs * sizeof(struct inoinfo *)); goto badsblabel; } bufinit(); if (sblock.fs_flags & FS_DOSOFTDEP) usedsoftdep = 1; else usedsoftdep = 0; return (1); badsblabel: ckfini(0); return (0); }