2 * fuse2fs.c - FUSE server for e2fsprogs.
4 * Copyright (C) 2014 Oracle.
7 * This file may be redistributed under the terms of the GNU Public
11 #define _FILE_OFFSET_BITS 64
12 #define FUSE_USE_VERSION 29
19 # include <linux/fs.h>
20 # include <linux/falloc.h>
21 # include <linux/xattr.h>
22 # define FUSE_PLATFORM_OPTS ",nonempty,big_writes"
23 # ifdef HAVE_SYS_ACL_H
24 # define TRANSLATE_LINUX_ACLS
27 # define FUSE_PLATFORM_OPTS ""
29 #ifdef TRANSLATE_LINUX_ACLS
32 #include <sys/ioctl.h>
36 #include "ext2fs/ext2fs.h"
37 #include "ext2fs/ext2_fs.h"
39 #include "../version.h"
44 #define _(a) (gettext(a))
46 #define N_(a) gettext_noop(a)
50 #define P_(singular, plural, n) (ngettext(singular, plural, n))
52 #define NLS_CAT_NAME "e2fsprogs"
55 #define LOCALEDIR "/usr/share/locale"
60 #define P_(singular, plural, n) ((n) == 1 ? (singular) : (plural))
63 static ext2_filsys global_fs; /* Try not to use this directly */
68 # define dbg_printf(f, a...) do {printf("FUSE2FS-" f, ## a); \
72 # define dbg_printf(f, a...)
75 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
78 # define SUPPORT_I_FLAGS
83 #ifdef FALLOC_FL_KEEP_SIZE
84 # define FL_KEEP_SIZE_FLAG FALLOC_FL_KEEP_SIZE
85 # define SUPPORT_FALLOCATE
87 # define FL_KEEP_SIZE_FLAG (0)
90 #ifdef FALLOC_FL_PUNCH_HOLE
91 # define FL_PUNCH_HOLE_FLAG FALLOC_FL_PUNCH_HOLE
93 # define FL_PUNCH_HOLE_FLAG (0)
96 errcode_t ext2fs_run_ext3_journal(ext2_filsys *fs);
98 #ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
99 int journal_enable_debug = -1;
102 /* ACL translation stuff */
103 #ifdef TRANSLATE_LINUX_ACLS
105 * Copied from acl_ea.h in libacl source; ACLs have to be sent to and from fuse
106 * in this format... at least on Linux.
108 #define ACL_EA_ACCESS "system.posix_acl_access"
109 #define ACL_EA_DEFAULT "system.posix_acl_default"
111 #define ACL_EA_VERSION 0x0002
121 #if __GNUC_PREREQ (4, 8)
122 #pragma GCC diagnostic push
123 #pragma GCC diagnostic ignored "-Wpedantic"
125 acl_ea_entry a_entries[0];
126 #if __GNUC_PREREQ (4, 8)
127 #pragma GCC diagnostic pop
131 static inline size_t acl_ea_size(int count)
133 return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
136 static inline int acl_ea_count(size_t size)
138 if (size < sizeof(acl_ea_header))
140 size -= sizeof(acl_ea_header);
141 if (size % sizeof(acl_ea_entry))
143 return size / sizeof(acl_ea_entry);
147 * ext4 ACL structures, copied from fs/ext4/acl.h.
149 #define EXT4_ACL_VERSION 0x0001
160 } ext4_acl_entry_short;
166 static inline size_t ext4_acl_size(int count)
169 return sizeof(ext4_acl_header) +
170 count * sizeof(ext4_acl_entry_short);
172 return sizeof(ext4_acl_header) +
173 4 * sizeof(ext4_acl_entry_short) +
174 (count - 4) * sizeof(ext4_acl_entry);
178 static inline int ext4_acl_count(size_t size)
182 size -= sizeof(ext4_acl_header);
183 s = size - 4 * sizeof(ext4_acl_entry_short);
185 if (size % sizeof(ext4_acl_entry_short))
187 return size / sizeof(ext4_acl_entry_short);
189 if (s % sizeof(ext4_acl_entry))
191 return s / sizeof(ext4_acl_entry) + 4;
194 static errcode_t fuse_to_ext4_acl(acl_ea_header *facl, size_t facl_sz,
195 ext4_acl_header **eacl, size_t *eacl_sz)
205 facl_count = acl_ea_count(facl_sz);
206 h_sz = ext4_acl_size(facl_count);
207 if (facl_count < 0 || facl->a_version != ACL_EA_VERSION)
208 return EXT2_ET_INVALID_ARGUMENT;
210 err = ext2fs_get_mem(h_sz, &h);
214 h->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
215 hptr = (unsigned char *) (h + 1);
216 for (i = 0, a = facl->a_entries; i < facl_count; i++, a++) {
217 e = (ext4_acl_entry *) hptr;
218 e->e_tag = ext2fs_cpu_to_le16(a->e_tag);
219 e->e_perm = ext2fs_cpu_to_le16(a->e_perm);
224 e->e_id = ext2fs_cpu_to_le32(a->e_id);
225 hptr += sizeof(ext4_acl_entry);
231 hptr += sizeof(ext4_acl_entry_short);
234 err = EXT2_ET_INVALID_ARGUMENT;
247 static errcode_t ext4_to_fuse_acl(acl_ea_header **facl, size_t *facl_sz,
248 ext4_acl_header *eacl, size_t eacl_sz)
258 eacl_count = ext4_acl_count(eacl_sz);
259 f_sz = acl_ea_size(eacl_count);
260 if (eacl_count < 0 ||
261 eacl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION))
262 return EXT2_ET_INVALID_ARGUMENT;
264 err = ext2fs_get_mem(f_sz, &f);
268 f->a_version = ACL_EA_VERSION;
269 hptr = (unsigned char *) (eacl + 1);
270 for (i = 0, a = f->a_entries; i < eacl_count; i++, a++) {
271 e = (ext4_acl_entry *) hptr;
272 a->e_tag = ext2fs_le16_to_cpu(e->e_tag);
273 a->e_perm = ext2fs_le16_to_cpu(e->e_perm);
278 a->e_id = ext2fs_le32_to_cpu(e->e_id);
279 hptr += sizeof(ext4_acl_entry);
285 hptr += sizeof(ext4_acl_entry_short);
288 err = EXT2_ET_INVALID_ARGUMENT;
300 #endif /* TRANSLATE_LINUX_ACLS */
303 * ext2_file_t contains a struct inode, so we can't leave files open.
304 * Use this as a proxy instead.
306 #define FUSE2FS_FILE_MAGIC (0xEF53DEAFUL)
307 struct fuse2fs_file_handle {
313 /* Main program context */
314 #define FUSE2FS_MAGIC (0xEF53DEADUL)
325 int alloc_all_blocks;
327 unsigned int next_generation;
330 #define FUSE2FS_CHECK_MAGIC(fs, ptr, num) do {if ((ptr)->magic != (num)) \
331 return translate_error((fs), 0, EXT2_ET_MAGIC_EXT2_FILE); \
334 #define FUSE2FS_CHECK_CONTEXT(ptr) do {if ((ptr)->magic != FUSE2FS_MAGIC) \
335 return translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); \
338 static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
339 const char *file, int line);
340 #define translate_error(fs, ino, err) __translate_error((fs), (err), (ino), \
352 #define EXT4_EPOCH_BITS 2
353 #define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1)
354 #define EXT4_NSEC_MASK (~0UL << EXT4_EPOCH_BITS)
357 * Extended fields will fit into an inode if the filesystem was formatted
358 * with large inodes (-I 256 or larger) and there are not currently any EAs
359 * consuming all of the available space. For new inodes we always reserve
360 * enough space for the kernel's known extended fields, but for inodes
361 * created with an old kernel this might not have been the case. None of
362 * the extended inode fields is critical for correct filesystem operation.
363 * This macro checks if a certain field fits in the inode. Note that
364 * inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize
366 #define EXT4_FITS_IN_INODE(ext4_inode, field) \
367 ((offsetof(typeof(*ext4_inode), field) + \
368 sizeof((ext4_inode)->field)) \
369 <= ((size_t) EXT2_GOOD_OLD_INODE_SIZE + \
370 (ext4_inode)->i_extra_isize)) \
372 static inline __u32 ext4_encode_extra_time(const struct timespec *time)
374 __u32 extra = sizeof(time->tv_sec) > 4 ?
375 ((time->tv_sec - (__s32)time->tv_sec) >> 32) &
377 return extra | (time->tv_nsec << EXT4_EPOCH_BITS);
380 static inline void ext4_decode_extra_time(struct timespec *time, __u32 extra)
382 if (sizeof(time->tv_sec) > 4 && (extra & EXT4_EPOCH_MASK)) {
383 __u64 extra_bits = extra & EXT4_EPOCH_MASK;
385 * Prior to kernel 3.14?, we had a broken decode function,
386 * wherein we effectively did this:
387 * if (extra_bits == 3)
390 time->tv_sec += extra_bits << 32;
392 time->tv_nsec = ((extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
395 #define EXT4_INODE_SET_XTIME(xtime, timespec, raw_inode) \
397 (raw_inode)->xtime = (timespec)->tv_sec; \
398 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
399 (raw_inode)->xtime ## _extra = \
400 ext4_encode_extra_time(timespec); \
403 #define EXT4_EINODE_SET_XTIME(xtime, timespec, raw_inode) \
405 if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
406 (raw_inode)->xtime = (timespec)->tv_sec; \
407 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
408 (raw_inode)->xtime ## _extra = \
409 ext4_encode_extra_time(timespec); \
412 #define EXT4_INODE_GET_XTIME(xtime, timespec, raw_inode) \
414 (timespec)->tv_sec = (signed)((raw_inode)->xtime); \
415 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
416 ext4_decode_extra_time((timespec), \
417 (raw_inode)->xtime ## _extra); \
419 (timespec)->tv_nsec = 0; \
422 #define EXT4_EINODE_GET_XTIME(xtime, timespec, raw_inode) \
424 if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
425 (timespec)->tv_sec = \
426 (signed)((raw_inode)->xtime); \
427 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
428 ext4_decode_extra_time((timespec), \
429 raw_inode->xtime ## _extra); \
431 (timespec)->tv_nsec = 0; \
434 static void get_now(struct timespec *now)
436 #ifdef CLOCK_REALTIME
437 if (!clock_gettime(CLOCK_REALTIME, now))
441 now->tv_sec = time(NULL);
445 static void increment_version(struct ext2_inode_large *inode)
449 ver = inode->osd1.linux1.l_i_version;
450 if (EXT4_FITS_IN_INODE(inode, i_version_hi))
451 ver |= (__u64)inode->i_version_hi << 32;
453 inode->osd1.linux1.l_i_version = ver;
454 if (EXT4_FITS_IN_INODE(inode, i_version_hi))
455 inode->i_version_hi = ver >> 32;
458 static void init_times(struct ext2_inode_large *inode)
463 EXT4_INODE_SET_XTIME(i_atime, &now, inode);
464 EXT4_INODE_SET_XTIME(i_ctime, &now, inode);
465 EXT4_INODE_SET_XTIME(i_mtime, &now, inode);
466 EXT4_EINODE_SET_XTIME(i_crtime, &now, inode);
467 increment_version(inode);
470 static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
471 struct ext2_inode_large *pinode)
475 struct ext2_inode_large inode;
479 /* If user already has a inode buffer, just update that */
481 increment_version(pinode);
482 EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
486 /* Otherwise we have to read-modify-write the inode */
487 memset(&inode, 0, sizeof(inode));
488 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
491 return translate_error(fs, ino, err);
493 increment_version(&inode);
494 EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
496 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
499 return translate_error(fs, ino, err);
504 static int update_atime(ext2_filsys fs, ext2_ino_t ino)
507 struct ext2_inode_large inode, *pinode;
508 struct timespec atime, mtime, now;
510 if (!(fs->flags & EXT2_FLAG_RW))
512 memset(&inode, 0, sizeof(inode));
513 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
516 return translate_error(fs, ino, err);
519 EXT4_INODE_GET_XTIME(i_atime, &atime, pinode);
520 EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode);
523 * If atime is newer than mtime and atime hasn't been updated in thirty
524 * seconds, skip the atime update. Same idea as Linux "relatime".
526 if (atime.tv_sec >= mtime.tv_sec && atime.tv_sec >= now.tv_sec - 30)
528 EXT4_INODE_SET_XTIME(i_atime, &now, &inode);
530 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
533 return translate_error(fs, ino, err);
538 static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
539 struct ext2_inode_large *pinode)
542 struct ext2_inode_large inode;
547 EXT4_INODE_SET_XTIME(i_mtime, &now, pinode);
548 EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
549 increment_version(pinode);
553 memset(&inode, 0, sizeof(inode));
554 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
557 return translate_error(fs, ino, err);
560 EXT4_INODE_SET_XTIME(i_mtime, &now, &inode);
561 EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
562 increment_version(&inode);
564 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
567 return translate_error(fs, ino, err);
572 static int ext2_file_type(unsigned int mode)
574 if (LINUX_S_ISREG(mode))
575 return EXT2_FT_REG_FILE;
577 if (LINUX_S_ISDIR(mode))
580 if (LINUX_S_ISCHR(mode))
581 return EXT2_FT_CHRDEV;
583 if (LINUX_S_ISBLK(mode))
584 return EXT2_FT_BLKDEV;
586 if (LINUX_S_ISLNK(mode))
587 return EXT2_FT_SYMLINK;
589 if (LINUX_S_ISFIFO(mode))
592 if (LINUX_S_ISSOCK(mode))
598 static int fs_can_allocate(struct fuse2fs *ff, blk64_t num)
600 ext2_filsys fs = ff->fs;
603 dbg_printf("%s: Asking for %llu; alloc_all=%d total=%llu free=%llu "
604 "rsvd=%llu\n", __func__, num, ff->alloc_all_blocks,
605 ext2fs_blocks_count(fs->super),
606 ext2fs_free_blocks_count(fs->super),
607 ext2fs_r_blocks_count(fs->super));
608 if (num > ext2fs_blocks_count(fs->super))
611 if (ff->alloc_all_blocks)
615 * Different meaning for r_blocks -- libext2fs has bugs where the FS
616 * can get corrupted if it totally runs out of blocks. Avoid this
617 * by refusing to allocate any of the reserve blocks to anybody.
619 reserved = ext2fs_r_blocks_count(fs->super);
621 reserved = ext2fs_blocks_count(fs->super) / 10;
622 return ext2fs_free_blocks_count(fs->super) > reserved + num;
625 static int fs_writeable(ext2_filsys fs)
627 return (fs->flags & EXT2_FLAG_RW) && (fs->super->s_error_count == 0);
630 static int check_inum_access(ext2_filsys fs, ext2_ino_t ino, mode_t mask)
632 struct fuse_context *ctxt = fuse_get_context();
633 struct ext2_inode inode;
637 /* no writing to read-only or broken fs */
638 if ((mask & W_OK) && !fs_writeable(fs))
641 err = ext2fs_read_inode(fs, ino, &inode);
643 return translate_error(fs, ino, err);
644 perms = inode.i_mode & 0777;
646 dbg_printf("access ino=%d mask=e%s%s%s perms=0%o fuid=%d fgid=%d "
647 "uid=%d gid=%d\n", ino,
648 (mask & R_OK ? "r" : ""), (mask & W_OK ? "w" : ""),
649 (mask & X_OK ? "x" : ""), perms, inode.i_uid, inode.i_gid,
650 ctxt->uid, ctxt->gid);
652 /* existence check */
658 (inode.i_flags & EXT2_IMMUTABLE_FL))
661 /* Figure out what root's allowed to do */
662 if (ctxt->uid == 0) {
663 /* Non-file access always ok */
664 if (!LINUX_S_ISREG(inode.i_mode))
667 /* R/W access to a file always ok */
671 /* X access to a file ok if a user/group/other can X */
675 /* Trying to execute a file that's not executable. BZZT! */
679 /* allow owner, if perms match */
680 if (inode.i_uid == ctxt->uid) {
681 if ((mask & (perms >> 6)) == mask)
686 /* allow group, if perms match */
687 if (inode.i_gid == ctxt->gid) {
688 if ((mask & (perms >> 3)) == mask)
693 /* otherwise check other */
694 if ((mask & perms) == mask)
699 static void op_destroy(void *p EXT2FS_ATTR((unused)))
701 struct fuse_context *ctxt = fuse_get_context();
702 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
706 if (ff->magic != FUSE2FS_MAGIC) {
707 translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC);
711 dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
712 if (fs->flags & EXT2_FLAG_RW) {
713 fs->super->s_state |= EXT2_VALID_FS;
714 if (fs->super->s_error_count)
715 fs->super->s_state |= EXT2_ERROR_FS;
716 ext2fs_mark_super_dirty(fs);
717 err = ext2fs_set_gdt_csum(fs);
719 translate_error(fs, 0, err);
721 err = ext2fs_flush2(fs, 0);
723 translate_error(fs, 0, err);
727 static void *op_init(struct fuse_conn_info *conn)
729 struct fuse_context *ctxt = fuse_get_context();
730 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
734 if (ff->magic != FUSE2FS_MAGIC) {
735 translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC);
739 dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
740 #ifdef FUSE_CAP_IOCTL_DIR
741 conn->want |= FUSE_CAP_IOCTL_DIR;
743 if (fs->flags & EXT2_FLAG_RW) {
744 fs->super->s_mnt_count++;
745 fs->super->s_mtime = time(NULL);
746 fs->super->s_state &= ~EXT2_VALID_FS;
747 ext2fs_mark_super_dirty(fs);
748 err = ext2fs_flush2(fs, 0);
750 translate_error(fs, 0, err);
755 static blkcnt_t blocks_from_inode(ext2_filsys fs,
756 struct ext2_inode_large *inode)
761 if (ext2fs_has_feature_huge_file(fs->super))
762 b += ((long long) inode->osd2.linux2.l_i_blocks_hi) << 32;
764 if (!ext2fs_has_feature_huge_file(fs->super) ||
765 !(inode->i_flags & EXT4_HUGE_FILE_FL))
766 b *= fs->blocksize / 512;
767 b *= EXT2FS_CLUSTER_RATIO(fs);
772 static int stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *statbuf)
774 struct ext2_inode_large inode;
780 memset(&inode, 0, sizeof(inode));
781 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
784 return translate_error(fs, ino, err);
786 memcpy(&fakedev, fs->super->s_uuid, sizeof(fakedev));
787 statbuf->st_dev = fakedev;
788 statbuf->st_ino = ino;
789 statbuf->st_mode = inode.i_mode;
790 statbuf->st_nlink = inode.i_links_count;
791 statbuf->st_uid = inode.i_uid;
792 statbuf->st_gid = inode.i_gid;
793 statbuf->st_size = EXT2_I_SIZE(&inode);
794 statbuf->st_blksize = fs->blocksize;
795 statbuf->st_blocks = blocks_from_inode(fs, &inode);
796 EXT4_INODE_GET_XTIME(i_atime, &tv, &inode);
797 statbuf->st_atime = tv.tv_sec;
798 EXT4_INODE_GET_XTIME(i_mtime, &tv, &inode);
799 statbuf->st_mtime = tv.tv_sec;
800 EXT4_INODE_GET_XTIME(i_ctime, &tv, &inode);
801 statbuf->st_ctime = tv.tv_sec;
802 if (LINUX_S_ISCHR(inode.i_mode) ||
803 LINUX_S_ISBLK(inode.i_mode)) {
804 if (inode.i_block[0])
805 statbuf->st_rdev = inode.i_block[0];
807 statbuf->st_rdev = inode.i_block[1];
813 static int op_getattr(const char *path, struct stat *statbuf)
815 struct fuse_context *ctxt = fuse_get_context();
816 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
822 FUSE2FS_CHECK_CONTEXT(ff);
824 dbg_printf("%s: path=%s\n", __func__, path);
825 pthread_mutex_lock(&ff->bfl);
826 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
828 ret = translate_error(fs, 0, err);
831 ret = stat_inode(fs, ino, statbuf);
833 pthread_mutex_unlock(&ff->bfl);
837 static int op_readlink(const char *path, char *buf, size_t len)
839 struct fuse_context *ctxt = fuse_get_context();
840 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
844 struct ext2_inode inode;
849 FUSE2FS_CHECK_CONTEXT(ff);
851 dbg_printf("%s: path=%s\n", __func__, path);
852 pthread_mutex_lock(&ff->bfl);
853 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
854 if (err || ino == 0) {
855 ret = translate_error(fs, 0, err);
859 err = ext2fs_read_inode(fs, ino, &inode);
861 ret = translate_error(fs, ino, err);
865 if (!LINUX_S_ISLNK(inode.i_mode)) {
871 if (inode.i_size < len)
873 if (ext2fs_is_fast_symlink(&inode))
874 memcpy(buf, (char *)inode.i_block, len);
876 /* big/inline symlink */
878 err = ext2fs_file_open(fs, ino, 0, &file);
880 ret = translate_error(fs, ino, err);
884 err = ext2fs_file_read(file, buf, len, &got);
885 if (err || got != len) {
886 ext2fs_file_close(file);
887 ret = translate_error(fs, ino, err);
892 err = ext2fs_file_close(file);
896 ret = translate_error(fs, ino, err);
902 if (fs_writeable(fs)) {
903 ret = update_atime(fs, ino);
909 pthread_mutex_unlock(&ff->bfl);
913 static int op_mknod(const char *path, mode_t mode, dev_t dev)
915 struct fuse_context *ctxt = fuse_get_context();
916 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
918 ext2_ino_t parent, child;
923 struct ext2_inode_large inode;
926 FUSE2FS_CHECK_CONTEXT(ff);
928 dbg_printf("%s: path=%s mode=0%o dev=0x%x\n", __func__, path, mode,
930 temp_path = strdup(path);
935 node_name = strrchr(temp_path, '/');
944 pthread_mutex_lock(&ff->bfl);
945 if (!fs_can_allocate(ff, 2)) {
950 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
953 ret = translate_error(fs, 0, err);
957 ret = check_inum_access(fs, parent, W_OK);
963 if (LINUX_S_ISCHR(mode))
964 filetype = EXT2_FT_CHRDEV;
965 else if (LINUX_S_ISBLK(mode))
966 filetype = EXT2_FT_BLKDEV;
967 else if (LINUX_S_ISFIFO(mode))
968 filetype = EXT2_FT_FIFO;
969 else if (LINUX_S_ISSOCK(mode))
970 filetype = EXT2_FT_SOCK;
976 err = ext2fs_new_inode(fs, parent, mode, 0, &child);
978 ret = translate_error(fs, 0, err);
982 dbg_printf("%s: create ino=%d/name=%s in dir=%d\n", __func__, child,
984 err = ext2fs_link(fs, parent, node_name, child, filetype);
985 if (err == EXT2_ET_DIR_NO_SPACE) {
986 err = ext2fs_expand_dir(fs, parent);
988 ret = translate_error(fs, parent, err);
992 err = ext2fs_link(fs, parent, node_name, child,
996 ret = translate_error(fs, parent, err);
1000 ret = update_mtime(fs, parent, NULL);
1004 memset(&inode, 0, sizeof(inode));
1005 inode.i_mode = mode;
1008 inode.i_block[1] = dev;
1010 inode.i_block[0] = dev;
1011 inode.i_links_count = 1;
1012 inode.i_extra_isize = sizeof(struct ext2_inode_large) -
1013 EXT2_GOOD_OLD_INODE_SIZE;
1014 inode.i_uid = ctxt->uid;
1015 inode.i_gid = ctxt->gid;
1017 err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode);
1019 ret = translate_error(fs, child, err);
1023 inode.i_generation = ff->next_generation++;
1025 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1028 ret = translate_error(fs, child, err);
1032 ext2fs_inode_alloc_stats2(fs, child, 1, 0);
1035 pthread_mutex_unlock(&ff->bfl);
1041 static int op_mkdir(const char *path, mode_t mode)
1043 struct fuse_context *ctxt = fuse_get_context();
1044 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1046 ext2_ino_t parent, child;
1050 struct ext2_inode_large inode;
1056 FUSE2FS_CHECK_CONTEXT(ff);
1058 dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode);
1059 temp_path = strdup(path);
1064 node_name = strrchr(temp_path, '/');
1073 pthread_mutex_lock(&ff->bfl);
1074 if (!fs_can_allocate(ff, 1)) {
1079 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1082 ret = translate_error(fs, 0, err);
1086 ret = check_inum_access(fs, parent, W_OK);
1090 /* Is the parent dir sgid? */
1091 err = ext2fs_read_inode_full(fs, parent, (struct ext2_inode *)&inode,
1094 ret = translate_error(fs, parent, err);
1097 parent_sgid = inode.i_mode & S_ISGID;
1101 err = ext2fs_mkdir(fs, parent, 0, node_name);
1102 if (err == EXT2_ET_DIR_NO_SPACE) {
1103 err = ext2fs_expand_dir(fs, parent);
1105 ret = translate_error(fs, parent, err);
1109 err = ext2fs_mkdir(fs, parent, 0, node_name);
1112 ret = translate_error(fs, parent, err);
1116 ret = update_mtime(fs, parent, NULL);
1120 /* Still have to update the uid/gid of the dir */
1121 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1124 ret = translate_error(fs, 0, err);
1127 dbg_printf("%s: created ino=%d/path=%s in dir=%d\n", __func__, child,
1130 memset(&inode, 0, sizeof(inode));
1131 err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode,
1134 ret = translate_error(fs, child, err);
1138 inode.i_uid = ctxt->uid;
1139 inode.i_gid = ctxt->gid;
1140 inode.i_mode = LINUX_S_IFDIR | (mode & ~(S_ISUID | fs->umask)) |
1142 inode.i_generation = ff->next_generation++;
1144 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1147 ret = translate_error(fs, child, err);
1151 /* Rewrite the directory block checksum, having set i_generation */
1152 if ((inode.i_flags & EXT4_INLINE_DATA_FL) ||
1153 !ext2fs_has_feature_metadata_csum(fs->super))
1155 err = ext2fs_new_dir_block(fs, child, parent, &block);
1157 ret = translate_error(fs, child, err);
1160 err = ext2fs_bmap2(fs, child, (struct ext2_inode *)&inode, NULL, 0, 0,
1163 ret = translate_error(fs, child, err);
1166 err = ext2fs_write_dir_block4(fs, blk, block, 0, child);
1168 ret = translate_error(fs, child, err);
1173 ext2fs_free_mem(&block);
1175 pthread_mutex_unlock(&ff->bfl);
1181 static int unlink_file_by_name(ext2_filsys fs, const char *path)
1185 char *filename = strdup(path);
1189 base_name = strrchr(filename, '/');
1191 *base_name++ = '\0';
1192 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, filename,
1196 return translate_error(fs, 0, err);
1199 dir = EXT2_ROOT_INO;
1200 base_name = filename;
1203 ret = check_inum_access(fs, dir, W_OK);
1209 dbg_printf("%s: unlinking name=%s from dir=%d\n", __func__,
1211 err = ext2fs_unlink(fs, dir, base_name, 0, 0);
1214 return translate_error(fs, dir, err);
1216 return update_mtime(fs, dir, NULL);
1219 static int remove_inode(struct fuse2fs *ff, ext2_ino_t ino)
1221 ext2_filsys fs = ff->fs;
1223 struct ext2_inode_large inode;
1226 memset(&inode, 0, sizeof(inode));
1227 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1230 ret = translate_error(fs, ino, err);
1233 dbg_printf("%s: put ino=%d links=%d\n", __func__, ino,
1234 inode.i_links_count);
1236 switch (inode.i_links_count) {
1238 return 0; /* XXX: already done? */
1240 inode.i_links_count--;
1241 inode.i_dtime = fs->now ? fs->now : time(0);
1244 inode.i_links_count--;
1247 ret = update_ctime(fs, ino, &inode);
1251 if (inode.i_links_count)
1254 /* Nobody holds this file; free its blocks! */
1255 err = ext2fs_free_ext_attr(fs, ino, &inode);
1259 if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1260 err = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1263 ret = translate_error(fs, ino, err);
1268 ext2fs_inode_alloc_stats2(fs, ino, -1,
1269 LINUX_S_ISDIR(inode.i_mode));
1272 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1275 ret = translate_error(fs, ino, err);
1282 static int __op_unlink(struct fuse2fs *ff, const char *path)
1284 ext2_filsys fs = ff->fs;
1289 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1291 ret = translate_error(fs, 0, err);
1295 ret = unlink_file_by_name(fs, path);
1299 ret = remove_inode(ff, ino);
1306 static int op_unlink(const char *path)
1308 struct fuse_context *ctxt = fuse_get_context();
1309 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1312 FUSE2FS_CHECK_CONTEXT(ff);
1313 pthread_mutex_lock(&ff->bfl);
1314 ret = __op_unlink(ff, path);
1315 pthread_mutex_unlock(&ff->bfl);
1324 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1325 int entry EXT2FS_ATTR((unused)),
1326 struct ext2_dir_entry *dirent,
1327 int offset EXT2FS_ATTR((unused)),
1328 int blocksize EXT2FS_ATTR((unused)),
1329 char *buf EXT2FS_ATTR((unused)),
1332 struct rd_struct *rds = (struct rd_struct *) private;
1334 if (dirent->inode == 0)
1336 if (((dirent->name_len & 0xFF) == 1) && (dirent->name[0] == '.'))
1338 if (((dirent->name_len & 0xFF) == 2) && (dirent->name[0] == '.') &&
1339 (dirent->name[1] == '.')) {
1340 rds->parent = dirent->inode;
1347 static int __op_rmdir(struct fuse2fs *ff, const char *path)
1349 ext2_filsys fs = ff->fs;
1352 struct ext2_inode_large inode;
1353 struct rd_struct rds;
1356 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &child);
1358 ret = translate_error(fs, 0, err);
1361 dbg_printf("%s: rmdir path=%s ino=%d\n", __func__, path, child);
1366 err = ext2fs_dir_iterate2(fs, child, 0, 0, rmdir_proc, &rds);
1368 ret = translate_error(fs, child, err);
1372 if (rds.empty == 0) {
1377 ret = unlink_file_by_name(fs, path);
1380 /* Directories have to be "removed" twice. */
1381 ret = remove_inode(ff, child);
1384 ret = remove_inode(ff, child);
1389 dbg_printf("%s: decr dir=%d link count\n", __func__,
1391 err = ext2fs_read_inode_full(fs, rds.parent,
1392 (struct ext2_inode *)&inode,
1395 ret = translate_error(fs, rds.parent, err);
1398 if (inode.i_links_count > 1)
1399 inode.i_links_count--;
1400 ret = update_mtime(fs, rds.parent, &inode);
1403 err = ext2fs_write_inode_full(fs, rds.parent,
1404 (struct ext2_inode *)&inode,
1407 ret = translate_error(fs, rds.parent, err);
1416 static int op_rmdir(const char *path)
1418 struct fuse_context *ctxt = fuse_get_context();
1419 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1422 FUSE2FS_CHECK_CONTEXT(ff);
1423 pthread_mutex_lock(&ff->bfl);
1424 ret = __op_rmdir(ff, path);
1425 pthread_mutex_unlock(&ff->bfl);
1429 static int op_symlink(const char *src, const char *dest)
1431 struct fuse_context *ctxt = fuse_get_context();
1432 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1434 ext2_ino_t parent, child;
1438 struct ext2_inode_large inode;
1441 FUSE2FS_CHECK_CONTEXT(ff);
1443 dbg_printf("%s: symlink %s to %s\n", __func__, src, dest);
1444 temp_path = strdup(dest);
1449 node_name = strrchr(temp_path, '/');
1458 pthread_mutex_lock(&ff->bfl);
1459 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1463 ret = translate_error(fs, 0, err);
1467 ret = check_inum_access(fs, parent, W_OK);
1472 /* Create symlink */
1473 err = ext2fs_symlink(fs, parent, 0, node_name, src);
1474 if (err == EXT2_ET_DIR_NO_SPACE) {
1475 err = ext2fs_expand_dir(fs, parent);
1477 ret = translate_error(fs, parent, err);
1481 err = ext2fs_symlink(fs, parent, 0, node_name, src);
1484 ret = translate_error(fs, parent, err);
1488 /* Update parent dir's mtime */
1489 ret = update_mtime(fs, parent, NULL);
1493 /* Still have to update the uid/gid of the symlink */
1494 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1497 ret = translate_error(fs, 0, err);
1500 dbg_printf("%s: symlinking ino=%d/name=%s to dir=%d\n", __func__,
1501 child, node_name, parent);
1503 memset(&inode, 0, sizeof(inode));
1504 err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode,
1507 ret = translate_error(fs, child, err);
1511 inode.i_uid = ctxt->uid;
1512 inode.i_gid = ctxt->gid;
1513 inode.i_generation = ff->next_generation++;
1515 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1518 ret = translate_error(fs, child, err);
1522 pthread_mutex_unlock(&ff->bfl);
1528 struct update_dotdot {
1529 ext2_ino_t new_dotdot;
1532 static int update_dotdot_helper(ext2_ino_t dir EXT2FS_ATTR((unused)),
1533 int entry EXT2FS_ATTR((unused)),
1534 struct ext2_dir_entry *dirent,
1535 int offset EXT2FS_ATTR((unused)),
1536 int blocksize EXT2FS_ATTR((unused)),
1537 char *buf EXT2FS_ATTR((unused)),
1540 struct update_dotdot *ud = priv_data;
1542 if (ext2fs_dirent_name_len(dirent) == 2 &&
1543 dirent->name[0] == '.' && dirent->name[1] == '.') {
1544 dirent->inode = ud->new_dotdot;
1545 return DIRENT_CHANGED | DIRENT_ABORT;
1551 static int op_rename(const char *from, const char *to)
1553 struct fuse_context *ctxt = fuse_get_context();
1554 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1557 ext2_ino_t from_ino, to_ino, to_dir_ino, from_dir_ino;
1558 char *temp_to = NULL, *temp_from = NULL;
1560 struct ext2_inode inode;
1561 struct update_dotdot ud;
1564 FUSE2FS_CHECK_CONTEXT(ff);
1566 dbg_printf("%s: renaming %s to %s\n", __func__, from, to);
1567 pthread_mutex_lock(&ff->bfl);
1568 if (!fs_can_allocate(ff, 5)) {
1573 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, from, &from_ino);
1574 if (err || from_ino == 0) {
1575 ret = translate_error(fs, 0, err);
1579 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, to, &to_ino);
1580 if (err && err != EXT2_ET_FILE_NOT_FOUND) {
1581 ret = translate_error(fs, 0, err);
1585 if (err == EXT2_ET_FILE_NOT_FOUND)
1588 /* Already the same file? */
1589 if (to_ino != 0 && to_ino == from_ino) {
1594 temp_to = strdup(to);
1600 temp_from = strdup(from);
1606 /* Find parent dir of the source and check write access */
1607 cp = strrchr(temp_from, '/');
1615 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_from,
1619 ret = translate_error(fs, 0, err);
1622 if (from_dir_ino == 0) {
1627 ret = check_inum_access(fs, from_dir_ino, W_OK);
1631 /* Find parent dir of the destination and check write access */
1632 cp = strrchr(temp_to, '/');
1640 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_to,
1644 ret = translate_error(fs, 0, err);
1647 if (to_dir_ino == 0) {
1652 ret = check_inum_access(fs, to_dir_ino, W_OK);
1656 /* If the target exists, unlink it first */
1658 err = ext2fs_read_inode(fs, to_ino, &inode);
1660 ret = translate_error(fs, to_ino, err);
1664 dbg_printf("%s: unlinking %s ino=%d\n", __func__,
1665 LINUX_S_ISDIR(inode.i_mode) ? "dir" : "file",
1667 if (LINUX_S_ISDIR(inode.i_mode))
1668 ret = __op_rmdir(ff, to);
1670 ret = __op_unlink(ff, to);
1675 /* Get ready to do the move */
1676 err = ext2fs_read_inode(fs, from_ino, &inode);
1678 ret = translate_error(fs, from_ino, err);
1682 /* Link in the new file */
1683 dbg_printf("%s: linking ino=%d/path=%s to dir=%d\n", __func__,
1684 from_ino, cp + 1, to_dir_ino);
1685 err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino,
1686 ext2_file_type(inode.i_mode));
1687 if (err == EXT2_ET_DIR_NO_SPACE) {
1688 err = ext2fs_expand_dir(fs, to_dir_ino);
1690 ret = translate_error(fs, to_dir_ino, err);
1694 err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino,
1695 ext2_file_type(inode.i_mode));
1698 ret = translate_error(fs, to_dir_ino, err);
1702 /* Update '..' pointer if dir */
1703 err = ext2fs_read_inode(fs, from_ino, &inode);
1705 ret = translate_error(fs, from_ino, err);
1709 if (LINUX_S_ISDIR(inode.i_mode)) {
1710 ud.new_dotdot = to_dir_ino;
1711 dbg_printf("%s: updating .. entry for dir=%d\n", __func__,
1713 err = ext2fs_dir_iterate2(fs, from_ino, 0, NULL,
1714 update_dotdot_helper, &ud);
1716 ret = translate_error(fs, from_ino, err);
1720 /* Decrease from_dir_ino's links_count */
1721 dbg_printf("%s: moving linkcount from dir=%d to dir=%d\n",
1722 __func__, from_dir_ino, to_dir_ino);
1723 err = ext2fs_read_inode(fs, from_dir_ino, &inode);
1725 ret = translate_error(fs, from_dir_ino, err);
1728 inode.i_links_count--;
1729 err = ext2fs_write_inode(fs, from_dir_ino, &inode);
1731 ret = translate_error(fs, from_dir_ino, err);
1735 /* Increase to_dir_ino's links_count */
1736 err = ext2fs_read_inode(fs, to_dir_ino, &inode);
1738 ret = translate_error(fs, to_dir_ino, err);
1741 inode.i_links_count++;
1742 err = ext2fs_write_inode(fs, to_dir_ino, &inode);
1744 ret = translate_error(fs, to_dir_ino, err);
1749 /* Update timestamps */
1750 ret = update_ctime(fs, from_ino, NULL);
1754 ret = update_mtime(fs, to_dir_ino, NULL);
1758 /* Remove the old file */
1759 ret = unlink_file_by_name(fs, from);
1763 /* Flush the whole mess out */
1764 err = ext2fs_flush2(fs, 0);
1766 ret = translate_error(fs, 0, err);
1772 pthread_mutex_unlock(&ff->bfl);
1776 static int op_link(const char *src, const char *dest)
1778 struct fuse_context *ctxt = fuse_get_context();
1779 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1784 ext2_ino_t parent, ino;
1785 struct ext2_inode_large inode;
1788 FUSE2FS_CHECK_CONTEXT(ff);
1790 dbg_printf("%s: src=%s dest=%s\n", __func__, src, dest);
1791 temp_path = strdup(dest);
1796 node_name = strrchr(temp_path, '/');
1805 pthread_mutex_lock(&ff->bfl);
1806 if (!fs_can_allocate(ff, 2)) {
1811 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1819 ret = check_inum_access(fs, parent, W_OK);
1824 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, src, &ino);
1825 if (err || ino == 0) {
1826 ret = translate_error(fs, 0, err);
1830 memset(&inode, 0, sizeof(inode));
1831 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1834 ret = translate_error(fs, ino, err);
1838 inode.i_links_count++;
1839 ret = update_ctime(fs, ino, &inode);
1843 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1846 ret = translate_error(fs, ino, err);
1850 dbg_printf("%s: linking ino=%d/name=%s to dir=%d\n", __func__, ino,
1852 err = ext2fs_link(fs, parent, node_name, ino,
1853 ext2_file_type(inode.i_mode));
1854 if (err == EXT2_ET_DIR_NO_SPACE) {
1855 err = ext2fs_expand_dir(fs, parent);
1857 ret = translate_error(fs, parent, err);
1861 err = ext2fs_link(fs, parent, node_name, ino,
1862 ext2_file_type(inode.i_mode));
1865 ret = translate_error(fs, parent, err);
1869 ret = update_mtime(fs, parent, NULL);
1874 pthread_mutex_unlock(&ff->bfl);
1880 static int op_chmod(const char *path, mode_t mode)
1882 struct fuse_context *ctxt = fuse_get_context();
1883 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1887 struct ext2_inode_large inode;
1890 FUSE2FS_CHECK_CONTEXT(ff);
1892 pthread_mutex_lock(&ff->bfl);
1893 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1895 ret = translate_error(fs, 0, err);
1898 dbg_printf("%s: path=%s mode=0%o ino=%d\n", __func__, path, mode, ino);
1900 memset(&inode, 0, sizeof(inode));
1901 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1904 ret = translate_error(fs, ino, err);
1908 if (ctxt->uid != 0 && ctxt->uid != inode.i_uid) {
1914 * XXX: We should really check that the inode gid is not in /any/
1915 * of the user's groups, but FUSE only tells us about the primary
1918 if (ctxt->uid != 0 && ctxt->gid != inode.i_gid)
1921 inode.i_mode &= ~0xFFF;
1922 inode.i_mode |= mode & 0xFFF;
1923 ret = update_ctime(fs, ino, &inode);
1927 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1930 ret = translate_error(fs, ino, err);
1935 pthread_mutex_unlock(&ff->bfl);
1939 static int op_chown(const char *path, uid_t owner, gid_t group)
1941 struct fuse_context *ctxt = fuse_get_context();
1942 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1946 struct ext2_inode_large inode;
1949 FUSE2FS_CHECK_CONTEXT(ff);
1951 pthread_mutex_lock(&ff->bfl);
1952 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1954 ret = translate_error(fs, 0, err);
1957 dbg_printf("%s: path=%s owner=%d group=%d ino=%d\n", __func__,
1958 path, owner, group, ino);
1960 memset(&inode, 0, sizeof(inode));
1961 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1964 ret = translate_error(fs, ino, err);
1968 /* FUSE seems to feed us ~0 to mean "don't change" */
1969 if (owner != (uid_t) ~0) {
1970 /* Only root gets to change UID. */
1971 if (ctxt->uid != 0 &&
1972 !(inode.i_uid == ctxt->uid && owner == ctxt->uid)) {
1976 inode.i_uid = owner;
1979 if (group != (gid_t) ~0) {
1980 /* Only root or the owner get to change GID. */
1981 if (ctxt->uid != 0 && inode.i_uid != ctxt->uid) {
1986 /* XXX: We /should/ check group membership but FUSE */
1987 inode.i_gid = group;
1990 ret = update_ctime(fs, ino, &inode);
1994 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1997 ret = translate_error(fs, ino, err);
2002 pthread_mutex_unlock(&ff->bfl);
2006 static int op_truncate(const char *path, off_t len)
2008 struct fuse_context *ctxt = fuse_get_context();
2009 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2016 FUSE2FS_CHECK_CONTEXT(ff);
2018 pthread_mutex_lock(&ff->bfl);
2019 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2020 if (err || ino == 0) {
2021 ret = translate_error(fs, 0, err);
2024 dbg_printf("%s: ino=%d len=%jd\n", __func__, ino, len);
2026 ret = check_inum_access(fs, ino, W_OK);
2030 err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
2032 ret = translate_error(fs, ino, err);
2036 err = ext2fs_file_set_size2(file, len);
2038 ret = translate_error(fs, ino, err);
2043 err = ext2fs_file_close(file);
2047 ret = translate_error(fs, ino, err);
2051 ret = update_mtime(fs, ino, NULL);
2054 pthread_mutex_unlock(&ff->bfl);
2059 static void detect_linux_executable_open(int kernel_flags, int *access_check,
2060 int *e2fs_open_flags)
2063 * On Linux, execve will bleed __FMODE_EXEC into the file mode flags,
2064 * and FUSE is more than happy to let that slip through.
2066 if (kernel_flags & 0x20) {
2067 *access_check = X_OK;
2068 *e2fs_open_flags &= ~EXT2_FILE_WRITE;
2072 static void detect_linux_executable_open(int kernel_flags, int *access_check,
2073 int *e2fs_open_flags)
2077 #endif /* __linux__ */
2079 static int __op_open(struct fuse2fs *ff, const char *path,
2080 struct fuse_file_info *fp)
2082 ext2_filsys fs = ff->fs;
2084 struct fuse2fs_file_handle *file;
2085 int check = 0, ret = 0;
2087 dbg_printf("%s: path=%s\n", __func__, path);
2088 err = ext2fs_get_mem(sizeof(*file), &file);
2090 return translate_error(fs, 0, err);
2091 file->magic = FUSE2FS_FILE_MAGIC;
2093 file->open_flags = 0;
2094 switch (fp->flags & O_ACCMODE) {
2100 file->open_flags |= EXT2_FILE_WRITE;
2103 check = R_OK | W_OK;
2104 file->open_flags |= EXT2_FILE_WRITE;
2108 detect_linux_executable_open(fp->flags, &check, &file->open_flags);
2110 if (fp->flags & O_CREAT)
2111 file->open_flags |= EXT2_FILE_CREATE;
2113 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &file->ino);
2114 if (err || file->ino == 0) {
2115 ret = translate_error(fs, 0, err);
2118 dbg_printf("%s: ino=%d\n", __func__, file->ino);
2120 ret = check_inum_access(fs, file->ino, check);
2123 * In a regular (Linux) fs driver, the kernel will open
2124 * binaries for reading if the user has --x privileges (i.e.
2125 * execute without read). Since the kernel doesn't have any
2126 * way to tell us if it's opening a file via execve, we'll
2127 * just assume that allowing access is ok if asking for ro mode
2128 * fails but asking for x mode succeeds. Of course we can
2129 * also employ undocumented hacks (see above).
2131 if (check == R_OK) {
2132 ret = check_inum_access(fs, file->ino, X_OK);
2138 fp->fh = (uintptr_t)file;
2142 ext2fs_free_mem(&file);
2146 static int op_open(const char *path, struct fuse_file_info *fp)
2148 struct fuse_context *ctxt = fuse_get_context();
2149 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2152 FUSE2FS_CHECK_CONTEXT(ff);
2153 pthread_mutex_lock(&ff->bfl);
2154 ret = __op_open(ff, path, fp);
2155 pthread_mutex_unlock(&ff->bfl);
2159 static int op_read(const char *path EXT2FS_ATTR((unused)), char *buf,
2160 size_t len, off_t offset,
2161 struct fuse_file_info *fp)
2163 struct fuse_context *ctxt = fuse_get_context();
2164 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2165 struct fuse2fs_file_handle *fh =
2166 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2170 unsigned int got = 0;
2173 FUSE2FS_CHECK_CONTEXT(ff);
2175 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2176 dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset,
2178 pthread_mutex_lock(&ff->bfl);
2179 err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
2181 ret = translate_error(fs, fh->ino, err);
2185 err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL);
2187 ret = translate_error(fs, fh->ino, err);
2191 err = ext2fs_file_read(efp, buf, len, &got);
2193 ret = translate_error(fs, fh->ino, err);
2198 err = ext2fs_file_close(efp);
2202 ret = translate_error(fs, fh->ino, err);
2206 if (fs_writeable(fs)) {
2207 ret = update_atime(fs, fh->ino);
2212 pthread_mutex_unlock(&ff->bfl);
2213 return got ? (int) got : ret;
2216 static int op_write(const char *path EXT2FS_ATTR((unused)),
2217 const char *buf, size_t len, off_t offset,
2218 struct fuse_file_info *fp)
2220 struct fuse_context *ctxt = fuse_get_context();
2221 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2222 struct fuse2fs_file_handle *fh =
2223 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2227 unsigned int got = 0;
2230 FUSE2FS_CHECK_CONTEXT(ff);
2232 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2233 dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset,
2235 pthread_mutex_lock(&ff->bfl);
2236 if (!fs_writeable(fs)) {
2241 if (!fs_can_allocate(ff, len / fs->blocksize)) {
2246 err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
2248 ret = translate_error(fs, fh->ino, err);
2252 err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL);
2254 ret = translate_error(fs, fh->ino, err);
2258 err = ext2fs_file_write(efp, buf, len, &got);
2260 ret = translate_error(fs, fh->ino, err);
2264 err = ext2fs_file_flush(efp);
2267 ret = translate_error(fs, fh->ino, err);
2272 err = ext2fs_file_close(efp);
2276 ret = translate_error(fs, fh->ino, err);
2280 ret = update_mtime(fs, fh->ino, NULL);
2285 pthread_mutex_unlock(&ff->bfl);
2286 return got ? (int) got : ret;
2289 static int op_release(const char *path EXT2FS_ATTR((unused)),
2290 struct fuse_file_info *fp)
2292 struct fuse_context *ctxt = fuse_get_context();
2293 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2294 struct fuse2fs_file_handle *fh =
2295 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2300 FUSE2FS_CHECK_CONTEXT(ff);
2302 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2303 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2304 pthread_mutex_lock(&ff->bfl);
2305 if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
2306 err = ext2fs_flush2(fs, EXT2_FLAG_FLUSH_NO_SYNC);
2308 ret = translate_error(fs, fh->ino, err);
2311 pthread_mutex_unlock(&ff->bfl);
2313 ext2fs_free_mem(&fh);
2318 static int op_fsync(const char *path EXT2FS_ATTR((unused)),
2319 int datasync EXT2FS_ATTR((unused)),
2320 struct fuse_file_info *fp)
2322 struct fuse_context *ctxt = fuse_get_context();
2323 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2324 struct fuse2fs_file_handle *fh =
2325 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2330 FUSE2FS_CHECK_CONTEXT(ff);
2332 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2333 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2334 /* For now, flush everything, even if it's slow */
2335 pthread_mutex_lock(&ff->bfl);
2336 if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
2337 err = ext2fs_flush2(fs, 0);
2339 ret = translate_error(fs, fh->ino, err);
2341 pthread_mutex_unlock(&ff->bfl);
2346 static int op_statfs(const char *path EXT2FS_ATTR((unused)),
2347 struct statvfs *buf)
2349 struct fuse_context *ctxt = fuse_get_context();
2350 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2353 blk64_t overhead, reserved, free;
2355 FUSE2FS_CHECK_CONTEXT(ff);
2357 dbg_printf("%s: path=%s\n", __func__, path);
2358 buf->f_bsize = fs->blocksize;
2364 overhead = fs->desc_blocks +
2365 fs->group_desc_count *
2366 (fs->inode_blocks_per_group + 2);
2367 reserved = ext2fs_r_blocks_count(fs->super);
2369 reserved = ext2fs_blocks_count(fs->super) / 10;
2370 free = ext2fs_free_blocks_count(fs->super);
2372 buf->f_blocks = ext2fs_blocks_count(fs->super) - overhead;
2373 buf->f_bfree = free;
2374 if (free < reserved)
2377 buf->f_bavail = free - reserved;
2378 buf->f_files = fs->super->s_inodes_count;
2379 buf->f_ffree = fs->super->s_free_inodes_count;
2380 buf->f_favail = fs->super->s_free_inodes_count;
2381 f = (uint64_t *)fs->super->s_uuid;
2387 if (fs->flags & EXT2_FLAG_RW)
2388 buf->f_flag |= ST_RDONLY;
2389 buf->f_namemax = EXT2_NAME_LEN;
2394 typedef errcode_t (*xattr_xlate_get)(void **cooked_buf, size_t *cooked_sz,
2395 const void *raw_buf, size_t raw_sz);
2396 typedef errcode_t (*xattr_xlate_set)(const void *cooked_buf, size_t cooked_sz,
2397 const void **raw_buf, size_t *raw_sz);
2398 struct xattr_translate {
2400 xattr_xlate_get get;
2401 xattr_xlate_set set;
2404 #define XATTR_TRANSLATOR(p, g, s) \
2406 .get = (xattr_xlate_get)(g), \
2407 .set = (xattr_xlate_set)(s)}
2409 static struct xattr_translate xattr_translators[] = {
2410 #ifdef TRANSLATE_LINUX_ACLS
2411 XATTR_TRANSLATOR(ACL_EA_ACCESS, ext4_to_fuse_acl, fuse_to_ext4_acl),
2412 XATTR_TRANSLATOR(ACL_EA_DEFAULT, ext4_to_fuse_acl, fuse_to_ext4_acl),
2414 XATTR_TRANSLATOR(NULL, NULL, NULL),
2416 #undef XATTR_TRANSLATOR
2418 static int op_getxattr(const char *path, const char *key, char *value,
2421 struct fuse_context *ctxt = fuse_get_context();
2422 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2424 struct ext2_xattr_handle *h;
2425 struct xattr_translate *xt;
2432 FUSE2FS_CHECK_CONTEXT(ff);
2434 pthread_mutex_lock(&ff->bfl);
2435 if (!ext2fs_has_feature_xattr(fs->super)) {
2440 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2441 if (err || ino == 0) {
2442 ret = translate_error(fs, 0, err);
2445 dbg_printf("%s: ino=%d\n", __func__, ino);
2447 ret = check_inum_access(fs, ino, R_OK);
2451 err = ext2fs_xattrs_open(fs, ino, &h);
2453 ret = translate_error(fs, ino, err);
2457 err = ext2fs_xattrs_read(h);
2459 ret = translate_error(fs, ino, err);
2463 err = ext2fs_xattr_get(h, key, &ptr, &plen);
2465 ret = translate_error(fs, ino, err);
2469 for (xt = xattr_translators; xt->prefix != NULL; xt++) {
2470 if (strncmp(key, xt->prefix, strlen(xt->prefix)) == 0) {
2471 err = xt->get(&cptr, &clen, ptr, plen);
2474 ext2fs_free_mem(&ptr);
2482 } else if (len < plen) {
2485 memcpy(value, ptr, plen);
2490 ext2fs_free_mem(&ptr);
2492 err = ext2fs_xattrs_close(&h);
2494 ret = translate_error(fs, ino, err);
2496 pthread_mutex_unlock(&ff->bfl);
2501 static int count_buffer_space(char *name, char *value EXT2FS_ATTR((unused)),
2502 size_t value_len EXT2FS_ATTR((unused)),
2505 unsigned int *x = data;
2507 *x = *x + strlen(name) + 1;
2511 static int copy_names(char *name, char *value EXT2FS_ATTR((unused)),
2512 size_t value_len EXT2FS_ATTR((unused)), void *data)
2516 strncpy(*b, name, strlen(name));
2517 *b = *b + strlen(name) + 1;
2522 static int op_listxattr(const char *path, char *names, size_t len)
2524 struct fuse_context *ctxt = fuse_get_context();
2525 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2527 struct ext2_xattr_handle *h;
2533 FUSE2FS_CHECK_CONTEXT(ff);
2535 pthread_mutex_lock(&ff->bfl);
2536 if (!ext2fs_has_feature_xattr(fs->super)) {
2541 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2542 if (err || ino == 0) {
2543 ret = translate_error(fs, ino, err);
2546 dbg_printf("%s: ino=%d\n", __func__, ino);
2548 ret = check_inum_access(fs, ino, R_OK);
2552 err = ext2fs_xattrs_open(fs, ino, &h);
2554 ret = translate_error(fs, ino, err);
2558 err = ext2fs_xattrs_read(h);
2560 ret = translate_error(fs, ino, err);
2564 /* Count buffer space needed for names */
2566 err = ext2fs_xattrs_iterate(h, count_buffer_space, &bufsz);
2568 ret = translate_error(fs, ino, err);
2575 } else if (len < bufsz) {
2580 /* Copy names out */
2581 memset(names, 0, len);
2582 err = ext2fs_xattrs_iterate(h, copy_names, &names);
2584 ret = translate_error(fs, ino, err);
2589 err = ext2fs_xattrs_close(&h);
2591 ret = translate_error(fs, ino, err);
2593 pthread_mutex_unlock(&ff->bfl);
2598 static int op_setxattr(const char *path EXT2FS_ATTR((unused)),
2599 const char *key, const char *value,
2600 size_t len, int flags EXT2FS_ATTR((unused)))
2602 struct fuse_context *ctxt = fuse_get_context();
2603 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2605 struct ext2_xattr_handle *h;
2606 struct xattr_translate *xt;
2613 FUSE2FS_CHECK_CONTEXT(ff);
2615 pthread_mutex_lock(&ff->bfl);
2616 if (!ext2fs_has_feature_xattr(fs->super)) {
2621 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2622 if (err || ino == 0) {
2623 ret = translate_error(fs, 0, err);
2626 dbg_printf("%s: ino=%d\n", __func__, ino);
2628 ret = check_inum_access(fs, ino, W_OK);
2629 if (ret == -EACCES) {
2635 err = ext2fs_xattrs_open(fs, ino, &h);
2637 ret = translate_error(fs, ino, err);
2641 err = ext2fs_xattrs_read(h);
2643 ret = translate_error(fs, ino, err);
2649 for (xt = xattr_translators; xt->prefix != NULL; xt++) {
2650 if (strncmp(key, xt->prefix, strlen(xt->prefix)) == 0) {
2651 err = xt->set(value, len, &cvalue, &clen);
2657 err = ext2fs_xattr_set(h, key, cvalue, clen);
2659 ret = translate_error(fs, ino, err);
2663 ret = update_ctime(fs, ino, NULL);
2665 if (cvalue != value)
2666 ext2fs_free_mem(&cvalue);
2668 err = ext2fs_xattrs_close(&h);
2670 ret = translate_error(fs, ino, err);
2672 pthread_mutex_unlock(&ff->bfl);
2677 static int op_removexattr(const char *path, const char *key)
2679 struct fuse_context *ctxt = fuse_get_context();
2680 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2682 struct ext2_xattr_handle *h;
2687 FUSE2FS_CHECK_CONTEXT(ff);
2689 pthread_mutex_lock(&ff->bfl);
2690 if (!ext2fs_has_feature_xattr(fs->super)) {
2695 if (!fs_can_allocate(ff, 1)) {
2700 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2701 if (err || ino == 0) {
2702 ret = translate_error(fs, 0, err);
2705 dbg_printf("%s: ino=%d\n", __func__, ino);
2707 ret = check_inum_access(fs, ino, W_OK);
2711 err = ext2fs_xattrs_open(fs, ino, &h);
2713 ret = translate_error(fs, ino, err);
2717 err = ext2fs_xattrs_read(h);
2719 ret = translate_error(fs, ino, err);
2723 err = ext2fs_xattr_remove(h, key);
2725 ret = translate_error(fs, ino, err);
2729 ret = update_ctime(fs, ino, NULL);
2731 err = ext2fs_xattrs_close(&h);
2733 ret = translate_error(fs, ino, err);
2735 pthread_mutex_unlock(&ff->bfl);
2740 struct readdir_iter {
2742 fuse_fill_dir_t func;
2745 static int op_readdir_iter(ext2_ino_t dir EXT2FS_ATTR((unused)),
2746 int entry EXT2FS_ATTR((unused)),
2747 struct ext2_dir_entry *dirent,
2748 int offset EXT2FS_ATTR((unused)),
2749 int blocksize EXT2FS_ATTR((unused)),
2750 char *buf EXT2FS_ATTR((unused)), void *data)
2752 struct readdir_iter *i = data;
2753 char namebuf[EXT2_NAME_LEN + 1];
2756 memcpy(namebuf, dirent->name, dirent->name_len & 0xFF);
2757 namebuf[dirent->name_len & 0xFF] = 0;
2758 ret = i->func(i->buf, namebuf, NULL, 0);
2760 return DIRENT_ABORT;
2765 static int op_readdir(const char *path EXT2FS_ATTR((unused)),
2766 void *buf, fuse_fill_dir_t fill_func,
2767 off_t offset EXT2FS_ATTR((unused)),
2768 struct fuse_file_info *fp)
2770 struct fuse_context *ctxt = fuse_get_context();
2771 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2772 struct fuse2fs_file_handle *fh =
2773 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2776 struct readdir_iter i;
2779 FUSE2FS_CHECK_CONTEXT(ff);
2781 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2782 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2783 pthread_mutex_lock(&ff->bfl);
2786 err = ext2fs_dir_iterate2(fs, fh->ino, 0, NULL, op_readdir_iter, &i);
2788 ret = translate_error(fs, fh->ino, err);
2792 if (fs_writeable(fs)) {
2793 ret = update_atime(fs, fh->ino);
2798 pthread_mutex_unlock(&ff->bfl);
2802 static int op_access(const char *path, int mask)
2804 struct fuse_context *ctxt = fuse_get_context();
2805 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2811 FUSE2FS_CHECK_CONTEXT(ff);
2813 dbg_printf("%s: path=%s mask=0x%x\n", __func__, path, mask);
2814 pthread_mutex_lock(&ff->bfl);
2815 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2816 if (err || ino == 0) {
2817 ret = translate_error(fs, 0, err);
2821 ret = check_inum_access(fs, ino, mask);
2826 pthread_mutex_unlock(&ff->bfl);
2830 static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
2832 struct fuse_context *ctxt = fuse_get_context();
2833 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2835 ext2_ino_t parent, child;
2840 struct ext2_inode_large inode;
2843 FUSE2FS_CHECK_CONTEXT(ff);
2845 dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode);
2846 temp_path = strdup(path);
2851 node_name = strrchr(temp_path, '/');
2860 pthread_mutex_lock(&ff->bfl);
2861 if (!fs_can_allocate(ff, 1)) {
2866 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
2869 ret = translate_error(fs, 0, err);
2873 ret = check_inum_access(fs, parent, W_OK);
2879 filetype = ext2_file_type(mode);
2881 err = ext2fs_new_inode(fs, parent, mode, 0, &child);
2883 ret = translate_error(fs, parent, err);
2887 dbg_printf("%s: creating ino=%d/name=%s in dir=%d\n", __func__, child,
2889 err = ext2fs_link(fs, parent, node_name, child, filetype);
2890 if (err == EXT2_ET_DIR_NO_SPACE) {
2891 err = ext2fs_expand_dir(fs, parent);
2893 ret = translate_error(fs, parent, err);
2897 err = ext2fs_link(fs, parent, node_name, child,
2901 ret = translate_error(fs, parent, err);
2905 ret = update_mtime(fs, parent, NULL);
2909 memset(&inode, 0, sizeof(inode));
2910 inode.i_mode = mode;
2911 inode.i_links_count = 1;
2912 inode.i_extra_isize = sizeof(struct ext2_inode_large) -
2913 EXT2_GOOD_OLD_INODE_SIZE;
2914 inode.i_uid = ctxt->uid;
2915 inode.i_gid = ctxt->gid;
2916 if (ext2fs_has_feature_extents(fs->super)) {
2917 ext2_extent_handle_t handle;
2919 inode.i_flags &= ~EXT4_EXTENTS_FL;
2920 ret = ext2fs_extent_open2(fs, child,
2921 (struct ext2_inode *)&inode, &handle);
2924 ext2fs_extent_free(handle);
2927 err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode);
2929 ret = translate_error(fs, child, err);
2933 inode.i_generation = ff->next_generation++;
2935 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
2938 ret = translate_error(fs, child, err);
2942 ext2fs_inode_alloc_stats2(fs, child, 1, 0);
2944 ret = __op_open(ff, path, fp);
2948 pthread_mutex_unlock(&ff->bfl);
2954 static int op_ftruncate(const char *path EXT2FS_ATTR((unused)),
2955 off_t len, struct fuse_file_info *fp)
2957 struct fuse_context *ctxt = fuse_get_context();
2958 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2959 struct fuse2fs_file_handle *fh =
2960 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2966 FUSE2FS_CHECK_CONTEXT(ff);
2968 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2969 dbg_printf("%s: ino=%d len=%jd\n", __func__, fh->ino, len);
2970 pthread_mutex_lock(&ff->bfl);
2971 if (!fs_writeable(fs)) {
2976 err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
2978 ret = translate_error(fs, fh->ino, err);
2982 err = ext2fs_file_set_size2(efp, len);
2984 ret = translate_error(fs, fh->ino, err);
2989 err = ext2fs_file_close(efp);
2993 ret = translate_error(fs, fh->ino, err);
2997 ret = update_mtime(fs, fh->ino, NULL);
3002 pthread_mutex_unlock(&ff->bfl);
3006 static int op_fgetattr(const char *path EXT2FS_ATTR((unused)),
3007 struct stat *statbuf,
3008 struct fuse_file_info *fp)
3010 struct fuse_context *ctxt = fuse_get_context();
3011 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3013 struct fuse2fs_file_handle *fh =
3014 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3017 FUSE2FS_CHECK_CONTEXT(ff);
3019 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3020 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3021 pthread_mutex_lock(&ff->bfl);
3022 ret = stat_inode(fs, fh->ino, statbuf);
3023 pthread_mutex_unlock(&ff->bfl);
3028 static int op_utimens(const char *path, const struct timespec ctv[2])
3030 struct fuse_context *ctxt = fuse_get_context();
3031 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3032 struct timespec tv[2];
3036 struct ext2_inode_large inode;
3039 FUSE2FS_CHECK_CONTEXT(ff);
3041 pthread_mutex_lock(&ff->bfl);
3042 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
3044 ret = translate_error(fs, 0, err);
3047 dbg_printf("%s: ino=%d\n", __func__, ino);
3049 ret = check_inum_access(fs, ino, W_OK);
3053 memset(&inode, 0, sizeof(inode));
3054 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
3057 ret = translate_error(fs, ino, err);
3064 if (tv[0].tv_nsec == UTIME_NOW)
3066 if (tv[1].tv_nsec == UTIME_NOW)
3068 #endif /* UTIME_NOW */
3070 if (tv[0].tv_nsec != UTIME_OMIT)
3071 EXT4_INODE_SET_XTIME(i_atime, tv, &inode);
3072 if (tv[1].tv_nsec != UTIME_OMIT)
3073 EXT4_INODE_SET_XTIME(i_mtime, tv + 1, &inode);
3074 #endif /* UTIME_OMIT */
3075 ret = update_ctime(fs, ino, &inode);
3079 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
3082 ret = translate_error(fs, ino, err);
3087 pthread_mutex_unlock(&ff->bfl);
3091 #ifdef SUPPORT_I_FLAGS
3092 static int ioctl_getflags(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3096 struct ext2_inode_large inode;
3098 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3099 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3100 memset(&inode, 0, sizeof(inode));
3101 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3104 return translate_error(fs, fh->ino, err);
3106 *(__u32 *)data = inode.i_flags & EXT2_FL_USER_VISIBLE;
3110 #define FUSE2FS_MODIFIABLE_IFLAGS \
3111 (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL | EXT2_NODUMP_FL | \
3112 EXT2_NOATIME_FL | EXT3_JOURNAL_DATA_FL | EXT2_DIRSYNC_FL | \
3115 static int ioctl_setflags(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3119 struct ext2_inode_large inode;
3121 __u32 flags = *(__u32 *)data;
3122 struct fuse_context *ctxt = fuse_get_context();
3124 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3125 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3126 memset(&inode, 0, sizeof(inode));
3127 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3130 return translate_error(fs, fh->ino, err);
3132 if (ctxt->uid != 0 && inode.i_uid != ctxt->uid)
3135 if ((inode.i_flags ^ flags) & ~FUSE2FS_MODIFIABLE_IFLAGS)
3138 inode.i_flags = (inode.i_flags & ~FUSE2FS_MODIFIABLE_IFLAGS) |
3139 (flags & FUSE2FS_MODIFIABLE_IFLAGS);
3141 ret = update_ctime(fs, fh->ino, &inode);
3145 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3148 return translate_error(fs, fh->ino, err);
3153 static int ioctl_getversion(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3157 struct ext2_inode_large inode;
3159 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3160 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3161 memset(&inode, 0, sizeof(inode));
3162 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3165 return translate_error(fs, fh->ino, err);
3167 *(__u32 *)data = inode.i_generation;
3171 static int ioctl_setversion(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3175 struct ext2_inode_large inode;
3177 __u32 generation = *(__u32 *)data;
3178 struct fuse_context *ctxt = fuse_get_context();
3180 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3181 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3182 memset(&inode, 0, sizeof(inode));
3183 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3186 return translate_error(fs, fh->ino, err);
3188 if (ctxt->uid != 0 && inode.i_uid != ctxt->uid)
3191 inode.i_generation = generation;
3193 ret = update_ctime(fs, fh->ino, &inode);
3197 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3200 return translate_error(fs, fh->ino, err);
3204 #endif /* SUPPORT_I_FLAGS */
3207 static int ioctl_fitrim(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3210 struct fstrim_range *fr = data;
3211 blk64_t start, end, max_blocks, b, cleared;
3214 start = fr->start / fs->blocksize;
3215 end = (fr->start + fr->len - 1) / fs->blocksize;
3216 dbg_printf("%s: start=%llu end=%llu\n", __func__, start, end);
3218 if (start < fs->super->s_first_data_block)
3219 start = fs->super->s_first_data_block;
3220 if (start >= ext2fs_blocks_count(fs->super))
3221 start = ext2fs_blocks_count(fs->super) - 1;
3223 if (end < fs->super->s_first_data_block)
3224 end = fs->super->s_first_data_block;
3225 if (end >= ext2fs_blocks_count(fs->super))
3226 end = ext2fs_blocks_count(fs->super) - 1;
3229 max_blocks = 2048ULL * 1024 * 1024 / fs->blocksize;
3232 while (start <= end) {
3233 err = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
3234 start, end, &start);
3238 return translate_error(fs, fh->ino, err);
3240 b = start + max_blocks < end ? start + max_blocks : end;
3241 err = ext2fs_find_first_set_block_bitmap2(fs->block_map,
3243 if (err && err != ENOENT)
3244 return translate_error(fs, fh->ino, err);
3245 if (b - start >= fr->minlen) {
3246 err = io_channel_discard(fs->io, start, b - start);
3248 return translate_error(fs, fh->ino, err);
3249 cleared += b - start;
3250 fr->len = cleared * fs->blocksize;
3259 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
3260 static int op_ioctl(const char *path EXT2FS_ATTR((unused)), int cmd,
3261 void *arg EXT2FS_ATTR((unused)),
3262 struct fuse_file_info *fp,
3263 unsigned int flags EXT2FS_ATTR((unused)), void *data)
3265 struct fuse_context *ctxt = fuse_get_context();
3266 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3267 struct fuse2fs_file_handle *fh =
3268 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3272 FUSE2FS_CHECK_CONTEXT(ff);
3274 pthread_mutex_lock(&ff->bfl);
3275 switch ((unsigned long) cmd) {
3276 #ifdef SUPPORT_I_FLAGS
3277 case EXT2_IOC_GETFLAGS:
3278 ret = ioctl_getflags(fs, fh, data);
3280 case EXT2_IOC_SETFLAGS:
3281 ret = ioctl_setflags(fs, fh, data);
3283 case EXT2_IOC_GETVERSION:
3284 ret = ioctl_getversion(fs, fh, data);
3286 case EXT2_IOC_SETVERSION:
3287 ret = ioctl_setversion(fs, fh, data);
3292 ret = ioctl_fitrim(fs, fh, data);
3296 dbg_printf("%s: Unknown ioctl %d\n", __func__, cmd);
3299 pthread_mutex_unlock(&ff->bfl);
3303 #endif /* FUSE 28 */
3305 static int op_bmap(const char *path, size_t blocksize EXT2FS_ATTR((unused)),
3308 struct fuse_context *ctxt = fuse_get_context();
3309 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3315 FUSE2FS_CHECK_CONTEXT(ff);
3317 pthread_mutex_lock(&ff->bfl);
3318 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
3320 ret = translate_error(fs, 0, err);
3323 dbg_printf("%s: ino=%d blk=%"PRIu64"\n", __func__, ino, *idx);
3325 err = ext2fs_bmap2(fs, ino, NULL, NULL, 0, *idx, 0, (blk64_t *)idx);
3327 ret = translate_error(fs, ino, err);
3332 pthread_mutex_unlock(&ff->bfl);
3336 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3337 # ifdef SUPPORT_FALLOCATE
3338 static int fallocate_helper(struct fuse_file_info *fp, int mode, off_t offset,
3341 struct fuse_context *ctxt = fuse_get_context();
3342 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3343 struct fuse2fs_file_handle *fh =
3344 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3346 struct ext2_inode_large inode;
3352 FUSE2FS_CHECK_CONTEXT(ff);
3354 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3355 start = offset / fs->blocksize;
3356 end = (offset + len - 1) / fs->blocksize;
3357 dbg_printf("%s: ino=%d mode=0x%x start=%jd end=%llu\n", __func__,
3358 fh->ino, mode, offset / fs->blocksize, end);
3359 if (!fs_can_allocate(ff, len / fs->blocksize))
3362 memset(&inode, 0, sizeof(inode));
3363 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3367 fsize = EXT2_I_SIZE(&inode);
3369 /* Allocate a bunch of blocks */
3370 flags = (mode & FL_KEEP_SIZE_FLAG ? 0 :
3371 EXT2_FALLOCATE_INIT_BEYOND_EOF);
3372 err = ext2fs_fallocate(fs, flags, fh->ino,
3373 (struct ext2_inode *)&inode,
3374 ~0ULL, start, end - start + 1);
3375 if (err && err != EXT2_ET_BLOCK_ALLOC_FAIL)
3376 return translate_error(fs, fh->ino, err);
3379 if (!(mode & FL_KEEP_SIZE_FLAG)) {
3380 if ((__u64) offset + len > fsize) {
3381 err = ext2fs_inode_size_set(fs,
3382 (struct ext2_inode *)&inode,
3385 return translate_error(fs, fh->ino, err);
3389 err = update_mtime(fs, fh->ino, &inode);
3393 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3396 return translate_error(fs, fh->ino, err);
3401 static errcode_t clean_block_middle(ext2_filsys fs, ext2_ino_t ino,
3402 struct ext2_inode_large *inode, off_t offset,
3403 off_t len, char **buf)
3410 residue = offset % fs->blocksize;
3415 err = ext2fs_get_mem(fs->blocksize, buf);
3420 err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0,
3421 offset / fs->blocksize, &retflags, &blk);
3424 if (!blk || (retflags & BMAP_RET_UNINIT))
3427 err = io_channel_read_blk(fs->io, blk, 1, *buf);
3431 memset(*buf + residue, 0, len);
3433 return io_channel_write_blk(fs->io, blk, 1, *buf);
3436 static errcode_t clean_block_edge(ext2_filsys fs, ext2_ino_t ino,
3437 struct ext2_inode_large *inode, off_t offset,
3438 int clean_before, char **buf)
3445 residue = offset % fs->blocksize;
3450 err = ext2fs_get_mem(fs->blocksize, buf);
3455 err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0,
3456 offset / fs->blocksize, &retflags, &blk);
3460 err = io_channel_read_blk(fs->io, blk, 1, *buf);
3463 if (!blk || (retflags & BMAP_RET_UNINIT))
3467 memset(*buf, 0, residue);
3469 memset(*buf + residue, 0, fs->blocksize - residue);
3471 return io_channel_write_blk(fs->io, blk, 1, *buf);
3474 static int punch_helper(struct fuse_file_info *fp, int mode, off_t offset,
3477 struct fuse_context *ctxt = fuse_get_context();
3478 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3479 struct fuse2fs_file_handle *fh =
3480 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3482 struct ext2_inode_large inode;
3487 FUSE2FS_CHECK_CONTEXT(ff);
3489 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3490 dbg_printf("%s: offset=%jd len=%jd\n", __func__, offset, len);
3492 /* kernel ext4 punch requires this flag to be set */
3493 if (!(mode & FL_KEEP_SIZE_FLAG))
3496 /* Punch out a bunch of blocks */
3497 start = (offset + fs->blocksize - 1) / fs->blocksize;
3498 end = (offset + len - fs->blocksize) / fs->blocksize;
3499 dbg_printf("%s: ino=%d mode=0x%x start=%llu end=%llu\n", __func__,
3500 fh->ino, mode, start, end);
3502 memset(&inode, 0, sizeof(inode));
3503 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3506 return translate_error(fs, fh->ino, err);
3508 /* Zero everything before the first block and after the last block */
3509 if ((offset / fs->blocksize) == ((offset + len) / fs->blocksize))
3510 err = clean_block_middle(fs, fh->ino, &inode, offset,
3513 err = clean_block_edge(fs, fh->ino, &inode, offset, 0, &buf);
3515 err = clean_block_edge(fs, fh->ino, &inode,
3516 offset + len, 1, &buf);
3519 ext2fs_free_mem(&buf);
3521 return translate_error(fs, fh->ino, err);
3523 /* Unmap full blocks in the middle */
3525 err = ext2fs_punch(fs, fh->ino, (struct ext2_inode *)&inode,
3528 return translate_error(fs, fh->ino, err);
3531 err = update_mtime(fs, fh->ino, &inode);
3535 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3538 return translate_error(fs, fh->ino, err);
3543 static int op_fallocate(const char *path EXT2FS_ATTR((unused)), int mode,
3544 off_t offset, off_t len,
3545 struct fuse_file_info *fp)
3547 struct fuse_context *ctxt = fuse_get_context();
3548 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3549 ext2_filsys fs = ff->fs;
3552 /* Catch unknown flags */
3553 if (mode & ~(FL_PUNCH_HOLE_FLAG | FL_KEEP_SIZE_FLAG))
3556 pthread_mutex_lock(&ff->bfl);
3557 if (!fs_writeable(fs)) {
3561 if (mode & FL_PUNCH_HOLE_FLAG)
3562 ret = punch_helper(fp, mode, offset, len);
3564 ret = fallocate_helper(fp, mode, offset, len);
3566 pthread_mutex_unlock(&ff->bfl);
3570 # endif /* SUPPORT_FALLOCATE */
3571 #endif /* FUSE 29 */
3573 static struct fuse_operations fs_ops = {
3575 .destroy = op_destroy,
3576 .getattr = op_getattr,
3577 .readlink = op_readlink,
3580 .unlink = op_unlink,
3582 .symlink = op_symlink,
3583 .rename = op_rename,
3587 .truncate = op_truncate,
3591 .statfs = op_statfs,
3592 .release = op_release,
3594 .setxattr = op_setxattr,
3595 .getxattr = op_getxattr,
3596 .listxattr = op_listxattr,
3597 .removexattr = op_removexattr,
3599 .readdir = op_readdir,
3600 .releasedir = op_release,
3601 .fsyncdir = op_fsync,
3602 .access = op_access,
3603 .create = op_create,
3604 .ftruncate = op_ftruncate,
3605 .fgetattr = op_fgetattr,
3606 .utimens = op_utimens,
3607 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3608 # if defined(UTIME_NOW) || defined(UTIME_OMIT)
3609 .flag_utime_omit_ok = 1,
3617 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
3619 .flag_nullpath_ok = 1,
3621 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3623 # ifdef SUPPORT_FALLOCATE
3624 .fallocate = op_fallocate,
3629 static int get_random_bytes(void *p, size_t sz)
3634 fd = open("/dev/urandom", O_RDONLY);
3636 perror("/dev/urandom");
3640 r = read(fd, p, sz);
3643 return (size_t) r == sz;
3652 #define FUSE2FS_OPT(t, p, v) { t, offsetof(struct fuse2fs, p), v }
3654 static struct fuse_opt fuse2fs_opts[] = {
3655 FUSE2FS_OPT("ro", ro, 1),
3656 FUSE2FS_OPT("errors=panic", panic_on_error, 1),
3657 FUSE2FS_OPT("minixdf", minixdf, 1),
3658 FUSE2FS_OPT("fuse2fs_debug", debug, 1),
3659 FUSE2FS_OPT("no_default_opts", no_default_opts, 1),
3661 FUSE_OPT_KEY("-V", FUSE2FS_VERSION),
3662 FUSE_OPT_KEY("--version", FUSE2FS_VERSION),
3663 FUSE_OPT_KEY("-h", FUSE2FS_HELP),
3664 FUSE_OPT_KEY("--help", FUSE2FS_HELP),
3665 FUSE_OPT_KEY("--helpfull", FUSE2FS_HELPFULL),
3670 static int fuse2fs_opt_proc(void *data, const char *arg,
3671 int key, struct fuse_args *outargs)
3673 struct fuse2fs *ff = data;
3676 case FUSE_OPT_KEY_NONOPT:
3678 ff->device = strdup(arg);
3683 case FUSE2FS_HELPFULL:
3685 "usage: %s device/image mountpoint [options]\n"
3687 "general options:\n"
3688 " -o opt,[opt...] mount options\n"
3689 " -h --help print help\n"
3690 " -V --version print version\n"
3692 "fuse2fs options:\n"
3693 " -o ro read-only mount\n"
3694 " -o errors=panic dump core on error\n"
3695 " -o minixdf minix-style df\n"
3696 " -o no_default_opts do not include default fuse options\n"
3697 " -o fuse2fs_debug enable fuse2fs debugging\n"
3700 if (key == FUSE2FS_HELPFULL) {
3701 fuse_opt_add_arg(outargs, "-ho");
3702 fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL);
3704 fprintf(stderr, "Try --helpfull to get a list of "
3705 "all flags, including the FUSE options.\n");
3709 case FUSE2FS_VERSION:
3710 fprintf(stderr, "fuse2fs %s (%s)\n", E2FSPROGS_VERSION,
3712 fuse_opt_add_arg(outargs, "--version");
3713 fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL);
3719 int main(int argc, char *argv[])
3721 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
3722 struct fuse2fs fctx;
3725 char extra_args[BUFSIZ];
3726 int ret = 0, flags = EXT2_FLAG_64BITS | EXT2_FLAG_EXCLUSIVE;
3728 memset(&fctx, 0, sizeof(fctx));
3729 fctx.magic = FUSE2FS_MAGIC;
3731 fuse_opt_parse(&args, &fctx, fuse2fs_opts, fuse2fs_opt_proc);
3732 if (fctx.device == NULL) {
3733 fprintf(stderr, "Missing ext4 device/image\n");
3734 fprintf(stderr, "See '%s -h' for usage\n", argv[0]);
3739 printf("%s", _("Mounting read-only.\n"));
3742 setlocale(LC_MESSAGES, "");
3743 setlocale(LC_CTYPE, "");
3744 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
3745 textdomain(NLS_CAT_NAME);
3746 set_com_err_gettext(gettext);
3748 add_error_table(&et_ext2_error_table);
3750 /* Set up error logging */
3751 logfile = getenv("FUSE2FS_LOGFILE");
3753 fctx.err_fp = fopen(logfile, "a");
3759 fctx.err_fp = stderr;
3761 /* Will we allow users to allocate every last block? */
3762 if (getenv("FUSE2FS_ALLOC_ALL_BLOCKS")) {
3763 printf(_("%s: Allowing users to allocate all blocks. "
3764 "This is dangerous!\n"), fctx.device);
3765 fctx.alloc_all_blocks = 1;
3768 /* Start up the fs (while we still can use stdout) */
3771 flags |= EXT2_FLAG_RW;
3772 err = ext2fs_open2(fctx.device, NULL, flags, 0, 0, unix_io_manager,
3775 printf(_("%s: %s.\n"), fctx.device, error_message(err));
3776 printf(_("Please run e2fsck -fy %s.\n"), fctx.device);
3779 fctx.fs = global_fs;
3780 global_fs->priv_data = &fctx;
3784 if (ext2fs_has_feature_journal_needs_recovery(global_fs->super)) {
3786 printf(_("%s: recovering journal\n"), fctx.device);
3787 err = ext2fs_run_ext3_journal(&global_fs);
3789 printf(_("%s: %s.\n"), fctx.device,
3790 error_message(err));
3791 printf(_("Please run e2fsck -fy %s.\n"),
3795 ext2fs_clear_feature_journal_needs_recovery(global_fs->super);
3796 ext2fs_mark_super_dirty(global_fs);
3798 printf("%s", _("Journal needs recovery; running "
3799 "`e2fsck -E journal_only' is required.\n"));
3805 if (ext2fs_has_feature_journal(global_fs->super))
3806 printf(_("%s: Writing to the journal is not supported.\n"),
3808 err = ext2fs_read_inode_bitmap(global_fs);
3810 translate_error(global_fs, 0, err);
3813 err = ext2fs_read_block_bitmap(global_fs);
3815 translate_error(global_fs, 0, err);
3820 if (!(global_fs->super->s_state & EXT2_VALID_FS))
3821 printf("%s", _("Warning: Mounting unchecked fs, running e2fsck "
3822 "is recommended.\n"));
3823 if (global_fs->super->s_max_mnt_count > 0 &&
3824 global_fs->super->s_mnt_count >= global_fs->super->s_max_mnt_count)
3825 printf("%s", _("Warning: Maximal mount count reached, running "
3826 "e2fsck is recommended.\n"));
3827 if (global_fs->super->s_checkinterval > 0 &&
3828 (time_t) (global_fs->super->s_lastcheck +
3829 global_fs->super->s_checkinterval) <= time(0))
3830 printf("%s", _("Warning: Check time reached; running e2fsck "
3831 "is recommended.\n"));
3832 if (global_fs->super->s_last_orphan)
3834 _("Orphans detected; running e2fsck is recommended.\n"));
3836 if (global_fs->super->s_state & EXT2_ERROR_FS) {
3838 _("Errors detected; running e2fsck is required.\n"));
3842 /* Initialize generation counter */
3843 get_random_bytes(&fctx.next_generation, sizeof(unsigned int));
3845 /* Set up default fuse parameters */
3846 snprintf(extra_args, BUFSIZ, "-okernel_cache,subtype=ext4,use_ino,"
3847 "fsname=%s,attr_timeout=0" FUSE_PLATFORM_OPTS,
3849 if (fctx.no_default_opts == 0)
3850 fuse_opt_add_arg(&args, extra_args);
3855 printf("fuse arguments:");
3856 for (i = 0; i < args.argc; i++)
3857 printf(" '%s'", args.argv[i]);
3861 pthread_mutex_init(&fctx.bfl, NULL);
3862 fuse_main(args.argc, args.argv, &fs_ops, &fctx);
3863 pthread_mutex_destroy(&fctx.bfl);
3868 err = ext2fs_close(global_fs);
3870 com_err(argv[0], err, "while closing fs");
3876 static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
3877 const char *file, int line)
3879 struct timespec now;
3881 struct fuse2fs *ff = fs->priv_data;
3884 /* Translate ext2 error to unix error code */
3885 if (err < EXT2_ET_BASE)
3886 goto no_translation;
3888 case EXT2_ET_NO_MEMORY:
3889 case EXT2_ET_TDB_ERR_OOM:
3892 case EXT2_ET_INVALID_ARGUMENT:
3893 case EXT2_ET_LLSEEK_FAILED:
3896 case EXT2_ET_NO_DIRECTORY:
3899 case EXT2_ET_FILE_NOT_FOUND:
3902 case EXT2_ET_DIR_NO_SPACE:
3905 case EXT2_ET_TOOSMALL:
3906 case EXT2_ET_BLOCK_ALLOC_FAIL:
3907 case EXT2_ET_INODE_ALLOC_FAIL:
3908 case EXT2_ET_EA_NO_SPACE:
3911 case EXT2_ET_SYMLINK_LOOP:
3914 case EXT2_ET_FILE_TOO_BIG:
3917 case EXT2_ET_TDB_ERR_EXISTS:
3918 case EXT2_ET_FILE_EXISTS:
3921 case EXT2_ET_MMP_FAILED:
3922 case EXT2_ET_MMP_FSCK_ON:
3925 case EXT2_ET_EA_KEY_NOT_FOUND:
3932 /* Sometimes fuse returns a garbage file handle pointer to us... */
3933 case EXT2_ET_MAGIC_EXT2_FILE:
3936 case EXT2_ET_UNIMPLEMENTED:
3950 fprintf(ff->err_fp, "FUSE2FS (%s): %s (inode #%d) at %s:%d.\n",
3951 fs->device_name ? fs->device_name : "???",
3952 error_message(err), ino, file, line);
3954 fprintf(ff->err_fp, "FUSE2FS (%s): %s at %s:%d.\n",
3955 fs->device_name ? fs->device_name : "???",
3956 error_message(err), file, line);
3959 /* Make a note in the error log */
3961 fs->super->s_last_error_time = now.tv_sec;
3962 fs->super->s_last_error_ino = ino;
3963 fs->super->s_last_error_line = line;
3964 fs->super->s_last_error_block = err; /* Yeah... */
3965 strncpy((char *)fs->super->s_last_error_func, file,
3966 sizeof(fs->super->s_last_error_func));
3967 if (fs->super->s_first_error_time == 0) {
3968 fs->super->s_first_error_time = now.tv_sec;
3969 fs->super->s_first_error_ino = ino;
3970 fs->super->s_first_error_line = line;
3971 fs->super->s_first_error_block = err;
3972 strncpy((char *)fs->super->s_first_error_func, file,
3973 sizeof(fs->super->s_first_error_func));
3976 fs->super->s_error_count++;
3977 ext2fs_mark_super_dirty(fs);
3979 if (ff->panic_on_error)