OSDN Git Service

Moved node checks into a separate function check_node().
[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         if (get_node(fi) != NULL)
164         {
165                 rc = exfat_flush_node(&ef, get_node(fi));
166                 if (rc != 0)
167                         return rc;
168         }
169         rc = exfat_flush(&ef);
170         if (rc != 0)
171                 return rc;
172         return exfat_fsync(ef.dev);
173 }
174
175 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
176                 off_t offset, struct fuse_file_info* fi)
177 {
178         ssize_t ret;
179
180         exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
181         ret = exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
182         if (ret < 0)
183                 return -EIO;
184         return ret;
185 }
186
187 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
188                 off_t offset, struct fuse_file_info* fi)
189 {
190         ssize_t ret;
191
192         exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
193         ret = exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
194         if (ret < 0)
195                 return -EIO;
196         return ret;
197 }
198
199 static int fuse_exfat_unlink(const char* path)
200 {
201         struct exfat_node* node;
202         int rc;
203
204         exfat_debug("[%s] %s", __func__, path);
205
206         rc = exfat_lookup(&ef, &node, path);
207         if (rc != 0)
208                 return rc;
209
210         rc = exfat_unlink(&ef, node);
211         exfat_put_node(&ef, node);
212         return rc;
213 }
214
215 static int fuse_exfat_rmdir(const char* path)
216 {
217         struct exfat_node* node;
218         int rc;
219
220         exfat_debug("[%s] %s", __func__, path);
221
222         rc = exfat_lookup(&ef, &node, path);
223         if (rc != 0)
224                 return rc;
225
226         rc = exfat_rmdir(&ef, node);
227         exfat_put_node(&ef, node);
228         return rc;
229 }
230
231 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
232 {
233         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
234         return exfat_mknod(&ef, path);
235 }
236
237 static int fuse_exfat_mkdir(const char* path, mode_t mode)
238 {
239         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
240         return exfat_mkdir(&ef, path);
241 }
242
243 static int fuse_exfat_rename(const char* old_path, const char* new_path)
244 {
245         exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
246         return exfat_rename(&ef, old_path, new_path);
247 }
248
249 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
250 {
251         struct exfat_node* node;
252         int rc;
253
254         exfat_debug("[%s] %s", __func__, path);
255
256         rc = exfat_lookup(&ef, &node, path);
257         if (rc != 0)
258                 return rc;
259
260         exfat_utimes(node, tv);
261         exfat_put_node(&ef, node);
262         return 0;
263 }
264
265 static int fuse_exfat_chmod(const char* path, mode_t mode)
266 {
267         const mode_t VALID_MODE_MASK = S_IFREG | S_IFDIR |
268                         S_IRWXU | S_IRWXG | S_IRWXO;
269
270         exfat_debug("[%s] %s 0%ho", __func__, path, mode);
271         if (mode & ~VALID_MODE_MASK)
272                 return -EPERM;
273         return 0;
274 }
275
276 static int fuse_exfat_chown(const char* path, uid_t uid, gid_t gid)
277 {
278         exfat_debug("[%s] %s %u:%u", __func__, path, uid, gid);
279         if (uid != ef.uid || gid != ef.gid)
280                 return -EPERM;
281         return 0;
282 }
283
284 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
285 {
286         exfat_debug("[%s]", __func__);
287
288         sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
289         sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
290         sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
291         sfs->f_bavail = exfat_count_free_clusters(&ef);
292         sfs->f_bfree = sfs->f_bavail;
293         sfs->f_namemax = EXFAT_NAME_MAX;
294
295         /*
296            Below are fake values because in exFAT there is
297            a) no simple way to count files;
298            b) no such thing as inode;
299            So here we assume that inode = cluster.
300         */
301         sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
302         sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
303         sfs->f_ffree = sfs->f_bavail;
304
305         return 0;
306 }
307
308 static void* fuse_exfat_init(struct fuse_conn_info* fci)
309 {
310         exfat_debug("[%s]", __func__);
311 #ifdef FUSE_CAP_BIG_WRITES
312         fci->want |= FUSE_CAP_BIG_WRITES;
313 #endif
314         return NULL;
315 }
316
317 static void fuse_exfat_destroy(void* unused)
318 {
319         exfat_debug("[%s]", __func__);
320         exfat_unmount(&ef);
321 }
322
323 static void usage(const char* prog)
324 {
325         fprintf(stderr, "Usage: %s [-d] [-o options] [-V] <device> <dir>\n", prog);
326         exit(1);
327 }
328
329 static struct fuse_operations fuse_exfat_ops =
330 {
331         .getattr        = fuse_exfat_getattr,
332         .truncate       = fuse_exfat_truncate,
333         .readdir        = fuse_exfat_readdir,
334         .open           = fuse_exfat_open,
335         .release        = fuse_exfat_release,
336         .fsync          = fuse_exfat_fsync,
337         .fsyncdir       = fuse_exfat_fsync,
338         .read           = fuse_exfat_read,
339         .write          = fuse_exfat_write,
340         .unlink         = fuse_exfat_unlink,
341         .rmdir          = fuse_exfat_rmdir,
342         .mknod          = fuse_exfat_mknod,
343         .mkdir          = fuse_exfat_mkdir,
344         .rename         = fuse_exfat_rename,
345         .utimens        = fuse_exfat_utimens,
346         .chmod          = fuse_exfat_chmod,
347         .chown          = fuse_exfat_chown,
348         .statfs         = fuse_exfat_statfs,
349         .init           = fuse_exfat_init,
350         .destroy        = fuse_exfat_destroy,
351 };
352
353 static char* add_option(char* options, const char* name, const char* value)
354 {
355         size_t size;
356
357         if (value)
358                 size = strlen(options) + strlen(name) + strlen(value) + 3;
359         else
360                 size = strlen(options) + strlen(name) + 2;
361
362         options = realloc(options, size);
363         if (options == NULL)
364         {
365                 exfat_error("failed to reallocate options string");
366                 return NULL;
367         }
368         strcat(options, ",");
369         strcat(options, name);
370         if (value)
371         {
372                 strcat(options, "=");
373                 strcat(options, value);
374         }
375         return options;
376 }
377
378 static char* add_user_option(char* options)
379 {
380         struct passwd* pw;
381
382         if (getuid() == 0)
383                 return options;
384
385         pw = getpwuid(getuid());
386         if (pw == NULL || pw->pw_name == NULL)
387         {
388                 free(options);
389                 exfat_error("failed to determine username");
390                 return NULL;
391         }
392         return add_option(options, "user", pw->pw_name);
393 }
394
395 static char* add_blksize_option(char* options, long cluster_size)
396 {
397         long page_size = sysconf(_SC_PAGESIZE);
398         char blksize[20];
399
400         if (page_size < 1)
401                 page_size = 0x1000;
402
403         snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
404         return add_option(options, "blksize", blksize);
405 }
406
407 static char* add_fuse_options(char* options, const char* spec)
408 {
409         options = add_option(options, "fsname", spec);
410         if (options == NULL)
411                 return NULL;
412         options = add_user_option(options);
413         if (options == NULL)
414                 return NULL;
415         options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb));
416         if (options == NULL)
417                 return NULL;
418
419         return options;
420 }
421
422 int main(int argc, char* argv[])
423 {
424         struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
425         struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
426         const char* spec = NULL;
427         const char* mount_point = NULL;
428         char* mount_options;
429         int debug = 0;
430         struct fuse_chan* fc = NULL;
431         struct fuse* fh = NULL;
432         int opt;
433
434         printf("FUSE exfat %u.%u.%u\n",
435                         EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
436
437         mount_options = strdup(default_options);
438         if (mount_options == NULL)
439         {
440                 exfat_error("failed to allocate options string");
441                 return 1;
442         }
443
444         while ((opt = getopt(argc, argv, "dno:Vv")) != -1)
445         {
446                 switch (opt)
447                 {
448                 case 'd':
449                         debug = 1;
450                         break;
451                 case 'n':
452                         break;
453                 case 'o':
454                         mount_options = add_option(mount_options, optarg, NULL);
455                         if (mount_options == NULL)
456                                 return 1;
457                         break;
458                 case 'V':
459                         free(mount_options);
460                         puts("Copyright (C) 2010-2013  Andrew Nayenko");
461                         return 0;
462                 case 'v':
463                         break;
464                 default:
465                         free(mount_options);
466                         usage(argv[0]);
467                         break;
468                 }
469         }
470         if (argc - optind != 2)
471         {
472                 free(mount_options);
473                 usage(argv[0]);
474         }
475         spec = argv[optind];
476         mount_point = argv[optind + 1];
477
478         if (exfat_mount(&ef, spec, mount_options) != 0)
479         {
480                 free(mount_options);
481                 return 1;
482         }
483
484         if (ef.ro == -1) /* read-only fallback was used */
485         {
486                 mount_options = add_option(mount_options, "ro", NULL);
487                 if (mount_options == NULL)
488                 {
489                         exfat_unmount(&ef);
490                         return 1;
491                 }
492         }
493
494         mount_options = add_fuse_options(mount_options, spec);
495         if (mount_options == NULL)
496         {
497                 exfat_unmount(&ef);
498                 return 1;
499         }
500
501         /* create arguments for fuse_mount() */
502         if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
503                 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
504                 fuse_opt_add_arg(&mount_args, mount_options) != 0)
505         {
506                 exfat_unmount(&ef);
507                 free(mount_options);
508                 return 1;
509         }
510
511         free(mount_options);
512
513         /* create FUSE mount point */
514         fc = fuse_mount(mount_point, &mount_args);
515         fuse_opt_free_args(&mount_args);
516         if (fc == NULL)
517         {
518                 exfat_unmount(&ef);
519                 return 1;
520         }
521
522         /* create arguments for fuse_new() */
523         if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
524                 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
525         {
526                 fuse_unmount(mount_point, fc);
527                 exfat_unmount(&ef);
528                 return 1;
529         }
530
531         /* create new FUSE file system */
532         fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
533                         sizeof(struct fuse_operations), NULL);
534         fuse_opt_free_args(&newfs_args);
535         if (fh == NULL)
536         {
537                 fuse_unmount(mount_point, fc);
538                 exfat_unmount(&ef);
539                 return 1;
540         }
541
542         /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
543         if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
544         {
545                 fuse_unmount(mount_point, fc);
546                 fuse_destroy(fh);
547                 exfat_unmount(&ef);
548                 exfat_error("failed to set signal handlers");
549                 return 1;
550         }
551
552         /* go to background (unless "-d" option is passed) and run FUSE
553            main loop */
554         if (fuse_daemonize(debug) == 0)
555         {
556                 if (fuse_loop(fh) != 0)
557                         exfat_error("FUSE loop failure");
558         }
559         else
560                 exfat_error("failed to daemonize");
561
562         fuse_remove_signal_handlers(fuse_get_session(fh));
563         /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
564         fuse_unmount(mount_point, fc);
565         fuse_destroy(fh);
566         return 0;
567 }