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 | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 16 | #include "fuse_misc.h" |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 17 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 18 | #include <stdio.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 19 | #include <string.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 20 | #include <stdlib.h> |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 21 | #include <stddef.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 22 | #include <unistd.h> |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 23 | #include <time.h> |
Miklos Szeredi | b3f9972 | 2005-11-16 13:00:24 +0000 | [diff] [blame] | 24 | #include <fcntl.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 25 | #include <limits.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 26 | #include <errno.h> |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 27 | #include <signal.h> |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 28 | #include <assert.h> |
Miklos Szeredi | 019b4e9 | 2001-12-26 18:08:09 +0000 | [diff] [blame] | 29 | #include <sys/param.h> |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 30 | #include <sys/uio.h> |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 31 | #include <sys/time.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 32 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 33 | #define FUSE_MAX_PATH 4096 |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 34 | #define FUSE_DEFAULT_INTR_SIGNAL SIGUSR1 |
Miklos Szeredi | 30e093a | 2005-04-03 17:44:54 +0000 | [diff] [blame] | 35 | |
Miklos Szeredi | e248e4b | 2005-12-14 16:18:32 +0000 | [diff] [blame] | 36 | #define FUSE_UNKNOWN_INO 0xffffffff |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 37 | #define OFFSET_MAX 0x7fffffffffffffffLL |
Miklos Szeredi | e248e4b | 2005-12-14 16:18:32 +0000 | [diff] [blame] | 38 | |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 39 | struct fuse_config { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 40 | unsigned int uid; |
| 41 | unsigned int gid; |
| 42 | unsigned int umask; |
| 43 | double entry_timeout; |
| 44 | double negative_timeout; |
| 45 | double attr_timeout; |
Miklos Szeredi | 6e806e9 | 2006-02-16 16:59:39 +0000 | [diff] [blame] | 46 | double ac_attr_timeout; |
| 47 | int ac_attr_timeout_set; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 48 | int debug; |
| 49 | int hard_remove; |
| 50 | int use_ino; |
| 51 | int readdir_ino; |
| 52 | int set_mode; |
| 53 | int set_uid; |
| 54 | int set_gid; |
| 55 | int direct_io; |
| 56 | int kernel_cache; |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 57 | int auto_cache; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 58 | int intr; |
| 59 | int intr_signal; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 60 | }; |
| 61 | |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 62 | struct fuse { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 63 | struct fuse_session *se; |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 64 | struct fuse_operations op; |
| 65 | int compat; |
| 66 | struct node **name_table; |
| 67 | size_t name_table_size; |
| 68 | struct node **id_table; |
| 69 | size_t id_table_size; |
| 70 | fuse_ino_t ctr; |
| 71 | unsigned int generation; |
| 72 | unsigned int hidectr; |
| 73 | pthread_mutex_t lock; |
| 74 | pthread_rwlock_t tree_lock; |
| 75 | void *user_data; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 76 | struct fuse_config conf; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 77 | int intr_installed; |
Miklos Szeredi | e2aa2e2 | 2005-07-15 13:31:36 +0000 | [diff] [blame] | 78 | }; |
| 79 | |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 80 | struct lock { |
| 81 | int type; |
| 82 | off_t start; |
| 83 | off_t end; |
| 84 | pid_t pid; |
| 85 | uint64_t owner; |
| 86 | struct lock *next; |
| 87 | }; |
| 88 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 89 | struct node { |
| 90 | struct node *name_next; |
| 91 | struct node *id_next; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 92 | fuse_ino_t nodeid; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 93 | unsigned int generation; |
| 94 | int refctr; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 95 | fuse_ino_t parent; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 96 | char *name; |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 97 | uint64_t nlookup; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 98 | int open_count; |
| 99 | int is_hidden; |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 100 | struct timespec stat_updated; |
| 101 | struct timespec mtime; |
| 102 | off_t size; |
| 103 | int cache_valid; |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 104 | struct lock *locks; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 105 | }; |
| 106 | |
| 107 | struct fuse_dirhandle { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 108 | pthread_mutex_t lock; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 109 | struct fuse *fuse; |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 110 | fuse_req_t req; |
Miklos Szeredi | 1b18802 | 2005-07-28 11:07:29 +0000 | [diff] [blame] | 111 | char *contents; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 112 | int allocated; |
Miklos Szeredi | b92d978 | 2005-02-07 16:10:49 +0000 | [diff] [blame] | 113 | unsigned len; |
Miklos Szeredi | c4c12ae | 2005-10-20 14:48:50 +0000 | [diff] [blame] | 114 | unsigned size; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 115 | unsigned needlen; |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 116 | int filled; |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 117 | uint64_t fh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 118 | int error; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 119 | fuse_ino_t nodeid; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 120 | }; |
| 121 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 122 | struct fuse_context_i { |
| 123 | struct fuse_context ctx; |
| 124 | fuse_req_t req; |
| 125 | }; |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 126 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 127 | static pthread_key_t fuse_context_key; |
| 128 | static pthread_mutex_t fuse_context_lock = PTHREAD_MUTEX_INITIALIZER; |
| 129 | static int fuse_context_ref; |
| 130 | |
| 131 | static int fuse_compat_open(struct fuse *, fuse_req_t, char *, |
| 132 | struct fuse_file_info *); |
| 133 | static void fuse_compat_release(struct fuse *, fuse_req_t, char *, |
| 134 | struct fuse_file_info *); |
| 135 | static int fuse_compat_opendir(struct fuse *, fuse_req_t, char *, |
| 136 | struct fuse_file_info *); |
| 137 | static int fuse_compat_statfs(struct fuse *, fuse_req_t, struct statvfs *); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 138 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 139 | 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] | 140 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 141 | size_t hash = nodeid % f->id_table_size; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 142 | struct node *node; |
| 143 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 144 | for (node = f->id_table[hash]; node != NULL; node = node->id_next) |
| 145 | if (node->nodeid == nodeid) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 146 | return node; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 147 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 148 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 149 | } |
| 150 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 151 | static struct node *get_node(struct fuse *f, fuse_ino_t nodeid) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 152 | { |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 153 | struct node *node = get_node_nocheck(f, nodeid); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 154 | if (!node) { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 155 | fprintf(stderr, "fuse internal error: node %llu not found\n", |
| 156 | (unsigned long long) nodeid); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 157 | abort(); |
| 158 | } |
| 159 | return node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 160 | } |
| 161 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 162 | static void free_node(struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 163 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 164 | free(node->name); |
| 165 | free(node); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 166 | } |
| 167 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 168 | static void unhash_id(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 169 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 170 | size_t hash = node->nodeid % f->id_table_size; |
| 171 | struct node **nodep = &f->id_table[hash]; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 172 | |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 173 | for (; *nodep != NULL; nodep = &(*nodep)->id_next) |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 174 | if (*nodep == node) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 175 | *nodep = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 176 | return; |
| 177 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 178 | } |
| 179 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 180 | static void hash_id(struct fuse *f, struct node *node) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 181 | { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 182 | size_t hash = node->nodeid % f->id_table_size; |
| 183 | node->id_next = f->id_table[hash]; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 184 | f->id_table[hash] = node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 185 | } |
| 186 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 187 | 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] | 188 | { |
| 189 | unsigned int hash = *name; |
| 190 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 191 | if (hash) |
| 192 | for (name += 1; *name != '\0'; name++) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 193 | hash = (hash << 5) - hash + *name; |
| 194 | |
| 195 | return (hash + parent) % f->name_table_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 196 | } |
| 197 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 198 | static void unref_node(struct fuse *f, struct node *node); |
| 199 | |
| 200 | static void unhash_name(struct fuse *f, struct node *node) |
| 201 | { |
| 202 | if (node->name) { |
| 203 | size_t hash = name_hash(f, node->parent, node->name); |
| 204 | struct node **nodep = &f->name_table[hash]; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 205 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 206 | for (; *nodep != NULL; nodep = &(*nodep)->name_next) |
| 207 | if (*nodep == node) { |
| 208 | *nodep = node->name_next; |
| 209 | node->name_next = NULL; |
| 210 | unref_node(f, get_node(f, node->parent)); |
| 211 | free(node->name); |
| 212 | node->name = NULL; |
| 213 | node->parent = 0; |
| 214 | return; |
| 215 | } |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 216 | fprintf(stderr, "fuse internal error: unable to unhash node: %llu\n", |
| 217 | (unsigned long long) node->nodeid); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 218 | abort(); |
| 219 | } |
| 220 | } |
| 221 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 222 | 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] | 223 | const char *name) |
| 224 | { |
| 225 | size_t hash = name_hash(f, parent, name); |
| 226 | node->name = strdup(name); |
| 227 | if (node->name == NULL) |
| 228 | return -1; |
| 229 | |
| 230 | get_node(f, parent)->refctr ++; |
| 231 | node->parent = parent; |
| 232 | node->name_next = f->name_table[hash]; |
| 233 | f->name_table[hash] = node; |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | static void delete_node(struct fuse *f, struct node *node) |
| 238 | { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 239 | if (f->conf.debug) { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 240 | printf("delete: %llu\n", (unsigned long long) node->nodeid); |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 241 | fflush(stdout); |
| 242 | } |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 243 | assert(!node->name); |
| 244 | unhash_id(f, node); |
| 245 | free_node(node); |
| 246 | } |
| 247 | |
| 248 | static void unref_node(struct fuse *f, struct node *node) |
| 249 | { |
| 250 | assert(node->refctr > 0); |
| 251 | node->refctr --; |
| 252 | if (!node->refctr) |
| 253 | delete_node(f, node); |
| 254 | } |
| 255 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 256 | static fuse_ino_t next_id(struct fuse *f) |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 257 | { |
| 258 | do { |
Miklos Szeredi | 7e7fa1f | 2006-10-08 15:41:20 +0000 | [diff] [blame] | 259 | f->ctr = (f->ctr + 1) & 0xffffffff; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 260 | if (!f->ctr) |
| 261 | f->generation ++; |
Miklos Szeredi | 7e7fa1f | 2006-10-08 15:41:20 +0000 | [diff] [blame] | 262 | } while (f->ctr == 0 || f->ctr == FUSE_UNKNOWN_INO || |
| 263 | get_node_nocheck(f, f->ctr) != NULL); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 264 | return f->ctr; |
| 265 | } |
| 266 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 267 | static struct node *lookup_node(struct fuse *f, fuse_ino_t parent, |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 268 | const char *name) |
| 269 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 270 | size_t hash = name_hash(f, parent, name); |
| 271 | struct node *node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 272 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 273 | for (node = f->name_table[hash]; node != NULL; node = node->name_next) |
| 274 | if (node->parent == parent && strcmp(node->name, name) == 0) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 275 | return node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 276 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 277 | return NULL; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 278 | } |
| 279 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 280 | static struct node *find_node(struct fuse *f, fuse_ino_t parent, |
| 281 | const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 282 | { |
| 283 | struct node *node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 284 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 285 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 286 | node = lookup_node(f, parent, name); |
Miklos Szeredi | e331c4b | 2005-07-06 13:34:02 +0000 | [diff] [blame] | 287 | if (node == NULL) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 288 | node = (struct node *) calloc(1, sizeof(struct node)); |
| 289 | if (node == NULL) |
| 290 | goto out_err; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 291 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 292 | node->refctr = 1; |
| 293 | node->nodeid = next_id(f); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 294 | node->open_count = 0; |
| 295 | node->is_hidden = 0; |
| 296 | node->generation = f->generation; |
| 297 | if (hash_name(f, node, parent, name) == -1) { |
| 298 | free(node); |
| 299 | node = NULL; |
| 300 | goto out_err; |
| 301 | } |
| 302 | hash_id(f, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 303 | } |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 304 | node->nlookup ++; |
Miklos Szeredi | c230991 | 2004-09-21 13:40:38 +0000 | [diff] [blame] | 305 | out_err: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 306 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 307 | return node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 308 | } |
| 309 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 310 | static char *add_name(char *buf, char *s, const char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 311 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 312 | size_t len = strlen(name); |
| 313 | s -= len; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 314 | if (s <= buf) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 315 | fprintf(stderr, "fuse: path too long: ...%s\n", s + len); |
| 316 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 317 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 318 | strncpy(s, name, len); |
| 319 | s--; |
| 320 | *s = '/'; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 321 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 322 | return s; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 323 | } |
| 324 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 325 | 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] | 326 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 327 | char buf[FUSE_MAX_PATH]; |
| 328 | char *s = buf + FUSE_MAX_PATH - 1; |
| 329 | struct node *node; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 330 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 331 | *s = '\0'; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 332 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 333 | if (name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 334 | s = add_name(buf, s, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 335 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 336 | return NULL; |
| 337 | } |
| 338 | |
| 339 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 340 | for (node = get_node(f, nodeid); node && node->nodeid != FUSE_ROOT_ID; |
| 341 | node = get_node(f, node->parent)) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 342 | if (node->name == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 343 | s = NULL; |
| 344 | break; |
| 345 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 346 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 347 | s = add_name(buf, s, node->name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 348 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 349 | break; |
| 350 | } |
| 351 | pthread_mutex_unlock(&f->lock); |
| 352 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 353 | if (node == NULL || s == NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 354 | return NULL; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 355 | else if (*s == '\0') |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 356 | return strdup("/"); |
| 357 | else |
| 358 | return strdup(s); |
| 359 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 360 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 361 | static char *get_path(struct fuse *f, fuse_ino_t nodeid) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 362 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 363 | return get_path_name(f, nodeid, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 364 | } |
| 365 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 366 | 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] | 367 | { |
| 368 | struct node *node; |
| 369 | if (nodeid == FUSE_ROOT_ID) |
| 370 | return; |
| 371 | pthread_mutex_lock(&f->lock); |
| 372 | node = get_node(f, nodeid); |
| 373 | assert(node->nlookup >= nlookup); |
| 374 | node->nlookup -= nlookup; |
| 375 | if (!node->nlookup) { |
| 376 | unhash_name(f, node); |
| 377 | unref_node(f, node); |
| 378 | } |
| 379 | pthread_mutex_unlock(&f->lock); |
| 380 | } |
| 381 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 382 | 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] | 383 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 384 | struct node *node; |
| 385 | |
| 386 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 387 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 388 | if (node != NULL) |
| 389 | unhash_name(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 390 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 391 | } |
| 392 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 393 | static int rename_node(struct fuse *f, fuse_ino_t olddir, const char *oldname, |
| 394 | fuse_ino_t newdir, const char *newname, int hide) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 395 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 396 | struct node *node; |
| 397 | struct node *newnode; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 398 | int err = 0; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 399 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 400 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 401 | node = lookup_node(f, olddir, oldname); |
| 402 | newnode = lookup_node(f, newdir, newname); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 403 | if (node == NULL) |
| 404 | goto out; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 405 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 406 | if (newnode != NULL) { |
| 407 | if (hide) { |
| 408 | fprintf(stderr, "fuse: hidden file got created during hiding\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 409 | err = -EBUSY; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 410 | goto out; |
| 411 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 412 | unhash_name(f, newnode); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 413 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 414 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 415 | unhash_name(f, node); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 416 | if (hash_name(f, node, newdir, newname) == -1) { |
| 417 | err = -ENOMEM; |
| 418 | goto out; |
| 419 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 420 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 421 | if (hide) |
| 422 | node->is_hidden = 1; |
| 423 | |
| 424 | out: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 425 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 426 | return err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 427 | } |
| 428 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 429 | 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] | 430 | { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 431 | if (!f->conf.use_ino) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 432 | stbuf->st_ino = nodeid; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 433 | if (f->conf.set_mode) |
| 434 | stbuf->st_mode = (stbuf->st_mode & S_IFMT) | (0777 & ~f->conf.umask); |
| 435 | if (f->conf.set_uid) |
| 436 | stbuf->st_uid = f->conf.uid; |
| 437 | if (f->conf.set_gid) |
| 438 | stbuf->st_gid = f->conf.gid; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 439 | } |
| 440 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 441 | static struct fuse *req_fuse(fuse_req_t req) |
| 442 | { |
| 443 | return (struct fuse *) fuse_req_userdata(req); |
| 444 | } |
| 445 | |
| 446 | static void fuse_intr_sighandler(int sig) |
| 447 | { |
| 448 | (void) sig; |
| 449 | /* Nothing to do */ |
| 450 | } |
| 451 | |
| 452 | struct fuse_intr_data { |
| 453 | pthread_t id; |
| 454 | pthread_cond_t cond; |
| 455 | int finished; |
| 456 | }; |
| 457 | |
| 458 | static void fuse_interrupt(fuse_req_t req, void *d_) |
| 459 | { |
| 460 | struct fuse_intr_data *d = d_; |
| 461 | struct fuse *f = req_fuse(req); |
| 462 | |
| 463 | if (d->id == pthread_self()) |
| 464 | return; |
| 465 | |
| 466 | pthread_mutex_lock(&f->lock); |
| 467 | while (!d->finished) { |
| 468 | struct timeval now; |
| 469 | struct timespec timeout; |
| 470 | |
| 471 | pthread_kill(d->id, f->conf.intr_signal); |
| 472 | gettimeofday(&now, NULL); |
| 473 | timeout.tv_sec = now.tv_sec + 1; |
| 474 | timeout.tv_nsec = now.tv_usec * 1000; |
| 475 | pthread_cond_timedwait(&d->cond, &f->lock, &timeout); |
| 476 | } |
| 477 | pthread_mutex_unlock(&f->lock); |
| 478 | } |
| 479 | |
| 480 | static void fuse_do_finish_interrupt(struct fuse *f, fuse_req_t req, |
| 481 | struct fuse_intr_data *d) |
| 482 | { |
| 483 | pthread_mutex_lock(&f->lock); |
| 484 | d->finished = 1; |
| 485 | pthread_cond_broadcast(&d->cond); |
| 486 | pthread_mutex_unlock(&f->lock); |
| 487 | fuse_req_interrupt_func(req, NULL, NULL); |
| 488 | pthread_cond_destroy(&d->cond); |
| 489 | } |
| 490 | |
| 491 | static void fuse_do_prepare_interrupt(fuse_req_t req, struct fuse_intr_data *d) |
| 492 | { |
| 493 | d->id = pthread_self(); |
| 494 | pthread_cond_init(&d->cond, NULL); |
| 495 | d->finished = 0; |
| 496 | fuse_req_interrupt_func(req, fuse_interrupt, d); |
| 497 | } |
| 498 | |
| 499 | static inline void fuse_finish_interrupt(struct fuse *f, fuse_req_t req, |
| 500 | struct fuse_intr_data *d) |
| 501 | { |
| 502 | if (f->conf.intr) |
| 503 | fuse_do_finish_interrupt(f, req, d); |
| 504 | } |
| 505 | |
| 506 | static inline void fuse_prepare_interrupt(struct fuse *f, fuse_req_t req, |
| 507 | struct fuse_intr_data *d) |
| 508 | { |
| 509 | if (f->conf.intr) |
| 510 | fuse_do_prepare_interrupt(req, d); |
| 511 | } |
| 512 | |
| 513 | static int fuse_do_getattr(struct fuse *f, fuse_req_t req, const char *path, |
| 514 | struct stat *buf) |
| 515 | { |
| 516 | int res; |
| 517 | struct fuse_intr_data d; |
| 518 | fuse_prepare_interrupt(f, req, &d); |
| 519 | res = f->op.getattr(path, buf); |
| 520 | fuse_finish_interrupt(f, req, &d); |
| 521 | return res; |
| 522 | } |
| 523 | |
| 524 | static int fuse_do_fgetattr(struct fuse *f, fuse_req_t req, const char *path, |
| 525 | struct stat *buf, struct fuse_file_info *fi) |
| 526 | { |
| 527 | int res; |
| 528 | struct fuse_intr_data d; |
| 529 | fuse_prepare_interrupt(f, req, &d); |
| 530 | res = f->op.fgetattr(path, buf, fi); |
| 531 | fuse_finish_interrupt(f, req, &d); |
| 532 | return res; |
| 533 | } |
| 534 | |
| 535 | static int fuse_do_rename(struct fuse *f, fuse_req_t req, const char *oldpath, |
| 536 | const char *newpath) |
| 537 | { |
| 538 | int res; |
| 539 | struct fuse_intr_data d; |
| 540 | fuse_prepare_interrupt(f, req, &d); |
| 541 | res = f->op.rename(oldpath, newpath); |
| 542 | fuse_finish_interrupt(f, req, &d); |
| 543 | return res; |
| 544 | } |
| 545 | |
| 546 | static int fuse_do_unlink(struct fuse *f, fuse_req_t req, const char *path) |
| 547 | { |
| 548 | int res; |
| 549 | struct fuse_intr_data d; |
| 550 | fuse_prepare_interrupt(f, req, &d); |
| 551 | res = f->op.unlink(path); |
| 552 | fuse_finish_interrupt(f, req, &d); |
| 553 | return res; |
| 554 | } |
| 555 | |
| 556 | static void fuse_do_release(struct fuse *f, fuse_req_t req, const char *path, |
| 557 | struct fuse_file_info *fi) |
| 558 | { |
| 559 | struct fuse_intr_data d; |
| 560 | fuse_prepare_interrupt(f, req, &d); |
| 561 | f->op.release(path, fi); |
| 562 | fuse_finish_interrupt(f, req, &d); |
| 563 | } |
| 564 | |
| 565 | static int fuse_do_opendir(struct fuse *f, fuse_req_t req, char *path, |
| 566 | struct fuse_file_info *fi) |
| 567 | { |
| 568 | int res; |
| 569 | struct fuse_intr_data d; |
| 570 | fuse_prepare_interrupt(f, req, &d); |
| 571 | res = f->op.opendir(path, fi); |
| 572 | fuse_finish_interrupt(f, req, &d); |
| 573 | return res; |
| 574 | } |
| 575 | |
| 576 | static int fuse_do_open(struct fuse *f, fuse_req_t req, char *path, |
| 577 | struct fuse_file_info *fi) |
| 578 | { |
| 579 | int res; |
| 580 | struct fuse_intr_data d; |
| 581 | fuse_prepare_interrupt(f, req, &d); |
| 582 | res = f->op.open(path, fi); |
| 583 | fuse_finish_interrupt(f, req, &d); |
| 584 | return res; |
| 585 | } |
| 586 | |
Miklos Szeredi | 4fca432 | 2006-10-01 14:41:04 +0000 | [diff] [blame] | 587 | static int fuse_do_flush(struct fuse *f, fuse_req_t req, const char *path, |
| 588 | struct fuse_file_info *fi) |
| 589 | { |
| 590 | int res; |
| 591 | struct fuse_intr_data d; |
| 592 | fuse_prepare_interrupt(f, req, &d); |
| 593 | res = f->op.flush(path, fi); |
| 594 | fuse_finish_interrupt(f, req, &d); |
| 595 | return res; |
| 596 | } |
| 597 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 598 | static int fuse_do_statfs(struct fuse *f, fuse_req_t req, const char *path, |
| 599 | struct statvfs *buf) |
| 600 | { |
| 601 | int res; |
| 602 | struct fuse_intr_data d; |
| 603 | fuse_prepare_interrupt(f, req, &d); |
| 604 | res = f->op.statfs(path, buf); |
| 605 | fuse_finish_interrupt(f, req, &d); |
| 606 | return res; |
| 607 | } |
| 608 | |
| 609 | static void fuse_do_releasedir(struct fuse *f, fuse_req_t req, |
| 610 | const char *path, struct fuse_file_info *fi) |
| 611 | { |
| 612 | struct fuse_intr_data d; |
| 613 | fuse_prepare_interrupt(f, req, &d); |
| 614 | f->op.releasedir(path, fi); |
| 615 | fuse_finish_interrupt(f, req, &d); |
| 616 | } |
| 617 | |
| 618 | static int fuse_do_create(struct fuse *f, fuse_req_t req, const char *path, |
| 619 | mode_t mode, struct fuse_file_info *fi) |
| 620 | { |
| 621 | int res; |
| 622 | struct fuse_intr_data d; |
| 623 | fuse_prepare_interrupt(f, req, &d); |
| 624 | res = f->op.create(path, mode, fi); |
| 625 | fuse_finish_interrupt(f, req, &d); |
| 626 | return res; |
| 627 | } |
| 628 | |
| 629 | static int fuse_do_lock(struct fuse *f, fuse_req_t req, const char *path, |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 630 | struct fuse_file_info *fi, int cmd, struct flock *lock) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 631 | { |
| 632 | int res; |
| 633 | struct fuse_intr_data d; |
| 634 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 635 | res = f->op.lock(path, fi, cmd, lock); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 636 | fuse_finish_interrupt(f, req, &d); |
| 637 | return res; |
| 638 | } |
| 639 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 640 | 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] | 641 | { |
| 642 | struct node *node; |
| 643 | int isopen = 0; |
| 644 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 645 | node = lookup_node(f, dir, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 646 | if (node && node->open_count > 0) |
| 647 | isopen = 1; |
| 648 | pthread_mutex_unlock(&f->lock); |
| 649 | return isopen; |
| 650 | } |
| 651 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 652 | static char *hidden_name(struct fuse *f, fuse_req_t req, fuse_ino_t dir, |
| 653 | const char *oldname, char *newname, size_t bufsize) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 654 | { |
| 655 | struct stat buf; |
| 656 | struct node *node; |
| 657 | struct node *newnode; |
| 658 | char *newpath; |
| 659 | int res; |
| 660 | int failctr = 10; |
| 661 | |
| 662 | if (!f->op.getattr) |
| 663 | return NULL; |
| 664 | |
| 665 | do { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 666 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 667 | node = lookup_node(f, dir, oldname); |
| 668 | if (node == NULL) { |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 669 | pthread_mutex_unlock(&f->lock); |
| 670 | return NULL; |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 671 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 672 | do { |
| 673 | f->hidectr ++; |
| 674 | snprintf(newname, bufsize, ".fuse_hidden%08x%08x", |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 675 | (unsigned int) node->nodeid, f->hidectr); |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 676 | newnode = lookup_node(f, dir, newname); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 677 | } while(newnode); |
| 678 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 679 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 680 | newpath = get_path_name(f, dir, newname); |
| 681 | if (!newpath) |
| 682 | break; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 683 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 684 | res = fuse_do_getattr(f, req, newpath, &buf); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 685 | if (res != 0) |
| 686 | break; |
| 687 | free(newpath); |
| 688 | newpath = NULL; |
| 689 | } while(--failctr); |
| 690 | |
| 691 | return newpath; |
| 692 | } |
| 693 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 694 | static int hide_node(struct fuse *f, fuse_req_t req, const char *oldpath, |
| 695 | fuse_ino_t dir, const char *oldname) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 696 | { |
| 697 | char newname[64]; |
| 698 | char *newpath; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 699 | int err = -EBUSY; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 700 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 701 | if (f->op.rename && f->op.unlink) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 702 | newpath = hidden_name(f, req, dir, oldname, newname, sizeof(newname)); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 703 | if (newpath) { |
Miklos Szeredi | bcfa416 | 2006-10-13 19:48:19 +0000 | [diff] [blame^] | 704 | err = fuse_do_rename(f, req, oldpath, newpath); |
| 705 | if (!err) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 706 | err = rename_node(f, dir, oldname, dir, newname, 1); |
| 707 | free(newpath); |
| 708 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 709 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 710 | return err; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 711 | } |
| 712 | |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 713 | static int mtime_eq(const struct stat *stbuf, const struct timespec *ts) |
| 714 | { |
| 715 | return stbuf->st_mtime == ts->tv_sec |
Csaba Henk | 3e3a125 | 2006-09-24 14:53:29 +0000 | [diff] [blame] | 716 | #ifdef FUSE_STAT_HAS_NANOSEC |
| 717 | && ST_MTIM(stbuf).tv_nsec == ts->tv_nsec |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 718 | #endif |
| 719 | ; |
| 720 | } |
| 721 | |
| 722 | static void mtime_set(const struct stat *stbuf, struct timespec *ts) |
| 723 | { |
Csaba Henk | 3e3a125 | 2006-09-24 14:53:29 +0000 | [diff] [blame] | 724 | #ifdef FUSE_STAT_HAS_NANOSEC |
| 725 | *ts = ST_MTIM(stbuf); |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 726 | #else |
| 727 | ts->tv_sec = stbuf->st_mtime; |
| 728 | #endif |
| 729 | } |
| 730 | |
Miklos Szeredi | 2512aaa | 2006-05-03 14:54:59 +0000 | [diff] [blame] | 731 | #ifndef CLOCK_MONOTONIC |
| 732 | #define CLOCK_MONOTONIC CLOCK_REALTIME |
| 733 | #endif |
| 734 | |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 735 | static void curr_time(struct timespec *now) |
| 736 | { |
| 737 | static clockid_t clockid = CLOCK_MONOTONIC; |
| 738 | int res = clock_gettime(clockid, now); |
| 739 | if (res == -1 && errno == EINVAL) { |
| 740 | clockid = CLOCK_REALTIME; |
| 741 | res = clock_gettime(clockid, now); |
| 742 | } |
| 743 | if (res == -1) { |
| 744 | perror("fuse: clock_gettime"); |
| 745 | abort(); |
| 746 | } |
| 747 | } |
| 748 | |
| 749 | static void update_stat(struct node *node, const struct stat *stbuf) |
| 750 | { |
| 751 | if (node->cache_valid && (!mtime_eq(stbuf, &node->mtime) || |
| 752 | stbuf->st_size != node->size)) |
| 753 | node->cache_valid = 0; |
| 754 | mtime_set(stbuf, &node->mtime); |
| 755 | node->size = stbuf->st_size; |
| 756 | curr_time(&node->stat_updated); |
| 757 | } |
| 758 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 759 | static int lookup_path(struct fuse *f, fuse_req_t req, fuse_ino_t nodeid, |
| 760 | const char *name, const char *path, |
| 761 | struct fuse_entry_param *e, struct fuse_file_info *fi) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 762 | { |
| 763 | int res; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 764 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 765 | memset(e, 0, sizeof(struct fuse_entry_param)); |
Miklos Szeredi | f7eec03 | 2005-10-28 13:09:50 +0000 | [diff] [blame] | 766 | if (fi && f->op.fgetattr) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 767 | res = fuse_do_fgetattr(f, req, path, &e->attr, fi); |
Miklos Szeredi | f7eec03 | 2005-10-28 13:09:50 +0000 | [diff] [blame] | 768 | else |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 769 | res = fuse_do_getattr(f, req, path, &e->attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 770 | if (res == 0) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 771 | struct node *node; |
| 772 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 773 | node = find_node(f, nodeid, name); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 774 | if (node == NULL) |
| 775 | res = -ENOMEM; |
| 776 | else { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 777 | e->ino = node->nodeid; |
| 778 | e->generation = node->generation; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 779 | e->entry_timeout = f->conf.entry_timeout; |
| 780 | e->attr_timeout = f->conf.attr_timeout; |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 781 | if (f->conf.auto_cache) { |
| 782 | pthread_mutex_lock(&f->lock); |
| 783 | update_stat(node, &e->attr); |
| 784 | pthread_mutex_unlock(&f->lock); |
| 785 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 786 | set_stat(f, e->ino, &e->attr); |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 787 | if (f->conf.debug) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 788 | printf(" NODEID: %lu\n", (unsigned long) e->ino); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 789 | fflush(stdout); |
| 790 | } |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 791 | } |
| 792 | } |
| 793 | return res; |
| 794 | } |
| 795 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 796 | static struct fuse_context_i *fuse_get_context_internal(void) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 797 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 798 | struct fuse_context_i *c; |
| 799 | |
| 800 | c = (struct fuse_context_i *) pthread_getspecific(fuse_context_key); |
| 801 | if (c == NULL) { |
| 802 | c = (struct fuse_context_i *) malloc(sizeof(struct fuse_context_i)); |
| 803 | if (c == NULL) { |
| 804 | /* This is hard to deal with properly, so just abort. If |
| 805 | memory is so low that the context cannot be allocated, |
| 806 | there's not much hope for the filesystem anyway */ |
| 807 | fprintf(stderr, "fuse: failed to allocate thread specific data\n"); |
| 808 | abort(); |
| 809 | } |
| 810 | pthread_setspecific(fuse_context_key, c); |
| 811 | } |
| 812 | return c; |
| 813 | } |
| 814 | |
| 815 | static void fuse_freecontext(void *data) |
| 816 | { |
| 817 | free(data); |
| 818 | } |
| 819 | |
| 820 | static int fuse_create_context_key(void) |
| 821 | { |
| 822 | int err = 0; |
| 823 | pthread_mutex_lock(&fuse_context_lock); |
| 824 | if (!fuse_context_ref) { |
| 825 | err = pthread_key_create(&fuse_context_key, fuse_freecontext); |
| 826 | if (err) { |
| 827 | fprintf(stderr, "fuse: failed to create thread specific key: %s\n", |
| 828 | strerror(err)); |
| 829 | pthread_mutex_unlock(&fuse_context_lock); |
| 830 | return -1; |
| 831 | } |
| 832 | } |
| 833 | fuse_context_ref++; |
| 834 | pthread_mutex_unlock(&fuse_context_lock); |
| 835 | return 0; |
| 836 | } |
| 837 | |
| 838 | static void fuse_delete_context_key(void) |
| 839 | { |
| 840 | pthread_mutex_lock(&fuse_context_lock); |
| 841 | fuse_context_ref--; |
| 842 | if (!fuse_context_ref) { |
| 843 | free(pthread_getspecific(fuse_context_key)); |
| 844 | pthread_key_delete(fuse_context_key); |
| 845 | } |
| 846 | pthread_mutex_unlock(&fuse_context_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 847 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 848 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 849 | static struct fuse *req_fuse_prepare(fuse_req_t req) |
| 850 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 851 | struct fuse_context_i *c = fuse_get_context_internal(); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 852 | const struct fuse_ctx *ctx = fuse_req_ctx(req); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 853 | c->req = req; |
| 854 | c->ctx.fuse = req_fuse(req); |
| 855 | c->ctx.uid = ctx->uid; |
| 856 | c->ctx.gid = ctx->gid; |
| 857 | c->ctx.pid = ctx->pid; |
| 858 | c->ctx.private_data = c->ctx.fuse->user_data; |
| 859 | return c->ctx.fuse; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 860 | } |
| 861 | |
| 862 | static inline void reply_err(fuse_req_t req, int err) |
| 863 | { |
| 864 | /* fuse_reply_err() uses non-negated errno values */ |
| 865 | fuse_reply_err(req, -err); |
| 866 | } |
| 867 | |
| 868 | static void reply_entry(fuse_req_t req, const struct fuse_entry_param *e, |
| 869 | int err) |
| 870 | { |
| 871 | if (!err) { |
Miklos Szeredi | b67f216 | 2006-02-20 10:55:33 +0000 | [diff] [blame] | 872 | struct fuse *f = req_fuse(req); |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 873 | if (fuse_reply_entry(req, e) == -ENOENT) |
Miklos Szeredi | b67f216 | 2006-02-20 10:55:33 +0000 | [diff] [blame] | 874 | forget_node(f, e->ino, 1); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 875 | } else |
| 876 | reply_err(req, err); |
| 877 | } |
| 878 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 879 | static void fuse_data_init(void *data, struct fuse_conn_info *conn) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 880 | { |
| 881 | struct fuse *f = (struct fuse *) data; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 882 | struct fuse_context_i *c = fuse_get_context_internal(); |
Miklos Szeredi | 4f5df5e | 2005-10-07 12:39:58 +0000 | [diff] [blame] | 883 | |
| 884 | memset(c, 0, sizeof(*c)); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 885 | c->ctx.fuse = f; |
| 886 | c->ctx.private_data = f->user_data; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 887 | |
| 888 | if (f->op.init) |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 889 | f->user_data = f->op.init(conn); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 890 | } |
| 891 | |
| 892 | static void fuse_data_destroy(void *data) |
| 893 | { |
| 894 | struct fuse *f = (struct fuse *) data; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 895 | struct fuse_context_i *c = fuse_get_context_internal(); |
Miklos Szeredi | 4f5df5e | 2005-10-07 12:39:58 +0000 | [diff] [blame] | 896 | |
| 897 | memset(c, 0, sizeof(*c)); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 898 | c->ctx.fuse = f; |
| 899 | c->ctx.private_data = f->user_data; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 900 | |
| 901 | if (f->op.destroy) |
| 902 | f->op.destroy(f->user_data); |
| 903 | } |
| 904 | |
| 905 | static void fuse_lookup(fuse_req_t req, fuse_ino_t parent, const char *name) |
| 906 | { |
| 907 | struct fuse *f = req_fuse_prepare(req); |
| 908 | struct fuse_entry_param e; |
| 909 | char *path; |
| 910 | int err; |
| 911 | |
| 912 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 913 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 914 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 915 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 916 | if (f->conf.debug) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 917 | printf("LOOKUP %s\n", path); |
| 918 | fflush(stdout); |
| 919 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 920 | err = -ENOSYS; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 921 | if (f->op.getattr) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 922 | err = lookup_path(f, req, parent, name, path, &e, NULL); |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 923 | if (err == -ENOENT && f->conf.negative_timeout != 0.0) { |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 924 | e.ino = 0; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 925 | e.entry_timeout = f->conf.negative_timeout; |
Miklos Szeredi | 2b47811 | 2005-11-28 13:27:10 +0000 | [diff] [blame] | 926 | err = 0; |
| 927 | } |
| 928 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 929 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 930 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 931 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 932 | reply_entry(req, &e, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 933 | } |
| 934 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 935 | 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] | 936 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 937 | struct fuse *f = req_fuse(req); |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 938 | if (f->conf.debug) { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 939 | printf("FORGET %llu/%lu\n", (unsigned long long) ino, nlookup); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 940 | fflush(stdout); |
| 941 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 942 | forget_node(f, ino, nlookup); |
| 943 | fuse_reply_none(req); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 944 | } |
| 945 | |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 946 | static void fuse_getattr(fuse_req_t req, fuse_ino_t ino, |
| 947 | struct fuse_file_info *fi) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 948 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 949 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 950 | struct stat buf; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 951 | char *path; |
| 952 | int err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 953 | |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 954 | (void) fi; |
Miklos Szeredi | 16dbf94 | 2006-09-02 13:20:40 +0000 | [diff] [blame] | 955 | memset(&buf, 0, sizeof(buf)); |
Miklos Szeredi | ecce1bf | 2005-08-25 15:19:06 +0000 | [diff] [blame] | 956 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 957 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 958 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 959 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 960 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 961 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 962 | if (f->op.getattr) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 963 | err = fuse_do_getattr(f, req, path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 964 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 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 | if (!err) { |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 968 | if (f->conf.auto_cache) { |
| 969 | pthread_mutex_lock(&f->lock); |
| 970 | update_stat(get_node(f, ino), &buf); |
| 971 | pthread_mutex_unlock(&f->lock); |
| 972 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 973 | set_stat(f, ino, &buf); |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 974 | fuse_reply_attr(req, &buf, f->conf.attr_timeout); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 975 | } else |
| 976 | reply_err(req, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 977 | } |
| 978 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 979 | static int do_chmod(struct fuse *f, fuse_req_t req, const char *path, |
| 980 | struct stat *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 981 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 982 | int err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 983 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 984 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 985 | if (f->op.chmod) { |
| 986 | struct fuse_intr_data d; |
| 987 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 988 | err = f->op.chmod(path, attr->st_mode); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 989 | fuse_finish_interrupt(f, req, &d); |
| 990 | } |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 991 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 992 | return err; |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 993 | } |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 994 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 995 | static int do_chown(struct fuse *f, fuse_req_t req, const char *path, |
| 996 | struct stat *attr, int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 997 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 998 | int err; |
| 999 | uid_t uid = (valid & FUSE_SET_ATTR_UID) ? attr->st_uid : (uid_t) -1; |
| 1000 | 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] | 1001 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1002 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1003 | if (f->op.chown) { |
| 1004 | struct fuse_intr_data d; |
| 1005 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1006 | err = f->op.chown(path, uid, gid); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1007 | fuse_finish_interrupt(f, req, &d); |
| 1008 | } |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 1009 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1010 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 1011 | } |
| 1012 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1013 | static int do_truncate(struct fuse *f, fuse_req_t req, const char *path, |
| 1014 | struct stat *attr, struct fuse_file_info *fi) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 1015 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1016 | int err; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1017 | struct fuse_intr_data d; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 1018 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1019 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1020 | if (fi && f->op.ftruncate) { |
| 1021 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | 11509ce | 2005-10-26 16:04:04 +0000 | [diff] [blame] | 1022 | err = f->op.ftruncate(path, attr->st_size, fi); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1023 | fuse_finish_interrupt(f, req, &d); |
| 1024 | } else if (f->op.truncate) { |
| 1025 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1026 | err = f->op.truncate(path, attr->st_size); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1027 | fuse_finish_interrupt(f, req, &d); |
| 1028 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1029 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 1030 | } |
| 1031 | |
Miklos Szeredi | c3b7681 | 2006-09-16 08:52:09 +0000 | [diff] [blame] | 1032 | static int do_utimens(struct fuse *f, fuse_req_t req, const char *path, |
| 1033 | struct stat *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 1034 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1035 | int err; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1036 | struct fuse_intr_data d; |
Miklos Szeredi | fa44077 | 2006-09-02 09:51:08 +0000 | [diff] [blame] | 1037 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1038 | err = -ENOSYS; |
Miklos Szeredi | c3b7681 | 2006-09-16 08:52:09 +0000 | [diff] [blame] | 1039 | if (f->op.utimens) { |
Miklos Szeredi | fa44077 | 2006-09-02 09:51:08 +0000 | [diff] [blame] | 1040 | struct timespec tv[2]; |
Csaba Henk | 3e3a125 | 2006-09-24 14:53:29 +0000 | [diff] [blame] | 1041 | #ifdef FUSE_STAT_HAS_NANOSEC |
| 1042 | tv[0] = ST_ATIM(attr); |
| 1043 | tv[1] = ST_MTIM(attr); |
| 1044 | #else |
| 1045 | tv[0].tv_sec = attr->st_atime; |
| 1046 | tv[0].tv_nsec = 0; |
| 1047 | tv[1].tv_sec = attr->st_mtime; |
| 1048 | tv[1].tv_nsec = 0; |
| 1049 | #endif |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1050 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | c3b7681 | 2006-09-16 08:52:09 +0000 | [diff] [blame] | 1051 | err = f->op.utimens(path, tv); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1052 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | fa44077 | 2006-09-02 09:51:08 +0000 | [diff] [blame] | 1053 | } else if (f->op.utime) { |
| 1054 | struct utimbuf buf; |
| 1055 | buf.actime = attr->st_atime; |
| 1056 | buf.modtime = attr->st_mtime; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1057 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1058 | err = f->op.utime(path, &buf); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1059 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | fa44077 | 2006-09-02 09:51:08 +0000 | [diff] [blame] | 1060 | } |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 1061 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1062 | return err; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 1063 | } |
| 1064 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1065 | 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] | 1066 | int valid, struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1067 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1068 | struct fuse *f = req_fuse_prepare(req); |
| 1069 | struct stat buf; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1070 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1071 | int err; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1072 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1073 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1074 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1075 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1076 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1077 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1078 | if (f->op.getattr) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1079 | err = 0; |
| 1080 | if (!err && (valid & FUSE_SET_ATTR_MODE)) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1081 | err = do_chmod(f, req, path, attr); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1082 | if (!err && (valid & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID))) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1083 | err = do_chown(f, req, path, attr, valid); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1084 | if (!err && (valid & FUSE_SET_ATTR_SIZE)) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1085 | err = do_truncate(f, req, path, attr, fi); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1086 | if (!err && (valid & (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) == (FUSE_SET_ATTR_ATIME | FUSE_SET_ATTR_MTIME)) |
Miklos Szeredi | c3b7681 | 2006-09-16 08:52:09 +0000 | [diff] [blame] | 1087 | err = do_utimens(f, req, path, attr); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1088 | if (!err) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1089 | err = fuse_do_getattr(f, req, path, &buf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1090 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1091 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1092 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1093 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1094 | if (!err) { |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 1095 | if (f->conf.auto_cache) { |
| 1096 | pthread_mutex_lock(&f->lock); |
| 1097 | update_stat(get_node(f, ino), &buf); |
| 1098 | pthread_mutex_unlock(&f->lock); |
| 1099 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1100 | set_stat(f, ino, &buf); |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1101 | fuse_reply_attr(req, &buf, f->conf.attr_timeout); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1102 | } else |
| 1103 | reply_err(req, err); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1104 | } |
| 1105 | |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 1106 | static void fuse_access(fuse_req_t req, fuse_ino_t ino, int mask) |
| 1107 | { |
| 1108 | struct fuse *f = req_fuse_prepare(req); |
| 1109 | char *path; |
| 1110 | int err; |
| 1111 | |
| 1112 | err = -ENOENT; |
| 1113 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1114 | path = get_path(f, ino); |
| 1115 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1116 | if (f->conf.debug) { |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 1117 | printf("ACCESS %s 0%o\n", path, mask); |
| 1118 | fflush(stdout); |
| 1119 | } |
| 1120 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1121 | if (f->op.access) { |
| 1122 | struct fuse_intr_data d; |
| 1123 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 1124 | err = f->op.access(path, mask); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1125 | fuse_finish_interrupt(f, req, &d); |
| 1126 | } |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 1127 | free(path); |
| 1128 | } |
| 1129 | pthread_rwlock_unlock(&f->tree_lock); |
| 1130 | reply_err(req, err); |
| 1131 | } |
| 1132 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1133 | static void fuse_readlink(fuse_req_t req, fuse_ino_t ino) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1134 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1135 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1136 | char linkname[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1137 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1138 | int err; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1139 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1140 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1141 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1142 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1143 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1144 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1145 | if (f->op.readlink) { |
| 1146 | struct fuse_intr_data d; |
| 1147 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1148 | err = f->op.readlink(path, linkname, sizeof(linkname)); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1149 | fuse_finish_interrupt(f, req, &d); |
| 1150 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1151 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1152 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1153 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1154 | if (!err) { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1155 | linkname[PATH_MAX] = '\0'; |
| 1156 | fuse_reply_readlink(req, linkname); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1157 | } else |
| 1158 | reply_err(req, err); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1159 | } |
| 1160 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1161 | static void fuse_mknod(fuse_req_t req, fuse_ino_t parent, const char *name, |
| 1162 | mode_t mode, dev_t rdev) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1163 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1164 | struct fuse *f = req_fuse_prepare(req); |
| 1165 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1166 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1167 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1168 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1169 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1170 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1171 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1172 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1173 | if (f->conf.debug) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1174 | printf("MKNOD %s\n", path); |
| 1175 | fflush(stdout); |
| 1176 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1177 | err = -ENOSYS; |
Miklos Szeredi | b3f9972 | 2005-11-16 13:00:24 +0000 | [diff] [blame] | 1178 | if (S_ISREG(mode) && f->op.create && f->op.getattr) { |
| 1179 | struct fuse_file_info fi; |
| 1180 | |
| 1181 | memset(&fi, 0, sizeof(fi)); |
| 1182 | fi.flags = O_CREAT | O_EXCL | O_WRONLY; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1183 | err = fuse_do_create(f, req, path, mode, &fi); |
Miklos Szeredi | b3f9972 | 2005-11-16 13:00:24 +0000 | [diff] [blame] | 1184 | if (!err) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1185 | err = lookup_path(f, req, parent, name, path, &e, &fi); |
Miklos Szeredi | b3f9972 | 2005-11-16 13:00:24 +0000 | [diff] [blame] | 1186 | if (f->op.release) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1187 | fuse_do_release(f, req, path, &fi); |
Miklos Szeredi | b3f9972 | 2005-11-16 13:00:24 +0000 | [diff] [blame] | 1188 | } |
| 1189 | } else if (f->op.mknod && f->op.getattr) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1190 | struct fuse_intr_data d; |
| 1191 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1192 | err = f->op.mknod(path, mode, rdev); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1193 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1194 | if (!err) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1195 | err = lookup_path(f, req, parent, name, path, &e, NULL); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1196 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1197 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1198 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1199 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1200 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1201 | } |
| 1202 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1203 | static void fuse_mkdir(fuse_req_t req, fuse_ino_t parent, const char *name, |
| 1204 | mode_t mode) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1205 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1206 | struct fuse *f = req_fuse_prepare(req); |
| 1207 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1208 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1209 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1210 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1211 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1212 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1213 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1214 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1215 | if (f->conf.debug) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1216 | printf("MKDIR %s\n", path); |
| 1217 | fflush(stdout); |
| 1218 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1219 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1220 | if (f->op.mkdir && f->op.getattr) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1221 | struct fuse_intr_data d; |
| 1222 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1223 | err = f->op.mkdir(path, mode); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1224 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1225 | if (!err) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1226 | err = lookup_path(f, req, parent, name, path, &e, NULL); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1227 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1228 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1229 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1230 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1231 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1232 | } |
| 1233 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1234 | 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] | 1235 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1236 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1237 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1238 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1239 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1240 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1241 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1242 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1243 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1244 | if (f->conf.debug) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1245 | printf("UNLINK %s\n", path); |
| 1246 | fflush(stdout); |
| 1247 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1248 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1249 | if (f->op.unlink) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1250 | if (!f->conf.hard_remove && is_open(f, parent, name)) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1251 | err = hide_node(f, req, path, parent, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1252 | else { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1253 | err = fuse_do_unlink(f, req, path); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1254 | if (!err) |
| 1255 | remove_node(f, parent, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1256 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1257 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1258 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1259 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1260 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1261 | reply_err(req, err); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1262 | } |
| 1263 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1264 | 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] | 1265 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1266 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1267 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1268 | int err; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1269 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1270 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1271 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1272 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1273 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1274 | if (f->conf.debug) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1275 | printf("RMDIR %s\n", path); |
| 1276 | fflush(stdout); |
| 1277 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1278 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1279 | if (f->op.rmdir) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1280 | struct fuse_intr_data d; |
| 1281 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1282 | err = f->op.rmdir(path); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1283 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1284 | if (!err) |
| 1285 | remove_node(f, parent, name); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1286 | } |
| 1287 | free(path); |
| 1288 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1289 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1290 | reply_err(req, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1291 | } |
| 1292 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1293 | static void fuse_symlink(fuse_req_t req, const char *linkname, |
| 1294 | fuse_ino_t parent, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1295 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1296 | struct fuse *f = req_fuse_prepare(req); |
| 1297 | struct fuse_entry_param e; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1298 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1299 | int err; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1300 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1301 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1302 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1303 | path = get_path_name(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1304 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1305 | if (f->conf.debug) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1306 | printf("SYMLINK %s\n", path); |
| 1307 | fflush(stdout); |
| 1308 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1309 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1310 | if (f->op.symlink && f->op.getattr) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1311 | struct fuse_intr_data d; |
| 1312 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1313 | err = f->op.symlink(linkname, path); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1314 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1315 | if (!err) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1316 | err = lookup_path(f, req, parent, name, path, &e, NULL); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1317 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1318 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1319 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1320 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1321 | reply_entry(req, &e, err); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1322 | } |
| 1323 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1324 | static void fuse_rename(fuse_req_t req, fuse_ino_t olddir, const char *oldname, |
| 1325 | fuse_ino_t newdir, const char *newname) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1326 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1327 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1328 | char *oldpath; |
| 1329 | char *newpath; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1330 | int err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1331 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1332 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1333 | pthread_rwlock_wrlock(&f->tree_lock); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1334 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1335 | if (oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1336 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1337 | if (newpath != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1338 | if (f->conf.debug) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1339 | printf("RENAME %s -> %s\n", oldpath, newpath); |
| 1340 | fflush(stdout); |
| 1341 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1342 | err = -ENOSYS; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1343 | if (f->op.rename) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1344 | err = 0; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1345 | if (!f->conf.hard_remove && |
Miklos Szeredi | 2529ca2 | 2004-07-13 15:36:52 +0000 | [diff] [blame] | 1346 | is_open(f, newdir, newname)) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1347 | err = hide_node(f, req, newpath, newdir, newname); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1348 | if (!err) { |
Miklos Szeredi | bcfa416 | 2006-10-13 19:48:19 +0000 | [diff] [blame^] | 1349 | err = fuse_do_rename(f, req, oldpath, newpath); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1350 | if (!err) |
| 1351 | err = rename_node(f, olddir, oldname, newdir, newname, 0); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1352 | } |
| 1353 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1354 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1355 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1356 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1357 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1358 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1359 | reply_err(req, err); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1360 | } |
| 1361 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1362 | static void fuse_link(fuse_req_t req, fuse_ino_t ino, fuse_ino_t newparent, |
| 1363 | const char *newname) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1364 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1365 | struct fuse *f = req_fuse_prepare(req); |
| 1366 | struct fuse_entry_param e; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1367 | char *oldpath; |
| 1368 | char *newpath; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1369 | int err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1370 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1371 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1372 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1373 | oldpath = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1374 | if (oldpath != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1375 | newpath = get_path_name(f, newparent, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1376 | if (newpath != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1377 | if (f->conf.debug) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1378 | printf("LINK %s\n", newpath); |
| 1379 | fflush(stdout); |
| 1380 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1381 | err = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1382 | if (f->op.link && f->op.getattr) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1383 | struct fuse_intr_data d; |
| 1384 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1385 | err = f->op.link(oldpath, newpath); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1386 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1387 | if (!err) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1388 | err = lookup_path(f, req, newparent, newname, newpath, &e, |
| 1389 | NULL); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1390 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1391 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1392 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1393 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1394 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1395 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1396 | reply_entry(req, &e, err); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1397 | } |
| 1398 | |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1399 | static void fuse_create(fuse_req_t req, fuse_ino_t parent, const char *name, |
| 1400 | mode_t mode, struct fuse_file_info *fi) |
| 1401 | { |
| 1402 | struct fuse *f = req_fuse_prepare(req); |
| 1403 | struct fuse_entry_param e; |
| 1404 | char *path; |
| 1405 | int err; |
| 1406 | |
| 1407 | err = -ENOENT; |
| 1408 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1409 | path = get_path_name(f, parent, name); |
| 1410 | if (path != NULL) { |
| 1411 | err = -ENOSYS; |
| 1412 | if (f->op.create && f->op.getattr) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1413 | err = fuse_do_create(f, req, path, mode, fi); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1414 | if (!err) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1415 | if (f->conf.debug) { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 1416 | printf("CREATE[%llu] flags: 0x%x %s\n", |
| 1417 | (unsigned long long) fi->fh, fi->flags, path); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1418 | fflush(stdout); |
| 1419 | } |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1420 | err = lookup_path(f, req, parent, name, path, &e, fi); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1421 | if (err) { |
| 1422 | if (f->op.release) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1423 | fuse_do_release(f, req, path, fi); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1424 | } else if (!S_ISREG(e.attr.st_mode)) { |
| 1425 | err = -EIO; |
| 1426 | if (f->op.release) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1427 | fuse_do_release(f, req, path, fi); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1428 | forget_node(f, e.ino, 1); |
| 1429 | } |
| 1430 | } |
| 1431 | } |
| 1432 | } |
| 1433 | |
| 1434 | if (!err) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1435 | if (f->conf.direct_io) |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1436 | fi->direct_io = 1; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1437 | if (f->conf.kernel_cache) |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1438 | fi->keep_cache = 1; |
| 1439 | |
| 1440 | pthread_mutex_lock(&f->lock); |
| 1441 | if (fuse_reply_create(req, &e, fi) == -ENOENT) { |
| 1442 | /* The open syscall was interrupted, so it must be cancelled */ |
| 1443 | if(f->op.release) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1444 | fuse_do_release(f, req, path, fi); |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 1445 | forget_node(f, e.ino, 1); |
| 1446 | } else { |
| 1447 | struct node *node = get_node(f, e.ino); |
| 1448 | node->open_count ++; |
| 1449 | } |
| 1450 | pthread_mutex_unlock(&f->lock); |
| 1451 | } else |
| 1452 | reply_err(req, err); |
| 1453 | |
| 1454 | if (path) |
| 1455 | free(path); |
| 1456 | pthread_rwlock_unlock(&f->tree_lock); |
| 1457 | } |
| 1458 | |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 1459 | static double diff_timespec(const struct timespec *t1, |
| 1460 | const struct timespec *t2) |
| 1461 | { |
| 1462 | return (t1->tv_sec - t2->tv_sec) + |
| 1463 | ((double) t1->tv_nsec - (double) t2->tv_nsec) / 1000000000.0; |
| 1464 | } |
| 1465 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1466 | static void open_auto_cache(struct fuse *f, fuse_req_t req, fuse_ino_t ino, |
| 1467 | const char *path, struct fuse_file_info *fi) |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 1468 | { |
| 1469 | struct node *node = get_node(f, ino); |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 1470 | if (node->cache_valid) { |
| 1471 | struct timespec now; |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 1472 | |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 1473 | curr_time(&now); |
Miklos Szeredi | 6e806e9 | 2006-02-16 16:59:39 +0000 | [diff] [blame] | 1474 | if (diff_timespec(&now, &node->stat_updated) > f->conf.ac_attr_timeout) { |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 1475 | struct stat stbuf; |
| 1476 | int err; |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 1477 | |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 1478 | if (f->op.fgetattr) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1479 | err = fuse_do_fgetattr(f, req, path, &stbuf, fi); |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 1480 | else |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1481 | err = fuse_do_getattr(f, req, path, &stbuf); |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 1482 | |
| 1483 | if (!err) |
| 1484 | update_stat(node, &stbuf); |
| 1485 | else |
| 1486 | node->cache_valid = 0; |
| 1487 | } |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 1488 | } |
| 1489 | if (node->cache_valid) |
| 1490 | fi->keep_cache = 1; |
Miklos Szeredi | 08dab16 | 2006-02-01 13:39:15 +0000 | [diff] [blame] | 1491 | |
| 1492 | node->cache_valid = 1; |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 1493 | } |
| 1494 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1495 | static void fuse_open(fuse_req_t req, fuse_ino_t ino, |
| 1496 | struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1497 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1498 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 31066bb | 2005-08-01 14:49:31 +0000 | [diff] [blame] | 1499 | char *path = NULL; |
| 1500 | int err = 0; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1501 | |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1502 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | 31066bb | 2005-08-01 14:49:31 +0000 | [diff] [blame] | 1503 | if (f->op.open) { |
| 1504 | err = -ENOENT; |
| 1505 | path = get_path(f, ino); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1506 | if (path != NULL) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1507 | err = fuse_compat_open(f, req, path, fi); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 1508 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1509 | if (!err) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1510 | if (f->conf.debug) { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 1511 | printf("OPEN[%llu] flags: 0x%x\n", (unsigned long long) fi->fh, |
| 1512 | fi->flags); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1513 | fflush(stdout); |
| 1514 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1515 | |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1516 | if (f->conf.direct_io) |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 1517 | fi->direct_io = 1; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1518 | if (f->conf.kernel_cache) |
Miklos Szeredi | e77cc07 | 2005-08-01 11:58:51 +0000 | [diff] [blame] | 1519 | fi->keep_cache = 1; |
| 1520 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1521 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 1522 | if (f->conf.auto_cache) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1523 | open_auto_cache(f, req, ino, path, fi); |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 1524 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1525 | if (fuse_reply_open(req, fi) == -ENOENT) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1526 | /* The open syscall was interrupted, so it must be cancelled */ |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1527 | if(f->op.release && path != NULL) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1528 | fuse_compat_release(f, req, path, fi); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1529 | } else { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1530 | struct node *node = get_node(f, ino); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1531 | node->open_count ++; |
| 1532 | } |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1533 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1534 | } else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1535 | reply_err(req, err); |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1536 | |
Miklos Szeredi | 9a31cca | 2004-06-26 21:11:25 +0000 | [diff] [blame] | 1537 | if (path) |
| 1538 | free(path); |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1539 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1540 | } |
| 1541 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1542 | static void fuse_read(fuse_req_t req, fuse_ino_t ino, size_t size, off_t off, |
| 1543 | struct fuse_file_info *fi) |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1544 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1545 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1546 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1547 | char *buf; |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1548 | int res; |
| 1549 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1550 | buf = (char *) malloc(size); |
| 1551 | if (buf == NULL) { |
| 1552 | reply_err(req, -ENOMEM); |
| 1553 | return; |
| 1554 | } |
| 1555 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1556 | res = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1557 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1558 | path = get_path(f, ino); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1559 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1560 | if (f->conf.debug) { |
Miklos Szeredi | a039f8f | 2006-02-24 16:21:58 +0000 | [diff] [blame] | 1561 | printf("READ[%llu] %lu bytes from %llu\n", |
| 1562 | (unsigned long long) fi->fh, (unsigned long) size, |
| 1563 | (unsigned long long) off); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1564 | fflush(stdout); |
| 1565 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1566 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1567 | res = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1568 | if (f->op.read) { |
| 1569 | struct fuse_intr_data d; |
| 1570 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1571 | res = f->op.read(path, buf, size, off, fi); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1572 | fuse_finish_interrupt(f, req, &d); |
| 1573 | } |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1574 | free(path); |
| 1575 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1576 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1577 | |
| 1578 | if (res >= 0) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1579 | if (f->conf.debug) { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 1580 | printf(" READ[%llu] %u bytes\n", (unsigned long long) fi->fh, |
| 1581 | res); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1582 | fflush(stdout); |
| 1583 | } |
Miklos Szeredi | f412d07 | 2005-10-14 21:24:32 +0000 | [diff] [blame] | 1584 | if ((size_t) res > size) |
| 1585 | fprintf(stderr, "fuse: read too many bytes"); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1586 | fuse_reply_buf(req, buf, res); |
| 1587 | } else |
| 1588 | reply_err(req, res); |
| 1589 | |
| 1590 | free(buf); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1591 | } |
| 1592 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1593 | static void fuse_write(fuse_req_t req, fuse_ino_t ino, const char *buf, |
| 1594 | size_t size, off_t off, struct fuse_file_info *fi) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1595 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1596 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1597 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1598 | int res; |
| 1599 | |
| 1600 | res = -ENOENT; |
| 1601 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1602 | path = get_path(f, ino); |
| 1603 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1604 | if (f->conf.debug) { |
Miklos Szeredi | a039f8f | 2006-02-24 16:21:58 +0000 | [diff] [blame] | 1605 | printf("WRITE%s[%llu] %lu bytes to %llu\n", |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 1606 | fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh, |
Miklos Szeredi | a039f8f | 2006-02-24 16:21:58 +0000 | [diff] [blame] | 1607 | (unsigned long) size, (unsigned long long) off); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1608 | fflush(stdout); |
| 1609 | } |
| 1610 | |
| 1611 | res = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1612 | if (f->op.write) { |
| 1613 | struct fuse_intr_data d; |
| 1614 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1615 | res = f->op.write(path, buf, size, off, fi); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1616 | fuse_finish_interrupt(f, req, &d); |
| 1617 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1618 | free(path); |
| 1619 | } |
| 1620 | pthread_rwlock_unlock(&f->tree_lock); |
| 1621 | |
Miklos Szeredi | f412d07 | 2005-10-14 21:24:32 +0000 | [diff] [blame] | 1622 | if (res >= 0) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1623 | if (f->conf.debug) { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 1624 | printf(" WRITE%s[%llu] %u bytes\n", |
| 1625 | fi->writepage ? "PAGE" : "", (unsigned long long) fi->fh, |
| 1626 | res); |
Miklos Szeredi | f412d07 | 2005-10-14 21:24:32 +0000 | [diff] [blame] | 1627 | fflush(stdout); |
| 1628 | } |
| 1629 | if ((size_t) res > size) |
| 1630 | fprintf(stderr, "fuse: wrote too many bytes"); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1631 | fuse_reply_write(req, res); |
Miklos Szeredi | f412d07 | 2005-10-14 21:24:32 +0000 | [diff] [blame] | 1632 | } else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1633 | reply_err(req, res); |
| 1634 | } |
| 1635 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1636 | static void fuse_release(fuse_req_t req, fuse_ino_t ino, |
| 1637 | struct fuse_file_info *fi) |
| 1638 | { |
| 1639 | struct fuse *f = req_fuse_prepare(req); |
| 1640 | char *path; |
| 1641 | struct node *node; |
Miklos Szeredi | c3b7681 | 2006-09-16 08:52:09 +0000 | [diff] [blame] | 1642 | int unlink_hidden = 0; |
Miklos Szeredi | 4fca432 | 2006-10-01 14:41:04 +0000 | [diff] [blame] | 1643 | int err = 0; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1644 | |
Miklos Szeredi | aa8258e | 2006-02-25 14:42:03 +0000 | [diff] [blame] | 1645 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1646 | path = get_path(f, ino); |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1647 | if (f->conf.debug) { |
Miklos Szeredi | 4fca432 | 2006-10-01 14:41:04 +0000 | [diff] [blame] | 1648 | printf("RELEASE%s[%llu] flags: 0x%x\n", fi->flush ? "+FLUSH" : "", |
| 1649 | (unsigned long long) fi->fh, fi->flags); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1650 | fflush(stdout); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1651 | } |
Miklos Szeredi | 4fca432 | 2006-10-01 14:41:04 +0000 | [diff] [blame] | 1652 | if (fi->flush && path && f->op.flush) |
| 1653 | err = fuse_do_flush(f, req, path, fi); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1654 | if (f->op.release) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1655 | fuse_compat_release(f, req, path, fi); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1656 | |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 1657 | pthread_mutex_lock(&f->lock); |
| 1658 | node = get_node(f, ino); |
| 1659 | assert(node->open_count > 0); |
| 1660 | --node->open_count; |
Miklos Szeredi | c3b7681 | 2006-09-16 08:52:09 +0000 | [diff] [blame] | 1661 | if (node->is_hidden && !node->open_count) { |
| 1662 | unlink_hidden = 1; |
| 1663 | node->is_hidden = 0; |
| 1664 | } |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 1665 | pthread_mutex_unlock(&f->lock); |
| 1666 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1667 | if(unlink_hidden && path) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1668 | fuse_do_unlink(f, req, path); |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1669 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1670 | if (path) |
| 1671 | free(path); |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1672 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1673 | |
Miklos Szeredi | 4fca432 | 2006-10-01 14:41:04 +0000 | [diff] [blame] | 1674 | reply_err(req, err); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1675 | } |
| 1676 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1677 | static void fuse_fsync(fuse_req_t req, fuse_ino_t ino, int datasync, |
| 1678 | struct fuse_file_info *fi) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1679 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1680 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1681 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1682 | int err; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1683 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1684 | err = -ENOENT; |
| 1685 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1686 | path = get_path(f, ino); |
| 1687 | if (path != NULL) { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1688 | if (f->conf.debug) { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 1689 | printf("FSYNC[%llu]\n", (unsigned long long) fi->fh); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1690 | fflush(stdout); |
| 1691 | } |
| 1692 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1693 | if (f->op.fsync) { |
| 1694 | struct fuse_intr_data d; |
| 1695 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1696 | err = f->op.fsync(path, datasync, fi); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1697 | fuse_finish_interrupt(f, req, &d); |
| 1698 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1699 | free(path); |
| 1700 | } |
| 1701 | pthread_rwlock_unlock(&f->tree_lock); |
| 1702 | reply_err(req, err); |
| 1703 | } |
| 1704 | |
| 1705 | static struct fuse_dirhandle *get_dirhandle(const struct fuse_file_info *llfi, |
| 1706 | struct fuse_file_info *fi) |
| 1707 | { |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 1708 | struct fuse_dirhandle *dh = (struct fuse_dirhandle *) (uintptr_t) llfi->fh; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1709 | memset(fi, 0, sizeof(struct fuse_file_info)); |
| 1710 | fi->fh = dh->fh; |
Miklos Szeredi | ead7f10 | 2005-11-28 16:02:27 +0000 | [diff] [blame] | 1711 | fi->fh_old = dh->fh; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1712 | return dh; |
| 1713 | } |
| 1714 | |
| 1715 | static void fuse_opendir(fuse_req_t req, fuse_ino_t ino, |
| 1716 | struct fuse_file_info *llfi) |
| 1717 | { |
| 1718 | struct fuse *f = req_fuse_prepare(req); |
| 1719 | struct fuse_dirhandle *dh; |
| 1720 | |
| 1721 | dh = (struct fuse_dirhandle *) malloc(sizeof(struct fuse_dirhandle)); |
| 1722 | if (dh == NULL) { |
| 1723 | reply_err(req, -ENOMEM); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1724 | return; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1725 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1726 | memset(dh, 0, sizeof(struct fuse_dirhandle)); |
| 1727 | dh->fuse = f; |
| 1728 | dh->contents = NULL; |
| 1729 | dh->len = 0; |
| 1730 | dh->filled = 0; |
| 1731 | dh->nodeid = ino; |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 1732 | fuse_mutex_init(&dh->lock); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1733 | |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 1734 | llfi->fh = (uintptr_t) dh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1735 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1736 | if (f->op.opendir) { |
| 1737 | struct fuse_file_info fi; |
| 1738 | char *path; |
| 1739 | int err; |
| 1740 | |
| 1741 | memset(&fi, 0, sizeof(fi)); |
| 1742 | fi.flags = llfi->flags; |
| 1743 | |
| 1744 | err = -ENOENT; |
| 1745 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1746 | path = get_path(f, ino); |
| 1747 | if (path != NULL) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1748 | err = fuse_compat_opendir(f, req, path, &fi); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 1749 | dh->fh = fi.fh; |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1750 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1751 | if (!err) { |
| 1752 | pthread_mutex_lock(&f->lock); |
| 1753 | if (fuse_reply_open(req, llfi) == -ENOENT) { |
| 1754 | /* The opendir syscall was interrupted, so it must be |
| 1755 | cancelled */ |
| 1756 | if(f->op.releasedir) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1757 | fuse_do_releasedir(f, req, path, &fi); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1758 | pthread_mutex_destroy(&dh->lock); |
| 1759 | free(dh); |
| 1760 | } |
| 1761 | pthread_mutex_unlock(&f->lock); |
| 1762 | } else { |
| 1763 | reply_err(req, err); |
| 1764 | free(dh); |
| 1765 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 1766 | free(path); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1767 | pthread_rwlock_unlock(&f->tree_lock); |
| 1768 | } else |
| 1769 | fuse_reply_open(req, llfi); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1770 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1771 | |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1772 | static int extend_contents(struct fuse_dirhandle *dh, unsigned minsize) |
| 1773 | { |
| 1774 | if (minsize > dh->size) { |
| 1775 | char *newptr; |
| 1776 | unsigned newsize = dh->size; |
| 1777 | if (!newsize) |
| 1778 | newsize = 1024; |
| 1779 | while (newsize < minsize) |
| 1780 | newsize *= 2; |
| 1781 | |
| 1782 | newptr = (char *) realloc(dh->contents, newsize); |
| 1783 | if (!newptr) { |
| 1784 | dh->error = -ENOMEM; |
| 1785 | return -1; |
| 1786 | } |
| 1787 | dh->contents = newptr; |
| 1788 | dh->size = newsize; |
| 1789 | } |
| 1790 | return 0; |
| 1791 | } |
| 1792 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1793 | static int fill_dir_common(struct fuse_dirhandle *dh, const char *name, |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1794 | const struct stat *statp, off_t off) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1795 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1796 | struct stat stbuf; |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1797 | size_t newlen; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1798 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1799 | if (statp) |
| 1800 | stbuf = *statp; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1801 | else { |
| 1802 | memset(&stbuf, 0, sizeof(stbuf)); |
Miklos Szeredi | e248e4b | 2005-12-14 16:18:32 +0000 | [diff] [blame] | 1803 | stbuf.st_ino = FUSE_UNKNOWN_INO; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1804 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1805 | |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1806 | if (!dh->fuse->conf.use_ino) { |
Miklos Szeredi | e248e4b | 2005-12-14 16:18:32 +0000 | [diff] [blame] | 1807 | stbuf.st_ino = FUSE_UNKNOWN_INO; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 1808 | if (dh->fuse->conf.readdir_ino) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1809 | struct node *node; |
| 1810 | pthread_mutex_lock(&dh->fuse->lock); |
| 1811 | node = lookup_node(dh->fuse, dh->nodeid, name); |
| 1812 | if (node) |
| 1813 | stbuf.st_ino = (ino_t) node->nodeid; |
| 1814 | pthread_mutex_unlock(&dh->fuse->lock); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1815 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1816 | } |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1817 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1818 | if (off) { |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1819 | if (extend_contents(dh, dh->needlen) == -1) |
| 1820 | return 1; |
| 1821 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1822 | dh->filled = 0; |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1823 | newlen = dh->len + fuse_add_direntry(dh->req, dh->contents + dh->len, |
| 1824 | dh->needlen - dh->len, name, |
| 1825 | &stbuf, off); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1826 | if (newlen > dh->needlen) |
| 1827 | return 1; |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1828 | } else { |
| 1829 | newlen = dh->len + fuse_add_direntry(dh->req, NULL, 0, name, NULL, 0); |
| 1830 | if (extend_contents(dh, newlen) == -1) |
Miklos Szeredi | c4c12ae | 2005-10-20 14:48:50 +0000 | [diff] [blame] | 1831 | return 1; |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1832 | |
| 1833 | fuse_add_direntry(dh->req, dh->contents + dh->len, dh->size - dh->len, |
| 1834 | name, &stbuf, newlen); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1835 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1836 | dh->len = newlen; |
| 1837 | return 0; |
| 1838 | } |
| 1839 | |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1840 | 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] | 1841 | off_t off) |
| 1842 | { |
Miklos Szeredi | f6e0ec6 | 2005-08-03 09:11:06 +0000 | [diff] [blame] | 1843 | return fill_dir_common((struct fuse_dirhandle *) buf, name, stbuf, off); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1844 | } |
| 1845 | |
| 1846 | static int fill_dir_old(struct fuse_dirhandle *dh, const char *name, int type, |
| 1847 | ino_t ino) |
| 1848 | { |
| 1849 | struct stat stbuf; |
| 1850 | |
| 1851 | memset(&stbuf, 0, sizeof(stbuf)); |
| 1852 | stbuf.st_mode = type << 12; |
| 1853 | stbuf.st_ino = ino; |
| 1854 | |
| 1855 | fill_dir_common(dh, name, &stbuf, 0); |
| 1856 | return dh->error; |
| 1857 | } |
| 1858 | |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1859 | static int readdir_fill(struct fuse *f, fuse_req_t req, fuse_ino_t ino, |
| 1860 | size_t size, off_t off, struct fuse_dirhandle *dh, |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1861 | struct fuse_file_info *fi) |
| 1862 | { |
| 1863 | int err = -ENOENT; |
| 1864 | char *path; |
| 1865 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1866 | path = get_path(f, ino); |
| 1867 | if (path != NULL) { |
| 1868 | dh->len = 0; |
| 1869 | dh->error = 0; |
| 1870 | dh->needlen = size; |
| 1871 | dh->filled = 1; |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1872 | dh->req = req; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1873 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1874 | if (f->op.readdir) { |
| 1875 | struct fuse_intr_data d; |
| 1876 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1877 | err = f->op.readdir(path, dh, fill_dir, off, fi); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1878 | fuse_finish_interrupt(f, req, &d); |
| 1879 | } else if (f->op.getdir) { |
| 1880 | struct fuse_intr_data d; |
| 1881 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1882 | err = f->op.getdir(path, dh, fill_dir_old); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1883 | fuse_finish_interrupt(f, req, &d); |
| 1884 | } |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1885 | dh->req = NULL; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1886 | if (!err) |
| 1887 | err = dh->error; |
| 1888 | if (err) |
| 1889 | dh->filled = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1890 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1891 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 1892 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1893 | return err; |
| 1894 | } |
Miklos Szeredi | e518374 | 2005-02-02 11:14:04 +0000 | [diff] [blame] | 1895 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1896 | static void fuse_readdir(fuse_req_t req, fuse_ino_t ino, size_t size, |
| 1897 | off_t off, struct fuse_file_info *llfi) |
| 1898 | { |
| 1899 | struct fuse *f = req_fuse_prepare(req); |
| 1900 | struct fuse_file_info fi; |
| 1901 | struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi); |
| 1902 | |
| 1903 | pthread_mutex_lock(&dh->lock); |
Miklos Szeredi | 77ccf65 | 2005-08-19 14:40:27 +0000 | [diff] [blame] | 1904 | /* According to SUS, directory contents need to be refreshed on |
| 1905 | rewinddir() */ |
| 1906 | if (!off) |
| 1907 | dh->filled = 0; |
| 1908 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1909 | if (!dh->filled) { |
Miklos Szeredi | 1bf64f4 | 2006-02-17 15:49:25 +0000 | [diff] [blame] | 1910 | int err = readdir_fill(f, req, ino, size, off, dh, &fi); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1911 | if (err) { |
| 1912 | reply_err(req, err); |
| 1913 | goto out; |
| 1914 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1915 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1916 | if (dh->filled) { |
| 1917 | if (off < dh->len) { |
| 1918 | if (off + size > dh->len) |
| 1919 | size = dh->len - off; |
| 1920 | } else |
| 1921 | size = 0; |
| 1922 | } else { |
| 1923 | size = dh->len; |
| 1924 | off = 0; |
| 1925 | } |
| 1926 | fuse_reply_buf(req, dh->contents + off, size); |
| 1927 | out: |
| 1928 | pthread_mutex_unlock(&dh->lock); |
| 1929 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1930 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1931 | static void fuse_releasedir(fuse_req_t req, fuse_ino_t ino, |
| 1932 | struct fuse_file_info *llfi) |
| 1933 | { |
| 1934 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 9b813af | 2005-07-21 07:59:37 +0000 | [diff] [blame] | 1935 | struct fuse_file_info fi; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1936 | struct fuse_dirhandle *dh = get_dirhandle(llfi, &fi); |
| 1937 | if (f->op.releasedir) { |
| 1938 | char *path; |
| 1939 | |
| 1940 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1941 | path = get_path(f, ino); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1942 | fuse_do_releasedir(f, req, path ? path : "-", &fi); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1943 | free(path); |
| 1944 | pthread_rwlock_unlock(&f->tree_lock); |
| 1945 | } |
| 1946 | pthread_mutex_lock(&dh->lock); |
| 1947 | pthread_mutex_unlock(&dh->lock); |
| 1948 | pthread_mutex_destroy(&dh->lock); |
| 1949 | free(dh->contents); |
| 1950 | free(dh); |
| 1951 | reply_err(req, 0); |
| 1952 | } |
| 1953 | |
| 1954 | static void fuse_fsyncdir(fuse_req_t req, fuse_ino_t ino, int datasync, |
| 1955 | struct fuse_file_info *llfi) |
| 1956 | { |
| 1957 | struct fuse *f = req_fuse_prepare(req); |
| 1958 | struct fuse_file_info fi; |
| 1959 | char *path; |
| 1960 | int err; |
| 1961 | |
| 1962 | get_dirhandle(llfi, &fi); |
| 1963 | |
| 1964 | err = -ENOENT; |
| 1965 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1966 | path = get_path(f, ino); |
| 1967 | if (path != NULL) { |
| 1968 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1969 | if (f->op.fsyncdir) { |
| 1970 | struct fuse_intr_data d; |
| 1971 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1972 | err = f->op.fsyncdir(path, datasync, &fi); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 1973 | fuse_finish_interrupt(f, req, &d); |
| 1974 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1975 | free(path); |
| 1976 | } |
| 1977 | pthread_rwlock_unlock(&f->tree_lock); |
| 1978 | reply_err(req, err); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1979 | } |
| 1980 | |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 1981 | static int default_statfs(struct statvfs *buf) |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1982 | { |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 1983 | buf->f_namemax = 255; |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1984 | buf->f_bsize = 512; |
| 1985 | return 0; |
| 1986 | } |
| 1987 | |
Miklos Szeredi | 3ded1a3 | 2006-08-18 18:43:50 +0000 | [diff] [blame] | 1988 | static void fuse_statfs(fuse_req_t req, fuse_ino_t ino) |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1989 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1990 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 1991 | struct statvfs buf; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 1992 | int err; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1993 | |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 1994 | memset(&buf, 0, sizeof(buf)); |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 1995 | if (f->op.statfs) { |
Miklos Szeredi | 3ded1a3 | 2006-08-18 18:43:50 +0000 | [diff] [blame] | 1996 | if (ino && (!f->compat || f->compat >= 26)) { |
| 1997 | char *path; |
| 1998 | pthread_rwlock_rdlock(&f->tree_lock); |
| 1999 | err = -ENOENT; |
| 2000 | path = get_path(f, ino); |
| 2001 | if (path) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2002 | err = fuse_do_statfs(f, req, path, &buf); |
Miklos Szeredi | 3ded1a3 | 2006-08-18 18:43:50 +0000 | [diff] [blame] | 2003 | free(path); |
| 2004 | } |
| 2005 | pthread_rwlock_unlock(&f->tree_lock); |
| 2006 | } else |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2007 | err = fuse_compat_statfs(f, req, &buf); |
Miklos Szeredi | 52cb09d | 2005-11-07 11:59:00 +0000 | [diff] [blame] | 2008 | } else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2009 | err = default_statfs(&buf); |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 2010 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2011 | if (!err) |
| 2012 | fuse_reply_statfs(req, &buf); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 2013 | else |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2014 | reply_err(req, err); |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 2015 | } |
| 2016 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2017 | static void fuse_setxattr(fuse_req_t req, fuse_ino_t ino, const char *name, |
| 2018 | const char *value, size_t size, int flags) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 2019 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2020 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 2021 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2022 | int err; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 2023 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2024 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 2025 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2026 | path = get_path(f, ino); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 2027 | if (path != NULL) { |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 2028 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2029 | if (f->op.setxattr) { |
| 2030 | struct fuse_intr_data d; |
| 2031 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2032 | err = f->op.setxattr(path, name, value, size, flags); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2033 | fuse_finish_interrupt(f, req, &d); |
| 2034 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2035 | free(path); |
| 2036 | } |
| 2037 | pthread_rwlock_unlock(&f->tree_lock); |
| 2038 | reply_err(req, err); |
| 2039 | } |
| 2040 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2041 | static int common_getxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino, |
| 2042 | const char *name, char *value, size_t size) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2043 | { |
| 2044 | int err; |
| 2045 | char *path; |
| 2046 | |
| 2047 | err = -ENOENT; |
| 2048 | pthread_rwlock_rdlock(&f->tree_lock); |
| 2049 | path = get_path(f, ino); |
| 2050 | if (path != NULL) { |
| 2051 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2052 | if (f->op.getxattr) { |
| 2053 | struct fuse_intr_data d; |
| 2054 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2055 | err = f->op.getxattr(path, name, value, size); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2056 | fuse_finish_interrupt(f, req, &d); |
| 2057 | } |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 2058 | free(path); |
| 2059 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 2060 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | ab97456 | 2005-04-07 15:40:21 +0000 | [diff] [blame] | 2061 | return err; |
| 2062 | } |
| 2063 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2064 | static void fuse_getxattr(fuse_req_t req, fuse_ino_t ino, const char *name, |
| 2065 | size_t size) |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 2066 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2067 | struct fuse *f = req_fuse_prepare(req); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 2068 | int res; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2069 | |
| 2070 | if (size) { |
| 2071 | char *value = (char *) malloc(size); |
| 2072 | if (value == NULL) { |
| 2073 | reply_err(req, -ENOMEM); |
| 2074 | return; |
| 2075 | } |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2076 | res = common_getxattr(f, req, ino, name, value, size); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2077 | if (res > 0) |
| 2078 | fuse_reply_buf(req, value, res); |
| 2079 | else |
| 2080 | reply_err(req, res); |
| 2081 | free(value); |
| 2082 | } else { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2083 | res = common_getxattr(f, req, ino, name, NULL, 0); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2084 | if (res >= 0) |
| 2085 | fuse_reply_xattr(req, res); |
| 2086 | else |
| 2087 | reply_err(req, res); |
| 2088 | } |
| 2089 | } |
| 2090 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2091 | static int common_listxattr(struct fuse *f, fuse_req_t req, fuse_ino_t ino, |
| 2092 | char *list, size_t size) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2093 | { |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 2094 | char *path; |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2095 | int err; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 2096 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2097 | err = -ENOENT; |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 2098 | pthread_rwlock_rdlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2099 | path = get_path(f, ino); |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 2100 | if (path != NULL) { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2101 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2102 | if (f->op.listxattr) { |
| 2103 | struct fuse_intr_data d; |
| 2104 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2105 | err = f->op.listxattr(path, list, size); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2106 | fuse_finish_interrupt(f, req, &d); |
| 2107 | } |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 2108 | free(path); |
| 2109 | } |
Miklos Szeredi | c2a33ee | 2005-05-09 13:29:17 +0000 | [diff] [blame] | 2110 | pthread_rwlock_unlock(&f->tree_lock); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2111 | return err; |
Miklos Szeredi | 4283ee7 | 2005-03-21 12:09:04 +0000 | [diff] [blame] | 2112 | } |
Miklos Szeredi | 3ead28e | 2005-01-18 21:23:41 +0000 | [diff] [blame] | 2113 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2114 | 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] | 2115 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2116 | struct fuse *f = req_fuse_prepare(req); |
| 2117 | int res; |
| 2118 | |
| 2119 | if (size) { |
| 2120 | char *list = (char *) malloc(size); |
| 2121 | if (list == NULL) { |
| 2122 | reply_err(req, -ENOMEM); |
| 2123 | return; |
| 2124 | } |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2125 | res = common_listxattr(f, req, ino, list, size); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2126 | if (res > 0) |
| 2127 | fuse_reply_buf(req, list, res); |
| 2128 | else |
| 2129 | reply_err(req, res); |
| 2130 | free(list); |
| 2131 | } else { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2132 | res = common_listxattr(f, req, ino, NULL, 0); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2133 | if (res >= 0) |
| 2134 | fuse_reply_xattr(req, res); |
| 2135 | else |
| 2136 | reply_err(req, res); |
| 2137 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 2138 | } |
| 2139 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2140 | static void fuse_removexattr(fuse_req_t req, fuse_ino_t ino, const char *name) |
| 2141 | { |
| 2142 | struct fuse *f = req_fuse_prepare(req); |
| 2143 | char *path; |
| 2144 | int err; |
| 2145 | |
| 2146 | err = -ENOENT; |
| 2147 | pthread_rwlock_rdlock(&f->tree_lock); |
| 2148 | path = get_path(f, ino); |
| 2149 | if (path != NULL) { |
| 2150 | err = -ENOSYS; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2151 | if (f->op.removexattr) { |
| 2152 | struct fuse_intr_data d; |
| 2153 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2154 | err = f->op.removexattr(path, name); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2155 | fuse_finish_interrupt(f, req, &d); |
| 2156 | } |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2157 | free(path); |
| 2158 | } |
| 2159 | pthread_rwlock_unlock(&f->tree_lock); |
| 2160 | reply_err(req, err); |
| 2161 | } |
| 2162 | |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2163 | static struct lock *locks_conflict(struct node *node, const struct lock *lock) |
| 2164 | { |
| 2165 | struct lock *l; |
| 2166 | |
| 2167 | for (l = node->locks; l; l = l->next) |
| 2168 | if (l->owner != lock->owner && |
| 2169 | lock->start <= l->end && l->start <= lock->end && |
| 2170 | (l->type == F_WRLCK || lock->type == F_WRLCK)) |
| 2171 | break; |
| 2172 | |
| 2173 | return l; |
| 2174 | } |
| 2175 | |
| 2176 | static void delete_lock(struct lock **lockp) |
| 2177 | { |
| 2178 | struct lock *l = *lockp; |
| 2179 | *lockp = l->next; |
| 2180 | free(l); |
| 2181 | } |
| 2182 | |
| 2183 | static void insert_lock(struct lock **pos, struct lock *lock) |
| 2184 | { |
| 2185 | lock->next = *pos; |
| 2186 | *pos = lock; |
| 2187 | } |
| 2188 | |
| 2189 | static int locks_insert(struct node *node, struct lock *lock) |
| 2190 | { |
| 2191 | struct lock **lp; |
| 2192 | struct lock *newl1 = NULL; |
| 2193 | struct lock *newl2 = NULL; |
| 2194 | |
| 2195 | if (lock->type != F_UNLCK || lock->start != 0 || lock->end != OFFSET_MAX) { |
| 2196 | newl1 = malloc(sizeof(struct lock)); |
| 2197 | newl2 = malloc(sizeof(struct lock)); |
| 2198 | |
| 2199 | if (!newl1 || !newl2) { |
| 2200 | free(newl1); |
| 2201 | free(newl2); |
| 2202 | return -ENOLCK; |
| 2203 | } |
| 2204 | } |
| 2205 | |
| 2206 | for (lp = &node->locks; *lp;) { |
| 2207 | struct lock *l = *lp; |
| 2208 | if (l->owner != lock->owner) |
| 2209 | goto skip; |
| 2210 | |
| 2211 | if (lock->type == l->type) { |
| 2212 | if (l->end < lock->start - 1) |
| 2213 | goto skip; |
| 2214 | if (lock->end < l->start - 1) |
| 2215 | break; |
| 2216 | if (l->start <= lock->start && lock->end <= l->end) |
| 2217 | goto out; |
| 2218 | if (l->start < lock->start) |
| 2219 | lock->start = l->start; |
| 2220 | if (lock->end < l->end) |
| 2221 | lock->end = l->end; |
| 2222 | goto delete; |
| 2223 | } else { |
| 2224 | if (l->end < lock->start) |
| 2225 | goto skip; |
| 2226 | if (lock->end < l->start) |
| 2227 | break; |
| 2228 | if (lock->start <= l->start && l->end <= lock->end) |
| 2229 | goto delete; |
| 2230 | if (l->end <= lock->end) { |
| 2231 | l->end = lock->start - 1; |
| 2232 | goto skip; |
| 2233 | } |
| 2234 | if (lock->start <= l->start) { |
| 2235 | l->start = lock->end + 1; |
| 2236 | break; |
| 2237 | } |
| 2238 | *newl2 = *l; |
| 2239 | newl2->start = lock->end + 1; |
| 2240 | l->end = lock->start - 1; |
| 2241 | insert_lock(&l->next, newl2); |
| 2242 | newl2 = NULL; |
| 2243 | } |
| 2244 | skip: |
| 2245 | lp = &l->next; |
| 2246 | continue; |
| 2247 | |
| 2248 | delete: |
| 2249 | delete_lock(lp); |
| 2250 | } |
| 2251 | if (lock->type != F_UNLCK) { |
| 2252 | *newl1 = *lock; |
| 2253 | insert_lock(lp, newl1); |
| 2254 | newl1 = NULL; |
| 2255 | } |
| 2256 | out: |
| 2257 | free(newl1); |
| 2258 | free(newl2); |
| 2259 | return 0; |
| 2260 | } |
| 2261 | |
| 2262 | static void flock_to_lock(struct flock *flock, struct lock *lock) |
| 2263 | { |
| 2264 | memset(lock, 0, sizeof(struct lock)); |
| 2265 | lock->type = flock->l_type; |
| 2266 | lock->start = flock->l_start; |
| 2267 | lock->end = flock->l_len ? flock->l_start + flock->l_len - 1 : OFFSET_MAX; |
| 2268 | lock->pid = flock->l_pid; |
| 2269 | } |
| 2270 | |
| 2271 | static void lock_to_flock(struct lock *lock, struct flock *flock) |
| 2272 | { |
| 2273 | flock->l_type = lock->type; |
| 2274 | flock->l_start = lock->start; |
| 2275 | flock->l_len = (lock->end == OFFSET_MAX) ? 0 : lock->end - lock->start + 1; |
| 2276 | flock->l_pid = lock->pid; |
| 2277 | } |
| 2278 | |
| 2279 | static void fuse_flush(fuse_req_t req, fuse_ino_t ino, |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2280 | struct fuse_file_info *fi) |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2281 | { |
| 2282 | struct fuse *f = req_fuse_prepare(req); |
| 2283 | char *path; |
| 2284 | int err; |
| 2285 | |
| 2286 | err = -ENOENT; |
| 2287 | pthread_rwlock_rdlock(&f->tree_lock); |
| 2288 | path = get_path(f, ino); |
| 2289 | if (path != NULL) { |
| 2290 | if (f->conf.debug) { |
| 2291 | printf("FLUSH[%llu]\n", (unsigned long long) fi->fh); |
| 2292 | fflush(stdout); |
| 2293 | } |
| 2294 | err = -ENOSYS; |
Miklos Szeredi | 4fca432 | 2006-10-01 14:41:04 +0000 | [diff] [blame] | 2295 | if (f->op.flush) |
| 2296 | err = fuse_do_flush(f, req, path, fi); |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2297 | free(path); |
| 2298 | } |
| 2299 | if (f->op.lock) { |
| 2300 | struct flock lock; |
| 2301 | struct lock l; |
| 2302 | memset(&lock, 0, sizeof(lock)); |
| 2303 | lock.l_type = F_UNLCK; |
| 2304 | lock.l_whence = SEEK_SET; |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2305 | fuse_do_lock(f, req, path, fi, F_SETLK, &lock); |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2306 | flock_to_lock(&lock, &l); |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2307 | l.owner = fi->lock_owner; |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2308 | pthread_mutex_lock(&f->lock); |
| 2309 | locks_insert(get_node(f, ino), &l); |
| 2310 | pthread_mutex_unlock(&f->lock); |
| 2311 | |
| 2312 | /* if op.lock() is defined FLUSH is needed regardless of op.flush() */ |
| 2313 | if (err == -ENOSYS) |
| 2314 | err = 0; |
| 2315 | } |
| 2316 | pthread_rwlock_unlock(&f->tree_lock); |
| 2317 | reply_err(req, err); |
| 2318 | } |
| 2319 | |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2320 | static int fuse_lock_common(fuse_req_t req, fuse_ino_t ino, |
| 2321 | struct fuse_file_info *fi, struct flock *lock, |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2322 | int cmd) |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2323 | { |
| 2324 | struct fuse *f = req_fuse_prepare(req); |
| 2325 | char *path; |
| 2326 | int err; |
| 2327 | |
| 2328 | err = -ENOENT; |
| 2329 | pthread_rwlock_rdlock(&f->tree_lock); |
| 2330 | path = get_path(f, ino); |
| 2331 | if (path != NULL) { |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2332 | err = fuse_do_lock(f, req, path, fi, cmd, lock); |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2333 | free(path); |
| 2334 | } |
| 2335 | pthread_rwlock_unlock(&f->tree_lock); |
| 2336 | return err; |
| 2337 | } |
| 2338 | |
| 2339 | static void fuse_getlk(fuse_req_t req, fuse_ino_t ino, |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2340 | struct fuse_file_info *fi, struct flock *lock) |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2341 | { |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2342 | int err; |
| 2343 | struct lock l; |
| 2344 | struct lock *conflict; |
| 2345 | struct fuse *f = req_fuse(req); |
| 2346 | |
| 2347 | flock_to_lock(lock, &l); |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2348 | l.owner = fi->lock_owner; |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2349 | pthread_mutex_lock(&f->lock); |
| 2350 | conflict = locks_conflict(get_node(f, ino), &l); |
| 2351 | if (conflict) |
| 2352 | lock_to_flock(conflict, lock); |
| 2353 | pthread_mutex_unlock(&f->lock); |
| 2354 | if (!conflict) |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2355 | err = fuse_lock_common(req, ino, fi, lock, F_GETLK); |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2356 | else |
| 2357 | err = 0; |
| 2358 | |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2359 | if (!err) |
| 2360 | fuse_reply_lock(req, lock); |
| 2361 | else |
| 2362 | reply_err(req, err); |
| 2363 | } |
| 2364 | |
| 2365 | static void fuse_setlk(fuse_req_t req, fuse_ino_t ino, |
| 2366 | struct fuse_file_info *fi, struct flock *lock, |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2367 | int sleep) |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2368 | { |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2369 | int err = fuse_lock_common(req, ino, fi, lock, sleep ? F_SETLKW : F_SETLK); |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2370 | if (!err) { |
| 2371 | struct fuse *f = req_fuse(req); |
| 2372 | struct lock l; |
| 2373 | flock_to_lock(lock, &l); |
Miklos Szeredi | 0740785 | 2006-09-30 20:03:52 +0000 | [diff] [blame] | 2374 | l.owner = fi->lock_owner; |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2375 | pthread_mutex_lock(&f->lock); |
| 2376 | locks_insert(get_node(f, ino), &l); |
| 2377 | pthread_mutex_unlock(&f->lock); |
| 2378 | } |
| 2379 | reply_err(req, err); |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2380 | } |
| 2381 | |
Miklos Szeredi | 708b481 | 2006-09-30 16:02:25 +0000 | [diff] [blame] | 2382 | static void fuse_bmap(fuse_req_t req, fuse_ino_t ino, size_t blocksize, |
| 2383 | uint64_t idx) |
| 2384 | { |
| 2385 | struct fuse *f = req_fuse_prepare(req); |
| 2386 | char *path; |
| 2387 | int err; |
| 2388 | |
| 2389 | err = -ENOENT; |
| 2390 | pthread_rwlock_rdlock(&f->tree_lock); |
| 2391 | path = get_path(f, ino); |
| 2392 | if (path != NULL) { |
| 2393 | err = -ENOSYS; |
| 2394 | if (f->op.bmap) |
| 2395 | err = f->op.bmap(path, blocksize, &idx); |
| 2396 | free(path); |
| 2397 | } |
| 2398 | pthread_rwlock_unlock(&f->tree_lock); |
| 2399 | if (!err) |
| 2400 | fuse_reply_bmap(req, idx); |
| 2401 | else |
| 2402 | reply_err(req, err); |
| 2403 | } |
| 2404 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2405 | static struct fuse_lowlevel_ops fuse_path_ops = { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2406 | .init = fuse_data_init, |
| 2407 | .destroy = fuse_data_destroy, |
| 2408 | .lookup = fuse_lookup, |
| 2409 | .forget = fuse_forget, |
| 2410 | .getattr = fuse_getattr, |
| 2411 | .setattr = fuse_setattr, |
Miklos Szeredi | b0b13d1 | 2005-10-26 12:53:25 +0000 | [diff] [blame] | 2412 | .access = fuse_access, |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2413 | .readlink = fuse_readlink, |
| 2414 | .mknod = fuse_mknod, |
| 2415 | .mkdir = fuse_mkdir, |
| 2416 | .unlink = fuse_unlink, |
| 2417 | .rmdir = fuse_rmdir, |
| 2418 | .symlink = fuse_symlink, |
| 2419 | .rename = fuse_rename, |
| 2420 | .link = fuse_link, |
Miklos Szeredi | d9079a7 | 2005-10-26 15:29:06 +0000 | [diff] [blame] | 2421 | .create = fuse_create, |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2422 | .open = fuse_open, |
| 2423 | .read = fuse_read, |
| 2424 | .write = fuse_write, |
| 2425 | .flush = fuse_flush, |
| 2426 | .release = fuse_release, |
| 2427 | .fsync = fuse_fsync, |
| 2428 | .opendir = fuse_opendir, |
| 2429 | .readdir = fuse_readdir, |
| 2430 | .releasedir = fuse_releasedir, |
| 2431 | .fsyncdir = fuse_fsyncdir, |
| 2432 | .statfs = fuse_statfs, |
| 2433 | .setxattr = fuse_setxattr, |
| 2434 | .getxattr = fuse_getxattr, |
| 2435 | .listxattr = fuse_listxattr, |
| 2436 | .removexattr = fuse_removexattr, |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2437 | .getlk = fuse_getlk, |
| 2438 | .setlk = fuse_setlk, |
Miklos Szeredi | 708b481 | 2006-09-30 16:02:25 +0000 | [diff] [blame] | 2439 | .bmap = fuse_bmap, |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2440 | }; |
| 2441 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2442 | static void free_cmd(struct fuse_cmd *cmd) |
| 2443 | { |
| 2444 | free(cmd->buf); |
| 2445 | free(cmd); |
| 2446 | } |
| 2447 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2448 | void fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 2449 | { |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 2450 | fuse_session_process(f->se, cmd->buf, cmd->buflen, cmd->ch); |
Miklos Szeredi | fa829b5 | 2005-12-02 11:05:41 +0000 | [diff] [blame] | 2451 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 2452 | } |
| 2453 | |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2454 | int fuse_exited(struct fuse *f) |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 2455 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2456 | return fuse_session_exited(f->se); |
| 2457 | } |
| 2458 | |
| 2459 | struct fuse_session *fuse_get_session(struct fuse *f) |
| 2460 | { |
| 2461 | return f->se; |
| 2462 | } |
| 2463 | |
| 2464 | static struct fuse_cmd *fuse_alloc_cmd(size_t bufsize) |
| 2465 | { |
| 2466 | struct fuse_cmd *cmd = (struct fuse_cmd *) malloc(sizeof(*cmd)); |
| 2467 | if (cmd == NULL) { |
| 2468 | fprintf(stderr, "fuse: failed to allocate cmd\n"); |
| 2469 | return NULL; |
| 2470 | } |
| 2471 | cmd->buf = (char *) malloc(bufsize); |
| 2472 | if (cmd->buf == NULL) { |
| 2473 | fprintf(stderr, "fuse: failed to allocate read buffer\n"); |
| 2474 | free(cmd); |
| 2475 | return NULL; |
| 2476 | } |
| 2477 | return cmd; |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 2478 | } |
| 2479 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2480 | struct fuse_cmd *fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 2481 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2482 | struct fuse_chan *ch = fuse_session_next_chan(f->se, NULL); |
| 2483 | size_t bufsize = fuse_chan_bufsize(ch); |
| 2484 | struct fuse_cmd *cmd = fuse_alloc_cmd(bufsize); |
| 2485 | if (cmd != NULL) { |
Miklos Szeredi | 8d975f6 | 2006-03-17 15:56:05 +0000 | [diff] [blame] | 2486 | int res = fuse_chan_recv(&ch, cmd->buf, bufsize); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2487 | if (res <= 0) { |
| 2488 | free_cmd(cmd); |
Miklos Szeredi | 5d9ce36 | 2006-03-01 12:10:13 +0000 | [diff] [blame] | 2489 | if (res < 0 && res != -EINTR && res != -EAGAIN) |
Miklos Szeredi | fa829b5 | 2005-12-02 11:05:41 +0000 | [diff] [blame] | 2490 | fuse_exit(f); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2491 | return NULL; |
| 2492 | } |
| 2493 | cmd->buflen = res; |
Miklos Szeredi | 178451d | 2005-08-15 13:19:07 +0000 | [diff] [blame] | 2494 | cmd->ch = ch; |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2495 | } |
| 2496 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 2497 | } |
| 2498 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2499 | int fuse_loop(struct fuse *f) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2500 | { |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2501 | if (f) |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2502 | return fuse_session_loop(f->se); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2503 | else |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2504 | return -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2505 | } |
| 2506 | |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 2507 | int fuse_invalidate(struct fuse *f, const char *path) |
| 2508 | { |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2509 | (void) f; |
| 2510 | (void) path; |
| 2511 | return -EINVAL; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 2512 | } |
| 2513 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 2514 | void fuse_exit(struct fuse *f) |
| 2515 | { |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2516 | fuse_session_exit(f->se); |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 2517 | } |
| 2518 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2519 | struct fuse_context *fuse_get_context(void) |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 2520 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2521 | return &fuse_get_context_internal()->ctx; |
| 2522 | } |
| 2523 | |
| 2524 | int fuse_interrupted(void) |
| 2525 | { |
| 2526 | return fuse_req_interrupted(fuse_get_context_internal()->req); |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 2527 | } |
| 2528 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2529 | void fuse_set_getcontext_func(struct fuse_context *(*func)(void)) |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 2530 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2531 | (void) func; |
| 2532 | /* no-op */ |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 2533 | } |
| 2534 | |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 2535 | enum { |
| 2536 | KEY_HELP, |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 2537 | }; |
| 2538 | |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2539 | #define FUSE_LIB_OPT(t, p, v) { t, offsetof(struct fuse_config, p), v } |
| 2540 | |
| 2541 | static const struct fuse_opt fuse_lib_opts[] = { |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 2542 | FUSE_OPT_KEY("-h", KEY_HELP), |
| 2543 | FUSE_OPT_KEY("--help", KEY_HELP), |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 2544 | FUSE_OPT_KEY("debug", FUSE_OPT_KEY_KEEP), |
| 2545 | FUSE_OPT_KEY("-d", FUSE_OPT_KEY_KEEP), |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2546 | FUSE_LIB_OPT("debug", debug, 1), |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 2547 | FUSE_LIB_OPT("-d", debug, 1), |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2548 | FUSE_LIB_OPT("hard_remove", hard_remove, 1), |
| 2549 | FUSE_LIB_OPT("use_ino", use_ino, 1), |
| 2550 | FUSE_LIB_OPT("readdir_ino", readdir_ino, 1), |
| 2551 | FUSE_LIB_OPT("direct_io", direct_io, 1), |
| 2552 | FUSE_LIB_OPT("kernel_cache", kernel_cache, 1), |
Miklos Szeredi | 320abe4 | 2006-01-30 18:14:51 +0000 | [diff] [blame] | 2553 | FUSE_LIB_OPT("auto_cache", auto_cache, 1), |
| 2554 | FUSE_LIB_OPT("noauto_cache", auto_cache, 0), |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2555 | FUSE_LIB_OPT("umask=", set_mode, 1), |
| 2556 | FUSE_LIB_OPT("umask=%o", umask, 0), |
| 2557 | FUSE_LIB_OPT("uid=", set_uid, 1), |
| 2558 | FUSE_LIB_OPT("uid=%d", uid, 0), |
| 2559 | FUSE_LIB_OPT("gid=", set_gid, 1), |
| 2560 | FUSE_LIB_OPT("gid=%d", gid, 0), |
| 2561 | FUSE_LIB_OPT("entry_timeout=%lf", entry_timeout, 0), |
| 2562 | FUSE_LIB_OPT("attr_timeout=%lf", attr_timeout, 0), |
Miklos Szeredi | 6e806e9 | 2006-02-16 16:59:39 +0000 | [diff] [blame] | 2563 | FUSE_LIB_OPT("ac_attr_timeout=%lf", ac_attr_timeout, 0), |
| 2564 | FUSE_LIB_OPT("ac_attr_timeout=", ac_attr_timeout_set, 1), |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2565 | FUSE_LIB_OPT("negative_timeout=%lf", negative_timeout, 0), |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2566 | FUSE_LIB_OPT("intr", intr, 1), |
| 2567 | FUSE_LIB_OPT("intr_signal=%d", intr_signal, 0), |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2568 | FUSE_OPT_END |
| 2569 | }; |
| 2570 | |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 2571 | static void fuse_lib_help(void) |
| 2572 | { |
| 2573 | fprintf(stderr, |
Miklos Szeredi | 0609146 | 2006-02-16 16:38:34 +0000 | [diff] [blame] | 2574 | " -o hard_remove immediate removal (don't hide files)\n" |
| 2575 | " -o use_ino let filesystem set inode numbers\n" |
| 2576 | " -o readdir_ino try to fill in d_ino in readdir\n" |
| 2577 | " -o direct_io use direct I/O\n" |
| 2578 | " -o kernel_cache cache files in kernel\n" |
| 2579 | " -o [no]auto_cache enable caching based on modification times\n" |
| 2580 | " -o umask=M set file permissions (octal)\n" |
| 2581 | " -o uid=N set file owner\n" |
| 2582 | " -o gid=N set file group\n" |
| 2583 | " -o entry_timeout=T cache timeout for names (1.0s)\n" |
| 2584 | " -o negative_timeout=T cache timeout for deleted names (0.0s)\n" |
Miklos Szeredi | 6e806e9 | 2006-02-16 16:59:39 +0000 | [diff] [blame] | 2585 | " -o attr_timeout=T cache timeout for attributes (1.0s)\n" |
| 2586 | " -o ac_attr_timeout=T auto cache timeout for attributes (attr_timeout)\n" |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2587 | " -o intr allow requests to be interrupted\n" |
| 2588 | " -o intr_signal=NUM signal to send on interrupt (%i)\n" |
| 2589 | "\n", FUSE_DEFAULT_INTR_SIGNAL); |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 2590 | } |
| 2591 | |
| 2592 | static int fuse_lib_opt_proc(void *data, const char *arg, int key, |
| 2593 | struct fuse_args *outargs) |
| 2594 | { |
| 2595 | (void) data; (void) arg; (void) outargs; |
| 2596 | |
| 2597 | if (key == KEY_HELP) |
| 2598 | fuse_lib_help(); |
| 2599 | |
| 2600 | return 1; |
| 2601 | } |
| 2602 | |
| 2603 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 2604 | int fuse_is_lib_option(const char *opt) |
| 2605 | { |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2606 | return fuse_lowlevel_is_lib_option(opt) || |
| 2607 | fuse_opt_match(fuse_lib_opts, opt); |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 2608 | } |
| 2609 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2610 | static int fuse_init_intr_signal(int signum, int *installed) |
| 2611 | { |
| 2612 | struct sigaction old_sa; |
| 2613 | |
| 2614 | if (sigaction(signum, NULL, &old_sa) == -1) { |
| 2615 | perror("fuse: cannot get old signal handler"); |
| 2616 | return -1; |
| 2617 | } |
| 2618 | |
| 2619 | if (old_sa.sa_handler == SIG_DFL) { |
| 2620 | struct sigaction sa; |
| 2621 | |
| 2622 | memset(&sa, 0, sizeof(struct sigaction)); |
| 2623 | sa.sa_handler = fuse_intr_sighandler; |
Miklos Szeredi | 0c59ebf | 2006-09-10 20:53:36 +0000 | [diff] [blame] | 2624 | sigemptyset(&sa.sa_mask); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2625 | |
| 2626 | if (sigaction(signum, &sa, NULL) == -1) { |
| 2627 | perror("fuse: cannot set interrupt signal handler"); |
| 2628 | return -1; |
| 2629 | } |
| 2630 | *installed = 1; |
| 2631 | } |
| 2632 | return 0; |
| 2633 | } |
| 2634 | |
Miklos Szeredi | 349bdda | 2006-09-07 11:48:16 +0000 | [diff] [blame] | 2635 | static void fuse_restore_intr_signal(int signum) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2636 | { |
Miklos Szeredi | 349bdda | 2006-09-07 11:48:16 +0000 | [diff] [blame] | 2637 | struct sigaction sa; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2638 | |
Miklos Szeredi | 349bdda | 2006-09-07 11:48:16 +0000 | [diff] [blame] | 2639 | memset(&sa, 0, sizeof(struct sigaction)); |
| 2640 | sa.sa_handler = SIG_DFL; |
| 2641 | sigaction(signum, &sa, NULL); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2642 | } |
| 2643 | |
Miklos Szeredi | 6f38541 | 2006-03-17 15:05:40 +0000 | [diff] [blame] | 2644 | struct fuse *fuse_new_common(struct fuse_chan *ch, struct fuse_args *args, |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2645 | const struct fuse_operations *op, |
Miklos Szeredi | 6f38541 | 2006-03-17 15:05:40 +0000 | [diff] [blame] | 2646 | size_t op_size, void *user_data, int compat) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2647 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2648 | struct fuse *f; |
| 2649 | struct node *root; |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2650 | struct fuse_lowlevel_ops llop = fuse_path_ops; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2651 | |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 2652 | if (sizeof(struct fuse_operations) < op_size) { |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2653 | 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] | 2654 | op_size = sizeof(struct fuse_operations); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2655 | } |
| 2656 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2657 | if (fuse_create_context_key() == -1) |
| 2658 | goto out; |
| 2659 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2660 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2661 | if (f == NULL) { |
| 2662 | fprintf(stderr, "fuse: failed to allocate fuse object\n"); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2663 | goto out_delete_context_key; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2664 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 2665 | |
Miklos Szeredi | 6f38541 | 2006-03-17 15:05:40 +0000 | [diff] [blame] | 2666 | f->user_data = user_data; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2667 | f->conf.entry_timeout = 1.0; |
| 2668 | f->conf.attr_timeout = 1.0; |
| 2669 | f->conf.negative_timeout = 0.0; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2670 | f->conf.intr_signal = FUSE_DEFAULT_INTR_SIGNAL; |
Miklos Szeredi | 6c0209a | 2005-08-02 13:31:28 +0000 | [diff] [blame] | 2671 | |
Miklos Szeredi | ad00597 | 2006-01-07 10:14:34 +0000 | [diff] [blame] | 2672 | 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] | 2673 | goto out_free; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2674 | |
Miklos Szeredi | 6e806e9 | 2006-02-16 16:59:39 +0000 | [diff] [blame] | 2675 | if (!f->conf.ac_attr_timeout_set) |
| 2676 | f->conf.ac_attr_timeout = f->conf.attr_timeout; |
| 2677 | |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2678 | #ifdef __FreeBSD__ |
| 2679 | /* |
| 2680 | * In FreeBSD, we always use these settings as inode numbers are needed to |
| 2681 | * make getcwd(3) work. |
| 2682 | */ |
Miklos Szeredi | 76fc3de | 2005-12-12 09:34:45 +0000 | [diff] [blame] | 2683 | f->conf.readdir_ino = 1; |
Miklos Szeredi | 659743b | 2005-12-09 17:41:42 +0000 | [diff] [blame] | 2684 | #endif |
| 2685 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 2686 | if (compat && compat <= 25) { |
| 2687 | if (fuse_sync_compat_args(args) == -1) |
| 2688 | goto out_free; |
| 2689 | } |
| 2690 | |
Miklos Szeredi | 9a5c11d | 2006-07-30 17:33:40 +0000 | [diff] [blame] | 2691 | memcpy(&f->op, op, op_size); |
| 2692 | if (!f->op.lock) { |
| 2693 | llop.getlk = NULL; |
| 2694 | llop.setlk = NULL; |
| 2695 | } |
| 2696 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2697 | f->se = fuse_lowlevel_new_common(args, &llop, sizeof(llop), f); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2698 | if (f->se == NULL) |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2699 | goto out_free; |
Miklos Szeredi | 2bb750e | 2005-10-03 14:54:24 +0000 | [diff] [blame] | 2700 | |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2701 | fuse_session_add_chan(f->se, ch); |
Miklos Szeredi | bd10a7b | 2005-07-15 09:59:59 +0000 | [diff] [blame] | 2702 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2703 | f->ctr = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 2704 | f->generation = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 2705 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2706 | f->name_table_size = 14057; |
| 2707 | f->name_table = (struct node **) |
| 2708 | calloc(1, sizeof(struct node *) * f->name_table_size); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2709 | if (f->name_table == NULL) { |
| 2710 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2711 | goto out_free_session; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2712 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2713 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2714 | f->id_table_size = 14057; |
| 2715 | f->id_table = (struct node **) |
| 2716 | calloc(1, sizeof(struct node *) * f->id_table_size); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2717 | if (f->id_table == NULL) { |
| 2718 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2719 | goto out_free_name_table; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2720 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2721 | |
Miklos Szeredi | 38f152c | 2006-09-03 18:28:52 +0000 | [diff] [blame] | 2722 | fuse_mutex_init(&f->lock); |
Miklos Szeredi | d0a777a | 2006-04-05 07:18:00 +0000 | [diff] [blame] | 2723 | pthread_rwlock_init(&f->tree_lock, NULL); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2724 | f->compat = compat; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2725 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2726 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2727 | if (root == NULL) { |
| 2728 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2729 | goto out_free_id_table; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2730 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2731 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2732 | root->name = strdup("/"); |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2733 | if (root->name == NULL) { |
| 2734 | fprintf(stderr, "fuse: memory allocation failed\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2735 | goto out_free_root; |
Miklos Szeredi | e56818b | 2004-12-12 11:45:24 +0000 | [diff] [blame] | 2736 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2737 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2738 | if (f->conf.intr && |
| 2739 | fuse_init_intr_signal(f->conf.intr_signal, &f->intr_installed) == -1) |
| 2740 | goto out_free_root_name; |
| 2741 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2742 | root->parent = 0; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2743 | root->nodeid = FUSE_ROOT_ID; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 2744 | root->generation = 0; |
Miklos Szeredi | 0f62d72 | 2005-01-04 12:45:54 +0000 | [diff] [blame] | 2745 | root->refctr = 1; |
Miklos Szeredi | 3800902 | 2005-05-08 19:47:22 +0000 | [diff] [blame] | 2746 | root->nlookup = 1; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2747 | hash_id(f, root); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2748 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2749 | return f; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2750 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2751 | out_free_root_name: |
| 2752 | free(root->name); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2753 | out_free_root: |
| 2754 | free(root); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2755 | out_free_id_table: |
| 2756 | free(f->id_table); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2757 | out_free_name_table: |
| 2758 | free(f->name_table); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2759 | out_free_session: |
| 2760 | fuse_session_destroy(f->se); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2761 | out_free: |
| 2762 | free(f); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2763 | out_delete_context_key: |
| 2764 | fuse_delete_context_key(); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2765 | out: |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 2766 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2767 | } |
| 2768 | |
Miklos Szeredi | 6f38541 | 2006-03-17 15:05:40 +0000 | [diff] [blame] | 2769 | struct fuse *fuse_new(struct fuse_chan *ch, struct fuse_args *args, |
| 2770 | const struct fuse_operations *op, size_t op_size, |
| 2771 | void *user_data) |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2772 | { |
Miklos Szeredi | 6f38541 | 2006-03-17 15:05:40 +0000 | [diff] [blame] | 2773 | return fuse_new_common(ch, args, op, op_size, user_data, 0); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2774 | } |
| 2775 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2776 | void fuse_destroy(struct fuse *f) |
| 2777 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2778 | size_t i; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2779 | struct fuse_context_i *c = fuse_get_context_internal(); |
| 2780 | |
| 2781 | if (f->conf.intr && f->intr_installed) |
| 2782 | fuse_restore_intr_signal(f->conf.intr_signal); |
Miklos Szeredi | ad51956 | 2006-07-31 11:07:40 +0000 | [diff] [blame] | 2783 | |
| 2784 | memset(c, 0, sizeof(*c)); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2785 | c->ctx.fuse = f; |
| 2786 | c->ctx.private_data = f->user_data; |
Miklos Szeredi | ad51956 | 2006-07-31 11:07:40 +0000 | [diff] [blame] | 2787 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2788 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2789 | struct node *node; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2790 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2791 | for (node = f->id_table[i]; node != NULL; node = node->id_next) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2792 | if (node->is_hidden) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2793 | char *path = get_path(f, node->nodeid); |
Miklos Szeredi | 21019c9 | 2005-05-09 11:22:41 +0000 | [diff] [blame] | 2794 | if (path) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2795 | f->op.unlink(path); |
Miklos Szeredi | 21019c9 | 2005-05-09 11:22:41 +0000 | [diff] [blame] | 2796 | free(path); |
| 2797 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2798 | } |
| 2799 | } |
| 2800 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2801 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2802 | struct node *node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2803 | struct node *next; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 2804 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2805 | for (node = f->id_table[i]; node != NULL; node = next) { |
| 2806 | next = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2807 | free_node(node); |
| 2808 | } |
| 2809 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 2810 | free(f->id_table); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2811 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 2812 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 55a8410 | 2006-06-06 10:16:38 +0000 | [diff] [blame] | 2813 | pthread_rwlock_destroy(&f->tree_lock); |
Miklos Szeredi | a148242 | 2005-08-14 23:00:27 +0000 | [diff] [blame] | 2814 | fuse_session_destroy(f->se); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 2815 | free(f); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2816 | fuse_delete_context_key(); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 2817 | } |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 2818 | |
Miklos Szeredi | eafdf42 | 2006-09-22 19:30:17 +0000 | [diff] [blame] | 2819 | #include "fuse_common_compat.h" |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2820 | #include "fuse_compat.h" |
| 2821 | |
Miklos Szeredi | 6f38541 | 2006-03-17 15:05:40 +0000 | [diff] [blame] | 2822 | static struct fuse *fuse_new_common_compat25(int fd, struct fuse_args *args, |
| 2823 | const struct fuse_operations *op, |
| 2824 | size_t op_size, int compat) |
| 2825 | { |
| 2826 | struct fuse *f = NULL; |
| 2827 | struct fuse_chan *ch = fuse_kern_chan_new(fd); |
| 2828 | |
| 2829 | if (ch) |
| 2830 | f = fuse_new_common(ch, args, op, op_size, NULL, compat); |
| 2831 | |
| 2832 | return f; |
| 2833 | } |
| 2834 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 2835 | #ifndef __FreeBSD__ |
| 2836 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2837 | static int fuse_compat_open(struct fuse *f, fuse_req_t req, char *path, |
| 2838 | struct fuse_file_info *fi) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2839 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2840 | int err; |
| 2841 | struct fuse_intr_data d; |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 2842 | if (!f->compat || f->compat >= 25) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2843 | err = fuse_do_open(f, req, path, fi); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2844 | else if (f->compat == 22) { |
Miklos Szeredi | eafdf42 | 2006-09-22 19:30:17 +0000 | [diff] [blame] | 2845 | struct fuse_file_info_compat tmp; |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2846 | memcpy(&tmp, fi, sizeof(tmp)); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2847 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2848 | err = ((struct fuse_operations_compat22 *) &f->op)->open(path, &tmp); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2849 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2850 | memcpy(fi, &tmp, sizeof(tmp)); |
| 2851 | fi->fh = tmp.fh; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2852 | } else { |
| 2853 | fuse_prepare_interrupt(f, req, &d); |
| 2854 | err = |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2855 | ((struct fuse_operations_compat2 *) &f->op)->open(path, fi->flags); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2856 | fuse_finish_interrupt(f, req, &d); |
| 2857 | } |
| 2858 | return err; |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2859 | } |
| 2860 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2861 | static void fuse_compat_release(struct fuse *f, fuse_req_t req, char *path, |
| 2862 | struct fuse_file_info *fi) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2863 | { |
| 2864 | if (!f->compat || f->compat >= 22) |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2865 | fuse_do_release(f, req, path ? path : "-", fi); |
| 2866 | else if (path) { |
| 2867 | struct fuse_intr_data d; |
| 2868 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2869 | ((struct fuse_operations_compat2 *) &f->op)->release(path, fi->flags); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2870 | fuse_finish_interrupt(f, req, &d); |
| 2871 | } |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2872 | } |
| 2873 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2874 | static int fuse_compat_opendir(struct fuse *f, fuse_req_t req, char *path, |
| 2875 | struct fuse_file_info *fi) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2876 | { |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 2877 | if (!f->compat || f->compat >= 25) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2878 | return fuse_do_opendir(f, req, path, fi); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2879 | } else { |
| 2880 | int err; |
Miklos Szeredi | eafdf42 | 2006-09-22 19:30:17 +0000 | [diff] [blame] | 2881 | struct fuse_file_info_compat tmp; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2882 | struct fuse_intr_data d; |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2883 | memcpy(&tmp, fi, sizeof(tmp)); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2884 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2885 | err = ((struct fuse_operations_compat22 *) &f->op)->opendir(path, &tmp); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2886 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2887 | memcpy(fi, &tmp, sizeof(tmp)); |
| 2888 | fi->fh = tmp.fh; |
| 2889 | return err; |
| 2890 | } |
| 2891 | } |
| 2892 | |
| 2893 | static void convert_statfs_compat(struct fuse_statfs_compat1 *compatbuf, |
| 2894 | struct statvfs *stbuf) |
| 2895 | { |
| 2896 | stbuf->f_bsize = compatbuf->block_size; |
| 2897 | stbuf->f_blocks = compatbuf->blocks; |
| 2898 | stbuf->f_bfree = compatbuf->blocks_free; |
| 2899 | stbuf->f_bavail = compatbuf->blocks_free; |
| 2900 | stbuf->f_files = compatbuf->files; |
| 2901 | stbuf->f_ffree = compatbuf->files_free; |
| 2902 | stbuf->f_namemax = compatbuf->namelen; |
| 2903 | } |
| 2904 | |
| 2905 | static void convert_statfs_old(struct statfs *oldbuf, struct statvfs *stbuf) |
| 2906 | { |
| 2907 | stbuf->f_bsize = oldbuf->f_bsize; |
| 2908 | stbuf->f_blocks = oldbuf->f_blocks; |
| 2909 | stbuf->f_bfree = oldbuf->f_bfree; |
| 2910 | stbuf->f_bavail = oldbuf->f_bavail; |
| 2911 | stbuf->f_files = oldbuf->f_files; |
| 2912 | stbuf->f_ffree = oldbuf->f_ffree; |
| 2913 | stbuf->f_namemax = oldbuf->f_namelen; |
| 2914 | } |
| 2915 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2916 | static int fuse_compat_statfs(struct fuse *f, fuse_req_t req, |
| 2917 | struct statvfs *buf) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2918 | { |
| 2919 | int err; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2920 | struct fuse_intr_data d; |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2921 | |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 2922 | if (!f->compat || f->compat >= 25) { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2923 | err = fuse_do_statfs(f, req, "/", buf); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2924 | } else if (f->compat > 11) { |
| 2925 | struct statfs oldbuf; |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2926 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2927 | err = ((struct fuse_operations_compat22 *) &f->op)->statfs("/", &oldbuf); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2928 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2929 | if (!err) |
| 2930 | convert_statfs_old(&oldbuf, buf); |
| 2931 | } else { |
| 2932 | struct fuse_statfs_compat1 compatbuf; |
| 2933 | memset(&compatbuf, 0, sizeof(struct fuse_statfs_compat1)); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2934 | fuse_prepare_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2935 | err = ((struct fuse_operations_compat1 *) &f->op)->statfs(&compatbuf); |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2936 | fuse_finish_interrupt(f, req, &d); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2937 | if (!err) |
| 2938 | convert_statfs_compat(&compatbuf, buf); |
| 2939 | } |
| 2940 | return err; |
| 2941 | } |
| 2942 | |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 2943 | static struct fuse *fuse_new_common_compat(int fd, const char *opts, |
| 2944 | const struct fuse_operations *op, |
| 2945 | size_t op_size, int compat) |
| 2946 | { |
| 2947 | struct fuse *f; |
| 2948 | struct fuse_args args = FUSE_ARGS_INIT(0, NULL); |
| 2949 | |
| 2950 | if (opts && |
| 2951 | (fuse_opt_add_arg(&args, "") == -1 || |
| 2952 | fuse_opt_add_arg(&args, "-o") == -1 || |
| 2953 | fuse_opt_add_arg(&args, opts) == -1)) { |
| 2954 | fuse_opt_free_args(&args); |
| 2955 | return NULL; |
| 2956 | } |
Miklos Szeredi | 6f38541 | 2006-03-17 15:05:40 +0000 | [diff] [blame] | 2957 | f = fuse_new_common_compat25(fd, &args, op, op_size, compat); |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 2958 | fuse_opt_free_args(&args); |
| 2959 | |
| 2960 | return f; |
| 2961 | } |
| 2962 | |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2963 | struct fuse *fuse_new_compat22(int fd, const char *opts, |
| 2964 | const struct fuse_operations_compat22 *op, |
| 2965 | size_t op_size) |
| 2966 | { |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 2967 | return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op, |
| 2968 | op_size, 22); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2969 | } |
| 2970 | |
| 2971 | struct fuse *fuse_new_compat2(int fd, const char *opts, |
| 2972 | const struct fuse_operations_compat2 *op) |
| 2973 | { |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 2974 | return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op, |
| 2975 | sizeof(struct fuse_operations_compat2), 21); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2976 | } |
| 2977 | |
| 2978 | struct fuse *fuse_new_compat1(int fd, int flags, |
| 2979 | const struct fuse_operations_compat1 *op) |
| 2980 | { |
| 2981 | const char *opts = NULL; |
| 2982 | if (flags & FUSE_DEBUG_COMPAT1) |
| 2983 | opts = "debug"; |
Miklos Szeredi | 95da860 | 2006-01-06 18:29:40 +0000 | [diff] [blame] | 2984 | return fuse_new_common_compat(fd, opts, (struct fuse_operations *) op, |
| 2985 | sizeof(struct fuse_operations_compat1), 11); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2986 | } |
| 2987 | |
Miklos Szeredi | f458b8c | 2004-12-07 16:46:42 +0000 | [diff] [blame] | 2988 | __asm__(".symver fuse_exited,__fuse_exited@"); |
| 2989 | __asm__(".symver fuse_process_cmd,__fuse_process_cmd@"); |
| 2990 | __asm__(".symver fuse_read_cmd,__fuse_read_cmd@"); |
| 2991 | __asm__(".symver fuse_set_getcontext_func,__fuse_set_getcontext_func@"); |
| 2992 | __asm__(".symver fuse_new_compat2,fuse_new@"); |
Miklos Szeredi | 3a77047 | 2005-11-11 21:32:42 +0000 | [diff] [blame] | 2993 | __asm__(".symver fuse_new_compat22,fuse_new@FUSE_2.2"); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2994 | |
| 2995 | #else /* __FreeBSD__ */ |
| 2996 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 2997 | static int fuse_compat_open(struct fuse *f, fuse_req_t req, char *path, |
| 2998 | struct fuse_file_info *fi) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 2999 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 3000 | return fuse_do_open(f, req, path, fi); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 3001 | } |
| 3002 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 3003 | static void fuse_compat_release(struct fuse *f, fuse_req_t req, char *path, |
| 3004 | struct fuse_file_info *fi) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 3005 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 3006 | fuse_do_release(f, req, path ? path : "-", fi); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 3007 | } |
| 3008 | |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 3009 | static int fuse_compat_opendir(struct fuse *f, fuse_req_t req, char *path, |
| 3010 | struct fuse_file_info *fi) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 3011 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 3012 | return fuse_do_opendir(f, req, path, fi); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 3013 | } |
| 3014 | |
Csaba Henk | 3e3a125 | 2006-09-24 14:53:29 +0000 | [diff] [blame] | 3015 | static int fuse_compat_statfs(struct fuse *f, fuse_req_t req, struct statvfs *buf) |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 3016 | { |
Miklos Szeredi | 288ed4e | 2006-09-07 06:02:44 +0000 | [diff] [blame] | 3017 | return fuse_do_statfs(f, req, "/", buf); |
Miklos Szeredi | 9c2ccb4 | 2005-11-17 17:11:48 +0000 | [diff] [blame] | 3018 | } |
| 3019 | |
| 3020 | #endif /* __FreeBSD__ */ |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 3021 | |
| 3022 | struct fuse *fuse_new_compat25(int fd, struct fuse_args *args, |
| 3023 | const struct fuse_operations_compat25 *op, |
| 3024 | size_t op_size) |
| 3025 | { |
Miklos Szeredi | 6f38541 | 2006-03-17 15:05:40 +0000 | [diff] [blame] | 3026 | return fuse_new_common_compat25(fd, args, (struct fuse_operations *) op, |
| 3027 | op_size, 25); |
Miklos Szeredi | 065f222 | 2006-01-20 15:15:21 +0000 | [diff] [blame] | 3028 | } |
| 3029 | |
| 3030 | __asm__(".symver fuse_new_compat25,fuse_new@FUSE_2.5"); |