fat: additions to support fat_fallocate
Subsystems (2)
PATCH v6 1/1 fat: additions to support fat_fallocate
From: Namjae Jeon <[email protected]>
v6: In v3 of the patch we released the allocated clutsers in fat_file_release on the basis of d_count. Moving this from fat_file_release to fat_evict_inode and on the basis of i_count , so that allocated clusters are released at the last refrence of the inode when inode evicts from memory .
In case of direct IO , writting in fallocated area fall back to buffered write.
Support for fibmap is also added for fallocated region by modifying fat_bmap to map cluster in case of read request for a block in fallocated region.
v5: change to avoid compilation warning:
fs/fat/inode.c: In function 'fat_zero_falloc_area':
Quoted 1 line
Quoted 1 line
fs/fat/inode.c:169:11: warning: comparison of distinct pointer
types lacks a cast [enabled by default]
v4: Rework based on review comments.
Add check in fat_setattr to release fallocated blocks on a truncate
v3: Release preallocated blocks at file release.
With FALLOC_FL_KEEP_SIZE, there is no way to distinguish if the mismatch between i_size and no. of clusters allocated is a consequence of fallocate or just plain corruption. When a non fallocate aware (old) linux fat driver tries to write to such a file, it throws an error.Also, fsck detects this as inconsistency and truncates the prealloc'd blocks.
To avoid this, as suggested by OGAWA, remove changes that make fallocate persistent across mounts and restrict lifetime of blocks from fallocate(2) to file release.
v2: On an area preallocated with FALLOC_FL_KEEP_SIZE, when a seek was done to an offset beyond i_size, the old (garbage) data was exposed as we did not zero out the area at allocation time. Added fat_zero_falloc_area() to fix this.
v1: Reworked an earlier patch of the same name
(https://lkml.org/lkml/2007/12/22/130) to fix some bugs:
i) Preallocated space was not persistent and was lost on remount. Fixed it.
ii) Did not zero out allocated clusters when FALLOC_FL_KEEP_SIZE was set, thereby speeding up preallocation time.
--- fs/fat/cache.c | 16 +++++++++-- fs/fat/file.c | 85 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/fat/inode.c | 74 +++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 171 insertions(+), 4 deletions(-)
fs/fat/cache.c +13 -3
@@ -312,6 +312,7 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,const unsigned char blocksize_bits = sb->s_blocksize_bits;sector_t last_block;int cluster, offset; +loff_t i_size =i_size_read (inode); *phys =0 ; *mapped_blocks =0 ;
@@ -323,11 +324,19 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,return 0 ; } - last_block = (i_size_read (inode) + (blocksize -1 )) >> blocksize_bits; + last_block = (i_size + (blocksize -1 )) >> blocksize_bits;if (sector >= last_block) { -if (!create) -return 0 ; +if (!create) { +/* +* to map cluster in case of read request +* for a block in fallocated region +*/ +if (MSDOS_I (inode)->mmu_private > +round_up (i_size, sb->s_blocksize)) + goto out_map_cluster; +return 0 ; + }/* * ->mmu_private can access on only allocation path. * (caller must hold ->i_mutex)
@@ -338,6 +347,7 @@ int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys,return 0 ; } +out_map_cluster: cluster = sector >> (sbi->cluster_bits - sb->s_blocksize_bits); offset = sector & (sbi->sec_per_clus -1 ); cluster =fat_bmap_cluster (inode, cluster);
fs/fat/file.c +85 -0
105 lines not on this page · raw message
fs/fat/inode.c +73 -1
101 lines not on this page · raw message
Signature · 1 line
1.7.9.5
OGAWA Hirofumi
maintainerNamjae Jeon <[email protected]> writes:
Entirely, we would have to consider in the case of write fail (e.g. -ENOSPC). If write failed, it will call truncate(). Then, it can be truncate the fallocate region too unexpectedly.
fs/fat/cache.c int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys, · line 336
Quoted 8 lines
fs/fat/cache.c +8
-0+if (!create) { +/* +* to map cluster in case of read request +* for a block in fallocated region +*/ +if (MSDOS_I (inode)->mmu_private > +round_up (i_size, sb->s_blocksize)) + goto out_map_cluster;
->mmu_private can't stable without i_mutex. I.e. racy.
Quoted 12 lines
fs/fat/file.c +12
-0+/* +* Preallocate space for a file. This implements fat's fallocate file +* operation, which gets called from sys_fallocate system call. User +* space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set +* we just allocate clusters without zeroing them out. Otherwise we +* allocate and zero out clusters via an expanding truncate. +*/ +static long fat_fallocate (struct file *file,int mode, +loff_t offset,loff_t len) +{ +int cluster, fclus, dclus; +int nr_cluster;/* Number of clusters to be allocated */
fs/fat/file.c out: · line 248
Quoted 10 lines
fs/fat/file.c +10
-0+loff_t nr_bytes;/* Number of bytes to be allocated*/ +loff_t free_bytes;/* Unused bytes in the last cluster of file*/ +struct inode *inode = file->f_mapping->host; +struct super_block *sb = inode->i_sb; +struct msdos_sb_info *sbi =MSDOS_SB (sb); +int err =0 ; + +/* No support for hole punch or other fallocate flags. */ +if (mode & ~FALLOC_FL_KEEP_SIZE ) +return -EOPNOTSUPP ;
Don't we need to check S_ISREG() here? If this inode is dir, fallocate() will break dir.
Review
Quoted 1 line
(quoted diff) +1
-0+ mutex_lock(&inode->i_mutex);
Don't we need to check inode_newsize_ok()? It seems nobody is checking rlimit.
fs/fat/file.c out: · line 256
Quoted 6 lines
fs/fat/file.c +6
-0+if ((offset + len) <=MSDOS_I (inode)->mmu_private) { +fat_msg (sb,KERN_ERR , +"fat_fallocate(): Blocks already allocated" ); + err = -EINVAL ; + goto error; + }
This is user error, so we should not print from kernel. Otherwise, attacker can make flood of kernel message.
Furthermore, is this right behavior? User should care about current fallocated size? Other FSes return -EINVAL too?
Quoted 7 lines
fs/fat/file.c +7
-0+if (mode &FALLOC_FL_KEEP_SIZE ) { +/* First compute the number of clusters to be allocated */ +if (inode->i_size >0 ) { + err =fat_get_cluster (inode,FAT_ENT_EOF , + &fclus, &dclus); +if (err <0 ) { + fat_msg(sb, KERN_ERR,
fs/fat/file.c out: · line 274
Quoted 10 lines
fs/fat/file.c +10
-0+ "fat_fallocate(): fat_get_cluster() error"); + goto error; + } + free_bytes = ((fclus +1 ) << sbi->cluster_bits) - + inode->i_size; + nr_bytes = offset + len - inode->i_size - free_bytes; +MSDOS_I (inode)->mmu_private = (fclus +1 ) << + sbi->cluster_bits; + }else + nr_bytes = offset + len - inode->i_size;
Hm, why do we need to re-compute ->mmu_private here?
Quoted 8 lines
static int fat_write_begin(struct file *file, struct
address_space*mapping,fs/fat/inode.c +3
-0loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata) { int err; +struct inode *inode = mapping->host; +struct super_block *sb = inode->i_sb; +loff_t i_size =i_size_read (inode);
fs/fat/inode.c static void fat_write_failed(struct address_space *mapping, loff_t to) · line 210
Quoted 10 lines
fs/fat/inode.c +10
-0+ +if (MSDOS_I (inode)->mmu_private >round_up (i_size, sb->s_blocksize) + && pos > i_size) { + err =fat_zero_falloc_area (file, mapping, pos); +if (err) { +fat_msg (sb,KERN_ERR , +"Error (%d) zeroing fallocated area" , err); +return err; + } + }
Again, I'm not fan of this way.
Normally, get_block() returns with buffer_new(). Then, caller checks blockdev buffer with
unmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
then, zeroed buffer. Do we really don't need to check this race?
Quoted 4 lines
static void
fat_evict_inode(struct inode *inode)
{fs/fat/inode.c +2
-0+ +struct super_block *sb = inode->i_sb;
fs/fat/inode.c EXPORT_SYMBOL_GPL(fat_build_inode); · line 561
Quoted 10 lines
fs/fat/inode.c +10
-0+ +/* +* Release unwritten fallocated blocks on file release. +* Do this only when the inode evict and i_count becomes 0. +*/ +mutex_lock (&inode->i_mutex); +if (round_up (inode->i_size, sb->s_blocksize) < +MSDOS_I (inode)->mmu_private &&atomic_read (&inode->i_count) ==0 ) +fat_truncate_blocks (inode, inode->i_size); +mutex_unlock (&inode->i_mutex);
This is strange.
- inode->i_count is always 0 here.
- nobody touch this inode anymore, so no need ->i_mutex.
- no need to truncate if ->i_nlink == 0. it should be done already.
fs/fat/inode.c EXPORT_SYMBOL_GPL(fat_build_inode); · line 565
Quoted 3 lines
fs/fat/inode.c +0
-0truncate_inode_pages (&inode->i_data,0 );if (!inode->i_nlink) { inode->i_size =0 ;
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
2013/9/21, OGAWA Hirofumi <[email protected]>:
Quoted 1 line
Namjae Jeon <[email protected]> writes:
Hi OGAWA.
Quoted 4 lines
Entirely, we would have to consider in the case of write fail (e.g.
-ENOSPC). If write failed, it will calltruncate(). Then, it can be truncate the fallocate region too unexpectedly.
I agree. I will update as your suggestion.
Quoted 11 lines
fs/fat/cache.c int fat_bmap(struct inode *inode, sector_t sector, sector_t *phys, · line 336
Quoted 9 lines
fs/fat/cache.c +8
-0+if (!create) { +/* +* to map cluster in case of read request +* for a block in fallocated region +*/ +if (MSDOS_I (inode)->mmu_private > +round_up (i_size, sb->s_blocksize)) + goto out_map_cluster;->mmu_private can't stable without
i_mutex. I.e. racy.
Yes. I will add lock here.
Quoted 18 lines
Quoted 18 lines
fs/fat/file.c +17
-0+/* +* Preallocate space for a file. This implements fat's fallocate file +* operation, which gets called from sys_fallocate system call. User +* space requests len bytes at offset. If FALLOC_FL_KEEP_SIZE is set +* we just allocate clusters without zeroing them out. Otherwise we +* allocate and zero out clusters via an expanding truncate. +*/ +static long fat_fallocate (struct file *file,int mode, +loff_t offset,loff_t len) +{ +int cluster, fclus, dclus; +int nr_cluster;/* Number of clusters to be allocated */ +loff_t nr_bytes;/* Number of bytes to be allocated*/ +loff_t free_bytes;/* Unused bytes in the last cluster of file*/ +struct inode *inode = file->f_mapping->host; +struct super_block *sb = inode->i_sb; +struct msdos_sb_info *sbi =MSDOS_SB (sb);
Quoted 8 lines
fs/fat/file.c out: · line 248
Quoted 5 lines
fs/fat/file.c +5
-0+int err =0 ; + +/* No support for hole punch or other fallocate flags. */ +if (mode & ~FALLOC_FL_KEEP_SIZE ) +return -EOPNOTSUPP ;Don't we need to check
S_ISREG()here? If this inode is dir,fallocate()will break dir.
No, It is surely needed. I will update.
Quoted 5 lines
Review
Quoted 2 lines
(quoted diff) +1
-0+ mutex_lock(&inode->i_mutex);Don't we need to check
inode_newsize_ok()? It seems nobody is checking rlimit.
Correct. I will update.
Quoted 10 lines
fs/fat/file.c out: · line 256
Quoted 7 lines
fs/fat/file.c +6
-0+if ((offset + len) <=MSDOS_I (inode)->mmu_private) { +fat_msg (sb,KERN_ERR , +"fat_fallocate(): Blocks already allocated" ); + err = -EINVAL ; + goto error; + }This is user error, so we should not print from kernel. Otherwise, attacker can make flood of kernel message.
okay, I will remove these codes.
Quoted 3 lines
Furthermore, is this right behavior? User should care about current fallocated size? Other FSes return
-EINVALtoo?
You're right. There is a mistake from me. Normally other fs return 0. I will update.
Quoted 12 lines
Quoted 12 lines
fs/fat/file.c +11
-0+if (mode &FALLOC_FL_KEEP_SIZE ) { +/* First compute the number of clusters to be allocated */ +if (inode->i_size >0 ) { + err =fat_get_cluster (inode,FAT_ENT_EOF , + &fclus, &dclus); +if (err <0 ) { +fat_msg (sb,KERN_ERR , +"fat_fallocate(): fat_get_cluster() error" ); + goto error; + } + free_bytes = ((fclus + 1) << sbi->cluster_bits) -
Quoted 8 lines
fs/fat/file.c out: · line 274
Quoted 6 lines
fs/fat/file.c +6
-0+ inode->i_size; + nr_bytes = offset + len - inode->i_size - free_bytes; +MSDOS_I (inode)->mmu_private = (fclus +1 ) << + sbi->cluster_bits; + }else + nr_bytes = offset + len - inode->i_size;Hm, why do we need to re-compute ->mmu_private here?
Not needed, I will remove unneeded compute code.
Quoted 23 lines
Quoted 10 lines
static int fat_write_begin(struct file *file, struct
address_space
*mapping,fs/fat/inode.c +3
-0loff_t pos, unsigned len, unsigned flags, struct page **pagep, void **fsdata) { int err; +struct inode *inode = mapping->host; +struct super_block *sb = inode->i_sb; +loff_t i_size =i_size_read (inode);fs/fat/inode.c static void fat_write_failed(struct address_space *mapping, loff_t to) · line 210
Quoted 10 lines
fs/fat/inode.c +10
-0+ +if (MSDOS_I (inode)->mmu_private >round_up (i_size, sb->s_blocksize) + && pos > i_size) { + err =fat_zero_falloc_area (file, mapping, pos); +if (err) { +fat_msg (sb,KERN_ERR , +"Error (%d) zeroing fallocated area" , err); +return err; + } + }Again, I'm not fan of this way.
Quoted 6 lines
Normally,
get_block()returns withbuffer_new(). Then, caller checks blockdev buffer withunmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);then, zeroed buffer. Do we really don't need to check this race?
We considered after your advice before. we reach for the conclusion that use this method.
because, Cluster is already allocated in fat fallocate and when we write with radom offset over i_size on fallocated region, It will be hit by fat cache in fat_bmap of get_block, which mean buffer is not set to new.
Quoted 18 lines
Quoted 5 lines
static void
fat_evict_inode(struct inode *inode)
{fs/fat/inode.c +2
-0+ +struct super_block *sb = inode->i_sb;fs/fat/inode.c EXPORT_SYMBOL_GPL(fat_build_inode); · line 561
Quoted 10 lines
fs/fat/inode.c +10
-0+ +/* +* Release unwritten fallocated blocks on file release. +* Do this only when the inode evict and i_count becomes 0. +*/ +mutex_lock (&inode->i_mutex); +if (round_up (inode->i_size, sb->s_blocksize) < +MSDOS_I (inode)->mmu_private &&atomic_read (&inode->i_count) ==0 ) +fat_truncate_blocks (inode, inode->i_size); +mutex_unlock (&inode->i_mutex);This is strange.
Quoted 8 lines
-
inode->i_countis always 0 here.
- nobody touch this inode anymore, so no need ->i_mutex.
- no need to truncate if ->i_nlink == 0. it should be done already.Quoted 3 lines
fs/fat/inode.c +0
-0truncate_inode_pages (&inode->i_data,0 );if (!inode->i_nlink) { inode->i_size =0 ;
Right. I will update patch.
Thanks for your valuable review!
Quoted 4 lines
Thanks.
--
OGAWA Hirofumi <[email protected]>
OGAWA Hirofumi
maintainerQuoted 17 lines · Namjae Jeon
Quoted 17 lines
fs/fat/inode.c static void fat_write_failed(struct address_space *mapping, loff_t to) · line 210
Quoted 9 lines
fs/fat/inode.c +9
-0+if (MSDOS_I (inode)->mmu_private >round_up (i_size, sb->s_blocksize) + && pos > i_size) { + err =fat_zero_falloc_area (file, mapping, pos); +if (err) { +fat_msg (sb,KERN_ERR , +"Error (%d) zeroing fallocated area" , err); +return err; + } + }Again, I'm not fan of this way.
Normally,
get_block()returns withbuffer_new(). Then, caller checks blockdev buffer withunmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);
Quoted 7 lines
Quoted 1 line
then, zeroed buffer. Do we really don't need to check this race?
We considered after your advice before. we reach for the conclusion that use this method.
because, Cluster is already allocated in fat fallocate and when we write with radom offset overi_sizeon fallocated region, It will be hit by fat cache infat_bmapofget_block, which mean buffer is not set to new.
Hm, how does it hit to fat cache? I think fat_alloc_clusters() and fat_chain_add() doesn't update fat cache, right? I.e. initial write after fallocate() should not hit fat cache over i_size?
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
2013/9/23, OGAWA Hirofumi <[email protected]>:
Quoted 27 lines
Quoted 17 lines · Namjae Jeon
Quoted 17 lines
fs/fat/inode.c static void fat_write_failed(struct address_space *mapping, loff_t to) · line 210
Quoted 9 lines
fs/fat/inode.c +9
-0+if (MSDOS_I (inode)->mmu_private >round_up (i_size, sb->s_blocksize) + && pos > i_size) { + err =fat_zero_falloc_area (file, mapping, pos); +if (err) { +fat_msg (sb,KERN_ERR , +"Error (%d) zeroing fallocated area" , err); +return err; + } + }Again, I'm not fan of this way.
Normally,
get_block()returns withbuffer_new(). Then, caller checks blockdev buffer withunmap_underlying_metadata(bh->b_bdev, bh->b_blocknr);Quoted 7 lines
Quoted 1 line
then, zeroed buffer. Do we really don't need to check this race?
We considered after your advice before. we reach for the conclusion that use this method.
because, Cluster is already allocated in fat fallocate and when we write with radom offset overi_sizeon fallocated region, It will be hit by fat cache infat_bmapofget_block, which mean buffer is not set to new.
Quoted 3 lines
Hm, how does it hit to fat cache? I think
fat_alloc_clusters()andfat_chain_add()doesn't update fat cache, right? I.e. initial write afterfallocate()should not hit fat cache overi_size?
Ah.. Sorry for wrong reply. old memory make me confusing. By allocating cluster in fat fallocate, when write, fat_bmap of get_block return physical sector number.
So buffer is not set to new in _fat_get_block.
When we fallocate with keep size on -> only clusters are added to the fat chain calling fat_get_cluster(),and add the cluster to cluster chain.
This doesn’t call fat_get_block() .
Now when we try to write in the fallocated region in the fat_write_begin() when it is called first time it checks that the mismatch is present between the mmu_private and mmu_actual (i.e., the file has pre-allocated blocks), and hence zero out the region ; Since buffer_new() is not set for fallocated region by fat_get_block() , we explicitly zero out the lseek'ed region using “fat_zero_falloc_area” and normal write occurs beyond that,and i_size is updated accordingly.
Thanks :)
Quoted 5 lines
Thanks.
--
OGAWA Hirofumi <[email protected]>
OGAWA Hirofumi
maintainerQuoted 16 lines · Namjae Jeon
Quoted 10 lines
Quoted 6 lines
We considered after your advice before. we reach for the conclusion that use this method.
because, Cluster is already allocated in fat fallocate and when we write with radom offset overi_sizeon fallocated region, It will be hit by fat cache infat_bmapofget_block, which mean buffer is not set to new.Hm, how does it hit to fat cache? I think
fat_alloc_clusters()andfat_chain_add()doesn't update fat cache, right? I.e. initial write afterfallocate()should not hit fat cache overi_size?Ah.. Sorry for wrong reply. old memory make me confusing. By allocating cluster in fat fallocate, when write,
fat_bmapofget_blockreturn physical sector number.
So buffer is not set to new in_fat_get_block.
Quoted 4 lines
When we fallocate with keep size on -> only clusters are added to the fat chain calling fat_get_cluster(),and add the cluster to cluster chain.
This doesn’t callfat_get_block().
Right.
Quoted 8 lines
Now when we try to write in the fallocated region in the
fat_write_begin()when it is called first time it checks that the mismatch is present between themmu_privateandmmu_actual(i.e., the file has pre-allocated blocks), and hence zero out the region ; Sincebuffer_new()is not set for fallocated region byfat_get_block(), we explicitly zero out the lseek'ed region using “fat_zero_falloc_area” and normal write occurs beyond that,andi_sizeis updated accordingly.
Yes. So I'm saying fixing fat_get_block() would not be hard.
For example, add new size the disk_size, totally 3 sizes - 1) i_size 2) mmu_private (aka, initialized size) 3) disk_size (aka, uninitialized size).
When called fat_get_block(), it checks the region between mmu_private and disk_size. If block hits that region, block is uninitialized area, so return as buffer_new().
Like this, I think it is not hard. Please consider like above example too.
Thanks.
Signature · 1 line
OGAWA Hirofumi <[email protected]>
2013/9/24, OGAWA Hirofumi <[email protected]>:
Quoted 40 lines
Quoted 16 lines · Namjae Jeon
Quoted 10 lines
Quoted 6 lines
We considered after your advice before. we reach for the conclusion that use this method.
because, Cluster is already allocated in fat fallocate and when we write with radom offset overi_sizeon fallocated region, It will be hit by fat cache infat_bmapofget_block, which mean buffer is not set to new.Hm, how does it hit to fat cache? I think
fat_alloc_clusters()andfat_chain_add()doesn't update fat cache, right? I.e. initial write afterfallocate()should not hit fat cache overi_size?Ah.. Sorry for wrong reply. old memory make me confusing. By allocating cluster in fat fallocate, when write,
fat_bmapofget_blockreturn physical sector number.
So buffer is not set to new in_fat_get_block.Quoted 4 lines
When we fallocate with keep size on -> only clusters are added to the fat chain calling fat_get_cluster(),and add the cluster to cluster chain.
This doesn’t callfat_get_block().Right.
Quoted 8 lines
Now when we try to write in the fallocated region in the
fat_write_begin()when it is called first time it checks that the mismatch is present between themmu_privateandmmu_actual(i.e., the file has pre-allocated blocks), and hence zero out the region ; Sincebuffer_new()is not set for fallocated region byfat_get_block(), we explicitly zero out the lseek'ed region using “fat_zero_falloc_area” and normal write occurs beyond that,andi_sizeis updated accordingly.Yes. So I'm saying fixing
fat_get_block()would not be hard.For example, add new size the
disk_size, totally 3 sizes - 1)i_size2)mmu_private(aka, initialized size) 3)disk_size(aka, uninitialized size).
Quoted 6 lines
When called
fat_get_block(), it checks the region betweenmmu_privateanddisk_size. If block hits that region, block is uninitialized area, so return asbuffer_new().Like this, I think it is not hard. Please consider like above example too.
Yes, I checked after change as your suggestion. it work fine. I will post the updated patch soon.
Thanks. OGAWA.
Quoted 5 lines
Thanks.
--
OGAWA Hirofumi <[email protected]>