Skip to contentSkip to conversation

CUSE: implement CUSE, take #3

PATCH In review Tejun Heo 2 participants 17 messages Apr 14, 2009 – May 8, 2009

Subsystems (2)
lore
Share & tools
lore

Tejun Heo

author
#0 lore raw

PATCH 1/1 CUSE: implement CUSE, take #3

Hello, Miklos.

This is the third take of implement-CUSE patchset. Sorry about the huge delay. :-)

Changes from the last take[L] are...

* Rather large FUSE update to make inode, superblock and mnt optional.

* CUSE no longer creates its superblock or mount. It simply sets cdev file->private_data to fuse_file and operates on it.

* cdev lifetime handling fixed. CUSE cdev now atomically looks up matching device (connection) like other cdevs do.

* CUSE init protocal updated. An additional request CUSE_INIT_DONE is sent after successful creation of devices. This is to allow userland server to synchronize init completion.

This patchset contains the following six patches.

0001-FUSE-misc-cleanups.patch 0002-FUSE-use-fuse-objects-as-the-main-objects-in-file-h.patch 0003-FUSE-make-ff-fi-optional.patch 0004-FUSE-update-fuse_conn_init-and-separate-out-fuse_.patch 0005-FUSE-export-symbols-to-be-used-by-CUSE.patch 0006-CUSE-implement-CUSE-Character-device-in-Userspace.patch

0001 is misc preparation. 0002 updates fuse file handling operations such that it primarily operations on fuse_file instead of vfs objects. This patch is rather huge but I couldn't think of a good meaningful way to chop it down further. As scary as it looks, it should have zero functional difference and most changes are mechanical.

0003 makes fuse_file->fi (added by 0002) optional so that fuse_file can be used separately.

0004-0005 prepares for CUSE implementation and 0006 does it.

I didn't replace the kthread init helper with req->end() callback for two reasons. Firstly, init involves issuing two commands - FUSE_INIT and CUSE_INIT. Chaning init sequence via req->end() would require another split in the init sequence. Secondly, the second command would have to be issued from ->end(), which is just hairy. I think doing the latter part of initialization in kthread is better here.

This patchset is on top of the current linus#master[1] and available in the following git tree.

git://git.kernel.org/pub/scm/linux/kernel/git/tj/misc.git cuse

diffstat follows.

 fs/Kconfig            |   10 
 fs/fuse/Makefile      |    1 
 fs/fuse/cuse.c        |  675 ++++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/dev.c         |    9 
 fs/fuse/dir.c         |   97 ++++---
 fs/fuse/file.c        |  613 +++++++++++++++++++++++----------------------
 fs/fuse/fuse_i.h      |  100 ++++++-
 fs/fuse/inode.c       |   54 ++--
 include/linux/cuse.h  |   49 +++
 include/linux/fuse.h  |    2 
 include/linux/magic.h |    5 
 11 files changed, 1237 insertions(+), 378 deletions(-)

Thanks.

--
tejun

[L] http://thread.gmane.org/gmane.linux.kernel/759831
[1] 80a04d3f2f94fb68b5df05e3ac6697130bc3467a

author
#1 lore raw

PATCH 1/6 FUSE: misc cleanups

* fuse_file_alloc() was structured in weird way. The success path was split between else block and code following the block. Restructure the code such that it's easier to read and modify.

* Unindent success path of fuse_release_common() to ease future changes.

* Rename fuse_file_do_ioctl() to fuse_file_ioctl_common() for consistency.

---
 fs/fuse/file.c |   89 ++++++++++++++++++++++++++++++--------------------------
 1 files changed, 48 insertions(+), 41 deletions(-)
