]>
Commit | Line | Data |
---|---|---|
2de121f0 | 1 | /* |
7387863d DDAG |
2 | * FUSE: Filesystem in Userspace |
3 | * Copyright (C) 2001-2007 Miklos Szeredi <[email protected]> | |
4 | * | |
5 | * Implementation of (most of) the low-level FUSE API. The session loop | |
6 | * functions are implemented in separate files. | |
7 | * | |
8 | * This program can be distributed under the terms of the GNU LGPLv2. | |
9 | * See the file COPYING.LIB | |
10 | */ | |
2de121f0 | 11 | |
09863ebc | 12 | #include "qemu/osdep.h" |
2de121f0 | 13 | #include "fuse_i.h" |
09863ebc | 14 | #include "standard-headers/linux/fuse.h" |
2de121f0 | 15 | #include "fuse_misc.h" |
7387863d | 16 | #include "fuse_opt.h" |
d14bf584 | 17 | #include "fuse_virtio.h" |
2de121f0 | 18 | |
7387863d DDAG |
19 | #include <assert.h> |
20 | #include <errno.h> | |
21 | #include <limits.h> | |
22 | #include <stddef.h> | |
2de121f0 DDAG |
23 | #include <stdio.h> |
24 | #include <stdlib.h> | |
2de121f0 | 25 | #include <string.h> |
2de121f0 | 26 | #include <sys/file.h> |
7387863d | 27 | #include <unistd.h> |
2de121f0 DDAG |
28 | |
29 | ||
30 | #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg))) | |
31 | #define OFFSET_MAX 0x7fffffffffffffffLL | |
32 | ||
2de121f0 | 33 | struct fuse_pollhandle { |
7387863d DDAG |
34 | uint64_t kh; |
35 | struct fuse_session *se; | |
2de121f0 DDAG |
36 | }; |
37 | ||
38 | static size_t pagesize; | |
39 | ||
40 | static __attribute__((constructor)) void fuse_ll_init_pagesize(void) | |
41 | { | |
7387863d | 42 | pagesize = getpagesize(); |
2de121f0 DDAG |
43 | } |
44 | ||
45 | static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr) | |
46 | { | |
7387863d DDAG |
47 | attr->ino = stbuf->st_ino; |
48 | attr->mode = stbuf->st_mode; | |
49 | attr->nlink = stbuf->st_nlink; | |
50 | attr->uid = stbuf->st_uid; | |
51 | attr->gid = stbuf->st_gid; | |
52 | attr->rdev = stbuf->st_rdev; | |
53 | attr->size = stbuf->st_size; | |
54 | attr->blksize = stbuf->st_blksize; | |
55 | attr->blocks = stbuf->st_blocks; | |
56 | attr->atime = stbuf->st_atime; | |
57 | attr->mtime = stbuf->st_mtime; | |
58 | attr->ctime = stbuf->st_ctime; | |
59 | attr->atimensec = ST_ATIM_NSEC(stbuf); | |
60 | attr->mtimensec = ST_MTIM_NSEC(stbuf); | |
61 | attr->ctimensec = ST_CTIM_NSEC(stbuf); | |
2de121f0 DDAG |
62 | } |
63 | ||
64 | static void convert_attr(const struct fuse_setattr_in *attr, struct stat *stbuf) | |
65 | { | |
7387863d DDAG |
66 | stbuf->st_mode = attr->mode; |
67 | stbuf->st_uid = attr->uid; | |
68 | stbuf->st_gid = attr->gid; | |
69 | stbuf->st_size = attr->size; | |
70 | stbuf->st_atime = attr->atime; | |
71 | stbuf->st_mtime = attr->mtime; | |
72 | stbuf->st_ctime = attr->ctime; | |
73 | ST_ATIM_NSEC_SET(stbuf, attr->atimensec); | |
74 | ST_MTIM_NSEC_SET(stbuf, attr->mtimensec); | |
75 | ST_CTIM_NSEC_SET(stbuf, attr->ctimensec); | |
2de121f0 DDAG |
76 | } |
77 | ||
7387863d | 78 | static size_t iov_length(const struct iovec *iov, size_t count) |
2de121f0 | 79 | { |
7387863d DDAG |
80 | size_t seg; |
81 | size_t ret = 0; | |
2de121f0 | 82 | |
7387863d DDAG |
83 | for (seg = 0; seg < count; seg++) { |
84 | ret += iov[seg].iov_len; | |
85 | } | |
86 | return ret; | |
2de121f0 DDAG |
87 | } |
88 | ||
89 | static void list_init_req(struct fuse_req *req) | |
90 | { | |
7387863d DDAG |
91 | req->next = req; |
92 | req->prev = req; | |
2de121f0 DDAG |
93 | } |
94 | ||
95 | static void list_del_req(struct fuse_req *req) | |
96 | { | |
7387863d DDAG |
97 | struct fuse_req *prev = req->prev; |
98 | struct fuse_req *next = req->next; | |
99 | prev->next = next; | |
100 | next->prev = prev; | |
2de121f0 DDAG |
101 | } |
102 | ||
103 | static void list_add_req(struct fuse_req *req, struct fuse_req *next) | |
104 | { | |
7387863d DDAG |
105 | struct fuse_req *prev = next->prev; |
106 | req->next = next; | |
107 | req->prev = prev; | |
108 | prev->next = req; | |
109 | next->prev = req; | |
2de121f0 DDAG |
110 | } |
111 | ||
112 | static void destroy_req(fuse_req_t req) | |
113 | { | |
7387863d DDAG |
114 | pthread_mutex_destroy(&req->lock); |
115 | free(req); | |
2de121f0 DDAG |
116 | } |
117 | ||
118 | void fuse_free_req(fuse_req_t req) | |
119 | { | |
7387863d DDAG |
120 | int ctr; |
121 | struct fuse_session *se = req->se; | |
2de121f0 | 122 | |
7387863d DDAG |
123 | pthread_mutex_lock(&se->lock); |
124 | req->u.ni.func = NULL; | |
125 | req->u.ni.data = NULL; | |
126 | list_del_req(req); | |
127 | ctr = --req->ctr; | |
128 | req->ch = NULL; | |
129 | pthread_mutex_unlock(&se->lock); | |
130 | if (!ctr) { | |
131 | destroy_req(req); | |
132 | } | |
2de121f0 DDAG |
133 | } |
134 | ||
135 | static struct fuse_req *fuse_ll_alloc_req(struct fuse_session *se) | |
136 | { | |
7387863d | 137 | struct fuse_req *req; |
2de121f0 | 138 | |
7387863d DDAG |
139 | req = (struct fuse_req *)calloc(1, sizeof(struct fuse_req)); |
140 | if (req == NULL) { | |
141 | fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate request\n"); | |
142 | } else { | |
143 | req->se = se; | |
144 | req->ctr = 1; | |
145 | list_init_req(req); | |
146 | fuse_mutex_init(&req->lock); | |
147 | } | |
2de121f0 | 148 | |
7387863d | 149 | return req; |
2de121f0 DDAG |
150 | } |
151 | ||
152 | /* Send data. If *ch* is NULL, send via session master fd */ | |
153 | static int fuse_send_msg(struct fuse_session *se, struct fuse_chan *ch, | |
7387863d | 154 | struct iovec *iov, int count) |
2de121f0 | 155 | { |
7387863d | 156 | struct fuse_out_header *out = iov[0].iov_base; |
2de121f0 | 157 | |
7387863d DDAG |
158 | out->len = iov_length(iov, count); |
159 | if (se->debug) { | |
160 | if (out->unique == 0) { | |
161 | fuse_log(FUSE_LOG_DEBUG, "NOTIFY: code=%d length=%u\n", out->error, | |
162 | out->len); | |
163 | } else if (out->error) { | |
164 | fuse_log(FUSE_LOG_DEBUG, | |
165 | " unique: %llu, error: %i (%s), outsize: %i\n", | |
166 | (unsigned long long)out->unique, out->error, | |
167 | strerror(-out->error), out->len); | |
168 | } else { | |
169 | fuse_log(FUSE_LOG_DEBUG, " unique: %llu, success, outsize: %i\n", | |
170 | (unsigned long long)out->unique, out->len); | |
171 | } | |
172 | } | |
2de121f0 | 173 | |
7387863d DDAG |
174 | abort(); /* virtio should have taken it before here */ |
175 | return 0; | |
2de121f0 DDAG |
176 | } |
177 | ||
178 | ||
179 | int fuse_send_reply_iov_nofree(fuse_req_t req, int error, struct iovec *iov, | |
7387863d | 180 | int count) |
2de121f0 | 181 | { |
7387863d | 182 | struct fuse_out_header out; |
2de121f0 | 183 | |
7387863d DDAG |
184 | if (error <= -1000 || error > 0) { |
185 | fuse_log(FUSE_LOG_ERR, "fuse: bad error value: %i\n", error); | |
186 | error = -ERANGE; | |
187 | } | |
2de121f0 | 188 | |
7387863d DDAG |
189 | out.unique = req->unique; |
190 | out.error = error; | |
2de121f0 | 191 | |
7387863d DDAG |
192 | iov[0].iov_base = &out; |
193 | iov[0].iov_len = sizeof(struct fuse_out_header); | |
2de121f0 | 194 | |
7387863d | 195 | return fuse_send_msg(req->se, req->ch, iov, count); |
2de121f0 DDAG |
196 | } |
197 | ||
198 | static int send_reply_iov(fuse_req_t req, int error, struct iovec *iov, | |
7387863d | 199 | int count) |
2de121f0 | 200 | { |
7387863d | 201 | int res; |
2de121f0 | 202 | |
7387863d DDAG |
203 | res = fuse_send_reply_iov_nofree(req, error, iov, count); |
204 | fuse_free_req(req); | |
205 | return res; | |
2de121f0 DDAG |
206 | } |
207 | ||
208 | static int send_reply(fuse_req_t req, int error, const void *arg, | |
7387863d | 209 | size_t argsize) |
2de121f0 | 210 | { |
7387863d DDAG |
211 | struct iovec iov[2]; |
212 | int count = 1; | |
213 | if (argsize) { | |
214 | iov[1].iov_base = (void *)arg; | |
215 | iov[1].iov_len = argsize; | |
216 | count++; | |
217 | } | |
218 | return send_reply_iov(req, error, iov, count); | |
2de121f0 DDAG |
219 | } |
220 | ||
221 | int fuse_reply_iov(fuse_req_t req, const struct iovec *iov, int count) | |
222 | { | |
7387863d DDAG |
223 | int res; |
224 | struct iovec *padded_iov; | |
2de121f0 | 225 | |
7387863d DDAG |
226 | padded_iov = malloc((count + 1) * sizeof(struct iovec)); |
227 | if (padded_iov == NULL) { | |
228 | return fuse_reply_err(req, ENOMEM); | |
229 | } | |
2de121f0 | 230 | |
7387863d DDAG |
231 | memcpy(padded_iov + 1, iov, count * sizeof(struct iovec)); |
232 | count++; | |
2de121f0 | 233 | |
7387863d DDAG |
234 | res = send_reply_iov(req, 0, padded_iov, count); |
235 | free(padded_iov); | |
2de121f0 | 236 | |
7387863d | 237 | return res; |
2de121f0 DDAG |
238 | } |
239 | ||
240 | ||
7387863d DDAG |
241 | /* |
242 | * 'buf` is allowed to be empty so that the proper size may be | |
243 | * allocated by the caller | |
244 | */ | |
2de121f0 | 245 | size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, |
7387863d | 246 | const char *name, const struct stat *stbuf, off_t off) |
2de121f0 | 247 | { |
7387863d DDAG |
248 | (void)req; |
249 | size_t namelen; | |
250 | size_t entlen; | |
251 | size_t entlen_padded; | |
252 | struct fuse_dirent *dirent; | |
2de121f0 | 253 | |
7387863d DDAG |
254 | namelen = strlen(name); |
255 | entlen = FUSE_NAME_OFFSET + namelen; | |
256 | entlen_padded = FUSE_DIRENT_ALIGN(entlen); | |
2de121f0 | 257 | |
7387863d DDAG |
258 | if ((buf == NULL) || (entlen_padded > bufsize)) { |
259 | return entlen_padded; | |
260 | } | |
2de121f0 | 261 | |
7387863d DDAG |
262 | dirent = (struct fuse_dirent *)buf; |
263 | dirent->ino = stbuf->st_ino; | |
264 | dirent->off = off; | |
265 | dirent->namelen = namelen; | |
266 | dirent->type = (stbuf->st_mode & S_IFMT) >> 12; | |
267 | memcpy(dirent->name, name, namelen); | |
268 | memset(dirent->name + namelen, 0, entlen_padded - entlen); | |
2de121f0 | 269 | |
7387863d | 270 | return entlen_padded; |
2de121f0 DDAG |
271 | } |
272 | ||
273 | static void convert_statfs(const struct statvfs *stbuf, | |
7387863d | 274 | struct fuse_kstatfs *kstatfs) |
2de121f0 | 275 | { |
7387863d DDAG |
276 | kstatfs->bsize = stbuf->f_bsize; |
277 | kstatfs->frsize = stbuf->f_frsize; | |
278 | kstatfs->blocks = stbuf->f_blocks; | |
279 | kstatfs->bfree = stbuf->f_bfree; | |
280 | kstatfs->bavail = stbuf->f_bavail; | |
281 | kstatfs->files = stbuf->f_files; | |
282 | kstatfs->ffree = stbuf->f_ffree; | |
283 | kstatfs->namelen = stbuf->f_namemax; | |
2de121f0 DDAG |
284 | } |
285 | ||
286 | static int send_reply_ok(fuse_req_t req, const void *arg, size_t argsize) | |
287 | { | |
7387863d | 288 | return send_reply(req, 0, arg, argsize); |
2de121f0 DDAG |
289 | } |
290 | ||
291 | int fuse_reply_err(fuse_req_t req, int err) | |
292 | { | |
7387863d | 293 | return send_reply(req, -err, NULL, 0); |
2de121f0 DDAG |
294 | } |
295 | ||
296 | void fuse_reply_none(fuse_req_t req) | |
297 | { | |
7387863d | 298 | fuse_free_req(req); |
2de121f0 DDAG |
299 | } |
300 | ||
301 | static unsigned long calc_timeout_sec(double t) | |
302 | { | |
7387863d DDAG |
303 | if (t > (double)ULONG_MAX) { |
304 | return ULONG_MAX; | |
305 | } else if (t < 0.0) { | |
306 | return 0; | |
307 | } else { | |
308 | return (unsigned long)t; | |
309 | } | |
2de121f0 DDAG |
310 | } |
311 | ||
312 | static unsigned int calc_timeout_nsec(double t) | |
313 | { | |
7387863d DDAG |
314 | double f = t - (double)calc_timeout_sec(t); |
315 | if (f < 0.0) { | |
316 | return 0; | |
317 | } else if (f >= 0.999999999) { | |
318 | return 999999999; | |
319 | } else { | |
320 | return (unsigned int)(f * 1.0e9); | |
321 | } | |
2de121f0 DDAG |
322 | } |
323 | ||
324 | static void fill_entry(struct fuse_entry_out *arg, | |
7387863d | 325 | const struct fuse_entry_param *e) |
2de121f0 | 326 | { |
7387863d DDAG |
327 | arg->nodeid = e->ino; |
328 | arg->generation = e->generation; | |
329 | arg->entry_valid = calc_timeout_sec(e->entry_timeout); | |
330 | arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout); | |
331 | arg->attr_valid = calc_timeout_sec(e->attr_timeout); | |
332 | arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout); | |
333 | convert_stat(&e->attr, &arg->attr); | |
2de121f0 DDAG |
334 | } |
335 | ||
7387863d DDAG |
336 | /* |
337 | * `buf` is allowed to be empty so that the proper size may be | |
338 | * allocated by the caller | |
339 | */ | |
2de121f0 | 340 | size_t fuse_add_direntry_plus(fuse_req_t req, char *buf, size_t bufsize, |
7387863d DDAG |
341 | const char *name, |
342 | const struct fuse_entry_param *e, off_t off) | |
343 | { | |
344 | (void)req; | |
345 | size_t namelen; | |
346 | size_t entlen; | |
347 | size_t entlen_padded; | |
348 | ||
349 | namelen = strlen(name); | |
350 | entlen = FUSE_NAME_OFFSET_DIRENTPLUS + namelen; | |
351 | entlen_padded = FUSE_DIRENT_ALIGN(entlen); | |
352 | if ((buf == NULL) || (entlen_padded > bufsize)) { | |
353 | return entlen_padded; | |
354 | } | |
355 | ||
356 | struct fuse_direntplus *dp = (struct fuse_direntplus *)buf; | |
357 | memset(&dp->entry_out, 0, sizeof(dp->entry_out)); | |
358 | fill_entry(&dp->entry_out, e); | |
359 | ||
360 | struct fuse_dirent *dirent = &dp->dirent; | |
361 | dirent->ino = e->attr.st_ino; | |
362 | dirent->off = off; | |
363 | dirent->namelen = namelen; | |
364 | dirent->type = (e->attr.st_mode & S_IFMT) >> 12; | |
365 | memcpy(dirent->name, name, namelen); | |
366 | memset(dirent->name + namelen, 0, entlen_padded - entlen); | |
367 | ||
368 | return entlen_padded; | |
369 | } | |
370 | ||
371 | static void fill_open(struct fuse_open_out *arg, const struct fuse_file_info *f) | |
372 | { | |
373 | arg->fh = f->fh; | |
374 | if (f->direct_io) { | |
375 | arg->open_flags |= FOPEN_DIRECT_IO; | |
376 | } | |
377 | if (f->keep_cache) { | |
378 | arg->open_flags |= FOPEN_KEEP_CACHE; | |
379 | } | |
380 | if (f->cache_readdir) { | |
381 | arg->open_flags |= FOPEN_CACHE_DIR; | |
382 | } | |
383 | if (f->nonseekable) { | |
384 | arg->open_flags |= FOPEN_NONSEEKABLE; | |
385 | } | |
2de121f0 DDAG |
386 | } |
387 | ||
388 | int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e) | |
389 | { | |
7387863d | 390 | struct fuse_entry_out arg; |
72c42e2d | 391 | size_t size = sizeof(arg); |
2de121f0 | 392 | |
7387863d DDAG |
393 | memset(&arg, 0, sizeof(arg)); |
394 | fill_entry(&arg, e); | |
395 | return send_reply_ok(req, &arg, size); | |
2de121f0 DDAG |
396 | } |
397 | ||
398 | int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, | |
7387863d | 399 | const struct fuse_file_info *f) |
2de121f0 | 400 | { |
7387863d | 401 | char buf[sizeof(struct fuse_entry_out) + sizeof(struct fuse_open_out)]; |
72c42e2d | 402 | size_t entrysize = sizeof(struct fuse_entry_out); |
7387863d DDAG |
403 | struct fuse_entry_out *earg = (struct fuse_entry_out *)buf; |
404 | struct fuse_open_out *oarg = (struct fuse_open_out *)(buf + entrysize); | |
2de121f0 | 405 | |
7387863d DDAG |
406 | memset(buf, 0, sizeof(buf)); |
407 | fill_entry(earg, e); | |
408 | fill_open(oarg, f); | |
409 | return send_reply_ok(req, buf, entrysize + sizeof(struct fuse_open_out)); | |
2de121f0 DDAG |
410 | } |
411 | ||
412 | int fuse_reply_attr(fuse_req_t req, const struct stat *attr, | |
7387863d | 413 | double attr_timeout) |
2de121f0 | 414 | { |
7387863d | 415 | struct fuse_attr_out arg; |
72c42e2d | 416 | size_t size = sizeof(arg); |
2de121f0 | 417 | |
7387863d DDAG |
418 | memset(&arg, 0, sizeof(arg)); |
419 | arg.attr_valid = calc_timeout_sec(attr_timeout); | |
420 | arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout); | |
421 | convert_stat(attr, &arg.attr); | |
2de121f0 | 422 | |
7387863d | 423 | return send_reply_ok(req, &arg, size); |
2de121f0 DDAG |
424 | } |
425 | ||
426 | int fuse_reply_readlink(fuse_req_t req, const char *linkname) | |
427 | { | |
7387863d | 428 | return send_reply_ok(req, linkname, strlen(linkname)); |
2de121f0 DDAG |
429 | } |
430 | ||
431 | int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f) | |
432 | { | |
7387863d | 433 | struct fuse_open_out arg; |
2de121f0 | 434 | |
7387863d DDAG |
435 | memset(&arg, 0, sizeof(arg)); |
436 | fill_open(&arg, f); | |
437 | return send_reply_ok(req, &arg, sizeof(arg)); | |
2de121f0 DDAG |
438 | } |
439 | ||
440 | int fuse_reply_write(fuse_req_t req, size_t count) | |
441 | { | |
7387863d | 442 | struct fuse_write_out arg; |
2de121f0 | 443 | |
7387863d DDAG |
444 | memset(&arg, 0, sizeof(arg)); |
445 | arg.size = count; | |
2de121f0 | 446 | |
7387863d | 447 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
448 | } |
449 | ||
450 | int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size) | |
451 | { | |
7387863d | 452 | return send_reply_ok(req, buf, size); |
2de121f0 DDAG |
453 | } |
454 | ||
455 | static int fuse_send_data_iov_fallback(struct fuse_session *se, | |
7387863d DDAG |
456 | struct fuse_chan *ch, struct iovec *iov, |
457 | int iov_count, struct fuse_bufvec *buf, | |
458 | size_t len) | |
2de121f0 | 459 | { |
7387863d DDAG |
460 | /* Optimize common case */ |
461 | if (buf->count == 1 && buf->idx == 0 && buf->off == 0 && | |
462 | !(buf->buf[0].flags & FUSE_BUF_IS_FD)) { | |
463 | /* | |
464 | * FIXME: also avoid memory copy if there are multiple buffers | |
465 | * but none of them contain an fd | |
466 | */ | |
2de121f0 | 467 | |
7387863d DDAG |
468 | iov[iov_count].iov_base = buf->buf[0].mem; |
469 | iov[iov_count].iov_len = len; | |
470 | iov_count++; | |
471 | return fuse_send_msg(se, ch, iov, iov_count); | |
472 | } | |
2de121f0 | 473 | |
7387863d DDAG |
474 | abort(); /* Will have taken vhost path */ |
475 | return 0; | |
2de121f0 DDAG |
476 | } |
477 | ||
2de121f0 | 478 | static int fuse_send_data_iov(struct fuse_session *se, struct fuse_chan *ch, |
7387863d | 479 | struct iovec *iov, int iov_count, |
8c3fe75e | 480 | struct fuse_bufvec *buf) |
2de121f0 | 481 | { |
7387863d | 482 | size_t len = fuse_buf_size(buf); |
2de121f0 | 483 | |
7387863d | 484 | return fuse_send_data_iov_fallback(se, ch, iov, iov_count, buf, len); |
2de121f0 | 485 | } |
2de121f0 | 486 | |
8c3fe75e | 487 | int fuse_reply_data(fuse_req_t req, struct fuse_bufvec *bufv) |
2de121f0 | 488 | { |
7387863d DDAG |
489 | struct iovec iov[2]; |
490 | struct fuse_out_header out; | |
491 | int res; | |
2de121f0 | 492 | |
7387863d DDAG |
493 | iov[0].iov_base = &out; |
494 | iov[0].iov_len = sizeof(struct fuse_out_header); | |
2de121f0 | 495 | |
7387863d DDAG |
496 | out.unique = req->unique; |
497 | out.error = 0; | |
2de121f0 | 498 | |
8c3fe75e | 499 | res = fuse_send_data_iov(req->se, req->ch, iov, 1, bufv); |
7387863d DDAG |
500 | if (res <= 0) { |
501 | fuse_free_req(req); | |
502 | return res; | |
503 | } else { | |
504 | return fuse_reply_err(req, res); | |
505 | } | |
2de121f0 DDAG |
506 | } |
507 | ||
508 | int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf) | |
509 | { | |
7387863d | 510 | struct fuse_statfs_out arg; |
72c42e2d | 511 | size_t size = sizeof(arg); |
2de121f0 | 512 | |
7387863d DDAG |
513 | memset(&arg, 0, sizeof(arg)); |
514 | convert_statfs(stbuf, &arg.st); | |
2de121f0 | 515 | |
7387863d | 516 | return send_reply_ok(req, &arg, size); |
2de121f0 DDAG |
517 | } |
518 | ||
519 | int fuse_reply_xattr(fuse_req_t req, size_t count) | |
520 | { | |
7387863d | 521 | struct fuse_getxattr_out arg; |
2de121f0 | 522 | |
7387863d DDAG |
523 | memset(&arg, 0, sizeof(arg)); |
524 | arg.size = count; | |
2de121f0 | 525 | |
7387863d | 526 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
527 | } |
528 | ||
529 | int fuse_reply_lock(fuse_req_t req, const struct flock *lock) | |
530 | { | |
7387863d | 531 | struct fuse_lk_out arg; |
2de121f0 | 532 | |
7387863d DDAG |
533 | memset(&arg, 0, sizeof(arg)); |
534 | arg.lk.type = lock->l_type; | |
535 | if (lock->l_type != F_UNLCK) { | |
536 | arg.lk.start = lock->l_start; | |
537 | if (lock->l_len == 0) { | |
538 | arg.lk.end = OFFSET_MAX; | |
539 | } else { | |
540 | arg.lk.end = lock->l_start + lock->l_len - 1; | |
541 | } | |
542 | } | |
543 | arg.lk.pid = lock->l_pid; | |
544 | return send_reply_ok(req, &arg, sizeof(arg)); | |
2de121f0 DDAG |
545 | } |
546 | ||
547 | int fuse_reply_bmap(fuse_req_t req, uint64_t idx) | |
548 | { | |
7387863d | 549 | struct fuse_bmap_out arg; |
2de121f0 | 550 | |
7387863d DDAG |
551 | memset(&arg, 0, sizeof(arg)); |
552 | arg.block = idx; | |
2de121f0 | 553 | |
7387863d | 554 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
555 | } |
556 | ||
557 | static struct fuse_ioctl_iovec *fuse_ioctl_iovec_copy(const struct iovec *iov, | |
7387863d DDAG |
558 | size_t count) |
559 | { | |
560 | struct fuse_ioctl_iovec *fiov; | |
561 | size_t i; | |
562 | ||
563 | fiov = malloc(sizeof(fiov[0]) * count); | |
564 | if (!fiov) { | |
565 | return NULL; | |
566 | } | |
567 | ||
568 | for (i = 0; i < count; i++) { | |
569 | fiov[i].base = (uintptr_t)iov[i].iov_base; | |
570 | fiov[i].len = iov[i].iov_len; | |
571 | } | |
572 | ||
573 | return fiov; | |
574 | } | |
575 | ||
576 | int fuse_reply_ioctl_retry(fuse_req_t req, const struct iovec *in_iov, | |
577 | size_t in_count, const struct iovec *out_iov, | |
578 | size_t out_count) | |
579 | { | |
580 | struct fuse_ioctl_out arg; | |
581 | struct fuse_ioctl_iovec *in_fiov = NULL; | |
582 | struct fuse_ioctl_iovec *out_fiov = NULL; | |
583 | struct iovec iov[4]; | |
584 | size_t count = 1; | |
585 | int res; | |
586 | ||
587 | memset(&arg, 0, sizeof(arg)); | |
588 | arg.flags |= FUSE_IOCTL_RETRY; | |
589 | arg.in_iovs = in_count; | |
590 | arg.out_iovs = out_count; | |
591 | iov[count].iov_base = &arg; | |
592 | iov[count].iov_len = sizeof(arg); | |
593 | count++; | |
594 | ||
72c42e2d DDAG |
595 | /* Can't handle non-compat 64bit ioctls on 32bit */ |
596 | if (sizeof(void *) == 4 && req->ioctl_64bit) { | |
597 | res = fuse_reply_err(req, EINVAL); | |
598 | goto out; | |
599 | } | |
7387863d | 600 | |
72c42e2d DDAG |
601 | if (in_count) { |
602 | in_fiov = fuse_ioctl_iovec_copy(in_iov, in_count); | |
603 | if (!in_fiov) { | |
604 | goto enomem; | |
7387863d | 605 | } |
7387863d | 606 | |
72c42e2d DDAG |
607 | iov[count].iov_base = (void *)in_fiov; |
608 | iov[count].iov_len = sizeof(in_fiov[0]) * in_count; | |
609 | count++; | |
610 | } | |
611 | if (out_count) { | |
612 | out_fiov = fuse_ioctl_iovec_copy(out_iov, out_count); | |
613 | if (!out_fiov) { | |
614 | goto enomem; | |
7387863d | 615 | } |
7387863d | 616 | |
72c42e2d DDAG |
617 | iov[count].iov_base = (void *)out_fiov; |
618 | iov[count].iov_len = sizeof(out_fiov[0]) * out_count; | |
619 | count++; | |
7387863d DDAG |
620 | } |
621 | ||
622 | res = send_reply_iov(req, 0, iov, count); | |
2de121f0 | 623 | out: |
7387863d DDAG |
624 | free(in_fiov); |
625 | free(out_fiov); | |
2de121f0 | 626 | |
7387863d | 627 | return res; |
2de121f0 DDAG |
628 | |
629 | enomem: | |
7387863d DDAG |
630 | res = fuse_reply_err(req, ENOMEM); |
631 | goto out; | |
2de121f0 DDAG |
632 | } |
633 | ||
634 | int fuse_reply_ioctl(fuse_req_t req, int result, const void *buf, size_t size) | |
635 | { | |
7387863d DDAG |
636 | struct fuse_ioctl_out arg; |
637 | struct iovec iov[3]; | |
638 | size_t count = 1; | |
2de121f0 | 639 | |
7387863d DDAG |
640 | memset(&arg, 0, sizeof(arg)); |
641 | arg.result = result; | |
642 | iov[count].iov_base = &arg; | |
643 | iov[count].iov_len = sizeof(arg); | |
644 | count++; | |
2de121f0 | 645 | |
7387863d DDAG |
646 | if (size) { |
647 | iov[count].iov_base = (char *)buf; | |
648 | iov[count].iov_len = size; | |
649 | count++; | |
650 | } | |
2de121f0 | 651 | |
7387863d | 652 | return send_reply_iov(req, 0, iov, count); |
2de121f0 DDAG |
653 | } |
654 | ||
655 | int fuse_reply_ioctl_iov(fuse_req_t req, int result, const struct iovec *iov, | |
7387863d | 656 | int count) |
2de121f0 | 657 | { |
7387863d DDAG |
658 | struct iovec *padded_iov; |
659 | struct fuse_ioctl_out arg; | |
660 | int res; | |
2de121f0 | 661 | |
7387863d DDAG |
662 | padded_iov = malloc((count + 2) * sizeof(struct iovec)); |
663 | if (padded_iov == NULL) { | |
664 | return fuse_reply_err(req, ENOMEM); | |
665 | } | |
2de121f0 | 666 | |
7387863d DDAG |
667 | memset(&arg, 0, sizeof(arg)); |
668 | arg.result = result; | |
669 | padded_iov[1].iov_base = &arg; | |
670 | padded_iov[1].iov_len = sizeof(arg); | |
2de121f0 | 671 | |
7387863d | 672 | memcpy(&padded_iov[2], iov, count * sizeof(struct iovec)); |
2de121f0 | 673 | |
7387863d DDAG |
674 | res = send_reply_iov(req, 0, padded_iov, count + 2); |
675 | free(padded_iov); | |
2de121f0 | 676 | |
7387863d | 677 | return res; |
2de121f0 DDAG |
678 | } |
679 | ||
680 | int fuse_reply_poll(fuse_req_t req, unsigned revents) | |
681 | { | |
7387863d | 682 | struct fuse_poll_out arg; |
2de121f0 | 683 | |
7387863d DDAG |
684 | memset(&arg, 0, sizeof(arg)); |
685 | arg.revents = revents; | |
2de121f0 | 686 | |
7387863d | 687 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
688 | } |
689 | ||
690 | int fuse_reply_lseek(fuse_req_t req, off_t off) | |
691 | { | |
7387863d | 692 | struct fuse_lseek_out arg; |
2de121f0 | 693 | |
7387863d DDAG |
694 | memset(&arg, 0, sizeof(arg)); |
695 | arg.offset = off; | |
2de121f0 | 696 | |
7387863d | 697 | return send_reply_ok(req, &arg, sizeof(arg)); |
2de121f0 DDAG |
698 | } |
699 | ||
700 | static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
701 | { | |
7387863d | 702 | char *name = (char *)inarg; |
2de121f0 | 703 | |
7387863d DDAG |
704 | if (req->se->op.lookup) { |
705 | req->se->op.lookup(req, nodeid, name); | |
706 | } else { | |
707 | fuse_reply_err(req, ENOSYS); | |
708 | } | |
2de121f0 DDAG |
709 | } |
710 | ||
711 | static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
712 | { | |
7387863d | 713 | struct fuse_forget_in *arg = (struct fuse_forget_in *)inarg; |
2de121f0 | 714 | |
7387863d DDAG |
715 | if (req->se->op.forget) { |
716 | req->se->op.forget(req, nodeid, arg->nlookup); | |
717 | } else { | |
718 | fuse_reply_none(req); | |
719 | } | |
2de121f0 DDAG |
720 | } |
721 | ||
722 | static void do_batch_forget(fuse_req_t req, fuse_ino_t nodeid, | |
7387863d | 723 | const void *inarg) |
2de121f0 | 724 | { |
7387863d DDAG |
725 | struct fuse_batch_forget_in *arg = (void *)inarg; |
726 | struct fuse_forget_one *param = (void *)PARAM(arg); | |
727 | unsigned int i; | |
2de121f0 | 728 | |
7387863d | 729 | (void)nodeid; |
2de121f0 | 730 | |
7387863d DDAG |
731 | if (req->se->op.forget_multi) { |
732 | req->se->op.forget_multi(req, arg->count, | |
733 | (struct fuse_forget_data *)param); | |
734 | } else if (req->se->op.forget) { | |
735 | for (i = 0; i < arg->count; i++) { | |
736 | struct fuse_forget_one *forget = ¶m[i]; | |
737 | struct fuse_req *dummy_req; | |
2de121f0 | 738 | |
7387863d DDAG |
739 | dummy_req = fuse_ll_alloc_req(req->se); |
740 | if (dummy_req == NULL) { | |
741 | break; | |
742 | } | |
2de121f0 | 743 | |
7387863d DDAG |
744 | dummy_req->unique = req->unique; |
745 | dummy_req->ctx = req->ctx; | |
746 | dummy_req->ch = NULL; | |
2de121f0 | 747 | |
7387863d DDAG |
748 | req->se->op.forget(dummy_req, forget->nodeid, forget->nlookup); |
749 | } | |
750 | fuse_reply_none(req); | |
751 | } else { | |
752 | fuse_reply_none(req); | |
753 | } | |
2de121f0 DDAG |
754 | } |
755 | ||
756 | static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
757 | { | |
7387863d DDAG |
758 | struct fuse_file_info *fip = NULL; |
759 | struct fuse_file_info fi; | |
2de121f0 | 760 | |
72c42e2d | 761 | struct fuse_getattr_in *arg = (struct fuse_getattr_in *)inarg; |
2de121f0 | 762 | |
72c42e2d DDAG |
763 | if (arg->getattr_flags & FUSE_GETATTR_FH) { |
764 | memset(&fi, 0, sizeof(fi)); | |
765 | fi.fh = arg->fh; | |
766 | fip = &fi; | |
7387863d | 767 | } |
2de121f0 | 768 | |
7387863d DDAG |
769 | if (req->se->op.getattr) { |
770 | req->se->op.getattr(req, nodeid, fip); | |
771 | } else { | |
772 | fuse_reply_err(req, ENOSYS); | |
773 | } | |
2de121f0 DDAG |
774 | } |
775 | ||
776 | static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
777 | { | |
7387863d DDAG |
778 | struct fuse_setattr_in *arg = (struct fuse_setattr_in *)inarg; |
779 | ||
780 | if (req->se->op.setattr) { | |
781 | struct fuse_file_info *fi = NULL; | |
782 | struct fuse_file_info fi_store; | |
783 | struct stat stbuf; | |
784 | memset(&stbuf, 0, sizeof(stbuf)); | |
785 | convert_attr(arg, &stbuf); | |
786 | if (arg->valid & FATTR_FH) { | |
787 | arg->valid &= ~FATTR_FH; | |
788 | memset(&fi_store, 0, sizeof(fi_store)); | |
789 | fi = &fi_store; | |
790 | fi->fh = arg->fh; | |
791 | } | |
792 | arg->valid &= FUSE_SET_ATTR_MODE | FUSE_SET_ATTR_UID | | |
793 | FUSE_SET_ATTR_GID | FUSE_SET_ATTR_SIZE | | |
794 | FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME | | |
795 | FUSE_SET_ATTR_ATIME_NOW | FUSE_SET_ATTR_MTIME_NOW | | |
796 | FUSE_SET_ATTR_CTIME; | |
797 | ||
798 | req->se->op.setattr(req, nodeid, &stbuf, arg->valid, fi); | |
799 | } else { | |
800 | fuse_reply_err(req, ENOSYS); | |
801 | } | |
2de121f0 DDAG |
802 | } |
803 | ||
804 | static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
805 | { | |
7387863d | 806 | struct fuse_access_in *arg = (struct fuse_access_in *)inarg; |
2de121f0 | 807 | |
7387863d DDAG |
808 | if (req->se->op.access) { |
809 | req->se->op.access(req, nodeid, arg->mask); | |
810 | } else { | |
811 | fuse_reply_err(req, ENOSYS); | |
812 | } | |
2de121f0 DDAG |
813 | } |
814 | ||
815 | static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
816 | { | |
7387863d | 817 | (void)inarg; |
2de121f0 | 818 | |
7387863d DDAG |
819 | if (req->se->op.readlink) { |
820 | req->se->op.readlink(req, nodeid); | |
821 | } else { | |
822 | fuse_reply_err(req, ENOSYS); | |
823 | } | |
2de121f0 DDAG |
824 | } |
825 | ||
826 | static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
827 | { | |
7387863d DDAG |
828 | struct fuse_mknod_in *arg = (struct fuse_mknod_in *)inarg; |
829 | char *name = PARAM(arg); | |
2de121f0 | 830 | |
72c42e2d | 831 | req->ctx.umask = arg->umask; |
2de121f0 | 832 | |
7387863d DDAG |
833 | if (req->se->op.mknod) { |
834 | req->se->op.mknod(req, nodeid, name, arg->mode, arg->rdev); | |
835 | } else { | |
836 | fuse_reply_err(req, ENOSYS); | |
837 | } | |
2de121f0 DDAG |
838 | } |
839 | ||
840 | static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
841 | { | |
7387863d | 842 | struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *)inarg; |
2de121f0 | 843 | |
72c42e2d | 844 | req->ctx.umask = arg->umask; |
2de121f0 | 845 | |
7387863d DDAG |
846 | if (req->se->op.mkdir) { |
847 | req->se->op.mkdir(req, nodeid, PARAM(arg), arg->mode); | |
848 | } else { | |
849 | fuse_reply_err(req, ENOSYS); | |
850 | } | |
2de121f0 DDAG |
851 | } |
852 | ||
853 | static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
854 | { | |
7387863d | 855 | char *name = (char *)inarg; |
2de121f0 | 856 | |
7387863d DDAG |
857 | if (req->se->op.unlink) { |
858 | req->se->op.unlink(req, nodeid, name); | |
859 | } else { | |
860 | fuse_reply_err(req, ENOSYS); | |
861 | } | |
2de121f0 DDAG |
862 | } |
863 | ||
864 | static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
865 | { | |
7387863d | 866 | char *name = (char *)inarg; |
2de121f0 | 867 | |
7387863d DDAG |
868 | if (req->se->op.rmdir) { |
869 | req->se->op.rmdir(req, nodeid, name); | |
870 | } else { | |
871 | fuse_reply_err(req, ENOSYS); | |
872 | } | |
2de121f0 DDAG |
873 | } |
874 | ||
875 | static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
876 | { | |
7387863d DDAG |
877 | char *name = (char *)inarg; |
878 | char *linkname = ((char *)inarg) + strlen((char *)inarg) + 1; | |
2de121f0 | 879 | |
7387863d DDAG |
880 | if (req->se->op.symlink) { |
881 | req->se->op.symlink(req, linkname, nodeid, name); | |
882 | } else { | |
883 | fuse_reply_err(req, ENOSYS); | |
884 | } | |
2de121f0 DDAG |
885 | } |
886 | ||
887 | static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
888 | { | |
7387863d DDAG |
889 | struct fuse_rename_in *arg = (struct fuse_rename_in *)inarg; |
890 | char *oldname = PARAM(arg); | |
891 | char *newname = oldname + strlen(oldname) + 1; | |
2de121f0 | 892 | |
7387863d DDAG |
893 | if (req->se->op.rename) { |
894 | req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, 0); | |
895 | } else { | |
896 | fuse_reply_err(req, ENOSYS); | |
897 | } | |
2de121f0 DDAG |
898 | } |
899 | ||
900 | static void do_rename2(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
901 | { | |
7387863d DDAG |
902 | struct fuse_rename2_in *arg = (struct fuse_rename2_in *)inarg; |
903 | char *oldname = PARAM(arg); | |
904 | char *newname = oldname + strlen(oldname) + 1; | |
2de121f0 | 905 | |
7387863d DDAG |
906 | if (req->se->op.rename) { |
907 | req->se->op.rename(req, nodeid, oldname, arg->newdir, newname, | |
908 | arg->flags); | |
909 | } else { | |
910 | fuse_reply_err(req, ENOSYS); | |
911 | } | |
2de121f0 DDAG |
912 | } |
913 | ||
914 | static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
915 | { | |
7387863d | 916 | struct fuse_link_in *arg = (struct fuse_link_in *)inarg; |
2de121f0 | 917 | |
7387863d DDAG |
918 | if (req->se->op.link) { |
919 | req->se->op.link(req, arg->oldnodeid, nodeid, PARAM(arg)); | |
920 | } else { | |
921 | fuse_reply_err(req, ENOSYS); | |
922 | } | |
2de121f0 DDAG |
923 | } |
924 | ||
925 | static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
926 | { | |
7387863d | 927 | struct fuse_create_in *arg = (struct fuse_create_in *)inarg; |
2de121f0 | 928 | |
7387863d DDAG |
929 | if (req->se->op.create) { |
930 | struct fuse_file_info fi; | |
931 | char *name = PARAM(arg); | |
2de121f0 | 932 | |
7387863d DDAG |
933 | memset(&fi, 0, sizeof(fi)); |
934 | fi.flags = arg->flags; | |
2de121f0 | 935 | |
72c42e2d | 936 | req->ctx.umask = arg->umask; |
2de121f0 | 937 | |
7387863d DDAG |
938 | req->se->op.create(req, nodeid, name, arg->mode, &fi); |
939 | } else { | |
940 | fuse_reply_err(req, ENOSYS); | |
941 | } | |
2de121f0 DDAG |
942 | } |
943 | ||
944 | static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
945 | { | |
7387863d DDAG |
946 | struct fuse_open_in *arg = (struct fuse_open_in *)inarg; |
947 | struct fuse_file_info fi; | |
2de121f0 | 948 | |
7387863d DDAG |
949 | memset(&fi, 0, sizeof(fi)); |
950 | fi.flags = arg->flags; | |
2de121f0 | 951 | |
7387863d DDAG |
952 | if (req->se->op.open) { |
953 | req->se->op.open(req, nodeid, &fi); | |
954 | } else { | |
955 | fuse_reply_open(req, &fi); | |
956 | } | |
2de121f0 DDAG |
957 | } |
958 | ||
959 | static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
960 | { | |
7387863d | 961 | struct fuse_read_in *arg = (struct fuse_read_in *)inarg; |
2de121f0 | 962 | |
7387863d DDAG |
963 | if (req->se->op.read) { |
964 | struct fuse_file_info fi; | |
2de121f0 | 965 | |
7387863d DDAG |
966 | memset(&fi, 0, sizeof(fi)); |
967 | fi.fh = arg->fh; | |
72c42e2d DDAG |
968 | fi.lock_owner = arg->lock_owner; |
969 | fi.flags = arg->flags; | |
7387863d DDAG |
970 | req->se->op.read(req, nodeid, arg->size, arg->offset, &fi); |
971 | } else { | |
972 | fuse_reply_err(req, ENOSYS); | |
973 | } | |
2de121f0 DDAG |
974 | } |
975 | ||
976 | static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
977 | { | |
7387863d DDAG |
978 | struct fuse_write_in *arg = (struct fuse_write_in *)inarg; |
979 | struct fuse_file_info fi; | |
980 | char *param; | |
2de121f0 | 981 | |
7387863d DDAG |
982 | memset(&fi, 0, sizeof(fi)); |
983 | fi.fh = arg->fh; | |
984 | fi.writepage = (arg->write_flags & FUSE_WRITE_CACHE) != 0; | |
2de121f0 | 985 | |
72c42e2d DDAG |
986 | fi.lock_owner = arg->lock_owner; |
987 | fi.flags = arg->flags; | |
988 | param = PARAM(arg); | |
2de121f0 | 989 | |
7387863d DDAG |
990 | if (req->se->op.write) { |
991 | req->se->op.write(req, nodeid, param, arg->size, arg->offset, &fi); | |
992 | } else { | |
993 | fuse_reply_err(req, ENOSYS); | |
994 | } | |
2de121f0 DDAG |
995 | } |
996 | ||
997 | static void do_write_buf(fuse_req_t req, fuse_ino_t nodeid, const void *inarg, | |
7387863d DDAG |
998 | const struct fuse_buf *ibuf) |
999 | { | |
1000 | struct fuse_session *se = req->se; | |
1001 | struct fuse_bufvec bufv = { | |
1002 | .buf[0] = *ibuf, | |
1003 | .count = 1, | |
1004 | }; | |
1005 | struct fuse_write_in *arg = (struct fuse_write_in *)inarg; | |
1006 | struct fuse_file_info fi; | |
1007 | ||
1008 | memset(&fi, 0, sizeof(fi)); | |
1009 | fi.fh = arg->fh; | |
1010 | fi.writepage = arg->write_flags & FUSE_WRITE_CACHE; | |
1011 | ||
72c42e2d DDAG |
1012 | fi.lock_owner = arg->lock_owner; |
1013 | fi.flags = arg->flags; | |
1014 | if (!(bufv.buf[0].flags & FUSE_BUF_IS_FD)) { | |
1015 | bufv.buf[0].mem = PARAM(arg); | |
7387863d | 1016 | } |
72c42e2d DDAG |
1017 | |
1018 | bufv.buf[0].size -= | |
1019 | sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in); | |
7387863d DDAG |
1020 | if (bufv.buf[0].size < arg->size) { |
1021 | fuse_log(FUSE_LOG_ERR, "fuse: do_write_buf: buffer size too small\n"); | |
1022 | fuse_reply_err(req, EIO); | |
1023 | return; | |
1024 | } | |
1025 | bufv.buf[0].size = arg->size; | |
1026 | ||
1027 | se->op.write_buf(req, nodeid, &bufv, arg->offset, &fi); | |
2de121f0 DDAG |
1028 | } |
1029 | ||
1030 | static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1031 | { | |
7387863d DDAG |
1032 | struct fuse_flush_in *arg = (struct fuse_flush_in *)inarg; |
1033 | struct fuse_file_info fi; | |
2de121f0 | 1034 | |
7387863d DDAG |
1035 | memset(&fi, 0, sizeof(fi)); |
1036 | fi.fh = arg->fh; | |
1037 | fi.flush = 1; | |
72c42e2d | 1038 | fi.lock_owner = arg->lock_owner; |
2de121f0 | 1039 | |
7387863d DDAG |
1040 | if (req->se->op.flush) { |
1041 | req->se->op.flush(req, nodeid, &fi); | |
1042 | } else { | |
1043 | fuse_reply_err(req, ENOSYS); | |
1044 | } | |
2de121f0 DDAG |
1045 | } |
1046 | ||
1047 | static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1048 | { | |
7387863d DDAG |
1049 | struct fuse_release_in *arg = (struct fuse_release_in *)inarg; |
1050 | struct fuse_file_info fi; | |
2de121f0 | 1051 | |
7387863d DDAG |
1052 | memset(&fi, 0, sizeof(fi)); |
1053 | fi.flags = arg->flags; | |
1054 | fi.fh = arg->fh; | |
72c42e2d DDAG |
1055 | fi.flush = (arg->release_flags & FUSE_RELEASE_FLUSH) ? 1 : 0; |
1056 | fi.lock_owner = arg->lock_owner; | |
7387863d DDAG |
1057 | if (arg->release_flags & FUSE_RELEASE_FLOCK_UNLOCK) { |
1058 | fi.flock_release = 1; | |
1059 | fi.lock_owner = arg->lock_owner; | |
1060 | } | |
2de121f0 | 1061 | |
7387863d DDAG |
1062 | if (req->se->op.release) { |
1063 | req->se->op.release(req, nodeid, &fi); | |
1064 | } else { | |
1065 | fuse_reply_err(req, 0); | |
1066 | } | |
2de121f0 DDAG |
1067 | } |
1068 | ||
1069 | static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1070 | { | |
7387863d DDAG |
1071 | struct fuse_fsync_in *arg = (struct fuse_fsync_in *)inarg; |
1072 | struct fuse_file_info fi; | |
1073 | int datasync = arg->fsync_flags & 1; | |
2de121f0 | 1074 | |
7387863d DDAG |
1075 | memset(&fi, 0, sizeof(fi)); |
1076 | fi.fh = arg->fh; | |
2de121f0 | 1077 | |
7387863d | 1078 | if (req->se->op.fsync) { |
1b209805 VG |
1079 | if (fi.fh == (uint64_t)-1) { |
1080 | req->se->op.fsync(req, nodeid, datasync, NULL); | |
1081 | } else { | |
1082 | req->se->op.fsync(req, nodeid, datasync, &fi); | |
1083 | } | |
7387863d DDAG |
1084 | } else { |
1085 | fuse_reply_err(req, ENOSYS); | |
1086 | } | |
2de121f0 DDAG |
1087 | } |
1088 | ||
1089 | static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1090 | { | |
7387863d DDAG |
1091 | struct fuse_open_in *arg = (struct fuse_open_in *)inarg; |
1092 | struct fuse_file_info fi; | |
2de121f0 | 1093 | |
7387863d DDAG |
1094 | memset(&fi, 0, sizeof(fi)); |
1095 | fi.flags = arg->flags; | |
2de121f0 | 1096 | |
7387863d DDAG |
1097 | if (req->se->op.opendir) { |
1098 | req->se->op.opendir(req, nodeid, &fi); | |
1099 | } else { | |
1100 | fuse_reply_open(req, &fi); | |
1101 | } | |
2de121f0 DDAG |
1102 | } |
1103 | ||
1104 | static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1105 | { | |
7387863d DDAG |
1106 | struct fuse_read_in *arg = (struct fuse_read_in *)inarg; |
1107 | struct fuse_file_info fi; | |
2de121f0 | 1108 | |
7387863d DDAG |
1109 | memset(&fi, 0, sizeof(fi)); |
1110 | fi.fh = arg->fh; | |
2de121f0 | 1111 | |
7387863d DDAG |
1112 | if (req->se->op.readdir) { |
1113 | req->se->op.readdir(req, nodeid, arg->size, arg->offset, &fi); | |
1114 | } else { | |
1115 | fuse_reply_err(req, ENOSYS); | |
1116 | } | |
2de121f0 DDAG |
1117 | } |
1118 | ||
1119 | static void do_readdirplus(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1120 | { | |
7387863d DDAG |
1121 | struct fuse_read_in *arg = (struct fuse_read_in *)inarg; |
1122 | struct fuse_file_info fi; | |
2de121f0 | 1123 | |
7387863d DDAG |
1124 | memset(&fi, 0, sizeof(fi)); |
1125 | fi.fh = arg->fh; | |
2de121f0 | 1126 | |
7387863d DDAG |
1127 | if (req->se->op.readdirplus) { |
1128 | req->se->op.readdirplus(req, nodeid, arg->size, arg->offset, &fi); | |
1129 | } else { | |
1130 | fuse_reply_err(req, ENOSYS); | |
1131 | } | |
2de121f0 DDAG |
1132 | } |
1133 | ||
1134 | static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1135 | { | |
7387863d DDAG |
1136 | struct fuse_release_in *arg = (struct fuse_release_in *)inarg; |
1137 | struct fuse_file_info fi; | |
2de121f0 | 1138 | |
7387863d DDAG |
1139 | memset(&fi, 0, sizeof(fi)); |
1140 | fi.flags = arg->flags; | |
1141 | fi.fh = arg->fh; | |
2de121f0 | 1142 | |
7387863d DDAG |
1143 | if (req->se->op.releasedir) { |
1144 | req->se->op.releasedir(req, nodeid, &fi); | |
1145 | } else { | |
1146 | fuse_reply_err(req, 0); | |
1147 | } | |
2de121f0 DDAG |
1148 | } |
1149 | ||
1150 | static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1151 | { | |
7387863d DDAG |
1152 | struct fuse_fsync_in *arg = (struct fuse_fsync_in *)inarg; |
1153 | struct fuse_file_info fi; | |
1154 | int datasync = arg->fsync_flags & 1; | |
2de121f0 | 1155 | |
7387863d DDAG |
1156 | memset(&fi, 0, sizeof(fi)); |
1157 | fi.fh = arg->fh; | |
2de121f0 | 1158 | |
7387863d DDAG |
1159 | if (req->se->op.fsyncdir) { |
1160 | req->se->op.fsyncdir(req, nodeid, datasync, &fi); | |
1161 | } else { | |
1162 | fuse_reply_err(req, ENOSYS); | |
1163 | } | |
2de121f0 DDAG |
1164 | } |
1165 | ||
1166 | static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1167 | { | |
7387863d DDAG |
1168 | (void)nodeid; |
1169 | (void)inarg; | |
2de121f0 | 1170 | |
7387863d DDAG |
1171 | if (req->se->op.statfs) { |
1172 | req->se->op.statfs(req, nodeid); | |
1173 | } else { | |
1174 | struct statvfs buf = { | |
1175 | .f_namemax = 255, | |
1176 | .f_bsize = 512, | |
1177 | }; | |
1178 | fuse_reply_statfs(req, &buf); | |
1179 | } | |
2de121f0 DDAG |
1180 | } |
1181 | ||
1182 | static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1183 | { | |
7387863d DDAG |
1184 | struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *)inarg; |
1185 | char *name = PARAM(arg); | |
1186 | char *value = name + strlen(name) + 1; | |
2de121f0 | 1187 | |
7387863d DDAG |
1188 | if (req->se->op.setxattr) { |
1189 | req->se->op.setxattr(req, nodeid, name, value, arg->size, arg->flags); | |
1190 | } else { | |
1191 | fuse_reply_err(req, ENOSYS); | |
1192 | } | |
2de121f0 DDAG |
1193 | } |
1194 | ||
1195 | static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1196 | { | |
7387863d | 1197 | struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *)inarg; |
2de121f0 | 1198 | |
7387863d DDAG |
1199 | if (req->se->op.getxattr) { |
1200 | req->se->op.getxattr(req, nodeid, PARAM(arg), arg->size); | |
1201 | } else { | |
1202 | fuse_reply_err(req, ENOSYS); | |
1203 | } | |
2de121f0 DDAG |
1204 | } |
1205 | ||
1206 | static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1207 | { | |
7387863d | 1208 | struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *)inarg; |
2de121f0 | 1209 | |
7387863d DDAG |
1210 | if (req->se->op.listxattr) { |
1211 | req->se->op.listxattr(req, nodeid, arg->size); | |
1212 | } else { | |
1213 | fuse_reply_err(req, ENOSYS); | |
1214 | } | |
2de121f0 DDAG |
1215 | } |
1216 | ||
1217 | static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1218 | { | |
7387863d | 1219 | char *name = (char *)inarg; |
2de121f0 | 1220 | |
7387863d DDAG |
1221 | if (req->se->op.removexattr) { |
1222 | req->se->op.removexattr(req, nodeid, name); | |
1223 | } else { | |
1224 | fuse_reply_err(req, ENOSYS); | |
1225 | } | |
2de121f0 DDAG |
1226 | } |
1227 | ||
1228 | static void convert_fuse_file_lock(struct fuse_file_lock *fl, | |
7387863d | 1229 | struct flock *flock) |
2de121f0 | 1230 | { |
7387863d DDAG |
1231 | memset(flock, 0, sizeof(struct flock)); |
1232 | flock->l_type = fl->type; | |
1233 | flock->l_whence = SEEK_SET; | |
1234 | flock->l_start = fl->start; | |
1235 | if (fl->end == OFFSET_MAX) { | |
1236 | flock->l_len = 0; | |
1237 | } else { | |
1238 | flock->l_len = fl->end - fl->start + 1; | |
1239 | } | |
1240 | flock->l_pid = fl->pid; | |
2de121f0 DDAG |
1241 | } |
1242 | ||
1243 | static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1244 | { | |
7387863d DDAG |
1245 | struct fuse_lk_in *arg = (struct fuse_lk_in *)inarg; |
1246 | struct fuse_file_info fi; | |
1247 | struct flock flock; | |
2de121f0 | 1248 | |
7387863d DDAG |
1249 | memset(&fi, 0, sizeof(fi)); |
1250 | fi.fh = arg->fh; | |
1251 | fi.lock_owner = arg->owner; | |
2de121f0 | 1252 | |
7387863d DDAG |
1253 | convert_fuse_file_lock(&arg->lk, &flock); |
1254 | if (req->se->op.getlk) { | |
1255 | req->se->op.getlk(req, nodeid, &fi, &flock); | |
1256 | } else { | |
1257 | fuse_reply_err(req, ENOSYS); | |
1258 | } | |
2de121f0 DDAG |
1259 | } |
1260 | ||
1261 | static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid, | |
7387863d DDAG |
1262 | const void *inarg, int sleep) |
1263 | { | |
1264 | struct fuse_lk_in *arg = (struct fuse_lk_in *)inarg; | |
1265 | struct fuse_file_info fi; | |
1266 | struct flock flock; | |
1267 | ||
1268 | memset(&fi, 0, sizeof(fi)); | |
1269 | fi.fh = arg->fh; | |
1270 | fi.lock_owner = arg->owner; | |
1271 | ||
1272 | if (arg->lk_flags & FUSE_LK_FLOCK) { | |
1273 | int op = 0; | |
1274 | ||
1275 | switch (arg->lk.type) { | |
1276 | case F_RDLCK: | |
1277 | op = LOCK_SH; | |
1278 | break; | |
1279 | case F_WRLCK: | |
1280 | op = LOCK_EX; | |
1281 | break; | |
1282 | case F_UNLCK: | |
1283 | op = LOCK_UN; | |
1284 | break; | |
1285 | } | |
1286 | if (!sleep) { | |
1287 | op |= LOCK_NB; | |
1288 | } | |
1289 | ||
1290 | if (req->se->op.flock) { | |
1291 | req->se->op.flock(req, nodeid, &fi, op); | |
1292 | } else { | |
1293 | fuse_reply_err(req, ENOSYS); | |
1294 | } | |
1295 | } else { | |
1296 | convert_fuse_file_lock(&arg->lk, &flock); | |
1297 | if (req->se->op.setlk) { | |
1298 | req->se->op.setlk(req, nodeid, &fi, &flock, sleep); | |
1299 | } else { | |
1300 | fuse_reply_err(req, ENOSYS); | |
1301 | } | |
1302 | } | |
2de121f0 DDAG |
1303 | } |
1304 | ||
1305 | static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1306 | { | |
7387863d | 1307 | do_setlk_common(req, nodeid, inarg, 0); |
2de121f0 DDAG |
1308 | } |
1309 | ||
1310 | static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1311 | { | |
7387863d | 1312 | do_setlk_common(req, nodeid, inarg, 1); |
2de121f0 DDAG |
1313 | } |
1314 | ||
1315 | static int find_interrupted(struct fuse_session *se, struct fuse_req *req) | |
1316 | { | |
7387863d DDAG |
1317 | struct fuse_req *curr; |
1318 | ||
1319 | for (curr = se->list.next; curr != &se->list; curr = curr->next) { | |
1320 | if (curr->unique == req->u.i.unique) { | |
1321 | fuse_interrupt_func_t func; | |
1322 | void *data; | |
1323 | ||
1324 | curr->ctr++; | |
1325 | pthread_mutex_unlock(&se->lock); | |
1326 | ||
1327 | /* Ugh, ugly locking */ | |
1328 | pthread_mutex_lock(&curr->lock); | |
1329 | pthread_mutex_lock(&se->lock); | |
1330 | curr->interrupted = 1; | |
1331 | func = curr->u.ni.func; | |
1332 | data = curr->u.ni.data; | |
1333 | pthread_mutex_unlock(&se->lock); | |
1334 | if (func) { | |
1335 | func(curr, data); | |
1336 | } | |
1337 | pthread_mutex_unlock(&curr->lock); | |
1338 | ||
1339 | pthread_mutex_lock(&se->lock); | |
1340 | curr->ctr--; | |
1341 | if (!curr->ctr) { | |
1342 | destroy_req(curr); | |
1343 | } | |
1344 | ||
1345 | return 1; | |
1346 | } | |
1347 | } | |
1348 | for (curr = se->interrupts.next; curr != &se->interrupts; | |
1349 | curr = curr->next) { | |
1350 | if (curr->u.i.unique == req->u.i.unique) { | |
1351 | return 1; | |
1352 | } | |
1353 | } | |
1354 | return 0; | |
2de121f0 DDAG |
1355 | } |
1356 | ||
1357 | static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1358 | { | |
7387863d DDAG |
1359 | struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *)inarg; |
1360 | struct fuse_session *se = req->se; | |
2de121f0 | 1361 | |
7387863d DDAG |
1362 | (void)nodeid; |
1363 | if (se->debug) { | |
1364 | fuse_log(FUSE_LOG_DEBUG, "INTERRUPT: %llu\n", | |
1365 | (unsigned long long)arg->unique); | |
1366 | } | |
2de121f0 | 1367 | |
7387863d | 1368 | req->u.i.unique = arg->unique; |
2de121f0 | 1369 | |
7387863d DDAG |
1370 | pthread_mutex_lock(&se->lock); |
1371 | if (find_interrupted(se, req)) { | |
1372 | destroy_req(req); | |
1373 | } else { | |
1374 | list_add_req(req, &se->interrupts); | |
1375 | } | |
1376 | pthread_mutex_unlock(&se->lock); | |
2de121f0 DDAG |
1377 | } |
1378 | ||
1379 | static struct fuse_req *check_interrupt(struct fuse_session *se, | |
7387863d DDAG |
1380 | struct fuse_req *req) |
1381 | { | |
1382 | struct fuse_req *curr; | |
1383 | ||
1384 | for (curr = se->interrupts.next; curr != &se->interrupts; | |
1385 | curr = curr->next) { | |
1386 | if (curr->u.i.unique == req->unique) { | |
1387 | req->interrupted = 1; | |
1388 | list_del_req(curr); | |
1389 | free(curr); | |
1390 | return NULL; | |
1391 | } | |
1392 | } | |
1393 | curr = se->interrupts.next; | |
1394 | if (curr != &se->interrupts) { | |
1395 | list_del_req(curr); | |
1396 | list_init_req(curr); | |
1397 | return curr; | |
1398 | } else { | |
1399 | return NULL; | |
1400 | } | |
2de121f0 DDAG |
1401 | } |
1402 | ||
1403 | static void do_bmap(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1404 | { | |
7387863d | 1405 | struct fuse_bmap_in *arg = (struct fuse_bmap_in *)inarg; |
2de121f0 | 1406 | |
7387863d DDAG |
1407 | if (req->se->op.bmap) { |
1408 | req->se->op.bmap(req, nodeid, arg->blocksize, arg->block); | |
1409 | } else { | |
1410 | fuse_reply_err(req, ENOSYS); | |
1411 | } | |
2de121f0 DDAG |
1412 | } |
1413 | ||
1414 | static void do_ioctl(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1415 | { | |
7387863d DDAG |
1416 | struct fuse_ioctl_in *arg = (struct fuse_ioctl_in *)inarg; |
1417 | unsigned int flags = arg->flags; | |
1418 | void *in_buf = arg->in_size ? PARAM(arg) : NULL; | |
1419 | struct fuse_file_info fi; | |
2de121f0 | 1420 | |
7387863d DDAG |
1421 | if (flags & FUSE_IOCTL_DIR && !(req->se->conn.want & FUSE_CAP_IOCTL_DIR)) { |
1422 | fuse_reply_err(req, ENOTTY); | |
1423 | return; | |
1424 | } | |
2de121f0 | 1425 | |
7387863d DDAG |
1426 | memset(&fi, 0, sizeof(fi)); |
1427 | fi.fh = arg->fh; | |
2de121f0 | 1428 | |
72c42e2d | 1429 | if (sizeof(void *) == 4 && !(flags & FUSE_IOCTL_32BIT)) { |
7387863d DDAG |
1430 | req->ioctl_64bit = 1; |
1431 | } | |
2de121f0 | 1432 | |
7387863d DDAG |
1433 | if (req->se->op.ioctl) { |
1434 | req->se->op.ioctl(req, nodeid, arg->cmd, (void *)(uintptr_t)arg->arg, | |
1435 | &fi, flags, in_buf, arg->in_size, arg->out_size); | |
1436 | } else { | |
1437 | fuse_reply_err(req, ENOSYS); | |
1438 | } | |
2de121f0 DDAG |
1439 | } |
1440 | ||
1441 | void fuse_pollhandle_destroy(struct fuse_pollhandle *ph) | |
1442 | { | |
7387863d | 1443 | free(ph); |
2de121f0 DDAG |
1444 | } |
1445 | ||
1446 | static void do_poll(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1447 | { | |
7387863d DDAG |
1448 | struct fuse_poll_in *arg = (struct fuse_poll_in *)inarg; |
1449 | struct fuse_file_info fi; | |
2de121f0 | 1450 | |
7387863d DDAG |
1451 | memset(&fi, 0, sizeof(fi)); |
1452 | fi.fh = arg->fh; | |
1453 | fi.poll_events = arg->events; | |
2de121f0 | 1454 | |
7387863d DDAG |
1455 | if (req->se->op.poll) { |
1456 | struct fuse_pollhandle *ph = NULL; | |
2de121f0 | 1457 | |
7387863d DDAG |
1458 | if (arg->flags & FUSE_POLL_SCHEDULE_NOTIFY) { |
1459 | ph = malloc(sizeof(struct fuse_pollhandle)); | |
1460 | if (ph == NULL) { | |
1461 | fuse_reply_err(req, ENOMEM); | |
1462 | return; | |
1463 | } | |
1464 | ph->kh = arg->kh; | |
1465 | ph->se = req->se; | |
1466 | } | |
2de121f0 | 1467 | |
7387863d DDAG |
1468 | req->se->op.poll(req, nodeid, &fi, ph); |
1469 | } else { | |
1470 | fuse_reply_err(req, ENOSYS); | |
1471 | } | |
2de121f0 DDAG |
1472 | } |
1473 | ||
1474 | static void do_fallocate(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1475 | { | |
7387863d DDAG |
1476 | struct fuse_fallocate_in *arg = (struct fuse_fallocate_in *)inarg; |
1477 | struct fuse_file_info fi; | |
2de121f0 | 1478 | |
7387863d DDAG |
1479 | memset(&fi, 0, sizeof(fi)); |
1480 | fi.fh = arg->fh; | |
2de121f0 | 1481 | |
7387863d DDAG |
1482 | if (req->se->op.fallocate) { |
1483 | req->se->op.fallocate(req, nodeid, arg->mode, arg->offset, arg->length, | |
1484 | &fi); | |
1485 | } else { | |
1486 | fuse_reply_err(req, ENOSYS); | |
1487 | } | |
2de121f0 DDAG |
1488 | } |
1489 | ||
7387863d DDAG |
1490 | static void do_copy_file_range(fuse_req_t req, fuse_ino_t nodeid_in, |
1491 | const void *inarg) | |
2de121f0 | 1492 | { |
7387863d DDAG |
1493 | struct fuse_copy_file_range_in *arg = |
1494 | (struct fuse_copy_file_range_in *)inarg; | |
1495 | struct fuse_file_info fi_in, fi_out; | |
2de121f0 | 1496 | |
7387863d DDAG |
1497 | memset(&fi_in, 0, sizeof(fi_in)); |
1498 | fi_in.fh = arg->fh_in; | |
2de121f0 | 1499 | |
7387863d DDAG |
1500 | memset(&fi_out, 0, sizeof(fi_out)); |
1501 | fi_out.fh = arg->fh_out; | |
2de121f0 DDAG |
1502 | |
1503 | ||
7387863d DDAG |
1504 | if (req->se->op.copy_file_range) { |
1505 | req->se->op.copy_file_range(req, nodeid_in, arg->off_in, &fi_in, | |
1506 | arg->nodeid_out, arg->off_out, &fi_out, | |
1507 | arg->len, arg->flags); | |
1508 | } else { | |
1509 | fuse_reply_err(req, ENOSYS); | |
1510 | } | |
2de121f0 DDAG |
1511 | } |
1512 | ||
1513 | static void do_lseek(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1514 | { | |
7387863d DDAG |
1515 | struct fuse_lseek_in *arg = (struct fuse_lseek_in *)inarg; |
1516 | struct fuse_file_info fi; | |
2de121f0 | 1517 | |
7387863d DDAG |
1518 | memset(&fi, 0, sizeof(fi)); |
1519 | fi.fh = arg->fh; | |
2de121f0 | 1520 | |
7387863d DDAG |
1521 | if (req->se->op.lseek) { |
1522 | req->se->op.lseek(req, nodeid, arg->offset, arg->whence, &fi); | |
1523 | } else { | |
1524 | fuse_reply_err(req, ENOSYS); | |
1525 | } | |
2de121f0 DDAG |
1526 | } |
1527 | ||
1528 | static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1529 | { | |
7387863d DDAG |
1530 | struct fuse_init_in *arg = (struct fuse_init_in *)inarg; |
1531 | struct fuse_init_out outarg; | |
1532 | struct fuse_session *se = req->se; | |
1533 | size_t bufsize = se->bufsize; | |
1534 | size_t outargsize = sizeof(outarg); | |
1535 | ||
1536 | (void)nodeid; | |
1537 | if (se->debug) { | |
1538 | fuse_log(FUSE_LOG_DEBUG, "INIT: %u.%u\n", arg->major, arg->minor); | |
1539 | if (arg->major == 7 && arg->minor >= 6) { | |
1540 | fuse_log(FUSE_LOG_DEBUG, "flags=0x%08x\n", arg->flags); | |
1541 | fuse_log(FUSE_LOG_DEBUG, "max_readahead=0x%08x\n", | |
1542 | arg->max_readahead); | |
1543 | } | |
1544 | } | |
1545 | se->conn.proto_major = arg->major; | |
1546 | se->conn.proto_minor = arg->minor; | |
1547 | se->conn.capable = 0; | |
1548 | se->conn.want = 0; | |
1549 | ||
1550 | memset(&outarg, 0, sizeof(outarg)); | |
1551 | outarg.major = FUSE_KERNEL_VERSION; | |
1552 | outarg.minor = FUSE_KERNEL_MINOR_VERSION; | |
1553 | ||
72c42e2d | 1554 | if (arg->major < 7 || (arg->major == 7 && arg->minor < 31)) { |
7387863d DDAG |
1555 | fuse_log(FUSE_LOG_ERR, "fuse: unsupported protocol version: %u.%u\n", |
1556 | arg->major, arg->minor); | |
1557 | fuse_reply_err(req, EPROTO); | |
1558 | return; | |
1559 | } | |
1560 | ||
1561 | if (arg->major > 7) { | |
1562 | /* Wait for a second INIT request with a 7.X version */ | |
1563 | send_reply_ok(req, &outarg, sizeof(outarg)); | |
1564 | return; | |
1565 | } | |
1566 | ||
72c42e2d DDAG |
1567 | if (arg->max_readahead < se->conn.max_readahead) { |
1568 | se->conn.max_readahead = arg->max_readahead; | |
1569 | } | |
1570 | if (arg->flags & FUSE_ASYNC_READ) { | |
1571 | se->conn.capable |= FUSE_CAP_ASYNC_READ; | |
1572 | } | |
1573 | if (arg->flags & FUSE_POSIX_LOCKS) { | |
1574 | se->conn.capable |= FUSE_CAP_POSIX_LOCKS; | |
1575 | } | |
1576 | if (arg->flags & FUSE_ATOMIC_O_TRUNC) { | |
1577 | se->conn.capable |= FUSE_CAP_ATOMIC_O_TRUNC; | |
1578 | } | |
1579 | if (arg->flags & FUSE_EXPORT_SUPPORT) { | |
1580 | se->conn.capable |= FUSE_CAP_EXPORT_SUPPORT; | |
1581 | } | |
1582 | if (arg->flags & FUSE_DONT_MASK) { | |
1583 | se->conn.capable |= FUSE_CAP_DONT_MASK; | |
1584 | } | |
1585 | if (arg->flags & FUSE_FLOCK_LOCKS) { | |
1586 | se->conn.capable |= FUSE_CAP_FLOCK_LOCKS; | |
1587 | } | |
1588 | if (arg->flags & FUSE_AUTO_INVAL_DATA) { | |
1589 | se->conn.capable |= FUSE_CAP_AUTO_INVAL_DATA; | |
1590 | } | |
1591 | if (arg->flags & FUSE_DO_READDIRPLUS) { | |
1592 | se->conn.capable |= FUSE_CAP_READDIRPLUS; | |
1593 | } | |
1594 | if (arg->flags & FUSE_READDIRPLUS_AUTO) { | |
1595 | se->conn.capable |= FUSE_CAP_READDIRPLUS_AUTO; | |
1596 | } | |
1597 | if (arg->flags & FUSE_ASYNC_DIO) { | |
1598 | se->conn.capable |= FUSE_CAP_ASYNC_DIO; | |
1599 | } | |
1600 | if (arg->flags & FUSE_WRITEBACK_CACHE) { | |
1601 | se->conn.capable |= FUSE_CAP_WRITEBACK_CACHE; | |
1602 | } | |
1603 | if (arg->flags & FUSE_NO_OPEN_SUPPORT) { | |
1604 | se->conn.capable |= FUSE_CAP_NO_OPEN_SUPPORT; | |
1605 | } | |
1606 | if (arg->flags & FUSE_PARALLEL_DIROPS) { | |
1607 | se->conn.capable |= FUSE_CAP_PARALLEL_DIROPS; | |
1608 | } | |
1609 | if (arg->flags & FUSE_POSIX_ACL) { | |
1610 | se->conn.capable |= FUSE_CAP_POSIX_ACL; | |
1611 | } | |
1612 | if (arg->flags & FUSE_HANDLE_KILLPRIV) { | |
1613 | se->conn.capable |= FUSE_CAP_HANDLE_KILLPRIV; | |
1614 | } | |
1615 | if (arg->flags & FUSE_NO_OPENDIR_SUPPORT) { | |
1616 | se->conn.capable |= FUSE_CAP_NO_OPENDIR_SUPPORT; | |
1617 | } | |
1618 | if (!(arg->flags & FUSE_MAX_PAGES)) { | |
1619 | size_t max_bufsize = FUSE_DEFAULT_MAX_PAGES_PER_REQ * getpagesize() + | |
1620 | FUSE_BUFFER_HEADER_SIZE; | |
1621 | if (bufsize > max_bufsize) { | |
1622 | bufsize = max_bufsize; | |
7387863d | 1623 | } |
7387863d | 1624 | } |
2de121f0 DDAG |
1625 | #ifdef HAVE_SPLICE |
1626 | #ifdef HAVE_VMSPLICE | |
72c42e2d | 1627 | se->conn.capable |= FUSE_CAP_SPLICE_WRITE | FUSE_CAP_SPLICE_MOVE; |
2de121f0 | 1628 | #endif |
72c42e2d | 1629 | se->conn.capable |= FUSE_CAP_SPLICE_READ; |
2de121f0 | 1630 | #endif |
72c42e2d | 1631 | se->conn.capable |= FUSE_CAP_IOCTL_DIR; |
7387863d DDAG |
1632 | |
1633 | /* | |
1634 | * Default settings for modern filesystems. | |
1635 | * | |
1636 | * Most of these capabilities were disabled by default in | |
1637 | * libfuse2 for backwards compatibility reasons. In libfuse3, | |
1638 | * we can finally enable them by default (as long as they're | |
1639 | * supported by the kernel). | |
1640 | */ | |
1641 | #define LL_SET_DEFAULT(cond, cap) \ | |
1642 | if ((cond) && (se->conn.capable & (cap))) \ | |
1643 | se->conn.want |= (cap) | |
1644 | LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_READ); | |
1645 | LL_SET_DEFAULT(1, FUSE_CAP_PARALLEL_DIROPS); | |
1646 | LL_SET_DEFAULT(1, FUSE_CAP_AUTO_INVAL_DATA); | |
1647 | LL_SET_DEFAULT(1, FUSE_CAP_HANDLE_KILLPRIV); | |
1648 | LL_SET_DEFAULT(1, FUSE_CAP_ASYNC_DIO); | |
1649 | LL_SET_DEFAULT(1, FUSE_CAP_IOCTL_DIR); | |
1650 | LL_SET_DEFAULT(1, FUSE_CAP_ATOMIC_O_TRUNC); | |
1651 | LL_SET_DEFAULT(se->op.write_buf, FUSE_CAP_SPLICE_READ); | |
1652 | LL_SET_DEFAULT(se->op.getlk && se->op.setlk, FUSE_CAP_POSIX_LOCKS); | |
1653 | LL_SET_DEFAULT(se->op.flock, FUSE_CAP_FLOCK_LOCKS); | |
1654 | LL_SET_DEFAULT(se->op.readdirplus, FUSE_CAP_READDIRPLUS); | |
1655 | LL_SET_DEFAULT(se->op.readdirplus && se->op.readdir, | |
1656 | FUSE_CAP_READDIRPLUS_AUTO); | |
1657 | se->conn.time_gran = 1; | |
1658 | ||
1659 | if (bufsize < FUSE_MIN_READ_BUFFER) { | |
1660 | fuse_log(FUSE_LOG_ERR, "fuse: warning: buffer size too small: %zu\n", | |
1661 | bufsize); | |
1662 | bufsize = FUSE_MIN_READ_BUFFER; | |
1663 | } | |
1664 | se->bufsize = bufsize; | |
1665 | ||
1666 | if (se->conn.max_write > bufsize - FUSE_BUFFER_HEADER_SIZE) { | |
1667 | se->conn.max_write = bufsize - FUSE_BUFFER_HEADER_SIZE; | |
1668 | } | |
1669 | ||
1670 | se->got_init = 1; | |
1671 | if (se->op.init) { | |
1672 | se->op.init(se->userdata, &se->conn); | |
1673 | } | |
1674 | ||
1675 | if (se->conn.want & (~se->conn.capable)) { | |
1676 | fuse_log(FUSE_LOG_ERR, | |
1677 | "fuse: error: filesystem requested capabilities " | |
1678 | "0x%x that are not supported by kernel, aborting.\n", | |
1679 | se->conn.want & (~se->conn.capable)); | |
1680 | fuse_reply_err(req, EPROTO); | |
1681 | se->error = -EPROTO; | |
1682 | fuse_session_exit(se); | |
1683 | return; | |
1684 | } | |
1685 | ||
1686 | if (se->conn.max_write < bufsize - FUSE_BUFFER_HEADER_SIZE) { | |
1687 | se->bufsize = se->conn.max_write + FUSE_BUFFER_HEADER_SIZE; | |
1688 | } | |
1689 | if (arg->flags & FUSE_MAX_PAGES) { | |
1690 | outarg.flags |= FUSE_MAX_PAGES; | |
1691 | outarg.max_pages = (se->conn.max_write - 1) / getpagesize() + 1; | |
1692 | } | |
1693 | ||
1694 | /* | |
1695 | * Always enable big writes, this is superseded | |
1696 | * by the max_write option | |
1697 | */ | |
1698 | outarg.flags |= FUSE_BIG_WRITES; | |
1699 | ||
1700 | if (se->conn.want & FUSE_CAP_ASYNC_READ) { | |
1701 | outarg.flags |= FUSE_ASYNC_READ; | |
1702 | } | |
1703 | if (se->conn.want & FUSE_CAP_POSIX_LOCKS) { | |
1704 | outarg.flags |= FUSE_POSIX_LOCKS; | |
1705 | } | |
1706 | if (se->conn.want & FUSE_CAP_ATOMIC_O_TRUNC) { | |
1707 | outarg.flags |= FUSE_ATOMIC_O_TRUNC; | |
1708 | } | |
1709 | if (se->conn.want & FUSE_CAP_EXPORT_SUPPORT) { | |
1710 | outarg.flags |= FUSE_EXPORT_SUPPORT; | |
1711 | } | |
1712 | if (se->conn.want & FUSE_CAP_DONT_MASK) { | |
1713 | outarg.flags |= FUSE_DONT_MASK; | |
1714 | } | |
1715 | if (se->conn.want & FUSE_CAP_FLOCK_LOCKS) { | |
1716 | outarg.flags |= FUSE_FLOCK_LOCKS; | |
1717 | } | |
1718 | if (se->conn.want & FUSE_CAP_AUTO_INVAL_DATA) { | |
1719 | outarg.flags |= FUSE_AUTO_INVAL_DATA; | |
1720 | } | |
1721 | if (se->conn.want & FUSE_CAP_READDIRPLUS) { | |
1722 | outarg.flags |= FUSE_DO_READDIRPLUS; | |
1723 | } | |
1724 | if (se->conn.want & FUSE_CAP_READDIRPLUS_AUTO) { | |
1725 | outarg.flags |= FUSE_READDIRPLUS_AUTO; | |
1726 | } | |
1727 | if (se->conn.want & FUSE_CAP_ASYNC_DIO) { | |
1728 | outarg.flags |= FUSE_ASYNC_DIO; | |
1729 | } | |
1730 | if (se->conn.want & FUSE_CAP_WRITEBACK_CACHE) { | |
1731 | outarg.flags |= FUSE_WRITEBACK_CACHE; | |
1732 | } | |
1733 | if (se->conn.want & FUSE_CAP_POSIX_ACL) { | |
1734 | outarg.flags |= FUSE_POSIX_ACL; | |
1735 | } | |
1736 | outarg.max_readahead = se->conn.max_readahead; | |
1737 | outarg.max_write = se->conn.max_write; | |
72c42e2d DDAG |
1738 | if (se->conn.max_background >= (1 << 16)) { |
1739 | se->conn.max_background = (1 << 16) - 1; | |
1740 | } | |
1741 | if (se->conn.congestion_threshold > se->conn.max_background) { | |
1742 | se->conn.congestion_threshold = se->conn.max_background; | |
7387863d | 1743 | } |
72c42e2d DDAG |
1744 | if (!se->conn.congestion_threshold) { |
1745 | se->conn.congestion_threshold = se->conn.max_background * 3 / 4; | |
7387863d DDAG |
1746 | } |
1747 | ||
72c42e2d DDAG |
1748 | outarg.max_background = se->conn.max_background; |
1749 | outarg.congestion_threshold = se->conn.congestion_threshold; | |
1750 | outarg.time_gran = se->conn.time_gran; | |
1751 | ||
7387863d DDAG |
1752 | if (se->debug) { |
1753 | fuse_log(FUSE_LOG_DEBUG, " INIT: %u.%u\n", outarg.major, | |
1754 | outarg.minor); | |
1755 | fuse_log(FUSE_LOG_DEBUG, " flags=0x%08x\n", outarg.flags); | |
1756 | fuse_log(FUSE_LOG_DEBUG, " max_readahead=0x%08x\n", | |
1757 | outarg.max_readahead); | |
1758 | fuse_log(FUSE_LOG_DEBUG, " max_write=0x%08x\n", outarg.max_write); | |
1759 | fuse_log(FUSE_LOG_DEBUG, " max_background=%i\n", | |
1760 | outarg.max_background); | |
1761 | fuse_log(FUSE_LOG_DEBUG, " congestion_threshold=%i\n", | |
1762 | outarg.congestion_threshold); | |
1763 | fuse_log(FUSE_LOG_DEBUG, " time_gran=%u\n", outarg.time_gran); | |
1764 | } | |
7387863d DDAG |
1765 | |
1766 | send_reply_ok(req, &outarg, outargsize); | |
2de121f0 DDAG |
1767 | } |
1768 | ||
1769 | static void do_destroy(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) | |
1770 | { | |
7387863d | 1771 | struct fuse_session *se = req->se; |
2de121f0 | 1772 | |
7387863d DDAG |
1773 | (void)nodeid; |
1774 | (void)inarg; | |
2de121f0 | 1775 | |
7387863d DDAG |
1776 | se->got_destroy = 1; |
1777 | if (se->op.destroy) { | |
1778 | se->op.destroy(se->userdata); | |
1779 | } | |
2de121f0 | 1780 | |
7387863d | 1781 | send_reply_ok(req, NULL, 0); |
2de121f0 DDAG |
1782 | } |
1783 | ||
2de121f0 | 1784 | static int send_notify_iov(struct fuse_session *se, int notify_code, |
7387863d | 1785 | struct iovec *iov, int count) |
2de121f0 | 1786 | { |
7387863d | 1787 | struct fuse_out_header out; |
2de121f0 | 1788 | |
7387863d DDAG |
1789 | if (!se->got_init) { |
1790 | return -ENOTCONN; | |
1791 | } | |
2de121f0 | 1792 | |
7387863d DDAG |
1793 | out.unique = 0; |
1794 | out.error = notify_code; | |
1795 | iov[0].iov_base = &out; | |
1796 | iov[0].iov_len = sizeof(struct fuse_out_header); | |
2de121f0 | 1797 | |
7387863d | 1798 | return fuse_send_msg(se, NULL, iov, count); |
2de121f0 DDAG |
1799 | } |
1800 | ||
1801 | int fuse_lowlevel_notify_poll(struct fuse_pollhandle *ph) | |
1802 | { | |
7387863d DDAG |
1803 | if (ph != NULL) { |
1804 | struct fuse_notify_poll_wakeup_out outarg; | |
1805 | struct iovec iov[2]; | |
2de121f0 | 1806 | |
7387863d | 1807 | outarg.kh = ph->kh; |
2de121f0 | 1808 | |
7387863d DDAG |
1809 | iov[1].iov_base = &outarg; |
1810 | iov[1].iov_len = sizeof(outarg); | |
2de121f0 | 1811 | |
7387863d DDAG |
1812 | return send_notify_iov(ph->se, FUSE_NOTIFY_POLL, iov, 2); |
1813 | } else { | |
1814 | return 0; | |
1815 | } | |
2de121f0 DDAG |
1816 | } |
1817 | ||
1818 | int fuse_lowlevel_notify_inval_inode(struct fuse_session *se, fuse_ino_t ino, | |
7387863d | 1819 | off_t off, off_t len) |
2de121f0 | 1820 | { |
7387863d DDAG |
1821 | struct fuse_notify_inval_inode_out outarg; |
1822 | struct iovec iov[2]; | |
1823 | ||
1824 | if (!se) { | |
1825 | return -EINVAL; | |
1826 | } | |
2de121f0 | 1827 | |
7387863d DDAG |
1828 | outarg.ino = ino; |
1829 | outarg.off = off; | |
1830 | outarg.len = len; | |
2de121f0 | 1831 | |
7387863d DDAG |
1832 | iov[1].iov_base = &outarg; |
1833 | iov[1].iov_len = sizeof(outarg); | |
2de121f0 | 1834 | |
7387863d | 1835 | return send_notify_iov(se, FUSE_NOTIFY_INVAL_INODE, iov, 2); |
2de121f0 DDAG |
1836 | } |
1837 | ||
1838 | int fuse_lowlevel_notify_inval_entry(struct fuse_session *se, fuse_ino_t parent, | |
7387863d | 1839 | const char *name, size_t namelen) |
2de121f0 | 1840 | { |
7387863d DDAG |
1841 | struct fuse_notify_inval_entry_out outarg; |
1842 | struct iovec iov[3]; | |
1843 | ||
1844 | if (!se) { | |
1845 | return -EINVAL; | |
1846 | } | |
2de121f0 | 1847 | |
7387863d DDAG |
1848 | outarg.parent = parent; |
1849 | outarg.namelen = namelen; | |
1850 | outarg.padding = 0; | |
2de121f0 | 1851 | |
7387863d DDAG |
1852 | iov[1].iov_base = &outarg; |
1853 | iov[1].iov_len = sizeof(outarg); | |
1854 | iov[2].iov_base = (void *)name; | |
1855 | iov[2].iov_len = namelen + 1; | |
2de121f0 | 1856 | |
7387863d | 1857 | return send_notify_iov(se, FUSE_NOTIFY_INVAL_ENTRY, iov, 3); |
2de121f0 DDAG |
1858 | } |
1859 | ||
7387863d DDAG |
1860 | int fuse_lowlevel_notify_delete(struct fuse_session *se, fuse_ino_t parent, |
1861 | fuse_ino_t child, const char *name, | |
1862 | size_t namelen) | |
2de121f0 | 1863 | { |
7387863d DDAG |
1864 | struct fuse_notify_delete_out outarg; |
1865 | struct iovec iov[3]; | |
2de121f0 | 1866 | |
7387863d DDAG |
1867 | if (!se) { |
1868 | return -EINVAL; | |
1869 | } | |
2de121f0 | 1870 | |
7387863d DDAG |
1871 | outarg.parent = parent; |
1872 | outarg.child = child; | |
1873 | outarg.namelen = namelen; | |
1874 | outarg.padding = 0; | |
2de121f0 | 1875 | |
7387863d DDAG |
1876 | iov[1].iov_base = &outarg; |
1877 | iov[1].iov_len = sizeof(outarg); | |
1878 | iov[2].iov_base = (void *)name; | |
1879 | iov[2].iov_len = namelen + 1; | |
2de121f0 | 1880 | |
7387863d | 1881 | return send_notify_iov(se, FUSE_NOTIFY_DELETE, iov, 3); |
2de121f0 DDAG |
1882 | } |
1883 | ||
1884 | int fuse_lowlevel_notify_store(struct fuse_session *se, fuse_ino_t ino, | |
8c3fe75e | 1885 | off_t offset, struct fuse_bufvec *bufv) |
2de121f0 | 1886 | { |
7387863d DDAG |
1887 | struct fuse_out_header out; |
1888 | struct fuse_notify_store_out outarg; | |
1889 | struct iovec iov[3]; | |
1890 | size_t size = fuse_buf_size(bufv); | |
1891 | int res; | |
2de121f0 | 1892 | |
7387863d DDAG |
1893 | if (!se) { |
1894 | return -EINVAL; | |
1895 | } | |
2de121f0 | 1896 | |
7387863d DDAG |
1897 | out.unique = 0; |
1898 | out.error = FUSE_NOTIFY_STORE; | |
2de121f0 | 1899 | |
7387863d DDAG |
1900 | outarg.nodeid = ino; |
1901 | outarg.offset = offset; | |
1902 | outarg.size = size; | |
1903 | outarg.padding = 0; | |
2de121f0 | 1904 | |
7387863d DDAG |
1905 | iov[0].iov_base = &out; |
1906 | iov[0].iov_len = sizeof(out); | |
1907 | iov[1].iov_base = &outarg; | |
1908 | iov[1].iov_len = sizeof(outarg); | |
2de121f0 | 1909 | |
8c3fe75e | 1910 | res = fuse_send_data_iov(se, NULL, iov, 2, bufv); |
7387863d DDAG |
1911 | if (res > 0) { |
1912 | res = -res; | |
1913 | } | |
2de121f0 | 1914 | |
7387863d | 1915 | return res; |
2de121f0 DDAG |
1916 | } |
1917 | ||
2de121f0 DDAG |
1918 | void *fuse_req_userdata(fuse_req_t req) |
1919 | { | |
7387863d | 1920 | return req->se->userdata; |
2de121f0 DDAG |
1921 | } |
1922 | ||
1923 | const struct fuse_ctx *fuse_req_ctx(fuse_req_t req) | |
1924 | { | |
7387863d | 1925 | return &req->ctx; |
2de121f0 DDAG |
1926 | } |
1927 | ||
1928 | void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, | |
7387863d | 1929 | void *data) |
2de121f0 | 1930 | { |
7387863d DDAG |
1931 | pthread_mutex_lock(&req->lock); |
1932 | pthread_mutex_lock(&req->se->lock); | |
1933 | req->u.ni.func = func; | |
1934 | req->u.ni.data = data; | |
1935 | pthread_mutex_unlock(&req->se->lock); | |
1936 | if (req->interrupted && func) { | |
1937 | func(req, data); | |
1938 | } | |
1939 | pthread_mutex_unlock(&req->lock); | |
2de121f0 DDAG |
1940 | } |
1941 | ||
1942 | int fuse_req_interrupted(fuse_req_t req) | |
1943 | { | |
7387863d | 1944 | int interrupted; |
2de121f0 | 1945 | |
7387863d DDAG |
1946 | pthread_mutex_lock(&req->se->lock); |
1947 | interrupted = req->interrupted; | |
1948 | pthread_mutex_unlock(&req->se->lock); | |
2de121f0 | 1949 | |
7387863d | 1950 | return interrupted; |
2de121f0 DDAG |
1951 | } |
1952 | ||
1953 | static struct { | |
7387863d DDAG |
1954 | void (*func)(fuse_req_t, fuse_ino_t, const void *); |
1955 | const char *name; | |
2de121f0 | 1956 | } fuse_ll_ops[] = { |
7387863d DDAG |
1957 | [FUSE_LOOKUP] = { do_lookup, "LOOKUP" }, |
1958 | [FUSE_FORGET] = { do_forget, "FORGET" }, | |
1959 | [FUSE_GETATTR] = { do_getattr, "GETATTR" }, | |
1960 | [FUSE_SETATTR] = { do_setattr, "SETATTR" }, | |
1961 | [FUSE_READLINK] = { do_readlink, "READLINK" }, | |
1962 | [FUSE_SYMLINK] = { do_symlink, "SYMLINK" }, | |
1963 | [FUSE_MKNOD] = { do_mknod, "MKNOD" }, | |
1964 | [FUSE_MKDIR] = { do_mkdir, "MKDIR" }, | |
1965 | [FUSE_UNLINK] = { do_unlink, "UNLINK" }, | |
1966 | [FUSE_RMDIR] = { do_rmdir, "RMDIR" }, | |
1967 | [FUSE_RENAME] = { do_rename, "RENAME" }, | |
1968 | [FUSE_LINK] = { do_link, "LINK" }, | |
1969 | [FUSE_OPEN] = { do_open, "OPEN" }, | |
1970 | [FUSE_READ] = { do_read, "READ" }, | |
1971 | [FUSE_WRITE] = { do_write, "WRITE" }, | |
1972 | [FUSE_STATFS] = { do_statfs, "STATFS" }, | |
1973 | [FUSE_RELEASE] = { do_release, "RELEASE" }, | |
1974 | [FUSE_FSYNC] = { do_fsync, "FSYNC" }, | |
1975 | [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" }, | |
1976 | [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" }, | |
1977 | [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" }, | |
1978 | [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" }, | |
1979 | [FUSE_FLUSH] = { do_flush, "FLUSH" }, | |
1980 | [FUSE_INIT] = { do_init, "INIT" }, | |
1981 | [FUSE_OPENDIR] = { do_opendir, "OPENDIR" }, | |
1982 | [FUSE_READDIR] = { do_readdir, "READDIR" }, | |
1983 | [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" }, | |
1984 | [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" }, | |
1985 | [FUSE_GETLK] = { do_getlk, "GETLK" }, | |
1986 | [FUSE_SETLK] = { do_setlk, "SETLK" }, | |
1987 | [FUSE_SETLKW] = { do_setlkw, "SETLKW" }, | |
1988 | [FUSE_ACCESS] = { do_access, "ACCESS" }, | |
1989 | [FUSE_CREATE] = { do_create, "CREATE" }, | |
1990 | [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" }, | |
1991 | [FUSE_BMAP] = { do_bmap, "BMAP" }, | |
1992 | [FUSE_IOCTL] = { do_ioctl, "IOCTL" }, | |
1993 | [FUSE_POLL] = { do_poll, "POLL" }, | |
1994 | [FUSE_FALLOCATE] = { do_fallocate, "FALLOCATE" }, | |
1995 | [FUSE_DESTROY] = { do_destroy, "DESTROY" }, | |
64c6f408 | 1996 | [FUSE_NOTIFY_REPLY] = { NULL, "NOTIFY_REPLY" }, |
7387863d DDAG |
1997 | [FUSE_BATCH_FORGET] = { do_batch_forget, "BATCH_FORGET" }, |
1998 | [FUSE_READDIRPLUS] = { do_readdirplus, "READDIRPLUS" }, | |
1999 | [FUSE_RENAME2] = { do_rename2, "RENAME2" }, | |
2000 | [FUSE_COPY_FILE_RANGE] = { do_copy_file_range, "COPY_FILE_RANGE" }, | |
2001 | [FUSE_LSEEK] = { do_lseek, "LSEEK" }, | |
2de121f0 DDAG |
2002 | }; |
2003 | ||
2004 | #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0])) | |
2005 | ||
2006 | static const char *opname(enum fuse_opcode opcode) | |
2007 | { | |
7387863d DDAG |
2008 | if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) { |
2009 | return "???"; | |
2010 | } else { | |
2011 | return fuse_ll_ops[opcode].name; | |
2012 | } | |
2de121f0 DDAG |
2013 | } |
2014 | ||
2de121f0 | 2015 | void fuse_session_process_buf(struct fuse_session *se, |
7387863d | 2016 | const struct fuse_buf *buf) |
2de121f0 | 2017 | { |
7387863d | 2018 | fuse_session_process_buf_int(se, buf, NULL); |
2de121f0 DDAG |
2019 | } |
2020 | ||
2021 | void fuse_session_process_buf_int(struct fuse_session *se, | |
7387863d DDAG |
2022 | const struct fuse_buf *buf, |
2023 | struct fuse_chan *ch) | |
2024 | { | |
2025 | struct fuse_in_header *in; | |
2026 | const void *inarg; | |
2027 | struct fuse_req *req; | |
2028 | int err; | |
2029 | ||
2030 | in = buf->mem; | |
2031 | ||
2032 | if (se->debug) { | |
2033 | fuse_log(FUSE_LOG_DEBUG, | |
2034 | "unique: %llu, opcode: %s (%i), nodeid: %llu, insize: %zu, " | |
2035 | "pid: %u\n", | |
2036 | (unsigned long long)in->unique, | |
2037 | opname((enum fuse_opcode)in->opcode), in->opcode, | |
2038 | (unsigned long long)in->nodeid, buf->size, in->pid); | |
2039 | } | |
2040 | ||
2041 | req = fuse_ll_alloc_req(se); | |
2042 | if (req == NULL) { | |
2043 | struct fuse_out_header out = { | |
2044 | .unique = in->unique, | |
2045 | .error = -ENOMEM, | |
2046 | }; | |
2047 | struct iovec iov = { | |
2048 | .iov_base = &out, | |
2049 | .iov_len = sizeof(struct fuse_out_header), | |
2050 | }; | |
2051 | ||
2052 | fuse_send_msg(se, ch, &iov, 1); | |
2053 | return; | |
2054 | } | |
2055 | ||
2056 | req->unique = in->unique; | |
2057 | req->ctx.uid = in->uid; | |
2058 | req->ctx.gid = in->gid; | |
2059 | req->ctx.pid = in->pid; | |
2060 | req->ch = ch; | |
2061 | ||
2062 | err = EIO; | |
2063 | if (!se->got_init) { | |
2064 | enum fuse_opcode expected; | |
2065 | ||
2066 | expected = se->cuse_data ? CUSE_INIT : FUSE_INIT; | |
2067 | if (in->opcode != expected) { | |
2068 | goto reply_err; | |
2069 | } | |
2070 | } else if (in->opcode == FUSE_INIT || in->opcode == CUSE_INIT) { | |
2071 | goto reply_err; | |
2072 | } | |
2073 | ||
2074 | err = EACCES; | |
2075 | /* Implement -o allow_root */ | |
2076 | if (se->deny_others && in->uid != se->owner && in->uid != 0 && | |
2077 | in->opcode != FUSE_INIT && in->opcode != FUSE_READ && | |
2078 | in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC && | |
2079 | in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR && | |
2080 | in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR && | |
2081 | in->opcode != FUSE_NOTIFY_REPLY && in->opcode != FUSE_READDIRPLUS) { | |
2082 | goto reply_err; | |
2083 | } | |
2084 | ||
2085 | err = ENOSYS; | |
2086 | if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) { | |
2087 | goto reply_err; | |
2088 | } | |
2089 | if (in->opcode != FUSE_INTERRUPT) { | |
2090 | struct fuse_req *intr; | |
2091 | pthread_mutex_lock(&se->lock); | |
2092 | intr = check_interrupt(se, req); | |
2093 | list_add_req(req, &se->list); | |
2094 | pthread_mutex_unlock(&se->lock); | |
2095 | if (intr) { | |
2096 | fuse_reply_err(intr, EAGAIN); | |
2097 | } | |
2098 | } | |
2099 | ||
2100 | inarg = (void *)&in[1]; | |
2101 | if (in->opcode == FUSE_WRITE && se->op.write_buf) { | |
2102 | do_write_buf(req, in->nodeid, inarg, buf); | |
7387863d DDAG |
2103 | } else { |
2104 | fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg); | |
2105 | } | |
2106 | ||
2107 | return; | |
2de121f0 DDAG |
2108 | |
2109 | reply_err: | |
7387863d | 2110 | fuse_reply_err(req, err); |
2de121f0 DDAG |
2111 | } |
2112 | ||
7387863d DDAG |
2113 | #define LL_OPTION(n, o, v) \ |
2114 | { \ | |
2115 | n, offsetof(struct fuse_session, o), v \ | |
2116 | } | |
2de121f0 DDAG |
2117 | |
2118 | static const struct fuse_opt fuse_ll_opts[] = { | |
205de006 DDAG |
2119 | LL_OPTION("debug", debug, 1), |
2120 | LL_OPTION("-d", debug, 1), | |
2121 | LL_OPTION("--debug", debug, 1), | |
2122 | LL_OPTION("allow_root", deny_others, 1), | |
2123 | LL_OPTION("--socket-path=%s", vu_socket_path, 0), | |
7387863d | 2124 | FUSE_OPT_END |
2de121f0 DDAG |
2125 | }; |
2126 | ||
2127 | void fuse_lowlevel_version(void) | |
2128 | { | |
7387863d DDAG |
2129 | printf("using FUSE kernel interface version %i.%i\n", FUSE_KERNEL_VERSION, |
2130 | FUSE_KERNEL_MINOR_VERSION); | |
2de121f0 DDAG |
2131 | } |
2132 | ||
2133 | void fuse_lowlevel_help(void) | |
2134 | { | |
7387863d DDAG |
2135 | /* |
2136 | * These are not all options, but the ones that are | |
2137 | * potentially of interest to an end-user | |
2138 | */ | |
205de006 DDAG |
2139 | printf( |
2140 | " -o allow_root allow access by root\n" | |
2141 | " --socket-path=PATH path for the vhost-user socket\n"); | |
2de121f0 DDAG |
2142 | } |
2143 | ||
2144 | void fuse_session_destroy(struct fuse_session *se) | |
2145 | { | |
7387863d DDAG |
2146 | if (se->got_init && !se->got_destroy) { |
2147 | if (se->op.destroy) { | |
2148 | se->op.destroy(se->userdata); | |
2149 | } | |
2150 | } | |
2151 | pthread_mutex_destroy(&se->lock); | |
2152 | free(se->cuse_data); | |
2153 | if (se->fd != -1) { | |
2154 | close(se->fd); | |
2155 | } | |
2156 | free(se); | |
2de121f0 DDAG |
2157 | } |
2158 | ||
2159 | ||
2de121f0 | 2160 | struct fuse_session *fuse_session_new(struct fuse_args *args, |
7387863d DDAG |
2161 | const struct fuse_lowlevel_ops *op, |
2162 | size_t op_size, void *userdata) | |
2163 | { | |
2164 | struct fuse_session *se; | |
2165 | ||
2166 | if (sizeof(struct fuse_lowlevel_ops) < op_size) { | |
2167 | fuse_log( | |
2168 | FUSE_LOG_ERR, | |
2169 | "fuse: warning: library too old, some operations may not work\n"); | |
2170 | op_size = sizeof(struct fuse_lowlevel_ops); | |
2171 | } | |
2172 | ||
2173 | if (args->argc == 0) { | |
2174 | fuse_log(FUSE_LOG_ERR, | |
2175 | "fuse: empty argv passed to fuse_session_new().\n"); | |
2176 | return NULL; | |
2177 | } | |
2178 | ||
2179 | se = (struct fuse_session *)calloc(1, sizeof(struct fuse_session)); | |
2180 | if (se == NULL) { | |
2181 | fuse_log(FUSE_LOG_ERR, "fuse: failed to allocate fuse object\n"); | |
2182 | goto out1; | |
2183 | } | |
2184 | se->fd = -1; | |
2185 | se->conn.max_write = UINT_MAX; | |
2186 | se->conn.max_readahead = UINT_MAX; | |
2187 | ||
2188 | /* Parse options */ | |
2189 | if (fuse_opt_parse(args, se, fuse_ll_opts, NULL) == -1) { | |
2190 | goto out2; | |
2191 | } | |
2192 | if (args->argc == 1 && args->argv[0][0] == '-') { | |
2193 | fuse_log(FUSE_LOG_ERR, | |
2194 | "fuse: warning: argv[0] looks like an option, but " | |
2195 | "will be ignored\n"); | |
2196 | } else if (args->argc != 1) { | |
2197 | int i; | |
2198 | fuse_log(FUSE_LOG_ERR, "fuse: unknown option(s): `"); | |
2199 | for (i = 1; i < args->argc - 1; i++) { | |
2200 | fuse_log(FUSE_LOG_ERR, "%s ", args->argv[i]); | |
2201 | } | |
2202 | fuse_log(FUSE_LOG_ERR, "%s'\n", args->argv[i]); | |
2203 | goto out4; | |
2204 | } | |
2205 | ||
d14bf584 DDAG |
2206 | if (!se->vu_socket_path) { |
2207 | fprintf(stderr, "fuse: missing -o vhost_user_socket option\n"); | |
2208 | goto out4; | |
2209 | } | |
2210 | ||
7387863d DDAG |
2211 | se->bufsize = FUSE_MAX_MAX_PAGES * getpagesize() + FUSE_BUFFER_HEADER_SIZE; |
2212 | ||
2213 | list_init_req(&se->list); | |
2214 | list_init_req(&se->interrupts); | |
7387863d DDAG |
2215 | fuse_mutex_init(&se->lock); |
2216 | ||
2217 | memcpy(&se->op, op, op_size); | |
2218 | se->owner = getuid(); | |
2219 | se->userdata = userdata; | |
2220 | ||
2221 | return se; | |
2de121f0 | 2222 | |
2de121f0 | 2223 | out4: |
7387863d | 2224 | fuse_opt_free_args(args); |
2de121f0 | 2225 | out2: |
7387863d | 2226 | free(se); |
2de121f0 | 2227 | out1: |
7387863d | 2228 | return NULL; |
2de121f0 DDAG |
2229 | } |
2230 | ||
67aab022 | 2231 | int fuse_session_mount(struct fuse_session *se) |
2de121f0 | 2232 | { |
d14bf584 | 2233 | return virtio_session_mount(se); |
2de121f0 DDAG |
2234 | } |
2235 | ||
2236 | int fuse_session_fd(struct fuse_session *se) | |
2237 | { | |
7387863d | 2238 | return se->fd; |
2de121f0 DDAG |
2239 | } |
2240 | ||
2241 | void fuse_session_unmount(struct fuse_session *se) | |
2242 | { | |
2de121f0 DDAG |
2243 | } |
2244 | ||
f6f3573c DDAG |
2245 | int fuse_lowlevel_is_virtio(struct fuse_session *se) |
2246 | { | |
2247 | return se->vu_socket_path != NULL; | |
2248 | } | |
2249 | ||
2de121f0 DDAG |
2250 | #ifdef linux |
2251 | int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]) | |
2252 | { | |
7387863d DDAG |
2253 | char *buf; |
2254 | size_t bufsize = 1024; | |
2255 | char path[128]; | |
2256 | int ret; | |
2257 | int fd; | |
2258 | unsigned long pid = req->ctx.pid; | |
2259 | char *s; | |
2de121f0 | 2260 | |
7387863d | 2261 | sprintf(path, "/proc/%lu/task/%lu/status", pid, pid); |
2de121f0 DDAG |
2262 | |
2263 | retry: | |
7387863d DDAG |
2264 | buf = malloc(bufsize); |
2265 | if (buf == NULL) { | |
2266 | return -ENOMEM; | |
2267 | } | |
2268 | ||
2269 | ret = -EIO; | |
2270 | fd = open(path, O_RDONLY); | |
2271 | if (fd == -1) { | |
2272 | goto out_free; | |
2273 | } | |
2274 | ||
2275 | ret = read(fd, buf, bufsize); | |
2276 | close(fd); | |
2277 | if (ret < 0) { | |
2278 | ret = -EIO; | |
2279 | goto out_free; | |
2280 | } | |
2281 | ||
2282 | if ((size_t)ret == bufsize) { | |
2283 | free(buf); | |
2284 | bufsize *= 4; | |
2285 | goto retry; | |
2286 | } | |
2287 | ||
2288 | ret = -EIO; | |
2289 | s = strstr(buf, "\nGroups:"); | |
2290 | if (s == NULL) { | |
2291 | goto out_free; | |
2292 | } | |
2293 | ||
2294 | s += 8; | |
2295 | ret = 0; | |
2296 | while (1) { | |
2297 | char *end; | |
2298 | unsigned long val = strtoul(s, &end, 0); | |
2299 | if (end == s) { | |
2300 | break; | |
2301 | } | |
2302 | ||
2303 | s = end; | |
2304 | if (ret < size) { | |
2305 | list[ret] = val; | |
2306 | } | |
2307 | ret++; | |
2308 | } | |
2de121f0 DDAG |
2309 | |
2310 | out_free: | |
7387863d DDAG |
2311 | free(buf); |
2312 | return ret; | |
2de121f0 DDAG |
2313 | } |
2314 | #else /* linux */ | |
2315 | /* | |
2316 | * This is currently not implemented on other than Linux... | |
2317 | */ | |
2318 | int fuse_req_getgroups(fuse_req_t req, int size, gid_t list[]) | |
2319 | { | |
7387863d DDAG |
2320 | (void)req; |
2321 | (void)size; | |
2322 | (void)list; | |
2323 | return -ENOSYS; | |
2de121f0 DDAG |
2324 | } |
2325 | #endif | |
2326 | ||
2327 | void fuse_session_exit(struct fuse_session *se) | |
2328 | { | |
7387863d | 2329 | se->exited = 1; |
2de121f0 DDAG |
2330 | } |
2331 | ||
2332 | void fuse_session_reset(struct fuse_session *se) | |
2333 | { | |
7387863d DDAG |
2334 | se->exited = 0; |
2335 | se->error = 0; | |
2de121f0 DDAG |
2336 | } |
2337 | ||
2338 | int fuse_session_exited(struct fuse_session *se) | |
2339 | { | |
7387863d | 2340 | return se->exited; |
2de121f0 | 2341 | } |