Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
| 4 | |
| 5 | This program can be distributed under the terms of the GNU LGPL. |
| 6 | See the file COPYING.LIB |
| 7 | */ |
| 8 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 9 | #include <config.h> |
| 10 | #include "fuse_lowlevel_i.h" |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 11 | #include "fuse_kernel.h" |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 12 | |
| 13 | #include <stdio.h> |
| 14 | #include <string.h> |
| 15 | #include <stdlib.h> |
| 16 | #include <unistd.h> |
| 17 | #include <limits.h> |
| 18 | #include <errno.h> |
| 19 | #include <assert.h> |
| 20 | #include <stdint.h> |
| 21 | #include <sys/param.h> |
| 22 | #include <sys/uio.h> |
| 23 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 24 | #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg))) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 25 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 26 | struct fuse_cmd { |
| 27 | char *buf; |
| 28 | size_t buflen; |
| 29 | }; |
| 30 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 31 | struct fuse_req { |
| 32 | struct fuse_ll *f; |
| 33 | uint64_t unique; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 34 | struct fuse_ctx ctx; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 35 | }; |
| 36 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 37 | #ifndef USE_UCLIBC |
| 38 | #define mutex_init(mut) pthread_mutex_init(mut, NULL) |
| 39 | #else |
| 40 | static void mutex_init(pthread_mutex_t *mut) |
| 41 | { |
| 42 | pthread_mutexattr_t attr; |
| 43 | pthread_mutexattr_init(&attr); |
| 44 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 45 | pthread_mutex_init(mut, &attr); |
| 46 | pthread_mutexattr_destroy(&attr); |
| 47 | } |
| 48 | #endif |
| 49 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 50 | static const char *opname(enum fuse_opcode opcode) |
| 51 | { |
| 52 | switch (opcode) { |
| 53 | case FUSE_LOOKUP: return "LOOKUP"; |
| 54 | case FUSE_FORGET: return "FORGET"; |
| 55 | case FUSE_GETATTR: return "GETATTR"; |
| 56 | case FUSE_SETATTR: return "SETATTR"; |
| 57 | case FUSE_READLINK: return "READLINK"; |
| 58 | case FUSE_SYMLINK: return "SYMLINK"; |
| 59 | case FUSE_MKNOD: return "MKNOD"; |
| 60 | case FUSE_MKDIR: return "MKDIR"; |
| 61 | case FUSE_UNLINK: return "UNLINK"; |
| 62 | case FUSE_RMDIR: return "RMDIR"; |
| 63 | case FUSE_RENAME: return "RENAME"; |
| 64 | case FUSE_LINK: return "LINK"; |
| 65 | case FUSE_OPEN: return "OPEN"; |
| 66 | case FUSE_READ: return "READ"; |
| 67 | case FUSE_WRITE: return "WRITE"; |
| 68 | case FUSE_STATFS: return "STATFS"; |
| 69 | case FUSE_FLUSH: return "FLUSH"; |
| 70 | case FUSE_RELEASE: return "RELEASE"; |
| 71 | case FUSE_FSYNC: return "FSYNC"; |
| 72 | case FUSE_SETXATTR: return "SETXATTR"; |
| 73 | case FUSE_GETXATTR: return "GETXATTR"; |
| 74 | case FUSE_LISTXATTR: return "LISTXATTR"; |
| 75 | case FUSE_REMOVEXATTR: return "REMOVEXATTR"; |
| 76 | case FUSE_INIT: return "INIT"; |
| 77 | case FUSE_OPENDIR: return "OPENDIR"; |
| 78 | case FUSE_READDIR: return "READDIR"; |
| 79 | case FUSE_RELEASEDIR: return "RELEASEDIR"; |
| 80 | case FUSE_FSYNCDIR: return "FSYNCDIR"; |
| 81 | default: return "???"; |
| 82 | } |
| 83 | } |
| 84 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 85 | static inline void fuse_dec_avail(struct fuse_ll *f) |
| 86 | { |
| 87 | pthread_mutex_lock(&f->worker_lock); |
| 88 | f->numavail --; |
| 89 | pthread_mutex_unlock(&f->worker_lock); |
| 90 | } |
| 91 | |
| 92 | static inline void fuse_inc_avail(struct fuse_ll *f) |
| 93 | { |
| 94 | pthread_mutex_lock(&f->worker_lock); |
| 95 | f->numavail ++; |
| 96 | pthread_mutex_unlock(&f->worker_lock); |
| 97 | } |
| 98 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 99 | static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 100 | { |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 101 | attr->ino = stbuf->st_ino; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 102 | attr->mode = stbuf->st_mode; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 103 | attr->nlink = stbuf->st_nlink; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 104 | attr->uid = stbuf->st_uid; |
| 105 | attr->gid = stbuf->st_gid; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 106 | attr->rdev = stbuf->st_rdev; |
| 107 | attr->size = stbuf->st_size; |
| 108 | attr->blocks = stbuf->st_blocks; |
| 109 | attr->atime = stbuf->st_atime; |
| 110 | attr->mtime = stbuf->st_mtime; |
| 111 | attr->ctime = stbuf->st_ctime; |
| 112 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
| 113 | attr->atimensec = stbuf->st_atim.tv_nsec; |
| 114 | attr->mtimensec = stbuf->st_mtim.tv_nsec; |
| 115 | attr->ctimensec = stbuf->st_ctim.tv_nsec; |
| 116 | #endif |
| 117 | } |
| 118 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 119 | static void convert_attr(const struct fuse_attr *attr, struct stat *stbuf) |
| 120 | { |
| 121 | stbuf->st_mode = attr->mode; |
| 122 | stbuf->st_uid = attr->uid; |
| 123 | stbuf->st_gid = attr->gid; |
| 124 | stbuf->st_size = attr->size; |
| 125 | stbuf->st_atime = attr->atime; |
| 126 | stbuf->st_mtime = attr->mtime; |
| 127 | stbuf->st_ctime = attr->ctime; |
| 128 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
| 129 | stbuf->st_atim.tv_nsec = attr->atimensec; |
| 130 | stbuf->st_mtim.tv_nsec = attr->mtimensec; |
| 131 | stbuf->st_ctim.tv_nsec = attr->ctimensec; |
| 132 | #endif |
| 133 | } |
| 134 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 135 | static size_t iov_length(const struct iovec *iov, size_t count) |
| 136 | { |
| 137 | size_t seg; |
| 138 | size_t ret = 0; |
| 139 | |
| 140 | for (seg = 0; seg < count; seg++) |
| 141 | ret += iov[seg].iov_len; |
| 142 | return ret; |
| 143 | } |
| 144 | |
| 145 | static int send_reply_raw(struct fuse_ll *f, const struct iovec iov[], |
| 146 | size_t count) |
| 147 | { |
| 148 | int res; |
| 149 | unsigned outsize = iov_length(iov, count); |
| 150 | struct fuse_out_header *out = (struct fuse_out_header *) iov[0].iov_base; |
| 151 | out->len = outsize; |
| 152 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 153 | if (f->debug) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 154 | printf(" unique: %llu, error: %i (%s), outsize: %i\n", |
| 155 | out->unique, out->error, strerror(-out->error), outsize); |
| 156 | fflush(stdout); |
| 157 | } |
| 158 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 159 | /* This needs to be done before the reply, otherwise the scheduler |
| 160 | could play tricks with us, and only let the counter be |
| 161 | increased long after the operation is done */ |
| 162 | fuse_inc_avail(f); |
| 163 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 164 | res = writev(f->fd, iov, count); |
| 165 | if (res == -1) { |
| 166 | /* ENOENT means the operation was interrupted */ |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 167 | if (!fuse_ll_exited(f) && errno != ENOENT) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 168 | perror("fuse: writing device"); |
| 169 | return -errno; |
| 170 | } |
| 171 | return 0; |
| 172 | } |
| 173 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 174 | static int send_reply(struct fuse_ll *f, uint64_t unique, int error, |
| 175 | const void *arg, size_t argsize) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 176 | { |
| 177 | struct fuse_out_header out; |
| 178 | struct iovec iov[2]; |
| 179 | size_t count; |
| 180 | |
| 181 | if (error <= -1000 || error > 0) { |
| 182 | fprintf(stderr, "fuse: bad error value: %i\n", error); |
| 183 | error = -ERANGE; |
| 184 | } |
| 185 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 186 | out.unique = unique; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 187 | out.error = error; |
| 188 | count = 1; |
| 189 | iov[0].iov_base = &out; |
| 190 | iov[0].iov_len = sizeof(struct fuse_out_header); |
| 191 | if (argsize && !error) { |
| 192 | count++; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 193 | iov[1].iov_base = (void *) arg; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 194 | iov[1].iov_len = argsize; |
| 195 | } |
| 196 | return send_reply_raw(f, iov, count); |
| 197 | } |
| 198 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 199 | size_t fuse_dirent_size(size_t namelen) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 200 | { |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 201 | return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 202 | } |
| 203 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 204 | char *fuse_add_dirent(char *buf, const char *name, const struct stat *stat, |
| 205 | off_t off) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 206 | { |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 207 | unsigned namelen = strlen(name); |
| 208 | unsigned entlen = FUSE_NAME_OFFSET + namelen; |
| 209 | unsigned entsize = fuse_dirent_size(namelen); |
| 210 | unsigned padlen = entsize - entlen; |
| 211 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 212 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 213 | dirent->ino = stat->st_ino; |
| 214 | dirent->off = off; |
| 215 | dirent->namelen = namelen; |
| 216 | dirent->type = (stat->st_mode & 0170000) >> 12; |
| 217 | strncpy(dirent->name, name, namelen); |
| 218 | if (padlen) |
| 219 | memset(buf + entlen, 0, padlen); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 220 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 221 | return buf + entsize; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 224 | static void convert_statfs(const struct statfs *statfs, |
| 225 | struct fuse_kstatfs *kstatfs) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 226 | { |
| 227 | kstatfs->bsize = statfs->f_bsize; |
| 228 | kstatfs->blocks = statfs->f_blocks; |
| 229 | kstatfs->bfree = statfs->f_bfree; |
| 230 | kstatfs->bavail = statfs->f_bavail; |
| 231 | kstatfs->files = statfs->f_files; |
| 232 | kstatfs->ffree = statfs->f_ffree; |
| 233 | kstatfs->namelen = statfs->f_namelen; |
| 234 | } |
| 235 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 236 | static void free_req(fuse_req_t req) |
| 237 | { |
| 238 | free(req); |
| 239 | } |
| 240 | |
| 241 | static int send_reply_req(fuse_req_t req, const void *arg, size_t argsize) |
| 242 | { |
| 243 | int res = send_reply(req->f, req->unique, 0, arg, argsize); |
| 244 | free_req(req); |
| 245 | return res; |
| 246 | } |
| 247 | |
| 248 | int fuse_reply_err(fuse_req_t req, int err) |
| 249 | { |
| 250 | int res = send_reply(req->f, req->unique, -err, NULL, 0); |
| 251 | free_req(req); |
| 252 | return res; |
| 253 | } |
| 254 | |
| 255 | int fuse_reply_none(fuse_req_t req) |
| 256 | { |
| 257 | free_req(req); |
| 258 | return 0; |
| 259 | } |
| 260 | |
| 261 | static unsigned long calc_timeout_sec(double t) |
| 262 | { |
| 263 | if (t > (double) ULONG_MAX) |
| 264 | return ULONG_MAX; |
| 265 | else if (t < 0.0) |
| 266 | return 0; |
| 267 | else |
| 268 | return (unsigned long) t; |
| 269 | } |
| 270 | |
| 271 | static unsigned int calc_timeout_nsec(double t) |
| 272 | { |
| 273 | double f = t - (double) calc_timeout_sec(t); |
| 274 | if (f < 0.0) |
| 275 | return 0; |
| 276 | else if (f >= 0.999999999) |
| 277 | return 999999999; |
| 278 | else |
| 279 | return (unsigned int) (f * 1.0e9); |
| 280 | } |
| 281 | |
| 282 | int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e) |
| 283 | { |
| 284 | struct fuse_entry_out arg; |
| 285 | |
| 286 | memset(&arg, 0, sizeof(arg)); |
| 287 | arg.nodeid = e->ino; |
| 288 | arg.generation = e->generation; |
| 289 | arg.entry_valid = calc_timeout_sec(e->entry_timeout); |
| 290 | arg.entry_valid_nsec = calc_timeout_nsec(e->entry_timeout); |
| 291 | arg.attr_valid = calc_timeout_sec(e->attr_timeout); |
| 292 | arg.attr_valid_nsec = calc_timeout_nsec(e->attr_timeout); |
| 293 | convert_stat(&e->attr, &arg.attr); |
| 294 | |
| 295 | return send_reply_req(req, &arg, sizeof(arg)); |
| 296 | } |
| 297 | |
| 298 | int fuse_reply_attr(fuse_req_t req, const struct stat *attr, |
| 299 | double attr_timeout) |
| 300 | { |
| 301 | struct fuse_attr_out arg; |
| 302 | |
| 303 | memset(&arg, 0, sizeof(arg)); |
| 304 | arg.attr_valid = calc_timeout_sec(attr_timeout); |
| 305 | arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout); |
| 306 | convert_stat(attr, &arg.attr); |
| 307 | |
| 308 | return send_reply_req(req, &arg, sizeof(arg)); |
| 309 | } |
| 310 | |
| 311 | int fuse_reply_readlink(fuse_req_t req, const char *link) |
| 312 | { |
| 313 | return send_reply_req(req, link, strlen(link)); |
| 314 | } |
| 315 | |
| 316 | int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f) |
| 317 | { |
| 318 | struct fuse_open_out arg; |
| 319 | |
| 320 | memset(&arg, 0, sizeof(arg)); |
| 321 | arg.fh = f->fh; |
| 322 | |
| 323 | return send_reply_req(req, &arg, sizeof(arg)); |
| 324 | } |
| 325 | |
| 326 | int fuse_reply_write(fuse_req_t req, size_t count) |
| 327 | { |
| 328 | struct fuse_write_out arg; |
| 329 | |
| 330 | memset(&arg, 0, sizeof(arg)); |
| 331 | arg.size = count; |
| 332 | |
| 333 | return send_reply_req(req, &arg, sizeof(arg)); |
| 334 | } |
| 335 | |
| 336 | int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size) |
| 337 | { |
| 338 | return send_reply_req(req, buf, size); |
| 339 | } |
| 340 | |
| 341 | int fuse_reply_statfs(fuse_req_t req, const struct statfs *statfs) |
| 342 | { |
| 343 | struct fuse_statfs_out arg; |
| 344 | |
| 345 | memset(&arg, 0, sizeof(arg)); |
| 346 | convert_statfs(statfs, &arg.st); |
| 347 | |
| 348 | return send_reply_req(req, &arg, sizeof(arg)); |
| 349 | } |
| 350 | |
| 351 | int fuse_reply_xattr(fuse_req_t req, size_t count) |
| 352 | { |
| 353 | struct fuse_getxattr_out arg; |
| 354 | |
| 355 | memset(&arg, 0, sizeof(arg)); |
| 356 | arg.size = count; |
| 357 | |
| 358 | return send_reply_req(req, &arg, sizeof(arg)); |
| 359 | } |
| 360 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 361 | static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, char *name) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 362 | { |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 363 | if (req->f->op.lookup) |
| 364 | req->f->op.lookup(req, nodeid, name); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 365 | else |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 366 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 367 | } |
| 368 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 369 | static void do_forget(fuse_req_t req, fuse_ino_t nodeid, |
| 370 | struct fuse_forget_in *arg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 371 | { |
| 372 | if (req->f->op.forget) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 373 | req->f->op.forget(req, nodeid, arg->nlookup); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | static void do_getattr(fuse_req_t req, fuse_ino_t nodeid) |
| 377 | { |
| 378 | if (req->f->op.getattr) |
| 379 | req->f->op.getattr(req, nodeid); |
| 380 | else |
| 381 | fuse_reply_err(req, ENOSYS); |
| 382 | } |
| 383 | |
| 384 | static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, |
| 385 | struct fuse_setattr_in *arg) |
| 386 | { |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 387 | if (req->f->op.setattr) { |
| 388 | struct stat stbuf; |
| 389 | convert_attr(&arg->attr, &stbuf); |
| 390 | req->f->op.setattr(req, nodeid, &stbuf, arg->valid); |
| 391 | } else |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 392 | fuse_reply_err(req, ENOSYS); |
| 393 | } |
| 394 | |
| 395 | static void do_readlink(fuse_req_t req, fuse_ino_t nodeid) |
| 396 | { |
| 397 | if (req->f->op.readlink) |
| 398 | req->f->op.readlink(req, nodeid); |
| 399 | else |
| 400 | fuse_reply_err(req, ENOSYS); |
| 401 | } |
| 402 | |
| 403 | static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, |
| 404 | struct fuse_mknod_in *arg) |
| 405 | { |
| 406 | if (req->f->op.mknod) |
| 407 | req->f->op.mknod(req, nodeid, PARAM(arg), arg->mode, arg->rdev); |
| 408 | else |
| 409 | fuse_reply_err(req, ENOSYS); |
| 410 | } |
| 411 | |
| 412 | static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, |
| 413 | struct fuse_mkdir_in *arg) |
| 414 | { |
| 415 | if (req->f->op.mkdir) |
| 416 | req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode); |
| 417 | else |
| 418 | fuse_reply_err(req, ENOSYS); |
| 419 | } |
| 420 | |
| 421 | static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, char *name) |
| 422 | { |
| 423 | if (req->f->op.unlink) |
| 424 | req->f->op.unlink(req, nodeid, name); |
| 425 | else |
| 426 | fuse_reply_err(req, ENOSYS); |
| 427 | } |
| 428 | |
| 429 | static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, char *name) |
| 430 | { |
| 431 | if (req->f->op.rmdir) |
| 432 | req->f->op.rmdir(req, nodeid, name); |
| 433 | else |
| 434 | fuse_reply_err(req, ENOSYS); |
| 435 | } |
| 436 | |
| 437 | static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, char *name, |
| 438 | char *link) |
| 439 | { |
| 440 | if (req->f->op.symlink) |
| 441 | req->f->op.symlink(req, link, nodeid, name); |
| 442 | else |
| 443 | fuse_reply_err(req, ENOSYS); |
| 444 | } |
| 445 | |
| 446 | static void do_rename(fuse_req_t req, fuse_ino_t nodeid, |
| 447 | struct fuse_rename_in *arg) |
| 448 | { |
| 449 | char *oldname = PARAM(arg); |
| 450 | char *newname = oldname + strlen(oldname) + 1; |
| 451 | |
| 452 | if (req->f->op.rename) |
| 453 | req->f->op.rename(req, nodeid, oldname, arg->newdir, newname); |
| 454 | else |
| 455 | fuse_reply_err(req, ENOSYS); |
| 456 | } |
| 457 | |
| 458 | static void do_link(fuse_req_t req, fuse_ino_t nodeid, |
| 459 | struct fuse_link_in *arg) |
| 460 | { |
| 461 | if (req->f->op.link) |
| 462 | req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg)); |
| 463 | else |
| 464 | fuse_reply_err(req, ENOSYS); |
| 465 | } |
| 466 | |
| 467 | static void do_open(fuse_req_t req, fuse_ino_t nodeid, |
| 468 | struct fuse_open_in *arg) |
| 469 | { |
| 470 | struct fuse_file_info fi; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 471 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 472 | memset(&fi, 0, sizeof(fi)); |
| 473 | fi.flags = arg->flags; |
| 474 | |
| 475 | if (req->f->op.open) |
| 476 | req->f->op.open(req, nodeid, &fi); |
| 477 | else |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 478 | fuse_reply_open(req, &fi); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 479 | } |
| 480 | |
| 481 | static void do_read(fuse_req_t req, fuse_ino_t nodeid, |
| 482 | struct fuse_read_in *arg) |
| 483 | { |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 484 | if (req->f->op.read) { |
| 485 | struct fuse_file_info fi; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 486 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 487 | memset(&fi, 0, sizeof(fi)); |
| 488 | fi.fh = arg->fh; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 489 | req->f->op.read(req, nodeid, arg->size, arg->offset, &fi); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 490 | } else |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 491 | fuse_reply_err(req, ENOSYS); |
| 492 | } |
| 493 | |
| 494 | static void do_write(fuse_req_t req, fuse_ino_t nodeid, |
| 495 | struct fuse_write_in *arg) |
| 496 | { |
| 497 | struct fuse_file_info fi; |
| 498 | |
| 499 | memset(&fi, 0, sizeof(fi)); |
| 500 | fi.fh = arg->fh; |
| 501 | fi.writepage = arg->write_flags & 1; |
| 502 | |
| 503 | if (req->f->op.write) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 504 | req->f->op.write(req, nodeid, PARAM(arg), arg->size, |
| 505 | arg->offset, &fi); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 506 | else |
| 507 | fuse_reply_err(req, ENOSYS); |
| 508 | } |
| 509 | |
| 510 | static void do_flush(fuse_req_t req, fuse_ino_t nodeid, |
| 511 | struct fuse_flush_in *arg) |
| 512 | { |
| 513 | struct fuse_file_info fi; |
| 514 | |
| 515 | memset(&fi, 0, sizeof(fi)); |
| 516 | fi.fh = arg->fh; |
| 517 | |
| 518 | if (req->f->op.flush) |
| 519 | req->f->op.flush(req, nodeid, &fi); |
| 520 | else |
| 521 | fuse_reply_err(req, ENOSYS); |
| 522 | } |
| 523 | |
| 524 | static void do_release(fuse_req_t req, fuse_ino_t nodeid, |
| 525 | struct fuse_release_in *arg) |
| 526 | { |
| 527 | struct fuse_file_info fi; |
| 528 | |
| 529 | memset(&fi, 0, sizeof(fi)); |
| 530 | fi.flags = arg->flags; |
| 531 | fi.fh = arg->fh; |
| 532 | |
| 533 | if (req->f->op.release) |
| 534 | req->f->op.release(req, nodeid, &fi); |
| 535 | else |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 536 | fuse_reply_err(req, 0); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 540 | struct fuse_fsync_in *inarg) |
| 541 | { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 542 | struct fuse_file_info fi; |
| 543 | |
| 544 | memset(&fi, 0, sizeof(fi)); |
| 545 | fi.fh = inarg->fh; |
| 546 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 547 | if (req->f->op.fsync) |
| 548 | req->f->op.fsync(req, nodeid, inarg->fsync_flags & 1, &fi); |
| 549 | else |
| 550 | fuse_reply_err(req, ENOSYS); |
| 551 | } |
| 552 | |
| 553 | static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, |
| 554 | struct fuse_open_in *arg) |
| 555 | { |
| 556 | struct fuse_file_info fi; |
| 557 | |
| 558 | memset(&fi, 0, sizeof(fi)); |
| 559 | fi.flags = arg->flags; |
| 560 | |
| 561 | if (req->f->op.opendir) |
| 562 | req->f->op.opendir(req, nodeid, &fi); |
| 563 | else |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 564 | fuse_reply_open(req, &fi); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 565 | } |
| 566 | |
| 567 | static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, |
| 568 | struct fuse_read_in *arg) |
| 569 | { |
| 570 | struct fuse_file_info fi; |
| 571 | |
| 572 | memset(&fi, 0, sizeof(fi)); |
| 573 | fi.fh = arg->fh; |
| 574 | |
| 575 | if (req->f->op.readdir) |
| 576 | req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi); |
| 577 | else |
| 578 | fuse_reply_err(req, ENOSYS); |
| 579 | } |
| 580 | |
| 581 | static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, |
| 582 | struct fuse_release_in *arg) |
| 583 | { |
| 584 | struct fuse_file_info fi; |
| 585 | |
| 586 | memset(&fi, 0, sizeof(fi)); |
| 587 | fi.flags = arg->flags; |
| 588 | fi.fh = arg->fh; |
| 589 | |
| 590 | if (req->f->op.releasedir) |
| 591 | req->f->op.releasedir(req, nodeid, &fi); |
| 592 | else |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 593 | fuse_reply_err(req, 0); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 594 | } |
| 595 | |
| 596 | static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, |
| 597 | struct fuse_fsync_in *inarg) |
| 598 | { |
| 599 | struct fuse_file_info fi; |
| 600 | |
| 601 | memset(&fi, 0, sizeof(fi)); |
| 602 | fi.fh = inarg->fh; |
| 603 | |
| 604 | if (req->f->op.fsyncdir) |
| 605 | req->f->op.fsyncdir(req, nodeid, inarg->fsync_flags & 1, &fi); |
| 606 | else |
| 607 | fuse_reply_err(req, ENOSYS); |
| 608 | } |
| 609 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 610 | static void do_statfs(fuse_req_t req) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 611 | { |
| 612 | if (req->f->op.statfs) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 613 | req->f->op.statfs(req); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 614 | else |
| 615 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 616 | } |
| 617 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 618 | static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 619 | struct fuse_setxattr_in *arg) |
| 620 | { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 621 | char *name = PARAM(arg); |
| 622 | unsigned char *value = name + strlen(name) + 1; |
| 623 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 624 | if (req->f->op.setxattr) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 625 | req->f->op.setxattr(req, nodeid, name, value, arg->size, |
| 626 | arg->flags); |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 627 | else |
| 628 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 629 | } |
| 630 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 631 | static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 632 | struct fuse_getxattr_in *arg) |
| 633 | { |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 634 | if (req->f->op.getxattr) |
| 635 | req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 636 | else |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 637 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 638 | } |
| 639 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 640 | static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 641 | struct fuse_getxattr_in *arg) |
| 642 | { |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 643 | if (req->f->op.listxattr) |
| 644 | req->f->op.listxattr(req, nodeid, arg->size); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 645 | else |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 646 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 647 | } |
| 648 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 649 | static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, char *name) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 650 | { |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 651 | if (req->f->op.removexattr) |
| 652 | req->f->op.removexattr(req, nodeid, name); |
| 653 | else |
| 654 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 657 | static void do_init(struct fuse_ll *f, uint64_t unique, |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 658 | struct fuse_init_in_out *arg) |
| 659 | { |
| 660 | struct fuse_init_in_out outarg; |
| 661 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 662 | if (f->debug) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 663 | printf("INIT: %u.%u\n", arg->major, arg->minor); |
| 664 | fflush(stdout); |
| 665 | } |
| 666 | f->got_init = 1; |
| 667 | if (f->op.init) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 668 | f->userdata = f->op.init(f->userdata); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 669 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 670 | f->major = FUSE_KERNEL_VERSION; |
| 671 | f->minor = FUSE_KERNEL_MINOR_VERSION; |
| 672 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 673 | memset(&outarg, 0, sizeof(outarg)); |
| 674 | outarg.major = f->major; |
| 675 | outarg.minor = f->minor; |
| 676 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 677 | if (f->debug) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 678 | printf(" INIT: %u.%u\n", outarg.major, outarg.minor); |
| 679 | fflush(stdout); |
| 680 | } |
| 681 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 682 | send_reply(f, unique, 0, &outarg, sizeof(outarg)); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 683 | } |
| 684 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 685 | void *fuse_req_userdata(fuse_req_t req) |
| 686 | { |
| 687 | return req->f->userdata; |
| 688 | } |
| 689 | |
| 690 | const struct fuse_ctx *fuse_req_ctx(fuse_req_t req) |
| 691 | { |
| 692 | return &req->ctx; |
| 693 | } |
| 694 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 695 | static void free_cmd(struct fuse_cmd *cmd) |
| 696 | { |
| 697 | free(cmd->buf); |
| 698 | free(cmd); |
| 699 | } |
| 700 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 701 | void fuse_ll_process_cmd(struct fuse_ll *f, struct fuse_cmd *cmd) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 702 | { |
| 703 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 704 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 705 | struct fuse_req *req; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 706 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 707 | fuse_dec_avail(f); |
| 708 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 709 | if (f->debug) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 710 | printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n", |
| 711 | in->unique, opname(in->opcode), in->opcode, |
| 712 | (unsigned long) in->nodeid, cmd->buflen); |
| 713 | fflush(stdout); |
| 714 | } |
| 715 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 716 | if (!f->got_init) { |
| 717 | if (in->opcode != FUSE_INIT) |
| 718 | send_reply(f, in->unique, -EPROTO, NULL, 0); |
| 719 | else |
| 720 | do_init(f, in->unique, (struct fuse_init_in_out *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 721 | goto out; |
| 722 | } |
| 723 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 724 | if (f->allow_root && in->uid != f->owner && in->uid != 0 && |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 725 | in->opcode != FUSE_INIT && in->opcode != FUSE_READ && |
| 726 | in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC && |
| 727 | in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR && |
| 728 | in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) { |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 729 | send_reply(f, in->unique, -EACCES, NULL, 0); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 730 | goto out; |
| 731 | } |
| 732 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 733 | req = (struct fuse_req *) malloc(sizeof(struct fuse_req)); |
| 734 | if (req == NULL) { |
| 735 | fprintf(stderr, "fuse: failed to allocate request\n"); |
| 736 | goto out; |
| 737 | } |
| 738 | |
| 739 | req->f = f; |
| 740 | req->unique = in->unique; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 741 | req->ctx.uid = in->uid; |
| 742 | req->ctx.gid = in->gid; |
| 743 | req->ctx.pid = in->pid; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 744 | |
| 745 | switch (in->opcode) { |
| 746 | case FUSE_LOOKUP: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 747 | do_lookup(req, in->nodeid, (char *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 748 | break; |
| 749 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 750 | case FUSE_FORGET: |
| 751 | do_forget(req, in->nodeid, (struct fuse_forget_in *) inarg); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 752 | break; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 753 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 754 | case FUSE_GETATTR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 755 | do_getattr(req, in->nodeid); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 756 | break; |
| 757 | |
| 758 | case FUSE_SETATTR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 759 | do_setattr(req, in->nodeid, (struct fuse_setattr_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 760 | break; |
| 761 | |
| 762 | case FUSE_READLINK: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 763 | do_readlink(req, in->nodeid); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 764 | break; |
| 765 | |
| 766 | case FUSE_MKNOD: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 767 | do_mknod(req, in->nodeid, (struct fuse_mknod_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 768 | break; |
| 769 | |
| 770 | case FUSE_MKDIR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 771 | do_mkdir(req, in->nodeid, (struct fuse_mkdir_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 772 | break; |
| 773 | |
| 774 | case FUSE_UNLINK: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 775 | do_unlink(req, in->nodeid, (char *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 776 | break; |
| 777 | |
| 778 | case FUSE_RMDIR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 779 | do_rmdir(req, in->nodeid, (char *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 780 | break; |
| 781 | |
| 782 | case FUSE_SYMLINK: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 783 | do_symlink(req, in->nodeid, (char *) inarg, |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 784 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 785 | break; |
| 786 | |
| 787 | case FUSE_RENAME: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 788 | do_rename(req, in->nodeid, (struct fuse_rename_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 789 | break; |
| 790 | |
| 791 | case FUSE_LINK: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 792 | do_link(req, in->nodeid, (struct fuse_link_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 793 | break; |
| 794 | |
| 795 | case FUSE_OPEN: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 796 | do_open(req, in->nodeid, (struct fuse_open_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 797 | break; |
| 798 | |
| 799 | case FUSE_FLUSH: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 800 | do_flush(req, in->nodeid, (struct fuse_flush_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 801 | break; |
| 802 | |
| 803 | case FUSE_RELEASE: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 804 | do_release(req, in->nodeid, (struct fuse_release_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 805 | break; |
| 806 | |
| 807 | case FUSE_READ: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 808 | do_read(req, in->nodeid, (struct fuse_read_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 809 | break; |
| 810 | |
| 811 | case FUSE_WRITE: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 812 | do_write(req, in->nodeid, (struct fuse_write_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 813 | break; |
| 814 | |
| 815 | case FUSE_STATFS: |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 816 | do_statfs(req); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 817 | break; |
| 818 | |
| 819 | case FUSE_FSYNC: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 820 | do_fsync(req, in->nodeid, (struct fuse_fsync_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 821 | break; |
| 822 | |
| 823 | case FUSE_SETXATTR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 824 | do_setxattr(req, in->nodeid, (struct fuse_setxattr_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 825 | break; |
| 826 | |
| 827 | case FUSE_GETXATTR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 828 | do_getxattr(req, in->nodeid, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 829 | break; |
| 830 | |
| 831 | case FUSE_LISTXATTR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 832 | do_listxattr(req, in->nodeid, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 833 | break; |
| 834 | |
| 835 | case FUSE_REMOVEXATTR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 836 | do_removexattr(req, in->nodeid, (char *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 837 | break; |
| 838 | |
| 839 | case FUSE_OPENDIR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 840 | do_opendir(req, in->nodeid, (struct fuse_open_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 841 | break; |
| 842 | |
| 843 | case FUSE_READDIR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 844 | do_readdir(req, in->nodeid, (struct fuse_read_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 845 | break; |
| 846 | |
| 847 | case FUSE_RELEASEDIR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 848 | do_releasedir(req, in->nodeid, (struct fuse_release_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 849 | break; |
| 850 | |
| 851 | case FUSE_FSYNCDIR: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 852 | do_fsyncdir(req, in->nodeid, (struct fuse_fsync_in *) inarg); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 853 | break; |
| 854 | |
| 855 | default: |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 856 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 857 | } |
| 858 | |
| 859 | out: |
| 860 | free_cmd(cmd); |
| 861 | } |
| 862 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 863 | void fuse_ll_exit(struct fuse_ll *f) |
| 864 | { |
| 865 | f->exited = 1; |
| 866 | } |
| 867 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 868 | int fuse_ll_exited(struct fuse_ll* f) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 869 | { |
| 870 | return f->exited; |
| 871 | } |
| 872 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 873 | struct fuse_cmd *fuse_ll_read_cmd(struct fuse_ll *f) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 874 | { |
| 875 | ssize_t res; |
| 876 | struct fuse_cmd *cmd; |
| 877 | struct fuse_in_header *in; |
| 878 | void *inarg; |
| 879 | |
| 880 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
| 881 | if (cmd == NULL) { |
| 882 | fprintf(stderr, "fuse: failed to allocate cmd in read\n"); |
| 883 | return NULL; |
| 884 | } |
| 885 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
| 886 | if (cmd->buf == NULL) { |
| 887 | fprintf(stderr, "fuse: failed to allocate read buffer\n"); |
| 888 | free(cmd); |
| 889 | return NULL; |
| 890 | } |
| 891 | in = (struct fuse_in_header *) cmd->buf; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 892 | inarg = cmd->buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 893 | |
| 894 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 895 | if (res == -1) { |
| 896 | free_cmd(cmd); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 897 | if (fuse_ll_exited(f) || errno == EINTR || errno == ENOENT) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 898 | return NULL; |
| 899 | |
| 900 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 901 | if (errno != ENODEV) { |
| 902 | /* BAD... This will happen again */ |
| 903 | perror("fuse: reading device"); |
| 904 | } |
| 905 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 906 | fuse_ll_exit(f); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 907 | return NULL; |
| 908 | } |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 909 | if ((size_t) res < sizeof(struct fuse_in_header)) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 910 | free_cmd(cmd); |
| 911 | /* Cannot happen */ |
| 912 | fprintf(stderr, "short read on fuse device\n"); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 913 | fuse_ll_exit(f); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 914 | return NULL; |
| 915 | } |
| 916 | cmd->buflen = res; |
| 917 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 918 | |
| 919 | return cmd; |
| 920 | } |
| 921 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 922 | int fuse_ll_loop(struct fuse_ll *f) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 923 | { |
| 924 | if (f == NULL) |
| 925 | return -1; |
| 926 | |
| 927 | while (1) { |
| 928 | struct fuse_cmd *cmd; |
| 929 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 930 | if (fuse_ll_exited(f)) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 931 | break; |
| 932 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 933 | cmd = fuse_ll_read_cmd(f); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 934 | if (cmd == NULL) |
| 935 | continue; |
| 936 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 937 | fuse_ll_process_cmd(f, cmd); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 938 | } |
| 939 | f->exited = 0; |
| 940 | return 0; |
| 941 | } |
| 942 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 943 | int fuse_ll_is_lib_option(const char *opt) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 944 | { |
| 945 | if (strcmp(opt, "debug") == 0 || |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 946 | strcmp(opt, "allow_root") == 0) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 947 | return 1; |
| 948 | else |
| 949 | return 0; |
| 950 | } |
| 951 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 952 | static int parse_ll_opts(struct fuse_ll *f, const char *opts) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 953 | { |
| 954 | if (opts) { |
| 955 | char *xopts = strdup(opts); |
| 956 | char *s = xopts; |
| 957 | char *opt; |
| 958 | |
| 959 | if (xopts == NULL) { |
| 960 | fprintf(stderr, "fuse: memory allocation failed\n"); |
| 961 | return -1; |
| 962 | } |
| 963 | |
| 964 | while((opt = strsep(&s, ","))) { |
| 965 | if (strcmp(opt, "debug") == 0) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 966 | f->debug = 1; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 967 | else if (strcmp(opt, "allow_root") == 0) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 968 | f->allow_root = 1; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 969 | else |
| 970 | fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt); |
| 971 | } |
| 972 | free(xopts); |
| 973 | } |
| 974 | return 0; |
| 975 | } |
| 976 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 977 | struct fuse_ll *fuse_ll_new(int fd, const char *opts, |
| 978 | const struct fuse_ll_operations *op, |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 979 | size_t op_size, void *userdata) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 980 | { |
| 981 | struct fuse_ll *f; |
| 982 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 983 | if (sizeof(struct fuse_ll_operations) < op_size) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 984 | fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n"); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 985 | op_size = sizeof(struct fuse_ll_operations); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 986 | } |
| 987 | |
| 988 | f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll)); |
| 989 | if (f == NULL) { |
| 990 | fprintf(stderr, "fuse: failed to allocate fuse object\n"); |
| 991 | goto out; |
| 992 | } |
| 993 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 994 | if (parse_ll_opts(f, opts) == -1) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 995 | goto out_free; |
| 996 | |
| 997 | f->fd = fd; |
| 998 | memcpy(&f->op, op, op_size); |
| 999 | f->exited = 0; |
| 1000 | f->owner = getuid(); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 1001 | f->userdata = userdata; |
| 1002 | mutex_init(&f->worker_lock); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1003 | |
| 1004 | return f; |
| 1005 | |
| 1006 | out_free: |
| 1007 | free(f); |
| 1008 | out: |
| 1009 | return NULL; |
| 1010 | } |
| 1011 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 1012 | void fuse_ll_destroy(struct fuse_ll *f) |
| 1013 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame^] | 1014 | if (f->op.destroy) |
| 1015 | f->op.destroy(f->userdata); |
| 1016 | |
| 1017 | pthread_mutex_destroy(&f->worker_lock); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 1018 | free(f); |
| 1019 | } |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1020 | |