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