ubifs: account for short copies in ubifs_write_end
Subsystems (2)
PATCH v2 1/1 ubifs: account for short copies in ubifs_write_end
ubifs_write_end() updates i_size and ui_size to pos + len even when fewer bytes were copied. A one-byte write at EOF from an unreadable buffer can fail with EFAULT while extending the file by one byte. Partial writes can also extend the file past the last byte copied.
Use pos + copied for size updates and cancel the budget when no data was copied. Keep the appending decision based on the requested length to match the inode budgeting and ui_mutex locking in ubifs_write_begin(). If a short copy ends within the old size, release any unused inode budget without reducing the size, and still unlock ui_mutex.
---
Changes in v2
- Add Zhihao Cheng's Reviewed-by.
v1 posted at https://lore.kernel.org/r/[email protected]
fs/ubifs/file.c | 29 +++++++++++++++++++---------- 1 file changed, 19 insertions(+), 10 deletions(-)
fs/ubifs/file.c +19 -10
@@ -523,8 +523,8 @@ static int ubifs_write_end(const struct kiocb *iocb,struct inode *inode = mapping->host;struct ubifs_inode *ui =ubifs_inode (inode);struct ubifs_info *c = inode->i_sb->s_fs_info; -loff_t end_pos = pos + len; -int appending = !!(end_pos > inode->i_size); +loff_t end_pos = pos + copied; +int appending = !!(pos + len > inode->i_size);dbg_gen ("ino %llu, pos %llu, pg %lu, len %u, copied %d, i_size %lld" , inode->i_ino, pos, folio->index, len, copied, inode->i_size);
@@ -552,6 +552,11 @@ static int ubifs_write_end(const struct kiocb *iocb, goto out; } +if (unlikely (!copied)) { +cancel_budget (c, folio, ui, appending); + goto out; + } +if (len ==folio_size (folio))folio_mark_uptodate (folio);
@@ -562,14 +567,18 @@ static int ubifs_write_end(const struct kiocb *iocb, }if (appending) { -i_size_write (inode, end_pos); - ui->ui_size = end_pos; -/* -* We do not set @I_DIRTY_PAGES (which means that -* the inode has dirty pages), this was done in -* filemap_dirty_folio(). -*/ -__mark_inode_dirty (inode,I_DIRTY_DATASYNC ); +if (end_pos > inode->i_size) { +i_size_write (inode, end_pos); + ui->ui_size = end_pos; +/* +* We do not set @I_DIRTY_PAGES (which means that +* the inode has dirty pages), this was done in +* filemap_dirty_folio(). +*/ +__mark_inode_dirty (inode,I_DIRTY_DATASYNC ); + }else if (!ui->dirty) { +ubifs_release_dirty_inode_budget (c, ui); + }ubifs_assert (c,mutex_is_locked (&ui->ui_mutex));mutex_unlock (&ui->ui_mutex); }
base-commit: 6628e7d4c6b25c8d18f1345e3a20d21829aae7e4
Signature · 1 line
2.50.1 (Apple Git-155)
Hi Richard,
Just following up on this patch.
Thanks,
Hungyu