passthrough.c
6.04 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
// SPDX-License-Identifier: GPL-2.0
#include "fuse_i.h"
#include <linux/fuse.h>
#include <linux/idr.h>
#include <linux/uio.h>
struct fuse_aio_req {
struct kiocb iocb;
struct kiocb *iocb_fuse;
};
static void fuse_copyattr(struct file *dst_file, struct file *src_file)
{
struct inode *dst = file_inode(dst_file);
struct inode *src = file_inode(src_file);
i_size_write(dst, i_size_read(src));
}
static inline rwf_t iocb_to_rw_flags(int ifl)
{
rwf_t flags = 0;
if (ifl & IOCB_APPEND)
flags |= RWF_APPEND;
if (ifl & IOCB_DSYNC)
flags |= RWF_DSYNC;
if (ifl & IOCB_HIPRI)
flags |= RWF_HIPRI;
if (ifl & IOCB_NOWAIT)
flags |= RWF_NOWAIT;
if (ifl & IOCB_SYNC)
flags |= RWF_SYNC;
return flags;
}
static void fuse_aio_cleanup_handler(struct fuse_aio_req *aio_req)
{
struct kiocb *iocb = &aio_req->iocb;
struct kiocb *iocb_fuse = aio_req->iocb_fuse;
if (iocb->ki_flags & IOCB_WRITE) {
__sb_writers_acquired(file_inode(iocb->ki_filp)->i_sb,
SB_FREEZE_WRITE);
file_end_write(iocb->ki_filp);
fuse_copyattr(iocb_fuse->ki_filp, iocb->ki_filp);
}
iocb_fuse->ki_pos = iocb->ki_pos;
kfree(aio_req);
}
static void fuse_aio_rw_complete(struct kiocb *iocb, long res, long res2)
{
struct fuse_aio_req *aio_req =
container_of(iocb, struct fuse_aio_req, iocb);
struct kiocb *iocb_fuse = aio_req->iocb_fuse;
fuse_aio_cleanup_handler(aio_req);
iocb_fuse->ki_complete(iocb_fuse, res, res2);
}
ssize_t fuse_passthrough_read_iter(struct kiocb *iocb_fuse,
struct iov_iter *iter)
{
ssize_t ret;
const struct cred *old_cred;
struct file *fuse_filp = iocb_fuse->ki_filp;
struct fuse_file *ff = fuse_filp->private_data;
struct file *passthrough_filp = ff->passthrough.filp;
if (!iov_iter_count(iter))
return 0;
old_cred = override_creds(ff->passthrough.cred);
if (is_sync_kiocb(iocb_fuse)) {
ret = vfs_iter_read(passthrough_filp, iter, &iocb_fuse->ki_pos,
iocb_to_rw_flags(iocb_fuse->ki_flags));
} else {
struct fuse_aio_req *aio_req;
aio_req = kmalloc(sizeof(struct fuse_aio_req), GFP_KERNEL);
if (!aio_req)
return -ENOMEM;
aio_req->iocb_fuse = iocb_fuse;
kiocb_clone(&aio_req->iocb, iocb_fuse, passthrough_filp);
aio_req->iocb.ki_complete = fuse_aio_rw_complete;
ret = call_read_iter(passthrough_filp, &aio_req->iocb, iter);
if (ret != -EIOCBQUEUED)
fuse_aio_cleanup_handler(aio_req);
}
revert_creds(old_cred);
return ret;
}
ssize_t fuse_passthrough_write_iter(struct kiocb *iocb_fuse,
struct iov_iter *iter)
{
ssize_t ret;
const struct cred *old_cred;
struct file *fuse_filp = iocb_fuse->ki_filp;
struct fuse_file *ff = fuse_filp->private_data;
struct inode *fuse_inode = file_inode(fuse_filp);
struct file *passthrough_filp = ff->passthrough.filp;
struct inode *passthrough_inode = file_inode(passthrough_filp);
if (!iov_iter_count(iter))
return 0;
inode_lock(fuse_inode);
old_cred = override_creds(ff->passthrough.cred);
if (is_sync_kiocb(iocb_fuse)) {
file_start_write(passthrough_filp);
ret = vfs_iter_write(passthrough_filp, iter, &iocb_fuse->ki_pos,
iocb_to_rw_flags(iocb_fuse->ki_flags));
file_end_write(passthrough_filp);
if (ret > 0)
fuse_copyattr(fuse_filp, passthrough_filp);
} else {
struct fuse_aio_req *aio_req;
aio_req = kmalloc(sizeof(struct fuse_aio_req), GFP_KERNEL);
if (!aio_req) {
ret = -ENOMEM;
goto out;
}
file_start_write(passthrough_filp);
__sb_writers_release(passthrough_inode->i_sb, SB_FREEZE_WRITE);
aio_req->iocb_fuse = iocb_fuse;
kiocb_clone(&aio_req->iocb, iocb_fuse, passthrough_filp);
aio_req->iocb.ki_complete = fuse_aio_rw_complete;
ret = call_write_iter(passthrough_filp, &aio_req->iocb, iter);
if (ret != -EIOCBQUEUED)
fuse_aio_cleanup_handler(aio_req);
}
out:
revert_creds(old_cred);
inode_unlock(fuse_inode);
return ret;
}
int fuse_passthrough_open(struct fuse_dev *fud,
struct fuse_passthrough_out *pto)
{
int res;
struct file *passthrough_filp;
struct fuse_conn *fc = fud->fc;
struct fuse_passthrough *passthrough;
if (!fc->passthrough)
return -EPERM;
/* This field is reserved for future implementation */
if (pto->len != 0)
return -EINVAL;
passthrough_filp = fget(pto->fd);
if (!passthrough_filp) {
pr_err("FUSE: invalid file descriptor for passthrough.\n");
return -EBADF;
}
if (!passthrough_filp->f_op->read_iter ||
!passthrough_filp->f_op->write_iter) {
pr_err("FUSE: passthrough file misses file operations.\n");
return -EBADF;
}
passthrough = kmalloc(sizeof(struct fuse_passthrough), GFP_KERNEL);
if (!passthrough)
return -ENOMEM;
passthrough->filp = passthrough_filp;
passthrough->cred = prepare_creds();
idr_preload(GFP_KERNEL);
spin_lock(&fc->passthrough_req_lock);
res = idr_alloc(&fc->passthrough_req, passthrough, 1, 0, GFP_ATOMIC);
spin_unlock(&fc->passthrough_req_lock);
idr_preload_end();
if (res <= 0) {
fuse_passthrough_release(passthrough);
kfree(passthrough);
}
return res;
}
int fuse_passthrough_setup(struct fuse_conn *fc, struct fuse_file *ff,
struct fuse_open_out *openarg)
{
struct inode *passthrough_inode;
struct super_block *passthrough_sb;
struct fuse_passthrough *passthrough;
int passthrough_fh = openarg->passthrough_fh;
if (!fc->passthrough)
return -EPERM;
/* Default case, passthrough is not requested */
if (passthrough_fh <= 0)
return -EINVAL;
spin_lock(&fc->passthrough_req_lock);
passthrough = idr_remove(&fc->passthrough_req, passthrough_fh);
spin_unlock(&fc->passthrough_req_lock);
if (!passthrough)
return -EINVAL;
passthrough_inode = file_inode(passthrough->filp);
passthrough_sb = passthrough_inode->i_sb;
if (passthrough_sb->s_stack_depth >= FILESYSTEM_MAX_STACK_DEPTH) {
pr_err("FUSE: fs stacking depth exceeded for passthrough\n");
fuse_passthrough_release(passthrough);
kfree(passthrough);
return -EINVAL;
}
ff->passthrough = *passthrough;
kfree(passthrough);
return 0;
}
void fuse_passthrough_release(struct fuse_passthrough *passthrough)
{
if (passthrough->filp) {
fput(passthrough->filp);
passthrough->filp = NULL;
}
if (passthrough->cred) {
put_cred(passthrough->cred);
passthrough->cred = NULL;
}
}