Skip to content

Commit a8dd5f1

Browse files
author
Miklos Szeredi
committed
fuse: create fuse_dev on /dev/fuse open instead of mount
Allocate struct fuse_dev when opening the device. This means that unlike before, ->private_data is always set to a valid pointer. The use of USE_DEV_SYNC_INIT magic pointer for the private_data is now replaced with a simple bool sync_init member. If sync INIT is not set, I/O on the device returns error before mount. Keep this behavior by checking for the ->fc member. If fud->fc is set, the mount has succeeded. Testing this used READ_ONCE(file->private_data) and smp_mb() to try and provide the necessary semantics. Switch this to smp_store_release() and smp_load_acquire(). Setting fud->fc is protected by fuse_mutex, this is unchanged. Will need this later so the /dev/fuse open file reference is not held during FSCONFIG_CMD_CREATE. Signed-off-by: Miklos Szeredi <mszeredi@redhat.com> Reviewed-by: "Darrick J. Wong" <djwong@kernel.org>
1 parent e45f591 commit a8dd5f1

5 files changed

Lines changed: 57 additions & 66 deletions

File tree

fs/fuse/dev.c

Lines changed: 18 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1548,32 +1548,24 @@ static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
15481548

15491549
static int fuse_dev_open(struct inode *inode, struct file *file)
15501550
{
1551-
/*
1552-
* The fuse device's file's private_data is used to hold
1553-
* the fuse_conn(ection) when it is mounted, and is used to
1554-
* keep track of whether the file has been mounted already.
1555-
*/
1556-
file->private_data = NULL;
1551+
struct fuse_dev *fud = fuse_dev_alloc();
1552+
1553+
if (!fud)
1554+
return -ENOMEM;
1555+
1556+
file->private_data = fud;
15571557
return 0;
15581558
}
15591559

15601560
struct fuse_dev *fuse_get_dev(struct file *file)
15611561
{
1562-
struct fuse_dev *fud = __fuse_get_dev(file);
1562+
struct fuse_dev *fud = fuse_file_to_fud(file);
15631563
int err;
15641564

1565-
if (likely(fud))
1566-
return fud;
1567-
1568-
err = wait_event_interruptible(fuse_dev_waitq,
1569-
READ_ONCE(file->private_data) != FUSE_DEV_SYNC_INIT);
1565+
err = wait_event_interruptible(fuse_dev_waitq, fuse_dev_fc_get(fud) != NULL);
15701566
if (err)
15711567
return ERR_PTR(err);
15721568

1573-
fud = __fuse_get_dev(file);
1574-
if (!fud)
1575-
return ERR_PTR(-EPERM);
1576-
15771569
return fud;
15781570
}
15791571

@@ -2547,10 +2539,10 @@ void fuse_wait_aborted(struct fuse_conn *fc)
25472539

25482540
int fuse_dev_release(struct inode *inode, struct file *file)
25492541
{
2550-
struct fuse_dev *fud = __fuse_get_dev(file);
2542+
struct fuse_dev *fud = fuse_file_to_fud(file);
2543+
struct fuse_conn *fc = fuse_dev_fc_get(fud);
25512544

2552-
if (fud) {
2553-
struct fuse_conn *fc = fud->fc;
2545+
if (fc) {
25542546
struct fuse_pqueue *fpq = &fud->pq;
25552547
LIST_HEAD(to_end);
25562548
unsigned int i;
@@ -2568,8 +2560,8 @@ int fuse_dev_release(struct inode *inode, struct file *file)
25682560
WARN_ON(fc->iq.fasync != NULL);
25692561
fuse_abort_conn(fc);
25702562
}
2571-
fuse_dev_free(fud);
25722563
}
2564+
fuse_dev_free(fud);
25732565
return 0;
25742566
}
25752567
EXPORT_SYMBOL_GPL(fuse_dev_release);
@@ -2587,16 +2579,12 @@ static int fuse_dev_fasync(int fd, struct file *file, int on)
25872579

