Skip to content

Commit

Permalink
udf: Replace bkl with the UDF_I(inode)->i_data_sem for protect udf_in…
Browse files Browse the repository at this point in the history
…ode_info struct

Replace bkl with the UDF_I(inode)->i_data_sem rw semaphore in
udf_release_file(), udf_symlink(), udf_symlink_filler(), udf_get_block(),
udf_block_map(), and udf_setattr(). The rule now is that any operation
on regular file's or symlink's extents (or generally allocation information
including goal block) needs to hold i_data_sem.

This work was supported by a hardware donation from the CE Linux Forum.

Signed-off-by: Alessio Igor Bogani <[email protected]>
Signed-off-by: Jan Kara <[email protected]>
  • Loading branch information
Alessio Igor Bogani authored and jankara committed Jan 6, 2011
1 parent d1668fe commit 4d0fb62
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 19 deletions.
4 changes: 2 additions & 2 deletions fs/udf/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,10 @@ static int udf_release_file(struct inode *inode, struct file *filp)
{
if (filp->f_mode & FMODE_WRITE) {
mutex_lock(&inode->i_mutex);
lock_kernel();
down_write(&UDF_I(inode)->i_data_sem);
udf_discard_prealloc(inode);
udf_truncate_tail_extent(inode);
unlock_kernel();
up_write(&UDF_I(inode)->i_data_sem);
mutex_unlock(&inode->i_mutex);
}
return 0;
Expand Down
19 changes: 10 additions & 9 deletions fs/udf/inode.c
Original file line number Diff line number Diff line change
Expand Up @@ -301,10 +301,9 @@ static int udf_get_block(struct inode *inode, sector_t block,
err = -EIO;
new = 0;
bh = NULL;

lock_kernel();

iinfo = UDF_I(inode);

down_write(&iinfo->i_data_sem);
if (block == iinfo->i_next_alloc_block + 1) {
iinfo->i_next_alloc_block++;
iinfo->i_next_alloc_goal++;
Expand All @@ -323,7 +322,7 @@ static int udf_get_block(struct inode *inode, sector_t block,
map_bh(bh_result, inode->i_sb, phys);

abort:
unlock_kernel();
up_write(&iinfo->i_data_sem);
return err;
}

Expand Down Expand Up @@ -1021,16 +1020,16 @@ void udf_truncate(struct inode *inode)
if (IS_APPEND(inode) || IS_IMMUTABLE(inode))
return;

lock_kernel();
iinfo = UDF_I(inode);
if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
down_write(&iinfo->i_data_sem);
if (inode->i_sb->s_blocksize <
(udf_file_entry_alloc_offset(inode) +
inode->i_size)) {
udf_expand_file_adinicb(inode, inode->i_size, &err);
if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
inode->i_size = iinfo->i_lenAlloc;
unlock_kernel();
up_write(&iinfo->i_data_sem);
return;
} else
udf_truncate_extents(inode);
Expand All @@ -1041,18 +1040,20 @@ void udf_truncate(struct inode *inode)
offset - udf_file_entry_alloc_offset(inode));
iinfo->i_lenAlloc = inode->i_size;
}
up_write(&iinfo->i_data_sem);
} else {
block_truncate_page(inode->i_mapping, inode->i_size,
udf_get_block);
down_write(&iinfo->i_data_sem);
udf_truncate_extents(inode);
up_write(&iinfo->i_data_sem);
}

inode->i_mtime = inode->i_ctime = current_fs_time(inode->i_sb);
if (IS_SYNC(inode))
udf_sync_inode(inode);
else
mark_inode_dirty(inode);
unlock_kernel();
}

static void __udf_read_inode(struct inode *inode)
Expand Down Expand Up @@ -2043,15 +2044,15 @@ long udf_block_map(struct inode *inode, sector_t block)
struct extent_position epos = {};
int ret;

lock_kernel();
down_read(&UDF_I(inode)->i_data_sem);

if (inode_bmap(inode, block, &epos, &eloc, &elen, &offset) ==
(EXT_RECORDED_ALLOCATED >> 30))
ret = udf_get_lb_pblock(inode->i_sb, &eloc, offset);
else
ret = 0;

unlock_kernel();
up_read(&UDF_I(inode)->i_data_sem);
brelse(epos.bh);

