Skip to contentSkip to conversation

fuse: readdir caching

PATCH In review Miklos Szeredi 2 participants 11 messages Sep 28, 2018

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

cover fuse: readdir caching

Allow caching readdir in fuse. Use the page cache for this, which solves cache shrinking on memory pressure.

I think it might make sense to extract something like this into a set of VFS helpers, so that other filesystems can also make use of this facility.

Thanks,
Miklos

---
Miklos Szeredi (9):
fuse: split out readdir.c
fuse: add FOPEN_CACHE_DIR
fuse: extract fuse_emit() helper
fuse: allow caching readdir
fuse: allow using readdir cache
fuse: add readdir cache version
fuse: use mtime for readdir cache verification
fuse: use iversion for readdir cache verification
fuse: reduce size of struct fuse_inode

 fs/fuse/Makefile          |   2 +-
 fs/fuse/dir.c             | 293 +++---------------------
 fs/fuse/file.c            |  10 +
 fs/fuse/fuse_i.h          |  83 ++++++-
 fs/fuse/inode.c           |  11 +-
 fs/fuse/readdir.c         | 567 ++++++++++++++++++++++++++++++++++++++++++++++
 include/uapi/linux/fuse.h |   7 +-
 7 files changed, 688 insertions(+), 285 deletions(-)
 create mode 100644 fs/fuse/readdir.c
Signature · 1 line
2.14.3
#1 lore raw

PATCH 1/9 fuse: split out readdir.c

Directory reading code is about to grow larger, so split it out from dir.c into a new source file.

---
 fs/fuse/Makefile  |   2 +-
 fs/fuse/dir.c     | 259 +-----------------------------------------------------
 fs/fuse/fuse_i.h  |  12 +++
 fs/fuse/readdir.c | 259 ++++++++++++++++++++++++++++++++++++++++++++++++++++++
 4 files changed, 274 insertions(+), 258 deletions(-)
 create mode 100644 fs/fuse/readdir.c
fs/fuse/Makefile +1 -1
index 60da84a86dab..f7b807bc1027 100644
@@ -5,4 +5,4 @@
 obj-$(CONFIG_FUSE_FS) += fuse.o
 obj-$(CONFIG_CUSE) += cuse.o
 
-fuse-objs := dev.o dir.o file.o inode.o control.o xattr.o acl.o
+fuse-objs := dev.o dir.o file.o inode.o control.o xattr.o acl.o readdir.o
fs/fuse/dir.c +2 -257

289 lines not on this page · raw message

fs/fuse/fuse_i.h +12 -0
index f78e9614bb5f..5e01ea3d137d 100644
@@ -697,6 +697,11 @@ static inline u64 get_node_id(struct inode *inode)
 	return get_fuse_inode(inode)->nodeid;
 }
 
+static inline int invalid_nodeid(u64 nodeid)
+{
+	return !nodeid || nodeid == FUSE_ROOT_ID;
+}
+
 /** Device operations */
 extern const struct file_operations fuse_dev_operations;
 
@@ -873,6 +878,9 @@ void fuse_invalidate_entry_cache(struct dentry *entry);
 
 void fuse_invalidate_atime(struct inode *inode);
 
+u64 entry_attr_timeout(struct fuse_entry_out *o);
+void fuse_change_entry_timeout(struct dentry *entry, struct fuse_entry_out *o);
+
 /**
  * Acquire reference to fuse_conn
  */
@@ -992,4 +1000,8 @@ struct posix_acl;
 struct posix_acl *fuse_get_acl(struct inode *inode, int type);
 int fuse_set_acl(struct inode *inode, struct posix_acl *acl, int type);
 
+
+/* readdir.c */
+int fuse_readdir(struct file *file, struct dir_context *ctx);
+
 #endif /* _FS_FUSE_I_H */
fs/fuse/readdir.c new file +259 -0

259 lines not on this page · raw message

Signature · 1 line
2.14.3
#2 lore raw

PATCH 2/9 fuse: add FOPEN_CACHE_DIR

