Beispiel #1
0
/*
 * Allocate a block in the filesystem.
 *
 * A preference may be optionally specified. If a preference is given
 * the following hierarchy is used to allocate a block:
 *   1) allocate the requested block.
 *   2) allocate a rotationally optimal block in the same cylinder.
 *   3) allocate a block in the same cylinder group.
 *   4) quadradically rehash into other cylinder groups, until an
 *        available block is located.
 * If no block preference is given the following hierarchy is used
 * to allocate a block:
 *   1) allocate a block in the cylinder group that contains the
 *        inode for the file.
 *   2) quadradically rehash into other cylinder groups, until an
 *        available block is located.
 */
int
ext2_alloc(struct inode *ip, daddr_t lbn, e4fs_daddr_t bpref, int size,
           struct ucred *cred, e4fs_daddr_t *bnp)
{
    struct m_ext2fs *fs;
    struct ext2mount *ump;
    int32_t bno;
    int cg;
    *bnp = 0;
    fs = ip->i_e2fs;
    ump = ip->i_ump;
    mtx_assert(EXT2_MTX(ump), MA_OWNED);
#ifdef INVARIANTS
    if ((u_int)size > fs->e2fs_bsize || blkoff(fs, size) != 0) {
        vn_printf(ip->i_devvp, "bsize = %lu, size = %d, fs = %s\n",
                  (long unsigned int)fs->e2fs_bsize, size, fs->e2fs_fsmnt);
        panic("ext2_alloc: bad size");
    }
    if (cred == NOCRED)
        panic("ext2_alloc: missing credential");
#endif /* INVARIANTS */
    if (size == fs->e2fs_bsize && fs->e2fs->e2fs_fbcount == 0)
        goto nospace;
    if (cred->cr_uid != 0 &&
            fs->e2fs->e2fs_fbcount < fs->e2fs->e2fs_rbcount)
        goto nospace;
    if (bpref >= fs->e2fs->e2fs_bcount)
        bpref = 0;
    if (bpref == 0)
        cg = ino_to_cg(fs, ip->i_number);
    else
        cg = dtog(fs, bpref);
    bno = (daddr_t)ext2_hashalloc(ip, cg, bpref, fs->e2fs_bsize,
                                  ext2_alloccg);
    if (bno > 0) {
        /* set next_alloc fields as done in block_getblk */
        ip->i_next_alloc_block = lbn;
        ip->i_next_alloc_goal = bno;

        ip->i_blocks += btodb(fs->e2fs_bsize);
        ip->i_flag |= IN_CHANGE | IN_UPDATE;
        *bnp = bno;
        return (0);
    }
nospace:
    EXT2_UNLOCK(ump);
    ext2_fserr(fs, cred->cr_uid, "filesystem full");
    uprintf("\n%s: write failed, filesystem is full\n", fs->e2fs_fsmnt);
    return (ENOSPC);
}
Beispiel #2
0
/*
 * ext2_reallocblks(struct vnode *a_vp, struct cluster_save *a_buflist)
 */
