hfsplus: convert regular file I/O to iomap-based operations
Subsystems (3)
Viacheslav Dubeyko
maintainercover hfsplus: convert regular file I/O to iomap-based operations
This series moves HFS+ regular file data I/O off the legacy buffer_head/blockdev_direct_IO() path onto iomap-based operations with the goal to retire the older buffer_head-based direct I/O infrastructure.
Patchset is based on for-next branch of HFS/HFS+ git tree [1].
[1] https://git.kernel.org/pub/scm/linux/kernel/git/vdubeyko/hfs.git/
Viacheslav Dubeyko (6):
hfs/hfsplus: exchange hardcoded number of extents on named constants
hfsplus: rework hfsplus_get_block() logic
hfsplus: take the bitmap page lock for allocate/free
hfsplus: add iomap operations for regular file data
hfsplus: add iomap-based file_operations
hfsplus: switch address_space_operations on iomap-based support
fs/hfsplus/Kconfig | 2 +- fs/hfsplus/Makefile | 5 +- fs/hfsplus/bitmap.c | 13 ++ fs/hfsplus/extents.c | 157 +++++++++++++------ fs/hfsplus/file.c | 309 +++++++++++++++++++++++++++++++++++++ fs/hfsplus/hfsplus_fs.h | 26 +++- fs/hfsplus/inode.c | 264 +++++++++---------------------- fs/hfsplus/iomap.c | 209 +++++++++++++++++++++++++ fs/hfsplus/iomap.h | 20 +++ include/linux/hfs_common.h | 8 +- 10 files changed, 765 insertions(+), 248 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.43.0
Viacheslav Dubeyko
maintainerPATCH 1/6 hfs/hfsplus: exchange hardcoded number of extents on named constants
Replace the magic 8/3 fork extent-count literals with named constants (HFS_FORK_EXTENT_COUNT and HFSPLUS_FORK_EXTENT_COUNT). No functional change.
--- include/linux/hfs_common.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-)
include/linux/hfs_common.h +6 -2
@@ -171,18 +171,22 @@ enum { HFS_XATTR_NAME, }; +#define HFS_FORK_EXTENT_COUNT (3) +struct hfs_extent {__be16 block;__be16 count; }; -typedef struct hfs_extent hfs_extent_rec [3 ]; +typedef struct hfs_extent hfs_extent_rec [HFS_FORK_EXTENT_COUNT ]; + +#define HFSPLUS_FORK_EXTENT_COUNT (8)/* A single contiguous area of a file */ struct hfsplus_extent {__be32 start_block;__be32 block_count; } __packed; -typedef struct hfsplus_extent hfsplus_extent_rec [8 ]; +typedef struct hfsplus_extent hfsplus_extent_rec [HFSPLUS_FORK_EXTENT_COUNT ];/* Information for a "Fork" in a file */ struct hfsplus_fork_raw {
Signature · 1 line
2.43.0
Viacheslav Dubeyko
maintainerPATCH 2/6 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 | 118 +++++++++++++++++++++++++++++----------- fs/hfsplus/hfsplus_fs.h | 2 + 2 files changed, 87 insertions(+), 33 deletions(-)
fs/hfsplus/extents.c +85 -33
@@ -48,18 +48,29 @@ 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) +/* +* hfsplus_ext_find_block() - find contiguous sequence of block +* +* 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 +79,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; }
@@ -223,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) +/* +* hfsplus_map_extent() - 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 )
@@ -272,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);
@@ -290,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
@@ -436,6 +436,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.43.0
Viacheslav Dubeyko
maintainerPATCH 3/6 hfsplus: take the bitmap page lock for allocate/free
In reply to Viacheslav DubeykoThe hfsplus_block_allocate() and hfsplus_block_free() kmap the allocation bitmap's pages and modify their bits in place under sbi->alloc_mutex, but without holding the page lock. That leaves the read-modify-write of the bitmap bits unprotected against a concurrent writeback of the same page, which can read a partially-updated bitmap word or race with the dirty-bit update.
This patch adds taking lock_page()/unlock_page() around each page's kmap/modify/kunmap section.
--- fs/hfsplus/bitmap.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
fs/hfsplus/bitmap.c +13 -0
@@ -39,6 +39,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, start = size; goto out; } +lock_page (page); pptr =kmap_local_page (page); curr = pptr + (offset & (PAGE_CACHE_BITS -1 )) /32 ; i = offset %32 ;
@@ -75,6 +76,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, curr++; }kunmap_local (pptr); +unlock_page (page); offset +=PAGE_CACHE_BITS ;if (offset >= size)break ;
@@ -84,6 +86,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, start = size; goto out; } +lock_page (page); curr = pptr =kmap_local_page (page);if ((size ^ offset) /PAGE_CACHE_BITS ) end = pptr +PAGE_CACHE_BITS /32 ;
@@ -98,6 +101,9 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, start = offset + (curr - pptr) *32 + i;if (start >= size) {hfs_dbg ("bitmap full\n" ); +kunmap_local (pptr); +unlock_page (page); + start = size; goto out; }/* do any partial u32 at the start */
@@ -128,6 +134,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, }set_page_dirty (page);kunmap_local (pptr); +unlock_page (page); offset +=PAGE_CACHE_BITS ; page =read_mapping_page (mapping, offset /PAGE_CACHE_BITS ,NULL );
@@ -135,6 +142,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, start = size; goto out; } +lock_page (page); pptr =kmap_local_page (page); curr = pptr; end = pptr +PAGE_CACHE_BITS /32 ;
@@ -152,6 +160,7 @@ int hfsplus_block_allocate(struct super_block *sb, u32 size, *curr =cpu_to_be32 (n);set_page_dirty (page);kunmap_local (pptr); +unlock_page (page); *max = offset + (curr - pptr) *32 + i - start; sbi->free_blocks -= *max;hfsplus_mark_mdb_dirty (sb);
@@ -185,6 +194,7 @@ int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count) page =read_mapping_page (mapping, pnr,NULL );if (IS_ERR (page)) goto kaboom; +lock_page (page); pptr =kmap_local_page (page); curr = pptr + (offset & (PAGE_CACHE_BITS -1 )) /32 ; end = pptr +PAGE_CACHE_BITS /32 ;
@@ -216,9 +226,11 @@ int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count)break ;set_page_dirty (page);kunmap_local (pptr); +unlock_page (page); page =read_mapping_page (mapping, ++pnr,NULL );if (IS_ERR (page)) goto kaboom; +lock_page (page); pptr =kmap_local_page (page); curr = pptr; end = pptr +PAGE_CACHE_BITS /32 ;
@@ -232,6 +244,7 @@ int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count) out:set_page_dirty (page);kunmap_local (pptr); +unlock_page (page); sbi->free_blocks += len;hfsplus_mark_mdb_dirty (sb);mutex_unlock (&sbi->alloc_mutex);
Signature · 1 line
2.43.0
Viacheslav Dubeyko
maintainerPATCH 4/6 hfsplus: add iomap operations for regular file data
In reply to Viacheslav DubeykoThis patch switches regular file operations on
iomap based approach.
--- fs/hfsplus/Kconfig | 1 + fs/hfsplus/Makefile | 5 +- fs/hfsplus/hfsplus_fs.h | 16 +++ fs/hfsplus/iomap.c | 209 ++++++++++++++++++++++++++++++++++++++++ fs/hfsplus/iomap.h | 20 ++++ 5 files changed, 249 insertions(+), 2 deletions(-) create mode 100644 fs/hfsplus/iomap.c create mode 100644 fs/hfsplus/iomap.h
fs/hfsplus/Kconfig +1 -0
@@ -6,6 +6,7 @@ config HFSPLUS_FS 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 -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 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/hfsplus_fs.h +16 -0
@@ -175,6 +175,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;
fs/hfsplus/iomap.c new file +209 -0
209 lines not on this page · raw message
fs/hfsplus/iomap.h new file +20 -0
@@ -0,0 +1,20 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +/* +* linux/fs/hfsplus/iomap.h +* +* iomap callback declarations for the hfsplus filesystem +*/ + +#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 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.43.0
Viacheslav Dubeyko
maintainerPATCH 5/6 hfsplus: add iomap-based file_operations
In reply to Viacheslav DubeykoThis patch adds fs/hfsplus/file.c with the goal of moving the file related operations from fs/hfsplus/inode.c. The hfsplus_file_open() and hfsplus_file_release() have been moved without any modifications. The hfsplus_file_fsync() no longer takes inode_lock() itself, because taking inode_lock() here would deadlock against callers that invoke fsync while already holding it (swapon()).
The hfsplus_file_operations have been reworked for supporting iomap-based operations.
--- fs/hfsplus/Makefile | 6 +- fs/hfsplus/file.c | 309 ++++++++++++++++++++++++++++++++++++++++ fs/hfsplus/hfsplus_fs.h | 7 +- fs/hfsplus/inode.c | 122 ---------------- 4 files changed, 317 insertions(+), 127 deletions(-) create mode 100644 fs/hfsplus/file.c
fs/hfsplus/Makefile +3 -3
@@ -5,9 +5,9 @@ obj-$(CONFIG_HFSPLUS_FS) += hfsplus.o -hfsplus-objs := super.o options.o inode.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 \ +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/file.c new file +309 -0
309 lines not on this page · raw message
fs/hfsplus/hfsplus_fs.h +5 -2
@@ -480,12 +480,15 @@ int hfsplus_cat_write_inode(struct inode *inode);int hfsplus_getattr (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 (struct mnt_idmap *idmap,struct dentry *dentry,struct file_kattr *fa); +/* 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); +/* ioctl.c */ long hfsplus_ioctl (struct file *filp,unsigned int cmd,unsigned long arg);
fs/hfsplus/inode.c +0 -122
140 lines not on this page · raw message
Signature · 1 line
2.43.0
Viacheslav Dubeyko
maintainerPATCH 6/6 hfsplus: switch address_space_operations on iomap-based support
In reply to Viacheslav DubeykoThis patch switches the regular file operations on iomap-based ones. The hfsplus_aops is redefined as the iomap-based operations. As a result, hfsplus_direct_IO() has been completely removed as a user of blockdev_direct_IO(). Also, unnecessary LEGACY_DIRECT_IO dependency has been removed from Kconfig.
--- fs/hfsplus/Kconfig | 1 - fs/hfsplus/extents.c | 39 +++++++---- fs/hfsplus/hfsplus_fs.h | 1 + fs/hfsplus/inode.c | 142 +++++++++++++++++++--------------------- 4 files changed, 96 insertions(+), 87 deletions(-)
fs/hfsplus/Kconfig +0 -1
@@ -5,7 +5,6 @@ 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
fs/hfsplus/extents.c +26 -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,
@@ -605,20 +606,32 @@ 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, 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 ; }else if (inode->i_size == hip->phys_size)return ;
fs/hfsplus/hfsplus_fs.h +1 -0
@@ -461,6 +461,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 +69 -73
197 lines not on this page · raw message
Signature · 1 line
2.43.0
Re: [PATCH 1/6] hfs/hfsplus: exchange hardcoded number of extents on named constants
In reply to Viacheslav DubeykoQuoted 3 lines · Viacheslav Dubeyko
Replace the magic 8/3 fork extent-count literals with named constants (
HFS_FORK_EXTENT_COUNTandHFSPLUS_FORK_EXTENT_COUNT). No functional change.
A typedef for an array of struct still looks a bit odd, but removing the hardcoded constants is a good step:
Re: [PATCH 2/6] hfsplus: rework hfsplus_get_block() logic
In reply to Viacheslav DubeykoLooks good:
Re: [PATCH 3/6] hfsplus: take the bitmap page lock for allocate/free
In reply to Viacheslav DubeykoQuoted 10 lines · Viacheslav Dubeyko
The
hfsplus_block_allocate()andhfsplus_block_free()kmap the allocation bitmap's pages and modify their bits in place undersbi->alloc_mutex, but without holding the page lock. That leaves the read-modify-write of the bitmap bits unprotected against a concurrent writeback of the same page, which can read a partially-updated bitmap word or race with the dirty-bit update.This patch adds taking lock_page()/unlock_page() around each page's kmap/modify/kunmap section.
You are still unprotected against concurrent writeback, as writeback drops the page lock before writing the data out.
Re: [PATCH 4/6] hfsplus: add iomap operations for regular file data
In reply to Viacheslav DubeykoQuoted 2 lines · Viacheslav Dubeyko
This patch switches regular file operations on
iomap based approach.
Maybe spell out the benefits tradeoffs, odd bits here? This commit log is a bit short.
Also does this actually work without the following patches?
fs/hfsplus/iomap.c line 3
Quoted 3 lines
fs/hfsplus/iomap.c new file +3
-0+// SPDX-License-Identifier: GPL-2.0 +/* +* linux/fs/hfsplus/iomap.c
Please don't put the file name into the file itself. It is pointless and prone to get out of date.
Quoted 5 lines
fs/hfsplus/iomap.c new file +5
-0+static int hfsplus_file_write_dio_end_io (struct kiocb *iocb,ssize_t size, +int error,unsigned int flags) +{ +struct inode *inode =file_inode (iocb->ki_filp); +
fs/hfsplus/iomap.c line 199
Quoted 10 lines
fs/hfsplus/iomap.c new file +10
-0+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 ; +}
We've have a copy of this in various places, maye add it to a header? Or even into the core code based on a flag so that these file systems don't need an end_io method?
Re: [PATCH 5/6] hfsplus: add iomap-based file_operations
In reply to Viacheslav DubeykoQuoted 7 lines · Viacheslav Dubeyko
This patch adds
fs/hfsplus/file.cwith the goal of moving the file related operations fromfs/hfsplus/inode.c. Thehfsplus_file_open()andhfsplus_file_release()have been moved without any modifications. Thehfsplus_file_fsync()no longer takesinode_lock()itself, because takinginode_lock()here would deadlock against callers that invoke fsync while already holding it (swapon()).
Please split the pure move into a prep patch and then start modifying the coe in the new place.
Re: [PATCH 6/6] hfsplus: switch address_space_operations on iomap-based support
In reply to Viacheslav DubeykoQuoted 18 lines
fs/hfsplus/extents.c +14
-0@@ -605,20 +606,32 @@ 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) { +if (S_ISREG (inode->i_mode)) { + res =hfsplus_iomap_cont_expand (inode, 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);
fs/hfsplus/extents.c void hfsplus_file_truncate(struct inode *inode) · line 632
Quoted 10 lines
fs/hfsplus/extents.c +10
-0+if (res) +return ; + + res =generic_write_end (NULL , mapping, + inode->i_size,0 ,0 , + folio, fsdata); +if (res <0 ) +return ; + +mark_inode_dirty (inode);
Can't this use the iomap zeroing helpers?
fs/hfsplus/inode.c static int hfsplus_writepages(struct address_space *mapping, · line 143
Quoted 13 lines
fs/hfsplus/inode.c +3
-3-static int hfsplus_writepages (struct address_space *mapping, +static int hfsplus_legacy_writepages (struct address_space *mapping,struct writeback_control *wbc) {return mpage_writepages (mapping, wbc, hfsplus_get_block);@@ -196,8 +139,8 @@ static int hfsplus_writepages(struct address_space *mapping,const struct address_space_operations hfsplus_btree_aops = { .dirty_folio = block_dirty_folio, .invalidate_folio = block_invalidate_folio, - .read_folio = hfsplus_read_folio, - .writepages = hfsplus_writepages, + .read_folio = hfsplus_legacy_read_folio, + .writepages = hfsplus_legacy_writepages,
Would btree be a better name then legacy? So the story here is there is a btree inode, and you want to keep the buffer_head based path for this, at least for now?
Quoted 14 lines
fs/hfsplus/inode.c +6
-4@@ -290,10 +274,22 @@ static int hfsplus_setattr(struct mnt_idmap *idmap, attr->ia_size != i_size_read(inode)) {inode_dio_wait (inode);if (attr->ia_size > inode->i_size) { - error =generic_cont_expand_simple (inode, - attr->ia_size); -if (error) -return error; +if (S_ISREG (inode->i_mode)) { +loff_t old_size = inode->i_size; + +i_size_write (inode, attr->ia_size); + error =hfsplus_iomap_cont_expand (inode, + attr->ia_size);
fs/hfsplus/inode.c static int hfsplus_setattr(struct mnt_idmap *idmap, · line 292
Quoted 10 lines
fs/hfsplus/inode.c +10
-0+if (error) { +i_size_write (inode, old_size); +return error; + } + }else { + error =generic_cont_expand_simple (inode, + attr->ia_size); +if (error) +return error; + }
You're not suppoed to see ->setattr for ATTR_SIZE for anything but regular files from the VFS. I don't think there's any hfsplus specific path that could cause this either, could it?
Viacheslav Dubeyko
maintainerRe: [PATCH 3/6] hfsplus: take the bitmap page lock for allocate/free
In reply to Christoph HellwigQuoted 9 lines · Christoph Hellwig
Quoted 8 lines · Viacheslav Dubeyko
The
hfsplus_block_allocate()andhfsplus_block_free()kmap the allocation bitmap's pages and modify their bits in place undersbi->alloc_mutex, but without holding the page lock. That leaves the read-modify-write of the bitmap bits unprotected against a concurrent writeback of the same page, which can read a partially-updated bitmap word or race with the dirty-bit update.
Quoted 5 lines
Quoted 2 lines
This patch adds taking lock_page()/unlock_page() around each page's kmap/modify/kunmap section.
You are still unprotected against concurrent writeback, as writeback drops the page lock before writing the data out.
Yeah, you are right. As far as I can see, the sbi->alloc_mutex is the main mechanism of synchronization of access to Allocation File (block bitmap). And I see that hfsplus_sync_fs() doesn't use it during filemap_write_and_wait(sbi->alloc_file->i_mapping). Probably, it should be used there. Let me double check it. What do you think?
Thanks,
Slava.
Viacheslav Dubeyko
maintainerRe: [PATCH 4/6] hfsplus: add iomap operations for regular file data
In reply to Christoph HellwigQuoted 6 lines · Christoph Hellwig
Quoted 2 lines · Viacheslav Dubeyko
This patch switches regular file operations on
iomap based approach.Maybe spell out the benefits tradeoffs, odd bits here? This commit log is a bit short.
OK. Let me explain it in more details.
Quoted 2 lines
Also does this actually work without the following patches?
The following patches are needed too. :)
Quoted 7 lines
fs/hfsplus/iomap.c line 3
Quoted 4 lines
fs/hfsplus/iomap.c new file +3
-0+// SPDX-License-Identifier: GPL-2.0 +/* +* linux/fs/hfsplus/iomap.c Please don't put the file name into the file itself. It is pointless and prone to get out of date.
I am simply following the driver context. Other HFS+ files contain this. I can remove it, of course, if you like.
Quoted 12 lines
Quoted 12 lines
+static int hfsplus_file_write_dio_end_io(struct kiocb *iocb,
ssize_tsize,(quoted diff) +1
-0+ int error, unsigned intflags)
fs/hfsplus/iomap.c new file +7
-0+{ +struct inode *inode =file_inode (iocb->ki_filp); + +if (error) +return error; + +if (size &&i_size_read (inode) < iocb->ki_pos + size) {
Quoted 8 lines
fs/hfsplus/iomap.c line 199
Quoted 6 lines
fs/hfsplus/iomap.c new file +6
-0+i_size_write (inode, iocb->ki_pos + size); +mark_inode_dirty (inode); + } + +return 0 ; +}We've have a copy of this in various places, maye add it to a header?
I can do this.
Quoted 2 lines
Or even into the core code based on a flag so that these file systems don't need an
end_iomethod?
Let me double check this.
Thanks,
Slava.
Viacheslav Dubeyko
maintainerRe: [PATCH 5/6] hfsplus: add iomap-based file_operations
In reply to Christoph HellwigQuoted 11 lines · Christoph Hellwig
Quoted 7 lines · Viacheslav Dubeyko
This patch adds
fs/hfsplus/file.cwith the goal of moving the file related operations fromfs/hfsplus/inode.c. Thehfsplus_file_open()andhfsplus_file_release()have been moved without any modifications. Thehfsplus_file_fsync()no longer takesinode_lock()itself, because takinginode_lock()here would deadlock against callers that invoke fsync while already holding it (swapon()).Please split the pure move into a prep patch and then start modifying the coe in the new place.
Makes sense. Let me rework it.
Thanks,
Slava.
Viacheslav Dubeyko
maintainerRe: [PATCH 6/6] hfsplus: switch address_space_operations on iomap-based support
In reply to Christoph HellwigQuoted 29 lines · Christoph Hellwig
Quoted 29 lines
(quoted diff) +0
-0@@ -605,20 +606,32 @@ void hfsplus_file_truncate(struct inode *inode)
inode->i_ino, (long long)hip->phys_size, inode-Quoted 1 line
i_size);
if (inode->i_size>hip->phys_size) {(quoted diff) +2
-0+ if (S_ISREG(inode->i_mode)) { + res = hfsplus_iomap_cont_expand(inode,
inode->i_size);fs/hfsplus/extents.c +6
-0+if (res) +return ; + +mark_inode_dirty (inode); + }else { + struct address_space *mapping = inode-Quoted 1 line
i_mapping;fs/hfsplus/extents.c +5
-0+struct folio *folio; +void *fsdata =NULL ; + + res = hfsplus_write_begin(NULL, mapping, + inode->i_size,0,
(quoted diff) +1
-0+ &folio,&fsdata);
fs/hfsplus/extents.c +5
-0+if (res) +return ; + + res = generic_write_end(NULL, mapping, + inode->i_size, 0,
Quoted 8 lines
fs/hfsplus/extents.c void hfsplus_file_truncate(struct inode *inode) · line 632
Quoted 6 lines
0,
fs/hfsplus/extents.c +5
-0+ folio, fsdata); +if (res <0 ) +return ; + +mark_inode_dirty (inode);Can't this use the iomap zeroing helpers?
Sounds like a good idea. Let me check it.
Quoted 12 lines
Quoted 12 lines
(quoted diff) +1
-1-static int hfsplus_writepages(struct address_space *mapping, +static int hfsplus_legacy_writepages(struct address_space*mapping,
structwriteback_control*wbc) {
returnmpage_writepages(mapping, wbc, hfsplus_get_block);fs/hfsplus/inode.c +0
-0@@ -196,8 +139,8 @@ static int hfsplus_writepages(struct address_space *mapping, const struct address_space_operations hfsplus_btree_aops = { .dirty_folio = block_dirty_folio, .invalidate_folio = block_invalidate_folio,
Quoted 8 lines
fs/hfsplus/inode.c static int hfsplus_writepages(struct address_space *mapping, · line 143
Quoted 4 lines
fs/hfsplus/inode.c +2
-2- .read_folio = hfsplus_read_folio, - .writepages = hfsplus_writepages, + .read_folio = hfsplus_legacy_read_folio, + .writepages = hfsplus_legacy_writepages,Would btree be a better name then legacy? So the story here is there is a btree inode, and you want to keep the
buffer_headbased path for this, at least for now?
Yeah, makes sense. I have the same feeling here. :)
Quoted 30 lines
Quoted 30 lines
fs/hfsplus/inode.c +5
-4@@ -290,10 +274,22 @@ static int hfsplus_setattr(struct mnt_idmap *idmap, attr->ia_size != i_size_read(inode)) { inode_dio_wait(inode); if (attr->ia_size > inode->i_size) { - error =generic_cont_expand_simple (inode, - attr- >ia_size); -if (error) -return error; +if (S_ISREG (inode->i_mode)) { +loff_t old_size = inode->i_size; + +i_size_write (inode, attr- >ia_size); + error =hfsplus_iomap_cont_expand(inode,
(quoted diff) +1
-0+
attr->ia_size);(quoted diff) +2
-0+ if (error) { + i_size_write(inode,
old_size);fs/hfsplus/inode.c +4
-0+return error; + } + }else { + error =generic_cont_expand_simple(inode,
(quoted diff) +1
-0+
attr->ia_size);
Quoted 8 lines
fs/hfsplus/inode.c static int hfsplus_setattr(struct mnt_idmap *idmap, · line 292
Quoted 3 lines
fs/hfsplus/inode.c +3
-0+if (error) +return error; + }You're not suppoed to see ->setattr for
ATTR_SIZEfor anything but regular files from the VFS. I don't think there's any hfsplus specific
path that could cause this either, could it?
I think you are right. Let me re-check the code here.
Thanks,
Slava.
Re: [PATCH 3/6] hfsplus: take the bitmap page lock for allocate/free
In reply to Viacheslav DubeykoQuoted 9 lines · Viacheslav Dubeyko
Quoted 2 lines
You are still unprotected against concurrent writeback, as writeback drops the page lock before writing the data out.
Yeah, you are right. As far as I can see, the
sbi->alloc_mutexis the main mechanism of synchronization of access to Allocation File (block bitmap). And I see thathfsplus_sync_fs()doesn't use it duringfilemap_write_and_wait(sbi->alloc_file->i_mapping). Probably, it should be used there. Let me double check it. What do you think?
I don't really know what scheme hfsplus uses for synchronization here, set mapping_set_stable_writes on the btree inode mapping and use folio_wait_stable after locking the folio.
Re: [PATCH 4/6] hfsplus: add iomap operations for regular file data
In reply to Viacheslav DubeykoQuoted 9 lines · Viacheslav Dubeyko
Quoted 6 lines
fs/hfsplus/iomap.c line 3
Quoted 3 lines
fs/hfsplus/iomap.c new file +3
-0+// SPDX-License-Identifier: GPL-2.0 +/* +* linux/fs/hfsplus/iomap.c Please don't put the file name into the file itself. It is pointless and prone to get out of date.
I am simply following the driver context. Other HFS+ files contain this. I can remove it, of course, if you like.
Yes, don't add it to new files, and remove it from existing ones if you have to touch them anyway.
Viacheslav Dubeyko
maintainerRe: [PATCH 4/6] hfsplus: add iomap operations for regular file data
In reply to Christoph HellwigHi Christoph,
Quoted 14 lines · Christoph Hellwig
Quoted 14 lines
+static int hfsplus_file_write_dio_end_io(struct kiocb *iocb,
ssize_tsize,(quoted diff) +1
-0+ int error, unsigned intflags)
fs/hfsplus/iomap.c new file +9
-0+{ +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);
Quoted 8 lines
fs/hfsplus/iomap.c line 73
Quoted 4 lines
fs/hfsplus/iomap.c new file +4
-0+ } + +return 0 ; +}We've have a copy of this in various places, maye add it to a header? Or even into the core code based on a flag so that these file systems don't need an
end_iomethod?
I think I need more clarification here. :) By "various places" do you mean HFS+ code or Linux kernel as whole?
I assume that you mean this code block:
+ if (size && i_size_read(inode) < iocb->ki_pos + size) {
+ i_size_write(inode, iocb->ki_pos + size);
+ mark_inode_dirty(inode);
+ }
Do you suggest to introduce this logic as a special method that can be used in other file systems too? Am I right?
Thanks,
Slava.
Re: [PATCH 4/6] hfsplus: add iomap operations for regular file data
Quoted 38 lines · Viacheslav Dubeyko
Hi Christoph,
Quoted 14 lines · Christoph Hellwig
Quoted 14 lines
+static int hfsplus_file_write_dio_end_io(struct kiocb *iocb,
ssize_tsize,(quoted diff) +1
-0+ int error, unsigned intflags)
fs/hfsplus/iomap.c new file +9
-0+{ +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);Quoted 8 lines
fs/hfsplus/iomap.c line 73
Quoted 4 lines
fs/hfsplus/iomap.c new file +4
-0+ } + +return 0 ; +}We've have a copy of this in various places, maye add it to a header? Or even into the core code based on a flag so that these file systems don't need an
end_iomethod?I think I need more clarification here. :) By "various places" do you mean HFS+ code or Linux kernel as whole?
I assume that you mean this code block:
fs/hfsplus/iomap.c new file +4
-0+if (size &&i_size_read (inode) < iocb->ki_pos + size) { +i_size_write (inode, iocb->ki_pos + size); +mark_inode_dirty (inode); + }
Quoted 2 lines
Do you suggest to introduce this logic as a special method that can be used in other file systems too? Am I right?
That was the original idea. But I think what is even better is to do it when no iomap_dio_ops.end_io method is defined. I think I did an audit that there is no such current case for writes, but we should double check it.