Skip to content

Commit 4864a6d

Browse files
amir73ilMiklos Szeredi
authored andcommitted
fuse: fix wrong ff->iomode state changes from parallel dio write
There is a confusion with fuse_file_uncached_io_{start,end} interface. These helpers do two things when called from passthrough open()/release(): 1. Take/drop negative refcount of fi->iocachectr (inode uncached io mode) 2. State change ff->iomode IOM_NONE <-> IOM_UNCACHED (file uncached open) The calls from parallel dio write path need to take a reference on fi->iocachectr, but they should not be changing ff->iomode state, because in this case, the fi->iocachectr reference does not stick around until file release(). Factor out helpers fuse_inode_uncached_io_{start,end}, to be used from parallel dio write path and rename fuse_file_*cached_io_{start,end} helpers to fuse_file_*cached_io_{open,release} to clarify the difference. Fixes: 205c1d8 ("fuse: allow parallel dio writes with FUSE_DIRECT_IO_ALLOW_MMAP") Signed-off-by: Amir Goldstein <[email protected]> Signed-off-by: Miklos Szeredi <[email protected]>
1 parent fec50db commit 4864a6d

File tree

4 files changed

+51
-25
lines changed

4 files changed

+51
-25
lines changed

fs/fuse/file.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1362,7 +1362,7 @@ static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from,
13621362
bool *exclusive)
13631363
{
13641364
struct inode *inode = file_inode(iocb->ki_filp);
1365-
struct fuse_file *ff = iocb->ki_filp->private_data;
1365+
struct fuse_inode *fi = get_fuse_inode(inode);
13661366

13671367
*exclusive = fuse_dio_wr_exclusive_lock(iocb, from);
13681368
if (*exclusive) {
@@ -1377,7 +1377,7 @@ static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from,
13771377
* have raced, so check it again.
13781378
*/
13791379
if (fuse_io_past_eof(iocb, from) ||
1380-
fuse_file_uncached_io_start(inode, ff, NULL) != 0) {
1380+
fuse_inode_uncached_io_start(fi, NULL) != 0) {
13811381
inode_unlock_shared(inode);
13821382
inode_lock(inode);
13831383
*exclusive = true;
@@ -1388,13 +1388,13 @@ static void fuse_dio_lock(struct kiocb *iocb, struct iov_iter *from,
13881388
static void fuse_dio_unlock(struct kiocb *iocb, bool exclusive)
13891389
{
13901390
struct inode *inode = file_inode(iocb->ki_filp);
1391-
struct fuse_file *ff = iocb->ki_filp->private_data;
1391+
struct fuse_inode *fi = get_fuse_inode(inode);
13921392

13931393
if (exclusive) {
13941394
inode_unlock(inode);
13951395
} else {
13961396
/* Allow opens in caching mode after last parallel dio end */
1397-
fuse_file_uncached_io_end(inode, ff);
1397+
fuse_inode_uncached_io_end(fi);
13981398
inode_unlock_shared(inode);
13991399
}
14001400
}
@@ -2574,8 +2574,10 @@ static int fuse_file_mmap(struct file *file, struct vm_area_struct *vma)
25742574
* First mmap of direct_io file enters caching inode io mode.
25752575
* Also waits for parallel dio writers to go into serial mode
25762576
* (exclusive instead of shared lock).
2577+
* After first mmap, the inode stays in caching io mode until
2578+
* the direct_io file release.
25772579
*/
2578-
rc = fuse_file_cached_io_start(inode, ff);
2580+
rc = fuse_file_cached_io_open(inode, ff);
25792581
if (rc)
25802582
return rc;
25812583
}

fs/fuse/fuse_i.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1394,9 +1394,10 @@ int fuse_fileattr_set(struct mnt_idmap *idmap,
13941394
struct dentry *dentry, struct fileattr *fa);
13951395

13961396
/* iomode.c */
1397-
int fuse_file_cached_io_start(struct inode *inode, struct fuse_file *ff);
1398-
int fuse_file_uncached_io_start(struct inode *inode, struct fuse_file *ff, struct fuse_backing *fb);
1399-
void fuse_file_uncached_io_end(struct inode *inode, struct fuse_file *ff);
1397+
int fuse_file_cached_io_open(struct inode *inode, struct fuse_file *ff);
1398+
int fuse_inode_uncached_io_start(struct fuse_inode *fi,
1399+
struct fuse_backing *fb);
1400+
void fuse_inode_uncached_io_end(struct fuse_inode *fi);
14001401

14011402
int fuse_file_io_open(struct file *file, struct inode *inode);
14021403
void fuse_file_io_release(struct fuse_file *ff, struct inode *inode);

fs/fuse/inode.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -175,6 +175,7 @@ static void fuse_evict_inode(struct inode *inode)
175175
}
176176
}
177177
if (S_ISREG(inode->i_mode) && !fuse_is_bad(inode)) {
178+
WARN_ON(fi->iocachectr != 0);
178179
WARN_ON(!list_empty(&fi->write_files));
179180
WARN_ON(!list_empty(&fi->queued_writes));
180181
}

fs/fuse/iomode.c

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -21,12 +21,13 @@ static inline bool fuse_is_io_cache_wait(struct fuse_inode *fi)
2121
}
2222