int
ext2_reallocblks(struct vop_reallocblks_args *ap)
{
#ifndef FANCY_REALLOC
/* kprintf("ext2_reallocblks not implemented\n"); */
return ENOSPC;
#else

	struct ext2_sb_info *fs;
	struct inode *ip;
	struct vnode *vp;
	struct buf *sbp, *ebp;
	daddr_t *bap, *sbap, *ebap;
	struct cluster_save *buflist;
	daddr_t start_lbn, end_lbn, soff, eoff, newblk, blkno;
	struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
	int i, len, start_lvl, end_lvl, pref, ssize;

	vp = ap->a_vp;
	ip = VTOI(vp);
	fs = ip->i_e2fs;
#ifdef UNKLAR
	if (fs->fs_contigsumsize <= 0)
		return (ENOSPC);
#endif
	buflist = ap->a_buflist;
	len = buflist->bs_nchildren;
	start_lbn = lblkno(fs, buflist->bs_children[0]->b_loffset);
	end_lbn = start_lbn + len - 1;
#if DIAGNOSTIC
	for (i = 1; i < len; i++) {
		if (buflist->bs_children[i]->b_loffset != lblktodoff(fs, start_lbn) + lblktodoff(fs, i))
			panic("ext2_reallocblks: non-cluster");
	}
#endif
	/*
	 * If the latest allocation is in a new block group, assume that
	 * the filesystem has decided to move and do not force it back to
	 * the previous block group.
	 */
	if (dtog(fs, dofftofsb(fs, buflist->bs_children[0]->b_bio2.bio_offset)) !=
	    dtog(fs, dofftofsb(fs, buflist->bs_children[len - 1]->b_bio2.bio_offset)))
		return (ENOSPC);
	if (ext2_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
	    ext2_getlbns(vp, end_lbn, end_ap, &end_lvl))
		return (ENOSPC);
	/*
	 * Get the starting offset and block map for the first block.
	 */
	if (start_lvl == 0) {
		sbap = &ip->i_db[0];
		soff = start_lbn;
	} else {
		idp = &start_ap[start_lvl - 1];
		if (bread(vp, lblktodoff(fs, idp->in_lbn), (int)fs->s_blocksize, NOCRED, &sbp)) {
			brelse(sbp);
			return (ENOSPC);
		}
		sbap = (daddr_t *)sbp->b_data;
		soff = idp->in_off;
	}
	/*
	 * Find the preferred location for the cluster.
	 */
	pref = ext2_blkpref(ip, start_lbn, soff, sbap);
	/*
	 * If the block range spans two block maps, get the second map.
	 */
	if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
		ssize = len;
	} else {
#if DIAGNOSTIC
		if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
			panic("ext2_reallocblk: start == end");
#endif
		ssize = len - (idp->in_off + 1);
		if (bread(vp, lblktodoff(fs, idp->in_lbn), (int)fs->s_blocksize, NOCRED, &ebp))
			goto fail;
		ebap = (daddr_t *)ebp->b_data;
	}
	/*
	 * Search the block map looking for an allocation of the desired size.
	 */
	if ((newblk = (daddr_t)ext2_hashalloc(ip, dtog(fs, pref), (long)pref,
	    len, (u_long (*)())ext2_clusteralloc)) == 0)
		goto fail;
	/*
	 * We have found a new contiguous block.
	 *
	 * First we have to replace the old block pointers with the new
	 * block pointers in the inode and indirect blocks associated
	 * with the file.
	 */
	blkno = newblk;
	for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->s_frags_per_block) {
		if (i == ssize)
			bap = ebap;
#if DIAGNOSTIC
		if (buflist->bs_children[i]->b_bio2.bio_offset != fsbtodoff(fs, *bap))
			panic("ext2_reallocblks: alloc mismatch");
#endif
		*bap++ = blkno;
	}
	/*
	 * Next we must write out the modified inode and indirect blocks.
	 * For strict correctness, the writes should be synchronous since
	 * the old block values may have been written to disk. In practise
	 * they are almost never written, but if we are concerned about
	 * strict correctness, the `doasyncfree' flag should be set to zero.
	 *
	 * The test on `doasyncfree' should be changed to test a flag
	 * that shows whether the associated buffers and inodes have
	 * been written. The flag should be set when the cluster is
	 * started and cleared whenever the buffer or inode is flushed.
	 * We can then check below to see if it is set, and do the
	 * synchronous write only when it has been cleared.
	 */
	if (sbap != &ip->i_db[0]) {
		if (doasyncfree)
			bdwrite(sbp);
		else
			bwrite(sbp);
	} else {
		ip->i_flag |= IN_CHANGE | IN_UPDATE;
		if (!doasyncfree)
			EXT2_UPDATE(vp, 1);
	}
	if (ssize < len)
		if (doasyncfree)
			bdwrite(ebp);
		else
			bwrite(ebp);
	/*
	 * Last, free the old blocks and assign the new blocks to the buffers.
	 */
	for (blkno = newblk, i = 0; i < len; i++, blkno += fs->s_frags_per_block) {
		ext2_blkfree(ip, dofftofsb(fs, buflist->bs_children[i]->b_bio2.bio_offset),
		    fs->s_blocksize);
		buflist->bs_children[i]->b_bio2.bio_offset = fsbtodoff(fs, blkno);
	}
	return (0);

fail:
	if (ssize < len)
		brelse(ebp);
	if (sbap != &ip->i_db[0])
		brelse(sbp);
	return (ENOSPC);

#endif /* FANCY_REALLOC */
}
Beispiel #3
0
/*
 * Allocate an inode in the filesystem.
 *
 */
