1 // SPDX-License-Identifier: GPL-2.0
5 * Copyright (c) 2024 CTERA Networks.
10 #include <linux/kernel.h>
11 #include <linux/sched.h>
12 #include <linux/file.h>
16 * Start cached io mode, where parallel dio writes are not allowed.
18 int fuse_file_cached_io_start(struct inode
*inode
, struct fuse_file
*ff
)
20 struct fuse_inode
*fi
= get_fuse_inode(inode
);
23 /* There are no io modes if server does not implement open */
24 if (!ff
->release_args
)
28 if (fi
->iocachectr
< 0) {
32 WARN_ON(ff
->iomode
== IOM_UNCACHED
);
33 if (ff
->iomode
== IOM_NONE
) {
34 ff
->iomode
= IOM_CACHED
;
35 if (fi
->iocachectr
== 0)
36 set_bit(FUSE_I_CACHE_IO_MODE
, &fi
->state
);
40 spin_unlock(&fi
->lock
);
44 static void fuse_file_cached_io_end(struct inode
*inode
, struct fuse_file
*ff
)
46 struct fuse_inode
*fi
= get_fuse_inode(inode
);
49 WARN_ON(fi
->iocachectr
<= 0);
50 WARN_ON(ff
->iomode
!= IOM_CACHED
);
51 ff
->iomode
= IOM_NONE
;
53 if (fi
->iocachectr
== 0)
54 clear_bit(FUSE_I_CACHE_IO_MODE
, &fi
->state
);
55 spin_unlock(&fi
->lock
);
58 /* Start strictly uncached io mode where cache access is not allowed */
59 static int fuse_file_uncached_io_start(struct inode
*inode
, struct fuse_file
*ff
)
61 struct fuse_inode
*fi
= get_fuse_inode(inode
);
65 if (fi
->iocachectr
> 0) {
69 WARN_ON(ff
->iomode
!= IOM_NONE
);
71 ff
->iomode
= IOM_UNCACHED
;
73 spin_unlock(&fi
->lock
);
77 static void fuse_file_uncached_io_end(struct inode
*inode
, struct fuse_file
*ff
)
79 struct fuse_inode
*fi
= get_fuse_inode(inode
);
82 WARN_ON(fi
->iocachectr
>= 0);
83 WARN_ON(ff
->iomode
!= IOM_UNCACHED
);
84 ff
->iomode
= IOM_NONE
;
86 spin_unlock(&fi
->lock
);
89 /* Request access to submit new io to inode via open file */
90 int fuse_file_io_open(struct file
*file
, struct inode
*inode
)
92 struct fuse_file
*ff
= file
->private_data
;
96 * io modes are not relevant with DAX and with server that does not
99 if (FUSE_IS_DAX(inode
) || !ff
->release_args
)
103 * FOPEN_PARALLEL_DIRECT_WRITES requires FOPEN_DIRECT_IO.
105 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
106 ff
->open_flags
&= ~FOPEN_PARALLEL_DIRECT_WRITES
;
109 * First parallel dio open denies caching inode io mode.
110 * First caching file open enters caching inode io mode.
112 * Note that if user opens a file open with O_DIRECT, but server did
113 * not specify FOPEN_DIRECT_IO, a later fcntl() could remove O_DIRECT,
114 * so we put the inode in caching mode to prevent parallel dio.
116 if (ff
->open_flags
& FOPEN_DIRECT_IO
) {
117 if (ff
->open_flags
& FOPEN_PARALLEL_DIRECT_WRITES
)
118 err
= fuse_file_uncached_io_start(inode
, ff
);
122 err
= fuse_file_cached_io_start(inode
, ff
);
130 pr_debug("failed to open file in requested io mode (open_flags=0x%x, err=%i).\n",
131 ff
->open_flags
, err
);
133 * The file open mode determines the inode io mode.
134 * Using incorrect open mode is a server mistake, which results in
135 * user visible failure of open() with EIO error.
140 /* No more pending io and no new io possible to inode via open/mmapped file */
141 void fuse_file_io_release(struct fuse_file
*ff
, struct inode
*inode
)
144 * Last parallel dio close allows caching inode io mode.
145 * Last caching file close exits caching inode io mode.
147 switch (ff
->iomode
) {
152 fuse_file_uncached_io_end(inode
, ff
);
155 fuse_file_cached_io_end(inode
, ff
);