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