25882580
static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
25892581
{
2590-
struct fuse_dev *fud;
2582+
struct fuse_dev *new_fud = fuse_file_to_fud(new);
25912583

2592-
if (__fuse_get_dev(new))
2584+
if (fuse_dev_fc_get(new_fud))
25932585
return -EINVAL;
25942586

2595-
fud = fuse_dev_alloc_install(fc);
2596-
if (!fud)
2597-
return -ENOMEM;
2598-
2599-
new->private_data = fud;
2587+
fuse_dev_install(new_fud, fc);
26002588
atomic_inc(&fc->dev_count);
26012589

26022590
return 0;
@@ -2667,10 +2655,11 @@ static long fuse_dev_ioctl_backing_close(struct file *file, __u32 __user *argp)
26672655
static long fuse_dev_ioctl_sync_init(struct file *file)
26682656
{
26692657
int err = -EINVAL;
2658+
struct fuse_dev *fud = fuse_file_to_fud(file);
26702659

26712660
mutex_lock(&fuse_mutex);
2672-
if (!__fuse_get_dev(file)) {
2673-
WRITE_ONCE(file->private_data, FUSE_DEV_SYNC_INIT);
2661+
if (!fuse_dev_fc_get(fud)) {
2662+
fud->sync_init = true;
26742663
err = 0;
26752664
}
26762665
mutex_unlock(&fuse_mutex);

fs/fuse/fuse_dev_i.h

Lines changed: 25 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -39,18 +39,35 @@ struct fuse_copy_state {
3939
} ring;
4040
};
4141

42-
#define FUSE_DEV_SYNC_INIT ((struct fuse_dev *) 1)
43-
#define FUSE_DEV_PTR_MASK (~1UL)
42+
/*
43+
* Lockless access is OK, because fud->fc is set once during mount and is valid
44+
* until the file is released.
45+
*/
46+
static inline struct fuse_conn *fuse_dev_fc_get(struct fuse_dev *fud)
47+
{
48+
/* Pairs with smp_store_release() in fuse_dev_fc_set() */
49+
return smp_load_acquire(&fud->fc);
50+
}
51+
52+
static inline void fuse_dev_fc_set(struct fuse_dev *fud, struct fuse_conn *fc)
53+
{
54+
/* Pairs with smp_load_acquire() in fuse_dev_fc_get() */
55+
smp_store_release(&fud->fc, fc);
56+
}
57+
58+
static inline struct fuse_dev *fuse_file_to_fud(struct file *file)
59+
{
60+
return file->private_data;
61+
}
4462

4563
static inline struct fuse_dev *__fuse_get_dev(struct file *file)
4664
{
47-
/*
48-
* Lockless access is OK, because file->private data is set
49-
* once during mount and is valid until the file is released.
50-
*/
51-
struct fuse_dev *fud = READ_ONCE(file->private_data);
65+
struct fuse_dev *fud = fuse_file_to_fud(file);
66+
67+
if (!fuse_dev_fc_get(fud))
68+
return NULL;
5269

53-
return (typeof(fud)) ((unsigned long) fud & FUSE_DEV_PTR_MASK);
70+
return fud;
5471
}
5572

5673
struct fuse_dev *fuse_get_dev(struct file *file);

fs/fuse/fuse_i.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -577,6 +577,9 @@ struct fuse_pqueue {
577577
* Fuse device instance
578578
*/
579579
struct fuse_dev {
580+
/** Issue FUSE_INIT synchronously */
581+
bool sync_init;
582+
580583
/** Fuse connection for this device */
581584
struct fuse_conn *fc;
582585

@@ -623,9 +626,6 @@ struct fuse_fs_context {
623626

624627
/* DAX device, may be NULL */
625628
struct dax_device *dax_dev;
626-
627-
/* fuse_dev pointer to fill in, should contain NULL on entry */
628-
void **fudptr;
629629
};
630630

631631
struct fuse_sync_bucket {

fs/fuse/inode.c

Lines changed: 11 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1641,7 +1641,7 @@ EXPORT_SYMBOL_GPL(fuse_dev_alloc);
16411641

16421642
void fuse_dev_install(struct fuse_dev *fud, struct fuse_conn *fc)
16431643
{
1644-
fud->fc = fuse_conn_get(fc);
1644+
fuse_dev_fc_set(fud, fuse_conn_get(fc));
16451645
spin_lock(&fc->lock);
16461646
list_add_tail(&fud->entry, &fc->devices);
16471647
spin_unlock(&fc->lock);
@@ -1663,7 +1663,7 @@ EXPORT_SYMBOL_GPL(fuse_dev_alloc_install);
16631663

16641664
void fuse_dev_free(struct fuse_dev *fud)
16651665
{
1666-
struct fuse_conn *fc = fud->fc;
1666+
struct fuse_conn *fc = fuse_dev_fc_get(fud);
16671667

16681668
if (fc) {
16691669
spin_lock(&fc->lock);
@@ -1826,7 +1826,7 @@ EXPORT_SYMBOL_GPL(fuse_init_fs_context_submount);
18261826

18271827
int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
18281828
{
1829-
struct fuse_dev *fud = NULL;
1829+
struct fuse_dev *fud = ctx->file ? fuse_file_to_fud(ctx->file) : NULL;
18301830
struct fuse_mount *fm = get_fuse_mount_super(sb);
18311831
struct fuse_conn *fc = fm->fc;
18321832
struct inode *root;
@@ -1860,18 +1860,11 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
18601860
goto err;
18611861
}
18621862

1863-
if (ctx->fudptr) {
1864-
err = -ENOMEM;
1865-
fud = fuse_dev_alloc_install(fc);
1866-
if (!fud)
1867-
goto err_free_dax;
1868-
}
1869-
18701863
fc->dev = sb->s_dev;
18711864
fm->sb = sb;
18721865
err = fuse_bdi_init(fc, sb);
18731866
if (err)
1874-
goto err_dev_free;
1867+
goto err_free_dax;
18751868

18761869
/* Handle umasking inside the fuse code */
18771870
if (sb->s_flags & SB_POSIXACL)
@@ -1893,15 +1886,15 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
18931886
set_default_d_op(sb, &fuse_dentry_operations);
18941887
root_dentry = d_make_root(root);
18951888
if (!root_dentry)
1896-
goto err_dev_free;
1889+
goto err_free_dax;
18971890

18981891
mutex_lock(&fuse_mutex);
18991892
err = -EINVAL;
1900-
if (ctx->fudptr && *ctx->fudptr) {
1901-
if (*ctx->fudptr == FUSE_DEV_SYNC_INIT)
1902-
fc->sync_init = 1;
1903-
else
1893+
if (fud) {
1894+
if (fuse_dev_fc_get(fud))
19041895
goto err_unlock;
1896+
if (fud->sync_init)
1897+
fc->sync_init = 1;
19051898
}
19061899

19071900
err = fuse_ctl_add_conn(fc);
@@ -1910,8 +1903,8 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
19101903

19111904
list_add_tail(&fc->entry, &fuse_conn_list);
19121905
sb->s_root = root_dentry;
1913-
if (ctx->fudptr) {
1914-
*ctx->fudptr = fud;
1906+
if (fud) {
1907+
fuse_dev_install(fud, fc);
19151908
wake_up_all(&fuse_dev_waitq);
19161909
}
19171910
mutex_unlock(&fuse_mutex);
@@ -1920,9 +1913,6 @@ int fuse_fill_super_common(struct super_block *sb, struct fuse_fs_context *ctx)
19201913
err_unlock:
19211914
mutex_unlock(&fuse_mutex);
19221915
dput(root_dentry);
1923-
err_dev_free:
1924-
if (fud)
1925-
fuse_dev_free(fud);
19261916
err_free_dax:
19271917
if (IS_ENABLED(CONFIG_FUSE_DAX))
19281918
fuse_dax_conn_free(fc);
@@ -1948,13 +1938,10 @@ static int fuse_fill_super(struct super_block *sb, struct fs_context *fsc)
19481938
if ((ctx->file->f_op != &fuse_dev_operations) ||
19491939
(ctx->file->f_cred->user_ns != sb->s_user_ns))
19501940
return -EINVAL;
1951-
ctx->fudptr = &ctx->file->private_data;
19521941

19531942
err = fuse_fill_super_common(sb, ctx);
19541943
if (err)
19551944
return err;
1956-
/* file->private_data shall be visible on all CPUs after this */
1957-
smp_mb();
19581945

19591946
fm = get_fuse_mount_super(sb);
19601947

fs/fuse/virtio_fs.c

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1590,8 +1590,6 @@ static int virtio_fs_fill_super(struct super_block *sb, struct fs_context *fsc)
15901590
goto err_free_fuse_devs;
15911591
}
15921592

1593-
/* virtiofs allocates and installs its own fuse devices */
1594-
ctx->fudptr = NULL;
15951593
if (ctx->dax_mode != FUSE_DAX_NEVER) {
15961594
if (ctx->dax_mode == FUSE_DAX_ALWAYS && !fs->dax_dev) {
15971595
err = -EINVAL;

0 commit comments

Comments
 (0)