Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2010 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdio.h> |
| 18 | #include <stdlib.h> |
| 19 | #include <string.h> |
| 20 | #include <unistd.h> |
| 21 | #include <errno.h> |
| 22 | #include <fcntl.h> |
| 23 | #include <sys/mount.h> |
| 24 | #include <sys/stat.h> |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 25 | #include <sys/statfs.h> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 26 | #include <sys/uio.h> |
| 27 | #include <dirent.h> |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 28 | #include <limits.h> |
Mike Lockwood | 1bedb73 | 2011-01-13 13:38:42 -0500 | [diff] [blame] | 29 | #include <ctype.h> |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 30 | #include <pthread.h> |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 31 | |
Brian Swetland | b14a2c6 | 2010-08-12 18:21:12 -0700 | [diff] [blame] | 32 | #include <private/android_filesystem_config.h> |
| 33 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 34 | #include "fuse.h" |
| 35 | |
| 36 | /* README |
| 37 | * |
| 38 | * What is this? |
| 39 | * |
| 40 | * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style |
| 41 | * directory permissions (all files are given fixed owner, group, and |
| 42 | * permissions at creation, owner, group, and permissions are not |
| 43 | * changeable, symlinks and hardlinks are not createable, etc. |
| 44 | * |
| 45 | * usage: sdcard <path> <uid> <gid> |
| 46 | * |
| 47 | * It must be run as root, but will change to uid/gid as soon as it |
Jeff Sharkey | cfa9f65 | 2012-04-09 17:09:38 -0700 | [diff] [blame] | 48 | * mounts a filesystem on /storage/sdcard. It will refuse to run if uid or |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 49 | * gid are zero. |
| 50 | * |
| 51 | * |
| 52 | * Things I believe to be true: |
| 53 | * |
| 54 | * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK, |
| 55 | * CREAT) must bump that node's refcount |
| 56 | * - don't forget that FORGET can forget multiple references (req->nlookup) |
| 57 | * - if an op that returns a fuse_entry fails writing the reply to the |
| 58 | * kernel, you must rollback the refcount to reflect the reference the |
| 59 | * kernel did not actually acquire |
| 60 | * |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 61 | */ |
| 62 | |
| 63 | #define FUSE_TRACE 0 |
| 64 | |
| 65 | #if FUSE_TRACE |
| 66 | #define TRACE(x...) fprintf(stderr,x) |
| 67 | #else |
| 68 | #define TRACE(x...) do {} while (0) |
| 69 | #endif |
| 70 | |
| 71 | #define ERROR(x...) fprintf(stderr,x) |
| 72 | |
| 73 | #define FUSE_UNKNOWN_INO 0xffffffff |
| 74 | |
Jeff Sharkey | cfa9f65 | 2012-04-09 17:09:38 -0700 | [diff] [blame] | 75 | #define MOUNT_POINT "/storage/sdcard0" |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 76 | |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 77 | /* Maximum number of bytes to write in one request. */ |
| 78 | #define MAX_WRITE (256 * 1024) |
| 79 | |
| 80 | /* Maximum number of bytes to read in one request. */ |
| 81 | #define MAX_READ (128 * 1024) |
| 82 | |
| 83 | /* Largest possible request. |
| 84 | * The request size is bounded by the maximum size of a FUSE_WRITE request because it has |
| 85 | * the largest possible data payload. */ |
| 86 | #define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE) |
| 87 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 88 | /* Default number of threads. */ |
| 89 | #define DEFAULT_NUM_THREADS 2 |
| 90 | |
| 91 | /* Pseudo-error constant used to indicate that no fuse status is needed |
| 92 | * or that a reply has already been written. */ |
| 93 | #define NO_STATUS 1 |
| 94 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 95 | struct handle { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 96 | int fd; |
| 97 | }; |
| 98 | |
| 99 | struct dirhandle { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 100 | DIR *d; |
| 101 | }; |
| 102 | |
| 103 | struct node { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 104 | __u32 refcount; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 105 | __u64 nid; |
| 106 | __u64 gen; |
| 107 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 108 | struct node *next; /* per-dir sibling list */ |
| 109 | struct node *child; /* first contained file by this dir */ |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 110 | struct node *parent; /* containing directory */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 111 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 112 | size_t namelen; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 113 | char *name; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 114 | /* If non-null, this is the real name of the file in the underlying storage. |
| 115 | * This may differ from the field "name" only by case. |
| 116 | * strlen(actual_name) will always equal strlen(name), so it is safe to use |
| 117 | * namelen for both fields. |
| 118 | */ |
| 119 | char *actual_name; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 120 | }; |
| 121 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 122 | /* Global data structure shared by all fuse handlers. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 123 | struct fuse { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 124 | pthread_mutex_t lock; |
| 125 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 126 | __u64 next_generation; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 127 | int fd; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 128 | struct node root; |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 129 | char rootpath[PATH_MAX]; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 132 | /* Private data used by a single fuse handler. */ |
| 133 | struct fuse_handler { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 134 | struct fuse* fuse; |
| 135 | int token; |
| 136 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 137 | /* To save memory, we never use the contents of the request buffer and the read |
| 138 | * buffer at the same time. This allows us to share the underlying storage. */ |
| 139 | union { |
| 140 | __u8 request_buffer[MAX_REQUEST_SIZE]; |
| 141 | __u8 read_buffer[MAX_READ]; |
| 142 | }; |
| 143 | }; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 144 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 145 | static inline void *id_to_ptr(__u64 nid) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 146 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 147 | return (void *) (uintptr_t) nid; |
| 148 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 149 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 150 | static inline __u64 ptr_to_id(void *ptr) |
| 151 | { |
| 152 | return (__u64) (uintptr_t) ptr; |
| 153 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 154 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 155 | static void acquire_node_locked(struct node* node) |
| 156 | { |
| 157 | node->refcount++; |
| 158 | TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount); |
| 159 | } |
| 160 | |
| 161 | static void remove_node_from_parent_locked(struct node* node); |
| 162 | |
| 163 | static void release_node_locked(struct node* node) |
| 164 | { |
| 165 | TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount); |
| 166 | if (node->refcount > 0) { |
| 167 | node->refcount--; |
| 168 | if (!node->refcount) { |
| 169 | TRACE("DESTROY %p (%s)\n", node, node->name); |
| 170 | remove_node_from_parent_locked(node); |
| 171 | |
| 172 | /* TODO: remove debugging - poison memory */ |
| 173 | memset(node->name, 0xef, node->namelen); |
| 174 | free(node->name); |
| 175 | free(node->actual_name); |
| 176 | memset(node, 0xfc, sizeof(*node)); |
| 177 | free(node); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 178 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 179 | } else { |
| 180 | ERROR("Zero refcnt %p\n", node); |
| 181 | } |
| 182 | } |
| 183 | |
| 184 | static void add_node_to_parent_locked(struct node *node, struct node *parent) { |
| 185 | node->parent = parent; |
| 186 | node->next = parent->child; |
| 187 | parent->child = node; |
| 188 | acquire_node_locked(parent); |
| 189 | } |
| 190 | |
| 191 | static void remove_node_from_parent_locked(struct node* node) |
| 192 | { |
| 193 | if (node->parent) { |
| 194 | if (node->parent->child == node) { |
| 195 | node->parent->child = node->parent->child->next; |
| 196 | } else { |
| 197 | struct node *node2; |
| 198 | node2 = node->parent->child; |
| 199 | while (node2->next != node) |
| 200 | node2 = node2->next; |
| 201 | node2->next = node->next; |
| 202 | } |
| 203 | release_node_locked(node->parent); |
| 204 | node->parent = NULL; |
| 205 | node->next = NULL; |
| 206 | } |
| 207 | } |
| 208 | |
| 209 | /* Gets the absolute path to a node into the provided buffer. |
| 210 | * |
| 211 | * Populates 'buf' with the path and returns the length of the path on success, |
| 212 | * or returns -1 if the path is too long for the provided buffer. |
| 213 | */ |
| 214 | static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize) |
| 215 | { |
| 216 | size_t namelen = node->namelen; |
| 217 | if (bufsize < namelen + 1) { |
| 218 | return -1; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 219 | } |
| 220 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 221 | ssize_t pathlen = 0; |
| 222 | if (node->parent) { |
| 223 | pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2); |
| 224 | if (pathlen < 0) { |
| 225 | return -1; |
| 226 | } |
| 227 | buf[pathlen++] = '/'; |
| 228 | } |
| 229 | |
| 230 | const char* name = node->actual_name ? node->actual_name : node->name; |
| 231 | memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */ |
| 232 | return pathlen + namelen; |
| 233 | } |
| 234 | |
| 235 | /* Finds the absolute path of a file within a given directory. |
| 236 | * Performs a case-insensitive search for the file and sets the buffer to the path |
| 237 | * of the first matching file. If 'search' is zero or if no match is found, sets |
| 238 | * the buffer to the path that the file would have, assuming the name were case-sensitive. |
| 239 | * |
| 240 | * Populates 'buf' with the path and returns the actual name (within 'buf') on success, |
| 241 | * or returns NULL if the path is too long for the provided buffer. |
| 242 | */ |
| 243 | static char* find_file_within(const char* path, const char* name, |
| 244 | char* buf, size_t bufsize, int search) |
| 245 | { |
| 246 | size_t pathlen = strlen(path); |
| 247 | size_t namelen = strlen(name); |
| 248 | size_t childlen = pathlen + namelen + 1; |
| 249 | char* actual; |
| 250 | |
| 251 | if (bufsize <= childlen) { |
| 252 | return NULL; |
| 253 | } |
| 254 | |
| 255 | memcpy(buf, path, pathlen); |
| 256 | buf[pathlen] = '/'; |
| 257 | actual = buf + pathlen + 1; |
| 258 | memcpy(actual, name, namelen + 1); |
| 259 | |
| 260 | if (search && access(buf, F_OK)) { |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 261 | struct dirent* entry; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 262 | DIR* dir = opendir(path); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 263 | if (!dir) { |
| 264 | ERROR("opendir %s failed: %s", path, strerror(errno)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 265 | return actual; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 266 | } |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 267 | while ((entry = readdir(dir))) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 268 | if (!strcasecmp(entry->d_name, name)) { |
| 269 | /* we have a match - replace the name, don't need to copy the null again */ |
| 270 | memcpy(actual, entry->d_name, namelen); |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 271 | break; |
| 272 | } |
| 273 | } |
| 274 | closedir(dir); |
| 275 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 276 | return actual; |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 277 | } |
| 278 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 279 | static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, __u64 nid) |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 280 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 281 | attr->ino = nid; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 282 | attr->size = s->st_size; |
| 283 | attr->blocks = s->st_blocks; |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 284 | attr->atime = s->st_atime; |
| 285 | attr->mtime = s->st_mtime; |
| 286 | attr->ctime = s->st_ctime; |
| 287 | attr->atimensec = s->st_atime_nsec; |
| 288 | attr->mtimensec = s->st_mtime_nsec; |
| 289 | attr->ctimensec = s->st_ctime_nsec; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 290 | attr->mode = s->st_mode; |
| 291 | attr->nlink = s->st_nlink; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 292 | |
Brian Swetland | b14a2c6 | 2010-08-12 18:21:12 -0700 | [diff] [blame] | 293 | /* force permissions to something reasonable: |
| 294 | * world readable |
| 295 | * writable by the sdcard group |
| 296 | */ |
| 297 | if (attr->mode & 0100) { |
| 298 | attr->mode = (attr->mode & (~0777)) | 0775; |
| 299 | } else { |
| 300 | attr->mode = (attr->mode & (~0777)) | 0664; |
| 301 | } |
| 302 | |
| 303 | /* all files owned by root.sdcard */ |
| 304 | attr->uid = 0; |
| 305 | attr->gid = AID_SDCARD_RW; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 306 | } |
| 307 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 308 | struct node *create_node_locked(struct fuse* fuse, |
| 309 | struct node *parent, const char *name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 310 | { |
| 311 | struct node *node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 312 | size_t namelen = strlen(name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 313 | |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 314 | node = calloc(1, sizeof(struct node)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 315 | if (!node) { |
| 316 | return NULL; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 317 | } |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 318 | node->name = malloc(namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 319 | if (!node->name) { |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 320 | free(node); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 321 | return NULL; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 322 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 323 | memcpy(node->name, name, namelen + 1); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 324 | if (strcmp(name, actual_name)) { |
| 325 | node->actual_name = malloc(namelen + 1); |
| 326 | if (!node->actual_name) { |
| 327 | free(node->name); |
| 328 | free(node); |
| 329 | return NULL; |
| 330 | } |
| 331 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 332 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 333 | node->namelen = namelen; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 334 | node->nid = ptr_to_id(node); |
| 335 | node->gen = fuse->next_generation++; |
| 336 | acquire_node_locked(node); |
| 337 | add_node_to_parent_locked(node, parent); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 338 | return node; |
| 339 | } |
| 340 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 341 | static int rename_node_locked(struct node *node, const char *name, |
| 342 | const char* actual_name) |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 343 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 344 | size_t namelen = strlen(name); |
| 345 | int need_actual_name = strcmp(name, actual_name); |
| 346 | |
| 347 | /* make the storage bigger without actually changing the name |
| 348 | * in case an error occurs part way */ |
| 349 | if (namelen > node->namelen) { |
| 350 | char* new_name = realloc(node->name, namelen + 1); |
| 351 | if (!new_name) { |
| 352 | return -ENOMEM; |
| 353 | } |
| 354 | node->name = new_name; |
| 355 | if (need_actual_name && node->actual_name) { |
| 356 | char* new_actual_name = realloc(node->actual_name, namelen + 1); |
| 357 | if (!new_actual_name) { |
| 358 | return -ENOMEM; |
| 359 | } |
| 360 | node->actual_name = new_actual_name; |
| 361 | } |
| 362 | } |
| 363 | |
| 364 | /* update the name, taking care to allocate storage before overwriting the old name */ |
| 365 | if (need_actual_name) { |
| 366 | if (!node->actual_name) { |
| 367 | node->actual_name = malloc(namelen + 1); |
| 368 | if (!node->actual_name) { |
| 369 | return -ENOMEM; |
| 370 | } |
| 371 | } |
| 372 | memcpy(node->actual_name, actual_name, namelen + 1); |
| 373 | } else { |
| 374 | free(node->actual_name); |
| 375 | node->actual_name = NULL; |
| 376 | } |
| 377 | memcpy(node->name, name, namelen + 1); |
| 378 | node->namelen = namelen; |
| 379 | return 0; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 380 | } |
| 381 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 382 | static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 383 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 384 | if (nid == FUSE_ROOT_ID) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 385 | return &fuse->root; |
| 386 | } else { |
| 387 | return id_to_ptr(nid); |
| 388 | } |
| 389 | } |
| 390 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 391 | static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid, |
| 392 | char* buf, size_t bufsize) |
| 393 | { |
| 394 | struct node* node = lookup_node_by_id_locked(fuse, nid); |
| 395 | if (node && get_node_path_locked(node, buf, bufsize) < 0) { |
| 396 | node = NULL; |
| 397 | } |
| 398 | return node; |
| 399 | } |
| 400 | |
| 401 | static struct node *lookup_child_by_name_locked(struct node *node, const char *name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 402 | { |
| 403 | for (node = node->child; node; node = node->next) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 404 | /* use exact string comparison, nodes that differ by case |
| 405 | * must be considered distinct even if they refer to the same |
| 406 | * underlying file as otherwise operations such as "mv x x" |
| 407 | * will not work because the source and target nodes are the same. */ |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 408 | if (!strcmp(name, node->name)) { |
| 409 | return node; |
| 410 | } |
| 411 | } |
| 412 | return 0; |
| 413 | } |
| 414 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 415 | static struct node* acquire_or_create_child_locked( |
| 416 | struct fuse* fuse, struct node* parent, |
| 417 | const char* name, const char* actual_name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 418 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 419 | struct node* child = lookup_child_by_name_locked(parent, name); |
| 420 | if (child) { |
| 421 | acquire_node_locked(child); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 422 | } else { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 423 | child = create_node_locked(fuse, parent, name, actual_name); |
Paul Eastham | 77085c5 | 2011-01-04 21:06:03 -0800 | [diff] [blame] | 424 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 425 | return child; |
Paul Eastham | 11ccdb3 | 2010-10-14 11:04:26 -0700 | [diff] [blame] | 426 | } |
| 427 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 428 | static void fuse_init(struct fuse *fuse, int fd, const char *path) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 429 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 430 | pthread_mutex_init(&fuse->lock, NULL); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 431 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 432 | fuse->fd = fd; |
| 433 | fuse->next_generation = 0; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 434 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 435 | memset(&fuse->root, 0, sizeof(fuse->root)); |
| 436 | fuse->root.nid = FUSE_ROOT_ID; /* 1 */ |
| 437 | fuse->root.refcount = 2; |
| 438 | fuse->root.namelen = strlen(path); |
| 439 | fuse->root.name = strdup(path); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 442 | static void fuse_status(struct fuse *fuse, __u64 unique, int err) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 443 | { |
| 444 | struct fuse_out_header hdr; |
| 445 | hdr.len = sizeof(hdr); |
| 446 | hdr.error = err; |
| 447 | hdr.unique = unique; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 448 | write(fuse->fd, &hdr, sizeof(hdr)); |
| 449 | } |
| 450 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 451 | static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 452 | { |
| 453 | struct fuse_out_header hdr; |
| 454 | struct iovec vec[2]; |
| 455 | int res; |
| 456 | |
| 457 | hdr.len = len + sizeof(hdr); |
| 458 | hdr.error = 0; |
| 459 | hdr.unique = unique; |
| 460 | |
| 461 | vec[0].iov_base = &hdr; |
| 462 | vec[0].iov_len = sizeof(hdr); |
| 463 | vec[1].iov_base = data; |
| 464 | vec[1].iov_len = len; |
| 465 | |
| 466 | res = writev(fuse->fd, vec, 2); |
| 467 | if (res < 0) { |
| 468 | ERROR("*** REPLY FAILED *** %d\n", errno); |
| 469 | } |
| 470 | } |
| 471 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 472 | static int fuse_reply_entry(struct fuse* fuse, __u64 unique, |
| 473 | struct node* parent, const char* name, const char* actual_name, |
| 474 | const char* path) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 475 | { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 476 | struct node* node; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 477 | struct fuse_entry_out out; |
| 478 | struct stat s; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 479 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 480 | if (lstat(path, &s) < 0) { |
| 481 | return -errno; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 482 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 483 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 484 | pthread_mutex_lock(&fuse->lock); |
| 485 | node = acquire_or_create_child_locked(fuse, parent, name, actual_name); |
| 486 | if (!node) { |
| 487 | pthread_mutex_unlock(&fuse->lock); |
| 488 | return -ENOMEM; |
| 489 | } |
| 490 | memset(&out, 0, sizeof(out)); |
| 491 | attr_from_stat(&out.attr, &s, node->nid); |
| 492 | out.attr_valid = 10; |
| 493 | out.entry_valid = 10; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 494 | out.nodeid = node->nid; |
| 495 | out.generation = node->gen; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 496 | pthread_mutex_unlock(&fuse->lock); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 497 | fuse_reply(fuse, unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 498 | return NO_STATUS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 499 | } |
| 500 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 501 | static int fuse_reply_attr(struct fuse* fuse, __u64 unique, __u64 nid, |
| 502 | const char* path) |
| 503 | { |
| 504 | struct fuse_attr_out out; |
| 505 | struct stat s; |
| 506 | |
| 507 | if (lstat(path, &s) < 0) { |
| 508 | return -errno; |
| 509 | } |
| 510 | memset(&out, 0, sizeof(out)); |
| 511 | attr_from_stat(&out.attr, &s, nid); |
| 512 | out.attr_valid = 10; |
| 513 | fuse_reply(fuse, unique, &out, sizeof(out)); |
| 514 | return NO_STATUS; |
| 515 | } |
| 516 | |
| 517 | static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 518 | const struct fuse_in_header *hdr, const char* name) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 519 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 520 | struct node* parent_node; |
| 521 | char parent_path[PATH_MAX]; |
| 522 | char child_path[PATH_MAX]; |
| 523 | const char* actual_name; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 524 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 525 | pthread_mutex_lock(&fuse->lock); |
| 526 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 527 | parent_path, sizeof(parent_path)); |
| 528 | TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid, |
| 529 | parent_node ? parent_node->name : "?"); |
| 530 | pthread_mutex_unlock(&fuse->lock); |
| 531 | |
| 532 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 533 | child_path, sizeof(child_path), 1))) { |
| 534 | return -ENOENT; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 535 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 536 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 537 | } |
| 538 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 539 | static int handle_forget(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 540 | const struct fuse_in_header *hdr, const struct fuse_forget_in *req) |
| 541 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 542 | struct node* node; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 543 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 544 | pthread_mutex_lock(&fuse->lock); |
| 545 | node = lookup_node_by_id_locked(fuse, hdr->nodeid); |
| 546 | TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup, |
| 547 | hdr->nodeid, node ? node->name : "?"); |
| 548 | if (node) { |
| 549 | __u64 n = req->nlookup; |
| 550 | while (n--) { |
| 551 | release_node_locked(node); |
| 552 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 553 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 554 | pthread_mutex_unlock(&fuse->lock); |
| 555 | return NO_STATUS; /* no reply */ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 556 | } |
| 557 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 558 | static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 559 | const struct fuse_in_header *hdr, const struct fuse_getattr_in *req) |
| 560 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 561 | struct node* node; |
| 562 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 563 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 564 | pthread_mutex_lock(&fuse->lock); |
| 565 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 566 | TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token, |
| 567 | req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?"); |
| 568 | pthread_mutex_unlock(&fuse->lock); |
| 569 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 570 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 571 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 572 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 573 | return fuse_reply_attr(fuse, hdr->unique, hdr->nodeid, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 574 | } |
| 575 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 576 | static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 577 | const struct fuse_in_header *hdr, const struct fuse_setattr_in *req) |
| 578 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 579 | struct node* node; |
| 580 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 581 | struct timespec times[2]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 582 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 583 | pthread_mutex_lock(&fuse->lock); |
| 584 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 585 | TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token, |
| 586 | req->fh, req->valid, hdr->nodeid, node ? node->name : "?"); |
| 587 | pthread_mutex_unlock(&fuse->lock); |
| 588 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 589 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 590 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 591 | } |
| 592 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 593 | /* XXX: incomplete implementation on purpose. |
| 594 | * chmod/chown should NEVER be implemented.*/ |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 595 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 596 | if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) { |
| 597 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 598 | } |
| 599 | |
| 600 | /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW |
| 601 | * are both set, then set it to the current time. Else, set it to the |
| 602 | * time specified in the request. Same goes for mtime. Use utimensat(2) |
| 603 | * as it allows ATIME and MTIME to be changed independently, and has |
| 604 | * nanosecond resolution which fuse also has. |
| 605 | */ |
| 606 | if (req->valid & (FATTR_ATIME | FATTR_MTIME)) { |
| 607 | times[0].tv_nsec = UTIME_OMIT; |
| 608 | times[1].tv_nsec = UTIME_OMIT; |
| 609 | if (req->valid & FATTR_ATIME) { |
| 610 | if (req->valid & FATTR_ATIME_NOW) { |
| 611 | times[0].tv_nsec = UTIME_NOW; |
| 612 | } else { |
| 613 | times[0].tv_sec = req->atime; |
| 614 | times[0].tv_nsec = req->atimensec; |
| 615 | } |
| 616 | } |
| 617 | if (req->valid & FATTR_MTIME) { |
| 618 | if (req->valid & FATTR_MTIME_NOW) { |
| 619 | times[1].tv_nsec = UTIME_NOW; |
| 620 | } else { |
| 621 | times[1].tv_sec = req->mtime; |
| 622 | times[1].tv_nsec = req->mtimensec; |
| 623 | } |
| 624 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 625 | TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n", |
| 626 | handler->token, path, times[0].tv_sec, times[1].tv_sec); |
| 627 | if (utimensat(-1, path, times, 0) < 0) { |
| 628 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 629 | } |
| 630 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 631 | return fuse_reply_attr(fuse, hdr->unique, hdr->nodeid, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 632 | } |
| 633 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 634 | static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 635 | const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name) |
| 636 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 637 | struct node* parent_node; |
| 638 | char parent_path[PATH_MAX]; |
| 639 | char child_path[PATH_MAX]; |
| 640 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 641 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 642 | pthread_mutex_lock(&fuse->lock); |
| 643 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 644 | parent_path, sizeof(parent_path)); |
| 645 | TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token, |
| 646 | name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 647 | pthread_mutex_unlock(&fuse->lock); |
| 648 | |
| 649 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 650 | child_path, sizeof(child_path), 1))) { |
| 651 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 652 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 653 | __u32 mode = (req->mode & (~0777)) | 0664; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 654 | if (mknod(child_path, mode, req->rdev) < 0) { |
| 655 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 656 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 657 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 658 | } |
| 659 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 660 | static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 661 | const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name) |
| 662 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 663 | struct node* parent_node; |
| 664 | char parent_path[PATH_MAX]; |
| 665 | char child_path[PATH_MAX]; |
| 666 | const char* actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 667 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 668 | pthread_mutex_lock(&fuse->lock); |
| 669 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 670 | parent_path, sizeof(parent_path)); |
| 671 | TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token, |
| 672 | name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 673 | pthread_mutex_unlock(&fuse->lock); |
| 674 | |
| 675 | if (!parent_node || !(actual_name = find_file_within(parent_path, name, |
| 676 | child_path, sizeof(child_path), 1))) { |
| 677 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 678 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 679 | __u32 mode = (req->mode & (~0777)) | 0775; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 680 | if (mkdir(child_path, mode) < 0) { |
| 681 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 682 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 683 | return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 684 | } |
| 685 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 686 | static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 687 | const struct fuse_in_header* hdr, const char* name) |
| 688 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 689 | struct node* parent_node; |
| 690 | char parent_path[PATH_MAX]; |
| 691 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 692 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 693 | pthread_mutex_lock(&fuse->lock); |
| 694 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 695 | parent_path, sizeof(parent_path)); |
| 696 | TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token, |
| 697 | name, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 698 | pthread_mutex_unlock(&fuse->lock); |
| 699 | |
| 700 | if (!parent_node || !find_file_within(parent_path, name, |
| 701 | child_path, sizeof(child_path), 1)) { |
| 702 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 703 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 704 | if (unlink(child_path) < 0) { |
| 705 | return -errno; |
| 706 | } |
| 707 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 708 | } |
| 709 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 710 | static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 711 | const struct fuse_in_header* hdr, const char* name) |
| 712 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 713 | struct node* parent_node; |
| 714 | char parent_path[PATH_MAX]; |
| 715 | char child_path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 716 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 717 | pthread_mutex_lock(&fuse->lock); |
| 718 | parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 719 | parent_path, sizeof(parent_path)); |
| 720 | TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token, |
| 721 | name, hdr->nodeid, parent_node ? parent_node->name : "?"); |
| 722 | pthread_mutex_unlock(&fuse->lock); |
| 723 | |
| 724 | if (!parent_node || !find_file_within(parent_path, name, |
| 725 | child_path, sizeof(child_path), 1)) { |
| 726 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 727 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 728 | if (rmdir(child_path) < 0) { |
| 729 | return -errno; |
| 730 | } |
| 731 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 732 | } |
| 733 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 734 | static int handle_rename(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 735 | const struct fuse_in_header* hdr, const struct fuse_rename_in* req, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 736 | const char* old_name, const char* new_name) |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 737 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 738 | struct node* old_parent_node; |
| 739 | struct node* new_parent_node; |
| 740 | struct node* child_node; |
| 741 | char old_parent_path[PATH_MAX]; |
| 742 | char new_parent_path[PATH_MAX]; |
| 743 | char old_child_path[PATH_MAX]; |
| 744 | char new_child_path[PATH_MAX]; |
| 745 | const char* new_actual_name; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 746 | int res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 747 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 748 | pthread_mutex_lock(&fuse->lock); |
| 749 | old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, |
| 750 | old_parent_path, sizeof(old_parent_path)); |
| 751 | new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir, |
| 752 | new_parent_path, sizeof(new_parent_path)); |
| 753 | TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token, |
| 754 | old_name, new_name, |
| 755 | hdr->nodeid, old_parent_node ? old_parent_node->name : "?", |
| 756 | req->newdir, new_parent_node ? new_parent_node->name : "?"); |
| 757 | if (!old_parent_node || !new_parent_node) { |
| 758 | res = -ENOENT; |
| 759 | goto lookup_error; |
| 760 | } |
| 761 | child_node = lookup_child_by_name_locked(old_parent_node, old_name); |
| 762 | if (!child_node || get_node_path_locked(child_node, |
| 763 | old_child_path, sizeof(old_child_path)) < 0) { |
| 764 | res = -ENOENT; |
| 765 | goto lookup_error; |
| 766 | } |
| 767 | acquire_node_locked(child_node); |
| 768 | pthread_mutex_unlock(&fuse->lock); |
| 769 | |
| 770 | /* Special case for renaming a file where destination is same path |
| 771 | * differing only by case. In this case we don't want to look for a case |
| 772 | * insensitive match. This allows commands like "mv foo FOO" to work as expected. |
| 773 | */ |
| 774 | int search = old_parent_node != new_parent_node |
| 775 | || strcasecmp(old_name, new_name); |
| 776 | if (!(new_actual_name = find_file_within(new_parent_path, new_name, |
| 777 | new_child_path, sizeof(new_child_path), search))) { |
| 778 | res = -ENOENT; |
| 779 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 780 | } |
| 781 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 782 | TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path); |
| 783 | res = rename(old_child_path, new_child_path); |
| 784 | if (res < 0) { |
| 785 | res = -errno; |
| 786 | goto io_error; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 787 | } |
| 788 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 789 | pthread_mutex_lock(&fuse->lock); |
| 790 | res = rename_node_locked(child_node, new_name, new_actual_name); |
| 791 | if (!res) { |
| 792 | remove_node_from_parent_locked(child_node); |
| 793 | add_node_to_parent_locked(child_node, new_parent_node); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 794 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 795 | goto done; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 796 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 797 | io_error: |
| 798 | pthread_mutex_lock(&fuse->lock); |
| 799 | done: |
| 800 | release_node_locked(child_node); |
| 801 | lookup_error: |
| 802 | pthread_mutex_unlock(&fuse->lock); |
| 803 | return res; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 804 | } |
| 805 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 806 | static int handle_open(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 807 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 808 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 809 | struct node* node; |
| 810 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 811 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 812 | struct handle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 813 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 814 | pthread_mutex_lock(&fuse->lock); |
| 815 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 816 | TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token, |
| 817 | req->flags, hdr->nodeid, node ? node->name : "?"); |
| 818 | pthread_mutex_unlock(&fuse->lock); |
| 819 | |
| 820 | if (!node) { |
| 821 | return -ENOENT; |
| 822 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 823 | h = malloc(sizeof(*h)); |
| 824 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 825 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 826 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 827 | TRACE("[%d] OPEN %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 828 | h->fd = open(path, req->flags); |
| 829 | if (h->fd < 0) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 830 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 831 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 832 | } |
| 833 | out.fh = ptr_to_id(h); |
| 834 | out.open_flags = 0; |
| 835 | out.padding = 0; |
| 836 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 837 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 838 | } |
| 839 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 840 | static int handle_read(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 841 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 842 | { |
| 843 | struct handle *h = id_to_ptr(req->fh); |
| 844 | __u64 unique = hdr->unique; |
| 845 | __u32 size = req->size; |
| 846 | __u64 offset = req->offset; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 847 | int res; |
| 848 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 849 | /* Don't access any other fields of hdr or req beyond this point, the read buffer |
| 850 | * overlaps the request buffer and will clobber data in the request. This |
| 851 | * saves us 128KB per request handler thread at the cost of this scary comment. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 852 | |
| 853 | TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token, |
| 854 | h, h->fd, size, offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 855 | if (size > sizeof(handler->read_buffer)) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 856 | return -EINVAL; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 857 | } |
| 858 | res = pread64(h->fd, handler->read_buffer, size, offset); |
| 859 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 860 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 861 | } |
| 862 | fuse_reply(fuse, unique, handler->read_buffer, res); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 863 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 864 | } |
| 865 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 866 | static int handle_write(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 867 | const struct fuse_in_header* hdr, const struct fuse_write_in* req, |
| 868 | const void* buffer) |
| 869 | { |
| 870 | struct fuse_write_out out; |
| 871 | struct handle *h = id_to_ptr(req->fh); |
| 872 | int res; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 873 | |
| 874 | TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token, |
| 875 | h, h->fd, req->size, req->offset); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 876 | res = pwrite64(h->fd, buffer, req->size, req->offset); |
| 877 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 878 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 879 | } |
| 880 | out.size = res; |
| 881 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 882 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 883 | } |
| 884 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 885 | static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 886 | const struct fuse_in_header* hdr) |
| 887 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 888 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 889 | struct statfs stat; |
| 890 | struct fuse_statfs_out out; |
| 891 | int res; |
| 892 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 893 | pthread_mutex_lock(&fuse->lock); |
| 894 | TRACE("[%d] STATFS\n", handler->token); |
| 895 | res = get_node_path_locked(&fuse->root, path, sizeof(path)); |
| 896 | pthread_mutex_unlock(&fuse->lock); |
| 897 | if (res < 0) { |
| 898 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 899 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 900 | if (statfs(fuse->root.name, &stat) < 0) { |
| 901 | return -errno; |
| 902 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 903 | memset(&out, 0, sizeof(out)); |
| 904 | out.st.blocks = stat.f_blocks; |
| 905 | out.st.bfree = stat.f_bfree; |
| 906 | out.st.bavail = stat.f_bavail; |
| 907 | out.st.files = stat.f_files; |
| 908 | out.st.ffree = stat.f_ffree; |
| 909 | out.st.bsize = stat.f_bsize; |
| 910 | out.st.namelen = stat.f_namelen; |
| 911 | out.st.frsize = stat.f_frsize; |
| 912 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 913 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 914 | } |
| 915 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 916 | static int handle_release(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 917 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 918 | { |
| 919 | struct handle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 920 | |
| 921 | TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 922 | close(h->fd); |
| 923 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 924 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 925 | } |
| 926 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 927 | static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 928 | const struct fuse_in_header* hdr, const struct fuse_fsync_in* req) |
| 929 | { |
| 930 | int is_data_sync = req->fsync_flags & 1; |
| 931 | struct handle *h = id_to_ptr(req->fh); |
| 932 | int res; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 933 | |
| 934 | TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token, |
| 935 | h, h->fd, is_data_sync); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 936 | res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd); |
| 937 | if (res < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 938 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 939 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 940 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 941 | } |
| 942 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 943 | static int handle_flush(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 944 | const struct fuse_in_header* hdr) |
| 945 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 946 | TRACE("[%d] FLUSH\n", handler->token); |
| 947 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 948 | } |
| 949 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 950 | static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 951 | const struct fuse_in_header* hdr, const struct fuse_open_in* req) |
| 952 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 953 | struct node* node; |
| 954 | char path[PATH_MAX]; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 955 | struct fuse_open_out out; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 956 | struct dirhandle *h; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 957 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 958 | pthread_mutex_lock(&fuse->lock); |
| 959 | node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path)); |
| 960 | TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token, |
| 961 | hdr->nodeid, node ? node->name : "?"); |
| 962 | pthread_mutex_unlock(&fuse->lock); |
| 963 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 964 | if (!node) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 965 | return -ENOENT; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 966 | } |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 967 | h = malloc(sizeof(*h)); |
| 968 | if (!h) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 969 | return -ENOMEM; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 970 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 971 | TRACE("[%d] OPENDIR %s\n", handler->token, path); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 972 | h->d = opendir(path); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 973 | if (!h->d) { |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 974 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 975 | return -errno; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 976 | } |
| 977 | out.fh = ptr_to_id(h); |
| 978 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 979 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 980 | } |
| 981 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 982 | static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 983 | const struct fuse_in_header* hdr, const struct fuse_read_in* req) |
| 984 | { |
| 985 | char buffer[8192]; |
| 986 | struct fuse_dirent *fde = (struct fuse_dirent*) buffer; |
| 987 | struct dirent *de; |
| 988 | struct dirhandle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 989 | |
| 990 | TRACE("[%d] READDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 991 | if (req->offset == 0) { |
| 992 | /* rewinddir() might have been called above us, so rewind here too */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 993 | TRACE("[%d] calling rewinddir()\n", handler->token); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 994 | rewinddir(h->d); |
| 995 | } |
| 996 | de = readdir(h->d); |
| 997 | if (!de) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 998 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 999 | } |
| 1000 | fde->ino = FUSE_UNKNOWN_INO; |
| 1001 | /* increment the offset so we can detect when rewinddir() seeks back to the beginning */ |
| 1002 | fde->off = req->offset + 1; |
| 1003 | fde->type = de->d_type; |
| 1004 | fde->namelen = strlen(de->d_name); |
| 1005 | memcpy(fde->name, de->d_name, fde->namelen + 1); |
| 1006 | fuse_reply(fuse, hdr->unique, fde, |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1007 | FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen)); |
| 1008 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1011 | static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1012 | const struct fuse_in_header* hdr, const struct fuse_release_in* req) |
| 1013 | { |
| 1014 | struct dirhandle *h = id_to_ptr(req->fh); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1015 | |
| 1016 | TRACE("[%d] RELEASEDIR %p\n", handler->token, h); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1017 | closedir(h->d); |
| 1018 | free(h); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1019 | return 0; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1020 | } |
| 1021 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1022 | static int handle_init(struct fuse* fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1023 | const struct fuse_in_header* hdr, const struct fuse_init_in* req) |
| 1024 | { |
| 1025 | struct fuse_init_out out; |
| 1026 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1027 | TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n", |
| 1028 | handler->token, req->major, req->minor, req->max_readahead, req->flags); |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1029 | out.major = FUSE_KERNEL_VERSION; |
| 1030 | out.minor = FUSE_KERNEL_MINOR_VERSION; |
| 1031 | out.max_readahead = req->max_readahead; |
| 1032 | out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES; |
| 1033 | out.max_background = 32; |
| 1034 | out.congestion_threshold = 32; |
| 1035 | out.max_write = MAX_WRITE; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1036 | fuse_reply(fuse, hdr->unique, &out, sizeof(out)); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1037 | return NO_STATUS; |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1038 | } |
| 1039 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1040 | static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler, |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1041 | const struct fuse_in_header *hdr, const void *data, size_t data_len) |
| 1042 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1043 | switch (hdr->opcode) { |
| 1044 | case FUSE_LOOKUP: { /* bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1045 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1046 | return handle_lookup(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1047 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1048 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1049 | case FUSE_FORGET: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1050 | const struct fuse_forget_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1051 | return handle_forget(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1052 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1053 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1054 | case FUSE_GETATTR: { /* getattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1055 | const struct fuse_getattr_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1056 | return handle_getattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1057 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1058 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1059 | case FUSE_SETATTR: { /* setattr_in -> attr_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1060 | const struct fuse_setattr_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1061 | return handle_setattr(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1062 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1063 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1064 | // case FUSE_READLINK: |
| 1065 | // case FUSE_SYMLINK: |
| 1066 | case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1067 | const struct fuse_mknod_in *req = data; |
| 1068 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1069 | return handle_mknod(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1070 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1071 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1072 | case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1073 | const struct fuse_mkdir_in *req = data; |
| 1074 | const char *name = ((const char*) data) + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1075 | return handle_mkdir(fuse, handler, hdr, req, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1076 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1077 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1078 | case FUSE_UNLINK: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1079 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1080 | return handle_unlink(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1081 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1082 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1083 | case FUSE_RMDIR: { /* bytez[] -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1084 | const char* name = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1085 | return handle_rmdir(fuse, handler, hdr, name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1086 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1087 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1088 | case FUSE_RENAME: { /* rename_in, oldname, newname -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1089 | const struct fuse_rename_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1090 | const char *old_name = ((const char*) data) + sizeof(*req); |
| 1091 | const char *new_name = old_name + strlen(old_name) + 1; |
| 1092 | return handle_rename(fuse, handler, hdr, req, old_name, new_name); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1093 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1094 | |
Jeff Brown | fc1e1a0 | 2012-05-25 17:24:17 -0700 | [diff] [blame] | 1095 | // case FUSE_LINK: |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1096 | case FUSE_OPEN: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1097 | const struct fuse_open_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1098 | return handle_open(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1099 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1100 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1101 | case FUSE_READ: { /* read_in -> byte[] */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1102 | const struct fuse_read_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1103 | return handle_read(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1104 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1105 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1106 | case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1107 | const struct fuse_write_in *req = data; |
| 1108 | const void* buffer = (const __u8*)data + sizeof(*req); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1109 | return handle_write(fuse, handler, hdr, req, buffer); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1110 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1111 | |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1112 | case FUSE_STATFS: { /* getattr_in -> attr_out */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1113 | return handle_statfs(fuse, handler, hdr); |
Mike Lockwood | 4553b08 | 2010-08-16 14:14:44 -0400 | [diff] [blame] | 1114 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1115 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1116 | case FUSE_RELEASE: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1117 | const struct fuse_release_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1118 | return handle_release(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1119 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1120 | |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1121 | case FUSE_FSYNC: { |
| 1122 | const struct fuse_fsync_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1123 | return handle_fsync(fuse, handler, hdr, req); |
Jeff Brown | 6fd921a | 2012-05-25 15:01:21 -0700 | [diff] [blame] | 1124 | } |
| 1125 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1126 | // case FUSE_SETXATTR: |
| 1127 | // case FUSE_GETXATTR: |
| 1128 | // case FUSE_LISTXATTR: |
| 1129 | // case FUSE_REMOVEXATTR: |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1130 | case FUSE_FLUSH: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1131 | return handle_flush(fuse, handler, hdr); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1132 | } |
| 1133 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1134 | case FUSE_OPENDIR: { /* open_in -> open_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1135 | const struct fuse_open_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1136 | return handle_opendir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1137 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1138 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1139 | case FUSE_READDIR: { |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1140 | const struct fuse_read_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1141 | return handle_readdir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1142 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1143 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1144 | case FUSE_RELEASEDIR: { /* release_in -> */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1145 | const struct fuse_release_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1146 | return handle_releasedir(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1147 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1148 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1149 | // case FUSE_FSYNCDIR: |
| 1150 | case FUSE_INIT: { /* init_in -> init_out */ |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1151 | const struct fuse_init_in *req = data; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1152 | return handle_init(fuse, handler, hdr, req); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1153 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1154 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1155 | default: { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1156 | TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n", |
| 1157 | handler->token, hdr->opcode, hdr->unique, hdr->nodeid); |
| 1158 | return -ENOSYS; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1159 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1160 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1161 | } |
| 1162 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1163 | static void handle_fuse_requests(struct fuse_handler* handler) |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1164 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1165 | struct fuse* fuse = handler->fuse; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1166 | for (;;) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1167 | ssize_t len = read(fuse->fd, |
| 1168 | handler->request_buffer, sizeof(handler->request_buffer)); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1169 | if (len < 0) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1170 | if (errno != EINTR) { |
| 1171 | ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno); |
| 1172 | } |
| 1173 | continue; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1174 | } |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1175 | |
| 1176 | if ((size_t)len < sizeof(struct fuse_in_header)) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1177 | ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len); |
| 1178 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1179 | } |
| 1180 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1181 | const struct fuse_in_header *hdr = (void*)handler->request_buffer; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1182 | if (hdr->len != (size_t)len) { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1183 | ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n", |
| 1184 | handler->token, (size_t)len, hdr->len); |
| 1185 | continue; |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1186 | } |
| 1187 | |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1188 | const void *data = handler->request_buffer + sizeof(struct fuse_in_header); |
Jeff Brown | 8471584 | 2012-05-25 14:07:47 -0700 | [diff] [blame] | 1189 | size_t data_len = len - sizeof(struct fuse_in_header); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1190 | __u64 unique = hdr->unique; |
| 1191 | int res = handle_fuse_request(fuse, handler, hdr, data, data_len); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1192 | |
| 1193 | /* We do not access the request again after this point because the underlying |
| 1194 | * buffer storage may have been reused while processing the request. */ |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1195 | |
| 1196 | if (res != NO_STATUS) { |
| 1197 | if (res) { |
| 1198 | TRACE("[%d] ERROR %d\n", handler->token, res); |
| 1199 | } |
| 1200 | fuse_status(fuse, unique, res); |
| 1201 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1202 | } |
| 1203 | } |
| 1204 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1205 | static void* start_handler(void* data) |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1206 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1207 | struct fuse_handler* handler = data; |
| 1208 | handle_fuse_requests(handler); |
| 1209 | return NULL; |
| 1210 | } |
| 1211 | |
| 1212 | static int ignite_fuse(struct fuse* fuse, int num_threads) |
| 1213 | { |
| 1214 | struct fuse_handler* handlers; |
| 1215 | int i; |
| 1216 | |
| 1217 | handlers = malloc(num_threads * sizeof(struct fuse_handler)); |
| 1218 | if (!handlers) { |
| 1219 | ERROR("cannot allocate storage for threads"); |
| 1220 | return -ENOMEM; |
| 1221 | } |
| 1222 | |
| 1223 | for (i = 0; i < num_threads; i++) { |
| 1224 | handlers[i].fuse = fuse; |
| 1225 | handlers[i].token = i; |
| 1226 | } |
| 1227 | |
| 1228 | for (i = 1; i < num_threads; i++) { |
| 1229 | pthread_t thread; |
| 1230 | int res = pthread_create(&thread, NULL, start_handler, &handlers[i]); |
| 1231 | if (res) { |
| 1232 | ERROR("failed to start thread #%d, error=%d", i, res); |
| 1233 | goto quit; |
| 1234 | } |
| 1235 | } |
| 1236 | handle_fuse_requests(&handlers[0]); |
| 1237 | ERROR("terminated prematurely"); |
| 1238 | |
| 1239 | /* don't bother killing all of the other threads or freeing anything, |
| 1240 | * should never get here anyhow */ |
| 1241 | quit: |
| 1242 | exit(1); |
Jeff Brown | 7729d24 | 2012-05-25 15:35:28 -0700 | [diff] [blame] | 1243 | } |
| 1244 | |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1245 | static int usage() |
| 1246 | { |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1247 | ERROR("usage: sdcard [-t<threads>] <path> <uid> <gid>\n" |
| 1248 | " -t<threads>: specify number of threads to use, default -t%d\n" |
| 1249 | "\n", DEFAULT_NUM_THREADS); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1250 | return 1; |
| 1251 | } |
| 1252 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1253 | static int run(const char* path, uid_t uid, gid_t gid, int num_threads) |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1254 | { |
| 1255 | int fd; |
| 1256 | char opts[256]; |
| 1257 | int res; |
| 1258 | struct fuse fuse; |
| 1259 | |
| 1260 | /* cleanup from previous instance, if necessary */ |
| 1261 | umount2(MOUNT_POINT, 2); |
| 1262 | |
| 1263 | fd = open("/dev/fuse", O_RDWR); |
| 1264 | if (fd < 0){ |
| 1265 | ERROR("cannot open fuse device (error %d)\n", errno); |
| 1266 | return -1; |
| 1267 | } |
| 1268 | |
| 1269 | snprintf(opts, sizeof(opts), |
| 1270 | "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d", |
| 1271 | fd, uid, gid); |
| 1272 | |
| 1273 | res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts); |
| 1274 | if (res < 0) { |
| 1275 | ERROR("cannot mount fuse filesystem (error %d)\n", errno); |
| 1276 | goto error; |
| 1277 | } |
| 1278 | |
| 1279 | res = setgid(gid); |
| 1280 | if (res < 0) { |
| 1281 | ERROR("cannot setgid (error %d)\n", errno); |
| 1282 | goto error; |
| 1283 | } |
| 1284 | |
| 1285 | res = setuid(uid); |
| 1286 | if (res < 0) { |
| 1287 | ERROR("cannot setuid (error %d)\n", errno); |
| 1288 | goto error; |
| 1289 | } |
| 1290 | |
| 1291 | fuse_init(&fuse, fd, path); |
| 1292 | |
| 1293 | umask(0); |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1294 | res = ignite_fuse(&fuse, num_threads); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1295 | |
| 1296 | /* we do not attempt to umount the file system here because we are no longer |
| 1297 | * running as the root user */ |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1298 | |
| 1299 | error: |
| 1300 | close(fd); |
| 1301 | return res; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1302 | } |
| 1303 | |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1304 | int main(int argc, char **argv) |
| 1305 | { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1306 | int res; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1307 | const char *path = NULL; |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1308 | uid_t uid = 0; |
| 1309 | gid_t gid = 0; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1310 | int num_threads = DEFAULT_NUM_THREADS; |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1311 | int i; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1312 | |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1313 | for (i = 1; i < argc; i++) { |
| 1314 | char* arg = argv[i]; |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1315 | if (!strncmp(arg, "-t", 2)) |
| 1316 | num_threads = strtoul(arg + 2, 0, 10); |
| 1317 | else if (!path) |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1318 | path = arg; |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1319 | else if (!uid) |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1320 | uid = strtoul(arg, 0, 10); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1321 | else if (!gid) |
Mike Lockwood | 575a2bb | 2011-01-23 14:46:30 -0800 | [diff] [blame] | 1322 | gid = strtoul(arg, 0, 10); |
| 1323 | else { |
| 1324 | ERROR("too many arguments\n"); |
| 1325 | return usage(); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1326 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1327 | } |
| 1328 | |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1329 | if (!path) { |
| 1330 | ERROR("no path specified\n"); |
| 1331 | return usage(); |
| 1332 | } |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1333 | if (!uid || !gid) { |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1334 | ERROR("uid and gid must be nonzero\n"); |
Mike Lockwood | 4f35e62 | 2011-01-12 14:39:44 -0500 | [diff] [blame] | 1335 | return usage(); |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1336 | } |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1337 | if (num_threads < 1) { |
| 1338 | ERROR("number of threads must be at least 1\n"); |
| 1339 | return usage(); |
| 1340 | } |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1341 | |
Jeff Brown | 6249b90 | 2012-05-26 14:32:54 -0700 | [diff] [blame] | 1342 | res = run(path, uid, gid, num_threads); |
Jeff Brown | 2656735 | 2012-05-25 13:27:43 -0700 | [diff] [blame] | 1343 | return res < 0 ? 1 : 0; |
Brian Swetland | 03ee947 | 2010-08-12 18:01:08 -0700 | [diff] [blame] | 1344 | } |