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