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