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 | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 22 | #define ENTRY_REVALIDATE_TIME 1 /* sec */ |
| 23 | #define ATTR_REVALIDATE_TIME 1 /* sec */ |
| 24 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 25 | static const char *opname(enum fuse_opcode opcode) |
| 26 | { |
| 27 | switch(opcode) { |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 28 | case FUSE_LOOKUP: return "LOOKUP"; |
| 29 | case FUSE_FORGET: return "FORGET"; |
| 30 | case FUSE_GETATTR: return "GETATTR"; |
| 31 | case FUSE_SETATTR: return "SETATTR"; |
| 32 | case FUSE_READLINK: return "READLINK"; |
| 33 | case FUSE_SYMLINK: return "SYMLINK"; |
| 34 | case FUSE_GETDIR: return "GETDIR"; |
| 35 | case FUSE_MKNOD: return "MKNOD"; |
| 36 | case FUSE_MKDIR: return "MKDIR"; |
| 37 | case FUSE_UNLINK: return "UNLINK"; |
| 38 | case FUSE_RMDIR: return "RMDIR"; |
| 39 | case FUSE_RENAME: return "RENAME"; |
| 40 | case FUSE_LINK: return "LINK"; |
| 41 | case FUSE_OPEN: return "OPEN"; |
| 42 | case FUSE_READ: return "READ"; |
| 43 | case FUSE_WRITE: return "WRITE"; |
| 44 | case FUSE_STATFS: return "STATFS"; |
Miklos Szeredi | 99f2074 | 2004-05-19 08:01:10 +0000 | [diff] [blame] | 45 | case FUSE_FLUSH: return "FLUSH"; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 46 | case FUSE_RELEASE: return "RELEASE"; |
| 47 | case FUSE_FSYNC: return "FSYNC"; |
| 48 | case FUSE_SETXATTR: return "SETXATTR"; |
| 49 | case FUSE_GETXATTR: return "GETXATTR"; |
| 50 | case FUSE_LISTXATTR: return "LISTXATTR"; |
| 51 | case FUSE_REMOVEXATTR: return "REMOVEXATTR"; |
Miklos Szeredi | 99f2074 | 2004-05-19 08:01:10 +0000 | [diff] [blame] | 52 | default: return "???"; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 53 | } |
| 54 | } |
| 55 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 56 | static inline void inc_avail(struct fuse *f) |
| 57 | { |
| 58 | pthread_mutex_lock(&f->lock); |
| 59 | f->numavail ++; |
| 60 | pthread_mutex_unlock(&f->lock); |
| 61 | } |
| 62 | |
| 63 | static inline void dec_avail(struct fuse *f) |
| 64 | { |
| 65 | pthread_mutex_lock(&f->lock); |
| 66 | f->numavail --; |
| 67 | pthread_mutex_unlock(&f->lock); |
| 68 | } |
| 69 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 70 | static struct node *__get_node(struct fuse *f, fino_t ino) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 71 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 72 | size_t hash = ino % f->ino_table_size; |
| 73 | struct node *node; |
| 74 | |
| 75 | for(node = f->ino_table[hash]; node != NULL; node = node->ino_next) |
| 76 | if(node->ino == ino) |
| 77 | return node; |
| 78 | |
| 79 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 80 | } |
| 81 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 82 | static struct node *get_node(struct fuse *f, fino_t ino) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 83 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 84 | struct node *node = __get_node(f, ino); |
| 85 | if(node != NULL) |
| 86 | return node; |
| 87 | |
| 88 | fprintf(stderr, "fuse internal error: inode %lu not found\n", ino); |
| 89 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 90 | } |
| 91 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 92 | static void hash_ino(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 93 | { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 94 | size_t hash = node->ino % f->ino_table_size; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 95 | node->ino_next = f->ino_table[hash]; |
| 96 | f->ino_table[hash] = node; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 97 | } |
| 98 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 99 | static void unhash_ino(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 100 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 101 | size_t hash = node->ino % f->ino_table_size; |
| 102 | struct node **nodep = &f->ino_table[hash]; |
| 103 | |
| 104 | for(; *nodep != NULL; nodep = &(*nodep)->ino_next) |
| 105 | if(*nodep == node) { |
| 106 | *nodep = node->ino_next; |
| 107 | return; |
| 108 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 111 | static fino_t next_ino(struct fuse *f) |
| 112 | { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 113 | do { |
| 114 | f->ctr++; |
| 115 | if(!f->ctr) |
| 116 | f->generation ++; |
| 117 | } while(f->ctr == 0 || __get_node(f, f->ctr) != NULL); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 118 | return f->ctr; |
| 119 | } |
| 120 | |
| 121 | static void free_node(struct node *node) |
| 122 | { |
| 123 | free(node->name); |
| 124 | free(node); |
| 125 | } |
| 126 | |
| 127 | static unsigned int name_hash(struct fuse *f, fino_t parent, const char *name) |
| 128 | { |
| 129 | unsigned int hash = *name; |
| 130 | |
| 131 | if(hash) |
| 132 | for(name += 1; *name != '\0'; name++) |
| 133 | hash = (hash << 5) - hash + *name; |
| 134 | |
| 135 | return (hash + parent) % f->name_table_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 136 | } |
| 137 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 138 | static struct node *lookup_node(struct fuse *f, fino_t parent, |
| 139 | const char *name) |
| 140 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 141 | size_t hash = name_hash(f, parent, name); |
| 142 | struct node *node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 143 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 144 | for(node = f->name_table[hash]; node != NULL; node = node->name_next) |
| 145 | if(node->parent == parent && strcmp(node->name, name) == 0) |
| 146 | return node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 147 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 148 | return NULL; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 151 | 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] | 152 | const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 153 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 154 | size_t hash = name_hash(f, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 155 | node->parent = parent; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 156 | node->name = strdup(name); |
| 157 | node->name_next = f->name_table[hash]; |
| 158 | f->name_table[hash] = node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 159 | } |
| 160 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 161 | static void unhash_name(struct fuse *f, struct node *node) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 162 | { |
| 163 | if(node->name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 164 | size_t hash = name_hash(f, node->parent, node->name); |
| 165 | struct node **nodep = &f->name_table[hash]; |
| 166 | |
| 167 | for(; *nodep != NULL; nodep = &(*nodep)->name_next) |
| 168 | if(*nodep == node) { |
| 169 | *nodep = node->name_next; |
| 170 | node->name_next = NULL; |
| 171 | free(node->name); |
| 172 | node->name = NULL; |
| 173 | node->parent = 0; |
| 174 | return; |
| 175 | } |
| 176 | fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n", |
| 177 | node->ino); |
| 178 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 179 | } |
| 180 | } |
| 181 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 182 | static struct node *find_node(struct fuse *f, fino_t parent, char *name, |
| 183 | struct fuse_attr *attr, int version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 184 | { |
| 185 | struct node *node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 186 | int mode = attr->mode & S_IFMT; |
| 187 | int rdev = 0; |
| 188 | |
| 189 | if(S_ISCHR(mode) || S_ISBLK(mode)) |
| 190 | rdev = attr->rdev; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 191 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 192 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 193 | node = lookup_node(f, parent, name); |
| 194 | if(node != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 195 | if(node->mode == mode && node->rdev == rdev) |
| 196 | goto out; |
| 197 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 198 | unhash_name(f, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 199 | } |
| 200 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 201 | node = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 202 | node->mode = mode; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 203 | node->rdev = rdev; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 204 | node->ino = next_ino(f); |
| 205 | node->generation = f->generation; |
| 206 | hash_ino(f, node); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 207 | hash_name(f, node, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 208 | |
| 209 | out: |
| 210 | node->version = version; |
| 211 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 212 | return node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 213 | } |
| 214 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 215 | static char *add_name(char *buf, char *s, const char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 216 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 217 | size_t len = strlen(name); |
| 218 | s -= len; |
| 219 | if(s <= buf) { |
| 220 | fprintf(stderr, "fuse: path too long: ...%s\n", s + len); |
| 221 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 222 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 223 | strncpy(s, name, len); |
| 224 | s--; |
| 225 | *s = '/'; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 226 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 227 | return s; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 228 | } |
| 229 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 230 | 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] | 231 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 232 | char buf[FUSE_MAX_PATH]; |
| 233 | char *s = buf + FUSE_MAX_PATH - 1; |
| 234 | struct node *node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 235 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 236 | *s = '\0'; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 237 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 238 | if(name != NULL) { |
| 239 | s = add_name(buf, s, name); |
| 240 | if(s == NULL) |
| 241 | return NULL; |
| 242 | } |
| 243 | |
| 244 | pthread_mutex_lock(&f->lock); |
| 245 | for(node = get_node(f, ino); node->ino != FUSE_ROOT_INO; |
| 246 | node = get_node(f, node->parent)) { |
| 247 | if(node->name == NULL) { |
| 248 | s = NULL; |
| 249 | break; |
| 250 | } |
| 251 | |
| 252 | s = add_name(buf, s, node->name); |
| 253 | if(s == NULL) |
| 254 | break; |
| 255 | } |
| 256 | pthread_mutex_unlock(&f->lock); |
| 257 | |
| 258 | if(s == NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 259 | return NULL; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 260 | else if(*s == '\0') |
| 261 | return strdup("/"); |
| 262 | else |
| 263 | return strdup(s); |
| 264 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 265 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 266 | static char *get_path(struct fuse *f, fino_t ino) |
| 267 | { |
| 268 | return get_path_name(f, ino, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 269 | } |
| 270 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 271 | static void destroy_node(struct fuse *f, fino_t ino, int version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 272 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 273 | struct node *node; |
| 274 | |
| 275 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 276 | node = get_node(f, ino); |
Miklos Szeredi | 39f2867 | 2001-11-14 14:52:54 +0000 | [diff] [blame] | 277 | if(node->version == version && ino != FUSE_ROOT_INO) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 278 | unhash_name(f, node); |
| 279 | unhash_ino(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 280 | free_node(node); |
| 281 | } |
| 282 | pthread_mutex_unlock(&f->lock); |
| 283 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 284 | } |
| 285 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 286 | static void remove_node(struct fuse *f, fino_t dir, const char *name) |
| 287 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 288 | struct node *node; |
| 289 | |
| 290 | pthread_mutex_lock(&f->lock); |
| 291 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 292 | if(node == NULL) { |
| 293 | fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n", |
| 294 | dir, name); |
| 295 | abort(); |
| 296 | } |
| 297 | unhash_name(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 298 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 299 | } |
| 300 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 301 | static void rename_node(struct fuse *f, fino_t olddir, const char *oldname, |
| 302 | fino_t newdir, const char *newname) |
| 303 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 304 | struct node *node; |
| 305 | struct node *newnode; |
| 306 | |
| 307 | pthread_mutex_lock(&f->lock); |
| 308 | node = lookup_node(f, olddir, oldname); |
| 309 | newnode = lookup_node(f, newdir, newname); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 310 | if(node == NULL) { |
| 311 | fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n", |
| 312 | olddir, oldname); |
| 313 | abort(); |
| 314 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 315 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 316 | if(newnode != NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 317 | unhash_name(f, newnode); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 318 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 319 | unhash_name(f, node); |
| 320 | hash_name(f, node, newdir, newname); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 321 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 322 | } |
| 323 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 324 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 325 | { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 326 | attr->mode = stbuf->st_mode; |
| 327 | attr->nlink = stbuf->st_nlink; |
| 328 | attr->uid = stbuf->st_uid; |
| 329 | attr->gid = stbuf->st_gid; |
| 330 | attr->rdev = stbuf->st_rdev; |
| 331 | attr->size = stbuf->st_size; |
| 332 | attr->blocks = stbuf->st_blocks; |
| 333 | attr->atime = stbuf->st_atime; |
| 334 | attr->atimensec = stbuf->st_atim.tv_nsec; |
| 335 | attr->mtime = stbuf->st_mtime; |
| 336 | attr->mtimensec = stbuf->st_mtim.tv_nsec; |
| 337 | attr->ctime = stbuf->st_ctime; |
| 338 | attr->ctimensec = stbuf->st_ctim.tv_nsec; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 339 | } |
| 340 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 341 | static int fill_dir(struct fuse_dirhandle *dh, char *name, int type) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 342 | { |
| 343 | struct fuse_dirent dirent; |
| 344 | size_t reclen; |
| 345 | size_t res; |
| 346 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 347 | dirent.ino = (unsigned long) -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 348 | dirent.namelen = strlen(name); |
| 349 | strncpy(dirent.name, name, sizeof(dirent.name)); |
| 350 | dirent.type = type; |
| 351 | reclen = FUSE_DIRENT_SIZE(&dirent); |
| 352 | res = fwrite(&dirent, reclen, 1, dh->fp); |
| 353 | if(res == 0) { |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 354 | perror("fuse: writing directory file"); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 355 | return -EIO; |
| 356 | } |
| 357 | return 0; |
| 358 | } |
| 359 | |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 360 | 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] | 361 | { |
| 362 | int res; |
| 363 | |
| 364 | if((f->flags & FUSE_DEBUG)) { |
| 365 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 366 | printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique, |
| 367 | out->error, strerror(-out->error), outsize); |
| 368 | fflush(stdout); |
| 369 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 370 | |
Miklos Szeredi | 4e71c9f | 2004-02-09 12:05:14 +0000 | [diff] [blame] | 371 | /* This needs to be done before the reply, otherwise the scheduler |
| 372 | could play tricks with us, and only let the counter be increased |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 373 | long after the operation is done */ |
| 374 | inc_avail(f); |
| 375 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 376 | res = write(f->fd, outbuf, outsize); |
| 377 | if(res == -1) { |
| 378 | /* ENOENT means the operation was interrupted */ |
Miklos Szeredi | 4e71c9f | 2004-02-09 12:05:14 +0000 | [diff] [blame] | 379 | if(!f->exited && errno != ENOENT) |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 380 | perror("fuse: writing device"); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 381 | return -errno; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 382 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 383 | return 0; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 384 | } |
| 385 | |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 386 | static int send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
| 387 | void *arg, size_t argsize) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 388 | { |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 389 | int res; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 390 | char *outbuf; |
| 391 | size_t outsize; |
| 392 | struct fuse_out_header *out; |
| 393 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 394 | if(error <= -1000 || error > 0) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 395 | fprintf(stderr, "fuse: bad error value: %i\n", error); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 396 | error = -ERANGE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 397 | } |
| 398 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 399 | if(error) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 400 | argsize = 0; |
| 401 | |
| 402 | outsize = sizeof(struct fuse_out_header) + argsize; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 403 | outbuf = (char *) malloc(outsize); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 404 | out = (struct fuse_out_header *) outbuf; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 405 | memset(out, 0, sizeof(struct fuse_out_header)); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 406 | out->unique = in->unique; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 407 | out->error = error; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 408 | if(argsize != 0) |
| 409 | memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize); |
| 410 | |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 411 | res = send_reply_raw(f, outbuf, outsize); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 412 | free(outbuf); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 413 | |
| 414 | return res; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 417 | static int lookup_path(struct fuse *f, fino_t ino, int version, char *name, |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 418 | const char *path, struct fuse_entry_out *arg) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 419 | { |
| 420 | int res; |
| 421 | struct stat buf; |
| 422 | |
| 423 | res = f->op.getattr(path, &buf); |
| 424 | if(res == 0) { |
| 425 | struct node *node; |
| 426 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 427 | memset(arg, 0, sizeof(struct fuse_entry_out)); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 428 | convert_stat(&buf, &arg->attr); |
| 429 | node = find_node(f, ino, name, &arg->attr, version); |
| 430 | arg->ino = node->ino; |
| 431 | arg->generation = node->generation; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 432 | arg->entry_valid = ENTRY_REVALIDATE_TIME; |
| 433 | arg->entry_valid_nsec = 0; |
| 434 | arg->attr_valid = ATTR_REVALIDATE_TIME; |
| 435 | arg->attr_valid_nsec = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 436 | if(f->flags & FUSE_DEBUG) { |
| 437 | printf(" INO: %li\n", arg->ino); |
| 438 | fflush(stdout); |
| 439 | } |
| 440 | } |
| 441 | return res; |
| 442 | } |
| 443 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 444 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 445 | { |
| 446 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 447 | char *path; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 448 | struct fuse_entry_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 449 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 450 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 451 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 452 | if(path != NULL) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 453 | if(f->flags & FUSE_DEBUG) { |
| 454 | printf("LOOKUP %s\n", path); |
| 455 | fflush(stdout); |
| 456 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 457 | res = -ENOSYS; |
| 458 | if(f->op.getattr) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 459 | res = lookup_path(f, in->ino, in->unique, name, path, &arg); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 460 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 461 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 462 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 463 | } |
| 464 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 465 | static void do_forget(struct fuse *f, struct fuse_in_header *in, |
| 466 | struct fuse_forget_in *arg) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 467 | { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 468 | if(f->flags & FUSE_DEBUG) { |
| 469 | printf("FORGET %li/%i\n", in->ino, arg->version); |
| 470 | fflush(stdout); |
| 471 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 472 | destroy_node(f, in->ino, arg->version); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 476 | { |
| 477 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 478 | char *path; |
| 479 | struct stat buf; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 480 | struct fuse_attr_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 481 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 482 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 483 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 484 | if(path != NULL) { |
| 485 | res = -ENOSYS; |
| 486 | if(f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 487 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 488 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 489 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 490 | |
| 491 | if(res == 0) { |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 492 | memset(&arg, 0, sizeof(struct fuse_attr_out)); |
| 493 | arg.attr_valid = ATTR_REVALIDATE_TIME; |
| 494 | arg.attr_valid_nsec = 0; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 495 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 496 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 497 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 498 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 499 | } |
| 500 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 501 | 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] | 502 | { |
| 503 | int res; |
| 504 | |
| 505 | res = -ENOSYS; |
| 506 | if(f->op.chmod) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 507 | res = f->op.chmod(path, attr->mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 508 | |
| 509 | return res; |
| 510 | } |
| 511 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 512 | 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] | 513 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 514 | { |
| 515 | int res; |
| 516 | uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1; |
| 517 | gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1; |
| 518 | |
| 519 | res = -ENOSYS; |
| 520 | if(f->op.chown) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 521 | res = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 522 | |
| 523 | return res; |
| 524 | } |
| 525 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 526 | static int do_truncate(struct fuse *f, const char *path, |
| 527 | struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 528 | { |
| 529 | int res; |
| 530 | |
| 531 | res = -ENOSYS; |
| 532 | if(f->op.truncate) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 533 | res = f->op.truncate(path, attr->size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 534 | |
| 535 | return res; |
| 536 | } |
| 537 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 538 | 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] | 539 | { |
| 540 | int res; |
| 541 | struct utimbuf buf; |
| 542 | buf.actime = attr->atime; |
| 543 | buf.modtime = attr->mtime; |
| 544 | res = -ENOSYS; |
| 545 | if(f->op.utime) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 546 | res = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 547 | |
| 548 | return res; |
| 549 | } |
| 550 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 551 | static void do_setattr(struct fuse *f, struct fuse_in_header *in, |
| 552 | struct fuse_setattr_in *arg) |
| 553 | { |
| 554 | int res; |
| 555 | char *path; |
| 556 | int valid = arg->valid; |
| 557 | struct fuse_attr *attr = &arg->attr; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 558 | struct fuse_attr_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 559 | |
| 560 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 561 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 562 | if(path != NULL) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 563 | res = -ENOSYS; |
| 564 | if(f->op.getattr) { |
| 565 | res = 0; |
| 566 | if(!res && (valid & FATTR_MODE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 567 | res = do_chmod(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 568 | if(!res && (valid & (FATTR_UID | FATTR_GID))) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 569 | res = do_chown(f, path, attr, valid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 570 | if(!res && (valid & FATTR_SIZE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 571 | res = do_truncate(f, path, attr); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 572 | if(!res && (valid & (FATTR_ATIME | FATTR_MTIME)) == |
| 573 | (FATTR_ATIME | FATTR_MTIME)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 574 | res = do_utime(f, path, attr); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 575 | if(!res) { |
| 576 | struct stat buf; |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 577 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 578 | if(!res) { |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 579 | memset(&outarg, 0, sizeof(struct fuse_attr_out)); |
| 580 | outarg.attr_valid = ATTR_REVALIDATE_TIME; |
| 581 | outarg.attr_valid_nsec = 0; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 582 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 583 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 584 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 585 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 586 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 587 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 588 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 589 | } |
| 590 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 591 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 592 | { |
| 593 | int res; |
| 594 | char link[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 595 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 596 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 597 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 598 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 599 | if(path != NULL) { |
| 600 | res = -ENOSYS; |
| 601 | if(f->op.readlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 602 | res = f->op.readlink(path, link, sizeof(link)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 603 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 604 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 605 | link[PATH_MAX] = '\0'; |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 606 | send_reply(f, in, res, link, res == 0 ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 610 | { |
| 611 | int res; |
| 612 | struct fuse_getdir_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 613 | struct fuse_dirhandle dh; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 614 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 615 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 616 | dh.fuse = f; |
| 617 | dh.fp = tmpfile(); |
| 618 | dh.dir = in->ino; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 619 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 620 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 621 | if(path != NULL) { |
| 622 | res = -ENOSYS; |
| 623 | if(f->op.getdir) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 624 | res = f->op.getdir(path, &dh, (fuse_dirfil_t) fill_dir); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 625 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 626 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 627 | fflush(dh.fp); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 628 | |
| 629 | memset(&arg, 0, sizeof(struct fuse_getdir_out)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 630 | arg.fd = fileno(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 631 | send_reply(f, in, res, &arg, sizeof(arg)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 632 | fclose(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 633 | } |
| 634 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 635 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 636 | struct fuse_mknod_in *inarg) |
| 637 | { |
| 638 | int res; |
| 639 | char *path; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 640 | char *name = PARAM(inarg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 641 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 642 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 643 | res = -ENOENT; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +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) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 646 | if(f->flags & FUSE_DEBUG) { |
| 647 | printf("MKNOD %s\n", path); |
| 648 | fflush(stdout); |
| 649 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 650 | res = -ENOSYS; |
| 651 | if(f->op.mknod && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 652 | res = f->op.mknod(path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 653 | if(res == 0) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 654 | res = lookup_path(f, in->ino, in->unique, name, path, &outarg); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 655 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 656 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 657 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 658 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 659 | } |
| 660 | |
| 661 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 662 | struct fuse_mkdir_in *inarg) |
| 663 | { |
| 664 | int res; |
| 665 | char *path; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 666 | char *name = PARAM(inarg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 667 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 668 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 669 | res = -ENOENT; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 670 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 671 | if(path != NULL) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 672 | if(f->flags & FUSE_DEBUG) { |
| 673 | printf("MKDIR %s\n", path); |
| 674 | fflush(stdout); |
| 675 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 676 | res = -ENOSYS; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 677 | if(f->op.mkdir && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 678 | res = f->op.mkdir(path, inarg->mode); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 679 | if(res == 0) |
| 680 | res = lookup_path(f, in->ino, in->unique, name, path, &outarg); |
| 681 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 682 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 683 | } |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 684 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 687 | static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 688 | { |
| 689 | int res; |
| 690 | char *path; |
| 691 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 692 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 693 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 694 | if(path != NULL) { |
| 695 | res = -ENOSYS; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 696 | if(f->op.unlink) { |
| 697 | res = f->op.unlink(path); |
| 698 | if(res == 0) |
| 699 | remove_node(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 700 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 701 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 702 | } |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 703 | send_reply(f, in, res, NULL, 0); |
| 704 | } |
| 705 | |
| 706 | static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name) |
| 707 | { |
| 708 | int res; |
| 709 | char *path; |
| 710 | |
| 711 | res = -ENOENT; |
| 712 | path = get_path_name(f, in->ino, name); |
| 713 | if(path != NULL) { |
| 714 | res = -ENOSYS; |
| 715 | if(f->op.rmdir) { |
| 716 | res = f->op.rmdir(path); |
| 717 | if(res == 0) |
| 718 | remove_node(f, in->ino, name); |
| 719 | } |
| 720 | free(path); |
| 721 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 722 | send_reply(f, in, res, NULL, 0); |
| 723 | } |
| 724 | |
| 725 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 726 | char *link) |
| 727 | { |
| 728 | int res; |
| 729 | char *path; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 730 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 731 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 732 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 733 | path = get_path_name(f, in->ino, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 734 | if(path != NULL) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 735 | if(f->flags & FUSE_DEBUG) { |
| 736 | printf("SYMLINK %s\n", path); |
| 737 | fflush(stdout); |
| 738 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 739 | res = -ENOSYS; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 740 | if(f->op.symlink && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 741 | res = f->op.symlink(link, path); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 742 | if(res == 0) |
| 743 | res = lookup_path(f, in->ino, in->unique, name, path, &outarg); |
| 744 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 745 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 746 | } |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 747 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 748 | } |
| 749 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 750 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 751 | struct fuse_rename_in *inarg) |
| 752 | { |
| 753 | int res; |
| 754 | fino_t olddir = in->ino; |
| 755 | fino_t newdir = inarg->newdir; |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 756 | char *oldname = PARAM(inarg); |
| 757 | char *newname = oldname + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 758 | char *oldpath; |
| 759 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 760 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 761 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 762 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 763 | if(oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 764 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 765 | if(newpath != NULL) { |
| 766 | res = -ENOSYS; |
| 767 | if(f->op.rename) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 768 | res = f->op.rename(oldpath, newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 769 | if(res == 0) |
| 770 | rename_node(f, olddir, oldname, newdir, newname); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 771 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 772 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 773 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 774 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 775 | send_reply(f, in, res, NULL, 0); |
| 776 | } |
| 777 | |
| 778 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 779 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 780 | { |
| 781 | int res; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 782 | char *oldpath; |
| 783 | char *newpath; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 784 | char *name = PARAM(arg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 785 | struct fuse_entry_out outarg; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 786 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 787 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 788 | oldpath = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 789 | if(oldpath != NULL) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 790 | newpath = get_path_name(f, arg->newdir, name); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 791 | if(newpath != NULL) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 792 | if(f->flags & FUSE_DEBUG) { |
| 793 | printf("LINK %s\n", newpath); |
| 794 | fflush(stdout); |
| 795 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 796 | res = -ENOSYS; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 797 | if(f->op.link && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 798 | res = f->op.link(oldpath, newpath); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 799 | if(res == 0) |
| 800 | res = lookup_path(f, arg->newdir, in->unique, name, |
| 801 | newpath, &outarg); |
| 802 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 803 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 804 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 805 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 806 | } |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 807 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 808 | } |
| 809 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 810 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 811 | struct fuse_open_in *arg) |
| 812 | { |
| 813 | int res; |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 814 | int res2; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 815 | char *path; |
| 816 | |
| 817 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 818 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 819 | if(path != NULL) { |
| 820 | res = -ENOSYS; |
| 821 | if(f->op.open) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 822 | res = f->op.open(path, arg->flags); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 823 | } |
| 824 | res2 = send_reply(f, in, res, NULL, 0); |
| 825 | if(path != NULL) { |
| 826 | /* The open syscall was interrupted, so it must be cancelled */ |
| 827 | if(res == 0 && res2 == -ENOENT && f->op.release) |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 828 | f->op.release(path, arg->flags); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 829 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 830 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 831 | } |
| 832 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 833 | static void do_flush(struct fuse *f, struct fuse_in_header *in) |
| 834 | { |
| 835 | char *path; |
| 836 | int res; |
| 837 | |
| 838 | res = -ENOENT; |
| 839 | path = get_path(f, in->ino); |
| 840 | if(path != NULL) { |
| 841 | res = -ENOSYS; |
| 842 | if(f->op.flush) |
| 843 | res = f->op.flush(path); |
| 844 | free(path); |
| 845 | } |
| 846 | send_reply(f, in, res, NULL, 0); |
| 847 | } |
| 848 | |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 849 | static void do_release(struct fuse *f, struct fuse_in_header *in, |
| 850 | struct fuse_open_in *arg) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 851 | { |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 852 | char *path; |
| 853 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 854 | path = get_path(f, in->ino); |
| 855 | if(path != NULL) { |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 856 | if(f->op.release) |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 857 | f->op.release(path, arg->flags); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 858 | free(path); |
| 859 | } |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 860 | } |
| 861 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 862 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 863 | struct fuse_read_in *arg) |
| 864 | { |
| 865 | int res; |
| 866 | char *path; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 867 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size); |
| 868 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 869 | char *buf = outbuf + sizeof(struct fuse_out_header); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 870 | size_t size; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 871 | size_t outsize; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 872 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 873 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 874 | path = get_path(f, in->ino); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 875 | if(path != NULL) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 876 | if(f->flags & FUSE_DEBUG) { |
| 877 | printf("READ %u bytes from %llu\n", arg->size, arg->offset); |
| 878 | fflush(stdout); |
| 879 | } |
| 880 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 881 | res = -ENOSYS; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 882 | if(f->op.read) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 883 | res = f->op.read(path, buf, arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 884 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 885 | } |
| 886 | |
| 887 | size = 0; |
| 888 | if(res > 0) { |
| 889 | size = res; |
| 890 | res = 0; |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 891 | if(f->flags & FUSE_DEBUG) { |
| 892 | printf(" READ %u bytes\n", size); |
| 893 | fflush(stdout); |
| 894 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 895 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 896 | memset(out, 0, sizeof(struct fuse_out_header)); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 897 | out->unique = in->unique; |
| 898 | out->error = res; |
| 899 | outsize = sizeof(struct fuse_out_header) + size; |
| 900 | |
| 901 | send_reply_raw(f, outbuf, outsize); |
| 902 | free(outbuf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 903 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 904 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 905 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 906 | struct fuse_write_in *arg) |
| 907 | { |
| 908 | int res; |
| 909 | char *path; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 910 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 911 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 912 | path = get_path(f, in->ino); |
| 913 | if(path != NULL) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 914 | if(f->flags & FUSE_DEBUG) { |
| 915 | printf("WRITE %u bytes to %llu\n", arg->size, arg->offset); |
| 916 | fflush(stdout); |
| 917 | } |
| 918 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 919 | res = -ENOSYS; |
| 920 | if(f->op.write) |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 921 | res = f->op.write(path, PARAM(arg), arg->size, arg->offset); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 922 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 923 | } |
| 924 | |
| 925 | if(res > 0) { |
| 926 | if((size_t) res != arg->size) { |
| 927 | fprintf(stderr, "short write: %u (should be %u)\n", res, |
| 928 | arg->size); |
Miklos Szeredi | 0e53508 | 2003-10-13 10:08:06 +0000 | [diff] [blame] | 929 | res = -EINVAL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 930 | } |
| 931 | else |
| 932 | res = 0; |
| 933 | } |
| 934 | |
| 935 | send_reply(f, in, res, NULL, 0); |
| 936 | } |
| 937 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 938 | static int default_statfs(struct statfs *buf) |
| 939 | { |
| 940 | buf->f_namelen = 255; |
| 941 | buf->f_bsize = 512; |
| 942 | return 0; |
| 943 | } |
| 944 | |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 945 | static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs) |
| 946 | { |
| 947 | kstatfs->bsize = statfs->f_bsize; |
| 948 | kstatfs->blocks = statfs->f_blocks; |
| 949 | kstatfs->bfree = statfs->f_bfree; |
| 950 | kstatfs->bavail = statfs->f_bavail; |
| 951 | kstatfs->files = statfs->f_files; |
| 952 | kstatfs->ffree = statfs->f_ffree; |
| 953 | kstatfs->namelen = statfs->f_namelen; |
| 954 | } |
| 955 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 956 | static void do_statfs(struct fuse *f, struct fuse_in_header *in) |
| 957 | { |
| 958 | int res; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 959 | struct fuse_statfs_out arg; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 960 | struct statfs buf; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 961 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 962 | memset(&buf, 0, sizeof(struct statfs)); |
| 963 | if(f->op.statfs) |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 964 | res = f->op.statfs("/", &buf); |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 965 | else |
| 966 | res = default_statfs(&buf); |
| 967 | |
| 968 | if(res == 0) |
| 969 | convert_statfs(&buf, &arg.st); |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 970 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 971 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 972 | } |
| 973 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 974 | static void do_fsync(struct fuse *f, struct fuse_in_header *in, |
| 975 | struct fuse_fsync_in *inarg) |
| 976 | { |
| 977 | int res; |
| 978 | char *path; |
| 979 | |
| 980 | res = -ENOENT; |
| 981 | path = get_path(f, in->ino); |
| 982 | if(path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 983 | res = -ENOSYS; |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 984 | if(f->op.fsync) |
| 985 | res = f->op.fsync(path, inarg->datasync); |
| 986 | free(path); |
| 987 | } |
| 988 | send_reply(f, in, res, NULL, 0); |
| 989 | } |
| 990 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 991 | static void do_setxattr(struct fuse *f, struct fuse_in_header *in, |
| 992 | struct fuse_setxattr_in *arg) |
| 993 | { |
| 994 | int res; |
| 995 | char *path; |
| 996 | char *name = PARAM(arg); |
| 997 | unsigned char *value = name + strlen(name) + 1; |
| 998 | |
| 999 | res = -ENOENT; |
| 1000 | path = get_path(f, in->ino); |
| 1001 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1002 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1003 | if (f->op.setxattr) |
| 1004 | res = f->op.setxattr(path, name, value, arg->size, arg->flags); |
| 1005 | free(path); |
| 1006 | } |
| 1007 | send_reply(f, in, res, NULL, 0); |
| 1008 | } |
| 1009 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1010 | static int common_getxattr(struct fuse *f, struct fuse_in_header *in, |
| 1011 | const char *name, char *value, size_t size) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1012 | { |
| 1013 | int res; |
| 1014 | char *path; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1015 | |
| 1016 | res = -ENOENT; |
| 1017 | path = get_path(f, in->ino); |
| 1018 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1019 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1020 | if (f->op.getxattr) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1021 | res = f->op.getxattr(path, name, value, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1022 | free(path); |
| 1023 | } |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1024 | return res; |
| 1025 | } |
| 1026 | |
| 1027 | static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in, |
| 1028 | const char *name, size_t size) |
| 1029 | { |
| 1030 | int res; |
| 1031 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size); |
| 1032 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 1033 | char *value = outbuf + sizeof(struct fuse_out_header); |
| 1034 | |
| 1035 | res = common_getxattr(f, in, name, value, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1036 | size = 0; |
| 1037 | if(res > 0) { |
| 1038 | size = res; |
| 1039 | res = 0; |
| 1040 | } |
| 1041 | memset(out, 0, sizeof(struct fuse_out_header)); |
| 1042 | out->unique = in->unique; |
| 1043 | out->error = res; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1044 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1045 | send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1046 | free(outbuf); |
| 1047 | } |
| 1048 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1049 | static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in, |
| 1050 | const char *name) |
| 1051 | { |
| 1052 | int res; |
| 1053 | struct fuse_getxattr_out arg; |
| 1054 | |
| 1055 | res = common_getxattr(f, in, name, NULL, 0); |
| 1056 | if(res >= 0) { |
| 1057 | arg.size = res; |
| 1058 | res = 0; |
| 1059 | } |
| 1060 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1061 | } |
| 1062 | |
| 1063 | static void do_getxattr(struct fuse *f, struct fuse_in_header *in, |
| 1064 | struct fuse_getxattr_in *arg) |
| 1065 | { |
| 1066 | char *name = PARAM(arg); |
| 1067 | |
| 1068 | if(arg->size) |
| 1069 | do_getxattr_read(f, in, name, arg->size); |
| 1070 | else |
| 1071 | do_getxattr_size(f, in, name); |
| 1072 | } |
| 1073 | |
| 1074 | static int common_listxattr(struct fuse *f, struct fuse_in_header *in, |
| 1075 | char *list, size_t size) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1076 | { |
| 1077 | int res; |
| 1078 | char *path; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1079 | |
| 1080 | res = -ENOENT; |
| 1081 | path = get_path(f, in->ino); |
| 1082 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1083 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1084 | if (f->op.listxattr) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1085 | res = f->op.listxattr(path, list, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1086 | free(path); |
| 1087 | } |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1088 | return res; |
| 1089 | } |
| 1090 | |
| 1091 | static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in, |
| 1092 | size_t size) |
| 1093 | { |
| 1094 | int res; |
| 1095 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size); |
| 1096 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 1097 | char *list = outbuf + sizeof(struct fuse_out_header); |
| 1098 | |
| 1099 | res = common_listxattr(f, in, list, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1100 | size = 0; |
| 1101 | if(res > 0) { |
| 1102 | size = res; |
| 1103 | res = 0; |
| 1104 | } |
| 1105 | memset(out, 0, sizeof(struct fuse_out_header)); |
| 1106 | out->unique = in->unique; |
| 1107 | out->error = res; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1108 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1109 | send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1110 | free(outbuf); |
| 1111 | } |
| 1112 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1113 | static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in) |
| 1114 | { |
| 1115 | int res; |
| 1116 | struct fuse_getxattr_out arg; |
| 1117 | |
| 1118 | res = common_listxattr(f, in, NULL, 0); |
| 1119 | if (res >= 0) { |
| 1120 | arg.size = res; |
| 1121 | res = 0; |
| 1122 | } |
| 1123 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1124 | } |
| 1125 | |
| 1126 | static void do_listxattr(struct fuse *f, struct fuse_in_header *in, |
| 1127 | struct fuse_getxattr_in *arg) |
| 1128 | { |
| 1129 | if(arg->size) |
| 1130 | do_listxattr_read(f, in, arg->size); |
| 1131 | else |
| 1132 | do_listxattr_size(f, in); |
| 1133 | } |
| 1134 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1135 | static void do_removexattr(struct fuse *f, struct fuse_in_header *in, |
| 1136 | char *name) |
| 1137 | { |
| 1138 | int res; |
| 1139 | char *path; |
| 1140 | |
| 1141 | res = -ENOENT; |
| 1142 | path = get_path(f, in->ino); |
| 1143 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1144 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1145 | if (f->op.removexattr) |
| 1146 | res = f->op.removexattr(path, name); |
| 1147 | free(path); |
| 1148 | } |
| 1149 | send_reply(f, in, res, NULL, 0); |
| 1150 | } |
| 1151 | |
| 1152 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1153 | static void free_cmd(struct fuse_cmd *cmd) |
| 1154 | { |
| 1155 | free(cmd->buf); |
| 1156 | free(cmd); |
| 1157 | } |
| 1158 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1159 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1160 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1161 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 1162 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 1163 | size_t argsize; |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 1164 | struct fuse_context *ctx = fuse_get_context(f); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1165 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1166 | dec_avail(f); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1167 | |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 1168 | if((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1169 | printf("unique: %i, opcode: %s (%i), ino: %li, insize: %i\n", |
| 1170 | in->unique, opname(in->opcode), in->opcode, in->ino, |
| 1171 | cmd->buflen); |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 1172 | fflush(stdout); |
| 1173 | } |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 1174 | |
| 1175 | ctx->uid = in->uid; |
| 1176 | ctx->gid = in->gid; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1177 | |
| 1178 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 1179 | |
| 1180 | switch(in->opcode) { |
| 1181 | case FUSE_LOOKUP: |
| 1182 | do_lookup(f, in, (char *) inarg); |
| 1183 | break; |
| 1184 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1185 | case FUSE_GETATTR: |
| 1186 | do_getattr(f, in); |
| 1187 | break; |
| 1188 | |
| 1189 | case FUSE_SETATTR: |
| 1190 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 1191 | break; |
| 1192 | |
| 1193 | case FUSE_READLINK: |
| 1194 | do_readlink(f, in); |
| 1195 | break; |
| 1196 | |
| 1197 | case FUSE_GETDIR: |
| 1198 | do_getdir(f, in); |
| 1199 | break; |
| 1200 | |
| 1201 | case FUSE_MKNOD: |
| 1202 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 1203 | break; |
| 1204 | |
| 1205 | case FUSE_MKDIR: |
| 1206 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 1207 | break; |
| 1208 | |
| 1209 | case FUSE_UNLINK: |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1210 | do_unlink(f, in, (char *) inarg); |
| 1211 | break; |
| 1212 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1213 | case FUSE_RMDIR: |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1214 | do_rmdir(f, in, (char *) inarg); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1215 | break; |
| 1216 | |
| 1217 | case FUSE_SYMLINK: |
| 1218 | do_symlink(f, in, (char *) inarg, |
| 1219 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 1220 | break; |
| 1221 | |
| 1222 | case FUSE_RENAME: |
| 1223 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 1224 | break; |
| 1225 | |
| 1226 | case FUSE_LINK: |
| 1227 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 1228 | break; |
| 1229 | |
| 1230 | case FUSE_OPEN: |
| 1231 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 1232 | break; |
| 1233 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1234 | case FUSE_FLUSH: |
| 1235 | do_flush(f, in); |
| 1236 | break; |
| 1237 | |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1238 | case FUSE_RELEASE: |
| 1239 | do_release(f, in, (struct fuse_open_in *) inarg); |
| 1240 | break; |
| 1241 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1242 | case FUSE_READ: |
| 1243 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 1244 | break; |
| 1245 | |
| 1246 | case FUSE_WRITE: |
| 1247 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 1248 | break; |
| 1249 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1250 | case FUSE_STATFS: |
| 1251 | do_statfs(f, in); |
| 1252 | break; |
| 1253 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1254 | case FUSE_FSYNC: |
| 1255 | do_fsync(f, in, (struct fuse_fsync_in *) inarg); |
| 1256 | break; |
| 1257 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1258 | case FUSE_SETXATTR: |
| 1259 | do_setxattr(f, in, (struct fuse_setxattr_in *) inarg); |
| 1260 | break; |
| 1261 | |
| 1262 | case FUSE_GETXATTR: |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1263 | do_getxattr(f, in, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1264 | break; |
| 1265 | |
| 1266 | case FUSE_LISTXATTR: |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1267 | do_listxattr(f, in, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1268 | break; |
| 1269 | |
| 1270 | case FUSE_REMOVEXATTR: |
| 1271 | do_removexattr(f, in, (char *) inarg); |
| 1272 | break; |
| 1273 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1274 | default: |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1275 | send_reply(f, in, -ENOSYS, NULL, 0); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1276 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1277 | |
| 1278 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1279 | } |
| 1280 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1281 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1282 | { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1283 | ssize_t res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1284 | struct fuse_cmd *cmd; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1285 | struct fuse_in_header *in; |
| 1286 | void *inarg; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1287 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1288 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
| 1289 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1290 | in = (struct fuse_in_header *) cmd->buf; |
| 1291 | inarg = cmd->buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1292 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1293 | do { |
| 1294 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 1295 | if(res == -1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1296 | free_cmd(cmd); |
Miklos Szeredi | 307242f | 2004-01-26 11:28:44 +0000 | [diff] [blame] | 1297 | if(f->exited || errno == EINTR) |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1298 | return NULL; |
| 1299 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1300 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 1301 | if(errno != ENODEV) { |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1302 | /* BAD... This will happen again */ |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1303 | perror("fuse: reading device"); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1304 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1305 | |
| 1306 | fuse_exit(f); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1307 | return NULL; |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 1308 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1309 | if((size_t) res < sizeof(struct fuse_in_header)) { |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1310 | free_cmd(cmd); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1311 | /* Cannot happen */ |
| 1312 | fprintf(stderr, "short read on fuse device\n"); |
| 1313 | fuse_exit(f); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1314 | return NULL; |
| 1315 | } |
| 1316 | cmd->buflen = res; |
| 1317 | |
| 1318 | /* Forget is special, it can be done without messing with threads. */ |
| 1319 | if(in->opcode == FUSE_FORGET) |
| 1320 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 1321 | |
| 1322 | } while(in->opcode == FUSE_FORGET); |
| 1323 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1324 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1325 | } |
| 1326 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1327 | void fuse_loop(struct fuse *f) |
| 1328 | { |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1329 | if(f == NULL) |
| 1330 | return; |
| 1331 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1332 | while(1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1333 | struct fuse_cmd *cmd; |
| 1334 | |
| 1335 | if(f->exited) |
| 1336 | return; |
| 1337 | |
| 1338 | cmd = __fuse_read_cmd(f); |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1339 | if(cmd == NULL) |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1340 | continue; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1341 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1342 | __fuse_process_cmd(f, cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1343 | } |
| 1344 | } |
| 1345 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1346 | void fuse_exit(struct fuse *f) |
| 1347 | { |
| 1348 | f->exited = 1; |
| 1349 | } |
| 1350 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1351 | struct fuse_context *fuse_get_context(struct fuse *f) |
| 1352 | { |
| 1353 | if(f->getcontext) |
| 1354 | return f->getcontext(f); |
| 1355 | else |
| 1356 | return &f->context; |
| 1357 | } |
| 1358 | |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1359 | static int check_version(struct fuse *f) |
| 1360 | { |
| 1361 | int res; |
| 1362 | FILE *vf = fopen(FUSE_VERSION_FILE, "r"); |
| 1363 | if(vf == NULL) { |
| 1364 | fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n", |
| 1365 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1366 | return -1; |
| 1367 | } |
| 1368 | res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver); |
| 1369 | fclose(vf); |
| 1370 | if(res != 2) { |
| 1371 | fprintf(stderr, "fuse: error reading %s\n", FUSE_VERSION_FILE); |
| 1372 | return -1; |
| 1373 | } |
| 1374 | if(f->majorver != FUSE_KERNEL_VERSION) { |
| 1375 | fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n", |
| 1376 | FUSE_KERNEL_VERSION); |
| 1377 | return -1; |
| 1378 | } |
| 1379 | if(f->minorver < FUSE_KERNEL_MINOR_VERSION) { |
| 1380 | fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i", |
| 1381 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1382 | return -1; |
| 1383 | } |
| 1384 | |
| 1385 | return 0; |
| 1386 | } |
| 1387 | |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1388 | 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] | 1389 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1390 | struct fuse *f; |
| 1391 | struct node *root; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1392 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1393 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1394 | |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1395 | if(check_version(f) == -1) { |
| 1396 | free(f); |
| 1397 | return NULL; |
| 1398 | } |
| 1399 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1400 | f->flags = flags; |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1401 | f->fd = fd; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1402 | f->ctr = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1403 | f->generation = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1404 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1405 | f->name_table_size = 14057; |
| 1406 | f->name_table = (struct node **) |
| 1407 | calloc(1, sizeof(struct node *) * f->name_table_size); |
| 1408 | f->ino_table_size = 14057; |
| 1409 | f->ino_table = (struct node **) |
| 1410 | calloc(1, sizeof(struct node *) * f->ino_table_size); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1411 | pthread_mutex_init(&f->lock, NULL); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1412 | f->numworker = 0; |
| 1413 | f->numavail = 0; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1414 | f->op = *op; |
| 1415 | f->getcontext = NULL; |
| 1416 | f->context.uid = 0; |
| 1417 | f->context.gid = 0; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1418 | f->exited = 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1419 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1420 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1421 | root->mode = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1422 | root->rdev = 0; |
| 1423 | root->name = strdup("/"); |
| 1424 | root->parent = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1425 | root->ino = FUSE_ROOT_INO; |
| 1426 | root->generation = 0; |
| 1427 | hash_ino(f, root); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1428 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1429 | return f; |
| 1430 | } |
| 1431 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1432 | void fuse_destroy(struct fuse *f) |
| 1433 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1434 | size_t i; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1435 | for(i = 0; i < f->ino_table_size; i++) { |
| 1436 | struct node *node; |
| 1437 | struct node *next; |
| 1438 | for(node = f->ino_table[i]; node != NULL; node = next) { |
| 1439 | next = node->ino_next; |
| 1440 | free_node(node); |
| 1441 | } |
| 1442 | } |
| 1443 | free(f->ino_table); |
| 1444 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1445 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1446 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1447 | } |