Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2006 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 4 | |
| 5 | This program can be distributed under the terms of the GNU LGPL. |
| 6 | See the file COPYING.LIB |
| 7 | */ |
| 8 | |
Miklos Szeredi | 9d3b7da | 2006-05-08 12:35:04 +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 | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 13 | #include "fuse_i.h" |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 14 | |
| 15 | #include <stdio.h> |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 16 | #include <stdlib.h> |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 17 | #include <stddef.h> |
| 18 | #include <string.h> |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 19 | #include <unistd.h> |
| 20 | #include <limits.h> |
| 21 | #include <errno.h> |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 22 | #include <pthread.h> |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 23 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 24 | #define PARAM(inarg) (((char *)(inarg)) + sizeof(*(inarg))) |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 25 | #define OFFSET_MAX 0x7fffffffffffffffLL |
| 26 | |
| 27 | struct fuse_ll; |
| 28 | |
| 29 | struct fuse_req { |
| 30 | struct fuse_ll *f; |
| 31 | uint64_t unique; |
| 32 | struct fuse_ctx ctx; |
| 33 | struct fuse_chan *ch; |
| 34 | int interrupted; |
| 35 | union { |
| 36 | struct { |
| 37 | uint64_t unique; |
| 38 | } i; |
| 39 | struct { |
| 40 | fuse_interrupt_func_t func; |
| 41 | void *data; |
| 42 | } ni; |
| 43 | } u; |
| 44 | struct fuse_req *next; |
| 45 | struct fuse_req *prev; |
| 46 | }; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 47 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 48 | struct fuse_ll { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 49 | int debug; |
| 50 | int allow_root; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 51 | struct fuse_lowlevel_ops op; |
| 52 | int got_init; |
| 53 | void *userdata; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 54 | uid_t owner; |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 55 | struct fuse_conn_info conn; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 56 | struct fuse_req list; |
| 57 | struct fuse_req interrupts; |
| 58 | pthread_mutex_t lock; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 59 | }; |
| 60 | |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 61 | #ifndef USE_UCLIBC |
| 62 | #define mutex_init(mut) pthread_mutex_init(mut, NULL) |
| 63 | #else |
| 64 | static void mutex_init(pthread_mutex_t *mut) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 65 | { |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 66 | pthread_mutexattr_t attr; |
| 67 | pthread_mutexattr_init(&attr); |
| 68 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 69 | pthread_mutex_init(mut, &attr); |
| 70 | pthread_mutexattr_destroy(&attr); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 71 | } |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 72 | #endif |
| 73 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 74 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 75 | static void convert_stat(const struct stat *stbuf, struct fuse_attr *attr) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 76 | { |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 77 | attr->ino = stbuf->st_ino; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 78 | attr->mode = stbuf->st_mode; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 79 | attr->nlink = stbuf->st_nlink; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 80 | attr->uid = stbuf->st_uid; |
| 81 | attr->gid = stbuf->st_gid; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 82 | attr->rdev = stbuf->st_rdev; |
| 83 | attr->size = stbuf->st_size; |
| 84 | attr->blocks = stbuf->st_blocks; |
| 85 | attr->atime = stbuf->st_atime; |
| 86 | attr->mtime = stbuf->st_mtime; |
| 87 | attr->ctime = stbuf->st_ctime; |
| 88 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
| 89 | attr->atimensec = stbuf->st_atim.tv_nsec; |
| 90 | attr->mtimensec = stbuf->st_mtim.tv_nsec; |
| 91 | attr->ctimensec = stbuf->st_ctim.tv_nsec; |
| 92 | #endif |
| 93 | } |
| 94 | |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 95 | 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] | 96 | { |
| 97 | stbuf->st_mode = attr->mode; |
| 98 | stbuf->st_uid = attr->uid; |
| 99 | stbuf->st_gid = attr->gid; |
| 100 | stbuf->st_size = attr->size; |
| 101 | stbuf->st_atime = attr->atime; |
| 102 | stbuf->st_mtime = attr->mtime; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 103 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
| 104 | stbuf->st_atim.tv_nsec = attr->atimensec; |
| 105 | stbuf->st_mtim.tv_nsec = attr->mtimensec; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 106 | #endif |
| 107 | } |
| 108 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 109 | static size_t iov_length(const struct iovec *iov, size_t count) |
| 110 | { |
| 111 | size_t seg; |
| 112 | size_t ret = 0; |
| 113 | |
| 114 | for (seg = 0; seg < count; seg++) |
| 115 | ret += iov[seg].iov_len; |
| 116 | return ret; |
| 117 | } |
| 118 | |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 119 | static void list_init_req(struct fuse_req *req) |
| 120 | { |
| 121 | req->next = req; |
| 122 | req->prev = req; |
| 123 | } |
| 124 | |
| 125 | static void list_del_req(struct fuse_req *req) |
| 126 | { |
| 127 | struct fuse_req *prev = req->prev; |
| 128 | struct fuse_req *next = req->next; |
| 129 | prev->next = next; |
| 130 | next->prev = prev; |
| 131 | } |
| 132 | |
| 133 | static void list_add_req(struct fuse_req *req, struct fuse_req *next) |
| 134 | { |
| 135 | struct fuse_req *prev = next->prev; |
| 136 | req->next = next; |
| 137 | req->prev = prev; |
| 138 | prev->next = req; |
| 139 | next->prev = req; |
| 140 | } |
| 141 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 142 | static void free_req(fuse_req_t req) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 143 | { |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 144 | pthread_mutex_lock(&req->f->lock); |
| 145 | list_del_req(req); |
| 146 | pthread_mutex_unlock(&req->f->lock); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 147 | free(req); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 148 | } |
| 149 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 150 | static int send_reply(fuse_req_t req, int error, const void *arg, |
| 151 | size_t argsize) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 152 | { |
| 153 | struct fuse_out_header out; |
| 154 | struct iovec iov[2]; |
| 155 | size_t count; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 156 | int res; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 157 | |
| 158 | if (error <= -1000 || error > 0) { |
| 159 | fprintf(stderr, "fuse: bad error value: %i\n", error); |
| 160 | error = -ERANGE; |
| 161 | } |
| 162 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 163 | out.unique = req->unique; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 164 | out.error = error; |
| 165 | count = 1; |
| 166 | iov[0].iov_base = &out; |
| 167 | iov[0].iov_len = sizeof(struct fuse_out_header); |
| 168 | if (argsize && !error) { |
| 169 | count++; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 170 | iov[1].iov_base = (void *) arg; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 171 | iov[1].iov_len = argsize; |
| 172 | } |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 173 | out.len = iov_length(iov, count); |
| 174 | |
| 175 | if (req->f->debug) { |
| 176 | printf(" unique: %llu, error: %i (%s), outsize: %i\n", |
| 177 | out.unique, out.error, strerror(-out.error), out.len); |
| 178 | fflush(stdout); |
| 179 | } |
| 180 | res = fuse_chan_send(req->ch, iov, count); |
| 181 | free_req(req); |
| 182 | |
| 183 | return res; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 186 | size_t fuse_dirent_size(size_t namelen) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 187 | { |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 188 | return FUSE_DIRENT_ALIGN(FUSE_NAME_OFFSET + namelen); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 189 | } |
| 190 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 191 | 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] | 192 | off_t off) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 193 | { |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 194 | unsigned namelen = strlen(name); |
| 195 | unsigned entlen = FUSE_NAME_OFFSET + namelen; |
| 196 | unsigned entsize = fuse_dirent_size(namelen); |
| 197 | unsigned padlen = entsize - entlen; |
| 198 | struct fuse_dirent *dirent = (struct fuse_dirent *) buf; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 199 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 200 | dirent->ino = stbuf->st_ino; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 201 | dirent->off = off; |
| 202 | dirent->namelen = namelen; |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 203 | dirent->type = (stbuf->st_mode & 0170000) >> 12; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 204 | strncpy(dirent->name, name, namelen); |
| 205 | if (padlen) |
| 206 | memset(buf + entlen, 0, padlen); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 207 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 208 | return buf + entsize; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 209 | } |
| 210 | |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 211 | size_t fuse_add_direntry(fuse_req_t req, char *buf, size_t bufsize, |
| 212 | const char *name, const struct stat *stbuf, off_t off) |
| 213 | { |
| 214 | size_t entsize; |
| 215 | |
| 216 | (void) req; |
| 217 | entsize = fuse_dirent_size(strlen(name)); |
| 218 | if (entsize <= bufsize && buf) |
| 219 | fuse_add_dirent(buf, name, stbuf, off); |
| 220 | return entsize; |
| 221 | } |
| 222 | |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 223 | static void convert_statfs(const struct statvfs *stbuf, |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 224 | struct fuse_kstatfs *kstatfs) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 225 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 226 | kstatfs->bsize = stbuf->f_bsize; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 227 | kstatfs->frsize = stbuf->f_frsize; |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 228 | kstatfs->blocks = stbuf->f_blocks; |
| 229 | kstatfs->bfree = stbuf->f_bfree; |
| 230 | kstatfs->bavail = stbuf->f_bavail; |
| 231 | kstatfs->files = stbuf->f_files; |
| 232 | kstatfs->ffree = stbuf->f_ffree; |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 233 | kstatfs->namelen = stbuf->f_namemax; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 234 | } |
| 235 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 236 | 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] | 237 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 238 | return send_reply(req, 0, arg, argsize); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | int fuse_reply_err(fuse_req_t req, int err) |
| 242 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 243 | return send_reply(req, -err, NULL, 0); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 244 | } |
| 245 | |
Miklos Szeredi | 836ab71 | 2005-10-03 14:11:59 +0000 | [diff] [blame] | 246 | void fuse_reply_none(fuse_req_t req) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 247 | { |
Miklos Szeredi | 8d975f6 | 2006-03-17 15:56:05 +0000 | [diff] [blame] | 248 | fuse_chan_send(req->ch, NULL, 0); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 249 | free_req(req); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | static unsigned long calc_timeout_sec(double t) |
| 253 | { |
| 254 | if (t > (double) ULONG_MAX) |
| 255 | return ULONG_MAX; |
| 256 | else if (t < 0.0) |
| 257 | return 0; |
| 258 | else |
| 259 | return (unsigned long) t; |
| 260 | } |
| 261 | |
| 262 | static unsigned int calc_timeout_nsec(double t) |
| 263 | { |
| 264 | double f = t - (double) calc_timeout_sec(t); |
| 265 | if (f < 0.0) |
| 266 | return 0; |
| 267 | else if (f >= 0.999999999) |
| 268 | return 999999999; |
| 269 | else |
| 270 | return (unsigned int) (f * 1.0e9); |
| 271 | } |
| 272 | |
Miklos Szeredi | b0c52c5 | 2005-08-23 15:39:43 +0000 | [diff] [blame] | 273 | static void fill_entry(struct fuse_entry_out *arg, |
| 274 | const struct fuse_entry_param *e) |
| 275 | { |
| 276 | arg->nodeid = e->ino; |
| 277 | arg->generation = e->generation; |
| 278 | arg->entry_valid = calc_timeout_sec(e->entry_timeout); |
| 279 | arg->entry_valid_nsec = calc_timeout_nsec(e->entry_timeout); |
| 280 | arg->attr_valid = calc_timeout_sec(e->attr_timeout); |
| 281 | arg->attr_valid_nsec = calc_timeout_nsec(e->attr_timeout); |
| 282 | convert_stat(&e->attr, &arg->attr); |
| 283 | } |
| 284 | |
| 285 | static void fill_open(struct fuse_open_out *arg, |
| 286 | const struct fuse_file_info *f) |
| 287 | { |
| 288 | arg->fh = f->fh; |
| 289 | if (f->direct_io) |
| 290 | arg->open_flags |= FOPEN_DIRECT_IO; |
| 291 | if (f->keep_cache) |
| 292 | arg->open_flags |= FOPEN_KEEP_CACHE; |
| 293 | } |
| 294 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 295 | int fuse_reply_entry(fuse_req_t req, const struct fuse_entry_param *e) |
| 296 | { |
| 297 | struct fuse_entry_out arg; |
| 298 | |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 299 | /* before ABI 7.4 e->ino == 0 was invalid, only ENOENT meant |
| 300 | negative entry */ |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 301 | if (!e->ino && req->f->conn.proto_minor < 4) |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 302 | return fuse_reply_err(req, ENOENT); |
| 303 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 304 | memset(&arg, 0, sizeof(arg)); |
Miklos Szeredi | b0c52c5 | 2005-08-23 15:39:43 +0000 | [diff] [blame] | 305 | fill_entry(&arg, e); |
| 306 | return send_reply_ok(req, &arg, sizeof(arg)); |
| 307 | } |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 308 | |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 309 | int fuse_reply_create(fuse_req_t req, const struct fuse_entry_param *e, |
| 310 | const struct fuse_file_info *f) |
| 311 | { |
| 312 | struct { |
| 313 | struct fuse_entry_out e; |
| 314 | struct fuse_open_out o; |
| 315 | } arg; |
| 316 | |
| 317 | memset(&arg, 0, sizeof(arg)); |
| 318 | fill_entry(&arg.e, e); |
| 319 | fill_open(&arg.o, f); |
| 320 | return send_reply_ok(req, &arg, sizeof(arg)); |
| 321 | } |
| 322 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 323 | int fuse_reply_attr(fuse_req_t req, const struct stat *attr, |
| 324 | double attr_timeout) |
| 325 | { |
| 326 | struct fuse_attr_out arg; |
| 327 | |
| 328 | memset(&arg, 0, sizeof(arg)); |
| 329 | arg.attr_valid = calc_timeout_sec(attr_timeout); |
| 330 | arg.attr_valid_nsec = calc_timeout_nsec(attr_timeout); |
| 331 | convert_stat(attr, &arg.attr); |
| 332 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 333 | return send_reply_ok(req, &arg, sizeof(arg)); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 336 | int fuse_reply_readlink(fuse_req_t req, const char *linkname) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 337 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 338 | return send_reply_ok(req, linkname, strlen(linkname)); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 339 | } |
| 340 | |
| 341 | int fuse_reply_open(fuse_req_t req, const struct fuse_file_info *f) |
| 342 | { |
| 343 | struct fuse_open_out arg; |
| 344 | |
| 345 | memset(&arg, 0, sizeof(arg)); |
Miklos Szeredi | b0c52c5 | 2005-08-23 15:39:43 +0000 | [diff] [blame] | 346 | fill_open(&arg, f); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 347 | return send_reply_ok(req, &arg, sizeof(arg)); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | int fuse_reply_write(fuse_req_t req, size_t count) |
| 351 | { |
| 352 | struct fuse_write_out arg; |
| 353 | |
| 354 | memset(&arg, 0, sizeof(arg)); |
| 355 | arg.size = count; |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 356 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 357 | return send_reply_ok(req, &arg, sizeof(arg)); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 358 | } |
| 359 | |
| 360 | int fuse_reply_buf(fuse_req_t req, const char *buf, size_t size) |
| 361 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 362 | return send_reply_ok(req, buf, size); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 363 | } |
| 364 | |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 365 | int fuse_reply_statfs(fuse_req_t req, const struct statvfs *stbuf) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 366 | { |
| 367 | struct fuse_statfs_out arg; |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 368 | size_t size = req->f->conn.proto_minor < 4 ? FUSE_COMPAT_STATFS_SIZE : sizeof(arg); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 369 | |
| 370 | memset(&arg, 0, sizeof(arg)); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 371 | convert_statfs(stbuf, &arg.st); |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 372 | |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 373 | return send_reply_ok(req, &arg, size); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 374 | } |
| 375 | |
| 376 | int fuse_reply_xattr(fuse_req_t req, size_t count) |
| 377 | { |
| 378 | struct fuse_getxattr_out arg; |
| 379 | |
| 380 | memset(&arg, 0, sizeof(arg)); |
| 381 | arg.size = count; |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 382 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 383 | return send_reply_ok(req, &arg, sizeof(arg)); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 386 | int fuse_reply_lock(fuse_req_t req, struct flock *lock) |
| 387 | { |
| 388 | struct fuse_lk_out arg; |
| 389 | |
| 390 | memset(&arg, 0, sizeof(arg)); |
| 391 | arg.lk.type = lock->l_type; |
| 392 | if (lock->l_type != F_UNLCK) { |
| 393 | arg.lk.start = lock->l_start; |
| 394 | if (lock->l_len == 0) |
| 395 | arg.lk.end = OFFSET_MAX; |
| 396 | else |
| 397 | arg.lk.end = lock->l_start + lock->l_len - 1; |
| 398 | } |
| 399 | arg.lk.pid = lock->l_pid; |
| 400 | return send_reply_ok(req, &arg, sizeof(arg)); |
| 401 | } |
| 402 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 403 | static void do_lookup(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 404 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 405 | char *name = (char *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 406 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 407 | if (req->f->op.lookup) |
| 408 | req->f->op.lookup(req, nodeid, name); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 409 | else |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 410 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 411 | } |
| 412 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 413 | static void do_forget(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 414 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 415 | struct fuse_forget_in *arg = (struct fuse_forget_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 416 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 417 | if (req->f->op.forget) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 418 | req->f->op.forget(req, nodeid, arg->nlookup); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 419 | } |
| 420 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 421 | static void do_getattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 422 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 423 | (void) inarg; |
| 424 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 425 | if (req->f->op.getattr) |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 426 | req->f->op.getattr(req, nodeid, NULL); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 427 | else |
| 428 | fuse_reply_err(req, ENOSYS); |
| 429 | } |
| 430 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 431 | static void do_setattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 432 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 433 | struct fuse_setattr_in *arg = (struct fuse_setattr_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 434 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 435 | if (req->f->op.setattr) { |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 436 | struct fuse_file_info *fi = NULL; |
| 437 | struct fuse_file_info fi_store; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 438 | struct stat stbuf; |
Miklos Szeredi | e3b8309 | 2005-07-22 17:24:30 +0000 | [diff] [blame] | 439 | memset(&stbuf, 0, sizeof(stbuf)); |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 440 | convert_attr(arg, &stbuf); |
| 441 | if (arg->valid & FATTR_FH) { |
| 442 | arg->valid &= ~FATTR_FH; |
| 443 | memset(&fi_store, 0, sizeof(fi_store)); |
| 444 | fi = &fi_store; |
| 445 | fi->fh = arg->fh; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 446 | fi->fh_old = fi->fh; |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 447 | } |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 448 | req->f->op.setattr(req, nodeid, &stbuf, arg->valid, fi); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 449 | } else |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 450 | fuse_reply_err(req, ENOSYS); |
| 451 | } |
| 452 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 453 | static void do_access(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 454 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 455 | struct fuse_access_in *arg = (struct fuse_access_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 456 | |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 457 | if (req->f->op.access) |
| 458 | req->f->op.access(req, nodeid, arg->mask); |
| 459 | else |
| 460 | fuse_reply_err(req, ENOSYS); |
| 461 | } |
| 462 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 463 | static void do_readlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 464 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 465 | (void) inarg; |
| 466 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 467 | if (req->f->op.readlink) |
| 468 | req->f->op.readlink(req, nodeid); |
| 469 | else |
| 470 | fuse_reply_err(req, ENOSYS); |
| 471 | } |
| 472 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 473 | static void do_mknod(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 474 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 475 | struct fuse_mknod_in *arg = (struct fuse_mknod_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 476 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 477 | if (req->f->op.mknod) |
| 478 | req->f->op.mknod(req, nodeid, PARAM(arg), arg->mode, arg->rdev); |
| 479 | else |
| 480 | fuse_reply_err(req, ENOSYS); |
| 481 | } |
| 482 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 483 | static void do_mkdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 484 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 485 | struct fuse_mkdir_in *arg = (struct fuse_mkdir_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 486 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 487 | if (req->f->op.mkdir) |
| 488 | req->f->op.mkdir(req, nodeid, PARAM(arg), arg->mode); |
| 489 | else |
| 490 | fuse_reply_err(req, ENOSYS); |
| 491 | } |
| 492 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 493 | static void do_unlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 494 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 495 | char *name = (char *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 496 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 497 | if (req->f->op.unlink) |
| 498 | req->f->op.unlink(req, nodeid, name); |
| 499 | else |
| 500 | fuse_reply_err(req, ENOSYS); |
| 501 | } |
| 502 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 503 | static void do_rmdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 504 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 505 | char *name = (char *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 506 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 507 | if (req->f->op.rmdir) |
| 508 | req->f->op.rmdir(req, nodeid, name); |
| 509 | else |
| 510 | fuse_reply_err(req, ENOSYS); |
| 511 | } |
| 512 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 513 | static void do_symlink(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 514 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 515 | char *name = (char *) inarg; |
| 516 | char *linkname = ((char *) inarg) + strlen((char *) inarg) + 1; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 517 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 518 | if (req->f->op.symlink) |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 519 | req->f->op.symlink(req, linkname, nodeid, name); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 520 | else |
| 521 | fuse_reply_err(req, ENOSYS); |
| 522 | } |
| 523 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 524 | static void do_rename(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 525 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 526 | struct fuse_rename_in *arg = (struct fuse_rename_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 527 | char *oldname = PARAM(arg); |
| 528 | char *newname = oldname + strlen(oldname) + 1; |
| 529 | |
| 530 | if (req->f->op.rename) |
| 531 | req->f->op.rename(req, nodeid, oldname, arg->newdir, newname); |
| 532 | else |
| 533 | fuse_reply_err(req, ENOSYS); |
| 534 | } |
| 535 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 536 | static void do_link(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 537 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 538 | struct fuse_link_in *arg = (struct fuse_link_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 539 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 540 | if (req->f->op.link) |
| 541 | req->f->op.link(req, arg->oldnodeid, nodeid, PARAM(arg)); |
| 542 | else |
| 543 | fuse_reply_err(req, ENOSYS); |
| 544 | } |
| 545 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 546 | static void do_create(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 547 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 548 | struct fuse_open_in *arg = (struct fuse_open_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 549 | |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 550 | if (req->f->op.create) { |
| 551 | struct fuse_file_info fi; |
| 552 | |
| 553 | memset(&fi, 0, sizeof(fi)); |
| 554 | fi.flags = arg->flags; |
| 555 | |
| 556 | req->f->op.create(req, nodeid, PARAM(arg), arg->mode, &fi); |
| 557 | } else |
| 558 | fuse_reply_err(req, ENOSYS); |
| 559 | } |
| 560 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 561 | static void do_open(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 562 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 563 | struct fuse_open_in *arg = (struct fuse_open_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 564 | struct fuse_file_info fi; |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 565 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 566 | memset(&fi, 0, sizeof(fi)); |
| 567 | fi.flags = arg->flags; |
| 568 | |
| 569 | if (req->f->op.open) |
| 570 | req->f->op.open(req, nodeid, &fi); |
| 571 | else |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 572 | fuse_reply_open(req, &fi); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 573 | } |
| 574 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 575 | static void do_read(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 576 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 577 | struct fuse_read_in *arg = (struct fuse_read_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 578 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 579 | if (req->f->op.read) { |
| 580 | struct fuse_file_info fi; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 581 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 582 | memset(&fi, 0, sizeof(fi)); |
| 583 | fi.fh = arg->fh; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 584 | fi.fh_old = fi.fh; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 585 | req->f->op.read(req, nodeid, arg->size, arg->offset, &fi); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 586 | } else |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 587 | fuse_reply_err(req, ENOSYS); |
| 588 | } |
| 589 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 590 | static void do_write(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 591 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 592 | struct fuse_write_in *arg = (struct fuse_write_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 593 | struct fuse_file_info fi; |
| 594 | |
| 595 | memset(&fi, 0, sizeof(fi)); |
| 596 | fi.fh = arg->fh; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 597 | fi.fh_old = fi.fh; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 598 | fi.writepage = arg->write_flags & 1; |
| 599 | |
| 600 | if (req->f->op.write) |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 601 | req->f->op.write(req, nodeid, PARAM(arg), arg->size, arg->offset, &fi); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 602 | else |
| 603 | fuse_reply_err(req, ENOSYS); |
| 604 | } |
| 605 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 606 | static void do_flush(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 607 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 608 | struct fuse_flush_in *arg = (struct fuse_flush_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 609 | struct fuse_file_info fi; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 610 | uint64_t lock_owner = 0; |
| 611 | |
| 612 | if (req->f->conn.proto_minor >= 7) |
| 613 | lock_owner = arg->lock_owner; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 614 | |
| 615 | memset(&fi, 0, sizeof(fi)); |
| 616 | fi.fh = arg->fh; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 617 | fi.fh_old = fi.fh; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 618 | |
| 619 | if (req->f->op.flush) |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 620 | req->f->op.flush(req, nodeid, &fi, lock_owner); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 621 | else |
| 622 | fuse_reply_err(req, ENOSYS); |
| 623 | } |
| 624 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 625 | static void do_release(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 626 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 627 | struct fuse_release_in *arg = (struct fuse_release_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 628 | struct fuse_file_info fi; |
| 629 | |
| 630 | memset(&fi, 0, sizeof(fi)); |
| 631 | fi.flags = arg->flags; |
| 632 | fi.fh = arg->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.release) |
| 636 | req->f->op.release(req, nodeid, &fi); |
| 637 | else |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 638 | fuse_reply_err(req, 0); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 639 | } |
| 640 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 641 | static void do_fsync(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 642 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 643 | struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 644 | struct fuse_file_info fi; |
| 645 | |
| 646 | memset(&fi, 0, sizeof(fi)); |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 647 | fi.fh = arg->fh; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 648 | fi.fh_old = fi.fh; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 649 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 650 | if (req->f->op.fsync) |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 651 | req->f->op.fsync(req, nodeid, arg->fsync_flags & 1, &fi); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 652 | else |
| 653 | fuse_reply_err(req, ENOSYS); |
| 654 | } |
| 655 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 656 | static void do_opendir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 657 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 658 | struct fuse_open_in *arg = (struct fuse_open_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 659 | struct fuse_file_info fi; |
| 660 | |
| 661 | memset(&fi, 0, sizeof(fi)); |
| 662 | fi.flags = arg->flags; |
| 663 | |
| 664 | if (req->f->op.opendir) |
| 665 | req->f->op.opendir(req, nodeid, &fi); |
| 666 | else |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 667 | fuse_reply_open(req, &fi); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 668 | } |
| 669 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 670 | static void do_readdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 671 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 672 | struct fuse_read_in *arg = (struct fuse_read_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 673 | struct fuse_file_info fi; |
| 674 | |
| 675 | memset(&fi, 0, sizeof(fi)); |
| 676 | fi.fh = arg->fh; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 677 | fi.fh_old = fi.fh; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 678 | |
| 679 | if (req->f->op.readdir) |
| 680 | req->f->op.readdir(req, nodeid, arg->size, arg->offset, &fi); |
| 681 | else |
| 682 | fuse_reply_err(req, ENOSYS); |
| 683 | } |
| 684 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 685 | static void do_releasedir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 686 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 687 | struct fuse_release_in *arg = (struct fuse_release_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 688 | struct fuse_file_info fi; |
| 689 | |
| 690 | memset(&fi, 0, sizeof(fi)); |
| 691 | fi.flags = arg->flags; |
| 692 | fi.fh = arg->fh; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 693 | fi.fh_old = fi.fh; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 694 | |
| 695 | if (req->f->op.releasedir) |
| 696 | req->f->op.releasedir(req, nodeid, &fi); |
| 697 | else |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 698 | fuse_reply_err(req, 0); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 699 | } |
| 700 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 701 | static void do_fsyncdir(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 702 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 703 | struct fuse_fsync_in *arg = (struct fuse_fsync_in *) inarg; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 704 | struct fuse_file_info fi; |
| 705 | |
| 706 | memset(&fi, 0, sizeof(fi)); |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 707 | fi.fh = arg->fh; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 708 | fi.fh_old = fi.fh; |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 709 | |
| 710 | if (req->f->op.fsyncdir) |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 711 | req->f->op.fsyncdir(req, nodeid, arg->fsync_flags & 1, &fi); |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 712 | else |
| 713 | fuse_reply_err(req, ENOSYS); |
| 714 | } |
| 715 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 716 | static void do_statfs(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 717 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 718 | (void) nodeid; |
| 719 | (void) inarg; |
| 720 | |
Miklos Szeredi | 2482fdb | 2005-07-12 13:23:53 +0000 | [diff] [blame] | 721 | if (req->f->op.statfs) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 722 | req->f->op.statfs(req); |
Miklos Szeredi | aa8258e | 2006-02-25 14:42:03 +0000 | [diff] [blame] | 723 | else { |
| 724 | struct statvfs buf = { |
| 725 | .f_namemax = 255, |
| 726 | .f_bsize = 512, |
| 727 | }; |
| 728 | fuse_reply_statfs(req, &buf); |
| 729 | } |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 730 | } |
| 731 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 732 | static void do_setxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 733 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 734 | struct fuse_setxattr_in *arg = (struct fuse_setxattr_in *) inarg; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 735 | char *name = PARAM(arg); |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 736 | char *value = name + strlen(name) + 1; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 737 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 738 | if (req->f->op.setxattr) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 739 | req->f->op.setxattr(req, nodeid, name, value, arg->size, |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 740 | arg->flags); |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 741 | else |
| 742 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 743 | } |
| 744 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 745 | static void do_getxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 746 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 747 | struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 748 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 749 | if (req->f->op.getxattr) |
| 750 | req->f->op.getxattr(req, nodeid, PARAM(arg), arg->size); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 751 | else |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 752 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 753 | } |
| 754 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 755 | static void do_listxattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 756 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 757 | struct fuse_getxattr_in *arg = (struct fuse_getxattr_in *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 758 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 759 | if (req->f->op.listxattr) |
| 760 | req->f->op.listxattr(req, nodeid, arg->size); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 761 | else |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 762 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 763 | } |
| 764 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 765 | static void do_removexattr(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 766 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 767 | char *name = (char *) inarg; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 768 | |
Miklos Szeredi | 4331a27 | 2005-07-12 14:51:04 +0000 | [diff] [blame] | 769 | if (req->f->op.removexattr) |
| 770 | req->f->op.removexattr(req, nodeid, name); |
| 771 | else |
| 772 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 773 | } |
| 774 | |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 775 | static void convert_fuse_file_lock(struct fuse_file_lock *fl, |
| 776 | struct flock *flock) |
| 777 | { |
| 778 | memset(flock, 0, sizeof(struct flock)); |
| 779 | flock->l_type = fl->type; |
| 780 | flock->l_whence = SEEK_SET; |
| 781 | flock->l_start = fl->start; |
| 782 | if (fl->end == OFFSET_MAX) |
| 783 | flock->l_len = 0; |
| 784 | else |
| 785 | flock->l_len = fl->end - fl->start + 1; |
| 786 | flock->l_pid = fl->pid; |
| 787 | } |
| 788 | |
| 789 | static void do_getlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
| 790 | { |
| 791 | struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg; |
| 792 | struct fuse_file_info fi; |
| 793 | struct flock flock; |
| 794 | |
| 795 | memset(&fi, 0, sizeof(fi)); |
| 796 | fi.fh = arg->fh; |
| 797 | |
| 798 | convert_fuse_file_lock(&arg->lk, &flock); |
| 799 | if (req->f->op.getlk) |
| 800 | req->f->op.getlk(req, nodeid, &fi, &flock, arg->owner); |
| 801 | else |
| 802 | fuse_reply_err(req, ENOSYS); |
| 803 | } |
| 804 | |
| 805 | static void do_setlk_common(fuse_req_t req, fuse_ino_t nodeid, |
| 806 | const void *inarg, int sleep) |
| 807 | { |
| 808 | struct fuse_lk_in *arg = (struct fuse_lk_in *) inarg; |
| 809 | struct fuse_file_info fi; |
| 810 | struct flock flock; |
| 811 | |
| 812 | memset(&fi, 0, sizeof(fi)); |
| 813 | fi.fh = arg->fh; |
| 814 | |
| 815 | convert_fuse_file_lock(&arg->lk, &flock); |
| 816 | if (req->f->op.setlk) |
| 817 | req->f->op.setlk(req, nodeid, &fi, &flock, arg->owner, sleep); |
| 818 | else |
| 819 | fuse_reply_err(req, ENOSYS); |
| 820 | } |
| 821 | |
| 822 | static void do_setlk(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
| 823 | { |
| 824 | do_setlk_common(req, nodeid, inarg, 0); |
| 825 | } |
| 826 | |
| 827 | static void do_setlkw(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
| 828 | { |
| 829 | do_setlk_common(req, nodeid, inarg, 1); |
| 830 | } |
| 831 | |
| 832 | static int find_interrupted(struct fuse_ll *f, struct fuse_req *req) |
| 833 | { |
| 834 | struct fuse_req *curr; |
| 835 | |
| 836 | for (curr = f->list.next; curr != &f->list; curr = curr->next) { |
| 837 | if (curr->unique == req->u.i.unique) { |
| 838 | curr->interrupted = 1; |
| 839 | if (curr->u.ni.func) |
| 840 | curr->u.ni.func(curr, curr->u.ni.data); |
| 841 | return 1; |
| 842 | } |
| 843 | } |
| 844 | for (curr = f->interrupts.next; curr != &f->interrupts; curr = curr->next) { |
| 845 | if (curr->u.i.unique == req->u.i.unique) |
| 846 | return 1; |
| 847 | } |
| 848 | return 0; |
| 849 | } |
| 850 | |
| 851 | static void do_interrupt(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
| 852 | { |
| 853 | struct fuse_interrupt_in *arg = (struct fuse_interrupt_in *) inarg; |
| 854 | struct fuse_ll *f = req->f; |
| 855 | |
| 856 | (void) nodeid; |
| 857 | if (f->debug) { |
| 858 | printf("INTERRUPT: %llu\n", (unsigned long long) arg->unique); |
| 859 | fflush(stdout); |
| 860 | } |
| 861 | |
| 862 | req->u.i.unique = arg->unique; |
| 863 | |
| 864 | pthread_mutex_lock(&f->lock); |
| 865 | if (find_interrupted(f, req)) |
| 866 | free(req); |
| 867 | else |
| 868 | list_add_req(req, &f->interrupts); |
| 869 | pthread_mutex_unlock(&f->lock); |
| 870 | } |
| 871 | |
| 872 | static void check_interrupt(struct fuse_ll *f, struct fuse_req *req) |
| 873 | { |
| 874 | struct fuse_req *curr; |
| 875 | |
| 876 | for (curr = f->interrupts.next; curr != &f->interrupts; curr = curr->next) { |
| 877 | if (curr->u.i.unique == req->unique) { |
| 878 | list_del_req(curr); |
| 879 | free(curr); |
| 880 | return; |
| 881 | } |
| 882 | } |
| 883 | curr = f->interrupts.next; |
| 884 | if (curr != &f->interrupts) |
| 885 | fuse_reply_err(curr, EAGAIN); |
| 886 | } |
| 887 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 888 | static void do_init(fuse_req_t req, fuse_ino_t nodeid, const void *inarg) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 889 | { |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 890 | struct fuse_init_in *arg = (struct fuse_init_in *) inarg; |
Miklos Szeredi | 044da2e | 2005-12-06 17:59:55 +0000 | [diff] [blame] | 891 | struct fuse_init_out outarg; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 892 | struct fuse_ll *f = req->f; |
Miklos Szeredi | 044da2e | 2005-12-06 17:59:55 +0000 | [diff] [blame] | 893 | size_t bufsize = fuse_chan_bufsize(req->ch); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 894 | |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 895 | (void) nodeid; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 896 | if (f->debug) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 897 | printf("INIT: %u.%u\n", arg->major, arg->minor); |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 898 | if (arg->major > 7 || (arg->major == 7 && arg->minor >= 6)) { |
| 899 | printf("flags=0x%08x\n", arg->flags); |
| 900 | printf("max_readahead=0x%08x\n", arg->max_readahead); |
| 901 | } |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 902 | fflush(stdout); |
| 903 | } |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 904 | f->conn.proto_major = arg->major; |
| 905 | f->conn.proto_minor = arg->minor; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 906 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 907 | if (arg->major > 7 || (arg->major == 7 && arg->minor >= 6)) { |
| 908 | if (f->conn.async_read) |
| 909 | f->conn.async_read = arg->flags & FUSE_ASYNC_READ; |
| 910 | if (arg->max_readahead < f->conn.max_readahead) |
| 911 | f->conn.max_readahead = arg->max_readahead; |
| 912 | } else { |
| 913 | f->conn.async_read = 0; |
| 914 | f->conn.max_readahead = 0; |
| 915 | } |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 916 | |
Miklos Szeredi | 154ffe2 | 2005-12-15 16:41:20 +0000 | [diff] [blame] | 917 | if (bufsize < FUSE_MIN_READ_BUFFER) { |
Miklos Szeredi | 044da2e | 2005-12-06 17:59:55 +0000 | [diff] [blame] | 918 | fprintf(stderr, "fuse: warning: buffer size too small: %i\n", bufsize); |
Miklos Szeredi | 154ffe2 | 2005-12-15 16:41:20 +0000 | [diff] [blame] | 919 | bufsize = FUSE_MIN_READ_BUFFER; |
Miklos Szeredi | 044da2e | 2005-12-06 17:59:55 +0000 | [diff] [blame] | 920 | } |
| 921 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 922 | bufsize -= 4096; |
| 923 | if (bufsize < f->conn.max_write) |
| 924 | f->conn.max_write = bufsize; |
| 925 | |
| 926 | f->got_init = 1; |
| 927 | if (f->op.init) |
| 928 | f->op.init(f->userdata, &f->conn); |
| 929 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 930 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 931 | outarg.major = FUSE_KERNEL_VERSION; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 932 | outarg.minor = FUSE_KERNEL_MINOR_VERSION; |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 933 | if (f->conn.async_read) |
| 934 | outarg.flags |= FUSE_ASYNC_READ; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 935 | if (f->op.getlk && f->op.setlk) |
| 936 | outarg.flags |= FUSE_POSIX_LOCKS; |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 937 | outarg.max_readahead = f->conn.max_readahead; |
| 938 | outarg.max_write = f->conn.max_write; |
Miklos Szeredi | 044da2e | 2005-12-06 17:59:55 +0000 | [diff] [blame] | 939 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 940 | if (f->debug) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 941 | printf(" INIT: %u.%u\n", outarg.major, outarg.minor); |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 942 | printf(" flags=0x%08x\n", outarg.flags); |
| 943 | printf(" max_readahead=0x%08x\n", outarg.max_readahead); |
| 944 | printf(" max_write=0x%08x\n", outarg.max_write); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 945 | fflush(stdout); |
| 946 | } |
| 947 | |
Miklos Szeredi | 044da2e | 2005-12-06 17:59:55 +0000 | [diff] [blame] | 948 | send_reply_ok(req, &outarg, arg->minor < 5 ? 8 : sizeof(outarg)); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 951 | void *fuse_req_userdata(fuse_req_t req) |
| 952 | { |
| 953 | return req->f->userdata; |
| 954 | } |
| 955 | |
| 956 | const struct fuse_ctx *fuse_req_ctx(fuse_req_t req) |
| 957 | { |
| 958 | return &req->ctx; |
| 959 | } |
| 960 | |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 961 | void fuse_req_interrupt_func(fuse_req_t req, fuse_interrupt_func_t func, |
| 962 | void *data) |
| 963 | { |
| 964 | struct fuse_ll *f = req->f; |
| 965 | |
| 966 | pthread_mutex_lock(&f->lock); |
| 967 | req->u.ni.func = func; |
| 968 | req->u.ni.data = data; |
| 969 | if (req->interrupted && func) |
| 970 | func(req, data); |
| 971 | pthread_mutex_unlock(&f->lock); |
| 972 | } |
| 973 | |
| 974 | static struct { |
| 975 | void (*func)(fuse_req_t, fuse_ino_t, const void *); |
| 976 | const char *name; |
| 977 | } fuse_ll_ops[] = { |
| 978 | [FUSE_LOOKUP] = { do_lookup, "LOOKUP" }, |
| 979 | [FUSE_FORGET] = { do_forget, "FORGET" }, |
| 980 | [FUSE_GETATTR] = { do_getattr, "GETATTR" }, |
| 981 | [FUSE_SETATTR] = { do_setattr, "SETATTR" }, |
| 982 | [FUSE_READLINK] = { do_readlink, "READLINK" }, |
| 983 | [FUSE_SYMLINK] = { do_symlink, "SYMLINK" }, |
| 984 | [FUSE_MKNOD] = { do_mknod, "MKNOD" }, |
| 985 | [FUSE_MKDIR] = { do_mkdir, "MKDIR" }, |
| 986 | [FUSE_UNLINK] = { do_unlink, "UNLINK" }, |
| 987 | [FUSE_RMDIR] = { do_rmdir, "RMDIR" }, |
| 988 | [FUSE_RENAME] = { do_rename, "RENAME" }, |
| 989 | [FUSE_LINK] = { do_link, "LINK" }, |
| 990 | [FUSE_OPEN] = { do_open, "OPEN" }, |
| 991 | [FUSE_READ] = { do_read, "READ" }, |
| 992 | [FUSE_WRITE] = { do_write, "WRITE" }, |
| 993 | [FUSE_STATFS] = { do_statfs, "STATFS" }, |
| 994 | [FUSE_RELEASE] = { do_release, "RELEASE" }, |
| 995 | [FUSE_FSYNC] = { do_fsync, "FSYNC" }, |
| 996 | [FUSE_SETXATTR] = { do_setxattr, "SETXATTR" }, |
| 997 | [FUSE_GETXATTR] = { do_getxattr, "GETXATTR" }, |
| 998 | [FUSE_LISTXATTR] = { do_listxattr, "LISTXATTR" }, |
| 999 | [FUSE_REMOVEXATTR] = { do_removexattr, "REMOVEXATTR" }, |
| 1000 | [FUSE_FLUSH] = { do_flush, "FLUSH" }, |
| 1001 | [FUSE_INIT] = { do_init, "INIT" }, |
| 1002 | [FUSE_OPENDIR] = { do_opendir, "OPENDIR" }, |
| 1003 | [FUSE_READDIR] = { do_readdir, "READDIR" }, |
| 1004 | [FUSE_RELEASEDIR] = { do_releasedir, "RELEASEDIR" }, |
| 1005 | [FUSE_FSYNCDIR] = { do_fsyncdir, "FSYNCDIR" }, |
| 1006 | [FUSE_GETLK] = { do_getlk, "GETLK" }, |
| 1007 | [FUSE_SETLK] = { do_setlk, "SETLK" }, |
| 1008 | [FUSE_SETLKW] = { do_setlkw, "SETLKW" }, |
| 1009 | [FUSE_ACCESS] = { do_access, "ACCESS" }, |
| 1010 | [FUSE_CREATE] = { do_create, "CREATE" }, |
| 1011 | [FUSE_INTERRUPT] = { do_interrupt, "INTERRUPT" }, |
Miklos Szeredi | 2c1381f | 2006-03-15 20:34:10 +0000 | [diff] [blame] | 1012 | }; |
| 1013 | |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 1014 | #define FUSE_MAXOP (sizeof(fuse_ll_ops) / sizeof(fuse_ll_ops[0])) |
| 1015 | |
| 1016 | static const char *opname(enum fuse_opcode opcode) |
| 1017 | { |
| 1018 | if (opcode >= FUSE_MAXOP || !fuse_ll_ops[opcode].name) |
| 1019 | return "???"; |
| 1020 | else |
| 1021 | return fuse_ll_ops[opcode].name; |
| 1022 | } |
| 1023 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1024 | static void fuse_ll_process(void *data, const char *buf, size_t len, |
| 1025 | struct fuse_chan *ch) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1026 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1027 | struct fuse_ll *f = (struct fuse_ll *) data; |
| 1028 | struct fuse_in_header *in = (struct fuse_in_header *) buf; |
| 1029 | const void *inarg = buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 1030 | struct fuse_req *req; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1031 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 1032 | if (f->debug) { |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1033 | printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n", |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 1034 | (unsigned long long) in->unique, |
| 1035 | opname((enum fuse_opcode) in->opcode), in->opcode, |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1036 | (unsigned long) in->nodeid, len); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1037 | fflush(stdout); |
| 1038 | } |
| 1039 | |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 1040 | req = (struct fuse_req *) calloc(1, sizeof(struct fuse_req)); |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 1041 | if (req == NULL) { |
| 1042 | fprintf(stderr, "fuse: failed to allocate request\n"); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1043 | return; |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 1044 | } |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 1045 | |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 1046 | req->f = f; |
| 1047 | req->unique = in->unique; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1048 | req->ctx.uid = in->uid; |
| 1049 | req->ctx.gid = in->gid; |
| 1050 | req->ctx.pid = in->pid; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1051 | req->ch = ch; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 1052 | list_init_req(req); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1053 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1054 | if (!f->got_init && in->opcode != FUSE_INIT) |
Miklos Szeredi | b3f9972 | 2005-11-16 13:00:24 +0000 | [diff] [blame] | 1055 | fuse_reply_err(req, EIO); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1056 | else if (f->allow_root && in->uid != f->owner && in->uid != 0 && |
| 1057 | in->opcode != FUSE_INIT && in->opcode != FUSE_READ && |
| 1058 | in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC && |
| 1059 | in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR && |
| 1060 | in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) { |
| 1061 | fuse_reply_err(req, EACCES); |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 1062 | } else if (in->opcode >= FUSE_MAXOP || !fuse_ll_ops[in->opcode].func) |
Miklos Szeredi | 76c1752 | 2005-07-13 14:08:19 +0000 | [diff] [blame] | 1063 | fuse_reply_err(req, ENOSYS); |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 1064 | else { |
| 1065 | if (in->opcode != FUSE_INTERRUPT) { |
| 1066 | pthread_mutex_lock(&f->lock); |
| 1067 | check_interrupt(f, req); |
| 1068 | list_add_req(req, &f->list); |
| 1069 | pthread_mutex_unlock(&f->lock); |
| 1070 | } |
| 1071 | fuse_ll_ops[in->opcode].func(req, in->nodeid, inarg); |
| 1072 | } |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1073 | } |
| 1074 | |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 1075 | enum { |
| 1076 | KEY_HELP, |
| 1077 | KEY_VERSION, |
| 1078 | }; |
| 1079 | |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1080 | static struct fuse_opt fuse_ll_opts[] = { |
| 1081 | { "debug", offsetof(struct fuse_ll, debug), 1 }, |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 1082 | { "-d", offsetof(struct fuse_ll, debug), 1 }, |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1083 | { "allow_root", offsetof(struct fuse_ll, allow_root), 1 }, |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1084 | { "max_write=%u", offsetof(struct fuse_ll, conn.max_write), 0 }, |
| 1085 | { "max_readahead=%u", offsetof(struct fuse_ll, conn.max_readahead), 0 }, |
| 1086 | { "async_read", offsetof(struct fuse_ll, conn.async_read), 1 }, |
| 1087 | { "sync_read", offsetof(struct fuse_ll, conn.async_read), 0 }, |
| 1088 | FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_DISCARD), |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 1089 | FUSE_OPT_KEY("-h", KEY_HELP), |
| 1090 | FUSE_OPT_KEY("--help", KEY_HELP), |
| 1091 | FUSE_OPT_KEY("-V", KEY_VERSION), |
| 1092 | FUSE_OPT_KEY("--version", KEY_VERSION), |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1093 | FUSE_OPT_END |
| 1094 | }; |
| 1095 | |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 1096 | static void fuse_ll_version(void) |
| 1097 | { |
| 1098 | fprintf(stderr, "using FUSE kernel interface version %i.%i\n", |
| 1099 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1100 | } |
| 1101 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1102 | static void fuse_ll_help(void) |
| 1103 | { |
| 1104 | fprintf(stderr, |
| 1105 | " -o max_write=N set maximum size of write requests\n" |
| 1106 | " -o max_readahead=N set maximum readahead\n" |
| 1107 | " -o async_read perform reads asynchronously (default)\n" |
| 1108 | " -o sync_read perform reads synchronously\n"); |
| 1109 | } |
| 1110 | |
Miklos Szeredi | caa0931 | 2005-12-14 23:25:00 +0000 | [diff] [blame] | 1111 | static int fuse_ll_opt_proc(void *data, const char *arg, int key, |
Miklos Szeredi | 73f4139 | 2005-12-16 11:12:16 +0000 | [diff] [blame] | 1112 | struct fuse_args *outargs) |
Miklos Szeredi | 3b534a4 | 2005-12-09 20:09:42 +0000 | [diff] [blame] | 1113 | { |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 1114 | (void) data; (void) outargs; |
Miklos Szeredi | cb402b0 | 2006-01-09 14:44:23 +0000 | [diff] [blame] | 1115 | |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 1116 | switch (key) { |
| 1117 | case KEY_HELP: |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1118 | fuse_ll_help(); |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 1119 | break; |
Miklos Szeredi | cb402b0 | 2006-01-09 14:44:23 +0000 | [diff] [blame] | 1120 | |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 1121 | case KEY_VERSION: |
| 1122 | fuse_ll_version(); |
| 1123 | break; |
| 1124 | |
| 1125 | default: |
| 1126 | fprintf(stderr, "fuse: unknown option `%s'\n", arg); |
| 1127 | } |
Miklos Szeredi | cb402b0 | 2006-01-09 14:44:23 +0000 | [diff] [blame] | 1128 | |
Miklos Szeredi | 3b534a4 | 2005-12-09 20:09:42 +0000 | [diff] [blame] | 1129 | return -1; |
| 1130 | } |
| 1131 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1132 | int fuse_lowlevel_is_lib_option(const char *opt) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1133 | { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1134 | return fuse_opt_match(fuse_ll_opts, opt); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1135 | } |
| 1136 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1137 | static void fuse_ll_destroy(void *data) |
| 1138 | { |
| 1139 | struct fuse_ll *f = (struct fuse_ll *) data; |
| 1140 | |
| 1141 | if (f->op.destroy) |
| 1142 | f->op.destroy(f->userdata); |
| 1143 | |
| 1144 | free(f); |
| 1145 | } |
| 1146 | |
Miklos Szeredi | 1ee1a11 | 2006-02-15 14:28:22 +0000 | [diff] [blame] | 1147 | /* |
| 1148 | * always call fuse_lowlevel_new_common() internally, to work around a |
| 1149 | * misfeature in the FreeBSD runtime linker, which links the old |
| 1150 | * version of a symbol to internal references. |
| 1151 | */ |
Miklos Szeredi | d65cdfe | 2006-02-15 14:25:17 +0000 | [diff] [blame] | 1152 | struct fuse_session *fuse_lowlevel_new_common(struct fuse_args *args, |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1153 | const struct fuse_lowlevel_ops *op, |
| 1154 | size_t op_size, void *userdata) |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1155 | { |
| 1156 | struct fuse_ll *f; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1157 | struct fuse_session *se; |
| 1158 | struct fuse_session_ops sop = { |
| 1159 | .process = fuse_ll_process, |
| 1160 | .destroy = fuse_ll_destroy, |
| 1161 | }; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1162 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1163 | if (sizeof(struct fuse_lowlevel_ops) < op_size) { |
Miklos Szeredi | b75d4b9 | 2005-10-11 10:12:08 +0000 | [diff] [blame] | 1164 | 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] | 1165 | op_size = sizeof(struct fuse_lowlevel_ops); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1166 | } |
| 1167 | |
| 1168 | f = (struct fuse_ll *) calloc(1, sizeof(struct fuse_ll)); |
| 1169 | if (f == NULL) { |
| 1170 | fprintf(stderr, "fuse: failed to allocate fuse object\n"); |
| 1171 | goto out; |
| 1172 | } |
| 1173 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1174 | f->conn.async_read = 1; |
| 1175 | f->conn.max_write = UINT_MAX; |
| 1176 | f->conn.max_readahead = UINT_MAX; |
Miklos Szeredi | b052a1a | 2006-06-28 14:51:20 +0000 | [diff] [blame] | 1177 | list_init_req(&f->list); |
| 1178 | list_init_req(&f->interrupts); |
| 1179 | mutex_init(&f->lock); |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1180 | |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 1181 | if (fuse_opt_parse(args, f, fuse_ll_opts, fuse_ll_opt_proc) == -1) |
| 1182 | goto out_free; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1183 | |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1184 | memcpy(&f->op, op, op_size); |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1185 | f->owner = getuid(); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1186 | f->userdata = userdata; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1187 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1188 | se = fuse_session_new(&sop, f); |
| 1189 | if (!se) |
| 1190 | goto out_free; |
| 1191 | |
| 1192 | return se; |
Miklos Szeredi | 1274494 | 2005-07-11 12:32:31 +0000 | [diff] [blame] | 1193 | |
| 1194 | out_free: |
| 1195 | free(f); |
| 1196 | out: |
| 1197 | return NULL; |
| 1198 | } |
Miklos Szeredi | c706ad9 | 2005-11-07 15:30:48 +0000 | [diff] [blame] | 1199 | |
Miklos Szeredi | d65cdfe | 2006-02-15 14:25:17 +0000 | [diff] [blame] | 1200 | |
Miklos Szeredi | d65cdfe | 2006-02-15 14:25:17 +0000 | [diff] [blame] | 1201 | struct fuse_session *fuse_lowlevel_new(struct fuse_args *args, |
| 1202 | const struct fuse_lowlevel_ops *op, |
| 1203 | size_t op_size, void *userdata) |
| 1204 | { |
| 1205 | return fuse_lowlevel_new_common(args, op, op_size, userdata); |
| 1206 | } |
| 1207 | |
| 1208 | |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1209 | #include "fuse_lowlevel_compat.h" |
| 1210 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1211 | #ifndef __FreeBSD__ |
| 1212 | |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1213 | static void fill_open_compat(struct fuse_open_out *arg, |
| 1214 | const struct fuse_file_info_compat *f) |
| 1215 | { |
| 1216 | arg->fh = f->fh; |
| 1217 | if (f->direct_io) |
| 1218 | arg->open_flags |= FOPEN_DIRECT_IO; |
| 1219 | if (f->keep_cache) |
| 1220 | arg->open_flags |= FOPEN_KEEP_CACHE; |
| 1221 | } |
| 1222 | |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 1223 | static void convert_statfs_compat(const struct statfs *compatbuf, |
| 1224 | struct statvfs *buf) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1225 | { |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 1226 | buf->f_bsize = compatbuf->f_bsize; |
| 1227 | buf->f_blocks = compatbuf->f_blocks; |
| 1228 | buf->f_bfree = compatbuf->f_bfree; |
| 1229 | buf->f_bavail = compatbuf->f_bavail; |
| 1230 | buf->f_files = compatbuf->f_files; |
| 1231 | buf->f_ffree = compatbuf->f_ffree; |
| 1232 | buf->f_namemax = compatbuf->f_namelen; |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1233 | } |
| 1234 | |
| 1235 | int fuse_reply_open_compat(fuse_req_t req, |
| 1236 | const struct fuse_file_info_compat *f) |
| 1237 | { |
| 1238 | struct fuse_open_out arg; |
| 1239 | |
| 1240 | memset(&arg, 0, sizeof(arg)); |
| 1241 | fill_open_compat(&arg, f); |
| 1242 | return send_reply_ok(req, &arg, sizeof(arg)); |
| 1243 | } |
| 1244 | |
| 1245 | int fuse_reply_statfs_compat(fuse_req_t req, const struct statfs *stbuf) |
| 1246 | { |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 1247 | struct statvfs newbuf; |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1248 | |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 1249 | memset(&newbuf, 0, sizeof(newbuf)); |
| 1250 | convert_statfs_compat(stbuf, &newbuf); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1251 | |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 1252 | return fuse_reply_statfs(req, &newbuf); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1253 | } |
| 1254 | |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 1255 | struct fuse_session *fuse_lowlevel_new_compat(const char *opts, |
| 1256 | const struct fuse_lowlevel_ops *op, |
| 1257 | size_t op_size, void *userdata) |
| 1258 | { |
| 1259 | struct fuse_session *se; |
| 1260 | struct fuse_args args = FUSE_ARGS_INIT(0, NULL); |
| 1261 | |
| 1262 | if (opts && |
| 1263 | (fuse_opt_add_arg(&args, "") == -1 || |
| 1264 | fuse_opt_add_arg(&args, "-o") == -1 || |
| 1265 | fuse_opt_add_arg(&args, opts) == -1)) { |
| 1266 | fuse_opt_free_args(&args); |
| 1267 | return NULL; |
| 1268 | } |
| 1269 | se = fuse_lowlevel_new(&args, op, op_size, userdata); |
| 1270 | fuse_opt_free_args(&args); |
| 1271 | |
| 1272 | return se; |
| 1273 | } |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1274 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1275 | struct fuse_ll_compat_conf { |
| 1276 | unsigned max_read; |
| 1277 | int set_max_read; |
| 1278 | }; |
| 1279 | |
| 1280 | static const struct fuse_opt fuse_ll_opts_compat[] = { |
| 1281 | { "max_read=", offsetof(struct fuse_ll_compat_conf, set_max_read), 1 }, |
| 1282 | { "max_read=%u", offsetof(struct fuse_ll_compat_conf, max_read), 0 }, |
| 1283 | FUSE_OPT_KEY("max_read=", FUSE_OPT_KEY_KEEP), |
| 1284 | FUSE_OPT_END |
| 1285 | }; |
| 1286 | |
| 1287 | int fuse_sync_compat_args(struct fuse_args *args) |
| 1288 | { |
| 1289 | struct fuse_ll_compat_conf conf; |
| 1290 | |
Miklos Szeredi | 67d26d4 | 2006-03-13 17:39:56 +0000 | [diff] [blame] | 1291 | memset(&conf, 0, sizeof(conf)); |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1292 | if (fuse_opt_parse(args, &conf, fuse_ll_opts_compat, NULL) == -1) |
| 1293 | return -1; |
| 1294 | |
| 1295 | if (fuse_opt_insert_arg(args, 1, "-osync_read")) |
| 1296 | return -1; |
| 1297 | |
| 1298 | if (conf.set_max_read) { |
| 1299 | char tmpbuf[64]; |
| 1300 | |
| 1301 | sprintf(tmpbuf, "-omax_readahead=%u", conf.max_read); |
| 1302 | if (fuse_opt_insert_arg(args, 1, tmpbuf) == -1) |
| 1303 | return -1; |
| 1304 | } |
| 1305 | return 0; |
| 1306 | } |
| 1307 | |
Miklos Szeredi | d65cdfe | 2006-02-15 14:25:17 +0000 | [diff] [blame] | 1308 | __asm__(".symver fuse_reply_statfs_compat,fuse_reply_statfs@FUSE_2.4"); |
| 1309 | __asm__(".symver fuse_reply_open_compat,fuse_reply_open@FUSE_2.4"); |
| 1310 | __asm__(".symver fuse_lowlevel_new_compat,fuse_lowlevel_new@FUSE_2.4"); |
| 1311 | |
| 1312 | #else /* __FreeBSD__ */ |
| 1313 | |
| 1314 | int fuse_sync_compat_args(struct fuse_args *args) |
| 1315 | { |
| 1316 | (void) args; |
| 1317 | return 0; |
| 1318 | } |
| 1319 | |
| 1320 | #endif /* __FreeBSD__ */ |
| 1321 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1322 | struct fuse_session *fuse_lowlevel_new_compat25(struct fuse_args *args, |
| 1323 | const struct fuse_lowlevel_ops_compat25 *op, |
| 1324 | size_t op_size, void *userdata) |
| 1325 | { |
| 1326 | if (fuse_sync_compat_args(args) == -1) |
| 1327 | return NULL; |
| 1328 | |
Miklos Szeredi | d65cdfe | 2006-02-15 14:25:17 +0000 | [diff] [blame] | 1329 | return fuse_lowlevel_new_common(args, |
| 1330 | (const struct fuse_lowlevel_ops *) op, |
| 1331 | op_size, userdata); |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1332 | } |
| 1333 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 1334 | __asm__(".symver fuse_lowlevel_new_compat25,fuse_lowlevel_new@FUSE_2.5"); |