Skip to contentSkip to conversation

Fix bug in FUSE where the attribute cache for a file was not cleared when a file is opened with O_TRUNC.

PATCH In review Ken Sumrall 3 participants 7 messages Oct 29, 2010 – Nov 12, 2010 1 message not in the archive FUSE filesystem core

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Fix bug in FUSE where the attribute cache for a file was not cleared when a file is opened with O_TRUNC.

---
 fs/fuse/file.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
fs/fuse/file.c +3 -0
index c822458..4fbe62c 100644
@@ -134,6 +134,7 @@ EXPORT_SYMBOL_GPL(fuse_do_open);
 void fuse_finish_open(struct inode *inode, struct file *file)
 {
 	struct fuse_file *ff = file->private_data;
+	struct fuse_conn *fc = get_fuse_conn(inode);
 
 	if (ff->open_flags & FOPEN_DIRECT_IO)
 		file->f_op = &fuse_direct_io_file_operations;
@@ -141,6 +142,8 @@ void fuse_finish_open(struct inode *inode, struct file *file)
 		invalidate_inode_pages2(inode->i_mapping);
 	if (ff->open_flags & FOPEN_NONSEEKABLE)
 		nonseekable_open(inode, file);
+	if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC))
+		fuse_invalidate_attr(inode);
 }
 
 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
Signature · 1 line
1.7.3.1
#1 lore raw
Quoted 18 lines · On Fri, 29 Oct 2010 16:41:40 -0700 Ken Sumrall <[email protected]> wrote:
---
 fs/fuse/file.c |    3 +++
 1 files changed, 3 insertions(+), 0 deletions(-)
fs/fuse/file.c +1 -0
index c822458..4fbe62c 100644
@@ -134,6 +134,7 @@ EXPORT_SYMBOL_GPL(fuse_do_open);
 void fuse_finish_open(struct inode *inode, struct file *file)
 {
 	struct fuse_file *ff = file->private_data;
+	struct fuse_conn *fc = get_fuse_conn(inode);
 
 	if (ff->open_flags & FOPEN_DIRECT_IO)
 		file->f_op = &fuse_direct_io_file_operations;

fs/fuse/file.c void fuse_finish_open(struct inode *inode, struct file *file) · line 149

Quoted 8 lines
fs/fuse/file.c +2 -0
@@ -141,6 +142,8 @@ void fuse_finish_open(struct inode *inode, struct file *file)
 		invalidate_inode_pages2(inode->i_mapping);
 	if (ff->open_flags & FOPEN_NONSEEKABLE)
 		nonseekable_open(inode, file);
+	if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC))
+		fuse_invalidate_attr(inode);
 }
 
 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)

What were the user-visible effects of this bug?

A message that is not in the archive (sent off-list or not yet received): [email protected]

#3 lore raw

PATCH 1/1 Fix bug in FUSE where the attribute cache for a file was not cleared when a file is opened with O_TRUNC.

Resending to the list, as it was rejected due to html content.

---------- Forwarded message ----------
From: Ken Sumrall <[email protected]>
Date: Fri, Oct 29, 2010 at 5:31 PM
Subject: Re: [PATCH] Fix bug in FUSE where the attribute cache for a file was not cleared when a file is opened with O_TRUNC.
To: Andrew Morton <[email protected]>
Cc: [email protected], Miklos Szeredi <[email protected]>, Jens Axboe <[email protected]>, Anfei <[email protected]>, "Anand V. Avati" <[email protected]>, [email protected]

Here is a snippet of code from a test case we have:
  {
   FILE*   pFile = NULL;
   int32_t data  = 0x01020304;
   LOGE("WRITETEST start");
   pFile = fopen("/sdcard/write_test", "wb");
   if( NULL != pFile ) {
       LOGE("WRITETEST : starting position %d", ftell(pFile)); fseek(pFile, 0, SEEK_END);
       LOGE("WRITETEST : starting size %d", ftell(pFile)); fwrite(&data, sizeof(int32_t), 1, pFile);
       fclose(pFile);
   } else {
       LOGE("WRITETEST : failed to open the file");
   }
   LOGE("WRITETEST end");
}
fopen("wb") turns into open(O_TRUNC) in bionic (the Android libc). The code is odd in that it then does fseek(SEEK_END) on a newly truncated file. However, that is completely legal, and should set the file pointer to offset 0. If the file had previously existed, it's attributes are still in the FUSE attribute
cache, so the fseek(SEEK_END) positions the file pointer to the end size of the file
before it was truncated.
This has probably never been seen before because most code that opens with O_TRUNC
just starts writing, and doesn't query the attributes of the file till after a write.  Writing clears
the attribute cache for the file so it is fetched from the filesystem the next time it is
requested.
___
Ken

Quoted 32 lines · Andrew Morton
Quoted 18 lines · On Fri, 29 Oct 2010 16:41:40 -0700 Ken Sumrall <[email protected]> wrote:

