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