hc
2024-03-26 e0728245c89800c2038c23308f2d88969d5b41c8
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// SPDX-License-Identifier: GPL-2.0
 
#include "fuse_i.h"
 
#include <linux/fuse.h>
#include <linux/idr.h>
#include <linux/uio.h>
 
#define PASSTHROUGH_IOCB_MASK                                                  \
   (IOCB_APPEND | IOCB_DSYNC | IOCB_HIPRI | IOCB_NOWAIT | IOCB_SYNC)
 
struct fuse_aio_req {
   struct kiocb iocb;
   struct kiocb *iocb_fuse;
};
 
static void fuse_file_accessed(struct file *dst_file, struct file *src_file)
{
   struct inode *dst_inode;
   struct inode *src_inode;
 
   if (dst_file->f_flags & O_NOATIME)
       return;
 
   dst_inode = file_inode(dst_file);
   src_inode = file_inode(src_file);
 
   if ((!timespec64_equal(&dst_inode->i_mtime, &src_inode->i_mtime) ||
        !timespec64_equal(&dst_inode->i_ctime, &src_inode->i_ctime))) {
       dst_inode->i_mtime = src_inode->i_mtime;
       dst_inode->i_ctime = src_inode->i_ctime;
   }
 
   touch_atime(&dst_file->f_path);
}
 
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);
 
   dst->i_atime = src->i_atime;
   dst->i_mtime = src->i_mtime;
   dst->i_ctime = src->i_ctime;
   i_size_write(dst, i_size_read(src));
}
 
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,
                            PASSTHROUGH_IOCB_MASK));
   } else {
       struct fuse_aio_req *aio_req;
 
       aio_req = kmalloc(sizeof(struct fuse_aio_req), GFP_KERNEL);
       if (!aio_req) {
           ret = -ENOMEM;
           goto out;
       }
 
       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);
   }
out:
   revert_creds(old_cred);
 
   fuse_file_accessed(fuse_filp, passthrough_filp);
 
   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);
 
   fuse_copyattr(fuse_filp, passthrough_filp);
 
   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,
                             PASSTHROUGH_IOCB_MASK));
       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;
}
 
ssize_t fuse_passthrough_mmap(struct file *file, struct vm_area_struct *vma)
{
   int ret;
   const struct cred *old_cred;
   struct fuse_file *ff = file->private_data;
   struct file *passthrough_filp = ff->passthrough.filp;
 
   if (!passthrough_filp->f_op->mmap)
       return -ENODEV;
 
   if (WARN_ON(file != vma->vm_file))
       return -EIO;
 
   vma->vm_file = get_file(passthrough_filp);
 
   old_cred = override_creds(ff->passthrough.cred);
   ret = call_mmap(vma->vm_file, vma);
   revert_creds(old_cred);
 
   if (ret)
       fput(passthrough_filp);
   else
       fput(file);
 
   fuse_file_accessed(file, passthrough_filp);
 
   return ret;
}
 
int fuse_passthrough_open(struct fuse_dev *fud, u32 lower_fd)
{
   int res;
   struct file *passthrough_filp;
   struct fuse_conn *fc = fud->fc;
   struct inode *passthrough_inode;
   struct super_block *passthrough_sb;
   struct fuse_passthrough *passthrough;
 
   if (!fc->passthrough)
       return -EPERM;
 
   passthrough_filp = fget(lower_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");
       res = -EBADF;
       goto err_free_file;
   }
 
   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");
       res = -EINVAL;
       goto err_free_file;
   }
 
   passthrough = kmalloc(sizeof(struct fuse_passthrough), GFP_KERNEL);
   if (!passthrough) {
       res = -ENOMEM;
       goto err_free_file;
   }
 
   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)
       return res;
 
   fuse_passthrough_release(passthrough);
   kfree(passthrough);
 
err_free_file:
   fput(passthrough_filp);
 
   return res;
}
 
int fuse_passthrough_setup(struct fuse_conn *fc, struct fuse_file *ff,
              struct fuse_open_out *openarg)
{
   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;
 
   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;
   }
}