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 | { |
| 758 | int res; |
| 759 | char *path; |
| 760 | |
| 761 | res = -ENOENT; |
| 762 | path = get_path(f, in->ino); |
| 763 | if(path != NULL) { |
| 764 | res = -ENOSYS; |
| 765 | if(f->op.release) |
| 766 | res = f->op.release(path); |
| 767 | free(path); |
| 768 | } |
| 769 | send_reply(f, in, res, NULL, 0); |
| 770 | } |
| 771 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 772 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 773 | struct fuse_read_in *arg) |
| 774 | { |
| 775 | int res; |
| 776 | char *path; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 777 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size); |
| 778 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 779 | char *buf = outbuf + sizeof(struct fuse_out_header); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 780 | size_t size; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 781 | size_t outsize; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 782 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 783 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 784 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 785 | if(path != NULL) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 786 | if(f->flags & FUSE_DEBUG) { |
| 787 | printf("READ %u bytes from %llu\n", arg->size, arg->offset); |
| 788 | fflush(stdout); |
| 789 | } |
| 790 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 791 | res = -ENOSYS; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 792 | if(f->op.read) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 793 | res = f->op.read(path, buf, arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 794 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 795 | } |
| 796 | |
| 797 | size = 0; |
| 798 | if(res > 0) { |
| 799 | size = res; |
| 800 | res = 0; |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 801 | if(f->flags & FUSE_DEBUG) { |
| 802 | printf(" READ %u bytes\n", size); |
| 803 | fflush(stdout); |
| 804 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 805 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 806 | memset(out, 0, sizeof(struct fuse_out_header)); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 807 | out->unique = in->unique; |
| 808 | out->error = res; |
| 809 | outsize = sizeof(struct fuse_out_header) + size; |
| 810 | |
| 811 | send_reply_raw(f, outbuf, outsize); |
| 812 | free(outbuf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 813 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 814 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 815 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 816 | struct fuse_write_in *arg) |
| 817 | { |
| 818 | int res; |
| 819 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 820 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 821 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 822 | path = get_path(f, in->ino); |
| 823 | if(path != NULL) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 824 | if(f->flags & FUSE_DEBUG) { |
| 825 | printf("WRITE %u bytes to %llu\n", arg->size, arg->offset); |
| 826 | fflush(stdout); |
| 827 | } |
| 828 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 829 | res = -ENOSYS; |
| 830 | if(f->op.write) |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 831 | res = f->op.write(path, PARAM(arg), arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 832 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 833 | } |
| 834 | |
| 835 | if(res > 0) { |
| 836 | if((size_t) res != arg->size) { |
| 837 | fprintf(stderr, "short write: %u (should be %u)\n", res, |
| 838 | arg->size); |
| 839 | res = -EIO; |
| 840 | } |
| 841 | else |
| 842 | res = 0; |
| 843 | } |
| 844 | |
| 845 | send_reply(f, in, res, NULL, 0); |
| 846 | } |
| 847 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 848 | static void do_statfs(struct fuse *f, struct fuse_in_header *in) |
| 849 | { |
| 850 | int res; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 851 | struct fuse_statfs_out arg; |
| 852 | |
| 853 | res = -ENOSYS; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 854 | if(f->op.statfs) { |
| 855 | memset(&arg, 0, sizeof(struct fuse_statfs_out)); |
Miklos Szeredi | 24ed945 | 2002-10-07 10:24:26 +0000 | [diff] [blame] | 856 | res = f->op.statfs((struct fuse_statfs *) &arg.st); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 857 | } |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 858 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 859 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 860 | } |
| 861 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 862 | static void free_cmd(struct fuse_cmd *cmd) |
| 863 | { |
| 864 | free(cmd->buf); |
| 865 | free(cmd); |
| 866 | } |
| 867 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 868 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 869 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 870 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 871 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 872 | size_t argsize; |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 873 | struct fuse_context *ctx = fuse_get_context(f); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 874 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 875 | dec_avail(f); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 876 | |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 877 | if((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame^] | 878 | printf("unique: %i, opcode: %s (%i), ino: %li, insize: %i\n", |
| 879 | in->unique, opname(in->opcode), in->opcode, in->ino, |
| 880 | cmd->buflen); |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 881 | fflush(stdout); |
| 882 | } |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 883 | |
| 884 | ctx->uid = in->uid; |
| 885 | ctx->gid = in->gid; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 886 | |
| 887 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 888 | |
| 889 | switch(in->opcode) { |
| 890 | case FUSE_LOOKUP: |
| 891 | do_lookup(f, in, (char *) inarg); |
| 892 | break; |
| 893 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 894 | case FUSE_GETATTR: |
| 895 | do_getattr(f, in); |
| 896 | break; |
| 897 | |
| 898 | case FUSE_SETATTR: |
| 899 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 900 | break; |
| 901 | |
| 902 | case FUSE_READLINK: |
| 903 | do_readlink(f, in); |
| 904 | break; |
| 905 | |
| 906 | case FUSE_GETDIR: |
| 907 | do_getdir(f, in); |
| 908 | break; |
| 909 | |
| 910 | case FUSE_MKNOD: |
| 911 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 912 | break; |
| 913 | |
| 914 | case FUSE_MKDIR: |
| 915 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 916 | break; |
| 917 | |
| 918 | case FUSE_UNLINK: |
| 919 | case FUSE_RMDIR: |
| 920 | do_remove(f, in, (char *) inarg); |
| 921 | break; |
| 922 | |
| 923 | case FUSE_SYMLINK: |
| 924 | do_symlink(f, in, (char *) inarg, |
| 925 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 926 | break; |
| 927 | |
| 928 | case FUSE_RENAME: |
| 929 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 930 | break; |
| 931 | |
| 932 | case FUSE_LINK: |
| 933 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 934 | break; |
| 935 | |
| 936 | case FUSE_OPEN: |
| 937 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 938 | break; |
| 939 | |
| 940 | case FUSE_READ: |
| 941 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 942 | break; |
| 943 | |
| 944 | case FUSE_WRITE: |
| 945 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 946 | break; |
| 947 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 948 | case FUSE_STATFS: |
| 949 | do_statfs(f, in); |
| 950 | break; |
| 951 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame^] | 952 | case FUSE_RELEASE: |
| 953 | do_release(f, in); |
| 954 | break; |
| 955 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 956 | default: |
| 957 | fprintf(stderr, "Operation %i not implemented\n", in->opcode); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 958 | send_reply(f, in, -ENOSYS, NULL, 0); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 959 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 960 | |
| 961 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 964 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 965 | { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 966 | ssize_t res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 967 | struct fuse_cmd *cmd; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 968 | struct fuse_in_header *in; |
| 969 | void *inarg; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 970 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 971 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
| 972 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 973 | in = (struct fuse_in_header *) cmd->buf; |
| 974 | inarg = cmd->buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 975 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 976 | do { |
| 977 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 978 | if(res == -1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 979 | free_cmd(cmd); |
| 980 | if(errno == EINTR) |
| 981 | return NULL; |
| 982 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 983 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 984 | if(errno != ENODEV) { |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 985 | /* BAD... This will happen again */ |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 986 | perror("fuse: reading device"); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 987 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 988 | |
| 989 | fuse_exit(f); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 990 | return NULL; |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 991 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 992 | if((size_t) res < sizeof(struct fuse_in_header)) { |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 993 | free_cmd(cmd); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 994 | /* Cannot happen */ |
| 995 | fprintf(stderr, "short read on fuse device\n"); |
| 996 | fuse_exit(f); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 997 | return NULL; |
| 998 | } |
| 999 | cmd->buflen = res; |
| 1000 | |
| 1001 | /* Forget is special, it can be done without messing with threads. */ |
| 1002 | if(in->opcode == FUSE_FORGET) |
| 1003 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 1004 | |
| 1005 | } while(in->opcode == FUSE_FORGET); |
| 1006 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1007 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1008 | } |
| 1009 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1010 | void fuse_loop(struct fuse *f) |
| 1011 | { |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1012 | while(1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1013 | struct fuse_cmd *cmd; |
| 1014 | |
| 1015 | if(f->exited) |
| 1016 | return; |
| 1017 | |
| 1018 | cmd = __fuse_read_cmd(f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1019 | if(cmd == NULL) |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1020 | continue; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1021 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1022 | __fuse_process_cmd(f, cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1023 | } |
| 1024 | } |
| 1025 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1026 | void fuse_exit(struct fuse *f) |
| 1027 | { |
| 1028 | f->exited = 1; |
| 1029 | } |
| 1030 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1031 | struct fuse_context *fuse_get_context(struct fuse *f) |
| 1032 | { |
| 1033 | if(f->getcontext) |
| 1034 | return f->getcontext(f); |
| 1035 | else |
| 1036 | return &f->context; |
| 1037 | } |
| 1038 | |
| 1039 | 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] | 1040 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1041 | struct fuse *f; |
| 1042 | struct node *root; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1043 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1044 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1045 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1046 | f->flags = flags; |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1047 | f->fd = fd; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1048 | f->ctr = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1049 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1050 | f->name_table_size = 14057; |
| 1051 | f->name_table = (struct node **) |
| 1052 | calloc(1, sizeof(struct node *) * f->name_table_size); |
| 1053 | f->ino_table_size = 14057; |
| 1054 | f->ino_table = (struct node **) |
| 1055 | calloc(1, sizeof(struct node *) * f->ino_table_size); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1056 | pthread_mutex_init(&f->lock, NULL); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1057 | f->numworker = 0; |
| 1058 | f->numavail = 0; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1059 | f->op = *op; |
| 1060 | f->getcontext = NULL; |
| 1061 | f->context.uid = 0; |
| 1062 | f->context.gid = 0; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1063 | f->exited = 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1064 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1065 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1066 | root->mode = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1067 | root->rdev = 0; |
| 1068 | root->name = strdup("/"); |
| 1069 | root->parent = 0; |
| 1070 | hash_ino(f, root, FUSE_ROOT_INO); |
| 1071 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1072 | return f; |
| 1073 | } |
| 1074 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1075 | void fuse_destroy(struct fuse *f) |
| 1076 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1077 | size_t i; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1078 | for(i = 0; i < f->ino_table_size; i++) { |
| 1079 | struct node *node; |
| 1080 | struct node *next; |
| 1081 | for(node = f->ino_table[i]; node != NULL; node = next) { |
| 1082 | next = node->ino_next; |
| 1083 | free_node(node); |
| 1084 | } |
| 1085 | } |
| 1086 | free(f->ino_table); |
| 1087 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1088 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1089 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1090 | } |