2011-06-20 20:20:36

by Christoph Hellwig

[permalink] [raw]
Subject: [PATCH 1/8] far: remove i_alloc_sem abuse

Add a new rw_semaphore to protect bmap against truncate. Previous
i_alloc_sem was abused for this, but it's going away in this series.

Signed-off-by: Christoph Hellwig <[email protected]>

Index: linux-2.6/fs/fat/inode.c
===================================================================
--- linux-2.6.orig/fs/fat/inode.c 2011-06-20 21:28:19.707963855 +0200
+++ linux-2.6/fs/fat/inode.c 2011-06-20 21:29:25.031293882 +0200
@@ -224,9 +224,9 @@ static sector_t _fat_bmap(struct address
sector_t blocknr;

/* fat_get_cluster() assumes the requested blocknr isn't truncated. */
- down_read(&mapping->host->i_alloc_sem);
+ down_read(&MSDOS_I(mapping->host)->truncate_lock);
blocknr = generic_block_bmap(mapping, block, fat_get_block);
- up_read(&mapping->host->i_alloc_sem);
+ up_read(&MSDOS_I(mapping->host)->truncate_lock);

return blocknr;
}
@@ -510,6 +510,8 @@ static struct inode *fat_alloc_inode(str
ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
if (!ei)
return NULL;
+
+ init_rwsem(&ei->truncate_lock);
return &ei->vfs_inode;
}

Index: linux-2.6/fs/fat/fat.h
===================================================================
--- linux-2.6.orig/fs/fat/fat.h 2011-06-20 21:28:19.724630522 +0200
+++ linux-2.6/fs/fat/fat.h 2011-06-20 21:29:25.034627215 +0200
@@ -109,6 +109,7 @@ struct msdos_inode_info {
int i_attrs; /* unused attribute bits */
loff_t i_pos; /* on-disk position of directory entry or 0 */
struct hlist_node i_fat_hash; /* hash by i_location */
+ struct rw_semaphore truncate_lock; /* protect bmap against truncate */
struct inode vfs_inode;
};

Index: linux-2.6/fs/fat/file.c
===================================================================
--- linux-2.6.orig/fs/fat/file.c 2011-06-20 21:28:19.744630521 +0200
+++ linux-2.6/fs/fat/file.c 2011-06-20 21:29:54.501292390 +0200
@@ -429,8 +429,10 @@ int fat_setattr(struct dentry *dentry, s
}

if (attr->ia_valid & ATTR_SIZE) {
+ down_write(&MSDOS_I(inode)->truncate_lock);
truncate_setsize(inode, attr->ia_size);
fat_truncate_blocks(inode, attr->ia_size);
+ up_write(&MSDOS_I(inode)->truncate_lock);
}

setattr_copy(inode, attr);



2011-06-21 15:57:43

by OGAWA Hirofumi

[permalink] [raw]
Subject: Re: [PATCH 1/8] far: remove i_alloc_sem abuse

Christoph Hellwig <[email protected]> writes:

> Add a new rw_semaphore to protect bmap against truncate. Previous
> i_alloc_sem was abused for this, but it's going away in this series.

In FAT case, ->i_mutex was better. But, last time I saw, shmfs was using
->i_mutex to call ->bmap. So, this was chosen instead.

I'm not checking current version yet though, if shmfs had change, we can
use ->i_mutex.

BTW, this patch looks good to me.

Thanks.

