hfsplus: convert regular file I/O to iomap
Subsystems (3)
Viacheslav Dubeyko
maintainerv5 cover hfsplus: convert regular file I/O to iomap
Switch the HFS+ regular file data path from buffer heads and blockdev_direct_IO() to iomap.
(1) adds a generic iomap_dio_end_io() helper to include/linux/iomap.h: a direct write ->end_io callback that updates i_size when the write goes beyond the end-of-file. It can be used by file systems that issue all extending direct writes synchronously under inode_lock.
(2) moves the file related operations (open, release, fsync and file_operations) from inode.c into the new file.c, without functional changes, to prepare the place for the iomap-based file operations.
(3) splits the extent lookup/allocation logic out of hfsplus_get_block() into hfsplus_map_extent(), which reports the mapping without a buffer_head, so that the iomap path can use it directly. No functional change for the buffer_head path.
(4) makes hfsplus_map_extent() return -EIO when no extent covers the requested block. On a corrupted volume, hfsplus_get_block() mapped such a block to allocation block 0, i.e. the boot blocks and the volume header. The iomap path relies on this check, since a zero-length extent would otherwise produce a zero-length mapping.
(5) adds the HFS+ iomap operations, implements iomap-based llseek/read_iter/write_iter, switches hfsplus_aops to iomap (removing hfsplus_direct_IO() and the LEGACY_DIRECT_IO dependency), and rolls back i_size and the allocated blocks when the zero-filling extension or the data write fails. HFS+ doesn't support holes, so extending writes and truncates zero-fill the gap up to the new end-of-file.
Changes since v4:
- iomap_dio_end_io() is extracted into a dedicated patch 1.
- Rebased on top of "hfsplus: don't take inode_lock in hfsplus_file_fsync()", "hfsplus: set s_maxbytes from the volume capacity" and "hfsplus: release preallocated blocks in hfsplus_file_truncate()", which are already applied.
- v5 patches 04, 06, 07 and 10 are squashed into patch 5.
- The -EIO check in hfsplus_map_extent() is moved out of the iomap conversion into the separate, documented patch 4.
- The phys_size change in hfsplus_get_block() is dropped: it is not related to iomap, since after the conversion hfsplus_get_block() only allocates blocks when phys_size is block-aligned.
- hfsplus_write_iomap_end() keeps fs_blocks and the inode byte count in whole blocks, as hfsplus_inode_read_fork() and hfsplus_file_truncate() do, while phys_size stays byte-granular for a partially written block.
- __hfsplus_iomap_begin() ends a mapping of initialized blocks at the block boundary above phys_size instead of at phys_size itself. With an unaligned on-disk size this used to produce a mapping length that is not a multiple of the logical block size, and an aligned O_DIRECT overwrite failed with -EINVAL.
- The fsync locking change is dropped from this series, since it is handled by the separate prep patch.
- The volume capacity check in hfsplus_file_write_iter() is dropped, since generic_write_checks() enforces s_maxbytes, which is set from the volume capacity.
Viacheslav Dubeyko (5):
iomap: add iomap_dio_end_io() helper
hfsplus: move file related operations to file.c
hfsplus: rework hfsplus_get_block() logic
hfsplus: return -EIO from hfsplus_map_extent() if no extent covers the
block
hfsplus: convert regular file I/O to iomap
fs/hfsplus/Kconfig | 2 +- fs/hfsplus/Makefile | 5 +- fs/hfsplus/extents.c | 163 +++++++++++++++------ fs/hfsplus/file.c | 308 ++++++++++++++++++++++++++++++++++++++++ fs/hfsplus/hfsplus_fs.h | 26 +++- fs/hfsplus/inode.c | 274 +++++++++++------------------------ fs/hfsplus/iomap.c | 254 +++++++++++++++++++++++++++++++++ fs/hfsplus/iomap.h | 17 +++ include/linux/iomap.h | 20 +++ 9 files changed, 823 insertions(+), 246 deletions(-) create mode 100644 fs/hfsplus/file.c create mode 100644 fs/hfsplus/iomap.c create mode 100644 fs/hfsplus/iomap.h
Signature · 1 line
2.53.0
Viacheslav Dubeyko
maintainerPATCH v5 1/5 iomap: add iomap_dio_end_io() helper
Several file systems implement an almost identical ->end_io callback for direct writes: if the write succeeded and went beyond the current end-of-file, update i_size and mark the inode dirty (for example, ext2_dio_write_end_io()). Add a generic iomap_dio_end_io() helper for this case, so that file systems don't need to open-code it.
The helper updates i_size without any locking, so it can be used only by file systems that issue all direct writes extending i_size synchronously (IOMAP_DIO_FORCE_WAIT) under inode_lock.
The first user will be HFS+.
--- include/linux/iomap.h | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+)
include/linux/iomap.h +20 -0
@@ -672,6 +672,26 @@ struct iomap_dio_ops {struct bio_set *bio_set; }; +/* +* Direct I/O completion handler +*/ +static inline +int iomap_dio_end_io (struct kiocb *iocb,ssize_t size, +int error,unsigned int flags) +{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (error) +return error; + +if (size &&i_size_read (inode) < iocb->ki_pos + size) { +i_size_write (inode, iocb->ki_pos + size); +mark_inode_dirty (inode); + } + +return 0 ; +} +/* * Wait for the I/O to complete in iomap_dio_rw even if the kiocb is not * synchronous.
Signature · 1 line
2.53.0
Christian Brauner
maintainerRe: [PATCH v5 1/5] iomap: add iomap_dio_end_io() helper
Quoted 3 lines · Viacheslav Dubeyko
The helper updates
i_sizewithout any locking, so it can be used only by file systems that issue all direct writes extendingi_sizesynchronously (IOMAP_DIO_FORCE_WAIT) underinode_lock.
Meh, this should really not be named so it looks like its the default completion handler. iomap_dio_end_io() really sounds like it.
Name it after what it does. Plus, check the rule. With IOMAP_DIO_FORCE_WAIT the completion runs in the context that still holds the inode lock, so the branch that updates i_size can assert that:
if (size && i_size_read(inode) < iocb->ki_pos + size) {
lockdep_assert_held_write(&inode->i_rwsem);
i_size_write(inode, iocb->ki_pos + size);
mark_inode_dirty(inode);
}include/linux/iomap.h struct iomap_dio_ops { · line 679
Quoted 2 lines
include/linux/iomap.h +2
-0+static inline +int iomap_dio_end_io(struct kiocb *iocb, ssize_t size,
This is an ->end_io callback so its address ends up in struct iomap_dio_ops. Please make it a regular exported function in fs/iomap/direct-io.c instead of a static inline in the header.
Quoted 5 lines
Several file systems implement an almost identical ->end_io callback for direct writes: if the write succeeded and went beyond the current end-of-file, update
i_sizeand mark the inode dirty (for example,ext2_dio_write_end_io()). Add a genericiomap_dio_end_io()helper for this case, so that file systems don't need to open-code it.
Hopefully that gets a follow-up patch right away and converts ext2.
Re: [PATCH v5 1/5] iomap: add iomap_dio_end_io() helper
Quoted 7 lines · Christian Brauner
Quoted 3 lines · Viacheslav Dubeyko
The helper updates
i_sizewithout any locking, so it can be used only by file systems that issue all direct writes extendingi_sizesynchronously (IOMAP_DIO_FORCE_WAIT) underinode_lock.Meh, this should really not be named so it looks like its the default completion handler.
iomap_dio_end_io()really sounds like it.
Well, it actually should be the default handler as it does the right thing for simple file systems. In fact I'd prefer to automatically call it if no ->end_io is set as that would remove a lot of boilerplate.
It would be nice to do that and remove the duplicate version in various file systems.
Viacheslav Dubeyko
maintainerRe: [PATCH v5 1/5] iomap: add iomap_dio_end_io() helper
Quoted 11 lines · Christoph Hellwig
Quoted 9 lines · Christian Brauner
Quoted 4 lines · Viacheslav Dubeyko
The helper updates
i_sizewithout any locking, so it can be used only
by file systems that issue all direct writes extendingi_sizesynchronously (IOMAP_DIO_FORCE_WAIT) underinode_lock.Meh, this should really not be named so it looks like its the default
completion handler.iomap_dio_end_io()really sounds like it.
Quoted 8 lines
Well, it actually should be the default handler as it does the right thing for simple file systems. In fact I'd prefer to automatically call it if no ->end_io is set as that would remove a lot of boilerplate.
It would be nice to do that and remove the duplicate version in various
file systems.
So, what's the final consensus on the function name? :) How should I name it?
Thanks,
Slava.
Viacheslav Dubeyko
maintainerRe: [PATCH v5 1/5] iomap: add iomap_dio_end_io() helper
In reply to Christian BraunerQuoted 14 lines · Christian Brauner
Quoted 4 lines · Viacheslav Dubeyko
The helper updates
i_sizewithout any locking, so it can be used only
by file systems that issue all direct writes extendingi_sizesynchronously (IOMAP_DIO_FORCE_WAIT) underinode_lock.Meh, this should really not be named so it looks like its the default completion handler.
iomap_dio_end_io()really sounds like it.Name it after what it does. Plus, check the rule. With
IOMAP_DIO_FORCE_WAITthe completion runs in the context that still holds
the inode lock, so the branch that updatesi_sizecan assert that:
Quoted 6 lines
if (size && i_size_read(inode) < iocb->ki_pos + size) {
lockdep_assert_held_write(&inode->i_rwsem);
i_size_write(inode, iocb->ki_pos + size);
mark_inode_dirty(inode);
}Makes sense. Thanks for suggestion.
Quoted 7 lines
include/linux/iomap.h struct iomap_dio_ops { · line 679
Quoted 2 lines
include/linux/iomap.h +2
-0+static inline +int iomap_dio_end_io(struct kiocb *iocb, ssize_t size,This is an ->end_io callback so its address ends up in struct
iomap_dio_ops. Please make it a regular exported function infs/iomap/direct-io.cinstead of a static inline in the header.
Sounds good.
Quoted 10 lines
Quoted 8 lines
Several file systems implement an almost identical ->end_io callback
for direct writes: if the write succeeded and went beyond the current
end-of-file, updatei_sizeand mark the inode dirty (for example,ext2_dio_write_end_io()). Add a genericiomap_dio_end_io()helper for
this case, so that file systems don't need to open-code it.Hopefully that gets a follow-up patch right away and converts ext2.
I'll do my best. :)
Thanks,
Slava.
Viacheslav Dubeyko
maintainerPATCH v5 2/5 hfsplus: move file related operations to file.c
In reply to Viacheslav DubeykoThis patch introduces fs/hfsplus/file.c and moves
file related operations from fs/hfsplus/inode.c
into the new file.
--- fs/hfsplus/Makefile | 5 +- fs/hfsplus/file.c | 127 ++++++++++++++++++++++++++++++++++++++++ fs/hfsplus/hfsplus_fs.h | 7 ++- fs/hfsplus/inode.c | 119 ------------------------------------- 4 files changed, 135 insertions(+), 123 deletions(-) create mode 100644 fs/hfsplus/file.c
fs/hfsplus/Makefile +3 -2
@@ -5,8 +5,9 @@ obj-$(CONFIG_HFSPLUS_FS) += hfsplus.o -hfsplus-objs := super.o options.o inode.o ioctl.o extents.o catalog.o dir.o btree.o \ - bnode.o brec.o bfind.o tables.o unicode.o wrapper.o bitmap.o part_tbl.o \ +hfsplus-objs := super.o options.o inode.o file.o ioctl.o extents.o catalog.o dir.o \ + btree.o bnode.o brec.o bfind.o tables.o unicode.o wrapper.o bitmap.o \ + part_tbl.o \ attributes.o xattr.o xattr_user.o xattr_security.o xattr_trusted.o # KUnit tests
fs/hfsplus/file.c new file +127 -0
127 lines not on this page · raw message
fs/hfsplus/hfsplus_fs.h +5 -2
@@ -436,6 +436,11 @@ int hfsplus_rename_cat(u32 cnid, struct inode *src_dir, const struct qstr *src_nextern const struct inode_operations hfsplus_dir_inode_operations;extern const struct file_operations hfsplus_dir_operations; +/* file.c */ +extern const struct file_operations hfsplus_file_operations; +int hfsplus_file_fsync (struct file *file,loff_t start,loff_t end, +int datasync); +/* extents.c */ int hfsplus_ext_cmp_key (const hfsplus_btree_key *k1,const hfsplus_btree_key *k2);
@@ -468,8 +473,6 @@ int hfsplus_cat_write_inode(struct inode *inode);int hfsplus_getattr (const struct mnt_idmap *idmap,const struct path *path,struct kstat *stat,u32 request_mask,unsigned int query_flags); -int hfsplus_file_fsync (struct file *file,loff_t start,loff_t end, -int datasync);int hfsplus_fileattr_get (struct dentry *dentry,struct file_kattr *fa);int hfsplus_fileattr_set (const struct mnt_idmap *idmap,struct dentry *dentry,struct file_kattr *fa);
fs/hfsplus/inode.c +0 -119
137 lines not on this page · raw message
Signature · 1 line
2.53.0
Re: [PATCH v5 2/5] hfsplus: move file related operations to file.c
fs/hfsplus/file.c line 1
Quoted 4 lines · Viacheslav Dubeyko
fs/hfsplus/file.c new file +1
-0@@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0
This loses the copyright information from inode.c
Viacheslav Dubeyko
maintainerRe: [PATCH v5 2/5] hfsplus: move file related operations to file.c
Quoted 7 lines · Christoph Hellwig
fs/hfsplus/file.c line 1
Quoted 4 lines · Viacheslav Dubeyko
fs/hfsplus/file.c new file +1
-0@@ -0,0 +1,127 @@ +// SPDX-License-Identifier: GPL-2.0 This loses the copyright information from
inode.c
Yeah, you are right. I missed this. Will add it to the next version of the patch.
Thanks,
Slava.
Viacheslav Dubeyko
maintainerPATCH v5 3/5 hfsplus: rework hfsplus_get_block() logic
In reply to Viacheslav DubeykoSplit the extent-lookup/allocate logic out of hfsplus_get_block() into a new hfsplus_map_extent(), which reports the mapping as (dblock, max_blocks, balloc) instead of filling in a buffer_head. hfsplus_get_block() becomes a thin wrapper around it for the buffer_head-based callers (B-tree metadata, symlinks).
No functional change to the existing buffer_head path. This is preparation for the iomap-based regular file I/O path added in a later patch, which will call hfsplus_map_extent() directly.
--- fs/hfsplus/extents.c | 116 ++++++++++++++++++++++++++++------------ fs/hfsplus/hfsplus_fs.h | 2 + 2 files changed, 85 insertions(+), 33 deletions(-)
fs/hfsplus/extents.c +83 -33
@@ -48,18 +48,27 @@ static void hfsplus_ext_build_key(hfsplus_btree_key *key, u32 cnid, key->ext.pad =0 ; } -static u32 hfsplus_ext_find_block (struct hfsplus_extent *ext,u32 off) +/* +* Find the disk allocation block for 'off' within an 8-entry +* extent record, and the number of further allocation blocks +* that are contiguous with it in the same extent entry. +*/ +static u32 hfsplus_ext_find_block (struct hfsplus_extent *ext,u32 off, +u32 *dblock) {int i;u32 count; -for (i =0 ; i <8 ; ext++, i++) { +for (i =0 ; i <HFSPLUS_FORK_EXTENT_COUNT ; ext++, i++) { count =be32_to_cpu (ext->block_count); -if (off < count) -return be32_to_cpu (ext->start_block) + off; +if (off < count) { + *dblock =be32_to_cpu (ext->start_block) + off; +return count - off; + } off -= count; }/* panic? */ + *dblock =0 ;return 0 ; }
@@ -68,7 +77,7 @@ static int hfsplus_ext_block_count(struct hfsplus_extent *ext)int i;u32 count =0 ; -for (i =0 ; i <8 ; ext++, i++) +for (i =0 ; i <HFSPLUS_FORK_EXTENT_COUNT ; ext++, i++) count +=be32_to_cpu (ext->block_count);return count; }
@@ -225,37 +234,46 @@ static int hfsplus_ext_read_extent(struct inode *inode, u32 block)return res; } -/* Get a block at iblock for inode, possibly allocating if create */ -int hfsplus_get_block (struct inode *inode,sector_t iblock, -struct buffer_head *bh_result,int create) +/* +* Find or allocate a sequence of allocation blocks. +* +* Looks up the allocation block at 'ablock' for inode, extending the +* file (via hfsplus_file_extend(), in clump_blocks-sized chunks) when +* 'create' is set and 'ablock' lies beyond the current allocation. +* +* On success, *dblock is the disk allocation block backing 'ablock', +* and *max_blocks is the number of further allocation blocks that are +* contiguous with it (i.e. the remaining length of the extent entry +* that contains 'ablock'), which may be smaller than the whole file's +* remaining allocation when the fork is fragmented across several +* extent entries. If a new extent had to be allocated to satisfy the +* request, *balloc (when non-NULL) is set to true. +*/ +int hfsplus_map_extent (struct inode *inode,u32 ablock,int create, +u32 *dblock,u32 *max_blocks,bool *balloc) { -struct super_block *sb = inode->i_sb; -struct hfsplus_sb_info *sbi =HFSPLUS_SB (sb);struct hfsplus_inode_info *hip =HFSPLUS_I (inode); -int res = -EIO ; -u32 ablock, dblock, mask; -sector_t sector; -int was_dirty =0 ; +int was_dirty; +int res; -/* Convert inode block to disk allocation block */ - ablock = iblock >> sbi->fs_shift; +if (balloc) + *balloc = false; -if (iblock >= hip->fs_blocks) { +if (ablock >= hip->alloc_blocks) {if (!create) -return 0 ; -if (iblock > hip->fs_blocks)return -EIO ; -if (ablock >= hip->alloc_blocks) { - res =hfsplus_file_extend (inode, false); -if (res) -return res; - } - }else - create =0 ; + res =hfsplus_file_extend (inode, false); +if (res) +return res; +if (balloc) + *balloc = true; + }if (ablock < hip->first_blocks) { - dblock =hfsplus_ext_find_block (hip->first_extents, ablock); - goto done; + *max_blocks =hfsplus_ext_find_block (hip->first_extents, + ablock, + dblock); +return 0 ; }if (inode->i_ino ==HFSPLUS_EXT_CNID )
@@ -274,11 +292,44 @@ int hfsplus_get_block(struct inode *inode, sector_t iblock,mutex_unlock (&hip->extents_lock);return -EIO ; } - dblock =hfsplus_ext_find_block (hip->cached_extents, - ablock - hip->cached_start); + *max_blocks =hfsplus_ext_find_block (hip->cached_extents, + ablock - hip->cached_start, + dblock);mutex_unlock (&hip->extents_lock); -done: +if (was_dirty) +mark_inode_dirty (inode); + +return 0 ; +} + +/* Get a block at iblock for inode, possibly allocating if create */ +int hfsplus_get_block (struct inode *inode,sector_t iblock, +struct buffer_head *bh_result,int create) +{ +struct super_block *sb = inode->i_sb; +struct hfsplus_sb_info *sbi =HFSPLUS_SB (sb); +struct hfsplus_inode_info *hip =HFSPLUS_I (inode); +u32 ablock, dblock, mask, max_blocks; +sector_t sector; +int res; + +/* Convert inode block to disk allocation block */ + ablock = iblock >> sbi->fs_shift; + +if (iblock >= hip->fs_blocks) { +if (!create) +return 0 ; +if (iblock > hip->fs_blocks) +return -EIO ; + }else + create =0 ; + + res =hfsplus_map_extent (inode, ablock, create, &dblock, &max_blocks, +NULL ); +if (res) +return res; +hfs_dbg ("ino %llu, iblock %llu - dblock %u\n" , inode->i_ino, (long long )iblock, dblock);
@@ -292,9 +343,8 @@ int hfsplus_get_block(struct inode *inode, sector_t iblock, hip->phys_size += sb->s_blocksize; hip->fs_blocks++;inode_add_bytes (inode, sb->s_blocksize); - } -if (create || was_dirty)mark_inode_dirty (inode); + }return 0 ; }
fs/hfsplus/hfsplus_fs.h +2 -0
@@ -447,6 +447,8 @@ int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,int hfsplus_ext_write_extent (struct inode *inode);int hfsplus_get_block (struct inode *inode,sector_t iblock,struct buffer_head *bh_result,int create); +int hfsplus_map_extent (struct inode *inode,u32 ablock,int create, +u32 *dblock,u32 *max_blocks,bool *balloc);int hfsplus_free_fork (struct super_block *sb,u32 cnid,struct hfsplus_fork_raw *fork,int type);int hfsplus_file_extend (struct inode *inode,bool zeroout);
Signature · 1 line
2.53.0
Re: [PATCH v5 3/5] hfsplus: rework hfsplus_get_block() logic
Quoted 5 lines · Viacheslav Dubeyko
+/*
+ * Find the disk allocation block for 'off' within an 8-entry
+ * extent record, and the number of further allocation blocks
+ * that are contiguous with it in the same extent entry.
+ */
You can use up all 80 characters for comments..
Otherwise looks good:
Viacheslav Dubeyko
maintainerPATCH v5 4/5 hfsplus: return -EIO from hfsplus_map_extent() if no extent covers the block
In reply to Viacheslav Dubeykohfsplus_ext_find_block() walks one 8-entry extent record and returns the number of contiguous allocation blocks starting at the requested offset. If the offset lies beyond all entries of the record, it sets *dblock to 0 and returns 0. hfsplus_map_extent() ignores this and returns success, so the caller gets allocation block 0 as the mapping.
This can only happen on a corrupted volume, for example when the extents overflow b-tree lacks a record for a range that is below alloc_blocks, or when the found record does not cover the requested block. THe hfsplus_get_block() then maps the file block to allocation block 0, which holds the boot blocks and the volume header. A read returns that metadata as file data, and writeback of a dirty page of such a file overwrites the volume header.
Return -EIO from hfsplus_map_extent() when hfsplus_ext_find_block() finds no extent, for both the fork's first extents and the cached extent record. The iomap conversion later in this series relies on this too: __hfsplus_iomap_begin() derives the mapping length from max_blocks, and a zero max_blocks would produce an invalid zero-length mapping instead of an error.
--- fs/hfsplus/extents.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-)
fs/hfsplus/extents.c +6 -1
@@ -67,7 +67,7 @@ static u32 hfsplus_ext_find_block(struct hfsplus_extent *ext, u32 off, } off -= count; } -/* panic? */ +/* 'off' lies beyond this extent record; callers treat 0 as an error */ *dblock =0 ;return 0 ; }
@@ -273,6 +273,8 @@ int hfsplus_map_extent(struct inode *inode, u32 ablock, int create, *max_blocks =hfsplus_ext_find_block (hip->first_extents, ablock, dblock); +if (!*max_blocks) +return -EIO ;return 0 ; }
@@ -300,6 +302,9 @@ int hfsplus_map_extent(struct inode *inode, u32 ablock, int create,if (was_dirty)mark_inode_dirty (inode); +if (!*max_blocks) +return -EIO ; +return 0 ; }
Signature · 1 line
2.53.0
Re: [PATCH v5 4/5] hfsplus: return -EIO from hfsplus_map_extent() if no extent covers the block
Looks good:
Viacheslav Dubeyko
maintainerPATCH v5 5/5 hfsplus: convert regular file I/O to iomap
In reply to Viacheslav DubeykoSwitch the HFS+ regular file data path from buffer heads and blockdev_direct_IO() to iomap.
Add iomap.h and iomap.c with hfsplus_iomap_ops, hfsplus_write_iomap_ops, hfsplus_writeback_ops and hfsplus_write_dio_ops. iomap.c implements __hfsplus_iomap_begin(), hfsplus_write_iomap_end(), hfsplus_iomap_cont_expand() and hfsplus_writeback_range(), which are the basis of the HFS+ iomap operations. Direct write completion uses the generic iomap_dio_end_io() helper.
Implement iomap-based hfsplus_file_llseek(), hfsplus_file_read_iter() and hfsplus_file_write_iter(). The volume capacity limit for writes is enforced by generic_write_checks(), since s_maxbytes is already set from the volume capacity.
Redefine hfsplus_aops as iomap-based operations for regular files. As a result, hfsplus_direct_IO() is removed as a user of blockdev_direct_IO(), and the LEGACY_DIRECT_IO dependency is dropped from Kconfig. hfsplus_file_truncate() extends regular files by means of hfsplus_iomap_cont_expand(), while other inodes still use the write_begin/write_end path.
HFS+ does not support holes, so a write or truncate that extends a file first zero-fills the range between the old end-of-file and the new one by means of hfsplus_iomap_cont_expand(). Introduce hfsplus_iomap_extend_size() that grows i_size, zero-fills the gap and rolls back on failure, and use it in hfsplus_file_write_iter() and hfsplus_setattr(). Introduce hfsplus_iomap_size_rollback() that waits for in-flight direct I/O, sets i_size, truncates the page cache beyond it and calls hfsplus_file_truncate() to release the blocks allocated past it. It is not called when phys_size is below the new size, since hfsplus_file_truncate() would zero-fill again in that case.
In hfsplus_file_write_iter(), roll back after the data write if fewer bytes than requested were written: to the original size if nothing was written, or to the current i_size after a short write. Queued asynchronous direct I/O is skipped, because direct I/O beyond phys_size is forced to be synchronous, so a queued request never extends the file.
--- fs/hfsplus/Kconfig | 2 +- fs/hfsplus/Makefile | 6 +- fs/hfsplus/extents.c | 40 +++++-- fs/hfsplus/file.c | 187 ++++++++++++++++++++++++++++- fs/hfsplus/hfsplus_fs.h | 17 +++ fs/hfsplus/inode.c | 155 ++++++++++++------------ fs/hfsplus/iomap.c | 254 ++++++++++++++++++++++++++++++++++++++++ fs/hfsplus/iomap.h | 17 +++ 8 files changed, 583 insertions(+), 95 deletions(-) create mode 100644 fs/hfsplus/iomap.c create mode 100644 fs/hfsplus/iomap.h
fs/hfsplus/Kconfig +1 -1
@@ -5,7 +5,7 @@ config HFSPLUS_FS select BUFFER_HEAD select NLS select NLS_UTF8 - select LEGACY_DIRECT_IO + select FS_IOMAP help If you say Y here, you will be able to mount extended format Macintosh-formatted hard drive partitions with full read-write access.
fs/hfsplus/Makefile +3 -3
@@ -5,9 +5,9 @@ obj-$(CONFIG_HFSPLUS_FS) += hfsplus.o -hfsplus-objs := super.o options.o inode.o file.o ioctl.o extents.o catalog.o dir.o \ - btree.o bnode.o brec.o bfind.o tables.o unicode.o wrapper.o bitmap.o \ - part_tbl.o \ +hfsplus-objs := super.o options.o inode.o file.o iomap.o ioctl.o extents.o \ + catalog.o dir.o btree.o bnode.o brec.o bfind.o tables.o \ + unicode.o wrapper.o bitmap.o part_tbl.o \ attributes.o xattr.o xattr_user.o xattr_security.o xattr_trusted.o # KUnit tests
fs/hfsplus/extents.c +27 -13
@@ -15,6 +15,7 @@#include "hfsplus_fs.h" #include "hfsplus_raw.h" +#include "iomap.h" /* Compare two extents keys, returns 0 on same, pos/neg for difference */ int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,
@@ -610,20 +611,33 @@ void hfsplus_file_truncate(struct inode *inode) inode->i_ino, (long long)hip->phys_size, inode->i_size);if (inode->i_size > hip->phys_size) { -struct address_space *mapping = inode->i_mapping; -struct folio *folio; -void *fsdata =NULL ; -loff_t size = inode->i_size; +if (S_ISREG (inode->i_mode)) { + res =hfsplus_iomap_cont_expand (inode, hip->phys_size, + inode->i_size); +if (res) +return ; + +mark_inode_dirty (inode); + }else { +struct address_space *mapping = inode->i_mapping; +struct folio *folio; +void *fsdata =NULL ; + + res =hfsplus_write_begin (NULL , mapping, + inode->i_size,0 , + &folio, &fsdata); +if (res) +return ; + + res =generic_write_end (NULL , mapping, + inode->i_size,0 ,0 , + folio, fsdata); +if (res <0 ) +return ; + +mark_inode_dirty (inode); + } - res =hfsplus_write_begin (NULL , mapping, size,0 , - &folio, &fsdata); -if (res) -return ; - res =generic_write_end (NULL , mapping, size,0 ,0 , - folio, fsdata); -if (res <0 ) -return ; -mark_inode_dirty (inode);return ; }
fs/hfsplus/file.c +184 -3
209 lines not on this page · raw message
fs/hfsplus/hfsplus_fs.h +17 -0
@@ -176,6 +176,22 @@ static inline struct hfsplus_sb_info *HFSPLUS_SB(struct super_block *sb)return sb->s_fs_info; } +/* +* Physical byte offset of allocation block 'dblock' on the volume. +*/ +static inline loff_t hfsplus_ablock_to_phys_bytes (struct super_block *sb, +u32 dblock) +{ +struct hfsplus_sb_info *sbi =HFSPLUS_SB (sb); +loff_t phys_bytes; + + phys_bytes = dblock; + phys_bytes <<= sbi->fs_shift; + phys_bytes += sbi->blockoffset; + phys_bytes <<= sb->s_blocksize_bits; + +return phys_bytes; +}struct hfsplus_inode_info {atomic_t opencnt;
@@ -456,6 +472,7 @@ void hfsplus_file_truncate(struct inode *inode);/* inode.c */ extern const struct address_space_operations hfsplus_aops; +extern const struct address_space_operations hfsplus_symlink_aops;extern const struct address_space_operations hfsplus_btree_aops;extern const struct dentry_operations hfsplus_dentry_operations;
fs/hfsplus/inode.c +80 -75
215 lines not on this page · raw message
fs/hfsplus/iomap.c new file +254 -0
254 lines not on this page · raw message
fs/hfsplus/iomap.h new file +17 -0
@@ -0,0 +1,17 @@ +/* SPDX-License-Identifier: GPL-2.0 */ + +#ifndef _LINUX_HFSPLUS_IOMAP_H +#define _LINUX_HFSPLUS_IOMAP_H + +extern const struct iomap_ops hfsplus_iomap_ops; +extern const struct iomap_ops hfsplus_write_iomap_ops; +extern const struct iomap_writeback_ops hfsplus_writeback_ops; +extern const struct iomap_dio_ops hfsplus_write_dio_ops; + +int hfsplus_iomap_cont_expand (struct inode *inode,loff_t from,loff_t to); +void hfsplus_iomap_size_rollback (struct inode *inode,loff_t size); +int hfsplus_iomap_extend_size (struct inode *inode,loff_t new_size); +int hfsplus_iomap_swap_activate (struct swap_info_struct *sis, +struct file *file,sector_t *span); + +#endif /* _LINUX_HFSPLUS_IOMAP_H */
Signature · 1 line
2.53.0
Quoted 173 lines · Viacheslav Dubeyko
Switch the HFS+ regular file data path from buffer heads and
blockdev_direct_IO()to iomap.Add
iomap.handiomap.cwithhfsplus_iomap_ops,hfsplus_write_iomap_ops,hfsplus_writeback_opsandhfsplus_write_dio_ops.iomap.cimplements__hfsplus_iomap_begin(),hfsplus_write_iomap_end(),hfsplus_iomap_cont_expand()andhfsplus_writeback_range(), which are the basis of the HFS+ iomap operations. Direct write completion uses the genericiomap_dio_end_io()helper.Implement iomap-based
hfsplus_file_llseek(),hfsplus_file_read_iter()andhfsplus_file_write_iter(). The volume capacity limit for writes is enforced bygeneric_write_checks(), sinces_maxbytesis already set from the volume capacity.Redefine
hfsplus_aopsas iomap-based operations for regular files. As a result,hfsplus_direct_IO()is removed as a user ofblockdev_direct_IO(), and theLEGACY_DIRECT_IOdependency is dropped fromKconfig.hfsplus_file_truncate()extends regular files by means ofhfsplus_iomap_cont_expand(), while other inodes still use the write_begin/write_end path.HFS+ does not support holes, so a write or truncate that extends a file first zero-fills the range between the old end-of-file and the new one by means of
hfsplus_iomap_cont_expand(). Introducehfsplus_iomap_extend_size()that growsi_size, zero-fills the gap and rolls back on failure, and use it inhfsplus_file_write_iter()andhfsplus_setattr(). Introducehfsplus_iomap_size_rollback()that waits for in-flight direct I/O, setsi_size, truncates the page cache beyond it and callshfsplus_file_truncate()to release the blocks allocated past it. It is not called whenphys_sizeis below the new size, sincehfsplus_file_truncate()would zero-fill again in that case.In
hfsplus_file_write_iter(), roll back after the data write if fewer bytes than requested were written: to the original size if nothing was written, or to the currenti_sizeafter a short write. Queued asynchronous direct I/O is skipped, because direct I/O beyondphys_sizeis forced to be synchronous, so a queued request never extends the file.cc Christoph Hellwig <[email protected]>cc John Paul Adrian Glaubitz <[email protected]>cc Yangtao Li <[email protected]>Signed-off-by Viacheslav Dubeyko <[email protected]>--- fs/hfsplus/Kconfig | 2 +- fs/hfsplus/Makefile | 6 +- fs/hfsplus/extents.c | 40 +++++-- fs/hfsplus/file.c | 187 ++++++++++++++++++++++++++++- fs/hfsplus/hfsplus_fs.h | 17 +++ fs/hfsplus/inode.c | 155 ++++++++++++------------ fs/hfsplus/iomap.c | 254 ++++++++++++++++++++++++++++++++++++++++ fs/hfsplus/iomap.h | 17 +++ 8 files changed, 583 insertions(+), 95 deletions(-) create mode 100644 fs/hfsplus/iomap.c create mode 100644 fs/hfsplus/iomap.hfs/hfsplus/Kconfig +1
-1@@ -5,7 +5,7 @@ config HFSPLUS_FS select BUFFER_HEAD select NLS select NLS_UTF8 - select LEGACY_DIRECT_IO + select FS_IOMAP help If you say Y here, you will be able to mount extended format Macintosh-formatted hard drive partitions with full read-write access.fs/hfsplus/Makefile +3
-3@@ -5,9 +5,9 @@ obj-$(CONFIG_HFSPLUS_FS) += hfsplus.o -hfsplus-objs := super.o options.o inode.o file.o ioctl.o extents.o catalog.o dir.o \ - btree.o bnode.o brec.o bfind.o tables.o unicode.o wrapper.o bitmap.o \ - part_tbl.o \ +hfsplus-objs := super.o options.o inode.o file.o iomap.o ioctl.o extents.o \ + catalog.o dir.o btree.o bnode.o brec.o bfind.o tables.o \ + unicode.o wrapper.o bitmap.o part_tbl.o \ attributes.o xattr.o xattr_user.o xattr_security.o xattr_trusted.o # KUnit testsfs/hfsplus/extents.c +27
-13@@ -15,6 +15,7 @@#include "hfsplus_fs.h" #include "hfsplus_raw.h" +#include "iomap.h" /* Compare two extents keys, returns 0 on same, pos/neg for difference */ int hfsplus_ext_cmp_key(const hfsplus_btree_key *k1,@@ -610,20 +611,33 @@ void hfsplus_file_truncate(struct inode *inode) inode->i_ino, (long long)hip->phys_size, inode->i_size);if (inode->i_size > hip->phys_size) { -struct address_space *mapping = inode->i_mapping; -struct folio *folio; -void *fsdata =NULL ; -loff_t size = inode->i_size; +if (S_ISREG (inode->i_mode)) { + res =hfsplus_iomap_cont_expand (inode, hip->phys_size, + inode->i_size); +if (res) +return ; + +mark_inode_dirty (inode); + }else { +struct address_space *mapping = inode->i_mapping; +struct folio *folio; +void *fsdata =NULL ; + + res =hfsplus_write_begin (NULL , mapping, + inode->i_size,0 , + &folio, &fsdata); +if (res) +return ; + + res =generic_write_end (NULL , mapping, + inode->i_size,0 ,0 , + folio, fsdata); +if (res <0 ) +return ; + +mark_inode_dirty (inode); + } - res =hfsplus_write_begin (NULL , mapping, size,0 , - &folio, &fsdata); -if (res) -return ; - res =generic_write_end (NULL , mapping, size,0 ,0 , - folio, fsdata); -if (res <0 ) -return ; -mark_inode_dirty (inode);return ; }fs/hfsplus/file.c +3
-0@@ -3,9 +3,11 @@#include <linux/fs.h> #include <linux/uio.h> #include <linux/mount.h> +#include <linux/iomap.h> #include "hfsplus_fs.h" #include "hfsplus_raw.h" +#include "iomap.h" static int hfsplus_file_open (struct inode *inode,struct file *file) {@@ -14,6 +16,7 @@ static int hfsplus_file_open(struct inode *inode, struct file *file)if (!(file->f_flags &O_LARGEFILE ) &&i_size_read (inode) >MAX_NON_LFS )return -EOVERFLOW ;atomic_inc (&HFSPLUS_I (inode)->opencnt); + file->f_mode |=FMODE_CAN_ODIRECT ;return 0 ; }
fs/hfsplus/file.c int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end, · line 124
Quoted 9 lines
fs/hfsplus/file.c +6
-0@@ -113,10 +116,188 @@ int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end,return error; } +/* +* Fall back to buffered I/O for the tail of a write that +* iomap_dio_rw() could not perform directly (unaligned tail, +* or no blocks could be mapped without allocation +* outside the direct path). +*/
Here you can use up the entire 80 characters as well.
fs/hfsplus/file.c int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end, · line 132
Quoted 8 lines
fs/hfsplus/file.c +8
-0+static ssize_t hfsplus_fallback_buffered_write (struct kiocb *iocb, +struct iov_iter *from) +{ +loff_t offset = iocb->ki_pos, end; +ssize_t written; +int ret; + + iocb->ki_flags &= ~IOCB_DIRECT ;
Does this need to wait for a zero i_dio_count just as it has been fixed in various files recently?
It would be kinda nice to factor all this fallback logic into a helper so that we have one maintained set of this logic instead of a copy in various file systems.
fs/hfsplus/file.c int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end, · line 162
Quoted 4 lines
fs/hfsplus/file.c +4
-0+ ret =iomap_dio_rw (iocb, from, + &hfsplus_write_iomap_ops, + &hfsplus_write_dio_ops, + dio_flags,NULL ,0 );
You can use up the space available:
ret = iomap_dio_rw(iocb, from, &hfsplus_write_iomap_ops, &hfsplus_write_dio_ops, dio_flags, NULL, 0);
fs/hfsplus/file.c int hfsplus_file_fsync(struct file *file, loff_t start, loff_t end, · line 219
Quoted 3 lines
fs/hfsplus/file.c +3
-0+if (iocb->ki_pos +iov_iter_count (iter) > +HFSPLUS_I (inode)->phys_size) + dio_flags |=IOMAP_DIO_FORCE_WAIT ;
Add a comment on why the wait is forced?