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 ",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)
326 int alloc_all_blocks;
328 unsigned int next_generation;
331 #define FUSE2FS_CHECK_MAGIC(fs, ptr, num) do {if ((ptr)->magic != (num)) \
332 return translate_error((fs), 0, EXT2_ET_MAGIC_EXT2_FILE); \
335 #define FUSE2FS_CHECK_CONTEXT(ptr) do {if ((ptr)->magic != FUSE2FS_MAGIC) \
336 return translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); \
339 static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
340 const char *file, int line);
341 #define translate_error(fs, ino, err) __translate_error((fs), (err), (ino), \
353 #define EXT4_EPOCH_BITS 2
354 #define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1)
355 #define EXT4_NSEC_MASK (~0UL << EXT4_EPOCH_BITS)
358 * Extended fields will fit into an inode if the filesystem was formatted
359 * with large inodes (-I 256 or larger) and there are not currently any EAs
360 * consuming all of the available space. For new inodes we always reserve
361 * enough space for the kernel's known extended fields, but for inodes
362 * created with an old kernel this might not have been the case. None of
363 * the extended inode fields is critical for correct filesystem operation.
364 * This macro checks if a certain field fits in the inode. Note that
365 * inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize
367 #define EXT4_FITS_IN_INODE(ext4_inode, field) \
368 ((offsetof(typeof(*ext4_inode), field) + \
369 sizeof((ext4_inode)->field)) \
370 <= ((size_t) EXT2_GOOD_OLD_INODE_SIZE + \
371 (ext4_inode)->i_extra_isize)) \
373 static inline __u32 ext4_encode_extra_time(const struct timespec *time)
375 __u32 extra = sizeof(time->tv_sec) > 4 ?
376 ((time->tv_sec - (__s32)time->tv_sec) >> 32) &
378 return extra | (time->tv_nsec << EXT4_EPOCH_BITS);
381 static inline void ext4_decode_extra_time(struct timespec *time, __u32 extra)
383 if (sizeof(time->tv_sec) > 4 && (extra & EXT4_EPOCH_MASK)) {
384 __u64 extra_bits = extra & EXT4_EPOCH_MASK;
386 * Prior to kernel 3.14?, we had a broken decode function,
387 * wherein we effectively did this:
388 * if (extra_bits == 3)
391 time->tv_sec += extra_bits << 32;
393 time->tv_nsec = ((extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
396 #define EXT4_INODE_SET_XTIME(xtime, timespec, raw_inode) \
398 (raw_inode)->xtime = (timespec)->tv_sec; \
399 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
400 (raw_inode)->xtime ## _extra = \
401 ext4_encode_extra_time(timespec); \
404 #define EXT4_EINODE_SET_XTIME(xtime, timespec, raw_inode) \
406 if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
407 (raw_inode)->xtime = (timespec)->tv_sec; \
408 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
409 (raw_inode)->xtime ## _extra = \
410 ext4_encode_extra_time(timespec); \
413 #define EXT4_INODE_GET_XTIME(xtime, timespec, raw_inode) \
415 (timespec)->tv_sec = (signed)((raw_inode)->xtime); \
416 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
417 ext4_decode_extra_time((timespec), \
418 (raw_inode)->xtime ## _extra); \
420 (timespec)->tv_nsec = 0; \
423 #define EXT4_EINODE_GET_XTIME(xtime, timespec, raw_inode) \
425 if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
426 (timespec)->tv_sec = \
427 (signed)((raw_inode)->xtime); \
428 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
429 ext4_decode_extra_time((timespec), \
430 raw_inode->xtime ## _extra); \
432 (timespec)->tv_nsec = 0; \
435 static void get_now(struct timespec *now)
437 #ifdef CLOCK_REALTIME
438 if (!clock_gettime(CLOCK_REALTIME, now))
442 now->tv_sec = time(NULL);
446 static void increment_version(struct ext2_inode_large *inode)
450 ver = inode->osd1.linux1.l_i_version;
451 if (EXT4_FITS_IN_INODE(inode, i_version_hi))
452 ver |= (__u64)inode->i_version_hi << 32;
454 inode->osd1.linux1.l_i_version = ver;
455 if (EXT4_FITS_IN_INODE(inode, i_version_hi))
456 inode->i_version_hi = ver >> 32;
459 static void init_times(struct ext2_inode_large *inode)
464 EXT4_INODE_SET_XTIME(i_atime, &now, inode);
465 EXT4_INODE_SET_XTIME(i_ctime, &now, inode);
466 EXT4_INODE_SET_XTIME(i_mtime, &now, inode);
467 EXT4_EINODE_SET_XTIME(i_crtime, &now, inode);
468 increment_version(inode);
471 static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
472 struct ext2_inode_large *pinode)
476 struct ext2_inode_large inode;
480 /* If user already has a inode buffer, just update that */
482 increment_version(pinode);
483 EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
487 /* Otherwise we have to read-modify-write the inode */
488 memset(&inode, 0, sizeof(inode));
489 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
492 return translate_error(fs, ino, err);
494 increment_version(&inode);
495 EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
497 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
500 return translate_error(fs, ino, err);
505 static int update_atime(ext2_filsys fs, ext2_ino_t ino)
508 struct ext2_inode_large inode, *pinode;
509 struct timespec atime, mtime, now;
511 if (!(fs->flags & EXT2_FLAG_RW))
513 memset(&inode, 0, sizeof(inode));
514 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
517 return translate_error(fs, ino, err);
520 EXT4_INODE_GET_XTIME(i_atime, &atime, pinode);
521 EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode);
524 * If atime is newer than mtime and atime hasn't been updated in thirty
525 * seconds, skip the atime update. Same idea as Linux "relatime".
527 if (atime.tv_sec >= mtime.tv_sec && atime.tv_sec >= now.tv_sec - 30)
529 EXT4_INODE_SET_XTIME(i_atime, &now, &inode);
531 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
534 return translate_error(fs, ino, err);
539 static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
540 struct ext2_inode_large *pinode)
543 struct ext2_inode_large inode;
548 EXT4_INODE_SET_XTIME(i_mtime, &now, pinode);
549 EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
550 increment_version(pinode);
554 memset(&inode, 0, sizeof(inode));
555 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
558 return translate_error(fs, ino, err);
561 EXT4_INODE_SET_XTIME(i_mtime, &now, &inode);
562 EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
563 increment_version(&inode);
565 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
568 return translate_error(fs, ino, err);
573 static int ext2_file_type(unsigned int mode)
575 if (LINUX_S_ISREG(mode))
576 return EXT2_FT_REG_FILE;
578 if (LINUX_S_ISDIR(mode))
581 if (LINUX_S_ISCHR(mode))
582 return EXT2_FT_CHRDEV;
584 if (LINUX_S_ISBLK(mode))
585 return EXT2_FT_BLKDEV;
587 if (LINUX_S_ISLNK(mode))
588 return EXT2_FT_SYMLINK;
590 if (LINUX_S_ISFIFO(mode))
593 if (LINUX_S_ISSOCK(mode))
599 static int fs_can_allocate(struct fuse2fs *ff, blk64_t num)
601 ext2_filsys fs = ff->fs;
604 dbg_printf("%s: Asking for %llu; alloc_all=%d total=%llu free=%llu "
605 "rsvd=%llu\n", __func__, num, ff->alloc_all_blocks,
606 ext2fs_blocks_count(fs->super),
607 ext2fs_free_blocks_count(fs->super),
608 ext2fs_r_blocks_count(fs->super));
609 if (num > ext2fs_blocks_count(fs->super))
612 if (ff->alloc_all_blocks)
616 * Different meaning for r_blocks -- libext2fs has bugs where the FS
617 * can get corrupted if it totally runs out of blocks. Avoid this
618 * by refusing to allocate any of the reserve blocks to anybody.
620 reserved = ext2fs_r_blocks_count(fs->super);
622 reserved = ext2fs_blocks_count(fs->super) / 10;
623 return ext2fs_free_blocks_count(fs->super) > reserved + num;
626 static int fs_writeable(ext2_filsys fs)
628 return (fs->flags & EXT2_FLAG_RW) && (fs->super->s_error_count == 0);
631 static int check_inum_access(ext2_filsys fs, ext2_ino_t ino, mode_t mask)
633 struct fuse_context *ctxt = fuse_get_context();
634 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
635 struct ext2_inode inode;
639 /* no writing to read-only or broken fs */
640 if ((mask & W_OK) && !fs_writeable(fs))
643 err = ext2fs_read_inode(fs, ino, &inode);
645 return translate_error(fs, ino, err);
646 perms = inode.i_mode & 0777;
648 dbg_printf("access ino=%d mask=e%s%s%s perms=0%o fuid=%d fgid=%d "
649 "uid=%d gid=%d\n", ino,
650 (mask & R_OK ? "r" : ""), (mask & W_OK ? "w" : ""),
651 (mask & X_OK ? "x" : ""), perms, inode_uid(inode),
652 inode_gid(inode), ctxt->uid, ctxt->gid);
654 /* existence check */
660 (inode.i_flags & EXT2_IMMUTABLE_FL))
663 /* Figure out what root's allowed to do */
664 if (ff->fakeroot || ctxt->uid == 0) {
665 /* Non-file access always ok */
666 if (!LINUX_S_ISREG(inode.i_mode))
669 /* R/W access to a file always ok */
673 /* X access to a file ok if a user/group/other can X */
677 /* Trying to execute a file that's not executable. BZZT! */
681 /* allow owner, if perms match */
682 if (inode_uid(inode) == ctxt->uid) {
683 if ((mask & (perms >> 6)) == mask)
688 /* allow group, if perms match */
689 if (inode_gid(inode) == ctxt->gid) {
690 if ((mask & (perms >> 3)) == mask)
695 /* otherwise check other */
696 if ((mask & perms) == mask)
701 static void op_destroy(void *p EXT2FS_ATTR((unused)))
703 struct fuse_context *ctxt = fuse_get_context();
704 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
708 if (ff->magic != FUSE2FS_MAGIC) {
709 translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC);
713 dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
714 if (fs->flags & EXT2_FLAG_RW) {
715 fs->super->s_state |= EXT2_VALID_FS;
716 if (fs->super->s_error_count)
717 fs->super->s_state |= EXT2_ERROR_FS;
718 ext2fs_mark_super_dirty(fs);
719 err = ext2fs_set_gdt_csum(fs);
721 translate_error(fs, 0, err);
723 err = ext2fs_flush2(fs, 0);
725 translate_error(fs, 0, err);
729 static void *op_init(struct fuse_conn_info *conn)
731 struct fuse_context *ctxt = fuse_get_context();
732 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
736 if (ff->magic != FUSE2FS_MAGIC) {
737 translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC);
741 dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
742 #ifdef FUSE_CAP_IOCTL_DIR
743 conn->want |= FUSE_CAP_IOCTL_DIR;
745 if (fs->flags & EXT2_FLAG_RW) {
746 fs->super->s_mnt_count++;
747 fs->super->s_mtime = time(NULL);
748 fs->super->s_state &= ~EXT2_VALID_FS;
749 ext2fs_mark_super_dirty(fs);
750 err = ext2fs_flush2(fs, 0);
752 translate_error(fs, 0, err);
757 static int stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *statbuf)
759 struct ext2_inode_large inode;
765 memset(&inode, 0, sizeof(inode));
766 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
769 return translate_error(fs, ino, err);
771 memcpy(&fakedev, fs->super->s_uuid, sizeof(fakedev));
772 statbuf->st_dev = fakedev;
773 statbuf->st_ino = ino;
774 statbuf->st_mode = inode.i_mode;
775 statbuf->st_nlink = inode.i_links_count;
776 statbuf->st_uid = inode_uid(inode);
777 statbuf->st_gid = inode_gid(inode);
778 statbuf->st_size = EXT2_I_SIZE(&inode);
779 statbuf->st_blksize = fs->blocksize;
780 statbuf->st_blocks = ext2fs_get_stat_i_blocks(fs,
781 (struct ext2_inode *)&inode);
782 EXT4_INODE_GET_XTIME(i_atime, &tv, &inode);
783 statbuf->st_atime = tv.tv_sec;
784 EXT4_INODE_GET_XTIME(i_mtime, &tv, &inode);
785 statbuf->st_mtime = tv.tv_sec;
786 EXT4_INODE_GET_XTIME(i_ctime, &tv, &inode);
787 statbuf->st_ctime = tv.tv_sec;
788 if (LINUX_S_ISCHR(inode.i_mode) ||
789 LINUX_S_ISBLK(inode.i_mode)) {
790 if (inode.i_block[0])
791 statbuf->st_rdev = inode.i_block[0];
793 statbuf->st_rdev = inode.i_block[1];
799 static int op_getattr(const char *path, struct stat *statbuf)
801 struct fuse_context *ctxt = fuse_get_context();
802 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
808 FUSE2FS_CHECK_CONTEXT(ff);
810 dbg_printf("%s: path=%s\n", __func__, path);
811 pthread_mutex_lock(&ff->bfl);
812 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
814 ret = translate_error(fs, 0, err);
817 ret = stat_inode(fs, ino, statbuf);
819 pthread_mutex_unlock(&ff->bfl);
823 static int op_readlink(const char *path, char *buf, size_t len)
825 struct fuse_context *ctxt = fuse_get_context();
826 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
830 struct ext2_inode inode;
835 FUSE2FS_CHECK_CONTEXT(ff);
837 dbg_printf("%s: path=%s\n", __func__, path);
838 pthread_mutex_lock(&ff->bfl);
839 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
840 if (err || ino == 0) {
841 ret = translate_error(fs, 0, err);
845 err = ext2fs_read_inode(fs, ino, &inode);
847 ret = translate_error(fs, ino, err);
851 if (!LINUX_S_ISLNK(inode.i_mode)) {
857 if (inode.i_size < len)
859 if (ext2fs_is_fast_symlink(&inode))
860 memcpy(buf, (char *)inode.i_block, len);
862 /* big/inline symlink */
864 err = ext2fs_file_open(fs, ino, 0, &file);
866 ret = translate_error(fs, ino, err);
870 err = ext2fs_file_read(file, buf, len, &got);
871 if (err || got != len) {
872 ext2fs_file_close(file);
873 ret = translate_error(fs, ino, err);
878 err = ext2fs_file_close(file);
882 ret = translate_error(fs, ino, err);
888 if (fs_writeable(fs)) {
889 ret = update_atime(fs, ino);
895 pthread_mutex_unlock(&ff->bfl);
899 static int op_mknod(const char *path, mode_t mode, dev_t dev)
901 struct fuse_context *ctxt = fuse_get_context();
902 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
904 ext2_ino_t parent, child;
909 struct ext2_inode_large inode;
912 FUSE2FS_CHECK_CONTEXT(ff);
914 dbg_printf("%s: path=%s mode=0%o dev=0x%x\n", __func__, path, mode,
916 temp_path = strdup(path);
921 node_name = strrchr(temp_path, '/');
930 pthread_mutex_lock(&ff->bfl);
931 if (!fs_can_allocate(ff, 2)) {
936 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
939 ret = translate_error(fs, 0, err);
943 ret = check_inum_access(fs, parent, W_OK);
949 if (LINUX_S_ISCHR(mode))
950 filetype = EXT2_FT_CHRDEV;
951 else if (LINUX_S_ISBLK(mode))
952 filetype = EXT2_FT_BLKDEV;
953 else if (LINUX_S_ISFIFO(mode))
954 filetype = EXT2_FT_FIFO;
955 else if (LINUX_S_ISSOCK(mode))
956 filetype = EXT2_FT_SOCK;
962 err = ext2fs_new_inode(fs, parent, mode, 0, &child);
964 ret = translate_error(fs, 0, err);
968 dbg_printf("%s: create ino=%d/name=%s in dir=%d\n", __func__, child,
970 err = ext2fs_link(fs, parent, node_name, child, filetype);
971 if (err == EXT2_ET_DIR_NO_SPACE) {
972 err = ext2fs_expand_dir(fs, parent);
974 ret = translate_error(fs, parent, err);
978 err = ext2fs_link(fs, parent, node_name, child,
982 ret = translate_error(fs, parent, err);
986 ret = update_mtime(fs, parent, NULL);
990 memset(&inode, 0, sizeof(inode));
994 inode.i_block[1] = dev;
996 inode.i_block[0] = dev;
997 inode.i_links_count = 1;
998 inode.i_extra_isize = sizeof(struct ext2_inode_large) -
999 EXT2_GOOD_OLD_INODE_SIZE;
1000 inode.i_uid = ctxt->uid;
1001 ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
1002 inode.i_gid = ctxt->gid;
1003 ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
1005 err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode);
1007 ret = translate_error(fs, child, err);
1011 inode.i_generation = ff->next_generation++;
1013 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1016 ret = translate_error(fs, child, err);
1020 ext2fs_inode_alloc_stats2(fs, child, 1, 0);
1023 pthread_mutex_unlock(&ff->bfl);
1029 static int op_mkdir(const char *path, mode_t mode)
1031 struct fuse_context *ctxt = fuse_get_context();
1032 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1034 ext2_ino_t parent, child;
1038 struct ext2_inode_large inode;
1044 FUSE2FS_CHECK_CONTEXT(ff);
1046 dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode);
1047 temp_path = strdup(path);
1052 node_name = strrchr(temp_path, '/');
1061 pthread_mutex_lock(&ff->bfl);
1062 if (!fs_can_allocate(ff, 1)) {
1067 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1070 ret = translate_error(fs, 0, err);
1074 ret = check_inum_access(fs, parent, W_OK);
1078 /* Is the parent dir sgid? */
1079 err = ext2fs_read_inode_full(fs, parent, (struct ext2_inode *)&inode,
1082 ret = translate_error(fs, parent, err);
1085 parent_sgid = inode.i_mode & S_ISGID;
1089 err = ext2fs_mkdir(fs, parent, 0, node_name);
1090 if (err == EXT2_ET_DIR_NO_SPACE) {
1091 err = ext2fs_expand_dir(fs, parent);
1093 ret = translate_error(fs, parent, err);
1097 err = ext2fs_mkdir(fs, parent, 0, node_name);
1100 ret = translate_error(fs, parent, err);
1104 ret = update_mtime(fs, parent, NULL);
1108 /* Still have to update the uid/gid of the dir */
1109 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1112 ret = translate_error(fs, 0, err);
1115 dbg_printf("%s: created ino=%d/path=%s in dir=%d\n", __func__, child,
1118 memset(&inode, 0, sizeof(inode));
1119 err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode,
1122 ret = translate_error(fs, child, err);
1126 inode.i_uid = ctxt->uid;
1127 ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
1128 inode.i_gid = ctxt->gid;
1129 ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
1130 inode.i_mode = LINUX_S_IFDIR | (mode & ~(S_ISUID | fs->umask)) |
1132 inode.i_generation = ff->next_generation++;
1134 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1137 ret = translate_error(fs, child, err);
1141 /* Rewrite the directory block checksum, having set i_generation */
1142 if ((inode.i_flags & EXT4_INLINE_DATA_FL) ||
1143 !ext2fs_has_feature_metadata_csum(fs->super))
1145 err = ext2fs_new_dir_block(fs, child, parent, &block);
1147 ret = translate_error(fs, child, err);
1150 err = ext2fs_bmap2(fs, child, (struct ext2_inode *)&inode, NULL, 0, 0,
1153 ret = translate_error(fs, child, err);
1156 err = ext2fs_write_dir_block4(fs, blk, block, 0, child);
1158 ret = translate_error(fs, child, err);
1163 ext2fs_free_mem(&block);
1165 pthread_mutex_unlock(&ff->bfl);
1171 static int unlink_file_by_name(ext2_filsys fs, const char *path)
1175 char *filename = strdup(path);
1179 base_name = strrchr(filename, '/');
1181 *base_name++ = '\0';
1182 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, filename,
1186 return translate_error(fs, 0, err);
1189 dir = EXT2_ROOT_INO;
1190 base_name = filename;
1193 ret = check_inum_access(fs, dir, W_OK);
1199 dbg_printf("%s: unlinking name=%s from dir=%d\n", __func__,
1201 err = ext2fs_unlink(fs, dir, base_name, 0, 0);
1204 return translate_error(fs, dir, err);
1206 return update_mtime(fs, dir, NULL);
1209 static int remove_inode(struct fuse2fs *ff, ext2_ino_t ino)
1211 ext2_filsys fs = ff->fs;
1213 struct ext2_inode_large inode;
1216 memset(&inode, 0, sizeof(inode));
1217 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1220 ret = translate_error(fs, ino, err);
1223 dbg_printf("%s: put ino=%d links=%d\n", __func__, ino,
1224 inode.i_links_count);
1226 switch (inode.i_links_count) {
1228 return 0; /* XXX: already done? */
1230 inode.i_links_count--;
1231 inode.i_dtime = fs->now ? fs->now : time(0);
1234 inode.i_links_count--;
1237 ret = update_ctime(fs, ino, &inode);
1241 if (inode.i_links_count)
1244 /* Nobody holds this file; free its blocks! */
1245 err = ext2fs_free_ext_attr(fs, ino, &inode);
1249 if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1250 err = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1253 ret = translate_error(fs, ino, err);
1258 ext2fs_inode_alloc_stats2(fs, ino, -1,
1259 LINUX_S_ISDIR(inode.i_mode));
1262 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1265 ret = translate_error(fs, ino, err);
1272 static int __op_unlink(struct fuse2fs *ff, const char *path)
1274 ext2_filsys fs = ff->fs;
1279 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1281 ret = translate_error(fs, 0, err);
1285 ret = unlink_file_by_name(fs, path);
1289 ret = remove_inode(ff, ino);
1296 static int op_unlink(const char *path)
1298 struct fuse_context *ctxt = fuse_get_context();
1299 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1302 FUSE2FS_CHECK_CONTEXT(ff);
1303 pthread_mutex_lock(&ff->bfl);
1304 ret = __op_unlink(ff, path);
1305 pthread_mutex_unlock(&ff->bfl);
1314 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1315 int entry EXT2FS_ATTR((unused)),
1316 struct ext2_dir_entry *dirent,
1317 int offset EXT2FS_ATTR((unused)),
1318 int blocksize EXT2FS_ATTR((unused)),
1319 char *buf EXT2FS_ATTR((unused)),
1322 struct rd_struct *rds = (struct rd_struct *) private;
1324 if (dirent->inode == 0)
1326 if (((dirent->name_len & 0xFF) == 1) && (dirent->name[0] == '.'))
1328 if (((dirent->name_len & 0xFF) == 2) && (dirent->name[0] == '.') &&
1329 (dirent->name[1] == '.')) {
1330 rds->parent = dirent->inode;
1337 static int __op_rmdir(struct fuse2fs *ff, const char *path)
1339 ext2_filsys fs = ff->fs;
1342 struct ext2_inode_large inode;
1343 struct rd_struct rds;
1346 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &child);
1348 ret = translate_error(fs, 0, err);
1351 dbg_printf("%s: rmdir path=%s ino=%d\n", __func__, path, child);
1356 err = ext2fs_dir_iterate2(fs, child, 0, 0, rmdir_proc, &rds);
1358 ret = translate_error(fs, child, err);
1362 if (rds.empty == 0) {
1367 ret = unlink_file_by_name(fs, path);
1370 /* Directories have to be "removed" twice. */
1371 ret = remove_inode(ff, child);
1374 ret = remove_inode(ff, child);
1379 dbg_printf("%s: decr dir=%d link count\n", __func__,
1381 err = ext2fs_read_inode_full(fs, rds.parent,
1382 (struct ext2_inode *)&inode,
1385 ret = translate_error(fs, rds.parent, err);
1388 if (inode.i_links_count > 1)
1389 inode.i_links_count--;
1390 ret = update_mtime(fs, rds.parent, &inode);
1393 err = ext2fs_write_inode_full(fs, rds.parent,
1394 (struct ext2_inode *)&inode,
1397 ret = translate_error(fs, rds.parent, err);
1406 static int op_rmdir(const char *path)
1408 struct fuse_context *ctxt = fuse_get_context();
1409 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1412 FUSE2FS_CHECK_CONTEXT(ff);
1413 pthread_mutex_lock(&ff->bfl);
1414 ret = __op_rmdir(ff, path);
1415 pthread_mutex_unlock(&ff->bfl);
1419 static int op_symlink(const char *src, const char *dest)
1421 struct fuse_context *ctxt = fuse_get_context();
1422 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1424 ext2_ino_t parent, child;
1428 struct ext2_inode_large inode;
1431 FUSE2FS_CHECK_CONTEXT(ff);
1433 dbg_printf("%s: symlink %s to %s\n", __func__, src, dest);
1434 temp_path = strdup(dest);
1439 node_name = strrchr(temp_path, '/');
1448 pthread_mutex_lock(&ff->bfl);
1449 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1453 ret = translate_error(fs, 0, err);
1457 ret = check_inum_access(fs, parent, W_OK);
1462 /* Create symlink */
1463 err = ext2fs_symlink(fs, parent, 0, node_name, src);
1464 if (err == EXT2_ET_DIR_NO_SPACE) {
1465 err = ext2fs_expand_dir(fs, parent);
1467 ret = translate_error(fs, parent, err);
1471 err = ext2fs_symlink(fs, parent, 0, node_name, src);
1474 ret = translate_error(fs, parent, err);
1478 /* Update parent dir's mtime */
1479 ret = update_mtime(fs, parent, NULL);
1483 /* Still have to update the uid/gid of the symlink */
1484 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1487 ret = translate_error(fs, 0, err);
1490 dbg_printf("%s: symlinking ino=%d/name=%s to dir=%d\n", __func__,
1491 child, node_name, parent);
1493 memset(&inode, 0, sizeof(inode));
1494 err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode,
1497 ret = translate_error(fs, child, err);
1501 inode.i_uid = ctxt->uid;
1502 ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
1503 inode.i_gid = ctxt->gid;
1504 ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
1505 inode.i_generation = ff->next_generation++;
1507 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1510 ret = translate_error(fs, child, err);
1514 pthread_mutex_unlock(&ff->bfl);
1520 struct update_dotdot {
1521 ext2_ino_t new_dotdot;
1524 static int update_dotdot_helper(ext2_ino_t dir EXT2FS_ATTR((unused)),
1525 int entry EXT2FS_ATTR((unused)),
1526 struct ext2_dir_entry *dirent,
1527 int offset EXT2FS_ATTR((unused)),
1528 int blocksize EXT2FS_ATTR((unused)),
1529 char *buf EXT2FS_ATTR((unused)),
1532 struct update_dotdot *ud = priv_data;
1534 if (ext2fs_dirent_name_len(dirent) == 2 &&
1535 dirent->name[0] == '.' && dirent->name[1] == '.') {
1536 dirent->inode = ud->new_dotdot;
1537 return DIRENT_CHANGED | DIRENT_ABORT;
1543 static int op_rename(const char *from, const char *to)
1545 struct fuse_context *ctxt = fuse_get_context();
1546 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1549 ext2_ino_t from_ino, to_ino, to_dir_ino, from_dir_ino;
1550 char *temp_to = NULL, *temp_from = NULL;
1552 struct ext2_inode inode;
1553 struct update_dotdot ud;
1556 FUSE2FS_CHECK_CONTEXT(ff);
1558 dbg_printf("%s: renaming %s to %s\n", __func__, from, to);
1559 pthread_mutex_lock(&ff->bfl);
1560 if (!fs_can_allocate(ff, 5)) {
1565 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, from, &from_ino);
1566 if (err || from_ino == 0) {
1567 ret = translate_error(fs, 0, err);
1571 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, to, &to_ino);
1572 if (err && err != EXT2_ET_FILE_NOT_FOUND) {
1573 ret = translate_error(fs, 0, err);
1577 if (err == EXT2_ET_FILE_NOT_FOUND)
1580 /* Already the same file? */
1581 if (to_ino != 0 && to_ino == from_ino) {
1586 temp_to = strdup(to);
1592 temp_from = strdup(from);
1598 /* Find parent dir of the source and check write access */
1599 cp = strrchr(temp_from, '/');
1607 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_from,
1611 ret = translate_error(fs, 0, err);
1614 if (from_dir_ino == 0) {
1619 ret = check_inum_access(fs, from_dir_ino, W_OK);
1623 /* Find parent dir of the destination and check write access */
1624 cp = strrchr(temp_to, '/');
1632 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_to,
1636 ret = translate_error(fs, 0, err);
1639 if (to_dir_ino == 0) {
1644 ret = check_inum_access(fs, to_dir_ino, W_OK);
1648 /* If the target exists, unlink it first */
1650 err = ext2fs_read_inode(fs, to_ino, &inode);
1652 ret = translate_error(fs, to_ino, err);
1656 dbg_printf("%s: unlinking %s ino=%d\n", __func__,
1657 LINUX_S_ISDIR(inode.i_mode) ? "dir" : "file",
1659 if (LINUX_S_ISDIR(inode.i_mode))
1660 ret = __op_rmdir(ff, to);
1662 ret = __op_unlink(ff, to);
1667 /* Get ready to do the move */
1668 err = ext2fs_read_inode(fs, from_ino, &inode);
1670 ret = translate_error(fs, from_ino, err);
1674 /* Link in the new file */
1675 dbg_printf("%s: linking ino=%d/path=%s to dir=%d\n", __func__,
1676 from_ino, cp + 1, to_dir_ino);
1677 err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino,
1678 ext2_file_type(inode.i_mode));
1679 if (err == EXT2_ET_DIR_NO_SPACE) {
1680 err = ext2fs_expand_dir(fs, to_dir_ino);
1682 ret = translate_error(fs, to_dir_ino, err);
1686 err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino,
1687 ext2_file_type(inode.i_mode));
1690 ret = translate_error(fs, to_dir_ino, err);
1694 /* Update '..' pointer if dir */
1695 err = ext2fs_read_inode(fs, from_ino, &inode);
1697 ret = translate_error(fs, from_ino, err);
1701 if (LINUX_S_ISDIR(inode.i_mode)) {
1702 ud.new_dotdot = to_dir_ino;
1703 dbg_printf("%s: updating .. entry for dir=%d\n", __func__,
1705 err = ext2fs_dir_iterate2(fs, from_ino, 0, NULL,
1706 update_dotdot_helper, &ud);
1708 ret = translate_error(fs, from_ino, err);
1712 /* Decrease from_dir_ino's links_count */
1713 dbg_printf("%s: moving linkcount from dir=%d to dir=%d\n",
1714 __func__, from_dir_ino, to_dir_ino);
1715 err = ext2fs_read_inode(fs, from_dir_ino, &inode);
1717 ret = translate_error(fs, from_dir_ino, err);
1720 inode.i_links_count--;
1721 err = ext2fs_write_inode(fs, from_dir_ino, &inode);
1723 ret = translate_error(fs, from_dir_ino, err);
1727 /* Increase to_dir_ino's links_count */
1728 err = ext2fs_read_inode(fs, to_dir_ino, &inode);
1730 ret = translate_error(fs, to_dir_ino, err);
1733 inode.i_links_count++;
1734 err = ext2fs_write_inode(fs, to_dir_ino, &inode);
1736 ret = translate_error(fs, to_dir_ino, err);
1741 /* Update timestamps */
1742 ret = update_ctime(fs, from_ino, NULL);
1746 ret = update_mtime(fs, to_dir_ino, NULL);
1750 /* Remove the old file */
1751 ret = unlink_file_by_name(fs, from);
1755 /* Flush the whole mess out */
1756 err = ext2fs_flush2(fs, 0);
1758 ret = translate_error(fs, 0, err);
1764 pthread_mutex_unlock(&ff->bfl);
1768 static int op_link(const char *src, const char *dest)
1770 struct fuse_context *ctxt = fuse_get_context();
1771 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1776 ext2_ino_t parent, ino;
1777 struct ext2_inode_large inode;
1780 FUSE2FS_CHECK_CONTEXT(ff);
1782 dbg_printf("%s: src=%s dest=%s\n", __func__, src, dest);
1783 temp_path = strdup(dest);
1788 node_name = strrchr(temp_path, '/');
1797 pthread_mutex_lock(&ff->bfl);
1798 if (!fs_can_allocate(ff, 2)) {
1803 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1811 ret = check_inum_access(fs, parent, W_OK);
1816 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, src, &ino);
1817 if (err || ino == 0) {
1818 ret = translate_error(fs, 0, err);
1822 memset(&inode, 0, sizeof(inode));
1823 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1826 ret = translate_error(fs, ino, err);
1830 inode.i_links_count++;
1831 ret = update_ctime(fs, ino, &inode);
1835 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1838 ret = translate_error(fs, ino, err);
1842 dbg_printf("%s: linking ino=%d/name=%s to dir=%d\n", __func__, ino,
1844 err = ext2fs_link(fs, parent, node_name, ino,
1845 ext2_file_type(inode.i_mode));
1846 if (err == EXT2_ET_DIR_NO_SPACE) {
1847 err = ext2fs_expand_dir(fs, parent);
1849 ret = translate_error(fs, parent, err);
1853 err = ext2fs_link(fs, parent, node_name, ino,
1854 ext2_file_type(inode.i_mode));
1857 ret = translate_error(fs, parent, err);
1861 ret = update_mtime(fs, parent, NULL);
1866 pthread_mutex_unlock(&ff->bfl);
1872 static int op_chmod(const char *path, mode_t mode)
1874 struct fuse_context *ctxt = fuse_get_context();
1875 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1879 struct ext2_inode_large inode;
1882 FUSE2FS_CHECK_CONTEXT(ff);
1884 pthread_mutex_lock(&ff->bfl);
1885 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1887 ret = translate_error(fs, 0, err);
1890 dbg_printf("%s: path=%s mode=0%o ino=%d\n", __func__, path, mode, ino);
1892 memset(&inode, 0, sizeof(inode));
1893 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1896 ret = translate_error(fs, ino, err);
1900 if (!ff->fakeroot && ctxt->uid != 0 && ctxt->uid != inode_uid(inode)) {
1906 * XXX: We should really check that the inode gid is not in /any/
1907 * of the user's groups, but FUSE only tells us about the primary
1910 if (!ff->fakeroot && ctxt->uid != 0 && ctxt->gid != inode_gid(inode))
1913 inode.i_mode &= ~0xFFF;
1914 inode.i_mode |= mode & 0xFFF;
1915 ret = update_ctime(fs, ino, &inode);
1919 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1922 ret = translate_error(fs, ino, err);
1927 pthread_mutex_unlock(&ff->bfl);
1931 static int op_chown(const char *path, uid_t owner, gid_t group)
1933 struct fuse_context *ctxt = fuse_get_context();
1934 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1938 struct ext2_inode_large inode;
1941 FUSE2FS_CHECK_CONTEXT(ff);
1943 pthread_mutex_lock(&ff->bfl);
1944 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1946 ret = translate_error(fs, 0, err);
1949 dbg_printf("%s: path=%s owner=%d group=%d ino=%d\n", __func__,
1950 path, owner, group, ino);
1952 memset(&inode, 0, sizeof(inode));
1953 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1956 ret = translate_error(fs, ino, err);
1960 /* FUSE seems to feed us ~0 to mean "don't change" */
1961 if (owner != (uid_t) ~0) {
1962 /* Only root gets to change UID. */
1963 if (!ff->fakeroot && ctxt->uid != 0 &&
1964 !(inode_uid(inode) == ctxt->uid && owner == ctxt->uid)) {
1968 inode.i_uid = owner;
1969 ext2fs_set_i_uid_high(inode, owner >> 16);
1972 if (group != (gid_t) ~0) {
1973 /* Only root or the owner get to change GID. */
1974 if (!ff->fakeroot && ctxt->uid != 0 &&
1975 inode_uid(inode) != ctxt->uid) {
1980 /* XXX: We /should/ check group membership but FUSE */
1981 inode.i_gid = group;
1982 ext2fs_set_i_gid_high(inode, group >> 16);
1985 ret = update_ctime(fs, ino, &inode);
1989 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1992 ret = translate_error(fs, ino, err);
1997 pthread_mutex_unlock(&ff->bfl);
2001 static int op_truncate(const char *path, off_t len)
2003 struct fuse_context *ctxt = fuse_get_context();
2004 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2011 FUSE2FS_CHECK_CONTEXT(ff);
2013 pthread_mutex_lock(&ff->bfl);
2014 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2015 if (err || ino == 0) {
2016 ret = translate_error(fs, 0, err);
2019 dbg_printf("%s: ino=%d len=%jd\n", __func__, ino, len);
2021 ret = check_inum_access(fs, ino, W_OK);
2025 err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
2027 ret = translate_error(fs, ino, err);
2031 err = ext2fs_file_set_size2(file, len);
2033 ret = translate_error(fs, ino, err);
2038 err = ext2fs_file_close(file);
2042 ret = translate_error(fs, ino, err);
2046 ret = update_mtime(fs, ino, NULL);
2049 pthread_mutex_unlock(&ff->bfl);
2054 static void detect_linux_executable_open(int kernel_flags, int *access_check,
2055 int *e2fs_open_flags)
2058 * On Linux, execve will bleed __FMODE_EXEC into the file mode flags,
2059 * and FUSE is more than happy to let that slip through.
2061 if (kernel_flags & 0x20) {
2062 *access_check = X_OK;
2063 *e2fs_open_flags &= ~EXT2_FILE_WRITE;
2067 static void detect_linux_executable_open(int kernel_flags, int *access_check,
2068 int *e2fs_open_flags)
2072 #endif /* __linux__ */
2074 static int __op_open(struct fuse2fs *ff, const char *path,
2075 struct fuse_file_info *fp)
2077 ext2_filsys fs = ff->fs;
2079 struct fuse2fs_file_handle *file;
2080 int check = 0, ret = 0;
2082 dbg_printf("%s: path=%s\n", __func__, path);
2083 err = ext2fs_get_mem(sizeof(*file), &file);
2085 return translate_error(fs, 0, err);
2086 file->magic = FUSE2FS_FILE_MAGIC;
2088 file->open_flags = 0;
2089 switch (fp->flags & O_ACCMODE) {
2095 file->open_flags |= EXT2_FILE_WRITE;
2098 check = R_OK | W_OK;
2099 file->open_flags |= EXT2_FILE_WRITE;
2103 detect_linux_executable_open(fp->flags, &check, &file->open_flags);
2105 if (fp->flags & O_CREAT)
2106 file->open_flags |= EXT2_FILE_CREATE;
2108 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &file->ino);
2109 if (err || file->ino == 0) {
2110 ret = translate_error(fs, 0, err);
2113 dbg_printf("%s: ino=%d\n", __func__, file->ino);
2115 ret = check_inum_access(fs, file->ino, check);
2118 * In a regular (Linux) fs driver, the kernel will open
2119 * binaries for reading if the user has --x privileges (i.e.
2120 * execute without read). Since the kernel doesn't have any
2121 * way to tell us if it's opening a file via execve, we'll
2122 * just assume that allowing access is ok if asking for ro mode
2123 * fails but asking for x mode succeeds. Of course we can
2124 * also employ undocumented hacks (see above).
2126 if (check == R_OK) {
2127 ret = check_inum_access(fs, file->ino, X_OK);
2133 fp->fh = (uintptr_t)file;
2137 ext2fs_free_mem(&file);
2141 static int op_open(const char *path, struct fuse_file_info *fp)
2143 struct fuse_context *ctxt = fuse_get_context();
2144 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2147 FUSE2FS_CHECK_CONTEXT(ff);
2148 pthread_mutex_lock(&ff->bfl);
2149 ret = __op_open(ff, path, fp);
2150 pthread_mutex_unlock(&ff->bfl);
2154 static int op_read(const char *path EXT2FS_ATTR((unused)), char *buf,
2155 size_t len, off_t offset,
2156 struct fuse_file_info *fp)
2158 struct fuse_context *ctxt = fuse_get_context();
2159 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2160 struct fuse2fs_file_handle *fh =
2161 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2165 unsigned int got = 0;
2168 FUSE2FS_CHECK_CONTEXT(ff);
2170 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2171 dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset,
2173 pthread_mutex_lock(&ff->bfl);
2174 err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
2176 ret = translate_error(fs, fh->ino, err);
2180 err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL);
2182 ret = translate_error(fs, fh->ino, err);
2186 err = ext2fs_file_read(efp, buf, len, &got);
2188 ret = translate_error(fs, fh->ino, err);
2193 err = ext2fs_file_close(efp);
2197 ret = translate_error(fs, fh->ino, err);
2201 if (fs_writeable(fs)) {
2202 ret = update_atime(fs, fh->ino);
2207 pthread_mutex_unlock(&ff->bfl);
2208 return got ? (int) got : ret;
2211 static int op_write(const char *path EXT2FS_ATTR((unused)),
2212 const char *buf, size_t len, off_t offset,
2213 struct fuse_file_info *fp)
2215 struct fuse_context *ctxt = fuse_get_context();
2216 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2217 struct fuse2fs_file_handle *fh =
2218 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2222 unsigned int got = 0;
2225 FUSE2FS_CHECK_CONTEXT(ff);
2227 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2228 dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset,
2230 pthread_mutex_lock(&ff->bfl);
2231 if (!fs_writeable(fs)) {
2236 if (!fs_can_allocate(ff, len / fs->blocksize)) {
2241 err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
2243 ret = translate_error(fs, fh->ino, err);
2247 err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL);
2249 ret = translate_error(fs, fh->ino, err);
2253 err = ext2fs_file_write(efp, buf, len, &got);
2255 ret = translate_error(fs, fh->ino, err);
2259 err = ext2fs_file_flush(efp);
2262 ret = translate_error(fs, fh->ino, err);
2267 err = ext2fs_file_close(efp);
2271 ret = translate_error(fs, fh->ino, err);
2275 ret = update_mtime(fs, fh->ino, NULL);
2280 pthread_mutex_unlock(&ff->bfl);
2281 return got ? (int) got : ret;
2284 static int op_release(const char *path EXT2FS_ATTR((unused)),
2285 struct fuse_file_info *fp)
2287 struct fuse_context *ctxt = fuse_get_context();
2288 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2289 struct fuse2fs_file_handle *fh =
2290 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2295 FUSE2FS_CHECK_CONTEXT(ff);
2297 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2298 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2299 pthread_mutex_lock(&ff->bfl);
2300 if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
2301 err = ext2fs_flush2(fs, EXT2_FLAG_FLUSH_NO_SYNC);
2303 ret = translate_error(fs, fh->ino, err);
2306 pthread_mutex_unlock(&ff->bfl);
2308 ext2fs_free_mem(&fh);
2313 static int op_fsync(const char *path EXT2FS_ATTR((unused)),
2314 int datasync EXT2FS_ATTR((unused)),
2315 struct fuse_file_info *fp)
2317 struct fuse_context *ctxt = fuse_get_context();
2318 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2319 struct fuse2fs_file_handle *fh =
2320 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2325 FUSE2FS_CHECK_CONTEXT(ff);
2327 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2328 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2329 /* For now, flush everything, even if it's slow */
2330 pthread_mutex_lock(&ff->bfl);
2331 if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
2332 err = ext2fs_flush2(fs, 0);
2334 ret = translate_error(fs, fh->ino, err);
2336 pthread_mutex_unlock(&ff->bfl);
2341 static int op_statfs(const char *path EXT2FS_ATTR((unused)),
2342 struct statvfs *buf)
2344 struct fuse_context *ctxt = fuse_get_context();
2345 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2348 blk64_t overhead, reserved, free;
2350 FUSE2FS_CHECK_CONTEXT(ff);
2352 dbg_printf("%s: path=%s\n", __func__, path);
2353 buf->f_bsize = fs->blocksize;
2359 overhead = fs->desc_blocks +
2360 (blk64_t)fs->group_desc_count *
2361 (fs->inode_blocks_per_group + 2);
2362 reserved = ext2fs_r_blocks_count(fs->super);
2364 reserved = ext2fs_blocks_count(fs->super) / 10;
2365 free = ext2fs_free_blocks_count(fs->super);
2367 buf->f_blocks = ext2fs_blocks_count(fs->super) - overhead;
2368 buf->f_bfree = free;
2369 if (free < reserved)
2372 buf->f_bavail = free - reserved;
2373 buf->f_files = fs->super->s_inodes_count;
2374 buf->f_ffree = fs->super->s_free_inodes_count;
2375 buf->f_favail = fs->super->s_free_inodes_count;
2376 f = (uint64_t *)fs->super->s_uuid;
2382 if (fs->flags & EXT2_FLAG_RW)
2383 buf->f_flag |= ST_RDONLY;
2384 buf->f_namemax = EXT2_NAME_LEN;
2389 typedef errcode_t (*xattr_xlate_get)(void **cooked_buf, size_t *cooked_sz,
2390 const void *raw_buf, size_t raw_sz);
2391 typedef errcode_t (*xattr_xlate_set)(const void *cooked_buf, size_t cooked_sz,
2392 const void **raw_buf, size_t *raw_sz);
2393 struct xattr_translate {
2395 xattr_xlate_get get;
2396 xattr_xlate_set set;
2399 #define XATTR_TRANSLATOR(p, g, s) \
2401 .get = (xattr_xlate_get)(g), \
2402 .set = (xattr_xlate_set)(s)}
2404 static struct xattr_translate xattr_translators[] = {
2405 #ifdef TRANSLATE_LINUX_ACLS
2406 XATTR_TRANSLATOR(ACL_EA_ACCESS, ext4_to_fuse_acl, fuse_to_ext4_acl),
2407 XATTR_TRANSLATOR(ACL_EA_DEFAULT, ext4_to_fuse_acl, fuse_to_ext4_acl),
2409 XATTR_TRANSLATOR(NULL, NULL, NULL),
2411 #undef XATTR_TRANSLATOR
2413 static int op_getxattr(const char *path, const char *key, char *value,
2416 struct fuse_context *ctxt = fuse_get_context();
2417 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2419 struct ext2_xattr_handle *h;
2420 struct xattr_translate *xt;
2427 FUSE2FS_CHECK_CONTEXT(ff);
2429 pthread_mutex_lock(&ff->bfl);
2430 if (!ext2fs_has_feature_xattr(fs->super)) {
2435 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2436 if (err || ino == 0) {
2437 ret = translate_error(fs, 0, err);
2440 dbg_printf("%s: ino=%d\n", __func__, ino);
2442 ret = check_inum_access(fs, ino, R_OK);
2446 err = ext2fs_xattrs_open(fs, ino, &h);
2448 ret = translate_error(fs, ino, err);
2452 err = ext2fs_xattrs_read(h);
2454 ret = translate_error(fs, ino, err);
2458 err = ext2fs_xattr_get(h, key, &ptr, &plen);
2460 ret = translate_error(fs, ino, err);
2464 for (xt = xattr_translators; xt->prefix != NULL; xt++) {
2465 if (strncmp(key, xt->prefix, strlen(xt->prefix)) == 0) {
2466 err = xt->get(&cptr, &clen, ptr, plen);
2469 ext2fs_free_mem(&ptr);
2477 } else if (len < plen) {
2480 memcpy(value, ptr, plen);
2485 ext2fs_free_mem(&ptr);
2487 err = ext2fs_xattrs_close(&h);
2489 ret = translate_error(fs, ino, err);
2491 pthread_mutex_unlock(&ff->bfl);
2496 static int count_buffer_space(char *name, char *value EXT2FS_ATTR((unused)),
2497 size_t value_len EXT2FS_ATTR((unused)),
2500 unsigned int *x = data;
2502 *x = *x + strlen(name) + 1;
2506 static int copy_names(char *name, char *value EXT2FS_ATTR((unused)),
2507 size_t value_len EXT2FS_ATTR((unused)), void *data)
2511 strncpy(*b, name, strlen(name));
2512 *b = *b + strlen(name) + 1;
2517 static int op_listxattr(const char *path, char *names, size_t len)
2519 struct fuse_context *ctxt = fuse_get_context();
2520 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2522 struct ext2_xattr_handle *h;
2528 FUSE2FS_CHECK_CONTEXT(ff);
2530 pthread_mutex_lock(&ff->bfl);
2531 if (!ext2fs_has_feature_xattr(fs->super)) {
2536 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2537 if (err || ino == 0) {
2538 ret = translate_error(fs, ino, err);
2541 dbg_printf("%s: ino=%d\n", __func__, ino);
2543 ret = check_inum_access(fs, ino, R_OK);
2547 err = ext2fs_xattrs_open(fs, ino, &h);
2549 ret = translate_error(fs, ino, err);
2553 err = ext2fs_xattrs_read(h);
2555 ret = translate_error(fs, ino, err);
2559 /* Count buffer space needed for names */
2561 err = ext2fs_xattrs_iterate(h, count_buffer_space, &bufsz);
2563 ret = translate_error(fs, ino, err);
2570 } else if (len < bufsz) {
2575 /* Copy names out */
2576 memset(names, 0, len);
2577 err = ext2fs_xattrs_iterate(h, copy_names, &names);
2579 ret = translate_error(fs, ino, err);
2584 err = ext2fs_xattrs_close(&h);
2586 ret = translate_error(fs, ino, err);
2588 pthread_mutex_unlock(&ff->bfl);
2593 static int op_setxattr(const char *path EXT2FS_ATTR((unused)),
2594 const char *key, const char *value,
2595 size_t len, int flags EXT2FS_ATTR((unused)))
2597 struct fuse_context *ctxt = fuse_get_context();
2598 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2600 struct ext2_xattr_handle *h;
2601 struct xattr_translate *xt;
2608 FUSE2FS_CHECK_CONTEXT(ff);
2610 pthread_mutex_lock(&ff->bfl);
2611 if (!ext2fs_has_feature_xattr(fs->super)) {
2616 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2617 if (err || ino == 0) {
2618 ret = translate_error(fs, 0, err);
2621 dbg_printf("%s: ino=%d\n", __func__, ino);
2623 ret = check_inum_access(fs, ino, W_OK);
2624 if (ret == -EACCES) {
2630 err = ext2fs_xattrs_open(fs, ino, &h);
2632 ret = translate_error(fs, ino, err);
2636 err = ext2fs_xattrs_read(h);
2638 ret = translate_error(fs, ino, err);
2644 for (xt = xattr_translators; xt->prefix != NULL; xt++) {
2645 if (strncmp(key, xt->prefix, strlen(xt->prefix)) == 0) {
2646 err = xt->set(value, len, &cvalue, &clen);
2652 err = ext2fs_xattr_set(h, key, cvalue, clen);
2654 ret = translate_error(fs, ino, err);
2658 ret = update_ctime(fs, ino, NULL);
2660 if (cvalue != value)
2661 ext2fs_free_mem(&cvalue);
2663 err = ext2fs_xattrs_close(&h);
2665 ret = translate_error(fs, ino, err);
2667 pthread_mutex_unlock(&ff->bfl);
2672 static int op_removexattr(const char *path, const char *key)
2674 struct fuse_context *ctxt = fuse_get_context();
2675 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2677 struct ext2_xattr_handle *h;
2682 FUSE2FS_CHECK_CONTEXT(ff);
2684 pthread_mutex_lock(&ff->bfl);
2685 if (!ext2fs_has_feature_xattr(fs->super)) {
2690 if (!fs_can_allocate(ff, 1)) {
2695 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2696 if (err || ino == 0) {
2697 ret = translate_error(fs, 0, err);
2700 dbg_printf("%s: ino=%d\n", __func__, ino);
2702 ret = check_inum_access(fs, ino, W_OK);
2706 err = ext2fs_xattrs_open(fs, ino, &h);
2708 ret = translate_error(fs, ino, err);
2712 err = ext2fs_xattrs_read(h);
2714 ret = translate_error(fs, ino, err);
2718 err = ext2fs_xattr_remove(h, key);
2720 ret = translate_error(fs, ino, err);
2724 ret = update_ctime(fs, ino, NULL);
2726 err = ext2fs_xattrs_close(&h);
2728 ret = translate_error(fs, ino, err);
2730 pthread_mutex_unlock(&ff->bfl);
2735 struct readdir_iter {
2737 fuse_fill_dir_t func;
2740 static int op_readdir_iter(ext2_ino_t dir EXT2FS_ATTR((unused)),
2741 int entry EXT2FS_ATTR((unused)),
2742 struct ext2_dir_entry *dirent,
2743 int offset EXT2FS_ATTR((unused)),
2744 int blocksize EXT2FS_ATTR((unused)),
2745 char *buf EXT2FS_ATTR((unused)), void *data)
2747 struct readdir_iter *i = data;
2748 char namebuf[EXT2_NAME_LEN + 1];
2751 memcpy(namebuf, dirent->name, dirent->name_len & 0xFF);
2752 namebuf[dirent->name_len & 0xFF] = 0;
2753 ret = i->func(i->buf, namebuf, NULL, 0);
2755 return DIRENT_ABORT;
2760 static int op_readdir(const char *path EXT2FS_ATTR((unused)),
2761 void *buf, fuse_fill_dir_t fill_func,
2762 off_t offset EXT2FS_ATTR((unused)),
2763 struct fuse_file_info *fp)
2765 struct fuse_context *ctxt = fuse_get_context();
2766 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2767 struct fuse2fs_file_handle *fh =
2768 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2771 struct readdir_iter i;
2774 FUSE2FS_CHECK_CONTEXT(ff);
2776 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2777 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2778 pthread_mutex_lock(&ff->bfl);
2781 err = ext2fs_dir_iterate2(fs, fh->ino, 0, NULL, op_readdir_iter, &i);
2783 ret = translate_error(fs, fh->ino, err);
2787 if (fs_writeable(fs)) {
2788 ret = update_atime(fs, fh->ino);
2793 pthread_mutex_unlock(&ff->bfl);
2797 static int op_access(const char *path, int mask)
2799 struct fuse_context *ctxt = fuse_get_context();
2800 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2806 FUSE2FS_CHECK_CONTEXT(ff);
2808 dbg_printf("%s: path=%s mask=0x%x\n", __func__, path, mask);
2809 pthread_mutex_lock(&ff->bfl);
2810 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2811 if (err || ino == 0) {
2812 ret = translate_error(fs, 0, err);
2816 ret = check_inum_access(fs, ino, mask);
2821 pthread_mutex_unlock(&ff->bfl);
2825 static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
2827 struct fuse_context *ctxt = fuse_get_context();
2828 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2830 ext2_ino_t parent, child;
2835 struct ext2_inode_large inode;
2838 FUSE2FS_CHECK_CONTEXT(ff);
2840 dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode);
2841 temp_path = strdup(path);
2846 node_name = strrchr(temp_path, '/');
2855 pthread_mutex_lock(&ff->bfl);
2856 if (!fs_can_allocate(ff, 1)) {
2861 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
2864 ret = translate_error(fs, 0, err);
2868 ret = check_inum_access(fs, parent, W_OK);
2874 filetype = ext2_file_type(mode);
2876 err = ext2fs_new_inode(fs, parent, mode, 0, &child);
2878 ret = translate_error(fs, parent, err);
2882 dbg_printf("%s: creating ino=%d/name=%s in dir=%d\n", __func__, child,
2884 err = ext2fs_link(fs, parent, node_name, child, filetype);
2885 if (err == EXT2_ET_DIR_NO_SPACE) {
2886 err = ext2fs_expand_dir(fs, parent);
2888 ret = translate_error(fs, parent, err);
2892 err = ext2fs_link(fs, parent, node_name, child,
2896 ret = translate_error(fs, parent, err);
2900 ret = update_mtime(fs, parent, NULL);
2904 memset(&inode, 0, sizeof(inode));
2905 inode.i_mode = mode;
2906 inode.i_links_count = 1;
2907 inode.i_extra_isize = sizeof(struct ext2_inode_large) -
2908 EXT2_GOOD_OLD_INODE_SIZE;
2909 inode.i_uid = ctxt->uid;
2910 ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
2911 inode.i_gid = ctxt->gid;
2912 ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
2913 if (ext2fs_has_feature_extents(fs->super)) {
2914 ext2_extent_handle_t handle;
2916 inode.i_flags &= ~EXT4_EXTENTS_FL;
2917 ret = ext2fs_extent_open2(fs, child,
2918 (struct ext2_inode *)&inode, &handle);
2921 ext2fs_extent_free(handle);
2924 err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode);
2926 ret = translate_error(fs, child, err);
2930 inode.i_generation = ff->next_generation++;
2932 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
2935 ret = translate_error(fs, child, err);
2939 ext2fs_inode_alloc_stats2(fs, child, 1, 0);
2941 ret = __op_open(ff, path, fp);
2945 pthread_mutex_unlock(&ff->bfl);
2951 static int op_ftruncate(const char *path EXT2FS_ATTR((unused)),
2952 off_t len, struct fuse_file_info *fp)
2954 struct fuse_context *ctxt = fuse_get_context();
2955 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2956 struct fuse2fs_file_handle *fh =
2957 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2963 FUSE2FS_CHECK_CONTEXT(ff);
2965 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2966 dbg_printf("%s: ino=%d len=%jd\n", __func__, fh->ino, len);
2967 pthread_mutex_lock(&ff->bfl);
2968 if (!fs_writeable(fs)) {
2973 err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
2975 ret = translate_error(fs, fh->ino, err);
2979 err = ext2fs_file_set_size2(efp, len);
2981 ret = translate_error(fs, fh->ino, err);
2986 err = ext2fs_file_close(efp);
2990 ret = translate_error(fs, fh->ino, err);
2994 ret = update_mtime(fs, fh->ino, NULL);
2999 pthread_mutex_unlock(&ff->bfl);
3003 static int op_fgetattr(const char *path EXT2FS_ATTR((unused)),
3004 struct stat *statbuf,
3005 struct fuse_file_info *fp)
3007 struct fuse_context *ctxt = fuse_get_context();
3008 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3010 struct fuse2fs_file_handle *fh =
3011 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3014 FUSE2FS_CHECK_CONTEXT(ff);
3016 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3017 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3018 pthread_mutex_lock(&ff->bfl);
3019 ret = stat_inode(fs, fh->ino, statbuf);
3020 pthread_mutex_unlock(&ff->bfl);
3025 static int op_utimens(const char *path, const struct timespec ctv[2])
3027 struct fuse_context *ctxt = fuse_get_context();
3028 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3029 struct timespec tv[2];
3033 struct ext2_inode_large inode;
3036 FUSE2FS_CHECK_CONTEXT(ff);
3038 pthread_mutex_lock(&ff->bfl);
3039 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
3041 ret = translate_error(fs, 0, err);
3044 dbg_printf("%s: ino=%d\n", __func__, ino);
3046 ret = check_inum_access(fs, ino, W_OK);
3050 memset(&inode, 0, sizeof(inode));
3051 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
3054 ret = translate_error(fs, ino, err);
3061 if (tv[0].tv_nsec == UTIME_NOW)
3063 if (tv[1].tv_nsec == UTIME_NOW)
3065 #endif /* UTIME_NOW */
3067 if (tv[0].tv_nsec != UTIME_OMIT)
3068 EXT4_INODE_SET_XTIME(i_atime, tv, &inode);
3069 if (tv[1].tv_nsec != UTIME_OMIT)
3070 EXT4_INODE_SET_XTIME(i_mtime, tv + 1, &inode);
3071 #endif /* UTIME_OMIT */
3072 ret = update_ctime(fs, ino, &inode);
3076 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
3079 ret = translate_error(fs, ino, err);
3084 pthread_mutex_unlock(&ff->bfl);
3088 #ifdef SUPPORT_I_FLAGS
3089 static int ioctl_getflags(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3093 struct ext2_inode_large inode;
3095 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3096 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3097 memset(&inode, 0, sizeof(inode));
3098 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3101 return translate_error(fs, fh->ino, err);
3103 *(__u32 *)data = inode.i_flags & EXT2_FL_USER_VISIBLE;
3107 #define FUSE2FS_MODIFIABLE_IFLAGS \
3108 (EXT2_IMMUTABLE_FL | EXT2_APPEND_FL | EXT2_NODUMP_FL | \
3109 EXT2_NOATIME_FL | EXT3_JOURNAL_DATA_FL | EXT2_DIRSYNC_FL | \
3112 static int ioctl_setflags(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3116 struct ext2_inode_large inode;
3118 __u32 flags = *(__u32 *)data;
3119 struct fuse_context *ctxt = fuse_get_context();
3120 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3122 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3123 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3124 memset(&inode, 0, sizeof(inode));
3125 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3128 return translate_error(fs, fh->ino, err);
3130 if (!ff->fakeroot && ctxt->uid != 0 && inode_uid(inode) != ctxt->uid)
3133 if ((inode.i_flags ^ flags) & ~FUSE2FS_MODIFIABLE_IFLAGS)
3136 inode.i_flags = (inode.i_flags & ~FUSE2FS_MODIFIABLE_IFLAGS) |
3137 (flags & FUSE2FS_MODIFIABLE_IFLAGS);
3139 ret = update_ctime(fs, fh->ino, &inode);
3143 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3146 return translate_error(fs, fh->ino, err);
3151 static int ioctl_getversion(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3155 struct ext2_inode_large inode;
3157 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3158 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3159 memset(&inode, 0, sizeof(inode));
3160 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3163 return translate_error(fs, fh->ino, err);
3165 *(__u32 *)data = inode.i_generation;
3169 static int ioctl_setversion(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3173 struct ext2_inode_large inode;
3175 __u32 generation = *(__u32 *)data;
3176 struct fuse_context *ctxt = fuse_get_context();
3177 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3179 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3180 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3181 memset(&inode, 0, sizeof(inode));
3182 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3185 return translate_error(fs, fh->ino, err);
3187 if (!ff->fakeroot && ctxt->uid != 0 && inode_uid(inode) != ctxt->uid)
3190 inode.i_generation = generation;
3192 ret = update_ctime(fs, fh->ino, &inode);
3196 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3199 return translate_error(fs, fh->ino, err);
3203 #endif /* SUPPORT_I_FLAGS */
3206 static int ioctl_fitrim(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3209 struct fstrim_range *fr = data;
3210 blk64_t start, end, max_blocks, b, cleared;
3213 start = fr->start / fs->blocksize;
3214 end = (fr->start + fr->len - 1) / fs->blocksize;
3215 dbg_printf("%s: start=%llu end=%llu\n", __func__, start, end);
3217 if (start < fs->super->s_first_data_block)
3218 start = fs->super->s_first_data_block;
3219 if (start >= ext2fs_blocks_count(fs->super))
3220 start = ext2fs_blocks_count(fs->super) - 1;
3222 if (end < fs->super->s_first_data_block)
3223 end = fs->super->s_first_data_block;
3224 if (end >= ext2fs_blocks_count(fs->super))
3225 end = ext2fs_blocks_count(fs->super) - 1;
3228 max_blocks = 2048ULL * 1024 * 1024 / fs->blocksize;
3231 while (start <= end) {
3232 err = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
3233 start, end, &start);
3237 return translate_error(fs, fh->ino, err);
3239 b = start + max_blocks < end ? start + max_blocks : end;
3240 err = ext2fs_find_first_set_block_bitmap2(fs->block_map,
3242 if (err && err != ENOENT)
3243 return translate_error(fs, fh->ino, err);
3244 if (b - start >= fr->minlen) {
3245 err = io_channel_discard(fs->io, start, b - start);
3247 return translate_error(fs, fh->ino, err);
3248 cleared += b - start;
3249 fr->len = cleared * fs->blocksize;
3258 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
3259 static int op_ioctl(const char *path EXT2FS_ATTR((unused)), int cmd,
3260 void *arg EXT2FS_ATTR((unused)),
3261 struct fuse_file_info *fp,
3262 unsigned int flags EXT2FS_ATTR((unused)), void *data)
3264 struct fuse_context *ctxt = fuse_get_context();
3265 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3266 struct fuse2fs_file_handle *fh =
3267 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3271 FUSE2FS_CHECK_CONTEXT(ff);
3273 pthread_mutex_lock(&ff->bfl);
3274 switch ((unsigned long) cmd) {
3275 #ifdef SUPPORT_I_FLAGS
3276 case EXT2_IOC_GETFLAGS:
3277 ret = ioctl_getflags(fs, fh, data);
3279 case EXT2_IOC_SETFLAGS:
3280 ret = ioctl_setflags(fs, fh, data);
3282 case EXT2_IOC_GETVERSION:
3283 ret = ioctl_getversion(fs, fh, data);
3285 case EXT2_IOC_SETVERSION:
3286 ret = ioctl_setversion(fs, fh, data);
3291 ret = ioctl_fitrim(fs, fh, data);
3295 dbg_printf("%s: Unknown ioctl %d\n", __func__, cmd);
3298 pthread_mutex_unlock(&ff->bfl);
3302 #endif /* FUSE 28 */
3304 static int op_bmap(const char *path, size_t blocksize EXT2FS_ATTR((unused)),
3307 struct fuse_context *ctxt = fuse_get_context();
3308 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3314 FUSE2FS_CHECK_CONTEXT(ff);
3316 pthread_mutex_lock(&ff->bfl);
3317 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
3319 ret = translate_error(fs, 0, err);
3322 dbg_printf("%s: ino=%d blk=%"PRIu64"\n", __func__, ino, *idx);
3324 err = ext2fs_bmap2(fs, ino, NULL, NULL, 0, *idx, 0, (blk64_t *)idx);
3326 ret = translate_error(fs, ino, err);
3331 pthread_mutex_unlock(&ff->bfl);
3335 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3336 # ifdef SUPPORT_FALLOCATE
3337 static int fallocate_helper(struct fuse_file_info *fp, int mode, off_t offset,
3340 struct fuse_context *ctxt = fuse_get_context();
3341 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3342 struct fuse2fs_file_handle *fh =
3343 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3345 struct ext2_inode_large inode;
3351 FUSE2FS_CHECK_CONTEXT(ff);
3353 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3354 start = offset / fs->blocksize;
3355 end = (offset + len - 1) / fs->blocksize;
3356 dbg_printf("%s: ino=%d mode=0x%x start=%jd end=%llu\n", __func__,
3357 fh->ino, mode, offset / fs->blocksize, end);
3358 if (!fs_can_allocate(ff, len / fs->blocksize))
3361 memset(&inode, 0, sizeof(inode));
3362 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3366 fsize = EXT2_I_SIZE(&inode);
3368 /* Allocate a bunch of blocks */
3369 flags = (mode & FL_KEEP_SIZE_FLAG ? 0 :
3370 EXT2_FALLOCATE_INIT_BEYOND_EOF);
3371 err = ext2fs_fallocate(fs, flags, fh->ino,
3372 (struct ext2_inode *)&inode,
3373 ~0ULL, start, end - start + 1);
3374 if (err && err != EXT2_ET_BLOCK_ALLOC_FAIL)
3375 return translate_error(fs, fh->ino, err);
3378 if (!(mode & FL_KEEP_SIZE_FLAG)) {
3379 if ((__u64) offset + len > fsize) {
3380 err = ext2fs_inode_size_set(fs,
3381 (struct ext2_inode *)&inode,
3384 return translate_error(fs, fh->ino, err);
3388 err = update_mtime(fs, fh->ino, &inode);
3392 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3395 return translate_error(fs, fh->ino, err);
3400 static errcode_t clean_block_middle(ext2_filsys fs, ext2_ino_t ino,
3401 struct ext2_inode_large *inode, off_t offset,
3402 off_t len, char **buf)
3409 residue = offset % fs->blocksize;
3414 err = ext2fs_get_mem(fs->blocksize, buf);
3419 err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0,
3420 offset / fs->blocksize, &retflags, &blk);
3423 if (!blk || (retflags & BMAP_RET_UNINIT))
3426 err = io_channel_read_blk(fs->io, blk, 1, *buf);
3430 memset(*buf + residue, 0, len);
3432 return io_channel_write_blk(fs->io, blk, 1, *buf);
3435 static errcode_t clean_block_edge(ext2_filsys fs, ext2_ino_t ino,
3436 struct ext2_inode_large *inode, off_t offset,
3437 int clean_before, char **buf)
3444 residue = offset % fs->blocksize;
3449 err = ext2fs_get_mem(fs->blocksize, buf);
3454 err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0,
3455 offset / fs->blocksize, &retflags, &blk);
3459 err = io_channel_read_blk(fs->io, blk, 1, *buf);
3462 if (!blk || (retflags & BMAP_RET_UNINIT))
3466 memset(*buf, 0, residue);
3468 memset(*buf + residue, 0, fs->blocksize - residue);
3470 return io_channel_write_blk(fs->io, blk, 1, *buf);
3473 static int punch_helper(struct fuse_file_info *fp, int mode, off_t offset,
3476 struct fuse_context *ctxt = fuse_get_context();
3477 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3478 struct fuse2fs_file_handle *fh =
3479 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3481 struct ext2_inode_large inode;
3486 FUSE2FS_CHECK_CONTEXT(ff);
3488 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3489 dbg_printf("%s: offset=%jd len=%jd\n", __func__, offset, len);
3491 /* kernel ext4 punch requires this flag to be set */
3492 if (!(mode & FL_KEEP_SIZE_FLAG))
3495 /* Punch out a bunch of blocks */
3496 start = (offset + fs->blocksize - 1) / fs->blocksize;
3497 end = (offset + len - fs->blocksize) / fs->blocksize;
3498 dbg_printf("%s: ino=%d mode=0x%x start=%llu end=%llu\n", __func__,
3499 fh->ino, mode, start, end);
3501 memset(&inode, 0, sizeof(inode));
3502 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3505 return translate_error(fs, fh->ino, err);
3507 /* Zero everything before the first block and after the last block */
3508 if ((offset / fs->blocksize) == ((offset + len) / fs->blocksize))
3509 err = clean_block_middle(fs, fh->ino, &inode, offset,
3512 err = clean_block_edge(fs, fh->ino, &inode, offset, 0, &buf);
3514 err = clean_block_edge(fs, fh->ino, &inode,
3515 offset + len, 1, &buf);
3518 ext2fs_free_mem(&buf);
3520 return translate_error(fs, fh->ino, err);
3522 /* Unmap full blocks in the middle */
3524 err = ext2fs_punch(fs, fh->ino, (struct ext2_inode *)&inode,
3527 return translate_error(fs, fh->ino, err);
3530 err = update_mtime(fs, fh->ino, &inode);
3534 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3537 return translate_error(fs, fh->ino, err);
3542 static int op_fallocate(const char *path EXT2FS_ATTR((unused)), int mode,
3543 off_t offset, off_t len,
3544 struct fuse_file_info *fp)
3546 struct fuse_context *ctxt = fuse_get_context();
3547 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3548 ext2_filsys fs = ff->fs;
3551 /* Catch unknown flags */
3552 if (mode & ~(FL_PUNCH_HOLE_FLAG | FL_KEEP_SIZE_FLAG))
3555 pthread_mutex_lock(&ff->bfl);
3556 if (!fs_writeable(fs)) {
3560 if (mode & FL_PUNCH_HOLE_FLAG)
3561 ret = punch_helper(fp, mode, offset, len);
3563 ret = fallocate_helper(fp, mode, offset, len);
3565 pthread_mutex_unlock(&ff->bfl);
3569 # endif /* SUPPORT_FALLOCATE */
3570 #endif /* FUSE 29 */
3572 static struct fuse_operations fs_ops = {
3574 .destroy = op_destroy,
3575 .getattr = op_getattr,
3576 .readlink = op_readlink,
3579 .unlink = op_unlink,
3581 .symlink = op_symlink,
3582 .rename = op_rename,
3586 .truncate = op_truncate,
3590 .statfs = op_statfs,
3591 .release = op_release,
3593 .setxattr = op_setxattr,
3594 .getxattr = op_getxattr,
3595 .listxattr = op_listxattr,
3596 .removexattr = op_removexattr,
3598 .readdir = op_readdir,
3599 .releasedir = op_release,
3600 .fsyncdir = op_fsync,
3601 .access = op_access,
3602 .create = op_create,
3603 .ftruncate = op_ftruncate,
3604 .fgetattr = op_fgetattr,
3605 .utimens = op_utimens,
3606 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3607 # if defined(UTIME_NOW) || defined(UTIME_OMIT)
3608 .flag_utime_omit_ok = 1,
3616 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
3618 .flag_nullpath_ok = 1,
3620 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3622 # ifdef SUPPORT_FALLOCATE
3623 .fallocate = op_fallocate,
3628 static int get_random_bytes(void *p, size_t sz)
3633 fd = open("/dev/urandom", O_RDONLY);
3635 perror("/dev/urandom");
3639 r = read(fd, p, sz);
3642 return (size_t) r == sz;
3651 #define FUSE2FS_OPT(t, p, v) { t, offsetof(struct fuse2fs, p), v }
3653 static struct fuse_opt fuse2fs_opts[] = {
3654 FUSE2FS_OPT("ro", ro, 1),
3655 FUSE2FS_OPT("errors=panic", panic_on_error, 1),
3656 FUSE2FS_OPT("minixdf", minixdf, 1),
3657 FUSE2FS_OPT("fakeroot", fakeroot, 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 fakeroot pretend to be root for permission checks\n"
3697 " -o no_default_opts do not include default fuse options\n"
3698 " -o fuse2fs_debug enable fuse2fs debugging\n"
3701 if (key == FUSE2FS_HELPFULL) {
3702 fuse_opt_add_arg(outargs, "-ho");
3703 fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL);
3705 fprintf(stderr, "Try --helpfull to get a list of "
3706 "all flags, including the FUSE options.\n");
3710 case FUSE2FS_VERSION:
3711 fprintf(stderr, "fuse2fs %s (%s)\n", E2FSPROGS_VERSION,
3713 fuse_opt_add_arg(outargs, "--version");
3714 fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL);
3720 int main(int argc, char *argv[])
3722 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
3723 struct fuse2fs fctx;
3726 char extra_args[BUFSIZ];
3727 int ret = 0, flags = EXT2_FLAG_64BITS | EXT2_FLAG_EXCLUSIVE;
3729 memset(&fctx, 0, sizeof(fctx));
3730 fctx.magic = FUSE2FS_MAGIC;
3732 fuse_opt_parse(&args, &fctx, fuse2fs_opts, fuse2fs_opt_proc);
3733 if (fctx.device == NULL) {
3734 fprintf(stderr, "Missing ext4 device/image\n");
3735 fprintf(stderr, "See '%s -h' for usage\n", argv[0]);
3740 printf("%s", _("Mounting read-only.\n"));
3743 setlocale(LC_MESSAGES, "");
3744 setlocale(LC_CTYPE, "");
3745 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
3746 textdomain(NLS_CAT_NAME);
3747 set_com_err_gettext(gettext);
3749 add_error_table(&et_ext2_error_table);
3751 /* Set up error logging */
3752 logfile = getenv("FUSE2FS_LOGFILE");
3754 fctx.err_fp = fopen(logfile, "a");
3760 fctx.err_fp = stderr;
3762 /* Will we allow users to allocate every last block? */
3763 if (getenv("FUSE2FS_ALLOC_ALL_BLOCKS")) {
3764 printf(_("%s: Allowing users to allocate all blocks. "
3765 "This is dangerous!\n"), fctx.device);
3766 fctx.alloc_all_blocks = 1;
3769 /* Start up the fs (while we still can use stdout) */
3772 flags |= EXT2_FLAG_RW;
3773 err = ext2fs_open2(fctx.device, NULL, flags, 0, 0, unix_io_manager,
3776 printf(_("%s: %s.\n"), fctx.device, error_message(err));
3777 printf(_("Please run e2fsck -fy %s.\n"), fctx.device);
3780 fctx.fs = global_fs;
3781 global_fs->priv_data = &fctx;
3785 if (ext2fs_has_feature_journal_needs_recovery(global_fs->super)) {
3787 printf(_("%s: recovering journal\n"), fctx.device);
3788 err = ext2fs_run_ext3_journal(&global_fs);
3790 printf(_("%s: %s.\n"), fctx.device,
3791 error_message(err));
3792 printf(_("Please run e2fsck -fy %s.\n"),
3796 ext2fs_clear_feature_journal_needs_recovery(global_fs->super);
3797 ext2fs_mark_super_dirty(global_fs);
3799 printf("%s", _("Journal needs recovery; running "
3800 "`e2fsck -E journal_only' is required.\n"));
3806 if (ext2fs_has_feature_journal(global_fs->super))
3807 printf(_("%s: Writing to the journal is not supported.\n"),
3809 err = ext2fs_read_inode_bitmap(global_fs);
3811 translate_error(global_fs, 0, err);
3814 err = ext2fs_read_block_bitmap(global_fs);
3816 translate_error(global_fs, 0, err);
3821 if (!(global_fs->super->s_state & EXT2_VALID_FS))
3822 printf("%s", _("Warning: Mounting unchecked fs, running e2fsck "
3823 "is recommended.\n"));
3824 if (global_fs->super->s_max_mnt_count > 0 &&
3825 global_fs->super->s_mnt_count >= global_fs->super->s_max_mnt_count)
3826 printf("%s", _("Warning: Maximal mount count reached, running "
3827 "e2fsck is recommended.\n"));
3828 if (global_fs->super->s_checkinterval > 0 &&
3829 (time_t) (global_fs->super->s_lastcheck +
3830 global_fs->super->s_checkinterval) <= time(0))
3831 printf("%s", _("Warning: Check time reached; running e2fsck "
3832 "is recommended.\n"));
3833 if (global_fs->super->s_last_orphan)
3835 _("Orphans detected; running e2fsck is recommended.\n"));
3837 if (global_fs->super->s_state & EXT2_ERROR_FS) {
3839 _("Errors detected; running e2fsck is required.\n"));
3843 /* Initialize generation counter */
3844 get_random_bytes(&fctx.next_generation, sizeof(unsigned int));
3846 /* Set up default fuse parameters */
3847 snprintf(extra_args, BUFSIZ, "-okernel_cache,subtype=ext4,use_ino,"
3848 "fsname=%s,attr_timeout=0" FUSE_PLATFORM_OPTS,
3850 if (fctx.no_default_opts == 0)
3851 fuse_opt_add_arg(&args, extra_args);
3853 if (fctx.fakeroot) {
3854 #ifdef HAVE_MOUNT_NODEV
3855 fuse_opt_add_arg(&args,"-onodev");
3857 #ifdef HAVE_MOUNT_NOSUID
3858 fuse_opt_add_arg(&args,"-onosuid");
3865 printf("fuse arguments:");
3866 for (i = 0; i < args.argc; i++)
3867 printf(" '%s'", args.argv[i]);
3871 pthread_mutex_init(&fctx.bfl, NULL);
3872 fuse_main(args.argc, args.argv, &fs_ops, &fctx);
3873 pthread_mutex_destroy(&fctx.bfl);
3878 err = ext2fs_close(global_fs);
3880 com_err(argv[0], err, "while closing fs");
3886 static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
3887 const char *file, int line)
3889 struct timespec now;
3891 struct fuse2fs *ff = fs->priv_data;
3894 /* Translate ext2 error to unix error code */
3895 if (err < EXT2_ET_BASE)
3896 goto no_translation;
3898 case EXT2_ET_NO_MEMORY:
3899 case EXT2_ET_TDB_ERR_OOM:
3902 case EXT2_ET_INVALID_ARGUMENT:
3903 case EXT2_ET_LLSEEK_FAILED:
3906 case EXT2_ET_NO_DIRECTORY:
3909 case EXT2_ET_FILE_NOT_FOUND:
3912 case EXT2_ET_DIR_NO_SPACE:
3915 case EXT2_ET_TOOSMALL:
3916 case EXT2_ET_BLOCK_ALLOC_FAIL:
3917 case EXT2_ET_INODE_ALLOC_FAIL:
3918 case EXT2_ET_EA_NO_SPACE:
3921 case EXT2_ET_SYMLINK_LOOP:
3924 case EXT2_ET_FILE_TOO_BIG:
3927 case EXT2_ET_TDB_ERR_EXISTS:
3928 case EXT2_ET_FILE_EXISTS:
3931 case EXT2_ET_MMP_FAILED:
3932 case EXT2_ET_MMP_FSCK_ON:
3935 case EXT2_ET_EA_KEY_NOT_FOUND:
3942 /* Sometimes fuse returns a garbage file handle pointer to us... */
3943 case EXT2_ET_MAGIC_EXT2_FILE:
3946 case EXT2_ET_UNIMPLEMENTED:
3960 fprintf(ff->err_fp, "FUSE2FS (%s): %s (inode #%d) at %s:%d.\n",
3961 fs->device_name ? fs->device_name : "???",
3962 error_message(err), ino, file, line);
3964 fprintf(ff->err_fp, "FUSE2FS (%s): %s at %s:%d.\n",
3965 fs->device_name ? fs->device_name : "???",
3966 error_message(err), file, line);
3969 /* Make a note in the error log */
3971 fs->super->s_last_error_time = now.tv_sec;
3972 fs->super->s_last_error_ino = ino;
3973 fs->super->s_last_error_line = line;
3974 fs->super->s_last_error_block = err; /* Yeah... */
3975 strncpy((char *)fs->super->s_last_error_func, file,
3976 sizeof(fs->super->s_last_error_func));
3977 if (fs->super->s_first_error_time == 0) {
3978 fs->super->s_first_error_time = now.tv_sec;
3979 fs->super->s_first_error_ino = ino;
3980 fs->super->s_first_error_line = line;
3981 fs->super->s_first_error_block = err;
3982 strncpy((char *)fs->super->s_first_error_func, file,
3983 sizeof(fs->super->s_first_error_func));
3986 fs->super->s_error_count++;
3987 ext2fs_mark_super_dirty(fs);
3989 if (ff->panic_on_error)