fs: don't set SB_RDONLY after filesystem errors
Subsystems (27)
Christian Brauner
maintainerv3 cover fs: don't set SB_RDONLY after filesystem errors
A lot of filesystems handle errors by setting SB_RDONLY from whatever context found the error and without holding s_umount. SB_RDONLY is supposed to only change under s_umount through a proper remount. So that races with everyone else writing sb->s_flags and it breaks
freezing: freeze_super() and thaw_super() decide from sb_rdonly() whether the s_writers rwsems need to be taken or released. An error while frozen makes thaw_super() leave them write-locked, every writer after that hangs in sb_start_write() and umount warns. jfs even oopses.
Karl proposed teaching thaw_super() to remember what the freeze took (cf. [1]). Jan preferred fixing the filesystems like ext4 and f2fs already did (cf. [2]) and I agree. v1 did that and every filesystem grew the same checks in ->open, ->setattr, ->fsync and friends, so Jan suggested a generic flag in the superblock instead (cf. [3]):
- sb->s_state gets SB_STATE_EMERGENCY_RO. It's only ever set and never cleared and SB_RDONLY stays untouched so freezing keeps working.
- a filesystem calls sb_set_emergency_ro() where it used to set SB_RDONLY
- the VFS refuses modifications with EROFS wherever sb_rdonly() does, also for files opened before the error, refuses the remount to read-write and a new freeze, and doesn't sync the superblock anymore:
syncfs(2) and Q_SYNC fail with EROFS, sync(2) and umount skip it (Jan)
- /proc/mounts, statmount() and statfs() show it read-only and a remount to read-only works, so sysrq u doesn't WARN anymore, and doesn't mark the filesystem clean
Writeback isn't stopped. The flusher and the block device flushes in sync(2) and kill_block_super() keep writing, so what ends up on disk is what it is on mainline today and every filesystem decides what its writeback still writes. The same goes for what doesn't pass through the
VFS: allocators, journals, the deletion of unlinked inodes and the xattr and ACL setters that ecryptfs calls without write access on the lower mount. That's about a third of the checks v1 had.
The stable fixes stay self-contained, a follow-up patch converts each of exfat, hfs, ntfs, zonefs and ext4 to the generic state. fat, exfat, hpfs and ntfs still set SB_RDONLY for errors during mount, nothing can freeze before SB_BORN. A few patches make the read-only remount work in the first place: exfat and ocfs2 didn't inherit their mount options on remount, gfs2 BUG()s when remounted read-only during a withdraw, and ext4 in its emergency state and a shut down xfs refused to go read-only at all, so sysrq u WARNed for them. ecryptfs gets a read-only fallback for its lower files, otherwise every lookup on top of an errored filesystem fails with EROFS and leaks the inode.
The first two patches are jfs fixes for mainline: umount after an error BUG()s because nothing commits the anonymous tlocks of allocations writeback didn't get to, and slow symlinks read from disk didn't get jfs_setattr().
There are user-visible changes but I think they're all fine. "mount -o remount,rw" doesn't make the filesystem writable again after an error, like ext4 since commit d3476f3dad4a ("ext4: don't set SB_RDONLY after filesystem errors"). Writes through file descriptors opened before the error, fsync(), syncfs() and Q_SYNC fail with EROFS and stores through shared mappings get SIGBUS. FIFREEZE and dm suspend with lockfs fail with EROFS like they already did for ext4, and the shutdown ioctls of ext4, exfat, ntfs and btrfs skip the freeze after an error. A second read-only mount of the device fails with EBUSY. And ext4 shows "ro" in /proc/mounts and reports ST_RDONLY again after an error.
This has dependencies on vfs-7.5.super (cf. [4]). With this no filesystem changes SB_RDONLY outside of mount and remount anymore.
[1]: https://lore.kernel.org/r/[email protected]
[2]: https://lore.kernel.org/r/hyhuckyohsabx2suvq3z7uy3pbgmihva6wgzxr3op7pzcrlu33@p7zzlgp2ehjf
[3]: https://lore.kernel.org/r/my2k7257m265fp5ad77n5hfndl23i56gsog3jwnsyocp3yxbnj@aphfqdiej2hs
[4]: https://lore.kernel.org/r/[email protected]
---
Changes in v3:
- New first patch: jfs frees the anonymous tlocks of an evicted inode
instead of BUG()ing on umount after an error. That's a mainline bug,
Cc stable.
- sync_filesystem() and sync(2) skip a superblock in the emergency
state, syncfs(2) and Q_SYNC fail with EROFS. That's the VFS patch from
the v1 thread after all, the jfs fix takes care of the BUG (Jan).
- freeze_super() refuses a new freeze in the emergency state with EROFS,
the exfat, ntfs, btrfs and ext4 shutdown ioctls skip the freeze after
an error.
- ext2, fat, affs, ufs and hfs don't refuse ->write_inode() anymore. The flusher wrote data and bitmaps but no inodes and new files were lost (sashiko).
- The ->sync_fs() checks of ext2, jfs, nilfs2, ocfs2 and ubifs are gone,
the VFS doesn't call them in that state.
- ext2: ext2_freeze() doesn't check the emergency state anymore (Jan).
- jfs: refuse a resize after an error, keep FM_DIRTY with nointegrity
when the error hit while frozen (sashiko).
- ntfs: ntfs_drop_big_inode() leaves unlinked inodes to chkdsk in the
emergency state like ntfs_evict_big_inode() (sashiko).
- btrfs_force_shutdown() sets BTRFS_FS_STATE_RO too (Qu).
- ubifs: always use the emergency state, don't sync the write-buffers
or the master node on a remount to read-only after an error, drop
dirty folios and inodes after an error (Zhihao, sashiko).
- ecryptfs doesn't try to write the header through a read-only lower
file when opening an empty file (sashiko).
- Dropped the selftests, fstests is the place for them (Christoph).
- The jfs symlink inode operations fix is a patch of its own.
- sb_rdonly_any() for the checks that need the read-only mount and the emergency state instead of open-coding the pair.
- Picked up the review tags. The ones for hfs, ntfs, ocfs2, nilfs2
and the ext4 conversion are kept although those patches changed a
bit (a ->sync_fs() or ->write_inode() hunk less, the shutdown ioctls
skip the freeze). Jan's tag on the VFS patch isn't, it changed too
much.
- Link to v2: https://patch.msgid.link/[email protected]
Changes in v2:
- Generic SB_STATE_EMERGENCY_RO in the superblock, the VFS refuses modifications. The checks at the VFS entry points of every filesystem
are gone (Jan).
- The VFS patch from the v1 thread also hooked write_inode() and sync_filesystem(). Dropped, jfs still has to commit queued allocations
after the error.
- /proc/mounts shows "ro" and statfs() reports ST_RDONLY again in the
emergency state.
- The stable-tagged fixes stay self-contained, follow-up patches convert
exfat, hfs, ntfs, zonefs and ext4 to the generic state.
- hfsplus checks the b-tree map records in hfsplus_fill_super() (Slava).
- Selftests expect ST_RDONLY and EROFS from fsync() on jfs too.
- ecryptfs refuses writes through a read-only lower file with EROFS. truncate(2) and creating a file ran into the WARN_ON_ONCE() in __kernel_write_iter() (sashiko).
- gfs2: bail out of the freeze loop when the filesystem withdrew in the meantime, keep the EIO from gfs2_make_fs_rw() for a remount to
read-write and don't show "withdrawn" in the mount options (Andreas).
- Don't show "emergency_ro" and "ro_ops" in the mount options. Remount
helpers that pass the shown options back trip over them. ext4 keeps
showing "emergency_ro" like it does now (Andreas).
- Rebased onto current mainline. ntfs persists its error state itself
since commit 521978c4f444, the error work uses that now, and the new
named stream paths refuse modifications in the emergency state too.
- statmount() reports the emergency state as read-only too and remap_verify_area() refuses FICLONE and friends in that state.
- nilfs2 refuses going read-write in the emergency state itself because nilfs_get_tree() reconfigures the superblock without
reconfigure_super(). zonefs keeps its check in ->write_iter() because
the loop driver and others call it without rw_verify_area().
- ntfs runs a pending error work before umount persists the error
state, hfsplus says which b-tree header node it couldn't load again.
- The reproducers moved out of the commit messages into git notes.
- Link to v1: https://patch.msgid.link/[email protected]
---
Christian Brauner (33):
jfs: free uncommitted anonymous tlocks when evicting an inode
jfs: use jfs_symlink_inode_operations in jfs_iget()
ecryptfs: don't leak the inode when a lookup can't open the lower file
ecryptfs: fall back to a read-only lower file
fs: add a generic emergency read-only state
ext2: don't set SB_RDONLY after filesystem errors
jfs: don't set SB_RDONLY after filesystem errors
nilfs2: don't set SB_RDONLY after filesystem errors
fat: don't set SB_RDONLY after filesystem errors
exfat: keep the current mount options on remount
exfat: don't set SB_RDONLY after filesystem errors
exfat: use the generic emergency read-only state
hfs: don't set SB_RDONLY after MDB write errors
hfs: use the generic emergency read-only state
hfsplus: don't set SB_RDONLY when creating the attributes tree
affs: don't set SB_RDONLY after filesystem errors
ufs: don't set SB_RDONLY after filesystem errors
hpfs: don't set SB_RDONLY after filesystem errors
ntfs: don't set SB_RDONLY after filesystem errors
ntfs: use the generic emergency read-only state
ocfs2: keep the current mount options on remount
ocfs2: don't set SB_RDONLY after filesystem errors
zonefs: don't set SB_RDONLY after I/O errors
zonefs: use the generic emergency read-only state
btrfs: check BTRFS_FS_STATE_RO instead of SB_RDONLY at runtime
btrfs: don't set SB_RDONLY after filesystem errors
gfs2: wait for a pending withdraw before remounting read-only
gfs2: don't set SB_RDONLY on withdraw
efivarfs: stop using SB_RDONLY for read-only efivar ops
ubifs: don't set SB_RDONLY after errors
ext4: allow remounting read-only in the emergency state
ext4: use the generic emergency read-only state
xfs: allow remounting read-only after a shutdown
Documentation/filesystems/porting.rst | 20 +++++++++++ Documentation/filesystems/zonefs.rst | 4 +-- fs/affs/amigaffs.c | 4 +-- fs/affs/super.c | 14 +++++--- fs/attr.c | 4 +++ fs/btrfs/acl.c | 3 +- fs/btrfs/bio.c | 2 +- fs/btrfs/block-group.c | 2 +- fs/btrfs/dev-replace.c | 2 +- fs/btrfs/discard.c | 2 +- fs/btrfs/disk-io.c | 4 +-- fs/btrfs/fs.h | 20 +++++++---- fs/btrfs/inode.c | 8 ++--- fs/btrfs/ioctl.c | 13 ++++--- fs/btrfs/messages.c | 16 +++++---- fs/btrfs/qgroup.c | 4 +-- fs/btrfs/reflink.c | 3 +- fs/btrfs/super.c | 35 +++++++++++++++--- fs/btrfs/sysfs.c | 4 +-- fs/btrfs/tree-checker.c | 2 +- fs/btrfs/volumes.c | 4 +-- fs/btrfs/xattr.c | 8 +++-- fs/ecryptfs/file.c | 4 ++- fs/ecryptfs/inode.c | 13 ++++++- fs/ecryptfs/kthread.c | 15 ++++++-- fs/ecryptfs/read_write.c | 3 ++ fs/efivarfs/file.c | 6 ++++ fs/efivarfs/inode.c | 15 ++++++++ fs/efivarfs/internal.h | 14 +++++++- fs/efivarfs/super.c | 26 +++++++++----- fs/exfat/exfat_fs.h | 10 ++++++ fs/exfat/file.c | 54 ++++++++++++++++++++-------- fs/exfat/inode.c | 11 +++--- fs/exfat/misc.c | 16 ++++++--- fs/exfat/namei.c | 27 ++++++++------ fs/exfat/super.c | 41 +++++++++++++-------- fs/ext2/balloc.c | 5 +++ fs/ext2/ialloc.c | 2 ++ fs/ext2/inode.c | 4 ++- fs/ext2/super.c | 13 ++++--- fs/ext2/xattr.c | 2 ++ fs/ext4/ioctl.c | 4 +++ fs/ext4/super.c | 19 ++++++---- fs/fat/dir.c | 3 ++ fs/fat/fatent.c | 11 +++++- fs/fat/file.c | 4 +++ fs/fat/inode.c | 13 +++++-- fs/fat/misc.c | 19 ++++++---- fs/gfs2/quota.c | 2 +- fs/gfs2/super.c | 40 +++++++++++++++++---- fs/gfs2/util.c | 7 ++-- fs/hfs/attr.c | 3 ++ fs/hfs/extent.c | 5 +++ fs/hfs/inode.c | 7 ++++ fs/hfs/mdb.c | 16 +++++---- fs/hfs/super.c | 2 +- fs/hfsplus/btree.c | 42 ++++++++++++++-------- fs/hfsplus/hfsplus_fs.h | 1 + fs/hfsplus/super.c | 5 +++ fs/hpfs/alloc.c | 4 +-- fs/hpfs/dir.c | 2 +- fs/hpfs/map.c | 3 +- fs/hpfs/super.c | 11 ++++-- fs/jfs/acl.c | 3 ++ fs/jfs/inode.c | 15 +++++++- fs/jfs/ioctl.c | 4 +++ fs/jfs/jfs_dtree.c | 3 +- fs/jfs/jfs_extent.c | 10 ++++++ fs/jfs/jfs_incore.h | 6 ++++ fs/jfs/jfs_inode.c | 3 ++ fs/jfs/jfs_txnmgr.c | 68 +++++++++++++++++++++++++++++++++++ fs/jfs/jfs_txnmgr.h | 1 + fs/jfs/super.c | 36 ++++++++++++++++--- fs/jfs/xattr.c | 3 ++ fs/namei.c | 3 +- fs/namespace.c | 5 ++- fs/nilfs2/inode.c | 4 +-- fs/nilfs2/ioctl.c | 4 +++ fs/nilfs2/mdt.c | 2 +- fs/nilfs2/page.c | 2 +- fs/nilfs2/segment.c | 13 ++++--- fs/nilfs2/sufile.h | 2 +- fs/nilfs2/super.c | 25 +++++++++---- fs/ntfs/aops.c | 5 +-- fs/ntfs/dir.c | 5 +-- fs/ntfs/ea.c | 10 ++++-- fs/ntfs/file.c | 63 +++++++++++++++++++++++--------- fs/ntfs/inode.c | 18 +++++++--- fs/ntfs/iomap.c | 6 ++-- fs/ntfs/mft.c | 10 ++++-- fs/ntfs/named_stream.c | 43 +++++++++++----------- fs/ntfs/namei.c | 40 ++++++++++++--------- fs/ntfs/super.c | 56 +++++++++++++++++++++++++---- fs/ntfs/volume.h | 13 +++++++ fs/ocfs2/acl.c | 3 ++ fs/ocfs2/super.c | 27 +++++++++++--- fs/ocfs2/xattr.c | 8 +++++ fs/open.c | 4 +++ fs/proc_namespace.c | 2 +- fs/quota/dquot.c | 27 ++++++++++---- fs/quota/quota.c | 13 +++++++ fs/read_write.c | 5 +++ fs/remap_range.c | 4 +++ fs/statfs.c | 9 +++-- fs/super.c | 17 +++++++++ fs/sync.c | 14 ++++++-- fs/ubifs/file.c | 12 +++++++ fs/ubifs/io.c | 6 +++- fs/ubifs/super.c | 40 +++++++++++++-------- fs/ubifs/xattr.c | 6 ++++ fs/ufs/cylinder.c | 12 ++++--- fs/ufs/super.c | 56 ++++++++++++++++++++--------- fs/xfs/xfs_super.c | 13 +++++-- fs/zonefs/file.c | 13 ++++--- fs/zonefs/super.c | 25 ++++++++----- fs/zonefs/zonefs.h | 1 + include/linux/fs/super.h | 23 ++++++++++++ include/linux/fs/super_types.h | 19 ++++++++++ mm/memory.c | 6 ++++ 119 files changed, 1185 insertions(+), 343 deletions(-)
---
base-commit: c339f4010dea8b0d754f84ace70cd1b089878ef8
change-id: 20261011-work-fs-sb_rdonly-f93e626bd0c2
Christian Brauner
maintainerPATCH v3 1/33 jfs: free uncommitted anonymous tlocks when evicting an inode
jfs doesn't log block allocations made through ->write_begin() and ->writepages() right away. extAlloc() records them in anonymous transaction locks that hang off the inode and puts the inode on the global anonymous list. They only get committed when a later transaction includes the inode, usually writeback calling ->write_inode() which ends up in jfs_commit_inode(). jfs_evict_inode() relies on that and BUG()s when the inode is still on the anonymous list.
But nothing guarantees that writeback ran before eviction. With errors=remount-ro, the default, an error sets SB_RDONLY and sync_filesystem() skips read-only superblocks. So unmounting the filesystem after an error evicts every inode that allocated blocks since it was last written back without committing anything and the BUG_ON() fires. Writing a file and hitting an error before the flusher got to the inode is enough:
kernel BUG at fs/jfs/inode.c:175!
Oops: invalid opcode: 0000 [#1] SMP KASAN NOPTI RIP: 0010:jfs_evict_inode+0x33c/0x440 Call Trace: <TASK> evict+0x336/0x8b0 evict_inodes+0x421/0x850 generic_shutdown_super+0xaf/0x530 kill_block_super+0x36/0xa0 deactivate_locked_super+0x97/0xf0 cleanup_mnt+0x9a/0x130 task_work_run+0x135/0x230 exit_to_user_mode_loop+0x171/0x560 do_syscall_64+0x410/0x5e0 entry_SYSCALL_64_after_hwframe+0x77/0x7f </TASK>
Just taking the inode off the anonymous list isn't enough. The tlocks come from TxLock which is shared by all jfs mounts, txLock() pointed mp->lid at them and pinned the metapages with metapage_nohomeok(). Leak them and every other jfs filesystem eventually blocks in txBeginAnon() waiting for tlocks that never come back.
So free them the way txUnlock() frees the tlocks of a committed
transaction: drop the metapage_nohomeok() pin, reset mp->lid and free the linelocks and the tlock itself. txAbort() isn't a good model here as it only drops the pin for pages that are on the logsync list and leaves the linelocks alone. A metapage that carries nothing but the uncommitted change isn't written home. The superblock is marked dirty after an error so fsck has to look at the filesystem anyway.
Note that jfs_sync() takes an inode it can't grab off the anonymous list without freeing anything so check jfs_ip->atlhead instead of the list.
--- fs/jfs/inode.c | 9 +++++++ fs/jfs/jfs_txnmgr.c | 68 +++++++++++++++++++++++++++++++++++++++++++++++++++++ fs/jfs/jfs_txnmgr.h | 1 + 3 files changed, 78 insertions(+)
fs/jfs/inode.c +9 -0
@@ -172,6 +172,15 @@ void jfs_evict_inode(struct inode *inode)clear_inode (inode);dquot_drop (inode); +/* +* Writeback commits the anonymous transactions of allocations. If the +* filesystem went read-only without a sync, e.g. after an error, +* nothing did. +*/ +if (ji->atlhead) { +WARN_ON_ONCE (!sb_rdonly (inode->i_sb)); +txAbortAnon (inode); + }BUG_ON (!list_empty (&ji->anon_inode_list));spin_lock_irq (&ji->ag_lock);
fs/jfs/jfs_txnmgr.c +68 -0
74 lines not on this page · raw message
fs/jfs/jfs_txnmgr.h +1 -0
@@ -288,6 +288,7 @@ extern struct linelock *txLinelock(struct linelock *);extern void txFreeMap (struct inode *,struct maplock *,struct tblock *,int );extern void txEA (tid_t ,struct inode *,dxd_t *,dxd_t *);extern void txFreelock (struct inode *); +extern void txAbortAnon (struct inode *);extern int lmLog (struct jfs_log *,struct tblock *,struct lrd *,struct tlock *);extern void txQuiesce (struct super_block *);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 01/33] jfs: free uncommitted anonymous tlocks when evicting an inode
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] jfs: false WARN_ON_ONCE() in jfs_evict_inode() on read-write mounts - [Medium] jfs: txAbortAnon()…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] jfs: false WARN_ON_ONCE() in jfs_evict_inode() on read-write mounts
- [Medium] jfs: txAbortAnon() after jfs_free_zero_link() leaks metapages and tlocks
- [Medium] jfs: race between jfs_evict_inode(), txAbortAnon(), and txQuiesce()
--
commit a15655dff32c48b09c331d3e03e198e2173008c1
Author: Christian Brauner <[email protected]>
jfs: free uncommitted anonymous tlocks when evicting an inode
This commit adds txAbortAnon() and calls it from jfs_evict_inode() to release uncommitted anonymous transaction locks and metapage pins when an inode is evicted before writeback commits them.
Quoted 5 lines
fs/jfs/inode.c +0
-0
fs/jfs/inode.c void jfs_evict_inode(struct inode *inode) · line 181
Quoted 10 lines
fs/jfs/inode.c +7
-0@@ -172,6 +172,15 @@ void jfs_evict_inode(struct inode *inode)clear_inode (inode);dquot_drop (inode); +/* +* Writeback commits the anonymous transactions of allocations. If the +* filesystem went read-only without a sync, e.g. after an error, +* nothing did. +*/ +if (ji->atlhead) { +WARN_ON_ONCE (!sb_rdonly (inode->i_sb));
[Severity: Medium]
Can WARN_ON_ONCE(!sb_rdonly(inode->i_sb)) fire on a read-write mount when ji->atlhead is left populated without setting COMMIT_Dirty?
In extAlloc(), xtExtend() and xtInsert() acquire anonymous tlocks on non-root xtree metapages via txLock() before splitting pages in xtSplitUp(). When xtSplitUp() fails with -ENOSPC or -EDQUOT, extAlloc() frees the data blocks and returns before calling mark_inode_dirty(ip):
fs/jfs/jfs_extent.c:extAlloc() {
...
if (xaddr && xaddr == nxaddr)
rc = xtExtend(0, ip, xoff, (int) nxlen, 0);
else
rc = xtInsert(0, ip, xflag, xoff, (int) nxlen, &nxaddr, 0);
/* if the extend or insert failed,
* free the newly allocated blocks and return the error.
*/
if (rc) {
dbFree(ip, nxaddr, nxlen);
dquot_free_block(ip, nxlen);
mutex_unlock(&JFS_IP(ip)->commit_mutex);
return (rc);
}
...
mark_inode_dirty(ip);
}Because COMMIT_Dirty is never set on the inode, jfs_write_inode() does not commit the anonymous tlocks, and evicting the inode triggers the warning on a read-write filesystem. The warning also triggers after a filesystem error when mounted with errors=continue, where jfs_handle_error() marks the superblock dirty without setting SB_RDONLY, or via extRecord() -> xtUpdate(), which is fixed in a later patch in this series (commit "jfs:
don't set SB_RDONLY after filesystem errors").
fs/jfs/inode.c void jfs_evict_inode(struct inode *inode) · line 183
Quoted 2 lines
fs/jfs/inode.c +2
-0+txAbortAnon (inode); + }
[Severity: Medium]
Does calling txAbortAnon() after jfs_free_zero_link() leak metapages and linelocks when an unlinked inode with COMMIT_Freewmap is evicted after an error?
Earlier in jfs_evict_inode(), an unlinked inode with COMMIT_Freewmap calls jfs_free_zero_link() -> xtTruncate(..., COMMIT_WMAP). For each non-root xtree metapage, xtTruncate() marks mp->lid with tlckFREELOCK, calls discard_metapage(mp), and finishes with txFreelock(ip):
fs/jfs/jfs_txnmgr.c:txFreelock() {
...
while ((lid = xtlck->next) != 0) {
tlck = lid_to_tlock(lid);
if (tlck->flag & tlckFREELOCK) {
xtlck->next = tlck->next;
txLockFree(lid);
} else {
...
}If the transaction in jfs_unlink() or jfs_rename() aborted after commitZeroLink() set COMMIT_Freewmap, ji->atlhead still holds anonymous tlckPAGELOCK tlocks when jfs_evict_inode() runs. txFreelock() unlinks those tlocks from ji->atlhead and frees lid before txAbortAnon() runs, without dropping mp->nohomeok, clearing mp->lid, or freeing chained linelocks. Calling txAbortAnon() before jfs_free_zero_link() would release the metapage pins and linelocks before xtTruncate() discards the pages.
Quoted 25 lines
fs/jfs/jfs_txnmgr.c +17
-0@@ -2581,6 +2581,74 @@ void txFreelock(struct inode *ip)TXN_UNLOCK (); } +/* +* txAbortAnon() +* +* function: free the anonymous tlocks of an inode that is evicted +* before writeback committed them, e.g. because an error made +* the filesystem read-only. +* +* A page that carries nothing but uncommitted changes isn't written home. +* A page that is also waiting to go home with committed changes (mp->lsn) +* goes home with both, after an error the superblock is marked dirty. +*/ +void txAbortAnon (struct inode *ip) +{ +struct jfs_inode_info *jfs_ip =JFS_IP (ip); +struct linelock *linelock; +struct metapage *mp; +struct tlock *tlck;
fs/jfs/jfs_txnmgr.c void txFreelock(struct inode *ip) · line 2610
Quoted 10 lines
fs/jfs/jfs_txnmgr.c +10
-0+lid_t lid, next, llid, k; + +/* txQuiesce() and jfs_sync() commit under the commit_mutex */ +mutex_lock (&jfs_ip->commit_mutex); + +TXN_LOCK (); + lid = jfs_ip->atlhead; + jfs_ip->atlhead = jfs_ip->atltail =0 ; +list_del_init (&jfs_ip->anon_inode_list); +TXN_UNLOCK ();
[Severity: Medium]
Can jfs_evict_inode() and txAbortAnon() race with txQuiesce() walking the global TxAnchor.anon_list?
txQuiesce() walks TxAnchor.anon_list (shared across all JFS mounts) under TXN_LOCK() and drops TXN_LOCK() without pinning the inode before taking commit_mutex:
CPU0 CPU1txQuiesce() jfs_evict_inode()
TXN_LOCK() txAbortAnon()
jfs_ip = list_entry(...) mutex_lock(&jfs_ip->commit_mutex)
TXN_UNLOCK() TXN_LOCK()
list_del_init(&jfs_ip->anon_inode_list)
TXN_UNLOCK()
...
mutex_unlock(&jfs_ip->commit_mutex)
...
destroy_inode(ip) txBegin(ip->i_sb, ...) mutex_lock(&jfs_ip->commit_mutex) <-- use-after-free
In the opposite interleaving, txCommit() called from txQuiesce() clears jfs_ip->atlhead = 0 before taking TXN_LOCK() to remove jfs_ip->anon_inode_list. Because jfs_evict_inode() checks if (ji->atlhead) without commit_mutex or TXN_LOCK(), it can see ji->atlhead == 0, skip txAbortAnon(), and hit BUG_ON(!list_empty(&ji->anon_inode_list)) or free the inode while txCommit() is still running.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
Christian Brauner
maintainerPATCH v3 2/33 jfs: use jfs_symlink_inode_operations in jfs_iget()
In reply to Christian Braunerjfs_symlink() gives slow symlinks jfs_symlink_inode_operations but jfs_iget() gives them page_symlink_inode_operations. So once the inode has been evicted a chown doesn't go through jfs_setattr() anymore and skips dquot_transfer(), and listxattr(2) doesn't see the symlink's xattrs. Use jfs_symlink_inode_operations in jfs_iget() as well like it already does for fast symlinks.
--- fs/jfs/inode.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
fs/jfs/inode.c +1 -1
@@ -47,7 +47,7 @@ struct inode *jfs_iget(struct super_block *sb, unsigned long ino) inode->i_fop = &jfs_dir_operations; }else if (S_ISLNK (inode->i_mode)) {if (inode->i_size >=IDATASIZE ) { - inode->i_op = &page_symlink_inode_operations; + inode->i_op = &jfs_symlink_inode_operations;inode_nohighmem (inode); inode->i_mapping->a_ops = &jfs_aops; }else {
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 02/33] jfs: use jfs_symlink_inode_operations in jfs_iget()
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
jfs_symlink()gives slow symlinksjfs_symlink_inode_operationsbutjfs_iget()gives thempage_symlink_inode_operations. So once the inode has been evicted a chown doesn't go throughjfs_setattr()anymore and skipsdquot_transfer(), and listxattr(2) doesn't see the symlink's xattrs. Usejfs_symlink_inode_operationsinjfs_iget()as well like it already does for fast symlinks.Fixes c7f2e1f0ac14 ("jfs: add jfs specific ->setattr call")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
Christian Brauner
maintainerPATCH v3 3/33 ecryptfs: don't leak the inode when a lookup can't open the lower file
In reply to Christian Braunerecryptfs_lookup_interpose() opens the lower file of a regular file to read the size from the header. If that fails it marks the inode bad and returns, but a new inode is still I_NEW and nobody ever unlocks or puts it. So it leaks together with its lower inode, a concurrent lookup through another hard link waits for it forever and umount complains about busy inodes. The lower open fails e.g. with ETXTBSY while the lower file is executed. A cached inode is marked bad for all its other users instead.
Use iget_failed() for a new inode and iput() for a cached one.
--- fs/ecryptfs/inode.c | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-)
fs/ecryptfs/inode.c +4 -1
@@ -362,7 +362,10 @@ static struct dentry *ecryptfs_lookup_interpose(struct dentry *dentry,if (S_ISREG (inode->i_mode)) { rc =ecryptfs_i_size_read (dentry, inode);if (rc) { -make_bad_inode (inode); +if (inode_state_read_once (inode) &I_NEW ) +iget_failed (inode); +else +iput (inode);return ERR_PTR (rc); } }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 03/33] ecryptfs: don't leak the inode when a lookup can't open the lower file
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
ecryptfs_lookup_interpose()opens the lower file of a regular file to read the size from the header. If that fails it marks the inode bad and returns, but a new inode is stillI_NEWand nobody ever unlocks or puts it. So it leaks together with its lower inode, a concurrent lookup through another hard link waits for it forever and umount complains about busy inodes. The lower open fails e.g. with ETXTBSY while the lower file is executed. A cached inode is marked bad for all its other users instead.
Quoted 8 lines
Use
iget_failed()for a new inode andiput()for a cached one.Fixes 3b06b3ebf441 ("eCryptfs: Fix new inode race condition")Reported-by Farhad Alemi <[email protected]>Closes https://lore.kernel.org/r/CA+0ovCgYP920GT68hAJK6Trjyv1kGv2dZAuem4LEunycQ=POww@mail.gmail.com open hereAcked-by Tyler Hicks <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=3
Christian Brauner
maintainerPATCH v3 4/33 ecryptfs: fall back to a read-only lower file
In reply to Christian Braunerecryptfs opens the lower file read-write unless the lower inode is IS_RDONLY(). A read-only mount underneath refuses that and so does ext4 after an error since commit d3476f3dad4a ("ext4: don't set SB_RDONLY after filesystem errors"). Then every lookup of a regular file fails with EROFS. The other filesystems are about to behave like ext4, so fall back to opening the lower file read-only. Opening an ecryptfs file for writing then fails with EPERM like on top of a read-only superblock.
truncate(2) and creating a file still write to the lower file and would trip the WARN_ON_ONCE() in __kernel_write_iter(). So let ecryptfs_write_lower() and __ecryptfs_truncate() refuse that with EROFS. Opening an empty file doesn't try to write the header through a read-only lower file at all, read_or_initialize_metadata() skips it and the open fails with EIO.
Note that ecryptfs never takes write access on the lower mount. On a read-only bind mount that was only a problem for directories so far. With this regular files can be unlinked and changed through it too.
--- fs/ecryptfs/file.c | 4 +++- fs/ecryptfs/inode.c | 8 ++++++++ fs/ecryptfs/kthread.c | 15 +++++++++++++-- fs/ecryptfs/read_write.c | 3 +++ 4 files changed, 27 insertions(+), 3 deletions(-)
fs/ecryptfs/file.c +3 -1
@@ -170,8 +170,10 @@ static int read_or_initialize_metadata(struct dentry *dentry) goto out; } +/* The header can only be written through a writable lower file. */ if (!(mount_crypt_stat->flags &ECRYPTFS_XATTR_METADATA_ENABLED ) && - !i_size_read (ecryptfs_inode_to_lower (inode))) { + !i_size_read (ecryptfs_inode_to_lower (inode)) && + (ecryptfs_inode_to_private (inode)->lower_file->f_mode &FMODE_WRITE )) { rc =ecryptfs_initialize_file (dentry, inode);if (!rc) goto out;
fs/ecryptfs/inode.c +8 -0
@@ -741,6 +741,7 @@ static int __ecryptfs_truncate(struct dentry *dentry, const struct iattr *ia)loff_t i_size =i_size_read (inode);loff_t lower_size_before_truncate;loff_t lower_size_after_truncate; +struct file *lower_file;struct iattr lower_ia;size_t num_zeros;int rc;
@@ -771,6 +772,13 @@ static int __ecryptfs_truncate(struct dentry *dentry, const struct iattr *ia)if (rc)return rc; +/* Fail before zeroing the page cache, see ecryptfs_write_lower(). */ + lower_file =ecryptfs_inode_to_private (inode)->lower_file; +if (!(lower_file->f_mode &FMODE_WRITE )) { + rc = -EROFS ; + goto out; + } +if (ia->ia_size > i_size) {char zero[] = {0x00 };
fs/ecryptfs/kthread.c +13 -2
@@ -30,6 +30,17 @@ static struct ecryptfs_kthread_ctl {static struct task_struct *ecryptfs_kthread; +/* A read-only mount or a filesystem that hit an error refuses O_RDWR. */ +static struct file *ecryptfs_open_lower (const struct path *path,int flags, +const struct cred *cred) +{ +struct file *file =dentry_open (path, flags, cred); + +if (PTR_ERR_OR_ZERO (file) == -EROFS && (flags &O_ACCMODE ) ==O_RDWR ) + file =dentry_open (path, (flags & ~O_ACCMODE ) |O_RDONLY , cred); +return file; +} +/** * ecryptfs_threadfn * @ignored: ignored
@@ -59,7 +70,7 @@ static int ecryptfs_threadfn(void *ignored) struct ecryptfs_open_req, kthread_ctl_list);list_del (&req->kthread_ctl_list); - *req->lower_file =dentry_open (&req->path, + *req->lower_file =ecryptfs_open_lower (&req->path, (O_RDWR |O_LARGEFILE ),current_cred ());complete (&req->done); }
@@ -132,7 +143,7 @@ int ecryptfs_privileged_open(struct file **lower_file,* lower file is fput() when all eCryptfs files for the inode are * released. */ flags |=IS_RDONLY (d_inode (lower_dentry)) ?O_RDONLY :O_RDWR ; - (*lower_file) =dentry_open (&req.path, flags, cred); + (*lower_file) =ecryptfs_open_lower (&req.path, flags, cred);if (!IS_ERR (*lower_file)) goto out;if ((flags &O_ACCMODE ) ==O_RDONLY ) {
fs/ecryptfs/read_write.c +3 -0
@@ -33,6 +33,9 @@ int ecryptfs_write_lower(struct inode *ecryptfs_inode, char *data, lower_file =ecryptfs_inode_to_private (ecryptfs_inode)->lower_file;if (!lower_file)return -EIO ; +/* ecryptfs_open_lower() may have fallen back to a read-only file. */ +if (!(lower_file->f_mode &FMODE_WRITE )) +return -EROFS ; rc =kernel_write (lower_file, data, size, &offset);mark_inode_dirty_sync (ecryptfs_inode);return rc;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 04/33] ecryptfs: fall back to a read-only lower file
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines
ecryptfs opens the lower file read-write unless the lower inode is
IS_RDONLY(). A read-only mount underneath refuses that and so does ext4 after an error since commitd3476f3dad4a("ext4: don't setSB_RDONLYafter filesystem errors"). Then every lookup of a regular file fails with EROFS. The other filesystems are about to behave like ext4, so fall back to opening the lower file read-only. Opening an ecryptfs file for writing then fails with EPERM like on top of a read-only superblock.truncate(2) and creating a file still write to the lower file and would trip the
WARN_ON_ONCE()in__kernel_write_iter(). So letecryptfs_write_lower()and__ecryptfs_truncate()refuse that with EROFS. Opening an empty file doesn't try to write the header through a read-only lower file at all,read_or_initialize_metadata()skips it and the open fails with EIO.
Quoted 5 lines
Note that ecryptfs never takes write access on the lower mount. On a read-only bind mount that was only a problem for directories so far. With this regular files can be unlinked and changed through it too.
Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=4
Christian Brauner
maintainerPATCH v3 5/33 fs: add a generic emergency read-only state
In reply to Christian Braunerext4 and f2fs don't set SB_RDONLY after errors anymore and refuse modifications themselves. Doing that in all the other filesystems means the same checks in ->open, ->setattr, ->fsync, the directory and quota operations and ->reconfigure everywhere. Jan suggested a flag in the superblock instead so the VFS can do it once (cf. [1]).
So add sb->s_state with SB_STATE_EMERGENCY_RO. It's only ever set and never cleared and SB_RDONLY stays untouched. A filesystem calls sb_set_emergency_ro() where it used to set SB_RDONLY and the VFS refuses modifications wherever sb_rdonly() would:
- __mnt_is_readonly() and sb_permission(), so everything that takes write access through mnt_want_write(), mnt_want_write_file() or mnt_get_write_access()
- notify_change(), rw_verify_area(), remap_verify_area(), vfs_fallocate(), vfs_fsync_range() and do_page_mkwrite() for files opened before the error
- reconfigure_super() going read-write
- the dquot code and quotactl(2)
- sync_filesystem() and the per-superblock passes of sync(2)
sb_rdonly_any() is true for a read-only mount and for the emergency state so nobody has to open-code the pair.
So umount and the ->reconfigure() implementations that sync first don't write such a superblock anymore either. syncfs(2) and Q_SYNC fail with EROFS so userspace learns that nothing was written. Jan suggested that (cf. [2]).
freeze_super() refuses a new freeze with EROFS. Nothing is synced anymore so the flusher would keep writing to the frozen filesystem. Joining a freeze that's already in effect still works and thaw_super() is unchanged so an error while frozen is undone cleanly. FIFREEZE and dm suspend with lockfs fail on an errored filesystem now, like they already did for ext4.
/proc/mounts, statmount() and statfs() report the superblock as read-only. "emergency_ro" isn't shown as a mount option because remount helpers pass the shown options back.
The filesystem still has to refuse what doesn't go through the VFS: its allocators, its journal and deleting unlinked inodes. Writeback isn't stopped. The flusher, sync_bdevs() in sync(2) and sync_blockdev() in kill_block_super() keep writing, so the filesystem decides what its writeback still writes. Without a journal refusing only part of it leaves the disk inconsistent. ext4 refuses data writeback in that state, jfs commits the allocations writeback hasn't committed yet. The VFS can't decide that.
[1]: https://lore.kernel.org/r/my2k7257m265fp5ad77n5hfndl23i56gsog3jwnsyocp3yxbnj@aphfqdiej2hs
[2]: https://lore.kernel.org/r/5qefa4mracb3x2z4vsdejmkyyfrhdcrixmmnooz4rzwcdfoejm@mqkvixj6afwp
--- Documentation/filesystems/porting.rst | 20 ++++++++++++++++++++ fs/attr.c | 4 ++++ fs/namei.c | 3 ++- fs/namespace.c | 5 ++++- fs/open.c | 4 ++++ fs/proc_namespace.c | 2 +- fs/quota/dquot.c | 27 +++++++++++++++++++++------ fs/quota/quota.c | 13 +++++++++++++ fs/read_write.c | 5 +++++ fs/remap_range.c | 4 ++++ fs/statfs.c | 9 +++++++-- fs/super.c | 17 +++++++++++++++++ fs/sync.c | 14 +++++++++++--- include/linux/fs/super.h | 23 +++++++++++++++++++++++ include/linux/fs/super_types.h | 19 +++++++++++++++++++ mm/memory.c | 6 ++++++ 16 files changed, 161 insertions(+), 14 deletions(-)
Documentation/filesystems/porting.rst +20 -0
@@ -1422,3 +1422,23 @@ helpers take a const struct mnt_idmap *. mnt_idmap() and file_mnt_idmap() return one. The idmapping is immutable so nothing should have modified it anyway. References are taken and dropped via mnt_idmap_get() and mnt_idmap_put() as before, both accept a const pointer. + +--- + +**recommended** + +Filesystems that set SB_RDONLY on the superblock after an error +(errors=remount-ro) must not do that anymore. SB_RDONLY only changes under +sb->s_umount through reconfigure_super() and freeze_super() and thaw_super() +rely on that. Call sb_set_emergency_ro() instead. The VFS then refuses +modifications with EROFS, refuses a remount to read-write and a new freeze, +doesn't sync the superblock anymore and shows it as read-only. The +filesystem still has to refuse what reaches it without passing through the +VFS: its own allocation and journal paths and the deletion of unlinked +inodes in ->evict_inode(). Test sb_emergency_ro() for that, or +sb_rdonly_any() which is also true for a read-only mount. Writeback isn't +stopped. The flusher keeps calling ->writepages() and ->write_inode() and +the block device is still flushed, so the filesystem decides what its +writeback still writes. Without a journal refusing only the inodes while +the data and the bitmaps go out loses data. A remount to read-only must not +mark the filesystem clean in that state.
fs/attr.c +4 -0
@@ -435,6 +435,10 @@ int notify_change(const struct mnt_idmap *idmap, struct dentry *dentry,WARN_ON_ONCE (!inode_is_locked (inode)); +/* An error made the filesystem read-only. */ +if (sb_emergency_ro (inode->i_sb)) +return -EROFS ; + error =may_setattr (idmap, inode, ia_valid);if (error)return error;
fs/namei.c +2 -1
@@ -607,7 +607,8 @@ static int sb_permission(struct super_block *sb, struct inode *inode, int mask)umode_t mode = inode->i_mode;/* Nobody gets write access to a read-only fs. */ -if (sb_rdonly (sb) && (S_ISREG (mode) ||S_ISDIR (mode) ||S_ISLNK (mode))) +if (sb_rdonly_any (sb) && + (S_ISREG (mode) ||S_ISDIR (mode) ||S_ISLNK (mode)))return -EROFS ; }return 0 ;
fs/namespace.c +4 -1
@@ -396,7 +396,7 @@ static struct mount *alloc_vfsmnt(const char *name)*/ bool __mnt_is_readonly (const struct vfsmount *mnt) { -return (mnt->mnt_flags &MNT_READONLY ) ||sb_rdonly (mnt->mnt_sb); +return (mnt->mnt_flags &MNT_READONLY ) ||sb_rdonly_any (mnt->mnt_sb); }EXPORT_SYMBOL_GPL (__mnt_is_readonly);
@@ -5492,6 +5492,9 @@ static void statmount_sb_basic(struct kstatmount *s) s->sm.sb_dev_minor =MINOR (sb->s_dev); s->sm.sb_magic = sb->s_magic; s->sm.sb_flags = sb->s_flags & (SB_RDONLY |SB_SYNCHRONOUS |SB_DIRSYNC |SB_LAZYTIME ); +/* An error made the filesystem read-only, SB_RDONLY stays clear. */ +if (sb_emergency_ro (sb)) + s->sm.sb_flags |=SB_RDONLY ; }static void statmount_mnt_parent (struct kstatmount *s,const struct mount *m)
fs/open.c +4 -0
@@ -324,6 +324,10 @@ int vfs_fallocate(struct file *file, int mode, loff_t offset, loff_t len)if (!S_ISREG (inode->i_mode) && !S_ISBLK (inode->i_mode))return -ENODEV ; +/* An error made the filesystem read-only. */ +if (S_ISREG (inode->i_mode) &&sb_emergency_ro (inode->i_sb)) +return -EROFS ; +/* Check for wraparound */ if (check_add_overflow (offset, len, &sum))return -EFBIG ;
fs/proc_namespace.c +1 -1
@@ -179,7 +179,7 @@ static int show_mountinfo(struct seq_file *m, struct vfsmount *mnt) }else {mangle (m, r->mnt_devname); } -seq_puts (m,sb_rdonly (sb) ?" ro" :" rw" ); +seq_puts (m,sb_rdonly_any (sb) ?" ro" :" rw" ); err =show_sb_opts (m, sb);if (err) goto out;
fs/quota/dquot.c +21 -6
@@ -495,6 +495,11 @@ int dquot_acquire(struct dquot *dquot)set_bit (DQ_READ_B , &dquot->dq_flags);/* Instantiate dquot if needed */ if (!dquot_active (dquot) && !dquot->dq_off) { +/* An error made the filesystem read-only. */ +if (sb_emergency_ro (dquot->dq_sb)) { + ret = -EROFS ; + goto out_iolock; + } ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk (dquot);/* Write the info if needed */ if (info_dirty (&dqopt->info[dquot->dq_id.type])) {
@@ -536,10 +541,12 @@ int dquot_commit(struct dquot *dquot) goto out_lock;/* Inactive dquot can be only if there was error during read/init * => we have better not writing it */ -if (dquot_active (dquot)) - ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk (dquot); -else +if (!dquot_active (dquot)) ret = -EIO ; +else if (sb_emergency_ro (dquot->dq_sb)) + ret = -EROFS ; +else + ret = dqopt->ops[dquot->dq_id.type]->commit_dqblk (dquot); out_lock:memalloc_nofs_restore (memalloc);mutex_unlock (&dquot->dq_lock);
@@ -561,7 +568,8 @@ int dquot_release(struct dquot *dquot)/* Check whether we are not racing with some other dqget() */ if (dquot_is_busy (dquot)) goto out_dqlock; -if (dqopt->ops[dquot->dq_id.type]->release_dqblk) { +if (dqopt->ops[dquot->dq_id.type]->release_dqblk && + !sb_emergency_ro (dquot->dq_sb)) { ret = dqopt->ops[dquot->dq_id.type]->release_dqblk (dquot);/* Write the info */ if (info_dirty (&dqopt->info[dquot->dq_id.type])) {
@@ -769,6 +777,10 @@ int dquot_quota_sync(struct super_block *sb, int type)int cnt;int ret; +/* An error made the filesystem read-only. */ +if (sb_emergency_ro (sb)) +return -EROFS ; + ret =dquot_writeback_dquots (sb, type);if (ret)return ret;
@@ -2148,6 +2160,9 @@ int dquot_commit_info(struct super_block *sb, int type) {struct quota_info *dqopt =sb_dqopt (sb); +/* An error made the filesystem read-only. */ +if (sb_emergency_ro (sb)) +return -EROFS ;return dqopt->ops[type]->write_file_info (sb, type); }EXPORT_SYMBOL (dquot_commit_info);
@@ -2271,7 +2286,7 @@ int dquot_disable(struct super_block *sb, int type, unsigned int flags)* Now all dquots should be invalidated, all writes done so we * should be only users of the info. No locks needed. */ -if (info_dirty (&dqopt->info[cnt])) +if (info_dirty (&dqopt->info[cnt]) && !sb_emergency_ro (sb)) sb->dq_op->write_info (sb, cnt);if (dqopt->ops[cnt]->free_file_info) dqopt->ops[cnt]->free_file_info (sb, cnt);
@@ -2288,7 +2303,7 @@ int dquot_disable(struct super_block *sb, int type, unsigned int flags)/* Sync the superblock so that buffers with quota data are written to * disk (and so userspace sees correct data afterwards). */ -if (sb->s_op->sync_fs) +if (sb->s_op->sync_fs && !sb_emergency_ro (sb)) sb->s_op->sync_fs (sb,1 );sync_blockdev (sb->s_bdev);/* Now the quota files are just ordinary files and we can set the
fs/quota/quota.c +13 -0
@@ -783,6 +783,19 @@ static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,if (ret <0 )return ret; +/* An error made the filesystem read-only. */ +if (sb_emergency_ro (sb)) { +switch (cmd) { +case Q_QUOTAON : +case Q_SETINFO : +case Q_SETQUOTA : +case Q_XQUOTAON : +case Q_XQUOTARM : +case Q_XSETQLIM : +return -EROFS ; + } + } +switch (cmd) {case Q_QUOTAON :return quota_quotaon (sb, type, id, path);
fs/read_write.c +5 -0
@@ -472,6 +472,11 @@ int rw_verify_area(int read_write, struct file *file, const loff_t *ppos, size_t } } +/* An error made the filesystem read-only. */ +if (read_write ==WRITE &&S_ISREG (file_inode (file)->i_mode) && +sb_emergency_ro (file_inode (file)->i_sb)) +return -EROFS ; + ret =security_file_permission (file, mask);if (ret)return ret;
fs/remap_range.c +4 -0
@@ -111,6 +111,10 @@ int remap_verify_area(struct file *file, loff_t pos, loff_t len, bool write)if (unlikely (check_add_overflow (pos, len, &tmp)))return -EINVAL ; +/* An error made the filesystem read-only. */ +if (write &&sb_emergency_ro (file_inode (file)->i_sb)) +return -EROFS ; + ret =security_file_permission (file, mask);if (ret)return ret;
fs/statfs.c +7 -2
@@ -48,8 +48,13 @@ static int flags_by_sb(int s_flags)static int calculate_f_flags (struct vfsmount *mnt) { -return ST_VALID |flags_by_mnt (mnt->mnt_flags) | -flags_by_sb (mnt->mnt_sb->s_flags); +int flags =ST_VALID |flags_by_mnt (mnt->mnt_flags) | +flags_by_sb (mnt->mnt_sb->s_flags); + +/* An error made the filesystem read-only, SB_RDONLY stays clear. */ +if (sb_emergency_ro (mnt->mnt_sb)) + flags |=ST_RDONLY ; +return flags; }static int statfs_by_dentry (struct dentry *dentry,struct kstatfs *buf)
fs/super.c +17 -0
@@ -1091,6 +1091,10 @@ int reconfigure_super(struct fs_context *fc) remount_ro = (fc->sb_flags &SB_RDONLY ) && !sb_rdonly (sb); } +/* An error made the filesystem read-only until it is unmounted. */ +if (remount_rw &&sb_emergency_ro (sb)) +return -EROFS ; +if (remount_ro) {if (!hlist_empty (&sb->s_pins)) {super_unlock_excl (sb);
@@ -2287,6 +2291,16 @@ int freeze_super(struct super_block *sb, enum freeze_holder who, const void *fre goto retry; } +/* +* After an error nothing gets synced anymore while the flusher may +* still write, so a new freeze can't give a stable filesystem. Joining +* a freeze that is already in effect above is fine. +*/ +if (sb_emergency_ro (sb)) { +deactivate_locked_super (sb); +return -EROFS ; + } +if (sb_rdonly (sb)) {/* Nothing to do really... */ WARN_ON_ONCE (freeze_inc (sb, who) >1 );
@@ -2309,6 +2323,9 @@ int freeze_super(struct super_block *sb, enum freeze_holder who, const void *fre/* All writers are done so after syncing there won't be dirty data */ ret =sync_filesystem (sb); +/* An error while we waited for the writers means nothing was synced. */ +if (!ret &&sb_emergency_ro (sb)) + ret = -EROFS ;if (ret) { sb->s_writers.frozen =SB_UNFROZEN ;sb_freeze_unlock (sb,SB_FREEZE_PAGEFAULT );
fs/sync.c +11 -3
@@ -39,8 +39,9 @@ int sync_filesystem(struct super_block *sb)/* * No point in syncing out anything if the filesystem is read-only. +* After an error made it read-only nothing is pushed out anymore. */ -if (sb_rdonly (sb)) +if (sb_rdonly_any (sb))return 0 ;/*
@@ -73,13 +74,13 @@ EXPORT_SYMBOL(sync_filesystem);static void sync_inodes_one_sb (struct super_block *sb,void *arg) { -if (!sb_rdonly (sb)) +if (!sb_rdonly_any (sb))sync_inodes_sb (sb); }static void sync_fs_one_sb (struct super_block *sb,void *arg) { -if (!sb_rdonly (sb) && !(sb->s_iflags &SB_I_SKIP_SYNC ) && +if (!sb_rdonly_any (sb) && !(sb->s_iflags &SB_I_SKIP_SYNC ) && sb->s_op->sync_fs) sb->s_op->sync_fs (sb, *(int *)arg); }
@@ -157,6 +158,9 @@ SYSCALL_DEFINE1(syncfs, int, fd)down_read (&sb->s_umount); ret =sync_filesystem (sb); +/* An error made the filesystem read-only. */ +if (!ret &&sb_emergency_ro (sb)) + ret = -EROFS ;up_read (&sb->s_umount); ret2 =errseq_check_and_advance (&sb->s_wb_err, &fd_file (f)->f_sb_err);
@@ -181,6 +185,10 @@ int vfs_fsync_range(struct file *file, loff_t start, loff_t end, int datasync)if (!file->f_op->fsync)return -EINVAL ; +/* Nothing is written back after an error, don't claim it is. */ +if (!special_file (file_inode (file)->i_mode) && +sb_emergency_ro (file_inode (file)->i_sb)) +return -EROFS ;if (!datasync)sync_lazytime (inode);return file->f_op->fsync (file, start, end, datasync);
include/linux/fs/super.h +23 -0
@@ -2,6 +2,7 @@#ifndef _LINUX_FS_SUPER_H#define _LINUX_FS_SUPER_H +#include <linux/bitops.h> #include <linux/fs/super_types.h> #include <linux/unicode.h>
@@ -187,6 +188,28 @@ static inline bool sb_rdonly(const struct super_block *sb)return sb->s_flags &SB_RDONLY ; } +/* +* An error made the filesystem read-only. Check this wherever sb_rdonly() +* gates a write but never where it describes the mount, e.g. in the read-only +* shortcuts of freeze_super() and thaw_super(). It can get set while frozen. +*/ +static inline bool sb_emergency_ro (const struct super_block *sb) +{ +return test_bit (SB_STATE_EMERGENCY_RO , &sb->s_state); +} + +/* True for the caller that set the state. */ +static inline bool sb_set_emergency_ro (struct super_block *sb) +{ +return !test_and_set_bit (SB_STATE_EMERGENCY_RO , &sb->s_state); +} + +/* A read-only mount or the emergency state. */ +static inline bool sb_rdonly_any (const struct super_block *sb) +{ +return sb_rdonly (sb) ||sb_emergency_ro (sb); +} +static inline bool sb_is_blkdev_sb (struct super_block *sb) {return IS_ENABLED (CONFIG_BLOCK ) && sb == blockdev_superblock;
include/linux/fs/super_types.h +19 -0
@@ -238,6 +238,9 @@ struct super_block {/* Read-only state of the superblock is being changed */ int s_readonly_remount; +/* SB_STATE_* bits, set with set_bit() and never cleared */ +unsigned long s_state; +/* per-sb errseq_t for reporting writeback errors via syncfs */ errseq_t s_wb_err;
@@ -353,4 +356,20 @@ struct super_block {#define SB_I_ALLOW_HSM 0x00004000/* Allow HSM events on this superblock */ #define SB_I_NO_DATA_INTEGRITY 0x00008000/* fs cannot guarantee data persistence on sync */ +/* +* sb->s_state bit numbers. Set with set_bit() from any context without +* s_umount and never cleared, so they can be tested without any ordering +* against the setter. Unlike SB_RDONLY which only changes under s_umount in +* reconfigure_super(). +*/ +enum sb_state_bits { +/* +* An error made the filesystem read-only. The VFS refuses modifications +* with EROFS, a remount to read-write and a new freeze until umount and +* doesn't sync the superblock anymore. The filesystem still has to +* refuse what doesn't pass through the VFS, see sb_emergency_ro(). +*/ +SB_STATE_EMERGENCY_RO =0 , +}; +#endif /* _LINUX_FS_SUPER_TYPES_H */
mm/memory.c +6 -0
@@ -3876,6 +3876,12 @@ static vm_fault_t do_page_mkwrite(struct vm_fault *vmf, struct folio *folio) IS_SWAPFILE(vmf->vma->vm_file->f_mapping->host))return VM_FAULT_SIGBUS ; +/* An error made the filesystem read-only. */ +if (vmf->vma->vm_file && +S_ISREG (file_inode (vmf->vma->vm_file)->i_mode) && +sb_emergency_ro (file_inode (vmf->vma->vm_file)->i_sb)) +return VM_FAULT_SIGBUS ; + ret = vmf->vma->vm_ops->page_mkwrite (vmf);/* Restore original flags so that caller is not surprised */ vmf->flags = old_flags;
Signature · 1 line
2.53.0
Re: [PATCH v3 05/33] fs: add a generic emergency read-only state
Quoted 49 lines · Christian Brauner
ext4 and f2fs don't set
SB_RDONLYafter errors anymore and refuse modifications themselves. Doing that in all the other filesystems means the same checks in ->open, ->setattr, ->fsync, the directory and quota operations and ->reconfigure everywhere. Jan suggested a flag in the superblock instead so the VFS can do it once (cf. [1]).So add
sb->s_statewithSB_STATE_EMERGENCY_RO. It's only ever set and never cleared andSB_RDONLYstays untouched. A filesystem callssb_set_emergency_ro()where it used to setSB_RDONLYand the VFS refuses modifications whereversb_rdonly()would:-
__mnt_is_readonly()andsb_permission(), so everything that takes write access throughmnt_want_write(),mnt_want_write_file()ormnt_get_write_access()
-notify_change(),rw_verify_area(),remap_verify_area(),vfs_fallocate(),vfs_fsync_range()anddo_page_mkwrite()for files opened before the error
-reconfigure_super()going read-write
- the dquot code and quotactl(2)
-sync_filesystem()and the per-superblock passes of sync(2)
sb_rdonly_any()is true for a read-only mount and for the emergency state so nobody has to open-code the pair.So umount and the ->reconfigure() implementations that sync first don't write such a superblock anymore either. syncfs(2) and
Q_SYNCfail with EROFS so userspace learns that nothing was written. Jan suggested that (cf. [2]).
freeze_super()refuses a new freeze with EROFS. Nothing is synced anymore so the flusher would keep writing to the frozen filesystem. Joining a freeze that's already in effect still works andthaw_super()is unchanged so an error while frozen is undone cleanly. FIFREEZE and dm suspend with lockfs fail on an errored filesystem now, like they already did for ext4.
/proc/mounts,statmount()andstatfs()report the superblock as read-only. "emergency_ro" isn't shown as a mount option because remount helpers pass the shown options back.The filesystem still has to refuse what doesn't go through the VFS: its allocators, its journal and deleting unlinked inodes. Writeback isn't stopped. The flusher,
sync_bdevs()in sync(2) andsync_blockdev()inkill_block_super()keep writing, so the filesystem decides what its writeback still writes. Without a journal refusing only part of it leaves the disk inconsistent. ext4 refuses data writeback in that state, jfs commits the allocations writeback hasn't committed yet. The VFS can't decide that.
Quoted 5 lines
[1]: https://lore.kernel.org/r/my2k7257m265fp5ad77n5hfndl23i56gsog3jwnsyocp3yxbnj@aphfqdiej2hs
[2]: https://lore.kernel.org/r/5qefa4mracb3x2z4vsdejmkyyfrhdcrixmmnooz4rzwcdfoejm@mqkvixj6afwpSuggested-by Jan Kara <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Just one nit below. Otherwise feel free to add:
Quoted 9 lines
fs/quota/quota.c +1
-0@@ -783,6 +783,19 @@ static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,if (ret <0 )return ret; +/* An error made the filesystem read-only. */
fs/quota/quota.c static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, · line 796
Quoted 10 lines
fs/quota/quota.c +10
-0+if (sb_emergency_ro (sb)) { +switch (cmd) { +case Q_QUOTAON : +case Q_SETINFO : +case Q_SETQUOTA : +case Q_XQUOTAON : +case Q_XQUOTARM : +case Q_XSETQLIM : +return -EROFS ; + }
I'd use existing quotactl_cmd_write() helper for this.
Honza
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
[email protected]
AI reviewRe: [PATCH v3 05/33] fs: add a generic emergency read-only state
In reply to Christian BraunerThank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] vfs: change_mount_ro_state() skips mnt_make_readonly() on sb_emergency_ro() - [Low] quota…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] vfs: change_mount_ro_state() skips mnt_make_readonly() on sb_emergency_ro()
- [Low] quota: do_quotactl() misses sb_emergency_ro() in Q_XQUOTASYNC and Q_SYNC
--
commit aa167d356b416c1c40da72fb215b9d988cb411bc
Author: Christian Brauner <[email protected]>
fs: add a generic emergency read-only state
This commit adds an SB_STATE_EMERGENCY_RO superblock state flag and updates VFS paths to reject modifications with -EROFS when a filesystem switches to emergency read-only mode without setting SB_RDONLY.
fs/namespace.c static struct mount *alloc_vfsmnt(const char *name) · line 401
Quoted 12 lines
fs/namespace.c +1
-1@@ -396,7 +396,7 @@ static struct mount *alloc_vfsmnt(const char *name)*/ bool __mnt_is_readonly (const struct vfsmount *mnt) { -return (mnt->mnt_flags &MNT_READONLY ) ||sb_rdonly (mnt->mnt_sb); +return (mnt->mnt_flags &MNT_READONLY ) ||sb_rdonly_any (mnt->mnt_sb); }EXPORT_SYMBOL_GPL (__mnt_is_readonly);
[Severity: Medium]
Does changing __mnt_is_readonly() to check sb_rdonly_any() cause change_mount_ro_state() to skip mnt_make_readonly() when sb_emergency_ro() is set?
fs/namespace.c:change_mount_ro_state() {
bool readonly_request = (mnt_flags & MNT_READONLY);
if (readonly_request == __mnt_is_readonly(&mnt->mnt))
return 0;
if (readonly_request)
return mnt_make_readonly(mnt);
...
}When a read-write mount has open writable files or active writers and the filesystem enters SB_STATE_EMERGENCY_RO, a mount(2) call with MS_REMOUNT | MS_BIND | MS_RDONLY (do_reconfigure_mnt()) sees __mnt_is_readonly() == true, skips mnt_make_readonly(), and sets MNT_READONLY via set_mount_attributes() without calling mnt_hold_writers(). If reconfigure_super() is later called to remount the superblock read-only, sb_prepare_remount_readonly() skips mounts that already have MNT_READONLY set and transitions the superblock to SB_RDONLY while active writers remain on that mount.
[ ... ]
Quoted 14 lines
fs/quota/quota.c +6
-0@@ -783,6 +783,19 @@ static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id,if (ret <0 )return ret; +/* An error made the filesystem read-only. */ +if (sb_emergency_ro (sb)) { +switch (cmd) { +case Q_QUOTAON : +case Q_SETINFO : +case Q_SETQUOTA :
fs/quota/quota.c static int do_quotactl(struct super_block *sb, int type, int cmd, qid_t id, · line 801
Quoted 10 lines
fs/quota/quota.c +7
-0+case Q_XQUOTAON : +case Q_XQUOTARM : +case Q_XSETQLIM : +return -EROFS ; + } + } +switch (cmd) {case Q_QUOTAON :return quota_quotaon (sb, type, id, path);
[Severity: Low]
Should Q_XQUOTASYNC and Q_SYNC also be rejected here when sb_emergency_ro(sb) is set (and quota_sync_one() guarded by !sb_rdonly_any(sb))?
In do_quotactl(), Q_XQUOTASYNC still checks sb_rdonly(sb) instead of sb_rdonly_any(sb), returning 0 rather than -EROFS in emergency read-only mode:
fs/quota/quota.c:do_quotactl() {
...
case Q_XQUOTASYNC:
if (sb_rdonly(sb))
return -EROFS;
/* XFS quotas are fully coherent now, making this call a noop */
return 0;
...
}Q_SYNC and quota_sync_one() also call ->quota_sync() directly, relying on dquot_quota_sync() rather than do_quotactl() to reject emergency read-only superblocks.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=5
Christian Brauner
maintainerPATCH v3 6/33 ext2: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerWith errors=remount-ro ext2_error() sets SB_RDONLY from whatever context hit the error and without holding s_umount. That races with everyone else changing sb->s_flags and it breaks freezing. If it happens while the filesystem is frozen thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked so every writer hangs. A lookup in a corrupted directory is enough. Karl proposed teaching thaw_super() about it but Jan preferred fixing the filesystems (cf. [1]) like ext4 and f2fs already did.
So call sb_set_emergency_ro() instead and let the VFS refuse modifications. ext2 still refuses allocations and ext2_xattr_set() itself, ecryptfs calls the latter without write access on the lower mount. Unlinked inodes are left to e2fsck. ext2_write_inode() keeps working because writeback still writes data, bitmaps, indirect and directory blocks after the error and they'd be lost without the inode. A thaw after an error while frozen still writes the superblock but EXT2_ERROR_FS stays set in it.
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. Writes through file descriptors opened before the error fail with EROFS.
[1]: https://lore.kernel.org/r/hyhuckyohsabx2suvq3z7uy3pbgmihva6wgzxr3op7pzcrlu33@p7zzlgp2ehjf
--- fs/ext2/balloc.c | 5 +++++ fs/ext2/ialloc.c | 2 ++ fs/ext2/inode.c | 4 +++- fs/ext2/super.c | 13 ++++++++----- fs/ext2/xattr.c | 2 ++ 5 files changed, 20 insertions(+), 6 deletions(-)
fs/ext2/balloc.c +5 -0
@@ -1236,6 +1236,11 @@ ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal, *errp = -ENOSPC ; sb = inode->i_sb; +if (sb_emergency_ro (sb)) { + *errp = -EROFS ; +return 0 ; + } +/* * Check quota for allocation of this block. */
fs/ext2/ialloc.c +2 -0
@@ -433,6 +433,8 @@ struct inode *ext2_new_inode(struct inode *dir, umode_t mode,int err; sb = dir->i_sb; +if (sb_emergency_ro (sb)) +return ERR_PTR (-EROFS ); inode =new_inode (sb);if (!inode)return ERR_PTR (-ENOMEM );
fs/ext2/inode.c +3 -1
@@ -71,7 +71,9 @@ void ext2_evict_inode(struct inode * inode)struct ext2_block_alloc_info *rsv;int want_delete =0 ; -if (!inode->i_nlink && !is_bad_inode (inode)) { +/* After an error made us read-only the inode is left to e2fsck. */ +if (!inode->i_nlink && !is_bad_inode (inode) && + !sb_emergency_ro (inode->i_sb)) { want_delete =1 ;dquot_initialize (inode); }else {
fs/ext2/super.c +8 -5
@@ -53,7 +53,7 @@ void ext2_error(struct super_block *sb, const char *function,struct ext2_sb_info *sbi =EXT2_SB (sb);struct ext2_super_block *es = sbi->s_es; -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) {spin_lock (&sbi->s_lock); sbi->s_mount_state |=EXT2_ERROR_FS ; es->s_state |=cpu_to_le16 (EXT2_ERROR_FS );
@@ -73,11 +73,11 @@ void ext2_error(struct super_block *sb, const char *function,if (test_opt (sb,ERRORS_PANIC ))panic ("EXT2-fs: panic from previous error\n" ); -if (!sb_rdonly (sb) &&test_opt (sb,ERRORS_RO )) { +/* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). */ +if (!sb_rdonly (sb) &&test_opt (sb,ERRORS_RO ) && +sb_set_emergency_ro (sb))ext2_msg (sb,KERN_CRIT ,"error: remounting filesystem read-only" ); - sb->s_flags |=SB_RDONLY ; - } } static void ext2_msg_fc(struct fs_context *fc, const char *prefix,
@@ -179,7 +179,7 @@ static void ext2_put_super (struct super_block * sb)ext2_xattr_destroy_cache (sbi->s_ea_block_cache); sbi->s_ea_block_cache =NULL ; -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) {struct ext2_super_block *es = sbi->s_es;spin_lock (&sbi->s_lock);
@@ -1360,6 +1360,9 @@ static int ext2_reconfigure(struct fs_context *fc)if ((bool )(flags &SB_RDONLY ) ==sb_rdonly (sb)) goto out_set;if (flags &SB_RDONLY ) { +/* After an error nothing may be written, just go read-only. */ +if (sb_emergency_ro (sb)) + goto out_set;if (le16_to_cpu (es->s_state) &EXT2_VALID_FS || !(sbi->s_mount_state &EXT2_VALID_FS )) goto out_set;
fs/ext2/xattr.c +2 -0
@@ -429,6 +429,8 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name,ea_idebug (inode,"name=%d.%s, value=%p, value_len=%ld" , name_index, name, value, (long )value_len); +if (sb_emergency_ro (sb)) +return -EROFS ;if (value ==NULL ) value_len =0 ;if (name ==NULL )
Signature · 1 line
2.53.0
Re: [PATCH v3 06/33] ext2: don't set SB_RDONLY after filesystem errors
Quoted 23 lines · Christian Brauner
With errors=remount-ro
ext2_error()setsSB_RDONLYfrom whatever context hit the error and without holdings_umount. That races with everyone else changingsb->s_flagsand it breaks freezing. If it happens while the filesystem is frozenthaw_super()sees a read-only superblock and leaves thes_writersrwsems locked so every writer hangs. A lookup in a corrupted directory is enough. Karl proposed teachingthaw_super()about it but Jan preferred fixing the filesystems (cf. [1]) like ext4 and f2fs already did.So call
sb_set_emergency_ro()instead and let the VFS refuse modifications. ext2 still refuses allocations andext2_xattr_set()itself, ecryptfs calls the latter without write access on the lower mount. Unlinked inodes are left to e2fsck.ext2_write_inode()keeps working because writeback still writes data, bitmaps, indirect and directory blocks after the error and they'd be lost without the inode. A thaw after an error while frozen still writes the superblock butEXT2_ERROR_FSstays set in it.After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. Writes through file descriptors opened before the error fail with EROFS.
Quoted 5 lines
[1]: https://lore.kernel.org/r/hyhuckyohsabx2suvq3z7uy3pbgmihva6wgzxr3op7pzcrlu33@p7zzlgp2ehjf
Looks good. Feel free to add:
Honza
Quoted 116 lines
--- fs/ext2/balloc.c | 5 +++++ fs/ext2/ialloc.c | 2 ++ fs/ext2/inode.c | 4 +++- fs/ext2/super.c | 13 ++++++++----- fs/ext2/xattr.c | 2 ++ 5 files changed, 20 insertions(+), 6 deletions(-)fs/ext2/balloc.c +5
-0@@ -1236,6 +1236,11 @@ ext2_fsblk_t ext2_new_blocks(struct inode *inode, ext2_fsblk_t goal, *errp = -ENOSPC ; sb = inode->i_sb; +if (sb_emergency_ro (sb)) { + *errp = -EROFS ; +return 0 ; + } +/* * Check quota for allocation of this block. */ fs/ext2/ialloc.c +2
-0@@ -433,6 +433,8 @@ struct inode *ext2_new_inode(struct inode *dir, umode_t mode,int err; sb = dir->i_sb; +if (sb_emergency_ro (sb)) +return ERR_PTR (-EROFS ); inode =new_inode (sb);if (!inode)return ERR_PTR (-ENOMEM );fs/ext2/inode.c +3
-1@@ -71,7 +71,9 @@ void ext2_evict_inode(struct inode * inode)struct ext2_block_alloc_info *rsv;int want_delete =0 ; -if (!inode->i_nlink && !is_bad_inode (inode)) { +/* After an error made us read-only the inode is left to e2fsck. */ +if (!inode->i_nlink && !is_bad_inode (inode) && + !sb_emergency_ro (inode->i_sb)) { want_delete =1 ;dquot_initialize (inode); }else {fs/ext2/super.c +8
-5@@ -53,7 +53,7 @@ void ext2_error(struct super_block *sb, const char *function,struct ext2_sb_info *sbi =EXT2_SB (sb);struct ext2_super_block *es = sbi->s_es; -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) {spin_lock (&sbi->s_lock); sbi->s_mount_state |=EXT2_ERROR_FS ; es->s_state |=cpu_to_le16 (EXT2_ERROR_FS );@@ -73,11 +73,11 @@ void ext2_error(struct super_block *sb, const char *function,if (test_opt (sb,ERRORS_PANIC ))panic ("EXT2-fs: panic from previous error\n" ); -if (!sb_rdonly (sb) &&test_opt (sb,ERRORS_RO )) { +/* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). */ +if (!sb_rdonly (sb) &&test_opt (sb,ERRORS_RO ) && +sb_set_emergency_ro (sb))ext2_msg (sb,KERN_CRIT ,"error: remounting filesystem read-only" ); - sb->s_flags |=SB_RDONLY ; - } } static void ext2_msg_fc(struct fs_context *fc, const char *prefix,@@ -179,7 +179,7 @@ static void ext2_put_super (struct super_block * sb)ext2_xattr_destroy_cache (sbi->s_ea_block_cache); sbi->s_ea_block_cache =NULL ; -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) {struct ext2_super_block *es = sbi->s_es;spin_lock (&sbi->s_lock);@@ -1360,6 +1360,9 @@ static int ext2_reconfigure(struct fs_context *fc)if ((bool )(flags &SB_RDONLY ) ==sb_rdonly (sb)) goto out_set;if (flags &SB_RDONLY ) { +/* After an error nothing may be written, just go read-only. */ +if (sb_emergency_ro (sb)) + goto out_set;if (le16_to_cpu (es->s_state) &EXT2_VALID_FS || !(sbi->s_mount_state &EXT2_VALID_FS )) goto out_set;fs/ext2/xattr.c +2
-0@@ -429,6 +429,8 @@ ext2_xattr_set(struct inode *inode, int name_index, const char *name,ea_idebug (inode,"name=%d.%s, value=%p, value_len=%ld" , name_index, name, value, (long )value_len); +if (sb_emergency_ro (sb)) +return -EROFS ;if (value ==NULL ) value_len =0 ;if (name ==NULL )Signature · 2 lines
-- 2.53.0
Signature · 2 lines
Jan Kara <[email protected]> SUSE Labs, CR
[email protected]
AI reviewRe: [PATCH v3 06/33] ext2: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerSashiko has reviewed this patch and found no issues.
Quoted 23 lines
With errors=remount-ro
ext2_error()setsSB_RDONLYfrom whatever context hit the error and without holdings_umount. That races with everyone else changingsb->s_flagsand it breaks freezing. If it happens while the filesystem is frozenthaw_super()sees a read-only superblock and leaves thes_writersrwsems locked so every writer hangs. A lookup in a corrupted directory is enough. Karl proposed teachingthaw_super()about it but Jan preferred fixing the filesystems (cf. [1]) like ext4 and f2fs already did.So call
sb_set_emergency_ro()instead and let the VFS refuse modifications. ext2 still refuses allocations andext2_xattr_set()itself, ecryptfs calls the latter without write access on the lower mount. Unlinked inodes are left to e2fsck.ext2_write_inode()keeps working because writeback still writes data, bitmaps, indirect and directory blocks after the error and they'd be lost without the inode. A thaw after an error while frozen still writes the superblock butEXT2_ERROR_FSstays set in it.After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. Writes through file descriptors opened before the error fail with EROFS.
Quoted 5 lines
[1]: https://lore.kernel.org/r/hyhuckyohsabx2suvq3z7uy3pbgmihva6wgzxr3op7pzcrlu33@p7zzlgp2ehjf
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=6
Christian Brauner
maintainerPATCH v3 7/33 jfs: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerWith errors=remount-ro, the jfs default, jfs_handle_error() sets SB_RDONLY from whatever context hit the error and without holding s_umount. If that happens while the filesystem is frozen thaw_super() skips jfs_unfreeze() and leaves the s_writers rwsems locked. Writers hang and umount shuts the log down a second time and crashes.
So call sb_set_emergency_ro() instead and let the VFS refuse modifications. jfs_unfreeze() used sb_rdonly() to tell whether jfs_freeze() shut the log down. Track that in JFS_FLAGS_LOG_SHUTDOWN instead so thaw undoes exactly what freeze did.
jfs refuses inode and block allocation and the xattr and ACL setters itself, before txBegin() because failing txCommit() with tlocks held trips an assert. Allocations writeback hasn't committed yet when the error hits leave anonymous tlocks behind. So jfs_dirty_inode() still queues commits for those inodes and extRecord() marks the inode dirty like extAlloc() does, writeback commits what it still gets to. jfs_evict_inode() frees whatever is left and only warns if the filesystem is neither read-only nor in the emergency state.
A resize is refused after an error. And with nointegrity jfs_unfreeze() records FM_DIRTY again when the error hit while the filesystem was frozen.
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. Writes through file descriptors opened before the error fail with EROFS.
--- fs/jfs/acl.c | 3 +++ fs/jfs/inode.c | 6 +++++- fs/jfs/ioctl.c | 4 ++++ fs/jfs/jfs_dtree.c | 3 ++- fs/jfs/jfs_extent.c | 10 ++++++++++ fs/jfs/jfs_incore.h | 6 ++++++ fs/jfs/jfs_inode.c | 3 +++ fs/jfs/super.c | 36 +++++++++++++++++++++++++++++++----- fs/jfs/xattr.c | 3 +++ 9 files changed, 67 insertions(+), 7 deletions(-)
fs/jfs/acl.c +3 -0
@@ -98,6 +98,9 @@ int jfs_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,struct inode *inode =d_inode (dentry);umode_t mode = inode->i_mode; +if (sb_emergency_ro (inode->i_sb)) +return -EROFS ; + tid =txBegin (inode->i_sb,0 );mutex_lock (&JFS_IP (inode)->commit_mutex);if (type ==ACL_TYPE_ACCESS && acl) {
fs/jfs/inode.c +5 -1
@@ -178,7 +178,7 @@ void jfs_evict_inode(struct inode *inode)* nothing did. */ if (ji->atlhead) { -WARN_ON_ONCE (!sb_rdonly (inode->i_sb)); +WARN_ON_ONCE (!sb_rdonly_any (inode->i_sb));txAbortAnon (inode); }BUG_ON (!list_empty (&ji->anon_inode_list));
@@ -196,6 +196,10 @@ void jfs_dirty_inode(struct inode *inode, int flags) {static int noisy =5 ; +/* After an error only allocations under way still get committed. */ +if (sb_emergency_ro (inode->i_sb) && !READ_ONCE (JFS_IP (inode)->atlhead)) +return ; +if (isReadOnly (inode)) {if (!special_file (inode->i_mode) && noisy) {/* kernel allows writes to devices on read-only
fs/jfs/ioctl.c +4 -0
@@ -116,6 +116,10 @@ long jfs_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +/* Don't trust the block map after an error. */ +if (sb_emergency_ro (sb)) +return -EROFS ; +if (!bdev_max_discard_sectors (sb->s_bdev)) {jfs_warn ("FITRIM not supported on device" );return -EOPNOTSUPP ;
fs/jfs/jfs_dtree.c +2 -1
@@ -2935,7 +2935,8 @@ int jfs_readdir(struct file *file, struct dir_context *ctx)if ((jfs_dirent->position <2 ) || (jfs_dirent->position >=JFS_IP (ip)->next_index)) { -if (!page_fixed && !isReadOnly (ip)) { +if (!page_fixed && !isReadOnly (ip) && + !sb_emergency_ro (ip->i_sb)) { fix_page =1 ;/* * setting overflow and setting
fs/jfs/jfs_extent.c +10 -0
@@ -74,6 +74,9 @@ extAlloc(struct inode *ip, s64 xlen, s64 pno, xad_t * xp, bool abnr)int rc;int xflag; +if (sb_emergency_ro (ip->i_sb)) +return -EROFS ; +if (isReadOnly (ip)) {jfs_error (ip->i_sb,"read-only filesystem\n" );return -EIO ;
@@ -258,6 +261,9 @@ int extRecord(struct inode *ip, xad_t * xp) {int rc; +if (sb_emergency_ro (ip->i_sb)) +return -EROFS ; +if (isReadOnly (ip)) {jfs_error (ip->i_sb,"read-only filesystem\n" );return -EIO ;
@@ -270,6 +276,10 @@ int extRecord(struct inode *ip, xad_t * xp)/* update the extent */ rc =xtUpdate (0 , ip, xp); +/* get the anonymous tlocks committed, like extAlloc() does */ +if (!rc) +mark_inode_dirty (ip); +mutex_unlock (&JFS_IP (ip)->commit_mutex);return rc; }
fs/jfs/jfs_incore.h +6 -0
@@ -192,6 +192,7 @@ struct jfs_sb_info {struct inode *direct_inode;/* metadata inode */ uint state;/* mount/recovery state */ unsigned long flag;/* mount time flags */ +unsigned long jfs_flags;/* runtime state, JFS_FLAGS_* */ uint p_state;/* state prior to going no integrity */ kuid_t uid;/* uid to override on-disk uid */ kgid_t gid;/* gid to override on-disk gid */
@@ -202,6 +203,11 @@ struct jfs_sb_info {/* jfs_sb_info commit_state */ #define IN_LAZYCOMMIT 1 +/* jfs_sb_info jfs_flags bits */ +enum { +JFS_FLAGS_LOG_SHUTDOWN ,/* jfs_freeze() shut the log down */ +}; +static inline struct jfs_inode_info *JFS_IP (struct inode *inode) { return container_of(inode, struct jfs_inode_info, vfs_inode);
fs/jfs/jfs_inode.c +3 -0
@@ -45,6 +45,9 @@ struct inode *ialloc(struct inode *parent, umode_t mode)struct jfs_inode_info *jfs_inode;int rc; +if (sb_emergency_ro (sb)) +return ERR_PTR (-EROFS ); + inode =new_inode (sb);if (!inode) {jfs_warn ("ialloc: new_inode returned NULL!" );
fs/jfs/super.c +31 -5
@@ -63,7 +63,7 @@ static void jfs_handle_error(struct super_block *sb) {struct jfs_sb_info *sbi =JFS_SBI (sb); -if (sb_rdonly (sb)) +if (sb_rdonly_any (sb))return ;updateSuper (sb,FM_DIRTY );
@@ -74,7 +74,8 @@ static void jfs_handle_error(struct super_block *sb)else if (sbi->flag &JFS_ERR_REMOUNT_RO ) {jfs_err ("ERROR: (device %s): remounting filesystem as read-only" , sb->s_id); - sb->s_flags |=SB_RDONLY ; +/* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). */ +sb_set_emergency_ro (sb); }/* nothing is done for continue beyond marking the superblock dirty */
@@ -367,6 +368,10 @@ static int jfs_reconfigure(struct fs_context *fc)int flag = ctx->flag;int ret; +/* Don't resize based on a block map we don't trust after an error. */ +if (ctx->resize &&sb_emergency_ro (sb)) +return -EROFS ; +sync_filesystem (sb);/* Transfer results of parsing to the sbi */
@@ -382,7 +387,8 @@ static int jfs_reconfigure(struct fs_context *fc) ctx->nls_map =NULL ;if (ctx->resize) { -if (sb_rdonly (sb)) { +/* An error can also hit during the sync above. */ +if (sb_rdonly_any (sb)) {pr_err ("JFS: resize requires volume to be mounted read-write\n" );return -EROFS ; }
@@ -415,6 +421,9 @@ static int jfs_reconfigure(struct fs_context *fc)return ret; }if (!sb_rdonly (sb) && readonly) { +/* After an error nothing may be written, umount closes the log. */ +if (sb_emergency_ro (sb)) +return 0 ; rc =dquot_suspend (sb,-1 );if (rc <0 )return rc;
@@ -587,6 +596,15 @@ static int jfs_freeze(struct super_block *sb)int rc =0 ;if (!sb_rdonly (sb)) { +/* +* After an error leave the log open and the superblock alone. +* Waiting for everything to be written home needs a quiesced +* log, jfs_flush_journal() BUG()s otherwise. +*/ +if (sb_emergency_ro (sb)) { +jfs_flush_journal (log,1 ); +return 0 ; + }txQuiesce (sb); rc =lmLogShutdown (log);if (rc) {
@@ -597,6 +615,7 @@ static int jfs_freeze(struct super_block *sb)return rc; } +set_bit (JFS_FLAGS_LOG_SHUTDOWN , &sbi->jfs_flags); rc =updateSuper (sb,FM_CLEAN );if (rc) {jfs_err ("jfs_freeze: updateSuper failed" );
@@ -616,15 +635,22 @@ static int jfs_unfreeze(struct super_block *sb)struct jfs_log *log = sbi->log;int rc =0 ; -if (!sb_rdonly (sb)) { +/* Undo what jfs_freeze() did, also if an error happened meanwhile. */ +if (test_bit (JFS_FLAGS_LOG_SHUTDOWN , &sbi->jfs_flags)) { rc =updateSuper (sb,FM_MOUNT );if (rc) {jfs_error (sb,"updateSuper failed\n" ); goto out; } +/* With nointegrity FM_MOUNT forgets an error while frozen. */ +if (sb_emergency_ro (sb)) +updateSuper (sb,FM_DIRTY ); rc =lmLogInit (log); -if (rc) +if (rc) {jfs_error (sb,"lmLogInit failed\n" ); + goto out; + } +clear_bit (JFS_FLAGS_LOG_SHUTDOWN , &sbi->jfs_flags); out:txResume (sb); }
fs/jfs/xattr.c +3 -0
@@ -936,6 +936,9 @@ static int __jfs_xattr_set(struct inode *inode, const char *name,tid_t tid;int rc; +if (sb_emergency_ro (inode->i_sb)) +return -EROFS ; + tid =txBegin (inode->i_sb,0 );mutex_lock (&ji->commit_mutex); rc =__jfs_setxattr (tid, inode, name, value, size, flags);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 07/33] jfs: don't set SB_RDONLY after filesystem errors
Sashiko has reviewed this patch and found no issues.
Quoted 18 lines
With errors=remount-ro, the jfs default,
jfs_handle_error()setsSB_RDONLYfrom whatever context hit the error and without holdings_umount. If that happens while the filesystem is frozenthaw_super()skipsjfs_unfreeze()and leaves thes_writersrwsems locked. Writers hang and umount shuts the log down a second time and crashes.So call
sb_set_emergency_ro()instead and let the VFS refuse modifications.jfs_unfreeze()usedsb_rdonly()to tell whetherjfs_freeze()shut the log down. Track that inJFS_FLAGS_LOG_SHUTDOWNinstead so thaw undoes exactly what freeze did.jfs refuses inode and block allocation and the xattr and ACL setters itself, before
txBegin()because failingtxCommit()with tlocks held trips an assert. Allocations writeback hasn't committed yet when the error hits leave anonymous tlocks behind. Sojfs_dirty_inode()still [ ... ]
before the error fail with EROFS.
Quoted 3 lines
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=7
Christian Brauner
maintainerPATCH v3 8/33 nilfs2: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerWith errors=remount-ro, the nilfs2 default, __nilfs_error() sets SB_RDONLY from whatever context hit the error and without holding s_umount. If that happens while the filesystem is frozen thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked so every writer hangs.
nilfs2 already treats a runtime SB_RDONLY as "degraded because of corruption" in a lot of places and that can stay. So call sb_set_emergency_ro() instead and use sb_rdonly_any() for every sb_rdonly() check that guards a write. FITRIM is refused and a remount to read-only doesn't write anything anymore. The VFS doesn't call nilfs_sync_fs() in that state.
nilfs2 explicitly supported "mount -o remount,rw" after an error so far. Now the state lasts until umount like on ext4. That includes a mount that reconfigures a superblock only snapshots kept alive. nilfs_get_tree() does that without reconfigure_super() so nilfs_reconfigure() refuses going read-write itself. Writes through file descriptors opened before the error fail with EROFS now instead of being dropped.
--- fs/nilfs2/inode.c | 4 ++-- fs/nilfs2/ioctl.c | 4 ++++ fs/nilfs2/mdt.c | 2 +- fs/nilfs2/page.c | 2 +- fs/nilfs2/segment.c | 13 ++++++------- fs/nilfs2/sufile.h | 2 +- fs/nilfs2/super.c | 25 ++++++++++++++++++------- 7 files changed, 33 insertions(+), 19 deletions(-)
fs/nilfs2/inode.c +2 -2
@@ -162,7 +162,7 @@ static int nilfs_writepages(struct address_space *mapping,struct inode *inode = mapping->host;int err =0 ; -if (sb_rdonly (inode->i_sb)) { +if (sb_rdonly_any (inode->i_sb)) {nilfs_clear_dirty_pages (mapping, false);return -EROFS ; }
@@ -848,7 +848,7 @@ void nilfs_evict_inode(struct inode *inode)truncate_inode_pages_final (&inode->i_data); nilfs = sb->s_fs_info; -if (unlikely (sb_rdonly (sb) || !nilfs->ns_writer)) { +if (unlikely (sb_rdonly_any (sb) || !nilfs->ns_writer)) {/* * If this inode is about to be disposed after the file system * has been degraded to read-only due to file system corruption
fs/nilfs2/ioctl.c +4 -0
@@ -1050,6 +1050,10 @@ static int nilfs_ioctl_trim_fs(struct inode *inode, void __user *argp)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +/* Don't trust the segment usage after an error. */ +if (sb_emergency_ro (inode->i_sb)) +return -EROFS ; +if (!bdev_max_discard_sectors (nilfs->ns_bdev))return -EOPNOTSUPP ;
fs/nilfs2/mdt.c +1 -1
@@ -400,7 +400,7 @@ static int nilfs_mdt_writeback(struct address_space *mapping,if (!inode)return 0 ; -if (sb_rdonly (inode->i_sb)) { +if (sb_rdonly_any (inode->i_sb)) {/* * It means that filesystem was remounted in read-only * mode because of error or metadata corruption. But we
fs/nilfs2/page.c +1 -1
@@ -259,7 +259,7 @@ int nilfs_copy_dirty_pages(struct address_space *dmap,folio_lock (folio);if (unlikely (!folio_test_dirty (folio))) { -if (WARN_ONCE (!sb_rdonly (smap_inode->i_sb), +if (WARN_ONCE (!sb_rdonly_any (smap_inode->i_sb),"inconsistent dirty state\n" )) goto unlock_folio;
fs/nilfs2/segment.c +6 -7
@@ -317,7 +317,7 @@ void nilfs_relax_pressure_in_lock(struct super_block *sb)struct the_nilfs *nilfs = sb->s_fs_info;struct nilfs_sc_info *sci = nilfs->ns_writer; -if (sb_rdonly (sb) ||unlikely (!sci) || !sci->sc_flush_request) +if (sb_rdonly_any (sb) ||unlikely (!sci) || !sci->sc_flush_request)return ;set_bit (NILFS_SC_PRIOR_FLUSH , &sci->sc_flags);
@@ -2099,7 +2099,7 @@ static int nilfs_segctor_do_construct(struct nilfs_sc_info *sci, int mode)struct the_nilfs *nilfs = sci->sc_super->s_fs_info;int err; -if (sb_rdonly (sci->sc_super)) +if (sb_rdonly_any (sci->sc_super))return -EROFS ;nilfs_sc_cstage_set (sci,NILFS_ST_INIT );
@@ -2323,7 +2323,7 @@ int nilfs_construct_segment(struct super_block *sb)struct nilfs_sc_info *sci = nilfs->ns_writer;struct nilfs_transaction_info *ti; -if (sb_rdonly (sb) ||unlikely (!sci)) +if (sb_rdonly_any (sb) ||unlikely (!sci))return -EROFS ;/* A call inside transactions causes a deadlock. */
@@ -2356,7 +2356,7 @@ int nilfs_construct_dsync_segment(struct super_block *sb, struct inode *inode,struct nilfs_transaction_info ti;int err =0 ; -if (sb_rdonly (sb) ||unlikely (!sci)) +if (sb_rdonly_any (sb) ||unlikely (!sci))return -EROFS ;nilfs_transaction_lock (sb, &ti,0 );
@@ -2870,9 +2870,8 @@ int nilfs_attach_log_writer(struct super_block *sb, struct nilfs_root *root)if (nilfs->ns_writer) {/* -* This happens if the filesystem is made read-only by -* __nilfs_error or nilfs_remount and then remounted -* read/write. In these cases, reuse the existing +* This happens if the filesystem is remounted read-only and +* then read/write again. In this case, reuse the existing * writer. */ return 0 ;
fs/nilfs2/sufile.h +1 -1
@@ -76,7 +76,7 @@ int nilfs_sufile_trim_fs(struct inode *sufile, struct fstrim_range *range);int _err = (err); \ \if (unlikely (_err)) \ - _err =WARN_ONCE (!sb_rdonly ((sufile)->i_sb), \ + _err =WARN_ONCE (!sb_rdonly_any ((sufile)->i_sb), \"unexpected sufile error %d\n" , _err) ? \ -EIO : -EROFS ; \ _err; \
fs/nilfs2/super.c +18 -7
@@ -137,12 +137,13 @@ void __nilfs_error(struct super_block *sb, const char *function,va_end (args); -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) {nilfs_set_error (sb);if (nilfs_test_opt (nilfs,ERRORS_RO )) {printk (KERN_CRIT "Remounting filesystem read-only\n" ); - sb->s_flags |=SB_RDONLY ; +/* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). */ +sb_set_emergency_ro (sb); } }
@@ -508,7 +509,7 @@ static void nilfs_put_super(struct super_block *sb)nilfs_detach_log_writer (sb); -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) {down_write (&nilfs->ns_sem);nilfs_cleanup_super (sb);up_write (&nilfs->ns_sem);
@@ -588,7 +589,7 @@ static int nilfs_freeze(struct super_block *sb)struct the_nilfs *nilfs = sb->s_fs_info;int err; -if (sb_rdonly (sb)) +if (sb_rdonly_any (sb))return 0 ;/* Mark super block clean */
@@ -602,7 +603,7 @@ static int nilfs_unfreeze(struct super_block *sb) {struct the_nilfs *nilfs = sb->s_fs_info; -if (sb_rdonly (sb)) +if (sb_rdonly_any (sb))return 0 ;down_write (&nilfs->ns_sem);
@@ -1083,7 +1084,7 @@ nilfs_fill_super(struct super_block *sb, struct fs_context *fc) goto failed_unload; } -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) { err =nilfs_attach_log_writer (sb, fsroot);if (err) goto failed_checkpoint;
@@ -1095,7 +1096,7 @@ nilfs_fill_super(struct super_block *sb, struct fs_context *fc)nilfs_put_root (fsroot); -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) {down_write (&nilfs->ns_sem);nilfs_setup_super (sb, true);up_write (&nilfs->ns_sem);
@@ -1141,6 +1142,10 @@ static int nilfs_reconfigure(struct fs_context *fc)if (fc->sb_flags &SB_RDONLY ) { sb->s_flags |=SB_RDONLY ; +/* After an error nothing may be written, just go read-only. */ +if (sb_emergency_ro (sb)) + goto out; +/* * Remounting a valid RW partition RDONLY, so set * the RDONLY flag and then mark the partition as valid again.
@@ -1152,6 +1157,12 @@ static int nilfs_reconfigure(struct fs_context *fc)__u64 features;struct nilfs_root *root; +/* The VFS doesn't refuse this when nilfs_get_tree() calls us. */ +if (sb_emergency_ro (sb)) { + err = -EROFS ; + goto ignore_opts; + } +/* * Mounting a RDONLY partition read-write, so reread and * store the current valid flag. (It may have been changed
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 08/33] nilfs2: don't set SB_RDONLY after filesystem errors
Sashiko has reviewed this patch and found no issues.
Quoted 21 lines
With errors=remount-ro, the nilfs2 default,
__nilfs_error()setsSB_RDONLYfrom whatever context hit the error and without holdings_umount. If that happens while the filesystem is frozenthaw_super()sees a read-only superblock and leaves thes_writersrwsems locked so every writer hangs.nilfs2 already treats a runtime
SB_RDONLYas "degraded because of corruption" in a lot of places and that can stay. So callsb_set_emergency_ro()instead and usesb_rdonly_any()for everysb_rdonly()check that guards a write. FITRIM is refused and a remount to read-only doesn't write anything anymore. The VFS doesn't callnilfs_sync_fs()in that state.nilfs2 explicitly supported "mount -o remount,rw" after an error so far. Now the state lasts until umount like on ext4. That includes a mount that reconfigures a superblock only snapshots kept alive.
nilfs_get_tree()does that withoutreconfigure_super()sonilfs_reconfigure()refuses going read-write itself. Writes through file descriptors opened before the error fail with EROFS now instead of being dropped.
Quoted 4 lines
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=8
Christian Brauner
maintainerPATCH v3 9/33 fat: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerWith errors=remount-ro, the default, __fat_fs_error() sets SB_RDONLY from whatever context hit the error and without holding s_umount. If that happens while the filesystem is frozen through bdev_freeze(), e.g. by a device mapper suspend, thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked so every writer hangs.
So call sb_set_emergency_ro() instead and let the VFS refuse modifications. fat still refuses FAT writes, cluster allocation, write_begin, O_DIRECT writes and FITRIM itself, leaves the volume marked dirty and doesn't free fallocated clusters on evict. fat_write_inode() keeps working. Writeback still writes the FAT and the directory blocks and rename and create leave the new entry with cluster 0 and size 0 until fat_write_inode() fills them in. Errors while mounting still set SB_RDONLY, nothing can freeze the superblock before SB_BORN.
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. A remount to read-only doesn't mark the volume clean. Writes through file descriptors opened before the error fail with EROFS.
--- fs/fat/dir.c | 3 +++ fs/fat/fatent.c | 11 ++++++++++- fs/fat/file.c | 4 ++++ fs/fat/inode.c | 13 +++++++++++-- fs/fat/misc.c | 19 +++++++++++++------ 5 files changed, 41 insertions(+), 9 deletions(-)
fs/fat/dir.c +3 -0
@@ -1329,6 +1329,9 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,loff_t pos;bool saw_eod; +if (unlikely (sb_emergency_ro (sb))) +return -EROFS ; + sinfo->nr_slots = nr_slots;/* First stage: search free directory entries */
fs/fat/fatent.c +10 -1
@@ -314,7 +314,7 @@ static void mark_fsinfo_dirty(struct super_block *sb) {struct msdos_sb_info *sbi =MSDOS_SB (sb); -if (sb_rdonly (sb) || !is_fat32 (sbi)) +if (sb_rdonly_any (sb) || !is_fat32 (sbi))return ;__mark_inode_dirty (sbi->fsinfo_inode,I_DIRTY_SYNC );
@@ -419,6 +419,9 @@ int fat_ent_write(struct inode *inode, struct fat_entry *fatent,const struct fatent_operations *ops =MSDOS_SB (sb)->fatent_ops;int err; +if (unlikely (sb_emergency_ro (sb))) +return -EROFS ; + ops->ent_put (fatent, new);if (wait) { err =fat_sync_bhs (fatent->bhs, fatent->nr_bhs);
@@ -513,6 +516,9 @@ static int __fat_alloc_clusters(struct inode *inode, int *cluster, int nr_clustestruct buffer_head *bhs[MAX_BUF_PER_PAGE ];int i, count, err, nr_bhs, idx_clus; +if (unlikely (sb_emergency_ro (sb))) +return -EROFS ; +lock_fat (sbi);if (sbi->free_clusters !=-1 && sbi->free_clus_valid && sbi->free_clusters < nr_cluster) {
@@ -620,6 +626,9 @@ int fat_free_clusters(struct inode *inode, int cluster)int i, err, nr_bhs;int first_cl = cluster, dirty_fsinfo =0 ; +if (unlikely (sb_emergency_ro (sb))) +return -EROFS ; + nr_bhs =0 ;fatent_init (&fatent);lock_fat (sbi);
fs/fat/file.c +4 -0
@@ -135,6 +135,10 @@ static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg)if (!capable (CAP_SYS_ADMIN ))return -EPERM ; +/* Don't trust the FAT after an error. */ +if (unlikely (sb_emergency_ro (sb))) +return -EROFS ; +if (!bdev_max_discard_sectors (sb->s_bdev))return -EOPNOTSUPP ;
fs/fat/inode.c +11 -2
@@ -228,6 +228,9 @@ static int fat_write_begin(const struct kiocb *iocb, {int err; +if (unlikely (sb_emergency_ro (mapping->host->i_sb))) +return -EROFS ; + err =cont_write_begin (iocb, mapping, pos, len, foliop, fsdata, fat_get_block, &MSDOS_I (mapping->host)->mmu_private);
@@ -264,6 +267,8 @@ static ssize_t fat_direct_IO(struct kiocb *iocb, struct iov_iter *iter)ssize_t ret;if (iov_iter_rw (iter) ==WRITE ) { +if (unlikely (sb_emergency_ro (inode->i_sb))) +return -EROFS ;/* * FIXME: blockdev_direct_IO() doesn't use ->write_begin(), * so we need to update the ->mmu_private to block boundary.
@@ -662,6 +667,10 @@ static int fat_sync_inode_metadata(struct inode *inode, static void fat_free_eofblocks(struct inode *inode) { + /* Nothing can be freed after an error. */ + if (sb_emergency_ro(inode->i_sb)) + return; + /* Release unwritten fallocated blocks on inode eviction. */ if ((inode->i_blocks << 9) > round_up(MSDOS_I(inode)->mmu_private,
@@ -710,8 +719,8 @@ static void fat_set_state(struct super_block *sb,struct fat_boot_sector *b;struct msdos_sb_info *sbi =MSDOS_SB (sb); -/* do not change any thing if mounted read only */ -if (sb_rdonly (sb) && !force) +/* do not change any thing if mounted read only or after an error */ +if ((sb_rdonly (sb) && !force) ||sb_emergency_ro (sb))return ;/* do not change state if fs was dirty */
fs/fat/misc.c +13 -6
@@ -14,9 +14,9 @@* fat_fs_error reports a file system problem that might indicate fa data * corruption/inconsistency. Depending on 'errors' mount option the * panic() is called, or error message is printed FAT and nothing is done, -* or filesystem is remounted read-only (default behavior). -* In case the file system is remounted read-only, it can be made writable -* again by remounting it. +* or the filesystem is made read-only (default behavior): a mount in +* progress comes up read-only, a mounted filesystem refuses all further +* modifications until it is unmounted. */ void __fat_fs_error (struct super_block *sb,int report,const char *fmt, ...) {
@@ -32,10 +32,17 @@ void __fat_fs_error(struct super_block *sb, int report, const char *fmt, ...)va_end (args); } -if (opts->errors ==FAT_ERRORS_PANIC ) +if (opts->errors ==FAT_ERRORS_PANIC ) {panic ("FAT-fs (%s): fs panic from previous error\n" , sb->s_id); -else if (opts->errors ==FAT_ERRORS_RO && !sb_rdonly (sb)) { - sb->s_flags |=SB_RDONLY ; + }else if (opts->errors ==FAT_ERRORS_RO && !sb_rdonly (sb)) { +/* +* Before SB_BORN the mounter holds s_umount so SB_RDONLY is +* fine. Later it needs a remount, see sb_set_emergency_ro(). +*/ +if (!(sb->s_flags &SB_BORN )) + sb->s_flags |=SB_RDONLY ; +else if (!sb_set_emergency_ro (sb)) +return ;fat_msg (sb,KERN_ERR ,"Filesystem has been set read-only" ); } }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 09/33] fat: don't set SB_RDONLY after filesystem errors
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] fat: fat_free() zeroes i_start on disk when fat_free_clusters() returns -EROFS - [Medium] fat…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] fat: fat_free() zeroes i_start on disk when fat_free_clusters() returns -EROFS
- [Medium] fat: directory modifications proceed after fat__get_entry() sets emergency RO
--
commit 7ce00aaef1e345a2ca01443ccff3c19884d9bcc3
Author: Christian Brauner <[email protected]>
fat: don't set SB_RDONLY after filesystem errors
This commit switches __fat_fs_error() from setting SB_RDONLY on a mounted superblock to calling sb_set_emergency_ro(), and adds sb_emergency_ro() checks across FAT write, allocation, and state paths.
Quoted 5 lines
fs/fat/dir.c +0
-0
fs/fat/dir.c int fat_add_entries(struct inode *dir, void *slots, int nr_slots, · line 1337
Quoted 9 lines
fs/fat/dir.c +3
-0@@ -1329,6 +1329,9 @@ int fat_add_entries(struct inode *dir, void *slots, int nr_slots,loff_t pos;bool saw_eod; +if (unlikely (sb_emergency_ro (sb))) +return -EROFS ; + sinfo->nr_slots = nr_slots;/* First stage: search free directory entries */
[Severity: Medium]
Can fat_add_entries() and fat_dir_empty() still modify directory buffers after a FAT error in fat__get_entry() sets SB_STATE_EMERGENCY_RO?
In fat__get_entry(), a fat_bmap() error calls fat_fs_error(), sets sb_emergency_ro(sb), and returns -1 just like normal EOF. In fat_add_entries(), if the fat_get_entry() loop hits a FAT error on a later cluster after collecting some free slots (0 < free_slots < nr_slots), it falls through to found:, writes the partial slots into bhs[], fails in fat_add_new_entries() -> fat_alloc_clusters() with -EROFS, and then overwrites the original slots (including 0x00 end-of-directory markers) with DELETED_FLAG in __fat_remove_entries().
Similarly, in fat_dir_empty(), a -1 return from fat_get_short_entry() after a FAT error makes a corrupted non-empty directory appear empty (return 0), and because neither fat_dir_empty() nor fat_remove_entries() checks sb_emergency_ro(sb), rmdir and rename proceed to delete the directory entry on disk after the filesystem has entered emergency read-only mode.
Quoted 4 lines
fs/fat/fatent.c +0
-0
[ ... ]
fs/fat/fatent.c int fat_free_clusters(struct inode *inode, int cluster) · line 634
Quoted 10 lines
fs/fat/fatent.c +3
-0@@ -620,6 +626,9 @@ int fat_free_clusters(struct inode *inode, int cluster)int i, err, nr_bhs;int first_cl = cluster, dirty_fsinfo =0 ; +if (unlikely (sb_emergency_ro (sb))) +return -EROFS ; + nr_bhs =0 ;fatent_init (&fatent);lock_fat (sbi);
[Severity: Medium]
Does returning -EROFS from fat_free_clusters() without guarding fat_free() cause fat_free() to zero i_start on disk while leaving the cluster chain allocated in the FAT?
When cont_write_begin() fails due to a FAT error that sets sb_emergency_ro(sb) (for example on a 0-byte file with fallocated clusters, or after allocating the first cluster of a multi-cluster write), fat_write_begin() calls fat_write_failed() -> fat_truncate_blocks(inode, 0) -> fat_free(inode, 0):
fs/fat/file.c:fat_free() {
...
if (!skip) {
MSDOS_I(inode)->i_start = 0;
MSDOS_I(inode)->i_logstart = 0;
}
MSDOS_I(inode)->i_attrs |= ATTR_ARCH;
fat_truncate_time(inode, NULL, FAT_UPDATE_CMTIME);
mark_inode_dirty(inode);
...
inode->i_blocks = skip << (MSDOS_SB(sb)->cluster_bits - 9);
/* Freeing the remained cluster chain */
return fat_free_clusters(inode, free_start);
}Because fat_free() clears i_start and marks the inode dirty before fat_free_clusters() returns -EROFS, and fat_write_inode() stays active in emergency read-only mode, writeback writes start = 0 to the directory entry on disk and orphans the allocated clusters in the FAT.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=9
Christian Brauner
maintainerPATCH v3 10/33 exfat: keep the current mount options on remount
In reply to Christian Braunerexfat_init_fs_context() only inherits uid, gid, fmask and dmask for a reconfigure. But exfat_reconfigure() refuses to change iocharset, keep_last_dots, sys_tz, time_offset and allow_utime. So every remount that doesn't repeat a non-default value for one of them fails with EINVAL, including emergency remount (sysrq u) which passes no options at all. And an option-less remount silently resets errors=, zero_size_dir and discard.
So start a reconfigure from the current options like gfs2 does. The iocharset string stays NULL in the new context because ->init_fs_context() runs without s_umount for mount(2) and a concurrent remount could free it. It's only compared if the remount names one. A shutdown turns discard off for good so exfat_reconfigure() keeps it off.
The next patch stops exfat from setting SB_RDONLY after errors. An errored exfat then stays read-write and emergency remount would run into this as well.
--- fs/exfat/super.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-)
fs/exfat/super.c +18 -13
@@ -797,14 +797,12 @@ static int exfat_reconfigure(struct fs_context *fc)mutex_unlock (&sbi->s_lock); } -if (new_opts->allow_utime == (unsigned short )-1 ) - new_opts->allow_utime = ~new_opts->fs_dmask &0022 ; -/* * Since the old settings of these mount options are cached in * inodes or dentries, they cannot be modified dynamically. */ - if (strcmp(new_opts->iocharset, cur_opts->iocharset) || + if ((new_opts->iocharset && + strcmp(new_opts->iocharset, cur_opts->iocharset)) || new_opts->keep_last_dots != cur_opts->keep_last_dots || new_opts->sys_tz != cur_opts->sys_tz || new_opts->time_offset != cur_opts->time_offset ||
@@ -815,6 +813,10 @@ static int exfat_reconfigure(struct fs_context *fc) new_opts->allow_utime != cur_opts->allow_utime)return -EINVAL ; +/* A shutdown turned discard off for good. */ +if (exfat_forced_shutdown (sb)) + new_opts->discard =0 ; +if (new_opts->discard != cur_opts->discard && new_opts->discard && !bdev_max_discard_sectors (sb->s_bdev)) {
@@ -823,6 +825,8 @@ static int exfat_reconfigure(struct fs_context *fc) }swap (*cur_opts, *new_opts); +/* The iocharset can't change, keep the current string. */ +swap (cur_opts->iocharset, new_opts->iocharset);return 0 ; }
@@ -849,23 +853,24 @@ static int exfat_init_fs_context(struct fs_context *fc)if (fc->purpose ==FS_CONTEXT_FOR_RECONFIGURE && fc->root) {struct super_block *sb = fc->root->d_sb; -struct exfat_mount_options *cur_opts = &EXFAT_SB (sb)->options; - sbi->options.fs_uid = cur_opts->fs_uid; - sbi->options.fs_gid = cur_opts->fs_gid; - sbi->options.fs_fmask = cur_opts->fs_fmask; - sbi->options.fs_dmask = cur_opts->fs_dmask; +/* +* Start from the current options so a remount only changes +* what it specifies. The iocharset string stays with the +* superblock, see exfat_reconfigure(). +*/ + sbi->options =EXFAT_SB (sb)->options; + sbi->options.iocharset =NULL ; }else { sbi->options.fs_uid =current_uid (); sbi->options.fs_gid =current_gid (); sbi->options.fs_fmask = current->fs->umask; sbi->options.fs_dmask = current->fs->umask; + sbi->options.allow_utime =-1 ; + sbi->options.errors =EXFAT_ERRORS_RO ; +exfat_set_iocharset (&sbi->options, exfat_default_iocharset); } - sbi->options.allow_utime =-1 ; - sbi->options.errors =EXFAT_ERRORS_RO ; -exfat_set_iocharset (&sbi->options, exfat_default_iocharset); - fc->s_fs_info = sbi; fc->ops = &exfat_context_ops;return 0 ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 10/33] exfat: keep the current mount options on remount
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines
exfat_init_fs_context()only inherits uid, gid, fmask and dmask for a reconfigure. Butexfat_reconfigure()refuses to change iocharset,keep_last_dots,sys_tz,time_offsetandallow_utime. So every remount that doesn't repeat a non-default value for one of them fails with EINVAL, including emergency remount (sysrq u) which passes no options at all. And an option-less remount silently resets errors=,zero_size_dirand discard.So start a reconfigure from the current options like gfs2 does. The iocharset string stays NULL in the new context because ->init_fs_context() runs without
s_umountfor mount(2) and a concurrent remount could free it. It's only compared if the remount names one. A shutdown turns discard off for good soexfat_reconfigure()keeps it off.
Quoted 7 lines
The next patch stops exfat from setting
SB_RDONLYafter errors. An errored exfat then stays read-write and emergency remount would run into this as well.Fixes e6fd5d3a4317 ("exfat: support modifying mount options via remount")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=10
Christian Brauner
maintainerPATCH v3 11/33 exfat: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerWith errors=remount-ro, the exfat default, __exfat_fs_error() sets SB_RDONLY without holding s_umount. If that happens while the filesystem is frozen through bdev_freeze(), e.g. by a device mapper suspend, thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked so every writer hangs.
exfat's shutdown state already stops modifications but it refuses reads as well. So add EXFAT_FLAGS_EMERGENCY_RO and exfat_emergency_state() which returns EIO after a shutdown and EROFS after an error like ext4 does, and set the new bit instead of SB_RDONLY. Errors while mounting still set SB_RDONLY, nothing can freeze the superblock before SB_BORN.
The write side shutdown checks use exfat_emergency_state() now. That includes ->writepages() and ->write_inode(), so dirty data from before the error is dropped like after a shutdown. Opening
and mmapping only check it for FMODE_WRITE so reads keep working. page_mkwrite, FS_IOC_SETFSLABEL and FITRIM get a check, the rename check moves up so it covers cross-directory moves too, and exfat_set_vol_flags() leaves the boot sector alone so the volume stays marked dirty.
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. A remount to read-only works, for non-default iocharset, time_offset, sys_tz, keep_last_dots or allow_utime that needs the previous patch. /proc/mounts keeps showing the filesystem read-write. Writes through file descriptors opened before the error fail with EROFS.
--- fs/exfat/exfat_fs.h | 19 +++++++++++++++++++ fs/exfat/file.c | 54 +++++++++++++++++++++++++++++++++++++++-------------- fs/exfat/inode.c | 11 +++++++---- fs/exfat/misc.c | 20 +++++++++++++++----- fs/exfat/namei.c | 27 ++++++++++++++++----------- fs/exfat/super.c | 10 ++++++++-- 6 files changed, 105 insertions(+), 36 deletions(-)
fs/exfat/exfat_fs.h +19 -0
@@ -123,6 +123,7 @@ enum {/* Superblock flags */ #define EXFAT_FLAGS_SHUTDOWN 1 +#define EXFAT_FLAGS_EMERGENCY_RO 2/* read-only after an error */ struct exfat_dentry_namebuf {char *lfn;
@@ -375,6 +376,24 @@ static inline int exfat_forced_shutdown(struct super_block *sb)return test_bit (EXFAT_FLAGS_SHUTDOWN , &EXFAT_SB (sb)->s_exfat_flags); } +static inline int exfat_emergency_ro (struct super_block *sb) +{ +return test_bit (EXFAT_FLAGS_EMERGENCY_RO , &EXFAT_SB (sb)->s_exfat_flags); +} + +/* +* Modifications are refused after a forced shutdown (-EIO) and after an error +* with errors=remount-ro (-EROFS). +*/ +static inline int exfat_emergency_state (struct super_block *sb) +{ +if (unlikely (exfat_forced_shutdown (sb))) +return -EIO ; +if (unlikely (exfat_emergency_ro (sb))) +return -EROFS ; +return 0 ; +} +/* * If ->i_mode can't hold 0222 (i.e. ATTR_RO), we use ->i_attrs to * save ATTR_RO instead of ->i_mode.
fs/exfat/file.c +40 -14
115 lines not on this page · raw message
fs/exfat/inode.c +7 -4
@@ -114,8 +114,9 @@ int exfat_write_inode(struct inode *inode, struct writeback_control *wbc) {int ret; -if (unlikely (exfat_forced_shutdown (inode->i_sb))) -return -EIO ; + ret =exfat_emergency_state (inode->i_sb); +if (unlikely (ret)) +return ret;mutex_lock (&EXFAT_SB (inode->i_sb)->s_lock); ret =__exfat_write_inode (inode, wbc->sync_mode ==WB_SYNC_ALL );
@@ -282,9 +283,11 @@ static int exfat_writepages(struct address_space *mapping, .wbc = wbc, .ops = &exfat_writeback_ops, }; +int ret; -if (unlikely (exfat_forced_shutdown (mapping->host->i_sb))) -return -EIO ; + ret =exfat_emergency_state (mapping->host->i_sb); +if (unlikely (ret)) +return ret;return iomap_writepages (&wpc); }
fs/exfat/misc.c +15 -5
@@ -19,13 +19,14 @@* exfat_fs_error reports a file system problem that might indicate fa data * corruption/inconsistency. Depending on 'errors' mount option the * panic() is called, or error message is printed FAT and nothing is done, -* or filesystem is remounted read-only (default behavior). -* In case the file system is remounted read-only, it can be made writable -* again by remounting it. +* or the filesystem is made read-only (default behavior): a mount in +* progress comes up read-only, a mounted filesystem refuses modifications +* until it is unmounted. */ void __exfat_fs_error (struct super_block *sb,int report,const char *fmt, ...) { -struct exfat_mount_options *opts = &EXFAT_SB (sb)->options; +struct exfat_sb_info *sbi =EXFAT_SB (sb); +struct exfat_mount_options *opts = &sbi->options;va_list args;struct va_format vaf;
@@ -41,7 +42,16 @@ void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)panic ("exFAT-fs (%s): fs panic from previous error\n" , sb->s_id); }else if (opts->errors ==EXFAT_ERRORS_RO && !sb_rdonly (sb)) { - sb->s_flags |=SB_RDONLY ; +/* +* Before SB_BORN the mounter holds s_umount so SB_RDONLY is +* fine. Later it needs a remount, set on a frozen superblock +* it makes thaw_super() leave s_writers locked. +*/ +if (!(sb->s_flags &SB_BORN )) + sb->s_flags |=SB_RDONLY ; +else if (test_and_set_bit (EXFAT_FLAGS_EMERGENCY_RO , + &sbi->s_exfat_flags)) +return ;exfat_err (sb,"Filesystem has been set read-only" ); } }
fs/exfat/namei.c +16 -11
@@ -562,8 +562,9 @@ static int exfat_create(const struct mnt_idmap *idmap, struct inode *dir,int err;loff_t size =i_size_read (dir); -if (unlikely (exfat_forced_shutdown (sb))) -return -EIO ; + err =exfat_emergency_state (sb); +if (unlikely (err)) +return err;mutex_lock (&EXFAT_SB (sb)->s_lock);exfat_set_volume_dirty (sb);
@@ -783,8 +784,9 @@ static int exfat_unlink(struct inode *dir, struct dentry *dentry)struct exfat_entry_set_cache es;int err =0 ; -if (unlikely (exfat_forced_shutdown (sb))) -return -EIO ; + err =exfat_emergency_state (sb); +if (unlikely (err)) +return err;mutex_lock (&EXFAT_SB (sb)->s_lock);if (ei->dir.dir ==DIR_DELETED ) {
@@ -836,8 +838,9 @@ static struct dentry *exfat_mkdir(const struct mnt_idmap *idmap, struct inode *dint err;loff_t size =i_size_read (dir); -if (unlikely (exfat_forced_shutdown (sb))) -return ERR_PTR (-EIO ); + err =exfat_emergency_state (sb); +if (unlikely (err)) +return ERR_PTR (err);mutex_lock (&EXFAT_SB (sb)->s_lock);exfat_set_volume_dirty (sb);
@@ -927,8 +930,9 @@ static int exfat_rmdir(struct inode *dir, struct dentry *dentry)struct exfat_entry_set_cache es;int err; -if (unlikely (exfat_forced_shutdown (sb))) -return -EIO ; + err =exfat_emergency_state (sb); +if (unlikely (err)) +return err;mutex_lock (&EXFAT_SB (inode->i_sb)->s_lock);
@@ -1011,9 +1015,6 @@ static int exfat_rename_file(struct inode *parent_inode,int sync =IS_DIRSYNC (parent_inode);unsigned int num_extra_entries, num_total_entries; -if (unlikely (exfat_forced_shutdown (sb))) -return -EIO ; - num_new_entries =exfat_calc_num_entries (p_uniname);if (num_new_entries <0 )return num_new_entries;
@@ -1283,6 +1284,10 @@ static int exfat_rename(const struct mnt_idmap *idmap,if (flags & ~RENAME_NOREPLACE )return -EINVAL ; + err =exfat_emergency_state (sb); +if (unlikely (err)) +return err; +mutex_lock (&EXFAT_SB (sb)->s_lock); old_inode = old_dentry->d_inode; new_inode = new_dentry->d_inode;
fs/exfat/super.c +8 -2
@@ -97,9 +97,10 @@ static int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flags) sbi->vol_flags = new_flags;/* skip updating volume dirty flag, -* if this volume has been mounted with read-only +* if this volume has been mounted with read-only, +* or an error left it read-only (it stays dirty then) */ -if (sb_rdonly (sb)) +if (sb_rdonly (sb) ||exfat_emergency_ro (sb))return 0 ; p_boot->vol_flags =cpu_to_le16 (new_flags);
@@ -788,6 +789,11 @@ static int exfat_reconfigure(struct fs_context *fc) fc->sb_flags |=SB_NODIRATIME ; +/* After an error the filesystem stays read-only until unmount. */ +if ((fc->sb_flags_mask &SB_RDONLY ) && !(fc->sb_flags &SB_RDONLY ) && +sb_rdonly (sb) &&exfat_emergency_ro (sb)) +return -EROFS ; +sync_filesystem (sb); if ((fc->sb_flags & (SB_FORCE | SB_RDONLY)) == SB_RDONLY &&
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 11/33] exfat: don't set SB_RDONLY after filesystem errors
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] exfat: exfat_page_mkwrite() checks emergency state and dirties before sb_start_pagefault() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] exfat: exfat_page_mkwrite() checks emergency state and dirties before sb_start_pagefault()
--
commit e1275d4c04928c834a6a34f6b3133c89c272c085
Author: Christian Brauner <[email protected]>
exfat: don't set SB_RDONLY after filesystem errors
This commit replaces setting SB_RDONLY on runtime filesystem errors with an EXFAT_FLAGS_EMERGENCY_RO flag and checks exfat_emergency_state() across write and modification paths so frozen filesystems can thaw cleanly.
Quoted 4 lines
fs/exfat/file.c +0
-0
[ ... ]
fs/exfat/file.c static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf) · line 997
Quoted 12 lines
fs/exfat/file.c +5
-0@@ -974,6 +987,11 @@ static vm_fault_t exfat_page_mkwrite(struct vm_fault *vmf)struct exfat_inode_info *ei =EXFAT_I (inode);vm_fault_t ret;loff_t new_valid_size, mmap_valid_size, fault_page_start; +int err; + + err =exfat_emergency_state (inode->i_sb); +if (unlikely (err)) +return vmf_fs_error (err);if (!inode_trylock (inode))return VM_FAULT_RETRY ;
[Severity: Medium]
Can a write fault that blocks in sb_start_pagefault() bypass exfat_emergency_state() if a filesystem error or shutdown happens while the filesystem is frozen?
CPU0 CPU1exfat_page_mkwrite()
exfat_emergency_state(sb) -> 0
inode_trylock(inode)
bdev_freeze()
...
__exfat_fs_error()
set_bit(EXFAT_FLAGS_EMERGENCY_RO, ...)
sb_start_pagefault(sb)
/* waits for thaw */
bdev_thaw() file_update_time(...)
iomap_page_mkwrite(...) <-- installs writable PTE after error
Because sb_start_pagefault() is called near the bottom of exfat_page_mkwrite(), after exfat_prepare_valid_range() and mark_inode_dirty(), a fault arriving on an already-frozen filesystem can also dirty gap folios and the inode while frozen and then sleep in sb_start_pagefault() while holding inode->i_rwsem:
fs/exfat/file.c:exfat_page_mkwrite() {
...
exfat_advance_zeroed_size(ei,
round_up(new_valid_size, i_blocksize(inode)));
exfat_set_valid_size(ei, new_valid_size);
mark_inode_dirty(inode);
}
sb_start_pagefault(inode->i_sb);
file_update_time(vmf->vma->vm_file);
filemap_invalidate_lock_shared(inode->i_mapping);
ret = iomap_page_mkwrite(vmf, &exfat_iomap_ops, NULL);
...
}Calling sb_start_pagefault() before inode_trylock() and checking exfat_emergency_state() after acquiring freeze protection would avoid both dirtying state while frozen and missing an error that occurred during the freeze.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=11
Christian Brauner
maintainerPATCH v3 12/33 exfat: use the generic emergency read-only state
In reply to Christian BraunerSet SB_STATE_EMERGENCY_RO instead of a private bit when an error makes the filesystem read-only. exfat_emergency_state() keeps telling a shutdown (EIO) and the emergency state (EROFS) apart so none of its callers change. The VFS refuses the remount to read-write now so exfat doesn't have to, and /proc/mounts shows the filesystem read-only again.
The VFS also refuses to freeze the superblock in that state. So FS_IOC_SHUTDOWN with EXFAT_GOING_DOWN_DEFAULT or _FULLSYNC shuts down without the freeze after an error, there's nothing left to sync anyway.
--- fs/exfat/exfat_fs.h | 13 ++----------- fs/exfat/misc.c | 6 ++---- fs/exfat/super.c | 12 ++++++------ 3 files changed, 10 insertions(+), 21 deletions(-)
fs/exfat/exfat_fs.h +2 -11
@@ -123,7 +123,6 @@ enum {/* Superblock flags */ #define EXFAT_FLAGS_SHUTDOWN 1 -#define EXFAT_FLAGS_EMERGENCY_RO 2/* read-only after an error */ struct exfat_dentry_namebuf {char *lfn;
@@ -376,20 +375,12 @@ static inline int exfat_forced_shutdown(struct super_block *sb)return test_bit (EXFAT_FLAGS_SHUTDOWN , &EXFAT_SB (sb)->s_exfat_flags); } -static inline int exfat_emergency_ro (struct super_block *sb) -{ -return test_bit (EXFAT_FLAGS_EMERGENCY_RO , &EXFAT_SB (sb)->s_exfat_flags); -} - -/* -* Modifications are refused after a forced shutdown (-EIO) and after an error -* with errors=remount-ro (-EROFS). -*/ +/* -EIO after a forced shutdown, -EROFS after an error with errors=remount-ro */ static inline int exfat_emergency_state (struct super_block *sb) {if (unlikely (exfat_forced_shutdown (sb)))return -EIO ; -if (unlikely (exfat_emergency_ro (sb))) +if (unlikely (sb_emergency_ro (sb)))return -EROFS ;return 0 ; }
fs/exfat/misc.c +2 -4
@@ -25,8 +25,7 @@*/ void __exfat_fs_error (struct super_block *sb,int report,const char *fmt, ...) { -struct exfat_sb_info *sbi =EXFAT_SB (sb); -struct exfat_mount_options *opts = &sbi->options; +struct exfat_mount_options *opts = &EXFAT_SB (sb)->options;va_list args;struct va_format vaf;
@@ -49,8 +48,7 @@ void __exfat_fs_error(struct super_block *sb, int report, const char *fmt, ...)*/ if (!(sb->s_flags &SB_BORN )) sb->s_flags |=SB_RDONLY ; -else if (test_and_set_bit (EXFAT_FLAGS_EMERGENCY_RO , - &sbi->s_exfat_flags)) +else if (!sb_set_emergency_ro (sb))return ;exfat_err (sb,"Filesystem has been set read-only" ); }
fs/exfat/super.c +6 -6
@@ -100,7 +100,7 @@ static int exfat_set_vol_flags(struct super_block *sb, unsigned short new_flags)* if this volume has been mounted with read-only, * or an error left it read-only (it stays dirty then) */ -if (sb_rdonly (sb) ||exfat_emergency_ro (sb)) +if (sb_rdonly_any (sb))return 0 ; p_boot->vol_flags =cpu_to_le16 (new_flags);
@@ -176,6 +176,11 @@ int exfat_force_shutdown(struct super_block *sb, u32 flags)if (exfat_forced_shutdown (sb))return 0 ; +/* After an error there is nothing to sync and freezing fails. */ +if ((flags ==EXFAT_GOING_DOWN_DEFAULT || + flags ==EXFAT_GOING_DOWN_FULLSYNC ) &&sb_emergency_ro (sb)) + flags =EXFAT_GOING_DOWN_NOSYNC ; +switch (flags) {case EXFAT_GOING_DOWN_DEFAULT :case EXFAT_GOING_DOWN_FULLSYNC :
@@ -789,11 +794,6 @@ static int exfat_reconfigure(struct fs_context *fc) fc->sb_flags |=SB_NODIRATIME ; -/* After an error the filesystem stays read-only until unmount. */ -if ((fc->sb_flags_mask &SB_RDONLY ) && !(fc->sb_flags &SB_RDONLY ) && -sb_rdonly (sb) &&exfat_emergency_ro (sb)) -return -EROFS ; -sync_filesystem (sb); if ((fc->sb_flags & (SB_FORCE | SB_RDONLY)) == SB_RDONLY &&
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 12/33] exfat: use the generic emergency read-only state
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] exfat: race with emergency read-only transition in exfat_force_shutdown() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] exfat: race with emergency read-only transition in exfat_force_shutdown()
--
commit 55555fc2a6a36c47e1e61fcbe8c22b69f7d5a5f8
Author: Christian Brauner <[email protected]>
exfat: use the generic emergency read-only state
This commit replaces EXFAT_FLAGS_EMERGENCY_RO with the VFS generic SB_STATE_EMERGENCY_RO state and skips freezing in exfat_force_shutdown() when the superblock is already in the emergency read-only state.
Quoted 4 lines
fs/exfat/super.c +0
-0
[ ... ]
fs/exfat/super.c int exfat_force_shutdown(struct super_block *sb, u32 flags) · line 186
Quoted 12 lines
fs/exfat/super.c +5
-0@@ -176,6 +176,11 @@ int exfat_force_shutdown(struct super_block *sb, u32 flags)if (exfat_forced_shutdown (sb))return 0 ; +/* After an error there is nothing to sync and freezing fails. */ +if ((flags ==EXFAT_GOING_DOWN_DEFAULT || + flags ==EXFAT_GOING_DOWN_FULLSYNC ) &&sb_emergency_ro (sb)) + flags =EXFAT_GOING_DOWN_NOSYNC ; +switch (flags) {case EXFAT_GOING_DOWN_DEFAULT :case EXFAT_GOING_DOWN_FULLSYNC :
[Severity: Medium]
Can exfat_force_shutdown() abort without setting EXFAT_FLAGS_SHUTDOWN if a filesystem error sets SB_STATE_EMERGENCY_RO while bdev_freeze() is running?
CPU0 CPU1exfat_force_shutdown()
sb_emergency_ro(sb) -> false
bdev_freeze(sb->s_bdev)
...
freeze_super(sb, ...)
sb_wait_write(sb, ...) /
sync_filesystem(sb)
__exfat_fs_error(sb, ...)
sb_set_emergency_ro(sb)
if (!ret && sb_emergency_ro(sb))
ret = -EROFS; if (ret)
return ret; <-- aborts without shutting down
Because freeze_super() checks sb_emergency_ro(sb) both on entry and after sync_filesystem(sb), an in-flight writer or writeback error during the freeze makes bdev_freeze() return -EROFS. exfat_force_shutdown() then returns -EROFS without setting EXFAT_FLAGS_SHUTDOWN or clearing opts->discard. Ignoring -EROFS from bdev_freeze() when sb_emergency_ro(sb) is set would still complete the shutdown.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=12
Christian Brauner
maintainerPATCH v3 13/33 hfs: don't set SB_RDONLY after MDB write errors
In reply to Christian BraunerSince commit fdbb95ff89b2 ("hfs: don't re-dirty MDB buffers after a write failure") hfs_mdb_commit() sets SB_RDONLY when a write of the MDB failed. It runs from the flush_mdb work and from ->sync_fs() and neither holds s_umount exclusively. freeze_super() decides from sb_rdonly() whether to take the s_writers rwsems and drops s_umount while it waits for writers. A sync(2) in that window sets SB_RDONLY, the freeze completes with the rwsems held and the thaw never releases them.
So record the condition in a new HFS_FLG_EMERGENCY_RO bit instead. Later calls of hfs_mdb_commit() return early like they did for sb_rdonly() and the MDB is never marked cleanly unmounted. hfs refuses modifications with EROFS itself: the namespace operations, setattr, the hfs.type and hfs.creator xattrs, opening for write, write_begin, O_DIRECT writes, block allocation and hfs_file_truncate(). Writing inodes back isn't refused because writeback still writes data and b-tree nodes.
B-tree growth isn't refused. A read can need a new b-tree node when it writes back a cached extent record, and failing that would make such reads fail with EIO until umount.
After a remount to read-only a remount to read-write fails with EROFS. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. A remount to read-only works and /proc/mounts keeps showing the filesystem read-write.
--- fs/hfs/attr.c | 3 +++ fs/hfs/dir.c | 12 ++++++++++++ fs/hfs/extent.c | 5 +++++ fs/hfs/hfs_fs.h | 11 +++++++++++ fs/hfs/inode.c | 16 ++++++++++++++++ fs/hfs/mdb.c | 16 ++++++++++------ fs/hfs/super.c | 5 ++++- 7 files changed, 61 insertions(+), 7 deletions(-)
fs/hfs/attr.c +3 -0
@@ -129,6 +129,9 @@ static int hfs_xattr_set(const struct xattr_handler *handler,if (!value)return -EOPNOTSUPP ; +if (unlikely (hfs_emergency_ro (inode->i_sb))) +return -EROFS ; +return __hfs_setxattr (inode, handler->flags, value, size, flags); }
fs/hfs/dir.c +12 -0
@@ -189,6 +189,9 @@ static int hfs_create(const struct mnt_idmap *idmap, struct inode *dir,struct inode *inode;int res; +if (unlikely (hfs_emergency_ro (dir->i_sb))) +return -EROFS ; + inode =hfs_new_inode (dir, &dentry->d_name, mode);if (IS_ERR (inode))return PTR_ERR (inode);
@@ -219,6 +222,9 @@ static struct dentry *hfs_mkdir(const struct mnt_idmap *idmap, struct inode *dirstruct inode *inode;int res; +if (unlikely (hfs_emergency_ro (dir->i_sb))) +return ERR_PTR (-EROFS ); + inode =hfs_new_inode (dir, &dentry->d_name, mode);if (IS_ERR (inode))return ERR_CAST (inode);
@@ -252,6 +258,9 @@ static int hfs_remove(struct inode *dir, struct dentry *dentry)struct inode *inode =d_inode (dentry);int res; +if (unlikely (hfs_emergency_ro (sb))) +return -EROFS ; +if (S_ISDIR (inode->i_mode) && inode->i_size !=2 )return -ENOTEMPTY ;
@@ -290,6 +299,9 @@ static int hfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,if (flags & ~RENAME_NOREPLACE )return -EINVAL ; +if (unlikely (hfs_emergency_ro (old_dir->i_sb))) +return -EROFS ; +/* Unlink destination if it already exists */ if (d_really_is_positive (new_dentry)) { res =hfs_remove (new_dir, new_dentry);
fs/hfs/extent.c +5 -0
@@ -356,6 +356,9 @@ int hfs_get_block(struct inode *inode, sector_t block,if (block >HFS_I (inode)->fs_blocks)return -EIO ;if (ablock >=HFS_I (inode)->alloc_blocks) { +/* No new allocations after an error. */ +if (unlikely (hfs_emergency_ro (sb))) +return -EROFS ; res =hfs_extend_file (inode);if (res)return res;
@@ -491,6 +494,8 @@ void hfs_file_truncate(struct inode *inode)hfs_dbg ("ino %llu, phys_size %llu -> i_size %llu\n" , inode->i_ino, (long long )HFS_I (inode)->phys_size, inode->i_size); +if (unlikely (hfs_emergency_ro (sb))) +return ;if (inode->i_size >HFS_I (inode)->phys_size) {struct address_space *mapping = inode->i_mapping;void *fsdata =NULL ;
fs/hfs/hfs_fs.h +11 -0
@@ -143,6 +143,7 @@ struct hfs_sb_info {#define HFS_FLG_BITMAP_DIRTY 0#define HFS_FLG_MDB_DIRTY 1#define HFS_FLG_ALT_MDB_DIRTY 2 +#define HFS_FLG_EMERGENCY_RO 3/* read-only after an MDB write error */ /* bitmap.c */ extern u32 hfs_vbm_search_free (struct super_block *sb,u32 goal,u32 *num_bits);
@@ -293,6 +294,16 @@ static inline const char *hfs_mdb_name(struct super_block *sb)return sb->s_id; } +/* +* Writing back the MDB failed and the filesystem stopped all modifications +* until unmount. SB_RDONLY isn't used for that because it must not change +* without s_umount. +*/ +static inline bool hfs_emergency_ro (struct super_block *sb) +{ +return test_bit (HFS_FLG_EMERGENCY_RO , &HFS_SB (sb)->flags); +} +static inline void hfs_bitmap_dirty (struct super_block *sb) {set_bit (HFS_FLG_BITMAP_DIRTY , &HFS_SB (sb)->flags);
fs/hfs/inode.c +16 -0
@@ -54,6 +54,9 @@ int hfs_write_begin(const struct kiocb *iocb, struct address_space *mapping,loff_t total_capacity;int ret; +if (unlikely (hfs_emergency_ro (inode->i_sb))) +return -EROFS ; + total_capacity = (loff_t )sbi->fs_ablocks * sbi->alloc_blksz;if (pos >= total_capacity)
@@ -139,6 +142,10 @@ static ssize_t hfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)size_t count =iov_iter_count (iter);ssize_t ret; +if (iov_iter_rw (iter) ==WRITE && +unlikely (hfs_emergency_ro (inode->i_sb))) +return -EROFS ; + ret =blockdev_direct_IO (iocb, inode, iter, hfs_get_block);/*
@@ -619,6 +626,8 @@ void hfs_evict_inode(struct inode *inode)static int hfs_file_open (struct inode *inode,struct file *file) { +if ((file->f_mode &FMODE_WRITE ) &&hfs_emergency_ro (inode->i_sb)) +return -EROFS ;if (HFS_IS_RSRC (inode)) inode =HFS_I (inode)->rsrc_inode;atomic_inc (&HFS_I (inode)->opencnt);
@@ -650,6 +659,9 @@ int hfs_inode_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,struct hfs_sb_info *hsb =HFS_SB (inode->i_sb);int error; +if (unlikely (hfs_emergency_ro (inode->i_sb))) +return -EROFS ; + error =setattr_prepare (&nop_mnt_idmap, dentry, attr);if (error)return error;
@@ -701,6 +713,10 @@ static int hfs_file_fsync(struct file *filp, loff_t start, loff_t end,struct super_block * sb;int ret, err; +/* Inodes aren't written back after an error. */ +if (unlikely (hfs_emergency_ro (inode->i_sb))) +return -EROFS ; + ret =file_write_and_wait_range (filp, start, end);if (ret)return ret;
fs/hfs/mdb.c +10 -6
@@ -322,8 +322,8 @@ int hfs_mdb_get(struct super_block *sb)* NONE * Returns: * 0 on success, -EIO if the MDB or alternate MDB buffer is no longer -* valid (e.g. after a prior write error), in which case the volume is -* remounted read-only. +* valid (e.g. after a prior write error), in which case the volume +* refuses all further modifications until it is unmounted. * Preconditions: * 'mdb' points to a "valid" (struct hfs_mdb). * Postconditions:
@@ -338,12 +338,16 @@ int hfs_mdb_commit(struct super_block *sb)struct hfs_mdb *mdb =HFS_SB (sb)->mdb;int ret =0 ; -if (sb_rdonly (sb)) +if (sb_rdonly (sb) ||hfs_emergency_ro (sb))return 0 ; +/* +* SB_RDONLY needs s_umount and a remount. Set on a frozen superblock +* it makes thaw_super() leave s_writers locked. +*/ if (!buffer_uptodate (HFS_SB (sb)->mdb_bh)) {pr_err ("primary MDB is corrupt, mounting read-only\n" ); - sb->s_flags |=SB_RDONLY ; +set_bit (HFS_FLG_EMERGENCY_RO , &HFS_SB (sb)->flags);return -EIO ; }
@@ -377,7 +381,7 @@ int hfs_mdb_commit(struct super_block *sb) HFS_SB(sb)->alt_mdb) {if (!buffer_uptodate (HFS_SB (sb)->alt_mdb_bh)) {pr_err ("alternate MDB is corrupt, mounting read-only\n" ); - sb->s_flags |=SB_RDONLY ; +set_bit (HFS_FLG_EMERGENCY_RO , &HFS_SB (sb)->flags); ret = -EIO ; goto out; }
@@ -428,7 +432,7 @@ int hfs_mdb_commit(struct super_block *sb)void hfs_mdb_close (struct super_block *sb) {/* update volume attributes */ -if (sb_rdonly (sb)) +if (sb_rdonly (sb) ||hfs_emergency_ro (sb))return ;if (!buffer_uptodate (HFS_SB (sb)->mdb_bh))
fs/hfs/super.c +4 -1
@@ -82,7 +82,7 @@ void hfs_mark_mdb_dirty(struct super_block *sb)struct hfs_sb_info *sbi =HFS_SB (sb);unsigned long delay; -if (sb_rdonly (sb)) +if (sb_rdonly (sb) ||hfs_emergency_ro (sb))return ;spin_lock (&sbi->work_lock);
@@ -131,6 +131,9 @@ static int hfs_reconfigure(struct fs_context *fc)return 0 ;if (!(fc->sb_flags &SB_RDONLY )) { +/* After an MDB write error it stays read-only until unmount. */ +if (hfs_emergency_ro (sb)) +return -EROFS ;if (!(HFS_SB (sb)->mdb->drAtrb &cpu_to_be16 (HFS_SB_ATTRIB_UNMNT ))) {pr_warn ("filesystem was not cleanly unmounted, running fsck.hfs is recommended. leaving read-only.\n" ); sb->s_flags |=SB_RDONLY ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 13/33] hfs: don't set SB_RDONLY after MDB write errors
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] hfs: early return in hfs_file_truncate() corrupts inode and catalog sizes --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] hfs: early return in hfs_file_truncate() corrupts inode and catalog sizes
--
commit bfa80e19c6c9ea6b12943abb2839781be7835d98
Author: Christian Brauner <[email protected]>
hfs: don't set SB_RDONLY after MDB write errors
Instead of setting SB_RDONLY without holding s_umount when an MDB write fails, this commit introduces HFS_FLG_EMERGENCY_RO and rejects filesystem modifications with -EROFS once the flag is set.
Quoted 4 lines
fs/hfs/extent.c +0
-0
[ ... ]
fs/hfs/extent.c void hfs_file_truncate(struct inode *inode) · line 501
Quoted 9 lines
fs/hfs/extent.c +2
-0@@ -491,6 +494,8 @@ void hfs_file_truncate(struct inode *inode)hfs_dbg ("ino %llu, phys_size %llu -> i_size %llu\n" , inode->i_ino, (long long )HFS_I (inode)->phys_size, inode->i_size); +if (unlikely (hfs_emergency_ro (sb))) +return ;if (inode->i_size >HFS_I (inode)->phys_size) {struct address_space *mapping = inode->i_mapping;void *fsdata =NULL ;
[Severity: Medium]
Can returning at the top of hfs_file_truncate() leave inode->i_size out of sync with HFS_I(inode)->phys_size and HFS_I(inode)->alloc_blocks?
CPU0 CPU1hfs_inode_setattr()
hfs_emergency_ro(sb) -> false
inode_dio_wait(inode)
hfs_mdb_commit()
set_bit(HFS_FLG_EMERGENCY_RO, ...) truncate_setsize(inode, ia_size)
hfs_file_truncate(inode)
if (hfs_emergency_ro(sb))
return; <-- skips i_size rollback
mark_inode_dirty(inode)
When an expanding truncate hits -EROFS in hfs_write_begin(), the inode->i_size > HFS_I(inode)->phys_size branch normally resets inode->i_size = HFS_I(inode)->phys_size. Returning before that branch leaves inode->i_size expanded without allocated blocks, and because hfs_write_inode() still runs after an MDB error, writeback persists LgLen > PyLen into the catalog B-tree record via hfs_inode_write_fork().
Similarly, if an extending write allocates blocks in hfs_get_block() and then fails a later block allocation after HFS_FLG_EMERGENCY_RO is set, hfs_write_failed() -> hfs_file_truncate() returns before the out: label resets phys_size, fs_blocks, and the inode byte count back to inode->i_size.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=13
Christian Brauner
maintainerPATCH v3 14/33 hfs: use the generic emergency read-only state
In reply to Christian BraunerSet SB_STATE_EMERGENCY_RO instead of a private bit and drop the checks the VFS does now. What's left is hfs_get_block(), hfs_file_truncate(), write_begin, O_DIRECT writes, the hfs.type and hfs.creator xattrs and the MDB commit, close and dirtying paths.
--- fs/hfs/attr.c | 2 +- fs/hfs/dir.c | 12 ------------ fs/hfs/extent.c | 4 ++-- fs/hfs/hfs_fs.h | 11 ----------- fs/hfs/inode.c | 13 ++----------- fs/hfs/mdb.c | 8 ++++---- fs/hfs/super.c | 5 +---- 7 files changed, 10 insertions(+), 45 deletions(-)
fs/hfs/attr.c +1 -1
@@ -129,7 +129,7 @@ static int hfs_xattr_set(const struct xattr_handler *handler,if (!value)return -EOPNOTSUPP ; -if (unlikely (hfs_emergency_ro (inode->i_sb))) +if (unlikely (sb_emergency_ro (inode->i_sb)))return -EROFS ;return __hfs_setxattr (inode, handler->flags, value, size, flags);
fs/hfs/dir.c +0 -12
@@ -189,9 +189,6 @@ static int hfs_create(const struct mnt_idmap *idmap, struct inode *dir,struct inode *inode;int res; -if (unlikely (hfs_emergency_ro (dir->i_sb))) -return -EROFS ; - inode =hfs_new_inode (dir, &dentry->d_name, mode);if (IS_ERR (inode))return PTR_ERR (inode);
@@ -222,9 +219,6 @@ static struct dentry *hfs_mkdir(const struct mnt_idmap *idmap, struct inode *dirstruct inode *inode;int res; -if (unlikely (hfs_emergency_ro (dir->i_sb))) -return ERR_PTR (-EROFS ); - inode =hfs_new_inode (dir, &dentry->d_name, mode);if (IS_ERR (inode))return ERR_CAST (inode);
@@ -258,9 +252,6 @@ static int hfs_remove(struct inode *dir, struct dentry *dentry)struct inode *inode =d_inode (dentry);int res; -if (unlikely (hfs_emergency_ro (sb))) -return -EROFS ; -if (S_ISDIR (inode->i_mode) && inode->i_size !=2 )return -ENOTEMPTY ;
@@ -299,9 +290,6 @@ static int hfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,if (flags & ~RENAME_NOREPLACE )return -EINVAL ; -if (unlikely (hfs_emergency_ro (old_dir->i_sb))) -return -EROFS ; -/* Unlink destination if it already exists */ if (d_really_is_positive (new_dentry)) { res =hfs_remove (new_dir, new_dentry);
fs/hfs/extent.c +2 -2
@@ -357,7 +357,7 @@ int hfs_get_block(struct inode *inode, sector_t block,return -EIO ;if (ablock >=HFS_I (inode)->alloc_blocks) {/* No new allocations after an error. */ -if (unlikely (hfs_emergency_ro (sb))) +if (unlikely (sb_emergency_ro (sb)))return -EROFS ; res =hfs_extend_file (inode);if (res)
@@ -494,7 +494,7 @@ void hfs_file_truncate(struct inode *inode)hfs_dbg ("ino %llu, phys_size %llu -> i_size %llu\n" , inode->i_ino, (long long )HFS_I (inode)->phys_size, inode->i_size); -if (unlikely (hfs_emergency_ro (sb))) +if (unlikely (sb_emergency_ro (sb)))return ;if (inode->i_size >HFS_I (inode)->phys_size) {struct address_space *mapping = inode->i_mapping;
fs/hfs/hfs_fs.h +0 -11
@@ -143,7 +143,6 @@ struct hfs_sb_info {#define HFS_FLG_BITMAP_DIRTY 0#define HFS_FLG_MDB_DIRTY 1#define HFS_FLG_ALT_MDB_DIRTY 2 -#define HFS_FLG_EMERGENCY_RO 3/* read-only after an MDB write error */ /* bitmap.c */ extern u32 hfs_vbm_search_free (struct super_block *sb,u32 goal,u32 *num_bits);
@@ -294,16 +293,6 @@ static inline const char *hfs_mdb_name(struct super_block *sb)return sb->s_id; } -/* -* Writing back the MDB failed and the filesystem stopped all modifications -* until unmount. SB_RDONLY isn't used for that because it must not change -* without s_umount. -*/ -static inline bool hfs_emergency_ro (struct super_block *sb) -{ -return test_bit (HFS_FLG_EMERGENCY_RO , &HFS_SB (sb)->flags); -} -static inline void hfs_bitmap_dirty (struct super_block *sb) {set_bit (HFS_FLG_BITMAP_DIRTY , &HFS_SB (sb)->flags);
fs/hfs/inode.c +2 -11
@@ -54,7 +54,7 @@ int hfs_write_begin(const struct kiocb *iocb, struct address_space *mapping,loff_t total_capacity;int ret; -if (unlikely (hfs_emergency_ro (inode->i_sb))) +if (unlikely (sb_emergency_ro (inode->i_sb)))return -EROFS ; total_capacity = (loff_t )sbi->fs_ablocks * sbi->alloc_blksz;
@@ -143,7 +143,7 @@ static ssize_t hfs_direct_IO(struct kiocb *iocb, struct iov_iter *iter)ssize_t ret;if (iov_iter_rw (iter) ==WRITE && -unlikely (hfs_emergency_ro (inode->i_sb))) +unlikely (sb_emergency_ro (inode->i_sb)))return -EROFS ; ret =blockdev_direct_IO (iocb, inode, iter, hfs_get_block);
@@ -626,8 +626,6 @@ void hfs_evict_inode(struct inode *inode)static int hfs_file_open (struct inode *inode,struct file *file) { -if ((file->f_mode &FMODE_WRITE ) &&hfs_emergency_ro (inode->i_sb)) -return -EROFS ;if (HFS_IS_RSRC (inode)) inode =HFS_I (inode)->rsrc_inode;atomic_inc (&HFS_I (inode)->opencnt);
@@ -659,9 +657,6 @@ int hfs_inode_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,struct hfs_sb_info *hsb =HFS_SB (inode->i_sb);int error; -if (unlikely (hfs_emergency_ro (inode->i_sb))) -return -EROFS ; - error =setattr_prepare (&nop_mnt_idmap, dentry, attr);if (error)return error;
@@ -713,10 +708,6 @@ static int hfs_file_fsync(struct file *filp, loff_t start, loff_t end,struct super_block * sb;int ret, err; -/* Inodes aren't written back after an error. */ -if (unlikely (hfs_emergency_ro (inode->i_sb))) -return -EROFS ; - ret =file_write_and_wait_range (filp, start, end);if (ret)return ret;
fs/hfs/mdb.c +4 -4
@@ -338,7 +338,7 @@ int hfs_mdb_commit(struct super_block *sb)struct hfs_mdb *mdb =HFS_SB (sb)->mdb;int ret =0 ; -if (sb_rdonly (sb) ||hfs_emergency_ro (sb)) +if (sb_rdonly_any (sb))return 0 ;/*
@@ -347,7 +347,7 @@ int hfs_mdb_commit(struct super_block *sb)*/ if (!buffer_uptodate (HFS_SB (sb)->mdb_bh)) {pr_err ("primary MDB is corrupt, mounting read-only\n" ); -set_bit (HFS_FLG_EMERGENCY_RO , &HFS_SB (sb)->flags); +sb_set_emergency_ro (sb);return -EIO ; }
@@ -381,7 +381,7 @@ int hfs_mdb_commit(struct super_block *sb) HFS_SB(sb)->alt_mdb) {if (!buffer_uptodate (HFS_SB (sb)->alt_mdb_bh)) {pr_err ("alternate MDB is corrupt, mounting read-only\n" ); -set_bit (HFS_FLG_EMERGENCY_RO , &HFS_SB (sb)->flags); +sb_set_emergency_ro (sb); ret = -EIO ; goto out; }
@@ -432,7 +432,7 @@ int hfs_mdb_commit(struct super_block *sb)void hfs_mdb_close (struct super_block *sb) {/* update volume attributes */ -if (sb_rdonly (sb) ||hfs_emergency_ro (sb)) +if (sb_rdonly_any (sb))return ;if (!buffer_uptodate (HFS_SB (sb)->mdb_bh))
fs/hfs/super.c +1 -4
@@ -82,7 +82,7 @@ void hfs_mark_mdb_dirty(struct super_block *sb)struct hfs_sb_info *sbi =HFS_SB (sb);unsigned long delay; -if (sb_rdonly (sb) ||hfs_emergency_ro (sb)) +if (sb_rdonly_any (sb))return ;spin_lock (&sbi->work_lock);
@@ -131,9 +131,6 @@ static int hfs_reconfigure(struct fs_context *fc)return 0 ;if (!(fc->sb_flags &SB_RDONLY )) { -/* After an MDB write error it stays read-only until unmount. */ -if (hfs_emergency_ro (sb)) -return -EROFS ;if (!(HFS_SB (sb)->mdb->drAtrb &cpu_to_be16 (HFS_SB_ATTRIB_UNMNT ))) {pr_warn ("filesystem was not cleanly unmounted, running fsck.hfs is recommended. leaving read-only.\n" ); sb->s_flags |=SB_RDONLY ;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 14/33] hfs: use the generic emergency read-only state
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
Set
SB_STATE_EMERGENCY_ROinstead of a private bit and drop the checks the VFS does now. What's left ishfs_get_block(),hfs_file_truncate(),write_begin,O_DIRECTwrites, the hfs.type and hfs.creator xattrs and the MDB commit, close and dirtying paths.Reviewed-by Viacheslav Dubeyko <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=14
Christian Brauner
maintainerPATCH v3 15/33 hfsplus: don't set SB_RDONLY when creating the attributes tree
In reply to Christian Braunerhfs_btree_open() sets SB_RDONLY when the map record of a b-tree doesn't mark the header node as used. That's fine while mounting. But hfsplus_create_attributes_file() calls it too when the first extended attribute is set on a filesystem that has no attributes file yet. That runs on the live filesystem without s_umount and a filesystem must not change SB_RDONLY behind the VFS' back.
hfsplus_create_attributes_file() opens a tree it has just created itself with the header node marked as used, so there's nothing to check there. Move the check into hfsplus_fill_super() and do it for every tree it opens. No functional changes.
--- fs/hfsplus/btree.c | 42 ++++++++++++++++++++++++++++-------------- fs/hfsplus/hfsplus_fs.h | 1 + fs/hfsplus/super.c | 5 +++++ 3 files changed, 34 insertions(+), 14 deletions(-)
fs/hfsplus/btree.c +28 -14
@@ -270,7 +270,6 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)struct hfs_btree *tree;struct hfs_btree_header_rec *head;struct address_space *mapping; -struct hfs_bnode *node;struct inode *inode;struct page *page;unsigned int size;
@@ -381,19 +380,6 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)kunmap_local (head);put_page (page); - node =hfs_bnode_find (tree,HFSPLUS_TREE_HEAD ); -if (IS_ERR (node)) - goto free_inode; - -if (!hfs_bmap_test_bit (node,0 )) { -pr_warn ("(%s): %s (cnid 0x%x) map record invalid or bitmap corruption detected, forcing read-only.\n" , - sb->s_id,hfs_btree_name (id), id); -pr_warn ("Run fsck.hfsplus to repair.\n" ); - sb->s_flags |=SB_RDONLY ; - } - -hfs_bnode_put (node); -return tree; fail_page:
@@ -407,6 +393,34 @@ struct hfs_btree *hfs_btree_open(struct super_block *sb, u32 id)return NULL ; } +/* +* Check the map record of a b-tree at mount. hfs_btree_open() can't do it +* because hfsplus_create_attributes_file() calls it on the live filesystem +* where SB_RDONLY must not be set. +*/ +int hfsplus_btree_check_map (struct hfs_btree *tree) +{ +struct super_block *sb = tree->sb; +struct hfs_bnode *node; + + node =hfs_bnode_find (tree,HFSPLUS_TREE_HEAD ); +if (IS_ERR (node)) { +pr_err ("failed to load %s header node\n" , +hfs_btree_name (tree->cnid)); +return PTR_ERR (node); + } + +if (!hfs_bmap_test_bit (node,0 )) { +pr_warn ("(%s): %s (cnid 0x%x) map record invalid or bitmap corruption detected, forcing read-only.\n" , + sb->s_id,hfs_btree_name (tree->cnid), tree->cnid); +pr_warn ("Run fsck.hfsplus to repair.\n" ); + sb->s_flags |=SB_RDONLY ; + } + +hfs_bnode_put (node); +return 0 ; +} +/* Release resources used by a btree */ void hfs_btree_close (struct hfs_btree *tree) {
fs/hfsplus/hfsplus_fs.h +1 -0
@@ -362,6 +362,7 @@ int hfsplus_block_free(struct super_block *sb, u32 offset, u32 count);u32 hfsplus_calc_btree_clump_size (u32 block_size,u32 node_size,u64 sectors,int file_id);struct hfs_btree *hfs_btree_open (struct super_block *sb,u32 id); +int hfsplus_btree_check_map (struct hfs_btree *tree);void hfs_btree_close (struct hfs_btree *tree);int hfs_btree_write (struct hfs_btree *tree);int hfs_bmap_reserve (struct hfs_btree *tree,u32 rsvd_nodes);
fs/hfsplus/super.c +5 -0
@@ -564,6 +564,11 @@ static int hfsplus_fill_super(struct super_block *sb, struct fs_context *fc) } sb->s_xattr = hfsplus_xattr_handlers; +if (hfsplus_btree_check_map (sbi->ext_tree) || +hfsplus_btree_check_map (sbi->cat_tree) || + (sbi->attr_tree &&hfsplus_btree_check_map (sbi->attr_tree))) + goto out_close_attr_tree; + inode =hfsplus_iget (sb,HFSPLUS_ALLOC_CNID );if (IS_ERR (inode)) {pr_err ("failed to load allocation file\n" );
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 15/33] hfsplus: don't set SB_RDONLY when creating the attributes tree
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
hfs_btree_open()setsSB_RDONLYwhen the map record of a b-tree doesn't mark the header node as used. That's fine while mounting. Buthfsplus_create_attributes_file()calls it too when the first extended attribute is set on a filesystem that has no attributes file yet. That runs on the live filesystem withouts_umountand a filesystem must not changeSB_RDONLYbehind the VFS' back.
Quoted 8 lines
hfsplus_create_attributes_file()opens a tree it has just created itself with the header node marked as used, so there's nothing to check there. Move the check intohfsplus_fill_super()and do it for every tree it opens. No functional changes.Suggested-by Viacheslav Dubeyko <[email protected]>Reviewed-by Viacheslav Dubeyko <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=15
Christian Brauner
maintainerPATCH v3 16/33 affs: don't set SB_RDONLY after filesystem errors
In reply to Christian Brauneraffs_error() sets SB_RDONLY from whatever context hit the error and without holding s_umount. If that happens while the filesystem is frozen through bdev_freeze(), e.g. by a device mapper suspend, thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked so every writer hangs.
So call sb_set_emergency_ro() instead and let the VFS refuse modifications. affs_commit_super() leaves the root block alone in that state. affs_write_inode() keeps working because writeback still writes the data and the bitmap and extension blocks the inode points at.
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. A remount to read-only works and keeps the bitmap around because evict and the last close still free blocks, affs_kill_sb() frees it. Writes through file descriptors opened before the error fail with EROFS.
--- fs/affs/amigaffs.c | 4 ++-- fs/affs/super.c | 14 ++++++++++---- 2 files changed, 12 insertions(+), 6 deletions(-)
fs/affs/amigaffs.c +2 -2
@@ -483,9 +483,9 @@ affs_error(struct super_block *sb, const char *function, const char *fmt, ...) vaf.fmt = fmt; vaf.va = &args;pr_crit ("error (device %s): %s(): %pV\n" , sb->s_id, function, &vaf); -if (!sb_rdonly (sb)) +/* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). */ +if (!sb_rdonly (sb) &&sb_set_emergency_ro (sb))pr_warn ("Remounting filesystem read-only\n" ); - sb->s_flags |=SB_RDONLY ;va_end (args); }
fs/affs/super.c +10 -4
@@ -36,6 +36,9 @@ affs_commit_super(struct super_block *sb, int wait)struct buffer_head *bh = sbi->s_root_bh;struct affs_root_tail *tail =AFFS_ROOT_TAIL (sb, bh); +if (sb_emergency_ro (sb)) +return ; +lock_buffer (bh);affs_secs_to_datestamp (ktime_get_real_seconds (), &tail->disk_change);affs_fix_checksum (sb, bh);
@@ -82,7 +85,7 @@ void affs_mark_sb_dirty(struct super_block *sb)struct affs_sb_info *sbi =AFFS_SB (sb);unsigned long delay; -if (sb_rdonly (sb)) +if (sb_rdonly_any (sb))return ;spin_lock (&sbi->work_lock);
@@ -550,10 +553,13 @@ static int affs_reconfigure(struct fs_context *fc)if ((bool )(fc->sb_flags &SB_RDONLY ) ==sb_rdonly (sb))return 0 ; -if (fc->sb_flags &SB_RDONLY ) -affs_free_bitmap (sb); -else +if (fc->sb_flags &SB_RDONLY ) { +/* After an error evict still frees blocks, keep the bitmap. */ +if (!sb_emergency_ro (sb)) +affs_free_bitmap (sb); + }else { res =affs_init_bitmap (sb, &fc->sb_flags); + }return res; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 16/33] affs: don't set SB_RDONLY after filesystem errors
Sashiko has reviewed this patch and found no issues.
Quoted 11 lines
affs_error()setsSB_RDONLYfrom whatever context hit the error and without holdings_umount. If that happens while the filesystem is frozen throughbdev_freeze(), e.g. by a device mapper suspend,thaw_super()sees a read-only superblock and leaves thes_writersrwsems locked so every writer hangs.So call
sb_set_emergency_ro()instead and let the VFS refuse modifications.affs_commit_super()leaves the root block alone in that state.affs_write_inode()keeps working because writeback still writes the data and the bitmap and extension blocks the inode points at.
Quoted 8 lines
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. A remount to read-only works and keeps the bitmap around because evict and the last close still free blocks,
affs_kill_sb()frees it. Writes through file descriptors opened before the error fail with EROFS.Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=16
Christian Brauner
maintainerPATCH v3 17/33 ufs: don't set SB_RDONLY after filesystem errors
In reply to Christian Braunerufs_error() sets SB_RDONLY on every error, whatever onerror= says, and ufs_panic() does too, both without holding s_umount. If that happens while the filesystem is frozen through bdev_freeze() thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked so every writer hangs. It also leaked the cylinder group info because ufs_put_super() doesn't free it for a read-only superblock, and a remount to read-write read it in again over the old copy.
So call sb_set_emergency_ro() instead and let the VFS refuse modifications. ufs_error() and ufs_panic() still mark the superblock UFS_FSBAD but don't queue ufs_sync_fs() anymore which does nothing in that state. ufs_put_cylinder() doesn't store the allocation rotors. ufs_write_inode() keeps working because writeback still writes the data and the blocks the inode points at.
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. A remount to read-only works and keeps the cylinder group info because evicting inodes that were unlinked while open still loads cylinder groups. Writes through file descriptors opened before the error fail with EROFS.
--- fs/ufs/cylinder.c | 12 +++++++----- fs/ufs/super.c | 56 ++++++++++++++++++++++++++++++++++++++----------------- 2 files changed, 46 insertions(+), 22 deletions(-)
fs/ufs/cylinder.c +7 -5
@@ -117,12 +117,14 @@ void ufs_put_cylinder (struct super_block * sb, unsigned bitmap_nr) }/* * rotor is not so important data, so we put it to disk -* at the end of working with cylinder +* at the end of working with cylinder, but not after an error */ - ucg->cg_rotor =cpu_to_fs32 (sb, ucpi->c_rotor); - ucg->cg_frotor =cpu_to_fs32 (sb, ucpi->c_frotor); - ucg->cg_irotor =cpu_to_fs32 (sb, ucpi->c_irotor); -ubh_mark_buffer_dirty (UCPI_UBH (ucpi)); +if (!sb_emergency_ro (sb)) { + ucg->cg_rotor =cpu_to_fs32 (sb, ucpi->c_rotor); + ucg->cg_frotor =cpu_to_fs32 (sb, ucpi->c_frotor); + ucg->cg_irotor =cpu_to_fs32 (sb, ucpi->c_irotor); +ubh_mark_buffer_dirty (UCPI_UBH (ucpi)); + }for (i =1 ; i <UCPI_UBH (ucpi)->count; i++) {brelse (UCPI_UBH (ucpi)->bh[i]); }
fs/ufs/super.c +39 -17
133 lines not on this page · raw message
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 17/33] ufs: don't set SB_RDONLY after filesystem errors
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] ufs: cylinder group leak and SB_RDONLY clear in ufs_reconfigure() --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] ufs: cylinder group leak and SB_RDONLY clear in ufs_reconfigure()
--
commit eaf412f5390fa6f1c30001040b118660141a9bd6
Author: Christian Brauner <[email protected]>
ufs: don't set SB_RDONLY after filesystem errors
This commit switches ufs_error() and ufs_panic() from setting SB_RDONLY to calling sb_set_emergency_ro(), and keeps cylinder group structures allocated across read-only remounts after an error so inode eviction can still use them.
Quoted 4 lines
fs/ufs/super.c +0
-0
[ ... ]
Quoted 19 lines
fs/ufs/super.c +7
-8@@ -1265,14 +1281,20 @@ static int ufs_reconfigure(struct fs_context *fc)* fs was mouted as rw, remounting ro */ if (fc->sb_flags &SB_RDONLY ) { -ufs_put_super_internal (sb); - usb1->fs_time =ufs_get_seconds (sb); -if ((flags &UFS_ST_MASK ) ==UFS_ST_SUN - || (flags &UFS_ST_MASK ) ==UFS_ST_SUNOS - || (flags &UFS_ST_MASK ) == UFS_ST_SUNx86) -ufs_set_fs_state (sb, usb1, usb3, -UFS_FSOK -fs32_to_cpu (sb, usb1->fs_time)); -ubh_mark_buffer_dirty (USPI_UBH (uspi)); +/* +* After an error leave the superblock alone and keep the +* cylinder groups, evict still needs them. +*/ +if (!sb_emergency_ro (sb)) { +ufs_put_super_internal (sb); + usb1->fs_time =ufs_get_seconds (sb);
fs/ufs/super.c static int ufs_reconfigure(struct fs_context *fc) · line 1300
Quoted 10 lines
fs/ufs/super.c +7
-0+if ((flags &UFS_ST_MASK ) ==UFS_ST_SUN + || (flags &UFS_ST_MASK ) ==UFS_ST_SUNOS + || (flags &UFS_ST_MASK ) == UFS_ST_SUNx86) +ufs_set_fs_state (sb, usb1, usb3, +UFS_FSOK -fs32_to_cpu (sb, usb1->fs_time)); +ubh_mark_buffer_dirty (USPI_UBH (uspi)); + } sb->s_flags |=SB_RDONLY ; }else {/*
[Severity: Medium]
Can reconfiguring via fspick() after an error and a read-only remount leak the retained cylinder group structures and clear SB_RDONLY?
When a read-write mount hits an error and is then remounted read-only, this branch skips ufs_put_super_internal() so sbi->s_csp, sbi->s_ucg, and sbi->s_ucpi stay allocated while SB_RDONLY is set on sb->s_flags.
If userspace then reconfigures the mount via fspick() and fsconfig() without specifying ro or rw (for example, changing onerror=), fc->sb_flags and fc->sb_flags_mask are both 0. reconfigure_super() only rejects sb_emergency_ro(sb) when SB_RDONLY is in fc->sb_flags_mask, and ufs_reconfigure() checks (bool)(fc->sb_flags & SB_RDONLY) == sb_rdonly(sb) without consulting fc->sb_flags_mask. Execution then falls into the else branch, where ufs_read_cylinder_structures() overwrites the existing sbi->s_csp, sbi->s_ucg, and sbi->s_ucpi pointers (leaking memory and buffer_heads) and clears SB_RDONLY from sb->s_flags.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=17
Christian Brauner
maintainerPATCH v3 18/33 hpfs: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerWith errors=remount-ro, the hpfs default, hpfs_error() sets SB_RDONLY on the first error without holding s_umount. If that happens while the filesystem is frozen through bdev_freeze() thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked so every writer hangs.
hpfs_error() is also called from hpfs_fill_super() where nothing can freeze the superblock yet, so keep setting SB_RDONLY there. After that call sb_set_emergency_ro() instead and let the VFS refuse modifications. FITRIM is refused and unmark_dirty() does nothing in that state so the next mount reports the volume as improperly stopped. hpfs_map_dnode() and hpfs_lookup() treat the state like read-only so lookups behave as before.
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. A remount to read-only doesn't write the spare block. Writes through file descriptors opened before the error fail with EROFS.
--- fs/hpfs/alloc.c | 4 ++-- fs/hpfs/dir.c | 2 +- fs/hpfs/map.c | 3 ++- fs/hpfs/super.c | 11 +++++++++-- 4 files changed, 14 insertions(+), 6 deletions(-)
fs/hpfs/alloc.c +2 -2
@@ -539,7 +539,7 @@ int hpfs_trim_fs(struct super_block *s, u64 start, u64 end, u64 minlen, unsignedreturn 0 ;if (start < sbi->sb_dirband_start + sbi->sb_dirband_size && end > sbi->sb_dirband_start) {hpfs_lock (s); -if (sb_rdonly (s)) { +if (sb_rdonly_any (s)) { err = -EROFS ; goto unlock_1; }
@@ -560,7 +560,7 @@ int hpfs_trim_fs(struct super_block *s, u64 start, u64 end, u64 minlen, unsigned end_bmp = (end +0x3fff ) >>14 ;while (start_bmp < end_bmp && !err) {hpfs_lock (s); -if (sb_rdonly (s)) { +if (sb_rdonly_any (s)) { err = -EROFS ; goto unlock_2; }
fs/hpfs/dir.c +1 -1
@@ -265,7 +265,7 @@ struct dentry *hpfs_lookup(struct inode *dir, struct dentry *dentry, unsigned in hpfs_result =hpfs_i (result);if (!de->directory) hpfs_result->i_parent_dir = dir->i_ino; -if (de->has_acl || de->has_xtd_perm)if (!sb_rdonly (dir->i_sb)) { +if ((de->has_acl || de->has_xtd_perm) && !sb_rdonly_any (dir->i_sb)) {hpfs_error (result->i_sb,"ACLs or XPERM found. This is probably HPFS386. This driver doesn't support it now. Send me some info on these structures" );iput (result); result =ERR_PTR (-EINVAL );
fs/hpfs/map.c +2 -1
@@ -288,7 +288,8 @@ struct dnode *hpfs_map_dnode(struct super_block *s, unsigned secno, goto bail; }if (((31 + de->namelen + de->down*4 +3 ) & ~3 ) !=le16_to_cpu (de->length)) { -if (((31 + de->namelen + de->down*4 +3 ) & ~3 ) <le16_to_cpu (de->length) && s->s_flags &SB_RDONLY ) goto ok; +if (((31 + de->namelen + de->down*4 +3 ) & ~3 ) <le16_to_cpu (de->length) && +sb_rdonly_any (s)) goto ok;hpfs_error (s,"namelen does not match dirent size in dnode %08x, dirent %03x, last %03x" , secno, p, pp); goto bail; }
fs/hpfs/super.c +9 -2
@@ -44,7 +44,7 @@ static void unmark_dirty(struct super_block *s) {struct buffer_head *bh;struct hpfs_spare_block *sb; -if (sb_rdonly (s))return ; +if (sb_rdonly_any (s))return ;sync_blockdev (s->s_bdev);if ((sb =hpfs_map_sector (s,17 , &bh,0 ))) { sb->dirty =hpfs_sb (s)->sb_chkdsk >1 -hpfs_sb (s)->sb_was_error;
@@ -81,7 +81,14 @@ void hpfs_error(struct super_block *s, const char *fmt, ...)else {pr_cont ("; remounting read-only\n" );mark_dirty (s,0 ); - s->s_flags |=SB_RDONLY ; +/* +* Before SB_BORN the mounter holds s_umount, +* later see sb_set_emergency_ro(). +*/ +if (s->s_flags &SB_BORN ) +sb_set_emergency_ro (s); +else + s->s_flags |=SB_RDONLY ; } }else if (sb_rdonly (s))pr_cont ("; going on - but anything won't be destroyed because it's read-only\n" );
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 18/33] hpfs: don't set SB_RDONLY after filesystem errors
Sashiko has reviewed this patch and found no issues.
Quoted 13 lines
With errors=remount-ro, the hpfs default,
hpfs_error()setsSB_RDONLYon the first error without holdings_umount. If that happens while the filesystem is frozen throughbdev_freeze()thaw_super()sees a read-only superblock and leaves thes_writersrwsems locked so every writer hangs.
hpfs_error()is also called fromhpfs_fill_super()where nothing can freeze the superblock yet, so keep settingSB_RDONLYthere. After that callsb_set_emergency_ro()instead and let the VFS refuse modifications. FITRIM is refused andunmark_dirty()does nothing in that state so the next mount reports the volume as improperly stopped.hpfs_map_dnode()andhpfs_lookup()treat the state like read-only so lookups behave as before.
Quoted 7 lines
After a remount to read-only a remount to read-write fails until umount. A plain remount to read-write right after the error succeeds but the filesystem stays read-only. A remount to read-only doesn't write the spare block. Writes through file descriptors opened before the error fail with EROFS.
Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=18
Christian Brauner
maintainerPATCH v3 19/33 ntfs: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerWith errors=remount-ro every ntfs_error() ends up in ntfs_handle_error() which sets SB_RDONLY without holding s_umount. If that happens while the volume is frozen through bdev_freeze() thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked so every writer hangs.
So set NV_Errors and a new NV_EmergencyRO flag instead like ext4 does. ntfs_emergency_state() returns EIO after a shutdown and EROFS after an error and the write side shutdown checks use it now. fallocate, ->set_acl(), FS_IOC_SETFSLABEL, FITRIM and ntfs_mft_record_free() get a check as well. Errors while mounting still set SB_RDONLY, nothing can freeze the superblock before SB_BORN.
NV_Errors already refuses a remount to read-write and since commit 521978c4f444 ("ntfs: sync the volume dirty bit with the recorded error state") the volume stays marked dirty once errors were recorded. But the dirty bit may not be on disk yet when the error hits and writeback is refused from then on, dirty data from before the error is dropped like after a shutdown. So a work item writes it right away because ntfs_error() can be called with mft records locked, and umount lets a pending one run first.
Unlinked inodes are left to chkdsk in that state. Their mft records are freed with direct bios while the index and bitmap updates of the deletion can't be written back anymore. And writing to an encrypted file only warns now. It called ntfs_error() which let anyone put the volume into the emergency state.
/proc/mounts keeps showing the volume read-write. Writes through file descriptors opened before the error fail with EROFS.
--- fs/ntfs/aops.c | 5 ++-- fs/ntfs/dir.c | 5 ++-- fs/ntfs/ea.c | 10 ++++++-- fs/ntfs/file.c | 63 ++++++++++++++++++++++++++++++++++++-------------- fs/ntfs/inode.c | 18 +++++++++++---- fs/ntfs/iomap.c | 6 +++-- fs/ntfs/mft.c | 10 ++++++-- fs/ntfs/named_stream.c | 43 +++++++++++++++++----------------- fs/ntfs/namei.c | 40 +++++++++++++++++++------------- fs/ntfs/super.c | 52 ++++++++++++++++++++++++++++++++++++----- fs/ntfs/volume.h | 16 +++++++++++++ 11 files changed, 194 insertions(+), 74 deletions(-)
fs/ntfs/aops.c +3 -2
@@ -263,8 +263,9 @@ static int ntfs_writepages(struct address_space *mapping,bool need_iput = false;int ret; -if (NVolShutdown (ni->vol)) -return -EIO ; + ret =ntfs_emergency_state (ni->vol); +if (ret) +return ret;if (!NInoNonResident (ni))return 0 ;
fs/ntfs/dir.c +3 -2
@@ -1131,8 +1131,9 @@ static int ntfs_dir_fsync(struct file *filp, loff_t start, loff_t end,ntfs_debug ("Entering for inode 0x%llx." , ni->mft_no); -if (NVolShutdown (vol)) -return -EIO ; + err =ntfs_emergency_state (vol); +if (err) +return err; ctx =ntfs_attr_get_search_ctx (ni,NULL );if (!ctx)
fs/ntfs/ea.c +8 -2
@@ -907,8 +907,9 @@ static int ntfs_setxattr(const struct xattr_handler *handler,if (ntfs_inode_is_named_stream (ni))return -EOPNOTSUPP ; -if (NVolShutdown (ni->vol)) -return -EIO ; + err =ntfs_emergency_state (ni->vol); +if (err) +return err;/* Only fileattr_set may change the flags and their cached state. */ if (!strcmp (name,"$LINUXFLAGS" ))
@@ -1193,6 +1194,11 @@ static noinline int ntfs_set_acl_ex(const struct mnt_idmap *idmap,int ntfs_set_acl (const struct mnt_idmap *idmap,struct dentry *dentry,struct posix_acl *acl,int type) { +int err; + + err =ntfs_emergency_state (NTFS_I (d_inode (dentry))->vol); +if (err) +return err;return ntfs_set_acl_ex (idmap,d_inode (dentry), acl, type, false); }
fs/ntfs/file.c +46 -17
130 lines not on this page · raw message
fs/ntfs/inode.c +14 -4
@@ -425,7 +425,8 @@ int ntfs_drop_big_inode(struct inode *inode)if (!inode->i_nlink) {struct ntfs_inode *ni =NTFS_I (inode); -if (ni->data_size ==0 ) +/* Like ntfs_evict_big_inode(), leave it to chkdsk. */ +if (ni->data_size ==0 ||ntfs_emergency_state (ni->vol))return 0 ;/* To avoid evict_inode call simultaneously */
@@ -2434,7 +2435,11 @@ void ntfs_evict_big_inode(struct inode *vi)truncate_inode_pages_final (&vi->i_data); -if (!vi->i_nlink) { +/* +* After an error the freed mft record would go out right away while +* the rest of the deletion can't, leave the inode to chkdsk. +*/ +if (!vi->i_nlink && !ntfs_emergency_state (ni->vol)) {if (!NInoAttr (ni)) {/* Never called with extent inodes */ WARN_ON (ni->nr_extents ==-1 );
@@ -2907,8 +2912,13 @@ int __ntfs_write_inode(struct inode *vi, int sync)ntfs_debug ("Entering for %sinode 0x%llx." ,NInoAttr (ni) ?"attr " :"" , ni->mft_no); -if (NVolShutdown (ni->vol)) -return -EIO ; +/* After an error only $Volume is still written, see ntfs_error_work(). */ + err =ntfs_emergency_state (ni->vol); +if (err) { +if (err != -EROFS || vi != ni->vol->vol_ino) +return err; + err =0 ; + }/* * Dirty attribute inodes are written via their real inodes so just
fs/ntfs/iomap.c +4 -2
@@ -739,9 +739,11 @@ static int __ntfs_write_iomap_begin(struct inode *inode, loff_t offset, {struct ntfs_inode *ni =NTFS_I (inode);struct ntfs_inode *mrec_ni; +int err; -if (NVolShutdown (ni->vol)) -return -EIO ; + err =ntfs_emergency_state (ni->vol); +if (err) +return err;if (!NInoNonResident (ni)) { mrec_ni = ntfs_inode_is_named_stream(ni) ?
fs/ntfs/mft.c +8 -2
@@ -2956,6 +2956,11 @@ int ntfs_mft_record_free(struct ntfs_volume *vol, struct ntfs_inode *ni)if (!vol || !ni)return -EINVAL ; +/* The freed record goes out right away, the rest of the deletion can't. */ + err =ntfs_emergency_state (vol); +if (err) +return err; +ntfs_debug ("Entering for inode 0x%llx.\n" , (long long )ni->mft_no); ni_mrec =map_mft_record (ni);
@@ -3423,8 +3428,9 @@ int ntfs_mft_writepages(struct address_space *mapping,struct folio *folio =NULL ;int error; -if (NVolShutdown (NTFS_I (mapping->host)->vol)) -return -EIO ; + error =ntfs_emergency_state (NTFS_I (mapping->host)->vol); +if (error) +return error;while ((folio =writeback_iter (mapping, wbc, folio, &error))) error =ntfs_write_mft_block (folio, wbc);
fs/ntfs/named_stream.c +22 -21
94 lines not on this page · raw message
fs/ntfs/namei.c +24 -16
@@ -782,8 +782,9 @@ static int ntfs_create(const struct mnt_idmap *idmap, struct inode *dir,__le16 *uname, *base_name =NULL , *stream_name =NULL ;int uname_len, stream_len, path_result, err; -if (NVolShutdown (vol)) -return -EIO ; + err =ntfs_emergency_state (vol); +if (err) +return err; path_result =ntfs_stream_path_names (vol, &dentry->d_name, &base_name, &uname_len, &stream_name, &stream_len);
@@ -1088,8 +1089,9 @@ static int ntfs_unlink(struct inode *dir, struct dentry *dentry)__le16 *uname =NULL ;int uname_len; -if (NVolShutdown (vol)) -return -EIO ; + err =ntfs_emergency_state (vol); +if (err) +return err;if (ntfs_stream_path_has_colon (vol, &dentry->d_name))return ntfs_unlink_named_stream (dir, dentry);
@@ -1136,8 +1138,9 @@ static struct dentry *ntfs_mkdir(const struct mnt_idmap *idmap, struct inode *di__le16 *uname;int uname_len; -if (NVolShutdown (vol)) -return ERR_PTR (-EIO ); + err =ntfs_emergency_state (vol); +if (err) +return ERR_PTR (err); err =ntfs_stream_path_check (vol, &dentry->d_name);if (err)
@@ -1181,8 +1184,9 @@ static int ntfs_rmdir(struct inode *dir, struct dentry *dentry)__le16 *uname =NULL ;int uname_len; -if (NVolShutdown (vol)) -return -EIO ; + err =ntfs_emergency_state (vol); +if (err) +return err; err =ntfs_stream_path_check (vol, &dentry->d_name);if (err)
@@ -1349,8 +1353,9 @@ static int ntfs_rename(const struct mnt_idmap *idmap, struct inode *old_dir,struct ntfs_inode *old_dir_ni =NTFS_I (old_dir), *new_dir_ni =NTFS_I (new_dir);bool new_dir_first = false; -if (NVolShutdown (old_dir_ni->vol)) -return -EIO ; + err =ntfs_emergency_state (vol); +if (err) +return err; err =ntfs_stream_path_check (vol, &old_dentry->d_name);if (err)
@@ -1503,8 +1508,9 @@ static int ntfs_symlink(const struct mnt_idmap *idmap, struct inode *dir,int usrc_len;int symlen =strlen (symname); -if (NVolShutdown (vol)) -return -EIO ; + err =ntfs_emergency_state (vol); +if (err) +return err; err =ntfs_stream_path_check (vol, &dentry->d_name);if (err)
@@ -1552,8 +1558,9 @@ static int ntfs_mknod(const struct mnt_idmap *idmap, struct inode *dir,__le16 *uname =NULL ;int uname_len; -if (NVolShutdown (vol)) -return -EIO ; + err =ntfs_emergency_state (vol); +if (err) +return err; err =ntfs_stream_path_check (vol, &dentry->d_name);if (err)
@@ -1608,8 +1615,9 @@ static int ntfs_link(struct dentry *old_dentry, struct inode *dir,int err;struct ntfs_inode *ni =NTFS_I (vi), *dir_ni =NTFS_I (dir); -if (NVolShutdown (vol)) -return -EIO ; + err =ntfs_emergency_state (vol); +if (err) +return err;if (NInoAttr (ni))return -EOPNOTSUPP ;
fs/ntfs/super.c +46 -6
95 lines not on this page · raw message
fs/ntfs/volume.h +16 -0
@@ -92,6 +92,7 @@* @sparse_compression_unit: Size of compression/sparse unit in clusters. * @lcn_empty_bits_per_page: Number of empty bits per page in the LCN bitmap. * @precalc_work: Work structure for background pre-calculation tasks. +* @error_work: Marks the volume dirty on disk after an error made it read-only. * @preallocated_size: reallocation size (in bytes). */ struct ntfs_volume {
@@ -166,6 +167,7 @@ struct ntfs_volume {u8 sparse_compression_unit;unsigned int *lcn_empty_bits_per_page;struct work_struct precalc_work; +struct work_struct error_work;loff_t preallocated_size; };
@@ -198,6 +200,8 @@ struct ntfs_volume {* NV_NativeSymlinkRel Translate absolute Windows reparse targets (native_symlink=rel). * NV_MftBootstrap Mount is still assembling $MFT's own runlist. * NV_StreamsWindows Interpret the final path component as file:stream. +* NV_EmergencyRO An error made the volume read-only until it is +* unmounted (errors=remount-ro). */ enum { NV_Errors,
@@ -220,6 +224,7 @@ enum { NV_SymlinkNative, NV_MftBootstrap, NV_StreamsWindows, + NV_EmergencyRO, };/*
@@ -261,6 +266,17 @@ DEFINE_NVOL_BIT_OPS(NativeSymlinkRel)DEFINE_NVOL_BIT_OPS (SymlinkNative)DEFINE_NVOL_BIT_OPS (MftBootstrap )DEFINE_NVOL_BIT_OPS (StreamsWindows) +DEFINE_NVOL_BIT_OPS (EmergencyRO) + +/* -EIO after a shutdown, -EROFS after an error with errors=remount-ro */ +static inline int ntfs_emergency_state(struct ntfs_volume *vol) +{ +if (unlikely (NVolShutdown (vol))) +return -EIO ; +if (unlikely (NVolEmergencyRO (vol))) +return -EROFS ; +return 0 ; +}static inline void ntfs_inc_free_clusters (struct ntfs_volume *vol,s64 nr) {
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 19/33] ntfs: don't set SB_RDONLY after filesystem errors
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Medium] ntfs: unlocked sb->s_flags RMW in ntfs_handle_error() via precalc_work --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Medium] ntfs: unlocked sb->s_flags RMW in ntfs_handle_error() via precalc_work
--
commit 41f5366b5799dc9babb537d585f77ff9063831e1
Author: Christian Brauner <[email protected]>
ntfs: don't set SB_RDONLY after filesystem errors
This commit replaces setting SB_RDONLY on runtime filesystem errors under errors=remount-ro with an NV_EmergencyRO volume flag and an asynchronous work item to persist the volume dirty bit.
Quoted 4 lines
fs/ntfs/super.c +0
-0
[ ... ]
fs/ntfs/super.c void ntfs_handle_error(struct super_block *sb) · line 442
Quoted 9 lines
fs/ntfs/super.c +4
-1@@ -434,7 +436,21 @@ void ntfs_handle_error(struct super_block *sb)return ;if (vol->on_errors ==ON_ERRORS_REMOUNT_RO ) { - sb->s_flags |=SB_RDONLY ; +if (!(sb->s_flags &SB_BORN )) { +/* Before SB_BORN the mounter holds s_umount. */ + sb->s_flags |=SB_RDONLY ; + }else {
[Severity: Medium]
Can precalc_free_clusters() reach this branch on a kworker thread before SB_BORN is set, racing on sb->s_flags without holding s_umount?
ntfs_fill_super() queues vol->precalc_work before returning to get_tree_bdev_flags() and vfs_get_tree():
CPU0 (mounter) CPU1 (kworker)vfs_get_tree()
get_tree_bdev_flags()
ntfs_fill_super()
queue_work(..., &vol->precalc_work)
precalc_free_clusters()
get_nr_free_clusters()
...
ntfs_error()
ntfs_handle_error()
/* !(sb->s_flags & SB_BORN) */
super_wake(sb, SB_BORN) sb->s_flags |= SB_RDONLY;
smp_store_release(&sb->s_flags, <-- unlocked RMW race
sb->s_flags | SB_BORN);During mount, load_system_files() does not populate the runlist for $Bitmap, so get_nr_free_clusters() decompresses $Bitmap's mapping pairs on the workqueue and calls ntfs_error() if reading or decompressing fails.
Because neither side updates sb->s_flags atomically and the kworker does not hold s_umount, the RMW in ntfs_handle_error() can overwrite SB_BORN (leaving later super_lock() callers hung waiting for SB_BORN) or lose SB_RDONLY. Taking the !SB_BORN branch from the workqueue also skips NVolSetErrors(vol) and NV_EmergencyRO, allowing a subsequent remount,rw in ntfs_reconfigure() to succeed despite the error.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=19
Christian Brauner
maintainerPATCH v3 20/33 ntfs: use the generic emergency read-only state
In reply to Christian BraunerSet SB_STATE_EMERGENCY_RO instead of the NV_EmergencyRO volume flag when an error makes the volume read-only. ntfs_emergency_state() keeps telling a shutdown (EIO) and the emergency state (EROFS) apart so all of its callers stay as they are. /proc/mounts shows the volume read-only again.
The VFS also refuses to freeze the superblock in that state. So FS_IOC_SHUTDOWN with FS_SHUTDOWN_FLAGS_DEFAULT or _LOGFLUSH shuts down without the freeze after an error, there's nothing left to flush anyway.
--- fs/ntfs/super.c | 14 +++++++++----- fs/ntfs/volume.h | 7 ++----- 2 files changed, 11 insertions(+), 10 deletions(-)
fs/ntfs/super.c +9 -5
@@ -441,13 +441,12 @@ void ntfs_handle_error(struct super_block *sb) sb->s_flags |=SB_RDONLY ; }else {/* -* NV_EmergencyRO makes the volume refuse modifications -* instead and NV_Errors keeps it from being remounted -* read-write. The error work marks the volume dirty on -* disk. +* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). +* NV_Errors keeps the volume from being remounted +* read-write and the error work marks it dirty. */ NVolSetErrors (vol); -if (test_and_set_bit (NV_EmergencyRO, &vol->flags)) +if (!sb_set_emergency_ro (sb))return ;queue_work (ntfs_wq, &vol->error_work); }
@@ -2272,6 +2271,11 @@ int ntfs_force_shutdown(struct super_block *sb, u32 flags)if (NVolShutdown (vol))return 0 ; +/* After an error there is nothing to flush and freezing fails. */ +if ((flags ==FS_SHUTDOWN_FLAGS_DEFAULT || + flags ==FS_SHUTDOWN_FLAGS_LOGFLUSH ) &&sb_emergency_ro (sb)) + flags =FS_SHUTDOWN_FLAGS_NOLOGFLUSH ; +switch (flags) {case FS_SHUTDOWN_FLAGS_DEFAULT :case FS_SHUTDOWN_FLAGS_LOGFLUSH :
fs/ntfs/volume.h +2 -5
@@ -16,6 +16,7 @@#include <linux/uidgid.h> #include <linux/workqueue.h> #include <linux/errseq.h> +#include <linux/fs.h> #include "layout.h"
@@ -200,8 +201,6 @@ struct ntfs_volume {* NV_NativeSymlinkRel Translate absolute Windows reparse targets (native_symlink=rel). * NV_MftBootstrap Mount is still assembling $MFT's own runlist. * NV_StreamsWindows Interpret the final path component as file:stream. -* NV_EmergencyRO An error made the volume read-only until it is -* unmounted (errors=remount-ro). */ enum { NV_Errors,
@@ -224,7 +223,6 @@ enum { NV_SymlinkNative, NV_MftBootstrap, NV_StreamsWindows, - NV_EmergencyRO, };/*
@@ -266,14 +264,13 @@ DEFINE_NVOL_BIT_OPS(NativeSymlinkRel)DEFINE_NVOL_BIT_OPS (SymlinkNative)DEFINE_NVOL_BIT_OPS (MftBootstrap )DEFINE_NVOL_BIT_OPS (StreamsWindows) -DEFINE_NVOL_BIT_OPS (EmergencyRO)/* -EIO after a shutdown, -EROFS after an error with errors=remount-ro */ static inline int ntfs_emergency_state(struct ntfs_volume *vol) {if (unlikely (NVolShutdown (vol)))return -EIO ; -if (unlikely (NVolEmergencyRO (vol))) +if (unlikely (sb_emergency_ro (vol->sb)))return -EROFS ;return 0 ; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 20/33] ntfs: use the generic emergency read-only state
Sashiko has reviewed this patch and found no issues.
Quoted 6 lines
Set
SB_STATE_EMERGENCY_ROinstead of theNV_EmergencyROvolume flag when an error makes the volume read-only.ntfs_emergency_state()keeps telling a shutdown (EIO) and the emergency state (EROFS) apart so all of its callers stay as they are./proc/mountsshows the volume read-only again.
Quoted 7 lines
The VFS also refuses to freeze the superblock in that state. So
FS_IOC_SHUTDOWNwithFS_SHUTDOWN_FLAGS_DEFAULTor_LOGFLUSHshuts down without the freeze after an error, there's nothing left to flush anyway.Reviewed-by Hyunchul Lee <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=20
Christian Brauner
maintainerPATCH v3 21/33 ocfs2: keep the current mount options on remount
In reply to Christian Braunerocfs2_init_fs_context() doesn't inherit the current mount options for a reconfigure. But ocfs2_check_set_options() insists on exactly one heartbeat mode and ocfs2_reconfigure() refuses to change the data mode. So every remount without heartbeat= fails with EINVAL, including emergency remount (sysrq u) which passes no options at all, and that WARNs for every ocfs2 mounted read-write. A remount that does pass heartbeat= silently resets every option it doesn't repeat, e.g. noacl, errors=, barrier= and preferred_slot=.
So start a reconfigure from the current options like exfat and gfs2 do. Asking for a different data or heartbeat mode still fails with EINVAL.
The next patch stops ocfs2 from setting SB_RDONLY after errors. An errored ocfs2 then stays read-write and emergency remount would run into this as well.
--- fs/ocfs2/super.c | 13 +++++++++++++ 1 file changed, 13 insertions(+)
fs/ocfs2/super.c +13 -0
@@ -1222,6 +1222,19 @@ static int ocfs2_init_fs_context(struct fs_context *fc) mopt->resv_level =OCFS2_DEFAULT_RESV_LEVEL ; mopt->dir_resv_level =-1 ; +/* +* Start from the current options so a remount only changes what it +* specifies, emergency remount passes none at all. +*/ +if (fc->purpose ==FS_CONTEXT_FOR_RECONFIGURE ) { +struct ocfs2_super *osb =OCFS2_SB (fc->root->d_sb); + + mopt->mount_opt = osb->s_mount_opt; + mopt->atime_quantum = osb->s_atime_quantum; + mopt->slot = osb->preferred_slot; + mopt->user_stack =ocfs2_userspace_stack (osb); + } + fc->fs_private = mopt; fc->ops = &ocfs2_context_ops;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 21/33] ocfs2: keep the current mount options on remount
Sashiko has reviewed this patch and found no issues.
Quoted 12 lines
ocfs2_init_fs_context()doesn't inherit the current mount options for a reconfigure. Butocfs2_check_set_options()insists on exactly one heartbeat mode andocfs2_reconfigure()refuses to change the data mode. So every remount without heartbeat= fails with EINVAL, including emergency remount (sysrq u) which passes no options at all, and that WARNs for every ocfs2 mounted read-write. A remount that does pass heartbeat= silently resets every option it doesn't repeat, e.g. noacl, errors=, barrier= and preferred_slot=.So start a reconfigure from the current options like exfat and gfs2 do. Asking for a different data or heartbeat mode still fails with EINVAL.
Quoted 6 lines
The next patch stops ocfs2 from setting
SB_RDONLYafter errors. An errored ocfs2 then stays read-write and emergency remount would run into this as well.Reviewed-by Joseph Qi <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=21
Christian Brauner
maintainerPATCH v3 22/33 ocfs2: don't set SB_RDONLY after filesystem errors
In reply to Christian BraunerWith errors=remount-ro, the default, ocfs2_handle_error() sets SB_RDONLY without holding s_umount. If that happens while the filesystem is frozen through bdev_freeze() thaw_super() leaves the s_writers rwsems locked. Writers hang and so does umount because ocfs2_shutdown_local_alloc() needs sb_start_intwrite().
The soft read-only state doesn't stop modifications on its own. Every read-only mount sets it and those still need transactions for recovery, the local alloc and the truncate log. Everything else was stopped by the VFS because of SB_RDONLY. So call sb_set_emergency_ro() instead which keeps it that way, and keep setting SOFT_RO for the existing ocfs2_emergency_state() checks.
The xattr and ACL setters get a check of their own because ecryptfs calls them without write access on the lower mount. It sits in the xattr handlers and not in ocfs2_xattr_set() because chmod and setfacl store the ACL through it after the new mode was already committed.
Quotas aren't enabled at mount in the error state because that writes the quota files. The VFS doesn't call ocfs2_sync_fs() in that state and a remount to read-only doesn't suspend quotas. Q_GETQUOTA and Q_GETNEXTQUOTA can still start transactions. I've left that alone.
Writes through file descriptors opened before the error fail with EROFS and umount completes instead of hanging.
--- fs/ocfs2/acl.c | 3 +++ fs/ocfs2/super.c | 14 +++++++++----- fs/ocfs2/xattr.c | 8 ++++++++ 3 files changed, 20 insertions(+), 5 deletions(-)
fs/ocfs2/acl.c +3 -0
@@ -268,6 +268,9 @@ int ocfs2_iop_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,struct ocfs2_lock_holder oh;struct inode *inode =d_inode (dentry); +if (unlikely (ocfs2_emergency_state (OCFS2_SB (inode->i_sb)))) +return -EROFS ; + had_lock =ocfs2_inode_lock_tracker (inode, &bh,1 , &oh);if (had_lock <0 )return had_lock;
fs/ocfs2/super.c +9 -5
@@ -647,8 +647,8 @@ static int ocfs2_reconfigure(struct fs_context *fc)/* We're going to/from readonly mode. */ if ((bool )(fc->sb_flags &SB_RDONLY ) !=sb_rdonly (sb)) { -/* Disable quota accounting before remounting RO */ -if (fc->sb_flags &SB_RDONLY ) { +/* Disable quota accounting before remounting RO, not after an error. */ +if ((fc->sb_flags &SB_RDONLY ) && !sb_emergency_ro (sb)) { ret =ocfs2_susp_quotas (osb,0 );if (ret <0 ) goto out;
@@ -1137,7 +1137,7 @@ static int ocfs2_fill_super(struct super_block *sb, struct fs_context *fc)/* Now we can initialize quotas because we can afford to wait * for cluster locks recovery now. That also means that truncation * log recovery can happen but that waits for proper quota setup */ -if (!sb_rdonly (sb)) { +if (!sb_rdonly_any (sb)) { status =ocfs2_enable_quotas (osb);if (status <0 ) {/* We have to err-out specially here because
@@ -2519,11 +2519,15 @@ static int ocfs2_handle_error(struct super_block *sb) rv = -EIO ; }else {/* default option */ rv = -EROFS ; -if (sb_rdonly (sb) &&ocfs2_emergency_state (osb)) +if (ocfs2_emergency_state (osb))return rv;pr_crit ("OCFS2: File system is now read-only.\n" ); - sb->s_flags |=SB_RDONLY ; +/* +* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). The +* soft read-only flag makes ocfs2_emergency_state() refuse. +*/ +sb_set_emergency_ro (sb);ocfs2_set_ro_flag (osb,0 ); }
fs/ocfs2/xattr.c +8 -0
@@ -7781,6 +7781,9 @@ static int ocfs2_xattr_security_set(const struct xattr_handler *handler, const char *name, const void *value, size_t size, int flags) { + if (unlikely(ocfs2_emergency_state(OCFS2_SB(inode->i_sb)))) + return -EROFS; +return ocfs2_xattr_set (inode,OCFS2_XATTR_INDEX_SECURITY , name, value, size, flags); }
@@ -7929,6 +7932,9 @@ static int ocfs2_xattr_trusted_set(const struct xattr_handler *handler, const char *name, const void *value, size_t size, int flags) { + if (unlikely(ocfs2_emergency_state(OCFS2_SB(inode->i_sb)))) + return -EROFS; +return ocfs2_xattr_set (inode,OCFS2_XATTR_INDEX_TRUSTED , name, value, size, flags); }
@@ -7964,6 +7970,8 @@ static int ocfs2_xattr_user_set(const struct xattr_handler *handler,if (osb->s_mount_opt &OCFS2_MOUNT_NOUSERXATTR )return -EOPNOTSUPP ; +if (unlikely (ocfs2_emergency_state (osb))) +return -EROFS ;return ocfs2_xattr_set (inode,OCFS2_XATTR_INDEX_USER , name, value, size, flags);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 22/33] ocfs2: don't set SB_RDONLY after filesystem errors
Sashiko has reviewed this patch and found no issues.
Quoted 23 lines
With errors=remount-ro, the default,
ocfs2_handle_error()setsSB_RDONLYwithout holdings_umount. If that happens while the filesystem is frozen throughbdev_freeze()thaw_super()leaves thes_writersrwsems locked. Writers hang and so does umount becauseocfs2_shutdown_local_alloc()needssb_start_intwrite().The soft read-only state doesn't stop modifications on its own. Every read-only mount sets it and those still need transactions for recovery, the local alloc and the truncate log. Everything else was stopped by the VFS because of
SB_RDONLY. So callsb_set_emergency_ro()instead which keeps it that way, and keep settingSOFT_ROfor the existingocfs2_emergency_state()checks.The xattr and ACL setters get a check of their own because ecryptfs calls them without write access on the lower mount. It sits in the xattr handlers and not in
ocfs2_xattr_set()because chmod and setfacl store the ACL through it after the new mode was already committed.Quotas aren't enabled at mount in the error state because that writes the quota files. The VFS doesn't call
ocfs2_sync_fs()in that state and a remount to read-only doesn't suspend quotas.Q_GETQUOTAandQ_GETNEXTQUOTAcan still start transactions. I've left that alone.
Quoted 5 lines
Writes through file descriptors opened before the error fail with EROFS and umount completes instead of hanging.
Reviewed-by Joseph Qi <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=22
Christian Brauner
maintainerPATCH v3 23/33 zonefs: don't set SB_RDONLY after I/O errors
In reply to Christian Braunerzonefs sets SB_RDONLY from its I/O error handling without holdings_umount: with errors=remount-ro, when the zone report fails and when closing an explicitly opened zone fails. If that happens while the filesystem is frozen through bdev_freeze() thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked. Writers hang and even a remount to read-write doesn't help because the next freeze blocks forever.
So record the error in a new ZONEFS_SB_EMERGENCY_RO bit instead. All three places go through zonefs_set_emergency_ro() which keeps printing the warning once. Write opens, write_iter and setattr fail with EROFS and page_mkwrite with SIGBUS. An error on a read-only mount doesn't set the bit.
The state lasts until umount and zonefs.rst says so. A remount to read-only works and writes nothing. /proc/mounts keeps showing the filesystem read-write. ftruncate() and write faults through file descriptors and mappings from before the error are refused now too.
--- Documentation/filesystems/zonefs.rst | 4 ++-- fs/zonefs/file.c | 17 +++++++++-------- fs/zonefs/super.c | 34 ++++++++++++++++++++++++++-------- fs/zonefs/zonefs.h | 16 ++++++++++++++++ 4 files changed, 53 insertions(+), 18 deletions(-)
Documentation/filesystems/zonefs.rst +2 -2
@@ -285,8 +285,8 @@ Further notes: * The "errors=remount-ro" mount option is the default behavior of zonefs I/O error processing if no errors mount option is specified. * With the "errors=remount-ro" mount option, the change of the file access - permissions to read-only applies to all files. The file system is remounted - read-only. + permissions to read-only applies to all files. A file system mounted + read-write refuses all writes until it is unmounted. * Access permission and file size changes due to the device transitioning zones to the offline condition are permanent. Remounting or reformatting the device with mkfs.zonefs (mkzonefs) will not change back offline zone files to a good
fs/zonefs/file.c +9 -8
@@ -299,7 +299,7 @@ static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)struct inode *inode =file_inode (vmf->vma->vm_file);vm_fault_t ret; -if (unlikely (IS_IMMUTABLE (inode))) +if (unlikely (IS_IMMUTABLE (inode) ||zonefs_emergency_ro (inode->i_sb)))return VM_FAULT_SIGBUS ;/*
@@ -601,7 +601,7 @@ static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)if (unlikely (IS_IMMUTABLE (inode)))return -EPERM ; -if (sb_rdonly (inode->i_sb)) +if (sb_rdonly (inode->i_sb) ||zonefs_emergency_ro (inode->i_sb))return -EROFS ;/* Write operations beyond the zone capacity are not allowed */
@@ -788,6 +788,9 @@ static int zonefs_file_open(struct inode *inode, struct file *file) {int ret; +if ((file->f_mode &FMODE_WRITE ) &&zonefs_emergency_ro (inode->i_sb)) +return -EROFS ; + file->f_mode |=FMODE_CAN_ODIRECT ; ret =generic_file_open (inode, file);if (ret)
@@ -825,17 +828,15 @@ static void zonefs_seq_file_write_close(struct inode *inode)/* * Leaving zones explicitly open may lead to a state * where most zones cannot be written (zone resources -* exhausted). So take preventive action by remounting -* read-only. +* exhausted). So take preventive action by making the +* file system read-only. */ if (z->z_flags &ZONEFS_ZONE_OPEN && - !(sb->s_flags &SB_RDONLY )) { + !sb_rdonly (sb) && !zonefs_emergency_ro (sb)) {zonefs_warn (sb,"closing zone at %llu failed %d\n" , z->z_sector, ret); -zonefs_warn (sb, -"remounting filesystem read-only\n" ); - sb->s_flags |=SB_RDONLY ; +zonefs_set_emergency_ro (sb); } goto unlock; }
fs/zonefs/super.c +26 -8
@@ -247,6 +247,19 @@ static void zonefs_inode_update_mode(struct inode *inode) z->z_mode = inode->i_mode; } +/* +* Refuse writes after an error. SB_RDONLY needs s_umount and a remount, set on +* a frozen file system it makes thaw_super() leave s_writers locked. +*/ +void zonefs_set_emergency_ro (struct super_block *sb) +{ +if (sb_rdonly (sb) || +test_and_set_bit (ZONEFS_SB_EMERGENCY_RO , &ZONEFS_SB (sb)->s_state)) +return ; + +zonefs_warn (sb,"remounting filesystem read-only\n" ); +} +static int zonefs_io_error_cb (struct blk_zone *zone,unsigned int idx,void *data) {
@@ -339,13 +352,11 @@ static void zonefs_handle_io_error(struct inode *inode, struct blk_zone *zone, z->z_flags &= ~ZONEFS_ZONE_OPEN ;/* -* If error=remount-ro was specified, any error result in remounting -* the volume as read-only. +* If error=remount-ro was specified, any error makes the volume +* read-only. */ -if ((sbi->s_mount_opts &ZONEFS_MNTOPT_ERRORS_RO ) && !sb_rdonly (sb)) { -zonefs_warn (sb,"remounting filesystem read-only\n" ); - sb->s_flags |=SB_RDONLY ; - } +if (sbi->s_mount_opts &ZONEFS_MNTOPT_ERRORS_RO ) +zonefs_set_emergency_ro (sb);/* * Update block usage stats and the inode size to prevent access to
@@ -404,8 +415,7 @@ void __zonefs_io_error(struct inode *inode, bool write)if (ret !=1 ) {zonefs_err (sb,"Get inode %llu zone information failed %d\n" , inode->i_ino, ret); -zonefs_warn (sb,"remounting filesystem read-only\n" ); - sb->s_flags |=SB_RDONLY ; +zonefs_set_emergency_ro (sb);return ; }
@@ -539,6 +549,9 @@ static int zonefs_inode_setattr(const struct mnt_idmap *idmap,struct inode *inode =d_inode (dentry);int ret; +if (unlikely (zonefs_emergency_ro (inode->i_sb))) +return -EROFS ; +if (unlikely (IS_IMMUTABLE (inode)))return -EPERM ;
@@ -1369,6 +1382,11 @@ static int zonefs_reconfigure(struct fs_context *fc)struct super_block *sb = fc->root->d_sb;struct zonefs_sb_info *sbi = sb->s_fs_info; +/* An error made the file system read-only until it is unmounted. */ +if (sb_rdonly (sb) && !(fc->sb_flags &SB_RDONLY ) && +zonefs_emergency_ro (sb)) +return -EROFS ; +sync_filesystem (fc->root->d_sb);/* Copy new options from ctx into sbi. */ sbi->s_mount_opts = ctx->s_mount_opts;
fs/zonefs/zonefs.h +16 -0
@@ -241,6 +241,16 @@ struct zonefs_sb_info {bool s_sysfs_registered;struct kobject s_kobj;struct completion s_kobj_unregister; + +unsigned long s_state; +}; + +/* +* Super block state bits (s_state). +*/ +enum { +/* An error made the file system read-only until it is unmounted */ +ZONEFS_SB_EMERGENCY_RO , };static inline struct zonefs_sb_info *ZONEFS_SB (struct super_block *sb)
@@ -248,6 +258,11 @@ static inline struct zonefs_sb_info *ZONEFS_SB(struct super_block *sb)return sb->s_fs_info; } +static inline bool zonefs_emergency_ro (struct super_block *sb) +{ +return test_bit (ZONEFS_SB_EMERGENCY_RO , &ZONEFS_SB (sb)->s_state); +} + #define zonefs_info(sb, format, args...) \ pr_info("zonefs (%s): " format, sb->s_id, ## args) #define zonefs_err(sb, format, args...) \
@@ -260,6 +275,7 @@ void zonefs_inode_account_active(struct inode *inode);int zonefs_inode_zone_mgmt (struct inode *inode,enum req_op op);void zonefs_i_size_write (struct inode *inode,loff_t isize);void zonefs_update_stats (struct inode *inode,loff_t new_isize); +void zonefs_set_emergency_ro (struct super_block *sb);void __zonefs_io_error (struct inode *inode,bool write);static inline void zonefs_io_error (struct inode *inode,bool write)
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 23/33] zonefs: don't set SB_RDONLY after I/O errors
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines
zonefs sets
SB_RDONLYfrom its I/O error handling without holdings_umount: with errors=remount-ro, when the zone report fails and when closing an explicitly opened zone fails. If that happens while the filesystem is frozen throughbdev_freeze()thaw_super()sees a read-only superblock and leaves thes_writersrwsems locked. Writers hang and even a remount to read-write doesn't help because the next freeze blocks forever.So record the error in a new
ZONEFS_SB_EMERGENCY_RObit instead. All three places go throughzonefs_set_emergency_ro()which keeps printing the warning once. Write opens,write_iterand setattr fail with EROFS andpage_mkwritewith SIGBUS. An error on a read-only mount doesn't set the bit.
Quoted 8 lines
The state lasts until umount and
zonefs.rstsays so. A remount to read-only works and writes nothing./proc/mountskeeps showing the filesystem read-write.ftruncate()and write faults through file descriptors and mappings from before the error are refused now too.Fixes 8dcc1a9d90c1 ("fs: New zonefs file system")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=23
Christian Brauner
maintainerPATCH v3 24/33 zonefs: use the generic emergency read-only state
In reply to Christian BraunerSet SB_STATE_EMERGENCY_RO instead of a private bit and drop the checks the VFS does now. /proc/mounts and statfs() report the filesystem as read-only again. zonefs_set_emergency_ro() still prints the warning once. The check in zonefs_seq_file_write_close() stays so a failed zone close doesn't make the filesystem read-only a second time. And so does the one in zonefs_file_write_iter() because the loop driver, nfs localio and others call ->write_iter() without rw_verify_area().
--- fs/zonefs/file.c | 10 ++++------ fs/zonefs/super.c | 11 +---------- fs/zonefs/zonefs.h | 15 --------------- 3 files changed, 5 insertions(+), 31 deletions(-)
fs/zonefs/file.c +4 -6
@@ -299,7 +299,7 @@ static vm_fault_t zonefs_filemap_page_mkwrite(struct vm_fault *vmf)struct inode *inode =file_inode (vmf->vma->vm_file);vm_fault_t ret; -if (unlikely (IS_IMMUTABLE (inode) ||zonefs_emergency_ro (inode->i_sb))) +if (unlikely (IS_IMMUTABLE (inode)))return VM_FAULT_SIGBUS ;/*
@@ -601,7 +601,8 @@ static ssize_t zonefs_file_write_iter(struct kiocb *iocb, struct iov_iter *from)if (unlikely (IS_IMMUTABLE (inode)))return -EPERM ; -if (sb_rdonly (inode->i_sb) ||zonefs_emergency_ro (inode->i_sb)) +/* loop and others call ->write_iter() without rw_verify_area() */ +if (sb_rdonly_any (inode->i_sb))return -EROFS ;/* Write operations beyond the zone capacity are not allowed */
@@ -788,9 +789,6 @@ static int zonefs_file_open(struct inode *inode, struct file *file) {int ret; -if ((file->f_mode &FMODE_WRITE ) &&zonefs_emergency_ro (inode->i_sb)) -return -EROFS ; - file->f_mode |=FMODE_CAN_ODIRECT ; ret =generic_file_open (inode, file);if (ret)
@@ -832,7 +830,7 @@ static void zonefs_seq_file_write_close(struct inode *inode)* file system read-only. */ if (z->z_flags &ZONEFS_ZONE_OPEN && - !sb_rdonly (sb) && !zonefs_emergency_ro (sb)) { + !sb_rdonly_any (sb)) {zonefs_warn (sb,"closing zone at %llu failed %d\n" , z->z_sector, ret);
fs/zonefs/super.c +1 -10
@@ -253,8 +253,7 @@ static void zonefs_inode_update_mode(struct inode *inode)*/ void zonefs_set_emergency_ro (struct super_block *sb) { -if (sb_rdonly (sb) || -test_and_set_bit (ZONEFS_SB_EMERGENCY_RO , &ZONEFS_SB (sb)->s_state)) +if (sb_rdonly (sb) || !sb_set_emergency_ro (sb))return ;zonefs_warn (sb,"remounting filesystem read-only\n" );
@@ -549,9 +548,6 @@ static int zonefs_inode_setattr(const struct mnt_idmap *idmap,struct inode *inode =d_inode (dentry);int ret; -if (unlikely (zonefs_emergency_ro (inode->i_sb))) -return -EROFS ; -if (unlikely (IS_IMMUTABLE (inode)))return -EPERM ;
@@ -1382,11 +1378,6 @@ static int zonefs_reconfigure(struct fs_context *fc)struct super_block *sb = fc->root->d_sb;struct zonefs_sb_info *sbi = sb->s_fs_info; -/* An error made the file system read-only until it is unmounted. */ -if (sb_rdonly (sb) && !(fc->sb_flags &SB_RDONLY ) && -zonefs_emergency_ro (sb)) -return -EROFS ; -sync_filesystem (fc->root->d_sb);/* Copy new options from ctx into sbi. */ sbi->s_mount_opts = ctx->s_mount_opts;
fs/zonefs/zonefs.h +0 -15
@@ -241,16 +241,6 @@ struct zonefs_sb_info {bool s_sysfs_registered;struct kobject s_kobj;struct completion s_kobj_unregister; - -unsigned long s_state; -}; - -/* -* Super block state bits (s_state). -*/ -enum { -/* An error made the file system read-only until it is unmounted */ -ZONEFS_SB_EMERGENCY_RO , };static inline struct zonefs_sb_info *ZONEFS_SB (struct super_block *sb)
@@ -258,11 +248,6 @@ static inline struct zonefs_sb_info *ZONEFS_SB(struct super_block *sb)return sb->s_fs_info; } -static inline bool zonefs_emergency_ro (struct super_block *sb) -{ -return test_bit (ZONEFS_SB_EMERGENCY_RO , &ZONEFS_SB (sb)->s_state); -} - #define zonefs_info(sb, format, args...) \ pr_info("zonefs (%s): " format, sb->s_id, ## args) #define zonefs_err(sb, format, args...) \
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 24/33] zonefs: use the generic emergency read-only state
Sashiko has reviewed this patch and found no issues.
Quoted 9 lines
Set
SB_STATE_EMERGENCY_ROinstead of a private bit and drop the checks the VFS does now./proc/mountsandstatfs()report the filesystem as read-only again.zonefs_set_emergency_ro()still prints the warning once. The check inzonefs_seq_file_write_close()stays so a failed zone close doesn't make the filesystem read-only a second time. And so does the one inzonefs_file_write_iter()because the loop driver, nfs localio and others call ->write_iter() withoutrw_verify_area().Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=24
Christian Brauner
maintainerPATCH v3 25/33 btrfs: check BTRFS_FS_STATE_RO instead of SB_RDONLY at runtime
In reply to Christian BraunerAdd btrfs_fs_rdonly() which tests BTRFS_FS_STATE_RO and use it instead of sb_rdonly() wherever btrfs checks for a read-only filesystem at
runtime: the error handler, read repair, async discard, close_ctree(), btrfs_inc_block_group_ro(), check_inode_item(), device replace and add, cancelling a balance, the sysfs label and force_chunk_alloc stores, the qgroup PERTRANS conversions and the subvolume orphan cleanup. And check it next to every btrfs_root_readonly() check that guards a modification, e.g. in btrfs_permission(), btrfs_setattr(), btrfs_update_time(), the xattr and ACL handlers, btrfs_fileattr_set() and btrfs_remap_file_range_prep().
BTRFS_FS_STATE_RO has always been set and cleared together with SB_RDONLY. The remaining sb_rdonly() users are mount and remount paths plus btrfs_is_full_ro() which only matters for rescue mounts.
This prepares for not setting SB_RDONLY from the error handler. Until then the only difference is that ftruncate() and FICLONE through files that were opened for writing before a forced remount to read-only, e.g. by sysrq u, fail with EROFS now instead of modifying the filesystem.
--- fs/btrfs/acl.c | 3 ++- fs/btrfs/bio.c | 2 +- fs/btrfs/block-group.c | 2 +- fs/btrfs/dev-replace.c | 2 +- fs/btrfs/discard.c | 2 +- fs/btrfs/disk-io.c | 4 ++-- fs/btrfs/fs.h | 10 ++++++++++ fs/btrfs/inode.c | 8 ++++---- fs/btrfs/ioctl.c | 8 ++++---- fs/btrfs/messages.c | 8 ++++---- fs/btrfs/qgroup.c | 4 ++-- fs/btrfs/reflink.c | 3 ++- fs/btrfs/sysfs.c | 4 ++-- fs/btrfs/tree-checker.c | 2 +- fs/btrfs/volumes.c | 4 ++-- fs/btrfs/xattr.c | 8 ++++++-- 16 files changed, 45 insertions(+), 29 deletions(-)
fs/btrfs/acl.c +2 -1
@@ -107,8 +107,9 @@ int btrfs_set_acl(const struct mnt_idmap *idmap, struct dentry *dentry,int ret;struct inode *inode =d_inode (dentry);umode_t old_mode = inode->i_mode; +struct btrfs_root *root =BTRFS_I (inode)->root; -if (btrfs_root_readonly (BTRFS_I (inode)->root)) +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (root->fs_info))return -EROFS ;if (type ==ACL_TYPE_ACCESS && acl) {
fs/btrfs/bio.c +1 -1
@@ -924,7 +924,7 @@ int btrfs_repair_bbio_failure(struct btrfs_bio *bbio, const struct bvec_iter *or* The fs either mounted RO or hit critical errors, no need * to continue repairing. */ -if (unlikely (sb_rdonly (fs_info->sb))) +if (unlikely (btrfs_fs_rdonly (fs_info)))return 0 ;if (btrfs_repair_one_zone (fs_info, logical))
fs/btrfs/block-group.c +1 -1
@@ -3138,7 +3138,7 @@ int btrfs_inc_block_group_ro(struct btrfs_block_group *cache,* In that case we should not start a new transaction on read-only fs. * Thus here we skip all chunk allocations. */ -if (sb_rdonly (fs_info->sb)) { +if (btrfs_fs_rdonly (fs_info)) {mutex_lock (&fs_info->ro_block_group_mutex); ret =inc_block_group_ro (cache, false);mutex_unlock (&fs_info->ro_block_group_mutex);
fs/btrfs/dev-replace.c +1 -1
@@ -1115,7 +1115,7 @@ int btrfs_dev_replace_cancel(struct btrfs_fs_info *fs_info)int result;int ret; -if (sb_rdonly (fs_info->sb)) +if (btrfs_fs_rdonly (fs_info))return -EROFS ;mutex_lock (&dev_replace->lock_finishing_cancel_unmount);
fs/btrfs/discard.c +1 -1
@@ -86,7 +86,7 @@ static bool btrfs_run_discard_work(const struct btrfs_discard_ctl *discard_ctl) struct btrfs_fs_info, discard_ctl); -return (!(fs_info->sb->s_flags &SB_RDONLY ) && +return (!btrfs_fs_rdonly (fs_info) &&test_bit (BTRFS_FS_DISCARD_RUNNING , &fs_info->flags)); }
fs/btrfs/disk-io.c +2 -2
@@ -177,7 +177,7 @@ static int btrfs_repair_eb_io_failure(const struct extent_buffer *eb,struct btrfs_bio *bbio;int ret; -if (sb_rdonly (fs_info->sb)) +if (btrfs_fs_rdonly (fs_info))return -EROFS ;/*
@@ -4483,7 +4483,7 @@ void __cold close_ctree(struct btrfs_fs_info *fs_info)/* Cancel or finish ongoing discard work */ btrfs_discard_cleanup (fs_info); -if (!sb_rdonly (fs_info->sb)) { +if (!btrfs_fs_rdonly (fs_info)) {/* * The cleaner kthread is stopped, so do one final pass over * unused block groups.
fs/btrfs/fs.h +10 -0
@@ -1194,6 +1194,16 @@ static inline int btrfs_need_cleaner_sleep(const struct btrfs_fs_info *fs_info)btrfs_fs_closing (fs_info); } +/* +* Check whether the filesystem is read-only. BTRFS_FS_STATE_RO is set and +* cleared together with SB_RDONLY on mount, on remount and when an error forces +* the filesystem read-only. Use this instead of sb_rdonly() at runtime. +*/ +static inline bool btrfs_fs_rdonly (const struct btrfs_fs_info *fs_info) +{ +return test_bit (BTRFS_FS_STATE_RO , &fs_info->fs_state); +} +static inline void btrfs_wake_unfinished_drop (struct btrfs_fs_info *fs_info) {clear_and_wake_up_bit (BTRFS_FS_UNFINISHED_DROPS , &fs_info->flags);
fs/btrfs/inode.c +4 -4
@@ -5433,7 +5433,7 @@ static int btrfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentry,struct btrfs_root *root =BTRFS_I (inode)->root;int ret; -if (btrfs_root_readonly (root)) +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (root->fs_info))return -EROFS ; ret =setattr_prepare (idmap, dentry, attr);
@@ -6058,7 +6058,7 @@ struct inode *btrfs_lookup_dentry(struct inode *dir, struct dentry *dentry)return ERR_CAST (inode);down_read (&fs_info->cleanup_work_sem); -if (!sb_rdonly (inode->vfs_inode.i_sb)) +if (!btrfs_fs_rdonly (fs_info)) ret =btrfs_orphan_cleanup (sub_root);up_read (&fs_info->cleanup_work_sem);if (ret) {
@@ -6421,7 +6421,7 @@ static int btrfs_update_time(struct inode *inode, enum fs_update_time type,struct btrfs_root *root =BTRFS_I (inode)->root;int dirty; -if (btrfs_root_readonly (root)) +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (root->fs_info))return -EROFS ;if (flags &IOCB_NOWAIT )return -EAGAIN ;
@@ -9316,7 +9316,7 @@ static int btrfs_permission(const struct mnt_idmap *idmap,if (mask &MAY_WRITE && (S_ISREG (mode) ||S_ISDIR (mode) ||S_ISLNK (mode))) { -if (btrfs_root_readonly (root)) +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (root->fs_info))return -EROFS ;if (BTRFS_I (inode)->flags &BTRFS_INODE_READONLY )return -EACCES ;
fs/btrfs/ioctl.c +4 -4
@@ -291,7 +291,7 @@ int btrfs_fileattr_set(const struct mnt_idmap *idmap,u32 inode_flags;bool prop_set = false; -if (btrfs_root_readonly (root)) +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (fs_info))return -EROFS ;if (fileattr_has_fsx (fa))
@@ -2453,7 +2453,7 @@ static int btrfs_ioctl_defrag(struct file *file, void __user *argp)if (ret)return ret; -if (btrfs_root_readonly (root)) { +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (root->fs_info)) { ret = -EROFS ; goto out; }
@@ -3191,7 +3191,7 @@ static long btrfs_ioctl_dev_replace(struct btrfs_fs_info *fs_info,switch (p->cmd) {case BTRFS_IOCTL_DEV_REPLACE_CMD_START : -if (sb_rdonly (fs_info->sb)) +if (btrfs_fs_rdonly (fs_info))return -EROFS ;if (!btrfs_exclop_start (fs_info,BTRFS_EXCLOP_DEV_REPLACE )) { ret =BTRFS_ERROR_DEV_EXCL_RUN_IN_PROGRESS ;
@@ -3927,7 +3927,7 @@ static long _btrfs_ioctl_set_received_subvol(struct file *file, goto out; } -if (btrfs_root_readonly (root)) { +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (fs_info)) { ret = -EROFS ; goto out; }
fs/btrfs/messages.c +4 -4
@@ -132,10 +132,10 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function#endif /* -* Special case: if the error is EROFS, and we're already under -* SB_RDONLY, then it is safe here. +* Special case: if the error is EROFS, and we're already read-only, +* then it is safe here. */ -if (error == -EROFS &&sb_rdonly (sb)) +if (error == -EROFS &&btrfs_fs_rdonly (fs_info))return ;#ifdef CONFIG_PRINTK
@@ -168,7 +168,7 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *functionif (!(sb->s_flags &SB_BORN ))return ; -if (sb_rdonly (sb)) +if (btrfs_fs_rdonly (fs_info))return ;btrfs_discard_stop (fs_info);
fs/btrfs/qgroup.c +2 -2
@@ -4658,7 +4658,7 @@ static void qgroup_convert_meta(struct btrfs_fs_info *fs_info, u64 ref_root,qgroup_rsv_release (fs_info, qgroup, num_bytes,BTRFS_QGROUP_RSV_META_PREALLOC ); -if (!sb_rdonly (fs_info->sb)) +if (!btrfs_fs_rdonly (fs_info))qgroup_rsv_add (fs_info, qgroup, num_bytes,BTRFS_QGROUP_RSV_META_PERTRANS );
@@ -4688,7 +4688,7 @@ void btrfs_qgroup_convert_reserved_meta(struct btrfs_root *root, int num_bytes) BTRFS_QGROUP_RSV_META_PREALLOC);trace_btrfs_qgroup_meta_convert (root, num_bytes);qgroup_convert_meta (fs_info,btrfs_root_id (root), num_bytes); -if (!sb_rdonly (fs_info->sb)) +if (!btrfs_fs_rdonly (fs_info))add_root_meta_rsv (root, num_bytes,BTRFS_QGROUP_RSV_META_PERTRANS ); }
fs/btrfs/reflink.c +2 -1
@@ -857,7 +857,8 @@ static int btrfs_remap_file_range_prep(struct file *file_in, loff_t pos_in,if (!(remap_flags &REMAP_FILE_DEDUP )) {struct btrfs_root *root_out = inode_out->root; -if (btrfs_root_readonly (root_out)) +if (btrfs_root_readonly (root_out) || +btrfs_fs_rdonly (root_out->fs_info))return -EROFS ;ASSERT (inode_in->vfs_inode.i_sb == inode_out->vfs_inode.i_sb);
fs/btrfs/sysfs.c +2 -2
@@ -751,7 +751,7 @@ static ssize_t btrfs_force_chunk_alloc_store(struct kobject *kobj,if (!capable (CAP_SYS_ADMIN ))return -EPERM ; -if (sb_rdonly (fs_info->sb)) +if (btrfs_fs_rdonly (fs_info))return -EROFS ; ret =kstrtobool (buf, &val);
@@ -980,7 +980,7 @@ static ssize_t btrfs_label_store(struct kobject *kobj,if (!fs_info)return -EPERM ; -if (sb_rdonly (fs_info->sb)) +if (btrfs_fs_rdonly (fs_info))return -EROFS ;/*
fs/btrfs/tree-checker.c +1 -1
@@ -1325,7 +1325,7 @@ static int check_inode_item(struct extent_buffer *leaf, "unknown incompat flags detected: 0x%x", flags);return -EUCLEAN ; } -if (unlikely (!sb_rdonly (fs_info->sb) && +if (unlikely (!btrfs_fs_rdonly (fs_info) && (ro_flags & ~BTRFS_INODE_RO_FLAG_MASK ))) { inode_item_err(leaf, slot, "unknown ro-compat flags detected on writeable mount: 0x%x",
fs/btrfs/volumes.c +2 -2
@@ -2964,7 +2964,7 @@ int btrfs_init_new_device(struct btrfs_fs_info *fs_info, const char *device_pathbool seeding_dev = false;bool locked = false; -if (sb_rdonly (sb) && !fs_devices->seeding) +if (btrfs_fs_rdonly (fs_info) && !fs_devices->seeding)return -EROFS ;/* Forbid freezing until the device is a committed member (or unwound). */
@@ -5297,7 +5297,7 @@ int btrfs_cancel_balance(struct btrfs_fs_info *fs_info)* mount time if the mount is read-write. Otherwise it's still paused * and we must not allow cancelling as it deletes the item. */ -if (sb_rdonly (fs_info->sb)) { +if (btrfs_fs_rdonly (fs_info)) {mutex_unlock (&fs_info->balance_mutex);return -EROFS ; }
fs/btrfs/xattr.c +6 -2
@@ -365,7 +365,9 @@ static int btrfs_xattr_handler_set(const struct xattr_handler *handler, const char *name, const void *buffer, size_t size, int flags) { - if (btrfs_root_readonly(BTRFS_I(inode)->root)) + struct btrfs_root *root = BTRFS_I(inode)->root; + +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (root->fs_info))return -EROFS ; name =xattr_full_name (handler, name);
@@ -409,7 +411,9 @@ static int btrfs_xattr_handler_set_security(const struct xattr_handler *handler,const void *buffer, size_t size, int flags) { - if (btrfs_root_readonly(BTRFS_I(inode)->root)) + struct btrfs_root *root = BTRFS_I(inode)->root; + +if (btrfs_root_readonly (root) ||btrfs_fs_rdonly (root->fs_info))return -EROFS ; name =xattr_full_name (handler, name);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 25/33] btrfs: check BTRFS_FS_STATE_RO instead of SB_RDONLY at runtime
Sashiko has reviewed this patch and found no issues.
Quoted 15 lines
Add
btrfs_fs_rdonly()which testsBTRFS_FS_STATE_ROand use it instead ofsb_rdonly()wherever btrfs checks for a read-only filesystem at
runtime: the error handler, read repair, async discard,close_ctree(),btrfs_inc_block_group_ro(),check_inode_item(), device replace and add, cancelling a balance, the sysfs label andforce_chunk_allocstores, the qgroup PERTRANS conversions and the subvolume orphan cleanup. And check it next to everybtrfs_root_readonly()check that guards a modification, e.g. inbtrfs_permission(),btrfs_setattr(),btrfs_update_time(), the xattr and ACL handlers,btrfs_fileattr_set()andbtrfs_remap_file_range_prep().
BTRFS_FS_STATE_ROhas always been set and cleared together withSB_RDONLY. The remainingsb_rdonly()users are mount and remount paths plusbtrfs_is_full_ro()which only matters for rescue mounts.
Quoted 6 lines
This prepares for not setting
SB_RDONLYfrom the error handler. Until then the only difference is thatftruncate()and FICLONE through files that were opened for writing before a forced remount to read-only, e.g. by sysrq u, fail with EROFS now instead of modifying the filesystem.Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=25
Christian Brauner
maintainerPATCH v3 26/33 btrfs: don't set SB_RDONLY after filesystem errors
In reply to Christian Brauner__btrfs_handle_fs_error() sets SB_RDONLY through btrfs_set_sb_rdonly() without holding s_umount. It runs from any context and every transaction abort ends up there. That races with everyone else changing sb->s_flags. And if it happens while the filesystem is frozen thaw_super() sees a read-only superblock and leaves the s_writers rwsems locked. Today only btrfs_zone_finish_endio_workfn() can get there after ->freeze_fs() but that's luck.
btrfs doesn't need SB_RDONLY for any of this. fs_error already stops new transactions and BTRFS_FS_STATE_RO is what btrfs checks since the previous patch. So only set that like ext4 and f2fs do and call sb_set_emergency_ro() so the VFS refuses modifications and reports the superblock as read-only. That's not done for rescue mounts or a superblock that already carries the error flag. btrfs_force_shutdown() sets BTRFS_FS_STATE_RO too but not the VFS state, so opening a file for writing fails with EROFS instead of EIO after a shutdown.
A remount to read-only now actually happens after an error, so btrfs_remount_ro() skips the commit and the unused block group pass that can't succeed anymore. Going read-write fails with EROFS instead of EINVAL. The VFS doesn't call ->sync_fs in that state and refuses a new freeze with EROFS, so BTRFS_IOC_SHUTDOWN shuts down without the freeze after an error. btrfs_freeze() still skips its commit after a shutdown or when the error races the freeze, and the thaw skips check_dev_super() because the superblocks on disk may not match after an error.
A second read-write mount of the filesystem succeeds now. A non-forced remount to read-only fails with EBUSY while files that were opened for writing before the error are still open.
--- fs/btrfs/fs.h | 16 ++++++---------- fs/btrfs/ioctl.c | 5 +++++ fs/btrfs/messages.c | 8 ++++++-- fs/btrfs/super.c | 35 +++++++++++++++++++++++++++++++---- 4 files changed, 48 insertions(+), 16 deletions(-)
fs/btrfs/fs.h +6 -10
@@ -1195,9 +1195,8 @@ static inline int btrfs_need_cleaner_sleep(const struct btrfs_fs_info *fs_info) }/* -* Check whether the filesystem is read-only. BTRFS_FS_STATE_RO is set and -* cleared together with SB_RDONLY on mount, on remount and when an error forces -* the filesystem read-only. Use this instead of sb_rdonly() at runtime. +* Set and cleared with SB_RDONLY on mount and remount but set alone after an +* error. Use this instead of sb_rdonly() at runtime. */ static inline bool btrfs_fs_rdonly (const struct btrfs_fs_info *fs_info) {
@@ -1233,15 +1232,12 @@ static inline bool btrfs_is_shutdown(const struct btrfs_fs_info *fs_info)static inline void btrfs_force_shutdown (struct btrfs_fs_info *fs_info) {/* -* Here we do not want to use handle_fs_error(), which will mark the fs -* read-only. -* Some call sites like shutdown ioctl will mark the fs shutdown when -* the fs is frozen. But thaw path will handle RO and RW fs -* differently. -* -* So here we only mark the fs error without flipping it RO. +* A shutdown isn't an error, so don't use btrfs_handle_fs_error(). +* BTRFS_FS_STATE_RO doesn't touch SB_RDONLY, so it's fine while the +* filesystem is frozen, e.g. for the shutdown ioctl. */ WRITE_ONCE (fs_info->fs_error, -EIO ); +set_bit (BTRFS_FS_STATE_RO , &fs_info->fs_state);if (!test_and_set_bit (BTRFS_FS_STATE_EMERGENCY_SHUTDOWN , &fs_info->fs_state)) {btrfs_crit (fs_info,"emergency shutdown" );fserror_report_shutdown (fs_info->sb,GFP_KERNEL );
fs/btrfs/ioctl.c +5 -0
@@ -5165,6 +5165,11 @@ static int btrfs_ioctl_shutdown(struct btrfs_fs_info *fs_info, unsigned long argif (btrfs_is_shutdown (fs_info))return 0 ; +/* After an error there is nothing to flush and freezing fails. */ +if (flags !=BTRFS_SHUTDOWN_FLAGS_NOLOGFLUSH && +sb_emergency_ro (fs_info->sb)) + flags =BTRFS_SHUTDOWN_FLAGS_NOLOGFLUSH ; +switch (flags) {case BTRFS_SHUTDOWN_FLAGS_LOGFLUSH :case BTRFS_SHUTDOWN_FLAGS_DEFAULT :
fs/btrfs/messages.c +6 -2
@@ -173,8 +173,12 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *functionbtrfs_discard_stop (fs_info); -/* Handle error by forcing the filesystem readonly. */ -btrfs_set_sb_rdonly (sb); +/* +* Handle error by forcing the filesystem readonly. SB_RDONLY needs +* s_umount, see sb_set_emergency_ro(). +*/ +set_bit (BTRFS_FS_STATE_RO , &fs_info->fs_state); +sb_set_emergency_ro (sb);btrfs_info (fs_info,"forced readonly" );/* * Note that a running device replace operation is not canceled here
fs/btrfs/super.c +31 -4
@@ -1041,6 +1041,18 @@ int btrfs_sync_fs(struct super_block *sb, int wait)return btrfs_commit_transaction (trans); } +/* +* Nothing can be committed after an error, don't fail syncfs(2) and freezing. +* BTRFS_IOC_SYNC calls btrfs_sync_fs() directly and still gets the error. +*/ +static int btrfs_super_sync_fs (struct super_block *sb,int wait) +{ +if (btrfs_fs_rdonly (btrfs_sb (sb))) +return 0 ; + +return btrfs_sync_fs (sb, wait); +} +static void print_rescue_option (struct seq_file *seq,const char *s,bool *printed) {seq_printf (seq,"%s%s" , (*printed) ?":" :",rescue=" , s);
@@ -1286,7 +1298,7 @@ static int btrfs_remount_rw(struct btrfs_fs_info *fs_info)if (unlikely (BTRFS_FS_ERROR (fs_info))) {btrfs_err (fs_info,"remounting read-write after error is not allowed" ); -return -EINVAL ; +return -EROFS ; }if (fs_info->fs_devices->rw_devices ==0 )
@@ -1348,9 +1360,11 @@ static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)* Setting SB_RDONLY will put the cleaner thread to sleep at the next * loop if it's already active. If it's already asleep, we'll leave * unused block groups on disk until we're mounted read-write again -* unless we clean them up here. +* unless we clean them up here. Not after an error though, that can't be +* committed anymore and may still finish zones. */ -btrfs_delete_unused_bgs (fs_info); +if (!BTRFS_FS_ERROR (fs_info)) +btrfs_delete_unused_bgs (fs_info);/* * The cleaner task could be already running before we set the flag
@@ -1385,6 +1399,10 @@ static int btrfs_remount_ro(struct btrfs_fs_info *fs_info)*/ btrfs_qgroup_wait_for_completion (fs_info, false); +/* Nothing can be committed after an error, don't fail the remount. */ +if (unlikely (BTRFS_FS_ERROR (fs_info))) +return 0 ; +return btrfs_commit_super (fs_info); }
@@ -2308,6 +2326,9 @@ static int btrfs_freeze(struct super_block *sb)struct btrfs_fs_info *fs_info =btrfs_sb (sb);set_bit (BTRFS_FS_FROZEN , &fs_info->flags); +/* Nothing can be committed after an error. */ +if (btrfs_fs_rdonly (fs_info)) +return 0 ;/* * We don't need a barrier here, we'll wait for any transaction that * could be in progress on other threads (and do delayed iputs that
@@ -2382,7 +2403,12 @@ static int btrfs_unfreeze(struct super_block *sb)* * And since the fs is frozen, no one can modify the fs yet, thus * we don't need to hold device_list_mutex. +* +* After an error the super blocks on disk may lag behind, skip it. */ +if (btrfs_fs_rdonly (fs_info)) + goto out; +list_for_each_entry (device, &fs_info->fs_devices->devices, dev_list) { ret =check_dev_super (device);if (ret <0 ) {
@@ -2392,6 +2418,7 @@ static int btrfs_unfreeze(struct super_block *sb)break ; } } +out:clear_bit (BTRFS_FS_FROZEN , &fs_info->flags);/*
@@ -2522,7 +2549,7 @@ static const struct super_operations btrfs_super_ops = { .drop_inode = btrfs_drop_inode, .evict_inode = btrfs_evict_inode, .put_super = btrfs_put_super, - .sync_fs = btrfs_sync_fs, + .sync_fs = btrfs_super_sync_fs, .show_options = btrfs_show_options, .show_devname = btrfs_show_devname, .alloc_inode = btrfs_alloc_inode,
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 26/33] btrfs: don't set SB_RDONLY after filesystem errors
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [Medium] btrfs: btrfs_super_sync_fs() and btrfs_freeze() return 0 on shutdown or error - [Medium] btrfs…
Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [Medium] btrfs: btrfs_super_sync_fs() and btrfs_freeze() return 0 on shutdown or error
- [Medium] btrfs: race between BTRFS_FS_STATE_RO and sb_set_emergency_ro() on fs error
--
commit 3d297b218ce5c34d64ff204c632eaa6ac7a2526b
Author: Christian Brauner <[email protected]>
btrfs: don't set SB_RDONLY after filesystem errors
This commit stops setting SB_RDONLY in __btrfs_handle_fs_error(), using BTRFS_FS_STATE_RO and sb_set_emergency_ro() instead, and updates shutdown, remount, sync_fs, and freeze handling for the new read-only state.
Quoted 4 lines
fs/btrfs/fs.h +0
-0
[ ... ]
Quoted 20 lines
fs/btrfs/fs.h +4
-7@@ -1233,15 +1232,12 @@ static inline bool btrfs_is_shutdown(const struct btrfs_fs_info *fs_info)static inline void btrfs_force_shutdown (struct btrfs_fs_info *fs_info) {/* -* Here we do not want to use handle_fs_error(), which will mark the fs -* read-only. -* Some call sites like shutdown ioctl will mark the fs shutdown when -* the fs is frozen. But thaw path will handle RO and RW fs -* differently. -* -* So here we only mark the fs error without flipping it RO. +* A shutdown isn't an error, so don't use btrfs_handle_fs_error(). +* BTRFS_FS_STATE_RO doesn't touch SB_RDONLY, so it's fine while the +* filesystem is frozen, e.g. for the shutdown ioctl. */ WRITE_ONCE (fs_info->fs_error, -EIO ); +set_bit (BTRFS_FS_STATE_RO , &fs_info->fs_state);if (!test_and_set_bit (BTRFS_FS_STATE_EMERGENCY_SHUTDOWN , &fs_info->fs_state)) {btrfs_crit (fs_info,"emergency shutdown" );fserror_report_shutdown (fs_info->sb,GFP_KERNEL );
[ ... ]
Quoted 7 lines
fs/btrfs/messages.c +0
-0@@ -173,8 +173,12 @@ void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *functionbtrfs_discard_stop (fs_info);
fs/btrfs/messages.c void __btrfs_handle_fs_error(struct btrfs_fs_info *fs_info, const char *function · line 182
Quoted 10 lines
fs/btrfs/messages.c +6
-2-/* Handle error by forcing the filesystem readonly. */ -btrfs_set_sb_rdonly (sb); +/* +* Handle error by forcing the filesystem readonly. SB_RDONLY needs +* s_umount, see sb_set_emergency_ro(). +*/ +set_bit (BTRFS_FS_STATE_RO , &fs_info->fs_state); +sb_set_emergency_ro (sb);btrfs_info (fs_info,"forced readonly" );
[Severity: Medium]
Can setting BTRFS_FS_STATE_RO before sb_set_emergency_ro(sb) let concurrent callers observe btrfs_fs_rdonly() == true while sb_emergency_ro(sb) is still false?
set_bit() is a relaxed atomic operation and __btrfs_handle_fs_error() returns early when btrfs_fs_rdonly(fs_info) is already set:
CPU0 CPU1__btrfs_handle_fs_error()
set_bit(BTRFS_FS_STATE_RO, ...)
ksys_syncfs()
sync_filesystem(sb)
btrfs_super_sync_fs(sb, 1)
if (btrfs_fs_rdonly(...))
return 0;
if (sb_emergency_ro(sb))
/* still false, returns 0 */ sb_set_emergency_ro(sb)
During this window, syncfs(2) and freeze_super() can see btrfs_fs_rdonly() == true in btrfs_super_sync_fs() and sb_emergency_ro(sb) == false in the VFS, so they return 0 without syncing instead of failing with -EROFS. Similarly, a second thread entering __btrfs_handle_fs_error() sees btrfs_fs_rdonly(fs_info) and returns early to its caller before CPU0 sets SB_STATE_EMERGENCY_RO, so a subsequent btrfs_ioctl_shutdown() or VFS call on CPU1 can still observe sb_emergency_ro(sb) == false.
Quoted 19 lines
fs/btrfs/super.c +11
-0@@ -1041,6 +1041,18 @@ int btrfs_sync_fs(struct super_block *sb, int wait)return btrfs_commit_transaction (trans); } +/* +* Nothing can be committed after an error, don't fail syncfs(2) and freezing. +* BTRFS_IOC_SYNC calls btrfs_sync_fs() directly and still gets the error. +*/ +static int btrfs_super_sync_fs (struct super_block *sb,int wait) +{ +if (btrfs_fs_rdonly (btrfs_sb (sb))) +return 0 ; + +return btrfs_sync_fs (sb, wait); +}
[ ... ]
fs/btrfs/super.c static int btrfs_freeze(struct super_block *sb) · line 2334
Quoted 10 lines
fs/btrfs/super.c +3
-0@@ -2308,6 +2326,9 @@ static int btrfs_freeze(struct super_block *sb)struct btrfs_fs_info *fs_info =btrfs_sb (sb);set_bit (BTRFS_FS_FROZEN , &fs_info->flags); +/* Nothing can be committed after an error. */ +if (btrfs_fs_rdonly (fs_info)) +return 0 ;/* * We don't need a barrier here, we'll wait for any transaction that * could be in progress on other threads (and do delayed iputs that
[Severity: Medium]
Does returning 0 when btrfs_fs_rdonly() is true cause syncfs(2) and freeze_super() to succeed without flushing after a forced shutdown?
btrfs_force_shutdown() now sets BTRFS_FS_STATE_RO without calling sb_set_emergency_ro(sb). After BTRFS_IOC_SHUTDOWN with BTRFS_SHUTDOWN_FLAGS_NOLOGFLUSH (or btrfs_shutdown()), dirty data and transactions were not flushed, yet sb_emergency_ro(sb) and sb_rdonly(sb) are both false. Calling syncfs(2) or FIFREEZE then reaches btrfs_super_sync_fs() and btrfs_freeze(), which see btrfs_fs_rdonly() == true and return 0 instead of failing as btrfs_sync_fs() and btrfs_commit_current_transaction() did via BTRFS_FS_ERROR(fs_info).
In addition, if an in-flight transaction aborts while freeze_super() is waiting in sb_wait_write(sb, SB_FREEZE_FS) (after freeze_super() already checked sb_emergency_ro(sb)), btrfs_freeze() now returns 0 and lets the freeze succeed on an errored filesystem. The comment above btrfs_super_sync_fs() also says not to fail syncfs(2) and freezing after an error, whereas the VFS fails both with -EROFS when sb_emergency_ro(sb) is set.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=26
Christian Brauner
maintainerPATCH v3 27/33 gfs2: wait for a pending withdraw before remounting read-only
In reply to Christian BraunerSince commit 9c4a3de6cd5b ("gfs2: Asynchronous withdraw") most of a withdraw happens in do_withdraw() from a work item and SDF_JOURNAL_LIVE stays set until then. A remount to read-only in that window bails out of its log flushes on the withdrawn filesystem and gfs2_quota_cleanup() hits its BUG_ON(). Flushing the work isn't enough because gfs2_withdraw() only queues it after setting SDF_WITHDRAWN and a withdraw can also start in the middle of gfs2_make_fs_ro().
So let do_withdraw() wake up waiters when it clears SDF_JOURNAL_LIVE and let gfs2_make_fs_ro() wait for that right before gfs2_quota_cleanup(). umount doesn't need to wait because it sets SDF_NORECOVERY first.
gfs2_make_fs_rw() could leave the bit set on a withdrawn filesystem too if do_withdraw() ran between its withdraw check and setting the bit. So do both under sd_log_flush_lock which do_withdraw() holds while it tests the bit.
--- fs/gfs2/super.c | 20 ++++++++++++++++---- fs/gfs2/util.c | 2 +- 2 files changed, 17 insertions(+), 5 deletions(-)
fs/gfs2/super.c +16 -4
@@ -146,12 +146,21 @@ int gfs2_make_fs_rw(struct gfs2_sbd *sdp) } error =gfs2_quota_init (sdp); -if (!error &&gfs2_withdrawn (sdp)) { -gfs2_quota_cleanup (sdp); +if (error) +return error; + +/* +* Check for a withdraw under the lock do_withdraw() holds while it +* takes a live journal down, so one of them sees the other. +*/ +down_write (&sdp->sd_log_flush_lock); +if (gfs2_withdrawn (sdp)) error = -EIO ; - } -if (!error) +else set_bit (SDF_JOURNAL_LIVE , &sdp->sd_flags); +up_write (&sdp->sd_log_flush_lock); +if (error) +gfs2_quota_cleanup (sdp);return error; }
@@ -567,6 +576,9 @@ void gfs2_make_fs_ro(struct gfs2_sbd *sdp) HZ * 5);gfs2_assert_warn (sdp,gfs2_log_is_empty (sdp)); } +/* A withdraw that do_withdraw() didn't finish yet leaves it live. */ +if (!test_bit (SDF_NORECOVERY , &sdp->sd_flags)) +wait_on_bit (&sdp->sd_flags,SDF_JOURNAL_LIVE ,TASK_UNINTERRUPTIBLE );gfs2_quota_cleanup (sdp); }
fs/gfs2/util.c +1 -1
@@ -121,7 +121,7 @@ static void do_withdraw(struct gfs2_sbd *sdp)up_write (&sdp->sd_log_flush_lock);return ; } -clear_bit (SDF_JOURNAL_LIVE , &sdp->sd_flags); +clear_and_wake_up_bit (SDF_JOURNAL_LIVE , &sdp->sd_flags);gfs2_ail_drain (sdp);/* frees all transactions */ up_write (&sdp->sd_log_flush_lock);
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 27/33] gfs2: wait for a pending withdraw before remounting read-only
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [High] gfs2: hang in gfs2_make_fs_ro() if withdraw occurs before SB_BORN --
Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [High] gfs2: hang in gfs2_make_fs_ro() if withdraw occurs before SB_BORN
--
commit f8b5b1daf48e3f825a415215536a375fb6cd8b9d
Author: Christian Brauner <[email protected]>
gfs2: wait for a pending withdraw before remounting read-only
This commit serializes setting SDF_JOURNAL_LIVE in gfs2_make_fs_rw() with do_withdraw() under sd_log_flush_lock, and makes gfs2_make_fs_ro() wait for SDF_JOURNAL_LIVE to clear before calling gfs2_quota_cleanup().
Quoted 31 lines
fs/gfs2/super.c +13
-4@@ -146,12 +146,21 @@ int gfs2_make_fs_rw(struct gfs2_sbd *sdp) } error =gfs2_quota_init (sdp); -if (!error &&gfs2_withdrawn (sdp)) { -gfs2_quota_cleanup (sdp); +if (error) +return error; + +/* +* Check for a withdraw under the lock do_withdraw() holds while it +* takes a live journal down, so one of them sees the other. +*/ +down_write (&sdp->sd_log_flush_lock); +if (gfs2_withdrawn (sdp)) error = -EIO ; - } -if (!error) +else set_bit (SDF_JOURNAL_LIVE , &sdp->sd_flags); +up_write (&sdp->sd_log_flush_lock); +if (error) +gfs2_quota_cleanup (sdp);return error; }
fs/gfs2/super.c void gfs2_make_fs_ro(struct gfs2_sbd *sdp) · line 583
Quoted 8 lines
fs/gfs2/super.c +3
-0@@ -567,6 +576,9 @@ void gfs2_make_fs_ro(struct gfs2_sbd *sdp) HZ * 5);gfs2_assert_warn (sdp,gfs2_log_is_empty (sdp)); } +/* A withdraw that do_withdraw() didn't finish yet leaves it live. */ +if (!test_bit (SDF_NORECOVERY , &sdp->sd_flags)) +wait_on_bit (&sdp->sd_flags,SDF_JOURNAL_LIVE ,TASK_UNINTERRUPTIBLE );gfs2_quota_cleanup (sdp); }
[Severity: High]
Can wait_on_bit() in gfs2_make_fs_ro() hang forever if a withdraw happens during initial mount after gfs2_make_fs_rw() sets SDF_JOURNAL_LIVE, but before vfs_get_tree() sets SB_BORN?
During initial read-write mount, gfs2_fill_super() starts the logd and quotad threads, registers the sysfs interface, and calls gfs2_make_fs_rw() before SB_BORN is set on sb->s_flags. If gfs2_withdraw() runs after gfs2_make_fs_rw() sets SDF_JOURNAL_LIVE and before vfs_get_tree() sets SB_BORN, it sets SDF_WITHDRAWN and returns early without scheduling sd_withdraw_work:
fs/gfs2/util.c:gfs2_withdraw() {
...
if (test_and_set_bit(SDF_WITHDRAWN, &sdp->sd_flags))
return;
dump_stack();
/*
* There is no need to withdraw when the superblock hasn't been
* fully initialized, yet.
*/
if (!(sdp->sd_vfs->s_flags & SB_BORN))
return;
fs_err(sdp, "about to withdraw this file system\n");
schedule_work(&sdp->sd_withdraw_work);
...
}CPU0 (mount / remount) CPU1 (kthread / workqueue / sysfs)vfs_get_tree()
...
gfs2_fill_super()
gfs2_make_fs_rw()
set_bit(SDF_JOURNAL_LIVE)
gfs2_withdraw()
test_and_set_bit(SDF_WITHDRAWN)
if (!(s_flags & SB_BORN))
return; sb->s_flags |= SB_BORN
gfs2_reconfigure()
gfs2_make_fs_ro()
gfs2_log_flush()
if (gfs2_withdrawn(sdp))
return;
wait_on_bit(..., SDF_JOURNAL_LIVE, ...)
<-- sleeps forever holding sb->s_umountBecause SDF_WITHDRAWN is already set, later calls to gfs2_withdraw() also return early, so do_withdraw() never runs to clear SDF_JOURNAL_LIVE. A subsequent read-only remount bails out of gfs2_log_flush() and blocks forever in wait_on_bit() with sb->s_umount held in write mode.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=27
Christian Brauner
maintainerPATCH v3 28/33 gfs2: don't set SB_RDONLY on withdraw
In reply to Christian Braunerdo_withdraw() sets SB_RDONLY from the withdraw work without holding s_umount, also on a frozen filesystem. FITHAW then fails because a withdrawn filesystem can't take the freeze glock. Emergency thaw and the gfs2 "freeze" sysfs file see SB_RDONLY and leave the s_writers rwsems locked. Writers hang and umount warns. And freezing after a withdraw fails and leaves the superblock frozen.
SDF_WITHDRAWN already stops all modifications, so stop setting SB_RDONLY. A few things have to come with that:
(1) gfs2_evict_inode() and gfs2_quota_sync() check gfs2_withdrawn() next to sb_rdonly().
(2) gfs2_write_inode() returns EIO once withdrawn because sync and umount don't skip writeback for a read-only superblock anymore.
(3) gfs2_do_thaw() thaws even if the freeze glock can't be taken again.
(4) gfs2_freeze_super() refuses a withdrawn filesystem with EIO and thaws the superblock itself if the withdraw happens while it tries to freeze.
This can't be split up. With only (3) FITHAW leaks the rwsems of the read-only superblock. With only the SB_RDONLY change FITHAW can't release the rwsems a freeze took after a withdraw.
A remount to read-only writes nothing once do_withdraw() ran and waits for it since the previous patch, so sysrq u works on a withdrawn gfs2. Going read-write still fails with EIO. A withdrawn filesystem stays "rw" in /proc/mounts and statfs() doesn't report ST_RDONLY anymore. The withdraw file in sysfs still shows it.
--- fs/gfs2/quota.c | 2 +- fs/gfs2/super.c | 20 ++++++++++++++++++-- fs/gfs2/util.c | 5 +++-- 3 files changed, 22 insertions(+), 5 deletions(-)
fs/gfs2/quota.c +1 -1
@@ -1335,7 +1335,7 @@ int gfs2_quota_sync(struct super_block *sb, int type)u64 sync_gen;int error =0 ; -if (sb_rdonly (sdp->sd_vfs)) +if (sb_rdonly (sdp->sd_vfs) ||gfs2_withdrawn (sdp))return 0 ; qda = kzalloc_objs(struct gfs2_quota_data *, max_qd);
fs/gfs2/super.c +18 -2
@@ -455,6 +455,9 @@ static int gfs2_write_inode(struct inode *inode, struct writeback_control *wbc)int ret =0 ;bool flush_all = (wbc->sync_mode ==WB_SYNC_ALL ||gfs2_is_jdata (ip)); +if (gfs2_withdrawn (sdp)) +return -EIO ; +if (flush_all) gfs2_log_flush(GFS2_SB(inode), gl, GFS2_LOG_HEAD_FLUSH_NORMAL |
@@ -686,7 +689,8 @@ static int gfs2_do_thaw(struct gfs2_sbd *sdp, enum freeze_holder who, const voidint error; error =gfs2_freeze_lock_shared (sdp); -if (error) +/* A withdrawn filesystem can't get the freeze glock back, thaw anyway. */ +if (error && !gfs2_withdrawn (sdp)) goto fail; error =thaw_super (sb, who, freeze_owner);if (!error)
@@ -751,6 +755,11 @@ static int gfs2_freeze_super(struct super_block *sb, enum freeze_holder who,mutex_unlock (&sdp->sd_freeze_mutex);return -EBUSY ; } +/* A withdrawn filesystem can't take the freeze glock anymore. */ +if (gfs2_withdrawn (sdp)) { +mutex_unlock (&sdp->sd_freeze_mutex); +return -EIO ; + }for (;;) { error =freeze_super (sb, who, freeze_owner);
@@ -767,6 +776,13 @@ static int gfs2_freeze_super(struct super_block *sb, enum freeze_holder who,break ; } +/* Withdrawn meanwhile, don't take the freeze glock again. */ +if (gfs2_withdrawn (sdp)) { + (void )thaw_super (sb, who, freeze_owner); + error = -EIO ; + goto out; + } + (void )gfs2_do_thaw (sdp, who, freeze_owner);if (error == -EBUSY )
@@ -1488,7 +1504,7 @@ static void gfs2_evict_inode(struct inode *inode)int ret;gfs2_holder_mark_uninitialized (&gh); -if (sb_rdonly (sb) || !ip->i_no_addr || !gl) +if (sb_rdonly (sb) ||gfs2_withdrawn (sdp) || !ip->i_no_addr || !gl) goto out;/*
fs/gfs2/util.c +3 -2
@@ -132,9 +132,10 @@ static void do_withdraw(struct gfs2_sbd *sdp)gfs2_log_is_empty (sdp), HZ * 5); - sdp->sd_vfs->s_flags |=SB_RDONLY ; -/* +* No SB_RDONLY, that needs s_umount. SDF_WITHDRAWN already stops all +* modifications. +* * Dequeue any pending non-system glock holders that can no * longer be granted because the file system is withdrawn. */
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 28/33] gfs2: don't set SB_RDONLY on withdraw
Sashiko has reviewed this patch and found no issues.
Quoted 17 lines
do_withdraw()setsSB_RDONLYfrom the withdraw work without holdings_umount, also on a frozen filesystem. FITHAW then fails because a withdrawn filesystem can't take the freeze glock. Emergency thaw and the gfs2 "freeze" sysfs file seeSB_RDONLYand leave thes_writersrwsems locked. Writers hang and umount warns. And freezing after a withdraw fails and leaves the superblock frozen.
SDF_WITHDRAWNalready stops all modifications, so stop settingSB_RDONLY. A few things have to come with that:(1)
gfs2_evict_inode()andgfs2_quota_sync()checkgfs2_withdrawn()next tosb_rdonly().
(2)gfs2_write_inode()returns EIO once withdrawn because sync and umount don't skip writeback for a read-only superblock anymore.
(3)gfs2_do_thaw()thaws even if the freeze glock can't be taken again. [ ... ]
Quoted 4 lines
Fixes 601ef0d52e96 ("gfs2: Force withdraw to replay journals and wait for it to finish")Fixes b77b4a4815a9 ("gfs2: Rework freeze / thaw logic")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=28
Christian Brauner
maintainerPATCH v3 29/33 efivarfs: stop using SB_RDONLY for read-only efivar ops
In reply to Christian BraunerSince commit 94f7f6182c72 ("efivarfs: automatically update super block flag") efivarfs_ops_notifier() sets or clears SB_RDONLY whenever the efivar ops switch between ops that can and ops that can't set variables, e.g. when tee_stmm_efi is probed or removed. It does that at any time without holding s_umount. efivarfs is frozen on every suspend and if the ops switch while it's frozen thaw_super() either leaves the s_writers rwsems locked or releases rwsems nobody holds.
So keep the state in efivarfs. The notifier sets and clears a new EFIVARFS_OPS_RDONLY flag instead and efivarfs_fill_super() initializes it under efivar_lock() so a switch can't slip in. fill_super doesn't set SB_RDONLY either. systemd mounts efivarfs before tee_stmm_efi is loaded and the mount has to become writable then. create, unlink, setattr, fileattr_set, write and opening for write fail with EROFS while the flag is set. Like the SB_RDONLY check this runs without efivars_lock so a write racing with a switch can still call a NULL ->set_variable. That's not new.
efivarfs_reconfigure() only refuses a remount from read-only to read-write while the flag is set and returns EROFS. It used to refuse every remount without "ro" which would break option-only remounts.
With read-only ops efivarfs is mounted "rw" now. statfs() doesn't report ST_RDONLY anymore and access(W_OK) succeeds.
--- fs/efivarfs/file.c | 6 ++++++ fs/efivarfs/inode.c | 15 +++++++++++++++ fs/efivarfs/internal.h | 14 +++++++++++++- fs/efivarfs/super.c | 26 ++++++++++++++++++-------- 4 files changed, 52 insertions(+), 9 deletions(-)
fs/efivarfs/file.c +6 -0
@@ -23,6 +23,9 @@ static ssize_t efivarfs_file_write(struct file *file,ssize_t bytes;bool set = false; +if (efivarfs_ops_rdonly (inode->i_sb)) +return -EROFS ; +if (count <sizeof (attributes))return -EINVAL ;
@@ -137,6 +140,9 @@ static int efivarfs_file_open(struct inode *inode, struct file *file) {struct efivar_entry *entry = inode->i_private; +if ((file->f_mode &FMODE_WRITE ) &&efivarfs_ops_rdonly (inode->i_sb)) +return -EROFS ; + file->private_data = entry;inode_lock (inode);
fs/efivarfs/inode.c +15 -0
@@ -15,6 +15,8 @@#include "internal.h" static const struct inode_operations efivarfs_file_inode_operations; +static int efivarfs_setattr (const struct mnt_idmap *idmap, +struct dentry *dentry,struct iattr *iattr); struct inode *efivarfs_get_inode(struct super_block *sb, const struct inode *dir, int mode,
@@ -83,6 +85,9 @@ static int efivarfs_create(const struct mnt_idmap *idmap, struct inode *dir,bool is_removable = false;efi_guid_t vendor; +if (efivarfs_ops_rdonly (dir->i_sb)) +return -EROFS ; +if (!efivarfs_valid_name (dentry->d_name.name, dentry->d_name.len))return -EINVAL ;
@@ -122,6 +127,9 @@ static int efivarfs_unlink(struct inode *dir, struct dentry *dentry) {struct efivar_entry *var =d_inode (dentry)->i_private; +if (efivarfs_ops_rdonly (dir->i_sb)) +return -EROFS ; +if (efivar_entry_delete (var))return -EINVAL ;
@@ -132,6 +140,7 @@ const struct inode_operations efivarfs_dir_inode_operations = { .lookup = simple_lookup, .unlink = efivarfs_unlink, .create = efivarfs_create, + .setattr = efivarfs_setattr, }; static int
@@ -155,6 +164,9 @@ efivarfs_fileattr_set(const struct mnt_idmap *idmap, {unsigned int i_flags =0 ; +if (efivarfs_ops_rdonly (dentry->d_sb)) +return -EROFS ; +if (fileattr_has_fsx (fa))return -EOPNOTSUPP ;
@@ -176,6 +188,9 @@ static int efivarfs_setattr(const struct mnt_idmap *idmap, struct dentry *dentrystruct inode *inode =d_inode (dentry);int error; +if (efivarfs_ops_rdonly (inode->i_sb)) +return -EROFS ; + error =setattr_prepare (idmap, dentry, iattr);if (error)return error;
fs/efivarfs/internal.h +13 -1
@@ -16,10 +16,22 @@ struct efivarfs_mount_opts {struct efivarfs_fs_info {struct efivarfs_mount_opts mount_opts; -struct super_block *sb;struct notifier_block nb; +unsigned long flags; }; +/* efivarfs_fs_info->flags */ +enum { +EFIVARFS_OPS_RDONLY ,/* the registered efivar ops can't set variables */ +}; + +static inline bool efivarfs_ops_rdonly (const struct super_block *sb) +{ +const struct efivarfs_fs_info *sfi = sb->s_fs_info; + +return test_bit (EFIVARFS_OPS_RDONLY , &sfi->flags); +} +struct efi_variable {efi_char16_t VariableName[EFI_VAR_NAME_LEN /sizeof (efi_char16_t)];efi_guid_t VendorGuid;
fs/efivarfs/super.c +18 -8
@@ -28,12 +28,16 @@ static int efivarfs_ops_notifier(struct notifier_block *nb, unsigned long event, { struct efivarfs_fs_info *sfi = container_of(nb, struct efivarfs_fs_info, nb); +/* +* This runs without s_umount so it can't touch SB_RDONLY. The +* modifying operations check EFIVARFS_OPS_RDONLY instead. +*/ switch (event) {case EFIVAR_OPS_RDONLY : - sfi->sb->s_flags |=SB_RDONLY ; +set_bit (EFIVARFS_OPS_RDONLY , &sfi->flags);break ;case EFIVAR_OPS_RDWR : - sfi->sb->s_flags &= ~SB_RDONLY ; +clear_bit (EFIVARFS_OPS_RDONLY , &sfi->flags);break ;default :return NOTIFY_DONE ;
@@ -384,9 +388,6 @@ static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc) sb->s_d_flags |=DCACHE_DONTCACHE ; sb->s_time_gran =1 ; -if (!efivar_supports_writes ()) - sb->s_flags |=SB_RDONLY ; - inode =efivarfs_get_inode (sb,NULL ,S_IFDIR |0755 ,0 , true);if (!inode)return -ENOMEM ;
@@ -397,9 +398,15 @@ static int efivarfs_fill_super(struct super_block *sb, struct fs_context *fc)if (!root)return -ENOMEM ; - sfi->sb = sb; +/* The ops can't switch between sampling them and registering. */ + err =efivar_lock (); +if (err) +return err; +if (!efivar_supports_writes ()) +set_bit (EFIVARFS_OPS_RDONLY , &sfi->flags); sfi->nb.notifier_call = efivarfs_ops_notifier; err =blocking_notifier_chain_register (&efivar_ops_nh, &sfi->nb); +efivar_unlock ();if (err)return err;
@@ -413,12 +420,15 @@ static int efivarfs_get_tree(struct fs_context *fc)static int efivarfs_reconfigure (struct fs_context *fc) { +struct super_block *sb = fc->root->d_sb;struct efivarfs_fs_info *sfi = fc->root->d_sb->s_fs_info;struct efivarfs_fs_info *new_sfi = fc->s_fs_info; -if (!efivar_supports_writes () && !(fc->sb_flags &SB_RDONLY )) { +/* Only refuse remounting a read-only mount read-write. */ +if ((fc->sb_flags_mask &SB_RDONLY ) && !(fc->sb_flags &SB_RDONLY ) && +sb_rdonly (sb) &&efivarfs_ops_rdonly (sb)) {pr_err ("Firmware does not support SetVariableRT. Can not remount with rw\n" ); -return -EINVAL ; +return -EROFS ; }/* statfs()/show_options() read nostatfs locklessly; benign race. */
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 29/33] efivarfs: stop using SB_RDONLY for read-only efivar ops
Sashiko has reviewed this patch and found no issues.
Quoted 22 lines
Since commit
94f7f6182c72("efivarfs: automatically update super block flag")efivarfs_ops_notifier()sets or clearsSB_RDONLYwhenever the efivar ops switch between ops that can and ops that can't set variables, e.g. whentee_stmm_efiis probed or removed. It does that at any time without holdings_umount. efivarfs is frozen on every suspend and if the ops switch while it's frozenthaw_super()either leaves thes_writersrwsems locked or releases rwsems nobody holds.So keep the state in efivarfs. The notifier sets and clears a new
EFIVARFS_OPS_RDONLYflag instead andefivarfs_fill_super()initializes it underefivar_lock()so a switch can't slip in.fill_superdoesn't setSB_RDONLYeither. systemd mounts efivarfs beforetee_stmm_efiis loaded and the mount has to become writable then. create, unlink, setattr,fileattr_set, write and opening for write fail with EROFS while the flag is set. Like theSB_RDONLYcheck this runs withoutefivars_lockso a write racing with a switch can still call a NULL ->set_variable. That's not new.
efivarfs_reconfigure()only refuses a remount from read-only to read-write while the flag is set and returns EROFS. It used to refuse every remount without "ro" which would break option-only remounts.
Quoted 6 lines
With read-only ops efivarfs is mounted "rw" now.
statfs()doesn't reportST_RDONLYanymore andaccess(W_OK)succeeds.Fixes 94f7f6182c72 ("efivarfs: automatically update super block flag")Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=29
Christian Brauner
maintainerPATCH v3 30/33 ubifs: don't set SB_RDONLY after errors
In reply to Christian Braunerubifs_ro_mode() sets c->ro_error and SB_RDONLY from whatever context hit the error, e.g. a failing write, and without holding s_umount. UBIFS can't be frozen but that still races with everyone else changing sb->s_flags. And c->ro_error alone already makes every write fail with EROFS.
So call sb_set_emergency_ro() instead. The background thread can hit the error before SB_BORN but doesn't hold s_umount either. The xattr setters check c->ro_error as well because ecryptfs calls them without write access on the lower mount and they change the inode in memory first.
So far a remount to read-only after an error was refused and a remount to read-write went through and cleared SB_RDONLY again because c->ro_mount was never set. Now going read-only works. Nothing is synced for it in that state and ubifs_remount_ro() skips the write-buffer syncs and the master node like ubifs_put_super() does and only cancels the write-buffer timers. Going read-write after that is refused by the VFS.
The flusher still gets to the folios and inodes that were dirty before the error, also after the remount which sets c->ro_mount and frees the write reserve buffer. Every journal write fails with EROFS after an error anyway. So do_writepage() and ubifs_write_inode() drop them without going near the journal. Otherwise reserve_space() trips its !c->ro_mount assert for every folio after the remount.
--- fs/ubifs/file.c | 12 ++++++++++++ fs/ubifs/io.c | 6 +++++- fs/ubifs/super.c | 40 ++++++++++++++++++++++++++-------------- fs/ubifs/xattr.c | 6 ++++++ 4 files changed, 49 insertions(+), 15 deletions(-)
fs/ubifs/file.c +12 -0
@@ -898,6 +898,13 @@ static int do_writepage(struct folio *folio, size_t len)folio_start_writeback (folio); +/* Nothing gets written after an error, drop the folio. */ +if (unlikely (c->ro_error)) { + err = -EROFS ; +mapping_set_error (folio->mapping, err); + goto out; + } + block = folio->index <<UBIFS_BLOCKS_PER_PAGE_SHIFT ;for (;;) { blen =min_t (size_t, len,UBIFS_BLOCK_SIZE );
@@ -918,6 +925,7 @@ static int do_writepage(struct folio *folio, size_t len)ubifs_ro_mode (c, err); } +out:ubifs_assert (c, folio->private !=NULL );if (folio_test_checked (folio))release_new_page_budget (c);
@@ -997,6 +1005,10 @@ static int ubifs_writepage(struct folio *folio, struct writeback_control *wbc) goto out_unlock; } +/* After an error do_writepage() drops the folio. */ +if (unlikely (c->ro_error)) +return do_writepage (folio, len); +spin_lock (&ui->ui_lock); synced_i_size = ui->synced_i_size;spin_unlock (&ui->ui_lock);
fs/ubifs/io.c +5 -1
@@ -72,7 +72,11 @@ void ubifs_ro_mode(struct ubifs_info *c, int err)if (!c->ro_error) { c->ro_error =1 ; c->no_chk_data_crc =0 ; - c->vfs_sb->s_flags |=SB_RDONLY ; +/* +* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). The +* background thread runs before SB_BORN already. +*/ +sb_set_emergency_ro (c->vfs_sb);ubifs_warn (c,"switched to read-only mode, error %d" , err);dump_stack (); }
fs/ubifs/super.c +26 -14
@@ -312,6 +312,12 @@ static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc)return 0 ; } +/* Nothing gets written after an error, drop the change. */ +if (unlikely (c->ro_error)) { + err = -EROFS ; + goto out; + } +/* * As an optimization, do not write orphan inodes to the media just * because this is not needed.
@@ -327,6 +333,7 @@ static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc) err =dbg_check_inode_size (c, inode, ui->ui_size); } +out: ui->dirty =0 ;mutex_unlock (&ui->ui_mutex);ubifs_release_dirty_inode_budget (c, ui);
@@ -1839,19 +1846,26 @@ static void ubifs_remount_ro(struct ubifs_info *c)dbg_save_space_info (c); -for (i =0 ; i < c->jhead_cnt; i++) { - err =ubifs_wbuf_sync (&c->jheads[i].wbuf); +/* Like ubifs_put_super(), don't write anything after an error. */ +if (!c->ro_error) { +for (i =0 ; i < c->jhead_cnt; i++) { + err =ubifs_wbuf_sync (&c->jheads[i].wbuf); +if (err) +ubifs_ro_mode (c, err); + } + + c->mst_node->flags &= ~cpu_to_le32 (UBIFS_MST_DIRTY ); + c->mst_node->flags |=cpu_to_le32 (UBIFS_MST_NO_ORPHS ); + c->mst_node->gc_lnum =cpu_to_le32 (c->gc_lnum); + err =ubifs_write_master (c);if (err)ubifs_ro_mode (c, err); + }else { +/* Syncing the write-buffers would have canceled them. */ +for (i =0 ; i < c->jhead_cnt; i++) +hrtimer_cancel (&c->jheads[i].wbuf.timer); } - c->mst_node->flags &= ~cpu_to_le32 (UBIFS_MST_DIRTY ); - c->mst_node->flags |=cpu_to_le32 (UBIFS_MST_NO_ORPHS ); - c->mst_node->gc_lnum =cpu_to_le32 (c->gc_lnum); - err =ubifs_write_master (c); -if (err) -ubifs_ro_mode (c, err); -vfree (c->orph_buf); c->orph_buf =NULL ;kfree (c->write_reserve_buf);
@@ -1977,10 +1991,7 @@ static int ubifs_reconfigure(struct fs_context *fc)if (err)return err; }else if (!c->ro_mount && (fc->sb_flags &SB_RDONLY )) { -if (c->ro_error) { -ubifs_msg (c,"cannot re-mount R/O due to prior errors" ); -return -EROFS ; - } +/* After an error ubifs_remount_ro() can't write but still works. */ ubifs_remount_ro (c); }
@@ -1994,7 +2005,8 @@ static int ubifs_reconfigure(struct fs_context *fc)mutex_unlock (&c->bu_mutex); } -if (!c->need_recovery) +/* Only valid without a media failure, see ubifs_put_super(). */ +if (!c->need_recovery && !c->ro_error)ubifs_assert (c, c->lst.taken_empty_lebs >0 );return 0 ;
fs/ubifs/xattr.c +6 -0
@@ -260,6 +260,9 @@ int ubifs_xattr_set(struct inode *host, const char *name, const void *value,if (check_lock)ubifs_assert (c,inode_is_locked (host)); +if (c->ro_error) +return -EROFS ; +if (size >UBIFS_MAX_INO_DATA )return -ERANGE ;
@@ -564,6 +567,9 @@ static int ubifs_xattr_remove(struct inode *host, const char *name)ubifs_assert (c,inode_is_locked (host)); +if (c->ro_error) +return -EROFS ; +if (fname_len (&nm) >UBIFS_MAX_NLEN )return -ENAMETOOLONG ;
Signature · 1 line
2.53.0
Re: [PATCH v3 30/33] ubifs: don't set SB_RDONLY after errors
Quoted 26 lines · Christian Brauner
ubifs_ro_mode()setsc->ro_errorandSB_RDONLYfrom whatever context hit the error, e.g. a failing write, and without holdings_umount. UBIFS can't be frozen but that still races with everyone else changingsb->s_flags. Andc->ro_erroralone already makes every write fail with EROFS.So call
sb_set_emergency_ro()instead. The background thread can hit the error beforeSB_BORNbut doesn't holds_umounteither. The xattr setters checkc->ro_erroras well because ecryptfs calls them without write access on the lower mount and they change the inode in memory first.So far a remount to read-only after an error was refused and a remount to read-write went through and cleared
SB_RDONLYagain becausec->ro_mountwas never set. Now going read-only works. Nothing is synced for it in that state andubifs_remount_ro()skips the write-buffer syncs and the master node likeubifs_put_super()does and only cancels the write-buffer timers. Going read-write after that is refused by the VFS.The flusher still gets to the folios and inodes that were dirty before the error, also after the remount which sets
c->ro_mountand frees the write reserve buffer. Every journal write fails with EROFS after an error anyway. Sodo_writepage()andubifs_write_inode()drop them without going near the journal. Otherwisereserve_space()trips its !c->ro_mount assert for every folio after the remount.
Quoted 7 lines
Signed-off-by Christian Brauner (Amutable) <[email protected]>--- fs/ubifs/file.c | 12 ++++++++++++ fs/ubifs/io.c | 6 +++++- fs/ubifs/super.c | 40 ++++++++++++++++++++++++++-------------- fs/ubifs/xattr.c | 6 ++++++ 4 files changed, 49 insertions(+), 15 deletions(-)
Quoted 163 lines
fs/ubifs/file.c +12
-0@@ -898,6 +898,13 @@ static int do_writepage(struct folio *folio, size_t len)folio_start_writeback (folio); +/* Nothing gets written after an error, drop the folio. */ +if (unlikely (c->ro_error)) { + err = -EROFS ; +mapping_set_error (folio->mapping, err); + goto out; + } + block = folio->index <<UBIFS_BLOCKS_PER_PAGE_SHIFT ;for (;;) { blen =min_t (size_t, len,UBIFS_BLOCK_SIZE );@@ -918,6 +925,7 @@ static int do_writepage(struct folio *folio, size_t len)ubifs_ro_mode (c, err); } +out:ubifs_assert (c, folio->private !=NULL );if (folio_test_checked (folio))release_new_page_budget (c);@@ -997,6 +1005,10 @@ static int ubifs_writepage(struct folio *folio, struct writeback_control *wbc) goto out_unlock; } +/* After an error do_writepage() drops the folio. */ +if (unlikely (c->ro_error)) +return do_writepage (folio, len); +spin_lock (&ui->ui_lock); synced_i_size = ui->synced_i_size;spin_unlock (&ui->ui_lock);fs/ubifs/io.c +5
-1@@ -72,7 +72,11 @@ void ubifs_ro_mode(struct ubifs_info *c, int err)if (!c->ro_error) { c->ro_error =1 ; c->no_chk_data_crc =0 ; - c->vfs_sb->s_flags |=SB_RDONLY ; +/* +* SB_RDONLY needs s_umount, see sb_set_emergency_ro(). The +* background thread runs before SB_BORN already. +*/ +sb_set_emergency_ro (c->vfs_sb);ubifs_warn (c,"switched to read-only mode, error %d" , err);dump_stack (); }fs/ubifs/super.c +26
-14@@ -312,6 +312,12 @@ static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc)return 0 ; } +/* Nothing gets written after an error, drop the change. */ +if (unlikely (c->ro_error)) { + err = -EROFS ; + goto out; + } +/* * As an optimization, do not write orphan inodes to the media just * because this is not needed. @@ -327,6 +333,7 @@ static int ubifs_write_inode(struct inode *inode, struct writeback_control *wbc) err =dbg_check_inode_size (c, inode, ui->ui_size); } +out: ui->dirty =0 ;mutex_unlock (&ui->ui_mutex);ubifs_release_dirty_inode_budget (c, ui);@@ -1839,19 +1846,26 @@ static void ubifs_remount_ro(struct ubifs_info *c)dbg_save_space_info (c); -for (i =0 ; i < c->jhead_cnt; i++) { - err =ubifs_wbuf_sync (&c->jheads[i].wbuf); +/* Like ubifs_put_super(), don't write anything after an error. */ +if (!c->ro_error) { +for (i =0 ; i < c->jhead_cnt; i++) { + err =ubifs_wbuf_sync (&c->jheads[i].wbuf); +if (err) +ubifs_ro_mode (c, err); + } + + c->mst_node->flags &= ~cpu_to_le32 (UBIFS_MST_DIRTY ); + c->mst_node->flags |=cpu_to_le32 (UBIFS_MST_NO_ORPHS ); + c->mst_node->gc_lnum =cpu_to_le32 (c->gc_lnum); + err =ubifs_write_master (c);if (err)ubifs_ro_mode (c, err); + }else { +/* Syncing the write-buffers would have canceled them. */ +for (i =0 ; i < c->jhead_cnt; i++) +hrtimer_cancel (&c->jheads[i].wbuf.timer); } - c->mst_node->flags &= ~cpu_to_le32 (UBIFS_MST_DIRTY ); - c->mst_node->flags |=cpu_to_le32 (UBIFS_MST_NO_ORPHS ); - c->mst_node->gc_lnum =cpu_to_le32 (c->gc_lnum); - err =ubifs_write_master (c); -if (err) -ubifs_ro_mode (c, err); -vfree (c->orph_buf); c->orph_buf =NULL ;kfree (c->write_reserve_buf);@@ -1977,10 +1991,7 @@ static int ubifs_reconfigure(struct fs_context *fc)if (err)return err; }else if (!c->ro_mount && (fc->sb_flags &SB_RDONLY )) { -if (c->ro_error) { -ubifs_msg (c,"cannot re-mount R/O due to prior errors" ); -return -EROFS ; - } +/* After an error ubifs_remount_ro() can't write but still works. */ ubifs_remount_ro (c); }@@ -1994,7 +2005,8 @@ static int ubifs_reconfigure(struct fs_context *fc)mutex_unlock (&c->bu_mutex); } -if (!c->need_recovery) +/* Only valid without a media failure, see ubifs_put_super(). */ +if (!c->need_recovery && !c->ro_error)ubifs_assert (c, c->lst.taken_empty_lebs >0 );return 0 ;fs/ubifs/xattr.c +6
-0@@ -260,6 +260,9 @@ int ubifs_xattr_set(struct inode *host, const char *name, const void *value,if (check_lock)ubifs_assert (c,inode_is_locked (host)); +if (c->ro_error) +return -EROFS ; +if (size >UBIFS_MAX_INO_DATA )return -ERANGE ;@@ -564,6 +567,9 @@ static int ubifs_xattr_remove(struct inode *host, const char *name)ubifs_assert (c,inode_is_locked (host)); +if (c->ro_error) +return -EROFS ; +if (fname_len (&nm) >UBIFS_MAX_NLEN )return -ENAMETOOLONG ;
[email protected]
AI reviewRe: [PATCH v3 30/33] ubifs: don't set SB_RDONLY after errors
In reply to Christian BraunerThank you for your contribution! Sashiko AI review found 3 potential issues to consider: - [Medium] ubifs: false space accounting error in ubifs_remount_ro() after error - [Medium] ubifs…
Thank you for your contribution! Sashiko AI review found 3 potential issues to consider:
- [Medium] ubifs: false space accounting error in ubifs_remount_ro() after error
- [Medium] ubifs: ubifs_fsync() trips !c->ro_mount assert after error remount
- [Medium] ubifs: race between in-flight writeback and ubifs_remount_ro() after error
--
commit fda6fbd307537dfd99ef75b3610022c5af4a9353
Author: Christian Brauner <[email protected]>
ubifs: don't set SB_RDONLY after errors
This commit switches ubifs_ro_mode() from setting SB_RDONLY directly on s_flags to calling sb_set_emergency_ro(), allows remounting read-only after an error, and drops dirty folios and inodes during writeback once c->ro_error is set.
Quoted 4 lines
fs/ubifs/super.c +0
-0
[ ... ]
Quoted 15 lines
fs/ubifs/super.c +9
-2@@ -1839,19 +1846,26 @@ static void ubifs_remount_ro(struct ubifs_info *c)dbg_save_space_info (c); -for (i =0 ; i < c->jhead_cnt; i++) { - err =ubifs_wbuf_sync (&c->jheads[i].wbuf); +/* Like ubifs_put_super(), don't write anything after an error. */ +if (!c->ro_error) { +for (i =0 ; i < c->jhead_cnt; i++) { + err =ubifs_wbuf_sync (&c->jheads[i].wbuf); +if (err) +ubifs_ro_mode (c, err); + } + + c->mst_node->flags &= ~cpu_to_le32 (UBIFS_MST_DIRTY );
fs/ubifs/super.c static void ubifs_remount_ro(struct ubifs_info *c) · line 1867
Quoted 10 lines
fs/ubifs/super.c +7
-0+ c->mst_node->flags |=cpu_to_le32 (UBIFS_MST_NO_ORPHS ); + c->mst_node->gc_lnum =cpu_to_le32 (c->gc_lnum); + err =ubifs_write_master (c);if (err)ubifs_ro_mode (c, err); + }else { +/* Syncing the write-buffers would have canceled them. */ +for (i =0 ; i < c->jhead_cnt; i++) +hrtimer_cancel (&c->jheads[i].wbuf.timer); }
[Severity: Medium]
Can an in-flight ubifs_fsync() trip the !c->ro_mount assertion in ubifs_wbuf_sync_nolock() when racing with a read-only remount after an error?
When c->ro_error is set, skipping ubifs_wbuf_sync() leaves wbuf->used and wbuf->inodes[] populated while ubifs_remount_ro() sets c->ro_mount = 1. Because vfs_fsync_range() does not hold s_umount or mnt_want_write(), an fsync() on an inode whose node is already buffered in a wbuf (with no dirty folios and ui->dirty == 0) can interleave with ubifs_remount_ro():
CPU0 CPU1ubifs_fsync()
if (c->ro_mount)
return 0;
file_write_and_wait_range()
ubifs_write_inode() -> 0
ubifs_remount_ro()
hrtimer_cancel(&wbuf->timer)
c->ro_mount = 1 ubifs_sync_wbufs_by_inode()
ubifs_wbuf_sync_nolock(wbuf)
ubifs_assert(c, !c->ro_media && !c->ro_mount)
<-- assertion failure
In ubifs_wbuf_sync_nolock(), the !c->ro_mount assertion runs before the if (c->ro_error) return -EROFS check, causing a warning and stack dump (or a panic when mounted with assert=panic).
fs/ubifs/super.c static void ubifs_remount_ro(struct ubifs_info *c) · line 1871
Quoted 11 lines
fs/ubifs/super.c +0
-8- - c->mst_node->flags &= ~cpu_to_le32 (UBIFS_MST_DIRTY ); - c->mst_node->flags |=cpu_to_le32 (UBIFS_MST_NO_ORPHS ); - c->mst_node->gc_lnum =cpu_to_le32 (c->gc_lnum); - err =ubifs_write_master (c); -if (err) -ubifs_ro_mode (c, err); -vfree (c->orph_buf); c->orph_buf =NULL ;kfree (c->write_reserve_buf);
[Severity: Medium]
Can ubifs_remount_ro() free c->write_reserve_buf and set c->ro_mount = 1 while an in-flight do_writepage() is still running?
Once ubifs_ro_mode() calls sb_set_emergency_ro(), sync_filesystem(sb) in ubifs_reconfigure() returns immediately at sb_rdonly_any(sb) without calling sync_inodes_sb() or waiting for in-flight writeback. A writeback caller that does not take mnt_want_write() or s_umount (such as sync_file_range()) can pass the unlocked c->ro_error check in do_writepage() just before an error occurs and then race with ubifs_remount_ro():
CPU0 CPU1do_writepage()
if (unlikely(c->ro_error))
ubifs_jnl_write_data()
ubifs_ro_mode()
c->ro_error = 1
sb_set_emergency_ro(sb)
ubifs_reconfigure()
sync_filesystem(sb) -> 0
ubifs_remount_ro()
kfree(c->write_reserve_buf)
c->write_reserve_buf = NULL
c->ro_mount = 1
mutex_lock(&c->write_reserve_mutex)
data = c->write_reserve_buf
data->ch.node_type = ...
<-- NULL dereference or use-after-freeEven when kmalloc() succeeds in ubifs_jnl_write_data(), make_reservation() subsequently calls reserve_space(), trippingubifs_assert(c, !c->ro_media && !c->ro_mount).
fs/ubifs/super.c static int ubifs_reconfigure(struct fs_context *fc) · line 1996
Quoted 11 lines
fs/ubifs/super.c +1
-4@@ -1977,10 +1991,7 @@ static int ubifs_reconfigure(struct fs_context *fc)if (err)return err; }else if (!c->ro_mount && (fc->sb_flags &SB_RDONLY )) { -if (c->ro_error) { -ubifs_msg (c,"cannot re-mount R/O due to prior errors" ); -return -EROFS ; - } +/* After an error ubifs_remount_ro() can't write but still works. */ ubifs_remount_ro (c); }
[Severity: Medium]
Should ubifs_remount_ro() skip dbg_save_space_info() and dbg_check_space_info() when c->ro_error is set, as ubifs_put_super() and the taken_empty_lebs check in ubifs_reconfigure() do?
Because sync_filesystem() is a no-op once sb_set_emergency_ro() is set, dirty folios and dirty inodes remain pending during ubifs_remount_ro(). dbg_save_space_info() snapshots ubifs_get_free_space_nolock(), which includes c->bi.data_growth and c->bi.dd_growth. If a concurrent writeback or folio release drops a dirty folio via do_writepage() or a dirty inode via ubifs_write_inode() before dbg_check_space_info() runs at the end of ubifs_remount_ro(), releasing that page or inode budget changes the reported free space and causes dbg_check_space_info() to log a false "free space changed" error and call dump_stack().
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=30
Christian Brauner
maintainerPATCH v3 31/33 ext4: allow remounting read-only in the emergency state
In reply to Christian Brauner__ext4_remount() refuses any change of the read-only state with EROFS while ext4_emergency_state() is set, i.e. after a shutdown or an error. Neither sets SB_RDONLY anymore, so a remount to read-only fails and the filesystem stays read-write. Emergency remount (sysrq u) asks with SB_FORCE and reconfigure_super() WARNs, a panic with panic_on_warn:
forced remount of a ext4 fs returned -30
Becoming read-only doesn't need to write anything. So allow it and skip everything in that branch that would touch the disk: sync_filesystem(), dquot_suspend(), the s_state update, ext4_mark_recovery_complete() and the superblock commit without a journal. Going read-write is still refused.
Note that ext4_emergency_state() only exists since v6.15. Older stable trees need ext4_forced_shutdown() instead.
--- fs/ext4/super.c | 18 ++++++++++++------ 1 file changed, 12 insertions(+), 6 deletions(-)
fs/ext4/super.c +12 -6
@@ -6761,11 +6761,16 @@ static int __ext4_remount(struct fs_context *fc, struct super_block *sb)if ((bool )(fc->sb_flags &SB_RDONLY ) !=sb_rdonly (sb)) {if (ext4_emergency_state (sb)) { - err = -EROFS ; - goto restore_opts; - } - -if (fc->sb_flags &SB_RDONLY ) { +/* +* Going read-only must work for emergency remount +* (sysrq u) but nothing may be written, just flip it. +*/ +if (!(fc->sb_flags &SB_RDONLY )) { + err = -EROFS ; + goto restore_opts; + } + sb->s_flags |=SB_RDONLY ; + }else if (fc->sb_flags &SB_RDONLY ) { err =sync_filesystem (sb);if (err <0 ) goto restore_opts;
@@ -6876,7 +6881,8 @@ static int __ext4_remount(struct fs_context *fc, struct super_block *sb) goto restore_opts; } -if (sbi->s_journal ==NULL && !(old_sb_flags &SB_RDONLY )) { +if (sbi->s_journal ==NULL && !(old_sb_flags &SB_RDONLY ) && + !ext4_emergency_state (sb)) { err =ext4_commit_super (sb);if (err) goto restore_opts;
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 31/33] ext4: allow remounting read-only in the emergency state
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines
__ext4_remount()refuses any change of the read-only state with EROFS whileext4_emergency_state()is set, i.e. after a shutdown or an error. Neither setsSB_RDONLYanymore, so a remount to read-only fails and the filesystem stays read-write. Emergency remount (sysrq u) asks withSB_FORCEandreconfigure_super()WARNs, a panic withpanic_on_warn:forced remount of a ext4 fs returned -30
Becoming read-only doesn't need to write anything. So allow it and skip everything in that branch that would touch the disk:
sync_filesystem(),dquot_suspend(), thes_stateupdate,ext4_mark_recovery_complete()and the superblock commit without a journal. Going read-write is still refused.
Quoted 8 lines
Note that
ext4_emergency_state()only exists since v6.15. Older stable trees needext4_forced_shutdown()instead.Fixes 95257987a638 ("ext4: drop EXT4_MF_FS_ABORTED flag")Fixes d3476f3dad4a ("ext4: don't set SB_RDONLY after filesystem errors")Reviewed-by Jan Kara <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=31
Christian Brauner
maintainerPATCH v3 32/33 ext4: use the generic emergency read-only state
In reply to Christian BraunerSet SB_STATE_EMERGENCY_RO next to EXT4_FLAGS_EMERGENCY_RO when an error makes the filesystem read-only. The VFS then refuses modifications before they reach ext4 and reports the superblock as read-only like it did when ext4 still set SB_RDONLY. The shutdown state stays ext4's own and so do all of ext4's checks because they cover both states. ext4 keeps showing "emergency_ro" in its mount options.
EXT4_IOC_SHUTDOWN with EXT4_GOING_FLAGS_DEFAULT fails after an error on mainline today because ext4_sync_fs() fails the freeze. freeze_super() refuses it in the emergency state now. ext4 refuses writes in that state anyway and the journal is aborted, so shut down without the freeze like EXT4_GOING_FLAGS_NOLOGFLUSH does.
--- fs/ext4/ioctl.c | 4 ++++ fs/ext4/super.c | 1 + 2 files changed, 5 insertions(+)
fs/ext4/ioctl.c +4 -0
@@ -821,6 +821,10 @@ int ext4_force_shutdown(struct super_block *sb, u32 flags)ext4_msg (sb,KERN_ALERT ,"shut down requested (%d)" , flags);trace_ext4_shutdown (sb, flags); +/* After an error there is nothing to flush and freezing fails. */ +if (flags ==EXT4_GOING_FLAGS_DEFAULT &&ext4_emergency_ro (sb)) + flags =EXT4_GOING_FLAGS_NOLOGFLUSH ; +switch (flags) {case EXT4_GOING_FLAGS_DEFAULT : ret =bdev_freeze (sb->s_bdev);
fs/ext4/super.c +1 -0
@@ -757,6 +757,7 @@ static void ext4_handle_error(struct super_block *sb, bool force_ro, int error,* and other problems. */ set_bit (EXT4_FLAGS_EMERGENCY_RO , &EXT4_SB (sb)->s_ext4_flags); +sb_set_emergency_ro (sb); }static void update_super_work (struct work_struct *work)
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 32/33] ext4: use the generic emergency read-only state
Sashiko has reviewed this patch and found no issues.
Quoted 7 lines
Set
SB_STATE_EMERGENCY_ROnext toEXT4_FLAGS_EMERGENCY_ROwhen an error makes the filesystem read-only. The VFS then refuses modifications before they reach ext4 and reports the superblock as read-only like it did when ext4 still setSB_RDONLY. The shutdown state stays ext4's own and so do all of ext4's checks because they cover both states. ext4 keeps showing "emergency_ro" in its mount options.
Quoted 8 lines
EXT4_IOC_SHUTDOWNwithEXT4_GOING_FLAGS_DEFAULTfails after an error on mainline today becauseext4_sync_fs()fails the freeze.freeze_super()refuses it in the emergency state now. ext4 refuses writes in that state anyway and the journal is aborted, so shut down without the freeze likeEXT4_GOING_FLAGS_NOLOGFLUSHdoes.Reviewed-by Jan Kara <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=32
Christian Brauner
maintainerPATCH v3 33/33 xfs: allow remounting read-only after a shutdown
In reply to Christian Braunerxfs_remount_ro() starts with sync_filesystem() which fails with EIO after a shutdown. So a shut down xfs can't be remounted read-only and emergency remount (sysrq u) WARNs in reconfigure_super(), a panic with panic_on_warn:
forced remount of a xfs fs returned -5
Going read-only doesn't need to write anything. So ignore the errors from flushing and from freeing the COW and post-EOF blocks once xfs is shut down, also if the flush shut it down itself. The rest of xfs_remount_ro() copes with a shutdown like unmount does. And refuse going back to read-write with EIO, xfs_remount_rw() didn't check for a shutdown at all.
--- fs/xfs/xfs_super.c | 13 ++++++++++--- 1 file changed, 10 insertions(+), 3 deletions(-)
fs/xfs/xfs_super.c +10 -3
@@ -2041,6 +2041,10 @@ xfs_remount_rw(struct xfs_sb *sbp = &mp->m_sb;int error; +/* Nothing can be written anymore after a shutdown. */ +if (xfs_is_shutdown (mp)) +return -EIO ; +if (mp->m_logdev_targp && mp->m_logdev_targp != mp->m_ddev_targp &&xfs_readonly_buftarg (mp->m_logdev_targp)) { xfs_warn(mp,
@@ -2115,9 +2119,12 @@ xfs_remount_ro( };int error; -/* Flush all the dirty data to disk. */ +/* +* Flush all the dirty data to disk. That fails after a shutdown, also +* one the flush itself caused, but going read-only still has to work. +*/ error =sync_filesystem (mp->m_super); -if (error) +if (error && !xfs_is_shutdown (mp))return error;/*
@@ -2133,7 +2140,7 @@ xfs_remount_ro(* cached inode, so this requires a synchronous cache scan. */ error =xfs_blockgc_free_space (mp, &icw); -if (error) { +if (error && !xfs_is_shutdown (mp)) {xfs_force_shutdown (mp,SHUTDOWN_CORRUPT_INCORE );return error; }
Signature · 1 line
2.53.0
[email protected]
AI reviewRe: [PATCH v3 33/33] xfs: allow remounting read-only after a shutdown
Sashiko has reviewed this patch and found no issues.
Quoted 14 lines
xfs_remount_ro()starts withsync_filesystem()which fails with EIO after a shutdown. So a shut down xfs can't be remounted read-only and emergency remount (sysrq u) WARNs inreconfigure_super(), a panic withpanic_on_warn:forced remount of a xfs fs returned -5
Going read-only doesn't need to write anything. So ignore the errors from flushing and from freeing the COW and post-EOF blocks once xfs is shut down, also if the flush shut it down itself. The rest of
xfs_remount_ro()copes with a shutdown like unmount does. And refuse going back to read-write with EIO,xfs_remount_rw()didn't check for a shutdown at all.
Quoted 4 lines
Fixes b97cca3ba909 ("xfs: only bother with sync_filesystem during readonly remount")Reviewed-by Christoph Hellwig <[email protected]>Signed-off-by Christian Brauner (Amutable) <[email protected]>
Sashiko has reviewed this patch and found no issues.
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=33
Christian Brauner
maintainerThis is now on vfs-7.5.super. There's a bunch of outstanding pre-existing fixes per-filesystems. So please, if you're a filesystem maintainer take a look.
I will declare this series ready, otherwise this will turn from the yak shaving that this is right now into yak-herd shaving.