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