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