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