diff mbox series

[RFC,07/20] fs: use must_set_pos()

Message ID 20240830-vfs-file-f_version-v1-7-6d3e4816aa7b@kernel.org (mailing list archive)
State New
Headers show
Series file: remove f_version | expand

Commit Message

Christian Brauner Aug. 30, 2024, 1:04 p.m. UTC
Make generic_file_llseek_size() use must_set_pos(). We'll use
must_set_pos() in another helper in a minutes. Remove __maybe_unused
from must_set_pos() now that it's used.

Signed-off-by: Christian Brauner <brauner@kernel.org>
---
 fs/read_write.c | 51 ++++++++++++++-------------------------------------
 1 file changed, 14 insertions(+), 37 deletions(-)

Comments

Jan Kara Sept. 3, 2024, 11:30 a.m. UTC | #1
On Fri 30-08-24 15:04:48, Christian Brauner wrote:
> Make generic_file_llseek_size() use must_set_pos(). We'll use
> must_set_pos() in another helper in a minutes. Remove __maybe_unused
> from must_set_pos() now that it's used.
> 
> Signed-off-by: Christian Brauner <brauner@kernel.org>

Frankly, it would have been a bit easier to review for me if 6 & 7 patches
were together as one code refactoring patch...

> +		guard(spinlock)(&file->f_lock);

You really love guards, don't you? :) Frankly, in this case I don't see the
point and it makes my visual pattern matching fail but I guess I'll get
used to it. Feel free to add:

Reviewed-by: Jan Kara <jack@suse.cz>

> +		return vfs_setpos(file, file->f_pos + offset, maxsize);
>  	}
>  
>  	return vfs_setpos(file, offset, maxsize);

								Honza
Christian Brauner Sept. 3, 2024, 11:41 a.m. UTC | #2
On Tue, Sep 03, 2024 at 01:30:10PM GMT, Jan Kara wrote:
> On Fri 30-08-24 15:04:48, Christian Brauner wrote:
> > Make generic_file_llseek_size() use must_set_pos(). We'll use
> > must_set_pos() in another helper in a minutes. Remove __maybe_unused
> > from must_set_pos() now that it's used.
> > 
> > Signed-off-by: Christian Brauner <brauner@kernel.org>
> 
> Frankly, it would have been a bit easier to review for me if 6 & 7 patches
> were together as one code refactoring patch...

Yeah, I had it that way but the resulting diff was really difficult to read.
I could've probably tried to use a different diff algorithm but this way
was easier.

> 
> > +		guard(spinlock)(&file->f_lock);
> 
> You really love guards, don't you? :) Frankly, in this case I don't see the

Yes. :)

> point and it makes my visual pattern matching fail but I guess I'll get
> used to it. Feel free to add:

I can remove it. I don't mind.
diff mbox series

Patch

diff --git a/fs/read_write.c b/fs/read_write.c
index acee26989d95..ad93b72cc378 100644
--- a/fs/read_write.c
+++ b/fs/read_write.c
@@ -97,7 +97,7 @@  EXPORT_SYMBOL(vfs_setpos);
  * Return: 0 if f_pos doesn't need to be updated, 1 if f_pos has to be
  * updated, and negative error code on failure.
  */
-static __maybe_unused int must_set_pos(struct file *file, loff_t *offset, int whence, loff_t eof)
+static int must_set_pos(struct file *file, loff_t *offset, int whence, loff_t eof)
 {
 	switch (whence) {
 	case SEEK_END:
@@ -157,45 +157,22 @@  loff_t
 generic_file_llseek_size(struct file *file, loff_t offset, int whence,
 		loff_t maxsize, loff_t eof)
 {
-	switch (whence) {
-	case SEEK_END:
-		offset += eof;
-		break;
-	case SEEK_CUR:
-		/*
-		 * Here we special-case the lseek(fd, 0, SEEK_CUR)
-		 * position-querying operation.  Avoid rewriting the "same"
-		 * f_pos value back to the file because a concurrent read(),
-		 * write() or lseek() might have altered it
-		 */
-		if (offset == 0)
-			return file->f_pos;
-		/*
-		 * f_lock protects against read/modify/write race with other
-		 * SEEK_CURs. Note that parallel writes and reads behave
-		 * like SEEK_SET.
-		 */
-		spin_lock(&file->f_lock);
-		offset = vfs_setpos(file, file->f_pos + offset, maxsize);
-		spin_unlock(&file->f_lock);
+	int ret;
+
+	ret = must_set_pos(file, &offset, whence, eof);
+	if (ret < 0)
+		return ret;
+	if (ret == 0)
 		return offset;
-	case SEEK_DATA:
-		/*
-		 * In the generic case the entire file is data, so as long as
-		 * offset isn't at the end of the file then the offset is data.
-		 */
-		if ((unsigned long long)offset >= eof)
-			return -ENXIO;
-		break;
-	case SEEK_HOLE:
+
+	if (whence == SEEK_CUR) {
 		/*
-		 * There is a virtual hole at the end of the file, so as long as
-		 * offset isn't i_size or larger, return i_size.
+		 * f_lock protects against read/modify/write race with
+		 * other SEEK_CURs. Note that parallel writes and reads
+		 * behave like SEEK_SET.
 		 */
-		if ((unsigned long long)offset >= eof)
-			return -ENXIO;
-		offset = eof;
-		break;
+		guard(spinlock)(&file->f_lock);
+		return vfs_setpos(file, file->f_pos + offset, maxsize);
 	}
 
 	return vfs_setpos(file, offset, maxsize);