In reply to Miklos Szeredi

Add flag returned by OPENDIR request to allow kernel to cache directory contents in page cache. The effect of FOPEN_CACHE_DIR is twofold:

a) if not already cached, it writes entries into the cache

b) if already cached, it allows reading entries from the cache

The FOPEN_KEEP_CACHE has the same effect as on regular files: unless this flag is given the cache is cleared upon completion of open.

So FOPEN_KEEP_CACHE and FOPEN_KEEP_CACHE flags should be used together to make use of the directory caching facility introduced in the following patches.

The FUSE_AUTO_INVAL_DATA flag returned in INIT reply also has the same affect on the directory cache as it has on data cache for regular files.

---
 include/uapi/linux/fuse.h | 7 ++++++-
 1 file changed, 6 insertions(+), 1 deletion(-)
include/uapi/linux/fuse.h +6 -1
index 92fa24c24c92..e30e3a6868cd 100644
@@ -116,6 +116,9 @@
  *
  *  7.27
  *  - add FUSE_ABORT_ERROR
+ *
+ *  7.28
+ *  - add FOPEN_CACHE_DIR
  */
 
 #ifndef _LINUX_FUSE_H
@@ -151,7 +154,7 @@
 #define FUSE_KERNEL_VERSION 7
 
 /** Minor version number of this interface */
-#define FUSE_KERNEL_MINOR_VERSION 27
+#define FUSE_KERNEL_MINOR_VERSION 28
 
 /** The node ID of the root inode */
 #define FUSE_ROOT_ID 1
@@ -219,10 +222,12 @@ struct fuse_file_lock {
  * FOPEN_DIRECT_IO: bypass page cache for this open file
  * FOPEN_KEEP_CACHE: don't invalidate the data cache on open
  * FOPEN_NONSEEKABLE: the file is not seekable
+ * FOPEN_CACHE_DIR: allow caching this directory
  */
 #define FOPEN_DIRECT_IO		(1 << 0)
 #define FOPEN_KEEP_CACHE	(1 << 1)
 #define FOPEN_NONSEEKABLE	(1 << 2)
+#define FOPEN_CACHE_DIR		(1 << 3)
 
 /**
  * INIT request/reply flags
Signature · 1 line
2.14.3
#3 lore raw

PATCH 3/9 fuse: extract fuse_emit() helper

In reply to Miklos Szeredi

Prepare for cache filling by introducing a helper for emitting a single directory entry.

---
 fs/fuse/readdir.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
fs/fuse/readdir.c +9 -4
index 3e100e00e21e..65336c93c1f4 100644
@@ -26,6 +26,13 @@ static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
 	return false;
 }
 
+static bool fuse_emit(struct file *file, struct dir_context *ctx,
+		      struct fuse_dirent *dirent)
+{
+	return dir_emit(ctx, dirent->name, dirent->namelen, dirent->ino,
+			dirent->type);
+}
+
 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
 			 struct dir_context *ctx)
 {
@@ -39,8 +46,7 @@ static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
 		if (memchr(dirent->name, '/', dirent->namelen) != NULL)
 			return -EIO;
 
-		if (!dir_emit(ctx, dirent->name, dirent->namelen,
-			       dirent->ino, dirent->type))
+		if (!fuse_emit(file, ctx, dirent))
 			break;
 
 		buf += reclen;
@@ -183,8 +189,7 @@ static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
 			   we need to send a FORGET for each of those
 			   which we did not link.
 			*/
-			over = !dir_emit(ctx, dirent->name, dirent->namelen,
-				       dirent->ino, dirent->type);
+			over = !fuse_emit(file, ctx, dirent);
 			if (!over)
 				ctx->pos = dirent->off;
 		}
Signature · 1 line
2.14.3
#4 lore raw

PATCH 4/9 fuse: allow caching readdir

In reply to Miklos Szeredi

This patch just adds the cache filling functions, which are invoked if FOPEN_CACHE_DIR flag is set in the OPENDIR reply.

Cache reading and cache invalidation are added by subsequent patches.