2323
/*
24-
* Start cached io mode.
24+
* Called on cached file open() and on first mmap() of direct_io file.
25+
* Takes cached_io inode mode reference to be dropped on file release.
2526
*
2627
* Blocks new parallel dio writes and waits for the in-progress parallel dio
2728
* writes to complete.
2829
*/
29-
int fuse_file_cached_io_start(struct inode *inode, struct fuse_file *ff)
30+
int fuse_file_cached_io_open(struct inode *inode, struct fuse_file *ff)
3031
{
3132
struct fuse_inode *fi = get_fuse_inode(inode);
3233

@@ -67,10 +68,9 @@ int fuse_file_cached_io_start(struct inode *inode, struct fuse_file *ff)
6768
return 0;
6869
}
6970

70-
static void fuse_file_cached_io_end(struct inode *inode, struct fuse_file *ff)
71+
static void fuse_file_cached_io_release(struct fuse_file *ff,
72+
struct fuse_inode *fi)
7173
{
72-
struct fuse_inode *fi = get_fuse_inode(inode);
73-
7474
spin_lock(&fi->lock);
7575
WARN_ON(fi->iocachectr <= 0);
7676
WARN_ON(ff->iomode != IOM_CACHED);
@@ -82,9 +82,8 @@ static void fuse_file_cached_io_end(struct inode *inode, struct fuse_file *ff)
8282
}
8383

