blob: 4d727899b922d6a288b27787fdb6d2af1e0f45df [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>
Brian Swetland03ee9472010-08-12 18:01:08 -070030
Brian Swetlandb14a2c62010-08-12 18:21:12 -070031#include <private/android_filesystem_config.h>
32
Brian Swetland03ee9472010-08-12 18:01:08 -070033#include "fuse.h"
34
35/* README
36 *
37 * What is this?
38 *
39 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
40 * directory permissions (all files are given fixed owner, group, and
41 * permissions at creation, owner, group, and permissions are not
42 * changeable, symlinks and hardlinks are not createable, etc.
43 *
44 * usage: sdcard <path> <uid> <gid>
45 *
46 * It must be run as root, but will change to uid/gid as soon as it
Jeff Sharkeycfa9f652012-04-09 17:09:38 -070047 * mounts a filesystem on /storage/sdcard. It will refuse to run if uid or
Brian Swetland03ee9472010-08-12 18:01:08 -070048 * gid are zero.
49 *
50 *
51 * Things I believe to be true:
52 *
53 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
54 * CREAT) must bump that node's refcount
55 * - don't forget that FORGET can forget multiple references (req->nlookup)
56 * - if an op that returns a fuse_entry fails writing the reply to the
57 * kernel, you must rollback the refcount to reflect the reference the
58 * kernel did not actually acquire
59 *
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 Sharkeycfa9f652012-04-09 17:09:38 -070074#define MOUNT_POINT "/storage/sdcard0"
Mike Lockwood4553b082010-08-16 14:14:44 -040075
Jeff Brown84715842012-05-25 14:07:47 -070076/* Maximum number of bytes to write in one request. */
77#define MAX_WRITE (256 * 1024)
78
79/* Maximum number of bytes to read in one request. */
80#define MAX_READ (128 * 1024)
81
82/* Largest possible request.
83 * The request size is bounded by the maximum size of a FUSE_WRITE request because it has
84 * the largest possible data payload. */
85#define MAX_REQUEST_SIZE (sizeof(struct fuse_in_header) + sizeof(struct fuse_write_in) + MAX_WRITE)
86
Brian Swetland03ee9472010-08-12 18:01:08 -070087struct handle {
88 struct node *node;
89 int fd;
90};
91
92struct dirhandle {
93 struct node *node;
94 DIR *d;
95};
96
97struct node {
98 __u64 nid;
99 __u64 gen;
100
Paul Eastham11ccdb32010-10-14 11:04:26 -0700101 struct node *next; /* per-dir sibling list */
102 struct node *child; /* first contained file by this dir */
103 struct node *all; /* global node list */
104 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -0700105
106 __u32 refcount;
107 __u32 namelen;
108
Paul Eastham11ccdb32010-10-14 11:04:26 -0700109 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800110 /* If non-null, this is the real name of the file in the underlying storage.
111 * This may differ from the field "name" only by case.
112 * strlen(actual_name) will always equal strlen(name), so it is safe to use
113 * namelen for both fields.
114 */
115 char *actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700116};
117
Jeff Brown7729d242012-05-25 15:35:28 -0700118/* Global data structure shared by all fuse handlers. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700119struct fuse {
120 __u64 next_generation;
121 __u64 next_node_id;
122
123 int fd;
124
125 struct node *all;
126
127 struct node root;
Jeff Brown7729d242012-05-25 15:35:28 -0700128 char rootpath[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700129};
130
Jeff Brown7729d242012-05-25 15:35:28 -0700131/* Private data used by a single fuse handler. */
132struct fuse_handler {
133 /* To save memory, we never use the contents of the request buffer and the read
134 * buffer at the same time. This allows us to share the underlying storage. */
135 union {
136 __u8 request_buffer[MAX_REQUEST_SIZE];
137 __u8 read_buffer[MAX_READ];
138 };
139};
Brian Swetland03ee9472010-08-12 18:01:08 -0700140
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800141#define NO_CASE_SENSITIVE_MATCH 0
142#define CASE_SENSITIVE_MATCH 1
Mike Lockwoodb94d3202011-01-17 21:06:26 -0800143
Paul Easthamf43219e2010-09-21 17:14:31 -0700144/*
145 * Get the real-life absolute path to a node.
146 * node: start at this node
147 * buf: storage for returned string
148 * name: append this string to path if set
149 */
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800150char *do_node_get_path(struct node *node, char *buf, const char *name, int match_case_insensitive)
Brian Swetland03ee9472010-08-12 18:01:08 -0700151{
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800152 struct node *in_node = node;
153 const char *in_name = name;
Jeff Brown7729d242012-05-25 15:35:28 -0700154 char *out = buf + PATH_MAX - 1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700155 int len;
156 out[0] = 0;
157
158 if (name) {
159 len = strlen(name);
160 goto start;
161 }
162
163 while (node) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800164 name = (node->actual_name ? node->actual_name : node->name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700165 len = node->namelen;
166 node = node->parent;
167 start:
168 if ((len + 1) > (out - buf))
169 return 0;
170 out -= len;
171 memcpy(out, name, len);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800172 /* avoid double slash at beginning of path */
173 if (out[0] != '/') {
174 out --;
175 out[0] = '/';
176 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700177 }
178
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800179 /* If we are searching for a file within node (rather than computing node's path)
180 * and fail, then we need to look for a case insensitive match.
181 */
182 if (in_name && match_case_insensitive && access(out, F_OK) != 0) {
Jeff Brown7729d242012-05-25 15:35:28 -0700183 char *path, buffer[PATH_MAX];
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800184 DIR* dir;
185 struct dirent* entry;
186 path = do_node_get_path(in_node, buffer, NULL, NO_CASE_SENSITIVE_MATCH);
187 dir = opendir(path);
188 if (!dir) {
189 ERROR("opendir %s failed: %s", path, strerror(errno));
190 return out;
191 }
192
193 while ((entry = readdir(dir))) {
194 if (!strcasecmp(entry->d_name, in_name)) {
195 /* we have a match - replace the name */
196 len = strlen(in_name);
Jeff Brown7729d242012-05-25 15:35:28 -0700197 memcpy(buf + PATH_MAX - len - 1, entry->d_name, len);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800198 break;
199 }
200 }
201 closedir(dir);
202 }
203
204 return out;
205}
206
207char *node_get_path(struct node *node, char *buf, const char *name)
208{
209 /* We look for case insensitive matches by default */
210 return do_node_get_path(node, buf, name, CASE_SENSITIVE_MATCH);
Brian Swetland03ee9472010-08-12 18:01:08 -0700211}
212
213void attr_from_stat(struct fuse_attr *attr, struct stat *s)
214{
215 attr->ino = s->st_ino;
216 attr->size = s->st_size;
217 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400218 attr->atime = s->st_atime;
219 attr->mtime = s->st_mtime;
220 attr->ctime = s->st_ctime;
221 attr->atimensec = s->st_atime_nsec;
222 attr->mtimensec = s->st_mtime_nsec;
223 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700224 attr->mode = s->st_mode;
225 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700226
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700227 /* force permissions to something reasonable:
228 * world readable
229 * writable by the sdcard group
230 */
231 if (attr->mode & 0100) {
232 attr->mode = (attr->mode & (~0777)) | 0775;
233 } else {
234 attr->mode = (attr->mode & (~0777)) | 0664;
235 }
236
237 /* all files owned by root.sdcard */
238 attr->uid = 0;
239 attr->gid = AID_SDCARD_RW;
Brian Swetland03ee9472010-08-12 18:01:08 -0700240}
241
242int node_get_attr(struct node *node, struct fuse_attr *attr)
243{
244 int res;
245 struct stat s;
Jeff Brown7729d242012-05-25 15:35:28 -0700246 char *path, buffer[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700247
248 path = node_get_path(node, buffer, 0);
249 res = lstat(path, &s);
250 if (res < 0) {
251 ERROR("lstat('%s') errno %d\n", path, errno);
252 return -1;
253 }
254
255 attr_from_stat(attr, &s);
256 attr->ino = node->nid;
257
258 return 0;
259}
260
Paul Eastham11ccdb32010-10-14 11:04:26 -0700261static void add_node_to_parent(struct node *node, struct node *parent) {
262 node->parent = parent;
263 node->next = parent->child;
264 parent->child = node;
Paul Eastham77085c52011-01-04 21:06:03 -0800265 parent->refcount++;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700266}
267
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800268/* Check to see if our parent directory already has a file with a name
269 * that differs only by case. If we find one, store it in the actual_name
270 * field so node_get_path will map it to this file in the underlying storage.
271 */
272static void node_find_actual_name(struct node *node)
273{
Jeff Brown7729d242012-05-25 15:35:28 -0700274 char *path, buffer[PATH_MAX];
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800275 const char *node_name = node->name;
276 DIR* dir;
277 struct dirent* entry;
278
279 if (!node->parent) return;
280
281 path = node_get_path(node->parent, buffer, 0);
282 dir = opendir(path);
283 if (!dir) {
284 ERROR("opendir %s failed: %s", path, strerror(errno));
285 return;
286 }
287
288 while ((entry = readdir(dir))) {
289 const char *test_name = entry->d_name;
290 if (strcmp(test_name, node_name) && !strcasecmp(test_name, node_name)) {
291 /* we have a match - differs but only by case */
292 node->actual_name = strdup(test_name);
293 if (!node->actual_name) {
294 ERROR("strdup failed - out of memory\n");
295 exit(1);
296 }
297 break;
298 }
299 }
300 closedir(dir);
301}
302
Brian Swetland03ee9472010-08-12 18:01:08 -0700303struct node *node_create(struct node *parent, const char *name, __u64 nid, __u64 gen)
304{
305 struct node *node;
306 int namelen = strlen(name);
307
Paul Eastham11ccdb32010-10-14 11:04:26 -0700308 node = calloc(1, sizeof(struct node));
Brian Swetland03ee9472010-08-12 18:01:08 -0700309 if (node == 0) {
310 return 0;
311 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700312 node->name = malloc(namelen + 1);
313 if (node->name == 0) {
314 free(node);
315 return 0;
316 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700317
318 node->nid = nid;
319 node->gen = gen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700320 add_node_to_parent(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700321 memcpy(node->name, name, namelen + 1);
322 node->namelen = namelen;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800323 node_find_actual_name(node);
Brian Swetland03ee9472010-08-12 18:01:08 -0700324 return node;
325}
326
Paul Eastham11ccdb32010-10-14 11:04:26 -0700327static char *rename_node(struct node *node, const char *name)
328{
329 node->namelen = strlen(name);
330 char *newname = realloc(node->name, node->namelen + 1);
331 if (newname == 0)
332 return 0;
333 node->name = newname;
334 memcpy(node->name, name, node->namelen + 1);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800335 node_find_actual_name(node);
Paul Eastham11ccdb32010-10-14 11:04:26 -0700336 return node->name;
337}
338
Brian Swetland03ee9472010-08-12 18:01:08 -0700339void fuse_init(struct fuse *fuse, int fd, const char *path)
340{
341 fuse->fd = fd;
342 fuse->next_node_id = 2;
343 fuse->next_generation = 0;
344
345 fuse->all = &fuse->root;
346
Terry Heo (Woncheol)8349cce2011-03-16 13:02:05 +0900347 memset(&fuse->root, 0, sizeof(fuse->root));
Brian Swetland03ee9472010-08-12 18:01:08 -0700348 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
Brian Swetland03ee9472010-08-12 18:01:08 -0700349 fuse->root.refcount = 2;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700350 rename_node(&fuse->root, path);
Brian Swetland03ee9472010-08-12 18:01:08 -0700351}
352
353static inline void *id_to_ptr(__u64 nid)
354{
Jeff Brown26567352012-05-25 13:27:43 -0700355 return (void *) (uintptr_t) nid;
Brian Swetland03ee9472010-08-12 18:01:08 -0700356}
357
358static inline __u64 ptr_to_id(void *ptr)
359{
Jeff Brown26567352012-05-25 13:27:43 -0700360 return (__u64) (uintptr_t) ptr;
Brian Swetland03ee9472010-08-12 18:01:08 -0700361}
362
363
364struct node *lookup_by_inode(struct fuse *fuse, __u64 nid)
365{
366 if (nid == FUSE_ROOT_ID) {
367 return &fuse->root;
368 } else {
369 return id_to_ptr(nid);
370 }
371}
372
373struct node *lookup_child_by_name(struct node *node, const char *name)
374{
375 for (node = node->child; node; node = node->next) {
376 if (!strcmp(name, node->name)) {
377 return node;
378 }
379 }
380 return 0;
381}
382
383struct node *lookup_child_by_inode(struct node *node, __u64 nid)
384{
385 for (node = node->child; node; node = node->next) {
386 if (node->nid == nid) {
387 return node;
388 }
389 }
390 return 0;
391}
392
Paul Eastham77085c52011-01-04 21:06:03 -0800393static void dec_refcount(struct node *node) {
394 if (node->refcount > 0) {
395 node->refcount--;
396 TRACE("dec_refcount %p(%s) -> %d\n", node, node->name, node->refcount);
397 } else {
398 ERROR("Zero refcnt %p\n", node);
399 }
400 }
401
Paul Eastham11ccdb32010-10-14 11:04:26 -0700402static struct node *remove_child(struct node *parent, __u64 nid)
403{
404 struct node *prev = 0;
405 struct node *node;
406
407 for (node = parent->child; node; node = node->next) {
408 if (node->nid == nid) {
409 if (prev) {
410 prev->next = node->next;
411 } else {
412 parent->child = node->next;
413 }
414 node->next = 0;
415 node->parent = 0;
Paul Eastham77085c52011-01-04 21:06:03 -0800416 dec_refcount(parent);
Paul Eastham11ccdb32010-10-14 11:04:26 -0700417 return node;
418 }
419 prev = node;
420 }
421 return 0;
422}
423
Brian Swetland03ee9472010-08-12 18:01:08 -0700424struct node *node_lookup(struct fuse *fuse, struct node *parent, const char *name,
425 struct fuse_attr *attr)
426{
427 int res;
428 struct stat s;
Jeff Brown7729d242012-05-25 15:35:28 -0700429 char *path, buffer[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700430 struct node *node;
431
432 path = node_get_path(parent, buffer, name);
433 /* XXX error? */
434
435 res = lstat(path, &s);
436 if (res < 0)
437 return 0;
438
439 node = lookup_child_by_name(parent, name);
440 if (!node) {
441 node = node_create(parent, name, fuse->next_node_id++, fuse->next_generation++);
442 if (!node)
443 return 0;
444 node->nid = ptr_to_id(node);
445 node->all = fuse->all;
446 fuse->all = node;
447 }
448
449 attr_from_stat(attr, &s);
450 attr->ino = node->nid;
451
452 return node;
453}
454
455void node_release(struct node *node)
456{
457 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
Paul Eastham77085c52011-01-04 21:06:03 -0800458 dec_refcount(node);
Brian Swetland03ee9472010-08-12 18:01:08 -0700459 if (node->refcount == 0) {
460 if (node->parent->child == node) {
461 node->parent->child = node->parent->child->next;
462 } else {
463 struct node *node2;
464
465 node2 = node->parent->child;
466 while (node2->next != node)
467 node2 = node2->next;
468 node2->next = node->next;
469 }
470
471 TRACE("DESTROY %p (%s)\n", node, node->name);
472
473 node_release(node->parent);
474
475 node->parent = 0;
476 node->next = 0;
477
478 /* TODO: remove debugging - poison memory */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700479 memset(node->name, 0xef, node->namelen);
480 free(node->name);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800481 free(node->actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800482 memset(node, 0xfc, sizeof(*node));
Brian Swetland03ee9472010-08-12 18:01:08 -0700483 free(node);
484 }
485}
486
487void fuse_status(struct fuse *fuse, __u64 unique, int err)
488{
489 struct fuse_out_header hdr;
490 hdr.len = sizeof(hdr);
491 hdr.error = err;
492 hdr.unique = unique;
493 if (err) {
494// ERROR("*** %d ***\n", err);
495 }
496 write(fuse->fd, &hdr, sizeof(hdr));
497}
498
499void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
500{
501 struct fuse_out_header hdr;
502 struct iovec vec[2];
503 int res;
504
505 hdr.len = len + sizeof(hdr);
506 hdr.error = 0;
507 hdr.unique = unique;
508
509 vec[0].iov_base = &hdr;
510 vec[0].iov_len = sizeof(hdr);
511 vec[1].iov_base = data;
512 vec[1].iov_len = len;
513
514 res = writev(fuse->fd, vec, 2);
515 if (res < 0) {
516 ERROR("*** REPLY FAILED *** %d\n", errno);
517 }
518}
519
520void lookup_entry(struct fuse *fuse, struct node *node,
521 const char *name, __u64 unique)
522{
523 struct fuse_entry_out out;
524
525 memset(&out, 0, sizeof(out));
526
527 node = node_lookup(fuse, node, name, &out.attr);
528 if (!node) {
529 fuse_status(fuse, unique, -ENOENT);
530 return;
531 }
532
533 node->refcount++;
534// fprintf(stderr,"ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
535 out.nodeid = node->nid;
536 out.generation = node->gen;
537 out.entry_valid = 10;
538 out.attr_valid = 10;
539
540 fuse_reply(fuse, unique, &out, sizeof(out));
541}
542
Jeff Brown7729d242012-05-25 15:35:28 -0700543void handle_fuse_request(struct fuse *fuse, struct fuse_handler* handler,
Jeff Brown84715842012-05-25 14:07:47 -0700544 const struct fuse_in_header *hdr, const void *data, size_t data_len)
Brian Swetland03ee9472010-08-12 18:01:08 -0700545{
546 struct node *node;
547
Brian Swetland03ee9472010-08-12 18:01:08 -0700548 if (hdr->nodeid) {
549 node = lookup_by_inode(fuse, hdr->nodeid);
550 if (!node) {
551 fuse_status(fuse, hdr->unique, -ENOENT);
552 return;
553 }
554 } else {
555 node = 0;
556 }
557
558 switch (hdr->opcode) {
559 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -0700560 const char* name = data;
561 TRACE("LOOKUP %llx %s\n", hdr->nodeid, name);
562 lookup_entry(fuse, node, name, hdr->unique);
Brian Swetland03ee9472010-08-12 18:01:08 -0700563 return;
564 }
Jeff Brown84715842012-05-25 14:07:47 -0700565
Brian Swetland03ee9472010-08-12 18:01:08 -0700566 case FUSE_FORGET: {
Jeff Brown84715842012-05-25 14:07:47 -0700567 const struct fuse_forget_in *req = data;
568 __u64 n = req->nlookup;
569 TRACE("FORGET %llx (%s) #%lld\n", hdr->nodeid, node->name, n);
Brian Swetland03ee9472010-08-12 18:01:08 -0700570 /* no reply */
Jeff Brown84715842012-05-25 14:07:47 -0700571 while (n--)
Brian Swetland03ee9472010-08-12 18:01:08 -0700572 node_release(node);
573 return;
574 }
Jeff Brown84715842012-05-25 14:07:47 -0700575
Brian Swetland03ee9472010-08-12 18:01:08 -0700576 case FUSE_GETATTR: { /* getattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -0700577 const struct fuse_getattr_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700578 struct fuse_attr_out out;
579
Paul Eastham11ccdb32010-10-14 11:04:26 -0700580 TRACE("GETATTR flags=%x fh=%llx\n", req->getattr_flags, req->fh);
Brian Swetland03ee9472010-08-12 18:01:08 -0700581
582 memset(&out, 0, sizeof(out));
583 node_get_attr(node, &out.attr);
584 out.attr_valid = 10;
585
586 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
587 return;
588 }
Jeff Brown84715842012-05-25 14:07:47 -0700589
Brian Swetland03ee9472010-08-12 18:01:08 -0700590 case FUSE_SETATTR: { /* setattr_in -> attr_out */
Jeff Brown84715842012-05-25 14:07:47 -0700591 const struct fuse_setattr_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700592 struct fuse_attr_out out;
Jeff Brown7729d242012-05-25 15:35:28 -0700593 char *path, buffer[PATH_MAX];
Paul Easthamf43219e2010-09-21 17:14:31 -0700594 int res = 0;
Ken Sumrall97919652011-03-18 11:53:15 -0700595 struct timespec times[2];
Paul Easthamf43219e2010-09-21 17:14:31 -0700596
Brian Swetland03ee9472010-08-12 18:01:08 -0700597 TRACE("SETATTR fh=%llx id=%llx valid=%x\n",
598 req->fh, hdr->nodeid, req->valid);
599
Ken Sumrall97919652011-03-18 11:53:15 -0700600 /* XXX: incomplete implementation on purpose. chmod/chown
601 * should NEVER be implemented.*/
Paul Easthamf43219e2010-09-21 17:14:31 -0700602
603 path = node_get_path(node, buffer, 0);
604 if (req->valid & FATTR_SIZE)
605 res = truncate(path, req->size);
Ken Sumrall97919652011-03-18 11:53:15 -0700606 if (res)
607 goto getout;
Brian Swetland03ee9472010-08-12 18:01:08 -0700608
Ken Sumrall97919652011-03-18 11:53:15 -0700609 /* Handle changing atime and mtime. If FATTR_ATIME_and FATTR_ATIME_NOW
610 * are both set, then set it to the current time. Else, set it to the
611 * time specified in the request. Same goes for mtime. Use utimensat(2)
612 * as it allows ATIME and MTIME to be changed independently, and has
613 * nanosecond resolution which fuse also has.
614 */
615 if (req->valid & (FATTR_ATIME | FATTR_MTIME)) {
616 times[0].tv_nsec = UTIME_OMIT;
617 times[1].tv_nsec = UTIME_OMIT;
618 if (req->valid & FATTR_ATIME) {
619 if (req->valid & FATTR_ATIME_NOW) {
620 times[0].tv_nsec = UTIME_NOW;
621 } else {
622 times[0].tv_sec = req->atime;
623 times[0].tv_nsec = req->atimensec;
624 }
625 }
626 if (req->valid & FATTR_MTIME) {
627 if (req->valid & FATTR_MTIME_NOW) {
628 times[1].tv_nsec = UTIME_NOW;
629 } else {
630 times[1].tv_sec = req->mtime;
631 times[1].tv_nsec = req->mtimensec;
632 }
633 }
634 TRACE("Calling utimensat on %s with atime %ld, mtime=%ld\n", path, times[0].tv_sec, times[1].tv_sec);
635 res = utimensat(-1, path, times, 0);
636 }
637
638 getout:
Brian Swetland03ee9472010-08-12 18:01:08 -0700639 memset(&out, 0, sizeof(out));
640 node_get_attr(node, &out.attr);
641 out.attr_valid = 10;
Paul Easthamf43219e2010-09-21 17:14:31 -0700642
643 if (res)
644 fuse_status(fuse, hdr->unique, -errno);
645 else
646 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Brian Swetland03ee9472010-08-12 18:01:08 -0700647 return;
648 }
Jeff Brown84715842012-05-25 14:07:47 -0700649
Brian Swetland03ee9472010-08-12 18:01:08 -0700650// case FUSE_READLINK:
651// case FUSE_SYMLINK:
652 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -0700653 const struct fuse_mknod_in *req = data;
654 const char *name = ((const char*) data) + sizeof(*req);
Jeff Brown7729d242012-05-25 15:35:28 -0700655 char *path, buffer[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700656 int res;
Mike Lockwood51b3a2d2011-01-12 07:35:46 -0500657
Brian Swetland03ee9472010-08-12 18:01:08 -0700658 TRACE("MKNOD %s @ %llx\n", name, hdr->nodeid);
659 path = node_get_path(node, buffer, name);
660
Jeff Brown84715842012-05-25 14:07:47 -0700661 __u32 mode = (req->mode & (~0777)) | 0664;
662 res = mknod(path, mode, req->rdev); /* XXX perm?*/
Brian Swetland03ee9472010-08-12 18:01:08 -0700663 if (res < 0) {
664 fuse_status(fuse, hdr->unique, -errno);
665 } else {
666 lookup_entry(fuse, node, name, hdr->unique);
667 }
668 return;
669 }
Jeff Brown84715842012-05-25 14:07:47 -0700670
Brian Swetland03ee9472010-08-12 18:01:08 -0700671 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
Jeff Brown84715842012-05-25 14:07:47 -0700672 const struct fuse_mkdir_in *req = data;
673 const char *name = ((const char*) data) + sizeof(*req);
Brian Swetland03ee9472010-08-12 18:01:08 -0700674 struct fuse_entry_out out;
Jeff Brown7729d242012-05-25 15:35:28 -0700675 char *path, buffer[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700676 int res;
Mike Lockwood51b3a2d2011-01-12 07:35:46 -0500677
Brian Swetland03ee9472010-08-12 18:01:08 -0700678 TRACE("MKDIR %s @ %llx 0%o\n", name, hdr->nodeid, req->mode);
679 path = node_get_path(node, buffer, name);
680
Jeff Brown84715842012-05-25 14:07:47 -0700681 __u32 mode = (req->mode & (~0777)) | 0775;
682 res = mkdir(path, mode);
Brian Swetland03ee9472010-08-12 18:01:08 -0700683 if (res < 0) {
684 fuse_status(fuse, hdr->unique, -errno);
685 } else {
686 lookup_entry(fuse, node, name, hdr->unique);
687 }
688 return;
689 }
Jeff Brown84715842012-05-25 14:07:47 -0700690
Brian Swetland03ee9472010-08-12 18:01:08 -0700691 case FUSE_UNLINK: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -0700692 const char* name = data;
Jeff Brown7729d242012-05-25 15:35:28 -0700693 char *path, buffer[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700694 int res;
Jeff Brown84715842012-05-25 14:07:47 -0700695 TRACE("UNLINK %s @ %llx\n", name, hdr->nodeid);
696 path = node_get_path(node, buffer, name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700697 res = unlink(path);
698 fuse_status(fuse, hdr->unique, res ? -errno : 0);
699 return;
700 }
Jeff Brown84715842012-05-25 14:07:47 -0700701
Brian Swetland03ee9472010-08-12 18:01:08 -0700702 case FUSE_RMDIR: { /* bytez[] -> */
Jeff Brown84715842012-05-25 14:07:47 -0700703 const char* name = data;
Jeff Brown7729d242012-05-25 15:35:28 -0700704 char *path, buffer[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700705 int res;
Jeff Brown84715842012-05-25 14:07:47 -0700706 TRACE("RMDIR %s @ %llx\n", name, hdr->nodeid);
707 path = node_get_path(node, buffer, name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700708 res = rmdir(path);
709 fuse_status(fuse, hdr->unique, res ? -errno : 0);
710 return;
711 }
Jeff Brown84715842012-05-25 14:07:47 -0700712
Brian Swetland03ee9472010-08-12 18:01:08 -0700713 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
Jeff Brown84715842012-05-25 14:07:47 -0700714 const struct fuse_rename_in *req = data;
715 const char *oldname = ((const char*) data) + sizeof(*req);
716 const char *newname = oldname + strlen(oldname) + 1;
Jeff Brown7729d242012-05-25 15:35:28 -0700717 char *oldpath, oldbuffer[PATH_MAX];
718 char *newpath, newbuffer[PATH_MAX];
Paul Eastham11ccdb32010-10-14 11:04:26 -0700719 struct node *target;
720 struct node *newparent;
Brian Swetland03ee9472010-08-12 18:01:08 -0700721 int res;
722
Paul Eastham11ccdb32010-10-14 11:04:26 -0700723 TRACE("RENAME %s->%s @ %llx\n", oldname, newname, hdr->nodeid);
724
725 target = lookup_child_by_name(node, oldname);
726 if (!target) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700727 fuse_status(fuse, hdr->unique, -ENOENT);
728 return;
729 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700730 oldpath = node_get_path(node, oldbuffer, oldname);
Paul Eastham11ccdb32010-10-14 11:04:26 -0700731
732 newparent = lookup_by_inode(fuse, req->newdir);
733 if (!newparent) {
734 fuse_status(fuse, hdr->unique, -ENOENT);
735 return;
736 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800737 if (newparent == node) {
738 /* Special case for renaming a file where destination
739 * is same path differing only by case.
740 * In this case we don't want to look for a case insensitive match.
741 * This allows commands like "mv foo FOO" to work as expected.
742 */
743 newpath = do_node_get_path(newparent, newbuffer, newname, NO_CASE_SENSITIVE_MATCH);
744 } else {
745 newpath = node_get_path(newparent, newbuffer, newname);
746 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700747
748 if (!remove_child(node, target->nid)) {
749 ERROR("RENAME remove_child not found");
750 fuse_status(fuse, hdr->unique, -ENOENT);
751 return;
752 }
753 if (!rename_node(target, newname)) {
754 fuse_status(fuse, hdr->unique, -ENOMEM);
755 return;
756 }
757 add_node_to_parent(target, newparent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700758
759 res = rename(oldpath, newpath);
Paul Eastham11ccdb32010-10-14 11:04:26 -0700760 TRACE("RENAME result %d\n", res);
761
Brian Swetland03ee9472010-08-12 18:01:08 -0700762 fuse_status(fuse, hdr->unique, res ? -errno : 0);
763 return;
764 }
Jeff Brown84715842012-05-25 14:07:47 -0700765
Brian Swetland03ee9472010-08-12 18:01:08 -0700766// case FUSE_LINK:
767 case FUSE_OPEN: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -0700768 const struct fuse_open_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700769 struct fuse_open_out out;
Jeff Brown7729d242012-05-25 15:35:28 -0700770 char *path, buffer[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700771 struct handle *h;
772
773 h = malloc(sizeof(*h));
774 if (!h) {
775 fuse_status(fuse, hdr->unique, -ENOMEM);
776 return;
777 }
778
779 path = node_get_path(node, buffer, 0);
780 TRACE("OPEN %llx '%s' 0%o fh=%p\n", hdr->nodeid, path, req->flags, h);
781 h->fd = open(path, req->flags);
782 if (h->fd < 0) {
783 ERROR("ERROR\n");
Yuncheol Heo2fc9fc72011-07-22 17:42:22 +0900784 fuse_status(fuse, hdr->unique, -errno);
Brian Swetland03ee9472010-08-12 18:01:08 -0700785 free(h);
786 return;
787 }
788 out.fh = ptr_to_id(h);
789 out.open_flags = 0;
790 out.padding = 0;
791 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
792 return;
793 }
Jeff Brown84715842012-05-25 14:07:47 -0700794
Brian Swetland03ee9472010-08-12 18:01:08 -0700795 case FUSE_READ: { /* read_in -> byte[] */
Jeff Brown84715842012-05-25 14:07:47 -0700796 const struct fuse_read_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700797 struct handle *h = id_to_ptr(req->fh);
Jeff Brown7729d242012-05-25 15:35:28 -0700798 __u64 unique = hdr->unique;
799 __u32 size = req->size;
800 __u64 offset = req->offset;
801 /* Don't access any other fields of hdr or req beyond this point, the read buffer
802 * overlaps the request buffer and will clobber data in the request. This
803 * saves us 128KB per request handler thread at the cost of this scary comment. */
Brian Swetland03ee9472010-08-12 18:01:08 -0700804 int res;
Jeff Brown7729d242012-05-25 15:35:28 -0700805 TRACE("READ %p(%d) %u@%llu\n", h, h->fd, size, offset);
806 if (size > sizeof(handler->read_buffer)) {
807 fuse_status(fuse, unique, -EINVAL);
Brian Swetland03ee9472010-08-12 18:01:08 -0700808 return;
809 }
Jeff Brown7729d242012-05-25 15:35:28 -0700810 res = pread64(h->fd, handler->read_buffer, size, offset);
Brian Swetland03ee9472010-08-12 18:01:08 -0700811 if (res < 0) {
Jeff Brown7729d242012-05-25 15:35:28 -0700812 fuse_status(fuse, unique, -errno);
Brian Swetland03ee9472010-08-12 18:01:08 -0700813 return;
814 }
Jeff Brown7729d242012-05-25 15:35:28 -0700815 fuse_reply(fuse, unique, handler->read_buffer, res);
Brian Swetland03ee9472010-08-12 18:01:08 -0700816 return;
817 }
Jeff Brown84715842012-05-25 14:07:47 -0700818
Brian Swetland03ee9472010-08-12 18:01:08 -0700819 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
Jeff Brown84715842012-05-25 14:07:47 -0700820 const struct fuse_write_in *req = data;
821 const void* buffer = (const __u8*)data + sizeof(*req);
Brian Swetland03ee9472010-08-12 18:01:08 -0700822 struct fuse_write_out out;
823 struct handle *h = id_to_ptr(req->fh);
824 int res;
825 TRACE("WRITE %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
Jeff Brown84715842012-05-25 14:07:47 -0700826 res = pwrite64(h->fd, buffer, req->size, req->offset);
Brian Swetland03ee9472010-08-12 18:01:08 -0700827 if (res < 0) {
Yuncheol Heo2fc9fc72011-07-22 17:42:22 +0900828 fuse_status(fuse, hdr->unique, -errno);
Brian Swetland03ee9472010-08-12 18:01:08 -0700829 return;
830 }
831 out.size = res;
832 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Jeff Brown84715842012-05-25 14:07:47 -0700833 return;
Brian Swetland03ee9472010-08-12 18:01:08 -0700834 }
Jeff Brown84715842012-05-25 14:07:47 -0700835
Mike Lockwood4553b082010-08-16 14:14:44 -0400836 case FUSE_STATFS: { /* getattr_in -> attr_out */
837 struct statfs stat;
838 struct fuse_statfs_out out;
839 int res;
840
841 TRACE("STATFS\n");
842
843 if (statfs(fuse->root.name, &stat)) {
844 fuse_status(fuse, hdr->unique, -errno);
845 return;
846 }
847
848 memset(&out, 0, sizeof(out));
849 out.st.blocks = stat.f_blocks;
850 out.st.bfree = stat.f_bfree;
851 out.st.bavail = stat.f_bavail;
852 out.st.files = stat.f_files;
853 out.st.ffree = stat.f_ffree;
854 out.st.bsize = stat.f_bsize;
855 out.st.namelen = stat.f_namelen;
856 out.st.frsize = stat.f_frsize;
857 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
858 return;
859 }
Jeff Brown84715842012-05-25 14:07:47 -0700860
Brian Swetland03ee9472010-08-12 18:01:08 -0700861 case FUSE_RELEASE: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -0700862 const struct fuse_release_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700863 struct handle *h = id_to_ptr(req->fh);
864 TRACE("RELEASE %p(%d)\n", h, h->fd);
865 close(h->fd);
866 free(h);
867 fuse_status(fuse, hdr->unique, 0);
868 return;
869 }
Jeff Brown84715842012-05-25 14:07:47 -0700870
Jeff Brown6fd921a2012-05-25 15:01:21 -0700871 case FUSE_FSYNC: {
872 const struct fuse_fsync_in *req = data;
873 int is_data_sync = req->fsync_flags & 1;
874 struct handle *h = id_to_ptr(req->fh);
875 int res;
876 TRACE("FSYNC %p(%d) is_data_sync=%d\n", h, h->fd, is_data_sync);
877 res = is_data_sync ? fdatasync(h->fd) : fsync(h->fd);
878 if (res < 0) {
879 fuse_status(fuse, hdr->unique, -errno);
880 return;
881 }
882 fuse_status(fuse, hdr->unique, 0);
883 return;
884 }
885
Brian Swetland03ee9472010-08-12 18:01:08 -0700886// case FUSE_SETXATTR:
887// case FUSE_GETXATTR:
888// case FUSE_LISTXATTR:
889// case FUSE_REMOVEXATTR:
Jeff Brown84715842012-05-25 14:07:47 -0700890 case FUSE_FLUSH: {
Brian Swetland03ee9472010-08-12 18:01:08 -0700891 fuse_status(fuse, hdr->unique, 0);
892 return;
Jeff Brown84715842012-05-25 14:07:47 -0700893 }
894
Brian Swetland03ee9472010-08-12 18:01:08 -0700895 case FUSE_OPENDIR: { /* open_in -> open_out */
Jeff Brown84715842012-05-25 14:07:47 -0700896 const struct fuse_open_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700897 struct fuse_open_out out;
Jeff Brown7729d242012-05-25 15:35:28 -0700898 char *path, buffer[PATH_MAX];
Brian Swetland03ee9472010-08-12 18:01:08 -0700899 struct dirhandle *h;
900
901 h = malloc(sizeof(*h));
902 if (!h) {
903 fuse_status(fuse, hdr->unique, -ENOMEM);
904 return;
905 }
906
907 path = node_get_path(node, buffer, 0);
908 TRACE("OPENDIR %llx '%s'\n", hdr->nodeid, path);
909 h->d = opendir(path);
910 if (h->d == 0) {
911 ERROR("ERROR\n");
912 fuse_status(fuse, hdr->unique, -errno);
913 free(h);
914 return;
915 }
916 out.fh = ptr_to_id(h);
917 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
918 return;
919 }
Jeff Brown84715842012-05-25 14:07:47 -0700920
Brian Swetland03ee9472010-08-12 18:01:08 -0700921 case FUSE_READDIR: {
Jeff Brown84715842012-05-25 14:07:47 -0700922 const struct fuse_read_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700923 char buffer[8192];
924 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
925 struct dirent *de;
926 struct dirhandle *h = id_to_ptr(req->fh);
927 TRACE("READDIR %p\n", h);
Mike Lockwood75e17a82011-01-25 17:22:47 -0800928 if (req->offset == 0) {
929 /* rewinddir() might have been called above us, so rewind here too */
930 TRACE("calling rewinddir()\n");
931 rewinddir(h->d);
932 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700933 de = readdir(h->d);
934 if (!de) {
935 fuse_status(fuse, hdr->unique, 0);
936 return;
937 }
938 fde->ino = FUSE_UNKNOWN_INO;
Mike Lockwood75e17a82011-01-25 17:22:47 -0800939 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
940 fde->off = req->offset + 1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700941 fde->type = de->d_type;
942 fde->namelen = strlen(de->d_name);
943 memcpy(fde->name, de->d_name, fde->namelen + 1);
944 fuse_reply(fuse, hdr->unique, fde,
945 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
946 return;
947 }
Jeff Brown84715842012-05-25 14:07:47 -0700948
Brian Swetland03ee9472010-08-12 18:01:08 -0700949 case FUSE_RELEASEDIR: { /* release_in -> */
Jeff Brown84715842012-05-25 14:07:47 -0700950 const struct fuse_release_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700951 struct dirhandle *h = id_to_ptr(req->fh);
952 TRACE("RELEASEDIR %p\n",h);
953 closedir(h->d);
954 free(h);
955 fuse_status(fuse, hdr->unique, 0);
956 return;
957 }
Jeff Brown84715842012-05-25 14:07:47 -0700958
Brian Swetland03ee9472010-08-12 18:01:08 -0700959// case FUSE_FSYNCDIR:
960 case FUSE_INIT: { /* init_in -> init_out */
Jeff Brown84715842012-05-25 14:07:47 -0700961 const struct fuse_init_in *req = data;
Brian Swetland03ee9472010-08-12 18:01:08 -0700962 struct fuse_init_out out;
963
964 TRACE("INIT ver=%d.%d maxread=%d flags=%x\n",
965 req->major, req->minor, req->max_readahead, req->flags);
966
967 out.major = FUSE_KERNEL_VERSION;
968 out.minor = FUSE_KERNEL_MINOR_VERSION;
969 out.max_readahead = req->max_readahead;
Sundar Ramane5d32122012-02-09 10:31:10 -0600970 out.flags = FUSE_ATOMIC_O_TRUNC | FUSE_BIG_WRITES;
Brian Swetland03ee9472010-08-12 18:01:08 -0700971 out.max_background = 32;
972 out.congestion_threshold = 32;
Jeff Brown84715842012-05-25 14:07:47 -0700973 out.max_write = MAX_WRITE;
Brian Swetland03ee9472010-08-12 18:01:08 -0700974
975 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
976 return;
977 }
Jeff Brown84715842012-05-25 14:07:47 -0700978
Brian Swetland03ee9472010-08-12 18:01:08 -0700979 default: {
Brian Swetland03ee9472010-08-12 18:01:08 -0700980 ERROR("NOTIMPL op=%d uniq=%llx nid=%llx\n",
981 hdr->opcode, hdr->unique, hdr->nodeid);
Jeff Brown84715842012-05-25 14:07:47 -0700982 fuse_status(fuse, hdr->unique, -ENOSYS);
Brian Swetland03ee9472010-08-12 18:01:08 -0700983 break;
984 }
Jeff Brown84715842012-05-25 14:07:47 -0700985 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700986}
987
Jeff Brown7729d242012-05-25 15:35:28 -0700988void handle_fuse_requests(struct fuse *fuse, struct fuse_handler* handler)
Brian Swetland03ee9472010-08-12 18:01:08 -0700989{
Brian Swetland03ee9472010-08-12 18:01:08 -0700990 for (;;) {
Jeff Brown7729d242012-05-25 15:35:28 -0700991 ssize_t len = read(fuse->fd, handler->request_buffer, sizeof(handler->request_buffer));
Brian Swetland03ee9472010-08-12 18:01:08 -0700992 if (len < 0) {
993 if (errno == EINTR)
994 continue;
995 ERROR("handle_fuse_requests: errno=%d\n", errno);
996 return;
997 }
Jeff Brown84715842012-05-25 14:07:47 -0700998
999 if ((size_t)len < sizeof(struct fuse_in_header)) {
1000 ERROR("request too short: len=%zu\n", (size_t)len);
1001 return;
1002 }
1003
Jeff Brown7729d242012-05-25 15:35:28 -07001004 const struct fuse_in_header *hdr = (void*)handler->request_buffer;
Jeff Brown84715842012-05-25 14:07:47 -07001005 if (hdr->len != (size_t)len) {
1006 ERROR("malformed header: len=%zu, hdr->len=%u\n", (size_t)len, hdr->len);
1007 return;
1008 }
1009
Jeff Brown7729d242012-05-25 15:35:28 -07001010 const void *data = handler->request_buffer + sizeof(struct fuse_in_header);
Jeff Brown84715842012-05-25 14:07:47 -07001011 size_t data_len = len - sizeof(struct fuse_in_header);
Jeff Brown7729d242012-05-25 15:35:28 -07001012 handle_fuse_request(fuse, handler, hdr, data, data_len);
1013
1014 /* We do not access the request again after this point because the underlying
1015 * buffer storage may have been reused while processing the request. */
Brian Swetland03ee9472010-08-12 18:01:08 -07001016 }
1017}
1018
Jeff Brown7729d242012-05-25 15:35:28 -07001019int ignite_fuse(struct fuse* fuse)
1020{
1021 /* use only one handler thread for now */
1022 struct fuse_handler handler;
1023 handle_fuse_requests(fuse, &handler);
1024 return 0;
1025}
1026
Mike Lockwood4f35e622011-01-12 14:39:44 -05001027static int usage()
1028{
Jeff Brown26567352012-05-25 13:27:43 -07001029 ERROR("usage: sdcard <path> <uid> <gid>\n\n");
1030 return 1;
1031}
1032
1033static int run(const char* path, uid_t uid, gid_t gid)
1034{
1035 int fd;
1036 char opts[256];
1037 int res;
1038 struct fuse fuse;
1039
1040 /* cleanup from previous instance, if necessary */
1041 umount2(MOUNT_POINT, 2);
1042
1043 fd = open("/dev/fuse", O_RDWR);
1044 if (fd < 0){
1045 ERROR("cannot open fuse device (error %d)\n", errno);
1046 return -1;
1047 }
1048
1049 snprintf(opts, sizeof(opts),
1050 "fd=%i,rootmode=40000,default_permissions,allow_other,user_id=%d,group_id=%d",
1051 fd, uid, gid);
1052
1053 res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts);
1054 if (res < 0) {
1055 ERROR("cannot mount fuse filesystem (error %d)\n", errno);
1056 goto error;
1057 }
1058
1059 res = setgid(gid);
1060 if (res < 0) {
1061 ERROR("cannot setgid (error %d)\n", errno);
1062 goto error;
1063 }
1064
1065 res = setuid(uid);
1066 if (res < 0) {
1067 ERROR("cannot setuid (error %d)\n", errno);
1068 goto error;
1069 }
1070
1071 fuse_init(&fuse, fd, path);
1072
1073 umask(0);
Jeff Brown7729d242012-05-25 15:35:28 -07001074 res = ignite_fuse(&fuse);
Jeff Brown26567352012-05-25 13:27:43 -07001075
1076 /* we do not attempt to umount the file system here because we are no longer
1077 * running as the root user */
Jeff Brown26567352012-05-25 13:27:43 -07001078
1079error:
1080 close(fd);
1081 return res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001082}
1083
Brian Swetland03ee9472010-08-12 18:01:08 -07001084int main(int argc, char **argv)
1085{
Brian Swetland03ee9472010-08-12 18:01:08 -07001086 int fd;
1087 int res;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001088 const char *path = NULL;
Jeff Brown26567352012-05-25 13:27:43 -07001089 uid_t uid = 0;
1090 gid_t gid = 0;
Mike Lockwood4f35e622011-01-12 14:39:44 -05001091 int i;
Brian Swetland03ee9472010-08-12 18:01:08 -07001092
Mike Lockwood4f35e622011-01-12 14:39:44 -05001093 for (i = 1; i < argc; i++) {
1094 char* arg = argv[i];
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001095 if (!path)
1096 path = arg;
Jeff Brown26567352012-05-25 13:27:43 -07001097 else if (!uid)
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001098 uid = strtoul(arg, 0, 10);
Jeff Brown26567352012-05-25 13:27:43 -07001099 else if (!gid)
Mike Lockwood575a2bb2011-01-23 14:46:30 -08001100 gid = strtoul(arg, 0, 10);
1101 else {
1102 ERROR("too many arguments\n");
1103 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -05001104 }
Brian Swetland03ee9472010-08-12 18:01:08 -07001105 }
1106
Mike Lockwood4f35e622011-01-12 14:39:44 -05001107 if (!path) {
1108 ERROR("no path specified\n");
1109 return usage();
1110 }
Jeff Brown26567352012-05-25 13:27:43 -07001111 if (!uid || !gid) {
Brian Swetland03ee9472010-08-12 18:01:08 -07001112 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -05001113 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -07001114 }
1115
Jeff Brown26567352012-05-25 13:27:43 -07001116 res = run(path, uid, gid);
1117 return res < 0 ? 1 : 0;
Brian Swetland03ee9472010-08-12 18:01:08 -07001118}