Skip to content

Commit

Permalink
nilfs2: replace BUG_ON and BUG calls triggerable from ioctl
Browse files Browse the repository at this point in the history
Pekka Enberg advised me:
> It would be nice if BUG(), BUG_ON(), and panic() calls would be
> converted to proper error handling using WARN_ON() calls. The BUG()
> call in nilfs_cpfile_delete_checkpoints(), for example, looks to be
> triggerable from user-space via the ioctl() system call.

This will follow the comment and keep them to a minimum.

Acked-by: Pekka Enberg <[email protected]>
Signed-off-by: Ryusuke Konishi <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
konis authored and torvalds committed Apr 7, 2009
1 parent 2c2e52f commit 1f5abe7
Show file tree
Hide file tree
Showing 13 changed files with 144 additions and 160 deletions.
27 changes: 10 additions & 17 deletions fs/nilfs2/btree.c
Original file line number Diff line number Diff line change
Expand Up @@ -425,7 +425,6 @@ static int nilfs_btree_node_lookup(const struct nilfs_btree *btree,
index++;

out:
BUG_ON(indexp == NULL);
*indexp = index;

return s == 0;
Expand Down Expand Up @@ -477,8 +476,6 @@ static int nilfs_btree_do_lookup(const struct nilfs_btree *btree,
__u64 ptr;
int level, index, found, ret;

BUG_ON(minlevel <= NILFS_BTREE_LEVEL_DATA);

node = nilfs_btree_get_root(btree);
level = nilfs_btree_node_get_level(btree, node);
if ((level < minlevel) ||
Expand All @@ -505,7 +502,7 @@ static int nilfs_btree_do_lookup(const struct nilfs_btree *btree,
if (index < nilfs_btree_node_nchildren_max(btree, node))
ptr = nilfs_btree_node_get_ptr(btree, node, index);
else {
BUG_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
WARN_ON(found || level != NILFS_BTREE_LEVEL_NODE_MIN);
/* insert */
ptr = NILFS_BMAP_INVALID_PTR;
}
Expand Down Expand Up @@ -1366,7 +1363,7 @@ static int nilfs_btree_prepare_delete(struct nilfs_btree *btree,
} else {
/* no siblings */
/* the only child of the root node */
BUG_ON(level != nilfs_btree_height(btree) - 2);
WARN_ON(level != nilfs_btree_height(btree) - 2);
if (nilfs_btree_node_get_nchildren(btree, node) - 1 <=
NILFS_BTREE_ROOT_NCHILDREN_MAX) {
path[level].bp_op = nilfs_btree_shrink;
Expand Down Expand Up @@ -1543,7 +1540,7 @@ static int nilfs_btree_gather_data(struct nilfs_bmap *bmap,
break;
case 3:
nchildren = nilfs_btree_node_get_nchildren(btree, root);
BUG_ON(nchildren > 1);
WARN_ON(nchildren > 1);
ptr = nilfs_btree_node_get_ptr(btree, root, nchildren - 1);
ret = nilfs_bmap_get_block(bmap, ptr, &bh);
if (ret < 0)
Expand All @@ -1552,7 +1549,7 @@ static int nilfs_btree_gather_data(struct nilfs_bmap *bmap,
break;
default:
node = NULL;
BUG();
return -EINVAL;
}

nchildren = nilfs_btree_node_get_nchildren(btree, node);
Expand Down Expand Up @@ -1833,14 +1830,13 @@ static int nilfs_btree_prepare_propagate_v(struct nilfs_btree *btree,
while ((++level < nilfs_btree_height(btree) - 1) &&
!buffer_dirty(path[level].bp_bh)) {

BUG_ON(buffer_nilfs_volatile(path[level].bp_bh));
WARN_ON(buffer_nilfs_volatile(path[level].bp_bh));
ret = nilfs_btree_prepare_update_v(btree, path, level);
if (ret < 0)
goto out;
}

/* success */
BUG_ON(maxlevelp == NULL);
*maxlevelp = level - 1;
return 0;

Expand Down Expand Up @@ -1909,7 +1905,7 @@ static int nilfs_btree_propagate(const struct nilfs_bmap *bmap,
__u64 key;
int level, ret;

BUG_ON(!buffer_dirty(bh));
WARN_ON(!buffer_dirty(bh));

btree = (struct nilfs_btree *)bmap;
path = nilfs_btree_alloc_path(btree);
Expand All @@ -1928,12 +1924,9 @@ static int nilfs_btree_propagate(const struct nilfs_bmap *bmap,

ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
if (ret < 0) {
/* BUG_ON(ret == -ENOENT); */
if (ret == -ENOENT) {
if (unlikely(ret == -ENOENT))
printk(KERN_CRIT "%s: key = %llu, level == %d\n",
__func__, (unsigned long long)key, level);
BUG();
}
goto out;
}

Expand Down Expand Up @@ -2117,7 +2110,7 @@ static int nilfs_btree_assign(struct nilfs_bmap *bmap,

ret = nilfs_btree_do_lookup(btree, path, key, NULL, level + 1);
if (ret < 0) {
BUG_ON(ret == -ENOENT);
WARN_ON(ret == -ENOENT);
goto out;
}

Expand Down Expand Up @@ -2175,12 +2168,12 @@ static int nilfs_btree_mark(struct nilfs_bmap *bmap, __u64 key, int level)

ret = nilfs_btree_do_lookup(btree, path, key, &ptr, level + 1);
if (ret < 0) {
BUG_ON(ret == -ENOENT);
WARN_ON(ret == -ENOENT);
goto out;
}
ret = nilfs_bmap_get_block(&btree->bt_bmap, ptr, &bh);
if (ret < 0) {
BUG_ON(ret == -ENOENT);
WARN_ON(ret == -ENOENT);
goto out;
}

Expand Down
38 changes: 21 additions & 17 deletions fs/nilfs2/cpfile.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,10 +40,7 @@ nilfs_cpfile_checkpoints_per_block(const struct inode *cpfile)
static unsigned long
nilfs_cpfile_get_blkoff(const struct inode *cpfile, __u64 cno)
{
__u64 tcno;

BUG_ON(cno == 0); /* checkpoint number 0 is invalid */
tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
__u64 tcno = cno + NILFS_MDT(cpfile)->mi_first_entry_offset - 1;
do_div(tcno, nilfs_cpfile_checkpoints_per_block(cpfile));
return (unsigned long)tcno;
}
Expand Down Expand Up @@ -96,7 +93,7 @@ nilfs_cpfile_block_sub_valid_checkpoints(const struct inode *cpfile,
struct nilfs_checkpoint *cp = kaddr + bh_offset(bh);
unsigned int count;

BUG_ON(le32_to_cpu(cp->cp_checkpoints_count) < n);
WARN_ON(le32_to_cpu(cp->cp_checkpoints_count) < n);
count = le32_to_cpu(cp->cp_checkpoints_count) - n;
cp->cp_checkpoints_count = cpu_to_le32(count);
return count;
Expand Down Expand Up @@ -178,6 +175,8 @@ static inline int nilfs_cpfile_delete_checkpoint_block(struct inode *cpfile,
* %-ENOMEM - Insufficient amount of memory available.
*
* %-ENOENT - No such checkpoint.
*
* %-EINVAL - invalid checkpoint.
*/
int nilfs_cpfile_get_checkpoint(struct inode *cpfile,
__u64 cno,
Expand All @@ -191,8 +190,9 @@ int nilfs_cpfile_get_checkpoint(struct inode *cpfile,
void *kaddr;
int ret;

BUG_ON(cno < 1 || cno > nilfs_mdt_cno(cpfile) ||
(cno < nilfs_mdt_cno(cpfile) && create));
if (unlikely(cno < 1 || cno > nilfs_mdt_cno(cpfile) ||
(cno < nilfs_mdt_cno(cpfile) && create)))
return -EINVAL;

down_write(&NILFS_MDT(cpfile)->mi_sem);

Expand Down Expand Up @@ -288,12 +288,11 @@ int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,
unsigned long tnicps;
int ret, ncps, nicps, count, i;

if ((start == 0) || (start > end)) {
printk(KERN_CRIT "%s: start = %llu, end = %llu\n",
__func__,
(unsigned long long)start,
(unsigned long long)end);
BUG();
if (unlikely(start == 0 || start > end)) {
printk(KERN_ERR "%s: invalid range of checkpoint numbers: "
"[%llu, %llu)\n", __func__,
(unsigned long long)start, (unsigned long long)end);
return -EINVAL;
}

/* cannot delete the latest checkpoint */
Expand Down Expand Up @@ -323,7 +322,7 @@ int nilfs_cpfile_delete_checkpoints(struct inode *cpfile,
cpfile, cno, cp_bh, kaddr);
nicps = 0;
for (i = 0; i < ncps; i++, cp = (void *)cp + cpsz) {
BUG_ON(nilfs_checkpoint_snapshot(cp));
WARN_ON(nilfs_checkpoint_snapshot(cp));
if (!nilfs_checkpoint_invalid(cp)) {
nilfs_checkpoint_set_invalid(cp);
nicps++;
Expand Down Expand Up @@ -393,6 +392,8 @@ static ssize_t nilfs_cpfile_do_get_cpinfo(struct inode *cpfile, __u64 *cnop,
int n, ret;
int ncps, i;

if (cno == 0)
return -ENOENT; /* checkpoint number 0 is invalid */
down_read(&NILFS_MDT(cpfile)->mi_sem);

for (n = 0; cno < cur_cno && n < nci; cno += ncps) {
Expand Down Expand Up @@ -532,9 +533,6 @@ int nilfs_cpfile_delete_checkpoint(struct inode *cpfile, __u64 cno)
ssize_t nci;
int ret;

/* checkpoint number 0 is invalid */
if (cno == 0)
return -ENOENT;
nci = nilfs_cpfile_do_get_cpinfo(cpfile, &tcno, &ci, 1);
if (nci < 0)
return nci;
Expand Down Expand Up @@ -582,6 +580,8 @@ static int nilfs_cpfile_set_snapshot(struct inode *cpfile, __u64 cno)
void *kaddr;
int ret;

if (cno == 0)
return -ENOENT; /* checkpoint number 0 is invalid */
down_write(&NILFS_MDT(cpfile)->mi_sem);

ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
Expand Down Expand Up @@ -698,6 +698,8 @@ static int nilfs_cpfile_clear_snapshot(struct inode *cpfile, __u64 cno)
void *kaddr;
int ret;

if (cno == 0)
return -ENOENT; /* checkpoint number 0 is invalid */
down_write(&NILFS_MDT(cpfile)->mi_sem);

ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &cp_bh);
Expand Down Expand Up @@ -813,6 +815,8 @@ int nilfs_cpfile_is_snapshot(struct inode *cpfile, __u64 cno)
void *kaddr;
int ret;

if (cno == 0)
return -ENOENT; /* checkpoint number 0 is invalid */
down_read(&NILFS_MDT(cpfile)->mi_sem);

ret = nilfs_cpfile_get_checkpoint_block(cpfile, cno, 0, &bh);
Expand Down
15 changes: 8 additions & 7 deletions fs/nilfs2/dat.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ int nilfs_dat_prepare_start(struct inode *dat, struct nilfs_palloc_req *req)
int ret;

ret = nilfs_dat_prepare_entry(dat, req, 0);
BUG_ON(ret == -ENOENT);
WARN_ON(ret == -ENOENT);
return ret;
}

Expand All @@ -157,7 +157,6 @@ void nilfs_dat_commit_start(struct inode *dat, struct nilfs_palloc_req *req,
(unsigned long long)le64_to_cpu(entry->de_start),
(unsigned long long)le64_to_cpu(entry->de_end),
(unsigned long long)le64_to_cpu(entry->de_blocknr));
BUG();
}
entry->de_blocknr = cpu_to_le64(blocknr);
kunmap_atomic(kaddr, KM_USER0);
Expand All @@ -180,7 +179,7 @@ int nilfs_dat_prepare_end(struct inode *dat, struct nilfs_palloc_req *req)

ret = nilfs_dat_prepare_entry(dat, req, 0);
if (ret < 0) {
BUG_ON(ret == -ENOENT);
WARN_ON(ret == -ENOENT);
return ret;
}

Expand Down Expand Up @@ -216,7 +215,7 @@ void nilfs_dat_commit_end(struct inode *dat, struct nilfs_palloc_req *req,
end = start = le64_to_cpu(entry->de_start);
if (!dead) {
end = nilfs_mdt_cno(dat);
BUG_ON(start > end);
WARN_ON(start > end);
}
entry->de_end = cpu_to_le64(end);
blocknr = le64_to_cpu(entry->de_blocknr);
Expand Down Expand Up @@ -324,14 +323,16 @@ int nilfs_dat_move(struct inode *dat, __u64 vblocknr, sector_t blocknr)
return ret;
kaddr = kmap_atomic(entry_bh->b_page, KM_USER0);
entry = nilfs_palloc_block_get_entry(dat, vblocknr, entry_bh, kaddr);
if (entry->de_blocknr == cpu_to_le64(0)) {
if (unlikely(entry->de_blocknr == cpu_to_le64(0))) {
printk(KERN_CRIT "%s: vbn = %llu, [%llu, %llu)\n", __func__,
(unsigned long long)vblocknr,
(unsigned long long)le64_to_cpu(entry->de_start),
(unsigned long long)le64_to_cpu(entry->de_end));
BUG();
kunmap_atomic(kaddr, KM_USER0);
brelse(entry_bh);
return -EINVAL;
}
BUG_ON(blocknr == 0);
WARN_ON(blocknr == 0);
entry->de_blocknr = cpu_to_le64(blocknr);
kunmap_atomic(kaddr, KM_USER0);

Expand Down
13 changes: 10 additions & 3 deletions fs/nilfs2/direct.c
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,6 @@ static int nilfs_direct_last_key(const struct nilfs_bmap *bmap, __u64 *keyp)
if (lastkey == NILFS_DIRECT_KEY_MAX + 1)
return -ENOENT;

BUG_ON(keyp == NULL);
*keyp = lastkey;

return 0;
Expand Down Expand Up @@ -366,9 +365,17 @@ static int nilfs_direct_assign(struct nilfs_bmap *bmap,

direct = (struct nilfs_direct *)bmap;
key = nilfs_bmap_data_get_key(bmap, *bh);
BUG_ON(key > NILFS_DIRECT_KEY_MAX);
if (unlikely(key > NILFS_DIRECT_KEY_MAX)) {
printk(KERN_CRIT "%s: invalid key: %llu\n", __func__,
(unsigned long long)key);
return -EINVAL;
}
ptr = nilfs_direct_get_ptr(direct, key);
BUG_ON(ptr == NILFS_BMAP_INVALID_PTR);
if (unlikely(ptr == NILFS_BMAP_INVALID_PTR)) {
printk(KERN_CRIT "%s: invalid pointer: %llu\n", __func__,
(unsigned long long)ptr);
return -EINVAL;
}

return direct->d_ops->dop_assign(direct, key, ptr, bh,
blocknr, binfo);
Expand Down
19 changes: 5 additions & 14 deletions fs/nilfs2/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -61,12 +61,6 @@ int nilfs_get_block(struct inode *inode, sector_t blkoff,
map_bh(bh_result, inode->i_sb, blknum);
goto out;
}
if (unlikely(ret == 1)) {
printk(KERN_ERR "nilfs_get_block: bmap_lookup returns "
"buffer_head pointer (blkoff=%llu, blknum=%lu)\n",
(unsigned long long)blkoff, blknum);
BUG();
}
/* data block was not found */
if (ret == -ENOENT && create) {
struct nilfs_transaction_info ti;
Expand All @@ -85,14 +79,14 @@ int nilfs_get_block(struct inode *inode, sector_t blkoff,
* However, the page having this block must
* be locked in this case.
*/
printk(KERN_ERR
printk(KERN_WARNING
"nilfs_get_block: a race condition "
"while inserting a data block. "
"(inode number=%lu, file block "
"offset=%llu)\n",
inode->i_ino,
(unsigned long long)blkoff);
BUG();
err = 0;
} else if (err == -EINVAL) {
nilfs_error(inode->i_sb, __func__,
"broken bmap (inode=%lu)\n",
Expand Down Expand Up @@ -621,7 +615,6 @@ void nilfs_truncate(struct inode *inode)
struct nilfs_transaction_info ti;
struct super_block *sb = inode->i_sb;
struct nilfs_inode_info *ii = NILFS_I(inode);
int ret;

if (!test_bit(NILFS_I_BMAP, &ii->i_state))
return;
Expand All @@ -630,8 +623,7 @@ void nilfs_truncate(struct inode *inode)

blocksize = sb->s_blocksize;
blkoff = (inode->i_size + blocksize - 1) >> sb->s_blocksize_bits;
ret = nilfs_transaction_begin(sb, &ti, 0);
BUG_ON(ret);
nilfs_transaction_begin(sb, &ti, 0); /* never fails */

block_truncate_page(inode->i_mapping, inode->i_size, nilfs_get_block);

Expand All @@ -652,16 +644,15 @@ void nilfs_delete_inode(struct inode *inode)
struct nilfs_transaction_info ti;
struct super_block *sb = inode->i_sb;
struct nilfs_inode_info *ii = NILFS_I(inode);
int err;

if (unlikely(is_bad_inode(inode))) {
if (inode->i_data.nrpages)
truncate_inode_pages(&inode->i_data, 0);
clear_inode(inode);
return;
}
err = nilfs_transaction_begin(sb, &ti, 0);
BUG_ON(err);
nilfs_transaction_begin(sb, &ti, 0); /* never fails */

if (inode->i_data.nrpages)
truncate_inode_pages(&inode->i_data, 0);

Expand Down
Loading

0 comments on commit 1f5abe7

Please sign in to comment.