Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
| 3 | Copyright (C) 2001 Miklos Szeredi (mszeredi@inf.bme.hu) |
| 4 | |
Miklos Szeredi | 8b39a9f | 2002-10-25 12:41:16 +0000 | [diff] [blame] | 5 | This program can be distributed under the terms of the GNU LGPL. |
| 6 | See the file COPYING.LIB |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
| 9 | #include "fuse_i.h" |
| 10 | #include <linux/fuse.h> |
| 11 | |
| 12 | #include <string.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 13 | #include <stdlib.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 14 | #include <unistd.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 15 | #include <limits.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 16 | #include <errno.h> |
Miklos Szeredi | 019b4e9 | 2001-12-26 18:08:09 +0000 | [diff] [blame] | 17 | #include <sys/param.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 18 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 19 | #define FUSE_MAX_PATH 4096 |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 20 | #define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg)) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 21 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 22 | static const char *opname(enum fuse_opcode opcode) |
| 23 | { |
| 24 | switch(opcode) { |
| 25 | case FUSE_LOOKUP: return "LOOKUP"; |
| 26 | case FUSE_FORGET: return "FORGET"; |
| 27 | case FUSE_GETATTR: return "GETATTR"; |
| 28 | case FUSE_SETATTR: return "SETATTR"; |
| 29 | case FUSE_READLINK: return "READLINK"; |
| 30 | case FUSE_SYMLINK: return "SYMLINK"; |
| 31 | case FUSE_GETDIR: return "GETDIR"; |
| 32 | case FUSE_MKNOD: return "MKNOD"; |
| 33 | case FUSE_MKDIR: return "MKDIR"; |
| 34 | case FUSE_UNLINK: return "UNLINK"; |
| 35 | case FUSE_RMDIR: return "RMDIR"; |
| 36 | case FUSE_RENAME: return "RENAME"; |
| 37 | case FUSE_LINK: return "LINK"; |
| 38 | case FUSE_OPEN: return "OPEN"; |
| 39 | case FUSE_READ: return "READ"; |
| 40 | case FUSE_WRITE: return "WRITE"; |
| 41 | case FUSE_STATFS: return "STATFS"; |
| 42 | case FUSE_RELEASE: return "RELEASE"; |
| 43 | default: return "???"; |
| 44 | } |
| 45 | } |
| 46 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 47 | static inline void inc_avail(struct fuse *f) |
| 48 | { |
| 49 | pthread_mutex_lock(&f->lock); |
| 50 | f->numavail ++; |
| 51 | pthread_mutex_unlock(&f->lock); |
| 52 | } |
| 53 | |
| 54 | static inline void dec_avail(struct fuse *f) |
| 55 | { |
| 56 | pthread_mutex_lock(&f->lock); |
| 57 | f->numavail --; |
| 58 | pthread_mutex_unlock(&f->lock); |
| 59 | } |
| 60 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 61 | static struct node *__get_node(struct fuse *f, fino_t ino) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 62 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 63 | size_t hash = ino % f->ino_table_size; |
| 64 | struct node *node; |
| 65 | |
| 66 | for(node = f->ino_table[hash]; node != NULL; node = node->ino_next) |
| 67 | if(node->ino == ino) |
| 68 | return node; |
| 69 | |
| 70 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 71 | } |
| 72 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 73 | static struct node *get_node(struct fuse *f, fino_t ino) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 74 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 75 | struct node *node = __get_node(f, ino); |
| 76 | if(node != NULL) |
| 77 | return node; |
| 78 | |
| 79 | fprintf(stderr, "fuse internal error: inode %lu not found\n", ino); |
| 80 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 81 | } |
| 82 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 83 | static void hash_ino(struct fuse *f, struct node *node, fino_t ino) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 84 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 85 | size_t hash = ino % f->ino_table_size; |
| 86 | node->ino = ino; |
| 87 | |
| 88 | node->ino_next = f->ino_table[hash]; |
| 89 | f->ino_table[hash] = node; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 92 | static void unhash_ino(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 93 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 94 | size_t hash = node->ino % f->ino_table_size; |
| 95 | struct node **nodep = &f->ino_table[hash]; |
| 96 | |
| 97 | for(; *nodep != NULL; nodep = &(*nodep)->ino_next) |
| 98 | if(*nodep == node) { |
| 99 | *nodep = node->ino_next; |
| 100 | return; |
| 101 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 102 | } |
| 103 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 104 | static fino_t get_ino(struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 105 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 106 | return node->ino; |
| 107 | } |
| 108 | |
| 109 | static fino_t next_ino(struct fuse *f) |
| 110 | { |
| 111 | while(f->ctr == 0 || __get_node(f, f->ctr) != NULL) |
| 112 | f->ctr++; |
| 113 | |
| 114 | return f->ctr; |
| 115 | } |
| 116 | |
| 117 | static void free_node(struct node *node) |
| 118 | { |
| 119 | free(node->name); |
| 120 | free(node); |
| 121 | } |
| 122 | |
| 123 | static unsigned int name_hash(struct fuse *f, fino_t parent, const char *name) |
| 124 | { |
| 125 | unsigned int hash = *name; |
| 126 | |
| 127 | if(hash) |
| 128 | for(name += 1; *name != '\0'; name++) |
| 129 | hash = (hash << 5) - hash + *name; |
| 130 | |
| 131 | return (hash + parent) % f->name_table_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 132 | } |
| 133 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 134 | static struct node *lookup_node(struct fuse *f, fino_t parent, |
| 135 | const char *name) |
| 136 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 137 | size_t hash = name_hash(f, parent, name); |
| 138 | struct node *node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 139 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 140 | for(node = f->name_table[hash]; node != NULL; node = node->name_next) |
| 141 | if(node->parent == parent && strcmp(node->name, name) == 0) |
| 142 | return node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 143 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 144 | return NULL; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 145 | } |
| 146 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 147 | static void hash_name(struct fuse *f, struct node *node, fino_t parent, |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 148 | const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 149 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 150 | size_t hash = name_hash(f, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 151 | node->parent = parent; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 152 | node->name = strdup(name); |
| 153 | node->name_next = f->name_table[hash]; |
| 154 | f->name_table[hash] = node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 157 | static void unhash_name(struct fuse *f, struct node *node) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 158 | { |
| 159 | if(node->name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 160 | size_t hash = name_hash(f, node->parent, node->name); |
| 161 | struct node **nodep = &f->name_table[hash]; |
| 162 | |
| 163 | for(; *nodep != NULL; nodep = &(*nodep)->name_next) |
| 164 | if(*nodep == node) { |
| 165 | *nodep = node->name_next; |
| 166 | node->name_next = NULL; |
| 167 | free(node->name); |
| 168 | node->name = NULL; |
| 169 | node->parent = 0; |
| 170 | return; |
| 171 | } |
| 172 | fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n", |
| 173 | node->ino); |
| 174 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 175 | } |
| 176 | } |
| 177 | |
| 178 | static fino_t find_node(struct fuse *f, fino_t parent, char *name, |
| 179 | struct fuse_attr *attr, int version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 180 | { |
| 181 | struct node *node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 182 | int mode = attr->mode & S_IFMT; |
| 183 | int rdev = 0; |
| 184 | |
| 185 | if(S_ISCHR(mode) || S_ISBLK(mode)) |
| 186 | rdev = attr->rdev; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 187 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 188 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 189 | node = lookup_node(f, parent, name); |
| 190 | if(node != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 191 | if(node->mode == mode && node->rdev == rdev) |
| 192 | goto out; |
| 193 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 194 | unhash_name(f, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 195 | } |
| 196 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 197 | node = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 198 | node->mode = mode; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 199 | node->rdev = rdev; |
| 200 | hash_ino(f, node, next_ino(f)); |
| 201 | hash_name(f, node, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 202 | |
| 203 | out: |
| 204 | node->version = version; |
| 205 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 206 | return get_ino(node); |
| 207 | } |
| 208 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 209 | static char *add_name(char *buf, char *s, const char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 210 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 211 | size_t len = strlen(name); |
| 212 | s -= len; |
| 213 | if(s <= buf) { |
| 214 | fprintf(stderr, "fuse: path too long: ...%s\n", s + len); |
| 215 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 216 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 217 | strncpy(s, name, len); |
| 218 | s--; |
| 219 | *s = '/'; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 220 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 221 | return s; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 222 | } |
| 223 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 224 | static char *get_path_name(struct fuse *f, fino_t ino, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 225 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 226 | char buf[FUSE_MAX_PATH]; |
| 227 | char *s = buf + FUSE_MAX_PATH - 1; |
| 228 | struct node *node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 229 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 230 | *s = '\0'; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 231 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 232 | if(name != NULL) { |
| 233 | s = add_name(buf, s, name); |
| 234 | if(s == NULL) |
| 235 | return NULL; |
| 236 | } |
| 237 | |
| 238 | pthread_mutex_lock(&f->lock); |
| 239 | for(node = get_node(f, ino); node->ino != FUSE_ROOT_INO; |
| 240 | node = get_node(f, node->parent)) { |
| 241 | if(node->name == NULL) { |
| 242 | s = NULL; |
| 243 | break; |
| 244 | } |
| 245 | |
| 246 | s = add_name(buf, s, node->name); |
| 247 | if(s == NULL) |
| 248 | break; |
| 249 | } |
| 250 | pthread_mutex_unlock(&f->lock); |
| 251 | |
| 252 | if(s == NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 253 | return NULL; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 254 | else if(*s == '\0') |
| 255 | return strdup("/"); |
| 256 | else |
| 257 | return strdup(s); |
| 258 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 259 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 260 | static char *get_path(struct fuse *f, fino_t ino) |
| 261 | { |
| 262 | return get_path_name(f, ino, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 263 | } |
| 264 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 265 | static void destroy_node(struct fuse *f, fino_t ino, int version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 266 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 267 | struct node *node; |
| 268 | |
| 269 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 270 | node = get_node(f, ino); |
Miklos Szeredi | 39f2867 | 2001-11-14 14:52:54 +0000 | [diff] [blame] | 271 | if(node->version == version && ino != FUSE_ROOT_INO) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 272 | unhash_name(f, node); |
| 273 | unhash_ino(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 274 | free_node(node); |
| 275 | } |
| 276 | pthread_mutex_unlock(&f->lock); |
| 277 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 280 | static void remove_node(struct fuse *f, fino_t dir, const char *name) |
| 281 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 282 | struct node *node; |
| 283 | |
| 284 | pthread_mutex_lock(&f->lock); |
| 285 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 286 | if(node == NULL) { |
| 287 | fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n", |
| 288 | dir, name); |
| 289 | abort(); |
| 290 | } |
| 291 | unhash_name(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 292 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 293 | } |
| 294 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 295 | static void rename_node(struct fuse *f, fino_t olddir, const char *oldname, |
| 296 | fino_t newdir, const char *newname) |
| 297 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 298 | struct node *node; |
| 299 | struct node *newnode; |
| 300 | |
| 301 | pthread_mutex_lock(&f->lock); |
| 302 | node = lookup_node(f, olddir, oldname); |
| 303 | newnode = lookup_node(f, newdir, newname); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 304 | if(node == NULL) { |
| 305 | fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n", |
| 306 | olddir, oldname); |
| 307 | abort(); |
| 308 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 309 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 310 | if(newnode != NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 311 | unhash_name(f, newnode); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 312 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 313 | unhash_name(f, node); |
| 314 | hash_name(f, node, newdir, newname); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 315 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 316 | } |
| 317 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 318 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 319 | { |
| 320 | attr->mode = stbuf->st_mode; |
| 321 | attr->nlink = stbuf->st_nlink; |
| 322 | attr->uid = stbuf->st_uid; |
| 323 | attr->gid = stbuf->st_gid; |
| 324 | attr->rdev = stbuf->st_rdev; |
| 325 | attr->size = stbuf->st_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 326 | attr->blocks = stbuf->st_blocks; |
| 327 | attr->atime = stbuf->st_atime; |
| 328 | attr->mtime = stbuf->st_mtime; |
| 329 | attr->ctime = stbuf->st_ctime; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 330 | attr->_dummy = 4096; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 333 | static int fill_dir(struct fuse_dirhandle *dh, char *name, int type) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 334 | { |
| 335 | struct fuse_dirent dirent; |
| 336 | size_t reclen; |
| 337 | size_t res; |
| 338 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 339 | dirent.ino = (unsigned long) -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 340 | dirent.namelen = strlen(name); |
| 341 | strncpy(dirent.name, name, sizeof(dirent.name)); |
| 342 | dirent.type = type; |
| 343 | reclen = FUSE_DIRENT_SIZE(&dirent); |
| 344 | res = fwrite(&dirent, reclen, 1, dh->fp); |
| 345 | if(res == 0) { |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 346 | perror("fuse: writing directory file"); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 347 | return -EIO; |
| 348 | } |
| 349 | return 0; |
| 350 | } |
| 351 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 352 | static void send_reply_raw(struct fuse *f, char *outbuf, size_t outsize) |
| 353 | { |
| 354 | int res; |
| 355 | |
| 356 | if((f->flags & FUSE_DEBUG)) { |
| 357 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 358 | printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique, |
| 359 | out->error, strerror(-out->error), outsize); |
| 360 | fflush(stdout); |
| 361 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 362 | |
| 363 | /* This needs to be done before the reply because otherwise the |
| 364 | scheduler can tricks with us, and only let the counter be increased |
| 365 | long after the operation is done */ |
| 366 | inc_avail(f); |
| 367 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 368 | res = write(f->fd, outbuf, outsize); |
| 369 | if(res == -1) { |
| 370 | /* ENOENT means the operation was interrupted */ |
| 371 | if(errno != ENOENT) |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 372 | perror("fuse: writing device"); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 373 | } |
| 374 | } |
| 375 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 376 | static void send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 377 | void *arg, size_t argsize) |
| 378 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 379 | char *outbuf; |
| 380 | size_t outsize; |
| 381 | struct fuse_out_header *out; |
| 382 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 383 | if(error <= -512 || error > 0) { |
| 384 | fprintf(stderr, "fuse: bad error value: %i\n", error); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 385 | error = -ERANGE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 386 | } |
| 387 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 388 | if(error) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 389 | argsize = 0; |
| 390 | |
| 391 | outsize = sizeof(struct fuse_out_header) + argsize; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 392 | outbuf = (char *) malloc(outsize); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 393 | out = (struct fuse_out_header *) outbuf; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 394 | memset(out, 0, sizeof(struct fuse_out_header)); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 395 | out->unique = in->unique; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 396 | out->error = error; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 397 | if(argsize != 0) |
| 398 | memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize); |
| 399 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 400 | send_reply_raw(f, outbuf, outsize); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 401 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 402 | free(outbuf); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 406 | { |
| 407 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 408 | char *path; |
| 409 | struct stat buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 410 | struct fuse_lookup_out arg; |
| 411 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 412 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 413 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 414 | if(path != NULL) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 415 | if(f->flags & FUSE_DEBUG) { |
| 416 | printf("LOOKUP %s\n", path); |
| 417 | fflush(stdout); |
| 418 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 419 | res = -ENOSYS; |
| 420 | if(f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 421 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 422 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 423 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 424 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 425 | if(res == 0) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 426 | memset(&arg, 0, sizeof(struct fuse_lookup_out)); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 427 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 428 | arg.ino = find_node(f, in->ino, name, &arg.attr, in->unique); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 429 | if(f->flags & FUSE_DEBUG) { |
| 430 | printf(" LOOKUP: %li\n", arg.ino); |
| 431 | fflush(stdout); |
| 432 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 433 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 434 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 435 | } |
| 436 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 437 | static void do_forget(struct fuse *f, struct fuse_in_header *in, |
| 438 | struct fuse_forget_in *arg) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 439 | { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 440 | if(f->flags & FUSE_DEBUG) { |
| 441 | printf("FORGET %li/%i\n", in->ino, arg->version); |
| 442 | fflush(stdout); |
| 443 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 444 | destroy_node(f, in->ino, arg->version); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 445 | } |
| 446 | |
| 447 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 448 | { |
| 449 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 450 | char *path; |
| 451 | struct stat buf; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 452 | struct fuse_getattr_out arg; |
| 453 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 454 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 455 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 456 | if(path != NULL) { |
| 457 | res = -ENOSYS; |
| 458 | if(f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 459 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 460 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 461 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 462 | |
| 463 | if(res == 0) { |
| 464 | memset(&arg, 0, sizeof(struct fuse_getattr_out)); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 465 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 466 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 467 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 468 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 469 | } |
| 470 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 471 | static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 472 | { |
| 473 | int res; |
| 474 | |
| 475 | res = -ENOSYS; |
| 476 | if(f->op.chmod) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 477 | res = f->op.chmod(path, attr->mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 478 | |
| 479 | return res; |
| 480 | } |
| 481 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 482 | static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr, |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 483 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 484 | { |
| 485 | int res; |
| 486 | uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1; |
| 487 | gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1; |
| 488 | |
| 489 | res = -ENOSYS; |
| 490 | if(f->op.chown) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 491 | res = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 492 | |
| 493 | return res; |
| 494 | } |
| 495 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 496 | static int do_truncate(struct fuse *f, const char *path, |
| 497 | struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 498 | { |
| 499 | int res; |
| 500 | |
| 501 | res = -ENOSYS; |
| 502 | if(f->op.truncate) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 503 | res = f->op.truncate(path, attr->size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 504 | |
| 505 | return res; |
| 506 | } |
| 507 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 508 | static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 509 | { |
| 510 | int res; |
| 511 | struct utimbuf buf; |
| 512 | buf.actime = attr->atime; |
| 513 | buf.modtime = attr->mtime; |
| 514 | res = -ENOSYS; |
| 515 | if(f->op.utime) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 516 | res = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 517 | |
| 518 | return res; |
| 519 | } |
| 520 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 521 | static void do_setattr(struct fuse *f, struct fuse_in_header *in, |
| 522 | struct fuse_setattr_in *arg) |
| 523 | { |
| 524 | int res; |
| 525 | char *path; |
| 526 | int valid = arg->valid; |
| 527 | struct fuse_attr *attr = &arg->attr; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 528 | struct fuse_setattr_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 529 | |
| 530 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 531 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 532 | if(path != NULL) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 533 | res = -ENOSYS; |
| 534 | if(f->op.getattr) { |
| 535 | res = 0; |
| 536 | if(!res && (valid & FATTR_MODE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 537 | res = do_chmod(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 538 | if(!res && (valid & (FATTR_UID | FATTR_GID))) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 539 | res = do_chown(f, path, attr, valid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 540 | if(!res && (valid & FATTR_SIZE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 541 | res = do_truncate(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 542 | if(!res && (valid & FATTR_UTIME)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 543 | res = do_utime(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 544 | if(!res) { |
| 545 | struct stat buf; |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 546 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 547 | if(!res) { |
| 548 | memset(&outarg, 0, sizeof(struct fuse_setattr_out)); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 549 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 550 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 551 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 552 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 553 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 554 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 555 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 556 | } |
| 557 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 558 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 559 | { |
| 560 | int res; |
| 561 | char link[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 562 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 563 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 564 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 565 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 566 | if(path != NULL) { |
| 567 | res = -ENOSYS; |
| 568 | if(f->op.readlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 569 | res = f->op.readlink(path, link, sizeof(link)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 570 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 571 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 572 | link[PATH_MAX] = '\0'; |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 573 | send_reply(f, in, res, link, res == 0 ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 577 | { |
| 578 | int res; |
| 579 | struct fuse_getdir_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 580 | struct fuse_dirhandle dh; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 581 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 582 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 583 | dh.fuse = f; |
| 584 | dh.fp = tmpfile(); |
| 585 | dh.dir = in->ino; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 586 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 587 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 588 | if(path != NULL) { |
| 589 | res = -ENOSYS; |
| 590 | if(f->op.getdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 591 | res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 592 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 593 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 594 | fflush(dh.fp); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 595 | |
| 596 | memset(&arg, 0, sizeof(struct fuse_getdir_out)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 597 | arg.fd = fileno(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 598 | send_reply(f, in, res, &arg, sizeof(arg)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 599 | fclose(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 602 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 603 | struct fuse_mknod_in *inarg) |
| 604 | { |
| 605 | int res; |
| 606 | char *path; |
| 607 | struct fuse_mknod_out outarg; |
| 608 | struct stat buf; |
| 609 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 610 | res = -ENOENT; |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 611 | path = get_path_name(f, in->ino, PARAM(inarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 612 | if(path != NULL) { |
| 613 | res = -ENOSYS; |
| 614 | if(f->op.mknod && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 615 | res = f->op.mknod(path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 616 | if(res == 0) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 617 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 618 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 619 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 620 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 621 | if(res == 0) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 622 | memset(&outarg, 0, sizeof(struct fuse_mknod_out)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 623 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 624 | outarg.ino = find_node(f, in->ino, PARAM(inarg), &outarg.attr, |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 625 | in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 626 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 627 | |
| 628 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 629 | } |
| 630 | |
| 631 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 632 | struct fuse_mkdir_in *inarg) |
| 633 | { |
| 634 | int res; |
| 635 | char *path; |
| 636 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 637 | res = -ENOENT; |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 638 | path = get_path_name(f, in->ino, PARAM(inarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 639 | if(path != NULL) { |
| 640 | res = -ENOSYS; |
| 641 | if(f->op.mkdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 642 | res = f->op.mkdir(path, inarg->mode); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 643 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 644 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 645 | send_reply(f, in, res, NULL, 0); |
| 646 | } |
| 647 | |
| 648 | static void do_remove(struct fuse *f, struct fuse_in_header *in, char *name) |
| 649 | { |
| 650 | int res; |
| 651 | char *path; |
| 652 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 653 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 654 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 655 | if(path != NULL) { |
| 656 | res = -ENOSYS; |
| 657 | if(in->opcode == FUSE_UNLINK) { |
| 658 | if(f->op.unlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 659 | res = f->op.unlink(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 660 | } |
| 661 | else { |
| 662 | if(f->op.rmdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 663 | res = f->op.rmdir(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 664 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 665 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 666 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 667 | if(res == 0) |
| 668 | remove_node(f, in->ino, name); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 669 | send_reply(f, in, res, NULL, 0); |
| 670 | } |
| 671 | |
| 672 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 673 | char *link) |
| 674 | { |
| 675 | int res; |
| 676 | char *path; |
| 677 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 678 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 679 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 680 | if(path != NULL) { |
| 681 | res = -ENOSYS; |
| 682 | if(f->op.symlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 683 | res = f->op.symlink(link, path); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 684 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 685 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 686 | send_reply(f, in, res, NULL, 0); |
| 687 | } |
| 688 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 689 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 690 | struct fuse_rename_in *inarg) |
| 691 | { |
| 692 | int res; |
| 693 | fino_t olddir = in->ino; |
| 694 | fino_t newdir = inarg->newdir; |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 695 | char *oldname = PARAM(inarg); |
| 696 | char *newname = oldname + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 697 | char *oldpath; |
| 698 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 699 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 700 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 701 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 702 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 703 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 704 | if(newpath != NULL) { |
| 705 | res = -ENOSYS; |
| 706 | if(f->op.rename) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 707 | res = f->op.rename(oldpath, newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 708 | if(res == 0) |
| 709 | rename_node(f, olddir, oldname, newdir, newname); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 710 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 711 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 712 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 713 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 714 | send_reply(f, in, res, NULL, 0); |
| 715 | } |
| 716 | |
| 717 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 718 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 719 | { |
| 720 | int res; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 721 | char *oldpath; |
| 722 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 723 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 724 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 725 | oldpath = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 726 | if(oldpath != NULL) { |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 727 | newpath = get_path_name(f, arg->newdir, PARAM(arg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 728 | if(newpath != NULL) { |
| 729 | res = -ENOSYS; |
| 730 | if(f->op.link) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 731 | res = f->op.link(oldpath, newpath); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 732 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 733 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 734 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 735 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 736 | send_reply(f, in, res, NULL, 0); |
| 737 | } |
| 738 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 739 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 740 | struct fuse_open_in *arg) |
| 741 | { |
| 742 | int res; |
| 743 | char *path; |
| 744 | |
| 745 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 746 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 747 | if(path != NULL) { |
| 748 | res = -ENOSYS; |
| 749 | if(f->op.open) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 750 | res = f->op.open(path, arg->flags); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 751 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 752 | } |
| 753 | send_reply(f, in, res, NULL, 0); |
| 754 | } |
| 755 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 756 | static void do_release(struct fuse *f, struct fuse_in_header *in) |
| 757 | { |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 758 | char *path; |
| 759 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 760 | path = get_path(f, in->ino); |
| 761 | if(path != NULL) { |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 762 | if(f->op.release) |
Miklos Szeredi | 383a9df | 2002-12-10 14:54:57 +0000 | [diff] [blame^] | 763 | f->op.release(path); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 764 | free(path); |
| 765 | } |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 766 | } |
| 767 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 768 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 769 | struct fuse_read_in *arg) |
| 770 | { |
| 771 | int res; |
| 772 | char *path; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 773 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size); |
| 774 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 775 | char *buf = outbuf + sizeof(struct fuse_out_header); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 776 | size_t size; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 777 | size_t outsize; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 778 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 779 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 780 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 781 | if(path != NULL) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 782 | if(f->flags & FUSE_DEBUG) { |
| 783 | printf("READ %u bytes from %llu\n", arg->size, arg->offset); |
| 784 | fflush(stdout); |
| 785 | } |
| 786 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 787 | res = -ENOSYS; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 788 | if(f->op.read) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 789 | res = f->op.read(path, buf, arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 790 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 791 | } |
| 792 | |
| 793 | size = 0; |
| 794 | if(res > 0) { |
| 795 | size = res; |
| 796 | res = 0; |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 797 | if(f->flags & FUSE_DEBUG) { |
| 798 | printf(" READ %u bytes\n", size); |
| 799 | fflush(stdout); |
| 800 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 801 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 802 | memset(out, 0, sizeof(struct fuse_out_header)); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 803 | out->unique = in->unique; |
| 804 | out->error = res; |
| 805 | outsize = sizeof(struct fuse_out_header) + size; |
| 806 | |
| 807 | send_reply_raw(f, outbuf, outsize); |
| 808 | free(outbuf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 809 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 810 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 811 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 812 | struct fuse_write_in *arg) |
| 813 | { |
| 814 | int res; |
| 815 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 816 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 817 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 818 | path = get_path(f, in->ino); |
| 819 | if(path != NULL) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 820 | if(f->flags & FUSE_DEBUG) { |
| 821 | printf("WRITE %u bytes to %llu\n", arg->size, arg->offset); |
| 822 | fflush(stdout); |
| 823 | } |
| 824 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 825 | res = -ENOSYS; |
| 826 | if(f->op.write) |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 827 | res = f->op.write(path, PARAM(arg), arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 828 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 829 | } |
| 830 | |
| 831 | if(res > 0) { |
| 832 | if((size_t) res != arg->size) { |
| 833 | fprintf(stderr, "short write: %u (should be %u)\n", res, |
| 834 | arg->size); |
| 835 | res = -EIO; |
| 836 | } |
| 837 | else |
| 838 | res = 0; |
| 839 | } |
| 840 | |
| 841 | send_reply(f, in, res, NULL, 0); |
| 842 | } |
| 843 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 844 | static void do_statfs(struct fuse *f, struct fuse_in_header *in) |
| 845 | { |
| 846 | int res; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 847 | struct fuse_statfs_out arg; |
| 848 | |
| 849 | res = -ENOSYS; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 850 | if(f->op.statfs) { |
| 851 | memset(&arg, 0, sizeof(struct fuse_statfs_out)); |
Miklos Szeredi | 24ed945 | 2002-10-07 10:24:26 +0000 | [diff] [blame] | 852 | res = f->op.statfs((struct fuse_statfs *) &arg.st); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 853 | } |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 854 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 855 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 856 | } |
| 857 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 858 | static void free_cmd(struct fuse_cmd *cmd) |
| 859 | { |
| 860 | free(cmd->buf); |
| 861 | free(cmd); |
| 862 | } |
| 863 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 864 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 865 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 866 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 867 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 868 | size_t argsize; |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 869 | struct fuse_context *ctx = fuse_get_context(f); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 870 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 871 | dec_avail(f); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 872 | |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 873 | if((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 874 | printf("unique: %i, opcode: %s (%i), ino: %li, insize: %i\n", |
| 875 | in->unique, opname(in->opcode), in->opcode, in->ino, |
| 876 | cmd->buflen); |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 877 | fflush(stdout); |
| 878 | } |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 879 | |
| 880 | ctx->uid = in->uid; |
| 881 | ctx->gid = in->gid; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 882 | |
| 883 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 884 | |
| 885 | switch(in->opcode) { |
| 886 | case FUSE_LOOKUP: |
| 887 | do_lookup(f, in, (char *) inarg); |
| 888 | break; |
| 889 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 890 | case FUSE_GETATTR: |
| 891 | do_getattr(f, in); |
| 892 | break; |
| 893 | |
| 894 | case FUSE_SETATTR: |
| 895 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 896 | break; |
| 897 | |
| 898 | case FUSE_READLINK: |
| 899 | do_readlink(f, in); |
| 900 | break; |
| 901 | |
| 902 | case FUSE_GETDIR: |
| 903 | do_getdir(f, in); |
| 904 | break; |
| 905 | |
| 906 | case FUSE_MKNOD: |
| 907 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 908 | break; |
| 909 | |
| 910 | case FUSE_MKDIR: |
| 911 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 912 | break; |
| 913 | |
| 914 | case FUSE_UNLINK: |
| 915 | case FUSE_RMDIR: |
| 916 | do_remove(f, in, (char *) inarg); |
| 917 | break; |
| 918 | |
| 919 | case FUSE_SYMLINK: |
| 920 | do_symlink(f, in, (char *) inarg, |
| 921 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 922 | break; |
| 923 | |
| 924 | case FUSE_RENAME: |
| 925 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 926 | break; |
| 927 | |
| 928 | case FUSE_LINK: |
| 929 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 930 | break; |
| 931 | |
| 932 | case FUSE_OPEN: |
| 933 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 934 | break; |
| 935 | |
| 936 | case FUSE_READ: |
| 937 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 938 | break; |
| 939 | |
| 940 | case FUSE_WRITE: |
| 941 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 942 | break; |
| 943 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 944 | case FUSE_STATFS: |
| 945 | do_statfs(f, in); |
| 946 | break; |
| 947 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 948 | case FUSE_RELEASE: |
| 949 | do_release(f, in); |
| 950 | break; |
| 951 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 952 | default: |
| 953 | fprintf(stderr, "Operation %i not implemented\n", in->opcode); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 954 | send_reply(f, in, -ENOSYS, NULL, 0); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 955 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 956 | |
| 957 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 958 | } |
| 959 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 960 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 961 | { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 962 | ssize_t res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 963 | struct fuse_cmd *cmd; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 964 | struct fuse_in_header *in; |
| 965 | void *inarg; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 966 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 967 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
| 968 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 969 | in = (struct fuse_in_header *) cmd->buf; |
| 970 | inarg = cmd->buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 971 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 972 | do { |
| 973 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 974 | if(res == -1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 975 | free_cmd(cmd); |
| 976 | if(errno == EINTR) |
| 977 | return NULL; |
| 978 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 979 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 980 | if(errno != ENODEV) { |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 981 | /* BAD... This will happen again */ |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 982 | perror("fuse: reading device"); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 983 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 984 | |
| 985 | fuse_exit(f); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 986 | return NULL; |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 987 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 988 | if((size_t) res < sizeof(struct fuse_in_header)) { |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 989 | free_cmd(cmd); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 990 | /* Cannot happen */ |
| 991 | fprintf(stderr, "short read on fuse device\n"); |
| 992 | fuse_exit(f); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 993 | return NULL; |
| 994 | } |
| 995 | cmd->buflen = res; |
| 996 | |
| 997 | /* Forget is special, it can be done without messing with threads. */ |
| 998 | if(in->opcode == FUSE_FORGET) |
| 999 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 1000 | |
| 1001 | } while(in->opcode == FUSE_FORGET); |
| 1002 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1003 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1004 | } |
| 1005 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1006 | void fuse_loop(struct fuse *f) |
| 1007 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1008 | while(1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1009 | struct fuse_cmd *cmd; |
| 1010 | |
| 1011 | if(f->exited) |
| 1012 | return; |
| 1013 | |
| 1014 | cmd = __fuse_read_cmd(f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1015 | if(cmd == NULL) |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1016 | continue; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1017 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1018 | __fuse_process_cmd(f, cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1019 | } |
| 1020 | } |
| 1021 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1022 | void fuse_exit(struct fuse *f) |
| 1023 | { |
| 1024 | f->exited = 1; |
| 1025 | } |
| 1026 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1027 | struct fuse_context *fuse_get_context(struct fuse *f) |
| 1028 | { |
| 1029 | if(f->getcontext) |
| 1030 | return f->getcontext(f); |
| 1031 | else |
| 1032 | return &f->context; |
| 1033 | } |
| 1034 | |
| 1035 | struct fuse *fuse_new(int fd, int flags, const struct fuse_operations *op) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1036 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1037 | struct fuse *f; |
| 1038 | struct node *root; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1039 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1040 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1041 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1042 | f->flags = flags; |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1043 | f->fd = fd; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1044 | f->ctr = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1045 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1046 | f->name_table_size = 14057; |
| 1047 | f->name_table = (struct node **) |
| 1048 | calloc(1, sizeof(struct node *) * f->name_table_size); |
| 1049 | f->ino_table_size = 14057; |
| 1050 | f->ino_table = (struct node **) |
| 1051 | calloc(1, sizeof(struct node *) * f->ino_table_size); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1052 | pthread_mutex_init(&f->lock, NULL); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1053 | f->numworker = 0; |
| 1054 | f->numavail = 0; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1055 | f->op = *op; |
| 1056 | f->getcontext = NULL; |
| 1057 | f->context.uid = 0; |
| 1058 | f->context.gid = 0; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1059 | f->exited = 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1060 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1061 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1062 | root->mode = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1063 | root->rdev = 0; |
| 1064 | root->name = strdup("/"); |
| 1065 | root->parent = 0; |
| 1066 | hash_ino(f, root, FUSE_ROOT_INO); |
| 1067 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1068 | return f; |
| 1069 | } |
| 1070 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1071 | void fuse_destroy(struct fuse *f) |
| 1072 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1073 | size_t i; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1074 | for(i = 0; i < f->ino_table_size; i++) { |
| 1075 | struct node *node; |
| 1076 | struct node *next; |
| 1077 | for(node = f->ino_table[i]; node != NULL; node = next) { |
| 1078 | next = node->ino_next; |
| 1079 | free_node(node); |
| 1080 | } |
| 1081 | } |
| 1082 | free(f->ino_table); |
| 1083 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1084 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1085 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1086 | } |