blob: a534dae8dba8bd54575f94f6c1bd98f6e7d7d3fa [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>
25#include <sys/uio.h>
26#include <dirent.h>
27
Brian Swetlandb14a2c62010-08-12 18:21:12 -070028#include <private/android_filesystem_config.h>
29
Brian Swetland03ee9472010-08-12 18:01:08 -070030#include "fuse.h"
31
32/* README
33 *
34 * What is this?
35 *
36 * sdcard is a program that uses FUSE to emulate FAT-on-sdcard style
37 * directory permissions (all files are given fixed owner, group, and
38 * permissions at creation, owner, group, and permissions are not
39 * changeable, symlinks and hardlinks are not createable, etc.
40 *
41 * usage: sdcard <path> <uid> <gid>
42 *
43 * It must be run as root, but will change to uid/gid as soon as it
44 * mounts a filesystem on /sdcard. It will refuse to run if uid or
45 * gid are zero.
46 *
47 *
48 * Things I believe to be true:
49 *
50 * - ops that return a fuse_entry (LOOKUP, MKNOD, MKDIR, LINK, SYMLINK,
51 * CREAT) must bump that node's refcount
52 * - don't forget that FORGET can forget multiple references (req->nlookup)
53 * - if an op that returns a fuse_entry fails writing the reply to the
54 * kernel, you must rollback the refcount to reflect the reference the
55 * kernel did not actually acquire
56 *
57 *
58 * Bugs:
59 *
60 * - need to move/rename node on RENAME
61 */
62
63#define FUSE_TRACE 0
64
65#if FUSE_TRACE
66#define TRACE(x...) fprintf(stderr,x)
67#else
68#define TRACE(x...) do {} while (0)
69#endif
70
71#define ERROR(x...) fprintf(stderr,x)
72
73#define FUSE_UNKNOWN_INO 0xffffffff
74
75struct 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
89 struct node *next;
90 struct node *child;
91 struct node *all;
92 struct node *parent;
93
94 __u32 refcount;
95 __u32 namelen;
96
97 char name[1];
98};
99
100struct fuse {
101 __u64 next_generation;
102 __u64 next_node_id;
103
104 int fd;
105
106 struct node *all;
107
108 struct node root;
109 char rootpath[1024];
110};
111
112#define PATH_BUFFER_SIZE 1024
113
114char *node_get_path(struct node *node, char *buf, const char *name)
115{
116 char *out = buf + PATH_BUFFER_SIZE - 1;
117 int len;
118 out[0] = 0;
119
120 if (name) {
121 len = strlen(name);
122 goto start;
123 }
124
125 while (node) {
126 name = node->name;
127 len = node->namelen;
128 node = node->parent;
129 start:
130 if ((len + 1) > (out - buf))
131 return 0;
132 out -= len;
133 memcpy(out, name, len);
134 out --;
135 out[0] = '/';
136 }
137
138 return out;
139}
140
141void attr_from_stat(struct fuse_attr *attr, struct stat *s)
142{
143 attr->ino = s->st_ino;
144 attr->size = s->st_size;
145 attr->blocks = s->st_blocks;
146 /* TODO: time */
147 attr->mode = s->st_mode;
148 attr->nlink = s->st_nlink;
Brian Swetland03ee9472010-08-12 18:01:08 -0700149
Brian Swetlandb14a2c62010-08-12 18:21:12 -0700150 /* force permissions to something reasonable:
151 * world readable
152 * writable by the sdcard group
153 */
154 if (attr->mode & 0100) {
155 attr->mode = (attr->mode & (~0777)) | 0775;
156 } else {
157 attr->mode = (attr->mode & (~0777)) | 0664;
158 }
159
160 /* all files owned by root.sdcard */
161 attr->uid = 0;
162 attr->gid = AID_SDCARD_RW;
Brian Swetland03ee9472010-08-12 18:01:08 -0700163}
164
165int node_get_attr(struct node *node, struct fuse_attr *attr)
166{
167 int res;
168 struct stat s;
169 char *path, buffer[PATH_BUFFER_SIZE];
170
171 path = node_get_path(node, buffer, 0);
172 res = lstat(path, &s);
173 if (res < 0) {
174 ERROR("lstat('%s') errno %d\n", path, errno);
175 return -1;
176 }
177
178 attr_from_stat(attr, &s);
179 attr->ino = node->nid;
180
181 return 0;
182}
183
184struct node *node_create(struct node *parent, const char *name, __u64 nid, __u64 gen)
185{
186 struct node *node;
187 int namelen = strlen(name);
188
189 node = calloc(1, sizeof(struct node) + namelen);
190 if (node == 0) {
191 return 0;
192 }
193
194 node->nid = nid;
195 node->gen = gen;
196 node->parent = parent;
197 node->next = parent->child;
198 parent->child = node;
199 memcpy(node->name, name, namelen + 1);
200 node->namelen = namelen;
201 parent->refcount++;
202
203 return node;
204}
205
206void fuse_init(struct fuse *fuse, int fd, const char *path)
207{
208 fuse->fd = fd;
209 fuse->next_node_id = 2;
210 fuse->next_generation = 0;
211
212 fuse->all = &fuse->root;
213
214 fuse->root.nid = FUSE_ROOT_ID; /* 1 */
215 fuse->root.next = 0;
216 fuse->root.child = 0;
217 fuse->root.parent = 0;
218
219 fuse->root.all = 0;
220 fuse->root.refcount = 2;
221
222 strcpy(fuse->root.name, path);
223 fuse->root.namelen = strlen(fuse->root.name);
224}
225
226static inline void *id_to_ptr(__u64 nid)
227{
228 return (void *) nid;
229}
230
231static inline __u64 ptr_to_id(void *ptr)
232{
233 return (__u64) ptr;
234}
235
236
237struct node *lookup_by_inode(struct fuse *fuse, __u64 nid)
238{
239 if (nid == FUSE_ROOT_ID) {
240 return &fuse->root;
241 } else {
242 return id_to_ptr(nid);
243 }
244}
245
246struct node *lookup_child_by_name(struct node *node, const char *name)
247{
248 for (node = node->child; node; node = node->next) {
249 if (!strcmp(name, node->name)) {
250 return node;
251 }
252 }
253 return 0;
254}
255
256struct node *lookup_child_by_inode(struct node *node, __u64 nid)
257{
258 for (node = node->child; node; node = node->next) {
259 if (node->nid == nid) {
260 return node;
261 }
262 }
263 return 0;
264}
265
266struct node *node_lookup(struct fuse *fuse, struct node *parent, const char *name,
267 struct fuse_attr *attr)
268{
269 int res;
270 struct stat s;
271 char *path, buffer[PATH_BUFFER_SIZE];
272 struct node *node;
273
274 path = node_get_path(parent, buffer, name);
275 /* XXX error? */
276
277 res = lstat(path, &s);
278 if (res < 0)
279 return 0;
280
281 node = lookup_child_by_name(parent, name);
282 if (!node) {
283 node = node_create(parent, name, fuse->next_node_id++, fuse->next_generation++);
284 if (!node)
285 return 0;
286 node->nid = ptr_to_id(node);
287 node->all = fuse->all;
288 fuse->all = node;
289 }
290
291 attr_from_stat(attr, &s);
292 attr->ino = node->nid;
293
294 return node;
295}
296
297void node_release(struct node *node)
298{
299 TRACE("RELEASE %p (%s) rc=%d\n", node, node->name, node->refcount);
300 node->refcount--;
301 if (node->refcount == 0) {
302 if (node->parent->child == node) {
303 node->parent->child = node->parent->child->next;
304 } else {
305 struct node *node2;
306
307 node2 = node->parent->child;
308 while (node2->next != node)
309 node2 = node2->next;
310 node2->next = node->next;
311 }
312
313 TRACE("DESTROY %p (%s)\n", node, node->name);
314
315 node_release(node->parent);
316
317 node->parent = 0;
318 node->next = 0;
319
320 /* TODO: remove debugging - poison memory */
321 memset(node, 0xef, sizeof(*node) + strlen(node->name));
322
323 free(node);
324 }
325}
326
327void fuse_status(struct fuse *fuse, __u64 unique, int err)
328{
329 struct fuse_out_header hdr;
330 hdr.len = sizeof(hdr);
331 hdr.error = err;
332 hdr.unique = unique;
333 if (err) {
334// ERROR("*** %d ***\n", err);
335 }
336 write(fuse->fd, &hdr, sizeof(hdr));
337}
338
339void fuse_reply(struct fuse *fuse, __u64 unique, void *data, int len)
340{
341 struct fuse_out_header hdr;
342 struct iovec vec[2];
343 int res;
344
345 hdr.len = len + sizeof(hdr);
346 hdr.error = 0;
347 hdr.unique = unique;
348
349 vec[0].iov_base = &hdr;
350 vec[0].iov_len = sizeof(hdr);
351 vec[1].iov_base = data;
352 vec[1].iov_len = len;
353
354 res = writev(fuse->fd, vec, 2);
355 if (res < 0) {
356 ERROR("*** REPLY FAILED *** %d\n", errno);
357 }
358}
359
360void lookup_entry(struct fuse *fuse, struct node *node,
361 const char *name, __u64 unique)
362{
363 struct fuse_entry_out out;
364
365 memset(&out, 0, sizeof(out));
366
367 node = node_lookup(fuse, node, name, &out.attr);
368 if (!node) {
369 fuse_status(fuse, unique, -ENOENT);
370 return;
371 }
372
373 node->refcount++;
374// fprintf(stderr,"ACQUIRE %p (%s) rc=%d\n", node, node->name, node->refcount);
375 out.nodeid = node->nid;
376 out.generation = node->gen;
377 out.entry_valid = 10;
378 out.attr_valid = 10;
379
380 fuse_reply(fuse, unique, &out, sizeof(out));
381}
382
383void handle_fuse_request(struct fuse *fuse, struct fuse_in_header *hdr, void *data, unsigned len)
384{
385 struct node *node;
386
387 if ((len < sizeof(*hdr)) || (hdr->len != len)) {
388 ERROR("malformed header\n");
389 return;
390 }
391
392 len -= hdr->len;
393
394 if (hdr->nodeid) {
395 node = lookup_by_inode(fuse, hdr->nodeid);
396 if (!node) {
397 fuse_status(fuse, hdr->unique, -ENOENT);
398 return;
399 }
400 } else {
401 node = 0;
402 }
403
404 switch (hdr->opcode) {
405 case FUSE_LOOKUP: { /* bytez[] -> entry_out */
406 TRACE("LOOKUP %llx %s\n", hdr->nodeid, (char*) data);
407 lookup_entry(fuse, node, (char*) data, hdr->unique);
408 return;
409 }
410 case FUSE_FORGET: {
411 struct fuse_forget_in *req = data;
412 TRACE("FORGET %llx (%s) #%lld\n", hdr->nodeid, node->name, req->nlookup);
413 /* no reply */
414 while (req->nlookup--)
415 node_release(node);
416 return;
417 }
418 case FUSE_GETATTR: { /* getattr_in -> attr_out */
419 struct fuse_getattr_in *req = data;
420 struct fuse_attr_out out;
421
422 TRACE("GETATTR flags=%x fh=%llx\n",req->getattr_flags, req->fh);
423
424 memset(&out, 0, sizeof(out));
425 node_get_attr(node, &out.attr);
426 out.attr_valid = 10;
427
428 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
429 return;
430 }
431 case FUSE_SETATTR: { /* setattr_in -> attr_out */
432 struct fuse_setattr_in *req = data;
433 struct fuse_attr_out out;
434 TRACE("SETATTR fh=%llx id=%llx valid=%x\n",
435 req->fh, hdr->nodeid, req->valid);
436
437 /* XXX */
438
439 memset(&out, 0, sizeof(out));
440 node_get_attr(node, &out.attr);
441 out.attr_valid = 10;
442 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
443 return;
444 }
445// case FUSE_READLINK:
446// case FUSE_SYMLINK:
447 case FUSE_MKNOD: { /* mknod_in, bytez[] -> entry_out */
448 struct fuse_mknod_in *req = data;
449 char *path, buffer[PATH_BUFFER_SIZE];
450 char *name = ((char*) data) + sizeof(*req);
451 int res;
452 TRACE("MKNOD %s @ %llx\n", name, hdr->nodeid);
453 path = node_get_path(node, buffer, name);
454
455 req->mode = (req->mode & (~0777)) | 0664;
456 res = mknod(path, req->mode, req->rdev); /* XXX perm?*/
457 if (res < 0) {
458 fuse_status(fuse, hdr->unique, -errno);
459 } else {
460 lookup_entry(fuse, node, name, hdr->unique);
461 }
462 return;
463 }
464 case FUSE_MKDIR: { /* mkdir_in, bytez[] -> entry_out */
465 struct fuse_mkdir_in *req = data;
466 struct fuse_entry_out out;
467 char *path, buffer[PATH_BUFFER_SIZE];
468 char *name = ((char*) data) + sizeof(*req);
469 int res;
470 TRACE("MKDIR %s @ %llx 0%o\n", name, hdr->nodeid, req->mode);
471 path = node_get_path(node, buffer, name);
472
473 req->mode = (req->mode & (~0777)) | 0775;
474 res = mkdir(path, req->mode);
475 if (res < 0) {
476 fuse_status(fuse, hdr->unique, -errno);
477 } else {
478 lookup_entry(fuse, node, name, hdr->unique);
479 }
480 return;
481 }
482 case FUSE_UNLINK: { /* bytez[] -> */
483 char *path, buffer[PATH_BUFFER_SIZE];
484 int res;
485 TRACE("UNLINK %s @ %llx\n", (char*) data, hdr->nodeid);
486 path = node_get_path(node, buffer, (char*) data);
487 res = unlink(path);
488 fuse_status(fuse, hdr->unique, res ? -errno : 0);
489 return;
490 }
491 case FUSE_RMDIR: { /* bytez[] -> */
492 char *path, buffer[PATH_BUFFER_SIZE];
493 int res;
494 TRACE("RMDIR %s @ %llx\n", (char*) data, hdr->nodeid);
495 path = node_get_path(node, buffer, (char*) data);
496 res = rmdir(path);
497 fuse_status(fuse, hdr->unique, res ? -errno : 0);
498 return;
499 }
500 case FUSE_RENAME: { /* rename_in, oldname, newname -> */
501 struct fuse_rename_in *req = data;
502 char *oldname = ((char*) data) + sizeof(*req);
503 char *newname = oldname + strlen(oldname) + 1;
504 char *oldpath, oldbuffer[PATH_BUFFER_SIZE];
505 char *newpath, newbuffer[PATH_BUFFER_SIZE];
506 struct node *newnode;
507 int res;
508
509 newnode = lookup_by_inode(fuse, req->newdir);
510 if (!newnode) {
511 fuse_status(fuse, hdr->unique, -ENOENT);
512 return;
513 }
514
515 oldpath = node_get_path(node, oldbuffer, oldname);
516 newpath = node_get_path(newnode, newbuffer, newname);
517
518 res = rename(oldpath, newpath);
519 fuse_status(fuse, hdr->unique, res ? -errno : 0);
520 return;
521 }
522// case FUSE_LINK:
523 case FUSE_OPEN: { /* open_in -> open_out */
524 struct fuse_open_in *req = data;
525 struct fuse_open_out out;
526 char *path, buffer[PATH_BUFFER_SIZE];
527 struct handle *h;
528
529 h = malloc(sizeof(*h));
530 if (!h) {
531 fuse_status(fuse, hdr->unique, -ENOMEM);
532 return;
533 }
534
535 path = node_get_path(node, buffer, 0);
536 TRACE("OPEN %llx '%s' 0%o fh=%p\n", hdr->nodeid, path, req->flags, h);
537 h->fd = open(path, req->flags);
538 if (h->fd < 0) {
539 ERROR("ERROR\n");
540 fuse_status(fuse, hdr->unique, errno);
541 free(h);
542 return;
543 }
544 out.fh = ptr_to_id(h);
545 out.open_flags = 0;
546 out.padding = 0;
547 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
548 return;
549 }
550 case FUSE_READ: { /* read_in -> byte[] */
551 char buffer[128 * 1024];
552 struct fuse_read_in *req = data;
553 struct handle *h = id_to_ptr(req->fh);
554 int res;
555 TRACE("READ %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
556 if (req->size > sizeof(buffer)) {
557 fuse_status(fuse, hdr->unique, -EINVAL);
558 return;
559 }
560 res = pread(h->fd, buffer, req->size, req->offset);
561 if (res < 0) {
562 fuse_status(fuse, hdr->unique, errno);
563 return;
564 }
565 fuse_reply(fuse, hdr->unique, buffer, res);
566 return;
567 }
568 case FUSE_WRITE: { /* write_in, byte[write_in.size] -> write_out */
569 struct fuse_write_in *req = data;
570 struct fuse_write_out out;
571 struct handle *h = id_to_ptr(req->fh);
572 int res;
573 TRACE("WRITE %p(%d) %u@%llu\n", h, h->fd, req->size, req->offset);
574 res = pwrite(h->fd, ((char*) data) + sizeof(*req), req->size, req->offset);
575 if (res < 0) {
576 fuse_status(fuse, hdr->unique, errno);
577 return;
578 }
579 out.size = res;
580 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
581 goto oops;
582 }
583// case FUSE_STATFS:
584 case FUSE_RELEASE: { /* release_in -> */
585 struct fuse_release_in *req = data;
586 struct handle *h = id_to_ptr(req->fh);
587 TRACE("RELEASE %p(%d)\n", h, h->fd);
588 close(h->fd);
589 free(h);
590 fuse_status(fuse, hdr->unique, 0);
591 return;
592 }
593// case FUSE_FSYNC:
594// case FUSE_SETXATTR:
595// case FUSE_GETXATTR:
596// case FUSE_LISTXATTR:
597// case FUSE_REMOVEXATTR:
598 case FUSE_FLUSH:
599 fuse_status(fuse, hdr->unique, 0);
600 return;
601 case FUSE_OPENDIR: { /* open_in -> open_out */
602 struct fuse_open_in *req = data;
603 struct fuse_open_out out;
604 char *path, buffer[PATH_BUFFER_SIZE];
605 struct dirhandle *h;
606
607 h = malloc(sizeof(*h));
608 if (!h) {
609 fuse_status(fuse, hdr->unique, -ENOMEM);
610 return;
611 }
612
613 path = node_get_path(node, buffer, 0);
614 TRACE("OPENDIR %llx '%s'\n", hdr->nodeid, path);
615 h->d = opendir(path);
616 if (h->d == 0) {
617 ERROR("ERROR\n");
618 fuse_status(fuse, hdr->unique, -errno);
619 free(h);
620 return;
621 }
622 out.fh = ptr_to_id(h);
623 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
624 return;
625 }
626 case FUSE_READDIR: {
627 struct fuse_read_in *req = data;
628 char buffer[8192];
629 struct fuse_dirent *fde = (struct fuse_dirent*) buffer;
630 struct dirent *de;
631 struct dirhandle *h = id_to_ptr(req->fh);
632 TRACE("READDIR %p\n", h);
633 de = readdir(h->d);
634 if (!de) {
635 fuse_status(fuse, hdr->unique, 0);
636 return;
637 }
638 fde->ino = FUSE_UNKNOWN_INO;
639 fde->off = 0;
640 fde->type = de->d_type;
641 fde->namelen = strlen(de->d_name);
642 memcpy(fde->name, de->d_name, fde->namelen + 1);
643 fuse_reply(fuse, hdr->unique, fde,
644 FUSE_DIRENT_ALIGN(sizeof(struct fuse_dirent) + fde->namelen));
645 return;
646 }
647 case FUSE_RELEASEDIR: { /* release_in -> */
648 struct fuse_release_in *req = data;
649 struct dirhandle *h = id_to_ptr(req->fh);
650 TRACE("RELEASEDIR %p\n",h);
651 closedir(h->d);
652 free(h);
653 fuse_status(fuse, hdr->unique, 0);
654 return;
655 }
656// case FUSE_FSYNCDIR:
657 case FUSE_INIT: { /* init_in -> init_out */
658 struct fuse_init_in *req = data;
659 struct fuse_init_out out;
660
661 TRACE("INIT ver=%d.%d maxread=%d flags=%x\n",
662 req->major, req->minor, req->max_readahead, req->flags);
663
664 out.major = FUSE_KERNEL_VERSION;
665 out.minor = FUSE_KERNEL_MINOR_VERSION;
666 out.max_readahead = req->max_readahead;
667 out.flags = 0;
668 out.max_background = 32;
669 out.congestion_threshold = 32;
670 out.max_write = 256 * 1024;
671
672 fuse_reply(fuse, hdr->unique, &out, sizeof(out));
673 return;
674 }
675 default: {
676 struct fuse_out_header h;
677 ERROR("NOTIMPL op=%d uniq=%llx nid=%llx\n",
678 hdr->opcode, hdr->unique, hdr->nodeid);
679
680 oops:
681 h.len = sizeof(h);
682 h.error = -ENOSYS;
683 h.unique = hdr->unique;
684 write(fuse->fd, &h, sizeof(h));
685 break;
686 }
687 }
688}
689
690void handle_fuse_requests(struct fuse *fuse)
691{
692 unsigned char req[256 * 1024 + 128];
693 int len;
694
695 for (;;) {
696 len = read(fuse->fd, req, 8192);
697 if (len < 0) {
698 if (errno == EINTR)
699 continue;
700 ERROR("handle_fuse_requests: errno=%d\n", errno);
701 return;
702 }
703 handle_fuse_request(fuse, (void*) req, (void*) (req + sizeof(struct fuse_in_header)), len);
704 }
705}
706
707int main(int argc, char **argv)
708{
709 struct fuse fuse;
710 char opts[256];
711 int fd;
712 int res;
713 unsigned uid;
714 unsigned gid;
715 const char *path;
716
717 if (argc != 4) {
718 ERROR("usage: sdcard <path> <uid> <gid>\n");
719 return -1;
720 }
721
722 uid = strtoul(argv[2], 0, 10);
723 gid = strtoul(argv[3], 0, 10);
724 if (!uid || !gid) {
725 ERROR("uid and gid must be nonzero\n");
726 return -1;
727 }
728
729 path = argv[1];
730
731 /* cleanup from previous instance, if necessary */
732 umount2("/sdcard", 2);
733
734 fd = open("/dev/fuse", O_RDWR);
735 if (fd < 0){
736 ERROR("cannot open fuse device (%d)\n", errno);
737 return -1;
738 }
739
740 sprintf(opts, "fd=%i,rootmode=40000,default_permissions,allow_other,"
741 "user_id=%d,group_id=%d", fd, uid, gid);
742
743 res = mount("/dev/fuse", "/sdcard", "fuse", MS_NOSUID | MS_NODEV, opts);
744 if (res < 0) {
745 ERROR("cannot mount fuse filesystem (%d)\n", errno);
746 return -1;
747 }
748
749 if (setgid(gid) < 0) {
750 ERROR("cannot setgid!\n");
751 return -1;
752 }
753 if (setuid(uid) < 0) {
754 ERROR("cannot setuid!\n");
755 return -1;
756 }
757
758 fuse_init(&fuse, fd, path);
759
760 umask(0);
761 handle_fuse_requests(&fuse);
762
763 return 0;
764}