static int fuse_dev_open(struct inode *inode, struct file *file)
{
- /*
- * The fuse device's file's private_data is used to hold
- * the fuse_conn(ection) when it is mounted, and is used to
- * keep track of whether the file has been mounted already.
- */
- file->private_data = NULL;
+ struct fuse_dev *fud = fuse_dev_alloc();
+
+ if (!fud)
+ return -ENOMEM;
+
+ file->private_data = fud;
return 0;
}
struct fuse_dev *fuse_get_dev(struct file *file)
{
- struct fuse_dev *fud = __fuse_get_dev(file);
+ struct fuse_dev *fud = fuse_file_to_fud(file);
int err;
- if (likely(fud))
- return fud;
-
- err = wait_event_interruptible(fuse_dev_waitq,
- READ_ONCE(file->private_data) != FUSE_DEV_SYNC_INIT);
+ err = wait_event_interruptible(fuse_dev_waitq, fuse_dev_fc_get(fud) != NULL);
if (err)
return ERR_PTR(err);
- fud = __fuse_get_dev(file);
- if (!fud)
- return ERR_PTR(-EPERM);
-
return fud;
}
int fuse_dev_release(struct inode *inode, struct file *file)
{
- struct fuse_dev *fud = __fuse_get_dev(file);
+ struct fuse_dev *fud = fuse_file_to_fud(file);
+ struct fuse_conn *fc = fuse_dev_fc_get(fud);
- if (fud) {
- struct fuse_conn *fc = fud->fc;
+ if (fc) {
struct fuse_pqueue *fpq = &fud->pq;
LIST_HEAD(to_end);
unsigned int i;
WARN_ON(fc->iq.fasync != NULL);
fuse_abort_conn(fc);
}
- fuse_dev_free(fud);
}
+ fuse_dev_free(fud);
return 0;
}
EXPORT_SYMBOL_GPL(fuse_dev_release);
static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
{
- struct fuse_dev *fud;
+ struct fuse_dev *new_fud = fuse_file_to_fud(new);
- if (__fuse_get_dev(new))
+ if (fuse_dev_fc_get(new_fud))
return -EINVAL;
- fud = fuse_dev_alloc_install(fc);
- if (!fud)
- return -ENOMEM;
-
- new->private_data = fud;
+ fuse_dev_install(new_fud, fc);
atomic_inc(&fc->dev_count);
return 0;
static long fuse_dev_ioctl_sync_init(struct file *file)
{
int err = -EINVAL;
+ struct fuse_dev *fud = fuse_file_to_fud(file);
mutex_lock(&fuse_mutex);
- if (!__fuse_get_dev(file)) {
- WRITE_ONCE(file->private_data, FUSE_DEV_SYNC_INIT);
+ if (!fuse_dev_fc_get(fud)) {
+ fud->sync_init = true;
err = 0;
}
mutex_unlock(&fuse_mutex);
} ring;
};
-#define FUSE_DEV_SYNC_INIT ((struct fuse_dev *) 1)
-#define FUSE_DEV_PTR_MASK (~1UL)
+/*
+ * Lockless access is OK, because fud->fc is set once during mount and is valid
+ * until the file is released.
+ */
+static inline struct fuse_conn *fuse_dev_fc_get(struct fuse_dev *fud)
+{
+ /* Pairs with smp_store_release() in fuse_dev_fc_set() */
+ return smp_load_acquire(&fud->fc);
+}
+
+static inline void fuse_dev_fc_set(struct fuse_dev *fud, struct fuse_conn *fc)
+{
+ /* Pairs with smp_load_acquire() in fuse_dev_fc_get() */
+ smp_store_release(&fud->fc, fc);
+}
+
+static inline struct fuse_dev *fuse_file_to_fud(struct file *file)
+{
+ return file->private_data;
+}
static inline struct fuse_dev *__fuse_get_dev(struct file *file)
{
- /*
- * Lockless access is OK, because file->private data is set
- * once during mount and is valid until the file is released.
- */
- struct fuse_dev *fud = READ_ONCE(file->private_data);
+ struct fuse_dev *fud = fuse_file_to_fud(file);
+
+ if (!fuse_dev_fc_get(fud))
+ return NULL;
- return (typeof(fud)) ((unsigned long) fud & FUSE_DEV_PTR_MASK);
+ return fud;
}
struct fuse_dev *fuse_get_dev(struct file *file);
* Fuse device instance
*/
struct fuse_dev {
+ /** Issue FUSE_INIT synchronously */
+ bool sync_init;
+
/** Fuse connection for this device */
struct fuse_conn *fc;
/* DAX device, may be NULL */
struct dax_device *dax_dev;
-
- /* fuse_dev pointer to fill in, should contain NULL on entry */
- void **fudptr;
};
struct fuse_sync_bucket {
void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
{
- fud->fc = fuse_conn_get(fc);
+ fuse_dev_fc_set(fud, fuse_conn_get(fc));
spin_lock(&fc->lock);
list_add_tail(&fud->entry, &fc->devices);
spin_unlock(&fc->lock);
void fuse_dev_free(struct fuse_dev *fud)
{
- struct fuse_conn *fc = fud->fc;
+ struct fuse_conn *fc = fuse_dev_fc_get(fud);
if (fc) {
spin_lock(&fc->lock);
int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
{
- struct fuse_dev *fud = NULL;
+ struct fuse_dev *fud = ctx->file ? fuse_file_to_fud(ctx->file) : NULL;
struct fuse_mount *fm = get_fuse_mount_super(sb);
struct fuse_conn *fc = fm->fc;
struct inode *root;
goto err;
}
- if (ctx->fudptr) {
- err = -ENOMEM;
- fud = fuse_dev_alloc_install(fc);
- if (!fud)
- goto err_free_dax;
- }
-
fc->dev = sb->s_dev;
fm->sb = sb;
err = fuse_bdi_init(fc, sb);
if (err)
- goto err_dev_free;
+ goto err_free_dax;
/* Handle umasking inside the fuse code */
if (sb->s_flags & SB_POSIXACL)
set_default_d_op(sb, &fuse_dentry_operations);
root_dentry = d_make_root(root);
if (!root_dentry)
- goto err_dev_free;
+ goto err_free_dax;
mutex_lock(&fuse_mutex);
err = -EINVAL;
- if (ctx->fudptr && *ctx->fudptr) {
- if (*ctx->fudptr == FUSE_DEV_SYNC_INIT)
- fc->sync_init = 1;
- else
+ if (fud) {
+ if (fuse_dev_fc_get(fud))
goto err_unlock;
+ if (fud->sync_init)
+ fc->sync_init = 1;
}
err = fuse_ctl_add_conn(fc);
list_add_tail(&fc->entry, &fuse_conn_list);
sb->s_root = root_dentry;
- if (ctx->fudptr) {
- *ctx->fudptr = fud;
+ if (fud) {
+ fuse_dev_install(fud, fc);
wake_up_all(&fuse_dev_waitq);
}
mutex_unlock(&fuse_mutex);
err_unlock:
mutex_unlock(&fuse_mutex);
dput(root_dentry);
- err_dev_free:
- if (fud)
- fuse_dev_free(fud);
err_free_dax:
if (IS_ENABLED(CONFIG_FUSE_DAX))
fuse_dax_conn_free(fc);
if ((ctx->file->f_op != &fuse_dev_operations) ||
(ctx->file->f_cred->user_ns != sb->s_user_ns))
return -EINVAL;
- ctx->fudptr = &ctx->file->private_data;
err = fuse_fill_super_common(sb, ctx);
if (err)
return err;
- /* file->private_data shall be visible on all CPUs after this */
- smp_mb();
fm = get_fuse_mount_super(sb);
goto err_free_fuse_devs;
}
- /* virtiofs allocates and installs its own fuse devices */
- ctx->fudptr = NULL;
if (ctx->dax_mode != FUSE_DAX_NEVER) {
if (ctx->dax_mode == FUSE_DAX_ALWAYS && !fs->dax_dev) {
err = -EINVAL;