int
ext2_valloc(struct vnode *pvp, int mode, struct ucred *cred, struct vnode **vpp)
{
    struct timespec ts;
    struct inode *pip;
    struct m_ext2fs *fs;
    struct inode *ip;
    struct ext2mount *ump;
    ino_t ino, ipref;
    int i, error, cg;

    *vpp = NULL;
    pip = VTOI(pvp);
    fs = pip->i_e2fs;
    ump = pip->i_ump;

    EXT2_LOCK(ump);
    if (fs->e2fs->e2fs_ficount == 0)
        goto noinodes;
    /*
     * If it is a directory then obtain a cylinder group based on
     * ext2_dirpref else obtain it using ino_to_cg. The preferred inode is
     * always the next inode.
     */
    if ((mode & IFMT) == IFDIR) {
        cg = ext2_dirpref(pip);
        if (fs->e2fs_contigdirs[cg] < 255)
            fs->e2fs_contigdirs[cg]++;
    } else {
        cg = ino_to_cg(fs, pip->i_number);
        if (fs->e2fs_contigdirs[cg] > 0)
            fs->e2fs_contigdirs[cg]--;
    }
    ipref = cg * fs->e2fs->e2fs_ipg + 1;
    ino = (ino_t)ext2_hashalloc(pip, cg, (long)ipref, mode, ext2_nodealloccg);

    if (ino == 0)
        goto noinodes;
    error = VFS_VGET(pvp->v_mount, ino, LK_EXCLUSIVE, vpp);
    if (error) {
        ext2_vfree(pvp, ino, mode);
        return (error);
    }
    ip = VTOI(*vpp);

    /*
     * The question is whether using VGET was such good idea at all:
     * Linux doesn't read the old inode in when it is allocating a
     * new one. I will set at least i_size and i_blocks to zero.
     */
    ip->i_size = 0;
    ip->i_blocks = 0;
    ip->i_mode = 0;
    ip->i_flags = 0;
    /* now we want to make sure that the block pointers are zeroed out */
    for (i = 0; i < NDADDR; i++)
        ip->i_db[i] = 0;
    for (i = 0; i < NIADDR; i++)
        ip->i_ib[i] = 0;

    /*
     * Set up a new generation number for this inode.
     * XXX check if this makes sense in ext2
     */
    if (ip->i_gen == 0 || ++ip->i_gen == 0)
        ip->i_gen = random() / 2 + 1;

    vfs_timestamp(&ts);
    ip->i_birthtime = ts.tv_sec;
    ip->i_birthnsec = ts.tv_nsec;

    /*
    printf("ext2_valloc: allocated inode %d\n", ino);
    */
    return (0);
noinodes:
    EXT2_UNLOCK(ump);
    ext2_fserr(fs, cred->cr_uid, "out of inodes");
    uprintf("\n%s: create/symlink failed, no inodes free\n", fs->e2fs_fsmnt);
    return (ENOSPC);
}
Beispiel #4
0
int
ext2_reallocblks(struct vop_reallocblks_args *ap)
{
    struct m_ext2fs *fs;
    struct inode *ip;
    struct vnode *vp;
    struct buf *sbp, *ebp;
    uint32_t *bap, *sbap, *ebap = 0;
    struct ext2mount *ump;
    struct cluster_save *buflist;
    struct indir start_ap[NIADDR + 1], end_ap[NIADDR + 1], *idp;
    e2fs_lbn_t start_lbn, end_lbn;
    int soff;
    e2fs_daddr_t newblk, blkno;
    int i, len, start_lvl, end_lvl, pref, ssize;

    if (doreallocblks == 0)
        return (ENOSPC);

    vp = ap->a_vp;
    ip = VTOI(vp);
    fs = ip->i_e2fs;
    ump = ip->i_ump;

    if (fs->e2fs_contigsumsize <= 0)
        return (ENOSPC);

    buflist = ap->a_buflist;
    len = buflist->bs_nchildren;
    start_lbn = buflist->bs_children[0]->b_lblkno;
    end_lbn = start_lbn + len - 1;
#ifdef INVARIANTS
    for (i = 1; i < len; i++)
        if (buflist->bs_children[i]->b_lblkno != start_lbn + i)
            panic("ext2_reallocblks: non-cluster");
#endif
    /*
     * If the cluster crosses the boundary for the first indirect
     * block, leave space for the indirect block. Indirect blocks
     * are initially laid out in a position after the last direct
     * block. Block reallocation would usually destroy locality by
     * moving the indirect block out of the way to make room for
     * data blocks if we didn't compensate here. We should also do
     * this for other indirect block boundaries, but it is only
     * important for the first one.
     */
    if (start_lbn < NDADDR && end_lbn >= NDADDR)
        return (ENOSPC);
    /*
     * If the latest allocation is in a new cylinder group, assume that
     * the filesystem has decided to move and do not force it back to
     * the previous cylinder group.
     */
    if (dtog(fs, dbtofsb(fs, buflist->bs_children[0]->b_blkno)) !=
            dtog(fs, dbtofsb(fs, buflist->bs_children[len - 1]->b_blkno)))
        return (ENOSPC);
    if (ext2_getlbns(vp, start_lbn, start_ap, &start_lvl) ||
            ext2_getlbns(vp, end_lbn, end_ap, &end_lvl))
        return (ENOSPC);
    /*
     * Get the starting offset and block map for the first block.
     */
    if (start_lvl == 0) {
        sbap = &ip->i_db[0];
        soff = start_lbn;
    } else {
        idp = &start_ap[start_lvl - 1];
        if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &sbp)) {
            brelse(sbp);
            return (ENOSPC);
        }
        sbap = (u_int *)sbp->b_data;
        soff = idp->in_off;
    }
    /*
     * If the block range spans two block maps, get the second map.
     */
    if (end_lvl == 0 || (idp = &end_ap[end_lvl - 1])->in_off + 1 >= len) {
        ssize = len;
    } else {
#ifdef INVARIANTS
        if (start_ap[start_lvl-1].in_lbn == idp->in_lbn)
            panic("ext2_reallocblks: start == end");
#endif
        ssize = len - (idp->in_off + 1);
        if (bread(vp, idp->in_lbn, (int)fs->e2fs_bsize, NOCRED, &ebp))
            goto fail;
        ebap = (u_int *)ebp->b_data;
    }
    /*
     * Find the preferred location for the cluster.
     */
    EXT2_LOCK(ump);
    pref = ext2_blkpref(ip, start_lbn, soff, sbap, 0);
    /*
     * Search the block map looking for an allocation of the desired size.
     */
    if ((newblk = (e2fs_daddr_t)ext2_hashalloc(ip, dtog(fs, pref), pref,
                  len, ext2_clusteralloc)) == 0) {
        EXT2_UNLOCK(ump);
        goto fail;
    }
    /*
     * We have found a new contiguous block.
     *
     * First we have to replace the old block pointers with the new
     * block pointers in the inode and indirect blocks associated
     * with the file.
     */
