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