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