blob: bff6e67d569e326550c2944ead32f5b7d513ac92 [file] [log] [blame]
Brian Swetland03ee9472010-08-12 18:01:08 -07001/*
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 Lockwood4553b082010-08-16 14:14:44 -040025#include <sys/statfs.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070026#include <sys/uio.h>
27#include <dirent.h>
Jeff Brown7729d242012-05-25 15:35:28 -070028#include <limits.h>
Mike Lockwood1bedb732011-01-13 13:38:42 -050029#include <ctype.h>
Jeff Brown6249b902012-05-26 14:32:54 -070030#include <pthread.h>
Ken Sumrall2fd72cc2013-02-08 16:50:55 -080031#include <sys/time.h>
32#include <sys/resource.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070033
Brian Swetlandb14a2c62010-08-12 18:21:12 -070034#include <private/android_filesystem_config.h>
35
Brian Swetland03ee9472010-08-12 18:01:08 -070036#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 Sharkeye169bd02012-08-13 16:44:42 -070047 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070048 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070049 * 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 Swetland03ee9472010-08-12 18:01:08 -070051 *
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 Swetland03ee9472010-08-12 18:01:08 -070060 */
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 Brown84715842012-05-25 14:07:47 -070074/* 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 Brown6249b902012-05-26 14:32:54 -070085/* 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 Swetland03ee9472010-08-12 18:01:08 -070092struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -070093 int fd;
94};
95
96struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -070097 DIR *d;
98};
99
100struct node {
Jeff Brown6249b902012-05-26 14:32:54 -0700101 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700102 __u64 nid;
103 __u64 gen;
104
Paul Eastham11ccdb32010-10-14 11:04:26 -0700105 struct node *next; /* per-dir sibling list */
106 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700107 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700108
Jeff Brown6249b902012-05-26 14:32:54 -0700109 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700110 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800111 /* 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 Swetland03ee9472010-08-12 18:01:08 -0700117};
118
Jeff Brown7729d242012-05-25 15:35:28 -0700119/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700120struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700121 pthread_mutex_t lock;
122
Brian Swetland03ee9472010-08-12 18:01:08 -0700123 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700124 int fd;
Brian Swetland03ee9472010-08-12 18:01:08 -0700125 struct node root;
Jeff Brown7729d242012-05-25 15:35:28 -0700126 char rootpath[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700127};
128
Jeff Brown7729d242012-05-25 15:35:28 -0700129/* Private data used by a single fuse handler. */
130struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700131 struct fuse* fuse;
132 int token;
133
Jeff Brown7729d242012-05-25 15:35:28 -0700134 /* 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 Swetland03ee9472010-08-12 18:01:08 -0700141
Jeff Brown6249b902012-05-26 14:32:54 -0700142static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700143{
Jeff Brown6249b902012-05-26 14:32:54 -0700144 return (void *) (uintptr_t) nid;
145}
Brian Swetland03ee9472010-08-12 18:01:08 -0700146
Jeff Brown6249b902012-05-26 14:32:54 -0700147static inline __u64 ptr_to_id(void *ptr)
148{
149 return (__u64) (uintptr_t) ptr;
150}
Brian Swetland03ee9472010-08-12 18:01:08 -0700151
Jeff Brown6249b902012-05-26 14:32:54 -0700152static 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
158static void remove_node_from_parent_locked(struct node* node);
159
160static 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 Lockwood575a2bb2011-01-23 14:46:30 -0800175 }
Jeff Brown6249b902012-05-26 14:32:54 -0700176 } else {
177 ERROR("Zero refcnt %p\n", node);
178 }
179}
180
181static 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
188static 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 */
211static 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 Swetland03ee9472010-08-12 18:01:08 -0700216 }
217
Jeff Brown6249b902012-05-26 14:32:54 -0700218 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 */
240static 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 Lockwood575a2bb2011-01-23 14:46:30 -0800258 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700259 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800260 if (!dir) {
261 ERROR("opendir %s failed: %s", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700262 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800263 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800264 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700265 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 Lockwood575a2bb2011-01-23 14:46:30 -0800268 break;
269 }
270 }
271 closedir(dir);
272 }
Jeff Brown6249b902012-05-26 14:32:54 -0700273 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800274}
275
Jeff Brown6249b902012-05-26 14:32:54 -0700276static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, __u64 nid)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800277{
Jeff Brown6249b902012-05-26 14:32:54 -0700278 attr->ino = nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700279 attr->size = s->st_size;
280 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400281 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 Swetland03ee9472010-08-12 18:01:08 -0700287 attr->mode = s->st_mode;
288 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700289
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700290 /* 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 Swetland03ee9472010-08-12 18:01:08 -0700303}
304
Jeff Brown6249b902012-05-26 14:32:54 -0700305struct node *create_node_locked(struct fuse* fuse,
306 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700307{
308 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700309 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700310
Paul Eastham11ccdb32010-10-14 11:04:26 -0700311 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700312 if (!node) {
313 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700314 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700315 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700316 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700317 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700318 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700319 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700320 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700321 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 Swetland03ee9472010-08-12 18:01:08 -0700330 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700331 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 Swetland03ee9472010-08-12 18:01:08 -0700335 return node;
336}
337
Jeff Brown6249b902012-05-26 14:32:54 -0700338static int rename_node_locked(struct node *node, const char *name,
339 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700340{
Jeff Brown6249b902012-05-26 14:32:54 -0700341 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 Eastham11ccdb32010-10-14 11:04:26 -0700377}
378
Jeff Brown6249b902012-05-26 14:32:54 -0700379static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700380{
Jeff Brown6249b902012-05-26 14:32:54 -0700381 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700382 return &fuse->root;
383 } else {
384 return id_to_ptr(nid);
385 }
386}
387
Jeff Brown6249b902012-05-26 14:32:54 -0700388static 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
398static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700399{
400 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700401 /* 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 Swetland03ee9472010-08-12 18:01:08 -0700405 if (!strcmp(name, node->name)) {
406 return node;
407 }
408 }
409 return 0;
410}
411
Jeff Brown6249b902012-05-26 14:32:54 -0700412static struct node* acquire_or_create_child_locked(
413 struct fuse* fuse, struct node* parent,
414 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700415{
Jeff Brown6249b902012-05-26 14:32:54 -0700416 struct node* child = lookup_child_by_name_locked(parent, name);
417 if (child) {
418 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800419 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700420 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800421 }
Jeff Brown6249b902012-05-26 14:32:54 -0700422 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700423}
424
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700425static void fuse_init(struct fuse *fuse, int fd, const char *source_path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700426{
Jeff Brown6249b902012-05-26 14:32:54 -0700427 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700428
Jeff Brown6249b902012-05-26 14:32:54 -0700429 fuse->fd = fd;
430 fuse->next_generation = 0;
Brian Swetland03ee9472010-08-12 18:01:08 -0700431
Jeff Brown6249b902012-05-26 14:32:54 -0700432 memset(&fuse->root, 0, sizeof(fuse->root));
433 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
434 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700435 fuse->root.namelen = strlen(source_path);
436 fuse->root.name = strdup(source_path);
Brian Swetland03ee9472010-08-12 18:01:08 -0700437}
438
Jeff Brown6249b902012-05-26 14:32:54 -0700439static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700440{
441 struct fuse_out_header hdr;
442 hdr.len = sizeof(hdr);
443 hdr.error = err;
444 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700445 write(fuse->fd, &hdr, sizeof(hdr));
446}
447
Jeff Brown6249b902012-05-26 14:32:54 -0700448static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700449{
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 Brown6249b902012-05-26 14:32:54 -0700469static 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 Swetland03ee9472010-08-12 18:01:08 -0700472{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700473 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700474 struct fuse_entry_out out;
475 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700476
Jeff Brown6249b902012-05-26 14:32:54 -0700477 if (lstat(path, &s) < 0) {
478 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700479 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700480
Jeff Brown6249b902012-05-26 14:32:54 -0700481 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 Swetland03ee9472010-08-12 18:01:08 -0700491 out.nodeid = node->nid;
492 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700493 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700494 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700495 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700496}
497
Jeff Brown6249b902012-05-26 14:32:54 -0700498static 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
514static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700515 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700516{
Jeff Brown6249b902012-05-26 14:32:54 -0700517 struct node* parent_node;
518 char parent_path[PATH_MAX];
519 char child_path[PATH_MAX];
520 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700521
Jeff Brown6249b902012-05-26 14:32:54 -0700522 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 Swetland03ee9472010-08-12 18:01:08 -0700532 }
Jeff Brown6249b902012-05-26 14:32:54 -0700533 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700534}
535
Jeff Brown6249b902012-05-26 14:32:54 -0700536static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700537 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
538{
Jeff Brown6249b902012-05-26 14:32:54 -0700539 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700540
Jeff Brown6249b902012-05-26 14:32:54 -0700541 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 Brownfc1e1a02012-05-25 17:24:17 -0700550 }
Jeff Brown6249b902012-05-26 14:32:54 -0700551 pthread_mutex_unlock(&fuse->lock);
552 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700553}
554
Jeff Brown6249b902012-05-26 14:32:54 -0700555static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700556 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
557{
Jeff Brown6249b902012-05-26 14:32:54 -0700558 struct node* node;
559 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700560
Jeff Brown6249b902012-05-26 14:32:54 -0700561 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 Brownfc1e1a02012-05-25 17:24:17 -0700567 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700568 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700569 }
Jeff Brown6249b902012-05-26 14:32:54 -0700570 return fuse_reply_attr(fuse, hdr->unique, hdr->nodeid, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700571}
572
Jeff Brown6249b902012-05-26 14:32:54 -0700573static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700574 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
575{
Jeff Brown6249b902012-05-26 14:32:54 -0700576 struct node* node;
577 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700578 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700579
Jeff Brown6249b902012-05-26 14:32:54 -0700580 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 Brownfc1e1a02012-05-25 17:24:17 -0700586 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700587 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700588 }
589
Jeff Brown6249b902012-05-26 14:32:54 -0700590 /* XXX: incomplete implementation on purpose.
591 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700592
Jeff Brown6249b902012-05-26 14:32:54 -0700593 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
594 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700595 }
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 Brown6249b902012-05-26 14:32:54 -0700622 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 Brownfc1e1a02012-05-25 17:24:17 -0700626 }
627 }
Jeff Brown6249b902012-05-26 14:32:54 -0700628 return fuse_reply_attr(fuse, hdr->unique, hdr->nodeid, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700629}
630
Jeff Brown6249b902012-05-26 14:32:54 -0700631static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700632 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
633{
Jeff Brown6249b902012-05-26 14:32:54 -0700634 struct node* parent_node;
635 char parent_path[PATH_MAX];
636 char child_path[PATH_MAX];
637 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700638
Jeff Brown6249b902012-05-26 14:32:54 -0700639 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 Brownfc1e1a02012-05-25 17:24:17 -0700649 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700650 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700651 if (mknod(child_path, mode, req->rdev) < 0) {
652 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700653 }
Jeff Brown6249b902012-05-26 14:32:54 -0700654 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700655}
656
Jeff Brown6249b902012-05-26 14:32:54 -0700657static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700658 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
659{
Jeff Brown6249b902012-05-26 14:32:54 -0700660 struct node* parent_node;
661 char parent_path[PATH_MAX];
662 char child_path[PATH_MAX];
663 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700664
Jeff Brown6249b902012-05-26 14:32:54 -0700665 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 Brownfc1e1a02012-05-25 17:24:17 -0700675 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700676 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700677 if (mkdir(child_path, mode) < 0) {
678 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700679 }
Jeff Brown6249b902012-05-26 14:32:54 -0700680 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700681}
682
Jeff Brown6249b902012-05-26 14:32:54 -0700683static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700684 const struct fuse_in_header* hdr, const char* name)
685{
Jeff Brown6249b902012-05-26 14:32:54 -0700686 struct node* parent_node;
687 char parent_path[PATH_MAX];
688 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700689
Jeff Brown6249b902012-05-26 14:32:54 -0700690 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 Brownfc1e1a02012-05-25 17:24:17 -0700700 }
Jeff Brown6249b902012-05-26 14:32:54 -0700701 if (unlink(child_path) < 0) {
702 return -errno;
703 }
704 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700705}
706
Jeff Brown6249b902012-05-26 14:32:54 -0700707static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700708 const struct fuse_in_header* hdr, const char* name)
709{
Jeff Brown6249b902012-05-26 14:32:54 -0700710 struct node* parent_node;
711 char parent_path[PATH_MAX];
712 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700713
Jeff Brown6249b902012-05-26 14:32:54 -0700714 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 Brownfc1e1a02012-05-25 17:24:17 -0700724 }
Jeff Brown6249b902012-05-26 14:32:54 -0700725 if (rmdir(child_path) < 0) {
726 return -errno;
727 }
728 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700729}
730
Jeff Brown6249b902012-05-26 14:32:54 -0700731static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700732 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700733 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700734{
Jeff Brown6249b902012-05-26 14:32:54 -0700735 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 Brownfc1e1a02012-05-25 17:24:17 -0700743 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700744
Jeff Brown6249b902012-05-26 14:32:54 -0700745 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 Brownfc1e1a02012-05-25 17:24:17 -0700777 }
778
Jeff Brown6249b902012-05-26 14:32:54 -0700779 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 Brownfc1e1a02012-05-25 17:24:17 -0700784 }
785
Jeff Brown6249b902012-05-26 14:32:54 -0700786 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 Brownfc1e1a02012-05-25 17:24:17 -0700791 }
Jeff Brown6249b902012-05-26 14:32:54 -0700792 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700793
Jeff Brown6249b902012-05-26 14:32:54 -0700794io_error:
795 pthread_mutex_lock(&fuse->lock);
796done:
797 release_node_locked(child_node);
798lookup_error:
799 pthread_mutex_unlock(&fuse->lock);
800 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700801}
802
Jeff Brown6249b902012-05-26 14:32:54 -0700803static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700804 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
805{
Jeff Brown6249b902012-05-26 14:32:54 -0700806 struct node* node;
807 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700808 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700809 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700810
Jeff Brown6249b902012-05-26 14:32:54 -0700811 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 Brownfc1e1a02012-05-25 17:24:17 -0700820 h = malloc(sizeof(*h));
821 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -0700822 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700823 }
Jeff Brown6249b902012-05-26 14:32:54 -0700824 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700825 h->fd = open(path, req->flags);
826 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700827 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700828 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700829 }
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 Brown6249b902012-05-26 14:32:54 -0700834 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700835}
836
Jeff Brown6249b902012-05-26 14:32:54 -0700837static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700838 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 Brown6249b902012-05-26 14:32:54 -0700844 int res;
845
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700846 /* 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 Brown6249b902012-05-26 14:32:54 -0700849
850 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
851 h, h->fd, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700852 if (size > sizeof(handler->read_buffer)) {
Jeff Brown6249b902012-05-26 14:32:54 -0700853 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700854 }
855 res = pread64(h->fd, handler->read_buffer, size, offset);
856 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700857 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700858 }
859 fuse_reply(fuse, unique, handler->read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -0700860 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700861}
862
Jeff Brown6249b902012-05-26 14:32:54 -0700863static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700864 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 Brown6249b902012-05-26 14:32:54 -0700870
871 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
872 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700873 res = pwrite64(h->fd, buffer, req->size, req->offset);
874 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700875 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700876 }
877 out.size = res;
878 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700879 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700880}
881
Jeff Brown6249b902012-05-26 14:32:54 -0700882static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700883 const struct fuse_in_header* hdr)
884{
Jeff Brown6249b902012-05-26 14:32:54 -0700885 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700886 struct statfs stat;
887 struct fuse_statfs_out out;
888 int res;
889
Jeff Brown6249b902012-05-26 14:32:54 -0700890 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 Brownfc1e1a02012-05-25 17:24:17 -0700896 }
Jeff Brown6249b902012-05-26 14:32:54 -0700897 if (statfs(fuse->root.name, &stat) < 0) {
898 return -errno;
899 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700900 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 Brown6249b902012-05-26 14:32:54 -0700910 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700911}
912
Jeff Brown6249b902012-05-26 14:32:54 -0700913static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700914 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
915{
916 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -0700917
918 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700919 close(h->fd);
920 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700921 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700922}
923
Jeff Brown6249b902012-05-26 14:32:54 -0700924static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700925 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 Brown6249b902012-05-26 14:32:54 -0700930
931 TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
932 h, h->fd, is_data_sync);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700933 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
934 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700935 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700936 }
Jeff Brown6249b902012-05-26 14:32:54 -0700937 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700938}
939
Jeff Brown6249b902012-05-26 14:32:54 -0700940static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700941 const struct fuse_in_header* hdr)
942{
Jeff Brown6249b902012-05-26 14:32:54 -0700943 TRACE("[%d] FLUSH\n", handler->token);
944 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700945}
946
Jeff Brown6249b902012-05-26 14:32:54 -0700947static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700948 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
949{
Jeff Brown6249b902012-05-26 14:32:54 -0700950 struct node* node;
951 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700953 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700954
Jeff Brown6249b902012-05-26 14:32:54 -0700955 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 Brownfc1e1a02012-05-25 17:24:17 -0700961 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700962 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700963 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700964 h = malloc(sizeof(*h));
965 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -0700966 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967 }
Jeff Brown6249b902012-05-26 14:32:54 -0700968 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -0700970 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700971 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700972 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700973 }
974 out.fh = ptr_to_id(h);
975 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700976 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700977}
978
Jeff Brown6249b902012-05-26 14:32:54 -0700979static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700980 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 Brown6249b902012-05-26 14:32:54 -0700986
987 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700988 if (req->offset == 0) {
989 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -0700990 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700991 rewinddir(h->d);
992 }
993 de = readdir(h->d);
994 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -0700995 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700996 }
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 Brown6249b902012-05-26 14:32:54 -07001004 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1005 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001006}
1007
Jeff Brown6249b902012-05-26 14:32:54 -07001008static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001009 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1010{
1011 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001012
1013 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001014 closedir(h->d);
1015 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001016 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001017}
1018
Jeff Brown6249b902012-05-26 14:32:54 -07001019static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001020 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1021{
1022 struct fuse_init_out out;
1023
Jeff Brown6249b902012-05-26 14:32:54 -07001024 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1025 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001026 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 Brownfc1e1a02012-05-25 17:24:17 -07001033 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001034 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001035}
1036
Jeff Brown6249b902012-05-26 14:32:54 -07001037static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001038 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1039{
Brian Swetland03ee9472010-08-12 18:01:08 -07001040 switch (hdr->opcode) {
1041 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001042 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001043 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001044 }
Jeff Brown84715842012-05-25 14:07:47 -07001045
Brian Swetland03ee9472010-08-12 18:01:08 -07001046 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001047 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001048 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001049 }
Jeff Brown84715842012-05-25 14:07:47 -07001050
Brian Swetland03ee9472010-08-12 18:01:08 -07001051 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001052 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001053 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001054 }
Jeff Brown84715842012-05-25 14:07:47 -07001055
Brian Swetland03ee9472010-08-12 18:01:08 -07001056 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001057 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001058 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001059 }
Jeff Brown84715842012-05-25 14:07:47 -07001060
Brian Swetland03ee9472010-08-12 18:01:08 -07001061// case FUSE_READLINK:
1062// case FUSE_SYMLINK:
1063 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001064 const struct fuse_mknod_in *req = data;
1065 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001066 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001067 }
Jeff Brown84715842012-05-25 14:07:47 -07001068
Brian Swetland03ee9472010-08-12 18:01:08 -07001069 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001070 const struct fuse_mkdir_in *req = data;
1071 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001072 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001073 }
Jeff Brown84715842012-05-25 14:07:47 -07001074
Brian Swetland03ee9472010-08-12 18:01:08 -07001075 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001076 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001077 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001078 }
Jeff Brown84715842012-05-25 14:07:47 -07001079
Brian Swetland03ee9472010-08-12 18:01:08 -07001080 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001081 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001082 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001083 }
Jeff Brown84715842012-05-25 14:07:47 -07001084
Brian Swetland03ee9472010-08-12 18:01:08 -07001085 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001086 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001087 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 Swetland03ee9472010-08-12 18:01:08 -07001090 }
Jeff Brown84715842012-05-25 14:07:47 -07001091
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001092// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001093 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001094 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001095 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001096 }
Jeff Brown84715842012-05-25 14:07:47 -07001097
Brian Swetland03ee9472010-08-12 18:01:08 -07001098 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001099 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001100 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001101 }
Jeff Brown84715842012-05-25 14:07:47 -07001102
Brian Swetland03ee9472010-08-12 18:01:08 -07001103 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001104 const struct fuse_write_in *req = data;
1105 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001106 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001107 }
Jeff Brown84715842012-05-25 14:07:47 -07001108
Mike Lockwood4553b082010-08-16 14:14:44 -04001109 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001110 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001111 }
Jeff Brown84715842012-05-25 14:07:47 -07001112
Brian Swetland03ee9472010-08-12 18:01:08 -07001113 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001114 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001115 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001116 }
Jeff Brown84715842012-05-25 14:07:47 -07001117
Jeff Brown6fd921a2012-05-25 15:01:21 -07001118 case FUSE_FSYNC: {
1119 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001120 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001121 }
1122
Brian Swetland03ee9472010-08-12 18:01:08 -07001123// case FUSE_SETXATTR:
1124// case FUSE_GETXATTR:
1125// case FUSE_LISTXATTR:
1126// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001127 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001128 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001129 }
1130
Brian Swetland03ee9472010-08-12 18:01:08 -07001131 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001132 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001133 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001134 }
Jeff Brown84715842012-05-25 14:07:47 -07001135
Brian Swetland03ee9472010-08-12 18:01:08 -07001136 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001137 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001138 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001139 }
Jeff Brown84715842012-05-25 14:07:47 -07001140
Brian Swetland03ee9472010-08-12 18:01:08 -07001141 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001142 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001143 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001144 }
Jeff Brown84715842012-05-25 14:07:47 -07001145
Brian Swetland03ee9472010-08-12 18:01:08 -07001146// case FUSE_FSYNCDIR:
1147 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001148 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001149 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001150 }
Jeff Brown84715842012-05-25 14:07:47 -07001151
Brian Swetland03ee9472010-08-12 18:01:08 -07001152 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001153 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1154 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1155 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001156 }
Jeff Brown84715842012-05-25 14:07:47 -07001157 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001158}
1159
Jeff Brown6249b902012-05-26 14:32:54 -07001160static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001161{
Jeff Brown6249b902012-05-26 14:32:54 -07001162 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001163 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001164 ssize_t len = read(fuse->fd,
1165 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001166 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001167 if (errno != EINTR) {
1168 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1169 }
1170 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001171 }
Jeff Brown84715842012-05-25 14:07:47 -07001172
1173 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001174 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1175 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001176 }
1177
Jeff Brown7729d242012-05-25 15:35:28 -07001178 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001179 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001180 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1181 handler->token, (size_t)len, hdr->len);
1182 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001183 }
1184
Jeff Brown7729d242012-05-25 15:35:28 -07001185 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001186 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001187 __u64 unique = hdr->unique;
1188 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001189
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 Brown6249b902012-05-26 14:32:54 -07001192
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 Swetland03ee9472010-08-12 18:01:08 -07001199 }
1200}
1201
Jeff Brown6249b902012-05-26 14:32:54 -07001202static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001203{
Jeff Brown6249b902012-05-26 14:32:54 -07001204 struct fuse_handler* handler = data;
1205 handle_fuse_requests(handler);
1206 return NULL;
1207}
1208
1209static 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 */
1238quit:
1239 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001240}
1241
Mike Lockwood4f35e622011-01-12 14:39:44 -05001242static int usage()
1243{
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001244 ERROR("usage: sdcard [-t<threads>] <source_path> <dest_path> <uid> <gid>\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001245 " -t<threads>: specify number of threads to use, default -t%d\n"
1246 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001247 return 1;
1248}
1249
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001250static int run(const char* source_path, const char* dest_path, uid_t uid, gid_t gid,
1251 int num_threads) {
Jeff Brown26567352012-05-25 13:27:43 -07001252 int fd;
1253 char opts[256];
1254 int res;
1255 struct fuse fuse;
1256
1257 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001258 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001259
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 Sharkeye169bd02012-08-13 16:44:42 -07001270 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001271 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 Sharkeye169bd02012-08-13 16:44:42 -07001288 fuse_init(&fuse, fd, source_path);
Jeff Brown26567352012-05-25 13:27:43 -07001289
1290 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001291 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001292
1293 /* we do not attempt to umount the file system here because we are no longer
1294 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001295
1296error:
1297 close(fd);
1298 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001299}
1300
Brian Swetland03ee9472010-08-12 18:01:08 -07001301int main(int argc, char **argv)
1302{
Brian Swetland03ee9472010-08-12 18:01:08 -07001303 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001304 const char *source_path = NULL;
1305 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001306 uid_t uid = 0;
1307 gid_t gid = 0;
Jeff Brown6249b902012-05-26 14:32:54 -07001308 int num_threads = DEFAULT_NUM_THREADS;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001309 int i;
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001310 struct rlimit rlim;
Brian Swetland03ee9472010-08-12 18:01:08 -07001311
Mike Lockwood4f35e622011-01-12 14:39:44 -05001312 for (i = 1; i < argc; i++) {
1313 char* arg = argv[i];
Jeff Brown6249b902012-05-26 14:32:54 -07001314 if (!strncmp(arg, "-t", 2))
1315 num_threads = strtoul(arg + 2, 0, 10);
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001316 else if (!source_path)
1317 source_path = arg;
1318 else if (!dest_path)
1319 dest_path = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001320 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 Lockwood575a2bb2011-01-23 14:46:30 -08001337 ERROR("too many arguments\n");
1338 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001339 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001340 }
1341
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001342 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 Lockwood4f35e622011-01-12 14:39:44 -05001348 return usage();
1349 }
Jeff Brown26567352012-05-25 13:27:43 -07001350 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001351 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001352 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001353 }
Jeff Brown6249b902012-05-26 14:32:54 -07001354 if (num_threads < 1) {
1355 ERROR("number of threads must be at least 1\n");
1356 return usage();
1357 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001358
Ken Sumrall2fd72cc2013-02-08 16:50:55 -08001359 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 Sharkeye169bd02012-08-13 16:44:42 -07001365 res = run(source_path, dest_path, uid, gid, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001366 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001367}