blob: 8d87ee92d84558ba86b3ba0841ddcdbe49b2be84 [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>
Brian Swetland03ee9472010-08-12 18:01:08 -070031
Brian Swetlandb14a2c62010-08-12 18:21:12 -070032#include <private/android_filesystem_config.h>
33
Brian Swetland03ee9472010-08-12 18:01:08 -070034#include "fuse.h"
35
36/* README
37 *
38 * What is this?
39 *
40 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
41 * directory permissions (all files are given fixed owner, group, and
42 * permissions at creation, owner, group, and permissions are not
43 * changeable, symlinks and hardlinks are not createable, etc.
44 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070045 * See usage() for command line options.
Brian Swetland03ee9472010-08-12 18:01:08 -070046 *
Jeff Sharkeye169bd02012-08-13 16:44:42 -070047 * It must be run as root, but will drop to requested UID/GID as soon as it
48 * mounts a filesystem. It will refuse to run if requested UID/GID are zero.
Brian Swetland03ee9472010-08-12 18:01:08 -070049 *
50 * Things I believe to be true:
51 *
52 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
53 * CREAT) must bump that node's refcount
54 * - don't forget that FORGET can forget multiple references (req->nlookup)
55 * - if an op that returns a fuse_entry fails writing the reply to the
56 * kernel, you must rollback the refcount to reflect the reference the
57 * kernel did not actually acquire
Brian Swetland03ee9472010-08-12 18:01:08 -070058 */
59
60#define FUSE_TRACE 0
61
62#if FUSE_TRACE
63#define TRACE(x...) fprintf(stderr,x)
64#else
65#define TRACE(x...) do {} while (0)
66#endif
67
68#define ERROR(x...) fprintf(stderr,x)
69
70#define FUSE_UNKNOWN_INO 0xffffffff
71
Jeff Brown84715842012-05-25 14:07:47 -070072/* Maximum number of bytes to write in one request. */
73#define MAX_WRITE (256 * 1024)
74
75/* Maximum number of bytes to read in one request. */
76#define MAX_READ (128 * 1024)
77
78/* Largest possible request.
79 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
80 * the largest possible data payload. */
81#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
82
Jeff Brown6249b902012-05-26 14:32:54 -070083/* Default number of threads. */
84#define DEFAULT_NUM_THREADS 2
85
86/* Pseudo-error constant used to indicate that no fuse status is needed
87 * or that a reply has already been written. */
88#define NO_STATUS 1
89
Brian Swetland03ee9472010-08-12 18:01:08 -070090struct handle {
Brian Swetland03ee9472010-08-12 18:01:08 -070091 int fd;
92};
93
94struct dirhandle {
Brian Swetland03ee9472010-08-12 18:01:08 -070095 DIR *d;
96};
97
98struct node {
Jeff Brown6249b902012-05-26 14:32:54 -070099 __u32 refcount;
Brian Swetland03ee9472010-08-12 18:01:08 -0700100 __u64 nid;
101 __u64 gen;
102
Paul Eastham11ccdb32010-10-14 11:04:26 -0700103 struct node *next; /* per-dir sibling list */
104 struct node *child; /* first contained file by this dir */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700105 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700106
Jeff Brown6249b902012-05-26 14:32:54 -0700107 size_t namelen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700108 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800109 /* If non-null, this is the real name of the file in the underlying storage.
110 * This may differ from the field "name" only by case.
111 * strlen(actual_name) will always equal strlen(name), so it is safe to use
112 * namelen for both fields.
113 */
114 char *actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700115};
116
Jeff Brown7729d242012-05-25 15:35:28 -0700117/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700118struct fuse {
Jeff Brown6249b902012-05-26 14:32:54 -0700119 pthread_mutex_t lock;
120
Brian Swetland03ee9472010-08-12 18:01:08 -0700121 __u64 next_generation;
Brian Swetland03ee9472010-08-12 18:01:08 -0700122 int fd;
Brian Swetland03ee9472010-08-12 18:01:08 -0700123 struct node root;
Jeff Brown7729d242012-05-25 15:35:28 -0700124 char rootpath[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700125};
126
Jeff Brown7729d242012-05-25 15:35:28 -0700127/* Private data used by a single fuse handler. */
128struct fuse_handler {
Jeff Brown6249b902012-05-26 14:32:54 -0700129 struct fuse* fuse;
130 int token;
131
Jeff Brown7729d242012-05-25 15:35:28 -0700132 /* To save memory, we never use the contents of the request buffer and the read
133 * buffer at the same time. This allows us to share the underlying storage. */
134 union {
135 __u8 request_buffer[MAX_REQUEST_SIZE];
136 __u8 read_buffer[MAX_READ];
137 };
138};
Brian Swetland03ee9472010-08-12 18:01:08 -0700139
Jeff Brown6249b902012-05-26 14:32:54 -0700140static inline void *id_to_ptr(__u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700141{
Jeff Brown6249b902012-05-26 14:32:54 -0700142 return (void *) (uintptr_t) nid;
143}
Brian Swetland03ee9472010-08-12 18:01:08 -0700144
Jeff Brown6249b902012-05-26 14:32:54 -0700145static inline __u64 ptr_to_id(void *ptr)
146{
147 return (__u64) (uintptr_t) ptr;
148}
Brian Swetland03ee9472010-08-12 18:01:08 -0700149
Jeff Brown6249b902012-05-26 14:32:54 -0700150static void acquire_node_locked(struct node* node)
151{
152 node->refcount++;
153 TRACE("ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
154}
155
156static void remove_node_from_parent_locked(struct node* node);
157
158static void release_node_locked(struct node* node)
159{
160 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
161 if (node->refcount > 0) {
162 node->refcount--;
163 if (!node->refcount) {
164 TRACE("DESTROY %p (%s)\n", node, node->name);
165 remove_node_from_parent_locked(node);
166
167 /* TODO: remove debugging - poison memory */
168 memset(node->name, 0xef, node->namelen);
169 free(node->name);
170 free(node->actual_name);
171 memset(node, 0xfc, sizeof(*node));
172 free(node);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800173 }
Jeff Brown6249b902012-05-26 14:32:54 -0700174 } else {
175 ERROR("Zero refcnt %p\n", node);
176 }
177}
178
179static void add_node_to_parent_locked(struct node *node, struct node *parent) {
180 node->parent = parent;
181 node->next = parent->child;
182 parent->child = node;
183 acquire_node_locked(parent);
184}
185
186static void remove_node_from_parent_locked(struct node* node)
187{
188 if (node->parent) {
189 if (node->parent->child == node) {
190 node->parent->child = node->parent->child->next;
191 } else {
192 struct node *node2;
193 node2 = node->parent->child;
194 while (node2->next != node)
195 node2 = node2->next;
196 node2->next = node->next;
197 }
198 release_node_locked(node->parent);
199 node->parent = NULL;
200 node->next = NULL;
201 }
202}
203
204/* Gets the absolute path to a node into the provided buffer.
205 *
206 * Populates 'buf' with the path and returns the length of the path on success,
207 * or returns -1 if the path is too long for the provided buffer.
208 */
209static ssize_t get_node_path_locked(struct node* node, char* buf, size_t bufsize)
210{
211 size_t namelen = node->namelen;
212 if (bufsize < namelen + 1) {
213 return -1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700214 }
215
Jeff Brown6249b902012-05-26 14:32:54 -0700216 ssize_t pathlen = 0;
217 if (node->parent) {
218 pathlen = get_node_path_locked(node->parent, buf, bufsize - namelen - 2);
219 if (pathlen < 0) {
220 return -1;
221 }
222 buf[pathlen++] = '/';
223 }
224
225 const char* name = node->actual_name ? node->actual_name : node->name;
226 memcpy(buf + pathlen, name, namelen + 1); /* include trailing \0 */
227 return pathlen + namelen;
228}
229
230/* Finds the absolute path of a file within a given directory.
231 * Performs a case-insensitive search for the file and sets the buffer to the path
232 * of the first matching file. If 'search' is zero or if no match is found, sets
233 * the buffer to the path that the file would have, assuming the name were case-sensitive.
234 *
235 * Populates 'buf' with the path and returns the actual name (within 'buf') on success,
236 * or returns NULL if the path is too long for the provided buffer.
237 */
238static char* find_file_within(const char* path, const char* name,
239 char* buf, size_t bufsize, int search)
240{
241 size_t pathlen = strlen(path);
242 size_t namelen = strlen(name);
243 size_t childlen = pathlen + namelen + 1;
244 char* actual;
245
246 if (bufsize <= childlen) {
247 return NULL;
248 }
249
250 memcpy(buf, path, pathlen);
251 buf[pathlen] = '/';
252 actual = buf + pathlen + 1;
253 memcpy(actual, name, namelen + 1);
254
255 if (search && access(buf, F_OK)) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800256 struct dirent* entry;
Jeff Brown6249b902012-05-26 14:32:54 -0700257 DIR* dir = opendir(path);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800258 if (!dir) {
259 ERROR("opendir %s failed: %s", path, strerror(errno));
Jeff Brown6249b902012-05-26 14:32:54 -0700260 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800261 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800262 while ((entry = readdir(dir))) {
Jeff Brown6249b902012-05-26 14:32:54 -0700263 if (!strcasecmp(entry->d_name, name)) {
264 /* we have a match - replace the name, don't need to copy the null again */
265 memcpy(actual, entry->d_name, namelen);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800266 break;
267 }
268 }
269 closedir(dir);
270 }
Jeff Brown6249b902012-05-26 14:32:54 -0700271 return actual;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800272}
273
Jeff Brown6249b902012-05-26 14:32:54 -0700274static void attr_from_stat(struct fuse_attr *attr, const struct stat *s, __u64 nid)
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800275{
Jeff Brown6249b902012-05-26 14:32:54 -0700276 attr->ino = nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700277 attr->size = s->st_size;
278 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400279 attr->atime = s->st_atime;
280 attr->mtime = s->st_mtime;
281 attr->ctime = s->st_ctime;
282 attr->atimensec = s->st_atime_nsec;
283 attr->mtimensec = s->st_mtime_nsec;
284 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700285 attr->mode = s->st_mode;
286 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700287
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700288 /* force permissions to something reasonable:
289 * world readable
290 * writable by the sdcard group
291 */
292 if (attr->mode & 0100) {
293 attr->mode = (attr->mode & (~0777)) | 0775;
294 } else {
295 attr->mode = (attr->mode & (~0777)) | 0664;
296 }
297
298 /* all files owned by root.sdcard */
299 attr->uid = 0;
300 attr->gid = AID_SDCARD_RW;
Brian Swetland03ee9472010-08-12 18:01:08 -0700301}
302
Jeff Brown6249b902012-05-26 14:32:54 -0700303struct node *create_node_locked(struct fuse* fuse,
304 struct node *parent, const char *name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700305{
306 struct node *node;
Jeff Brown6249b902012-05-26 14:32:54 -0700307 size_t namelen = strlen(name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700308
Paul Eastham11ccdb32010-10-14 11:04:26 -0700309 node = calloc(1, sizeof(struct node));
Jeff Brown6249b902012-05-26 14:32:54 -0700310 if (!node) {
311 return NULL;
Brian Swetland03ee9472010-08-12 18:01:08 -0700312 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700313 node->name = malloc(namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700314 if (!node->name) {
Paul Eastham11ccdb32010-10-14 11:04:26 -0700315 free(node);
Jeff Brown6249b902012-05-26 14:32:54 -0700316 return NULL;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700317 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700318 memcpy(node->name, name, namelen + 1);
Jeff Brown6249b902012-05-26 14:32:54 -0700319 if (strcmp(name, actual_name)) {
320 node->actual_name = malloc(namelen + 1);
321 if (!node->actual_name) {
322 free(node->name);
323 free(node);
324 return NULL;
325 }
326 memcpy(node->actual_name, actual_name, namelen + 1);
327 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700328 node->namelen = namelen;
Jeff Brown6249b902012-05-26 14:32:54 -0700329 node->nid = ptr_to_id(node);
330 node->gen = fuse->next_generation++;
331 acquire_node_locked(node);
332 add_node_to_parent_locked(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700333 return node;
334}
335
Jeff Brown6249b902012-05-26 14:32:54 -0700336static int rename_node_locked(struct node *node, const char *name,
337 const char* actual_name)
Paul Eastham11ccdb32010-10-14 11:04:26 -0700338{
Jeff Brown6249b902012-05-26 14:32:54 -0700339 size_t namelen = strlen(name);
340 int need_actual_name = strcmp(name, actual_name);
341
342 /* make the storage bigger without actually changing the name
343 * in case an error occurs part way */
344 if (namelen > node->namelen) {
345 char* new_name = realloc(node->name, namelen + 1);
346 if (!new_name) {
347 return -ENOMEM;
348 }
349 node->name = new_name;
350 if (need_actual_name && node->actual_name) {
351 char* new_actual_name = realloc(node->actual_name, namelen + 1);
352 if (!new_actual_name) {
353 return -ENOMEM;
354 }
355 node->actual_name = new_actual_name;
356 }
357 }
358
359 /* update the name, taking care to allocate storage before overwriting the old name */
360 if (need_actual_name) {
361 if (!node->actual_name) {
362 node->actual_name = malloc(namelen + 1);
363 if (!node->actual_name) {
364 return -ENOMEM;
365 }
366 }
367 memcpy(node->actual_name, actual_name, namelen + 1);
368 } else {
369 free(node->actual_name);
370 node->actual_name = NULL;
371 }
372 memcpy(node->name, name, namelen + 1);
373 node->namelen = namelen;
374 return 0;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700375}
376
Jeff Brown6249b902012-05-26 14:32:54 -0700377static struct node *lookup_node_by_id_locked(struct fuse *fuse, __u64 nid)
Brian Swetland03ee9472010-08-12 18:01:08 -0700378{
Jeff Brown6249b902012-05-26 14:32:54 -0700379 if (nid == FUSE_ROOT_ID) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700380 return &fuse->root;
381 } else {
382 return id_to_ptr(nid);
383 }
384}
385
Jeff Brown6249b902012-05-26 14:32:54 -0700386static struct node* lookup_node_and_path_by_id_locked(struct fuse* fuse, __u64 nid,
387 char* buf, size_t bufsize)
388{
389 struct node* node = lookup_node_by_id_locked(fuse, nid);
390 if (node && get_node_path_locked(node, buf, bufsize) < 0) {
391 node = NULL;
392 }
393 return node;
394}
395
396static struct node *lookup_child_by_name_locked(struct node *node, const char *name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700397{
398 for (node = node->child; node; node = node->next) {
Jeff Brown6249b902012-05-26 14:32:54 -0700399 /* use exact string comparison, nodes that differ by case
400 * must be considered distinct even if they refer to the same
401 * underlying file as otherwise operations such as "mv x x"
402 * will not work because the source and target nodes are the same. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700403 if (!strcmp(name, node->name)) {
404 return node;
405 }
406 }
407 return 0;
408}
409
Jeff Brown6249b902012-05-26 14:32:54 -0700410static struct node* acquire_or_create_child_locked(
411 struct fuse* fuse, struct node* parent,
412 const char* name, const char* actual_name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700413{
Jeff Brown6249b902012-05-26 14:32:54 -0700414 struct node* child = lookup_child_by_name_locked(parent, name);
415 if (child) {
416 acquire_node_locked(child);
Paul Eastham77085c52011-01-04 21:06:03 -0800417 } else {
Jeff Brown6249b902012-05-26 14:32:54 -0700418 child = create_node_locked(fuse, parent, name, actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800419 }
Jeff Brown6249b902012-05-26 14:32:54 -0700420 return child;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700421}
422
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700423static void fuse_init(struct fuse *fuse, int fd, const char *source_path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700424{
Jeff Brown6249b902012-05-26 14:32:54 -0700425 pthread_mutex_init(&fuse->lock, NULL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700426
Jeff Brown6249b902012-05-26 14:32:54 -0700427 fuse->fd = fd;
428 fuse->next_generation = 0;
Brian Swetland03ee9472010-08-12 18:01:08 -0700429
Jeff Brown6249b902012-05-26 14:32:54 -0700430 memset(&fuse->root, 0, sizeof(fuse->root));
431 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
432 fuse->root.refcount = 2;
Jeff Sharkeye169bd02012-08-13 16:44:42 -0700433 fuse->root.namelen = strlen(source_path);
434 fuse->root.name = strdup(source_path);
Brian Swetland03ee9472010-08-12 18:01:08 -0700435}
436
Jeff Brown6249b902012-05-26 14:32:54 -0700437static void fuse_status(struct fuse *fuse, __u64 unique, int err)
Brian Swetland03ee9472010-08-12 18:01:08 -0700438{
439 struct fuse_out_header hdr;
440 hdr.len = sizeof(hdr);
441 hdr.error = err;
442 hdr.unique = unique;
Brian Swetland03ee9472010-08-12 18:01:08 -0700443 write(fuse->fd, &hdr, sizeof(hdr));
444}
445
Jeff Brown6249b902012-05-26 14:32:54 -0700446static void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700447{
448 struct fuse_out_header hdr;
449 struct iovec vec[2];
450 int res;
451
452 hdr.len = len + sizeof(hdr);
453 hdr.error = 0;
454 hdr.unique = unique;
455
456 vec[0].iov_base = &hdr;
457 vec[0].iov_len = sizeof(hdr);
458 vec[1].iov_base = data;
459 vec[1].iov_len = len;
460
461 res = writev(fuse->fd, vec, 2);
462 if (res < 0) {
463 ERROR("*** REPLY FAILED *** %d\n", errno);
464 }
465}
466
Jeff Brown6249b902012-05-26 14:32:54 -0700467static int fuse_reply_entry(struct fuse* fuse, __u64 unique,
468 struct node* parent, const char* name, const char* actual_name,
469 const char* path)
Brian Swetland03ee9472010-08-12 18:01:08 -0700470{
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700471 struct node* node;
Jeff Brown6249b902012-05-26 14:32:54 -0700472 struct fuse_entry_out out;
473 struct stat s;
Brian Swetland03ee9472010-08-12 18:01:08 -0700474
Jeff Brown6249b902012-05-26 14:32:54 -0700475 if (lstat(path, &s) < 0) {
476 return -errno;
Brian Swetland03ee9472010-08-12 18:01:08 -0700477 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700478
Jeff Brown6249b902012-05-26 14:32:54 -0700479 pthread_mutex_lock(&fuse->lock);
480 node = acquire_or_create_child_locked(fuse, parent, name, actual_name);
481 if (!node) {
482 pthread_mutex_unlock(&fuse->lock);
483 return -ENOMEM;
484 }
485 memset(&out, 0, sizeof(out));
486 attr_from_stat(&out.attr, &s, node->nid);
487 out.attr_valid = 10;
488 out.entry_valid = 10;
Brian Swetland03ee9472010-08-12 18:01:08 -0700489 out.nodeid = node->nid;
490 out.generation = node->gen;
Jeff Brown6249b902012-05-26 14:32:54 -0700491 pthread_mutex_unlock(&fuse->lock);
Brian Swetland03ee9472010-08-12 18:01:08 -0700492 fuse_reply(fuse, unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700493 return NO_STATUS;
Brian Swetland03ee9472010-08-12 18:01:08 -0700494}
495
Jeff Brown6249b902012-05-26 14:32:54 -0700496static int fuse_reply_attr(struct fuse* fuse, __u64 unique, __u64 nid,
497 const char* path)
498{
499 struct fuse_attr_out out;
500 struct stat s;
501
502 if (lstat(path, &s) < 0) {
503 return -errno;
504 }
505 memset(&out, 0, sizeof(out));
506 attr_from_stat(&out.attr, &s, nid);
507 out.attr_valid = 10;
508 fuse_reply(fuse, unique, &out, sizeof(out));
509 return NO_STATUS;
510}
511
512static int handle_lookup(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700513 const struct fuse_in_header *hdr, const char* name)
Brian Swetland03ee9472010-08-12 18:01:08 -0700514{
Jeff Brown6249b902012-05-26 14:32:54 -0700515 struct node* parent_node;
516 char parent_path[PATH_MAX];
517 char child_path[PATH_MAX];
518 const char* actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700519
Jeff Brown6249b902012-05-26 14:32:54 -0700520 pthread_mutex_lock(&fuse->lock);
521 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
522 parent_path, sizeof(parent_path));
523 TRACE("[%d] LOOKUP %s @ %llx (%s)\n", handler->token, name, hdr->nodeid,
524 parent_node ? parent_node->name : "?");
525 pthread_mutex_unlock(&fuse->lock);
526
527 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
528 child_path, sizeof(child_path), 1))) {
529 return -ENOENT;
Brian Swetland03ee9472010-08-12 18:01:08 -0700530 }
Jeff Brown6249b902012-05-26 14:32:54 -0700531 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700532}
533
Jeff Brown6249b902012-05-26 14:32:54 -0700534static int handle_forget(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700535 const struct fuse_in_header *hdr, const struct fuse_forget_in *req)
536{
Jeff Brown6249b902012-05-26 14:32:54 -0700537 struct node* node;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700538
Jeff Brown6249b902012-05-26 14:32:54 -0700539 pthread_mutex_lock(&fuse->lock);
540 node = lookup_node_by_id_locked(fuse, hdr->nodeid);
541 TRACE("[%d] FORGET #%lld @ %llx (%s)\n", handler->token, req->nlookup,
542 hdr->nodeid, node ? node->name : "?");
543 if (node) {
544 __u64 n = req->nlookup;
545 while (n--) {
546 release_node_locked(node);
547 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700548 }
Jeff Brown6249b902012-05-26 14:32:54 -0700549 pthread_mutex_unlock(&fuse->lock);
550 return NO_STATUS; /* no reply */
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700551}
552
Jeff Brown6249b902012-05-26 14:32:54 -0700553static int handle_getattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700554 const struct fuse_in_header *hdr, const struct fuse_getattr_in *req)
555{
Jeff Brown6249b902012-05-26 14:32:54 -0700556 struct node* node;
557 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700558
Jeff Brown6249b902012-05-26 14:32:54 -0700559 pthread_mutex_lock(&fuse->lock);
560 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
561 TRACE("[%d] GETATTR flags=%x fh=%llx @ %llx (%s)\n", handler->token,
562 req->getattr_flags, req->fh, hdr->nodeid, node ? node->name : "?");
563 pthread_mutex_unlock(&fuse->lock);
564
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700565 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700566 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700567 }
Jeff Brown6249b902012-05-26 14:32:54 -0700568 return fuse_reply_attr(fuse, hdr->unique, hdr->nodeid, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700569}
570
Jeff Brown6249b902012-05-26 14:32:54 -0700571static int handle_setattr(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700572 const struct fuse_in_header *hdr, const struct fuse_setattr_in *req)
573{
Jeff Brown6249b902012-05-26 14:32:54 -0700574 struct node* node;
575 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700576 struct timespec times[2];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700577
Jeff Brown6249b902012-05-26 14:32:54 -0700578 pthread_mutex_lock(&fuse->lock);
579 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
580 TRACE("[%d] SETATTR fh=%llx valid=%x @ %llx (%s)\n", handler->token,
581 req->fh, req->valid, hdr->nodeid, node ? node->name : "?");
582 pthread_mutex_unlock(&fuse->lock);
583
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700584 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700585 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700586 }
587
Jeff Brown6249b902012-05-26 14:32:54 -0700588 /* XXX: incomplete implementation on purpose.
589 * chmod/chown should NEVER be implemented.*/
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700590
Jeff Brown6249b902012-05-26 14:32:54 -0700591 if ((req->valid & FATTR_SIZE) && truncate(path, req->size) < 0) {
592 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700593 }
594
595 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
596 * are both set, then set it to the current time. Else, set it to the
597 * time specified in the request. Same goes for mtime. Use utimensat(2)
598 * as it allows ATIME and MTIME to be changed independently, and has
599 * nanosecond resolution which fuse also has.
600 */
601 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
602 times[0].tv_nsec = UTIME_OMIT;
603 times[1].tv_nsec = UTIME_OMIT;
604 if (req->valid & FATTR_ATIME) {
605 if (req->valid & FATTR_ATIME_NOW) {
606 times[0].tv_nsec = UTIME_NOW;
607 } else {
608 times[0].tv_sec = req->atime;
609 times[0].tv_nsec = req->atimensec;
610 }
611 }
612 if (req->valid & FATTR_MTIME) {
613 if (req->valid & FATTR_MTIME_NOW) {
614 times[1].tv_nsec = UTIME_NOW;
615 } else {
616 times[1].tv_sec = req->mtime;
617 times[1].tv_nsec = req->mtimensec;
618 }
619 }
Jeff Brown6249b902012-05-26 14:32:54 -0700620 TRACE("[%d] Calling utimensat on %s with atime %ld, mtime=%ld\n",
621 handler->token, path, times[0].tv_sec, times[1].tv_sec);
622 if (utimensat(-1, path, times, 0) < 0) {
623 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700624 }
625 }
Jeff Brown6249b902012-05-26 14:32:54 -0700626 return fuse_reply_attr(fuse, hdr->unique, hdr->nodeid, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700627}
628
Jeff Brown6249b902012-05-26 14:32:54 -0700629static int handle_mknod(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700630 const struct fuse_in_header* hdr, const struct fuse_mknod_in* req, const char* name)
631{
Jeff Brown6249b902012-05-26 14:32:54 -0700632 struct node* parent_node;
633 char parent_path[PATH_MAX];
634 char child_path[PATH_MAX];
635 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700636
Jeff Brown6249b902012-05-26 14:32:54 -0700637 pthread_mutex_lock(&fuse->lock);
638 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
639 parent_path, sizeof(parent_path));
640 TRACE("[%d] MKNOD %s 0%o @ %llx (%s)\n", handler->token,
641 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
642 pthread_mutex_unlock(&fuse->lock);
643
644 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
645 child_path, sizeof(child_path), 1))) {
646 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700647 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700648 __u32 mode = (req->mode & (~0777)) | 0664;
Jeff Brown6249b902012-05-26 14:32:54 -0700649 if (mknod(child_path, mode, req->rdev) < 0) {
650 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700651 }
Jeff Brown6249b902012-05-26 14:32:54 -0700652 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700653}
654
Jeff Brown6249b902012-05-26 14:32:54 -0700655static int handle_mkdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700656 const struct fuse_in_header* hdr, const struct fuse_mkdir_in* req, const char* name)
657{
Jeff Brown6249b902012-05-26 14:32:54 -0700658 struct node* parent_node;
659 char parent_path[PATH_MAX];
660 char child_path[PATH_MAX];
661 const char* actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700662
Jeff Brown6249b902012-05-26 14:32:54 -0700663 pthread_mutex_lock(&fuse->lock);
664 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
665 parent_path, sizeof(parent_path));
666 TRACE("[%d] MKDIR %s 0%o @ %llx (%s)\n", handler->token,
667 name, req->mode, hdr->nodeid, parent_node ? parent_node->name : "?");
668 pthread_mutex_unlock(&fuse->lock);
669
670 if (!parent_node || !(actual_name = find_file_within(parent_path, name,
671 child_path, sizeof(child_path), 1))) {
672 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700673 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700674 __u32 mode = (req->mode & (~0777)) | 0775;
Jeff Brown6249b902012-05-26 14:32:54 -0700675 if (mkdir(child_path, mode) < 0) {
676 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700677 }
Jeff Brown6249b902012-05-26 14:32:54 -0700678 return fuse_reply_entry(fuse, hdr->unique, parent_node, name, actual_name, child_path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700679}
680
Jeff Brown6249b902012-05-26 14:32:54 -0700681static int handle_unlink(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700682 const struct fuse_in_header* hdr, const char* name)
683{
Jeff Brown6249b902012-05-26 14:32:54 -0700684 struct node* parent_node;
685 char parent_path[PATH_MAX];
686 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700687
Jeff Brown6249b902012-05-26 14:32:54 -0700688 pthread_mutex_lock(&fuse->lock);
689 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
690 parent_path, sizeof(parent_path));
691 TRACE("[%d] UNLINK %s @ %llx (%s)\n", handler->token,
692 name, hdr->nodeid, parent_node ? parent_node->name : "?");
693 pthread_mutex_unlock(&fuse->lock);
694
695 if (!parent_node || !find_file_within(parent_path, name,
696 child_path, sizeof(child_path), 1)) {
697 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700698 }
Jeff Brown6249b902012-05-26 14:32:54 -0700699 if (unlink(child_path) < 0) {
700 return -errno;
701 }
702 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700703}
704
Jeff Brown6249b902012-05-26 14:32:54 -0700705static int handle_rmdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700706 const struct fuse_in_header* hdr, const char* name)
707{
Jeff Brown6249b902012-05-26 14:32:54 -0700708 struct node* parent_node;
709 char parent_path[PATH_MAX];
710 char child_path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700711
Jeff Brown6249b902012-05-26 14:32:54 -0700712 pthread_mutex_lock(&fuse->lock);
713 parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
714 parent_path, sizeof(parent_path));
715 TRACE("[%d] RMDIR %s @ %llx (%s)\n", handler->token,
716 name, hdr->nodeid, parent_node ? parent_node->name : "?");
717 pthread_mutex_unlock(&fuse->lock);
718
719 if (!parent_node || !find_file_within(parent_path, name,
720 child_path, sizeof(child_path), 1)) {
721 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700722 }
Jeff Brown6249b902012-05-26 14:32:54 -0700723 if (rmdir(child_path) < 0) {
724 return -errno;
725 }
726 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700727}
728
Jeff Brown6249b902012-05-26 14:32:54 -0700729static int handle_rename(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700730 const struct fuse_in_header* hdr, const struct fuse_rename_in* req,
Jeff Brown6249b902012-05-26 14:32:54 -0700731 const char* old_name, const char* new_name)
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700732{
Jeff Brown6249b902012-05-26 14:32:54 -0700733 struct node* old_parent_node;
734 struct node* new_parent_node;
735 struct node* child_node;
736 char old_parent_path[PATH_MAX];
737 char new_parent_path[PATH_MAX];
738 char old_child_path[PATH_MAX];
739 char new_child_path[PATH_MAX];
740 const char* new_actual_name;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700741 int res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700742
Jeff Brown6249b902012-05-26 14:32:54 -0700743 pthread_mutex_lock(&fuse->lock);
744 old_parent_node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid,
745 old_parent_path, sizeof(old_parent_path));
746 new_parent_node = lookup_node_and_path_by_id_locked(fuse, req->newdir,
747 new_parent_path, sizeof(new_parent_path));
748 TRACE("[%d] RENAME %s->%s @ %llx (%s) -> %llx (%s)\n", handler->token,
749 old_name, new_name,
750 hdr->nodeid, old_parent_node ? old_parent_node->name : "?",
751 req->newdir, new_parent_node ? new_parent_node->name : "?");
752 if (!old_parent_node || !new_parent_node) {
753 res = -ENOENT;
754 goto lookup_error;
755 }
756 child_node = lookup_child_by_name_locked(old_parent_node, old_name);
757 if (!child_node || get_node_path_locked(child_node,
758 old_child_path, sizeof(old_child_path)) < 0) {
759 res = -ENOENT;
760 goto lookup_error;
761 }
762 acquire_node_locked(child_node);
763 pthread_mutex_unlock(&fuse->lock);
764
765 /* Special case for renaming a file where destination is same path
766 * differing only by case. In this case we don't want to look for a case
767 * insensitive match. This allows commands like "mv foo FOO" to work as expected.
768 */
769 int search = old_parent_node != new_parent_node
770 || strcasecmp(old_name, new_name);
771 if (!(new_actual_name = find_file_within(new_parent_path, new_name,
772 new_child_path, sizeof(new_child_path), search))) {
773 res = -ENOENT;
774 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700775 }
776
Jeff Brown6249b902012-05-26 14:32:54 -0700777 TRACE("[%d] RENAME %s->%s\n", handler->token, old_child_path, new_child_path);
778 res = rename(old_child_path, new_child_path);
779 if (res < 0) {
780 res = -errno;
781 goto io_error;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700782 }
783
Jeff Brown6249b902012-05-26 14:32:54 -0700784 pthread_mutex_lock(&fuse->lock);
785 res = rename_node_locked(child_node, new_name, new_actual_name);
786 if (!res) {
787 remove_node_from_parent_locked(child_node);
788 add_node_to_parent_locked(child_node, new_parent_node);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700789 }
Jeff Brown6249b902012-05-26 14:32:54 -0700790 goto done;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700791
Jeff Brown6249b902012-05-26 14:32:54 -0700792io_error:
793 pthread_mutex_lock(&fuse->lock);
794done:
795 release_node_locked(child_node);
796lookup_error:
797 pthread_mutex_unlock(&fuse->lock);
798 return res;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700799}
800
Jeff Brown6249b902012-05-26 14:32:54 -0700801static int handle_open(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700802 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
803{
Jeff Brown6249b902012-05-26 14:32:54 -0700804 struct node* node;
805 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700806 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700807 struct handle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700808
Jeff Brown6249b902012-05-26 14:32:54 -0700809 pthread_mutex_lock(&fuse->lock);
810 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
811 TRACE("[%d] OPEN 0%o @ %llx (%s)\n", handler->token,
812 req->flags, hdr->nodeid, node ? node->name : "?");
813 pthread_mutex_unlock(&fuse->lock);
814
815 if (!node) {
816 return -ENOENT;
817 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700818 h = malloc(sizeof(*h));
819 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -0700820 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700821 }
Jeff Brown6249b902012-05-26 14:32:54 -0700822 TRACE("[%d] OPEN %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700823 h->fd = open(path, req->flags);
824 if (h->fd < 0) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700825 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700826 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700827 }
828 out.fh = ptr_to_id(h);
829 out.open_flags = 0;
830 out.padding = 0;
831 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700832 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700833}
834
Jeff Brown6249b902012-05-26 14:32:54 -0700835static int handle_read(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700836 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
837{
838 struct handle *h = id_to_ptr(req->fh);
839 __u64 unique = hdr->unique;
840 __u32 size = req->size;
841 __u64 offset = req->offset;
Jeff Brown6249b902012-05-26 14:32:54 -0700842 int res;
843
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700844 /* Don't access any other fields of hdr or req beyond this point, the read buffer
845 * overlaps the request buffer and will clobber data in the request. This
846 * saves us 128KB per request handler thread at the cost of this scary comment. */
Jeff Brown6249b902012-05-26 14:32:54 -0700847
848 TRACE("[%d] READ %p(%d) %u@%llu\n", handler->token,
849 h, h->fd, size, offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700850 if (size > sizeof(handler->read_buffer)) {
Jeff Brown6249b902012-05-26 14:32:54 -0700851 return -EINVAL;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700852 }
853 res = pread64(h->fd, handler->read_buffer, size, offset);
854 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700855 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700856 }
857 fuse_reply(fuse, unique, handler->read_buffer, res);
Jeff Brown6249b902012-05-26 14:32:54 -0700858 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700859}
860
Jeff Brown6249b902012-05-26 14:32:54 -0700861static int handle_write(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700862 const struct fuse_in_header* hdr, const struct fuse_write_in* req,
863 const void* buffer)
864{
865 struct fuse_write_out out;
866 struct handle *h = id_to_ptr(req->fh);
867 int res;
Jeff Brown6249b902012-05-26 14:32:54 -0700868
869 TRACE("[%d] WRITE %p(%d) %u@%llu\n", handler->token,
870 h, h->fd, req->size, req->offset);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700871 res = pwrite64(h->fd, buffer, req->size, req->offset);
872 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700873 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700874 }
875 out.size = res;
876 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700877 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700878}
879
Jeff Brown6249b902012-05-26 14:32:54 -0700880static int handle_statfs(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700881 const struct fuse_in_header* hdr)
882{
Jeff Brown6249b902012-05-26 14:32:54 -0700883 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700884 struct statfs stat;
885 struct fuse_statfs_out out;
886 int res;
887
Jeff Brown6249b902012-05-26 14:32:54 -0700888 pthread_mutex_lock(&fuse->lock);
889 TRACE("[%d] STATFS\n", handler->token);
890 res = get_node_path_locked(&fuse->root, path, sizeof(path));
891 pthread_mutex_unlock(&fuse->lock);
892 if (res < 0) {
893 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700894 }
Jeff Brown6249b902012-05-26 14:32:54 -0700895 if (statfs(fuse->root.name, &stat) < 0) {
896 return -errno;
897 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700898 memset(&out, 0, sizeof(out));
899 out.st.blocks = stat.f_blocks;
900 out.st.bfree = stat.f_bfree;
901 out.st.bavail = stat.f_bavail;
902 out.st.files = stat.f_files;
903 out.st.ffree = stat.f_ffree;
904 out.st.bsize = stat.f_bsize;
905 out.st.namelen = stat.f_namelen;
906 out.st.frsize = stat.f_frsize;
907 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700908 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700909}
910
Jeff Brown6249b902012-05-26 14:32:54 -0700911static int handle_release(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700912 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
913{
914 struct handle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -0700915
916 TRACE("[%d] RELEASE %p(%d)\n", handler->token, h, h->fd);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700917 close(h->fd);
918 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700919 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700920}
921
Jeff Brown6249b902012-05-26 14:32:54 -0700922static int handle_fsync(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700923 const struct fuse_in_header* hdr, const struct fuse_fsync_in* req)
924{
925 int is_data_sync = req->fsync_flags & 1;
926 struct handle *h = id_to_ptr(req->fh);
927 int res;
Jeff Brown6249b902012-05-26 14:32:54 -0700928
929 TRACE("[%d] FSYNC %p(%d) is_data_sync=%d\n", handler->token,
930 h, h->fd, is_data_sync);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700931 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
932 if (res < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -0700933 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700934 }
Jeff Brown6249b902012-05-26 14:32:54 -0700935 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700936}
937
Jeff Brown6249b902012-05-26 14:32:54 -0700938static int handle_flush(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700939 const struct fuse_in_header* hdr)
940{
Jeff Brown6249b902012-05-26 14:32:54 -0700941 TRACE("[%d] FLUSH\n", handler->token);
942 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700943}
944
Jeff Brown6249b902012-05-26 14:32:54 -0700945static int handle_opendir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700946 const struct fuse_in_header* hdr, const struct fuse_open_in* req)
947{
Jeff Brown6249b902012-05-26 14:32:54 -0700948 struct node* node;
949 char path[PATH_MAX];
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700950 struct fuse_open_out out;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700951 struct dirhandle *h;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700952
Jeff Brown6249b902012-05-26 14:32:54 -0700953 pthread_mutex_lock(&fuse->lock);
954 node = lookup_node_and_path_by_id_locked(fuse, hdr->nodeid, path, sizeof(path));
955 TRACE("[%d] OPENDIR @ %llx (%s)\n", handler->token,
956 hdr->nodeid, node ? node->name : "?");
957 pthread_mutex_unlock(&fuse->lock);
958
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700959 if (!node) {
Jeff Brown6249b902012-05-26 14:32:54 -0700960 return -ENOENT;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700961 }
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700962 h = malloc(sizeof(*h));
963 if (!h) {
Jeff Brown6249b902012-05-26 14:32:54 -0700964 return -ENOMEM;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700965 }
Jeff Brown6249b902012-05-26 14:32:54 -0700966 TRACE("[%d] OPENDIR %s\n", handler->token, path);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700967 h->d = opendir(path);
Jeff Brown6249b902012-05-26 14:32:54 -0700968 if (!h->d) {
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700969 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -0700970 return -errno;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700971 }
972 out.fh = ptr_to_id(h);
973 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -0700974 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700975}
976
Jeff Brown6249b902012-05-26 14:32:54 -0700977static int handle_readdir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700978 const struct fuse_in_header* hdr, const struct fuse_read_in* req)
979{
980 char buffer[8192];
981 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
982 struct dirent *de;
983 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -0700984
985 TRACE("[%d] READDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700986 if (req->offset == 0) {
987 /* rewinddir() might have been called above us, so rewind here too */
Jeff Brown6249b902012-05-26 14:32:54 -0700988 TRACE("[%d] calling rewinddir()\n", handler->token);
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700989 rewinddir(h->d);
990 }
991 de = readdir(h->d);
992 if (!de) {
Jeff Brown6249b902012-05-26 14:32:54 -0700993 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -0700994 }
995 fde->ino = FUSE_UNKNOWN_INO;
996 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
997 fde->off = req->offset + 1;
998 fde->type = de->d_type;
999 fde->namelen = strlen(de->d_name);
1000 memcpy(fde->name, de->d_name, fde->namelen + 1);
1001 fuse_reply(fuse, hdr->unique, fde,
Jeff Brown6249b902012-05-26 14:32:54 -07001002 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
1003 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001004}
1005
Jeff Brown6249b902012-05-26 14:32:54 -07001006static int handle_releasedir(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001007 const struct fuse_in_header* hdr, const struct fuse_release_in* req)
1008{
1009 struct dirhandle *h = id_to_ptr(req->fh);
Jeff Brown6249b902012-05-26 14:32:54 -07001010
1011 TRACE("[%d] RELEASEDIR %p\n", handler->token, h);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001012 closedir(h->d);
1013 free(h);
Jeff Brown6249b902012-05-26 14:32:54 -07001014 return 0;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001015}
1016
Jeff Brown6249b902012-05-26 14:32:54 -07001017static int handle_init(struct fuse* fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001018 const struct fuse_in_header* hdr, const struct fuse_init_in* req)
1019{
1020 struct fuse_init_out out;
1021
Jeff Brown6249b902012-05-26 14:32:54 -07001022 TRACE("[%d] INIT ver=%d.%d maxread=%d flags=%x\n",
1023 handler->token, req->major, req->minor, req->max_readahead, req->flags);
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001024 out.major = FUSE_KERNEL_VERSION;
1025 out.minor = FUSE_KERNEL_MINOR_VERSION;
1026 out.max_readahead = req->max_readahead;
1027 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
1028 out.max_background = 32;
1029 out.congestion_threshold = 32;
1030 out.max_write = MAX_WRITE;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001031 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown6249b902012-05-26 14:32:54 -07001032 return NO_STATUS;
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001033}
1034
Jeff Brown6249b902012-05-26 14:32:54 -07001035static int handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001036 const struct fuse_in_header *hdr, const void *data, size_t data_len)
1037{
Brian Swetland03ee9472010-08-12 18:01:08 -07001038 switch (hdr->opcode) {
1039 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001040 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001041 return handle_lookup(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001042 }
Jeff Brown84715842012-05-25 14:07:47 -07001043
Brian Swetland03ee9472010-08-12 18:01:08 -07001044 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -07001045 const struct fuse_forget_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001046 return handle_forget(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001047 }
Jeff Brown84715842012-05-25 14:07:47 -07001048
Brian Swetland03ee9472010-08-12 18:01:08 -07001049 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001050 const struct fuse_getattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001051 return handle_getattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001052 }
Jeff Brown84715842012-05-25 14:07:47 -07001053
Brian Swetland03ee9472010-08-12 18:01:08 -07001054 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -07001055 const struct fuse_setattr_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001056 return handle_setattr(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001057 }
Jeff Brown84715842012-05-25 14:07:47 -07001058
Brian Swetland03ee9472010-08-12 18:01:08 -07001059// case FUSE_READLINK:
1060// case FUSE_SYMLINK:
1061 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001062 const struct fuse_mknod_in *req = data;
1063 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001064 return handle_mknod(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001065 }
Jeff Brown84715842012-05-25 14:07:47 -07001066
Brian Swetland03ee9472010-08-12 18:01:08 -07001067 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -07001068 const struct fuse_mkdir_in *req = data;
1069 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001070 return handle_mkdir(fuse, handler, hdr, req, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001071 }
Jeff Brown84715842012-05-25 14:07:47 -07001072
Brian Swetland03ee9472010-08-12 18:01:08 -07001073 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001074 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001075 return handle_unlink(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001076 }
Jeff Brown84715842012-05-25 14:07:47 -07001077
Brian Swetland03ee9472010-08-12 18:01:08 -07001078 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -07001079 const char* name = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001080 return handle_rmdir(fuse, handler, hdr, name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001081 }
Jeff Brown84715842012-05-25 14:07:47 -07001082
Brian Swetland03ee9472010-08-12 18:01:08 -07001083 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -07001084 const struct fuse_rename_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001085 const char *old_name = ((const char*) data) + sizeof(*req);
1086 const char *new_name = old_name + strlen(old_name) + 1;
1087 return handle_rename(fuse, handler, hdr, req, old_name, new_name);
Brian Swetland03ee9472010-08-12 18:01:08 -07001088 }
Jeff Brown84715842012-05-25 14:07:47 -07001089
Jeff Brownfc1e1a02012-05-25 17:24:17 -07001090// case FUSE_LINK:
Brian Swetland03ee9472010-08-12 18:01:08 -07001091 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001092 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001093 return handle_open(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001094 }
Jeff Brown84715842012-05-25 14:07:47 -07001095
Brian Swetland03ee9472010-08-12 18:01:08 -07001096 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -07001097 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001098 return handle_read(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001099 }
Jeff Brown84715842012-05-25 14:07:47 -07001100
Brian Swetland03ee9472010-08-12 18:01:08 -07001101 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -07001102 const struct fuse_write_in *req = data;
1103 const void* buffer = (const __u8*)data + sizeof(*req);
Jeff Brown6249b902012-05-26 14:32:54 -07001104 return handle_write(fuse, handler, hdr, req, buffer);
Brian Swetland03ee9472010-08-12 18:01:08 -07001105 }
Jeff Brown84715842012-05-25 14:07:47 -07001106
Mike Lockwood4553b082010-08-16 14:14:44 -04001107 case FUSE_STATFS: { /* getattr_in -> attr_out */
Jeff Brown6249b902012-05-26 14:32:54 -07001108 return handle_statfs(fuse, handler, hdr);
Mike Lockwood4553b082010-08-16 14:14:44 -04001109 }
Jeff Brown84715842012-05-25 14:07:47 -07001110
Brian Swetland03ee9472010-08-12 18:01:08 -07001111 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001112 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001113 return handle_release(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001114 }
Jeff Brown84715842012-05-25 14:07:47 -07001115
Jeff Brown6fd921a2012-05-25 15:01:21 -07001116 case FUSE_FSYNC: {
1117 const struct fuse_fsync_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001118 return handle_fsync(fuse, handler, hdr, req);
Jeff Brown6fd921a2012-05-25 15:01:21 -07001119 }
1120
Brian Swetland03ee9472010-08-12 18:01:08 -07001121// case FUSE_SETXATTR:
1122// case FUSE_GETXATTR:
1123// case FUSE_LISTXATTR:
1124// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -07001125 case FUSE_FLUSH: {
Jeff Brown6249b902012-05-26 14:32:54 -07001126 return handle_flush(fuse, handler, hdr);
Jeff Brown84715842012-05-25 14:07:47 -07001127 }
1128
Brian Swetland03ee9472010-08-12 18:01:08 -07001129 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -07001130 const struct fuse_open_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001131 return handle_opendir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001132 }
Jeff Brown84715842012-05-25 14:07:47 -07001133
Brian Swetland03ee9472010-08-12 18:01:08 -07001134 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -07001135 const struct fuse_read_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001136 return handle_readdir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001137 }
Jeff Brown84715842012-05-25 14:07:47 -07001138
Brian Swetland03ee9472010-08-12 18:01:08 -07001139 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -07001140 const struct fuse_release_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001141 return handle_releasedir(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001142 }
Jeff Brown84715842012-05-25 14:07:47 -07001143
Brian Swetland03ee9472010-08-12 18:01:08 -07001144// case FUSE_FSYNCDIR:
1145 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -07001146 const struct fuse_init_in *req = data;
Jeff Brown6249b902012-05-26 14:32:54 -07001147 return handle_init(fuse, handler, hdr, req);
Brian Swetland03ee9472010-08-12 18:01:08 -07001148 }
Jeff Brown84715842012-05-25 14:07:47 -07001149
Brian Swetland03ee9472010-08-12 18:01:08 -07001150 default: {
Jeff Brown6249b902012-05-26 14:32:54 -07001151 TRACE("[%d] NOTIMPL op=%d uniq=%llx nid=%llx\n",
1152 handler->token, hdr->opcode, hdr->unique, hdr->nodeid);
1153 return -ENOSYS;
Brian Swetland03ee9472010-08-12 18:01:08 -07001154 }
Jeff Brown84715842012-05-25 14:07:47 -07001155 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001156}
1157
Jeff Brown6249b902012-05-26 14:32:54 -07001158static void handle_fuse_requests(struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -07001159{
Jeff Brown6249b902012-05-26 14:32:54 -07001160 struct fuse* fuse = handler->fuse;
Brian Swetland03ee9472010-08-12 18:01:08 -07001161 for (;;) {
Jeff Brown6249b902012-05-26 14:32:54 -07001162 ssize_t len = read(fuse->fd,
1163 handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -07001164 if (len < 0) {
Jeff Brown6249b902012-05-26 14:32:54 -07001165 if (errno != EINTR) {
1166 ERROR("[%d] handle_fuse_requests: errno=%d\n", handler->token, errno);
1167 }
1168 continue;
Brian Swetland03ee9472010-08-12 18:01:08 -07001169 }
Jeff Brown84715842012-05-25 14:07:47 -07001170
1171 if ((size_t)len < sizeof(struct fuse_in_header)) {
Jeff Brown6249b902012-05-26 14:32:54 -07001172 ERROR("[%d] request too short: len=%zu\n", handler->token, (size_t)len);
1173 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001174 }
1175
Jeff Brown7729d242012-05-25 15:35:28 -07001176 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001177 if (hdr->len != (size_t)len) {
Jeff Brown6249b902012-05-26 14:32:54 -07001178 ERROR("[%d] malformed header: len=%zu, hdr->len=%u\n",
1179 handler->token, (size_t)len, hdr->len);
1180 continue;
Jeff Brown84715842012-05-25 14:07:47 -07001181 }
1182
Jeff Brown7729d242012-05-25 15:35:28 -07001183 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001184 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown6249b902012-05-26 14:32:54 -07001185 __u64 unique = hdr->unique;
1186 int res = handle_fuse_request(fuse, handler, hdr, data, data_len);
Jeff Brown7729d242012-05-25 15:35:28 -07001187
1188 /* We do not access the request again after this point because the underlying
1189 * buffer storage may have been reused while processing the request. */
Jeff Brown6249b902012-05-26 14:32:54 -07001190
1191 if (res != NO_STATUS) {
1192 if (res) {
1193 TRACE("[%d] ERROR %d\n", handler->token, res);
1194 }
1195 fuse_status(fuse, unique, res);
1196 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001197 }
1198}
1199
Jeff Brown6249b902012-05-26 14:32:54 -07001200static void* start_handler(void* data)
Jeff Brown7729d242012-05-25 15:35:28 -07001201{
Jeff Brown6249b902012-05-26 14:32:54 -07001202 struct fuse_handler* handler = data;
1203 handle_fuse_requests(handler);
1204 return NULL;
1205}
1206
1207static int ignite_fuse(struct fuse* fuse, int num_threads)
1208{
1209 struct fuse_handler* handlers;
1210 int i;
1211
1212 handlers = malloc(num_threads * sizeof(struct fuse_handler));
1213 if (!handlers) {
1214 ERROR("cannot allocate storage for threads");
1215 return -ENOMEM;
1216 }
1217
1218 for (i = 0; i < num_threads; i++) {
1219 handlers[i].fuse = fuse;
1220 handlers[i].token = i;
1221 }
1222
1223 for (i = 1; i < num_threads; i++) {
1224 pthread_t thread;
1225 int res = pthread_create(&thread, NULL, start_handler, &handlers[i]);
1226 if (res) {
1227 ERROR("failed to start thread #%d, error=%d", i, res);
1228 goto quit;
1229 }
1230 }
1231 handle_fuse_requests(&handlers[0]);
1232 ERROR("terminated prematurely");
1233
1234 /* don't bother killing all of the other threads or freeing anything,
1235 * should never get here anyhow */
1236quit:
1237 exit(1);
Jeff Brown7729d242012-05-25 15:35:28 -07001238}
1239
Mike Lockwood4f35e622011-01-12 14:39:44 -05001240static int usage()
1241{
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001242 ERROR("usage: sdcard [-t<threads>] <source_path> <dest_path> <uid> <gid>\n"
Jeff Brown6249b902012-05-26 14:32:54 -07001243 " -t<threads>: specify number of threads to use, default -t%d\n"
1244 "\n", DEFAULT_NUM_THREADS);
Jeff Brown26567352012-05-25 13:27:43 -07001245 return 1;
1246}
1247
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001248static int run(const char* source_path, const char* dest_path, uid_t uid, gid_t gid,
1249 int num_threads) {
Jeff Brown26567352012-05-25 13:27:43 -07001250 int fd;
1251 char opts[256];
1252 int res;
1253 struct fuse fuse;
1254
1255 /* cleanup from previous instance, if necessary */
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001256 umount2(dest_path, 2);
Jeff Brown26567352012-05-25 13:27:43 -07001257
1258 fd = open("/dev/fuse", O_RDWR);
1259 if (fd < 0){
1260 ERROR("cannot open fuse device (error %d)\n", errno);
1261 return -1;
1262 }
1263
1264 snprintf(opts, sizeof(opts),
1265 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1266 fd, uid, gid);
1267
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001268 res = mount("/dev/fuse", dest_path, "fuse", MS_NOSUID | MS_NODEV, opts);
Jeff Brown26567352012-05-25 13:27:43 -07001269 if (res < 0) {
1270 ERROR("cannot mount fuse filesystem (error %d)\n", errno);
1271 goto error;
1272 }
1273
1274 res = setgid(gid);
1275 if (res < 0) {
1276 ERROR("cannot setgid (error %d)\n", errno);
1277 goto error;
1278 }
1279
1280 res = setuid(uid);
1281 if (res < 0) {
1282 ERROR("cannot setuid (error %d)\n", errno);
1283 goto error;
1284 }
1285
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001286 fuse_init(&fuse, fd, source_path);
Jeff Brown26567352012-05-25 13:27:43 -07001287
1288 umask(0);
Jeff Brown6249b902012-05-26 14:32:54 -07001289 res = ignite_fuse(&fuse, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001290
1291 /* we do not attempt to umount the file system here because we are no longer
1292 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001293
1294error:
1295 close(fd);
1296 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001297}
1298
Brian Swetland03ee9472010-08-12 18:01:08 -07001299int main(int argc, char **argv)
1300{
Brian Swetland03ee9472010-08-12 18:01:08 -07001301 int res;
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001302 const char *source_path = NULL;
1303 const char *dest_path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001304 uid_t uid = 0;
1305 gid_t gid = 0;
Jeff Brown6249b902012-05-26 14:32:54 -07001306 int num_threads = DEFAULT_NUM_THREADS;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001307 int i;
Brian Swetland03ee9472010-08-12 18:01:08 -07001308
Mike Lockwood4f35e622011-01-12 14:39:44 -05001309 for (i = 1; i < argc; i++) {
1310 char* arg = argv[i];
Jeff Brown6249b902012-05-26 14:32:54 -07001311 if (!strncmp(arg, "-t", 2))
1312 num_threads = strtoul(arg + 2, 0, 10);
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001313 else if (!source_path)
1314 source_path = arg;
1315 else if (!dest_path)
1316 dest_path = arg;
Jean-Baptiste Querue92372b2012-08-15 09:54:30 -07001317 else if (!uid) {
1318 char* endptr = NULL;
1319 errno = 0;
1320 uid = strtoul(arg, &endptr, 10);
1321 if (*endptr != '\0' || errno != 0) {
1322 ERROR("Invalid uid");
1323 return usage();
1324 }
1325 } else if (!gid) {
1326 char* endptr = NULL;
1327 errno = 0;
1328 gid = strtoul(arg, &endptr, 10);
1329 if (*endptr != '\0' || errno != 0) {
1330 ERROR("Invalid gid");
1331 return usage();
1332 }
1333 } else {
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001334 ERROR("too many arguments\n");
1335 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001336 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001337 }
1338
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001339 if (!source_path) {
1340 ERROR("no source path specified\n");
1341 return usage();
1342 }
1343 if (!dest_path) {
1344 ERROR("no dest path specified\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001345 return usage();
1346 }
Jeff Brown26567352012-05-25 13:27:43 -07001347 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001348 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001349 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001350 }
Jeff Brown6249b902012-05-26 14:32:54 -07001351 if (num_threads < 1) {
1352 ERROR("number of threads must be at least 1\n");
1353 return usage();
1354 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001355
Jeff Sharkeye169bd02012-08-13 16:44:42 -07001356 res = run(source_path, dest_path, uid, gid, num_threads);
Jeff Brown26567352012-05-25 13:27:43 -07001357 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001358}