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