---
 fs/fuse/file.c |    3 +++

 1 files changed, 3 insertions(+), 0 deletions(-)
fs/fuse/file.c +1 -0
index c822458..4fbe62c 100644
@@ -134,6 +134,7 @@ EXPORT_SYMBOL_GPL(fuse_do_open);
  void fuse_finish_open(struct inode *inode, struct file *file)
  {
       struct fuse_file *ff = file->private_data;
+     struct fuse_conn *fc = get_fuse_conn(inode);
 
       if (ff->open_flags & FOPEN_DIRECT_IO)
               file->f_op = &fuse_direct_io_file_operations;

fs/fuse/file.c void fuse_finish_open(struct inode *inode, struct file *file) · line 149

Quoted 8 lines
fs/fuse/file.c +2 -0
@@ -141,6 +142,8 @@ void fuse_finish_open(struct inode *inode, struct file *file)
               invalidate_inode_pages2(inode->i_mapping);
       if (ff->open_flags & FOPEN_NONSEEKABLE)
               nonseekable_open(inode, file);
+     if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC))
+             fuse_invalidate_attr(inode);
  }
 
  int fuse_open_common(struct inode *inode, struct file *file, bool isdir)

What were the user-visible effects of this bug?

maintainer
#4 lore raw
In reply to a message not in the archive

Ken,

Thanks for the patch.

Here's a slightly updated one. Does this also look OK?

Miklos

----
Subject: fuse: fix attributes after open(O_TRUNC)
From: Ken Sumrall <[email protected]>

The attribute cache for a file was not being cleared when a file is opened with O_TRUNC.

If the filesystem's open operation truncates the file ("atomic_o_trunc" feature flag is set) then the kernel should invalidate the cached st_mtime and st_ctime attributes.

Also i_size should be explicitly be set to zero as it is used sometimes without refreshing the cache.

---

 fs/fuse/file.c |   10 ++++++++++
 1 file changed, 10 insertions(+)
fs/fuse/file.c +10 -0
Index: linux-2.6/fs/fuse/file.c
===================================================================
@@ -134,6 +134,7 @@ EXPORT_SYMBOL_GPL(fuse_do_open);
 void fuse_finish_open(struct inode *inode, struct file *file)
 {
 	struct fuse_file *ff = file->private_data;
+	struct fuse_conn *fc = get_fuse_conn(inode);
 
 	if (ff->open_flags & FOPEN_DIRECT_IO)
 		file->f_op = &fuse_direct_io_file_operations;
@@ -141,6 +142,15 @@ void fuse_finish_open(struct inode *inod
 		invalidate_inode_pages2(inode->i_mapping);
 	if (ff->open_flags & FOPEN_NONSEEKABLE)
 		nonseekable_open(inode, file);
+	if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
+		struct fuse_inode *fi = get_fuse_inode(inode);
+
+		spin_lock(&fc->lock);
+		fi->attr_version = ++fc->attr_version;
+		i_size_write(inode, 0);
+		spin_unlock(&fc->lock);
+		fuse_invalidate_attr(inode);
+	}
 }
 
 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
#5 lore raw

I'm no FUSE expert, so I don't feel qualified to fully evaluate the changes, but the
locking looks correct and non-blocking, and the code still passes our test case and
doesn't crash, so it gets my vote of approval.

___
Ken

Quoted 63 lines · Miklos Szeredi

Ken,

Thanks for the patch.

Here's a slightly updated one.  Does this also look OK?

Miklos

----
Subject: fuse: fix attributes after open(O_TRUNC)
From: Ken Sumrall <[email protected]>

The attribute cache for a file was not being cleared when a file is opened with O_TRUNC.

If the filesystem's open operation truncates the file ("atomic_o_trunc" feature flag is set) then the kernel should invalidate the cached st_mtime and st_ctime attributes.

Also i_size should be explicitly be set to zero as it is used sometimes without refreshing the cache.

---

 fs/fuse/file.c |   10 ++++++++++

 1 file changed, 10 insertions(+)

Index: linux-2.6/fs/fuse/file.c
===================================================================

fs/fuse/file.c    2010-10-30 13:14:07.000000000 +0200 renamed from fs/fuse/file.c       2010-10-27 14:04:02.000000000 +0200 +10 -0
@@ -134,6 +134,7 @@ EXPORT_SYMBOL_GPL(fuse_do_open);
  void fuse_finish_open(struct inode *inode, struct file *file)
  {
        struct fuse_file *ff = file->private_data;
+       struct fuse_conn *fc = get_fuse_conn(inode);
 
        if (ff->open_flags & FOPEN_DIRECT_IO)
                file->f_op = &fuse_direct_io_file_operations;
@@ -141,6 +142,15 @@ void fuse_finish_open(struct inode *inod
                invalidate_inode_pages2(inode->i_mapping);
        if (ff->open_flags & FOPEN_NONSEEKABLE)
                nonseekable_open(inode, file);
+       if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
+               struct fuse_inode *fi = get_fuse_inode(inode);
+
+               spin_lock(&fc->lock);
+               fi->attr_version = ++fc->attr_version;
+               i_size_write(inode, 0);
+               spin_unlock(&fc->lock);
+               fuse_invalidate_attr(inode);
+       }
  }
 
  int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