if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_VARCONV))
Expand Down
7 changes: 4 additions & 3 deletions fs/udf/namei.c
Original file line number Diff line number Diff line change
Expand Up @@ -893,18 +893,18 @@ static int udf_symlink(struct inode *dir, struct dentry *dentry,
struct udf_inode_info *iinfo;
struct super_block *sb = dir->i_sb;

lock_kernel();
inode = udf_new_inode(dir, S_IFLNK | S_IRWXUGO, &err);
if (!inode)
goto out;

iinfo = UDF_I(inode);
down_write(&iinfo->i_data_sem);
name = kmalloc(UDF_NAME_LEN, GFP_NOFS);
if (!name) {
err = -ENOMEM;
goto out_no_entry;
}

iinfo = UDF_I(inode);
inode->i_data.a_ops = &udf_symlink_aops;
inode->i_op = &udf_symlink_inode_operations;

Expand Down Expand Up @@ -1024,6 +1024,7 @@ static int udf_symlink(struct inode *dir, struct dentry *dentry,
udf_write_fi(dir, &cfi, fi, &fibh, NULL, NULL);
if (UDF_I(dir)->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB)
mark_inode_dirty(dir);
up_write(&iinfo->i_data_sem);
if (fibh.sbh != fibh.ebh)
brelse(fibh.ebh);
brelse(fibh.sbh);
Expand All @@ -1032,10 +1033,10 @@ static int udf_symlink(struct inode *dir, struct dentry *dentry,

out:
kfree(name);
unlock_kernel();
return err;

out_no_entry:
up_write(&iinfo->i_data_sem);
inode_dec_link_count(inode);
iput(inode);
goto out;
Expand Down
1 change: 1 addition & 0 deletions fs/udf/super.c
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,7 @@ static struct inode *udf_alloc_inode(struct super_block *sb)
ei->i_next_alloc_block = 0;
ei->i_next_alloc_goal = 0;
ei->i_strat4096 = 0;
init_rwsem(&ei->i_data_sem);

return &ei->vfs_inode;
}
Expand Down
12 changes: 7 additions & 5 deletions fs/udf/symlink.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,6 @@
#include <linux/mm.h>
#include <linux/stat.h>
#include <linux/pagemap.h>
#include <linux/smp_lock.h>
#include <linux/buffer_head.h>
#include "udf_i.h"

Expand Down Expand Up @@ -78,13 +77,16 @@ static int udf_symlink_filler(struct file *file, struct page *page)
int err = -EIO;
unsigned char *p = kmap(page);
struct udf_inode_info *iinfo;
uint32_t pos;

lock_kernel();
iinfo = UDF_I(inode);
pos = udf_block_map(inode, 0);

down_read(&iinfo->i_data_sem);
if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
symlink = iinfo->i_ext.i_data + iinfo->i_lenEAttr;
} else {
bh = sb_bread(inode->i_sb, udf_block_map(inode, 0));
bh = sb_bread(inode->i_sb, pos);

if (!bh)
goto out;
Expand All @@ -95,14 +97,14 @@ static int udf_symlink_filler(struct file *file, struct page *page)
udf_pc_to_char(inode->i_sb, symlink, inode->i_size, p);
brelse(bh);

unlock_kernel();
up_read(&iinfo->i_data_sem);
SetPageUptodate(page);
kunmap(page);
unlock_page(page);
return 0;

out:
unlock_kernel();
up_read(&iinfo->i_data_sem);
SetPageError(page);
kunmap(page);
unlock_page(page);
Expand Down
13 changes: 13 additions & 0 deletions fs/udf/udf_i.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,18 @@
#ifndef _UDF_I_H
#define _UDF_I_H

/*
* The i_data_sem and i_mutex serve for protection of allocation information
* of a regular files and symlinks. This includes all extents belonging to
* the file/symlink, a fact whether data are in-inode or in external data
* blocks, preallocation, goal block information... When extents are read,
* i_mutex or i_data_sem must be held (for reading is enough in case of
* i_data_sem). When extents are changed, i_data_sem must be held for writing
* and also i_mutex must be held.
*
* For directories i_mutex is used for all the necessary protection.
*/

struct udf_inode_info {
struct timespec i_crtime;
/* Physical address of inode */
Expand All @@ -21,6 +33,7 @@ struct udf_inode_info {
struct long_ad *i_lad;
__u8 *i_data;
} i_ext;
struct rw_semaphore i_data_sem;
struct inode vfs_inode;
};

Expand Down

0 comments on commit 4d0fb62

Please sign in to comment.