Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1 | /* |
| 2 | FUSE: Filesystem in Userspace |
Miklos Szeredi | 2e6b6f2 | 2004-07-07 19:19:53 +0000 | [diff] [blame] | 3 | Copyright (C) 2001-2004 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 | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 9 | #include <config.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 10 | #include "fuse_i.h" |
Miklos Szeredi | b9b94cd | 2004-12-01 18:56:39 +0000 | [diff] [blame] | 11 | #include "fuse_kernel.h" |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 12 | |
| 13 | #include <string.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 14 | #include <stdlib.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 15 | #include <unistd.h> |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 16 | #include <limits.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 17 | #include <errno.h> |
Miklos Szeredi | 019b4e9 | 2001-12-26 18:08:09 +0000 | [diff] [blame] | 18 | #include <sys/param.h> |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 19 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 20 | #define FUSE_KERNEL_MINOR_VERSION_NEED 1 |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 21 | #define FUSE_VERSION_FILE_OLD "/proc/fs/fuse/version" |
Miklos Szeredi | 162bcbb | 2004-11-29 23:43:44 +0000 | [diff] [blame] | 22 | #define FUSE_VERSION_FILE_NEW "/sys/fs/fuse/version" |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 23 | #define FUSE_DEV_OLD "/proc/fs/fuse/dev" |
| 24 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 25 | #define FUSE_MAX_PATH 4096 |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 26 | #define PARAM(inarg) (((char *)(inarg)) + sizeof(*inarg)) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 27 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 28 | #define ENTRY_REVALIDATE_TIME 1 /* sec */ |
| 29 | #define ATTR_REVALIDATE_TIME 1 /* sec */ |
| 30 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 31 | static struct fuse_context *(*fuse_getcontext)(void) = NULL; |
| 32 | |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 33 | static const char *opname(enum fuse_opcode opcode) |
| 34 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 35 | switch (opcode) { |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 36 | case FUSE_LOOKUP: return "LOOKUP"; |
| 37 | case FUSE_FORGET: return "FORGET"; |
| 38 | case FUSE_GETATTR: return "GETATTR"; |
| 39 | case FUSE_SETATTR: return "SETATTR"; |
| 40 | case FUSE_READLINK: return "READLINK"; |
| 41 | case FUSE_SYMLINK: return "SYMLINK"; |
| 42 | case FUSE_GETDIR: return "GETDIR"; |
| 43 | case FUSE_MKNOD: return "MKNOD"; |
| 44 | case FUSE_MKDIR: return "MKDIR"; |
| 45 | case FUSE_UNLINK: return "UNLINK"; |
| 46 | case FUSE_RMDIR: return "RMDIR"; |
| 47 | case FUSE_RENAME: return "RENAME"; |
| 48 | case FUSE_LINK: return "LINK"; |
| 49 | case FUSE_OPEN: return "OPEN"; |
| 50 | case FUSE_READ: return "READ"; |
| 51 | case FUSE_WRITE: return "WRITE"; |
| 52 | case FUSE_STATFS: return "STATFS"; |
Miklos Szeredi | 99f2074 | 2004-05-19 08:01:10 +0000 | [diff] [blame] | 53 | case FUSE_FLUSH: return "FLUSH"; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 54 | case FUSE_RELEASE: return "RELEASE"; |
| 55 | case FUSE_FSYNC: return "FSYNC"; |
| 56 | case FUSE_SETXATTR: return "SETXATTR"; |
| 57 | case FUSE_GETXATTR: return "GETXATTR"; |
| 58 | case FUSE_LISTXATTR: return "LISTXATTR"; |
| 59 | case FUSE_REMOVEXATTR: return "REMOVEXATTR"; |
Miklos Szeredi | 99f2074 | 2004-05-19 08:01:10 +0000 | [diff] [blame] | 60 | default: return "???"; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 64 | |
| 65 | static inline void dec_avail(struct fuse *f) |
| 66 | { |
| 67 | pthread_mutex_lock(&f->lock); |
| 68 | f->numavail --; |
| 69 | pthread_mutex_unlock(&f->lock); |
| 70 | } |
| 71 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 72 | static struct node *__get_node(struct fuse *f, nodeid_t nodeid) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 73 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 74 | size_t hash = nodeid % f->id_table_size; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 75 | struct node *node; |
| 76 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 77 | for (node = f->id_table[hash]; node != NULL; node = node->id_next) |
| 78 | if (node->nodeid == nodeid) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 79 | return node; |
| 80 | |
| 81 | return NULL; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 82 | } |
| 83 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 84 | static struct node *get_node(struct fuse *f, nodeid_t nodeid) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 85 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 86 | struct node *node = __get_node(f, nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 87 | if (node != NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 88 | return node; |
| 89 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 90 | fprintf(stderr, "fuse internal error: inode %lu not found\n", nodeid); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 91 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 92 | } |
| 93 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 94 | static void hash_id(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 95 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 96 | size_t hash = node->nodeid % f->id_table_size; |
| 97 | node->id_next = f->id_table[hash]; |
| 98 | f->id_table[hash] = node; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 99 | } |
| 100 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 101 | static void unhash_id(struct fuse *f, struct node *node) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 102 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 103 | size_t hash = node->nodeid % f->id_table_size; |
| 104 | struct node **nodep = &f->id_table[hash]; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 105 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 106 | for (; *nodep != NULL; nodep = &(*nodep)->id_next) |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 107 | if (*nodep == node) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 108 | *nodep = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 109 | return; |
| 110 | } |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 111 | } |
| 112 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 113 | static nodeid_t next_id(struct fuse *f) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 114 | { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 115 | do { |
| 116 | f->ctr++; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 117 | if (!f->ctr) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 118 | f->generation ++; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 119 | } while (f->ctr == 0 || __get_node(f, f->ctr) != NULL); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 120 | return f->ctr; |
| 121 | } |
| 122 | |
| 123 | static void free_node(struct node *node) |
| 124 | { |
| 125 | free(node->name); |
| 126 | free(node); |
| 127 | } |
| 128 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 129 | static unsigned int name_hash(struct fuse *f, nodeid_t parent, const char *name) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 130 | { |
| 131 | unsigned int hash = *name; |
| 132 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 133 | if (hash) |
| 134 | for (name += 1; *name != '\0'; name++) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 135 | hash = (hash << 5) - hash + *name; |
| 136 | |
| 137 | return (hash + parent) % f->name_table_size; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 138 | } |
| 139 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 140 | static struct node *__lookup_node(struct fuse *f, nodeid_t parent, |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 141 | const char *name) |
| 142 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 143 | size_t hash = name_hash(f, parent, name); |
| 144 | struct node *node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 145 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 146 | for (node = f->name_table[hash]; node != NULL; node = node->name_next) |
| 147 | if (node->parent == parent && strcmp(node->name, name) == 0) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 148 | return node; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 149 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 150 | return NULL; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 151 | } |
| 152 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 153 | static struct node *lookup_node(struct fuse *f, nodeid_t parent, |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 154 | const char *name) |
| 155 | { |
| 156 | struct node *node; |
| 157 | |
| 158 | pthread_mutex_lock(&f->lock); |
| 159 | node = __lookup_node(f, parent, name); |
| 160 | pthread_mutex_unlock(&f->lock); |
| 161 | if (node != NULL) |
| 162 | return node; |
| 163 | |
| 164 | fprintf(stderr, "fuse internal error: node %lu/%s not found\n", parent, |
| 165 | name); |
| 166 | abort(); |
| 167 | } |
| 168 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 169 | static int hash_name(struct fuse *f, struct node *node, nodeid_t parent, |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 170 | const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 171 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 172 | size_t hash = name_hash(f, parent, name); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 173 | node->parent = parent; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 174 | node->name = strdup(name); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 175 | if (node->name == NULL) |
| 176 | return -1; |
| 177 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 178 | node->name_next = f->name_table[hash]; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 179 | f->name_table[hash] = node; |
| 180 | return 0; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 181 | } |
| 182 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 183 | static void unhash_name(struct fuse *f, struct node *node) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 184 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 185 | if (node->name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 186 | size_t hash = name_hash(f, node->parent, node->name); |
| 187 | struct node **nodep = &f->name_table[hash]; |
| 188 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 189 | for (; *nodep != NULL; nodep = &(*nodep)->name_next) |
| 190 | if (*nodep == node) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 191 | *nodep = node->name_next; |
| 192 | node->name_next = NULL; |
| 193 | free(node->name); |
| 194 | node->name = NULL; |
| 195 | node->parent = 0; |
| 196 | return; |
| 197 | } |
| 198 | fprintf(stderr, "fuse internal error: unable to unhash node: %lu\n", |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 199 | node->nodeid); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 200 | abort(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 201 | } |
| 202 | } |
| 203 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 204 | static struct node *find_node(struct fuse *f, nodeid_t parent, char *name, |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 205 | struct fuse_attr *attr, int version) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 206 | { |
| 207 | struct node *node; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 208 | int mode = attr->mode & S_IFMT; |
| 209 | int rdev = 0; |
| 210 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 211 | if (S_ISCHR(mode) || S_ISBLK(mode)) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 212 | rdev = attr->rdev; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 213 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 214 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 215 | node = __lookup_node(f, parent, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 216 | if (node != NULL) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 217 | if (node->mode == mode && node->rdev == rdev && |
| 218 | (!(f->flags & FUSE_USE_INO) || node->ino == attr->ino)) { |
| 219 | if (!(f->flags & FUSE_USE_INO)) |
| 220 | attr->ino = node->nodeid; |
| 221 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 222 | goto out; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 223 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 224 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 225 | unhash_name(f, node); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 226 | } |
| 227 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 228 | node = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 229 | if (node == NULL) |
Miklos Szeredi | c230991 | 2004-09-21 13:40:38 +0000 | [diff] [blame] | 230 | goto out_err; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 231 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 232 | node->nodeid = next_id(f); |
| 233 | if (!(f->flags & FUSE_USE_INO)) |
| 234 | attr->ino = node->nodeid; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 235 | node->mode = mode; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 236 | node->rdev = rdev; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 237 | node->ino = attr->ino; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 238 | node->open_count = 0; |
| 239 | node->is_hidden = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 240 | node->generation = f->generation; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 241 | if (hash_name(f, node, parent, name) == -1) { |
| 242 | free(node); |
Miklos Szeredi | c230991 | 2004-09-21 13:40:38 +0000 | [diff] [blame] | 243 | node = NULL; |
| 244 | goto out_err; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 245 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 246 | hash_id(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 247 | |
Miklos Szeredi | c230991 | 2004-09-21 13:40:38 +0000 | [diff] [blame] | 248 | out: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 249 | node->version = version; |
Miklos Szeredi | c230991 | 2004-09-21 13:40:38 +0000 | [diff] [blame] | 250 | out_err: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 251 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 252 | return node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 253 | } |
| 254 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 255 | static int path_lookup(struct fuse *f, const char *path, nodeid_t *nodeidp, |
| 256 | unsigned long *inop) |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 257 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 258 | nodeid_t nodeid; |
| 259 | unsigned long ino; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 260 | int err; |
| 261 | char *s; |
| 262 | char *name; |
| 263 | char *tmp = strdup(path); |
| 264 | if (!tmp) |
| 265 | return -ENOMEM; |
| 266 | |
| 267 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 268 | nodeid = FUSE_ROOT_ID; |
| 269 | ino = nodeid; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 270 | err = 0; |
| 271 | for (s = tmp; (name = strsep(&s, "/")) != NULL; ) { |
| 272 | if (name[0]) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 273 | struct node *node = __lookup_node(f, nodeid, name); |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 274 | if (node == NULL) { |
| 275 | err = -ENOENT; |
| 276 | break; |
| 277 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 278 | nodeid = node->nodeid; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 279 | ino = node->ino; |
| 280 | } |
| 281 | } |
| 282 | pthread_mutex_unlock(&f->lock); |
| 283 | free(tmp); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 284 | if (!err) { |
| 285 | *nodeidp = nodeid; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 286 | *inop = ino; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 287 | } |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 288 | |
| 289 | return err; |
| 290 | } |
| 291 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 292 | static char *add_name(char *buf, char *s, const char *name) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 293 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 294 | size_t len = strlen(name); |
| 295 | s -= len; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 296 | if (s <= buf) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 297 | fprintf(stderr, "fuse: path too long: ...%s\n", s + len); |
| 298 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 299 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 300 | strncpy(s, name, len); |
| 301 | s--; |
| 302 | *s = '/'; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 303 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 304 | return s; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 305 | } |
| 306 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 307 | static char *get_path_name(struct fuse *f, nodeid_t nodeid, const char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 308 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 309 | char buf[FUSE_MAX_PATH]; |
| 310 | char *s = buf + FUSE_MAX_PATH - 1; |
| 311 | struct node *node; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 312 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 313 | *s = '\0'; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 314 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 315 | if (name != NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 316 | s = add_name(buf, s, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 317 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 318 | return NULL; |
| 319 | } |
| 320 | |
| 321 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 322 | for (node = get_node(f, nodeid); node->nodeid != FUSE_ROOT_ID; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 323 | node = get_node(f, node->parent)) { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 324 | if (node->name == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 325 | s = NULL; |
| 326 | break; |
| 327 | } |
| 328 | |
| 329 | s = add_name(buf, s, node->name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 330 | if (s == NULL) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 331 | break; |
| 332 | } |
| 333 | pthread_mutex_unlock(&f->lock); |
| 334 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 335 | if (s == NULL) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 336 | return NULL; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 337 | else if (*s == '\0') |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 338 | return strdup("/"); |
| 339 | else |
| 340 | return strdup(s); |
| 341 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 342 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 343 | static char *get_path(struct fuse *f, nodeid_t nodeid) |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 344 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 345 | return get_path_name(f, nodeid, NULL); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 346 | } |
| 347 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 348 | static void destroy_node(struct fuse *f, nodeid_t nodeid, int version) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 349 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 350 | struct node *node; |
| 351 | |
| 352 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 353 | node = __get_node(f, nodeid); |
| 354 | if (node && node->version == version && nodeid != FUSE_ROOT_ID) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 355 | unhash_name(f, node); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 356 | unhash_id(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 357 | free_node(node); |
| 358 | } |
| 359 | pthread_mutex_unlock(&f->lock); |
| 360 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 361 | } |
| 362 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 363 | static void remove_node(struct fuse *f, nodeid_t dir, const char *name) |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 364 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 365 | struct node *node; |
| 366 | |
| 367 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 368 | node = __lookup_node(f, dir, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 369 | if (node == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 370 | fprintf(stderr, "fuse internal error: unable to remove node %lu/%s\n", |
| 371 | dir, name); |
| 372 | abort(); |
| 373 | } |
| 374 | unhash_name(f, node); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 375 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 376 | } |
| 377 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 378 | static int rename_node(struct fuse *f, nodeid_t olddir, const char *oldname, |
| 379 | nodeid_t newdir, const char *newname, int hide) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 380 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 381 | struct node *node; |
| 382 | struct node *newnode; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 383 | int err = 0; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 384 | |
| 385 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 386 | node = __lookup_node(f, olddir, oldname); |
| 387 | newnode = __lookup_node(f, newdir, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 388 | if (node == NULL) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 389 | fprintf(stderr, "fuse internal error: unable to rename node %lu/%s\n", |
| 390 | olddir, oldname); |
| 391 | abort(); |
| 392 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 393 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 394 | if (newnode != NULL) { |
| 395 | if (hide) { |
| 396 | fprintf(stderr, "fuse: hidden file got created during hiding\n"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 397 | err = -EBUSY; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 398 | goto out; |
| 399 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 400 | unhash_name(f, newnode); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 401 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 402 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 403 | unhash_name(f, node); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 404 | if (hash_name(f, node, newdir, newname) == -1) { |
| 405 | err = -ENOMEM; |
| 406 | goto out; |
| 407 | } |
| 408 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 409 | if (hide) |
| 410 | node->is_hidden = 1; |
| 411 | |
| 412 | out: |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 413 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 414 | return err; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 415 | } |
| 416 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 417 | static void convert_stat(struct stat *stbuf, struct fuse_attr *attr) |
| 418 | { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 419 | attr->ino = stbuf->st_ino; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 420 | attr->mode = stbuf->st_mode; |
| 421 | attr->nlink = stbuf->st_nlink; |
| 422 | attr->uid = stbuf->st_uid; |
| 423 | attr->gid = stbuf->st_gid; |
| 424 | attr->rdev = stbuf->st_rdev; |
| 425 | attr->size = stbuf->st_size; |
| 426 | attr->blocks = stbuf->st_blocks; |
| 427 | attr->atime = stbuf->st_atime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 428 | attr->mtime = stbuf->st_mtime; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 429 | attr->ctime = stbuf->st_ctime; |
Miklos Szeredi | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 430 | #ifdef HAVE_STRUCT_STAT_ST_ATIM |
| 431 | attr->atimensec = stbuf->st_atim.tv_nsec; |
| 432 | attr->mtimensec = stbuf->st_mtim.tv_nsec; |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 433 | attr->ctimensec = stbuf->st_ctim.tv_nsec; |
Miklos Szeredi | cb26451 | 2004-06-23 18:52:50 +0000 | [diff] [blame] | 434 | #endif |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 435 | } |
| 436 | |
Miklos Szeredi | 8fb48fe | 2004-11-08 14:48:52 +0000 | [diff] [blame] | 437 | static int fill_dir(struct fuse_dirhandle *dh, const char *name, int type, |
| 438 | ino_t ino) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 439 | { |
| 440 | struct fuse_dirent dirent; |
| 441 | size_t reclen; |
| 442 | size_t res; |
| 443 | |
Miklos Szeredi | 8fb48fe | 2004-11-08 14:48:52 +0000 | [diff] [blame] | 444 | if ((dh->fuse->flags & FUSE_USE_INO)) |
| 445 | dirent.ino = ino; |
| 446 | else |
| 447 | dirent.ino = (unsigned long) -1; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 448 | dirent.namelen = strlen(name); |
| 449 | strncpy(dirent.name, name, sizeof(dirent.name)); |
| 450 | dirent.type = type; |
| 451 | reclen = FUSE_DIRENT_SIZE(&dirent); |
| 452 | res = fwrite(&dirent, reclen, 1, dh->fp); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 453 | if (res == 0) { |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 454 | perror("fuse: writing directory file"); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 455 | return -EIO; |
| 456 | } |
| 457 | return 0; |
| 458 | } |
| 459 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 460 | static int send_reply_raw(struct fuse *f, char *outbuf, size_t outsize, |
| 461 | int locked) |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 462 | { |
| 463 | int res; |
| 464 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 465 | if ((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 466 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 467 | printf(" unique: %i, error: %i (%s), outsize: %i\n", out->unique, |
| 468 | out->error, strerror(-out->error), outsize); |
| 469 | fflush(stdout); |
| 470 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 471 | |
Miklos Szeredi | 4e71c9f | 2004-02-09 12:05:14 +0000 | [diff] [blame] | 472 | /* This needs to be done before the reply, otherwise the scheduler |
| 473 | could play tricks with us, and only let the counter be increased |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 474 | long after the operation is done */ |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 475 | if (!locked) |
| 476 | pthread_mutex_lock(&f->lock); |
| 477 | f->numavail ++; |
| 478 | if (!locked) |
| 479 | pthread_mutex_unlock(&f->lock); |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 480 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 481 | res = write(f->fd, outbuf, outsize); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 482 | if (res == -1) { |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 483 | /* ENOENT means the operation was interrupted */ |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 484 | if (!f->exited && errno != ENOENT) |
Miklos Szeredi | 9624998 | 2001-11-21 12:21:19 +0000 | [diff] [blame] | 485 | perror("fuse: writing device"); |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 486 | return -errno; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 487 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 488 | return 0; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 489 | } |
| 490 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 491 | static int __send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
| 492 | void *arg, size_t argsize, int locked) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 493 | { |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 494 | int res; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 495 | char *outbuf; |
| 496 | size_t outsize; |
| 497 | struct fuse_out_header *out; |
| 498 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 499 | if (error <= -1000 || error > 0) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 500 | fprintf(stderr, "fuse: bad error value: %i\n", error); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 501 | error = -ERANGE; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 502 | } |
| 503 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 504 | if (error) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 505 | argsize = 0; |
| 506 | |
| 507 | outsize = sizeof(struct fuse_out_header) + argsize; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 508 | outbuf = (char *) malloc(outsize); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 509 | if (outbuf == NULL) { |
| 510 | fprintf(stderr, "fuse: failed to allocate reply buffer\n"); |
| 511 | res = -ENOMEM; |
| 512 | } else { |
| 513 | out = (struct fuse_out_header *) outbuf; |
| 514 | memset(out, 0, sizeof(struct fuse_out_header)); |
| 515 | out->unique = in->unique; |
| 516 | out->error = error; |
| 517 | if (argsize != 0) |
| 518 | memcpy(outbuf + sizeof(struct fuse_out_header), arg, argsize); |
| 519 | |
| 520 | res = send_reply_raw(f, outbuf, outsize, locked); |
| 521 | free(outbuf); |
| 522 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 523 | |
| 524 | return res; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 525 | } |
| 526 | |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 527 | static int send_reply(struct fuse *f, struct fuse_in_header *in, int error, |
| 528 | void *arg, size_t argsize) |
| 529 | { |
| 530 | return __send_reply(f, in, error, arg, argsize, 0); |
| 531 | } |
| 532 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 533 | static int is_open(struct fuse *f, nodeid_t dir, const char *name) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 534 | { |
| 535 | struct node *node; |
| 536 | int isopen = 0; |
| 537 | pthread_mutex_lock(&f->lock); |
| 538 | node = __lookup_node(f, dir, name); |
| 539 | if (node && node->open_count > 0) |
| 540 | isopen = 1; |
| 541 | pthread_mutex_unlock(&f->lock); |
| 542 | return isopen; |
| 543 | } |
| 544 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 545 | static char *hidden_name(struct fuse *f, nodeid_t dir, const char *oldname, |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 546 | char *newname, size_t bufsize) |
| 547 | { |
| 548 | struct stat buf; |
| 549 | struct node *node; |
| 550 | struct node *newnode; |
| 551 | char *newpath; |
| 552 | int res; |
| 553 | int failctr = 10; |
| 554 | |
| 555 | if (!f->op.getattr) |
| 556 | return NULL; |
| 557 | |
| 558 | do { |
| 559 | node = lookup_node(f, dir, oldname); |
| 560 | pthread_mutex_lock(&f->lock); |
| 561 | do { |
| 562 | f->hidectr ++; |
| 563 | snprintf(newname, bufsize, ".fuse_hidden%08x%08x", |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 564 | (unsigned int) node->nodeid, f->hidectr); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 565 | newnode = __lookup_node(f, dir, newname); |
| 566 | } while(newnode); |
| 567 | pthread_mutex_unlock(&f->lock); |
| 568 | |
| 569 | newpath = get_path_name(f, dir, newname); |
| 570 | if (!newpath) |
| 571 | break; |
| 572 | |
| 573 | res = f->op.getattr(newpath, &buf); |
| 574 | if (res != 0) |
| 575 | break; |
| 576 | free(newpath); |
| 577 | newpath = NULL; |
| 578 | } while(--failctr); |
| 579 | |
| 580 | return newpath; |
| 581 | } |
| 582 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 583 | static int hide_node(struct fuse *f, const char *oldpath, nodeid_t dir, |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 584 | const char *oldname) |
| 585 | { |
| 586 | char newname[64]; |
| 587 | char *newpath; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 588 | int err = -EBUSY; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 589 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 590 | if (f->op.rename && f->op.unlink) { |
| 591 | newpath = hidden_name(f, dir, oldname, newname, sizeof(newname)); |
| 592 | if (newpath) { |
| 593 | int res = f->op.rename(oldpath, newpath); |
| 594 | if (res == 0) |
| 595 | err = rename_node(f, dir, oldname, dir, newname, 1); |
| 596 | free(newpath); |
| 597 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 598 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 599 | return err; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 600 | } |
| 601 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 602 | static int lookup_path(struct fuse *f, nodeid_t nodeid, int version, char *name, |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 603 | const char *path, struct fuse_entry_out *arg) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 604 | { |
| 605 | int res; |
| 606 | struct stat buf; |
| 607 | |
| 608 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 609 | if (res == 0) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 610 | struct node *node; |
| 611 | |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 612 | memset(arg, 0, sizeof(struct fuse_entry_out)); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 613 | convert_stat(&buf, &arg->attr); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 614 | node = find_node(f, nodeid, name, &arg->attr, version); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 615 | if (node == NULL) |
| 616 | res = -ENOMEM; |
| 617 | else { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 618 | arg->nodeid = node->nodeid; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 619 | arg->generation = node->generation; |
| 620 | arg->entry_valid = ENTRY_REVALIDATE_TIME; |
| 621 | arg->entry_valid_nsec = 0; |
| 622 | arg->attr_valid = ATTR_REVALIDATE_TIME; |
| 623 | arg->attr_valid_nsec = 0; |
| 624 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 625 | printf(" NODEID: %li\n", arg->nodeid); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 626 | fflush(stdout); |
| 627 | } |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | return res; |
| 631 | } |
| 632 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 633 | static void do_lookup(struct fuse *f, struct fuse_in_header *in, char *name) |
| 634 | { |
| 635 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 636 | int res2; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 637 | char *path; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 638 | struct fuse_entry_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 639 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 640 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 641 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 642 | if (path != NULL) { |
| 643 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 644 | printf("LOOKUP %s\n", path); |
| 645 | fflush(stdout); |
| 646 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 647 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 648 | if (f->op.getattr) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 649 | res = lookup_path(f, in->nodeid, in->unique, name, path, &arg); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 650 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 651 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 652 | res2 = send_reply(f, in, res, &arg, sizeof(arg)); |
| 653 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 654 | destroy_node(f, arg.nodeid, in->unique); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 655 | } |
| 656 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 657 | static void do_forget(struct fuse *f, struct fuse_in_header *in, |
| 658 | struct fuse_forget_in *arg) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 659 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 660 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 661 | printf("FORGET %li/%i\n", in->nodeid, arg->version); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 662 | fflush(stdout); |
| 663 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 664 | destroy_node(f, in->nodeid, arg->version); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 665 | } |
| 666 | |
| 667 | static void do_getattr(struct fuse *f, struct fuse_in_header *in) |
| 668 | { |
| 669 | int res; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 670 | char *path; |
| 671 | struct stat buf; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 672 | struct fuse_attr_out arg; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 673 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 674 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 675 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 676 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 677 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 678 | if (f->op.getattr) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 679 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 680 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 681 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 682 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 683 | if (res == 0) { |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 684 | memset(&arg, 0, sizeof(struct fuse_attr_out)); |
| 685 | arg.attr_valid = ATTR_REVALIDATE_TIME; |
| 686 | arg.attr_valid_nsec = 0; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 687 | convert_stat(&buf, &arg.attr); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 688 | if (!(f->flags & FUSE_USE_INO)) |
| 689 | arg.attr.ino = in->nodeid; |
| 690 | else { |
| 691 | struct node *node = get_node(f, in->nodeid); |
| 692 | node->ino = arg.attr.ino; |
| 693 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 694 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 695 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 696 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 697 | } |
| 698 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 699 | static int do_chmod(struct fuse *f, const char *path, struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 700 | { |
| 701 | int res; |
| 702 | |
| 703 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 704 | if (f->op.chmod) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 705 | res = f->op.chmod(path, attr->mode); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 706 | |
| 707 | return res; |
| 708 | } |
| 709 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 710 | static int do_chown(struct fuse *f, const char *path, struct fuse_attr *attr, |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 711 | int valid) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 712 | { |
| 713 | int res; |
| 714 | uid_t uid = (valid & FATTR_UID) ? attr->uid : (uid_t) -1; |
| 715 | gid_t gid = (valid & FATTR_GID) ? attr->gid : (gid_t) -1; |
| 716 | |
| 717 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 718 | if (f->op.chown) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 719 | res = f->op.chown(path, uid, gid); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 720 | |
| 721 | return res; |
| 722 | } |
| 723 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 724 | static int do_truncate(struct fuse *f, const char *path, |
| 725 | struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 726 | { |
| 727 | int res; |
| 728 | |
| 729 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 730 | if (f->op.truncate) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 731 | res = f->op.truncate(path, attr->size); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 732 | |
| 733 | return res; |
| 734 | } |
| 735 | |
Miklos Szeredi | 680a69a | 2001-11-16 13:31:14 +0000 | [diff] [blame] | 736 | static int do_utime(struct fuse *f, const char *path, struct fuse_attr *attr) |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 737 | { |
| 738 | int res; |
| 739 | struct utimbuf buf; |
| 740 | buf.actime = attr->atime; |
| 741 | buf.modtime = attr->mtime; |
| 742 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 743 | if (f->op.utime) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 744 | res = f->op.utime(path, &buf); |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 745 | |
| 746 | return res; |
| 747 | } |
| 748 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 749 | static void do_setattr(struct fuse *f, struct fuse_in_header *in, |
| 750 | struct fuse_setattr_in *arg) |
| 751 | { |
| 752 | int res; |
| 753 | char *path; |
| 754 | int valid = arg->valid; |
| 755 | struct fuse_attr *attr = &arg->attr; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 756 | struct fuse_attr_out outarg; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 757 | |
| 758 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 759 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 760 | if (path != NULL) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 761 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 762 | if (f->op.getattr) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 763 | res = 0; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 764 | if (!res && (valid & FATTR_MODE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 765 | res = do_chmod(f, path, attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 766 | if (!res && (valid & (FATTR_UID | FATTR_GID))) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 767 | res = do_chown(f, path, attr, valid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 768 | if (!res && (valid & FATTR_SIZE)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 769 | res = do_truncate(f, path, attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 770 | if (!res && (valid & (FATTR_ATIME | FATTR_MTIME)) == |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 771 | (FATTR_ATIME | FATTR_MTIME)) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 772 | res = do_utime(f, path, attr); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 773 | if (!res) { |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 774 | struct stat buf; |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 775 | res = f->op.getattr(path, &buf); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 776 | if (!res) { |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 777 | memset(&outarg, 0, sizeof(struct fuse_attr_out)); |
| 778 | outarg.attr_valid = ATTR_REVALIDATE_TIME; |
| 779 | outarg.attr_valid_nsec = 0; |
Miklos Szeredi | f3ea83b | 2001-11-07 14:55:16 +0000 | [diff] [blame] | 780 | convert_stat(&buf, &outarg.attr); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 781 | if (!(f->flags & FUSE_USE_INO)) |
| 782 | outarg.attr.ino = in->nodeid; |
| 783 | else { |
| 784 | struct node *node = get_node(f, in->nodeid); |
| 785 | node->ino = outarg.attr.ino; |
| 786 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 787 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 788 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 789 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 790 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 791 | } |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 792 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 793 | } |
| 794 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 795 | static void do_readlink(struct fuse *f, struct fuse_in_header *in) |
| 796 | { |
| 797 | int res; |
| 798 | char link[PATH_MAX + 1]; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 799 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 800 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 801 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 802 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 803 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 804 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 805 | if (f->op.readlink) |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 806 | res = f->op.readlink(path, link, sizeof(link)); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 807 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 808 | } |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 809 | link[PATH_MAX] = '\0'; |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 810 | send_reply(f, in, res, link, res == 0 ? strlen(link) : 0); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 811 | } |
| 812 | |
| 813 | static void do_getdir(struct fuse *f, struct fuse_in_header *in) |
| 814 | { |
| 815 | int res; |
| 816 | struct fuse_getdir_out arg; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 817 | struct fuse_dirhandle dh; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 818 | char *path; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 819 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 820 | dh.fuse = f; |
| 821 | dh.fp = tmpfile(); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 822 | dh.dir = in->nodeid; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 823 | |
Miklos Szeredi | 65afea1 | 2004-09-14 07:13:45 +0000 | [diff] [blame] | 824 | res = -EIO; |
| 825 | if (dh.fp == NULL) |
| 826 | perror("fuse: failed to create temporary file"); |
| 827 | else { |
| 828 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 829 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 65afea1 | 2004-09-14 07:13:45 +0000 | [diff] [blame] | 830 | if (path != NULL) { |
| 831 | res = -ENOSYS; |
| 832 | if (f->op.getdir) |
Miklos Szeredi | 8fb48fe | 2004-11-08 14:48:52 +0000 | [diff] [blame] | 833 | res = f->op.getdir(path, &dh, fill_dir); |
Miklos Szeredi | 65afea1 | 2004-09-14 07:13:45 +0000 | [diff] [blame] | 834 | free(path); |
| 835 | } |
| 836 | fflush(dh.fp); |
| 837 | } |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 838 | memset(&arg, 0, sizeof(struct fuse_getdir_out)); |
Miklos Szeredi | 65afea1 | 2004-09-14 07:13:45 +0000 | [diff] [blame] | 839 | if (res == 0) |
| 840 | arg.fd = fileno(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 841 | send_reply(f, in, res, &arg, sizeof(arg)); |
Miklos Szeredi | 65afea1 | 2004-09-14 07:13:45 +0000 | [diff] [blame] | 842 | if (dh.fp != NULL) |
| 843 | fclose(dh.fp); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 844 | } |
| 845 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 846 | static void do_mknod(struct fuse *f, struct fuse_in_header *in, |
| 847 | struct fuse_mknod_in *inarg) |
| 848 | { |
| 849 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 850 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 851 | char *path; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 852 | char *name = PARAM(inarg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 853 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 854 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 855 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 856 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 857 | if (path != NULL) { |
| 858 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 859 | printf("MKNOD %s\n", path); |
| 860 | fflush(stdout); |
| 861 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 862 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 863 | if (f->op.mknod && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 864 | res = f->op.mknod(path, inarg->mode, inarg->rdev); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 865 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 866 | res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 867 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 868 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 869 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 870 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 871 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 872 | destroy_node(f, outarg.nodeid, in->unique); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 873 | } |
| 874 | |
| 875 | static void do_mkdir(struct fuse *f, struct fuse_in_header *in, |
| 876 | struct fuse_mkdir_in *inarg) |
| 877 | { |
| 878 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 879 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 880 | char *path; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 881 | char *name = PARAM(inarg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 882 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 883 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 884 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 885 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 886 | if (path != NULL) { |
| 887 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 888 | printf("MKDIR %s\n", path); |
| 889 | fflush(stdout); |
| 890 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 891 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 892 | if (f->op.mkdir && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 893 | res = f->op.mkdir(path, inarg->mode); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 894 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 895 | res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 896 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 897 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 898 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 899 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 900 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 901 | destroy_node(f, outarg.nodeid, in->unique); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 902 | } |
| 903 | |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 904 | static void do_unlink(struct fuse *f, struct fuse_in_header *in, char *name) |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 905 | { |
| 906 | int res; |
| 907 | char *path; |
| 908 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 909 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 910 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 911 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 912 | if (f->flags & FUSE_DEBUG) { |
| 913 | printf("UNLINK %s\n", path); |
| 914 | fflush(stdout); |
| 915 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 916 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 917 | if (f->op.unlink) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 918 | if (!(f->flags & FUSE_HARD_REMOVE) && is_open(f, in->nodeid, name)) |
| 919 | res = hide_node(f, path, in->nodeid, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 920 | else { |
| 921 | res = f->op.unlink(path); |
| 922 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 923 | remove_node(f, in->nodeid, name); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 924 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 925 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 926 | free(path); |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 927 | } |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 928 | send_reply(f, in, res, NULL, 0); |
| 929 | } |
| 930 | |
| 931 | static void do_rmdir(struct fuse *f, struct fuse_in_header *in, char *name) |
| 932 | { |
| 933 | int res; |
| 934 | char *path; |
| 935 | |
| 936 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 937 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 938 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 939 | if (f->flags & FUSE_DEBUG) { |
| 940 | printf("RMDIR %s\n", path); |
| 941 | fflush(stdout); |
| 942 | } |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 943 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 944 | if (f->op.rmdir) { |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 945 | res = f->op.rmdir(path); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 946 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 947 | remove_node(f, in->nodeid, name); |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 948 | } |
| 949 | free(path); |
| 950 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 951 | send_reply(f, in, res, NULL, 0); |
| 952 | } |
| 953 | |
| 954 | static void do_symlink(struct fuse *f, struct fuse_in_header *in, char *name, |
| 955 | char *link) |
| 956 | { |
| 957 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 958 | int res2; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 959 | char *path; |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 960 | struct fuse_entry_out outarg; |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 961 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 962 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 963 | path = get_path_name(f, in->nodeid, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 964 | if (path != NULL) { |
| 965 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 966 | printf("SYMLINK %s\n", path); |
| 967 | fflush(stdout); |
| 968 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 969 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 970 | if (f->op.symlink && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 971 | res = f->op.symlink(link, path); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 972 | if (res == 0) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 973 | res = lookup_path(f, in->nodeid, in->unique, name, path, &outarg); |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 974 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 975 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 976 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 977 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 978 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 979 | destroy_node(f, outarg.nodeid, in->unique); |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 980 | |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 981 | } |
| 982 | |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 983 | static void do_rename(struct fuse *f, struct fuse_in_header *in, |
| 984 | struct fuse_rename_in *inarg) |
| 985 | { |
| 986 | int res; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 987 | nodeid_t olddir = in->nodeid; |
| 988 | nodeid_t newdir = inarg->newdir; |
Miklos Szeredi | 6bf8b68 | 2002-10-28 08:49:39 +0000 | [diff] [blame] | 989 | char *oldname = PARAM(inarg); |
| 990 | char *newname = oldname + strlen(oldname) + 1; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 991 | char *oldpath; |
| 992 | char *newpath; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 993 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 994 | res = -ENOENT; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 995 | oldpath = get_path_name(f, olddir, oldname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 996 | if (oldpath != NULL) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 997 | newpath = get_path_name(f, newdir, newname); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 998 | if (newpath != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 999 | if (f->flags & FUSE_DEBUG) { |
| 1000 | printf("RENAME %s -> %s\n", oldpath, newpath); |
| 1001 | fflush(stdout); |
| 1002 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1003 | res = -ENOSYS; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1004 | if (f->op.rename) { |
| 1005 | res = 0; |
Miklos Szeredi | 2529ca2 | 2004-07-13 15:36:52 +0000 | [diff] [blame] | 1006 | if (!(f->flags & FUSE_HARD_REMOVE) && |
| 1007 | is_open(f, newdir, newname)) |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1008 | res = hide_node(f, newpath, newdir, newname); |
| 1009 | if (res == 0) { |
| 1010 | res = f->op.rename(oldpath, newpath); |
| 1011 | if (res == 0) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1012 | res = rename_node(f, olddir, oldname, newdir, newname, 0); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1013 | } |
| 1014 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1015 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1016 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1017 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1018 | } |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1019 | send_reply(f, in, res, NULL, 0); |
| 1020 | } |
| 1021 | |
| 1022 | static void do_link(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1023 | struct fuse_link_in *arg) |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1024 | { |
| 1025 | int res; |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 1026 | int res2; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1027 | char *oldpath; |
| 1028 | char *newpath; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1029 | char *name = PARAM(arg); |
Miklos Szeredi | 254d5ed | 2004-03-02 11:11:24 +0000 | [diff] [blame] | 1030 | struct fuse_entry_out outarg; |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1031 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1032 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1033 | oldpath = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1034 | if (oldpath != NULL) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1035 | newpath = get_path_name(f, arg->newdir, name); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1036 | if (newpath != NULL) { |
| 1037 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1038 | printf("LINK %s\n", newpath); |
| 1039 | fflush(stdout); |
| 1040 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1041 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1042 | if (f->op.link && f->op.getattr) { |
Miklos Szeredi | 0a7077f | 2001-11-11 18:20:17 +0000 | [diff] [blame] | 1043 | res = f->op.link(oldpath, newpath); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1044 | if (res == 0) |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1045 | res = lookup_path(f, arg->newdir, in->unique, name, |
| 1046 | newpath, &outarg); |
| 1047 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1048 | free(newpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1049 | } |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1050 | free(oldpath); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1051 | } |
Miklos Szeredi | 2778f6c | 2004-06-21 09:45:30 +0000 | [diff] [blame] | 1052 | res2 = send_reply(f, in, res, &outarg, sizeof(outarg)); |
| 1053 | if (res == 0 && res2 == -ENOENT) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1054 | destroy_node(f, outarg.nodeid, in->unique); |
Miklos Szeredi | 19dff1b | 2001-10-30 15:06:52 +0000 | [diff] [blame] | 1055 | } |
| 1056 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1057 | static void do_open(struct fuse *f, struct fuse_in_header *in, |
| 1058 | struct fuse_open_in *arg) |
| 1059 | { |
| 1060 | int res; |
| 1061 | char *path; |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1062 | struct fuse_open_out outarg; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1063 | struct fuse_file_info fi; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1064 | |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1065 | memset(&fi, 0, sizeof(fi)); |
| 1066 | fi.flags = arg->flags; |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1067 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1068 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1069 | if (path != NULL) { |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1070 | res = -ENOSYS; |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1071 | if (f->op.open.curr) { |
| 1072 | if (!f->compat) |
| 1073 | res = f->op.open.curr(path, &fi); |
| 1074 | else |
| 1075 | res = f->op.open.compat2(path, fi.flags); |
| 1076 | } |
Miklos Szeredi | 40b9a5a | 2002-12-10 16:09:02 +0000 | [diff] [blame] | 1077 | } |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1078 | if (res == 0) { |
| 1079 | int res2; |
| 1080 | |
| 1081 | /* If the request is interrupted the lock must be held until |
| 1082 | the cancellation is finished. Otherwise there could be |
| 1083 | races with rename/unlink, against which the kernel can't |
| 1084 | protect */ |
| 1085 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1086 | outarg.fh = fi.fh; |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1087 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 1eea032 | 2004-09-27 18:50:11 +0000 | [diff] [blame] | 1088 | printf("OPEN[%lu] flags: 0x%x\n", outarg.fh, arg->flags); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1089 | fflush(stdout); |
| 1090 | } |
| 1091 | |
| 1092 | res2 = __send_reply(f, in, res, &outarg, sizeof(outarg), 1); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1093 | if(res2 == -ENOENT) { |
| 1094 | /* The open syscall was interrupted, so it must be cancelled */ |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1095 | if(f->op.release.curr) { |
| 1096 | if (!f->compat) |
| 1097 | f->op.release.curr(path, &fi); |
| 1098 | else |
| 1099 | f->op.release.compat2(path, fi.flags); |
| 1100 | } |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1101 | } else |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1102 | get_node(f, in->nodeid)->open_count ++; |
Miklos Szeredi | 73798f9 | 2004-07-12 15:55:11 +0000 | [diff] [blame] | 1103 | pthread_mutex_unlock(&f->lock); |
| 1104 | |
| 1105 | } else |
| 1106 | send_reply(f, in, res, NULL, 0); |
| 1107 | |
Miklos Szeredi | 9a31cca | 2004-06-26 21:11:25 +0000 | [diff] [blame] | 1108 | if (path) |
| 1109 | free(path); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1112 | static void do_flush(struct fuse *f, struct fuse_in_header *in, |
| 1113 | struct fuse_flush_in *arg) |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1114 | { |
| 1115 | char *path; |
| 1116 | int res; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1117 | struct fuse_file_info fi; |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1118 | |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1119 | memset(&fi, 0, sizeof(fi)); |
| 1120 | fi.fh = arg->fh; |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1121 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1122 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1123 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1124 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 1eea032 | 2004-09-27 18:50:11 +0000 | [diff] [blame] | 1125 | printf("FLUSH[%lu]\n", arg->fh); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1126 | fflush(stdout); |
| 1127 | } |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1128 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1129 | if (f->op.flush) |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1130 | res = f->op.flush(path, &fi); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1131 | free(path); |
| 1132 | } |
| 1133 | send_reply(f, in, res, NULL, 0); |
| 1134 | } |
| 1135 | |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1136 | static void do_release(struct fuse *f, struct fuse_in_header *in, |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1137 | struct fuse_release_in *arg) |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1138 | { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1139 | struct node *node; |
| 1140 | char *path; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1141 | struct fuse_file_info fi; |
| 1142 | |
| 1143 | memset(&fi, 0, sizeof(fi)); |
| 1144 | fi.flags = arg->flags; |
| 1145 | fi.fh = arg->fh; |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1146 | |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1147 | pthread_mutex_lock(&f->lock); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1148 | node = get_node(f, in->nodeid); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1149 | --node->open_count; |
| 1150 | pthread_mutex_unlock(&f->lock); |
| 1151 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1152 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1153 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1154 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 1eea032 | 2004-09-27 18:50:11 +0000 | [diff] [blame] | 1155 | printf("RELEASE[%lu]\n", arg->fh); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1156 | fflush(stdout); |
| 1157 | } |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1158 | if (f->op.release.curr) { |
| 1159 | if (!f->compat) |
| 1160 | f->op.release.curr(path, &fi); |
| 1161 | else |
| 1162 | f->op.release.compat2(path, fi.flags); |
| 1163 | } |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1164 | |
| 1165 | if(node->is_hidden && node->open_count == 0) |
| 1166 | /* can now clean up this hidden file */ |
| 1167 | f->op.unlink(path); |
| 1168 | |
| 1169 | free(path); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1170 | } |
Miklos Szeredi | 556d03d | 2004-06-30 11:13:41 +0000 | [diff] [blame] | 1171 | send_reply(f, in, 0, NULL, 0); |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1172 | } |
| 1173 | |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1174 | static void do_read(struct fuse *f, struct fuse_in_header *in, |
| 1175 | struct fuse_read_in *arg) |
| 1176 | { |
| 1177 | int res; |
| 1178 | char *path; |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1179 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + arg->size); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1180 | if (outbuf == NULL) |
| 1181 | send_reply(f, in, -ENOMEM, NULL, 0); |
| 1182 | else { |
| 1183 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 1184 | char *buf = outbuf + sizeof(struct fuse_out_header); |
| 1185 | size_t size; |
| 1186 | size_t outsize; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1187 | struct fuse_file_info fi; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1188 | |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1189 | memset(&fi, 0, sizeof(fi)); |
| 1190 | fi.fh = arg->fh; |
| 1191 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1192 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1193 | path = get_path(f, in->nodeid); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1194 | if (path != NULL) { |
| 1195 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 1eea032 | 2004-09-27 18:50:11 +0000 | [diff] [blame] | 1196 | printf("READ[%lu] %u bytes from %llu\n", arg->fh, arg->size, |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1197 | arg->offset); |
| 1198 | fflush(stdout); |
| 1199 | } |
| 1200 | |
| 1201 | res = -ENOSYS; |
| 1202 | if (f->op.read) |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1203 | res = f->op.read(path, buf, arg->size, arg->offset, &fi); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1204 | free(path); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1205 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1206 | |
| 1207 | size = 0; |
| 1208 | if (res >= 0) { |
| 1209 | size = res; |
| 1210 | res = 0; |
| 1211 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 1eea032 | 2004-09-27 18:50:11 +0000 | [diff] [blame] | 1212 | printf(" READ[%lu] %u bytes\n", arg->fh, size); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1213 | fflush(stdout); |
| 1214 | } |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1215 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1216 | memset(out, 0, sizeof(struct fuse_out_header)); |
| 1217 | out->unique = in->unique; |
| 1218 | out->error = res; |
| 1219 | outsize = sizeof(struct fuse_out_header) + size; |
| 1220 | |
| 1221 | send_reply_raw(f, outbuf, outsize, 0); |
| 1222 | free(outbuf); |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1223 | } |
Miklos Szeredi | 5e18348 | 2001-10-31 14:52:35 +0000 | [diff] [blame] | 1224 | } |
Miklos Szeredi | b483c93 | 2001-10-29 14:57:57 +0000 | [diff] [blame] | 1225 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1226 | static void do_write(struct fuse *f, struct fuse_in_header *in, |
| 1227 | struct fuse_write_in *arg) |
| 1228 | { |
| 1229 | int res; |
| 1230 | char *path; |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 1231 | struct fuse_write_out outarg; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1232 | struct fuse_file_info fi; |
| 1233 | |
| 1234 | memset(&fi, 0, sizeof(fi)); |
| 1235 | fi.fh = arg->fh; |
Miklos Szeredi | d59bb9d | 2004-12-07 10:04:24 +0000 | [diff] [blame^] | 1236 | fi.writepage = arg->writepage; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1237 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1238 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1239 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1240 | if (path != NULL) { |
| 1241 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 1eea032 | 2004-09-27 18:50:11 +0000 | [diff] [blame] | 1242 | printf("WRITE%s[%lu] %u bytes to %llu\n", |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1243 | arg->writepage ? "PAGE" : "", arg->fh, arg->size, |
| 1244 | arg->offset); |
Miklos Szeredi | 6ebe234 | 2002-01-06 21:44:16 +0000 | [diff] [blame] | 1245 | fflush(stdout); |
| 1246 | } |
| 1247 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1248 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1249 | if (f->op.write) |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1250 | res = f->op.write(path, PARAM(arg), arg->size, arg->offset, &fi); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1251 | free(path); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1252 | } |
| 1253 | |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 1254 | if (res >= 0) { |
| 1255 | outarg.size = res; |
| 1256 | res = 0; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1257 | } |
| 1258 | |
Miklos Szeredi | ad051c3 | 2004-07-02 09:22:50 +0000 | [diff] [blame] | 1259 | send_reply(f, in, res, &outarg, sizeof(outarg)); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1260 | } |
| 1261 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1262 | static int default_statfs(struct statfs *buf) |
| 1263 | { |
| 1264 | buf->f_namelen = 255; |
| 1265 | buf->f_bsize = 512; |
| 1266 | return 0; |
| 1267 | } |
| 1268 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1269 | static void convert_statfs_compat(struct _fuse_statfs_compat1 *compatbuf, |
| 1270 | struct statfs *statfs) |
| 1271 | { |
| 1272 | statfs->f_bsize = compatbuf->block_size; |
| 1273 | statfs->f_blocks = compatbuf->blocks; |
| 1274 | statfs->f_bfree = compatbuf->blocks_free; |
| 1275 | statfs->f_bavail = compatbuf->blocks_free; |
| 1276 | statfs->f_files = compatbuf->files; |
| 1277 | statfs->f_ffree = compatbuf->files_free; |
| 1278 | statfs->f_namelen = compatbuf->namelen; |
| 1279 | } |
| 1280 | |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1281 | static void convert_statfs(struct statfs *statfs, struct fuse_kstatfs *kstatfs) |
| 1282 | { |
| 1283 | kstatfs->bsize = statfs->f_bsize; |
| 1284 | kstatfs->blocks = statfs->f_blocks; |
| 1285 | kstatfs->bfree = statfs->f_bfree; |
| 1286 | kstatfs->bavail = statfs->f_bavail; |
| 1287 | kstatfs->files = statfs->f_files; |
| 1288 | kstatfs->ffree = statfs->f_ffree; |
| 1289 | kstatfs->namelen = statfs->f_namelen; |
| 1290 | } |
| 1291 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1292 | static void do_statfs(struct fuse *f, struct fuse_in_header *in) |
| 1293 | { |
| 1294 | int res; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1295 | struct fuse_statfs_out arg; |
Miklos Szeredi | 18e75e4 | 2004-02-19 14:23:27 +0000 | [diff] [blame] | 1296 | struct statfs buf; |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1297 | |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1298 | memset(&buf, 0, sizeof(struct statfs)); |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1299 | if (f->op.statfs.curr) { |
| 1300 | if (!f->compat || f->compat > 11) |
| 1301 | res = f->op.statfs.curr("/", &buf); |
| 1302 | else { |
| 1303 | struct _fuse_statfs_compat1 compatbuf; |
| 1304 | memset(&compatbuf, 0, sizeof(struct _fuse_statfs_compat1)); |
| 1305 | res = f->op.statfs.compat1(&compatbuf); |
| 1306 | if (res == 0) |
| 1307 | convert_statfs_compat(&compatbuf, &buf); |
| 1308 | } |
| 1309 | } |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1310 | else |
| 1311 | res = default_statfs(&buf); |
| 1312 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1313 | if (res == 0) |
Miklos Szeredi | 77f3994 | 2004-03-25 11:17:52 +0000 | [diff] [blame] | 1314 | convert_statfs(&buf, &arg.st); |
Miklos Szeredi | 4b2bef4 | 2002-01-09 12:23:27 +0000 | [diff] [blame] | 1315 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1316 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1317 | } |
| 1318 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1319 | static void do_fsync(struct fuse *f, struct fuse_in_header *in, |
| 1320 | struct fuse_fsync_in *inarg) |
| 1321 | { |
| 1322 | int res; |
| 1323 | char *path; |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1324 | struct fuse_file_info fi; |
| 1325 | |
| 1326 | memset(&fi, 0, sizeof(fi)); |
| 1327 | fi.fh = inarg->fh; |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1328 | |
| 1329 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1330 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1331 | if (path != NULL) { |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1332 | if (f->flags & FUSE_DEBUG) { |
Miklos Szeredi | 1eea032 | 2004-09-27 18:50:11 +0000 | [diff] [blame] | 1333 | printf("FSYNC[%lu]\n", inarg->fh); |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1334 | fflush(stdout); |
| 1335 | } |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1336 | res = -ENOSYS; |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1337 | if (f->op.fsync) |
Miklos Szeredi | fb28c5e | 2004-11-26 12:15:06 +0000 | [diff] [blame] | 1338 | res = f->op.fsync(path, inarg->datasync, &fi); |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1339 | free(path); |
| 1340 | } |
| 1341 | send_reply(f, in, res, NULL, 0); |
| 1342 | } |
| 1343 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1344 | static void do_setxattr(struct fuse *f, struct fuse_in_header *in, |
| 1345 | struct fuse_setxattr_in *arg) |
| 1346 | { |
| 1347 | int res; |
| 1348 | char *path; |
| 1349 | char *name = PARAM(arg); |
| 1350 | unsigned char *value = name + strlen(name) + 1; |
| 1351 | |
| 1352 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1353 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1354 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1355 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1356 | if (f->op.setxattr) |
| 1357 | res = f->op.setxattr(path, name, value, arg->size, arg->flags); |
| 1358 | free(path); |
| 1359 | } |
| 1360 | send_reply(f, in, res, NULL, 0); |
| 1361 | } |
| 1362 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1363 | static int common_getxattr(struct fuse *f, struct fuse_in_header *in, |
| 1364 | const char *name, char *value, size_t size) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1365 | { |
| 1366 | int res; |
| 1367 | char *path; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1368 | |
| 1369 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1370 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1371 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1372 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1373 | if (f->op.getxattr) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1374 | res = f->op.getxattr(path, name, value, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1375 | free(path); |
| 1376 | } |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1377 | return res; |
| 1378 | } |
| 1379 | |
| 1380 | static void do_getxattr_read(struct fuse *f, struct fuse_in_header *in, |
| 1381 | const char *name, size_t size) |
| 1382 | { |
| 1383 | int res; |
| 1384 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1385 | if (outbuf == NULL) |
| 1386 | send_reply(f, in, -ENOMEM, NULL, 0); |
| 1387 | else { |
| 1388 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 1389 | char *value = outbuf + sizeof(struct fuse_out_header); |
| 1390 | |
| 1391 | res = common_getxattr(f, in, name, value, size); |
| 1392 | size = 0; |
| 1393 | if (res > 0) { |
| 1394 | size = res; |
| 1395 | res = 0; |
| 1396 | } |
| 1397 | memset(out, 0, sizeof(struct fuse_out_header)); |
| 1398 | out->unique = in->unique; |
| 1399 | out->error = res; |
| 1400 | |
| 1401 | send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0); |
| 1402 | free(outbuf); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1403 | } |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1404 | } |
| 1405 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1406 | static void do_getxattr_size(struct fuse *f, struct fuse_in_header *in, |
| 1407 | const char *name) |
| 1408 | { |
| 1409 | int res; |
| 1410 | struct fuse_getxattr_out arg; |
| 1411 | |
| 1412 | res = common_getxattr(f, in, name, NULL, 0); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1413 | if (res >= 0) { |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1414 | arg.size = res; |
| 1415 | res = 0; |
| 1416 | } |
| 1417 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1418 | } |
| 1419 | |
| 1420 | static void do_getxattr(struct fuse *f, struct fuse_in_header *in, |
| 1421 | struct fuse_getxattr_in *arg) |
| 1422 | { |
| 1423 | char *name = PARAM(arg); |
| 1424 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1425 | if (arg->size) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1426 | do_getxattr_read(f, in, name, arg->size); |
| 1427 | else |
| 1428 | do_getxattr_size(f, in, name); |
| 1429 | } |
| 1430 | |
| 1431 | static int common_listxattr(struct fuse *f, struct fuse_in_header *in, |
| 1432 | char *list, size_t size) |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1433 | { |
| 1434 | int res; |
| 1435 | char *path; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1436 | |
| 1437 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1438 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1439 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1440 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1441 | if (f->op.listxattr) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1442 | res = f->op.listxattr(path, list, size); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1443 | free(path); |
| 1444 | } |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1445 | return res; |
| 1446 | } |
| 1447 | |
| 1448 | static void do_listxattr_read(struct fuse *f, struct fuse_in_header *in, |
| 1449 | size_t size) |
| 1450 | { |
| 1451 | int res; |
| 1452 | char *outbuf = (char *) malloc(sizeof(struct fuse_out_header) + size); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1453 | if (outbuf == NULL) |
| 1454 | send_reply(f, in, -ENOMEM, NULL, 0); |
| 1455 | else { |
| 1456 | struct fuse_out_header *out = (struct fuse_out_header *) outbuf; |
| 1457 | char *list = outbuf + sizeof(struct fuse_out_header); |
| 1458 | |
| 1459 | res = common_listxattr(f, in, list, size); |
| 1460 | size = 0; |
| 1461 | if (res > 0) { |
| 1462 | size = res; |
| 1463 | res = 0; |
| 1464 | } |
| 1465 | memset(out, 0, sizeof(struct fuse_out_header)); |
| 1466 | out->unique = in->unique; |
| 1467 | out->error = res; |
| 1468 | |
| 1469 | send_reply_raw(f, outbuf, sizeof(struct fuse_out_header) + size, 0); |
| 1470 | free(outbuf); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1471 | } |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1472 | } |
| 1473 | |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1474 | static void do_listxattr_size(struct fuse *f, struct fuse_in_header *in) |
| 1475 | { |
| 1476 | int res; |
| 1477 | struct fuse_getxattr_out arg; |
| 1478 | |
| 1479 | res = common_listxattr(f, in, NULL, 0); |
| 1480 | if (res >= 0) { |
| 1481 | arg.size = res; |
| 1482 | res = 0; |
| 1483 | } |
| 1484 | send_reply(f, in, res, &arg, sizeof(arg)); |
| 1485 | } |
| 1486 | |
| 1487 | static void do_listxattr(struct fuse *f, struct fuse_in_header *in, |
| 1488 | struct fuse_getxattr_in *arg) |
| 1489 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1490 | if (arg->size) |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1491 | do_listxattr_read(f, in, arg->size); |
| 1492 | else |
| 1493 | do_listxattr_size(f, in); |
| 1494 | } |
| 1495 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1496 | static void do_removexattr(struct fuse *f, struct fuse_in_header *in, |
| 1497 | char *name) |
| 1498 | { |
| 1499 | int res; |
| 1500 | char *path; |
| 1501 | |
| 1502 | res = -ENOENT; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1503 | path = get_path(f, in->nodeid); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1504 | if (path != NULL) { |
Miklos Szeredi | 63b8c1c | 2004-06-03 14:45:04 +0000 | [diff] [blame] | 1505 | res = -ENOSYS; |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1506 | if (f->op.removexattr) |
| 1507 | res = f->op.removexattr(path, name); |
| 1508 | free(path); |
| 1509 | } |
| 1510 | send_reply(f, in, res, NULL, 0); |
| 1511 | } |
| 1512 | |
| 1513 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1514 | static void free_cmd(struct fuse_cmd *cmd) |
| 1515 | { |
| 1516 | free(cmd->buf); |
| 1517 | free(cmd); |
| 1518 | } |
| 1519 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1520 | void __fuse_process_cmd(struct fuse *f, struct fuse_cmd *cmd) |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1521 | { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1522 | struct fuse_in_header *in = (struct fuse_in_header *) cmd->buf; |
| 1523 | void *inarg = cmd->buf + sizeof(struct fuse_in_header); |
| 1524 | size_t argsize; |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1525 | struct fuse_context *ctx = fuse_get_context(); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1526 | |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1527 | dec_avail(f); |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1528 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1529 | if ((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1530 | printf("unique: %i, opcode: %s (%i), nodeid: %li, insize: %i\n", |
| 1531 | in->unique, opname(in->opcode), in->opcode, in->nodeid, |
Miklos Szeredi | c8ba237 | 2002-12-10 12:26:00 +0000 | [diff] [blame] | 1532 | cmd->buflen); |
Miklos Szeredi | c0938ea | 2001-11-07 12:35:06 +0000 | [diff] [blame] | 1533 | fflush(stdout); |
| 1534 | } |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 1535 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1536 | ctx->fuse = f; |
Miklos Szeredi | fe25def | 2001-12-20 15:38:05 +0000 | [diff] [blame] | 1537 | ctx->uid = in->uid; |
| 1538 | ctx->gid = in->gid; |
Miklos Szeredi | 1f18db5 | 2004-09-27 06:54:49 +0000 | [diff] [blame] | 1539 | ctx->pid = in->pid; |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1540 | |
| 1541 | argsize = cmd->buflen - sizeof(struct fuse_in_header); |
| 1542 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1543 | switch (in->opcode) { |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1544 | case FUSE_LOOKUP: |
| 1545 | do_lookup(f, in, (char *) inarg); |
| 1546 | break; |
| 1547 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1548 | case FUSE_GETATTR: |
| 1549 | do_getattr(f, in); |
| 1550 | break; |
| 1551 | |
| 1552 | case FUSE_SETATTR: |
| 1553 | do_setattr(f, in, (struct fuse_setattr_in *) inarg); |
| 1554 | break; |
| 1555 | |
| 1556 | case FUSE_READLINK: |
| 1557 | do_readlink(f, in); |
| 1558 | break; |
| 1559 | |
| 1560 | case FUSE_GETDIR: |
| 1561 | do_getdir(f, in); |
| 1562 | break; |
| 1563 | |
| 1564 | case FUSE_MKNOD: |
| 1565 | do_mknod(f, in, (struct fuse_mknod_in *) inarg); |
| 1566 | break; |
| 1567 | |
| 1568 | case FUSE_MKDIR: |
| 1569 | do_mkdir(f, in, (struct fuse_mkdir_in *) inarg); |
| 1570 | break; |
| 1571 | |
| 1572 | case FUSE_UNLINK: |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1573 | do_unlink(f, in, (char *) inarg); |
| 1574 | break; |
| 1575 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1576 | case FUSE_RMDIR: |
Miklos Szeredi | b595861 | 2004-02-20 14:10:49 +0000 | [diff] [blame] | 1577 | do_rmdir(f, in, (char *) inarg); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1578 | break; |
| 1579 | |
| 1580 | case FUSE_SYMLINK: |
| 1581 | do_symlink(f, in, (char *) inarg, |
| 1582 | ((char *) inarg) + strlen((char *) inarg) + 1); |
| 1583 | break; |
| 1584 | |
| 1585 | case FUSE_RENAME: |
| 1586 | do_rename(f, in, (struct fuse_rename_in *) inarg); |
| 1587 | break; |
| 1588 | |
| 1589 | case FUSE_LINK: |
| 1590 | do_link(f, in, (struct fuse_link_in *) inarg); |
| 1591 | break; |
| 1592 | |
| 1593 | case FUSE_OPEN: |
| 1594 | do_open(f, in, (struct fuse_open_in *) inarg); |
| 1595 | break; |
| 1596 | |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1597 | case FUSE_FLUSH: |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1598 | do_flush(f, in, (struct fuse_flush_in *) inarg); |
Miklos Szeredi | e2e4ac2 | 2004-05-18 08:45:28 +0000 | [diff] [blame] | 1599 | break; |
| 1600 | |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1601 | case FUSE_RELEASE: |
Miklos Szeredi | 209f5d0 | 2004-07-24 19:56:16 +0000 | [diff] [blame] | 1602 | do_release(f, in, (struct fuse_release_in *) inarg); |
Miklos Szeredi | 9478e86 | 2002-12-11 09:50:26 +0000 | [diff] [blame] | 1603 | break; |
| 1604 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1605 | case FUSE_READ: |
| 1606 | do_read(f, in, (struct fuse_read_in *) inarg); |
| 1607 | break; |
| 1608 | |
| 1609 | case FUSE_WRITE: |
| 1610 | do_write(f, in, (struct fuse_write_in *) inarg); |
| 1611 | break; |
| 1612 | |
Mark Glines | d84b39a | 2002-01-07 16:32:02 +0000 | [diff] [blame] | 1613 | case FUSE_STATFS: |
| 1614 | do_statfs(f, in); |
| 1615 | break; |
| 1616 | |
Miklos Szeredi | 5e43f2c | 2003-12-12 14:06:41 +0000 | [diff] [blame] | 1617 | case FUSE_FSYNC: |
| 1618 | do_fsync(f, in, (struct fuse_fsync_in *) inarg); |
| 1619 | break; |
| 1620 | |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1621 | case FUSE_SETXATTR: |
| 1622 | do_setxattr(f, in, (struct fuse_setxattr_in *) inarg); |
| 1623 | break; |
| 1624 | |
| 1625 | case FUSE_GETXATTR: |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1626 | do_getxattr(f, in, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1627 | break; |
| 1628 | |
| 1629 | case FUSE_LISTXATTR: |
Miklos Szeredi | 03cebae | 2004-03-31 10:19:18 +0000 | [diff] [blame] | 1630 | do_listxattr(f, in, (struct fuse_getxattr_in *) inarg); |
Miklos Szeredi | 3ed8423 | 2004-03-30 15:17:26 +0000 | [diff] [blame] | 1631 | break; |
| 1632 | |
| 1633 | case FUSE_REMOVEXATTR: |
| 1634 | do_removexattr(f, in, (char *) inarg); |
| 1635 | break; |
| 1636 | |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1637 | default: |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1638 | send_reply(f, in, -ENOSYS, NULL, 0); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1639 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1640 | |
| 1641 | free_cmd(cmd); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1642 | } |
| 1643 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1644 | int __fuse_exited(struct fuse* f) |
| 1645 | { |
| 1646 | return f->exited; |
| 1647 | } |
| 1648 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1649 | struct fuse_cmd *__fuse_read_cmd(struct fuse *f) |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1650 | { |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1651 | ssize_t res; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1652 | struct fuse_cmd *cmd; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1653 | struct fuse_in_header *in; |
| 1654 | void *inarg; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1655 | |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1656 | cmd = (struct fuse_cmd *) malloc(sizeof(struct fuse_cmd)); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1657 | if (cmd == NULL) { |
| 1658 | fprintf(stderr, "fuse: failed to allocate cmd in read\n"); |
| 1659 | return NULL; |
| 1660 | } |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1661 | cmd->buf = (char *) malloc(FUSE_MAX_IN); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1662 | if (cmd->buf == NULL) { |
| 1663 | fprintf(stderr, "fuse: failed to allocate read buffer\n"); |
| 1664 | free(cmd); |
| 1665 | return NULL; |
| 1666 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1667 | in = (struct fuse_in_header *) cmd->buf; |
| 1668 | inarg = cmd->buf + sizeof(struct fuse_in_header); |
Miklos Szeredi | 4369643 | 2001-11-18 19:15:05 +0000 | [diff] [blame] | 1669 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1670 | res = read(f->fd, cmd->buf, FUSE_MAX_IN); |
| 1671 | if (res == -1) { |
| 1672 | free_cmd(cmd); |
| 1673 | if (__fuse_exited(f) || errno == EINTR) |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1674 | return NULL; |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1675 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1676 | /* ENODEV means we got unmounted, so we silenty return failure */ |
| 1677 | if (errno != ENODEV) { |
| 1678 | /* BAD... This will happen again */ |
| 1679 | perror("fuse: reading device"); |
| 1680 | } |
| 1681 | |
| 1682 | fuse_exit(f); |
| 1683 | return NULL; |
| 1684 | } |
| 1685 | if ((size_t) res < sizeof(struct fuse_in_header)) { |
| 1686 | free_cmd(cmd); |
| 1687 | /* Cannot happen */ |
| 1688 | fprintf(stderr, "short read on fuse device\n"); |
| 1689 | fuse_exit(f); |
| 1690 | return NULL; |
| 1691 | } |
| 1692 | cmd->buflen = res; |
| 1693 | |
| 1694 | /* Forget is special, it can be done without messing with threads. */ |
| 1695 | if (in->opcode == FUSE_FORGET) { |
| 1696 | do_forget(f, in, (struct fuse_forget_in *) inarg); |
| 1697 | free_cmd(cmd); |
| 1698 | return NULL; |
| 1699 | } |
Miklos Szeredi | 99ddf0e | 2001-11-25 17:19:59 +0000 | [diff] [blame] | 1700 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1701 | return cmd; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1702 | } |
| 1703 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1704 | int fuse_loop(struct fuse *f) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1705 | { |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1706 | if (f == NULL) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1707 | return -1; |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1708 | |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1709 | while (1) { |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1710 | struct fuse_cmd *cmd; |
| 1711 | |
Miklos Szeredi | 65cf7c7 | 2004-06-30 11:34:56 +0000 | [diff] [blame] | 1712 | if (__fuse_exited(f)) |
Miklos Szeredi | 874e3c1 | 2004-11-01 23:15:20 +0000 | [diff] [blame] | 1713 | break; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1714 | |
| 1715 | cmd = __fuse_read_cmd(f); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1716 | if (cmd == NULL) |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1717 | continue; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1718 | |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1719 | __fuse_process_cmd(f, cmd); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1720 | } |
Miklos Szeredi | 874e3c1 | 2004-11-01 23:15:20 +0000 | [diff] [blame] | 1721 | f->exited = 0; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1722 | return 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1723 | } |
| 1724 | |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1725 | int fuse_invalidate(struct fuse *f, const char *path) |
| 1726 | { |
| 1727 | int res; |
| 1728 | int err; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1729 | nodeid_t nodeid; |
| 1730 | unsigned long ino; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1731 | struct fuse_user_header h; |
| 1732 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1733 | err = path_lookup(f, path, &nodeid, &ino); |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1734 | if (err) { |
| 1735 | if (err == -ENOENT) |
| 1736 | return 0; |
| 1737 | else |
| 1738 | return err; |
| 1739 | } |
| 1740 | |
| 1741 | memset(&h, 0, sizeof(struct fuse_user_header)); |
| 1742 | h.opcode = FUSE_INVALIDATE; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1743 | h.nodeid = nodeid; |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1744 | h.ino = ino; |
| 1745 | |
| 1746 | if ((f->flags & FUSE_DEBUG)) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1747 | printf("INVALIDATE nodeid: %li\n", nodeid); |
Miklos Szeredi | 891b874 | 2004-07-29 09:27:49 +0000 | [diff] [blame] | 1748 | fflush(stdout); |
| 1749 | } |
| 1750 | |
| 1751 | res = write(f->fd, &h, sizeof(struct fuse_user_header)); |
| 1752 | if (res == -1) { |
| 1753 | if (errno != ENOENT) { |
| 1754 | perror("fuse: writing device"); |
| 1755 | return -errno; |
| 1756 | } |
| 1757 | } |
| 1758 | return 0; |
| 1759 | } |
| 1760 | |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1761 | void fuse_exit(struct fuse *f) |
| 1762 | { |
| 1763 | f->exited = 1; |
| 1764 | } |
| 1765 | |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1766 | struct fuse_context *fuse_get_context() |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1767 | { |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1768 | static struct fuse_context context; |
| 1769 | if (fuse_getcontext) |
| 1770 | return fuse_getcontext(); |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1771 | else |
Miklos Szeredi | d169f31 | 2004-09-22 08:48:26 +0000 | [diff] [blame] | 1772 | return &context; |
| 1773 | } |
| 1774 | |
| 1775 | void __fuse_set_getcontext_func(struct fuse_context *(*func)(void)) |
| 1776 | { |
| 1777 | fuse_getcontext = func; |
Miklos Szeredi | 2e50d43 | 2001-12-20 12:17:25 +0000 | [diff] [blame] | 1778 | } |
| 1779 | |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1780 | static int check_version(struct fuse *f) |
| 1781 | { |
| 1782 | int res; |
Miklos Szeredi | 162bcbb | 2004-11-29 23:43:44 +0000 | [diff] [blame] | 1783 | const char *version_file = FUSE_VERSION_FILE_NEW; |
Miklos Szeredi | f3845c4 | 2004-11-20 11:18:34 +0000 | [diff] [blame] | 1784 | FILE *vf = fopen(version_file, "r"); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1785 | if (vf == NULL) { |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 1786 | version_file = FUSE_VERSION_FILE_OLD; |
Miklos Szeredi | f3845c4 | 2004-11-20 11:18:34 +0000 | [diff] [blame] | 1787 | vf = fopen(version_file, "r"); |
| 1788 | if (vf == NULL) { |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 1789 | struct stat tmp; |
| 1790 | if (stat(FUSE_DEV_OLD, &tmp) != -1) { |
| 1791 | fprintf(stderr, "fuse: kernel interface too old, need >= %i.%i\n", |
| 1792 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1793 | return -1; |
| 1794 | } else { |
| 1795 | fprintf(stderr, "fuse: warning: version of kernel interface unknown\n"); |
| 1796 | return 0; |
| 1797 | } |
Miklos Szeredi | f3845c4 | 2004-11-20 11:18:34 +0000 | [diff] [blame] | 1798 | } |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1799 | } |
| 1800 | res = fscanf(vf, "%i.%i", &f->majorver, &f->minorver); |
| 1801 | fclose(vf); |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1802 | if (res != 2) { |
Miklos Szeredi | f3845c4 | 2004-11-20 11:18:34 +0000 | [diff] [blame] | 1803 | fprintf(stderr, "fuse: error reading %s\n", version_file); |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1804 | return -1; |
| 1805 | } |
Miklos Szeredi | 7eafcce | 2004-06-19 22:42:38 +0000 | [diff] [blame] | 1806 | if (f->majorver != FUSE_KERNEL_VERSION) { |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1807 | fprintf(stderr, "fuse: bad kernel interface major version: needs %i\n", |
| 1808 | FUSE_KERNEL_VERSION); |
| 1809 | return -1; |
| 1810 | } |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1811 | if (f->minorver < FUSE_KERNEL_MINOR_VERSION_NEED) { |
Miklos Szeredi | 256739a | 2004-11-20 12:22:37 +0000 | [diff] [blame] | 1812 | fprintf(stderr, "fuse: kernel interface too old: need >= %i.%i\n", |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1813 | FUSE_KERNEL_VERSION, FUSE_KERNEL_MINOR_VERSION); |
| 1814 | return -1; |
| 1815 | } |
| 1816 | |
| 1817 | return 0; |
| 1818 | } |
| 1819 | |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1820 | |
| 1821 | int fuse_is_lib_option(const char *opt) |
| 1822 | { |
| 1823 | if (strcmp(opt, "debug") == 0 || |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1824 | strcmp(opt, "hard_remove") == 0 || |
| 1825 | strcmp(opt, "use_ino") == 0) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1826 | return 1; |
| 1827 | else |
| 1828 | return 0; |
| 1829 | } |
| 1830 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1831 | static int parse_lib_opts(struct fuse *f, const char *opts) |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1832 | { |
| 1833 | if (opts) { |
| 1834 | char *xopts = strdup(opts); |
| 1835 | char *s = xopts; |
| 1836 | char *opt; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1837 | |
| 1838 | if (xopts == NULL) |
| 1839 | return -1; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1840 | |
| 1841 | while((opt = strsep(&s, ","))) { |
| 1842 | if (strcmp(opt, "debug") == 0) |
| 1843 | f->flags |= FUSE_DEBUG; |
| 1844 | else if (strcmp(opt, "hard_remove") == 0) |
| 1845 | f->flags |= FUSE_HARD_REMOVE; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1846 | else if (strcmp(opt, "use_ino") == 0) |
| 1847 | f->flags |= FUSE_USE_INO; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1848 | else |
| 1849 | fprintf(stderr, "fuse: warning: unknown option `%s'\n", opt); |
| 1850 | } |
| 1851 | free(xopts); |
| 1852 | } |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1853 | return 0; |
Miklos Szeredi | bd7661b | 2004-07-23 17:16:29 +0000 | [diff] [blame] | 1854 | } |
| 1855 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1856 | struct fuse *fuse_new_common(int fd, const char *opts, |
| 1857 | const struct fuse_operations *op, |
| 1858 | size_t op_size, int compat) |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1859 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1860 | struct fuse *f; |
| 1861 | struct node *root; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1862 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1863 | if (sizeof(struct fuse_operations_i) < op_size) { |
| 1864 | fprintf(stderr, "fuse: warning: library too old, some operations may not not work\n"); |
| 1865 | op_size = sizeof(struct fuse_operations_i); |
| 1866 | } |
| 1867 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1868 | f = (struct fuse *) calloc(1, sizeof(struct fuse)); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1869 | if (f == NULL) |
| 1870 | goto out; |
Miklos Szeredi | 2df1c04 | 2001-11-06 15:07:17 +0000 | [diff] [blame] | 1871 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1872 | if (check_version(f) == -1) |
| 1873 | goto out_free; |
Miklos Szeredi | c40748a | 2004-02-20 16:38:45 +0000 | [diff] [blame] | 1874 | |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1875 | if (parse_lib_opts(f, opts) == -1) |
| 1876 | goto out_free; |
| 1877 | |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1878 | f->fd = fd; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1879 | f->ctr = 0; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1880 | f->generation = 0; |
Miklos Szeredi | fff56ab | 2001-11-16 10:12:59 +0000 | [diff] [blame] | 1881 | /* FIXME: Dynamic hash table */ |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1882 | f->name_table_size = 14057; |
| 1883 | f->name_table = (struct node **) |
| 1884 | calloc(1, sizeof(struct node *) * f->name_table_size); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1885 | if (f->name_table == NULL) |
| 1886 | goto out_free; |
| 1887 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1888 | f->id_table_size = 14057; |
| 1889 | f->id_table = (struct node **) |
| 1890 | calloc(1, sizeof(struct node *) * f->id_table_size); |
| 1891 | if (f->id_table == NULL) |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1892 | goto out_free_name_table; |
| 1893 | |
Miklos Szeredi | a25d4c2 | 2004-11-23 22:32:16 +0000 | [diff] [blame] | 1894 | #ifndef USE_UCLIBC |
| 1895 | pthread_mutex_init(&f->lock, NULL); |
| 1896 | #else |
| 1897 | { |
| 1898 | pthread_mutexattr_t attr; |
| 1899 | pthread_mutexattr_init(&attr); |
| 1900 | pthread_mutexattr_settype(&attr, PTHREAD_MUTEX_ADAPTIVE_NP); |
| 1901 | pthread_mutex_init(&f->lock, &attr); |
| 1902 | pthread_mutexattr_destroy(&attr); |
| 1903 | } |
| 1904 | #endif |
Miklos Szeredi | 3323203 | 2001-11-19 17:55:51 +0000 | [diff] [blame] | 1905 | f->numworker = 0; |
| 1906 | f->numavail = 0; |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1907 | memcpy(&f->op, op, op_size); |
| 1908 | f->compat = compat; |
Miklos Szeredi | 0f48a26 | 2002-12-05 14:23:01 +0000 | [diff] [blame] | 1909 | f->exited = 0; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1910 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1911 | root = (struct node *) calloc(1, sizeof(struct node)); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1912 | if (root == NULL) |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1913 | goto out_free_id_table; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1914 | |
Miklos Szeredi | 8cffdb9 | 2001-11-09 14:49:18 +0000 | [diff] [blame] | 1915 | root->mode = 0; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1916 | root->rdev = 0; |
| 1917 | root->name = strdup("/"); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1918 | if (root->name == NULL) |
| 1919 | goto out_free_root; |
| 1920 | |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1921 | root->parent = 0; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1922 | root->nodeid = FUSE_ROOT_ID; |
Miklos Szeredi | 76f6578 | 2004-02-19 16:55:40 +0000 | [diff] [blame] | 1923 | root->generation = 0; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1924 | hash_id(f, root); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1925 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1926 | return f; |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1927 | |
| 1928 | out_free_root: |
| 1929 | free(root); |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1930 | out_free_id_table: |
| 1931 | free(f->id_table); |
Miklos Szeredi | b2cf956 | 2004-09-16 08:42:40 +0000 | [diff] [blame] | 1932 | out_free_name_table: |
| 1933 | free(f->name_table); |
| 1934 | out_free: |
| 1935 | free(f); |
| 1936 | out: |
| 1937 | fprintf(stderr, "fuse: failed to allocate fuse object\n"); |
| 1938 | return NULL; |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1939 | } |
| 1940 | |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1941 | struct fuse *fuse_new(int fd, const char *opts, |
| 1942 | const struct fuse_operations *op, size_t op_size) |
| 1943 | { |
| 1944 | return fuse_new_common(fd, opts, op, op_size, 0); |
| 1945 | } |
| 1946 | |
| 1947 | struct fuse *_fuse_new_compat2(int fd, const char *opts, |
| 1948 | const struct _fuse_operations_compat2 *op) |
| 1949 | { |
| 1950 | return fuse_new_common(fd, opts, (struct fuse_operations *) op, |
| 1951 | sizeof(struct _fuse_operations_compat2), 21); |
| 1952 | } |
| 1953 | |
| 1954 | struct fuse *_fuse_new_compat1(int fd, int flags, |
| 1955 | const struct _fuse_operations_compat1 *op) |
| 1956 | { |
| 1957 | char *opts = NULL; |
| 1958 | if (flags & _FUSE_DEBUG_COMPAT1) |
| 1959 | opts = "debug"; |
| 1960 | return fuse_new_common(fd, opts, (struct fuse_operations *) op, |
| 1961 | sizeof(struct _fuse_operations_compat1), 11); |
| 1962 | } |
| 1963 | |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1964 | void fuse_destroy(struct fuse *f) |
| 1965 | { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1966 | size_t i; |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1967 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1968 | struct node *node; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1969 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1970 | for (node = f->id_table[i]; node != NULL; node = node->id_next) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1971 | if (node->is_hidden) { |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1972 | char *path = get_path(f, node->nodeid); |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1973 | if (path) |
| 1974 | f->op.unlink(path); |
| 1975 | } |
| 1976 | } |
| 1977 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1978 | for (i = 0; i < f->id_table_size; i++) { |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1979 | struct node *node; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1980 | struct node *next; |
Miklos Szeredi | 1ea9c96 | 2004-06-24 21:00:00 +0000 | [diff] [blame] | 1981 | |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1982 | for (node = f->id_table[i]; node != NULL; node = next) { |
| 1983 | next = node->id_next; |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1984 | free_node(node); |
| 1985 | } |
| 1986 | } |
Miklos Szeredi | a13d900 | 2004-11-02 17:32:03 +0000 | [diff] [blame] | 1987 | free(f->id_table); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1988 | free(f->name_table); |
Miklos Szeredi | a181e61 | 2001-11-06 12:03:23 +0000 | [diff] [blame] | 1989 | pthread_mutex_destroy(&f->lock); |
Miklos Szeredi | 97c61e9 | 2001-11-07 12:09:43 +0000 | [diff] [blame] | 1990 | free(f); |
Miklos Szeredi | 85c74fc | 2001-10-28 19:44:14 +0000 | [diff] [blame] | 1991 | } |
Miklos Szeredi | 0b6a0ad | 2004-12-04 00:40:50 +0000 | [diff] [blame] | 1992 | |
| 1993 | __asm__(".symver _fuse_new_compat2,fuse_new@"); |