#6 lore raw
In reply to Miklos Szeredi
Quoted 37 lines · On Sat, 30 Oct 2010 13:31:23 +0200 Miklos Szeredi <[email protected]> wrote:

Subject: fuse: fix attributes after open(O_TRUNC)
From: Ken Sumrall <[email protected]>

The attribute cache for a file was not being cleared when a file is opened with O_TRUNC.

If the filesystem's open operation truncates the file ("atomic_o_trunc" feature flag is set) then the kernel should invalidate the cached st_mtime and st_ctime attributes.

Also i_size should be explicitly be set to zero as it is used sometimes without refreshing the cache.

---

 fs/fuse/file.c |   10 ++++++++++
 1 file changed, 10 insertions(+)

Index: linux-2.6/fs/fuse/file.c
===================================================================

fs/fuse/file.c +1 -0
@@ -134,6 +134,7 @@ EXPORT_SYMBOL_GPL(fuse_do_open);
 void fuse_finish_open(struct inode *inode, struct file *file)
 {
 	struct fuse_file *ff = file->private_data;
+	struct fuse_conn *fc = get_fuse_conn(inode);
 
 	if (ff->open_flags & FOPEN_DIRECT_IO)
 		file->f_op = &fuse_direct_io_file_operations;

fs/fuse/file.c void fuse_finish_open(struct inode *inod · line 150

Quoted 9 lines
fs/fuse/file.c +6 -0
@@ -141,6 +142,15 @@ void fuse_finish_open(struct inode *inod
 		invalidate_inode_pages2(inode->i_mapping);
 	if (ff->open_flags & FOPEN_NONSEEKABLE)
 		nonseekable_open(inode, file);
+	if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
+		struct fuse_inode *fi = get_fuse_inode(inode);
+
+		spin_lock(&fc->lock);
+		fi->attr_version = ++fc->attr_version;
+		i_size_write(inode, 0);

i_size_write() requires that i_mutex be held, as described at the i_size_write() definition site.

Is it reliably the case that i_mutex is held here?

Quoted 6 lines
fs/fuse/file.c +3 -0
+		spin_unlock(&fc->lock);
+		fuse_invalidate_attr(inode);
+	}
 }
 
 int fuse_open_common(struct inode *inode, struct file *file, bool isdir)
maintainer
#7 lore raw
Quoted 53 lines · Andrew Morton
Quoted 37 lines · On Sat, 30 Oct 2010 13:31:23 +0200 Miklos Szeredi <[email protected]> wrote:

Subject: fuse: fix attributes after open(O_TRUNC)
From: Ken Sumrall <[email protected]>

The attribute cache for a file was not being cleared when a file is opened with O_TRUNC.

If the filesystem's open operation truncates the file ("atomic_o_trunc" feature flag is set) then the kernel should invalidate the cached st_mtime and st_ctime attributes.

Also i_size should be explicitly be set to zero as it is used sometimes without refreshing the cache.

---

 fs/fuse/file.c |   10 ++++++++++
 1 file changed, 10 insertions(+)

Index: linux-2.6/fs/fuse/file.c
===================================================================

fs/fuse/file.c +1 -0
@@ -134,6 +134,7 @@ EXPORT_SYMBOL_GPL(fuse_do_open);
 void fuse_finish_open(struct inode *inode, struct file *file)
 {
 	struct fuse_file *ff = file->private_data;
+	struct fuse_conn *fc = get_fuse_conn(inode);
 
 	if (ff->open_flags & FOPEN_DIRECT_IO)
 		file->f_op = &fuse_direct_io_file_operations;

fs/fuse/file.c void fuse_finish_open(struct inode *inod · line 150

Quoted 9 lines
fs/fuse/file.c +6 -0
@@ -141,6 +142,15 @@ void fuse_finish_open(struct inode *inod
 		invalidate_inode_pages2(inode->i_mapping);
 	if (ff->open_flags & FOPEN_NONSEEKABLE)
 		nonseekable_open(inode, file);
+	if (fc->atomic_o_trunc && (file->f_flags & O_TRUNC)) {
+		struct fuse_inode *fi = get_fuse_inode(inode);
+
+		spin_lock(&fc->lock);
+		fi->attr_version = ++fc->attr_version;
+		i_size_write(inode, 0);

i_size_write() requires that i_mutex be held, as described at the i_size_write() definition site.

Quoted 1 line

Is it reliably the case that i_mutex is held here?

No. AFAICS any lock will do, not just i_mutex. Fuse consistently uses fc->lock to protect i_size_write().

Thanks,
Miklos