fs/fuse/file.c +48 -41
index 2b25133..01c7245 100644
@@ -49,22 +49,26 @@ static int fuse_send_open(struct inode *inode, struct file *file, int isdir,
 struct fuse_file *fuse_file_alloc(struct fuse_conn *fc)
 {
 	struct fuse_file *ff;
+
 	ff = kmalloc(sizeof(struct fuse_file), GFP_KERNEL);
-	if (ff) {
-		ff->reserved_req = fuse_request_alloc();
-		if (!ff->reserved_req) {
-			kfree(ff);
-			return NULL;
-		} else {
-			INIT_LIST_HEAD(&ff->write_entry);
-			atomic_set(&ff->count, 0);
-			spin_lock(&fc->lock);
-			ff->kh = ++fc->khctr;
-			spin_unlock(&fc->lock);
-		}
-		RB_CLEAR_NODE(&ff->polled_node);
-		init_waitqueue_head(&ff->poll_wait);
+	if (unlikely(!ff))
+		return NULL;
+
+	ff->reserved_req = fuse_request_alloc();
+	if (unlikely(!ff->reserved_req)) {
+		kfree(ff);
+		return NULL;
 	}
+
+	INIT_LIST_HEAD(&ff->write_entry);
+	atomic_set(&ff->count, 0);
+	RB_CLEAR_NODE(&ff->polled_node);
+	init_waitqueue_head(&ff->poll_wait);
+
+	spin_lock(&fc->lock);
+	ff->kh = ++fc->khctr;
+	spin_unlock(&fc->lock);
+
 	return ff;
 }
 
@@ -158,34 +162,37 @@ void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
 
 int fuse_release_common(struct inode *inode, struct file *file, int isdir)
 {
-	struct fuse_file *ff = file->private_data;
-	if (ff) {
-		struct fuse_conn *fc = get_fuse_conn(inode);
-		struct fuse_req *req = ff->reserved_req;
+	struct fuse_conn *fc;
+	struct fuse_file *ff;
+	struct fuse_req *req;
 
-		fuse_release_fill(ff, get_node_id(inode), file->f_flags,
-				  isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
+	ff = file->private_data;
+	if (unlikely(!ff))
+		return 0;	/* return value is ignored by VFS */
 
-		/* Hold vfsmount and dentry until release is finished */
-		req->misc.release.vfsmount = mntget(file->f_path.mnt);
-		req->misc.release.dentry = dget(file->f_path.dentry);
+	fc = get_fuse_conn(inode);
+	req = ff->reserved_req;
 
-		spin_lock(&fc->lock);
-		list_del(&ff->write_entry);
-		if (!RB_EMPTY_NODE(&ff->polled_node))
-			rb_erase(&ff->polled_node, &fc->polled_files);
-		spin_unlock(&fc->lock);
+	fuse_release_fill(ff, get_node_id(inode), file->f_flags,
+			  isdir ? FUSE_RELEASEDIR : FUSE_RELEASE);
 
-		wake_up_interruptible_sync(&ff->poll_wait);
-		/*
-		 * Normally this will send the RELEASE request,
-		 * however if some asynchronous READ or WRITE requests
-		 * are outstanding, the sending will be delayed
-		 */
-		fuse_file_put(ff);
-	}
+	/* Hold vfsmount and dentry until release is finished */
+	req->misc.release.vfsmount = mntget(file->f_path.mnt);
+	req->misc.release.dentry = dget(file->f_path.dentry);
+
+	spin_lock(&fc->lock);
+	list_del(&ff->write_entry);
+	if (!RB_EMPTY_NODE(&ff->polled_node))
+		rb_erase(&ff->polled_node, &fc->polled_files);
+	spin_unlock(&fc->lock);
 
-	/* Return value is ignored by VFS */
+	wake_up_interruptible_sync(&ff->poll_wait);
+	/*
+	 * Normally this will send the RELEASE request, however if
+	 * some asynchronous READ or WRITE requests are outstanding,
+	 * the sending will be delayed.
+	 */
+	fuse_file_put(ff);
 	return 0;
 }
 
@@ -1601,8 +1608,8 @@ static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
  * limits ioctl data transfers to well-formed ioctls and is the forced
  * behavior for all FUSE servers.
  */
-static long fuse_file_do_ioctl(struct file *file, unsigned int cmd,
-			       unsigned long arg, unsigned int flags)
+static long fuse_file_ioctl_common(struct file *file, unsigned int cmd,
+				   unsigned long arg, unsigned int flags)
 {
 	struct inode *inode = file->f_dentry->d_inode;
 	struct fuse_file *ff = file->private_data;
@@ -1779,13 +1786,13 @@ static long fuse_file_do_ioctl(struct file *file, unsigned int cmd,
 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
 			    unsigned long arg)
 {
-	return fuse_file_do_ioctl(file, cmd, arg, 0);
+	return fuse_file_ioctl_common(file, cmd, arg, 0);
 }
 
 static long fuse_file_compat_ioctl(struct file *file, unsigned int cmd,
 				   unsigned long arg)
 {
-	return fuse_file_do_ioctl(file, cmd, arg, FUSE_IOCTL_COMPAT);
+	return fuse_file_ioctl_common(file, cmd, arg, FUSE_IOCTL_COMPAT);
 }
 
 /*
Signature · 1 line
1.6.0.2
author
#2 lore raw

PATCH 2/6 FUSE: use fuse objects as the main objects in file handling functions

In reply to Tejun Heo

Generic struct file and/or inode have been used as handle in file related functions and fuse specific data structures - fuse_file and fuse_inode - didn't have enough linkage to other objects and thus couldn't be used as object handles (other objects couldn't be reached via ff). This works fine but at times which object is being needed by a function isn't clear and requires all fuse files to have full generic filesystem inode/superblock which won't be true for cuse files.

This patch adds ->fc, ->fi and ->file pointers to struct fuse_file and replace req->inode with req->fi and use fuse_file and fuse_inode as the main object handle in file related functions.

Functions which deal with a file now only take a pointer to struct fuse_file as object handle and deferences all other objects via it. The following three helpers are added to help non-trivial dereferencing and ease future changes.

fuse_file_nodeid(ff) : get nodeid of @ff
fuse_file_vfs_inode(ff) : return generic vfs inode backing @ff
fuse_file_bad(ff) : test whether ff's vfs inode is bad

Whenever generic vfs inode is used, it's explicitly named vfs_inode to distinguish its usage. vfs_inode is necessary and used only for open, writeback and locking.

* For functions which take both @file and @inode parameters, it's assumed that @file->f_dentry->d_inode == @inode. For functions which get @mapping someway, it's assumed that @file->f_mapping == @mapping.

* fuse_open_common() now takes @fc, @fi, @vfs_inode and initializes @ff accordingly.

* fuse_release_common() now takes @ff. The file->private_data == NULL test is moved to its callers and fuse_release_common() is made void.

* fuse_fsync() assumes that @file->f_dentry == @de.

* fuse_readpage() always sets ff->req and fuse_readpages_end() tests fc->async_read directly.

* fuse_readpages_fill() now gets struct fuse_req ** instead of struct fuse_fill_data *. fuse_file is dereferenced using req->ff and all the rest from the fuse_file. struct fuse_fill_data is dropped.

* fuse_write_fill() now takes @f_flags instead of @file.

---
 fs/fuse/dir.c    |   97 ++++++-----
 fs/fuse/file.c   |  497 ++++++++++++++++++++++++++----------------------------
 fs/fuse/fuse_i.h |   54 +++++--
 3 files changed, 333 insertions(+), 315 deletions(-)
fs/fuse/dir.c +54 -43
index 8b8eebc..63b9546 100644
@@ -84,9 +84,9 @@ static u64 entry_attr_timeout(struct fuse_entry_out *o)
  * Mark the attributes as stale, so that at the next call to
  * ->getattr() they will be fetched from userspace
  */
-void fuse_invalidate_attr(struct inode *inode)
+void fuse_invalidate_attr(struct fuse_inode *fi)
 {
-	get_fuse_inode(inode)->i_time = 0;
+	fi->i_time = 0;
 }
 
 /*
@@ -408,7 +408,7 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
 		goto out_put_forget_req;
 
 	err = -ENOMEM;
-	ff = fuse_file_alloc(fc);
+	ff = fuse_file_alloc(fc, NULL, NULL);	/* will set fi and file later */
 	if (!ff)
 		goto out_put_request;
 
@@ -454,17 +454,19 @@ static int fuse_create_open(struct inode *dir, struct dentry *entry, int mode,
 		fuse_send_forget(fc, forget_req, outentry.nodeid, 1);
 		return -ENOMEM;
 	}
+	ff->fi = get_fuse_inode(inode);
 	fuse_put_request(fc, forget_req);
 	d_instantiate(entry, inode);
 	fuse_change_entry_timeout(entry, &outentry);
-	fuse_invalidate_attr(dir);
+	fuse_invalidate_attr(ff->fi);
 	file = lookup_instantiate_filp(nd, entry, generic_file_open);
 	if (IS_ERR(file)) {
 		ff->fh = outopen.fh;
 		fuse_sync_release(fc, ff, outentry.nodeid, flags);
 		return PTR_ERR(file);
 	}
-	fuse_finish_open(inode, file, ff, &outopen);
+	ff->file = file;
+	fuse_finish_open(ff, &outopen);
 	return 0;
 
  out_free_ff:
@@ -540,7 +542,7 @@ static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
 		d_instantiate(entry, inode);
 
 	fuse_change_entry_timeout(entry, &outarg);
-	fuse_invalidate_attr(dir);
+	fuse_invalidate_attr(get_fuse_inode(dir));
 	return 0;
 
  out_put_forget_req:
@@ -643,8 +645,8 @@ static int fuse_unlink(struct inode *dir, struct dentry *entry)
 		 * lookup/getattr.
 		 */
 		clear_nlink(inode);
-		fuse_invalidate_attr(inode);
-		fuse_invalidate_attr(dir);
+		fuse_invalidate_attr(get_fuse_inode(inode));
+		fuse_invalidate_attr(get_fuse_inode(dir));
 		fuse_invalidate_entry_cache(entry);
 	} else if (err == -EINTR)
 		fuse_invalidate_entry(entry);
@@ -669,7 +671,7 @@ static int fuse_rmdir(struct inode *dir, struct dentry *entry)
 	fuse_put_request(fc, req);
 	if (!err) {
 		clear_nlink(entry->d_inode);
-		fuse_invalidate_attr(dir);
+		fuse_invalidate_attr(get_fuse_inode(dir));
 		fuse_invalidate_entry_cache(entry);
 	} else if (err == -EINTR)
 		fuse_invalidate_entry(entry);
@@ -702,11 +704,11 @@ static int fuse_rename(struct inode *olddir, struct dentry *oldent,
 	fuse_put_request(fc, req);
 	if (!err) {
 		/* ctime changes */
-		fuse_invalidate_attr(oldent->d_inode);
+		fuse_invalidate_attr(get_fuse_inode(oldent->d_inode));
 
-		fuse_invalidate_attr(olddir);
+		fuse_invalidate_attr(get_fuse_inode(olddir));
 		if (olddir != newdir)
-			fuse_invalidate_attr(newdir);
+			fuse_invalidate_attr(get_fuse_inode(newdir));
 
 		/* newent will end up negative */
 		if (newent->d_inode)
@@ -752,7 +754,7 @@ static int fuse_link(struct dentry *entry, struct inode *newdir,
 	   etc.)
 	*/
 	if (!err || err == -EINTR)
-		fuse_invalidate_attr(inode);
+		fuse_invalidate_attr(get_fuse_inode(inode));
 	return err;
 }
 
@@ -831,21 +833,20 @@ static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
 	return err;
 }
 
-int fuse_update_attributes(struct inode *inode, struct kstat *stat,
+int fuse_update_attributes(struct fuse_inode *fi, struct kstat *stat,
 			   struct file *file, bool *refreshed)
 {
-	struct fuse_inode *fi = get_fuse_inode(inode);
 	int err;
 	bool r;
 
 	if (fi->i_time < get_jiffies_64()) {
 		r = true;
-		err = fuse_do_getattr(inode, stat, file);
+		err = fuse_do_getattr(&fi->inode, stat, file);
 	} else {
 		r = false;
 		err = 0;
 		if (stat) {
-			generic_fillattr(inode, stat);
+			generic_fillattr(&fi->inode, stat);
 			stat->mode = fi->orig_i_mode;
 		}
 	}
@@ -950,7 +951,8 @@ static int fuse_permission(struct inode *inode, int mask)
 	 */
 	if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
 	    ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
-		err = fuse_update_attributes(inode, NULL, NULL, &refreshed);
+		err = fuse_update_attributes(get_fuse_inode(inode), NULL, NULL,
+					     &refreshed);
 		if (err)
 			return err;
 	}
@@ -1016,11 +1018,11 @@ static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
 	int err;
 	size_t nbytes;
 	struct page *page;
-	struct inode *inode = file->f_path.dentry->d_inode;
-	struct fuse_conn *fc = get_fuse_conn(inode);
+	struct fuse_file *ff = file->private_data;
+	struct fuse_conn *fc = ff->fc;
 	struct fuse_req *req;
 
-	if (is_bad_inode(inode))
+	if (fuse_file_bad(ff))
 		return -EIO;
 
 	req = fuse_get_req(fc);
@@ -1035,7 +1037,7 @@ static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
 	req->out.argpages = 1;
 	req->num_pages = 1;
 	req->pages[0] = page;
-	fuse_read_fill(req, file, inode, file->f_pos, PAGE_SIZE, FUSE_READDIR);
+	fuse_read_fill(req, ff, file->f_pos, PAGE_SIZE, FUSE_READDIR);
 	fuse_request_send(fc, req);
 	nbytes = req->out.args[0].size;
 	err = req->out.h.error;
@@ -1045,7 +1047,7 @@ static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
 				    filldir);
 
 	__free_page(page);
-	fuse_invalidate_attr(inode); /* atime changed */
+	fuse_invalidate_attr(ff->fi); /* atime changed */
 	return err;
 }
 
@@ -1078,7 +1080,7 @@ static char *read_link(struct dentry *dentry)
 		link[req->out.args[0].size] = '\0';
  out:
 	fuse_put_request(fc, req);
-	fuse_invalidate_attr(inode); /* atime changed */
+	fuse_invalidate_attr(get_fuse_inode(inode)); /* atime changed */
 	return link;
 }
 
@@ -1101,18 +1103,29 @@ static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
 
 static int fuse_dir_open(struct inode *inode, struct file *file)
 {
-	return fuse_open_common(inode, file, 1);
+	return fuse_open_common(get_fuse_conn(inode), get_fuse_inode(inode),
+				inode, file, 1);
 }
 
 static int fuse_dir_release(struct inode *inode, struct file *file)
 {
-	return fuse_release_common(inode, file, 1);
+	struct fuse_file *ff = file->private_data;
+
+	if (ff)
+		fuse_release_common(ff, 1);
+	return 0;	/* return value is ignored by VFS */
 }
 
 static int fuse_dir_fsync(struct file *file, struct dentry *de, int datasync)
 {
+	struct fuse_file *ff;
+
 	/* nfsd can call this with no file */
-	return file ? fuse_fsync_common(file, de, datasync, 1) : 0;
+	if (!file)
+		return 0;
+
+	ff = file->private_data;
+	return fuse_fsync_common(ff, datasync, 1);
 }
 
 static bool update_mtime(unsigned ivalid)
@@ -1163,12 +1176,11 @@ static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
  * This is done by adding a negative bias to the inode write counter
  * and waiting for all pending writes to finish.
  */
-void fuse_set_nowrite(struct inode *inode)
+void fuse_set_nowrite(struct fuse_inode *fi)
 {
-	struct fuse_conn *fc = get_fuse_conn(inode);
-	struct fuse_inode *fi = get_fuse_inode(inode);
+	struct fuse_conn *fc = get_fuse_conn(&fi->inode);
 
-	BUG_ON(!mutex_is_locked(&inode->i_mutex));
+	BUG_ON(!mutex_is_locked(&fi->inode.i_mutex));
 
 	spin_lock(&fc->lock);
 	BUG_ON(fi->writectr < 0);
@@ -1183,21 +1195,19 @@ void fuse_set_nowrite(struct inode *inode)
  * Remove the bias from the writecounter and send any queued
  * writepages.
  */
-static void __fuse_release_nowrite(struct inode *inode)
+static void __fuse_release_nowrite(struct fuse_inode *fi)
 {
-	struct fuse_inode *fi = get_fuse_inode(inode);
-
 	BUG_ON(fi->writectr != FUSE_NOWRITE);
 	fi->writectr = 0;
-	fuse_flush_writepages(inode);
+	fuse_flush_writepages(fi);
 }
 
-void fuse_release_nowrite(struct inode *inode)
+void fuse_release_nowrite(struct fuse_inode *fi)
 {
-	struct fuse_conn *fc = get_fuse_conn(inode);
+	struct fuse_conn *fc = get_fuse_conn(&fi->inode);
 
 	spin_lock(&fc->lock);
-	__fuse_release_nowrite(inode);
+	__fuse_release_nowrite(fi);
 	spin_unlock(&fc->lock);
 }
 
@@ -1214,6 +1224,7 @@ static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
 {
 	struct inode *inode = entry->d_inode;
 	struct fuse_conn *fc = get_fuse_conn(inode);
+	struct fuse_inode *fi = get_fuse_inode(inode);
 	struct fuse_req *req;
 	struct fuse_setattr_in inarg;
 	struct fuse_attr_out outarg;
@@ -1250,7 +1261,7 @@ static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
 		return PTR_ERR(req);
 
 	if (is_truncate)
-		fuse_set_nowrite(inode);
+		fuse_set_nowrite(fi);
 
 	memset(&inarg, 0, sizeof(inarg));
 	memset(&outarg, 0, sizeof(outarg));
@@ -1281,7 +1292,7 @@ static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
 	fuse_put_request(fc, req);
 	if (err) {
 		if (err == -EINTR)
-			fuse_invalidate_attr(inode);
+			fuse_invalidate_attr(fi);
 		goto error;
 	}
 
@@ -1299,7 +1310,7 @@ static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
 
 	if (is_truncate) {
 		/* NOTE: this may release/reacquire fc->lock */
-		__fuse_release_nowrite(inode);
+		__fuse_release_nowrite(fi);
 	}
 	spin_unlock(&fc->lock);
 
@@ -1317,7 +1328,7 @@ static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
 
 error:
 	if (is_truncate)
-		fuse_release_nowrite(inode);
+		fuse_release_nowrite(fi);
 
 	return err;
 }
@@ -1339,7 +1350,7 @@ static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
 	if (!fuse_allow_task(fc, current))
 		return -EACCES;
 
-	return fuse_update_attributes(inode, stat, NULL, NULL);
+	return fuse_update_attributes(get_fuse_inode(inode), stat, NULL, NULL);
 }
 
 static int fuse_setxattr(struct dentry *entry, const char *name,
fs/fuse/file.c +240 -257

1,215 lines not on this page · raw message

fs/fuse/fuse_i.h +39 -15

109 lines not on this page · raw message

Signature · 1 line
1.6.0.2
author
#3 lore raw

PATCH 3/6 FUSE: make ff->fi optional

In reply to Tejun Heo

Allow ff->fi to be NULL. fi related operations are skipped if ff->fi is unavailable and nodeid is assumed to be zero.

---
 fs/fuse/file.c   |   58 +++++++++++++++++++++++++++++++++++++----------------
 fs/fuse/fuse_i.h |    2 +-
 2 files changed, 41 insertions(+), 19 deletions(-)
fs/fuse/file.c +40 -18
index f32d2c8..54ad406 100644
@@ -339,7 +339,8 @@ int fuse_fsync_common(struct fuse_file *ff, int datasync, int isdir)
 	if (err)
 		return err;
 
-	fuse_sync_writes(ff->fi);
+	if (ff->fi)
+		fuse_sync_writes(ff->fi);
 
 	req = fuse_get_req(fc);
 	if (IS_ERR(req))
@@ -413,6 +414,9 @@ static void fuse_read_update_size(struct fuse_file *ff, loff_t size,
 	struct fuse_conn *fc = ff->fc;
 	struct fuse_inode *fi = ff->fi;
 
+	if (!fi)
+		return;
+
 	spin_lock(&fc->lock);
 	if (attr_ver == fi->attr_version && size < fi->inode.i_size) {
 		fi->attr_version = ++fc->attr_version;
@@ -441,7 +445,8 @@ static int fuse_readpage(struct file *file, struct page *page)
 	 * page-cache page, so make sure we read a properly synced
 	 * page.
 	 */
-	fuse_wait_on_page_writeback(ff->fi, page->index);
+	if (ff->fi)
+		fuse_wait_on_page_writeback(ff->fi, page->index);
 
 	req = fuse_get_req(fc);
 	err = PTR_ERR(req);
@@ -468,7 +473,8 @@ static int fuse_readpage(struct file *file, struct page *page)
 		SetPageUptodate(page);
 	}
 
-	fuse_invalidate_attr(ff->fi); /* atime changed */
+	if (ff->fi)
+		fuse_invalidate_attr(ff->fi); /* atime changed */
  out:
 	unlock_page(page);
 	return err;
@@ -489,7 +495,8 @@ static void fuse_readpages_end(struct fuse_conn *fc, struct fuse_req *req)
 		fuse_read_update_size(ff, pos, req->misc.read.attr_ver);
 	}
 
-	fuse_invalidate_attr(ff->fi); /* atime changed */
+	if (ff->fi)
+		fuse_invalidate_attr(ff->fi); /* atime changed */
 
 	for (i = 0; i < req->num_pages; i++) {
 		struct page *page = req->pages[i];
@@ -534,7 +541,8 @@ static int fuse_readpages_fill(void *data, struct page *page)
 	struct fuse_file *ff = req->ff;
 	struct fuse_conn *fc = ff->fc;
 
-	fuse_wait_on_page_writeback(ff->fi, page->index);
+	if (ff->fi)
+		fuse_wait_on_page_writeback(ff->fi, page->index);
 
 	if (req->num_pages &&
 	    (req->num_pages == FUSE_MAX_PAGES_PER_REQ ||
@@ -587,7 +595,7 @@ static ssize_t fuse_file_aio_read(struct kiocb *iocb, const struct iovec *iov,
 	struct fuse_file *ff = iocb->ki_filp->private_data;
 	struct fuse_inode *fi = ff->fi;
 
-	if (pos + iov_length(iov, nr_segs) > i_size_read(&fi->inode)) {
+	if (fi && pos + iov_length(iov, nr_segs) > i_size_read(&fi->inode)) {
 		int err;
 		/*
 		 * If trying to read past EOF, make sure the i_size
@@ -661,6 +669,9 @@ static void fuse_write_update_size(struct fuse_file *ff, loff_t pos)
 	struct fuse_conn *fc = ff->fc;
 	struct fuse_inode *fi = ff->fi;
 
+	if (!fi)
+		return;
+
 	spin_lock(&fc->lock);
 	fi->attr_version = ++fc->attr_version;
 	if (pos > fi->inode.i_size)
@@ -684,7 +695,8 @@ static int fuse_buffered_write(struct fuse_file *ff, loff_t pos, unsigned count,
 	 * Make sure writepages on the same page are not mixed up with
 	 * plain writes.
 	 */
-	fuse_wait_on_page_writeback(ff->fi, page->index);
+	if (ff->fi)
+		fuse_wait_on_page_writeback(ff->fi, page->index);
 
 	req = fuse_get_req(fc);
 	if (IS_ERR(req))
@@ -705,7 +717,8 @@ static int fuse_buffered_write(struct fuse_file *ff, loff_t pos, unsigned count,
 		if (count == PAGE_CACHE_SIZE)
 			SetPageUptodate(page);
 	}
-	fuse_invalidate_attr(ff->fi);
+	if (ff->fi)
+		fuse_invalidate_attr(ff->fi);
 	return err ? err : nres;
 }
 
@@ -731,8 +744,10 @@ static size_t fuse_send_write_pages(struct fuse_req *req, struct fuse_file *ff,
 	unsigned offset;
 	unsigned i;
 
-	for (i = 0; i < req->num_pages; i++)
-		fuse_wait_on_page_writeback(ff->fi, req->pages[i]->index);
+	if (ff->fi)
+		for (i = 0; i < req->num_pages; i++)
+			fuse_wait_on_page_writeback(ff->fi,
+						    req->pages[i]->index);
 
 	res = fuse_send_write(req, ff, pos, count, NULL);
 
@@ -863,7 +878,8 @@ static ssize_t fuse_perform_write(struct fuse_file *ff, struct iov_iter *ii,
 	if (res > 0)
 		fuse_write_update_size(ff, pos);
 
-	fuse_invalidate_attr(ff->fi);
+	if (ff->fi)
+		fuse_invalidate_attr(ff->fi);
 
 	return res > 0 ? res : err;
 }
@@ -1030,7 +1046,8 @@ static ssize_t fuse_direct_io(struct fuse_file *ff, const char __user *buf,
 			fuse_write_update_size(ff, pos);
 		*ppos = pos;
 	}
-	fuse_invalidate_attr(ff->fi);
+	if (ff->fi)
+		fuse_invalidate_attr(ff->fi);
 
 	return res;
 }
@@ -1266,9 +1283,11 @@ static struct vm_operations_struct fuse_file_vm_ops = {
 
 static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
 {
-	if ((vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
-		struct fuse_file *ff = file->private_data;
-		struct fuse_conn *fc = ff->fc;
+	struct fuse_file *ff = file->private_data;
+	struct fuse_conn *fc = ff->fc;
+
+	if (ff->fi &&
+	    (vma->vm_flags & VM_SHARED) && (vma->vm_flags & VM_MAYWRITE)) {
 		/*
 		 * file may be written through mmap, so chain it onto the
 		 * inodes's write_file list
@@ -1479,9 +1498,12 @@ static loff_t fuse_file_llseek(struct file *file, loff_t offset, int origin)
 	mutex_lock(&vfs_inode->i_mutex);
 	switch (origin) {
 	case SEEK_END:
-		retval = fuse_update_attributes(ff->fi, NULL, file, NULL);
-		if (retval)
-			goto exit;
+		if (ff->fi) {
+			retval = fuse_update_attributes(ff->fi, NULL, file,
+							NULL);
+			if (retval)
+				goto exit;
+		}
 		offset += i_size_read(vfs_inode);
 		break;
 	case SEEK_CUR:
fs/fuse/fuse_i.h +1 -1
index c9ffaa0..016a745 100644
@@ -501,7 +501,7 @@ static inline u64 get_node_id(struct inode *inode)
 
 static inline u64 fuse_file_nodeid(struct fuse_file *ff)
 {
-	return ff->fi->nodeid;
+	return ff->fi ? ff->fi->nodeid : 0;
 }
 
 static inline struct inode *fuse_file_vfs_inode(struct fuse_file *ff)
Signature · 1 line
1.6.0.2
author
#4 lore raw

PATCH 4/6 FUSE: update fuse_conn_init() and separate out fuse_conn_kill()

In reply to Tejun Heo

Update fuse_conn_init() such that it takes @devt and @bdev instead of @sb and skip bdi registration if device number is not available. Also, separate out fuse_conn_kill() from fuse_put_super() and export it.

These will be used to implement cuse.

---
 fs/fuse/fuse_i.h |    7 ++++++-
 fs/fuse/inode.c  |   49 +++++++++++++++++++++++++++++--------------------
 2 files changed, 35 insertions(+), 21 deletions(-)
fs/fuse/fuse_i.h +6 -1
index 016a745..d2643df 100644
@@ -679,7 +679,12 @@ struct fuse_conn *fuse_conn_get(struct fuse_conn *fc);
 /**
  * Initialize fuse_conn
  */
-int fuse_conn_init(struct fuse_conn *fc, struct super_block *sb);
+int fuse_conn_init(struct fuse_conn *fc, dev_t devt, struct block_device *bdev);
+
+/**
+ * Destroy fuse_conn
+ */
+void fuse_conn_kill(struct fuse_conn *fc);
 
 /**
  * Release reference to fuse_conn
fs/fuse/inode.c +29 -20

87 lines not on this page · raw message

Signature · 1 line
1.6.0.2
author
#5 lore raw

PATCH 5/6 FUSE: export symbols to be used by CUSE

In reply to Tejun Heo

Export the following symbols for CUSE.

fuse_conn_put()
fuse_conn_get()
fuse_send_init()
fuse_flush()
fuse_fsync()
fuse_direct_io()
fuse_direct_read()
fuse_file_ioctl_common()
fuse_file_poll()
fuse_request_alloc()
fuse_get_req()
fuse_put_request()
fuse_request_send()
fuse_abort_conn()
fuse_dev_release()
fuse_dev_operations

---
 fs/fuse/dev.c    |    9 ++++++++-
 fs/fuse/file.c   |   27 ++++++++++++++++++---------
 fs/fuse/fuse_i.h |   39 +++++++++++++++++++++++++++++++++++++++
 fs/fuse/inode.c  |    5 ++++-
 4 files changed, 69 insertions(+), 11 deletions(-)
fs/fuse/dev.c +8 -1
index ba76b68..2a17249 100644
@@ -46,6 +46,7 @@ struct fuse_req *fuse_request_alloc(void)
 		fuse_request_init(req);
 	return req;
 }
+EXPORT_SYMBOL_GPL(fuse_request_alloc);
 
 struct fuse_req *fuse_request_alloc_nofs(void)
 {
@@ -124,6 +125,7 @@ struct fuse_req *fuse_get_req(struct fuse_conn *fc)
 	atomic_dec(&fc->num_waiting);
 	return ERR_PTR(err);
 }
+EXPORT_SYMBOL_GPL(fuse_get_req);
 
 /*
  * Return request in fuse_file->reserved_req.  However that may
@@ -208,6 +210,7 @@ void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
 			fuse_request_free(req);
 	}
 }
+EXPORT_SYMBOL_GPL(fuse_put_request);
 
 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
 {
@@ -400,6 +403,7 @@ void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
 	}
 	spin_unlock(&fc->lock);
 }
+EXPORT_SYMBOL_GPL(fuse_request_send);
 
 static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
 					    struct fuse_req *req)
@@ -1105,8 +1109,9 @@ void fuse_abort_conn(struct fuse_conn *fc)
 	}
 	spin_unlock(&fc->lock);
 }
+EXPORT_SYMBOL_GPL(fuse_abort_conn);
 
-static int fuse_dev_release(struct inode *inode, struct file *file)
+int fuse_dev_release(struct inode *inode, struct file *file)
 {
 	struct fuse_conn *fc = fuse_get_conn(file);
 	if (fc) {
@@ -1120,6 +1125,7 @@ static int fuse_dev_release(struct inode *inode, struct file *file)
 
 	return 0;
 }
+EXPORT_SYMBOL_GPL(fuse_dev_release);
 
 static int fuse_dev_fasync(int fd, struct file *file, int on)
 {
@@ -1142,6 +1148,7 @@ const struct file_operations fuse_dev_operations = {
 	.release	= fuse_dev_release,
 	.fasync		= fuse_dev_fasync,
 };
+EXPORT_SYMBOL_GPL(fuse_dev_operations);
 
 static struct miscdevice fuse_miscdevice = {
 	.minor = FUSE_MINOR,
fs/fuse/file.c +18 -9
index 54ad406..700cdf9 100644
@@ -12,6 +12,7 @@
 #include <linux/slab.h>
 #include <linux/kernel.h>
 #include <linux/sched.h>
+#include <linux/module.h>
 
 static const struct file_operations fuse_direct_io_file_operations;
 
@@ -149,6 +150,7 @@ int fuse_open_common(struct fuse_conn *fc, struct fuse_inode *fi,
 
 	return err;
 }
+EXPORT_SYMBOL_GPL(fuse_open_common);
 
 void fuse_release_fill(struct fuse_file *ff, u64 nodeid, int flags, int opcode)
 {
@@ -190,6 +192,7 @@ void fuse_release_common(struct fuse_file *ff, int isdir)
 	 */
 	fuse_file_put(ff);
 }
+EXPORT_SYMBOL_GPL(fuse_release_common);
 
 static int fuse_open(struct inode *inode, struct file *file)
 {
@@ -268,7 +271,7 @@ static int fuse_wait_on_page_writeback(struct fuse_inode *fi, pgoff_t index)
 	return 0;
 }
 
-static int fuse_flush(struct file *file, fl_owner_t id)
+int fuse_flush(struct file *file, fl_owner_t id)
 {
 	struct fuse_file *ff = file->private_data;
 	struct fuse_conn *fc = ff->fc;
@@ -301,6 +304,7 @@ static int fuse_flush(struct file *file, fl_owner_t id)
 	}
 	return err;
 }
+EXPORT_SYMBOL_GPL(fuse_flush);
 
 /*
  * Wait for all pending writepages on the inode to finish.
@@ -367,12 +371,13 @@ int fuse_fsync_common(struct fuse_file *ff, int datasync, int isdir)
 	return err;
 }
 
-static int fuse_fsync(struct file *file, struct dentry *de, int datasync)
+int fuse_fsync(struct file *file, struct dentry *de, int datasync)
 {
 	struct fuse_file *ff = file->private_data;
 
 	return fuse_fsync_common(ff, datasync, 0);
 }
+EXPORT_SYMBOL_GPL(fuse_fsync);
 
 void fuse_read_fill(struct fuse_req *req, struct fuse_file *ff,
 		    loff_t pos, size_t count, int opcode)
@@ -987,8 +992,8 @@ static int fuse_get_user_pages(struct fuse_req *req, const char __user *buf,
 	return 0;
 }
 
-static ssize_t fuse_direct_io(struct fuse_file *ff, const char __user *buf,
-			      size_t count, loff_t *ppos, int write)
+ssize_t fuse_direct_io(struct fuse_file *ff, const char __user *buf,
+		       size_t count, loff_t *ppos, int write)
 {
 	struct fuse_conn *fc = ff->fc;
 	size_t nmax = write ? fc->max_write : fc->max_read;
@@ -1051,14 +1056,16 @@ static ssize_t fuse_direct_io(struct fuse_file *ff, const char __user *buf,
 
 	return res;
 }
+EXPORT_SYMBOL_GPL(fuse_direct_io);
 
-static ssize_t fuse_direct_read(struct file *file, char __user *buf,
-				     size_t count, loff_t *ppos)
+ssize_t fuse_direct_read(struct file *file, char __user *buf, size_t count,
+			 loff_t *ppos)
 {
 	struct fuse_file *ff = file->private_data;
 
 	return fuse_direct_io(ff, buf, count, ppos, 0);
 }
+EXPORT_SYMBOL_GPL(fuse_direct_read);
 
 static ssize_t fuse_direct_write(struct file *file, const char __user *buf,
 				 size_t count, loff_t *ppos)
@@ -1611,8 +1618,8 @@ static int fuse_ioctl_copy_user(struct page **pages, struct iovec *iov,
  * limits ioctl data transfers to well-formed ioctls and is the forced
  * behavior for all FUSE servers.
  */
-static long fuse_file_ioctl_common(struct fuse_file *ff, unsigned int cmd,
-				   unsigned long arg, unsigned int flags)
+long fuse_file_ioctl_common(struct fuse_file *ff, unsigned int cmd,
+			    unsigned long arg, unsigned int flags)
 {
 	struct fuse_conn *fc = ff->fc;
 	struct fuse_ioctl_in inarg = {
@@ -1783,6 +1790,7 @@ static long fuse_file_ioctl_common(struct fuse_file *ff, unsigned int cmd,
 
 	return err ? err : outarg.result;
 }
+EXPORT_SYMBOL_GPL(fuse_file_ioctl_common);
 
 static long fuse_file_ioctl(struct file *file, unsigned int cmd,
 			    unsigned long arg)
@@ -1852,7 +1860,7 @@ static void fuse_register_polled_file(struct fuse_file *ff)
 	spin_unlock(&fc->lock);
 }
 
-static unsigned fuse_file_poll(struct file *file, poll_table *wait)
+unsigned fuse_file_poll(struct file *file, poll_table *wait)
 {
 	struct fuse_file *ff = file->private_data;
 	struct fuse_conn *fc = ff->fc;
@@ -1899,6 +1907,7 @@ static unsigned fuse_file_poll(struct file *file, poll_table *wait)
 	}
 	return POLLERR;
 }
+EXPORT_SYMBOL_GPL(fuse_file_poll);
 
 /*
  * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
fs/fuse/fuse_i.h +39 -0
index d2643df..cdab92d 100644
@@ -530,6 +530,11 @@ int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
 		     struct fuse_entry_out *outarg, struct inode **inode);
 
 /**
+ * Send INIT command
+ */
+void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req);
+
+/**
  * Send FORGET command
  */
 void fuse_send_forget(struct fuse_conn *fc, struct fuse_req *req,
@@ -566,6 +571,35 @@ void fuse_release_common(struct fuse_file *ff, int isdir);
 int fuse_fsync_common(struct fuse_file *ff, int datasync, int isdir);
 
 /**
+ * Send READ or WRITE request for direct IO
+ */
+ssize_t fuse_direct_io(struct fuse_file *ff, const char __user *buf,
+		       size_t count, loff_t *ppos, int write);
+ssize_t fuse_direct_read(struct file *file, char __user *buf, size_t count,
+			 loff_t *ppos);
+
+/**
+ * Send FLUSH request
+ */
+int fuse_flush(struct file *file, fl_owner_t id);
+
+/**
+ * Send FSYNCDIR or FSYNC request
+ */
+int fuse_fsync(struct file *file, struct dentry *de, int datasync);
+
+/**
+ * Send IOCTL request
+ */
+long fuse_file_ioctl_common(struct fuse_file *ff, unsigned int cmd,
+			    unsigned long arg, unsigned int flags);
+
+/**
+ * Send POLL request
+ */
+unsigned fuse_file_poll(struct file *file, poll_table *wait);
+
+/**
  * Notify poll wakeup
  */
 int fuse_notify_poll_wakeup(struct fuse_conn *fc,
@@ -608,6 +642,11 @@ void fuse_truncate(struct address_space *mapping, loff_t offset);
 int fuse_dev_init(void);
 
 /**
+ * Release the client device
+ */
+int fuse_dev_release(struct inode *inode, struct file *file);
+
+/**
  * Cleanup the client device
  */
 void fuse_dev_cleanup(void);
fs/fuse/inode.c +4 -1
index 699b228..a01f52c 100644
@@ -545,12 +545,14 @@ void fuse_conn_put(struct fuse_conn *fc)
 		fc->release(fc);
 	}
 }
+EXPORT_SYMBOL_GPL(fuse_conn_put);
 
 struct fuse_conn *fuse_conn_get(struct fuse_conn *fc)
 {
 	atomic_inc(&fc->count);
 	return fc;
 }
+EXPORT_SYMBOL_GPL(fuse_conn_get);
 
 static struct inode *fuse_get_root_inode(struct super_block *sb, unsigned mode)
 {
@@ -774,7 +776,7 @@ static void process_init_reply(struct fuse_conn *fc, struct fuse_req *req)
 	wake_up_all(&fc->blocked_waitq);
 }
 
-static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
+void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
 {
 	struct fuse_init_in *arg = &req->misc.init_in;
 
@@ -797,6 +799,7 @@ static void fuse_send_init(struct fuse_conn *fc, struct fuse_req *req)
 	req->end = process_init_reply;
 	fuse_request_send_background(fc, req);
 }
+EXPORT_SYMBOL_GPL(fuse_send_init);
 
 static void fuse_free_conn(struct fuse_conn *fc)
 {
Signature · 1 line
1.6.0.2
author
#6 lore raw

PATCH 6/6 CUSE: implement CUSE - Character device in Userspace

In reply to Tejun Heo

CUSE enables implementing character devices in userspace. With recent additions of ioctl and poll support, FUSE already has most of what's necessary to implement character devices. All CUSE has to do is bonding all those components - FUSE, chardev and the driver model - nicely.

When client opens /dev/cuse, kernel starts conversation with CUSE_INIT. The client tells CUSE which device it wants to create. As the previous patch made fuse_file usable without associated fuse_inode, CUSE doesn't create super block or inodes. It attaches fuse_file to cdev file->private_data during open and set ff->fi to NULL. The rest of the operation is almost identical to FUSE direct IO case.

Each CUSE device has a corresponding directory /sys/class/cuse/DEVNAME (which is symlink to /sys/devices/virtual/class/DEVNAME if SYSFS_DEPRECATED is turned off) which hosts "waiting" and "abort" among other things. Those two files have the same meaning as the FUSE control files.

The only notable lacking feature compared to in-kernel implementation is mmap support.

---
 fs/Kconfig            |   10 +
 fs/fuse/Makefile      |    1 +
 fs/fuse/cuse.c        |  675 +++++++++++++++++++++++++++++++++++++++++++++++++
 fs/fuse/file.c        |    4 +-
 include/linux/cuse.h  |   49 ++++
 include/linux/fuse.h  |    2 +
 include/linux/magic.h |    5 +-
 7 files changed, 743 insertions(+), 3 deletions(-)
 create mode 100644 fs/fuse/cuse.c
 create mode 100644 include/linux/cuse.h
fs/Kconfig +10 -0
index 9f7270f..525da2e 100644
@@ -62,6 +62,16 @@ source "fs/autofs/Kconfig"
 source "fs/autofs4/Kconfig"
 source "fs/fuse/Kconfig"
 
+config CUSE
+	tristate "Character device in Userpace support"
+	depends on FUSE_FS
+	help
+	  This FUSE extension allows character devices to be
+	  implemented in userspace.
+
+	  If you want to develop or use userspace character device
+	  based on CUSE, answer Y or M.
+
 config GENERIC_ACL
 	bool
 	select FS_POSIX_ACL
fs/fuse/Makefile +1 -0
index 7243706..e95eeb4 100644
@@ -3,5 +3,6 @@
 #
 
 obj-$(CONFIG_FUSE_FS) += fuse.o
+obj-$(CONFIG_CUSE) += cuse.o
 
 fuse-objs := dev.o dir.o file.o inode.o control.o
fs/fuse/cuse.c new file +675 -0

675 lines not on this page · raw message

fs/fuse/file.c +3 -1
index 700cdf9..7492577 100644
@@ -14,6 +14,7 @@
 #include <linux/sched.h>
 #include <linux/module.h>
 
+static const struct file_operations fuse_file_operations;
 static const struct file_operations fuse_direct_io_file_operations;
 
 static int fuse_send_open(struct fuse_file *ff, int isdir,
@@ -110,7 +111,8 @@ void fuse_finish_open(struct fuse_file *ff, struct fuse_open_out *outarg)
 {
 	struct inode *vfs_inode = fuse_file_vfs_inode(ff);
 
-	if (outarg->open_flags & FOPEN_DIRECT_IO)
+	if (outarg->open_flags & FOPEN_DIRECT_IO &&
+	    ff->file->f_op == &fuse_file_operations)
 		ff->file->f_op = &fuse_direct_io_file_operations;
 	if (!(outarg->open_flags & FOPEN_KEEP_CACHE))
 		invalidate_inode_pages2(vfs_inode->i_mapping);
include/linux/cuse.h new file +49 -0

49 lines not on this page · raw message

include/linux/fuse.h +2 -0
index 162e5de..cc51548 100644
@@ -210,6 +210,8 @@ enum fuse_opcode {
 	FUSE_DESTROY       = 38,
 	FUSE_IOCTL         = 39,
 	FUSE_POLL          = 40,
+
+	CUSE_BASE          = 4096,
 };
 
 enum fuse_notify_code {
include/linux/magic.h +3 -2
index 5b4e28b..4596c9e 100644
@@ -3,10 +3,11 @@
 
 #define ADFS_SUPER_MAGIC	0xadf5
 #define AFFS_SUPER_MAGIC	0xadff
-#define AFS_SUPER_MAGIC                0x5346414F
+#define AFS_SUPER_MAGIC		0x5346414F
 #define AUTOFS_SUPER_MAGIC	0x0187
 #define CODA_SUPER_MAGIC	0x73757245
-#define DEBUGFS_MAGIC          0x64626720
+#define CUSE_SUPER_MAGIC	0x43555345
+#define DEBUGFS_MAGIC		0x64626720
 #define SYSFS_MAGIC		0x62656572
 #define SECURITYFS_MAGIC	0x73636673
 #define TMPFS_MAGIC		0x01021994
Signature · 1 line
1.6.0.2
maintainer
#8 lore raw
In reply to Tejun Heo

Hello Tejun,

Sorry for the delay, I was busy reimplementing some of the stuff in your patchset. The current state is here:

git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git cuse-test

http://git.kernel.org/?p=linux/kernel/git/mszeredi/fuse.git;a=shortlog;h=cuse-test

What I changed (IIRC):

- Don't store pointer to struct inode, struct fuse_inode, and struct file in struct fuse_file. Instead store nodeid in struct fuse_file, no worries about object lifetimes this way.

- Don't implement fsync and flush in CUSE, they don't really make sense on character devices. At least I can't think of any use for them.

- release() in CUSE can be synchronous. Asynchronous release causes many headaches in fuse, but is necessary due to the possible DoS that an unprivileged filesystem could do with an unrelated process. Since CUSE servers are always privileged, this should not be an issue.

- Other cleanups.

I still think that the INIT sequence in CUSE is way too complicated and that the kernel thread is unnecessary. How about doing the init with a single CUSE_INIT message which embeds the fuse_init_in and fuse_init_out structures. I also don't see the purpose of the CUSE_INIT_DONE message. Isn't it enough not to send any other messages while the initialization is not complete? This is what fuse does.

I'm currently testing with ossp and some things do work, but I'm also having issues with some apps. But that's a topic for another mail...

Thanks,
Miklos

author
#9 lore raw

Hello,

Quoted 7 lines · Miklos Szeredi

Hello Tejun,

Sorry for the delay, I was busy reimplementing some of the stuff in your patchset. The current state is here:

git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git cuse-test

Quoted 7 lines

http://git.kernel.org/?p=linux/kernel/git/mszeredi/fuse.git;a=shortlog;h=cuse-test

What I changed (IIRC):

- Don't store pointer to struct inode, struct fuse_inode, and struct file in struct fuse_file. Instead store nodeid in struct fuse_file, no worries about object lifetimes this way.

Hmm... there really isn't any object lifetime issue with or without the pointers as both fuse_file and fuse_inode follow VFS lifetime rules. I tried several different approaches including adding nodeid but what pushed me toward adding the pointers was that the parameter passing and which uses/depends on what becomes a bit too complex with other approaches. e.g. How do you discern between needing vfs inode and fuse inode?

Quoted 8 lines

- Don't implement fsync and flush in CUSE, they don't really make sense on character devices. At least I can't think of any use for them.
- release() in CUSE can be synchronous. Asynchronous release causes many headaches in fuse, but is necessary due to the possible DoS that an unprivileged filesystem could do with an unrelated process. Since CUSE servers are always privileged, this should not be an issue.

Cool.

Quoted 6 lines

- Other cleanups.

I still think that the INIT sequence in CUSE is way too complicated and that the kernel thread is unnecessary. How about doing the init with a single CUSE_INIT message which embeds the fuse_init_in and fuse_init_out structures.

I don't see why the kernel thread is such a big problem. It's just a kernel thread. We can surely fold FUSE_INIT inside FUSE_INIT tho but one way or the other doesn't make whole lot of difference.

Quoted 3 lines

I also don't see the purpose of the CUSE_INIT_DONE message. Isn't it enough not to send any other messages while the initialization is not complete? This is what fuse does.

The difference between FUSE and CUSE in this regard is that the synchornous part of initialization is much less for CUSE. ie. For FUSE, after the mount is complete, all accesses go to FUSE, so it's all good. However, for CUSE, the device nodes are setup asynchronously, so there's no such inherent synchronization point as mount. This becomes a problem when CUSE is started from modprobe to emulate devices on demand because modprobe should not return before the device nodes are setup.

Thanks.

Signature · 1 line
tejun
maintainer
#10 lore raw
Quoted 11 lines · Tejun Heo
Quoted 5 lines · Miklos Szeredi

What I changed (IIRC):

- Don't store pointer to struct inode, struct fuse_inode, and struct file in struct fuse_file. Instead store nodeid in struct fuse_file, no worries about object lifetimes this way.

Hmm... there really isn't any object lifetime issue with or without the pointers as both fuse_file and fuse_inode follow VFS lifetime rules.

I did find some problems with lifetimes (it was I think struct file still being used in release code, but it may have been something different).

Quoted 5 lines

I tried several different approaches including adding nodeid
but what pushed me toward adding the pointers was that the parameter passing and which uses/depends on what becomes a bit too complex with other approaches. e.g. How do you discern between needing vfs inode and fuse inode?

VFS inode should never be needed by the CUSE codepaths. Well, maybe for setting access/modification times, but in any case those need not be shared with fuse code.

Quoted 14 lines
Quoted 9 lines

- Don't implement fsync and flush in CUSE, they don't really make sense on character devices. At least I can't think of any use for them.
- release() in CUSE can be synchronous. Asynchronous release causes many headaches in fuse, but is necessary due to the possible DoS that an unprivileged filesystem could do with an unrelated process. Since CUSE servers are always privileged, this should not be an issue.

Cool.

Quoted 2 lines

- Other cleanups.

Quoted 7 lines
Quoted 4 lines

I still think that the INIT sequence in CUSE is way too complicated and that the kernel thread is unnecessary. How about doing the init with a single CUSE_INIT message which embeds the fuse_init_in and fuse_init_out structures.

I don't see why the kernel thread is such a big problem. It's just a kernel thread.

I think it's an unnecessary complication. Threads are useful if something needs to be _done_, but in this case nothing is done, just waiting for the reply. One example why I find it hackish is that you are returning error values from the thread, as if it was a synchronous thing, but those errors are obviously lost.

I don't see any problems with doing the device initialization in the context of the replying process (which will usually be the same as the one opening the device anyway).

Quoted 2 lines

We can surely fold FUSE_INIT inside FUSE_INIT tho but
one way or the other doesn't make whole lot of difference.

I just think it would be cleaner, both code-wise and protocol-wise. I can do the above cleanups if you have no objection.

Quoted 4 lines
Quoted 3 lines

I also don't see the purpose of the CUSE_INIT_DONE message. Isn't it enough not to send any other messages while the initialization is not complete? This is what fuse does.

Quoted 8 lines

The difference between FUSE and CUSE in this regard is that the synchornous part of initialization is much less for CUSE. ie. For FUSE, after the mount is complete, all accesses go to FUSE, so it's all good. However, for CUSE, the device nodes are setup asynchronously, so there's no such inherent synchronization point as mount. This becomes a problem when CUSE is started from modprobe to emulate devices on demand because modprobe should not return before the device nodes are setup.

Hmm, OK.

Thanks,
Miklos

author
#11 lore raw

Hello,

Quoted 3 lines · Miklos Szeredi

VFS inode should never be needed by the CUSE codepaths. Well, maybe for setting access/modification times, but in any case those need not be shared with fuse code.

One more usage: locking.

Quoted 4 lines
Quoted 4 lines
Quoted 4 lines

I still think that the INIT sequence in CUSE is way too complicated and that the kernel thread is unnecessary. How about doing the init with a single CUSE_INIT message which embeds the fuse_init_in and fuse_init_out structures.

Quoted 8 lines
Quoted 2 lines

I don't see why the kernel thread is such a big problem. It's just a kernel thread.

I think it's an unnecessary complication. Threads are useful if something needs to be _done_, but in this case nothing is done, just waiting for the reply. One example why I find it hackish is that you are returning error values from the thread, as if it was a synchronous thing, but those errors are obviously lost.

That doesn't have anything to do with whether it's done in kernel thread or not. If that bothers you, just make it return 0 with comment /* return value ignored */.

Quoted 3 lines

I don't see any problems with doing the device initialization in the context of the replying process (which will usually be the same as the one opening the device anyway).

I don't know. If it's simple, sure why not. If it makes things even marginally more complex (state saving and so on), why go that way?

In my first job, I used to work on a quite complex state machine based server which had to do almost everything in multiple functions. In the end, it was extremely ugly and ultimately the wrong kind of optimization, so I much prefer having a stack whenever it makes things simpler and the overhead isn't an issue. What are the actual benefits of doing things in several different functions in this case? Performance definitely is not a concern here.

Quoted 5 lines
Quoted 2 lines

We can surely fold FUSE_INIT inside FUSE_INIT tho but
one way or the other doesn't make whole lot of difference.

I just think it would be cleaner, both code-wise and protocol-wise. I can do the above cleanups if you have no objection.

Sure, if you think things would be better that way. Please go ahead.

Thanks.

Signature · 1 line
tejun
maintainer
#12 lore raw

Hi Tejun,

Quoted 6 lines · Tejun Heo
Quoted 3 lines · Miklos Szeredi

VFS inode should never be needed by the CUSE codepaths. Well, maybe for setting access/modification times, but in any case those need not be shared with fuse code.

One more usage: locking.

You mean inode->i_mutex locking? It doesn't look to me as if cuse would need to do any of that.

Quoted 6 lines
Quoted 3 lines

I don't see any problems with doing the device initialization in the context of the replying process (which will usually be the same as the one opening the device anyway).

I don't know. If it's simple, sure why not. If it makes things even marginally more complex (state saving and so on), why go that way?

OK, I folded FUSE_INIT into CUSE_INIT, and moved the CUSE_INIT reply processing into the ->end() callback. This also allows us to remove the CUSE_INIT_DONE message, since now the write(2) on the cuse device will only return when the device initialization is complete.

Overall it results in some 100 lines less code (well, some of that is because I folded <linux/cuse.h> into <linux/fuse.h>). But the simpler initialization sequence is, I think, definitely worth it.

The patches are pushed out to the 'cuse' and 'for-next' branches of

git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git

I'll post the updated libfuse patch as well. Please let me know if you find anything wrong with the current patches.

Thanks,
Miklos

author
#13 lore raw

Hello,

Quoted 7 lines · Miklos Szeredi
Quoted 4 lines
Quoted 3 lines

VFS inode should never be needed by the CUSE codepaths. Well, maybe for setting access/modification times, but in any case those need not be shared with fuse code.

One more usage: locking.

You mean inode->i_mutex locking? It doesn't look to me as if cuse would need to do any of that.

Well, there are a few places in file.c and dir.c IIRC and I think there's one path which CUSE uses (or used, it might have gone away with your changes). I was concerned with the different assumptions FUSE code paths are under depending on whether the code is used by CUSE or that, which is not at all obvious from looking at the FUSE code only, so the large update to put FUSE file handling paths under the same assumptions which are safe for CUSE. I think it's too subtle but it's also true that the paths used by CUSE are quite limited, so as long as you can keep a close eye on it I guess it would be okay.

Quoted 15 lines
Quoted 5 lines
Quoted 3 lines

I don't see any problems with doing the device initialization in the context of the replying process (which will usually be the same as the one opening the device anyway).

I don't know. If it's simple, sure why not. If it makes things even marginally more complex (state saving and so on), why go that way?

OK, I folded FUSE_INIT into CUSE_INIT, and moved the CUSE_INIT reply processing into the ->end() callback. This also allows us to remove the CUSE_INIT_DONE message, since now the write(2) on the cuse device will only return when the device initialization is complete.

Overall it results in some 100 lines less code (well, some of that is because I folded <linux/cuse.h> into <linux/fuse.h>). But the simpler initialization sequence is, I think, definitely worth it.

Quoted 6 lines

The patches are pushed out to the 'cuse' and 'for-next' branches of

git://git.kernel.org/pub/scm/linux/kernel/git/mszeredi/fuse.git

I'll post the updated libfuse patch as well. Please let me know if you find anything wrong with the current patches.

Looks good to me. Much prettier with two inits merged.

Thanks.

Signature · 1 line
tejun
author
#14 lore raw
Quoted 4 lines · Tejun Heo
Quoted 2 lines

I'll post the updated libfuse patch as well. Please let me know if you find anything wrong with the current patches.

Looks good to me. Much prettier with two inits merged.

Oh... what happened to CUSE_INIT_DONE?

Signature · 1 line
tejun
maintainer
#15 lore raw
Quoted 7 lines · Tejun Heo
Quoted 4 lines · Tejun Heo
Quoted 2 lines

I'll post the updated libfuse patch as well. Please let me know if you find anything wrong with the current patches.

Looks good to me. Much prettier with two inits merged.

Oh... what happened to CUSE_INIT_DONE?

By moving the CUSE_INIT reply processing into the ->end() callback, the device initialization becomes synchronous with the process writing the message into the cuse device.

This means, that the process can simply wait for the write() on the device to complete before proceeding.

I've updated the libfuse patch so it calls ->init_done() after sending the CUSE_INIT reply.

Thanks,
Miklos

author
#16 lore raw
Quoted 11 lines · Miklos Szeredi
Quoted 5 lines · Tejun Heo
Quoted 3 lines · Tejun Heo
Quoted 2 lines

I'll post the updated libfuse patch as well. Please let me know if you find anything wrong with the current patches.

Looks good to me. Much prettier with two inits merged.

Oh... what happened to CUSE_INIT_DONE?

By moving the CUSE_INIT reply processing into the ->end() callback, the device initialization becomes synchronous with the process writing the message into the cuse device.

Quoted 5 lines

This means, that the process can simply wait for the write() on the device to complete before proceeding.

I've updated the libfuse patch so it calls ->init_done() after sending the CUSE_INIT reply.

Ah... cool. Thanks.

Signature · 1 line
tejun