> Signed-off-by: Christoph Hellwig <[email protected]>
>
> Index: linux-2.6/fs/fat/inode.c
> ===================================================================
> --- linux-2.6.orig/fs/fat/inode.c 2011-06-20 21:28:19.707963855 +0200
> +++ linux-2.6/fs/fat/inode.c 2011-06-20 21:29:25.031293882 +0200
> @@ -224,9 +224,9 @@ static sector_t _fat_bmap(struct address
> sector_t blocknr;
>
> /* fat_get_cluster() assumes the requested blocknr isn't truncated. */
> - down_read(&mapping->host->i_alloc_sem);
> + down_read(&MSDOS_I(mapping->host)->truncate_lock);
> blocknr = generic_block_bmap(mapping, block, fat_get_block);
> - up_read(&mapping->host->i_alloc_sem);
> + up_read(&MSDOS_I(mapping->host)->truncate_lock);
>
> return blocknr;
> }
> @@ -510,6 +510,8 @@ static struct inode *fat_alloc_inode(str
> ei = kmem_cache_alloc(fat_inode_cachep, GFP_NOFS);
> if (!ei)
> return NULL;
> +
> + init_rwsem(&ei->truncate_lock);
> return &ei->vfs_inode;
> }
>
> Index: linux-2.6/fs/fat/fat.h
> ===================================================================
> --- linux-2.6.orig/fs/fat/fat.h 2011-06-20 21:28:19.724630522 +0200
> +++ linux-2.6/fs/fat/fat.h 2011-06-20 21:29:25.034627215 +0200
> @@ -109,6 +109,7 @@ struct msdos_inode_info {
> int i_attrs; /* unused attribute bits */
> loff_t i_pos; /* on-disk position of directory entry or 0 */
> struct hlist_node i_fat_hash; /* hash by i_location */
> + struct rw_semaphore truncate_lock; /* protect bmap against truncate */
> struct inode vfs_inode;
> };
>
> Index: linux-2.6/fs/fat/file.c
> ===================================================================
> --- linux-2.6.orig/fs/fat/file.c 2011-06-20 21:28:19.744630521 +0200
> +++ linux-2.6/fs/fat/file.c 2011-06-20 21:29:54.501292390 +0200
> @@ -429,8 +429,10 @@ int fat_setattr(struct dentry *dentry, s
> }
>
> if (attr->ia_valid & ATTR_SIZE) {
> + down_write(&MSDOS_I(inode)->truncate_lock);
> truncate_setsize(inode, attr->ia_size);
> fat_truncate_blocks(inode, attr->ia_size);
> + up_write(&MSDOS_I(inode)->truncate_lock);
> }
>
> setattr_copy(inode, attr);
>

--
OGAWA Hirofumi <[email protected]>

2011-06-21 16:09:29

by OGAWA Hirofumi

[permalink] [raw]
Subject: Re: [PATCH 1/8] far: remove i_alloc_sem abuse

OGAWA Hirofumi <[email protected]> writes:

> Christoph Hellwig <[email protected]> writes:
>
>> Add a new rw_semaphore to protect bmap against truncate. Previous
>> i_alloc_sem was abused for this, but it's going away in this series.
>
> In FAT case, ->i_mutex was better. But, last time I saw, shmfs was using
> ->i_mutex to call ->bmap. So, this was chosen instead.
>
> I'm not checking current version yet though, if shmfs had change, we can
> use ->i_mutex.

It was swapfile. And unfortunately it doesn't have change.

> BTW, this patch looks good to me.
--
OGAWA Hirofumi <[email protected]>

2011-06-21 16:09:42

by Christoph Hellwig

[permalink] [raw]
Subject: Re: [PATCH 1/8] far: remove i_alloc_sem abuse

On Wed, Jun 22, 2011 at 12:57:43AM +0900, OGAWA Hirofumi wrote:
> Christoph Hellwig <[email protected]> writes:
>
> > Add a new rw_semaphore to protect bmap against truncate. Previous
> > i_alloc_sem was abused for this, but it's going away in this series.
>
> In FAT case, ->i_mutex was better. But, last time I saw, shmfs was using
> ->i_mutex to call ->bmap. So, this was chosen instead.
>
> I'm not checking current version yet though, if shmfs had change, we can
> use ->i_mutex.

I tried that first, but it gives an instant deadlock when using a
swapfile on fat.