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