#include <asm/uaccess.h>
+/*
+ * ext_pblock:
+ * combine low and high parts of physical block number into ext4_fsblk_t
+ */
+static ext4_fsblk_t ext_pblock(struct ext4_extent *ex)
+{
+ ext4_fsblk_t block;
+
+ block = le32_to_cpu(ex->ee_start);
+ block |= ((ext4_fsblk_t) le16_to_cpu(ex->ee_start_hi) << 31) << 1;
+ return block;
+}
+
+/*
+ * idx_pblock:
+ * combine low and high parts of a leaf physical block number into ext4_fsblk_t
+ */
+static ext4_fsblk_t idx_pblock(struct ext4_extent_idx *ix)
+{
+ ext4_fsblk_t block;
+
+ block = le32_to_cpu(ix->ei_leaf);
+ block |= ((ext4_fsblk_t) le16_to_cpu(ix->ei_leaf_hi) << 31) << 1;
+ return block;
+}
+
+/*
+ * ext4_ext_store_pblock:
+ * stores a large physical block number into an extent struct,
+ * breaking it into parts
+ */
+static void ext4_ext_store_pblock(struct ext4_extent *ex, ext4_fsblk_t pb)
+{
+ ex->ee_start = cpu_to_le32((unsigned long) (pb & 0xffffffff));
+ ex->ee_start_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
+}
+
+/*
+ * ext4_idx_store_pblock:
+ * stores a large physical block number into an index struct,
+ * breaking it into parts
+ */
+static void ext4_idx_store_pblock(struct ext4_extent_idx *ix, ext4_fsblk_t pb)
+{
+ ix->ei_leaf = cpu_to_le32((unsigned long) (pb & 0xffffffff));
+ ix->ei_leaf_hi = cpu_to_le16((unsigned long) ((pb >> 31) >> 1) & 0xffff);
+}
+
static int ext4_ext_check_header(const char *function, struct inode *inode,
struct ext4_extent_header *eh)
{
return err;
}
-static int ext4_ext_find_goal(struct inode *inode,
+static ext4_fsblk_t ext4_ext_find_goal(struct inode *inode,
struct ext4_ext_path *path,
- unsigned long block)
+ ext4_fsblk_t block)
{
struct ext4_inode_info *ei = EXT4_I(inode);
- unsigned long bg_start;
- unsigned long colour;
+ ext4_fsblk_t bg_start;
+ ext4_grpblk_t colour;
int depth;
if (path) {
depth = path->p_depth;
/* try to predict block placement */
- if ((ex = path[depth].p_ext))
- return le32_to_cpu(ex->ee_start)
- + (block - le32_to_cpu(ex->ee_block));
+ ex = path[depth].p_ext;
+ if (ex)
+ return ext_pblock(ex)+(block-le32_to_cpu(ex->ee_block));
- /* it looks index is empty
- * try to find starting from index itself */
+ /* it looks like index is empty;
+ * try to find starting block from index itself */
if (path[depth].p_bh)
return path[depth].p_bh->b_blocknr;
}
return bg_start + colour + block;
}
-static int
+static ext4_fsblk_t
ext4_ext_new_block(handle_t *handle, struct inode *inode,
struct ext4_ext_path *path,
struct ext4_extent *ex, int *err)
{
- int goal, newblock;
+ ext4_fsblk_t goal, newblock;
goal = ext4_ext_find_goal(inode, path, le32_to_cpu(ex->ee_block));
newblock = ext4_new_block(handle, inode, goal, err);
return newblock;
}
-static inline int ext4_ext_space_block(struct inode *inode)
+static int ext4_ext_space_block(struct inode *inode)
{
int size;
size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
/ sizeof(struct ext4_extent);
-#ifdef AGRESSIVE_TEST
+#ifdef AGGRESSIVE_TEST
if (size > 6)
size = 6;
#endif
return size;
}
-static inline int ext4_ext_space_block_idx(struct inode *inode)
+static int ext4_ext_space_block_idx(struct inode *inode)
{
int size;
size = (inode->i_sb->s_blocksize - sizeof(struct ext4_extent_header))
/ sizeof(struct ext4_extent_idx);
-#ifdef AGRESSIVE_TEST
+#ifdef AGGRESSIVE_TEST
if (size > 5)
size = 5;
#endif
return size;
}
-static inline int ext4_ext_space_root(struct inode *inode)
+static int ext4_ext_space_root(struct inode *inode)
{
int size;
size = sizeof(EXT4_I(inode)->i_data);
size -= sizeof(struct ext4_extent_header);
size /= sizeof(struct ext4_extent);
-#ifdef AGRESSIVE_TEST
+#ifdef AGGRESSIVE_TEST
if (size > 3)
size = 3;
#endif
return size;
}
-static inline int ext4_ext_space_root_idx(struct inode *inode)
+static int ext4_ext_space_root_idx(struct inode *inode)
{
int size;
size = sizeof(EXT4_I(inode)->i_data);
size -= sizeof(struct ext4_extent_header);
size /= sizeof(struct ext4_extent_idx);
-#ifdef AGRESSIVE_TEST
+#ifdef AGGRESSIVE_TEST
if (size > 4)
size = 4;
#endif
ext_debug("path:");
for (k = 0; k <= l; k++, path++) {
if (path->p_idx) {
- ext_debug(" %d->%d", le32_to_cpu(path->p_idx->ei_block),
- le32_to_cpu(path->p_idx->ei_leaf));
+ ext_debug(" %d->%llu", le32_to_cpu(path->p_idx->ei_block),
+ idx_pblock(path->p_idx));
} else if (path->p_ext) {
- ext_debug(" %d:%d:%d",
+ ext_debug(" %d:%d:%llu ",
le32_to_cpu(path->p_ext->ee_block),
le16_to_cpu(path->p_ext->ee_len),
- le32_to_cpu(path->p_ext->ee_start));
+ ext_pblock(path->p_ext));
} else
ext_debug(" []");
}
ex = EXT_FIRST_EXTENT(eh);
for (i = 0; i < le16_to_cpu(eh->eh_entries); i++, ex++) {
- ext_debug("%d:%d:%d ", le32_to_cpu(ex->ee_block),
- le16_to_cpu(ex->ee_len),
- le32_to_cpu(ex->ee_start));
+ ext_debug("%d:%d:%llu ", le32_to_cpu(ex->ee_block),
+ le16_to_cpu(ex->ee_len), ext_pblock(ex));
}
ext_debug("\n");
}
}
/*
- * binary search for closest index by given block
+ * ext4_ext_binsearch_idx:
+ * binary search for the closest index of the given block
*/
static void
ext4_ext_binsearch_idx(struct inode *inode, struct ext4_ext_path *path, int block)
}
path->p_idx = l - 1;
- ext_debug(" -> %d->%d ", le32_to_cpu(path->p_idx->ei_block),
- le32_to_cpu(path->p_idx->ei_leaf));
+ ext_debug(" -> %d->%lld ", le32_to_cpu(path->p_idx->ei_block),
+ idx_block(path->p_idx));
#ifdef CHECK_BINSEARCH
{
}
/*
- * binary search for closest extent by given block
+ * ext4_ext_binsearch:
+ * binary search for closest extent of the given block
*/
static void
ext4_ext_binsearch(struct inode *inode, struct ext4_ext_path *path, int block)
if (eh->eh_entries == 0) {
/*
- * this leaf is empty yet:
- * we get such a leaf in split/add case
+ * this leaf is empty:
+ * we get such a leaf in split/add case
*/
return;
}
}
path->p_ext = l - 1;
- ext_debug(" -> %d:%d:%d ",
+ ext_debug(" -> %d:%llu:%d ",
le32_to_cpu(path->p_ext->ee_block),
- le32_to_cpu(path->p_ext->ee_start),
- le16_to_cpu(path->p_ext->ee_len));
+ ext_pblock(path->p_ext),
+ le16_to_cpu(path->p_ext->ee_len));
#ifdef CHECK_BINSEARCH
{
/* account possible depth increase */
if (!path) {
- path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 2),
+ path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 2),
GFP_NOFS);
if (!path)
return ERR_PTR(-ENOMEM);
alloc = 1;
}
- memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
path[0].p_hdr = eh;
/* walk through the tree */
ext_debug("depth %d: num %d, max %d\n",
ppos, le16_to_cpu(eh->eh_entries), le16_to_cpu(eh->eh_max));
ext4_ext_binsearch_idx(inode, path + ppos, block);
- path[ppos].p_block = le32_to_cpu(path[ppos].p_idx->ei_leaf);
+ path[ppos].p_block = idx_pblock(path[ppos].p_idx);
path[ppos].p_depth = i;
path[ppos].p_ext = NULL;
}
/*
- * insert new index [logical;ptr] into the block at cupr
- * it check where to insert: before curp or after curp
+ * ext4_ext_insert_index:
+ * insert new index [@logical;@ptr] into the block at @curp;
+ * check where to insert: before @curp or after @curp
*/
static int ext4_ext_insert_index(handle_t *handle, struct inode *inode,
struct ext4_ext_path *curp,
- int logical, int ptr)
+ int logical, ext4_fsblk_t ptr)
{
struct ext4_extent_idx *ix;
int len, err;
- if ((err = ext4_ext_get_access(handle, inode, curp)))
+ err = ext4_ext_get_access(handle, inode, curp);
+ if (err)
return err;
BUG_ON(logical == le32_to_cpu(curp->p_idx->ei_block));
}
ix->ei_block = cpu_to_le32(logical);
- ix->ei_leaf = cpu_to_le32(ptr);
+ ext4_idx_store_pblock(ix, ptr);
curp->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(curp->p_hdr->eh_entries)+1);
BUG_ON(le16_to_cpu(curp->p_hdr->eh_entries)
}
/*
- * routine inserts new subtree into the path, using free index entry
- * at depth 'at:
- * - allocates all needed blocks (new leaf and all intermediate index blocks)
- * - makes decision where to split
- * - moves remaining extens and index entries (right to the split point)
- * into the newly allocated blocks
- * - initialize subtree
+ * ext4_ext_split:
+ * inserts new subtree into the path, using free index entry
+ * at depth @at:
+ * - allocates all needed blocks (new leaf and all intermediate index blocks)
+ * - makes decision where to split
+ * - moves remaining extents and index entries (right to the split point)
+ * into the newly allocated blocks
+ * - initializes subtree
*/
static int ext4_ext_split(handle_t *handle, struct inode *inode,
struct ext4_ext_path *path,
struct ext4_extent_idx *fidx;
struct ext4_extent *ex;
int i = at, k, m, a;
- unsigned long newblock, oldblock;
+ ext4_fsblk_t newblock, oldblock;
__le32 border;
- int *ablocks = NULL; /* array of allocated blocks */
+ ext4_fsblk_t *ablocks = NULL; /* array of allocated blocks */
int err = 0;
/* make decision: where to split? */
- /* FIXME: now desicion is simplest: at current extent */
+ /* FIXME: now decision is simplest: at current extent */
- /* if current leaf will be splitted, then we should use
+ /* if current leaf will be split, then we should use
* border from split point */
BUG_ON(path[depth].p_ext > EXT_MAX_EXTENT(path[depth].p_hdr));
if (path[depth].p_ext != EXT_MAX_EXTENT(path[depth].p_hdr)) {
border = path[depth].p_ext[1].ee_block;
- ext_debug("leaf will be splitted."
+ ext_debug("leaf will be split."
" next leaf starts at %d\n",
le32_to_cpu(border));
} else {
}
/*
- * if error occurs, then we break processing
- * and turn filesystem read-only. so, index won't
+ * If error occurs, then we break processing
+ * and mark filesystem read-only. index won't
* be inserted and tree will be in consistent
- * state. next mount will repair buffers too
+ * state. Next mount will repair buffers too.
*/
/*
- * get array to track all allocated blocks
- * we need this to handle errors and free blocks
- * upon them
+ * Get array to track all allocated blocks.
+ * We need this to handle errors and free blocks
+ * upon them.
*/
- ablocks = kmalloc(sizeof(unsigned long) * depth, GFP_NOFS);
+ ablocks = kzalloc(sizeof(ext4_fsblk_t) * depth, GFP_NOFS);
if (!ablocks)
return -ENOMEM;
- memset(ablocks, 0, sizeof(unsigned long) * depth);
/* allocate all needed blocks */
ext_debug("allocate %d blocks for indexes/leaf\n", depth - at);
}
lock_buffer(bh);
- if ((err = ext4_journal_get_create_access(handle, bh)))
+ err = ext4_journal_get_create_access(handle, bh);
+ if (err)
goto cleanup;
neh = ext_block_hdr(bh);
neh->eh_depth = 0;
ex = EXT_FIRST_EXTENT(neh);
- /* move remain of path[depth] to the new leaf */
+ /* move remainder of path[depth] to the new leaf */
BUG_ON(path[depth].p_hdr->eh_entries != path[depth].p_hdr->eh_max);
/* start copy from next extent */
/* TODO: we could do it by single memmove */
path[depth].p_ext++;
while (path[depth].p_ext <=
EXT_MAX_EXTENT(path[depth].p_hdr)) {
- ext_debug("move %d:%d:%d in new leaf %lu\n",
+ ext_debug("move %d:%llu:%d in new leaf %llu\n",
le32_to_cpu(path[depth].p_ext->ee_block),
- le32_to_cpu(path[depth].p_ext->ee_start),
+ ext_pblock(path[depth].p_ext),
le16_to_cpu(path[depth].p_ext->ee_len),
newblock);
/*memmove(ex++, path[depth].p_ext++,
set_buffer_uptodate(bh);
unlock_buffer(bh);
- if ((err = ext4_journal_dirty_metadata(handle, bh)))
+ err = ext4_journal_dirty_metadata(handle, bh);
+ if (err)
goto cleanup;
brelse(bh);
bh = NULL;
/* correct old leaf */
if (m) {
- if ((err = ext4_ext_get_access(handle, inode, path + depth)))
+ err = ext4_ext_get_access(handle, inode, path + depth);
+ if (err)
goto cleanup;
path[depth].p_hdr->eh_entries =
cpu_to_le16(le16_to_cpu(path[depth].p_hdr->eh_entries)-m);
- if ((err = ext4_ext_dirty(handle, inode, path + depth)))
+ err = ext4_ext_dirty(handle, inode, path + depth);
+ if (err)
goto cleanup;
}
while (k--) {
oldblock = newblock;
newblock = ablocks[--a];
- bh = sb_getblk(inode->i_sb, newblock);
+ bh = sb_getblk(inode->i_sb, (ext4_fsblk_t)newblock);
if (!bh) {
err = -EIO;
goto cleanup;
}
lock_buffer(bh);
- if ((err = ext4_journal_get_create_access(handle, bh)))
+ err = ext4_journal_get_create_access(handle, bh);
+ if (err)
goto cleanup;
neh = ext_block_hdr(bh);
neh->eh_depth = cpu_to_le16(depth - i);
fidx = EXT_FIRST_INDEX(neh);
fidx->ei_block = border;
- fidx->ei_leaf = cpu_to_le32(oldblock);
+ ext4_idx_store_pblock(fidx, oldblock);
- ext_debug("int.index at %d (block %lu): %lu -> %lu\n", i,
+ ext_debug("int.index at %d (block %llu): %lu -> %llu\n", i,
newblock, (unsigned long) le32_to_cpu(border),
oldblock);
/* copy indexes */
BUG_ON(EXT_MAX_INDEX(path[i].p_hdr) !=
EXT_LAST_INDEX(path[i].p_hdr));
while (path[i].p_idx <= EXT_MAX_INDEX(path[i].p_hdr)) {
- ext_debug("%d: move %d:%d in new index %lu\n", i,
+ ext_debug("%d: move %d:%d in new index %llu\n", i,
le32_to_cpu(path[i].p_idx->ei_block),
- le32_to_cpu(path[i].p_idx->ei_leaf),
+ idx_pblock(path[i].p_idx),
newblock);
/*memmove(++fidx, path[i].p_idx++,
sizeof(struct ext4_extent_idx));
set_buffer_uptodate(bh);
unlock_buffer(bh);
- if ((err = ext4_journal_dirty_metadata(handle, bh)))
+ err = ext4_journal_dirty_metadata(handle, bh);
+ if (err)
goto cleanup;
brelse(bh);
bh = NULL;
}
/* insert new index */
- if (err)
- goto cleanup;
-
err = ext4_ext_insert_index(handle, inode, path + at,
le32_to_cpu(border), newblock);
}
/*
- * routine implements tree growing procedure:
- * - allocates new block
- * - moves top-level data (index block or leaf) into the new block
- * - initialize new top-level, creating index that points to the
- * just created block
+ * ext4_ext_grow_indepth:
+ * implements tree growing procedure:
+ * - allocates new block
+ * - moves top-level data (index block or leaf) into the new block
+ * - initializes new top-level, creating index that points to the
+ * just created block
*/
static int ext4_ext_grow_indepth(handle_t *handle, struct inode *inode,
struct ext4_ext_path *path,
struct ext4_extent_header *neh;
struct ext4_extent_idx *fidx;
struct buffer_head *bh;
- unsigned long newblock;
+ ext4_fsblk_t newblock;
int err = 0;
newblock = ext4_ext_new_block(handle, inode, path, newext, &err);
}
lock_buffer(bh);
- if ((err = ext4_journal_get_create_access(handle, bh))) {
+ err = ext4_journal_get_create_access(handle, bh);
+ if (err) {
unlock_buffer(bh);
goto out;
}
set_buffer_uptodate(bh);
unlock_buffer(bh);
- if ((err = ext4_journal_dirty_metadata(handle, bh)))
+ err = ext4_journal_dirty_metadata(handle, bh);
+ if (err)
goto out;
/* create index in new top-level index: num,max,pointer */
- if ((err = ext4_ext_get_access(handle, inode, curp)))
+ err = ext4_ext_get_access(handle, inode, curp);
+ if (err)
goto out;
curp->p_hdr->eh_magic = EXT4_EXT_MAGIC;
curp->p_idx = EXT_FIRST_INDEX(curp->p_hdr);
/* FIXME: it works, but actually path[0] can be index */
curp->p_idx->ei_block = EXT_FIRST_EXTENT(path[0].p_hdr)->ee_block;
- curp->p_idx->ei_leaf = cpu_to_le32(newblock);
+ ext4_idx_store_pblock(curp->p_idx, newblock);
neh = ext_inode_hdr(inode);
fidx = EXT_FIRST_INDEX(neh);
- ext_debug("new root: num %d(%d), lblock %d, ptr %d\n",
+ ext_debug("new root: num %d(%d), lblock %d, ptr %llu\n",
le16_to_cpu(neh->eh_entries), le16_to_cpu(neh->eh_max),
- le32_to_cpu(fidx->ei_block), le32_to_cpu(fidx->ei_leaf));
+ le32_to_cpu(fidx->ei_block), idx_pblock(fidx));
neh->eh_depth = cpu_to_le16(path->p_depth + 1);
err = ext4_ext_dirty(handle, inode, curp);
}
/*
- * routine finds empty index and adds new leaf. if no free index found
- * then it requests in-depth growing
+ * ext4_ext_create_new_leaf:
+ * finds empty index and adds new leaf.
+ * if no free index is found, then it requests in-depth growing.
*/
static int ext4_ext_create_new_leaf(handle_t *handle, struct inode *inode,
struct ext4_ext_path *path,
curp--;
}
- /* we use already allocated block for index block
- * so, subsequent data blocks should be contigoues */
+ /* we use already allocated block for index block,
+ * so subsequent data blocks should be contiguous */
if (EXT_HAS_FREE_INDEX(curp)) {
/* if we found index with free entry, then use that
* entry: create all needed subtree and add new leaf */
}
/*
- * only first (depth 0 -> 1) produces free space
- * in all other cases we have to split growed tree
+ * only first (depth 0 -> 1) produces free space;
+ * in all other cases we have to split the grown tree
*/
depth = ext_depth(inode);
if (path[depth].p_hdr->eh_entries == path[depth].p_hdr->eh_max) {
- /* now we need split */
+ /* now we need to split */
goto repeat;
}
}
}
/*
- * returns allocated block in subsequent extent or EXT_MAX_BLOCK
- * NOTE: it consider block number from index entry as
- * allocated block. thus, index entries have to be consistent
- * with leafs
+ * ext4_ext_next_allocated_block:
+ * returns allocated block in subsequent extent or EXT_MAX_BLOCK.
+ * NOTE: it considers block number from index entry as
+ * allocated block. Thus, index entries have to be consistent
+ * with leaves.
*/
static unsigned long
ext4_ext_next_allocated_block(struct ext4_ext_path *path)
}
/*
+ * ext4_ext_next_leaf_block:
* returns first allocated block from next leaf or EXT_MAX_BLOCK
*/
static unsigned ext4_ext_next_leaf_block(struct inode *inode,
- struct ext4_ext_path *path)
+ struct ext4_ext_path *path)
{
int depth;
}
/*
- * if leaf gets modified and modified extent is first in the leaf
- * then we have to correct all indexes above
+ * ext4_ext_correct_indexes:
+ * if leaf gets modified and modified extent is first in the leaf,
+ * then we have to correct all indexes above.
* TODO: do we need to correct tree in all cases?
*/
int ext4_ext_correct_indexes(handle_t *handle, struct inode *inode,
}
/*
- * TODO: we need correction if border is smaller then current one
+ * TODO: we need correction if border is smaller than current one
*/
k = depth - 1;
border = path[depth].p_ext->ee_block;
- if ((err = ext4_ext_get_access(handle, inode, path + k)))
+ err = ext4_ext_get_access(handle, inode, path + k);
+ if (err)
return err;
path[k].p_idx->ei_block = border;
- if ((err = ext4_ext_dirty(handle, inode, path + k)))
+ err = ext4_ext_dirty(handle, inode, path + k);
+ if (err)
return err;
while (k--) {
/* change all left-side indexes */
if (path[k+1].p_idx != EXT_FIRST_INDEX(path[k+1].p_hdr))
break;
- if ((err = ext4_ext_get_access(handle, inode, path + k)))
+ err = ext4_ext_get_access(handle, inode, path + k);
+ if (err)
break;
path[k].p_idx->ei_block = border;
- if ((err = ext4_ext_dirty(handle, inode, path + k)))
+ err = ext4_ext_dirty(handle, inode, path + k);
+ if (err)
break;
}
return err;
}
-static int inline
+static int
ext4_can_extents_be_merged(struct inode *inode, struct ext4_extent *ex1,
struct ext4_extent *ex2)
{
- /* FIXME: 48bit support */
- if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len)
- != le32_to_cpu(ex2->ee_block))
+ if (le32_to_cpu(ex1->ee_block) + le16_to_cpu(ex1->ee_len) !=
+ le32_to_cpu(ex2->ee_block))
return 0;
-#ifdef AGRESSIVE_TEST
+ /*
+ * To allow future support for preallocated extents to be added
+ * as an RO_COMPAT feature, refuse to merge to extents if
+ * this can result in the top bit of ee_len being set.
+ */
+ if (le16_to_cpu(ex1->ee_len) + le16_to_cpu(ex2->ee_len) > EXT_MAX_LEN)
+ return 0;
+#ifdef AGGRESSIVE_TEST
if (le16_to_cpu(ex1->ee_len) >= 4)
return 0;
#endif
- if (le32_to_cpu(ex1->ee_start) + le16_to_cpu(ex1->ee_len)
- == le32_to_cpu(ex2->ee_start))
+ if (ext_pblock(ex1) + le16_to_cpu(ex1->ee_len) == ext_pblock(ex2))
return 1;
return 0;
}
/*
- * this routine tries to merge requsted extent into the existing
- * extent or inserts requested extent as new one into the tree,
- * creating new leaf in no-space case
+ * ext4_ext_insert_extent:
+ * tries to merge requsted extent into the existing extent or
+ * inserts requested extent as new one into the tree,
+ * creating new leaf in the no-space case.
*/
int ext4_ext_insert_extent(handle_t *handle, struct inode *inode,
struct ext4_ext_path *path,
/* try to insert block into found extent and return */
if (ex && ext4_can_extents_be_merged(inode, ex, newext)) {
- ext_debug("append %d block to %d:%d (from %d)\n",
+ ext_debug("append %d block to %d:%d (from %llu)\n",
le16_to_cpu(newext->ee_len),
le32_to_cpu(ex->ee_block),
- le16_to_cpu(ex->ee_len),
- le32_to_cpu(ex->ee_start));
- if ((err = ext4_ext_get_access(handle, inode, path + depth)))
+ le16_to_cpu(ex->ee_len), ext_pblock(ex));
+ err = ext4_ext_get_access(handle, inode, path + depth);
+ if (err)
return err;
ex->ee_len = cpu_to_le16(le16_to_cpu(ex->ee_len)
+ le16_to_cpu(newext->ee_len));
}
/*
- * there is no free space in found leaf
- * we're gonna add new leaf in the tree
+ * There is no free space in the found leaf.
+ * We're gonna add a new leaf in the tree.
*/
err = ext4_ext_create_new_leaf(handle, inode, path, newext);
if (err)
has_space:
nearex = path[depth].p_ext;
- if ((err = ext4_ext_get_access(handle, inode, path + depth)))
+ err = ext4_ext_get_access(handle, inode, path + depth);
+ if (err)
goto cleanup;
if (!nearex) {
/* there is no extent in this leaf, create first one */
- ext_debug("first extent in the leaf: %d:%d:%d\n",
+ ext_debug("first extent in the leaf: %d:%llu:%d\n",
le32_to_cpu(newext->ee_block),
- le32_to_cpu(newext->ee_start),
+ ext_pblock(newext),
le16_to_cpu(newext->ee_len));
path[depth].p_ext = EXT_FIRST_EXTENT(eh);
} else if (le32_to_cpu(newext->ee_block)
len = EXT_MAX_EXTENT(eh) - nearex;
len = (len - 1) * sizeof(struct ext4_extent);
len = len < 0 ? 0 : len;
- ext_debug("insert %d:%d:%d after: nearest 0x%p, "
+ ext_debug("insert %d:%llu:%d after: nearest 0x%p, "
"move %d from 0x%p to 0x%p\n",
le32_to_cpu(newext->ee_block),
- le32_to_cpu(newext->ee_start),
+ ext_pblock(newext),
le16_to_cpu(newext->ee_len),
nearex, len, nearex + 1, nearex + 2);
memmove(nearex + 2, nearex + 1, len);
BUG_ON(newext->ee_block == nearex->ee_block);
len = (EXT_MAX_EXTENT(eh) - nearex) * sizeof(struct ext4_extent);
len = len < 0 ? 0 : len;
- ext_debug("insert %d:%d:%d before: nearest 0x%p, "
+ ext_debug("insert %d:%llu:%d before: nearest 0x%p, "
"move %d from 0x%p to 0x%p\n",
le32_to_cpu(newext->ee_block),
- le32_to_cpu(newext->ee_start),
+ ext_pblock(newext),
le16_to_cpu(newext->ee_len),
nearex, len, nearex + 1, nearex + 2);
memmove(nearex + 1, nearex, len);
nearex = path[depth].p_ext;
nearex->ee_block = newext->ee_block;
nearex->ee_start = newext->ee_start;
+ nearex->ee_start_hi = newext->ee_start_hi;
nearex->ee_len = newext->ee_len;
- /* FIXME: support for large fs */
- nearex->ee_start_hi = 0;
merge:
/* try to merge extents to the right */
} else {
cbex.ec_block = le32_to_cpu(ex->ee_block);
cbex.ec_len = le16_to_cpu(ex->ee_len);
- cbex.ec_start = le32_to_cpu(ex->ee_start);
+ cbex.ec_start = ext_pblock(ex);
cbex.ec_type = EXT4_EXT_CACHE_EXTENT;
}
return err;
}
-static inline void
+static void
ext4_ext_put_in_cache(struct inode *inode, __u32 block,
__u32 len, __u32 start, int type)
{
}
/*
- * this routine calculate boundaries of the gap requested block fits into
+ * ext4_ext_put_gap_in_cache:
+ * calculate boundaries of the gap that the requested block fits into
* and cache this gap
*/
-static inline void
+static void
ext4_ext_put_gap_in_cache(struct inode *inode, struct ext4_ext_path *path,
unsigned long block)
{
ext4_ext_put_in_cache(inode, lblock, len, 0, EXT4_EXT_CACHE_GAP);
}
-static inline int
+static int
ext4_ext_in_cache(struct inode *inode, unsigned long block,
struct ext4_extent *ex)
{
cex->ec_type != EXT4_EXT_CACHE_EXTENT);
if (block >= cex->ec_block && block < cex->ec_block + cex->ec_len) {
ex->ee_block = cpu_to_le32(cex->ec_block);
- ex->ee_start = cpu_to_le32(cex->ec_start);
+ ext4_ext_store_pblock(ex, cex->ec_start);
ex->ee_len = cpu_to_le16(cex->ec_len);
- ext_debug("%lu cached by %lu:%lu:%lu\n",
+ ext_debug("%lu cached by %lu:%lu:%llu\n",
(unsigned long) block,
(unsigned long) cex->ec_block,
(unsigned long) cex->ec_len,
- (unsigned long) cex->ec_start);
+ cex->ec_start);
return cex->ec_type;
}
}
/*
- * routine removes index from the index block
- * it's used in truncate case only. thus all requests are for
- * last index in the block only
+ * ext4_ext_rm_idx:
+ * removes index from the index block.
+ * It's used in truncate case only, thus all requests are for
+ * last index in the block only.
*/
int ext4_ext_rm_idx(handle_t *handle, struct inode *inode,
struct ext4_ext_path *path)
{
struct buffer_head *bh;
int err;
- unsigned long leaf;
+ ext4_fsblk_t leaf;
/* free index block */
path--;
- leaf = le32_to_cpu(path->p_idx->ei_leaf);
+ leaf = idx_pblock(path->p_idx);
BUG_ON(path->p_hdr->eh_entries == 0);
- if ((err = ext4_ext_get_access(handle, inode, path)))
+ err = ext4_ext_get_access(handle, inode, path);
+ if (err)
return err;
path->p_hdr->eh_entries = cpu_to_le16(le16_to_cpu(path->p_hdr->eh_entries)-1);
- if ((err = ext4_ext_dirty(handle, inode, path)))
+ err = ext4_ext_dirty(handle, inode, path);
+ if (err)
return err;
- ext_debug("index is empty, remove it, free block %lu\n", leaf);
+ ext_debug("index is empty, remove it, free block %llu\n", leaf);
bh = sb_find_get_block(inode->i_sb, leaf);
ext4_forget(handle, 1, inode, bh, leaf);
ext4_free_blocks(handle, inode, leaf, 1);
}
/*
- * This routine returns max. credits extent tree can consume.
+ * ext4_ext_calc_credits_for_insert:
+ * This routine returns max. credits that the extent tree can consume.
* It should be OK for low-performance paths like ->writepage()
- * To allow many writing process to fit a single transaction,
- * caller should calculate credits under truncate_mutex and
- * pass actual path.
+ * To allow many writing processes to fit into a single transaction,
+ * the caller should calculate credits under truncate_mutex and
+ * pass the actual path.
*/
-int inline ext4_ext_calc_credits_for_insert(struct inode *inode,
+int ext4_ext_calc_credits_for_insert(struct inode *inode,
struct ext4_ext_path *path)
{
int depth, needed;
}
/*
- * given 32bit logical block (4294967296 blocks), max. tree
+ * given 32-bit logical block (4294967296 blocks), max. tree
* can be 4 levels in depth -- 4 * 340^4 == 53453440000.
- * let's also add one more level for imbalance.
+ * Let's also add one more level for imbalance.
*/
depth = 5;
needed = 2;
/*
- * tree can be full, so it'd need to grow in depth:
- * allocation + old root + new root
+ * tree can be full, so it would need to grow in depth:
+ * we need one credit to modify old root, credits for
+ * new root will be added in split accounting
*/
- needed += 2 + 1 + 1;
+ needed += 1;
/*
- * Index split can happen, we'd need:
+ * Index split can happen, we would need:
* allocate intermediate indexes (bitmap + group)
* + change two blocks at each level, but root (already included)
*/
- needed = (depth * 2) + (depth * 2);
+ needed += (depth * 2) + (depth * 2);
/* any allocation modifies superblock */
needed += 1;
if (from >= le32_to_cpu(ex->ee_block)
&& to == le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - 1) {
/* tail removal */
- unsigned long num, start;
+ unsigned long num;
+ ext4_fsblk_t start;
num = le32_to_cpu(ex->ee_block) + le16_to_cpu(ex->ee_len) - from;
- start = le32_to_cpu(ex->ee_start) + le16_to_cpu(ex->ee_len) - num;
- ext_debug("free last %lu blocks starting %lu\n", num, start);
+ start = ext_pblock(ex) + le16_to_cpu(ex->ee_len) - num;
+ ext_debug("free last %lu blocks starting %llu\n", num, start);
for (i = 0; i < num; i++) {
bh = sb_find_get_block(inode->i_sb, start + i);
ext4_forget(handle, 0, inode, bh, start + i);
BUG_ON(b != ex_ee_block + ex_ee_len - 1);
}
- /* at present, extent can't cross block group */
+ /* at present, extent can't cross block group: */
/* leaf + bitmap + group desc + sb + inode */
credits = 5;
if (ex == EXT_FIRST_EXTENT(eh)) {
goto out;
if (num == 0) {
- /* this extent is removed entirely mark slot unused */
- ex->ee_start = 0;
+ /* this extent is removed; mark slot entirely unused */
+ ext4_ext_store_pblock(ex, 0);
eh->eh_entries = cpu_to_le16(le16_to_cpu(eh->eh_entries)-1);
}
if (err)
goto out;
- ext_debug("new extent: %u:%u:%u\n", block, num,
- le32_to_cpu(ex->ee_start));
+ ext_debug("new extent: %u:%u:%llu\n", block, num,
+ ext_pblock(ex));
ex--;
ex_ee_block = le32_to_cpu(ex->ee_block);
ex_ee_len = le16_to_cpu(ex->ee_len);
}
/*
- * returns 1 if current index have to be freed (even partial)
+ * ext4_ext_more_to_rm:
+ * returns 1 if current index has to be freed (even partial)
*/
-static int inline
+static int
ext4_ext_more_to_rm(struct ext4_ext_path *path)
{
BUG_ON(path->p_idx == NULL);
return 0;
/*
- * if truncate on deeper level happened it it wasn't partial
+ * if truncate on deeper level happened, it wasn't partial,
* so we have to consider current index for truncation
*/
if (le16_to_cpu(path->p_hdr->eh_entries) == path->p_block)
ext4_ext_invalidate_cache(inode);
/*
- * we start scanning from right side freeing all the blocks
- * after i_size and walking into the deep
+ * We start scanning from right side, freeing all the blocks
+ * after i_size and walking into the tree depth-wise.
*/
- path = kmalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
+ path = kzalloc(sizeof(struct ext4_ext_path) * (depth + 1), GFP_KERNEL);
if (path == NULL) {
ext4_journal_stop(handle);
return -ENOMEM;
}
- memset(path, 0, sizeof(struct ext4_ext_path) * (depth + 1));
path[0].p_hdr = ext_inode_hdr(inode);
if (ext4_ext_check_header(__FUNCTION__, inode, path[0].p_hdr)) {
err = -EIO;
if (i == depth) {
/* this is leaf block */
err = ext4_ext_rm_leaf(handle, inode, path, start);
- /* root level have p_bh == NULL, brelse() eats this */
+ /* root level has p_bh == NULL, brelse() eats this */
brelse(path[i].p_bh);
path[i].p_bh = NULL;
i--;
BUG_ON(path[i].p_hdr->eh_magic != EXT4_EXT_MAGIC);
if (!path[i].p_idx) {
- /* this level hasn't touched yet */
+ /* this level hasn't been touched yet */
path[i].p_idx = EXT_LAST_INDEX(path[i].p_hdr);
path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries)+1;
ext_debug("init index ptr: hdr 0x%p, num %d\n",
path[i].p_hdr,
le16_to_cpu(path[i].p_hdr->eh_entries));
} else {
- /* we've already was here, see at next index */
+ /* we were already here, see at next index */
path[i].p_idx--;
}
path[i].p_idx);
if (ext4_ext_more_to_rm(path + i)) {
/* go to the next level */
- ext_debug("move to level %d (block %d)\n",
- i + 1, le32_to_cpu(path[i].p_idx->ei_leaf));
+ ext_debug("move to level %d (block %llu)\n",
+ i + 1, idx_pblock(path[i].p_idx));
memset(path + i + 1, 0, sizeof(*path));
path[i+1].p_bh =
- sb_bread(sb, le32_to_cpu(path[i].p_idx->ei_leaf));
+ sb_bread(sb, idx_pblock(path[i].p_idx));
if (!path[i+1].p_bh) {
/* should we reset i_size? */
err = -EIO;
break;
}
- /* put actual number of indexes to know is this
- * number got changed at the next iteration */
+ /* save actual number of indexes since this
+ * number is changed at the next iteration */
path[i].p_block = le16_to_cpu(path[i].p_hdr->eh_entries);
i++;
} else {
- /* we finish processing this index, go up */
+ /* we finished processing this index, go up */
if (path[i].p_hdr->eh_entries == 0 && i > 0) {
- /* index is empty, remove it
+ /* index is empty, remove it;
* handle must be already prepared by the
* truncatei_leaf() */
err = ext4_ext_rm_idx(handle, inode, path + i);
}
- /* root level have p_bh == NULL, brelse() eats this */
+ /* root level has p_bh == NULL, brelse() eats this */
brelse(path[i].p_bh);
path[i].p_bh = NULL;
i--;
/* TODO: flexible tree reduction should be here */
if (path->p_hdr->eh_entries == 0) {
/*
- * truncate to zero freed all the tree
- * so, we need to correct eh_depth
+ * truncate to zero freed all the tree,
+ * so we need to correct eh_depth
*/
err = ext4_ext_get_access(handle, inode, path);
if (err == 0) {
if (test_opt(sb, EXTENTS)) {
printk("EXT4-fs: file extents enabled");
-#ifdef AGRESSIVE_TEST
- printk(", agressive tests");
+#ifdef AGGRESSIVE_TEST
+ printk(", aggressive tests");
#endif
#ifdef CHECK_BINSEARCH
printk(", check binsearch");
#endif
}
-int ext4_ext_get_blocks(handle_t *handle, struct inode *inode, sector_t iblock,
+int ext4_ext_get_blocks(handle_t *handle, struct inode *inode,
+ ext4_fsblk_t iblock,
unsigned long max_blocks, struct buffer_head *bh_result,
int create, int extend_disksize)
{
struct ext4_ext_path *path = NULL;
struct ext4_extent newex, *ex;
- int goal, newblock, err = 0, depth;
+ ext4_fsblk_t goal, newblock;
+ int err = 0, depth;
unsigned long allocated = 0;
__clear_bit(BH_New, &bh_result->b_state);
mutex_lock(&EXT4_I(inode)->truncate_mutex);
/* check in cache */
- if ((goal = ext4_ext_in_cache(inode, iblock, &newex))) {
+ goal = ext4_ext_in_cache(inode, iblock, &newex);
+ if (goal) {
if (goal == EXT4_EXT_CACHE_GAP) {
if (!create) {
/* block isn't allocated yet and
- * user don't want to allocate it */
+ * user doesn't want to allocate it */
goto out2;
}
/* we should allocate requested block */
/* block is already allocated */
newblock = iblock
- le32_to_cpu(newex.ee_block)
- + le32_to_cpu(newex.ee_start);
- /* number of remain blocks in the extent */
+ + ext_pblock(&newex);
+ /* number of remaining blocks in the extent */
allocated = le16_to_cpu(newex.ee_len) -
(iblock - le32_to_cpu(newex.ee_block));
goto out;
depth = ext_depth(inode);
/*
- * consistent leaf must not be empty
- * this situations is possible, though, _during_ tree modification
+ * consistent leaf must not be empty;
+ * this situation is possible, though, _during_ tree modification;
* this is why assert can't be put in ext4_ext_find_extent()
*/
BUG_ON(path[depth].p_ext == NULL && depth != 0);
- if ((ex = path[depth].p_ext)) {
+ ex = path[depth].p_ext;
+ if (ex) {
unsigned long ee_block = le32_to_cpu(ex->ee_block);
- unsigned long ee_start = le32_to_cpu(ex->ee_start);
+ ext4_fsblk_t ee_start = ext_pblock(ex);
unsigned short ee_len = le16_to_cpu(ex->ee_len);
- /* if found exent covers block, simple return it */
+
+ /*
+ * Allow future support for preallocated extents to be added
+ * as an RO_COMPAT feature:
+ * Uninitialized extents are treated as holes, except that
+ * we avoid (fail) allocating new blocks during a write.
+ */
+ if (ee_len > EXT_MAX_LEN)
+ goto out2;
+ /* if found extent covers block, simply return it */
if (iblock >= ee_block && iblock < ee_block + ee_len) {
newblock = iblock - ee_block + ee_start;
- /* number of remain blocks in the extent */
+ /* number of remaining blocks in the extent */
allocated = ee_len - (iblock - ee_block);
- ext_debug("%d fit into %lu:%d -> %d\n", (int) iblock,
+ ext_debug("%d fit into %lu:%d -> %llu\n", (int) iblock,
ee_block, ee_len, newblock);
ext4_ext_put_in_cache(inode, ee_block, ee_len,
ee_start, EXT4_EXT_CACHE_EXTENT);
}
/*
- * requested block isn't allocated yet
+ * requested block isn't allocated yet;
* we couldn't try to create block if create flag is zero
*/
if (!create) {
- /* put just found gap into cache to speedup subsequest reqs */
+ /* put just found gap into cache to speed up
+ * subsequent requests */
ext4_ext_put_gap_in_cache(inode, path, iblock);
goto out2;
}
/*
- * Okay, we need to do block allocation. Lazily initialize the block
- * allocation info here if necessary
- */
+ * Okay, we need to do block allocation. Lazily initialize the block
+ * allocation info here if necessary.
+ */
if (S_ISREG(inode->i_mode) && (!EXT4_I(inode)->i_block_alloc_info))
ext4_init_block_alloc_info(inode);
newblock = ext4_new_blocks(handle, inode, goal, &allocated, &err);
if (!newblock)
goto out2;
- ext_debug("allocate new block: goal %d, found %d/%lu\n",
+ ext_debug("allocate new block: goal %llu, found %llu/%lu\n",
goal, newblock, allocated);
/* try to insert new extent into found leaf and return */
newex.ee_block = cpu_to_le32(iblock);
- newex.ee_start = cpu_to_le32(newblock);
+ ext4_ext_store_pblock(&newex, newblock);
newex.ee_len = cpu_to_le16(allocated);
err = ext4_ext_insert_extent(handle, inode, path, &newex);
if (err)
EXT4_I(inode)->i_disksize = inode->i_size;
/* previous routine could use block we allocated */
- newblock = le32_to_cpu(newex.ee_start);
+ newblock = ext_pblock(&newex);
__set_bit(BH_New, &bh_result->b_state);
ext4_ext_put_in_cache(inode, iblock, allocated, newblock,
ext4_ext_invalidate_cache(inode);
/*
- * TODO: optimization is possible here
- * probably we need not scaning at all,
- * because page truncation is enough
+ * TODO: optimization is possible here.
+ * Probably we need not scan at all,
+ * because page truncation is enough.
*/
if (ext4_orphan_add(handle, inode))
goto out_stop;
err = ext4_ext_remove_space(inode, last_block);
/* In a multi-transaction truncate, we only make the final
- * transaction synchronous */
+ * transaction synchronous. */
if (IS_SYNC(inode))
handle->h_sync = 1;
out_stop:
/*
- * If this was a simple ftruncate(), and the file will remain alive
+ * If this was a simple ftruncate() and the file will remain alive,
* then we need to clear up the orphan record which we created above.
* However, if this was a real unlink then we were called by
* ext4_delete_inode(), and we allow that function to clean up the
}
/*
- * this routine calculate max number of blocks we could modify
+ * ext4_ext_writepage_trans_blocks:
+ * calculate max number of blocks we could modify
* in order to allocate new block for an inode
*/
int ext4_ext_writepage_trans_blocks(struct inode *inode, int num)
needed = ext4_ext_calc_credits_for_insert(inode, NULL);
- /* caller want to allocate num blocks, but note it includes sb */
+ /* caller wants to allocate num blocks, but note it includes sb */
needed = needed * num - (num - 1);
#ifdef CONFIG_QUOTA