3 FUSE-based exFAT implementation. Requires FUSE 2.6 or later.
5 Free exFAT implementation.
6 Copyright (C) 2010-2013 Andrew Nayenko
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.
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.
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.
23 #define FUSE_USE_VERSION 26
33 #include <sys/types.h>
37 #define exfat_debug(format, ...)
39 #if !defined(FUSE_VERSION) || (FUSE_VERSION < 26)
40 #error FUSE 2.6 or later is required
43 const char* default_options = "ro_fallback,allow_other,blkdev,big_writes,"
48 static struct exfat_node* get_node(const struct fuse_file_info* fi)
50 return (struct exfat_node*) (size_t) fi->fh;
53 static void set_node(struct fuse_file_info* fi, struct exfat_node* node)
55 fi->fh = (uint64_t) (size_t) node;
58 static int fuse_exfat_getattr(const char* path, struct stat* stbuf)
60 struct exfat_node* node;
63 exfat_debug("[%s] %s", __func__, path);
65 rc = exfat_lookup(&ef, &node, path);
69 exfat_stat(&ef, node, stbuf);
70 exfat_put_node(&ef, node);
74 static int fuse_exfat_truncate(const char* path, off_t size)
76 struct exfat_node* node;
79 exfat_debug("[%s] %s, %"PRId64, __func__, path, size);
81 rc = exfat_lookup(&ef, &node, path);
85 rc = exfat_truncate(&ef, node, size, true);
86 exfat_put_node(&ef, node);
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)
93 struct exfat_node* parent;
94 struct exfat_node* node;
95 struct exfat_iterator it;
97 char name[UTF8_BYTES(EXFAT_NAME_MAX) + 1];
99 exfat_debug("[%s] %s", __func__, path);
101 rc = exfat_lookup(&ef, &parent, path);
104 if (!(parent->flags & EXFAT_ATTRIB_DIR))
106 exfat_put_node(&ef, parent);
107 exfat_error("`%s' is not a directory (0x%x)", path, parent->flags);
111 filler(buffer, ".", NULL, 0);
112 filler(buffer, "..", NULL, 0);
114 rc = exfat_opendir(&ef, parent, &it);
117 exfat_put_node(&ef, parent);
118 exfat_error("failed to open directory `%s'", path);
121 while ((node = exfat_readdir(&ef, &it)))
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);
130 exfat_closedir(&ef, &it);
131 exfat_put_node(&ef, parent);
135 static int fuse_exfat_open(const char* path, struct fuse_file_info* fi)
137 struct exfat_node* node;
140 exfat_debug("[%s] %s", __func__, path);
142 rc = exfat_lookup(&ef, &node, path);
150 static int fuse_exfat_release(const char* path, struct fuse_file_info* fi)
152 exfat_debug("[%s] %s", __func__, path);
153 exfat_put_node(&ef, get_node(fi));
157 static int fuse_exfat_fsync(const char* path, int datasync,
158 struct fuse_file_info *fi)
162 exfat_debug("[%s] %s", __func__, path);
163 rc = exfat_flush_node(&ef, get_node(fi));
166 rc = exfat_flush(&ef);
169 return exfat_fsync(ef.dev);
172 static int fuse_exfat_read(const char* path, char* buffer, size_t size,
173 off_t offset, struct fuse_file_info* fi)
177 exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
178 ret = exfat_generic_pread(&ef, get_node(fi), buffer, size, offset);
184 static int fuse_exfat_write(const char* path, const char* buffer, size_t size,
185 off_t offset, struct fuse_file_info* fi)
189 exfat_debug("[%s] %s (%zu bytes)", __func__, path, size);
190 ret = exfat_generic_pwrite(&ef, get_node(fi), buffer, size, offset);
196 static int fuse_exfat_unlink(const char* path)
198 struct exfat_node* node;
201 exfat_debug("[%s] %s", __func__, path);
203 rc = exfat_lookup(&ef, &node, path);
207 rc = exfat_unlink(&ef, node);
208 exfat_put_node(&ef, node);
212 static int fuse_exfat_rmdir(const char* path)
214 struct exfat_node* node;
217 exfat_debug("[%s] %s", __func__, path);
219 rc = exfat_lookup(&ef, &node, path);
223 rc = exfat_rmdir(&ef, node);
224 exfat_put_node(&ef, node);
228 static int fuse_exfat_mknod(const char* path, mode_t mode, dev_t dev)
230 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
231 return exfat_mknod(&ef, path);
234 static int fuse_exfat_mkdir(const char* path, mode_t mode)
236 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
237 return exfat_mkdir(&ef, path);
240 static int fuse_exfat_rename(const char* old_path, const char* new_path)
242 exfat_debug("[%s] %s => %s", __func__, old_path, new_path);
243 return exfat_rename(&ef, old_path, new_path);
246 static int fuse_exfat_utimens(const char* path, const struct timespec tv[2])
248 struct exfat_node* node;
251 exfat_debug("[%s] %s", __func__, path);
253 rc = exfat_lookup(&ef, &node, path);
257 exfat_utimes(node, tv);
258 exfat_put_node(&ef, node);
262 static int fuse_exfat_chmod(const char* path, mode_t mode)
264 const mode_t VALID_MODE_MASK = S_IFREG | S_IFDIR |
265 S_IRWXU | S_IRWXG | S_IRWXO;
267 exfat_debug("[%s] %s 0%ho", __func__, path, mode);
268 if (mode & ~VALID_MODE_MASK)
273 static int fuse_exfat_chown(const char* path, uid_t uid, gid_t gid)
275 exfat_debug("[%s] %s %u:%u", __func__, path, uid, gid);
276 if (uid != ef.uid || gid != ef.gid)
281 static int fuse_exfat_statfs(const char* path, struct statvfs* sfs)
283 exfat_debug("[%s]", __func__);
285 sfs->f_bsize = CLUSTER_SIZE(*ef.sb);
286 sfs->f_frsize = CLUSTER_SIZE(*ef.sb);
287 sfs->f_blocks = le64_to_cpu(ef.sb->sector_count) >> ef.sb->spc_bits;
288 sfs->f_bavail = exfat_count_free_clusters(&ef);
289 sfs->f_bfree = sfs->f_bavail;
290 sfs->f_namemax = EXFAT_NAME_MAX;
293 Below are fake values because in exFAT there is
294 a) no simple way to count files;
295 b) no such thing as inode;
296 So here we assume that inode = cluster.
298 sfs->f_files = (sfs->f_blocks - sfs->f_bfree) >> ef.sb->spc_bits;
299 sfs->f_favail = sfs->f_bfree >> ef.sb->spc_bits;
300 sfs->f_ffree = sfs->f_bavail;
305 static void* fuse_exfat_init(struct fuse_conn_info* fci)
307 exfat_debug("[%s]", __func__);
308 #ifdef FUSE_CAP_BIG_WRITES
309 fci->want |= FUSE_CAP_BIG_WRITES;
314 static void fuse_exfat_destroy(void* unused)
316 exfat_debug("[%s]", __func__);
320 static void usage(const char* prog)
322 fprintf(stderr, "Usage: %s [-d] [-o options] [-V] <device> <dir>\n", prog);
326 static struct fuse_operations fuse_exfat_ops =
328 .getattr = fuse_exfat_getattr,
329 .truncate = fuse_exfat_truncate,
330 .readdir = fuse_exfat_readdir,
331 .open = fuse_exfat_open,
332 .release = fuse_exfat_release,
333 .fsync = fuse_exfat_fsync,
334 .fsyncdir = fuse_exfat_fsync,
335 .read = fuse_exfat_read,
336 .write = fuse_exfat_write,
337 .unlink = fuse_exfat_unlink,
338 .rmdir = fuse_exfat_rmdir,
339 .mknod = fuse_exfat_mknod,
340 .mkdir = fuse_exfat_mkdir,
341 .rename = fuse_exfat_rename,
342 .utimens = fuse_exfat_utimens,
343 .chmod = fuse_exfat_chmod,
344 .chown = fuse_exfat_chown,
345 .statfs = fuse_exfat_statfs,
346 .init = fuse_exfat_init,
347 .destroy = fuse_exfat_destroy,
350 static char* add_option(char* options, const char* name, const char* value)
355 size = strlen(options) + strlen(name) + strlen(value) + 3;
357 size = strlen(options) + strlen(name) + 2;
359 options = realloc(options, size);
362 exfat_error("failed to reallocate options string");
365 strcat(options, ",");
366 strcat(options, name);
369 strcat(options, "=");
370 strcat(options, value);
375 static char* add_user_option(char* options)
382 pw = getpwuid(getuid());
383 if (pw == NULL || pw->pw_name == NULL)
386 exfat_error("failed to determine username");
389 return add_option(options, "user", pw->pw_name);
392 static char* add_blksize_option(char* options, long cluster_size)
394 long page_size = sysconf(_SC_PAGESIZE);
400 snprintf(blksize, sizeof(blksize), "%ld", MIN(page_size, cluster_size));
401 return add_option(options, "blksize", blksize);
404 static char* add_fuse_options(char* options, const char* spec)
406 options = add_option(options, "fsname", spec);
409 options = add_user_option(options);
412 options = add_blksize_option(options, CLUSTER_SIZE(*ef.sb));
419 int mount_exfat_main(int argc, char* argv[])
421 struct fuse_args mount_args = FUSE_ARGS_INIT(0, NULL);
422 struct fuse_args newfs_args = FUSE_ARGS_INIT(0, NULL);
423 const char* spec = NULL;
424 const char* mount_point = NULL;
427 struct fuse_chan* fc = NULL;
428 struct fuse* fh = NULL;
431 printf("FUSE exfat %u.%u.%u\n",
432 EXFAT_VERSION_MAJOR, EXFAT_VERSION_MINOR, EXFAT_VERSION_PATCH);
434 mount_options = strdup(default_options);
435 if (mount_options == NULL)
437 exfat_error("failed to allocate options string");
441 while ((opt = getopt(argc, argv, "dno:Vv")) != -1)
451 mount_options = add_option(mount_options, optarg, NULL);
452 if (mount_options == NULL)
457 puts("Copyright (C) 2010-2013 Andrew Nayenko");
467 if (argc - optind != 2)
473 mount_point = argv[optind + 1];
475 if (exfat_mount(&ef, spec, mount_options) != 0)
481 if (ef.ro == -1) /* read-only fallback was used */
483 mount_options = add_option(mount_options, "ro", NULL);
484 if (mount_options == NULL)
491 mount_options = add_fuse_options(mount_options, spec);
492 if (mount_options == NULL)
498 /* create arguments for fuse_mount() */
499 if (fuse_opt_add_arg(&mount_args, "exfat") != 0 ||
500 fuse_opt_add_arg(&mount_args, "-o") != 0 ||
501 fuse_opt_add_arg(&mount_args, mount_options) != 0)
510 /* create FUSE mount point */
511 fc = fuse_mount(mount_point, &mount_args);
512 fuse_opt_free_args(&mount_args);
519 /* create arguments for fuse_new() */
520 if (fuse_opt_add_arg(&newfs_args, "") != 0 ||
521 (debug && fuse_opt_add_arg(&newfs_args, "-d") != 0))
523 fuse_unmount(mount_point, fc);
528 /* create new FUSE file system */
529 fh = fuse_new(fc, &newfs_args, &fuse_exfat_ops,
530 sizeof(struct fuse_operations), NULL);
531 fuse_opt_free_args(&newfs_args);
534 fuse_unmount(mount_point, fc);
539 /* exit session on HUP, TERM and INT signals and ignore PIPE signal */
540 if (fuse_set_signal_handlers(fuse_get_session(fh)) != 0)
542 fuse_unmount(mount_point, fc);
545 exfat_error("failed to set signal handlers");
549 /* go to background (unless "-d" option is passed) and run FUSE
551 if (fuse_daemonize(debug) == 0)
553 if (fuse_loop(fh) != 0)
554 exfat_error("FUSE loop failure");
557 exfat_error("failed to daemonize");
559 fuse_remove_signal_handlers(fuse_get_session(fh));
560 /* note that fuse_unmount() must be called BEFORE fuse_destroy() */
561 fuse_unmount(mount_point, fc);