The directory cache uses the page cache. Directory entries are packed into a page in the same format as in the READDIR reply. A page only contains whole entries, the space at the end of the page is cleared. The page is locked while being modified.

Multiple parallel readdirs on the same directory can fill the cache; the only constraint is that continuity must be maintained (d_off of last entry points to position of current entry).

---
 fs/fuse/fuse_i.h  | 15 ++++++++++
 fs/fuse/inode.c   |  4 +++
 fs/fuse/readdir.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 107 insertions(+), 1 deletion(-)
fs/fuse/fuse_i.h +15 -0
index 5e01ea3d137d..d01c4606c149 100644
@@ -103,6 +103,21 @@ struct fuse_inode {
 	/** List of writepage requestst (pending or sent) */
 	struct list_head writepages;
 
+	/** readdir cache */
+	struct {
+		/** true if fully cached */
+		bool cached;
+
+		/** size of cache */
+		loff_t size;
+
+		/** position at end of cache (position of next entry) */
+		loff_t pos;
+
+		/** protects above fields */
+		spinlock_t lock;
+	} rdc;
+
 	/** Miscellaneous bits describing inode state */
 	unsigned long state;
 
fs/fuse/inode.c +4 -0
index db9e60b7eb69..03d8105a851d 100644
@@ -100,6 +100,10 @@ static struct inode *fuse_alloc_inode(struct super_block *sb)
 	INIT_LIST_HEAD(&fi->queued_writes);
 	INIT_LIST_HEAD(&fi->writepages);
 	init_waitqueue_head(&fi->page_waitq);
+	spin_lock_init(&fi->rdc.lock);
+	fi->rdc.cached = false;
+	fi->rdc.size = 0;
+	fi->rdc.pos = 0;
 	mutex_init(&fi->mutex);
 	fi->forget = fuse_alloc_forget();
 	if (!fi->forget) {
fs/fuse/readdir.c +88 -1

104 lines not on this page · raw message

Signature · 1 line
2.14.3
#5 lore raw

Re: [PATCH 4/9] fuse: allow caching readdir

fs/fuse/fuse_i.h struct fuse_inode { · line 109

Quoted 8 lines · Miklos Szeredi
fs/fuse/fuse_i.h +4 -0
@@ -103,6 +103,21 @@ struct fuse_inode {
 	/** List of writepage requestst (pending or sent) */
 	struct list_head writepages;
 
+	/** readdir cache */
+	struct {
+		/** true if fully cached */
+		bool cached;

umm, you're using kernel-doc markers without formatting this comment as kernel-doc. See Documentation/doc-guide/kernel-doc.rst but I believe you should be writing this as:

		/** @rdc.cached: true if fully cached */

or just use /* */ if you don't want kernel-doc.

#6 lore raw

PATCH 5/9 fuse: allow using readdir cache

In reply to Miklos Szeredi

The cache is only used if it's completed, not while it's still being filled; this constraint could be lifted later, if it turns out to be useful.

Introduce state in struct fuse_file that indicates the position within the cache. After a seek, reset the position to the beginning of the cache and search the cache for the current position. If the current position is not found in the cache, then fall back to uncached readdir.

It can also happen that page(s) disappear from the cache, in which case we must also fall back to uncached readdir.

---
 fs/fuse/file.c    |   2 +
 fs/fuse/fuse_i.h  |  15 ++++++
 fs/fuse/readdir.c | 148 ++++++++++++++++++++++++++++++++++++++++++++++++++++--
 3 files changed, 161 insertions(+), 4 deletions(-)
fs/fuse/file.c +2 -0
index 32d0b883e74f..101e64897b5f 100644
@@ -59,6 +59,7 @@ struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
 	}
 
 	INIT_LIST_HEAD(&ff->write_entry);
+	mutex_init(&ff->readdir.lock);
 	refcount_set(&ff->count, 1);
 	RB_CLEAR_NODE(&ff->polled_node);
 	init_waitqueue_head(&ff->poll_wait);
@@ -73,6 +74,7 @@ struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
 void fuse_file_free(struct fuse_file *ff)
 {
 	fuse_request_free(ff->reserved_req);
+	mutex_destroy(&ff->readdir.lock);
 	kfree(ff);
 }
 
fs/fuse/fuse_i.h +15 -0
index d01c4606c149..116fe14053f1 100644
@@ -163,6 +163,21 @@ struct fuse_file {
 	/** Entry on inode's write_files list */
 	struct list_head write_entry;
 
+	/** Readdir related */
+	struct {
+		/**
+		 * Protects below fields against (crazy) parallel readdir on
+		 * same open file.  Uncontended in the normal case.
+		 */
+		struct mutex lock;
+
+		/** Dir stream position */
+		loff_t pos;
+
+		/** Offset in cache */
+		loff_t cache_off;
+	} readdir;
+
 	/** RB node to be linked on fuse_conn->polled_files */
 	struct rb_node polled_node;
 
fs/fuse/readdir.c +144 -4

163 lines not on this page · raw message

Signature · 1 line
2.14.3
#7 lore raw

PATCH 6/9 fuse: add readdir cache version

In reply to Miklos Szeredi

Allow the cache to be invalidated when page(s) have gone missing. In this case increment the version of the cache and reset to an empty state.

Add a version number to the directory stream in struct fuse_file as well, indicating the version of the cache it's supposed to be reading. If the cache version doesn't match the stream's version, then reset the stream to the beginning of the cache.

---
 fs/fuse/fuse_i.h  |  7 +++++++
 fs/fuse/inode.c   |  1 +
 fs/fuse/readdir.c | 45 ++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 52 insertions(+), 1 deletion(-)
fs/fuse/fuse_i.h +7 -0
index 116fe14053f1..ef018ea5bcd9 100644
@@ -114,6 +114,9 @@ struct fuse_inode {
 		/** position at end of cache (position of next entry) */
 		loff_t pos;
 
+		/** version of the cache */
+		u64 version;
+
 		/** protects above fields */
 		spinlock_t lock;
 	} rdc;
@@ -176,6 +179,10 @@ struct fuse_file {
 
 		/** Offset in cache */
 		loff_t cache_off;
+
+		/** Version of cache we are reading */
+		u64 version;
+
 	} readdir;
 
 	/** RB node to be linked on fuse_conn->polled_files */
fs/fuse/inode.c +1 -0
index 03d8105a851d..6d0a87308f86 100644
@@ -104,6 +104,7 @@ static struct inode *fuse_alloc_inode(struct super_block *sb)
 	fi->rdc.cached = false;
 	fi->rdc.size = 0;
 	fi->rdc.pos = 0;
+	fi->rdc.version = 0;
 	mutex_init(&fi->mutex);
 	fi->forget = fuse_alloc_forget();
 	if (!fi->forget) {
fs/fuse/readdir.c +44 -1

88 lines not on this page · raw message

Signature · 1 line
2.14.3
#8 lore raw

PATCH 7/9 fuse: use mtime for readdir cache verification

In reply to Miklos Szeredi

Store the modification time of the directory in the cache, obtained before starting to fill the cache.

When reading the cache, verify that the directory hasn't changed, by checking if current modification time is the same as the one stored in the cache.

This only needs to be done when the current file position is at the beginning of the directory, as mandated by POSIX.

---
 fs/fuse/fuse_i.h  |  3 +++
 fs/fuse/readdir.c | 38 ++++++++++++++++++++++++++++++++++----
 2 files changed, 37 insertions(+), 4 deletions(-)
fs/fuse/fuse_i.h +3 -0
index ef018ea5bcd9..e498f9edf01f 100644
@@ -117,6 +117,9 @@ struct fuse_inode {
 		/** version of the cache */
 		u64 version;
 
+		/** modification time of directory when cache was started */
+		struct timespec64 mtime;
+
 		/** protects above fields */
 		spinlock_t lock;
 	} rdc;
fs/fuse/readdir.c +34 -4
index edb445c4cfbd..f96525cef518 100644
@@ -397,8 +397,10 @@ static enum fuse_parse_result fuse_parse_cache(struct fuse_file *ff,
 	return res;
 }
 
-static void fuse_rdc_reset(struct fuse_inode *fi)
+static void fuse_rdc_reset(struct inode *inode)
 {
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
 	fi->rdc.cached = false;
 	fi->rdc.version++;
 	fi->rdc.size = 0;
@@ -411,6 +413,7 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 {
 	struct fuse_file *ff = file->private_data;
 	struct inode *inode = file_inode(file);
+	struct fuse_conn *fc = get_fuse_conn(inode);
 	struct fuse_inode *fi = get_fuse_inode(inode);
 	enum fuse_parse_result res;
 	pgoff_t index;
@@ -424,12 +427,40 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		ff->readdir.cache_off = 0;
 	}
 
+	/*
+	 * We're just about to start reading into the cache or reading the
+	 * cache; both cases require an up-to-date mtime value.
+	 */
+	if (!ctx->pos && fc->auto_inval_data) {
+		int err = fuse_update_attributes(inode, file);
+
+		if (err)
+			return err;
+	}
+
 retry:
 	spin_lock(&fi->rdc.lock);
+retry_locked:
 	if (!fi->rdc.cached) {
+		/* Starting cache? Set cache mtime. */
+		if (!ctx->pos && !fi->rdc.size) {
+			fi->rdc.mtime = inode->i_mtime;
+		}
 		spin_unlock(&fi->rdc.lock);
 		return UNCACHED;
 	}
+	/*
+	 * When at the beginning of the directory (i.e. just after opendir(3) or
+	 * rewinddir(3)), then need to check whether directory contents have
+	 * changed, and reset the cache if so.
+	 */
+	if (!ctx->pos) {
+		if (!timespec64_equal(&fi->rdc.mtime, &inode->i_mtime)) {
+			fuse_rdc_reset(inode);
+			goto retry_locked;
+		}
+	}
+
 	/*
 	 * If cache version changed since the last getdents() call, then reset
 	 * the cache stream.
@@ -467,9 +498,8 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		 * Uh-oh: page gone missing, cache is useless
 		 */
 		if (fi->rdc.version == ff->readdir.version)
-			fuse_rdc_reset(fi);
-		spin_unlock(&fi->rdc.lock);
-		return UNCACHED;
+			fuse_rdc_reset(inode);
+		goto retry_locked;
 	}
 
 	/* Make sure it's still the same version after getting the page. */
Signature · 1 line
2.14.3
#9 lore raw

PATCH 8/9 fuse: use iversion for readdir cache verification

In reply to Miklos Szeredi

Use the internal iversion counter to make sure modifications of the directory through this filesystem are not missed by the mtime check (due to mtime granularity).

---
 fs/fuse/dir.c     | 21 ++++++++++++++-------
 fs/fuse/fuse_i.h  |  3 +++
 fs/fuse/readdir.c |  5 ++++-
 3 files changed, 21 insertions(+), 8 deletions(-)
fs/fuse/dir.c +14 -7
index 3a333b0ea9ad..6800fdc3e730 100644
@@ -14,6 +14,7 @@
 #include <linux/namei.h>
 #include <linux/slab.h>
 #include <linux/xattr.h>
+#include <linux/iversion.h>
 #include <linux/posix_acl.h>
 
 static void fuse_advise_use_readdirplus(struct inode *dir)
@@ -89,6 +90,12 @@ void fuse_invalidate_attr(struct inode *inode)
 	get_fuse_inode(inode)->i_time = 0;
 }
 
+static void fuse_dir_changed(struct inode *dir)
+{
+	fuse_invalidate_attr(dir);
+	inode_maybe_inc_iversion(dir, false);
+}
+
 /**
  * Mark the attributes as stale due to an atime change.  Avoid the invalidate if
  * atime is not used.
@@ -447,7 +454,7 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry,
 	kfree(forget);
 	d_instantiate(entry, inode);
 	fuse_change_entry_timeout(entry, &outentry);
-	fuse_invalidate_attr(dir);
+	fuse_dir_changed(dir);
 	err = finish_open(file, entry, generic_file_open);
 	if (err) {
 		fuse_sync_release(ff, flags);
@@ -561,7 +568,7 @@ static int create_new_entry(struct fuse_conn *fc, struct fuse_args *args,
 	} else {
 		fuse_change_entry_timeout(entry, &outarg);
 	}
-	fuse_invalidate_attr(dir);
+	fuse_dir_changed(dir);
 	return 0;
 
  out_put_forget_req:
@@ -671,7 +678,7 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)
 			drop_nlink(inode);
 		spin_unlock(&fc->lock);
 		fuse_invalidate_attr(inode);
-		fuse_invalidate_attr(dir);
+		fuse_dir_changed(dir);
 		fuse_invalidate_entry_cache(entry);
 		fuse_update_ctime(inode);
 	} else if (err == -EINTR)
@@ -693,7 +700,7 @@ static int fuse_rmdir(struct inode *dir, struct dentry *entry)
 	err = fuse_simple_request(fc, &args);
 	if (!err) {
 		clear_nlink(d_inode(entry));
-		fuse_invalidate_attr(dir);
+		fuse_dir_changed(dir);
 		fuse_invalidate_entry_cache(entry);
 	} else if (err == -EINTR)
 		fuse_invalidate_entry(entry);
@@ -732,9 +739,9 @@ static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
 			fuse_update_ctime(d_inode(newent));
 		}
 
-		fuse_invalidate_attr(olddir);
+		fuse_dir_changed(olddir);
 		if (olddir != newdir)
-			fuse_invalidate_attr(newdir);
+			fuse_dir_changed(newdir);
 
 		/* newent will end up negative */
 		if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
@@ -967,7 +974,7 @@ int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
 	if (!entry)
 		goto unlock;
 
-	fuse_invalidate_attr(parent);
+	fuse_dir_changed(parent);
 	fuse_invalidate_entry(entry);
 
 	if (child_nodeid != 0 && d_really_is_positive(entry)) {
fs/fuse/fuse_i.h +3 -0
index e498f9edf01f..1e8d20e86760 100644
@@ -120,6 +120,9 @@ struct fuse_inode {
 		/** modification time of directory when cache was started */
 		struct timespec64 mtime;
 
+		/** iversion of directory when cache was started */
+		u64 iversion;
+
 		/** protects above fields */
 		spinlock_t lock;
 	} rdc;
fs/fuse/readdir.c +4 -1
index f96525cef518..d91ae7449b73 100644
@@ -8,6 +8,7 @@
 
 
 #include "fuse_i.h"
+#include <linux/iversion.h>
 #include <linux/posix_acl.h>
 
 static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
@@ -445,6 +446,7 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 		/* Starting cache? Set cache mtime. */
 		if (!ctx->pos && !fi->rdc.size) {
 			fi->rdc.mtime = inode->i_mtime;
+			fi->rdc.iversion = inode_query_iversion(inode);
 		}
 		spin_unlock(&fi->rdc.lock);
 		return UNCACHED;
@@ -455,7 +457,8 @@ static int fuse_readdir_cached(struct file *file, struct dir_context *ctx)
 	 * changed, and reset the cache if so.
 	 */
 	if (!ctx->pos) {
-		if (!timespec64_equal(&fi->rdc.mtime, &inode->i_mtime)) {
+		if (inode_peek_iversion(inode) != fi->rdc.iversion ||
+		    !timespec64_equal(&fi->rdc.mtime, &inode->i_mtime)) {
 			fuse_rdc_reset(inode);
 			goto retry_locked;
 		}
Signature · 1 line
2.14.3
#10 lore raw

PATCH 9/9 fuse: reduce size of struct fuse_inode

In reply to Miklos Szeredi

Do this by grouping fields used for cached writes and putting them into a union with fileds used for cached readdir (with obviously no overlap, since we don't have hybrid objects).

---
 fs/fuse/dir.c    | 13 +++++++++++-
 fs/fuse/file.c   |  8 ++++++++
 fs/fuse/fuse_i.h | 62 +++++++++++++++++++++++++++++++-------------------------
 fs/fuse/inode.c  | 16 ++++-----------
 4 files changed, 58 insertions(+), 41 deletions(-)
fs/fuse/dir.c +12 -1
index 6800fdc3e730..d1b2f42d746e 100644
@@ -1414,8 +1414,11 @@ int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
 		file = NULL;
 	}
 
-	if (attr->ia_valid & ATTR_SIZE)
+	if (attr->ia_valid & ATTR_SIZE) {
+		if (WARN_ON(!S_ISREG(inode->i_mode)))
+			return -EIO;
 		is_truncate = true;
+	}
 
 	if (is_truncate) {
 		fuse_set_nowrite(inode);
@@ -1619,8 +1622,16 @@ void fuse_init_common(struct inode *inode)
 
 void fuse_init_dir(struct inode *inode)
 {
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
 	inode->i_op = &fuse_dir_inode_operations;
 	inode->i_fop = &fuse_dir_operations;
+
+	spin_lock_init(&fi->rdc.lock);
+	fi->rdc.cached = false;
+	fi->rdc.size = 0;
+	fi->rdc.pos = 0;
+	fi->rdc.version = 0;
 }
 
 void fuse_init_symlink(struct inode *inode)
fs/fuse/file.c +8 -0
index 101e64897b5f..1ca3fe5be3e4 100644
@@ -3064,6 +3064,14 @@ static const struct address_space_operations fuse_file_aops  = {
 
 void fuse_init_file_inode(struct inode *inode)
 {
+	struct fuse_inode *fi = get_fuse_inode(inode);
+
 	inode->i_fop = &fuse_file_operations;
 	inode->i_data.a_ops = &fuse_file_aops;
+
+	INIT_LIST_HEAD(&fi->write_files);
+	INIT_LIST_HEAD(&fi->queued_writes);
+	fi->writectr = 0;
+	init_waitqueue_head(&fi->page_waitq);
+	INIT_LIST_HEAD(&fi->writepages);
 }
fs/fuse/fuse_i.h +34 -28

79 lines not on this page · raw message

fs/fuse/inode.c +4 -12
index 6d0a87308f86..bd58ab7e1858 100644
@@ -93,18 +93,8 @@ static struct inode *fuse_alloc_inode(struct super_block *sb)
 	fi->nodeid = 0;
 	fi->nlookup = 0;
 	fi->attr_version = 0;
-	fi->writectr = 0;
 	fi->orig_ino = 0;
 	fi->state = 0;
-	INIT_LIST_HEAD(&fi->write_files);
-	INIT_LIST_HEAD(&fi->queued_writes);
-	INIT_LIST_HEAD(&fi->writepages);
-	init_waitqueue_head(&fi->page_waitq);
-	spin_lock_init(&fi->rdc.lock);
-	fi->rdc.cached = false;
-	fi->rdc.size = 0;
-	fi->rdc.pos = 0;
-	fi->rdc.version = 0;
 	mutex_init(&fi->mutex);
 	fi->forget = fuse_alloc_forget();
 	if (!fi->forget) {
@@ -124,8 +114,10 @@ static void fuse_i_callback(struct rcu_head *head)
 static void fuse_destroy_inode(struct inode *inode)
 {
 	struct fuse_inode *fi = get_fuse_inode(inode);
-	BUG_ON(!list_empty(&fi->write_files));
-	BUG_ON(!list_empty(&fi->queued_writes));
+	if (S_ISREG(inode->i_mode)) {
+		WARN_ON(!list_empty(&fi->write_files));
+		WARN_ON(!list_empty(&fi->queued_writes));
+	}
 	mutex_destroy(&fi->mutex);
 	kfree(fi->forget);
 	call_rcu(&inode->i_rcu, fuse_i_callback);
Signature · 1 line
2.14.3