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