blob: fd003a7fdc20b893f2bb9fb2493a9528a9dd401f [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>
Mike Lockwood1bedb732011-01-13 13:38:42 -050028#include <ctype.h>
Brian Swetland03ee9472010-08-12 18:01:08 -070029
Brian Swetlandb14a2c62010-08-12 18:21:12 -070030#include <private/android_filesystem_config.h>
31
Brian Swetland03ee9472010-08-12 18:01:08 -070032#include "fuse.h"
33
34/* README
35 *
36 * What is this?
37 *
38 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
39 * directory permissions (all files are given fixed owner, group, and
40 * permissions at creation, owner, group, and permissions are not
41 * changeable, symlinks and hardlinks are not createable, etc.
42 *
43 * usage: sdcard <path> <uid> <gid>
44 *
45 * It must be run as root, but will change to uid/gid as soon as it
Mike Lockwood4553b082010-08-16 14:14:44 -040046 * mounts a filesystem on /mnt/sdcard. It will refuse to run if uid or
Brian Swetland03ee9472010-08-12 18:01:08 -070047 * gid are zero.
48 *
49 *
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
58 *
Brian Swetland03ee9472010-08-12 18:01:08 -070059 */
60
61#define FUSE_TRACE 0
62
63#if FUSE_TRACE
64#define TRACE(x...) fprintf(stderr,x)
65#else
66#define TRACE(x...) do {} while (0)
67#endif
68
69#define ERROR(x...) fprintf(stderr,x)
70
71#define FUSE_UNKNOWN_INO 0xffffffff
72
Mike Lockwood4553b082010-08-16 14:14:44 -040073#define MOUNT_POINT "/mnt/sdcard"
74
Brian Swetland03ee9472010-08-12 18:01:08 -070075struct handle {
76 struct node *node;
77 int fd;
78};
79
80struct dirhandle {
81 struct node *node;
82 DIR *d;
83};
84
85struct node {
86 __u64 nid;
87 __u64 gen;
88
Paul Eastham11ccdb32010-10-14 11:04:26 -070089 struct node *next; /* per-dir sibling list */
90 struct node *child; /* first contained file by this dir */
91 struct node *all; /* global node list */
92 struct node *parent; /* containing directory */
Brian Swetland03ee9472010-08-12 18:01:08 -070093
94 __u32 refcount;
95 __u32 namelen;
96
Paul Eastham11ccdb32010-10-14 11:04:26 -070097 char *name;
Mike Lockwood575a2bb2011-01-23 14:46:30 -080098 /* If non-null, this is the real name of the file in the underlying storage.
99 * This may differ from the field "name" only by case.
100 * strlen(actual_name) will always equal strlen(name), so it is safe to use
101 * namelen for both fields.
102 */
103 char *actual_name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700104};
105
106struct fuse {
107 __u64 next_generation;
108 __u64 next_node_id;
109
110 int fd;
111
112 struct node *all;
113
114 struct node root;
115 char rootpath[1024];
116};
117
Mike Lockwood1bedb732011-01-13 13:38:42 -0500118static unsigned uid = -1;
119static unsigned gid = -1;
Mike Lockwood4f35e622011-01-12 14:39:44 -0500120
Brian Swetland03ee9472010-08-12 18:01:08 -0700121#define PATH_BUFFER_SIZE 1024
122
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800123#define NO_CASE_SENSITIVE_MATCH 0
124#define CASE_SENSITIVE_MATCH 1
Mike Lockwoodb94d3202011-01-17 21:06:26 -0800125
Paul Easthamf43219e2010-09-21 17:14:31 -0700126/*
127 * Get the real-life absolute path to a node.
128 * node: start at this node
129 * buf: storage for returned string
130 * name: append this string to path if set
131 */
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800132char *do_node_get_path(struct node *node, char *buf, const char *name, int match_case_insensitive)
Brian Swetland03ee9472010-08-12 18:01:08 -0700133{
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800134 struct node *in_node = node;
135 const char *in_name = name;
Brian Swetland03ee9472010-08-12 18:01:08 -0700136 char *out = buf + PATH_BUFFER_SIZE - 1;
137 int len;
138 out[0] = 0;
139
140 if (name) {
141 len = strlen(name);
142 goto start;
143 }
144
145 while (node) {
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800146 name = (node->actual_name ? node->actual_name : node->name);
Brian Swetland03ee9472010-08-12 18:01:08 -0700147 len = node->namelen;
148 node = node->parent;
149 start:
150 if ((len + 1) > (out - buf))
151 return 0;
152 out -= len;
153 memcpy(out, name, len);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800154 /* avoid double slash at beginning of path */
155 if (out[0] != '/') {
156 out --;
157 out[0] = '/';
158 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700159 }
160
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800161 /* If we are searching for a file within node (rather than computing node's path)
162 * and fail, then we need to look for a case insensitive match.
163 */
164 if (in_name && match_case_insensitive && access(out, F_OK) != 0) {
165 char *path, buffer[PATH_BUFFER_SIZE];
166 DIR* dir;
167 struct dirent* entry;
168 path = do_node_get_path(in_node, buffer, NULL, NO_CASE_SENSITIVE_MATCH);
169 dir = opendir(path);
170 if (!dir) {
171 ERROR("opendir %s failed: %s", path, strerror(errno));
172 return out;
173 }
174
175 while ((entry = readdir(dir))) {
176 if (!strcasecmp(entry->d_name, in_name)) {
177 /* we have a match - replace the name */
178 len = strlen(in_name);
179 memcpy(buf + PATH_BUFFER_SIZE - len - 1, entry->d_name, len);
180 break;
181 }
182 }
183 closedir(dir);
184 }
185
186 return out;
187}
188
189char *node_get_path(struct node *node, char *buf, const char *name)
190{
191 /* We look for case insensitive matches by default */
192 return do_node_get_path(node, buf, name, CASE_SENSITIVE_MATCH);
Brian Swetland03ee9472010-08-12 18:01:08 -0700193}
194
195void attr_from_stat(struct fuse_attr *attr, struct stat *s)
196{
197 attr->ino = s->st_ino;
198 attr->size = s->st_size;
199 attr->blocks = s->st_blocks;
Mike Lockwood4553b082010-08-16 14:14:44 -0400200 attr->atime = s->st_atime;
201 attr->mtime = s->st_mtime;
202 attr->ctime = s->st_ctime;
203 attr->atimensec = s->st_atime_nsec;
204 attr->mtimensec = s->st_mtime_nsec;
205 attr->ctimensec = s->st_ctime_nsec;
Brian Swetland03ee9472010-08-12 18:01:08 -0700206 attr->mode = s->st_mode;
207 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700208
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700209 /* force permissions to something reasonable:
210 * world readable
211 * writable by the sdcard group
212 */
213 if (attr->mode & 0100) {
214 attr->mode = (attr->mode & (~0777)) | 0775;
215 } else {
216 attr->mode = (attr->mode & (~0777)) | 0664;
217 }
218
219 /* all files owned by root.sdcard */
220 attr->uid = 0;
221 attr->gid = AID_SDCARD_RW;
Brian Swetland03ee9472010-08-12 18:01:08 -0700222}
223
224int node_get_attr(struct node *node, struct fuse_attr *attr)
225{
226 int res;
227 struct stat s;
228 char *path, buffer[PATH_BUFFER_SIZE];
229
230 path = node_get_path(node, buffer, 0);
231 res = lstat(path, &s);
232 if (res < 0) {
233 ERROR("lstat('%s') errno %d\n", path, errno);
234 return -1;
235 }
236
237 attr_from_stat(attr, &s);
238 attr->ino = node->nid;
239
240 return 0;
241}
242
Paul Eastham11ccdb32010-10-14 11:04:26 -0700243static void add_node_to_parent(struct node *node, struct node *parent) {
244 node->parent = parent;
245 node->next = parent->child;
246 parent->child = node;
Paul Eastham77085c52011-01-04 21:06:03 -0800247 parent->refcount++;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700248}
249
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800250/* Check to see if our parent directory already has a file with a name
251 * that differs only by case. If we find one, store it in the actual_name
252 * field so node_get_path will map it to this file in the underlying storage.
253 */
254static void node_find_actual_name(struct node *node)
255{
256 char *path, buffer[PATH_BUFFER_SIZE];
257 const char *node_name = node->name;
258 DIR* dir;
259 struct dirent* entry;
260
261 if (!node->parent) return;
262
263 path = node_get_path(node->parent, buffer, 0);
264 dir = opendir(path);
265 if (!dir) {
266 ERROR("opendir %s failed: %s", path, strerror(errno));
267 return;
268 }
269
270 while ((entry = readdir(dir))) {
271 const char *test_name = entry->d_name;
272 if (strcmp(test_name, node_name) && !strcasecmp(test_name, node_name)) {
273 /* we have a match - differs but only by case */
274 node->actual_name = strdup(test_name);
275 if (!node->actual_name) {
276 ERROR("strdup failed - out of memory\n");
277 exit(1);
278 }
279 break;
280 }
281 }
282 closedir(dir);
283}
284
Brian Swetland03ee9472010-08-12 18:01:08 -0700285struct node *node_create(struct node *parent, const char *name, __u64 nid, __u64 gen)
286{
287 struct node *node;
288 int namelen = strlen(name);
289
Paul Eastham11ccdb32010-10-14 11:04:26 -0700290 node = calloc(1, sizeof(struct node));
Brian Swetland03ee9472010-08-12 18:01:08 -0700291 if (node == 0) {
292 return 0;
293 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700294 node->name = malloc(namelen + 1);
295 if (node->name == 0) {
296 free(node);
297 return 0;
298 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700299
300 node->nid = nid;
301 node->gen = gen;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700302 add_node_to_parent(node, parent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700303 memcpy(node->name, name, namelen + 1);
304 node->namelen = namelen;
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800305 node_find_actual_name(node);
Brian Swetland03ee9472010-08-12 18:01:08 -0700306 return node;
307}
308
Paul Eastham11ccdb32010-10-14 11:04:26 -0700309static char *rename_node(struct node *node, const char *name)
310{
311 node->namelen = strlen(name);
312 char *newname = realloc(node->name, node->namelen + 1);
313 if (newname == 0)
314 return 0;
315 node->name = newname;
316 memcpy(node->name, name, node->namelen + 1);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800317 node_find_actual_name(node);
Paul Eastham11ccdb32010-10-14 11:04:26 -0700318 return node->name;
319}
320
Brian Swetland03ee9472010-08-12 18:01:08 -0700321void fuse_init(struct fuse *fuse, int fd, const char *path)
322{
323 fuse->fd = fd;
324 fuse->next_node_id = 2;
325 fuse->next_generation = 0;
326
327 fuse->all = &fuse->root;
328
Terry Heo (Woncheol)8349cce2011-03-16 13:02:05 +0900329 memset(&fuse->root, 0, sizeof(fuse->root));
Brian Swetland03ee9472010-08-12 18:01:08 -0700330 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
Brian Swetland03ee9472010-08-12 18:01:08 -0700331 fuse->root.refcount = 2;
Paul Eastham11ccdb32010-10-14 11:04:26 -0700332 rename_node(&fuse->root, path);
Brian Swetland03ee9472010-08-12 18:01:08 -0700333}
334
335static inline void *id_to_ptr(__u64 nid)
336{
337 return (void *) nid;
338}
339
340static inline __u64 ptr_to_id(void *ptr)
341{
342 return (__u64) ptr;
343}
344
345
346struct node *lookup_by_inode(struct fuse *fuse, __u64 nid)
347{
348 if (nid == FUSE_ROOT_ID) {
349 return &fuse->root;
350 } else {
351 return id_to_ptr(nid);
352 }
353}
354
355struct node *lookup_child_by_name(struct node *node, const char *name)
356{
357 for (node = node->child; node; node = node->next) {
358 if (!strcmp(name, node->name)) {
359 return node;
360 }
361 }
362 return 0;
363}
364
365struct node *lookup_child_by_inode(struct node *node, __u64 nid)
366{
367 for (node = node->child; node; node = node->next) {
368 if (node->nid == nid) {
369 return node;
370 }
371 }
372 return 0;
373}
374
Paul Eastham77085c52011-01-04 21:06:03 -0800375static void dec_refcount(struct node *node) {
376 if (node->refcount > 0) {
377 node->refcount--;
378 TRACE("dec_refcount %p(%s) -> %d\n", node, node->name, node->refcount);
379 } else {
380 ERROR("Zero refcnt %p\n", node);
381 }
382 }
383
Paul Eastham11ccdb32010-10-14 11:04:26 -0700384static struct node *remove_child(struct node *parent, __u64 nid)
385{
386 struct node *prev = 0;
387 struct node *node;
388
389 for (node = parent->child; node; node = node->next) {
390 if (node->nid == nid) {
391 if (prev) {
392 prev->next = node->next;
393 } else {
394 parent->child = node->next;
395 }
396 node->next = 0;
397 node->parent = 0;
Paul Eastham77085c52011-01-04 21:06:03 -0800398 dec_refcount(parent);
Paul Eastham11ccdb32010-10-14 11:04:26 -0700399 return node;
400 }
401 prev = node;
402 }
403 return 0;
404}
405
Brian Swetland03ee9472010-08-12 18:01:08 -0700406struct node *node_lookup(struct fuse *fuse, struct node *parent, const char *name,
407 struct fuse_attr *attr)
408{
409 int res;
410 struct stat s;
411 char *path, buffer[PATH_BUFFER_SIZE];
412 struct node *node;
413
414 path = node_get_path(parent, buffer, name);
415 /* XXX error? */
416
417 res = lstat(path, &s);
418 if (res < 0)
419 return 0;
420
421 node = lookup_child_by_name(parent, name);
422 if (!node) {
423 node = node_create(parent, name, fuse->next_node_id++, fuse->next_generation++);
424 if (!node)
425 return 0;
426 node->nid = ptr_to_id(node);
427 node->all = fuse->all;
428 fuse->all = node;
429 }
430
431 attr_from_stat(attr, &s);
432 attr->ino = node->nid;
433
434 return node;
435}
436
437void node_release(struct node *node)
438{
439 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
Paul Eastham77085c52011-01-04 21:06:03 -0800440 dec_refcount(node);
Brian Swetland03ee9472010-08-12 18:01:08 -0700441 if (node->refcount == 0) {
442 if (node->parent->child == node) {
443 node->parent->child = node->parent->child->next;
444 } else {
445 struct node *node2;
446
447 node2 = node->parent->child;
448 while (node2->next != node)
449 node2 = node2->next;
450 node2->next = node->next;
451 }
452
453 TRACE("DESTROY %p (%s)\n", node, node->name);
454
455 node_release(node->parent);
456
457 node->parent = 0;
458 node->next = 0;
459
460 /* TODO: remove debugging - poison memory */
Paul Eastham11ccdb32010-10-14 11:04:26 -0700461 memset(node->name, 0xef, node->namelen);
462 free(node->name);
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800463 free(node->actual_name);
Paul Eastham77085c52011-01-04 21:06:03 -0800464 memset(node, 0xfc, sizeof(*node));
Brian Swetland03ee9472010-08-12 18:01:08 -0700465 free(node);
466 }
467}
468
469void fuse_status(struct fuse *fuse, __u64 unique, int err)
470{
471 struct fuse_out_header hdr;
472 hdr.len = sizeof(hdr);
473 hdr.error = err;
474 hdr.unique = unique;
475 if (err) {
476// ERROR("*** %d ***\n", err);
477 }
478 write(fuse->fd, &hdr, sizeof(hdr));
479}
480
481void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
482{
483 struct fuse_out_header hdr;
484 struct iovec vec[2];
485 int res;
486
487 hdr.len = len + sizeof(hdr);
488 hdr.error = 0;
489 hdr.unique = unique;
490
491 vec[0].iov_base = &hdr;
492 vec[0].iov_len = sizeof(hdr);
493 vec[1].iov_base = data;
494 vec[1].iov_len = len;
495
496 res = writev(fuse->fd, vec, 2);
497 if (res < 0) {
498 ERROR("*** REPLY FAILED *** %d\n", errno);
499 }
500}
501
502void lookup_entry(struct fuse *fuse, struct node *node,
503 const char *name, __u64 unique)
504{
505 struct fuse_entry_out out;
506
507 memset(&out, 0, sizeof(out));
508
509 node = node_lookup(fuse, node, name, &out.attr);
510 if (!node) {
511 fuse_status(fuse, unique, -ENOENT);
512 return;
513 }
514
515 node->refcount++;
516// fprintf(stderr,"ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
517 out.nodeid = node->nid;
518 out.generation = node->gen;
519 out.entry_valid = 10;
520 out.attr_valid = 10;
521
522 fuse_reply(fuse, unique, &out, sizeof(out));
523}
524
525void handle_fuse_request(struct fuse *fuse, struct fuse_in_header *hdr, void *data, unsigned len)
526{
527 struct node *node;
528
529 if ((len < sizeof(*hdr)) || (hdr->len != len)) {
530 ERROR("malformed header\n");
531 return;
532 }
533
534 len -= hdr->len;
535
536 if (hdr->nodeid) {
537 node = lookup_by_inode(fuse, hdr->nodeid);
538 if (!node) {
539 fuse_status(fuse, hdr->unique, -ENOENT);
540 return;
541 }
542 } else {
543 node = 0;
544 }
545
546 switch (hdr->opcode) {
547 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
548 TRACE("LOOKUP %llx %s\n", hdr->nodeid, (char*) data);
549 lookup_entry(fuse, node, (char*) data, hdr->unique);
550 return;
551 }
552 case FUSE_FORGET: {
553 struct fuse_forget_in *req = data;
554 TRACE("FORGET %llx (%s) #%lld\n", hdr->nodeid, node->name, req->nlookup);
555 /* no reply */
556 while (req->nlookup--)
557 node_release(node);
558 return;
559 }
560 case FUSE_GETATTR: { /* getattr_in -> attr_out */
561 struct fuse_getattr_in *req = data;
562 struct fuse_attr_out out;
563
Paul Eastham11ccdb32010-10-14 11:04:26 -0700564 TRACE("GETATTR flags=%x fh=%llx\n", req->getattr_flags, req->fh);
Brian Swetland03ee9472010-08-12 18:01:08 -0700565
566 memset(&out, 0, sizeof(out));
567 node_get_attr(node, &out.attr);
568 out.attr_valid = 10;
569
570 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
571 return;
572 }
573 case FUSE_SETATTR: { /* setattr_in -> attr_out */
574 struct fuse_setattr_in *req = data;
575 struct fuse_attr_out out;
Paul Easthamf43219e2010-09-21 17:14:31 -0700576 char *path, buffer[PATH_BUFFER_SIZE];
577 int res = 0;
578
Brian Swetland03ee9472010-08-12 18:01:08 -0700579 TRACE("SETATTR fh=%llx id=%llx valid=%x\n",
580 req->fh, hdr->nodeid, req->valid);
581
Paul Easthamf43219e2010-09-21 17:14:31 -0700582 /* XXX: incomplete implementation -- truncate only. chmod/chown
583 * should NEVER be implemented. */
584
585 path = node_get_path(node, buffer, 0);
586 if (req->valid & FATTR_SIZE)
587 res = truncate(path, req->size);
Brian Swetland03ee9472010-08-12 18:01:08 -0700588
589 memset(&out, 0, sizeof(out));
590 node_get_attr(node, &out.attr);
591 out.attr_valid = 10;
Paul Easthamf43219e2010-09-21 17:14:31 -0700592
593 if (res)
594 fuse_status(fuse, hdr->unique, -errno);
595 else
596 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
Brian Swetland03ee9472010-08-12 18:01:08 -0700597 return;
598 }
599// case FUSE_READLINK:
600// case FUSE_SYMLINK:
601 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
602 struct fuse_mknod_in *req = data;
603 char *path, buffer[PATH_BUFFER_SIZE];
604 char *name = ((char*) data) + sizeof(*req);
605 int res;
Mike Lockwood51b3a2d2011-01-12 07:35:46 -0500606
Brian Swetland03ee9472010-08-12 18:01:08 -0700607 TRACE("MKNOD %s @ %llx\n", name, hdr->nodeid);
608 path = node_get_path(node, buffer, name);
609
610 req->mode = (req->mode & (~0777)) | 0664;
611 res = mknod(path, req->mode, req->rdev); /* XXX perm?*/
612 if (res < 0) {
613 fuse_status(fuse, hdr->unique, -errno);
614 } else {
615 lookup_entry(fuse, node, name, hdr->unique);
616 }
617 return;
618 }
619 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
620 struct fuse_mkdir_in *req = data;
621 struct fuse_entry_out out;
622 char *path, buffer[PATH_BUFFER_SIZE];
623 char *name = ((char*) data) + sizeof(*req);
624 int res;
Mike Lockwood51b3a2d2011-01-12 07:35:46 -0500625
Brian Swetland03ee9472010-08-12 18:01:08 -0700626 TRACE("MKDIR %s @ %llx 0%o\n", name, hdr->nodeid, req->mode);
627 path = node_get_path(node, buffer, name);
628
629 req->mode = (req->mode & (~0777)) | 0775;
630 res = mkdir(path, req->mode);
631 if (res < 0) {
632 fuse_status(fuse, hdr->unique, -errno);
633 } else {
634 lookup_entry(fuse, node, name, hdr->unique);
635 }
636 return;
637 }
638 case FUSE_UNLINK: { /* bytez[] -> */
639 char *path, buffer[PATH_BUFFER_SIZE];
640 int res;
641 TRACE("UNLINK %s @ %llx\n", (char*) data, hdr->nodeid);
642 path = node_get_path(node, buffer, (char*) data);
643 res = unlink(path);
644 fuse_status(fuse, hdr->unique, res ? -errno : 0);
645 return;
646 }
647 case FUSE_RMDIR: { /* bytez[] -> */
648 char *path, buffer[PATH_BUFFER_SIZE];
649 int res;
650 TRACE("RMDIR %s @ %llx\n", (char*) data, hdr->nodeid);
651 path = node_get_path(node, buffer, (char*) data);
652 res = rmdir(path);
653 fuse_status(fuse, hdr->unique, res ? -errno : 0);
654 return;
655 }
656 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
657 struct fuse_rename_in *req = data;
658 char *oldname = ((char*) data) + sizeof(*req);
659 char *newname = oldname + strlen(oldname) + 1;
660 char *oldpath, oldbuffer[PATH_BUFFER_SIZE];
661 char *newpath, newbuffer[PATH_BUFFER_SIZE];
Paul Eastham11ccdb32010-10-14 11:04:26 -0700662 struct node *target;
663 struct node *newparent;
Brian Swetland03ee9472010-08-12 18:01:08 -0700664 int res;
665
Paul Eastham11ccdb32010-10-14 11:04:26 -0700666 TRACE("RENAME %s->%s @ %llx\n", oldname, newname, hdr->nodeid);
667
668 target = lookup_child_by_name(node, oldname);
669 if (!target) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700670 fuse_status(fuse, hdr->unique, -ENOENT);
671 return;
672 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700673 oldpath = node_get_path(node, oldbuffer, oldname);
Paul Eastham11ccdb32010-10-14 11:04:26 -0700674
675 newparent = lookup_by_inode(fuse, req->newdir);
676 if (!newparent) {
677 fuse_status(fuse, hdr->unique, -ENOENT);
678 return;
679 }
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800680 if (newparent == node) {
681 /* Special case for renaming a file where destination
682 * is same path differing only by case.
683 * In this case we don't want to look for a case insensitive match.
684 * This allows commands like "mv foo FOO" to work as expected.
685 */
686 newpath = do_node_get_path(newparent, newbuffer, newname, NO_CASE_SENSITIVE_MATCH);
687 } else {
688 newpath = node_get_path(newparent, newbuffer, newname);
689 }
Paul Eastham11ccdb32010-10-14 11:04:26 -0700690
691 if (!remove_child(node, target->nid)) {
692 ERROR("RENAME remove_child not found");
693 fuse_status(fuse, hdr->unique, -ENOENT);
694 return;
695 }
696 if (!rename_node(target, newname)) {
697 fuse_status(fuse, hdr->unique, -ENOMEM);
698 return;
699 }
700 add_node_to_parent(target, newparent);
Brian Swetland03ee9472010-08-12 18:01:08 -0700701
702 res = rename(oldpath, newpath);
Paul Eastham11ccdb32010-10-14 11:04:26 -0700703 TRACE("RENAME result %d\n", res);
704
Brian Swetland03ee9472010-08-12 18:01:08 -0700705 fuse_status(fuse, hdr->unique, res ? -errno : 0);
706 return;
707 }
708// case FUSE_LINK:
709 case FUSE_OPEN: { /* open_in -> open_out */
710 struct fuse_open_in *req = data;
711 struct fuse_open_out out;
712 char *path, buffer[PATH_BUFFER_SIZE];
713 struct handle *h;
714
715 h = malloc(sizeof(*h));
716 if (!h) {
717 fuse_status(fuse, hdr->unique, -ENOMEM);
718 return;
719 }
720
721 path = node_get_path(node, buffer, 0);
722 TRACE("OPEN %llx '%s' 0%o fh=%p\n", hdr->nodeid, path, req->flags, h);
723 h->fd = open(path, req->flags);
724 if (h->fd < 0) {
725 ERROR("ERROR\n");
726 fuse_status(fuse, hdr->unique, errno);
727 free(h);
728 return;
729 }
730 out.fh = ptr_to_id(h);
731 out.open_flags = 0;
732 out.padding = 0;
733 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
734 return;
735 }
736 case FUSE_READ: { /* read_in -> byte[] */
737 char buffer[128 * 1024];
738 struct fuse_read_in *req = data;
739 struct handle *h = id_to_ptr(req->fh);
740 int res;
741 TRACE("READ %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
742 if (req->size > sizeof(buffer)) {
743 fuse_status(fuse, hdr->unique, -EINVAL);
744 return;
745 }
Kenny Root90749772011-01-11 15:38:31 -0800746 res = pread64(h->fd, buffer, req->size, req->offset);
Brian Swetland03ee9472010-08-12 18:01:08 -0700747 if (res < 0) {
748 fuse_status(fuse, hdr->unique, errno);
749 return;
750 }
751 fuse_reply(fuse, hdr->unique, buffer, res);
752 return;
753 }
754 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
755 struct fuse_write_in *req = data;
756 struct fuse_write_out out;
757 struct handle *h = id_to_ptr(req->fh);
758 int res;
759 TRACE("WRITE %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
Kenny Root90749772011-01-11 15:38:31 -0800760 res = pwrite64(h->fd, ((char*) data) + sizeof(*req), req->size, req->offset);
Brian Swetland03ee9472010-08-12 18:01:08 -0700761 if (res < 0) {
762 fuse_status(fuse, hdr->unique, errno);
763 return;
764 }
765 out.size = res;
766 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
767 goto oops;
768 }
Mike Lockwood4553b082010-08-16 14:14:44 -0400769 case FUSE_STATFS: { /* getattr_in -> attr_out */
770 struct statfs stat;
771 struct fuse_statfs_out out;
772 int res;
773
774 TRACE("STATFS\n");
775
776 if (statfs(fuse->root.name, &stat)) {
777 fuse_status(fuse, hdr->unique, -errno);
778 return;
779 }
780
781 memset(&out, 0, sizeof(out));
782 out.st.blocks = stat.f_blocks;
783 out.st.bfree = stat.f_bfree;
784 out.st.bavail = stat.f_bavail;
785 out.st.files = stat.f_files;
786 out.st.ffree = stat.f_ffree;
787 out.st.bsize = stat.f_bsize;
788 out.st.namelen = stat.f_namelen;
789 out.st.frsize = stat.f_frsize;
790 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
791 return;
792 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700793 case FUSE_RELEASE: { /* release_in -> */
794 struct fuse_release_in *req = data;
795 struct handle *h = id_to_ptr(req->fh);
796 TRACE("RELEASE %p(%d)\n", h, h->fd);
797 close(h->fd);
798 free(h);
799 fuse_status(fuse, hdr->unique, 0);
800 return;
801 }
802// case FUSE_FSYNC:
803// case FUSE_SETXATTR:
804// case FUSE_GETXATTR:
805// case FUSE_LISTXATTR:
806// case FUSE_REMOVEXATTR:
807 case FUSE_FLUSH:
808 fuse_status(fuse, hdr->unique, 0);
809 return;
810 case FUSE_OPENDIR: { /* open_in -> open_out */
811 struct fuse_open_in *req = data;
812 struct fuse_open_out out;
813 char *path, buffer[PATH_BUFFER_SIZE];
814 struct dirhandle *h;
815
816 h = malloc(sizeof(*h));
817 if (!h) {
818 fuse_status(fuse, hdr->unique, -ENOMEM);
819 return;
820 }
821
822 path = node_get_path(node, buffer, 0);
823 TRACE("OPENDIR %llx '%s'\n", hdr->nodeid, path);
824 h->d = opendir(path);
825 if (h->d == 0) {
826 ERROR("ERROR\n");
827 fuse_status(fuse, hdr->unique, -errno);
828 free(h);
829 return;
830 }
831 out.fh = ptr_to_id(h);
832 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
833 return;
834 }
835 case FUSE_READDIR: {
836 struct fuse_read_in *req = data;
837 char buffer[8192];
838 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
839 struct dirent *de;
840 struct dirhandle *h = id_to_ptr(req->fh);
841 TRACE("READDIR %p\n", h);
Mike Lockwood75e17a82011-01-25 17:22:47 -0800842 if (req->offset == 0) {
843 /* rewinddir() might have been called above us, so rewind here too */
844 TRACE("calling rewinddir()\n");
845 rewinddir(h->d);
846 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700847 de = readdir(h->d);
848 if (!de) {
849 fuse_status(fuse, hdr->unique, 0);
850 return;
851 }
852 fde->ino = FUSE_UNKNOWN_INO;
Mike Lockwood75e17a82011-01-25 17:22:47 -0800853 /* increment the offset so we can detect when rewinddir() seeks back to the beginning */
854 fde->off = req->offset + 1;
Brian Swetland03ee9472010-08-12 18:01:08 -0700855 fde->type = de->d_type;
856 fde->namelen = strlen(de->d_name);
857 memcpy(fde->name, de->d_name, fde->namelen + 1);
858 fuse_reply(fuse, hdr->unique, fde,
859 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
860 return;
861 }
862 case FUSE_RELEASEDIR: { /* release_in -> */
863 struct fuse_release_in *req = data;
864 struct dirhandle *h = id_to_ptr(req->fh);
865 TRACE("RELEASEDIR %p\n",h);
866 closedir(h->d);
867 free(h);
868 fuse_status(fuse, hdr->unique, 0);
869 return;
870 }
871// case FUSE_FSYNCDIR:
872 case FUSE_INIT: { /* init_in -> init_out */
873 struct fuse_init_in *req = data;
874 struct fuse_init_out out;
875
876 TRACE("INIT ver=%d.%d maxread=%d flags=%x\n",
877 req->major, req->minor, req->max_readahead, req->flags);
878
879 out.major = FUSE_KERNEL_VERSION;
880 out.minor = FUSE_KERNEL_MINOR_VERSION;
881 out.max_readahead = req->max_readahead;
Mike Lockwoodfc1a13b2010-08-20 10:29:29 -0400882 out.flags = FUSE_ATOMIC_O_TRUNC;
Brian Swetland03ee9472010-08-12 18:01:08 -0700883 out.max_background = 32;
884 out.congestion_threshold = 32;
885 out.max_write = 256 * 1024;
886
887 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
888 return;
889 }
890 default: {
891 struct fuse_out_header h;
892 ERROR("NOTIMPL op=%d uniq=%llx nid=%llx\n",
893 hdr->opcode, hdr->unique, hdr->nodeid);
894
895 oops:
896 h.len = sizeof(h);
897 h.error = -ENOSYS;
898 h.unique = hdr->unique;
899 write(fuse->fd, &h, sizeof(h));
900 break;
901 }
902 }
903}
904
905void handle_fuse_requests(struct fuse *fuse)
906{
907 unsigned char req[256 * 1024 + 128];
908 int len;
909
910 for (;;) {
911 len = read(fuse->fd, req, 8192);
912 if (len < 0) {
913 if (errno == EINTR)
914 continue;
915 ERROR("handle_fuse_requests: errno=%d\n", errno);
916 return;
917 }
918 handle_fuse_request(fuse, (void*) req, (void*) (req + sizeof(struct fuse_in_header)), len);
919 }
920}
921
Mike Lockwood4f35e622011-01-12 14:39:44 -0500922static int usage()
923{
Mike Lockwood1bedb732011-01-13 13:38:42 -0500924 ERROR("usage: sdcard [-l -f] <path> <uid> <gid>\n\n\t-l force file names to lower case when creating new files\n\t-f fix up file system before starting (repairs bad file name case and group ownership)\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -0500925 return -1;
926}
927
Brian Swetland03ee9472010-08-12 18:01:08 -0700928int main(int argc, char **argv)
929{
930 struct fuse fuse;
931 char opts[256];
932 int fd;
933 int res;
Mike Lockwood4f35e622011-01-12 14:39:44 -0500934 const char *path = NULL;
Mike Lockwood4f35e622011-01-12 14:39:44 -0500935 int i;
Brian Swetland03ee9472010-08-12 18:01:08 -0700936
Mike Lockwood4f35e622011-01-12 14:39:44 -0500937 for (i = 1; i < argc; i++) {
938 char* arg = argv[i];
Mike Lockwood575a2bb2011-01-23 14:46:30 -0800939 if (!path)
940 path = arg;
941 else if (uid == -1)
942 uid = strtoul(arg, 0, 10);
943 else if (gid == -1)
944 gid = strtoul(arg, 0, 10);
945 else {
946 ERROR("too many arguments\n");
947 return usage();
Mike Lockwood4f35e622011-01-12 14:39:44 -0500948 }
Brian Swetland03ee9472010-08-12 18:01:08 -0700949 }
950
Mike Lockwood4f35e622011-01-12 14:39:44 -0500951 if (!path) {
952 ERROR("no path specified\n");
953 return usage();
954 }
955 if (uid <= 0 || gid <= 0) {
Brian Swetland03ee9472010-08-12 18:01:08 -0700956 ERROR("uid and gid must be nonzero\n");
Mike Lockwood4f35e622011-01-12 14:39:44 -0500957 return usage();
Brian Swetland03ee9472010-08-12 18:01:08 -0700958 }
959
Brian Swetland03ee9472010-08-12 18:01:08 -0700960 /* cleanup from previous instance, if necessary */
Mike Lockwood4553b082010-08-16 14:14:44 -0400961 umount2(MOUNT_POINT, 2);
Brian Swetland03ee9472010-08-12 18:01:08 -0700962
963 fd = open("/dev/fuse", O_RDWR);
964 if (fd < 0){
965 ERROR("cannot open fuse device (%d)\n", errno);
966 return -1;
967 }
968
969 sprintf(opts, "fd=%i,rootmode=40000,default_permissions,allow_other,"
970 "user_id=%d,group_id=%d", fd, uid, gid);
971
Mike Lockwood4553b082010-08-16 14:14:44 -0400972 res = mount("/dev/fuse", MOUNT_POINT, "fuse", MS_NOSUID | MS_NODEV, opts);
Brian Swetland03ee9472010-08-12 18:01:08 -0700973 if (res < 0) {
974 ERROR("cannot mount fuse filesystem (%d)\n", errno);
975 return -1;
976 }
977
978 if (setgid(gid) < 0) {
979 ERROR("cannot setgid!\n");
980 return -1;
981 }
982 if (setuid(uid) < 0) {
983 ERROR("cannot setuid!\n");
984 return -1;
985 }
986
Brian Swetland03ee9472010-08-12 18:01:08 -0700987 fuse_init(&fuse, fd, path);
988
989 umask(0);
990 handle_fuse_requests(&fuse);
991
992 return 0;
993}