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