OSDN Git Service

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