OSDN Git Service

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