All of lore.kernel.org
 help / color / mirror / Atom feed
From: Jan Kara <jack@suse.cz>
To: <linux-fsdevel@vger.kernel.org>
Cc: Jan Kara <jack@suse.cz>
Subject: [PATCH 15/22] udf: Push i_data_sem locking into udf_expand_file_adinicb()
Date: Tue, 24 Jan 2023 13:18:01 +0100	[thread overview]
Message-ID: <20230124121814.25951-15-jack@suse.cz> (raw)
In-Reply-To: <20230124120835.21728-1-jack@suse.cz>

The checks we do in udf_setsize() and udf_file_write_iter() are safe to
do only with i_rwsem locked as it stabilizes both file type and file
size. Hence we don't need to lock i_data_sem before we enter
udf_expand_file_adinicb() which simplifies the locking somewhat.

Signed-off-by: Jan Kara <jack@suse.cz>
---
 fs/udf/file.c  | 11 +++++------
 fs/udf/inode.c | 18 ++++++------------
 2 files changed, 11 insertions(+), 18 deletions(-)

diff --git a/fs/udf/file.c b/fs/udf/file.c
index 8be51161f3e5..60524814c594 100644
--- a/fs/udf/file.c
+++ b/fs/udf/file.c
@@ -148,7 +148,6 @@ static ssize_t udf_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 	if (retval <= 0)
 		goto out;
 
-	down_write(&iinfo->i_data_sem);
 	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB &&
 	    inode->i_sb->s_blocksize < (udf_file_entry_alloc_offset(inode) +
 				 iocb->ki_pos + iov_iter_count(from))) {
@@ -158,15 +157,15 @@ static ssize_t udf_file_write_iter(struct kiocb *iocb, struct iov_iter *from)
 			udf_debug("udf_expand_adinicb: err=%d\n", err);
 			return err;
 		}
-	} else
-		up_write(&iinfo->i_data_sem);
+	}
 
 	retval = __generic_file_write_iter(iocb, from);
 out:
-	down_write(&iinfo->i_data_sem);
-	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB && retval > 0)
+	if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB && retval > 0) {
+		down_write(&iinfo->i_data_sem);
 		iinfo->i_lenAlloc = inode->i_size;
-	up_write(&iinfo->i_data_sem);
+		up_write(&iinfo->i_data_sem);
+	}
 	inode_unlock(inode);
 
 	if (retval > 0) {
diff --git a/fs/udf/inode.c b/fs/udf/inode.c
index 4554d1e54eb3..b13c35335dd1 100644
--- a/fs/udf/inode.c
+++ b/fs/udf/inode.c
@@ -247,7 +247,6 @@ const struct address_space_operations udf_aops = {
 /*
  * Expand file stored in ICB to a normal one-block-file
  *
- * This function requires i_data_sem for writing and releases it.
  * This function requires i_mutex held
  */
 int udf_expand_file_adinicb(struct inode *inode)
@@ -259,6 +258,7 @@ int udf_expand_file_adinicb(struct inode *inode)
 
 	WARN_ON_ONCE(!inode_is_locked(inode));
 	if (!iinfo->i_lenAlloc) {
+		down_write(&iinfo->i_data_sem);
 		if (UDF_QUERY_FLAG(inode->i_sb, UDF_FLAG_USE_SHORT_AD))
 			iinfo->i_alloc_type = ICBTAG_FLAG_AD_SHORT;
 		else
@@ -269,11 +269,6 @@ int udf_expand_file_adinicb(struct inode *inode)
 		mark_inode_dirty(inode);
 		return 0;
 	}
-	/*
-	 * Release i_data_sem so that we can lock a page - page lock ranks
-	 * above i_data_sem. i_mutex still protects us against file changes.
-	 */
-	up_write(&iinfo->i_data_sem);
 
 	page = find_or_create_page(inode->i_mapping, 0, GFP_NOFS);
 	if (!page)
@@ -1160,19 +1155,18 @@ int udf_setsize(struct inode *inode, loff_t newsize)
 
 	iinfo = UDF_I(inode);
 	if (newsize > inode->i_size) {
-		down_write(&iinfo->i_data_sem);
 		if (iinfo->i_alloc_type == ICBTAG_FLAG_AD_IN_ICB) {
-			if (bsize <
+			if (bsize >=
 			    (udf_file_entry_alloc_offset(inode) + newsize)) {
-				err = udf_expand_file_adinicb(inode);
-				if (err)
-					return err;
 				down_write(&iinfo->i_data_sem);
-			} else {
 				iinfo->i_lenAlloc = newsize;
 				goto set_size;
 			}
+			err = udf_expand_file_adinicb(inode);
+			if (err)
+				return err;
 		}
+		down_write(&iinfo->i_data_sem);
 		err = udf_extend_file(inode, newsize);
 		if (err) {
 			up_write(&iinfo->i_data_sem);
-- 
2.35.3


  parent reply	other threads:[~2023-01-24 12:18 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-01-24 12:17 [PATCH 0/22] udf: Fix couple of preallocation related bugs Jan Kara
2023-01-24 12:17 ` [PATCH 01/22] udf: Unify types in anchor block detection Jan Kara
2023-01-24 12:17 ` [PATCH 02/22] udf: Drop VARCONV support Jan Kara
2023-01-24 12:17 ` [PATCH 03/22] udf: Move incrementing of goal block directly into inode_getblk() Jan Kara
2023-01-24 12:17 ` [PATCH 04/22] udf: Factor out block mapping into udf_map_block() Jan Kara
2023-01-24 12:17 ` [PATCH 05/22] udf: Use udf_bread() in udf_get_pblock_virt15() Jan Kara
2023-01-24 12:17 ` [PATCH 06/22] udf: Use udf_bread() in udf_load_vat() Jan Kara
2023-01-24 12:17 ` [PATCH 07/22] udf: Do not call udf_block_map() on ICB files Jan Kara
2023-01-24 12:17 ` [PATCH 08/22] udf: Convert udf_symlink_filler() to use udf_bread() Jan Kara
2023-01-24 12:17 ` [PATCH 09/22] udf: Fold udf_block_map() into udf_map_block() Jan Kara
2023-01-24 12:17 ` [PATCH 10/22] udf: Pass mapping request into inode_getblk() Jan Kara
2023-01-24 12:17 ` [PATCH 11/22] udf: Add flag to disable block preallocation Jan Kara
2023-01-24 12:17 ` [PATCH 12/22] udf: Use udf_map_block() in udf_getblk() Jan Kara
2023-01-24 12:17 ` [PATCH 13/22] udf: Fold udf_getblk() into udf_bread() Jan Kara
2023-01-24 12:18 ` [PATCH 14/22] udf: Protect rename against modification of moved directory Jan Kara
2023-01-24 12:18 ` Jan Kara [this message]
2023-01-24 12:18 ` [PATCH 16/22] udf: Push i_data_sem locking into udf_extend_file() Jan Kara
2023-01-24 12:18 ` [PATCH 17/22] udf: Simplify error handling in udf_file_write_iter() Jan Kara
2023-01-24 12:18 ` [PATCH 18/22] udf: Protect truncate and file type conversion with invalidate_lock Jan Kara
2023-01-24 12:18 ` [PATCH 19/22] udf: Allocate blocks on write page fault Jan Kara
2023-01-24 12:18 ` [PATCH 20/22] udf: Do not allocate blocks on page writeback Jan Kara
2023-01-24 12:18 ` [PATCH 21/22] udf: Fix file corruption when appending just after end of preallocated extent Jan Kara
2023-01-24 12:18 ` [PATCH 22/22] udf: Fix off-by-one error when discarding preallocation Jan Kara

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20230124121814.25951-15-jack@suse.cz \
    --to=jack@suse.cz \
    --cc=linux-fsdevel@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.