#ifdef DEBUG
    printf("realloc: ino %d, lbns %jd-%jd\n\told:", ip->i_number,
           (intmax_t)start_lbn, (intmax_t)end_lbn);
#endif /* DEBUG */
    blkno = newblk;
    for (bap = &sbap[soff], i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
        if (i == ssize) {
            bap = ebap;
            soff = -i;
        }
#ifdef INVARIANTS
        if (buflist->bs_children[i]->b_blkno != fsbtodb(fs, *bap))
            panic("ext2_reallocblks: alloc mismatch");
#endif
#ifdef DEBUG
        printf(" %d,", *bap);
#endif /* DEBUG */
        *bap++ = blkno;
    }
    /*
     * Next we must write out the modified inode and indirect blocks.
     * For strict correctness, the writes should be synchronous since
     * the old block values may have been written to disk. In practise
     * they are almost never written, but if we are concerned about
     * strict correctness, the `doasyncfree' flag should be set to zero.
     *
     * The test on `doasyncfree' should be changed to test a flag
     * that shows whether the associated buffers and inodes have
     * been written. The flag should be set when the cluster is
     * started and cleared whenever the buffer or inode is flushed.
     * We can then check below to see if it is set, and do the
     * synchronous write only when it has been cleared.
     */
    if (sbap != &ip->i_db[0]) {
        if (doasyncfree)
            bdwrite(sbp);
        else
            bwrite(sbp);
    } else {
        ip->i_flag |= IN_CHANGE | IN_UPDATE;
        if (!doasyncfree)
            ext2_update(vp, 1);
    }
    if (ssize < len) {
        if (doasyncfree)
            bdwrite(ebp);
        else
            bwrite(ebp);
    }
    /*
     * Last, free the old blocks and assign the new blocks to the buffers.
     */
#ifdef DEBUG
    printf("\n\tnew:");
#endif /* DEBUG */
    for (blkno = newblk, i = 0; i < len; i++, blkno += fs->e2fs_fpb) {
        ext2_blkfree(ip, dbtofsb(fs, buflist->bs_children[i]->b_blkno),
                     fs->e2fs_bsize);
        buflist->bs_children[i]->b_blkno = fsbtodb(fs, blkno);
#ifdef DEBUG
        printf(" %d,", blkno);
#endif /* DEBUG */
    }
#ifdef DEBUG
    printf("\n");
#endif /* DEBUG */
    return (0);

fail:
    if (ssize < len)
        brelse(ebp);
    if (sbap != &ip->i_db[0])
        brelse(sbp);
    return (ENOSPC);
}