Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 149f607 | 2005-01-10 12:29:28 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 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 | |
Miklos Szeredi | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 9 | #include <config.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 10 | #include "fuse_i.h" |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 11 | #include "fuse_compat.h" |
Miklos Szeredi | b9b94cd | 2004-12-01 18:56:39 +0000 | [diff] [blame] | 12 | #include "fuse_kernel.h" |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 13 | #include "fuse_kernel_compat5.h" |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 14 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 15 | #include <stdio.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 16 | #include <string.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 17 | #include <stdlib.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 18 | #include <unistd.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 19 | #include <limits.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 20 | #include <errno.h> |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 21 | #include <assert.h> |
| 22 | #include <stdint.h> |
Miklos Szeredi | 019b4e9 | 2001-12-26 18:08:09 +0000 | [diff] [blame] | 23 | #include <sys/param.h> |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 24 | #include <sys/uio.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 25 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 26 | /* FUSE flags: */ |
| 27 | |
| 28 | /** Enable debuging output */ |
| 29 | #define FUSE_DEBUG (1 << 1) |
| 30 | |
| 31 | /** If a file is removed but it's still open, don't hide the file but |
| 32 | remove it immediately */ |
| 33 | #define FUSE_HARD_REMOVE (1 << 2) |
| 34 | |
| 35 | /** Use st_ino field in getattr instead of generating inode numbers */ |
| 36 | #define FUSE_USE_INO (1 << 3) |
| 37 | |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame^] | 38 | /** Only allow root or the owner to access the filesystem */ |
| 39 | #define FUSE_ALLOW_ROOT (1 << 4) |
| 40 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 41 | #define FUSE_KERNEL_MINOR_VERSION_NEED 1 |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 42 | #define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version" |
Miklos Szeredi | 162bcbb | 2004-11-29 23:43:44 +0000 | [diff] [blame] | 43 | #define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version" |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 44 | #define FUSE_DEV_OLD "/proc/fs/fuse/dev" |
| 45 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 46 | #define FUSE_MAX_PATH 4096 |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 47 | #define PARAM_T(inarg, type) (((char *)(inarg)) + sizeof(type)) |
| 48 | #define PARAM(inarg) PARAM_T(inarg, *(inarg)) |
| 49 | #define PARAM_COMPAT(f, inarg, type) \ |
| 50 | ((f)->major == 5 ? PARAM_T(inarg, struct type ## _compat5) : PARAM(inarg)) |
| 51 | |
| 52 | #define MEMBER_COMPAT(f, ptr, memb, type) \ |
| 53 | ((f)->major == 5 ? &((struct type ## _compat5 *) (ptr))->memb : &ptr->memb) |
| 54 | |
| 55 | #define SIZEOF_COMPAT(f, type) \ |
| 56 | ((f)->major == 5 ? sizeof(struct type ## _compat5) : sizeof(struct type)) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 57 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 58 | #define ENTRY_REVALIDATE_TIME 1 /* sec */ |
| 59 | #define ATTR_REVALIDATE_TIME 1 /* sec */ |
| 60 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 61 | |
| 62 | struct node { |
| 63 | struct node *name_next; |
| 64 | struct node *id_next; |
| 65 | nodeid_t nodeid; |
| 66 | unsigned int generation; |
| 67 | int refctr; |
| 68 | nodeid_t parent; |
| 69 | char *name; |
| 70 | uint64_t version; |
| 71 | int open_count; |
| 72 | int is_hidden; |
| 73 | }; |
| 74 | |
| 75 | struct fuse_dirhandle { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 76 | pthread_mutex_t lock; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 77 | struct fuse *fuse; |
Miklos Szeredi | b92d978 | 2005-02-07 16:10:49 +0000 | [diff] [blame] | 78 | unsigned char *contents; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 79 | int allocated; |
Miklos Szeredi | b92d978 | 2005-02-07 16:10:49 +0000 | [diff] [blame] | 80 | unsigned len; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 81 | unsigned needlen; |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 82 | int filled; |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 83 | unsigned long fh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 84 | int error; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
| 87 | struct fuse_cmd { |
| 88 | char *buf; |
| 89 | size_t buflen; |
| 90 | }; |
| 91 | |
| 92 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 93 | static struct fuse_context *(*fuse_getcontext)(void) = NULL; |
| 94 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 95 | #ifndef USE_UCLIBC |
| 96 | #define mutex_init(mut) pthread_mutex_init(mut, NULL) |
| 97 | #else |
| 98 | static void mutex_init(pthread_mutex_t mut) |
| 99 | { |
| 100 | pthread_mutexattr_t attr; |
| 101 | pthread_mutexattr_init(&attr); |
| 102 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 103 | pthread_mutex_init(mut, &attr); |
| 104 | pthread_mutexattr_destroy(&attr); |
| 105 | } |
| 106 | #endif |
| 107 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 108 | static const char *opname(enum fuse_opcode opcode) |
| 109 | { |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 110 | switch (opcode) { |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 111 | case FUSE_LOOKUP: return "LOOKUP"; |
| 112 | case FUSE_FORGET: return "FORGET"; |
| 113 | case FUSE_GETATTR: return "GETATTR"; |
| 114 | case FUSE_SETATTR: return "SETATTR"; |
| 115 | case FUSE_READLINK: return "READLINK"; |
| 116 | case FUSE_SYMLINK: return "SYMLINK"; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 117 | case FUSE_MKNOD: return "MKNOD"; |
| 118 | case FUSE_MKDIR: return "MKDIR"; |
| 119 | case FUSE_UNLINK: return "UNLINK"; |
| 120 | case FUSE_RMDIR: return "RMDIR"; |
| 121 | case FUSE_RENAME: return "RENAME"; |
| 122 | case FUSE_LINK: return "LINK"; |
| 123 | case FUSE_OPEN: return "OPEN"; |
| 124 | case FUSE_READ: return "READ"; |
| 125 | case FUSE_WRITE: return "WRITE"; |
| 126 | case FUSE_STATFS: return "STATFS"; |
Miklos Szeredi | 99f2074 | 2004-05-19 08:01:10 +0000 | [diff] [blame] | 127 | case FUSE_FLUSH: return "FLUSH"; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 128 | case FUSE_RELEASE: return "RELEASE"; |
| 129 | case FUSE_FSYNC: return "FSYNC"; |
| 130 | case FUSE_SETXATTR: return "SETXATTR"; |
| 131 | case FUSE_GETXATTR: return "GETXATTR"; |
| 132 | case FUSE_LISTXATTR: return "LISTXATTR"; |
| 133 | case FUSE_REMOVEXATTR: return "REMOVEXATTR"; |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 134 | case FUSE_INIT: return "INIT"; |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 135 | case FUSE_OPENDIR: return "OPENDIR"; |
| 136 | case FUSE_READDIR: return "READDIR"; |
| 137 | case FUSE_RELEASEDIR: return "RELEASEDIR"; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 138 | case FUSE_FSYNCDIR: return "FSYNCDIR"; |
Miklos Szeredi | 99f2074 | 2004-05-19 08:01:10 +0000 | [diff] [blame] | 139 | default: return "???"; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 140 | } |
| 141 | } |
| 142 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 143 | static inline void fuse_dec_avail(struct fuse *f) |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 144 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 145 | pthread_mutex_lock(&f->worker_lock); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 146 | f->numavail --; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 147 | pthread_mutex_unlock(&f->worker_lock); |
| 148 | } |
| 149 | |
| 150 | static inline void fuse_inc_avail(struct fuse *f) |
| 151 | { |
| 152 | pthread_mutex_lock(&f->worker_lock); |
| 153 | f->numavail ++; |
| 154 | pthread_mutex_unlock(&f->worker_lock); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 157 | static struct node *get_node_nocheck(struct fuse *f, nodeid_t nodeid) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 158 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 159 | size_t hash = nodeid % f->id_table_size; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 160 | struct node *node; |
| 161 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 162 | for (node = f->id_table[hash]; node != NULL; node = node->id_next) |
| 163 | if (node->nodeid == nodeid) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 164 | return node; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 165 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 166 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 169 | static struct node *get_node(struct fuse *f, nodeid_t nodeid) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 170 | { |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 171 | struct node *node = get_node_nocheck(f, nodeid); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 172 | if (!node) { |
| 173 | fprintf(stderr, "fuse internal error: node %lu not found\n", |
| 174 | nodeid); |
| 175 | abort(); |
| 176 | } |
| 177 | return node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 180 | static void free_node(struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 181 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 182 | free(node->name); |
| 183 | free(node); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 184 | } |
| 185 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 186 | static void unhash_id(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 187 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 188 | size_t hash = node->nodeid % f->id_table_size; |
| 189 | struct node **nodep = &f->id_table[hash]; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 190 | |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 191 | for (; *nodep != NULL; nodep = &(*nodep)->id_next) |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 192 | if (*nodep == node) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 193 | *nodep = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 194 | return; |
| 195 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 198 | static void hash_id(struct fuse *f, struct node *node) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 199 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 200 | size_t hash = node->nodeid % f->id_table_size; |
| 201 | node->id_next = f->id_table[hash]; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 202 | f->id_table[hash] = node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 203 | } |
| 204 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 205 | static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 206 | { |
| 207 | unsigned int hash = *name; |
| 208 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 209 | if (hash) |
| 210 | for (name += 1; *name != '\0'; name++) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 211 | hash = (hash << 5) - hash + *name; |
| 212 | |
| 213 | return (hash + parent) % f->name_table_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 214 | } |
| 215 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 216 | static void unref_node(struct fuse *f, struct node *node); |
| 217 | |
| 218 | static void unhash_name(struct fuse *f, struct node *node) |
| 219 | { |
| 220 | if (node->name) { |
| 221 | size_t hash = name_hash(f, node->parent, node->name); |
| 222 | struct node **nodep = &f->name_table[hash]; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 223 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 224 | for (; *nodep != NULL; nodep = &(*nodep)->name_next) |
| 225 | if (*nodep == node) { |
| 226 | *nodep = node->name_next; |
| 227 | node->name_next = NULL; |
| 228 | unref_node(f, get_node(f, node->parent)); |
| 229 | free(node->name); |
| 230 | node->name = NULL; |
| 231 | node->parent = 0; |
| 232 | return; |
| 233 | } |
| 234 | fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n", |
| 235 | node->nodeid); |
| 236 | abort(); |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static int hash_name(struct fuse *f, struct node *node, nodeid_t parent, |
| 241 | const char *name) |
| 242 | { |
| 243 | size_t hash = name_hash(f, parent, name); |
| 244 | node->name = strdup(name); |
| 245 | if (node->name == NULL) |
| 246 | return -1; |
| 247 | |
| 248 | get_node(f, parent)->refctr ++; |
| 249 | node->parent = parent; |
| 250 | node->name_next = f->name_table[hash]; |
| 251 | f->name_table[hash] = node; |
| 252 | return 0; |
| 253 | } |
| 254 | |
| 255 | static void delete_node(struct fuse *f, struct node *node) |
| 256 | { |
| 257 | assert(!node->name); |
| 258 | unhash_id(f, node); |
| 259 | free_node(node); |
| 260 | } |
| 261 | |
| 262 | static void unref_node(struct fuse *f, struct node *node) |
| 263 | { |
| 264 | assert(node->refctr > 0); |
| 265 | node->refctr --; |
| 266 | if (!node->refctr) |
| 267 | delete_node(f, node); |
| 268 | } |
| 269 | |
| 270 | static nodeid_t next_id(struct fuse *f) |
| 271 | { |
| 272 | do { |
| 273 | f->ctr++; |
| 274 | if (!f->ctr) |
| 275 | f->generation ++; |
| 276 | } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL); |
| 277 | return f->ctr; |
| 278 | } |
| 279 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 280 | static struct node *lookup_node(struct fuse *f, nodeid_t parent, |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 281 | const char *name) |
| 282 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 283 | size_t hash = name_hash(f, parent, name); |
| 284 | struct node *node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 285 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 286 | for (node = f->name_table[hash]; node != NULL; node = node->name_next) |
| 287 | if (node->parent == parent && strcmp(node->name, name) == 0) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 288 | return node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 289 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 290 | return NULL; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 291 | } |
| 292 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 293 | static struct node *find_node(struct fuse *f, nodeid_t parent, char *name, |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 294 | struct fuse_attr *attr, uint64_t version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 295 | { |
| 296 | struct node *node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 297 | int mode = attr->mode & S_IFMT; |
| 298 | int rdev = 0; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 299 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 300 | if (S_ISCHR(mode) || S_ISBLK(mode)) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 301 | rdev = attr->rdev; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 302 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 303 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 304 | node = lookup_node(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 305 | if (node != NULL) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 306 | if (!(f->flags & FUSE_USE_INO)) |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 307 | attr->ino = node->nodeid; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 308 | } else { |
| 309 | node = (struct node *) calloc(1, sizeof(struct node)); |
| 310 | if (node == NULL) |
| 311 | goto out_err; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 312 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 313 | node->refctr = 1; |
| 314 | node->nodeid = next_id(f); |
| 315 | if (!(f->flags & FUSE_USE_INO)) |
| 316 | attr->ino = node->nodeid; |
| 317 | node->open_count = 0; |
| 318 | node->is_hidden = 0; |
| 319 | node->generation = f->generation; |
| 320 | if (hash_name(f, node, parent, name) == -1) { |
| 321 | free(node); |
| 322 | node = NULL; |
| 323 | goto out_err; |
| 324 | } |
| 325 | hash_id(f, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 326 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 327 | node->version = version; |
Miklos Szeredi | c230991 | 2004-09-21 13:40:38 +0000 | [diff] [blame] | 328 | out_err: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 329 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 330 | return node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 331 | } |
| 332 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 333 | static char *add_name(char *buf, char *s, const char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 334 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 335 | size_t len = strlen(name); |
| 336 | s -= len; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 337 | if (s <= buf) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 338 | fprintf(stderr, "fuse: path too long: ...%s\n", s + len); |
| 339 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 340 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 341 | strncpy(s, name, len); |
| 342 | s--; |
| 343 | *s = '/'; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 344 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 345 | return s; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 348 | static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 349 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 350 | char buf[FUSE_MAX_PATH]; |
| 351 | char *s = buf + FUSE_MAX_PATH - 1; |
| 352 | struct node *node; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 353 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 354 | *s = '\0'; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 355 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 356 | if (name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 357 | s = add_name(buf, s, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 358 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 359 | return NULL; |
| 360 | } |
| 361 | |
| 362 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 363 | for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID; |
| 364 | node = get_node(f, node->parent)) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 365 | if (node->name == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 366 | s = NULL; |
| 367 | break; |
| 368 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 369 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 370 | s = add_name(buf, s, node->name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 371 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 372 | break; |
| 373 | } |
| 374 | pthread_mutex_unlock(&f->lock); |
| 375 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 376 | if (node == NULL || s == NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 377 | return NULL; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 378 | else if (*s == '\0') |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 379 | return strdup("/"); |
| 380 | else |
| 381 | return strdup(s); |
| 382 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 383 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 384 | static char *get_path(struct fuse *f, nodeid_t nodeid) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 385 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 386 | return get_path_name(f, nodeid, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 387 | } |
| 388 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 389 | static void forget_node(struct fuse *f, nodeid_t nodeid, uint64_t version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 390 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 391 | struct node *node; |
| 392 | |
| 393 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 394 | node = get_node(f, nodeid); |
| 395 | if (node->version == version && nodeid != FUSE_ROOT_ID) { |
| 396 | node->version = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 397 | unhash_name(f, node); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 398 | unref_node(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 399 | } |
| 400 | pthread_mutex_unlock(&f->lock); |
| 401 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 402 | } |
| 403 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 404 | static void remove_node(struct fuse *f, nodeid_t dir, const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 405 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 406 | struct node *node; |
| 407 | |
| 408 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 409 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 410 | if (node != NULL) |
| 411 | unhash_name(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 412 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 413 | } |
| 414 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 415 | static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname, |
| 416 | nodeid_t newdir, const char *newname, int hide) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 417 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 418 | struct node *node; |
| 419 | struct node *newnode; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 420 | int err = 0; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 421 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 422 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 423 | node = lookup_node(f, olddir, oldname); |
| 424 | newnode = lookup_node(f, newdir, newname); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 425 | if (node == NULL) |
| 426 | goto out; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 427 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 428 | if (newnode != NULL) { |
| 429 | if (hide) { |
| 430 | fprintf(stderr, "fuse: hidden file got created during hiding\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 431 | err = -EBUSY; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 432 | goto out; |
| 433 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 434 | unhash_name(f, newnode); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 435 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 436 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 437 | unhash_name(f, node); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 438 | if (hash_name(f, node, newdir, newname) == -1) { |
| 439 | err = -ENOMEM; |
| 440 | goto out; |
| 441 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 442 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 443 | if (hide) |
| 444 | node->is_hidden = 1; |
| 445 | |
| 446 | out: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 447 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 448 | return err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 449 | } |
| 450 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 451 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 452 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 453 | attr->ino = stbuf->st_ino; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 454 | attr->mode = stbuf->st_mode; |
| 455 | attr->nlink = stbuf->st_nlink; |
| 456 | attr->uid = stbuf->st_uid; |
| 457 | attr->gid = stbuf->st_gid; |
| 458 | attr->rdev = stbuf->st_rdev; |
| 459 | attr->size = stbuf->st_size; |
| 460 | attr->blocks = stbuf->st_blocks; |
| 461 | attr->atime = stbuf->st_atime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 462 | attr->mtime = stbuf->st_mtime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 463 | attr->ctime = stbuf->st_ctime; |
Miklos Szeredi | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 464 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
| 465 | attr->atimensec = stbuf->st_atim.tv_nsec; |
| 466 | attr->mtimensec = stbuf->st_mtim.tv_nsec; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 467 | attr->ctimensec = stbuf->st_ctim.tv_nsec; |
Miklos Szeredi | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 468 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 469 | } |
| 470 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 471 | static size_t iov_length(const struct iovec *iov, size_t count) |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 472 | { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 473 | size_t seg; |
| 474 | size_t ret = 0; |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 475 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 476 | for (seg = 0; seg < count; seg++) |
| 477 | ret += iov[seg].iov_len; |
| 478 | return ret; |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 479 | } |
| 480 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 481 | static int send_reply_raw(struct fuse *f, const struct iovec iov[], |
| 482 | size_t count) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 483 | { |
| 484 | int res; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 485 | unsigned outsize = iov_length(iov, count); |
| 486 | struct fuse_out_header *out = (struct fuse_out_header *) iov[0].iov_base; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 487 | out->len = outsize; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 488 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 489 | if ((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 490 | printf(" unique: %llu, error: %i (%s), outsize: %i\n", |
| 491 | out->unique, out->error, strerror(-out->error), outsize); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 492 | fflush(stdout); |
| 493 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 494 | |
Miklos Szeredi | 4e71c9f | 2004-02-09 12:05:14 +0000 | [diff] [blame] | 495 | /* This needs to be done before the reply, otherwise the scheduler |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 496 | could play tricks with us, and only let the counter be |
| 497 | increased long after the operation is done */ |
| 498 | fuse_inc_avail(f); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 499 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 500 | res = writev(f->fd, iov, count); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 501 | if (res == -1) { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 502 | /* ENOENT means the operation was interrupted */ |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 503 | if (!f->exited && errno != ENOENT) |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 504 | perror("fuse: writing device"); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 505 | return -errno; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 506 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 507 | return 0; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 508 | } |
| 509 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 510 | static int send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
| 511 | void *arg, size_t argsize) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 512 | { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 513 | struct fuse_out_header out; |
| 514 | struct iovec iov[2]; |
| 515 | size_t count; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 516 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 517 | if (error <= -1000 || error > 0) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 518 | fprintf(stderr, "fuse: bad error value: %i\n", error); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 519 | error = -ERANGE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 520 | } |
| 521 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 522 | out.unique = in->unique; |
| 523 | out.error = error; |
| 524 | count = 1; |
| 525 | iov[0].iov_base = &out; |
| 526 | iov[0].iov_len = sizeof(struct fuse_out_header); |
| 527 | if (argsize && !error) { |
| 528 | count++; |
| 529 | iov[1].iov_base = arg; |
| 530 | iov[1].iov_len = argsize; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 531 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 532 | return send_reply_raw(f, iov, count); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 533 | } |
| 534 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 535 | static int is_open(struct fuse *f, nodeid_t dir, const char *name) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 536 | { |
| 537 | struct node *node; |
| 538 | int isopen = 0; |
| 539 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 540 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 541 | if (node && node->open_count > 0) |
| 542 | isopen = 1; |
| 543 | pthread_mutex_unlock(&f->lock); |
| 544 | return isopen; |
| 545 | } |
| 546 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 547 | static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname, |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 548 | char *newname, size_t bufsize) |
| 549 | { |
| 550 | struct stat buf; |
| 551 | struct node *node; |
| 552 | struct node *newnode; |
| 553 | char *newpath; |
| 554 | int res; |
| 555 | int failctr = 10; |
| 556 | |
| 557 | if (!f->op.getattr) |
| 558 | return NULL; |
| 559 | |
| 560 | do { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 561 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 562 | node = lookup_node(f, dir, oldname); |
| 563 | if (node == NULL) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 564 | pthread_mutex_unlock(&f->lock); |
| 565 | return NULL; |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 566 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 567 | do { |
| 568 | f->hidectr ++; |
| 569 | snprintf(newname, bufsize, ".fuse_hidden%08x%08x", |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 570 | (unsigned int) node->nodeid, f->hidectr); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 571 | newnode = lookup_node(f, dir, newname); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 572 | } while(newnode); |
| 573 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 574 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 575 | newpath = get_path_name(f, dir, newname); |
| 576 | if (!newpath) |
| 577 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 578 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 579 | res = f->op.getattr(newpath, &buf); |
| 580 | if (res != 0) |
| 581 | break; |
| 582 | free(newpath); |
| 583 | newpath = NULL; |
| 584 | } while(--failctr); |
| 585 | |
| 586 | return newpath; |
| 587 | } |
| 588 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 589 | static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir, |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 590 | const char *oldname) |
| 591 | { |
| 592 | char newname[64]; |
| 593 | char *newpath; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 594 | int err = -EBUSY; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 595 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 596 | if (f->op.rename && f->op.unlink) { |
| 597 | newpath = hidden_name(f, dir, oldname, newname, sizeof(newname)); |
| 598 | if (newpath) { |
| 599 | int res = f->op.rename(oldpath, newpath); |
| 600 | if (res == 0) |
| 601 | err = rename_node(f, dir, oldname, dir, newname, 1); |
| 602 | free(newpath); |
| 603 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 604 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 605 | return err; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 606 | } |
| 607 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 608 | static int lookup_path(struct fuse *f, nodeid_t nodeid, uint64_t version, |
| 609 | char *name, const char *path, |
| 610 | struct fuse_entry_out *arg) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 611 | { |
| 612 | int res; |
| 613 | struct stat buf; |
| 614 | |
| 615 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 616 | if (res == 0) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 617 | struct node *node; |
| 618 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 619 | memset(arg, 0, sizeof(struct fuse_entry_out)); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 620 | convert_stat(&buf, &arg->attr); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 621 | node = find_node(f, nodeid, name, &arg->attr, version); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 622 | if (node == NULL) |
| 623 | res = -ENOMEM; |
| 624 | else { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 625 | arg->nodeid = node->nodeid; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 626 | arg->generation = node->generation; |
| 627 | arg->entry_valid = ENTRY_REVALIDATE_TIME; |
| 628 | arg->entry_valid_nsec = 0; |
| 629 | arg->attr_valid = ATTR_REVALIDATE_TIME; |
| 630 | arg->attr_valid_nsec = 0; |
| 631 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 632 | printf(" NODEID: %lu\n", (unsigned long) arg->nodeid); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 633 | fflush(stdout); |
| 634 | } |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 635 | } |
| 636 | } |
| 637 | return res; |
| 638 | } |
| 639 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 640 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 641 | { |
| 642 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 643 | int res2; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 644 | char *path; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 645 | struct fuse_entry_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 646 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 647 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 648 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 649 | if (path != NULL) { |
| 650 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 651 | printf("LOOKUP %s\n", path); |
| 652 | fflush(stdout); |
| 653 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 654 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 655 | if (f->op.getattr) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 656 | res = lookup_path(f, in->nodeid, in->unique, name, path, &arg); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 657 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 658 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 659 | res2 = send_reply(f, in, res, &arg, sizeof(arg)); |
| 660 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 661 | forget_node(f, arg.nodeid, in->unique); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 662 | } |
| 663 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 664 | static void do_forget(struct fuse *f, struct fuse_in_header *in, |
| 665 | struct fuse_forget_in *arg) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 666 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 667 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 668 | printf("FORGET %lu/%llu\n", (unsigned long) in->nodeid, |
| 669 | arg->version); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 670 | fflush(stdout); |
| 671 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 672 | forget_node(f, in->nodeid, arg->version); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 676 | { |
| 677 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 678 | char *path; |
| 679 | struct stat buf; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 680 | struct fuse_attr_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 681 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 682 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 683 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 684 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 685 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 686 | if (f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 687 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 688 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 689 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 690 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 691 | if (res == 0) { |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 692 | memset(&arg, 0, sizeof(struct fuse_attr_out)); |
| 693 | arg.attr_valid = ATTR_REVALIDATE_TIME; |
| 694 | arg.attr_valid_nsec = 0; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 695 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 696 | if (!(f->flags & FUSE_USE_INO)) |
| 697 | arg.attr.ino = in->nodeid; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 698 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 699 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 700 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 701 | } |
| 702 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 703 | 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] | 704 | { |
| 705 | int res; |
| 706 | |
| 707 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 708 | if (f->op.chmod) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 709 | res = f->op.chmod(path, attr->mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 710 | |
| 711 | return res; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 712 | } |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 713 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 714 | 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] | 715 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 716 | { |
| 717 | int res; |
| 718 | uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1; |
| 719 | gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 720 | |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 721 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 722 | if (f->op.chown) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 723 | res = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 724 | |
| 725 | return res; |
| 726 | } |
| 727 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 728 | static int do_truncate(struct fuse *f, const char *path, |
| 729 | struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 730 | { |
| 731 | int res; |
| 732 | |
| 733 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 734 | if (f->op.truncate) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 735 | res = f->op.truncate(path, attr->size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 736 | |
| 737 | return res; |
| 738 | } |
| 739 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 740 | 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] | 741 | { |
| 742 | int res; |
| 743 | struct utimbuf buf; |
| 744 | buf.actime = attr->atime; |
| 745 | buf.modtime = attr->mtime; |
| 746 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 747 | if (f->op.utime) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 748 | res = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 749 | |
| 750 | return res; |
| 751 | } |
| 752 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 753 | static void do_setattr(struct fuse *f, struct fuse_in_header *in, |
| 754 | struct fuse_setattr_in *arg) |
| 755 | { |
| 756 | int res; |
| 757 | char *path; |
| 758 | int valid = arg->valid; |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 759 | struct fuse_attr *attr = MEMBER_COMPAT(f, arg, attr, fuse_setattr_in); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 760 | struct fuse_attr_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 761 | |
| 762 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 763 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 764 | if (path != NULL) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 765 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 766 | if (f->op.getattr) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 767 | res = 0; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 768 | if (!res && (valid & FATTR_MODE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 769 | res = do_chmod(f, path, attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 770 | if (!res && (valid & (FATTR_UID | FATTR_GID))) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 771 | res = do_chown(f, path, attr, valid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 772 | if (!res && (valid & FATTR_SIZE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 773 | res = do_truncate(f, path, attr); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 774 | if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) == |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 775 | (FATTR_ATIME | FATTR_MTIME)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 776 | res = do_utime(f, path, attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 777 | if (!res) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 778 | struct stat buf; |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 779 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 780 | if (!res) { |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 781 | memset(&outarg, 0, sizeof(struct fuse_attr_out)); |
| 782 | outarg.attr_valid = ATTR_REVALIDATE_TIME; |
| 783 | outarg.attr_valid_nsec = 0; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 784 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 785 | if (!(f->flags & FUSE_USE_INO)) |
| 786 | outarg.attr.ino = in->nodeid; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 787 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 788 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 789 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 790 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 791 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 792 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 795 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 796 | { |
| 797 | int res; |
| 798 | char link[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 799 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 800 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 801 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 802 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 803 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 804 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 805 | if (f->op.readlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 806 | res = f->op.readlink(path, link, sizeof(link)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 807 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 808 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 809 | link[PATH_MAX] = '\0'; |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 810 | send_reply(f, in, res, link, res == 0 ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 811 | } |
| 812 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 813 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 814 | struct fuse_mknod_in *inarg) |
| 815 | { |
| 816 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 817 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 818 | char *path; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 819 | char *name = PARAM(inarg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 820 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 821 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 822 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 823 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 824 | if (path != NULL) { |
| 825 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 826 | printf("MKNOD %s\n", path); |
| 827 | fflush(stdout); |
| 828 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 829 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 830 | if (f->op.mknod && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 831 | res = f->op.mknod(path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 832 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 833 | res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 834 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 835 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 836 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 837 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 838 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 839 | forget_node(f, outarg.nodeid, in->unique); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 840 | } |
| 841 | |
| 842 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 843 | struct fuse_mkdir_in *inarg) |
| 844 | { |
| 845 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 846 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 847 | char *path; |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 848 | char *name = PARAM_COMPAT(f, inarg, fuse_mkdir_in); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 849 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 850 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 851 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 852 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 853 | if (path != NULL) { |
| 854 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 855 | printf("MKDIR %s\n", path); |
| 856 | fflush(stdout); |
| 857 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 858 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 859 | if (f->op.mkdir && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 860 | res = f->op.mkdir(path, inarg->mode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 861 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 862 | res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 863 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 864 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 865 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 866 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 867 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 868 | forget_node(f, outarg.nodeid, in->unique); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 869 | } |
| 870 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 871 | 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] | 872 | { |
| 873 | int res; |
| 874 | char *path; |
| 875 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 876 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 877 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 878 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 879 | if (f->flags & FUSE_DEBUG) { |
| 880 | printf("UNLINK %s\n", path); |
| 881 | fflush(stdout); |
| 882 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 883 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 884 | if (f->op.unlink) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 885 | if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name)) |
| 886 | res = hide_node(f, path, in->nodeid, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 887 | else { |
| 888 | res = f->op.unlink(path); |
| 889 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 890 | remove_node(f, in->nodeid, name); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 891 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 892 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 893 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 894 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 895 | } |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 896 | send_reply(f, in, res, NULL, 0); |
| 897 | } |
| 898 | |
| 899 | static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name) |
| 900 | { |
| 901 | int res; |
| 902 | char *path; |
| 903 | |
| 904 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 905 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 906 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 907 | if (f->flags & FUSE_DEBUG) { |
| 908 | printf("RMDIR %s\n", path); |
| 909 | fflush(stdout); |
| 910 | } |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 911 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 912 | if (f->op.rmdir) { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 913 | res = f->op.rmdir(path); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 914 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 915 | remove_node(f, in->nodeid, name); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 916 | } |
| 917 | free(path); |
| 918 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 919 | send_reply(f, in, res, NULL, 0); |
| 920 | } |
| 921 | |
| 922 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 923 | char *link) |
| 924 | { |
| 925 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 926 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 927 | char *path; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 928 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 929 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 930 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 931 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 932 | if (path != NULL) { |
| 933 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 934 | printf("SYMLINK %s\n", path); |
| 935 | fflush(stdout); |
| 936 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 937 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 938 | if (f->op.symlink && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 939 | res = f->op.symlink(link, path); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 940 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 941 | res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 942 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 943 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 944 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 945 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 946 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 947 | forget_node(f, outarg.nodeid, in->unique); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 948 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 949 | } |
| 950 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 951 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 952 | struct fuse_rename_in *inarg) |
| 953 | { |
| 954 | int res; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 955 | nodeid_t olddir = in->nodeid; |
| 956 | nodeid_t newdir = inarg->newdir; |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 957 | char *oldname = PARAM(inarg); |
| 958 | char *newname = oldname + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 959 | char *oldpath; |
| 960 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 961 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 962 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 963 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 964 | if (oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 965 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 966 | if (newpath != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 967 | if (f->flags & FUSE_DEBUG) { |
| 968 | printf("RENAME %s -> %s\n", oldpath, newpath); |
| 969 | fflush(stdout); |
| 970 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 971 | res = -ENOSYS; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 972 | if (f->op.rename) { |
| 973 | res = 0; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 974 | if (!(f->flags & FUSE_HARD_REMOVE) && |
Miklos Szeredi | 2529ca2 | 2004-07-13 15:36:52 +0000 | [diff] [blame] | 975 | is_open(f, newdir, newname)) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 976 | res = hide_node(f, newpath, newdir, newname); |
| 977 | if (res == 0) { |
| 978 | res = f->op.rename(oldpath, newpath); |
| 979 | if (res == 0) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 980 | res = rename_node(f, olddir, oldname, newdir, newname, 0); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 981 | } |
| 982 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 983 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 984 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 985 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 986 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 987 | send_reply(f, in, res, NULL, 0); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 988 | } |
| 989 | |
| 990 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 991 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 992 | { |
| 993 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 994 | int res2; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 995 | char *oldpath; |
| 996 | char *newpath; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 997 | char *name = PARAM(arg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 998 | struct fuse_entry_out outarg; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 999 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1000 | res = -ENOENT; |
Miklos Szeredi | ed6b5dd | 2005-01-26 17:07:59 +0000 | [diff] [blame] | 1001 | oldpath = get_path(f, arg->oldnodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1002 | if (oldpath != NULL) { |
Miklos Szeredi | ed6b5dd | 2005-01-26 17:07:59 +0000 | [diff] [blame] | 1003 | newpath = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1004 | if (newpath != NULL) { |
| 1005 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1006 | printf("LINK %s\n", newpath); |
| 1007 | fflush(stdout); |
| 1008 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1009 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1010 | if (f->op.link && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 1011 | res = f->op.link(oldpath, newpath); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1012 | if (res == 0) |
Miklos Szeredi | ed6b5dd | 2005-01-26 17:07:59 +0000 | [diff] [blame] | 1013 | res = lookup_path(f, in->nodeid, in->unique, name, |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1014 | newpath, &outarg); |
| 1015 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1016 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1017 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1018 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1019 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 1020 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 1021 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1022 | forget_node(f, outarg.nodeid, in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1023 | } |
| 1024 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1025 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 1026 | struct fuse_open_in *arg) |
| 1027 | { |
| 1028 | int res; |
| 1029 | char *path; |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1030 | struct fuse_open_out outarg; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1031 | struct fuse_file_info fi; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1032 | |
Miklos Szeredi | ed3c97c | 2005-02-15 17:04:50 +0000 | [diff] [blame] | 1033 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1034 | memset(&fi, 0, sizeof(fi)); |
| 1035 | fi.flags = arg->flags; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1036 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1037 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1038 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1039 | res = -ENOSYS; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1040 | if (f->op.open) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1041 | if (!f->compat) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1042 | res = f->op.open(path, &fi); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1043 | else |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1044 | res = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi.flags); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1045 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 1046 | } |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1047 | if (res == 0) { |
| 1048 | int res2; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1049 | outarg.fh = fi.fh; |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1050 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1051 | printf("OPEN[%lu] flags: 0x%x\n", fi.fh, arg->flags); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1052 | fflush(stdout); |
| 1053 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1054 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1055 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1056 | res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out)); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1057 | if(res2 == -ENOENT) { |
| 1058 | /* The open syscall was interrupted, so it must be cancelled */ |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1059 | if(f->op.release) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1060 | if (!f->compat) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1061 | f->op.release(path, &fi); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1062 | else |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1063 | ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1064 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1065 | } else { |
| 1066 | struct node *node = get_node(f, in->nodeid); |
| 1067 | node->open_count ++; |
| 1068 | } |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1069 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1070 | } else |
| 1071 | send_reply(f, in, res, NULL, 0); |
| 1072 | |
Miklos Szeredi | 9a31cca | 2004-06-26 21:11:25 +0000 | [diff] [blame] | 1073 | if (path) |
| 1074 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1075 | } |
| 1076 | |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1077 | static void do_flush(struct fuse *f, struct fuse_in_header *in, |
| 1078 | struct fuse_flush_in *arg) |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1079 | { |
| 1080 | char *path; |
| 1081 | int res; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1082 | struct fuse_file_info fi; |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1083 | |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1084 | memset(&fi, 0, sizeof(fi)); |
| 1085 | fi.fh = arg->fh; |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1086 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1087 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1088 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1089 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1090 | printf("FLUSH[%lu]\n", (unsigned long) arg->fh); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1091 | fflush(stdout); |
| 1092 | } |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1093 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1094 | if (f->op.flush) |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1095 | res = f->op.flush(path, &fi); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1096 | free(path); |
| 1097 | } |
| 1098 | send_reply(f, in, res, NULL, 0); |
| 1099 | } |
| 1100 | |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1101 | static void do_release(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1102 | struct fuse_release_in *arg) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1103 | { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1104 | struct node *node; |
| 1105 | char *path; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1106 | struct fuse_file_info fi; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1107 | int unlink_hidden; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1108 | |
| 1109 | memset(&fi, 0, sizeof(fi)); |
| 1110 | fi.flags = arg->flags; |
| 1111 | fi.fh = arg->fh; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1112 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1113 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1114 | node = get_node(f, in->nodeid); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1115 | assert(node->open_count > 0); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1116 | --node->open_count; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1117 | unlink_hidden = (node->is_hidden && !node->open_count); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1118 | pthread_mutex_unlock(&f->lock); |
| 1119 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1120 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1121 | if (f->flags & FUSE_DEBUG) { |
| 1122 | printf("RELEASE[%lu] flags: 0x%x\n", fi.fh, fi.flags); |
| 1123 | fflush(stdout); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1124 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1125 | if (f->op.release) { |
| 1126 | if (!f->compat) |
| 1127 | f->op.release(path ? path : "-", &fi); |
| 1128 | else if (path) |
| 1129 | ((struct fuse_operations_compat2 *) &f->op)->release(path, fi.flags); |
| 1130 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1131 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1132 | if(unlink_hidden && path) |
| 1133 | f->op.unlink(path); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1134 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1135 | if (path) |
| 1136 | free(path); |
| 1137 | |
Miklos Szeredi | 556d03d | 2004-06-30 11:13:41 +0000 | [diff] [blame] | 1138 | send_reply(f, in, 0, NULL, 0); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1139 | } |
| 1140 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1141 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 1142 | struct fuse_read_in *arg) |
| 1143 | { |
| 1144 | int res; |
| 1145 | char *path; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1146 | size_t size; |
| 1147 | char *buf; |
| 1148 | struct fuse_file_info fi; |
| 1149 | |
| 1150 | buf = (char *) malloc(arg->size); |
| 1151 | if (buf == NULL) { |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1152 | send_reply(f, in, -ENOMEM, NULL, 0); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1153 | return; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1154 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1155 | |
| 1156 | memset(&fi, 0, sizeof(fi)); |
| 1157 | fi.fh = arg->fh; |
| 1158 | |
| 1159 | res = -ENOENT; |
| 1160 | path = get_path(f, in->nodeid); |
| 1161 | if (path != NULL) { |
| 1162 | if (f->flags & FUSE_DEBUG) { |
| 1163 | printf("READ[%lu] %u bytes from %llu\n", |
| 1164 | (unsigned long) arg->fh, arg->size, arg->offset); |
| 1165 | fflush(stdout); |
| 1166 | } |
| 1167 | |
| 1168 | res = -ENOSYS; |
| 1169 | if (f->op.read) |
| 1170 | res = f->op.read(path, buf, arg->size, arg->offset, &fi); |
| 1171 | free(path); |
| 1172 | } |
| 1173 | |
| 1174 | size = 0; |
| 1175 | if (res >= 0) { |
| 1176 | size = res; |
| 1177 | res = 0; |
| 1178 | if (f->flags & FUSE_DEBUG) { |
| 1179 | printf(" READ[%lu] %u bytes\n", (unsigned long) arg->fh, |
| 1180 | size); |
| 1181 | fflush(stdout); |
| 1182 | } |
| 1183 | } |
| 1184 | |
| 1185 | send_reply(f, in, res, buf, size); |
| 1186 | free(buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1187 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1188 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1189 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 1190 | struct fuse_write_in *arg) |
| 1191 | { |
| 1192 | int res; |
| 1193 | char *path; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 1194 | struct fuse_write_out outarg; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1195 | struct fuse_file_info fi; |
| 1196 | |
| 1197 | memset(&fi, 0, sizeof(fi)); |
| 1198 | fi.fh = arg->fh; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1199 | fi.writepage = arg->write_flags & 1; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1200 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1201 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1202 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1203 | if (path != NULL) { |
| 1204 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 1eea032 | 2004-09-27 18:50:11 +0000 | [diff] [blame] | 1205 | printf("WRITE%s[%lu] %u bytes to %llu\n", |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1206 | (arg->write_flags & 1) ? "PAGE" : "", |
| 1207 | (unsigned long) arg->fh, arg->size, arg->offset); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1208 | fflush(stdout); |
| 1209 | } |
| 1210 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1211 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1212 | if (f->op.write) |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1213 | res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1214 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1215 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1216 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1217 | memset(&outarg, 0, sizeof(outarg)); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1218 | if (res >= 0) { |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 1219 | outarg.size = res; |
| 1220 | res = 0; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1221 | } |
| 1222 | |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1223 | send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_write_out)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1224 | } |
| 1225 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1226 | static int default_statfs(struct statfs *buf) |
| 1227 | { |
| 1228 | buf->f_namelen = 255; |
| 1229 | buf->f_bsize = 512; |
| 1230 | return 0; |
| 1231 | } |
| 1232 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1233 | static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf, |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1234 | struct statfs *statfs) |
| 1235 | { |
| 1236 | statfs->f_bsize = compatbuf->block_size; |
| 1237 | statfs->f_blocks = compatbuf->blocks; |
| 1238 | statfs->f_bfree = compatbuf->blocks_free; |
| 1239 | statfs->f_bavail = compatbuf->blocks_free; |
| 1240 | statfs->f_files = compatbuf->files; |
| 1241 | statfs->f_ffree = compatbuf->files_free; |
| 1242 | statfs->f_namelen = compatbuf->namelen; |
| 1243 | } |
| 1244 | |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1245 | static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs) |
| 1246 | { |
| 1247 | kstatfs->bsize = statfs->f_bsize; |
| 1248 | kstatfs->blocks = statfs->f_blocks; |
| 1249 | kstatfs->bfree = statfs->f_bfree; |
| 1250 | kstatfs->bavail = statfs->f_bavail; |
| 1251 | kstatfs->files = statfs->f_files; |
| 1252 | kstatfs->ffree = statfs->f_ffree; |
| 1253 | kstatfs->namelen = statfs->f_namelen; |
| 1254 | } |
| 1255 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1256 | static void do_statfs(struct fuse *f, struct fuse_in_header *in) |
| 1257 | { |
| 1258 | int res; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1259 | struct fuse_statfs_out arg; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1260 | struct statfs buf; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1261 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1262 | memset(&buf, 0, sizeof(struct statfs)); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1263 | if (f->op.statfs) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1264 | if (!f->compat || f->compat > 11) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1265 | res = f->op.statfs("/", &buf); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1266 | else { |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1267 | struct fuse_statfs_compat1 compatbuf; |
| 1268 | memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1)); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1269 | res = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1270 | if (res == 0) |
| 1271 | convert_statfs_compat(&compatbuf, &buf); |
| 1272 | } |
| 1273 | } |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1274 | else |
| 1275 | res = default_statfs(&buf); |
| 1276 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1277 | if (res == 0) |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1278 | convert_statfs(&buf, &arg.st); |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 1279 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1280 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1281 | } |
| 1282 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1283 | static void do_fsync(struct fuse *f, struct fuse_in_header *in, |
| 1284 | struct fuse_fsync_in *inarg) |
| 1285 | { |
| 1286 | int res; |
| 1287 | char *path; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1288 | struct fuse_file_info fi; |
| 1289 | |
| 1290 | memset(&fi, 0, sizeof(fi)); |
| 1291 | fi.fh = inarg->fh; |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1292 | |
| 1293 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1294 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1295 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1296 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1297 | printf("FSYNC[%lu]\n", (unsigned long) inarg->fh); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1298 | fflush(stdout); |
| 1299 | } |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1300 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1301 | if (f->op.fsync) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1302 | res = f->op.fsync(path, inarg->fsync_flags & 1, &fi); |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1303 | free(path); |
| 1304 | } |
| 1305 | send_reply(f, in, res, NULL, 0); |
| 1306 | } |
| 1307 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1308 | static void do_setxattr(struct fuse *f, struct fuse_in_header *in, |
| 1309 | struct fuse_setxattr_in *arg) |
| 1310 | { |
| 1311 | int res; |
| 1312 | char *path; |
| 1313 | char *name = PARAM(arg); |
| 1314 | unsigned char *value = name + strlen(name) + 1; |
| 1315 | |
| 1316 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1317 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1318 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1319 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1320 | if (f->op.setxattr) |
| 1321 | res = f->op.setxattr(path, name, value, arg->size, arg->flags); |
| 1322 | free(path); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1323 | } |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1324 | send_reply(f, in, res, NULL, 0); |
| 1325 | } |
| 1326 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1327 | static int common_getxattr(struct fuse *f, struct fuse_in_header *in, |
| 1328 | const char *name, char *value, size_t size) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1329 | { |
| 1330 | int res; |
| 1331 | char *path; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1332 | |
| 1333 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1334 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1335 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1336 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1337 | if (f->op.getxattr) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1338 | res = f->op.getxattr(path, name, value, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1339 | free(path); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1340 | } |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1341 | return res; |
| 1342 | } |
| 1343 | |
| 1344 | static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in, |
| 1345 | const char *name, size_t size) |
| 1346 | { |
| 1347 | int res; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1348 | char *value = (char *) malloc(size); |
| 1349 | if (value == NULL) { |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1350 | send_reply(f, in, -ENOMEM, NULL, 0); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1351 | return; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1352 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1353 | res = common_getxattr(f, in, name, value, size); |
| 1354 | size = 0; |
| 1355 | if (res > 0) { |
| 1356 | size = res; |
| 1357 | res = 0; |
| 1358 | } |
| 1359 | send_reply(f, in, res, value, size); |
| 1360 | free(value); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1361 | } |
| 1362 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1363 | static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in, |
| 1364 | const char *name) |
| 1365 | { |
| 1366 | int res; |
| 1367 | struct fuse_getxattr_out arg; |
| 1368 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1369 | memset(&arg, 0, sizeof(arg)); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1370 | res = common_getxattr(f, in, name, NULL, 0); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1371 | if (res >= 0) { |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1372 | arg.size = res; |
| 1373 | res = 0; |
| 1374 | } |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1375 | send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out)); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1376 | } |
| 1377 | |
| 1378 | static void do_getxattr(struct fuse *f, struct fuse_in_header *in, |
| 1379 | struct fuse_getxattr_in *arg) |
| 1380 | { |
| 1381 | char *name = PARAM(arg); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1382 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1383 | if (arg->size) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1384 | do_getxattr_read(f, in, name, arg->size); |
| 1385 | else |
| 1386 | do_getxattr_size(f, in, name); |
| 1387 | } |
| 1388 | |
| 1389 | static int common_listxattr(struct fuse *f, struct fuse_in_header *in, |
| 1390 | char *list, size_t size) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1391 | { |
| 1392 | int res; |
| 1393 | char *path; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1394 | |
| 1395 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1396 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1397 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1398 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1399 | if (f->op.listxattr) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1400 | res = f->op.listxattr(path, list, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1401 | free(path); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1402 | } |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1403 | return res; |
| 1404 | } |
| 1405 | |
| 1406 | static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in, |
| 1407 | size_t size) |
| 1408 | { |
| 1409 | int res; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1410 | char *list = (char *) malloc(size); |
| 1411 | if (list == NULL) { |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1412 | send_reply(f, in, -ENOMEM, NULL, 0); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1413 | return; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1414 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1415 | res = common_listxattr(f, in, list, size); |
| 1416 | size = 0; |
| 1417 | if (res > 0) { |
| 1418 | size = res; |
| 1419 | res = 0; |
| 1420 | } |
| 1421 | send_reply(f, in, res, list, size); |
| 1422 | free(list); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1423 | } |
| 1424 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1425 | static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in) |
| 1426 | { |
| 1427 | int res; |
| 1428 | struct fuse_getxattr_out arg; |
| 1429 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1430 | memset(&arg, 0, sizeof(arg)); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1431 | res = common_listxattr(f, in, NULL, 0); |
| 1432 | if (res >= 0) { |
| 1433 | arg.size = res; |
| 1434 | res = 0; |
| 1435 | } |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1436 | send_reply(f, in, res, &arg, SIZEOF_COMPAT(f, fuse_getxattr_out)); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1437 | } |
| 1438 | |
| 1439 | static void do_listxattr(struct fuse *f, struct fuse_in_header *in, |
| 1440 | struct fuse_getxattr_in *arg) |
| 1441 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1442 | if (arg->size) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1443 | do_listxattr_read(f, in, arg->size); |
| 1444 | else |
| 1445 | do_listxattr_size(f, in); |
| 1446 | } |
| 1447 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1448 | static void do_removexattr(struct fuse *f, struct fuse_in_header *in, |
| 1449 | char *name) |
| 1450 | { |
| 1451 | int res; |
| 1452 | char *path; |
| 1453 | |
| 1454 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1455 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1456 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1457 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1458 | if (f->op.removexattr) |
| 1459 | res = f->op.removexattr(path, name); |
| 1460 | free(path); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1461 | } |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1462 | send_reply(f, in, res, NULL, 0); |
| 1463 | } |
| 1464 | |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 1465 | static void do_init(struct fuse *f, struct fuse_in_header *in, |
| 1466 | struct fuse_init_in_out *arg) |
| 1467 | { |
| 1468 | struct fuse_init_in_out outarg; |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1469 | |
| 1470 | if (in->padding == 5) { |
| 1471 | arg->minor = arg->major; |
| 1472 | arg->major = in->padding; |
| 1473 | } |
| 1474 | |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 1475 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1476 | printf("INIT: %u.%u\n", arg->major, arg->minor); |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 1477 | fflush(stdout); |
| 1478 | } |
| 1479 | f->got_init = 1; |
Miklos Szeredi | 159bd7e | 2005-02-28 17:32:16 +0000 | [diff] [blame] | 1480 | if (f->op.init) |
| 1481 | f->user_data = f->op.init(); |
| 1482 | |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1483 | if (arg->major == 5) { |
| 1484 | f->major = 5; |
| 1485 | f->minor = 1; |
| 1486 | } else { |
| 1487 | f->major = FUSE_KERNEL_VERSION; |
| 1488 | f->minor = FUSE_KERNEL_MINOR_VERSION; |
| 1489 | } |
| 1490 | memset(&outarg, 0, sizeof(outarg)); |
| 1491 | outarg.major = f->major; |
| 1492 | outarg.minor = f->minor; |
| 1493 | |
| 1494 | if (f->flags & FUSE_DEBUG) { |
| 1495 | printf(" INIT: %u.%u\n", outarg.major, outarg.minor); |
| 1496 | fflush(stdout); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1497 | } |
| 1498 | |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 1499 | send_reply(f, in, 0, &outarg, sizeof(outarg)); |
| 1500 | } |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1501 | |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1502 | static struct fuse_dirhandle *get_dirhandle(unsigned long fh) |
| 1503 | { |
| 1504 | return (struct fuse_dirhandle *) fh; |
| 1505 | } |
| 1506 | |
| 1507 | static void do_opendir(struct fuse *f, struct fuse_in_header *in, |
| 1508 | struct fuse_open_in *arg) |
| 1509 | { |
| 1510 | int res; |
| 1511 | struct fuse_open_out outarg; |
| 1512 | struct fuse_dirhandle *dh; |
| 1513 | |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1514 | dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle)); |
| 1515 | if (dh == NULL) { |
| 1516 | send_reply(f, in, -ENOMEM, NULL, 0); |
| 1517 | return; |
| 1518 | } |
| 1519 | memset(dh, 0, sizeof(struct fuse_dirhandle)); |
| 1520 | dh->fuse = f; |
| 1521 | dh->contents = NULL; |
| 1522 | dh->len = 0; |
| 1523 | dh->filled = 0; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1524 | mutex_init(&dh->lock); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1525 | |
| 1526 | memset(&outarg, 0, sizeof(outarg)); |
| 1527 | outarg.fh = (unsigned long) dh; |
| 1528 | |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 1529 | if (f->op.opendir) { |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1530 | struct fuse_file_info fi; |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 1531 | char *path; |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1532 | |
| 1533 | memset(&fi, 0, sizeof(fi)); |
| 1534 | fi.flags = arg->flags; |
| 1535 | |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 1536 | res = -ENOENT; |
| 1537 | path = get_path(f, in->nodeid); |
| 1538 | if (path != NULL) { |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 1539 | res = f->op.opendir(path, &fi); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1540 | dh->fh = fi.fh; |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 1541 | } |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1542 | if (res == 0) { |
| 1543 | int res2; |
| 1544 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1545 | res2 = send_reply(f, in, res, &outarg, SIZEOF_COMPAT(f, fuse_open_out)); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1546 | if(res2 == -ENOENT) { |
| 1547 | /* The opendir syscall was interrupted, so it must be |
| 1548 | cancelled */ |
| 1549 | if(f->op.releasedir) |
| 1550 | f->op.releasedir(path, &fi); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1551 | pthread_mutex_destroy(&dh->lock); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1552 | free(dh); |
| 1553 | } |
| 1554 | pthread_mutex_unlock(&f->lock); |
| 1555 | } else { |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 1556 | send_reply(f, in, res, NULL, 0); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1557 | free(dh); |
Miklos Szeredi | f43f063 | 2005-02-28 11:46:56 +0000 | [diff] [blame] | 1558 | } |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1559 | free(path); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1560 | } else |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1561 | send_reply(f, in, 0, &outarg, SIZEOF_COMPAT(f, fuse_open_out)); |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1562 | } |
| 1563 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1564 | static int fill_dir_common(struct fuse_dirhandle *dh, const char *name, |
| 1565 | int type, ino_t ino, off_t off) |
| 1566 | { |
| 1567 | unsigned namelen = strlen(name); |
| 1568 | unsigned entlen; |
| 1569 | unsigned entsize; |
| 1570 | unsigned padlen; |
| 1571 | unsigned newlen; |
| 1572 | unsigned char *newptr; |
| 1573 | |
| 1574 | if (!(dh->fuse->flags & FUSE_USE_INO)) |
| 1575 | ino = (ino_t) -1; |
| 1576 | |
| 1577 | if (namelen > FUSE_NAME_MAX) |
| 1578 | namelen = FUSE_NAME_MAX; |
| 1579 | else if (!namelen) { |
| 1580 | dh->error = -EIO; |
| 1581 | return 1; |
| 1582 | } |
| 1583 | |
| 1584 | entlen = (dh->fuse->major == 5 ? |
| 1585 | FUSE_NAME_OFFSET_COMPAT5 : FUSE_NAME_OFFSET) + namelen; |
| 1586 | entsize = FUSE_DIRENT_ALIGN(entlen); |
| 1587 | padlen = entsize - entlen; |
| 1588 | newlen = dh->len + entsize; |
| 1589 | if (off && dh->fuse->major != 5) { |
| 1590 | dh->filled = 0; |
| 1591 | if (newlen > dh->needlen) |
| 1592 | return 1; |
| 1593 | } |
| 1594 | |
| 1595 | newptr = realloc(dh->contents, newlen); |
| 1596 | if (!newptr) { |
| 1597 | dh->error = -ENOMEM; |
| 1598 | return 1; |
| 1599 | } |
| 1600 | dh->contents = newptr; |
| 1601 | if (dh->fuse->major == 5) { |
| 1602 | struct fuse_dirent_compat5 *dirent; |
| 1603 | dirent = (struct fuse_dirent_compat5 *) (dh->contents + dh->len); |
| 1604 | dirent->ino = ino; |
| 1605 | dirent->namelen = namelen; |
| 1606 | dirent->type = type; |
| 1607 | strncpy(dirent->name, name, namelen); |
| 1608 | } else { |
| 1609 | struct fuse_dirent *dirent; |
| 1610 | dirent = (struct fuse_dirent *) (dh->contents + dh->len); |
| 1611 | dirent->ino = ino; |
| 1612 | dirent->off = off ? off : newlen; |
| 1613 | dirent->namelen = namelen; |
| 1614 | dirent->type = type; |
| 1615 | strncpy(dirent->name, name, namelen); |
| 1616 | } |
| 1617 | if (padlen) |
| 1618 | memset(dh->contents + dh->len + entlen, 0, padlen); |
| 1619 | dh->len = newlen; |
| 1620 | return 0; |
| 1621 | } |
| 1622 | |
| 1623 | static int fill_dir(void *buf, const char *name, const struct stat *stat, |
| 1624 | off_t off) |
| 1625 | { |
| 1626 | int type = stat ? (stat->st_mode & 0170000) >> 12 : 0; |
| 1627 | ino_t ino = stat ? stat->st_ino : (ino_t) -1; |
| 1628 | return fill_dir_common(buf, name, type, ino, off); |
| 1629 | } |
| 1630 | |
| 1631 | static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type, |
| 1632 | ino_t ino) |
| 1633 | { |
| 1634 | fill_dir_common(dh, name, type, ino, 0); |
| 1635 | return dh->error; |
| 1636 | } |
| 1637 | |
| 1638 | static int readdir_fill(struct fuse *f, struct fuse_in_header *in, |
| 1639 | struct fuse_read_in *arg, struct fuse_dirhandle *dh) |
| 1640 | { |
| 1641 | int err = -ENOENT; |
| 1642 | char *path = get_path(f, in->nodeid); |
| 1643 | if (path != NULL) { |
| 1644 | struct fuse_file_info fi; |
| 1645 | |
| 1646 | memset(&fi, 0, sizeof(fi)); |
| 1647 | fi.fh = dh->fh; |
| 1648 | |
| 1649 | dh->len = 0; |
| 1650 | dh->error = 0; |
| 1651 | dh->needlen = arg->size; |
| 1652 | dh->filled = 1; |
| 1653 | err = -ENOSYS; |
| 1654 | if (f->op.readdir) { |
| 1655 | off_t offset = f->major == 5 ? 0 : arg->offset; |
| 1656 | err = f->op.readdir(path, dh, fill_dir, offset, &fi); |
| 1657 | } else if (f->op.getdir) |
| 1658 | err = f->op.getdir(path, dh, fill_dir_old); |
| 1659 | if (!err) |
| 1660 | err = dh->error; |
| 1661 | if (err) |
| 1662 | dh->filled = 0; |
| 1663 | free(path); |
| 1664 | } |
| 1665 | return err; |
| 1666 | } |
| 1667 | |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1668 | static void do_readdir(struct fuse *f, struct fuse_in_header *in, |
| 1669 | struct fuse_read_in *arg) |
| 1670 | { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1671 | int err = 0; |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1672 | struct fuse_dirhandle *dh = get_dirhandle(arg->fh); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1673 | size_t size = 0; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1674 | unsigned char *buf = NULL; |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1675 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1676 | pthread_mutex_lock(&dh->lock); |
| 1677 | if (!dh->filled) { |
| 1678 | err = readdir_fill(f, in, arg, dh); |
| 1679 | if (err) |
| 1680 | goto out; |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1681 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1682 | if (dh->filled) { |
Miklos Szeredi | b92d978 | 2005-02-07 16:10:49 +0000 | [diff] [blame] | 1683 | if (arg->offset < dh->len) { |
| 1684 | size = arg->size; |
| 1685 | if (arg->offset + size > dh->len) |
| 1686 | size = dh->len - arg->offset; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1687 | buf = dh->contents + arg->offset; |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1688 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1689 | } else { |
| 1690 | size = dh->len; |
| 1691 | buf = dh->contents; |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1692 | } |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1693 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1694 | out: |
| 1695 | send_reply(f, in, err, buf, size); |
| 1696 | pthread_mutex_unlock(&dh->lock); |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1697 | } |
| 1698 | |
| 1699 | static void do_releasedir(struct fuse *f, struct fuse_in_header *in, |
| 1700 | struct fuse_release_in *arg) |
| 1701 | { |
| 1702 | struct fuse_dirhandle *dh = get_dirhandle(arg->fh); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1703 | if (f->op.releasedir) { |
| 1704 | char *path; |
| 1705 | struct fuse_file_info fi; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1706 | |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1707 | memset(&fi, 0, sizeof(fi)); |
| 1708 | fi.fh = dh->fh; |
| 1709 | |
| 1710 | path = get_path(f, in->nodeid); |
| 1711 | f->op.releasedir(path ? path : "-", &fi); |
| 1712 | free(path); |
| 1713 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1714 | pthread_mutex_lock(&dh->lock); |
| 1715 | pthread_mutex_unlock(&dh->lock); |
| 1716 | pthread_mutex_destroy(&dh->lock); |
Miklos Szeredi | b92d978 | 2005-02-07 16:10:49 +0000 | [diff] [blame] | 1717 | free(dh->contents); |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1718 | free(dh); |
| 1719 | send_reply(f, in, 0, NULL, 0); |
| 1720 | } |
| 1721 | |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1722 | static void do_fsyncdir(struct fuse *f, struct fuse_in_header *in, |
| 1723 | struct fuse_fsync_in *inarg) |
| 1724 | { |
| 1725 | int res; |
| 1726 | char *path; |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1727 | struct fuse_dirhandle *dh = get_dirhandle(inarg->fh); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1728 | struct fuse_file_info fi; |
| 1729 | |
| 1730 | memset(&fi, 0, sizeof(fi)); |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 1731 | fi.fh = dh->fh; |
| 1732 | |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1733 | res = -ENOENT; |
| 1734 | path = get_path(f, in->nodeid); |
| 1735 | if (path != NULL) { |
| 1736 | res = -ENOSYS; |
| 1737 | if (f->op.fsyncdir) |
| 1738 | res = f->op.fsyncdir(path, inarg->fsync_flags & 1, &fi); |
| 1739 | free(path); |
| 1740 | } |
| 1741 | send_reply(f, in, res, NULL, 0); |
| 1742 | } |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1743 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1744 | static void free_cmd(struct fuse_cmd *cmd) |
| 1745 | { |
| 1746 | free(cmd->buf); |
| 1747 | free(cmd); |
| 1748 | } |
| 1749 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1750 | void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1751 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1752 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1753 | void *inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header); |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1754 | struct fuse_context *ctx = fuse_get_context(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1755 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1756 | fuse_dec_avail(f); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1757 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1758 | if ((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1759 | printf("unique: %llu, opcode: %s (%i), nodeid: %lu, insize: %i\n", |
| 1760 | in->unique, opname(in->opcode), in->opcode, |
| 1761 | (unsigned long) in->nodeid, cmd->buflen); |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 1762 | fflush(stdout); |
| 1763 | } |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 1764 | |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 1765 | if (!f->got_init && in->opcode != FUSE_INIT) { |
| 1766 | /* Old kernel version probably */ |
| 1767 | send_reply(f, in, -EPROTO, NULL, 0); |
| 1768 | goto out; |
| 1769 | } |
| 1770 | |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame^] | 1771 | if ((f->flags & FUSE_ALLOW_ROOT) && in->uid != f->owner && in->uid != 0 && |
| 1772 | in->opcode != FUSE_INIT && in->opcode != FUSE_READ && |
| 1773 | in->opcode != FUSE_WRITE && in->opcode != FUSE_FSYNC && |
| 1774 | in->opcode != FUSE_RELEASE && in->opcode != FUSE_READDIR && |
| 1775 | in->opcode != FUSE_FSYNCDIR && in->opcode != FUSE_RELEASEDIR) { |
| 1776 | send_reply(f, in, -EACCES, NULL, 0); |
| 1777 | goto out; |
| 1778 | } |
| 1779 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1780 | ctx->fuse = f; |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 1781 | ctx->uid = in->uid; |
| 1782 | ctx->gid = in->gid; |
Miklos Szeredi | 1f18db5 | 2004-09-27 06:54:49 +0000 | [diff] [blame] | 1783 | ctx->pid = in->pid; |
Miklos Szeredi | 159bd7e | 2005-02-28 17:32:16 +0000 | [diff] [blame] | 1784 | ctx->private_data = f->user_data; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1785 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1786 | switch (in->opcode) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1787 | case FUSE_LOOKUP: |
| 1788 | do_lookup(f, in, (char *) inarg); |
| 1789 | break; |
| 1790 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1791 | case FUSE_GETATTR: |
| 1792 | do_getattr(f, in); |
| 1793 | break; |
| 1794 | |
| 1795 | case FUSE_SETATTR: |
| 1796 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 1797 | break; |
| 1798 | |
| 1799 | case FUSE_READLINK: |
| 1800 | do_readlink(f, in); |
| 1801 | break; |
| 1802 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1803 | case FUSE_MKNOD: |
| 1804 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 1805 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1806 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1807 | case FUSE_MKDIR: |
| 1808 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 1809 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1810 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1811 | case FUSE_UNLINK: |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1812 | do_unlink(f, in, (char *) inarg); |
| 1813 | break; |
| 1814 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1815 | case FUSE_RMDIR: |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1816 | do_rmdir(f, in, (char *) inarg); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1817 | break; |
| 1818 | |
| 1819 | case FUSE_SYMLINK: |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1820 | do_symlink(f, in, (char *) inarg, |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1821 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 1822 | break; |
| 1823 | |
| 1824 | case FUSE_RENAME: |
| 1825 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 1826 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1827 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1828 | case FUSE_LINK: |
| 1829 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 1830 | break; |
| 1831 | |
| 1832 | case FUSE_OPEN: |
| 1833 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 1834 | break; |
| 1835 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1836 | case FUSE_FLUSH: |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1837 | do_flush(f, in, (struct fuse_flush_in *) inarg); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1838 | break; |
| 1839 | |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1840 | case FUSE_RELEASE: |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1841 | do_release(f, in, (struct fuse_release_in *) inarg); |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1842 | break; |
| 1843 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1844 | case FUSE_READ: |
| 1845 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 1846 | break; |
| 1847 | |
| 1848 | case FUSE_WRITE: |
| 1849 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 1850 | break; |
| 1851 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1852 | case FUSE_STATFS: |
| 1853 | do_statfs(f, in); |
| 1854 | break; |
| 1855 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1856 | case FUSE_FSYNC: |
| 1857 | do_fsync(f, in, (struct fuse_fsync_in *) inarg); |
| 1858 | break; |
| 1859 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1860 | case FUSE_SETXATTR: |
| 1861 | do_setxattr(f, in, (struct fuse_setxattr_in *) inarg); |
| 1862 | break; |
| 1863 | |
| 1864 | case FUSE_GETXATTR: |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1865 | do_getxattr(f, in, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1866 | break; |
| 1867 | |
| 1868 | case FUSE_LISTXATTR: |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1869 | do_listxattr(f, in, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1870 | break; |
| 1871 | |
| 1872 | case FUSE_REMOVEXATTR: |
| 1873 | do_removexattr(f, in, (char *) inarg); |
| 1874 | break; |
| 1875 | |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 1876 | case FUSE_INIT: |
| 1877 | do_init(f, in, (struct fuse_init_in_out *) inarg); |
| 1878 | break; |
| 1879 | |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1880 | case FUSE_OPENDIR: |
| 1881 | do_opendir(f, in, (struct fuse_open_in *) inarg); |
| 1882 | break; |
| 1883 | |
| 1884 | case FUSE_READDIR: |
| 1885 | do_readdir(f, in, (struct fuse_read_in *) inarg); |
| 1886 | break; |
| 1887 | |
| 1888 | case FUSE_RELEASEDIR: |
| 1889 | do_releasedir(f, in, (struct fuse_release_in *) inarg); |
| 1890 | break; |
| 1891 | |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1892 | case FUSE_FSYNCDIR: |
| 1893 | do_fsyncdir(f, in, (struct fuse_fsync_in *) inarg); |
| 1894 | break; |
| 1895 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1896 | default: |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1897 | send_reply(f, in, -ENOSYS, NULL, 0); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1898 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1899 | |
Miklos Szeredi | 3f0005f | 2005-01-04 19:24:31 +0000 | [diff] [blame] | 1900 | out: |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1901 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1902 | } |
| 1903 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1904 | int fuse_exited(struct fuse* f) |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1905 | { |
| 1906 | return f->exited; |
| 1907 | } |
| 1908 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1909 | struct fuse_cmd *fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1910 | { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1911 | ssize_t res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1912 | struct fuse_cmd *cmd; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1913 | struct fuse_in_header *in; |
| 1914 | void *inarg; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1915 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1916 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1917 | if (cmd == NULL) { |
| 1918 | fprintf(stderr, "fuse: failed to allocate cmd in read\n"); |
| 1919 | return NULL; |
| 1920 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1921 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1922 | if (cmd->buf == NULL) { |
| 1923 | fprintf(stderr, "fuse: failed to allocate read buffer\n"); |
| 1924 | free(cmd); |
| 1925 | return NULL; |
| 1926 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1927 | in = (struct fuse_in_header *) cmd->buf; |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1928 | inarg = cmd->buf + SIZEOF_COMPAT(f, fuse_in_header); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1929 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1930 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 1931 | if (res == -1) { |
| 1932 | free_cmd(cmd); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1933 | if (fuse_exited(f) || errno == EINTR || errno == ENOENT) |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1934 | return NULL; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1935 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1936 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 1937 | if (errno != ENODEV) { |
| 1938 | /* BAD... This will happen again */ |
| 1939 | perror("fuse: reading device"); |
| 1940 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1941 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1942 | fuse_exit(f); |
| 1943 | return NULL; |
| 1944 | } |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 1945 | if ((size_t) res < SIZEOF_COMPAT(f, fuse_in_header)) { |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1946 | free_cmd(cmd); |
| 1947 | /* Cannot happen */ |
| 1948 | fprintf(stderr, "short read on fuse device\n"); |
| 1949 | fuse_exit(f); |
| 1950 | return NULL; |
| 1951 | } |
| 1952 | cmd->buflen = res; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1953 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1954 | /* Forget is special, it can be done without messing with threads. */ |
| 1955 | if (in->opcode == FUSE_FORGET) { |
| 1956 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 1957 | free_cmd(cmd); |
| 1958 | return NULL; |
| 1959 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1960 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1961 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1962 | } |
| 1963 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1964 | int fuse_loop(struct fuse *f) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1965 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1966 | if (f == NULL) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1967 | return -1; |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1968 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1969 | while (1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1970 | struct fuse_cmd *cmd; |
| 1971 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1972 | if (fuse_exited(f)) |
Miklos Szeredi | 874e3c1 | 2004-11-01 23:15:20 +0000 | [diff] [blame] | 1973 | break; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1974 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1975 | cmd = fuse_read_cmd(f); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1976 | if (cmd == NULL) |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1977 | continue; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1978 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1979 | fuse_process_cmd(f, cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1980 | } |
Miklos Szeredi | 874e3c1 | 2004-11-01 23:15:20 +0000 | [diff] [blame] | 1981 | f->exited = 0; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1982 | return 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1983 | } |
| 1984 | |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1985 | int fuse_invalidate(struct fuse *f, const char *path) |
| 1986 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1987 | (void) f; |
| 1988 | (void) path; |
| 1989 | return -EINVAL; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1990 | } |
| 1991 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1992 | void fuse_exit(struct fuse *f) |
| 1993 | { |
| 1994 | f->exited = 1; |
| 1995 | } |
| 1996 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1997 | struct fuse_context *fuse_get_context() |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1998 | { |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1999 | static struct fuse_context context; |
| 2000 | if (fuse_getcontext) |
| 2001 | return fuse_getcontext(); |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 2002 | else |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 2003 | return &context; |
| 2004 | } |
| 2005 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2006 | void fuse_set_getcontext_func(struct fuse_context *(*func)(void)) |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 2007 | { |
| 2008 | fuse_getcontext = func; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 2009 | } |
| 2010 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 2011 | int fuse_is_lib_option(const char *opt) |
| 2012 | { |
| 2013 | if (strcmp(opt, "debug") == 0 || |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2014 | strcmp(opt, "hard_remove") == 0 || |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame^] | 2015 | strcmp(opt, "use_ino") == 0 || |
| 2016 | strcmp(opt, "allow_root") == 0) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 2017 | return 1; |
| 2018 | else |
| 2019 | return 0; |
| 2020 | } |
| 2021 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2022 | static int parse_lib_opts(struct fuse *f, const char *opts) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 2023 | { |
| 2024 | if (opts) { |
| 2025 | char *xopts = strdup(opts); |
| 2026 | char *s = xopts; |
| 2027 | char *opt; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2028 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2029 | if (xopts == NULL) { |
| 2030 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2031 | return -1; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2032 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 2033 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 2034 | while((opt = strsep(&s, ","))) { |
| 2035 | if (strcmp(opt, "debug") == 0) |
| 2036 | f->flags |= FUSE_DEBUG; |
| 2037 | else if (strcmp(opt, "hard_remove") == 0) |
| 2038 | f->flags |= FUSE_HARD_REMOVE; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2039 | else if (strcmp(opt, "use_ino") == 0) |
| 2040 | f->flags |= FUSE_USE_INO; |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame^] | 2041 | else if (strcmp(opt, "allow_root") == 0) |
| 2042 | f->flags |= FUSE_ALLOW_ROOT; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 2043 | else |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 2044 | fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt); |
| 2045 | } |
| 2046 | free(xopts); |
| 2047 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2048 | return 0; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 2049 | } |
| 2050 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2051 | struct fuse *fuse_new_common(int fd, const char *opts, |
| 2052 | const struct fuse_operations *op, |
| 2053 | size_t op_size, int compat) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2054 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2055 | struct fuse *f; |
| 2056 | struct node *root; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2057 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 2058 | if (sizeof(struct fuse_operations) < op_size) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2059 | fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n"); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 2060 | op_size = sizeof(struct fuse_operations); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2061 | } |
| 2062 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2063 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2064 | if (f == NULL) { |
| 2065 | fprintf(stderr, "fuse: failed to allocate fuse object\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2066 | goto out; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2067 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 2068 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2069 | if (parse_lib_opts(f, opts) == -1) |
| 2070 | goto out_free; |
| 2071 | |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 2072 | f->fd = fd; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2073 | f->ctr = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 2074 | f->generation = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 2075 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2076 | f->name_table_size = 14057; |
| 2077 | f->name_table = (struct node **) |
| 2078 | calloc(1, sizeof(struct node *) * f->name_table_size); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2079 | if (f->name_table == NULL) { |
| 2080 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2081 | goto out_free; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2082 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2083 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2084 | f->id_table_size = 14057; |
| 2085 | f->id_table = (struct node **) |
| 2086 | calloc(1, sizeof(struct node *) * f->id_table_size); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2087 | if (f->id_table == NULL) { |
| 2088 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2089 | goto out_free_name_table; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2090 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2091 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 2092 | mutex_init(&f->lock); |
| 2093 | mutex_init(&f->worker_lock); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 2094 | f->numworker = 0; |
| 2095 | f->numavail = 0; |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2096 | memcpy(&f->op, op, op_size); |
| 2097 | f->compat = compat; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 2098 | f->exited = 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2099 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2100 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2101 | if (root == NULL) { |
| 2102 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2103 | goto out_free_id_table; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2104 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2105 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2106 | root->name = strdup("/"); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2107 | if (root->name == NULL) { |
| 2108 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2109 | goto out_free_root; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2110 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2111 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2112 | root->parent = 0; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2113 | root->nodeid = FUSE_ROOT_ID; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 2114 | root->generation = 0; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 2115 | root->refctr = 1; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2116 | hash_id(f, root); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2117 | |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame^] | 2118 | f->owner = getuid(); |
| 2119 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2120 | return f; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2121 | |
| 2122 | out_free_root: |
| 2123 | free(root); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2124 | out_free_id_table: |
| 2125 | free(f->id_table); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2126 | out_free_name_table: |
| 2127 | free(f->name_table); |
| 2128 | out_free: |
| 2129 | free(f); |
| 2130 | out: |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2131 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2132 | } |
| 2133 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2134 | struct fuse *fuse_new(int fd, const char *opts, |
| 2135 | const struct fuse_operations *op, size_t op_size) |
| 2136 | { |
| 2137 | return fuse_new_common(fd, opts, op, op_size, 0); |
| 2138 | } |
| 2139 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2140 | struct fuse *fuse_new_compat2(int fd, const char *opts, |
| 2141 | const struct fuse_operations_compat2 *op) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2142 | { |
| 2143 | return fuse_new_common(fd, opts, (struct fuse_operations *) op, |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2144 | sizeof(struct fuse_operations_compat2), 21); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2145 | } |
| 2146 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2147 | struct fuse *fuse_new_compat1(int fd, int flags, |
| 2148 | const struct fuse_operations_compat1 *op) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2149 | { |
| 2150 | char *opts = NULL; |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2151 | if (flags & FUSE_DEBUG_COMPAT1) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2152 | opts = "debug"; |
| 2153 | return fuse_new_common(fd, opts, (struct fuse_operations *) op, |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2154 | sizeof(struct fuse_operations_compat1), 11); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2155 | } |
| 2156 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2157 | void fuse_destroy(struct fuse *f) |
| 2158 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2159 | size_t i; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2160 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2161 | struct node *node; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2162 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2163 | for (node = f->id_table[i]; node != NULL; node = node->id_next) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2164 | if (node->is_hidden) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2165 | char *path = get_path(f, node->nodeid); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2166 | if (path) |
| 2167 | f->op.unlink(path); |
| 2168 | } |
| 2169 | } |
| 2170 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2171 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2172 | struct node *node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2173 | struct node *next; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2174 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2175 | for (node = f->id_table[i]; node != NULL; node = next) { |
| 2176 | next = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2177 | free_node(node); |
| 2178 | } |
| 2179 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2180 | free(f->id_table); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2181 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 2182 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 2183 | pthread_mutex_destroy(&f->worker_lock); |
Miklos Szeredi | 159bd7e | 2005-02-28 17:32:16 +0000 | [diff] [blame] | 2184 | if (f->op.destroy) |
| 2185 | f->op.destroy(f->user_data); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2186 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2187 | } |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2188 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2189 | __asm__(".symver fuse_exited,__fuse_exited@"); |
| 2190 | __asm__(".symver fuse_process_cmd,__fuse_process_cmd@"); |
| 2191 | __asm__(".symver fuse_read_cmd,__fuse_read_cmd@"); |
| 2192 | __asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@"); |
| 2193 | __asm__(".symver fuse_new_compat2,fuse_new@"); |