OSDN Git Service

333c2c49caf911cb93978e117b2f0b7559851de2
[android-x86/external-exfat.git] / fuse / main.c
1 /*
2         main.c (01.09.09)
3         FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
4
5         Free exFAT implementation.
6         Copyright (C) 2010-2013  Andrew Nayenko
7
8         This program is free software; you can redistribute it and/or modify
9         it under the terms of the GNU General Public License as published by
10         the Free Software Foundation, either version 2 of the License, or
11         (at your option) any later version.
12
13         This program is distributed in the hope that it will be useful,
14         but WITHOUT ANY WARRANTY; without even the implied warranty of
15         MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16         GNU General Public License for more details.
17
18         You should have received a copy of the GNU General Public License along
19         with this program; if not, write to the Free Software Foundation, Inc.,
20         51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
23 #define FUSE_USE_VERSION 26
24 #include <fuse.h>
25 #include <errno.h>
26 #include <fcntl.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <exfat.h>
31 #include <inttypes.h>
32 #include <limits.h>
33 #include <sys/types.h>
34 #include <pwd.h>
35 #include <unistd.h>
36
37 #define exfat_debug(format, ...)
38
39 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
40         #error FUSE 2.6 or later is required
41 #endif
42
43 const char* default_options = "ro_fallback,allow_other,blkdev,big_writes,"
44                 "defer_permissions";
45
46 struct exfat ef;
47
48 static struct exfat_node* get_node(const struct fuse_file_info* fi)
49 {
50         return (struct exfat_node*) (size_t) fi->fh;
51 }
52
53 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
54 {
55         fi->fh = (uint64_t) (size_t) node;
56 }
57
58 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
59 {
60         struct exfat_node* node;
61         int rc;
62
63         exfat_debug("[%s] %s", __func__, path);
64
65         rc = exfat_lookup(&ef, &node, path);
66         if (rc != 0)
67                 return rc;
68
69         exfat_stat(&ef, node, stbuf);
70         exfat_put_node(&ef, node);
71         return 0;
72 }
73
74 static int fuse_exfat_truncate(const char* path, off_t size)
75 {
76         struct exfat_node* node;
77         int rc;
78
79         exfat_debug("[%s] %s, %"PRId64, __func__, path, size);
80
81         rc = exfat_lookup(&ef, &node, path);
82         if (rc != 0)
83                 return rc;
84
85         rc = exfat_truncate(&ef, node, size, true);
86         exfat_put_node(&ef, node);
87         return rc;
88 }
89
90 static int fuse_exfat_readdir(const char* path, void* buffer,
91                 fuse_fill_dir_t filler, off_t offset, struct fuse_file_info* fi)
92 {
93         struct exfat_node* parent;
94         struct exfat_node* node;
95         struct exfat_iterator it;
96         int rc;
97         char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
98
99         exfat_debug("[%s] %s", __func__, path);
100
101         rc = exfat_lookup(&ef, &parent, path);
102         if (rc != 0)
103                 return rc;
104         if (!(parent->flags & EXFAT_ATTRIB_DIR))
105         {
106                 exfat_put_node(&ef, parent);
107                 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
108                 return -ENOTDIR;
109         }
110
111         filler(buffer, ".", NULL, 0);
112         filler(buffer, "..", NULL, 0);
113
114         rc = exfat_opendir(&ef, parent, &it);
115         if (rc != 0)
116         {
117                 exfat_put_node(&ef, parent);
118                 exfat_error("failed to open directory `%s'", path);
119                 return rc;
120         }
121         while ((node = exfat_readdir(&ef, &it)))
122         {
123                 exfat_get_name(node, name, sizeof(name) - 1);
124                 exfat_debug("[%s] %s: %s, %"PRId64" bytes, cluster 0x%x", __func__,
125                                 name, IS_CONTIGUOUS(*node) ? "contiguous" : "fragmented",
126                                 node->size, node->start_cluster);
127                 filler(buffer, name, NULL, 0);
128                 exfat_put_node(&ef, node);
129         }
130         exfat_closedir(&ef, &it);
131         exfat_put_node(&ef, parent);
132         return 0;
133 }
134
135 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
136 {
137         struct exfat_node* node;
138         int rc;
139
140         exfat_debug("[%s] %s", __func__, path);
141
142         rc = exfat_lookup(&ef, &node, path);
143         if (rc != 0)
144                 return rc;
145         set_node(fi, node);
146         fi->keep_cache = 1;
147         return 0;
148 }
149
150 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
151 {
152         exfat_debug("[%s] %s", __func__, path);
153         exfat_put_node(&ef, get_node(fi));
154         return 0;
155 }
156
157 static int fuse_exfat_fsync(const char* path, int datasync,
158                 struct fuse_file_info *fi)
159 {
160         int rc;
161
162         exfat_debug("[%s] %s", __func__, path);
163         rc = exfat_flush_node(&ef, get_node(fi));
164         if (rc != 0)
165                 return rc;
166         exfat_flush(&ef);
167         return exfat_fsync(ef.dev);
168 }
169
170 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
171                 off_t offset, struct fuse_file_info* fi)
172 {
173         ssize_t ret;
174
175         exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
176         ret = exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
177         if (ret < 0)
178                 return -EIO;
179         return ret;
180 }
181
182 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
183                 off_t offset, struct fuse_file_info* fi)
184 {
185         ssize_t ret;
186
187         exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
188         ret = exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
189         if (ret < 0)
190                 return -EIO;
191         return ret;
192 }
193
194 static int fuse_exfat_unlink(const char* path)
195 {
196         struct exfat_node* node;
197         int rc;
198
199         exfat_debug("[%s] %s", __func__, path);
200
201         rc = exfat_lookup(&ef, &node, path);
202         if (rc != 0)
203                 return rc;
204
205         rc = exfat_unlink(&ef, node);
206         exfat_put_node(&ef, node);
207         return rc;
208 }
209
210 static int fuse_exfat_rmdir(const char* path)
211 {
212         struct exfat_node* node;
213         int rc;
214
215         exfat_debug("[%s] %s", __func__, path);
216
217         rc = exfat_lookup(&ef, &node, path);
218         if (rc != 0)
219                 return rc;
220
221         rc = exfat_rmdir(&ef, node);
222         exfat_put_node(&ef, node);
223         return rc;
224 }
225
226 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
227 {
228         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
229         return exfat_mknod(&ef, path);
230 }
231
232 static int fuse_exfat_mkdir(const char* path, mode_t mode)
233 {
234         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
235         return exfat_mkdir(&ef, path);
236 }
237
238 static int fuse_exfat_rename(const char* old_path, const char* new_path)
239 {
240         exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
241         return exfat_rename(&ef, old_path, new_path);
242 }
243
244 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
245 {
246         struct exfat_node* node;
247         int rc;
248
249         exfat_debug("[%s] %s", __func__, path);
250
251         rc = exfat_lookup(&ef, &node, path);
252         if (rc != 0)
253                 return rc;
254
255         exfat_utimes(node, tv);
256         exfat_put_node(&ef, node);
257         return 0;
258 }
259
260 static int fuse_exfat_chmod(const char* path, mode_t mode)
261 {
262         const mode_t VALID_MODE_MASK = S_IFREG | S_IFDIR |
263                         S_IRWXU | S_IRWXG | S_IRWXO;
264
265         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
266         if (mode & ~VALID_MODE_MASK)
267                 return -EPERM;
268         return 0;
269 }
270
271 static int fuse_exfat_chown(const char* path, uid_t uid, gid_t gid)
272 {
273         exfat_debug("[%s] %s %u:%u", __func__, path, uid, gid);
274         if (uid != ef.uid || gid != ef.gid)
275                 return -EPERM;
276         return 0;
277 }
278
279 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
280 {
281         exfat_debug("[%s]", __func__);
282
283         sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
284         sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
285         sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
286         sfs->f_bavail = exfat_count_free_clusters(&ef);
287         sfs->f_bfree = sfs->f_bavail;
288         sfs->f_namemax = EXFAT_NAME_MAX;
289
290         /*
291            Below are fake values because in exFAT there is
292            a) no simple way to count files;
293            b) no such thing as inode;
294            So here we assume that inode = cluster.
295         */
296         sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
297         sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
298         sfs->f_ffree = sfs->f_bavail;
299
300         return 0;
301 }
302
303 static void* fuse_exfat_init(struct fuse_conn_info* fci)
304 {
305         exfat_debug("[%s]", __func__);
306 #ifdef FUSE_CAP_BIG_WRITES
307         fci->want |= FUSE_CAP_BIG_WRITES;
308 #endif
309         return NULL;
310 }
311
312 static void fuse_exfat_destroy(void* unused)
313 {
314         exfat_debug("[%s]", __func__);
315         exfat_unmount(&ef);
316 }
317
318 static void usage(const char* prog)
319 {
320         fprintf(stderr, "Usage: %s [-d] [-o options] [-V] <device> <dir>\n", prog);
321         exit(1);
322 }
323
324 static struct fuse_operations fuse_exfat_ops =
325 {
326         .getattr        = fuse_exfat_getattr,
327         .truncate       = fuse_exfat_truncate,
328         .readdir        = fuse_exfat_readdir,
329         .open           = fuse_exfat_open,
330         .release        = fuse_exfat_release,
331         .fsync          = fuse_exfat_fsync,
332         .fsyncdir       = fuse_exfat_fsync,
333         .read           = fuse_exfat_read,
334         .write          = fuse_exfat_write,
335         .unlink         = fuse_exfat_unlink,
336         .rmdir          = fuse_exfat_rmdir,
337         .mknod          = fuse_exfat_mknod,
338         .mkdir          = fuse_exfat_mkdir,
339         .rename         = fuse_exfat_rename,
340         .utimens        = fuse_exfat_utimens,
341         .chmod          = fuse_exfat_chmod,
342         .chown          = fuse_exfat_chown,
343         .statfs         = fuse_exfat_statfs,
344         .init           = fuse_exfat_init,
345         .destroy        = fuse_exfat_destroy,
346 };
347
348 static char* add_option(char* options, const char* name, const char* value)
349 {
350         size_t size;
351
352         if (value)
353                 size = strlen(options) + strlen(name) + strlen(value) + 3;
354         else
355                 size = strlen(options) + strlen(name) + 2;
356
357         options = realloc(options, size);
358         if (options == NULL)
359         {
360                 exfat_error("failed to reallocate options string");
361                 return NULL;
362         }
363         strcat(options, ",");
364         strcat(options, name);
365         if (value)
366         {
367                 strcat(options, "=");
368                 strcat(options, value);
369         }
370         return options;
371 }
372
373 static char* add_user_option(char* options)
374 {
375         struct passwd* pw;
376
377         if (getuid() == 0)
378                 return options;
379
380         pw = getpwuid(getuid());
381         if (pw == NULL || pw->pw_name == NULL)
382         {
383                 free(options);
384                 exfat_error("failed to determine username");
385                 return NULL;
386         }
387         return add_option(options, "user", pw->pw_name);
388 }
389
390 static char* add_blksize_option(char* options, long cluster_size)
391 {
392         long page_size = sysconf(_SC_PAGESIZE);
393         char blksize[20];
394
395         if (page_size < 1)
396                 page_size = 0x1000;
397
398         snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
399         return add_option(options, "blksize", blksize);
400 }
401
402 static char* add_fuse_options(char* options, const char* spec)
403 {
404         options = add_option(options, "fsname", spec);
405         if (options == NULL)
406                 return NULL;
407         options = add_user_option(options);
408         if (options == NULL)
409                 return NULL;
410         options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb));
411         if (options == NULL)
412                 return NULL;
413
414         return options;
415 }
416
417 int main(int argc, char* argv[])
418 {
419         struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
420         struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
421         const char* spec = NULL;
422         const char* mount_point = NULL;
423         char* mount_options;
424         int debug = 0;
425         struct fuse_chan* fc = NULL;
426         struct fuse* fh = NULL;
427         int opt;
428
429         printf("FUSE exfat %u.%u.%u\n",
430                         EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
431
432         mount_options = strdup(default_options);
433         if (mount_options == NULL)
434         {
435                 exfat_error("failed to allocate options string");
436                 return 1;
437         }
438
439         while ((opt = getopt(argc, argv, "dno:Vv")) != -1)
440         {
441                 switch (opt)
442                 {
443                 case 'd':
444                         debug = 1;
445                         break;
446                 case 'n':
447                         break;
448                 case 'o':
449                         mount_options = add_option(mount_options, optarg, NULL);
450                         if (mount_options == NULL)
451                                 return 1;
452                         break;
453                 case 'V':
454                         free(mount_options);
455                         puts("Copyright (C) 2010-2013  Andrew Nayenko");
456                         return 0;
457                 case 'v':
458                         break;
459                 default:
460                         free(mount_options);
461                         usage(argv[0]);
462                         break;
463                 }
464         }
465         if (argc - optind != 2)
466         {
467                 free(mount_options);
468                 usage(argv[0]);
469         }
470         spec = argv[optind];
471         mount_point = argv[optind + 1];
472
473         if (exfat_mount(&ef, spec, mount_options) != 0)
474         {
475                 free(mount_options);
476                 return 1;
477         }
478
479         if (ef.ro == -1) /* read-only fallback was used */
480         {
481                 mount_options = add_option(mount_options, "ro", NULL);
482                 if (mount_options == NULL)
483                 {
484                         exfat_unmount(&ef);
485                         return 1;
486                 }
487         }
488
489         mount_options = add_fuse_options(mount_options, spec);
490         if (mount_options == NULL)
491         {
492                 exfat_unmount(&ef);
493                 return 1;
494         }
495
496         /* create arguments for fuse_mount() */
497         if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
498                 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
499                 fuse_opt_add_arg(&mount_args, mount_options) != 0)
500         {
501                 exfat_unmount(&ef);
502                 free(mount_options);
503                 return 1;
504         }
505
506         free(mount_options);
507
508         /* create FUSE mount point */
509         fc = fuse_mount(mount_point, &mount_args);
510         fuse_opt_free_args(&mount_args);
511         if (fc == NULL)
512         {
513                 exfat_unmount(&ef);
514                 return 1;
515         }
516
517         /* create arguments for fuse_new() */
518         if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
519                 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
520         {
521                 fuse_unmount(mount_point, fc);
522                 exfat_unmount(&ef);
523                 return 1;
524         }
525
526         /* create new FUSE file system */
527         fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
528                         sizeof(struct fuse_operations), NULL);
529         fuse_opt_free_args(&newfs_args);
530         if (fh == NULL)
531         {
532                 fuse_unmount(mount_point, fc);
533                 exfat_unmount(&ef);
534                 return 1;
535         }
536
537         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
538         if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
539         {
540                 fuse_unmount(mount_point, fc);
541                 fuse_destroy(fh);
542                 exfat_unmount(&ef);
543                 exfat_error("failed to set signal handlers");
544                 return 1;
545         }
546
547         /* go to background (unless "-d" option is passed) and run FUSE
548            main loop */
549         if (fuse_daemonize(debug) == 0)
550         {
551                 if (fuse_loop(fh) != 0)
552                         exfat_error("FUSE loop failure");
553         }
554         else
555                 exfat_error("failed to daemonize");
556
557         fuse_remove_signal_handlers(fuse_get_session(fh));
558         /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
559         fuse_unmount(mount_point, fc);
560         fuse_destroy(fh);
561         return 0;
562 }