Esempio n. 1
0
int hfs_extend_file(struct inode *inode)
{
	struct super_block *sb = inode->i_sb;
	u32 start, len, goal;
	int res;

	mutex_lock(&HFS_I(inode)->extents_lock);
	if (HFS_I(inode)->alloc_blocks == HFS_I(inode)->first_blocks)
		goal = hfs_ext_lastblock(HFS_I(inode)->first_extents);
	else {
		res = hfs_ext_read_extent(inode, HFS_I(inode)->alloc_blocks);
		if (res)
			goto out;
		goal = hfs_ext_lastblock(HFS_I(inode)->cached_extents);
	}

	len = HFS_I(inode)->clump_blocks;
	start = hfs_vbm_search_free(sb, goal, &len);
	if (!len) {
		res = -ENOSPC;
		goto out;
	}

	dprint(DBG_EXTENT, "extend %lu: %u,%u\n", inode->i_ino, start, len);
	if (HFS_I(inode)->alloc_blocks == HFS_I(inode)->first_blocks) {
		if (!HFS_I(inode)->first_blocks) {
			dprint(DBG_EXTENT, "first extents\n");
			/* no extents yet */
			HFS_I(inode)->first_extents[0].block = cpu_to_be16(start);
			HFS_I(inode)->first_extents[0].count = cpu_to_be16(len);
			res = 0;
		} else {
			/* try to append to extents in inode */
			res = hfs_add_extent(HFS_I(inode)->first_extents,
					     HFS_I(inode)->alloc_blocks,
					     start, len);
			if (res == -ENOSPC)
				goto insert_extent;
		}
		if (!res) {
			hfs_dump_extent(HFS_I(inode)->first_extents);
			HFS_I(inode)->first_blocks += len;
		}
	} else {
		res = hfs_add_extent(HFS_I(inode)->cached_extents,
				     HFS_I(inode)->alloc_blocks -
				     HFS_I(inode)->cached_start,
				     start, len);
		if (!res) {
			hfs_dump_extent(HFS_I(inode)->cached_extents);
			HFS_I(inode)->flags |= HFS_FLG_EXT_DIRTY;
			HFS_I(inode)->cached_blocks += len;
		} else if (res == -ENOSPC)
			goto insert_extent;
	}
out:
	mutex_unlock(&HFS_I(inode)->extents_lock);
	if (!res) {
		HFS_I(inode)->alloc_blocks += len;
		mark_inode_dirty(inode);
		if (inode->i_ino < HFS_FIRSTUSER_CNID)
			set_bit(HFS_FLG_ALT_MDB_DIRTY, &HFS_SB(sb)->flags);
		set_bit(HFS_FLG_MDB_DIRTY, &HFS_SB(sb)->flags);
		mark_sb_dirty(sb);
	}
	return res;

insert_extent:
	dprint(DBG_EXTENT, "insert new extent\n");
	hfs_ext_write_extent(inode);

	memset(HFS_I(inode)->cached_extents, 0, sizeof(hfs_extent_rec));
	HFS_I(inode)->cached_extents[0].block = cpu_to_be16(start);
	HFS_I(inode)->cached_extents[0].count = cpu_to_be16(len);
	hfs_dump_extent(HFS_I(inode)->cached_extents);
	HFS_I(inode)->flags |= HFS_FLG_EXT_DIRTY|HFS_FLG_EXT_NEW;
	HFS_I(inode)->cached_start = HFS_I(inode)->alloc_blocks;
	HFS_I(inode)->cached_blocks = len;

	res = 0;
	goto out;
}
Esempio n. 2
0
/*
 * grow_fork()
 *
 * Try to add enough allocation blocks to 'fork'
 * so that it is 'ablock' allocation blocks long.
 */
static int grow_fork(struct hfs_fork *fork, int ablocks)
{
    struct hfs_cat_entry *entry = fork->entry;
    struct hfs_mdb *mdb = entry->mdb;
    struct hfs_extent *ext;
    int i, start, err;
    hfs_u16 need, len=0;
    hfs_u32 ablksz = mdb->alloc_blksz;
    hfs_u32 blocks, clumpablks;

    blocks = fork->psize;
    need = ablocks - blocks/ablksz;
    if (need < 1) { /* no need to grow the fork */
        return 0;
    }

    /* round up to clumpsize */
    if (entry->u.file.clumpablks) {
        clumpablks = entry->u.file.clumpablks;
    } else {
        clumpablks = mdb->clumpablks;
    }
    need = ((need + clumpablks - 1) / clumpablks) * clumpablks;

    /* find last extent record and try to extend it */
    if (!(ext = find_ext(fork, blocks/ablksz - 1))) {
        /* somehow we couldn't find the end of the file! */
        return -1;
    }

    /* determine which is the last used extent in the record */
    /* then try to allocate the blocks immediately following it */
    for (i=2; (i>=0) && !ext->length[i]; --i) {};
    if (i>=0) {
        /* try to extend the last extent */
        start = ext->block[i] + ext->length[i];

        err = 0;
        lock_bitmap(mdb);
        len = hfs_vbm_count_free(mdb, start);
        if (!len) {
            unlock_bitmap(mdb);
            goto more_extents;
        }
        if (need < len) {
            len = need;
        }
        err = hfs_set_vbm_bits(mdb, start, len);
        unlock_bitmap(mdb);
        if (err) {
            relse_ext(ext);
            return -1;
        }

        zero_blocks(mdb, start, len);

        ext->length[i] += len;
        ext->end += len;
        blocks = (fork->psize += len * ablksz);
        need -= len;
        update_ext(fork, ext);
    }

more_extents:
    /* add some more extents */
    while (need) {
        len = need;
        err = 0;
        lock_bitmap(mdb);
        start = hfs_vbm_search_free(mdb, &len);
        if (need < len) {
            len = need;
        }
        err = hfs_set_vbm_bits(mdb, start, len);
        unlock_bitmap(mdb);
        if (!len || err) {
            relse_ext(ext);
            return -1;
        }
        zero_blocks(mdb, start, len);

        /* determine which is the first free extent in the record */
        for (i=0; (i<3) && ext->length[i]; ++i) {};
        if (i < 3) {
            ext->block[i] = start;
            ext->length[i] = len;
            ext->end += len;
            update_ext(fork, ext);
        } else {
            if (!(ext = new_extent(fork, ext, blocks/ablksz,
                                   start, len, ablksz))) {
                lock_bitmap(mdb);
                hfs_clear_vbm_bits(mdb, start, len);
                unlock_bitmap(mdb);
                return -1;
            }
        }
        blocks = (fork->psize += len * ablksz);
        need -= len;
    }
    set_cache(fork, ext);
    relse_ext(ext);
    return 0;
}