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 | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 9 | |
| 10 | /* For pthread_rwlock_t */ |
| 11 | #define _GNU_SOURCE |
| 12 | |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 13 | #include "fuse_i.h" |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 14 | #include "fuse_lowlevel.h" |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 15 | #include "fuse_compat.h" |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 16 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 17 | #include <stdio.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 18 | #include <string.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 19 | #include <stdlib.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 20 | #include <unistd.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 21 | #include <limits.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 22 | #include <errno.h> |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 23 | #include <assert.h> |
| 24 | #include <stdint.h> |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 25 | #include <pthread.h> |
Miklos Szeredi | 019b4e9 | 2001-12-26 18:08:09 +0000 | [diff] [blame] | 26 | #include <sys/param.h> |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 27 | #include <sys/uio.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 28 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 29 | /* FUSE flags: */ |
| 30 | |
| 31 | /** Enable debuging output */ |
| 32 | #define FUSE_DEBUG (1 << 1) |
| 33 | |
| 34 | /** If a file is removed but it's still open, don't hide the file but |
| 35 | remove it immediately */ |
| 36 | #define FUSE_HARD_REMOVE (1 << 2) |
| 37 | |
| 38 | /** Use st_ino field in getattr instead of generating inode numbers */ |
| 39 | #define FUSE_USE_INO (1 << 3) |
| 40 | |
Miklos Szeredi | 33be22d | 2005-05-27 09:12:43 +0000 | [diff] [blame] | 41 | /** Make a best effort to fill in inode number in a readdir **/ |
| 42 | #define FUSE_READDIR_INO (1 << 5) |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 43 | |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 44 | /** Ignore file mode supplied by the filesystem, and create one based |
| 45 | on the 'umask' option */ |
| 46 | #define FUSE_SET_MODE (1 << 6) |
| 47 | |
| 48 | /** Ignore st_uid supplied by the filesystem and set it based on the |
| 49 | 'uid' option*/ |
| 50 | #define FUSE_SET_UID (1 << 7) |
| 51 | |
| 52 | /** Ignore st_gid supplied by the filesystem and set it based on the |
| 53 | 'gid' option*/ |
| 54 | #define FUSE_SET_GID (1 << 8) |
| 55 | |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 56 | /** Bypass the page cache for read and write operations */ |
| 57 | #define FUSE_DIRECT_IO (1 << 9) |
| 58 | |
| 59 | /** If the FUSE_KERNEL_CACHE flag is given, then cached data will not |
| 60 | be flushed on open */ |
| 61 | #define FUSE_KERNEL_CACHE (1 << 10) |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 62 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 63 | #define FUSE_MAX_PATH 4096 |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 64 | |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 65 | struct fuse { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 66 | struct fuse_session *se; |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 67 | int flags; |
| 68 | struct fuse_operations op; |
| 69 | int compat; |
| 70 | struct node **name_table; |
| 71 | size_t name_table_size; |
| 72 | struct node **id_table; |
| 73 | size_t id_table_size; |
| 74 | fuse_ino_t ctr; |
| 75 | unsigned int generation; |
| 76 | unsigned int hidectr; |
| 77 | pthread_mutex_t lock; |
| 78 | pthread_rwlock_t tree_lock; |
| 79 | void *user_data; |
| 80 | uid_t uid; |
| 81 | gid_t gid; |
| 82 | mode_t umask; |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 83 | double entry_timeout; |
| 84 | double attr_timeout; |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 85 | }; |
| 86 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 87 | struct node { |
| 88 | struct node *name_next; |
| 89 | struct node *id_next; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 90 | fuse_ino_t nodeid; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 91 | unsigned int generation; |
| 92 | int refctr; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 93 | fuse_ino_t parent; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 94 | char *name; |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 95 | uint64_t nlookup; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 96 | int open_count; |
| 97 | int is_hidden; |
| 98 | }; |
| 99 | |
| 100 | struct fuse_dirhandle { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 101 | pthread_mutex_t lock; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 102 | struct fuse *fuse; |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 103 | char *contents; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 104 | int allocated; |
Miklos Szeredi | b92d978 | 2005-02-07 16:10:49 +0000 | [diff] [blame] | 105 | unsigned len; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 106 | unsigned needlen; |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 107 | int filled; |
Miklos Szeredi | ede1f7a | 2005-04-01 21:08:57 +0000 | [diff] [blame] | 108 | unsigned long fh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 109 | int error; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 110 | fuse_ino_t nodeid; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 111 | }; |
| 112 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 113 | static struct fuse_context *(*fuse_getcontext)(void) = NULL; |
| 114 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 115 | #ifndef USE_UCLIBC |
| 116 | #define mutex_init(mut) pthread_mutex_init(mut, NULL) |
| 117 | #else |
Miklos Szeredi | 129ef8f | 2005-06-20 13:48:42 +0000 | [diff] [blame] | 118 | static void mutex_init(pthread_mutex_t *mut) |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 119 | { |
| 120 | pthread_mutexattr_t attr; |
| 121 | pthread_mutexattr_init(&attr); |
| 122 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 123 | pthread_mutex_init(mut, &attr); |
| 124 | pthread_mutexattr_destroy(&attr); |
| 125 | } |
| 126 | #endif |
| 127 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 128 | static struct node *get_node_nocheck(struct fuse *f, fuse_ino_t nodeid) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 129 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 130 | size_t hash = nodeid % f->id_table_size; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 131 | struct node *node; |
| 132 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 133 | for (node = f->id_table[hash]; node != NULL; node = node->id_next) |
| 134 | if (node->nodeid == nodeid) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 135 | return node; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 136 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 137 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 140 | static struct node *get_node(struct fuse *f, fuse_ino_t nodeid) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 141 | { |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 142 | struct node *node = get_node_nocheck(f, nodeid); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 143 | if (!node) { |
| 144 | fprintf(stderr, "fuse internal error: node %lu not found\n", |
| 145 | nodeid); |
| 146 | abort(); |
| 147 | } |
| 148 | return node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 151 | static void free_node(struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 152 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 153 | free(node->name); |
| 154 | free(node); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 155 | } |
| 156 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 157 | static void unhash_id(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 158 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 159 | size_t hash = node->nodeid % f->id_table_size; |
| 160 | struct node **nodep = &f->id_table[hash]; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 161 | |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 162 | for (; *nodep != NULL; nodep = &(*nodep)->id_next) |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 163 | if (*nodep == node) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 164 | *nodep = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 165 | return; |
| 166 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 167 | } |
| 168 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 169 | static void hash_id(struct fuse *f, struct node *node) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 170 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 171 | size_t hash = node->nodeid % f->id_table_size; |
| 172 | node->id_next = f->id_table[hash]; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 173 | f->id_table[hash] = node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 176 | static unsigned int name_hash(struct fuse *f, fuse_ino_t parent, const char *name) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 177 | { |
| 178 | unsigned int hash = *name; |
| 179 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 180 | if (hash) |
| 181 | for (name += 1; *name != '\0'; name++) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 182 | hash = (hash << 5) - hash + *name; |
| 183 | |
| 184 | return (hash + parent) % f->name_table_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 187 | static void unref_node(struct fuse *f, struct node *node); |
| 188 | |
| 189 | static void unhash_name(struct fuse *f, struct node *node) |
| 190 | { |
| 191 | if (node->name) { |
| 192 | size_t hash = name_hash(f, node->parent, node->name); |
| 193 | struct node **nodep = &f->name_table[hash]; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 194 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 195 | for (; *nodep != NULL; nodep = &(*nodep)->name_next) |
| 196 | if (*nodep == node) { |
| 197 | *nodep = node->name_next; |
| 198 | node->name_next = NULL; |
| 199 | unref_node(f, get_node(f, node->parent)); |
| 200 | free(node->name); |
| 201 | node->name = NULL; |
| 202 | node->parent = 0; |
| 203 | return; |
| 204 | } |
| 205 | fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n", |
| 206 | node->nodeid); |
| 207 | abort(); |
| 208 | } |
| 209 | } |
| 210 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 211 | static int hash_name(struct fuse *f, struct node *node, fuse_ino_t parent, |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 212 | const char *name) |
| 213 | { |
| 214 | size_t hash = name_hash(f, parent, name); |
| 215 | node->name = strdup(name); |
| 216 | if (node->name == NULL) |
| 217 | return -1; |
| 218 | |
| 219 | get_node(f, parent)->refctr ++; |
| 220 | node->parent = parent; |
| 221 | node->name_next = f->name_table[hash]; |
| 222 | f->name_table[hash] = node; |
| 223 | return 0; |
| 224 | } |
| 225 | |
| 226 | static void delete_node(struct fuse *f, struct node *node) |
| 227 | { |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 228 | if (f->flags & FUSE_DEBUG) { |
| 229 | printf("delete: %lu\n", node->nodeid); |
| 230 | fflush(stdout); |
| 231 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 232 | assert(!node->name); |
| 233 | unhash_id(f, node); |
| 234 | free_node(node); |
| 235 | } |
| 236 | |
| 237 | static void unref_node(struct fuse *f, struct node *node) |
| 238 | { |
| 239 | assert(node->refctr > 0); |
| 240 | node->refctr --; |
| 241 | if (!node->refctr) |
| 242 | delete_node(f, node); |
| 243 | } |
| 244 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 245 | static fuse_ino_t next_id(struct fuse *f) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 246 | { |
| 247 | do { |
| 248 | f->ctr++; |
| 249 | if (!f->ctr) |
| 250 | f->generation ++; |
| 251 | } while (f->ctr == 0 || get_node_nocheck(f, f->ctr) != NULL); |
| 252 | return f->ctr; |
| 253 | } |
| 254 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 255 | static struct node *lookup_node(struct fuse *f, fuse_ino_t parent, |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 256 | const char *name) |
| 257 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 258 | size_t hash = name_hash(f, parent, name); |
| 259 | struct node *node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 260 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 261 | for (node = f->name_table[hash]; node != NULL; node = node->name_next) |
| 262 | if (node->parent == parent && strcmp(node->name, name) == 0) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 263 | return node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 264 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 265 | return NULL; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 266 | } |
| 267 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 268 | static struct node *find_node(struct fuse *f, fuse_ino_t parent, |
| 269 | const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 270 | { |
| 271 | struct node *node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 272 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 273 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 274 | node = lookup_node(f, parent, name); |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 275 | if (node == NULL) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 276 | node = (struct node *) calloc(1, sizeof(struct node)); |
| 277 | if (node == NULL) |
| 278 | goto out_err; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 279 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 280 | node->refctr = 1; |
| 281 | node->nodeid = next_id(f); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 282 | node->open_count = 0; |
| 283 | node->is_hidden = 0; |
| 284 | node->generation = f->generation; |
| 285 | if (hash_name(f, node, parent, name) == -1) { |
| 286 | free(node); |
| 287 | node = NULL; |
| 288 | goto out_err; |
| 289 | } |
| 290 | hash_id(f, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 291 | } |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 292 | node->nlookup ++; |
Miklos Szeredi | c230991 | 2004-09-21 13:40:38 +0000 | [diff] [blame] | 293 | out_err: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 294 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 295 | return node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 296 | } |
| 297 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 298 | static char *add_name(char *buf, char *s, const char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 299 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 300 | size_t len = strlen(name); |
| 301 | s -= len; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 302 | if (s <= buf) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 303 | fprintf(stderr, "fuse: path too long: ...%s\n", s + len); |
| 304 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 305 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 306 | strncpy(s, name, len); |
| 307 | s--; |
| 308 | *s = '/'; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 309 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 310 | return s; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 311 | } |
| 312 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 313 | static char *get_path_name(struct fuse *f, fuse_ino_t nodeid, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 314 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 315 | char buf[FUSE_MAX_PATH]; |
| 316 | char *s = buf + FUSE_MAX_PATH - 1; |
| 317 | struct node *node; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 318 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 319 | *s = '\0'; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 320 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 321 | if (name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 322 | s = add_name(buf, s, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 323 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 324 | return NULL; |
| 325 | } |
| 326 | |
| 327 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 328 | for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID; |
| 329 | node = get_node(f, node->parent)) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 330 | if (node->name == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 331 | s = NULL; |
| 332 | break; |
| 333 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 334 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 335 | s = add_name(buf, s, node->name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 336 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 337 | break; |
| 338 | } |
| 339 | pthread_mutex_unlock(&f->lock); |
| 340 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 341 | if (node == NULL || s == NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 342 | return NULL; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 343 | else if (*s == '\0') |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 344 | return strdup("/"); |
| 345 | else |
| 346 | return strdup(s); |
| 347 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 348 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 349 | static char *get_path(struct fuse *f, fuse_ino_t nodeid) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 350 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 351 | return get_path_name(f, nodeid, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 352 | } |
| 353 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 354 | static void forget_node(struct fuse *f, fuse_ino_t nodeid, uint64_t nlookup) |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 355 | { |
| 356 | struct node *node; |
| 357 | if (nodeid == FUSE_ROOT_ID) |
| 358 | return; |
| 359 | pthread_mutex_lock(&f->lock); |
| 360 | node = get_node(f, nodeid); |
| 361 | assert(node->nlookup >= nlookup); |
| 362 | node->nlookup -= nlookup; |
| 363 | if (!node->nlookup) { |
| 364 | unhash_name(f, node); |
| 365 | unref_node(f, node); |
| 366 | } |
| 367 | pthread_mutex_unlock(&f->lock); |
| 368 | } |
| 369 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 370 | static void remove_node(struct fuse *f, fuse_ino_t dir, const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 371 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 372 | struct node *node; |
| 373 | |
| 374 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 375 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 376 | if (node != NULL) |
| 377 | unhash_name(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 378 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 379 | } |
| 380 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 381 | static int rename_node(struct fuse *f, fuse_ino_t olddir, const char *oldname, |
| 382 | fuse_ino_t newdir, const char *newname, int hide) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 383 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 384 | struct node *node; |
| 385 | struct node *newnode; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 386 | int err = 0; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 387 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 388 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 389 | node = lookup_node(f, olddir, oldname); |
| 390 | newnode = lookup_node(f, newdir, newname); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 391 | if (node == NULL) |
| 392 | goto out; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 393 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 394 | if (newnode != NULL) { |
| 395 | if (hide) { |
| 396 | fprintf(stderr, "fuse: hidden file got created during hiding\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 397 | err = -EBUSY; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 398 | goto out; |
| 399 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 400 | unhash_name(f, newnode); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 401 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 402 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 403 | unhash_name(f, node); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 404 | if (hash_name(f, node, newdir, newname) == -1) { |
| 405 | err = -ENOMEM; |
| 406 | goto out; |
| 407 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 408 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 409 | if (hide) |
| 410 | node->is_hidden = 1; |
| 411 | |
| 412 | out: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 413 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 414 | return err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 417 | static void set_stat(struct fuse *f, fuse_ino_t nodeid, struct stat *stbuf) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 418 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 419 | if (!(f->flags & FUSE_USE_INO)) |
| 420 | stbuf->st_ino = nodeid; |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 421 | if (f->flags & FUSE_SET_MODE) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 422 | stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->umask); |
| 423 | if (f->flags & FUSE_SET_UID) |
| 424 | stbuf->st_uid = f->uid; |
| 425 | if (f->flags & FUSE_SET_GID) |
| 426 | stbuf->st_gid = f->gid; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 429 | static int is_open(struct fuse *f, fuse_ino_t dir, const char *name) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 430 | { |
| 431 | struct node *node; |
| 432 | int isopen = 0; |
| 433 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 434 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 435 | if (node && node->open_count > 0) |
| 436 | isopen = 1; |
| 437 | pthread_mutex_unlock(&f->lock); |
| 438 | return isopen; |
| 439 | } |
| 440 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 441 | static char *hidden_name(struct fuse *f, fuse_ino_t dir, const char *oldname, |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 442 | char *newname, size_t bufsize) |
| 443 | { |
| 444 | struct stat buf; |
| 445 | struct node *node; |
| 446 | struct node *newnode; |
| 447 | char *newpath; |
| 448 | int res; |
| 449 | int failctr = 10; |
| 450 | |
| 451 | if (!f->op.getattr) |
| 452 | return NULL; |
| 453 | |
| 454 | do { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 455 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 456 | node = lookup_node(f, dir, oldname); |
| 457 | if (node == NULL) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 458 | pthread_mutex_unlock(&f->lock); |
| 459 | return NULL; |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 460 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 461 | do { |
| 462 | f->hidectr ++; |
| 463 | snprintf(newname, bufsize, ".fuse_hidden%08x%08x", |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 464 | (unsigned int) node->nodeid, f->hidectr); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 465 | newnode = lookup_node(f, dir, newname); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 466 | } while(newnode); |
| 467 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 468 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 469 | newpath = get_path_name(f, dir, newname); |
| 470 | if (!newpath) |
| 471 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 472 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 473 | res = f->op.getattr(newpath, &buf); |
| 474 | if (res != 0) |
| 475 | break; |
| 476 | free(newpath); |
| 477 | newpath = NULL; |
| 478 | } while(--failctr); |
| 479 | |
| 480 | return newpath; |
| 481 | } |
| 482 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 483 | static int hide_node(struct fuse *f, const char *oldpath, fuse_ino_t dir, |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 484 | const char *oldname) |
| 485 | { |
| 486 | char newname[64]; |
| 487 | char *newpath; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 488 | int err = -EBUSY; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 489 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 490 | if (f->op.rename && f->op.unlink) { |
| 491 | newpath = hidden_name(f, dir, oldname, newname, sizeof(newname)); |
| 492 | if (newpath) { |
| 493 | int res = f->op.rename(oldpath, newpath); |
| 494 | if (res == 0) |
| 495 | err = rename_node(f, dir, oldname, dir, newname, 1); |
| 496 | free(newpath); |
| 497 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 498 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 499 | return err; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 500 | } |
| 501 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 502 | static int lookup_path(struct fuse *f, fuse_ino_t nodeid, const char *name, |
| 503 | const char *path, struct fuse_entry_param *e) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 504 | { |
| 505 | int res; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 506 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 507 | memset(e, 0, sizeof(struct fuse_entry_param)); |
| 508 | res = f->op.getattr(path, &e->attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 509 | if (res == 0) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 510 | struct node *node; |
| 511 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 512 | node = find_node(f, nodeid, name); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 513 | if (node == NULL) |
| 514 | res = -ENOMEM; |
| 515 | else { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 516 | e->ino = node->nodeid; |
| 517 | e->generation = node->generation; |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 518 | e->entry_timeout = f->entry_timeout; |
| 519 | e->attr_timeout = f->attr_timeout; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 520 | set_stat(f, e->ino, &e->attr); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 521 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 522 | printf(" NODEID: %lu\n", (unsigned long) e->ino); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 523 | fflush(stdout); |
| 524 | } |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 525 | } |
| 526 | } |
| 527 | return res; |
| 528 | } |
| 529 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 530 | static struct fuse *req_fuse(fuse_req_t req) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 531 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 532 | return (struct fuse *) fuse_req_userdata(req); |
| 533 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 534 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 535 | static struct fuse *req_fuse_prepare(fuse_req_t req) |
| 536 | { |
| 537 | struct fuse_context *c = fuse_get_context(); |
| 538 | const struct fuse_ctx *ctx = fuse_req_ctx(req); |
| 539 | c->fuse = req_fuse(req); |
| 540 | c->uid = ctx->uid; |
| 541 | c->gid = ctx->gid; |
| 542 | c->pid = ctx->pid; |
| 543 | c->private_data = c->fuse->user_data; |
| 544 | |
| 545 | return c->fuse; |
| 546 | } |
| 547 | |
| 548 | static inline void reply_err(fuse_req_t req, int err) |
| 549 | { |
| 550 | /* fuse_reply_err() uses non-negated errno values */ |
| 551 | fuse_reply_err(req, -err); |
| 552 | } |
| 553 | |
| 554 | static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e, |
| 555 | int err) |
| 556 | { |
| 557 | if (!err) { |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 558 | if (fuse_reply_entry(req, e) == -ENOENT) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 559 | forget_node(req_fuse(req), e->ino, 1); |
| 560 | } else |
| 561 | reply_err(req, err); |
| 562 | } |
| 563 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 564 | static void fuse_data_init(void *data) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 565 | { |
| 566 | struct fuse *f = (struct fuse *) data; |
| 567 | |
| 568 | if (f->op.init) |
| 569 | f->user_data = f->op.init(); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 570 | } |
| 571 | |
| 572 | static void fuse_data_destroy(void *data) |
| 573 | { |
| 574 | struct fuse *f = (struct fuse *) data; |
| 575 | |
| 576 | if (f->op.destroy) |
| 577 | f->op.destroy(f->user_data); |
| 578 | } |
| 579 | |
| 580 | static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name) |
| 581 | { |
| 582 | struct fuse *f = req_fuse_prepare(req); |
| 583 | struct fuse_entry_param e; |
| 584 | char *path; |
| 585 | int err; |
| 586 | |
| 587 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 588 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 589 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 590 | if (path != NULL) { |
| 591 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 592 | printf("LOOKUP %s\n", path); |
| 593 | fflush(stdout); |
| 594 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 595 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 596 | if (f->op.getattr) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 597 | err = lookup_path(f, parent, name, path, &e); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 598 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 599 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 600 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 601 | reply_entry(req, &e, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 602 | } |
| 603 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 604 | static void fuse_forget(fuse_req_t req, fuse_ino_t ino, unsigned long nlookup) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 605 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 606 | struct fuse *f = req_fuse(req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 607 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 608 | printf("FORGET %lu/%lu\n", ino, nlookup); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 609 | fflush(stdout); |
| 610 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 611 | forget_node(f, ino, nlookup); |
| 612 | fuse_reply_none(req); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 615 | static void fuse_getattr(fuse_req_t req, fuse_ino_t ino, |
| 616 | struct fuse_file_info *fi) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 617 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 618 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 619 | struct stat buf; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 620 | char *path; |
| 621 | int err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 622 | |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 623 | (void) fi; |
| 624 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 625 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 626 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 627 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 628 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 629 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 630 | if (f->op.getattr) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 631 | err = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 632 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 633 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 634 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 635 | if (!err) { |
| 636 | set_stat(f, ino, &buf); |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 637 | fuse_reply_attr(req, &buf, f->attr_timeout); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 638 | } else |
| 639 | reply_err(req, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 640 | } |
| 641 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 642 | static int do_chmod(struct fuse *f, const char *path, struct stat *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 643 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 644 | int err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 645 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 646 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 647 | if (f->op.chmod) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 648 | err = f->op.chmod(path, attr->st_mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 649 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 650 | return err; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 651 | } |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 652 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 653 | static int do_chown(struct fuse *f, const char *path, struct stat *attr, |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 654 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 655 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 656 | int err; |
| 657 | uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t) -1; |
| 658 | gid_t gid = (valid & FUSE_SET_ATTR_GID) ? attr->st_gid : (gid_t) -1; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 659 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 660 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 661 | if (f->op.chown) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 662 | err = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 663 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 664 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 665 | } |
| 666 | |
Miklos Szeredi | 5e3a738 | 2005-09-08 14:40:16 +0000 | [diff] [blame^] | 667 | static int do_truncate(struct fuse *f, const char *path, struct stat *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 668 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 669 | int err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 670 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 671 | err = -ENOSYS; |
Miklos Szeredi | 5e3a738 | 2005-09-08 14:40:16 +0000 | [diff] [blame^] | 672 | if (f->op.truncate) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 673 | err = f->op.truncate(path, attr->st_size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 674 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 675 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 676 | } |
| 677 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 678 | static int do_utime(struct fuse *f, const char *path, struct stat *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 679 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 680 | int err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 681 | struct utimbuf buf; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 682 | buf.actime = attr->st_atime; |
| 683 | buf.modtime = attr->st_mtime; |
| 684 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 685 | if (f->op.utime) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 686 | err = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 687 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 688 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 689 | } |
| 690 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 691 | static void fuse_setattr(fuse_req_t req, fuse_ino_t ino, struct stat *attr, |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 692 | int valid, struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 693 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 694 | struct fuse *f = req_fuse_prepare(req); |
| 695 | struct stat buf; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 696 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 697 | int err; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 698 | |
Miklos Szeredi | 5e3a738 | 2005-09-08 14:40:16 +0000 | [diff] [blame^] | 699 | (void) fi; |
| 700 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 701 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 702 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 703 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 704 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 705 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 706 | if (f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 707 | err = 0; |
| 708 | if (!err && (valid & FUSE_SET_ATTR_MODE)) |
| 709 | err = do_chmod(f, path, attr); |
| 710 | if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID))) |
| 711 | err = do_chown(f, path, attr, valid); |
| 712 | if (!err && (valid & FUSE_SET_ATTR_SIZE)) |
Miklos Szeredi | 5e3a738 | 2005-09-08 14:40:16 +0000 | [diff] [blame^] | 713 | err = do_truncate(f, path, attr); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 714 | if (!err && (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) == (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) |
| 715 | err = do_utime(f, path, attr); |
| 716 | if (!err) |
| 717 | err = f->op.getattr(path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 718 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 719 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 720 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 721 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 722 | if (!err) { |
| 723 | set_stat(f, ino, &buf); |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 724 | fuse_reply_attr(req, &buf, f->attr_timeout); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 725 | } else |
| 726 | reply_err(req, err); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 727 | } |
| 728 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 729 | static void fuse_readlink(fuse_req_t req, fuse_ino_t ino) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 730 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 731 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 732 | char linkname[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 733 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 734 | int err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 735 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 736 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 737 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 738 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 739 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 740 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 741 | if (f->op.readlink) |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 742 | err = f->op.readlink(path, linkname, sizeof(linkname)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 743 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 744 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 745 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 746 | if (!err) { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 747 | linkname[PATH_MAX] = '\0'; |
| 748 | fuse_reply_readlink(req, linkname); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 749 | } else |
| 750 | reply_err(req, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 753 | static void fuse_mknod(fuse_req_t req, fuse_ino_t parent, const char *name, |
| 754 | mode_t mode, dev_t rdev) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 755 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 756 | struct fuse *f = req_fuse_prepare(req); |
| 757 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 758 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 759 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 760 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 761 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 762 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 763 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 764 | if (path != NULL) { |
| 765 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 766 | printf("MKNOD %s\n", path); |
| 767 | fflush(stdout); |
| 768 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 769 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 770 | if (f->op.mknod && f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 771 | err = f->op.mknod(path, mode, rdev); |
| 772 | if (!err) |
| 773 | err = lookup_path(f, parent, name, path, &e); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 774 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 775 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 776 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 777 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 778 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 779 | } |
| 780 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 781 | static void fuse_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name, |
| 782 | mode_t mode) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 783 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 784 | struct fuse *f = req_fuse_prepare(req); |
| 785 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 786 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 787 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 788 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 789 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 790 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 791 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 792 | if (path != NULL) { |
| 793 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 794 | printf("MKDIR %s\n", path); |
| 795 | fflush(stdout); |
| 796 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 797 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 798 | if (f->op.mkdir && f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 799 | err = f->op.mkdir(path, mode); |
| 800 | if (!err) |
| 801 | err = lookup_path(f, parent, name, path, &e); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 802 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 803 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 804 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 805 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 806 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 807 | } |
| 808 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 809 | static void fuse_unlink(fuse_req_t req, fuse_ino_t parent, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 810 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 811 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 812 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 813 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 814 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 815 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 816 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 817 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 818 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 819 | if (f->flags & FUSE_DEBUG) { |
| 820 | printf("UNLINK %s\n", path); |
| 821 | fflush(stdout); |
| 822 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 823 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 824 | if (f->op.unlink) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 825 | if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, parent, name)) |
| 826 | err = hide_node(f, path, parent, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 827 | else { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 828 | err = f->op.unlink(path); |
| 829 | if (!err) |
| 830 | remove_node(f, parent, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 831 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 832 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 833 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 834 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 835 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 836 | reply_err(req, err); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 837 | } |
| 838 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 839 | static void fuse_rmdir(fuse_req_t req, fuse_ino_t parent, const char *name) |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 840 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 841 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 842 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 843 | int err; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 844 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 845 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 846 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 847 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 848 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 849 | if (f->flags & FUSE_DEBUG) { |
| 850 | printf("RMDIR %s\n", path); |
| 851 | fflush(stdout); |
| 852 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 853 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 854 | if (f->op.rmdir) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 855 | err = f->op.rmdir(path); |
| 856 | if (!err) |
| 857 | remove_node(f, parent, name); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 858 | } |
| 859 | free(path); |
| 860 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 861 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 862 | reply_err(req, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 863 | } |
| 864 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 865 | static void fuse_symlink(fuse_req_t req, const char *linkname, |
| 866 | fuse_ino_t parent, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 867 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 868 | struct fuse *f = req_fuse_prepare(req); |
| 869 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 870 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 871 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 872 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 873 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 874 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 875 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 876 | if (path != NULL) { |
| 877 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 878 | printf("SYMLINK %s\n", path); |
| 879 | fflush(stdout); |
| 880 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 881 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 882 | if (f->op.symlink && f->op.getattr) { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 883 | err = f->op.symlink(linkname, path); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 884 | if (!err) |
| 885 | err = lookup_path(f, parent, name, path, &e); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 886 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 887 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 888 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 889 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 890 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 891 | } |
| 892 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 893 | static void fuse_rename(fuse_req_t req, fuse_ino_t olddir, const char *oldname, |
| 894 | fuse_ino_t newdir, const char *newname) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 895 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 896 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 897 | char *oldpath; |
| 898 | char *newpath; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 899 | int err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 900 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 901 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 902 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 903 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 904 | if (oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 905 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 906 | if (newpath != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 907 | if (f->flags & FUSE_DEBUG) { |
| 908 | printf("RENAME %s -> %s\n", oldpath, newpath); |
| 909 | fflush(stdout); |
| 910 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 911 | err = -ENOSYS; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 912 | if (f->op.rename) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 913 | err = 0; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 914 | if (!(f->flags & FUSE_HARD_REMOVE) && |
Miklos Szeredi | 2529ca2 | 2004-07-13 15:36:52 +0000 | [diff] [blame] | 915 | is_open(f, newdir, newname)) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 916 | err = hide_node(f, newpath, newdir, newname); |
| 917 | if (!err) { |
| 918 | err = f->op.rename(oldpath, newpath); |
| 919 | if (!err) |
| 920 | err = rename_node(f, olddir, oldname, newdir, newname, 0); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 921 | } |
| 922 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 923 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 924 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 925 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 926 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 927 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 928 | reply_err(req, err); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 929 | } |
| 930 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 931 | static void fuse_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, |
| 932 | const char *newname) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 933 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 934 | struct fuse *f = req_fuse_prepare(req); |
| 935 | struct fuse_entry_param e; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 936 | char *oldpath; |
| 937 | char *newpath; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 938 | int err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 939 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 940 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 941 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 942 | oldpath = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 943 | if (oldpath != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 944 | newpath = get_path_name(f, newparent, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 945 | if (newpath != NULL) { |
| 946 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 947 | printf("LINK %s\n", newpath); |
| 948 | fflush(stdout); |
| 949 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 950 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 951 | if (f->op.link && f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 952 | err = f->op.link(oldpath, newpath); |
| 953 | if (!err) |
| 954 | err = lookup_path(f, newparent, newname, newpath, &e); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 955 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 956 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 957 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 958 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 959 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 960 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 961 | reply_entry(req, &e, err); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 962 | } |
| 963 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 964 | static void fuse_open(fuse_req_t req, fuse_ino_t ino, |
| 965 | struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 966 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 967 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 31066bb | 2005-08-01 14:49:31 +0000 | [diff] [blame] | 968 | char *path = NULL; |
| 969 | int err = 0; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 970 | |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 971 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | 31066bb | 2005-08-01 14:49:31 +0000 | [diff] [blame] | 972 | if (f->op.open) { |
| 973 | err = -ENOENT; |
| 974 | path = get_path(f, ino); |
| 975 | if (path != NULL) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 976 | if (!f->compat) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 977 | err = f->op.open(path, fi); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 978 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 979 | err = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi->flags); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 980 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 981 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 982 | if (!err) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 983 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 984 | printf("OPEN[%lu] flags: 0x%x\n", fi->fh, fi->flags); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 985 | fflush(stdout); |
| 986 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 987 | |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 988 | if (f->flags & FUSE_DIRECT_IO) |
| 989 | fi->direct_io = 1; |
| 990 | if (f->flags & FUSE_KERNEL_CACHE) |
| 991 | fi->keep_cache = 1; |
| 992 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 993 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 994 | if (fuse_reply_open(req, fi) == -ENOENT) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 995 | /* The open syscall was interrupted, so it must be cancelled */ |
Miklos Szeredi | 31066bb | 2005-08-01 14:49:31 +0000 | [diff] [blame] | 996 | if(f->op.release && path != NULL) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 997 | if (!f->compat) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 998 | f->op.release(path, fi); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 999 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1000 | ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1001 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1002 | } else { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1003 | struct node *node = get_node(f, ino); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1004 | node->open_count ++; |
| 1005 | } |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1006 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1007 | } else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1008 | reply_err(req, err); |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1009 | |
Miklos Szeredi | 9a31cca | 2004-06-26 21:11:25 +0000 | [diff] [blame] | 1010 | if (path) |
| 1011 | free(path); |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1012 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1013 | } |
| 1014 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1015 | static void fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, |
| 1016 | struct fuse_file_info *fi) |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1017 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1018 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1019 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1020 | char *buf; |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1021 | int res; |
| 1022 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1023 | buf = (char *) malloc(size); |
| 1024 | if (buf == NULL) { |
| 1025 | reply_err(req, -ENOMEM); |
| 1026 | return; |
| 1027 | } |
| 1028 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1029 | res = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1030 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1031 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1032 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1033 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1034 | printf("READ[%lu] %u bytes from %llu\n", fi->fh, size, off); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1035 | fflush(stdout); |
| 1036 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1037 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1038 | res = -ENOSYS; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1039 | if (f->op.read) |
| 1040 | res = f->op.read(path, buf, size, off, fi); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1041 | free(path); |
| 1042 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1043 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1044 | |
| 1045 | if (res >= 0) { |
| 1046 | if (f->flags & FUSE_DEBUG) { |
| 1047 | printf(" READ[%lu] %u bytes\n", fi->fh, res); |
| 1048 | fflush(stdout); |
| 1049 | } |
| 1050 | fuse_reply_buf(req, buf, res); |
| 1051 | } else |
| 1052 | reply_err(req, res); |
| 1053 | |
| 1054 | free(buf); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1057 | static void fuse_write(fuse_req_t req, fuse_ino_t ino, const char *buf, |
| 1058 | size_t size, off_t off, struct fuse_file_info *fi) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1059 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1060 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1061 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1062 | int res; |
| 1063 | |
| 1064 | res = -ENOENT; |
| 1065 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1066 | path = get_path(f, ino); |
| 1067 | if (path != NULL) { |
| 1068 | if (f->flags & FUSE_DEBUG) { |
| 1069 | printf("WRITE%s[%lu] %u bytes to %llu\n", |
| 1070 | fi->writepage ? "PAGE" : "", fi->fh, size, off); |
| 1071 | fflush(stdout); |
| 1072 | } |
| 1073 | |
| 1074 | res = -ENOSYS; |
| 1075 | if (f->op.write) |
| 1076 | res = f->op.write(path, buf, size, off, fi); |
| 1077 | free(path); |
| 1078 | } |
| 1079 | pthread_rwlock_unlock(&f->tree_lock); |
| 1080 | |
| 1081 | if (res >= 0) |
| 1082 | fuse_reply_write(req, res); |
| 1083 | else |
| 1084 | reply_err(req, res); |
| 1085 | } |
| 1086 | |
| 1087 | static void fuse_flush(fuse_req_t req, fuse_ino_t ino, |
| 1088 | struct fuse_file_info *fi) |
| 1089 | { |
| 1090 | struct fuse *f = req_fuse_prepare(req); |
| 1091 | char *path; |
| 1092 | int err; |
| 1093 | |
| 1094 | err = -ENOENT; |
| 1095 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1096 | path = get_path(f, ino); |
| 1097 | if (path != NULL) { |
| 1098 | if (f->flags & FUSE_DEBUG) { |
| 1099 | printf("FLUSH[%lu]\n", fi->fh); |
| 1100 | fflush(stdout); |
| 1101 | } |
| 1102 | err = -ENOSYS; |
| 1103 | if (f->op.flush) |
| 1104 | err = f->op.flush(path, fi); |
| 1105 | free(path); |
| 1106 | } |
| 1107 | pthread_rwlock_unlock(&f->tree_lock); |
| 1108 | reply_err(req, err); |
| 1109 | } |
| 1110 | |
| 1111 | static void fuse_release(fuse_req_t req, fuse_ino_t ino, |
| 1112 | struct fuse_file_info *fi) |
| 1113 | { |
| 1114 | struct fuse *f = req_fuse_prepare(req); |
| 1115 | char *path; |
| 1116 | struct node *node; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1117 | int unlink_hidden; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1118 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1119 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1120 | node = get_node(f, ino); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1121 | assert(node->open_count > 0); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1122 | --node->open_count; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1123 | unlink_hidden = (node->is_hidden && !node->open_count); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1124 | pthread_mutex_unlock(&f->lock); |
| 1125 | |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1126 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1127 | path = get_path(f, ino); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1128 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1129 | printf("RELEASE[%lu] flags: 0x%x\n", fi->fh, fi->flags); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1130 | fflush(stdout); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1131 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1132 | if (f->op.release) { |
| 1133 | if (!f->compat) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1134 | f->op.release(path ? path : "-", fi); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1135 | else if (path) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1136 | ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1137 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1138 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1139 | if(unlink_hidden && path) |
| 1140 | f->op.unlink(path); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1141 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1142 | if (path) |
| 1143 | free(path); |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1144 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1145 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1146 | reply_err(req, 0); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1147 | } |
| 1148 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1149 | static void fuse_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, |
| 1150 | struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1151 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1152 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1153 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1154 | int err; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1155 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1156 | err = -ENOENT; |
| 1157 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1158 | path = get_path(f, ino); |
| 1159 | if (path != NULL) { |
| 1160 | if (f->flags & FUSE_DEBUG) { |
| 1161 | printf("FSYNC[%lu]\n", fi->fh); |
| 1162 | fflush(stdout); |
| 1163 | } |
| 1164 | err = -ENOSYS; |
| 1165 | if (f->op.fsync) |
| 1166 | err = f->op.fsync(path, datasync, fi); |
| 1167 | free(path); |
| 1168 | } |
| 1169 | pthread_rwlock_unlock(&f->tree_lock); |
| 1170 | reply_err(req, err); |
| 1171 | } |
| 1172 | |
| 1173 | static struct fuse_dirhandle *get_dirhandle(const struct fuse_file_info *llfi, |
| 1174 | struct fuse_file_info *fi) |
| 1175 | { |
| 1176 | struct fuse_dirhandle *dh = (struct fuse_dirhandle *) llfi->fh; |
| 1177 | memset(fi, 0, sizeof(struct fuse_file_info)); |
| 1178 | fi->fh = dh->fh; |
| 1179 | return dh; |
| 1180 | } |
| 1181 | |
| 1182 | static void fuse_opendir(fuse_req_t req, fuse_ino_t ino, |
| 1183 | struct fuse_file_info *llfi) |
| 1184 | { |
| 1185 | struct fuse *f = req_fuse_prepare(req); |
| 1186 | struct fuse_dirhandle *dh; |
| 1187 | |
| 1188 | dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle)); |
| 1189 | if (dh == NULL) { |
| 1190 | reply_err(req, -ENOMEM); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1191 | return; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1192 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1193 | memset(dh, 0, sizeof(struct fuse_dirhandle)); |
| 1194 | dh->fuse = f; |
| 1195 | dh->contents = NULL; |
| 1196 | dh->len = 0; |
| 1197 | dh->filled = 0; |
| 1198 | dh->nodeid = ino; |
| 1199 | mutex_init(&dh->lock); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1200 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1201 | llfi->fh = (unsigned long) dh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1202 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1203 | if (f->op.opendir) { |
| 1204 | struct fuse_file_info fi; |
| 1205 | char *path; |
| 1206 | int err; |
| 1207 | |
| 1208 | memset(&fi, 0, sizeof(fi)); |
| 1209 | fi.flags = llfi->flags; |
| 1210 | |
| 1211 | err = -ENOENT; |
| 1212 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1213 | path = get_path(f, ino); |
| 1214 | if (path != NULL) { |
| 1215 | err = f->op.opendir(path, &fi); |
| 1216 | dh->fh = fi.fh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1217 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1218 | if (!err) { |
| 1219 | pthread_mutex_lock(&f->lock); |
| 1220 | if (fuse_reply_open(req, llfi) == -ENOENT) { |
| 1221 | /* The opendir syscall was interrupted, so it must be |
| 1222 | cancelled */ |
| 1223 | if(f->op.releasedir) |
| 1224 | f->op.releasedir(path, &fi); |
| 1225 | pthread_mutex_destroy(&dh->lock); |
| 1226 | free(dh); |
| 1227 | } |
| 1228 | pthread_mutex_unlock(&f->lock); |
| 1229 | } else { |
| 1230 | reply_err(req, err); |
| 1231 | free(dh); |
| 1232 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1233 | free(path); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1234 | pthread_rwlock_unlock(&f->tree_lock); |
| 1235 | } else |
| 1236 | fuse_reply_open(req, llfi); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1237 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1238 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1239 | static int fill_dir_common(struct fuse_dirhandle *dh, const char *name, |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1240 | const struct stat *statp, off_t off) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1241 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1242 | struct stat stbuf; |
| 1243 | unsigned namelen = strlen(name); |
| 1244 | unsigned entsize; |
| 1245 | unsigned newlen; |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 1246 | char *newptr; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1247 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1248 | if (statp) |
| 1249 | stbuf = *statp; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1250 | else { |
| 1251 | memset(&stbuf, 0, sizeof(stbuf)); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1252 | stbuf.st_ino = (ino_t) -1; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1253 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1254 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1255 | if (!(dh->fuse->flags & FUSE_USE_INO)) { |
| 1256 | stbuf.st_ino = (ino_t) -1; |
| 1257 | if (dh->fuse->flags & FUSE_READDIR_INO) { |
| 1258 | struct node *node; |
| 1259 | pthread_mutex_lock(&dh->fuse->lock); |
| 1260 | node = lookup_node(dh->fuse, dh->nodeid, name); |
| 1261 | if (node) |
| 1262 | stbuf.st_ino = (ino_t) node->nodeid; |
| 1263 | pthread_mutex_unlock(&dh->fuse->lock); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1264 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1265 | } |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1266 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1267 | entsize = fuse_dirent_size(namelen); |
| 1268 | newlen = dh->len + entsize; |
| 1269 | |
| 1270 | if (off) { |
| 1271 | dh->filled = 0; |
| 1272 | if (newlen > dh->needlen) |
| 1273 | return 1; |
| 1274 | } |
| 1275 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1276 | newptr = (char *) realloc(dh->contents, newlen); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1277 | if (!newptr) { |
| 1278 | dh->error = -ENOMEM; |
| 1279 | return 1; |
| 1280 | } |
| 1281 | dh->contents = newptr; |
| 1282 | fuse_add_dirent(dh->contents + dh->len, name, &stbuf, off ? off : newlen); |
| 1283 | dh->len = newlen; |
| 1284 | return 0; |
| 1285 | } |
| 1286 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1287 | static int fill_dir(void *buf, const char *name, const struct stat *stbuf, |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1288 | off_t off) |
| 1289 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1290 | return fill_dir_common((struct fuse_dirhandle *) buf, name, stbuf, off); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
| 1293 | static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type, |
| 1294 | ino_t ino) |
| 1295 | { |
| 1296 | struct stat stbuf; |
| 1297 | |
| 1298 | memset(&stbuf, 0, sizeof(stbuf)); |
| 1299 | stbuf.st_mode = type << 12; |
| 1300 | stbuf.st_ino = ino; |
| 1301 | |
| 1302 | fill_dir_common(dh, name, &stbuf, 0); |
| 1303 | return dh->error; |
| 1304 | } |
| 1305 | |
| 1306 | static int readdir_fill(struct fuse *f, fuse_ino_t ino, size_t size, |
| 1307 | off_t off, struct fuse_dirhandle *dh, |
| 1308 | struct fuse_file_info *fi) |
| 1309 | { |
| 1310 | int err = -ENOENT; |
| 1311 | char *path; |
| 1312 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1313 | path = get_path(f, ino); |
| 1314 | if (path != NULL) { |
| 1315 | dh->len = 0; |
| 1316 | dh->error = 0; |
| 1317 | dh->needlen = size; |
| 1318 | dh->filled = 1; |
| 1319 | err = -ENOSYS; |
| 1320 | if (f->op.readdir) |
| 1321 | err = f->op.readdir(path, dh, fill_dir, off, fi); |
| 1322 | else if (f->op.getdir) |
| 1323 | err = f->op.getdir(path, dh, fill_dir_old); |
| 1324 | if (!err) |
| 1325 | err = dh->error; |
| 1326 | if (err) |
| 1327 | dh->filled = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1328 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1329 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1330 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1331 | return err; |
| 1332 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1333 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1334 | static void fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, |
| 1335 | off_t off, struct fuse_file_info *llfi) |
| 1336 | { |
| 1337 | struct fuse *f = req_fuse_prepare(req); |
| 1338 | struct fuse_file_info fi; |
| 1339 | struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi); |
| 1340 | |
| 1341 | pthread_mutex_lock(&dh->lock); |
Miklos Szeredi | 77ccf65 | 2005-08-19 14:40:27 +0000 | [diff] [blame] | 1342 | /* According to SUS, directory contents need to be refreshed on |
| 1343 | rewinddir() */ |
| 1344 | if (!off) |
| 1345 | dh->filled = 0; |
| 1346 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1347 | if (!dh->filled) { |
| 1348 | int err = readdir_fill(f, ino, size, off, dh, &fi); |
| 1349 | if (err) { |
| 1350 | reply_err(req, err); |
| 1351 | goto out; |
| 1352 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1353 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1354 | if (dh->filled) { |
| 1355 | if (off < dh->len) { |
| 1356 | if (off + size > dh->len) |
| 1357 | size = dh->len - off; |
| 1358 | } else |
| 1359 | size = 0; |
| 1360 | } else { |
| 1361 | size = dh->len; |
| 1362 | off = 0; |
| 1363 | } |
| 1364 | fuse_reply_buf(req, dh->contents + off, size); |
| 1365 | out: |
| 1366 | pthread_mutex_unlock(&dh->lock); |
| 1367 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1368 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1369 | static void fuse_releasedir(fuse_req_t req, fuse_ino_t ino, |
| 1370 | struct fuse_file_info *llfi) |
| 1371 | { |
| 1372 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 1373 | struct fuse_file_info fi; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1374 | struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi); |
| 1375 | if (f->op.releasedir) { |
| 1376 | char *path; |
| 1377 | |
| 1378 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1379 | path = get_path(f, ino); |
| 1380 | f->op.releasedir(path ? path : "-", &fi); |
| 1381 | free(path); |
| 1382 | pthread_rwlock_unlock(&f->tree_lock); |
| 1383 | } |
| 1384 | pthread_mutex_lock(&dh->lock); |
| 1385 | pthread_mutex_unlock(&dh->lock); |
| 1386 | pthread_mutex_destroy(&dh->lock); |
| 1387 | free(dh->contents); |
| 1388 | free(dh); |
| 1389 | reply_err(req, 0); |
| 1390 | } |
| 1391 | |
| 1392 | static void fuse_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, |
| 1393 | struct fuse_file_info *llfi) |
| 1394 | { |
| 1395 | struct fuse *f = req_fuse_prepare(req); |
| 1396 | struct fuse_file_info fi; |
| 1397 | char *path; |
| 1398 | int err; |
| 1399 | |
| 1400 | get_dirhandle(llfi, &fi); |
| 1401 | |
| 1402 | err = -ENOENT; |
| 1403 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1404 | path = get_path(f, ino); |
| 1405 | if (path != NULL) { |
| 1406 | err = -ENOSYS; |
| 1407 | if (f->op.fsyncdir) |
| 1408 | err = f->op.fsyncdir(path, datasync, &fi); |
| 1409 | free(path); |
| 1410 | } |
| 1411 | pthread_rwlock_unlock(&f->tree_lock); |
| 1412 | reply_err(req, err); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1413 | } |
| 1414 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1415 | static int default_statfs(struct statfs *buf) |
| 1416 | { |
| 1417 | buf->f_namelen = 255; |
| 1418 | buf->f_bsize = 512; |
| 1419 | return 0; |
| 1420 | } |
| 1421 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1422 | static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf, |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1423 | struct statfs *stbuf) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1424 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1425 | stbuf->f_bsize = compatbuf->block_size; |
| 1426 | stbuf->f_blocks = compatbuf->blocks; |
| 1427 | stbuf->f_bfree = compatbuf->blocks_free; |
| 1428 | stbuf->f_bavail = compatbuf->blocks_free; |
| 1429 | stbuf->f_files = compatbuf->files; |
| 1430 | stbuf->f_ffree = compatbuf->files_free; |
| 1431 | stbuf->f_namelen = compatbuf->namelen; |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1432 | } |
| 1433 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1434 | static void fuse_statfs(fuse_req_t req) |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1435 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1436 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1437 | struct statfs buf; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1438 | int err; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1439 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1440 | memset(&buf, 0, sizeof(struct statfs)); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1441 | if (f->op.statfs) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1442 | if (!f->compat || f->compat > 11) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1443 | err = f->op.statfs("/", &buf); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1444 | else { |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1445 | struct fuse_statfs_compat1 compatbuf; |
| 1446 | memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1)); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1447 | err = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf); |
| 1448 | if (!err) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1449 | convert_statfs_compat(&compatbuf, &buf); |
| 1450 | } |
| 1451 | } |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1452 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1453 | err = default_statfs(&buf); |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1454 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1455 | if (!err) |
| 1456 | fuse_reply_statfs(req, &buf); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1457 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1458 | reply_err(req, err); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1459 | } |
| 1460 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1461 | static void fuse_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, |
| 1462 | const char *value, size_t size, int flags) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1463 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1464 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1465 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1466 | int err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1467 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1468 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1469 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1470 | path = get_path(f, ino); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1471 | if (path != NULL) { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1472 | err = -ENOSYS; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1473 | if (f->op.setxattr) |
| 1474 | err = f->op.setxattr(path, name, value, size, flags); |
| 1475 | free(path); |
| 1476 | } |
| 1477 | pthread_rwlock_unlock(&f->tree_lock); |
| 1478 | reply_err(req, err); |
| 1479 | } |
| 1480 | |
| 1481 | static int common_getxattr(struct fuse *f, fuse_ino_t ino, const char *name, |
| 1482 | char *value, size_t size) |
| 1483 | { |
| 1484 | int err; |
| 1485 | char *path; |
| 1486 | |
| 1487 | err = -ENOENT; |
| 1488 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1489 | path = get_path(f, ino); |
| 1490 | if (path != NULL) { |
| 1491 | err = -ENOSYS; |
| 1492 | if (f->op.getxattr) |
| 1493 | err = f->op.getxattr(path, name, value, size); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1494 | free(path); |
| 1495 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1496 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1497 | return err; |
| 1498 | } |
| 1499 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1500 | static void fuse_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, |
| 1501 | size_t size) |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1502 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1503 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1504 | int res; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1505 | |
| 1506 | if (size) { |
| 1507 | char *value = (char *) malloc(size); |
| 1508 | if (value == NULL) { |
| 1509 | reply_err(req, -ENOMEM); |
| 1510 | return; |
| 1511 | } |
| 1512 | res = common_getxattr(f, ino, name, value, size); |
| 1513 | if (res > 0) |
| 1514 | fuse_reply_buf(req, value, res); |
| 1515 | else |
| 1516 | reply_err(req, res); |
| 1517 | free(value); |
| 1518 | } else { |
| 1519 | res = common_getxattr(f, ino, name, NULL, 0); |
| 1520 | if (res >= 0) |
| 1521 | fuse_reply_xattr(req, res); |
| 1522 | else |
| 1523 | reply_err(req, res); |
| 1524 | } |
| 1525 | } |
| 1526 | |
| 1527 | static int common_listxattr(struct fuse *f, fuse_ino_t ino, char *list, |
| 1528 | size_t size) |
| 1529 | { |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1530 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1531 | int err; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1532 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1533 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1534 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1535 | path = get_path(f, ino); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1536 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1537 | err = -ENOSYS; |
| 1538 | if (f->op.listxattr) |
| 1539 | err = f->op.listxattr(path, list, size); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1540 | free(path); |
| 1541 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1542 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1543 | return err; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1544 | } |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1545 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1546 | static void fuse_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1547 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1548 | struct fuse *f = req_fuse_prepare(req); |
| 1549 | int res; |
| 1550 | |
| 1551 | if (size) { |
| 1552 | char *list = (char *) malloc(size); |
| 1553 | if (list == NULL) { |
| 1554 | reply_err(req, -ENOMEM); |
| 1555 | return; |
| 1556 | } |
| 1557 | res = common_listxattr(f, ino, list, size); |
| 1558 | if (res > 0) |
| 1559 | fuse_reply_buf(req, list, res); |
| 1560 | else |
| 1561 | reply_err(req, res); |
| 1562 | free(list); |
| 1563 | } else { |
| 1564 | res = common_listxattr(f, ino, NULL, 0); |
| 1565 | if (res >= 0) |
| 1566 | fuse_reply_xattr(req, res); |
| 1567 | else |
| 1568 | reply_err(req, res); |
| 1569 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1570 | } |
| 1571 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1572 | static void fuse_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name) |
| 1573 | { |
| 1574 | struct fuse *f = req_fuse_prepare(req); |
| 1575 | char *path; |
| 1576 | int err; |
| 1577 | |
| 1578 | err = -ENOENT; |
| 1579 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1580 | path = get_path(f, ino); |
| 1581 | if (path != NULL) { |
| 1582 | err = -ENOSYS; |
| 1583 | if (f->op.removexattr) |
| 1584 | err = f->op.removexattr(path, name); |
| 1585 | free(path); |
| 1586 | } |
| 1587 | pthread_rwlock_unlock(&f->tree_lock); |
| 1588 | reply_err(req, err); |
| 1589 | } |
| 1590 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1591 | static struct fuse_lowlevel_ops fuse_path_ops = { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1592 | .init = fuse_data_init, |
| 1593 | .destroy = fuse_data_destroy, |
| 1594 | .lookup = fuse_lookup, |
| 1595 | .forget = fuse_forget, |
| 1596 | .getattr = fuse_getattr, |
| 1597 | .setattr = fuse_setattr, |
| 1598 | .readlink = fuse_readlink, |
| 1599 | .mknod = fuse_mknod, |
| 1600 | .mkdir = fuse_mkdir, |
| 1601 | .unlink = fuse_unlink, |
| 1602 | .rmdir = fuse_rmdir, |
| 1603 | .symlink = fuse_symlink, |
| 1604 | .rename = fuse_rename, |
| 1605 | .link = fuse_link, |
| 1606 | .open = fuse_open, |
| 1607 | .read = fuse_read, |
| 1608 | .write = fuse_write, |
| 1609 | .flush = fuse_flush, |
| 1610 | .release = fuse_release, |
| 1611 | .fsync = fuse_fsync, |
| 1612 | .opendir = fuse_opendir, |
| 1613 | .readdir = fuse_readdir, |
| 1614 | .releasedir = fuse_releasedir, |
| 1615 | .fsyncdir = fuse_fsyncdir, |
| 1616 | .statfs = fuse_statfs, |
| 1617 | .setxattr = fuse_setxattr, |
| 1618 | .getxattr = fuse_getxattr, |
| 1619 | .listxattr = fuse_listxattr, |
| 1620 | .removexattr = fuse_removexattr, |
| 1621 | }; |
| 1622 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1623 | static void free_cmd(struct fuse_cmd *cmd) |
| 1624 | { |
| 1625 | free(cmd->buf); |
| 1626 | free(cmd); |
| 1627 | } |
| 1628 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1629 | void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1630 | { |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 1631 | fuse_session_process(f->se, cmd->buf, cmd->buflen, cmd->ch); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1632 | } |
| 1633 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1634 | int fuse_exited(struct fuse *f) |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1635 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1636 | return fuse_session_exited(f->se); |
| 1637 | } |
| 1638 | |
| 1639 | struct fuse_session *fuse_get_session(struct fuse *f) |
| 1640 | { |
| 1641 | return f->se; |
| 1642 | } |
| 1643 | |
| 1644 | static struct fuse_cmd *fuse_alloc_cmd(size_t bufsize) |
| 1645 | { |
| 1646 | struct fuse_cmd *cmd = (struct fuse_cmd *) malloc(sizeof(*cmd)); |
| 1647 | if (cmd == NULL) { |
| 1648 | fprintf(stderr, "fuse: failed to allocate cmd\n"); |
| 1649 | return NULL; |
| 1650 | } |
| 1651 | cmd->buf = (char *) malloc(bufsize); |
| 1652 | if (cmd->buf == NULL) { |
| 1653 | fprintf(stderr, "fuse: failed to allocate read buffer\n"); |
| 1654 | free(cmd); |
| 1655 | return NULL; |
| 1656 | } |
| 1657 | return cmd; |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1658 | } |
| 1659 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1660 | struct fuse_cmd *fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1661 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1662 | struct fuse_chan *ch = fuse_session_next_chan(f->se, NULL); |
| 1663 | size_t bufsize = fuse_chan_bufsize(ch); |
| 1664 | struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize); |
| 1665 | if (cmd != NULL) { |
| 1666 | int res = fuse_chan_receive(ch, cmd->buf, bufsize); |
| 1667 | if (res <= 0) { |
| 1668 | free_cmd(cmd); |
| 1669 | return NULL; |
| 1670 | } |
| 1671 | cmd->buflen = res; |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 1672 | cmd->ch = ch; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1673 | } |
| 1674 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1677 | int fuse_loop(struct fuse *f) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1678 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1679 | if (f) |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1680 | return fuse_session_loop(f->se); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1681 | else |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1682 | return -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1683 | } |
| 1684 | |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1685 | int fuse_invalidate(struct fuse *f, const char *path) |
| 1686 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1687 | (void) f; |
| 1688 | (void) path; |
| 1689 | return -EINVAL; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1690 | } |
| 1691 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1692 | void fuse_exit(struct fuse *f) |
| 1693 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1694 | fuse_session_exit(f->se); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1695 | } |
| 1696 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1697 | struct fuse_context *fuse_get_context() |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1698 | { |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1699 | static struct fuse_context context; |
| 1700 | if (fuse_getcontext) |
| 1701 | return fuse_getcontext(); |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1702 | else |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1703 | return &context; |
| 1704 | } |
| 1705 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1706 | void fuse_set_getcontext_func(struct fuse_context *(*func)(void)) |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1707 | { |
| 1708 | fuse_getcontext = func; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1709 | } |
| 1710 | |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 1711 | static int begins_with(const char *s, const char *beg) |
| 1712 | { |
| 1713 | if (strncmp(s, beg, strlen(beg)) == 0) |
| 1714 | return 1; |
| 1715 | else |
| 1716 | return 0; |
| 1717 | } |
| 1718 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1719 | int fuse_is_lib_option(const char *opt) |
| 1720 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1721 | if (fuse_lowlevel_is_lib_option(opt) || |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1722 | strcmp(opt, "debug") == 0 || |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1723 | strcmp(opt, "hard_remove") == 0 || |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 1724 | strcmp(opt, "use_ino") == 0 || |
Miklos Szeredi | 33be22d | 2005-05-27 09:12:43 +0000 | [diff] [blame] | 1725 | strcmp(opt, "allow_root") == 0 || |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 1726 | strcmp(opt, "readdir_ino") == 0 || |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 1727 | strcmp(opt, "direct_io") == 0 || |
| 1728 | strcmp(opt, "kernel_cache") == 0 || |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 1729 | begins_with(opt, "umask=") || |
| 1730 | begins_with(opt, "uid=") || |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 1731 | begins_with(opt, "gid=") || |
| 1732 | begins_with(opt, "entry_timeout=") || |
| 1733 | begins_with(opt, "attr_timeout=")) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1734 | return 1; |
| 1735 | else |
| 1736 | return 0; |
| 1737 | } |
| 1738 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1739 | static int parse_lib_opts(struct fuse *f, const char *opts, char **llopts) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1740 | { |
| 1741 | if (opts) { |
| 1742 | char *xopts = strdup(opts); |
| 1743 | char *s = xopts; |
| 1744 | char *opt; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1745 | char *d = xopts; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1746 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1747 | if (xopts == NULL) { |
| 1748 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1749 | return -1; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1750 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1751 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1752 | while((opt = strsep(&s, ","))) { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1753 | if (fuse_lowlevel_is_lib_option(opt)) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1754 | size_t optlen = strlen(opt); |
| 1755 | if (strcmp(opt, "debug") == 0) |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 1756 | f->flags |= FUSE_DEBUG; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1757 | memmove(d, opt, optlen); |
| 1758 | d += optlen; |
| 1759 | *d++ = ','; |
| 1760 | } else if (strcmp(opt, "hard_remove") == 0) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1761 | f->flags |= FUSE_HARD_REMOVE; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1762 | else if (strcmp(opt, "use_ino") == 0) |
| 1763 | f->flags |= FUSE_USE_INO; |
Miklos Szeredi | 33be22d | 2005-05-27 09:12:43 +0000 | [diff] [blame] | 1764 | else if (strcmp(opt, "readdir_ino") == 0) |
| 1765 | f->flags |= FUSE_READDIR_INO; |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 1766 | else if (strcmp(opt, "direct_io") == 0) |
| 1767 | f->flags |= FUSE_DIRECT_IO; |
| 1768 | else if (strcmp(opt, "kernel_cache") == 0) |
| 1769 | f->flags |= FUSE_KERNEL_CACHE; |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 1770 | else if (sscanf(opt, "umask=%o", &f->umask) == 1) |
| 1771 | f->flags |= FUSE_SET_MODE; |
| 1772 | else if (sscanf(opt, "uid=%u", &f->uid) == 1) |
| 1773 | f->flags |= FUSE_SET_UID; |
| 1774 | else if(sscanf(opt, "gid=%u", &f->gid) == 1) |
| 1775 | f->flags |= FUSE_SET_GID; |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 1776 | else if (sscanf(opt, "entry_timeout=%lf", &f->entry_timeout) == 1) |
| 1777 | /* nop */; |
| 1778 | else if (sscanf(opt, "attr_timeout=%lf", &f->attr_timeout) == 1) |
| 1779 | /* nop */; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1780 | else |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1781 | fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt); |
| 1782 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1783 | if (d != xopts) { |
| 1784 | d[-1] = '\0'; |
| 1785 | *llopts = xopts; |
| 1786 | } |
| 1787 | else |
| 1788 | free(xopts); |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1789 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1790 | return 0; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1791 | } |
| 1792 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1793 | struct fuse *fuse_new_common(int fd, const char *opts, |
| 1794 | const struct fuse_operations *op, |
| 1795 | size_t op_size, int compat) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1796 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1797 | struct fuse_chan *ch; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1798 | struct fuse *f; |
| 1799 | struct node *root; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1800 | char *llopts = NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1801 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1802 | if (sizeof(struct fuse_operations) < op_size) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1803 | 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] | 1804 | op_size = sizeof(struct fuse_operations); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1805 | } |
| 1806 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1807 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1808 | if (f == NULL) { |
| 1809 | fprintf(stderr, "fuse: failed to allocate fuse object\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1810 | goto out; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1811 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1812 | |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 1813 | f->entry_timeout = 1.0; |
| 1814 | f->attr_timeout = 1.0; |
| 1815 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1816 | if (parse_lib_opts(f, opts, &llopts) == -1) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1817 | goto out_free; |
| 1818 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1819 | f->se = fuse_lowlevel_new(llopts, &fuse_path_ops, sizeof(fuse_path_ops), f); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1820 | free(llopts); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1821 | if (f->se == NULL) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1822 | goto out_free; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1823 | |
| 1824 | ch = fuse_kern_chan_new(fd); |
| 1825 | if (ch == NULL) |
| 1826 | goto out_free_session; |
| 1827 | |
| 1828 | fuse_session_add_chan(f->se, ch); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1829 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1830 | f->ctr = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1831 | f->generation = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1832 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1833 | f->name_table_size = 14057; |
| 1834 | f->name_table = (struct node **) |
| 1835 | calloc(1, sizeof(struct node *) * f->name_table_size); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1836 | if (f->name_table == NULL) { |
| 1837 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1838 | goto out_free_session; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1839 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1840 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1841 | f->id_table_size = 14057; |
| 1842 | f->id_table = (struct node **) |
| 1843 | calloc(1, sizeof(struct node *) * f->id_table_size); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1844 | if (f->id_table == NULL) { |
| 1845 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1846 | goto out_free_name_table; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1847 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1848 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1849 | mutex_init(&f->lock); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1850 | memcpy(&f->op, op, op_size); |
| 1851 | f->compat = compat; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1852 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1853 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1854 | if (root == NULL) { |
| 1855 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1856 | goto out_free_id_table; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1857 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1858 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1859 | root->name = strdup("/"); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1860 | if (root->name == NULL) { |
| 1861 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1862 | goto out_free_root; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1863 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1864 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1865 | root->parent = 0; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1866 | root->nodeid = FUSE_ROOT_ID; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1867 | root->generation = 0; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1868 | root->refctr = 1; |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 1869 | root->nlookup = 1; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1870 | hash_id(f, root); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1871 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1872 | return f; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1873 | |
| 1874 | out_free_root: |
| 1875 | free(root); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1876 | out_free_id_table: |
| 1877 | free(f->id_table); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1878 | out_free_name_table: |
| 1879 | free(f->name_table); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1880 | out_free_session: |
| 1881 | fuse_session_destroy(f->se); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1882 | out_free: |
| 1883 | free(f); |
| 1884 | out: |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1885 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1886 | } |
| 1887 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1888 | struct fuse *fuse_new(int fd, const char *opts, |
| 1889 | const struct fuse_operations *op, size_t op_size) |
| 1890 | { |
| 1891 | return fuse_new_common(fd, opts, op, op_size, 0); |
| 1892 | } |
| 1893 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1894 | struct fuse *fuse_new_compat2(int fd, const char *opts, |
| 1895 | const struct fuse_operations_compat2 *op) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1896 | { |
| 1897 | return fuse_new_common(fd, opts, (struct fuse_operations *) op, |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1898 | sizeof(struct fuse_operations_compat2), 21); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1899 | } |
| 1900 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1901 | struct fuse *fuse_new_compat1(int fd, int flags, |
| 1902 | const struct fuse_operations_compat1 *op) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1903 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1904 | const char *opts = NULL; |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1905 | if (flags & FUSE_DEBUG_COMPAT1) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1906 | opts = "debug"; |
| 1907 | return fuse_new_common(fd, opts, (struct fuse_operations *) op, |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1908 | sizeof(struct fuse_operations_compat1), 11); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1909 | } |
| 1910 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1911 | void fuse_destroy(struct fuse *f) |
| 1912 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1913 | size_t i; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1914 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1915 | struct node *node; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1916 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1917 | for (node = f->id_table[i]; node != NULL; node = node->id_next) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1918 | if (node->is_hidden) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1919 | char *path = get_path(f, node->nodeid); |
Miklos Szeredi | 21019c9 | 2005-05-09 11:22:41 +0000 | [diff] [blame] | 1920 | if (path) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1921 | f->op.unlink(path); |
Miklos Szeredi | 21019c9 | 2005-05-09 11:22:41 +0000 | [diff] [blame] | 1922 | free(path); |
| 1923 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1924 | } |
| 1925 | } |
| 1926 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1927 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1928 | struct node *node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1929 | struct node *next; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1930 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1931 | for (node = f->id_table[i]; node != NULL; node = next) { |
| 1932 | next = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1933 | free_node(node); |
| 1934 | } |
| 1935 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1936 | free(f->id_table); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1937 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1938 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1939 | fuse_session_destroy(f->se); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1940 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1941 | } |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1942 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1943 | __asm__(".symver fuse_exited,__fuse_exited@"); |
| 1944 | __asm__(".symver fuse_process_cmd,__fuse_process_cmd@"); |
| 1945 | __asm__(".symver fuse_read_cmd,__fuse_read_cmd@"); |
| 1946 | __asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@"); |
| 1947 | __asm__(".symver fuse_new_compat2,fuse_new@"); |