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 | |
Miklos Szeredi | b75d4b9 | 2005-10-11 10:12:08 +0000 | [diff] [blame^] | 31 | /** Enable debugging output */ |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 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; |
Miklos Szeredi | 4f5df5e | 2005-10-07 12:39:58 +0000 | [diff] [blame] | 567 | struct fuse_context *c = fuse_get_context(); |
| 568 | |
| 569 | memset(c, 0, sizeof(*c)); |
| 570 | c->fuse = f; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 571 | |
| 572 | if (f->op.init) |
| 573 | f->user_data = f->op.init(); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | static void fuse_data_destroy(void *data) |
| 577 | { |
| 578 | struct fuse *f = (struct fuse *) data; |
Miklos Szeredi | 4f5df5e | 2005-10-07 12:39:58 +0000 | [diff] [blame] | 579 | struct fuse_context *c = fuse_get_context(); |
| 580 | |
| 581 | memset(c, 0, sizeof(*c)); |
| 582 | c->fuse = f; |
| 583 | c->private_data = f->user_data; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 584 | |
| 585 | if (f->op.destroy) |
| 586 | f->op.destroy(f->user_data); |
| 587 | } |
| 588 | |
| 589 | static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name) |
| 590 | { |
| 591 | struct fuse *f = req_fuse_prepare(req); |
| 592 | struct fuse_entry_param e; |
| 593 | char *path; |
| 594 | int err; |
| 595 | |
| 596 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 597 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 598 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 599 | if (path != NULL) { |
| 600 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 601 | printf("LOOKUP %s\n", path); |
| 602 | fflush(stdout); |
| 603 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 604 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 605 | if (f->op.getattr) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 606 | err = lookup_path(f, parent, name, path, &e); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 607 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 608 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 609 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 610 | reply_entry(req, &e, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 613 | 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] | 614 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 615 | struct fuse *f = req_fuse(req); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 616 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 617 | printf("FORGET %lu/%lu\n", ino, nlookup); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 618 | fflush(stdout); |
| 619 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 620 | forget_node(f, ino, nlookup); |
| 621 | fuse_reply_none(req); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 622 | } |
| 623 | |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 624 | static void fuse_getattr(fuse_req_t req, fuse_ino_t ino, |
| 625 | struct fuse_file_info *fi) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 626 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 627 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 628 | struct stat buf; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 629 | char *path; |
| 630 | int err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 631 | |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 632 | (void) fi; |
| 633 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 634 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 635 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 636 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 637 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 638 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 639 | if (f->op.getattr) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 640 | err = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 641 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 642 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 643 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 644 | if (!err) { |
| 645 | set_stat(f, ino, &buf); |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 646 | fuse_reply_attr(req, &buf, f->attr_timeout); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 647 | } else |
| 648 | reply_err(req, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 649 | } |
| 650 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 651 | 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] | 652 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 653 | int err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 654 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 655 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 656 | if (f->op.chmod) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 657 | err = f->op.chmod(path, attr->st_mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 658 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 659 | return err; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 660 | } |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 661 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 662 | 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] | 663 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 664 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 665 | int err; |
| 666 | uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t) -1; |
| 667 | 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] | 668 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 669 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 670 | if (f->op.chown) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 671 | err = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 672 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 673 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 674 | } |
| 675 | |
Miklos Szeredi | 5e3a738 | 2005-09-08 14:40:16 +0000 | [diff] [blame] | 676 | 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] | 677 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 678 | int err; |
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 | err = -ENOSYS; |
Miklos Szeredi | 5e3a738 | 2005-09-08 14:40:16 +0000 | [diff] [blame] | 681 | if (f->op.truncate) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 682 | err = f->op.truncate(path, attr->st_size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 683 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 684 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 685 | } |
| 686 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 687 | 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] | 688 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 689 | int err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 690 | struct utimbuf buf; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 691 | buf.actime = attr->st_atime; |
| 692 | buf.modtime = attr->st_mtime; |
| 693 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 694 | if (f->op.utime) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 695 | err = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 696 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 697 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 698 | } |
| 699 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 700 | 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] | 701 | int valid, struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 702 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 703 | struct fuse *f = req_fuse_prepare(req); |
| 704 | struct stat buf; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 705 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 706 | int err; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 707 | |
Miklos Szeredi | 5e3a738 | 2005-09-08 14:40:16 +0000 | [diff] [blame] | 708 | (void) fi; |
| 709 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 710 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 711 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 712 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 713 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 714 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 715 | if (f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 716 | err = 0; |
| 717 | if (!err && (valid & FUSE_SET_ATTR_MODE)) |
| 718 | err = do_chmod(f, path, attr); |
| 719 | if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID))) |
| 720 | err = do_chown(f, path, attr, valid); |
| 721 | if (!err && (valid & FUSE_SET_ATTR_SIZE)) |
Miklos Szeredi | 5e3a738 | 2005-09-08 14:40:16 +0000 | [diff] [blame] | 722 | err = do_truncate(f, path, attr); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 723 | if (!err && (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) == (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) |
| 724 | err = do_utime(f, path, attr); |
| 725 | if (!err) |
| 726 | err = f->op.getattr(path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 727 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 728 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 729 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 730 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 731 | if (!err) { |
| 732 | set_stat(f, ino, &buf); |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 733 | fuse_reply_attr(req, &buf, f->attr_timeout); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 734 | } else |
| 735 | reply_err(req, err); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 736 | } |
| 737 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 738 | static void fuse_readlink(fuse_req_t req, fuse_ino_t ino) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 739 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 740 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 741 | char linkname[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 742 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 743 | int err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 744 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 745 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 746 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 747 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 748 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 749 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 750 | if (f->op.readlink) |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 751 | err = f->op.readlink(path, linkname, sizeof(linkname)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 752 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 753 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 754 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 755 | if (!err) { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 756 | linkname[PATH_MAX] = '\0'; |
| 757 | fuse_reply_readlink(req, linkname); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 758 | } else |
| 759 | reply_err(req, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 760 | } |
| 761 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 762 | static void fuse_mknod(fuse_req_t req, fuse_ino_t parent, const char *name, |
| 763 | mode_t mode, dev_t rdev) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 764 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 765 | struct fuse *f = req_fuse_prepare(req); |
| 766 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 767 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 768 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 769 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 770 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 771 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 772 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 773 | if (path != NULL) { |
| 774 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 775 | printf("MKNOD %s\n", path); |
| 776 | fflush(stdout); |
| 777 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 778 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 779 | if (f->op.mknod && f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 780 | err = f->op.mknod(path, mode, rdev); |
| 781 | if (!err) |
| 782 | err = lookup_path(f, parent, name, path, &e); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 783 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 784 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 785 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 786 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 787 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 788 | } |
| 789 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 790 | static void fuse_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name, |
| 791 | mode_t mode) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 792 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 793 | struct fuse *f = req_fuse_prepare(req); |
| 794 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 795 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 796 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 797 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 798 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 799 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 800 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 801 | if (path != NULL) { |
| 802 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 803 | printf("MKDIR %s\n", path); |
| 804 | fflush(stdout); |
| 805 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 806 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 807 | if (f->op.mkdir && f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 808 | err = f->op.mkdir(path, mode); |
| 809 | if (!err) |
| 810 | err = lookup_path(f, parent, name, path, &e); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 811 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 812 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 813 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 814 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 815 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 816 | } |
| 817 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 818 | 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] | 819 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 820 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 821 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 822 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 823 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 824 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 825 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 826 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 827 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 828 | if (f->flags & FUSE_DEBUG) { |
| 829 | printf("UNLINK %s\n", path); |
| 830 | fflush(stdout); |
| 831 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 832 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 833 | if (f->op.unlink) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 834 | if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, parent, name)) |
| 835 | err = hide_node(f, path, parent, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 836 | else { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 837 | err = f->op.unlink(path); |
| 838 | if (!err) |
| 839 | remove_node(f, parent, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 840 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 841 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 842 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 843 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 844 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 845 | reply_err(req, err); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 846 | } |
| 847 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 848 | 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] | 849 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 850 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 851 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 852 | int err; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 853 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 854 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 855 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 856 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 857 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 858 | if (f->flags & FUSE_DEBUG) { |
| 859 | printf("RMDIR %s\n", path); |
| 860 | fflush(stdout); |
| 861 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 862 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 863 | if (f->op.rmdir) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 864 | err = f->op.rmdir(path); |
| 865 | if (!err) |
| 866 | remove_node(f, parent, name); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 867 | } |
| 868 | free(path); |
| 869 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 870 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 871 | reply_err(req, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 872 | } |
| 873 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 874 | static void fuse_symlink(fuse_req_t req, const char *linkname, |
| 875 | fuse_ino_t parent, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 876 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 877 | struct fuse *f = req_fuse_prepare(req); |
| 878 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 879 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 880 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 881 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 882 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 883 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 884 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 885 | if (path != NULL) { |
| 886 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 887 | printf("SYMLINK %s\n", path); |
| 888 | fflush(stdout); |
| 889 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 890 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 891 | if (f->op.symlink && f->op.getattr) { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 892 | err = f->op.symlink(linkname, path); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 893 | if (!err) |
| 894 | err = lookup_path(f, parent, name, path, &e); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 895 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 896 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 897 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 898 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 899 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 900 | } |
| 901 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 902 | static void fuse_rename(fuse_req_t req, fuse_ino_t olddir, const char *oldname, |
| 903 | fuse_ino_t newdir, const char *newname) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 904 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 905 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 906 | char *oldpath; |
| 907 | char *newpath; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 908 | int err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 909 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 910 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 911 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 912 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 913 | if (oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 914 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 915 | if (newpath != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 916 | if (f->flags & FUSE_DEBUG) { |
| 917 | printf("RENAME %s -> %s\n", oldpath, newpath); |
| 918 | fflush(stdout); |
| 919 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 920 | err = -ENOSYS; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 921 | if (f->op.rename) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 922 | err = 0; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 923 | if (!(f->flags & FUSE_HARD_REMOVE) && |
Miklos Szeredi | 2529ca2 | 2004-07-13 15:36:52 +0000 | [diff] [blame] | 924 | is_open(f, newdir, newname)) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 925 | err = hide_node(f, newpath, newdir, newname); |
| 926 | if (!err) { |
| 927 | err = f->op.rename(oldpath, newpath); |
| 928 | if (!err) |
| 929 | err = rename_node(f, olddir, oldname, newdir, newname, 0); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 930 | } |
| 931 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 932 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 933 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 934 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 935 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 936 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 937 | reply_err(req, err); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 938 | } |
| 939 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 940 | static void fuse_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, |
| 941 | const char *newname) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 942 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 943 | struct fuse *f = req_fuse_prepare(req); |
| 944 | struct fuse_entry_param e; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 945 | char *oldpath; |
| 946 | char *newpath; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 947 | int err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 948 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 949 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 950 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 951 | oldpath = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 952 | if (oldpath != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 953 | newpath = get_path_name(f, newparent, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 954 | if (newpath != NULL) { |
| 955 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 956 | printf("LINK %s\n", newpath); |
| 957 | fflush(stdout); |
| 958 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 959 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 960 | if (f->op.link && f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 961 | err = f->op.link(oldpath, newpath); |
| 962 | if (!err) |
| 963 | err = lookup_path(f, newparent, newname, newpath, &e); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 964 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 965 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 966 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 967 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 968 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 969 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 970 | reply_entry(req, &e, err); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 971 | } |
| 972 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 973 | static void fuse_open(fuse_req_t req, fuse_ino_t ino, |
| 974 | struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 975 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 976 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 31066bb | 2005-08-01 14:49:31 +0000 | [diff] [blame] | 977 | char *path = NULL; |
| 978 | int err = 0; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 979 | |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 980 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | 31066bb | 2005-08-01 14:49:31 +0000 | [diff] [blame] | 981 | if (f->op.open) { |
| 982 | err = -ENOENT; |
| 983 | path = get_path(f, ino); |
| 984 | if (path != NULL) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 985 | if (!f->compat) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 986 | err = f->op.open(path, fi); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 987 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 988 | err = ((struct fuse_operations_compat2 *) &f->op)->open(path, fi->flags); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 989 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 990 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 991 | if (!err) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 992 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 993 | printf("OPEN[%lu] flags: 0x%x\n", fi->fh, fi->flags); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 994 | fflush(stdout); |
| 995 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 996 | |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 997 | if (f->flags & FUSE_DIRECT_IO) |
| 998 | fi->direct_io = 1; |
| 999 | if (f->flags & FUSE_KERNEL_CACHE) |
| 1000 | fi->keep_cache = 1; |
| 1001 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1002 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1003 | if (fuse_reply_open(req, fi) == -ENOENT) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1004 | /* The open syscall was interrupted, so it must be cancelled */ |
Miklos Szeredi | 31066bb | 2005-08-01 14:49:31 +0000 | [diff] [blame] | 1005 | if(f->op.release && path != NULL) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1006 | if (!f->compat) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1007 | f->op.release(path, fi); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1008 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1009 | ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1010 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1011 | } else { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1012 | struct node *node = get_node(f, ino); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1013 | node->open_count ++; |
| 1014 | } |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1015 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1016 | } else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1017 | reply_err(req, err); |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1018 | |
Miklos Szeredi | 9a31cca | 2004-06-26 21:11:25 +0000 | [diff] [blame] | 1019 | if (path) |
| 1020 | free(path); |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1021 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1022 | } |
| 1023 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1024 | static void fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, |
| 1025 | struct fuse_file_info *fi) |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1026 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1027 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1028 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1029 | char *buf; |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1030 | int res; |
| 1031 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1032 | buf = (char *) malloc(size); |
| 1033 | if (buf == NULL) { |
| 1034 | reply_err(req, -ENOMEM); |
| 1035 | return; |
| 1036 | } |
| 1037 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1038 | res = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1039 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1040 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1041 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1042 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1043 | printf("READ[%lu] %u bytes from %llu\n", fi->fh, size, off); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1044 | fflush(stdout); |
| 1045 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1046 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1047 | res = -ENOSYS; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1048 | if (f->op.read) |
| 1049 | res = f->op.read(path, buf, size, off, fi); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1050 | free(path); |
| 1051 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1052 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1053 | |
| 1054 | if (res >= 0) { |
| 1055 | if (f->flags & FUSE_DEBUG) { |
| 1056 | printf(" READ[%lu] %u bytes\n", fi->fh, res); |
| 1057 | fflush(stdout); |
| 1058 | } |
| 1059 | fuse_reply_buf(req, buf, res); |
| 1060 | } else |
| 1061 | reply_err(req, res); |
| 1062 | |
| 1063 | free(buf); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1064 | } |
| 1065 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1066 | static void fuse_write(fuse_req_t req, fuse_ino_t ino, const char *buf, |
| 1067 | size_t size, off_t off, struct fuse_file_info *fi) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1068 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1069 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1070 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1071 | int res; |
| 1072 | |
| 1073 | res = -ENOENT; |
| 1074 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1075 | path = get_path(f, ino); |
| 1076 | if (path != NULL) { |
| 1077 | if (f->flags & FUSE_DEBUG) { |
| 1078 | printf("WRITE%s[%lu] %u bytes to %llu\n", |
| 1079 | fi->writepage ? "PAGE" : "", fi->fh, size, off); |
| 1080 | fflush(stdout); |
| 1081 | } |
| 1082 | |
| 1083 | res = -ENOSYS; |
| 1084 | if (f->op.write) |
| 1085 | res = f->op.write(path, buf, size, off, fi); |
| 1086 | free(path); |
| 1087 | } |
| 1088 | pthread_rwlock_unlock(&f->tree_lock); |
| 1089 | |
| 1090 | if (res >= 0) |
| 1091 | fuse_reply_write(req, res); |
| 1092 | else |
| 1093 | reply_err(req, res); |
| 1094 | } |
| 1095 | |
| 1096 | static void fuse_flush(fuse_req_t req, fuse_ino_t ino, |
| 1097 | struct fuse_file_info *fi) |
| 1098 | { |
| 1099 | struct fuse *f = req_fuse_prepare(req); |
| 1100 | char *path; |
| 1101 | int err; |
| 1102 | |
| 1103 | err = -ENOENT; |
| 1104 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1105 | path = get_path(f, ino); |
| 1106 | if (path != NULL) { |
| 1107 | if (f->flags & FUSE_DEBUG) { |
| 1108 | printf("FLUSH[%lu]\n", fi->fh); |
| 1109 | fflush(stdout); |
| 1110 | } |
| 1111 | err = -ENOSYS; |
| 1112 | if (f->op.flush) |
| 1113 | err = f->op.flush(path, fi); |
| 1114 | free(path); |
| 1115 | } |
| 1116 | pthread_rwlock_unlock(&f->tree_lock); |
| 1117 | reply_err(req, err); |
| 1118 | } |
| 1119 | |
| 1120 | static void fuse_release(fuse_req_t req, fuse_ino_t ino, |
| 1121 | struct fuse_file_info *fi) |
| 1122 | { |
| 1123 | struct fuse *f = req_fuse_prepare(req); |
| 1124 | char *path; |
| 1125 | struct node *node; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1126 | int unlink_hidden; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1127 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1128 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1129 | node = get_node(f, ino); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1130 | assert(node->open_count > 0); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1131 | --node->open_count; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1132 | unlink_hidden = (node->is_hidden && !node->open_count); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1133 | pthread_mutex_unlock(&f->lock); |
| 1134 | |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1135 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1136 | path = get_path(f, ino); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1137 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1138 | printf("RELEASE[%lu] flags: 0x%x\n", fi->fh, fi->flags); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1139 | fflush(stdout); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1140 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1141 | if (f->op.release) { |
| 1142 | if (!f->compat) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1143 | f->op.release(path ? path : "-", fi); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1144 | else if (path) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1145 | ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1146 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1147 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1148 | if(unlink_hidden && path) |
| 1149 | f->op.unlink(path); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1150 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1151 | if (path) |
| 1152 | free(path); |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1153 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1154 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1155 | reply_err(req, 0); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1156 | } |
| 1157 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1158 | static void fuse_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, |
| 1159 | struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1160 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1161 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1162 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1163 | int err; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1164 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1165 | err = -ENOENT; |
| 1166 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1167 | path = get_path(f, ino); |
| 1168 | if (path != NULL) { |
| 1169 | if (f->flags & FUSE_DEBUG) { |
| 1170 | printf("FSYNC[%lu]\n", fi->fh); |
| 1171 | fflush(stdout); |
| 1172 | } |
| 1173 | err = -ENOSYS; |
| 1174 | if (f->op.fsync) |
| 1175 | err = f->op.fsync(path, datasync, fi); |
| 1176 | free(path); |
| 1177 | } |
| 1178 | pthread_rwlock_unlock(&f->tree_lock); |
| 1179 | reply_err(req, err); |
| 1180 | } |
| 1181 | |
| 1182 | static struct fuse_dirhandle *get_dirhandle(const struct fuse_file_info *llfi, |
| 1183 | struct fuse_file_info *fi) |
| 1184 | { |
| 1185 | struct fuse_dirhandle *dh = (struct fuse_dirhandle *) llfi->fh; |
| 1186 | memset(fi, 0, sizeof(struct fuse_file_info)); |
| 1187 | fi->fh = dh->fh; |
| 1188 | return dh; |
| 1189 | } |
| 1190 | |
| 1191 | static void fuse_opendir(fuse_req_t req, fuse_ino_t ino, |
| 1192 | struct fuse_file_info *llfi) |
| 1193 | { |
| 1194 | struct fuse *f = req_fuse_prepare(req); |
| 1195 | struct fuse_dirhandle *dh; |
| 1196 | |
| 1197 | dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle)); |
| 1198 | if (dh == NULL) { |
| 1199 | reply_err(req, -ENOMEM); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1200 | return; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1201 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1202 | memset(dh, 0, sizeof(struct fuse_dirhandle)); |
| 1203 | dh->fuse = f; |
| 1204 | dh->contents = NULL; |
| 1205 | dh->len = 0; |
| 1206 | dh->filled = 0; |
| 1207 | dh->nodeid = ino; |
| 1208 | mutex_init(&dh->lock); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1209 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1210 | llfi->fh = (unsigned long) dh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1211 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1212 | if (f->op.opendir) { |
| 1213 | struct fuse_file_info fi; |
| 1214 | char *path; |
| 1215 | int err; |
| 1216 | |
| 1217 | memset(&fi, 0, sizeof(fi)); |
| 1218 | fi.flags = llfi->flags; |
| 1219 | |
| 1220 | err = -ENOENT; |
| 1221 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1222 | path = get_path(f, ino); |
| 1223 | if (path != NULL) { |
| 1224 | err = f->op.opendir(path, &fi); |
| 1225 | dh->fh = fi.fh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1226 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1227 | if (!err) { |
| 1228 | pthread_mutex_lock(&f->lock); |
| 1229 | if (fuse_reply_open(req, llfi) == -ENOENT) { |
| 1230 | /* The opendir syscall was interrupted, so it must be |
| 1231 | cancelled */ |
| 1232 | if(f->op.releasedir) |
| 1233 | f->op.releasedir(path, &fi); |
| 1234 | pthread_mutex_destroy(&dh->lock); |
| 1235 | free(dh); |
| 1236 | } |
| 1237 | pthread_mutex_unlock(&f->lock); |
| 1238 | } else { |
| 1239 | reply_err(req, err); |
| 1240 | free(dh); |
| 1241 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1242 | free(path); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1243 | pthread_rwlock_unlock(&f->tree_lock); |
| 1244 | } else |
| 1245 | fuse_reply_open(req, llfi); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1246 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1247 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1248 | static int fill_dir_common(struct fuse_dirhandle *dh, const char *name, |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1249 | const struct stat *statp, off_t off) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1250 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1251 | struct stat stbuf; |
| 1252 | unsigned namelen = strlen(name); |
| 1253 | unsigned entsize; |
| 1254 | unsigned newlen; |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 1255 | char *newptr; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1256 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1257 | if (statp) |
| 1258 | stbuf = *statp; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1259 | else { |
| 1260 | memset(&stbuf, 0, sizeof(stbuf)); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1261 | stbuf.st_ino = (ino_t) -1; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1262 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1263 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1264 | if (!(dh->fuse->flags & FUSE_USE_INO)) { |
| 1265 | stbuf.st_ino = (ino_t) -1; |
| 1266 | if (dh->fuse->flags & FUSE_READDIR_INO) { |
| 1267 | struct node *node; |
| 1268 | pthread_mutex_lock(&dh->fuse->lock); |
| 1269 | node = lookup_node(dh->fuse, dh->nodeid, name); |
| 1270 | if (node) |
| 1271 | stbuf.st_ino = (ino_t) node->nodeid; |
| 1272 | pthread_mutex_unlock(&dh->fuse->lock); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1273 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1274 | } |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1275 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1276 | entsize = fuse_dirent_size(namelen); |
| 1277 | newlen = dh->len + entsize; |
| 1278 | |
| 1279 | if (off) { |
| 1280 | dh->filled = 0; |
| 1281 | if (newlen > dh->needlen) |
| 1282 | return 1; |
| 1283 | } |
| 1284 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1285 | newptr = (char *) realloc(dh->contents, newlen); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1286 | if (!newptr) { |
| 1287 | dh->error = -ENOMEM; |
| 1288 | return 1; |
| 1289 | } |
| 1290 | dh->contents = newptr; |
| 1291 | fuse_add_dirent(dh->contents + dh->len, name, &stbuf, off ? off : newlen); |
| 1292 | dh->len = newlen; |
| 1293 | return 0; |
| 1294 | } |
| 1295 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1296 | 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] | 1297 | off_t off) |
| 1298 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1299 | return fill_dir_common((struct fuse_dirhandle *) buf, name, stbuf, off); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1300 | } |
| 1301 | |
| 1302 | static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type, |
| 1303 | ino_t ino) |
| 1304 | { |
| 1305 | struct stat stbuf; |
| 1306 | |
| 1307 | memset(&stbuf, 0, sizeof(stbuf)); |
| 1308 | stbuf.st_mode = type << 12; |
| 1309 | stbuf.st_ino = ino; |
| 1310 | |
| 1311 | fill_dir_common(dh, name, &stbuf, 0); |
| 1312 | return dh->error; |
| 1313 | } |
| 1314 | |
| 1315 | static int readdir_fill(struct fuse *f, fuse_ino_t ino, size_t size, |
| 1316 | off_t off, struct fuse_dirhandle *dh, |
| 1317 | struct fuse_file_info *fi) |
| 1318 | { |
| 1319 | int err = -ENOENT; |
| 1320 | char *path; |
| 1321 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1322 | path = get_path(f, ino); |
| 1323 | if (path != NULL) { |
| 1324 | dh->len = 0; |
| 1325 | dh->error = 0; |
| 1326 | dh->needlen = size; |
| 1327 | dh->filled = 1; |
| 1328 | err = -ENOSYS; |
| 1329 | if (f->op.readdir) |
| 1330 | err = f->op.readdir(path, dh, fill_dir, off, fi); |
| 1331 | else if (f->op.getdir) |
| 1332 | err = f->op.getdir(path, dh, fill_dir_old); |
| 1333 | if (!err) |
| 1334 | err = dh->error; |
| 1335 | if (err) |
| 1336 | dh->filled = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1337 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1338 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1339 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1340 | return err; |
| 1341 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1342 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1343 | static void fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, |
| 1344 | off_t off, struct fuse_file_info *llfi) |
| 1345 | { |
| 1346 | struct fuse *f = req_fuse_prepare(req); |
| 1347 | struct fuse_file_info fi; |
| 1348 | struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi); |
| 1349 | |
| 1350 | pthread_mutex_lock(&dh->lock); |
Miklos Szeredi | 77ccf65 | 2005-08-19 14:40:27 +0000 | [diff] [blame] | 1351 | /* According to SUS, directory contents need to be refreshed on |
| 1352 | rewinddir() */ |
| 1353 | if (!off) |
| 1354 | dh->filled = 0; |
| 1355 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1356 | if (!dh->filled) { |
| 1357 | int err = readdir_fill(f, ino, size, off, dh, &fi); |
| 1358 | if (err) { |
| 1359 | reply_err(req, err); |
| 1360 | goto out; |
| 1361 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1362 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1363 | if (dh->filled) { |
| 1364 | if (off < dh->len) { |
| 1365 | if (off + size > dh->len) |
| 1366 | size = dh->len - off; |
| 1367 | } else |
| 1368 | size = 0; |
| 1369 | } else { |
| 1370 | size = dh->len; |
| 1371 | off = 0; |
| 1372 | } |
| 1373 | fuse_reply_buf(req, dh->contents + off, size); |
| 1374 | out: |
| 1375 | pthread_mutex_unlock(&dh->lock); |
| 1376 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1377 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1378 | static void fuse_releasedir(fuse_req_t req, fuse_ino_t ino, |
| 1379 | struct fuse_file_info *llfi) |
| 1380 | { |
| 1381 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 1382 | struct fuse_file_info fi; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1383 | struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi); |
| 1384 | if (f->op.releasedir) { |
| 1385 | char *path; |
| 1386 | |
| 1387 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1388 | path = get_path(f, ino); |
| 1389 | f->op.releasedir(path ? path : "-", &fi); |
| 1390 | free(path); |
| 1391 | pthread_rwlock_unlock(&f->tree_lock); |
| 1392 | } |
| 1393 | pthread_mutex_lock(&dh->lock); |
| 1394 | pthread_mutex_unlock(&dh->lock); |
| 1395 | pthread_mutex_destroy(&dh->lock); |
| 1396 | free(dh->contents); |
| 1397 | free(dh); |
| 1398 | reply_err(req, 0); |
| 1399 | } |
| 1400 | |
| 1401 | static void fuse_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, |
| 1402 | struct fuse_file_info *llfi) |
| 1403 | { |
| 1404 | struct fuse *f = req_fuse_prepare(req); |
| 1405 | struct fuse_file_info fi; |
| 1406 | char *path; |
| 1407 | int err; |
| 1408 | |
| 1409 | get_dirhandle(llfi, &fi); |
| 1410 | |
| 1411 | err = -ENOENT; |
| 1412 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1413 | path = get_path(f, ino); |
| 1414 | if (path != NULL) { |
| 1415 | err = -ENOSYS; |
| 1416 | if (f->op.fsyncdir) |
| 1417 | err = f->op.fsyncdir(path, datasync, &fi); |
| 1418 | free(path); |
| 1419 | } |
| 1420 | pthread_rwlock_unlock(&f->tree_lock); |
| 1421 | reply_err(req, err); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1422 | } |
| 1423 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1424 | static int default_statfs(struct statfs *buf) |
| 1425 | { |
| 1426 | buf->f_namelen = 255; |
| 1427 | buf->f_bsize = 512; |
| 1428 | return 0; |
| 1429 | } |
| 1430 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1431 | static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf, |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1432 | struct statfs *stbuf) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1433 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1434 | stbuf->f_bsize = compatbuf->block_size; |
| 1435 | stbuf->f_blocks = compatbuf->blocks; |
| 1436 | stbuf->f_bfree = compatbuf->blocks_free; |
| 1437 | stbuf->f_bavail = compatbuf->blocks_free; |
| 1438 | stbuf->f_files = compatbuf->files; |
| 1439 | stbuf->f_ffree = compatbuf->files_free; |
| 1440 | stbuf->f_namelen = compatbuf->namelen; |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1441 | } |
| 1442 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1443 | static void fuse_statfs(fuse_req_t req) |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1444 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1445 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1446 | struct statfs buf; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1447 | int err; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1448 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1449 | memset(&buf, 0, sizeof(struct statfs)); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1450 | if (f->op.statfs) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1451 | if (!f->compat || f->compat > 11) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1452 | err = f->op.statfs("/", &buf); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1453 | else { |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1454 | struct fuse_statfs_compat1 compatbuf; |
| 1455 | memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1)); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1456 | err = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf); |
| 1457 | if (!err) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1458 | convert_statfs_compat(&compatbuf, &buf); |
| 1459 | } |
| 1460 | } |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1461 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1462 | err = default_statfs(&buf); |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1463 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1464 | if (!err) |
| 1465 | fuse_reply_statfs(req, &buf); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1466 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1467 | reply_err(req, err); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1468 | } |
| 1469 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1470 | static void fuse_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, |
| 1471 | const char *value, size_t size, int flags) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1472 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1473 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1474 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1475 | int err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1476 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1477 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1478 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1479 | path = get_path(f, ino); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1480 | if (path != NULL) { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1481 | err = -ENOSYS; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1482 | if (f->op.setxattr) |
| 1483 | err = f->op.setxattr(path, name, value, size, flags); |
| 1484 | free(path); |
| 1485 | } |
| 1486 | pthread_rwlock_unlock(&f->tree_lock); |
| 1487 | reply_err(req, err); |
| 1488 | } |
| 1489 | |
| 1490 | static int common_getxattr(struct fuse *f, fuse_ino_t ino, const char *name, |
| 1491 | char *value, size_t size) |
| 1492 | { |
| 1493 | int err; |
| 1494 | char *path; |
| 1495 | |
| 1496 | err = -ENOENT; |
| 1497 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1498 | path = get_path(f, ino); |
| 1499 | if (path != NULL) { |
| 1500 | err = -ENOSYS; |
| 1501 | if (f->op.getxattr) |
| 1502 | err = f->op.getxattr(path, name, value, size); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1503 | free(path); |
| 1504 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1505 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1506 | return err; |
| 1507 | } |
| 1508 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1509 | static void fuse_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, |
| 1510 | size_t size) |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1511 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1512 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1513 | int res; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1514 | |
| 1515 | if (size) { |
| 1516 | char *value = (char *) malloc(size); |
| 1517 | if (value == NULL) { |
| 1518 | reply_err(req, -ENOMEM); |
| 1519 | return; |
| 1520 | } |
| 1521 | res = common_getxattr(f, ino, name, value, size); |
| 1522 | if (res > 0) |
| 1523 | fuse_reply_buf(req, value, res); |
| 1524 | else |
| 1525 | reply_err(req, res); |
| 1526 | free(value); |
| 1527 | } else { |
| 1528 | res = common_getxattr(f, ino, name, NULL, 0); |
| 1529 | if (res >= 0) |
| 1530 | fuse_reply_xattr(req, res); |
| 1531 | else |
| 1532 | reply_err(req, res); |
| 1533 | } |
| 1534 | } |
| 1535 | |
| 1536 | static int common_listxattr(struct fuse *f, fuse_ino_t ino, char *list, |
| 1537 | size_t size) |
| 1538 | { |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1539 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1540 | int err; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1541 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1542 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1543 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1544 | path = get_path(f, ino); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1545 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1546 | err = -ENOSYS; |
| 1547 | if (f->op.listxattr) |
| 1548 | err = f->op.listxattr(path, list, size); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1549 | free(path); |
| 1550 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1551 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1552 | return err; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 1553 | } |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 1554 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1555 | 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] | 1556 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1557 | struct fuse *f = req_fuse_prepare(req); |
| 1558 | int res; |
| 1559 | |
| 1560 | if (size) { |
| 1561 | char *list = (char *) malloc(size); |
| 1562 | if (list == NULL) { |
| 1563 | reply_err(req, -ENOMEM); |
| 1564 | return; |
| 1565 | } |
| 1566 | res = common_listxattr(f, ino, list, size); |
| 1567 | if (res > 0) |
| 1568 | fuse_reply_buf(req, list, res); |
| 1569 | else |
| 1570 | reply_err(req, res); |
| 1571 | free(list); |
| 1572 | } else { |
| 1573 | res = common_listxattr(f, ino, NULL, 0); |
| 1574 | if (res >= 0) |
| 1575 | fuse_reply_xattr(req, res); |
| 1576 | else |
| 1577 | reply_err(req, res); |
| 1578 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1579 | } |
| 1580 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1581 | static void fuse_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name) |
| 1582 | { |
| 1583 | struct fuse *f = req_fuse_prepare(req); |
| 1584 | char *path; |
| 1585 | int err; |
| 1586 | |
| 1587 | err = -ENOENT; |
| 1588 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1589 | path = get_path(f, ino); |
| 1590 | if (path != NULL) { |
| 1591 | err = -ENOSYS; |
| 1592 | if (f->op.removexattr) |
| 1593 | err = f->op.removexattr(path, name); |
| 1594 | free(path); |
| 1595 | } |
| 1596 | pthread_rwlock_unlock(&f->tree_lock); |
| 1597 | reply_err(req, err); |
| 1598 | } |
| 1599 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1600 | static struct fuse_lowlevel_ops fuse_path_ops = { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1601 | .init = fuse_data_init, |
| 1602 | .destroy = fuse_data_destroy, |
| 1603 | .lookup = fuse_lookup, |
| 1604 | .forget = fuse_forget, |
| 1605 | .getattr = fuse_getattr, |
| 1606 | .setattr = fuse_setattr, |
| 1607 | .readlink = fuse_readlink, |
| 1608 | .mknod = fuse_mknod, |
| 1609 | .mkdir = fuse_mkdir, |
| 1610 | .unlink = fuse_unlink, |
| 1611 | .rmdir = fuse_rmdir, |
| 1612 | .symlink = fuse_symlink, |
| 1613 | .rename = fuse_rename, |
| 1614 | .link = fuse_link, |
| 1615 | .open = fuse_open, |
| 1616 | .read = fuse_read, |
| 1617 | .write = fuse_write, |
| 1618 | .flush = fuse_flush, |
| 1619 | .release = fuse_release, |
| 1620 | .fsync = fuse_fsync, |
| 1621 | .opendir = fuse_opendir, |
| 1622 | .readdir = fuse_readdir, |
| 1623 | .releasedir = fuse_releasedir, |
| 1624 | .fsyncdir = fuse_fsyncdir, |
| 1625 | .statfs = fuse_statfs, |
| 1626 | .setxattr = fuse_setxattr, |
| 1627 | .getxattr = fuse_getxattr, |
| 1628 | .listxattr = fuse_listxattr, |
| 1629 | .removexattr = fuse_removexattr, |
| 1630 | }; |
| 1631 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1632 | static void free_cmd(struct fuse_cmd *cmd) |
| 1633 | { |
| 1634 | free(cmd->buf); |
| 1635 | free(cmd); |
| 1636 | } |
| 1637 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1638 | void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1639 | { |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 1640 | fuse_session_process(f->se, cmd->buf, cmd->buflen, cmd->ch); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1641 | } |
| 1642 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1643 | int fuse_exited(struct fuse *f) |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1644 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1645 | return fuse_session_exited(f->se); |
| 1646 | } |
| 1647 | |
| 1648 | struct fuse_session *fuse_get_session(struct fuse *f) |
| 1649 | { |
| 1650 | return f->se; |
| 1651 | } |
| 1652 | |
| 1653 | static struct fuse_cmd *fuse_alloc_cmd(size_t bufsize) |
| 1654 | { |
| 1655 | struct fuse_cmd *cmd = (struct fuse_cmd *) malloc(sizeof(*cmd)); |
| 1656 | if (cmd == NULL) { |
| 1657 | fprintf(stderr, "fuse: failed to allocate cmd\n"); |
| 1658 | return NULL; |
| 1659 | } |
| 1660 | cmd->buf = (char *) malloc(bufsize); |
| 1661 | if (cmd->buf == NULL) { |
| 1662 | fprintf(stderr, "fuse: failed to allocate read buffer\n"); |
| 1663 | free(cmd); |
| 1664 | return NULL; |
| 1665 | } |
| 1666 | return cmd; |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1667 | } |
| 1668 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1669 | struct fuse_cmd *fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1670 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1671 | struct fuse_chan *ch = fuse_session_next_chan(f->se, NULL); |
| 1672 | size_t bufsize = fuse_chan_bufsize(ch); |
| 1673 | struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize); |
| 1674 | if (cmd != NULL) { |
| 1675 | int res = fuse_chan_receive(ch, cmd->buf, bufsize); |
| 1676 | if (res <= 0) { |
| 1677 | free_cmd(cmd); |
| 1678 | return NULL; |
| 1679 | } |
| 1680 | cmd->buflen = res; |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 1681 | cmd->ch = ch; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1682 | } |
| 1683 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1684 | } |
| 1685 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1686 | int fuse_loop(struct fuse *f) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1687 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1688 | if (f) |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1689 | return fuse_session_loop(f->se); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1690 | else |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1691 | return -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1692 | } |
| 1693 | |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1694 | int fuse_invalidate(struct fuse *f, const char *path) |
| 1695 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1696 | (void) f; |
| 1697 | (void) path; |
| 1698 | return -EINVAL; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1699 | } |
| 1700 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1701 | void fuse_exit(struct fuse *f) |
| 1702 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1703 | fuse_session_exit(f->se); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1704 | } |
| 1705 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1706 | struct fuse_context *fuse_get_context() |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1707 | { |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1708 | static struct fuse_context context; |
| 1709 | if (fuse_getcontext) |
| 1710 | return fuse_getcontext(); |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1711 | else |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1712 | return &context; |
| 1713 | } |
| 1714 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1715 | void fuse_set_getcontext_func(struct fuse_context *(*func)(void)) |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1716 | { |
| 1717 | fuse_getcontext = func; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1718 | } |
| 1719 | |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 1720 | static int begins_with(const char *s, const char *beg) |
| 1721 | { |
| 1722 | if (strncmp(s, beg, strlen(beg)) == 0) |
| 1723 | return 1; |
| 1724 | else |
| 1725 | return 0; |
| 1726 | } |
| 1727 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1728 | int fuse_is_lib_option(const char *opt) |
| 1729 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1730 | if (fuse_lowlevel_is_lib_option(opt) || |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1731 | strcmp(opt, "debug") == 0 || |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1732 | strcmp(opt, "hard_remove") == 0 || |
Miklos Szeredi | 0111f9d | 2005-04-22 12:04:55 +0000 | [diff] [blame] | 1733 | strcmp(opt, "use_ino") == 0 || |
Miklos Szeredi | 33be22d | 2005-05-27 09:12:43 +0000 | [diff] [blame] | 1734 | strcmp(opt, "allow_root") == 0 || |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 1735 | strcmp(opt, "readdir_ino") == 0 || |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 1736 | strcmp(opt, "direct_io") == 0 || |
| 1737 | strcmp(opt, "kernel_cache") == 0 || |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 1738 | begins_with(opt, "umask=") || |
| 1739 | begins_with(opt, "uid=") || |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 1740 | begins_with(opt, "gid=") || |
| 1741 | begins_with(opt, "entry_timeout=") || |
| 1742 | begins_with(opt, "attr_timeout=")) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1743 | return 1; |
| 1744 | else |
| 1745 | return 0; |
| 1746 | } |
| 1747 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1748 | 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] | 1749 | { |
| 1750 | if (opts) { |
| 1751 | char *xopts = strdup(opts); |
| 1752 | char *s = xopts; |
| 1753 | char *opt; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1754 | char *d = xopts; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1755 | |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1756 | if (xopts == NULL) { |
| 1757 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1758 | return -1; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1759 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1760 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1761 | while((opt = strsep(&s, ","))) { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1762 | if (fuse_lowlevel_is_lib_option(opt)) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1763 | size_t optlen = strlen(opt); |
| 1764 | if (strcmp(opt, "debug") == 0) |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 1765 | f->flags |= FUSE_DEBUG; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1766 | memmove(d, opt, optlen); |
| 1767 | d += optlen; |
| 1768 | *d++ = ','; |
| 1769 | } else if (strcmp(opt, "hard_remove") == 0) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1770 | f->flags |= FUSE_HARD_REMOVE; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1771 | else if (strcmp(opt, "use_ino") == 0) |
| 1772 | f->flags |= FUSE_USE_INO; |
Miklos Szeredi | 33be22d | 2005-05-27 09:12:43 +0000 | [diff] [blame] | 1773 | else if (strcmp(opt, "readdir_ino") == 0) |
| 1774 | f->flags |= FUSE_READDIR_INO; |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 1775 | else if (strcmp(opt, "direct_io") == 0) |
| 1776 | f->flags |= FUSE_DIRECT_IO; |
| 1777 | else if (strcmp(opt, "kernel_cache") == 0) |
| 1778 | f->flags |= FUSE_KERNEL_CACHE; |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 1779 | else if (sscanf(opt, "umask=%o", &f->umask) == 1) |
| 1780 | f->flags |= FUSE_SET_MODE; |
| 1781 | else if (sscanf(opt, "uid=%u", &f->uid) == 1) |
| 1782 | f->flags |= FUSE_SET_UID; |
| 1783 | else if(sscanf(opt, "gid=%u", &f->gid) == 1) |
| 1784 | f->flags |= FUSE_SET_GID; |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 1785 | else if (sscanf(opt, "entry_timeout=%lf", &f->entry_timeout) == 1) |
| 1786 | /* nop */; |
| 1787 | else if (sscanf(opt, "attr_timeout=%lf", &f->attr_timeout) == 1) |
| 1788 | /* nop */; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1789 | else |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1790 | fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt); |
| 1791 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1792 | if (d != xopts) { |
| 1793 | d[-1] = '\0'; |
| 1794 | *llopts = xopts; |
| 1795 | } |
| 1796 | else |
| 1797 | free(xopts); |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1798 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1799 | return 0; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1800 | } |
| 1801 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1802 | struct fuse *fuse_new_common(int fd, const char *opts, |
| 1803 | const struct fuse_operations *op, |
| 1804 | size_t op_size, int compat) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1805 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1806 | struct fuse_chan *ch; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1807 | struct fuse *f; |
| 1808 | struct node *root; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1809 | char *llopts = NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1810 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1811 | if (sizeof(struct fuse_operations) < op_size) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1812 | 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] | 1813 | op_size = sizeof(struct fuse_operations); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1814 | } |
| 1815 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1816 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1817 | if (f == NULL) { |
| 1818 | fprintf(stderr, "fuse: failed to allocate fuse object\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1819 | goto out; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1820 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1821 | |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 1822 | f->entry_timeout = 1.0; |
| 1823 | f->attr_timeout = 1.0; |
| 1824 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1825 | if (parse_lib_opts(f, opts, &llopts) == -1) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1826 | goto out_free; |
| 1827 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1828 | 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] | 1829 | free(llopts); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1830 | if (f->se == NULL) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1831 | goto out_free; |
Miklos Szeredi | 2bb750e | 2005-10-03 14:54:24 +0000 | [diff] [blame] | 1832 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1833 | ch = fuse_kern_chan_new(fd); |
| 1834 | if (ch == NULL) |
| 1835 | goto out_free_session; |
Miklos Szeredi | 2bb750e | 2005-10-03 14:54:24 +0000 | [diff] [blame] | 1836 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1837 | fuse_session_add_chan(f->se, ch); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1838 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1839 | f->ctr = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1840 | f->generation = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1841 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1842 | f->name_table_size = 14057; |
| 1843 | f->name_table = (struct node **) |
| 1844 | calloc(1, sizeof(struct node *) * f->name_table_size); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1845 | if (f->name_table == NULL) { |
| 1846 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1847 | goto out_free_session; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1848 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1849 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1850 | f->id_table_size = 14057; |
| 1851 | f->id_table = (struct node **) |
| 1852 | calloc(1, sizeof(struct node *) * f->id_table_size); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1853 | if (f->id_table == NULL) { |
| 1854 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1855 | goto out_free_name_table; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1856 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1857 | |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1858 | mutex_init(&f->lock); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1859 | memcpy(&f->op, op, op_size); |
| 1860 | f->compat = compat; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1861 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1862 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1863 | if (root == NULL) { |
| 1864 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1865 | goto out_free_id_table; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1866 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1867 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1868 | root->name = strdup("/"); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1869 | if (root->name == NULL) { |
| 1870 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1871 | goto out_free_root; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 1872 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1873 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1874 | root->parent = 0; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1875 | root->nodeid = FUSE_ROOT_ID; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1876 | root->generation = 0; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1877 | root->refctr = 1; |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 1878 | root->nlookup = 1; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1879 | hash_id(f, root); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1880 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1881 | return f; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1882 | |
| 1883 | out_free_root: |
| 1884 | free(root); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1885 | out_free_id_table: |
| 1886 | free(f->id_table); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1887 | out_free_name_table: |
| 1888 | free(f->name_table); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1889 | out_free_session: |
| 1890 | fuse_session_destroy(f->se); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1891 | out_free: |
| 1892 | free(f); |
| 1893 | out: |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1894 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1895 | } |
| 1896 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1897 | struct fuse *fuse_new(int fd, const char *opts, |
| 1898 | const struct fuse_operations *op, size_t op_size) |
| 1899 | { |
| 1900 | return fuse_new_common(fd, opts, op, op_size, 0); |
| 1901 | } |
| 1902 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1903 | struct fuse *fuse_new_compat2(int fd, const char *opts, |
| 1904 | const struct fuse_operations_compat2 *op) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1905 | { |
| 1906 | return fuse_new_common(fd, opts, (struct fuse_operations *) op, |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1907 | sizeof(struct fuse_operations_compat2), 21); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1908 | } |
| 1909 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1910 | struct fuse *fuse_new_compat1(int fd, int flags, |
| 1911 | const struct fuse_operations_compat1 *op) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1912 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1913 | const char *opts = NULL; |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1914 | if (flags & FUSE_DEBUG_COMPAT1) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1915 | opts = "debug"; |
| 1916 | return fuse_new_common(fd, opts, (struct fuse_operations *) op, |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1917 | sizeof(struct fuse_operations_compat1), 11); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1918 | } |
| 1919 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1920 | void fuse_destroy(struct fuse *f) |
| 1921 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1922 | size_t i; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1923 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1924 | struct node *node; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1925 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1926 | for (node = f->id_table[i]; node != NULL; node = node->id_next) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1927 | if (node->is_hidden) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1928 | char *path = get_path(f, node->nodeid); |
Miklos Szeredi | 21019c9 | 2005-05-09 11:22:41 +0000 | [diff] [blame] | 1929 | if (path) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1930 | f->op.unlink(path); |
Miklos Szeredi | 21019c9 | 2005-05-09 11:22:41 +0000 | [diff] [blame] | 1931 | free(path); |
| 1932 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1933 | } |
| 1934 | } |
| 1935 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1936 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1937 | struct node *node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1938 | struct node *next; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1939 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1940 | for (node = f->id_table[i]; node != NULL; node = next) { |
| 1941 | next = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1942 | free_node(node); |
| 1943 | } |
| 1944 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1945 | free(f->id_table); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1946 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1947 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 1948 | fuse_session_destroy(f->se); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1949 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1950 | } |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1951 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 1952 | __asm__(".symver fuse_exited,__fuse_exited@"); |
| 1953 | __asm__(".symver fuse_process_cmd,__fuse_process_cmd@"); |
| 1954 | __asm__(".symver fuse_read_cmd,__fuse_read_cmd@"); |
| 1955 | __asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@"); |
| 1956 | __asm__(".symver fuse_new_compat2,fuse_new@"); |