static int mkentry(struct inodesc *idesc) { struct ext2fs_direct *dirp = idesc->id_dirp; struct ext2fs_direct newent; int newlen, oldlen; newent.e2d_type = 0; /* XXX gcc */ newent.e2d_namlen = strlen(idesc->id_name); if (sblock.e2fs.e2fs_rev > E2FS_REV0 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) newent.e2d_type = inot2ext2dt(typemap[idesc->id_parent]); newlen = EXT2FS_DIRSIZ(newent.e2d_namlen); if (dirp->e2d_ino != 0) oldlen = EXT2FS_DIRSIZ(dirp->e2d_namlen); else oldlen = 0; if (fs2h16(dirp->e2d_reclen) - oldlen < newlen) return (KEEPON); newent.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - oldlen); dirp->e2d_reclen = h2fs16(oldlen); dirp = (struct ext2fs_direct *)(((char *)dirp) + oldlen); dirp->e2d_ino = h2fs32(idesc->id_parent); /* ino to be entered is in id_parent */ dirp->e2d_reclen = newent.e2d_reclen; dirp->e2d_namlen = newent.e2d_namlen; dirp->e2d_type = newent.e2d_type; memcpy(dirp->e2d_name, idesc->id_name, (size_t)(dirp->e2d_namlen)); return (ALTERED|STOP); }
/* * allocate a new directory */ int allocdir(ino_t parent, ino_t request, int mode) { ino_t ino; struct ext2fs_dinode *dp; struct bufarea *bp; struct ext2fs_dirtemplate *dirp; ino = allocino(request, IFDIR|mode); dirhead.dot_reclen = h2fs16(12); /* XXX */ dirhead.dotdot_reclen = h2fs16(sblock.e2fs_bsize - 12); /* XXX */ dirhead.dot_namlen = 1; if (sblock.e2fs.e2fs_rev > E2FS_REV0 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) dirhead.dot_type = EXT2_FT_DIR; else dirhead.dot_type = 0; dirhead.dotdot_namlen = 2; if (sblock.e2fs.e2fs_rev > E2FS_REV0 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) dirhead.dotdot_type = EXT2_FT_DIR; else dirhead.dotdot_type = 0; dirp = &dirhead; dirp->dot_ino = h2fs32(ino); dirp->dotdot_ino = h2fs32(parent); dp = ginode(ino); bp = getdirblk(fs2h32(dp->e2di_blocks[0]), sblock.e2fs_bsize); if (bp->b_errs) { freeino(ino); return (0); } memcpy(bp->b_un.b_buf, dirp, sizeof(struct ext2fs_dirtemplate)); dirty(bp); dp->e2di_nlink = h2fs16(2); inodirty(); if (ino == EXT2_ROOTINO) { lncntp[ino] = fs2h16(dp->e2di_nlink); cacheino(dp, ino); return(ino); } if (statemap[parent] != DSTATE && statemap[parent] != DFOUND) { freeino(ino); return (0); } cacheino(dp, ino); statemap[ino] = statemap[parent]; if (statemap[ino] == DSTATE) { lncntp[ino] = fs2h16(dp->e2di_nlink); lncntp[parent]++; } dp = ginode(parent); dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) + 1); inodirty(); return (ino); }
/* * get next entry in a directory. */ static struct ext2fs_direct * fsck_readdir(struct inodesc *idesc) { struct ext2fs_direct *dp, *ndp; struct bufarea *bp; long size, blksiz, fix, dploc; blksiz = idesc->id_numfrags * sblock.e2fs_bsize; bp = getdirblk(idesc->id_blkno, blksiz); if (idesc->id_loc % sblock.e2fs_bsize == 0 && idesc->id_filesize > 0 && idesc->id_loc < blksiz) { dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc); if (dircheck(idesc, dp)) goto dpok; if (idesc->id_fix == IGNORE) return (0); fix = dofix(idesc, "DIRECTORY CORRUPTED"); bp = getdirblk(idesc->id_blkno, blksiz); dp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc); dp->e2d_reclen = h2fs16(sblock.e2fs_bsize); dp->e2d_ino = 0; dp->e2d_namlen = 0; dp->e2d_type = 0; dp->e2d_name[0] = '\0'; if (fix) dirty(bp); idesc->id_loc += sblock.e2fs_bsize; idesc->id_filesize -= sblock.e2fs_bsize; return (dp); } dpok: if (idesc->id_filesize <= 0 || idesc->id_loc >= blksiz) return NULL; dploc = idesc->id_loc; dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc); idesc->id_loc += fs2h16(dp->e2d_reclen); idesc->id_filesize -= fs2h16(dp->e2d_reclen); if ((idesc->id_loc % sblock.e2fs_bsize) == 0) return (dp); ndp = (struct ext2fs_direct *)(bp->b_un.b_buf + idesc->id_loc); if (idesc->id_loc < blksiz && idesc->id_filesize > 0 && dircheck(idesc, ndp) == 0) { size = sblock.e2fs_bsize - (idesc->id_loc % sblock.e2fs_bsize); idesc->id_loc += size; idesc->id_filesize -= size; if (idesc->id_fix == IGNORE) return (0); fix = dofix(idesc, "DIRECTORY CORRUPTED"); bp = getdirblk(idesc->id_blkno, blksiz); dp = (struct ext2fs_direct *)(bp->b_un.b_buf + dploc); dp->e2d_reclen = h2fs16(fs2h16(dp->e2d_reclen) + size); if (fix) dirty(bp); } return (dp); }
void adjust(struct inodesc *idesc, short lcnt) { struct ext2fs_dinode *dp; dp = ginode(idesc->id_number); if (fs2h16(dp->e2di_nlink) == lcnt) { if (linkup(idesc->id_number, (ino_t)0) == 0) clri(idesc, "UNREF", 0); } else { pwarn("LINK COUNT %s", (lfdir == idesc->id_number) ? lfname : ((fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? "DIR" : "FILE")); pinode(idesc->id_number); printf(" COUNT %d SHOULD BE %d", fs2h16(dp->e2di_nlink), fs2h16(dp->e2di_nlink) - lcnt); if (preen) { if (lcnt < 0) { printf("\n"); pfatal("LINK COUNT INCREASING"); } printf(" (ADJUSTED)\n"); } if (preen || reply("ADJUST") == 1) { dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - lcnt); inodirty(); } } }
/* * Remove a directory entry after a call to namei, using * the parameters which it left in nameidata. The entry * dp->i_offset contains the offset into the directory of the * entry to be eliminated. The dp->i_count field contains the * size of the previous record in the directory. If this * is 0, the first entry is being deleted, so we need only * zero the inode number to mark the entry as free. If the * entry is not the first in the directory, we must reclaim * the space of the now empty record by adding the record size * to the size of the previous entry. */ int ext2fs_dirremove(struct vnode *dvp, struct componentname *cnp) { struct inode *dp; struct ext2fs_direct *ep; struct buf *bp; int error; dp = VTOI(dvp); if (dp->i_count == 0) { /* * First entry in block: set d_ino to zero. */ error = ext2fs_bufatoff(dp, (off_t)dp->i_offset, (char **)&ep, &bp); if (error != 0) return (error); ep->e2d_ino = 0; error = VOP_BWRITE(bp); dp->i_flag |= IN_CHANGE | IN_UPDATE; return (error); } /* * Collapse new free space into previous entry. */ error = ext2fs_bufatoff(dp, (off_t)(dp->i_offset - dp->i_count), (char **)&ep, &bp); if (error != 0) return (error); ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + dp->i_reclen); error = VOP_BWRITE(bp); dp->i_flag |= IN_CHANGE | IN_UPDATE; return (error); }
void copyback_sb(struct bufarea *bp) { /* Copy the in-memory superblock back to buffer */ bp->b_un.b_fs->e2fs_icount = h2fs32(sblock.e2fs.e2fs_icount); bp->b_un.b_fs->e2fs_bcount = h2fs32(sblock.e2fs.e2fs_bcount); bp->b_un.b_fs->e2fs_rbcount = h2fs32(sblock.e2fs.e2fs_rbcount); bp->b_un.b_fs->e2fs_fbcount = h2fs32(sblock.e2fs.e2fs_fbcount); bp->b_un.b_fs->e2fs_ficount = h2fs32(sblock.e2fs.e2fs_ficount); bp->b_un.b_fs->e2fs_first_dblock = h2fs32(sblock.e2fs.e2fs_first_dblock); bp->b_un.b_fs->e2fs_log_bsize = h2fs32(sblock.e2fs.e2fs_log_bsize); bp->b_un.b_fs->e2fs_fsize = h2fs32(sblock.e2fs.e2fs_fsize); bp->b_un.b_fs->e2fs_bpg = h2fs32(sblock.e2fs.e2fs_bpg); bp->b_un.b_fs->e2fs_fpg = h2fs32(sblock.e2fs.e2fs_fpg); bp->b_un.b_fs->e2fs_ipg = h2fs32(sblock.e2fs.e2fs_ipg); bp->b_un.b_fs->e2fs_mtime = h2fs32(sblock.e2fs.e2fs_mtime); bp->b_un.b_fs->e2fs_wtime = h2fs32(sblock.e2fs.e2fs_wtime); bp->b_un.b_fs->e2fs_lastfsck = h2fs32(sblock.e2fs.e2fs_lastfsck); bp->b_un.b_fs->e2fs_fsckintv = h2fs32(sblock.e2fs.e2fs_fsckintv); bp->b_un.b_fs->e2fs_creator = h2fs32(sblock.e2fs.e2fs_creator); bp->b_un.b_fs->e2fs_rev = h2fs32(sblock.e2fs.e2fs_rev); bp->b_un.b_fs->e2fs_mnt_count = h2fs16(sblock.e2fs.e2fs_mnt_count); bp->b_un.b_fs->e2fs_max_mnt_count = h2fs16(sblock.e2fs.e2fs_max_mnt_count); bp->b_un.b_fs->e2fs_magic = h2fs16(sblock.e2fs.e2fs_magic); bp->b_un.b_fs->e2fs_state = h2fs16(sblock.e2fs.e2fs_state); bp->b_un.b_fs->e2fs_beh = h2fs16(sblock.e2fs.e2fs_beh); bp->b_un.b_fs->e2fs_ruid = h2fs16(sblock.e2fs.e2fs_ruid); bp->b_un.b_fs->e2fs_rgid = h2fs16(sblock.e2fs.e2fs_rgid); }
/* * Attempt to expand the size of a directory */ static int expanddir(struct ext2fs_dinode *dp, char *name) { daddr_t lastbn, newblk; struct bufarea *bp; char *firstblk; lastbn = lblkno(&sblock, inosize(dp)); if (lastbn >= NDADDR - 1 || fs2h32(dp->e2di_blocks[lastbn]) == 0 || inosize(dp) == 0) { return (0); } if ((newblk = allocblk()) == 0) { return (0); } dp->e2di_blocks[lastbn + 1] = dp->e2di_blocks[lastbn]; dp->e2di_blocks[lastbn] = h2fs32(newblk); inossize(dp, inosize(dp) + sblock.e2fs_bsize); dp->e2di_nblock = h2fs32(fs2h32(dp->e2di_nblock) + 1); bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]), sblock.e2fs_bsize); if (bp->b_errs) goto bad; if ((firstblk = malloc(sblock.e2fs_bsize)) == NULL) err(8, "cannot allocate first block"); memcpy(firstblk, bp->b_un.b_buf, sblock.e2fs_bsize); bp = getdirblk(newblk, sblock.e2fs_bsize); if (bp->b_errs) { free(firstblk); goto bad; } memcpy(bp->b_un.b_buf, firstblk, sblock.e2fs_bsize); free(firstblk); dirty(bp); bp = getdirblk(fs2h32(dp->e2di_blocks[lastbn + 1]), sblock.e2fs_bsize); if (bp->b_errs) goto bad; emptydir.dot_reclen = h2fs16(sblock.e2fs_bsize); memcpy(bp->b_un.b_buf, &emptydir, sizeof emptydir); pwarn("NO SPACE LEFT IN %s", name); if (preen) printf(" (EXPANDED)\n"); else if (reply("EXPAND") == 0) goto bad; dirty(bp); inodirty(); return (1); bad: dp->e2di_blocks[lastbn] = dp->e2di_blocks[lastbn + 1]; dp->e2di_blocks[lastbn + 1] = 0; inossize(dp, inosize(dp) - sblock.e2fs_bsize); dp->e2di_nblock = h2fs32(fs2h32(dp->e2di_nblock) - 1); freeblk(newblk); return (0); }
/* * free a directory inode */ static void freedir(ino_t ino, ino_t parent) { struct ext2fs_dinode *dp; if (ino != parent) { dp = ginode(parent); dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) - 1); inodirty(); } freeino(ino); }
/* * Remove a directory entry after a call to namei, using * the auxiliary results it provided. The entry * ulr_offset contains the offset into the directory of the * entry to be eliminated. The ulr_count field contains the * size of the previous record in the directory. If this * is 0, the first entry is being deleted, so we need only * zero the inode number to mark the entry as free. If the * entry is not the first in the directory, we must reclaim * the space of the now empty record by adding the record size * to the size of the previous entry. */ int ext2fs_dirremove(struct vnode *dvp, const struct ufs_lookup_results *ulr, struct componentname *cnp) { struct inode *dp; struct ext2fs_direct *ep; struct buf *bp; int error; dp = VTOI(dvp); if (ulr->ulr_count == 0) { /* * First entry in block: set d_ino to zero. */ error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset, (void *)&ep, &bp); if (error != 0) return (error); ep->e2d_ino = 0; error = VOP_BWRITE(bp->b_vp, bp); dp->i_flag |= IN_CHANGE | IN_UPDATE; return (error); } /* * Collapse new free space into previous entry. */ error = ext2fs_blkatoff(dvp, (off_t)(ulr->ulr_offset - ulr->ulr_count), (void *)&ep, &bp); if (error != 0) return (error); ep->e2d_reclen = h2fs16(fs2h16(ep->e2d_reclen) + ulr->ulr_reclen); error = VOP_BWRITE(bp->b_vp, bp); dp->i_flag |= IN_CHANGE | IN_UPDATE; return (error); }
int linkup(ino_t orphan, ino_t parentdir) { struct ext2fs_dinode *dp; int lostdir; ino_t oldlfdir; struct inodesc idesc; char tempname[BUFSIZ]; memset(&idesc, 0, sizeof(struct inodesc)); dp = ginode(orphan); lostdir = (fs2h16(dp->e2di_mode) & IFMT) == IFDIR; pwarn("UNREF %s ", lostdir ? "DIR" : "FILE"); pinode(orphan); if (preen && inosize(dp) == 0) return (0); if (preen) printf(" (RECONNECTED)\n"); else if (reply("RECONNECT") == 0) return (0); if (lfdir == 0) { dp = ginode(EXT2_ROOTINO); idesc.id_name = lfname; idesc.id_type = DATA; idesc.id_func = findino; idesc.id_number = EXT2_ROOTINO; if ((ckinode(dp, &idesc) & FOUND) != 0) { lfdir = idesc.id_parent; } else { pwarn("NO lost+found DIRECTORY"); if (preen || reply("CREATE")) { lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode); if (lfdir != 0) { if (makeentry(EXT2_ROOTINO, lfdir, lfname) != 0) { if (preen) printf(" (CREATED)\n"); } else { freedir(lfdir, EXT2_ROOTINO); lfdir = 0; if (preen) printf("\n"); } } } } if (lfdir == 0) { pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY"); printf("\n\n"); return (0); } } dp = ginode(lfdir); if ((fs2h16(dp->e2di_mode) & IFMT) != IFDIR) { pfatal("lost+found IS NOT A DIRECTORY"); if (reply("REALLOCATE") == 0) return (0); oldlfdir = lfdir; if ((lfdir = allocdir(EXT2_ROOTINO, (ino_t)0, lfmode)) == 0) { pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); return (0); } if ((changeino(EXT2_ROOTINO, lfname, lfdir) & ALTERED) == 0) { pfatal("SORRY. CANNOT CREATE lost+found DIRECTORY\n\n"); return (0); } inodirty(); idesc.id_type = ADDR; idesc.id_func = pass4check; idesc.id_number = oldlfdir; adjust(&idesc, lncntp[oldlfdir] + 1); lncntp[oldlfdir] = 0; dp = ginode(lfdir); } if (statemap[lfdir] != DFOUND) { pfatal("SORRY. NO lost+found DIRECTORY\n\n"); return (0); } (void)lftempname(tempname, orphan); if (makeentry(lfdir, orphan, tempname) == 0) { pfatal("SORRY. NO SPACE IN lost+found DIRECTORY"); printf("\n\n"); return (0); } lncntp[orphan]--; if (lostdir) { if ((changeino(orphan, "..", lfdir) & ALTERED) == 0 && parentdir != (ino_t)-1) (void)makeentry(orphan, lfdir, ".."); dp = ginode(lfdir); dp->e2di_nlink = h2fs16(fs2h16(dp->e2di_nlink) +1); inodirty(); lncntp[lfdir]++; pwarn("DIR I=%llu CONNECTED. ", (unsigned long long)orphan); if (parentdir != (ino_t)-1) printf("PARENT WAS I=%llu\n", (unsigned long long)parentdir); if (preen == 0) printf("\n"); } return (1); }
/* * Write a directory entry after a call to namei, using the parameters * that it left in nameidata. The argument ip is the inode which the new * directory entry will refer to. Dvp is a pointer to the directory to * be written, which was left locked by namei. Remaining parameters * (ulr_offset, ulr_count) indicate how the space for the new * entry is to be obtained. */ int ext2fs_direnter(struct inode *ip, struct vnode *dvp, const struct ufs_lookup_results *ulr, struct componentname *cnp) { struct ext2fs_direct *ep, *nep; struct inode *dp; struct buf *bp; struct ext2fs_direct newdir; struct iovec aiov; struct uio auio; u_int dsize; int error, loc, newentrysize, spacefree; char *dirbuf; struct ufsmount *ump = VFSTOUFS(dvp->v_mount); int dirblksiz = ump->um_dirblksiz; dp = VTOI(dvp); newdir.e2d_ino = h2fs32(ip->i_number); newdir.e2d_namlen = cnp->cn_namelen; if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 && (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) { newdir.e2d_type = inot2ext2dt(IFTODT(ip->i_e2fs_mode)); } else { newdir.e2d_type = 0; } memcpy(newdir.e2d_name, cnp->cn_nameptr, (unsigned)cnp->cn_namelen + 1); newentrysize = EXT2FS_DIRSIZ(cnp->cn_namelen); if (ulr->ulr_count == 0) { /* * If ulr_count is 0, then namei could find no * space in the directory. Here, ulr_offset will * be on a directory block boundary and we will write the * new entry into a fresh block. */ if (ulr->ulr_offset & (dirblksiz - 1)) panic("ext2fs_direnter: newblk"); auio.uio_offset = ulr->ulr_offset; newdir.e2d_reclen = h2fs16(dirblksiz); auio.uio_resid = newentrysize; aiov.iov_len = newentrysize; aiov.iov_base = (void *)&newdir; auio.uio_iov = &aiov; auio.uio_iovcnt = 1; auio.uio_rw = UIO_WRITE; UIO_SETUP_SYSSPACE(&auio); error = VOP_WRITE(dvp, &auio, IO_SYNC, cnp->cn_cred); if (dirblksiz > dvp->v_mount->mnt_stat.f_bsize) /* XXX should grow with balloc() */ panic("ext2fs_direnter: frag size"); else if (!error) { error = ext2fs_setsize(dp, roundup(ext2fs_size(dp), dirblksiz)); if (error) return (error); dp->i_flag |= IN_CHANGE; uvm_vnp_setsize(dvp, ext2fs_size(dp)); } return (error); } /* * If ulr_count is non-zero, then namei found space * for the new entry in the range ulr_offset to * ulr_offset + ulr_count in the directory. * To use this space, we may have to compact the entries located * there, by copying them together towards the beginning of the * block, leaving the free space in one usable chunk at the end. */ /* * Get the block containing the space for the new directory entry. */ if ((error = ext2fs_blkatoff(dvp, (off_t)ulr->ulr_offset, &dirbuf, &bp)) != 0) return (error); /* * Find space for the new entry. In the simple case, the entry at * offset base will have the space. If it does not, then namei * arranged that compacting the region ulr_offset to * ulr_offset + ulr_count would yield the * space. */ ep = (struct ext2fs_direct *)dirbuf; dsize = EXT2FS_DIRSIZ(ep->e2d_namlen); spacefree = fs2h16(ep->e2d_reclen) - dsize; for (loc = fs2h16(ep->e2d_reclen); loc < ulr->ulr_count; ) { nep = (struct ext2fs_direct *)(dirbuf + loc); if (ep->e2d_ino) { /* trim the existing slot */ ep->e2d_reclen = h2fs16(dsize); ep = (struct ext2fs_direct *)((char *)ep + dsize); } else { /* overwrite; nothing there; header is ours */ spacefree += dsize; } dsize = EXT2FS_DIRSIZ(nep->e2d_namlen); spacefree += fs2h16(nep->e2d_reclen) - dsize; loc += fs2h16(nep->e2d_reclen); memcpy((void *)ep, (void *)nep, dsize); } /* * Update the pointer fields in the previous entry (if any), * copy in the new entry, and write out the block. */ if (ep->e2d_ino == 0) { #ifdef DIAGNOSTIC if (spacefree + dsize < newentrysize) panic("ext2fs_direnter: compact1"); #endif newdir.e2d_reclen = h2fs16(spacefree + dsize); } else { #ifdef DIAGNOSTIC if (spacefree < newentrysize) { printf("ext2fs_direnter: compact2 %u %u", (u_int)spacefree, (u_int)newentrysize); panic("ext2fs_direnter: compact2"); } #endif newdir.e2d_reclen = h2fs16(spacefree); ep->e2d_reclen = h2fs16(dsize); ep = (struct ext2fs_direct *)((char *)ep + dsize); } memcpy((void *)ep, (void *)&newdir, (u_int)newentrysize); error = VOP_BWRITE(bp->b_vp, bp); dp->i_flag |= IN_CHANGE | IN_UPDATE; if (!error && ulr->ulr_endoff && ulr->ulr_endoff < ext2fs_size(dp)) error = ext2fs_truncate(dvp, (off_t)ulr->ulr_endoff, IO_SYNC, cnp->cn_cred); return (error); }
/* * Mkdir system call */ int ext2fs_mkdir(void *v) { struct vop_mkdir_args *ap = v; struct vnode *dvp = ap->a_dvp; struct vattr *vap = ap->a_vap; struct componentname *cnp = ap->a_cnp; struct inode *ip, *dp; struct vnode *tvp; struct ext2fs_dirtemplate dirtemplate; mode_t dmode; int error; #ifdef DIAGNOSTIC if ((cnp->cn_flags & HASBUF) == 0) panic("ext2fs_mkdir: no name"); #endif dp = VTOI(dvp); if ((nlink_t)dp->i_e2fs_nlink >= LINK_MAX) { error = EMLINK; goto out; } dmode = vap->va_mode & ACCESSPERMS; dmode |= IFDIR; /* * Must simulate part of ext2fs_makeinode here to acquire the inode, * but not have it entered in the parent directory. The entry is * made later after writing "." and ".." entries. */ if ((error = ext2fs_inode_alloc(dp, dmode, cnp->cn_cred, &tvp)) != 0) goto out; ip = VTOI(tvp); ip->i_e2fs_uid = cnp->cn_cred->cr_uid; ip->i_e2fs_gid = dp->i_e2fs_gid; ip->i_flag |= IN_ACCESS | IN_CHANGE | IN_UPDATE; ip->i_e2fs_mode = dmode; tvp->v_type = VDIR; /* Rest init'd in getnewvnode(). */ ip->i_e2fs_nlink = 2; error = ext2fs_update(ip, NULL, NULL, 1); /* * Bump link count in parent directory * to reflect work done below. Should * be done before reference is created * so reparation is possible if we crash. */ dp->i_e2fs_nlink++; dp->i_flag |= IN_CHANGE; if ((error = ext2fs_update(dp, NULL, NULL, 1)) != 0) goto bad; /* Initialize directory with "." and ".." from static template. */ bzero(&dirtemplate, sizeof(dirtemplate)); dirtemplate.dot_ino = h2fs32(ip->i_number); dirtemplate.dot_reclen = h2fs16(12); dirtemplate.dot_namlen = 1; if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 && (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) { dirtemplate.dot_type = EXT2_FT_DIR; } dirtemplate.dot_name[0] = '.'; dirtemplate.dotdot_ino = h2fs32(dp->i_number); dirtemplate.dotdot_reclen = h2fs16(VTOI(dvp)->i_e2fs->e2fs_bsize - 12); dirtemplate.dotdot_namlen = 2; if (ip->i_e2fs->e2fs.e2fs_rev > E2FS_REV0 && (ip->i_e2fs->e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) { dirtemplate.dotdot_type = EXT2_FT_DIR; } dirtemplate.dotdot_name[0] = dirtemplate.dotdot_name[1] = '.'; error = vn_rdwr(UIO_WRITE, tvp, (caddr_t)&dirtemplate, sizeof (dirtemplate), (off_t)0, UIO_SYSSPACE, IO_NODELOCKED|IO_SYNC, cnp->cn_cred, NULL, curproc); if (error) { dp->i_e2fs_nlink--; dp->i_flag |= IN_CHANGE; goto bad; } if (VTOI(dvp)->i_e2fs->e2fs_bsize > VFSTOUFS(dvp->v_mount)->um_mountp->mnt_stat.f_bsize) panic("ext2fs_mkdir: blksize"); /* XXX should grow with balloc() */ else { error = ext2fs_setsize(ip, VTOI(dvp)->i_e2fs->e2fs_bsize); if (error) { dp->i_e2fs_nlink--; dp->i_flag |= IN_CHANGE; goto bad; } ip->i_flag |= IN_CHANGE; } /* Directory set up, now install its entry in the parent directory. */ error = ext2fs_direnter(ip, dvp, cnp); if (error != 0) { dp->i_e2fs_nlink--; dp->i_flag |= IN_CHANGE; } bad: /* * No need to do an explicit VOP_TRUNCATE here, vrele will do this * for us because we set the link count to 0. */ if (error) { ip->i_e2fs_nlink = 0; ip->i_flag |= IN_CHANGE; vput(tvp); } else *ap->a_vpp = tvp; out: pool_put(&namei_pool, cnp->cn_pnbuf); vput(dvp); return (error); }
void pass2(void) { struct ext2fs_dinode *dp; struct inoinfo **inpp, *inp; struct inoinfo **inpend; struct inodesc curino; struct ext2fs_dinode dino; char pathbuf[MAXPATHLEN + 1]; switch (statemap[EXT2_ROOTINO]) { case USTATE: pfatal("ROOT INODE UNALLOCATED"); if (reply("ALLOCATE") == 0) exit(FSCK_EXIT_CHECK_FAILED); if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO) errexit("CANNOT ALLOCATE ROOT INODE"); break; case DCLEAR: pfatal("DUPS/BAD IN ROOT INODE"); if (reply("REALLOCATE")) { freeino(EXT2_ROOTINO); if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO) errexit("CANNOT ALLOCATE ROOT INODE"); break; } if (reply("CONTINUE") == 0) exit(FSCK_EXIT_CHECK_FAILED); break; case FSTATE: case FCLEAR: pfatal("ROOT INODE NOT DIRECTORY"); if (reply("REALLOCATE")) { freeino(EXT2_ROOTINO); if (allocdir(EXT2_ROOTINO, EXT2_ROOTINO, 0755) != EXT2_ROOTINO) errexit("CANNOT ALLOCATE ROOT INODE"); break; } if (reply("FIX") == 0) exit(FSCK_EXIT_CHECK_FAILED); dp = ginode(EXT2_ROOTINO); dp->e2di_mode = h2fs16((fs2h16(dp->e2di_mode) & ~IFMT) | IFDIR); inodirty(); break; case DSTATE: break; default: errexit("BAD STATE %d FOR ROOT INODE", statemap[EXT2_ROOTINO]); } /* * Sort the directory list into disk block order. */ qsort((char *)inpsort, (size_t)inplast, sizeof *inpsort, blksort); /* * Check the integrity of each directory. */ memset(&curino, 0, sizeof(struct inodesc)); curino.id_type = DATA; curino.id_func = pass2check; inpend = &inpsort[inplast]; for (inpp = inpsort; inpp < inpend; inpp++) { inp = *inpp; if (inp->i_isize == 0) continue; if (inp->i_isize < MINDIRSIZE) { direrror(inp->i_number, "DIRECTORY TOO SHORT"); inp->i_isize = roundup(MINDIRSIZE, sblock.e2fs_bsize); if (reply("FIX") == 1) { dp = ginode(inp->i_number); inossize(dp, inp->i_isize); inodirty(); } } else if ((inp->i_isize & (sblock.e2fs_bsize - 1)) != 0) { getpathname(pathbuf, sizeof(pathbuf), inp->i_number, inp->i_number); pwarn("DIRECTORY %s: LENGTH %lu NOT MULTIPLE OF %d", pathbuf, (u_long)inp->i_isize, sblock.e2fs_bsize); if (preen) printf(" (ADJUSTED)\n"); inp->i_isize = roundup(inp->i_isize, sblock.e2fs_bsize); if (preen || reply("ADJUST") == 1) { dp = ginode(inp->i_number); inossize(dp, inp->i_isize); inodirty(); } } memset(&dino, 0, sizeof(struct ext2fs_dinode)); dino.e2di_mode = h2fs16(IFDIR); inossize(&dino, inp->i_isize); memcpy(&dino.e2di_blocks[0], &inp->i_blks[0], (size_t)inp->i_numblks); curino.id_number = inp->i_number; curino.id_parent = inp->i_parent; (void)ckinode(&dino, &curino); } /* * Now that the parents of all directories have been found, * make another pass to verify the value of `..' */ for (inpp = inpsort; inpp < inpend; inpp++) { inp = *inpp; if (inp->i_parent == 0 || inp->i_isize == 0) continue; if (inp->i_dotdot == inp->i_parent || inp->i_dotdot == (ino_t)-1) continue; if (inp->i_dotdot == 0) { inp->i_dotdot = inp->i_parent; fileerror(inp->i_parent, inp->i_number, "MISSING '..'"); if (reply("FIX") == 0) continue; (void)makeentry(inp->i_number, inp->i_parent, ".."); lncntp[inp->i_parent]--; continue; } fileerror(inp->i_parent, inp->i_number, "BAD INODE NUMBER FOR '..'"); if (reply("FIX") == 0) continue; lncntp[inp->i_dotdot]++; lncntp[inp->i_parent]--; inp->i_dotdot = inp->i_parent; (void)changeino(inp->i_number, "..", inp->i_parent); } /* * Mark all the directories that can be found from the root. */ propagate(); }
static int pass2check(struct inodesc *idesc) { struct ext2fs_direct *dirp = idesc->id_dirp; struct inoinfo *inp; int n, entrysize, ret = 0; struct ext2fs_dinode *dp; const char *errmsg; struct ext2fs_direct proto; char namebuf[MAXPATHLEN + 1]; char pathbuf[MAXPATHLEN + 1]; /* * check for "." */ if (idesc->id_entryno != 0) goto chk1; if (fs2h32(dirp->e2d_ino) != 0 && dirp->e2d_namlen == 1 && dirp->e2d_name[0] == '.') { if (fs2h32(dirp->e2d_ino) != idesc->id_number) { direrror(idesc->id_number, "BAD INODE NUMBER FOR '.'"); dirp->e2d_ino = h2fs32(idesc->id_number); if (reply("FIX") == 1) ret |= ALTERED; } if (sblock.e2fs.e2fs_rev > E2FS_REV0 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) && (dirp->e2d_type != EXT2_FT_DIR)) { direrror(idesc->id_number, "BAD TYPE VALUE FOR '.'"); dirp->e2d_type = EXT2_FT_DIR; if (reply("FIX") == 1) ret |= ALTERED; } goto chk1; } direrror(idesc->id_number, "MISSING '.'"); proto.e2d_ino = h2fs32(idesc->id_number); proto.e2d_namlen = 1; if (sblock.e2fs.e2fs_rev > E2FS_REV0 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) proto.e2d_type = EXT2_FT_DIR; else proto.e2d_type = 0; (void)strlcpy(proto.e2d_name, ".", sizeof(proto.e2d_name)); entrysize = EXT2FS_DIRSIZ(proto.e2d_namlen); if (fs2h32(dirp->e2d_ino) != 0 && strcmp(dirp->e2d_name, "..") != 0) { pfatal("CANNOT FIX, FIRST ENTRY IN DIRECTORY CONTAINS %s\n", dirp->e2d_name); } else if (fs2h16(dirp->e2d_reclen) < entrysize) { pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '.'\n"); } else if (fs2h16(dirp->e2d_reclen) < 2 * entrysize) { proto.e2d_reclen = dirp->e2d_reclen; memcpy(dirp, &proto, (size_t)entrysize); if (reply("FIX") == 1) ret |= ALTERED; } else { n = fs2h16(dirp->e2d_reclen) - entrysize; proto.e2d_reclen = h2fs16(entrysize); memcpy(dirp, &proto, (size_t)entrysize); idesc->id_entryno++; lncntp[fs2h32(dirp->e2d_ino)]--; dirp = (struct ext2fs_direct *)((char *)(dirp) + entrysize); memset(dirp, 0, (size_t)n); dirp->e2d_reclen = h2fs16(n); if (reply("FIX") == 1) ret |= ALTERED; } chk1: if (idesc->id_entryno > 1) goto chk2; inp = getinoinfo(idesc->id_number); proto.e2d_ino = h2fs32(inp->i_parent); proto.e2d_namlen = 2; if (sblock.e2fs.e2fs_rev > E2FS_REV0 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE)) proto.e2d_type = EXT2_FT_DIR; else proto.e2d_type = 0; (void)strlcpy(proto.e2d_name, "..", sizeof(proto.e2d_name)); entrysize = EXT2FS_DIRSIZ(2); if (idesc->id_entryno == 0) { n = EXT2FS_DIRSIZ(dirp->e2d_namlen); if (fs2h16(dirp->e2d_reclen) < n + entrysize) goto chk2; proto.e2d_reclen = h2fs16(fs2h16(dirp->e2d_reclen) - n); dirp->e2d_reclen = h2fs16(n); idesc->id_entryno++; lncntp[fs2h32(dirp->e2d_ino)]--; dirp = (struct ext2fs_direct *)((char *)(dirp) + n); memset(dirp, 0, (size_t)fs2h16(proto.e2d_reclen)); dirp->e2d_reclen = proto.e2d_reclen; } if (fs2h32(dirp->e2d_ino) != 0 && dirp->e2d_namlen == 2 && strncmp(dirp->e2d_name, "..", 2) == 0) { inp->i_dotdot = fs2h32(dirp->e2d_ino); if (sblock.e2fs.e2fs_rev > E2FS_REV0 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) && dirp->e2d_type != EXT2_FT_DIR) { direrror(idesc->id_number, "BAD TYPE VALUE FOR '..'"); dirp->e2d_type = EXT2_FT_DIR; if (reply("FIX") == 1) ret |= ALTERED; } goto chk2; } if (fs2h32(dirp->e2d_ino) != 0 && dirp->e2d_namlen == 1 && strncmp(dirp->e2d_name, ".", 1) != 0) { fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); pfatal("CANNOT FIX, SECOND ENTRY IN DIRECTORY CONTAINS %s\n", dirp->e2d_name); inp->i_dotdot = (ino_t)-1; } else if (fs2h16(dirp->e2d_reclen) < entrysize) { fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); pfatal("CANNOT FIX, INSUFFICIENT SPACE TO ADD '..'\n"); inp->i_dotdot = (ino_t)-1; } else if (inp->i_parent != 0) { /* * We know the parent, so fix now. */ inp->i_dotdot = inp->i_parent; fileerror(inp->i_parent, idesc->id_number, "MISSING '..'"); proto.e2d_reclen = dirp->e2d_reclen; memcpy(dirp, &proto, (size_t)entrysize); if (reply("FIX") == 1) ret |= ALTERED; } idesc->id_entryno++; if (fs2h32(dirp->e2d_ino) != 0) lncntp[fs2h32(dirp->e2d_ino)]--; return (ret|KEEPON); chk2: if (fs2h32(dirp->e2d_ino) == 0) return (ret|KEEPON); if (dirp->e2d_namlen <= 2 && dirp->e2d_name[0] == '.' && idesc->id_entryno >= 2) { if (dirp->e2d_namlen == 1) { direrror(idesc->id_number, "EXTRA '.' ENTRY"); dirp->e2d_ino = 0; if (reply("FIX") == 1) ret |= ALTERED; return (KEEPON | ret); } if (dirp->e2d_name[1] == '.') { direrror(idesc->id_number, "EXTRA '..' ENTRY"); dirp->e2d_ino = 0; if (reply("FIX") == 1) ret |= ALTERED; return (KEEPON | ret); } } idesc->id_entryno++; n = 0; if (fs2h32(dirp->e2d_ino) > maxino || (fs2h32(dirp->e2d_ino) < EXT2_FIRSTINO && fs2h32(dirp->e2d_ino) != EXT2_ROOTINO)) { fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "I OUT OF RANGE"); n = reply("REMOVE"); } else { again: switch (statemap[fs2h32(dirp->e2d_ino)]) { case USTATE: if (idesc->id_entryno <= 2) break; fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "UNALLOCATED"); n = reply("REMOVE"); break; case DCLEAR: case FCLEAR: if (idesc->id_entryno <= 2) break; if (statemap[fs2h32(dirp->e2d_ino)] == FCLEAR) errmsg = "DUP/BAD"; else if (!preen) errmsg = "ZERO LENGTH DIRECTORY"; else { n = 1; break; } fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), errmsg); if ((n = reply("REMOVE")) == 1) break; dp = ginode(fs2h32(dirp->e2d_ino)); statemap[fs2h32(dirp->e2d_ino)] = (fs2h16(dp->e2di_mode) & IFMT) == IFDIR ? DSTATE : FSTATE; lncntp[fs2h32(dirp->e2d_ino)] = fs2h16(dp->e2di_nlink); goto again; case DSTATE: case DFOUND: inp = getinoinfo(fs2h32(dirp->e2d_ino)); if (inp->i_parent != 0 && idesc->id_entryno > 2) { getpathname(pathbuf, sizeof(pathbuf), idesc->id_number, idesc->id_number); getpathname(namebuf, sizeof(namebuf), fs2h32(dirp->e2d_ino), fs2h32(dirp->e2d_ino)); pwarn("%s %s %s\n", pathbuf, "IS AN EXTRANEOUS HARD LINK TO DIRECTORY", namebuf); if (preen) printf(" (IGNORED)\n"); else if ((n = reply("REMOVE")) == 1) break; } if (idesc->id_entryno > 2) inp->i_parent = idesc->id_number; /* fall through */ case FSTATE: if (sblock.e2fs.e2fs_rev > E2FS_REV0 && (sblock.e2fs.e2fs_features_incompat & EXT2F_INCOMPAT_FTYPE) && dirp->e2d_type != inot2ext2dt(typemap[fs2h32(dirp->e2d_ino)])) { dirp->e2d_type = inot2ext2dt(typemap[fs2h32(dirp->e2d_ino)]); fileerror(idesc->id_number, fs2h32(dirp->e2d_ino), "BAD TYPE VALUE"); if (reply("FIX") == 1) ret |= ALTERED; } lncntp[fs2h32(dirp->e2d_ino)]--; break; default: errexit("BAD STATE %d FOR INODE I=%d", statemap[fs2h32(dirp->e2d_ino)], fs2h32(dirp->e2d_ino)); } } if (n == 0) return (ret|KEEPON); dirp->e2d_ino = 0; return (ret|KEEPON|ALTERED); }
void pass5(void) { int c; struct m_ext2fs *fs = &sblock; daddr_t dbase, dmax; daddr_t d; long i, j; struct inodesc idesc[3]; struct bufarea *ino_bitmap = NULL, *blk_bitmap = NULL; char *ibmap, *bbmap; u_int32_t cs_ndir, cs_nbfree, cs_nifree; char msg[255]; cs_ndir = 0; cs_nbfree = 0; cs_nifree = 0; ibmap = malloc(fs->e2fs_bsize); bbmap = malloc(fs->e2fs_bsize); if (ibmap == NULL || bbmap == NULL) { errexit("out of memory\n"); } for (c = 0; c < fs->e2fs_ncg; c++) { u_int32_t nbfree = 0; u_int32_t nifree = 0; u_int32_t ndirs = 0; nbfree = 0; nifree = fs->e2fs.e2fs_ipg; ndirs = 0; if (blk_bitmap == NULL) { blk_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap), fs->e2fs_bsize); } else { getblk(blk_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_b_bitmap), fs->e2fs_bsize); } if (ino_bitmap == NULL) { ino_bitmap = getdatablk(fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap), fs->e2fs_bsize); } else { getblk(ino_bitmap, fs2h32(fs->e2fs_gd[c].ext2bgd_i_bitmap), fs->e2fs_bsize); } memset(bbmap, 0, fs->e2fs_bsize); memset(ibmap, 0, fs->e2fs_bsize); memset(&idesc[0], 0, sizeof idesc); for (i = 0; i < 3; i++) { idesc[i].id_type = ADDR; } j = fs->e2fs.e2fs_ipg * c + 1; for (i = 0; i < fs->e2fs.e2fs_ipg; j++, i++) { if ((j < EXT2_FIRSTINO) && (j != EXT2_ROOTINO)) { setbit(ibmap, i); nifree--; continue; } if (j > fs->e2fs.e2fs_icount) { setbit(ibmap, i); continue; } switch (statemap[j]) { case USTATE: break; case DSTATE: case DCLEAR: case DFOUND: ndirs++; /* fall through */ case FSTATE: case FCLEAR: nifree--; setbit(ibmap, i); break; default: errexit("BAD STATE %d FOR INODE I=%ld\n", statemap[j], j); } } /* fill in unused par of the inode map */ for (i = fs->e2fs.e2fs_ipg / NBBY; i < fs->e2fs_bsize; i++) ibmap[i] = 0xff; dbase = c * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock; dmax = (c+1) * sblock.e2fs.e2fs_bpg + sblock.e2fs.e2fs_first_dblock; for (i = 0, d = dbase; d < dmax; d ++, i ++) { if (testbmap(d) || d >= sblock.e2fs.e2fs_bcount) { setbit(bbmap, i); continue; } else { nbfree++; } } cs_nbfree += nbfree; cs_nifree += nifree; cs_ndir += ndirs; if (debug && (fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree || fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree || fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs)) { printf("summary info for cg %d is %d, %d, %d," "should be %d, %d, %d\n", c, fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree), fs2h16(fs->e2fs_gd[c].ext2bgd_nifree), fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs), nbfree, nifree, ndirs); } (void)snprintf(msg, sizeof(msg), "SUMMARY INFORMATIONS WRONG FOR CG #%d", c); if ((fs2h16(fs->e2fs_gd[c].ext2bgd_nbfree) != nbfree || fs2h16(fs->e2fs_gd[c].ext2bgd_nifree) != nifree || fs2h16(fs->e2fs_gd[c].ext2bgd_ndirs) != ndirs) && dofix(&idesc[0], msg)) { fs->e2fs_gd[c].ext2bgd_nbfree = h2fs16(nbfree); fs->e2fs_gd[c].ext2bgd_nifree = h2fs16(nifree); fs->e2fs_gd[c].ext2bgd_ndirs = h2fs16(ndirs); sbdirty(); } if (debug && memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize)) { printf("blk_bitmap:\n"); print_bmap(blk_bitmap->b_un.b_buf, fs->e2fs_bsize); printf("bbmap:\n"); print_bmap(bbmap, fs->e2fs_bsize); } (void)snprintf(msg, sizeof(msg), "BLK(S) MISSING IN BIT MAPS #%d", c); if (memcmp(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize) && dofix(&idesc[1], msg)) { memcpy(blk_bitmap->b_un.b_buf, bbmap, fs->e2fs_bsize); dirty(blk_bitmap); } if (debug && memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize)) { printf("ino_bitmap:\n"); print_bmap(ino_bitmap->b_un.b_buf, fs->e2fs_bsize); printf("ibmap:\n"); print_bmap(ibmap, fs->e2fs_bsize); } (void)snprintf(msg, sizeof(msg), "INODE(S) MISSING IN BIT MAPS #%d", c); if (memcmp(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize) && dofix(&idesc[1], msg)) { memcpy(ino_bitmap->b_un.b_buf, ibmap, fs->e2fs_bsize); dirty(ino_bitmap); } } if (debug && (fs->e2fs.e2fs_fbcount != cs_nbfree || fs->e2fs.e2fs_ficount != cs_nifree)) { printf("summary info bad in superblock: %d, %d should be %d, %d\n", fs->e2fs.e2fs_fbcount, fs->e2fs.e2fs_ficount, cs_nbfree, cs_nifree); } if ((fs->e2fs.e2fs_fbcount != cs_nbfree || fs->e2fs.e2fs_ficount != cs_nifree) && dofix(&idesc[0], "SUPERBLK SUMMARY INFORMATION BAD")) { fs->e2fs.e2fs_fbcount = cs_nbfree; fs->e2fs.e2fs_ficount = cs_nifree; sbdirty(); } free(ibmap); free(bbmap); }