8484
/* Start strictly uncached io mode where cache access is not allowed */
85-
int fuse_file_uncached_io_start(struct inode *inode, struct fuse_file *ff, struct fuse_backing *fb)
85+
int fuse_inode_uncached_io_start(struct fuse_inode *fi, struct fuse_backing *fb)
8686
{
87-
struct fuse_inode *fi = get_fuse_inode(inode);
8887
struct fuse_backing *oldfb;
8988
int err = 0;
9089

@@ -99,9 +98,7 @@ int fuse_file_uncached_io_start(struct inode *inode, struct fuse_file *ff, struc
9998
err = -ETXTBSY;
10099
goto unlock;
101100
}
102-
WARN_ON(ff->iomode != IOM_NONE);
103101
fi->iocachectr--;
104-
ff->iomode = IOM_UNCACHED;
105102

106103
/* fuse inode holds a single refcount of backing file */
107104
if (!oldfb) {
@@ -115,15 +112,29 @@ int fuse_file_uncached_io_start(struct inode *inode, struct fuse_file *ff, struc
115112
return err;
116113
}
117114

118-
void fuse_file_uncached_io_end(struct inode *inode, struct fuse_file *ff)
115+
/* Takes uncached_io inode mode reference to be dropped on file release */
116+
static int fuse_file_uncached_io_open(struct inode *inode,
117+
struct fuse_file *ff,
118+
struct fuse_backing *fb)
119119
{
120120
struct fuse_inode *fi = get_fuse_inode(inode);
121+
int err;
122+
123+
err = fuse_inode_uncached_io_start(fi, fb);
124+
if (err)
125+
return err;
126+
127+
WARN_ON(ff->iomode != IOM_NONE);
128+
ff->iomode = IOM_UNCACHED;
129+
return 0;
130+
}
131+
132+
void fuse_inode_uncached_io_end(struct fuse_inode *fi)
133+
{
121134
struct fuse_backing *oldfb = NULL;
122135

123136
spin_lock(&fi->lock);
124137
WARN_ON(fi->iocachectr >= 0);
125-
WARN_ON(ff->iomode != IOM_UNCACHED);
126-
ff->iomode = IOM_NONE;
127138
fi->iocachectr++;
128139
if (!fi->iocachectr) {
129140
wake_up(&fi->direct_io_waitq);
@@ -134,6 +145,15 @@ void fuse_file_uncached_io_end(struct inode *inode, struct fuse_file *ff)
134145
fuse_backing_put(oldfb);
135146
}
136147

148+
/* Drop uncached_io reference from passthrough open */
149+
static void fuse_file_uncached_io_release(struct fuse_file *ff,
150+
struct fuse_inode *fi)
151+
{
152+
WARN_ON(ff->iomode != IOM_UNCACHED);
153+
ff->iomode = IOM_NONE;
154+
fuse_inode_uncached_io_end(fi);
155+
}
156+
137157
/*
138158
* Open flags that are allowed in combination with FOPEN_PASSTHROUGH.
139159
* A combination of FOPEN_PASSTHROUGH and FOPEN_DIRECT_IO means that read/write
@@ -163,7 +183,7 @@ static int fuse_file_passthrough_open(struct inode *inode, struct file *file)
163183
return PTR_ERR(fb);
164184

165185
/* First passthrough file open denies caching inode io mode */
166-
err = fuse_file_uncached_io_start(inode, ff, fb);
186+
err = fuse_file_uncached_io_open(inode, ff, fb);
167187
if (!err)
168188
return 0;
169189

@@ -216,7 +236,7 @@ int fuse_file_io_open(struct file *file, struct inode *inode)
216236
if (ff->open_flags & FOPEN_PASSTHROUGH)
217237
err = fuse_file_passthrough_open(inode, file);
218238
else
219-
err = fuse_file_cached_io_start(inode, ff);
239+
err = fuse_file_cached_io_open(inode, ff);
220240
if (err)
221241
goto fail;
222242

@@ -236,19 +256,21 @@ int fuse_file_io_open(struct file *file, struct inode *inode)
236256
/* No more pending io and no new io possible to inode via open/mmapped file */
237257
void fuse_file_io_release(struct fuse_file *ff, struct inode *inode)
238258
{
259+
struct fuse_inode *fi = get_fuse_inode(inode);
260+
239261
/*
240-
* Last parallel dio close allows caching inode io mode.
262+
* Last passthrough file close allows caching inode io mode.
241263
* Last caching file close exits caching inode io mode.
242264
*/
243265
switch (ff->iomode) {
244266
case IOM_NONE:
245267
/* Nothing to do */
246268
break;
247269
case IOM_UNCACHED:
248-
fuse_file_uncached_io_end(inode, ff);
270+
fuse_file_uncached_io_release(ff, fi);
249271
break;
250272
case IOM_CACHED:
251-
fuse_file_cached_io_end(inode, ff);
273+
fuse_file_cached_io_release(ff, fi);
252274
break;
253275
}
254276
}

0 commit comments

Comments
 (0)