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