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