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 long offset;
330 unsigned int next_generation;
333 #define FUSE2FS_CHECK_MAGIC(fs, ptr, num) do {if ((ptr)->magic != (num)) \
334 return translate_error((fs), 0, EXT2_ET_MAGIC_EXT2_FILE); \
337 #define FUSE2FS_CHECK_CONTEXT(ptr) do {if ((ptr)->magic != FUSE2FS_MAGIC) \
338 return translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC); \
341 static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
342 const char *file, int line);
343 #define translate_error(fs, ino, err) __translate_error((fs), (err), (ino), \
355 #define EXT4_EPOCH_BITS 2
356 #define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1)
357 #define EXT4_NSEC_MASK (~0UL << EXT4_EPOCH_BITS)
360 * Extended fields will fit into an inode if the filesystem was formatted
361 * with large inodes (-I 256 or larger) and there are not currently any EAs
362 * consuming all of the available space. For new inodes we always reserve
363 * enough space for the kernel's known extended fields, but for inodes
364 * created with an old kernel this might not have been the case. None of
365 * the extended inode fields is critical for correct filesystem operation.
366 * This macro checks if a certain field fits in the inode. Note that
367 * inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize
369 #define EXT4_FITS_IN_INODE(ext4_inode, field) \
370 ((offsetof(typeof(*ext4_inode), field) + \
371 sizeof((ext4_inode)->field)) \
372 <= ((size_t) EXT2_GOOD_OLD_INODE_SIZE + \
373 (ext4_inode)->i_extra_isize)) \
375 static inline __u32 ext4_encode_extra_time(const struct timespec *time)
377 __u32 extra = sizeof(time->tv_sec) > 4 ?
378 ((time->tv_sec - (__s32)time->tv_sec) >> 32) &
380 return extra | (time->tv_nsec << EXT4_EPOCH_BITS);
383 static inline void ext4_decode_extra_time(struct timespec *time, __u32 extra)
385 if (sizeof(time->tv_sec) > 4 && (extra & EXT4_EPOCH_MASK)) {
386 __u64 extra_bits = extra & EXT4_EPOCH_MASK;
388 * Prior to kernel 3.14?, we had a broken decode function,
389 * wherein we effectively did this:
390 * if (extra_bits == 3)
393 time->tv_sec += extra_bits << 32;
395 time->tv_nsec = ((extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
398 #define EXT4_INODE_SET_XTIME(xtime, timespec, raw_inode) \
400 (raw_inode)->xtime = (timespec)->tv_sec; \
401 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
402 (raw_inode)->xtime ## _extra = \
403 ext4_encode_extra_time(timespec); \
406 #define EXT4_EINODE_SET_XTIME(xtime, timespec, raw_inode) \
408 if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
409 (raw_inode)->xtime = (timespec)->tv_sec; \
410 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
411 (raw_inode)->xtime ## _extra = \
412 ext4_encode_extra_time(timespec); \
415 #define EXT4_INODE_GET_XTIME(xtime, timespec, raw_inode) \
417 (timespec)->tv_sec = (signed)((raw_inode)->xtime); \
418 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
419 ext4_decode_extra_time((timespec), \
420 (raw_inode)->xtime ## _extra); \
422 (timespec)->tv_nsec = 0; \
425 #define EXT4_EINODE_GET_XTIME(xtime, timespec, raw_inode) \
427 if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
428 (timespec)->tv_sec = \
429 (signed)((raw_inode)->xtime); \
430 if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
431 ext4_decode_extra_time((timespec), \
432 raw_inode->xtime ## _extra); \
434 (timespec)->tv_nsec = 0; \
437 static void get_now(struct timespec *now)
439 #ifdef CLOCK_REALTIME
440 if (!clock_gettime(CLOCK_REALTIME, now))
444 now->tv_sec = time(NULL);
448 static void increment_version(struct ext2_inode_large *inode)
452 ver = inode->osd1.linux1.l_i_version;
453 if (EXT4_FITS_IN_INODE(inode, i_version_hi))
454 ver |= (__u64)inode->i_version_hi << 32;
456 inode->osd1.linux1.l_i_version = ver;
457 if (EXT4_FITS_IN_INODE(inode, i_version_hi))
458 inode->i_version_hi = ver >> 32;
461 static void init_times(struct ext2_inode_large *inode)
466 EXT4_INODE_SET_XTIME(i_atime, &now, inode);
467 EXT4_INODE_SET_XTIME(i_ctime, &now, inode);
468 EXT4_INODE_SET_XTIME(i_mtime, &now, inode);
469 EXT4_EINODE_SET_XTIME(i_crtime, &now, inode);
470 increment_version(inode);
473 static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
474 struct ext2_inode_large *pinode)
478 struct ext2_inode_large inode;
482 /* If user already has a inode buffer, just update that */
484 increment_version(pinode);
485 EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
489 /* Otherwise we have to read-modify-write the inode */
490 memset(&inode, 0, sizeof(inode));
491 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
494 return translate_error(fs, ino, err);
496 increment_version(&inode);
497 EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
499 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
502 return translate_error(fs, ino, err);
507 static int update_atime(ext2_filsys fs, ext2_ino_t ino)
510 struct ext2_inode_large inode, *pinode;
511 struct timespec atime, mtime, now;
513 if (!(fs->flags & EXT2_FLAG_RW))
515 memset(&inode, 0, sizeof(inode));
516 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
519 return translate_error(fs, ino, err);
522 EXT4_INODE_GET_XTIME(i_atime, &atime, pinode);
523 EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode);
526 * If atime is newer than mtime and atime hasn't been updated in thirty
527 * seconds, skip the atime update. Same idea as Linux "relatime".
529 if (atime.tv_sec >= mtime.tv_sec && atime.tv_sec >= now.tv_sec - 30)
531 EXT4_INODE_SET_XTIME(i_atime, &now, &inode);
533 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
536 return translate_error(fs, ino, err);
541 static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
542 struct ext2_inode_large *pinode)
545 struct ext2_inode_large inode;
550 EXT4_INODE_SET_XTIME(i_mtime, &now, pinode);
551 EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
552 increment_version(pinode);
556 memset(&inode, 0, sizeof(inode));
557 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
560 return translate_error(fs, ino, err);
563 EXT4_INODE_SET_XTIME(i_mtime, &now, &inode);
564 EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
565 increment_version(&inode);
567 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
570 return translate_error(fs, ino, err);
575 static int ext2_file_type(unsigned int mode)
577 if (LINUX_S_ISREG(mode))
578 return EXT2_FT_REG_FILE;
580 if (LINUX_S_ISDIR(mode))
583 if (LINUX_S_ISCHR(mode))
584 return EXT2_FT_CHRDEV;
586 if (LINUX_S_ISBLK(mode))
587 return EXT2_FT_BLKDEV;
589 if (LINUX_S_ISLNK(mode))
590 return EXT2_FT_SYMLINK;
592 if (LINUX_S_ISFIFO(mode))
595 if (LINUX_S_ISSOCK(mode))
601 static int fs_can_allocate(struct fuse2fs *ff, blk64_t num)
603 ext2_filsys fs = ff->fs;
606 dbg_printf("%s: Asking for %llu; alloc_all=%d total=%llu free=%llu "
607 "rsvd=%llu\n", __func__, num, ff->alloc_all_blocks,
608 ext2fs_blocks_count(fs->super),
609 ext2fs_free_blocks_count(fs->super),
610 ext2fs_r_blocks_count(fs->super));
611 if (num > ext2fs_blocks_count(fs->super))
614 if (ff->alloc_all_blocks)
618 * Different meaning for r_blocks -- libext2fs has bugs where the FS
619 * can get corrupted if it totally runs out of blocks. Avoid this
620 * by refusing to allocate any of the reserve blocks to anybody.
622 reserved = ext2fs_r_blocks_count(fs->super);
624 reserved = ext2fs_blocks_count(fs->super) / 10;
625 return ext2fs_free_blocks_count(fs->super) > reserved + num;
628 static int fs_writeable(ext2_filsys fs)
630 return (fs->flags & EXT2_FLAG_RW) && (fs->super->s_error_count == 0);
633 static int check_inum_access(ext2_filsys fs, ext2_ino_t ino, mode_t mask)
635 struct fuse_context *ctxt = fuse_get_context();
636 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
637 struct ext2_inode inode;
641 /* no writing to read-only or broken fs */
642 if ((mask & W_OK) && !fs_writeable(fs))
645 err = ext2fs_read_inode(fs, ino, &inode);
647 return translate_error(fs, ino, err);
648 perms = inode.i_mode & 0777;
650 dbg_printf("access ino=%d mask=e%s%s%s perms=0%o fuid=%d fgid=%d "
651 "uid=%d gid=%d\n", ino,
652 (mask & R_OK ? "r" : ""), (mask & W_OK ? "w" : ""),
653 (mask & X_OK ? "x" : ""), perms, inode_uid(inode),
654 inode_gid(inode), ctxt->uid, ctxt->gid);
656 /* existence check */
662 (inode.i_flags & EXT2_IMMUTABLE_FL))
665 /* Figure out what root's allowed to do */
666 if (ff->fakeroot || ctxt->uid == 0) {
667 /* Non-file access always ok */
668 if (!LINUX_S_ISREG(inode.i_mode))
671 /* R/W access to a file always ok */
675 /* X access to a file ok if a user/group/other can X */
679 /* Trying to execute a file that's not executable. BZZT! */
683 /* allow owner, if perms match */
684 if (inode_uid(inode) == ctxt->uid) {
685 if ((mask & (perms >> 6)) == mask)
690 /* allow group, if perms match */
691 if (inode_gid(inode) == ctxt->gid) {
692 if ((mask & (perms >> 3)) == mask)
697 /* otherwise check other */
698 if ((mask & perms) == mask)
703 static void op_destroy(void *p EXT2FS_ATTR((unused)))
705 struct fuse_context *ctxt = fuse_get_context();
706 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
710 if (ff->magic != FUSE2FS_MAGIC) {
711 translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC);
715 dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
716 if (fs->flags & EXT2_FLAG_RW) {
717 fs->super->s_state |= EXT2_VALID_FS;
718 if (fs->super->s_error_count)
719 fs->super->s_state |= EXT2_ERROR_FS;
720 ext2fs_mark_super_dirty(fs);
721 err = ext2fs_set_gdt_csum(fs);
723 translate_error(fs, 0, err);
725 err = ext2fs_flush2(fs, 0);
727 translate_error(fs, 0, err);
731 static void *op_init(struct fuse_conn_info *conn)
733 struct fuse_context *ctxt = fuse_get_context();
734 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
738 if (ff->magic != FUSE2FS_MAGIC) {
739 translate_error(global_fs, 0, EXT2_ET_BAD_MAGIC);
743 dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
744 #ifdef FUSE_CAP_IOCTL_DIR
745 conn->want |= FUSE_CAP_IOCTL_DIR;
747 if (fs->flags & EXT2_FLAG_RW) {
748 fs->super->s_mnt_count++;
749 fs->super->s_mtime = time(NULL);
750 fs->super->s_state &= ~EXT2_VALID_FS;
751 ext2fs_mark_super_dirty(fs);
752 err = ext2fs_flush2(fs, 0);
754 translate_error(fs, 0, err);
759 static int stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *statbuf)
761 struct ext2_inode_large inode;
767 memset(&inode, 0, sizeof(inode));
768 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
771 return translate_error(fs, ino, err);
773 memcpy(&fakedev, fs->super->s_uuid, sizeof(fakedev));
774 statbuf->st_dev = fakedev;
775 statbuf->st_ino = ino;
776 statbuf->st_mode = inode.i_mode;
777 statbuf->st_nlink = inode.i_links_count;
778 statbuf->st_uid = inode_uid(inode);
779 statbuf->st_gid = inode_gid(inode);
780 statbuf->st_size = EXT2_I_SIZE(&inode);
781 statbuf->st_blksize = fs->blocksize;
782 statbuf->st_blocks = ext2fs_get_stat_i_blocks(fs,
783 (struct ext2_inode *)&inode);
784 EXT4_INODE_GET_XTIME(i_atime, &tv, &inode);
785 statbuf->st_atime = tv.tv_sec;
786 EXT4_INODE_GET_XTIME(i_mtime, &tv, &inode);
787 statbuf->st_mtime = tv.tv_sec;
788 EXT4_INODE_GET_XTIME(i_ctime, &tv, &inode);
789 statbuf->st_ctime = tv.tv_sec;
790 if (LINUX_S_ISCHR(inode.i_mode) ||
791 LINUX_S_ISBLK(inode.i_mode)) {
792 if (inode.i_block[0])
793 statbuf->st_rdev = inode.i_block[0];
795 statbuf->st_rdev = inode.i_block[1];
801 static int op_getattr(const char *path, struct stat *statbuf)
803 struct fuse_context *ctxt = fuse_get_context();
804 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
810 FUSE2FS_CHECK_CONTEXT(ff);
812 dbg_printf("%s: path=%s\n", __func__, path);
813 pthread_mutex_lock(&ff->bfl);
814 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
816 ret = translate_error(fs, 0, err);
819 ret = stat_inode(fs, ino, statbuf);
821 pthread_mutex_unlock(&ff->bfl);
825 static int op_readlink(const char *path, char *buf, size_t len)
827 struct fuse_context *ctxt = fuse_get_context();
828 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
832 struct ext2_inode inode;
837 FUSE2FS_CHECK_CONTEXT(ff);
839 dbg_printf("%s: path=%s\n", __func__, path);
840 pthread_mutex_lock(&ff->bfl);
841 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
842 if (err || ino == 0) {
843 ret = translate_error(fs, 0, err);
847 err = ext2fs_read_inode(fs, ino, &inode);
849 ret = translate_error(fs, ino, err);
853 if (!LINUX_S_ISLNK(inode.i_mode)) {
859 if (inode.i_size < len)
861 if (ext2fs_is_fast_symlink(&inode))
862 memcpy(buf, (char *)inode.i_block, len);
864 /* big/inline symlink */
866 err = ext2fs_file_open(fs, ino, 0, &file);
868 ret = translate_error(fs, ino, err);
872 err = ext2fs_file_read(file, buf, len, &got);
873 if (err || got != len) {
874 ext2fs_file_close(file);
875 ret = translate_error(fs, ino, err);
880 err = ext2fs_file_close(file);
884 ret = translate_error(fs, ino, err);
890 if (fs_writeable(fs)) {
891 ret = update_atime(fs, ino);
897 pthread_mutex_unlock(&ff->bfl);
901 static int op_mknod(const char *path, mode_t mode, dev_t dev)
903 struct fuse_context *ctxt = fuse_get_context();
904 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
906 ext2_ino_t parent, child;
911 struct ext2_inode_large inode;
914 FUSE2FS_CHECK_CONTEXT(ff);
916 dbg_printf("%s: path=%s mode=0%o dev=0x%x\n", __func__, path, mode,
918 temp_path = strdup(path);
923 node_name = strrchr(temp_path, '/');
932 pthread_mutex_lock(&ff->bfl);
933 if (!fs_can_allocate(ff, 2)) {
938 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
941 ret = translate_error(fs, 0, err);
945 ret = check_inum_access(fs, parent, W_OK);
951 if (LINUX_S_ISCHR(mode))
952 filetype = EXT2_FT_CHRDEV;
953 else if (LINUX_S_ISBLK(mode))
954 filetype = EXT2_FT_BLKDEV;
955 else if (LINUX_S_ISFIFO(mode))
956 filetype = EXT2_FT_FIFO;
957 else if (LINUX_S_ISSOCK(mode))
958 filetype = EXT2_FT_SOCK;
964 err = ext2fs_new_inode(fs, parent, mode, 0, &child);
966 ret = translate_error(fs, 0, err);
970 dbg_printf("%s: create ino=%d/name=%s in dir=%d\n", __func__, child,
972 err = ext2fs_link(fs, parent, node_name, child, filetype);
973 if (err == EXT2_ET_DIR_NO_SPACE) {
974 err = ext2fs_expand_dir(fs, parent);
976 ret = translate_error(fs, parent, err);
980 err = ext2fs_link(fs, parent, node_name, child,
984 ret = translate_error(fs, parent, err);
988 ret = update_mtime(fs, parent, NULL);
992 memset(&inode, 0, sizeof(inode));
996 inode.i_block[1] = dev;
998 inode.i_block[0] = dev;
999 inode.i_links_count = 1;
1000 inode.i_extra_isize = sizeof(struct ext2_inode_large) -
1001 EXT2_GOOD_OLD_INODE_SIZE;
1002 inode.i_uid = ctxt->uid;
1003 ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
1004 inode.i_gid = ctxt->gid;
1005 ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
1007 err = ext2fs_write_new_inode(fs, child, (struct ext2_inode *)&inode);
1009 ret = translate_error(fs, child, err);
1013 inode.i_generation = ff->next_generation++;
1015 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1018 ret = translate_error(fs, child, err);
1022 ext2fs_inode_alloc_stats2(fs, child, 1, 0);
1025 pthread_mutex_unlock(&ff->bfl);
1031 static int op_mkdir(const char *path, mode_t mode)
1033 struct fuse_context *ctxt = fuse_get_context();
1034 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1036 ext2_ino_t parent, child;
1040 struct ext2_inode_large inode;
1046 FUSE2FS_CHECK_CONTEXT(ff);
1048 dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode);
1049 temp_path = strdup(path);
1054 node_name = strrchr(temp_path, '/');
1063 pthread_mutex_lock(&ff->bfl);
1064 if (!fs_can_allocate(ff, 1)) {
1069 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1072 ret = translate_error(fs, 0, err);
1076 ret = check_inum_access(fs, parent, W_OK);
1080 /* Is the parent dir sgid? */
1081 err = ext2fs_read_inode_full(fs, parent, (struct ext2_inode *)&inode,
1084 ret = translate_error(fs, parent, err);
1087 parent_sgid = inode.i_mode & S_ISGID;
1091 err = ext2fs_mkdir(fs, parent, 0, node_name);
1092 if (err == EXT2_ET_DIR_NO_SPACE) {
1093 err = ext2fs_expand_dir(fs, parent);
1095 ret = translate_error(fs, parent, err);
1099 err = ext2fs_mkdir(fs, parent, 0, node_name);
1102 ret = translate_error(fs, parent, err);
1106 ret = update_mtime(fs, parent, NULL);
1110 /* Still have to update the uid/gid of the dir */
1111 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1114 ret = translate_error(fs, 0, err);
1117 dbg_printf("%s: created ino=%d/path=%s in dir=%d\n", __func__, child,
1120 memset(&inode, 0, sizeof(inode));
1121 err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode,
1124 ret = translate_error(fs, child, err);
1128 inode.i_uid = ctxt->uid;
1129 ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
1130 inode.i_gid = ctxt->gid;
1131 ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
1132 inode.i_mode = LINUX_S_IFDIR | (mode & ~(S_ISUID | fs->umask)) |
1134 inode.i_generation = ff->next_generation++;
1136 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1139 ret = translate_error(fs, child, err);
1143 /* Rewrite the directory block checksum, having set i_generation */
1144 if ((inode.i_flags & EXT4_INLINE_DATA_FL) ||
1145 !ext2fs_has_feature_metadata_csum(fs->super))
1147 err = ext2fs_new_dir_block(fs, child, parent, &block);
1149 ret = translate_error(fs, child, err);
1152 err = ext2fs_bmap2(fs, child, (struct ext2_inode *)&inode, NULL, 0, 0,
1155 ret = translate_error(fs, child, err);
1158 err = ext2fs_write_dir_block4(fs, blk, block, 0, child);
1160 ret = translate_error(fs, child, err);
1165 ext2fs_free_mem(&block);
1167 pthread_mutex_unlock(&ff->bfl);
1173 static int unlink_file_by_name(ext2_filsys fs, const char *path)
1177 char *filename = strdup(path);
1181 base_name = strrchr(filename, '/');
1183 *base_name++ = '\0';
1184 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, filename,
1188 return translate_error(fs, 0, err);
1191 dir = EXT2_ROOT_INO;
1192 base_name = filename;
1195 ret = check_inum_access(fs, dir, W_OK);
1201 dbg_printf("%s: unlinking name=%s from dir=%d\n", __func__,
1203 err = ext2fs_unlink(fs, dir, base_name, 0, 0);
1206 return translate_error(fs, dir, err);
1208 return update_mtime(fs, dir, NULL);
1211 static int remove_inode(struct fuse2fs *ff, ext2_ino_t ino)
1213 ext2_filsys fs = ff->fs;
1215 struct ext2_inode_large inode;
1218 memset(&inode, 0, sizeof(inode));
1219 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1222 ret = translate_error(fs, ino, err);
1225 dbg_printf("%s: put ino=%d links=%d\n", __func__, ino,
1226 inode.i_links_count);
1228 switch (inode.i_links_count) {
1230 return 0; /* XXX: already done? */
1232 inode.i_links_count--;
1233 inode.i_dtime = fs->now ? fs->now : time(0);
1236 inode.i_links_count--;
1239 ret = update_ctime(fs, ino, &inode);
1243 if (inode.i_links_count)
1246 /* Nobody holds this file; free its blocks! */
1247 err = ext2fs_free_ext_attr(fs, ino, &inode);
1251 if (ext2fs_inode_has_valid_blocks2(fs, (struct ext2_inode *)&inode)) {
1252 err = ext2fs_punch(fs, ino, (struct ext2_inode *)&inode, NULL,
1255 ret = translate_error(fs, ino, err);
1260 ext2fs_inode_alloc_stats2(fs, ino, -1,
1261 LINUX_S_ISDIR(inode.i_mode));
1264 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1267 ret = translate_error(fs, ino, err);
1274 static int __op_unlink(struct fuse2fs *ff, const char *path)
1276 ext2_filsys fs = ff->fs;
1281 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1283 ret = translate_error(fs, 0, err);
1287 ret = unlink_file_by_name(fs, path);
1291 ret = remove_inode(ff, ino);
1298 static int op_unlink(const char *path)
1300 struct fuse_context *ctxt = fuse_get_context();
1301 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1304 FUSE2FS_CHECK_CONTEXT(ff);
1305 pthread_mutex_lock(&ff->bfl);
1306 ret = __op_unlink(ff, path);
1307 pthread_mutex_unlock(&ff->bfl);
1316 static int rmdir_proc(ext2_ino_t dir EXT2FS_ATTR((unused)),
1317 int entry EXT2FS_ATTR((unused)),
1318 struct ext2_dir_entry *dirent,
1319 int offset EXT2FS_ATTR((unused)),
1320 int blocksize EXT2FS_ATTR((unused)),
1321 char *buf EXT2FS_ATTR((unused)),
1324 struct rd_struct *rds = (struct rd_struct *) private;
1326 if (dirent->inode == 0)
1328 if (((dirent->name_len & 0xFF) == 1) && (dirent->name[0] == '.'))
1330 if (((dirent->name_len & 0xFF) == 2) && (dirent->name[0] == '.') &&
1331 (dirent->name[1] == '.')) {
1332 rds->parent = dirent->inode;
1339 static int __op_rmdir(struct fuse2fs *ff, const char *path)
1341 ext2_filsys fs = ff->fs;
1344 struct ext2_inode_large inode;
1345 struct rd_struct rds;
1348 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &child);
1350 ret = translate_error(fs, 0, err);
1353 dbg_printf("%s: rmdir path=%s ino=%d\n", __func__, path, child);
1358 err = ext2fs_dir_iterate2(fs, child, 0, 0, rmdir_proc, &rds);
1360 ret = translate_error(fs, child, err);
1364 if (rds.empty == 0) {
1369 ret = unlink_file_by_name(fs, path);
1372 /* Directories have to be "removed" twice. */
1373 ret = remove_inode(ff, child);
1376 ret = remove_inode(ff, child);
1381 dbg_printf("%s: decr dir=%d link count\n", __func__,
1383 err = ext2fs_read_inode_full(fs, rds.parent,
1384 (struct ext2_inode *)&inode,
1387 ret = translate_error(fs, rds.parent, err);
1390 if (inode.i_links_count > 1)
1391 inode.i_links_count--;
1392 ret = update_mtime(fs, rds.parent, &inode);
1395 err = ext2fs_write_inode_full(fs, rds.parent,
1396 (struct ext2_inode *)&inode,
1399 ret = translate_error(fs, rds.parent, err);
1408 static int op_rmdir(const char *path)
1410 struct fuse_context *ctxt = fuse_get_context();
1411 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1414 FUSE2FS_CHECK_CONTEXT(ff);
1415 pthread_mutex_lock(&ff->bfl);
1416 ret = __op_rmdir(ff, path);
1417 pthread_mutex_unlock(&ff->bfl);
1421 static int op_symlink(const char *src, const char *dest)
1423 struct fuse_context *ctxt = fuse_get_context();
1424 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1426 ext2_ino_t parent, child;
1430 struct ext2_inode_large inode;
1433 FUSE2FS_CHECK_CONTEXT(ff);
1435 dbg_printf("%s: symlink %s to %s\n", __func__, src, dest);
1436 temp_path = strdup(dest);
1441 node_name = strrchr(temp_path, '/');
1450 pthread_mutex_lock(&ff->bfl);
1451 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1455 ret = translate_error(fs, 0, err);
1459 ret = check_inum_access(fs, parent, W_OK);
1464 /* Create symlink */
1465 err = ext2fs_symlink(fs, parent, 0, node_name, src);
1466 if (err == EXT2_ET_DIR_NO_SPACE) {
1467 err = ext2fs_expand_dir(fs, parent);
1469 ret = translate_error(fs, parent, err);
1473 err = ext2fs_symlink(fs, parent, 0, node_name, src);
1476 ret = translate_error(fs, parent, err);
1480 /* Update parent dir's mtime */
1481 ret = update_mtime(fs, parent, NULL);
1485 /* Still have to update the uid/gid of the symlink */
1486 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1489 ret = translate_error(fs, 0, err);
1492 dbg_printf("%s: symlinking ino=%d/name=%s to dir=%d\n", __func__,
1493 child, node_name, parent);
1495 memset(&inode, 0, sizeof(inode));
1496 err = ext2fs_read_inode_full(fs, child, (struct ext2_inode *)&inode,
1499 ret = translate_error(fs, child, err);
1503 inode.i_uid = ctxt->uid;
1504 ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
1505 inode.i_gid = ctxt->gid;
1506 ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
1507 inode.i_generation = ff->next_generation++;
1509 err = ext2fs_write_inode_full(fs, child, (struct ext2_inode *)&inode,
1512 ret = translate_error(fs, child, err);
1516 pthread_mutex_unlock(&ff->bfl);
1522 struct update_dotdot {
1523 ext2_ino_t new_dotdot;
1526 static int update_dotdot_helper(ext2_ino_t dir EXT2FS_ATTR((unused)),
1527 int entry EXT2FS_ATTR((unused)),
1528 struct ext2_dir_entry *dirent,
1529 int offset EXT2FS_ATTR((unused)),
1530 int blocksize EXT2FS_ATTR((unused)),
1531 char *buf EXT2FS_ATTR((unused)),
1534 struct update_dotdot *ud = priv_data;
1536 if (ext2fs_dirent_name_len(dirent) == 2 &&
1537 dirent->name[0] == '.' && dirent->name[1] == '.') {
1538 dirent->inode = ud->new_dotdot;
1539 return DIRENT_CHANGED | DIRENT_ABORT;
1545 static int op_rename(const char *from, const char *to)
1547 struct fuse_context *ctxt = fuse_get_context();
1548 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1551 ext2_ino_t from_ino, to_ino, to_dir_ino, from_dir_ino;
1552 char *temp_to = NULL, *temp_from = NULL;
1554 struct ext2_inode inode;
1555 struct update_dotdot ud;
1558 FUSE2FS_CHECK_CONTEXT(ff);
1560 dbg_printf("%s: renaming %s to %s\n", __func__, from, to);
1561 pthread_mutex_lock(&ff->bfl);
1562 if (!fs_can_allocate(ff, 5)) {
1567 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, from, &from_ino);
1568 if (err || from_ino == 0) {
1569 ret = translate_error(fs, 0, err);
1573 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, to, &to_ino);
1574 if (err && err != EXT2_ET_FILE_NOT_FOUND) {
1575 ret = translate_error(fs, 0, err);
1579 if (err == EXT2_ET_FILE_NOT_FOUND)
1582 /* Already the same file? */
1583 if (to_ino != 0 && to_ino == from_ino) {
1588 temp_to = strdup(to);
1594 temp_from = strdup(from);
1600 /* Find parent dir of the source and check write access */
1601 cp = strrchr(temp_from, '/');
1609 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_from,
1613 ret = translate_error(fs, 0, err);
1616 if (from_dir_ino == 0) {
1621 ret = check_inum_access(fs, from_dir_ino, W_OK);
1625 /* Find parent dir of the destination and check write access */
1626 cp = strrchr(temp_to, '/');
1634 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_to,
1638 ret = translate_error(fs, 0, err);
1641 if (to_dir_ino == 0) {
1646 ret = check_inum_access(fs, to_dir_ino, W_OK);
1650 /* If the target exists, unlink it first */
1652 err = ext2fs_read_inode(fs, to_ino, &inode);
1654 ret = translate_error(fs, to_ino, err);
1658 dbg_printf("%s: unlinking %s ino=%d\n", __func__,
1659 LINUX_S_ISDIR(inode.i_mode) ? "dir" : "file",
1661 if (LINUX_S_ISDIR(inode.i_mode))
1662 ret = __op_rmdir(ff, to);
1664 ret = __op_unlink(ff, to);
1669 /* Get ready to do the move */
1670 err = ext2fs_read_inode(fs, from_ino, &inode);
1672 ret = translate_error(fs, from_ino, err);
1676 /* Link in the new file */
1677 dbg_printf("%s: linking ino=%d/path=%s to dir=%d\n", __func__,
1678 from_ino, cp + 1, to_dir_ino);
1679 err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino,
1680 ext2_file_type(inode.i_mode));
1681 if (err == EXT2_ET_DIR_NO_SPACE) {
1682 err = ext2fs_expand_dir(fs, to_dir_ino);
1684 ret = translate_error(fs, to_dir_ino, err);
1688 err = ext2fs_link(fs, to_dir_ino, cp + 1, from_ino,
1689 ext2_file_type(inode.i_mode));
1692 ret = translate_error(fs, to_dir_ino, err);
1696 /* Update '..' pointer if dir */
1697 err = ext2fs_read_inode(fs, from_ino, &inode);
1699 ret = translate_error(fs, from_ino, err);
1703 if (LINUX_S_ISDIR(inode.i_mode)) {
1704 ud.new_dotdot = to_dir_ino;
1705 dbg_printf("%s: updating .. entry for dir=%d\n", __func__,
1707 err = ext2fs_dir_iterate2(fs, from_ino, 0, NULL,
1708 update_dotdot_helper, &ud);
1710 ret = translate_error(fs, from_ino, err);
1714 /* Decrease from_dir_ino's links_count */
1715 dbg_printf("%s: moving linkcount from dir=%d to dir=%d\n",
1716 __func__, from_dir_ino, to_dir_ino);
1717 err = ext2fs_read_inode(fs, from_dir_ino, &inode);
1719 ret = translate_error(fs, from_dir_ino, err);
1722 inode.i_links_count--;
1723 err = ext2fs_write_inode(fs, from_dir_ino, &inode);
1725 ret = translate_error(fs, from_dir_ino, err);
1729 /* Increase to_dir_ino's links_count */
1730 err = ext2fs_read_inode(fs, to_dir_ino, &inode);
1732 ret = translate_error(fs, to_dir_ino, err);
1735 inode.i_links_count++;
1736 err = ext2fs_write_inode(fs, to_dir_ino, &inode);
1738 ret = translate_error(fs, to_dir_ino, err);
1743 /* Update timestamps */
1744 ret = update_ctime(fs, from_ino, NULL);
1748 ret = update_mtime(fs, to_dir_ino, NULL);
1752 /* Remove the old file */
1753 ret = unlink_file_by_name(fs, from);
1757 /* Flush the whole mess out */
1758 err = ext2fs_flush2(fs, 0);
1760 ret = translate_error(fs, 0, err);
1766 pthread_mutex_unlock(&ff->bfl);
1770 static int op_link(const char *src, const char *dest)
1772 struct fuse_context *ctxt = fuse_get_context();
1773 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1778 ext2_ino_t parent, ino;
1779 struct ext2_inode_large inode;
1782 FUSE2FS_CHECK_CONTEXT(ff);
1784 dbg_printf("%s: src=%s dest=%s\n", __func__, src, dest);
1785 temp_path = strdup(dest);
1790 node_name = strrchr(temp_path, '/');
1799 pthread_mutex_lock(&ff->bfl);
1800 if (!fs_can_allocate(ff, 2)) {
1805 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
1813 ret = check_inum_access(fs, parent, W_OK);
1818 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, src, &ino);
1819 if (err || ino == 0) {
1820 ret = translate_error(fs, 0, err);
1824 memset(&inode, 0, sizeof(inode));
1825 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1828 ret = translate_error(fs, ino, err);
1832 inode.i_links_count++;
1833 ret = update_ctime(fs, ino, &inode);
1837 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1840 ret = translate_error(fs, ino, err);
1844 dbg_printf("%s: linking ino=%d/name=%s to dir=%d\n", __func__, ino,
1846 err = ext2fs_link(fs, parent, node_name, ino,
1847 ext2_file_type(inode.i_mode));
1848 if (err == EXT2_ET_DIR_NO_SPACE) {
1849 err = ext2fs_expand_dir(fs, parent);
1851 ret = translate_error(fs, parent, err);
1855 err = ext2fs_link(fs, parent, node_name, ino,
1856 ext2_file_type(inode.i_mode));
1859 ret = translate_error(fs, parent, err);
1863 ret = update_mtime(fs, parent, NULL);
1868 pthread_mutex_unlock(&ff->bfl);
1874 static int op_chmod(const char *path, mode_t mode)
1876 struct fuse_context *ctxt = fuse_get_context();
1877 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1881 struct ext2_inode_large inode;
1884 FUSE2FS_CHECK_CONTEXT(ff);
1886 pthread_mutex_lock(&ff->bfl);
1887 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1889 ret = translate_error(fs, 0, err);
1892 dbg_printf("%s: path=%s mode=0%o ino=%d\n", __func__, path, mode, ino);
1894 memset(&inode, 0, sizeof(inode));
1895 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1898 ret = translate_error(fs, ino, err);
1902 if (!ff->fakeroot && ctxt->uid != 0 && ctxt->uid != inode_uid(inode)) {
1908 * XXX: We should really check that the inode gid is not in /any/
1909 * of the user's groups, but FUSE only tells us about the primary
1912 if (!ff->fakeroot && ctxt->uid != 0 && ctxt->gid != inode_gid(inode))
1915 inode.i_mode &= ~0xFFF;
1916 inode.i_mode |= mode & 0xFFF;
1917 ret = update_ctime(fs, ino, &inode);
1921 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1924 ret = translate_error(fs, ino, err);
1929 pthread_mutex_unlock(&ff->bfl);
1933 static int op_chown(const char *path, uid_t owner, gid_t group)
1935 struct fuse_context *ctxt = fuse_get_context();
1936 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
1940 struct ext2_inode_large inode;
1943 FUSE2FS_CHECK_CONTEXT(ff);
1945 pthread_mutex_lock(&ff->bfl);
1946 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
1948 ret = translate_error(fs, 0, err);
1951 dbg_printf("%s: path=%s owner=%d group=%d ino=%d\n", __func__,
1952 path, owner, group, ino);
1954 memset(&inode, 0, sizeof(inode));
1955 err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
1958 ret = translate_error(fs, ino, err);
1962 /* FUSE seems to feed us ~0 to mean "don't change" */
1963 if (owner != (uid_t) ~0) {
1964 /* Only root gets to change UID. */
1965 if (!ff->fakeroot && ctxt->uid != 0 &&
1966 !(inode_uid(inode) == ctxt->uid && owner == ctxt->uid)) {
1970 inode.i_uid = owner;
1971 ext2fs_set_i_uid_high(inode, owner >> 16);
1974 if (group != (gid_t) ~0) {
1975 /* Only root or the owner get to change GID. */
1976 if (!ff->fakeroot && ctxt->uid != 0 &&
1977 inode_uid(inode) != ctxt->uid) {
1982 /* XXX: We /should/ check group membership but FUSE */
1983 inode.i_gid = group;
1984 ext2fs_set_i_gid_high(inode, group >> 16);
1987 ret = update_ctime(fs, ino, &inode);
1991 err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
1994 ret = translate_error(fs, ino, err);
1999 pthread_mutex_unlock(&ff->bfl);
2003 static int op_truncate(const char *path, off_t len)
2005 struct fuse_context *ctxt = fuse_get_context();
2006 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2013 FUSE2FS_CHECK_CONTEXT(ff);
2015 pthread_mutex_lock(&ff->bfl);
2016 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2017 if (err || ino == 0) {
2018 ret = translate_error(fs, 0, err);
2021 dbg_printf("%s: ino=%d len=%jd\n", __func__, ino, len);
2023 ret = check_inum_access(fs, ino, W_OK);
2027 err = ext2fs_file_open(fs, ino, EXT2_FILE_WRITE, &file);
2029 ret = translate_error(fs, ino, err);
2033 err = ext2fs_file_set_size2(file, len);
2035 ret = translate_error(fs, ino, err);
2040 err = ext2fs_file_close(file);
2044 ret = translate_error(fs, ino, err);
2048 ret = update_mtime(fs, ino, NULL);
2051 pthread_mutex_unlock(&ff->bfl);
2056 static void detect_linux_executable_open(int kernel_flags, int *access_check,
2057 int *e2fs_open_flags)
2060 * On Linux, execve will bleed __FMODE_EXEC into the file mode flags,
2061 * and FUSE is more than happy to let that slip through.
2063 if (kernel_flags & 0x20) {
2064 *access_check = X_OK;
2065 *e2fs_open_flags &= ~EXT2_FILE_WRITE;
2069 static void detect_linux_executable_open(int kernel_flags, int *access_check,
2070 int *e2fs_open_flags)
2074 #endif /* __linux__ */
2076 static int __op_open(struct fuse2fs *ff, const char *path,
2077 struct fuse_file_info *fp)
2079 ext2_filsys fs = ff->fs;
2081 struct fuse2fs_file_handle *file;
2082 int check = 0, ret = 0;
2084 dbg_printf("%s: path=%s\n", __func__, path);
2085 err = ext2fs_get_mem(sizeof(*file), &file);
2087 return translate_error(fs, 0, err);
2088 file->magic = FUSE2FS_FILE_MAGIC;
2090 file->open_flags = 0;
2091 switch (fp->flags & O_ACCMODE) {
2097 file->open_flags |= EXT2_FILE_WRITE;
2100 check = R_OK | W_OK;
2101 file->open_flags |= EXT2_FILE_WRITE;
2105 detect_linux_executable_open(fp->flags, &check, &file->open_flags);
2107 if (fp->flags & O_CREAT)
2108 file->open_flags |= EXT2_FILE_CREATE;
2110 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &file->ino);
2111 if (err || file->ino == 0) {
2112 ret = translate_error(fs, 0, err);
2115 dbg_printf("%s: ino=%d\n", __func__, file->ino);
2117 ret = check_inum_access(fs, file->ino, check);
2120 * In a regular (Linux) fs driver, the kernel will open
2121 * binaries for reading if the user has --x privileges (i.e.
2122 * execute without read). Since the kernel doesn't have any
2123 * way to tell us if it's opening a file via execve, we'll
2124 * just assume that allowing access is ok if asking for ro mode
2125 * fails but asking for x mode succeeds. Of course we can
2126 * also employ undocumented hacks (see above).
2128 if (check == R_OK) {
2129 ret = check_inum_access(fs, file->ino, X_OK);
2135 fp->fh = (uintptr_t)file;
2139 ext2fs_free_mem(&file);
2143 static int op_open(const char *path, struct fuse_file_info *fp)
2145 struct fuse_context *ctxt = fuse_get_context();
2146 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2149 FUSE2FS_CHECK_CONTEXT(ff);
2150 pthread_mutex_lock(&ff->bfl);
2151 ret = __op_open(ff, path, fp);
2152 pthread_mutex_unlock(&ff->bfl);
2156 static int op_read(const char *path EXT2FS_ATTR((unused)), char *buf,
2157 size_t len, off_t offset,
2158 struct fuse_file_info *fp)
2160 struct fuse_context *ctxt = fuse_get_context();
2161 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2162 struct fuse2fs_file_handle *fh =
2163 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2167 unsigned int got = 0;
2170 FUSE2FS_CHECK_CONTEXT(ff);
2172 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2173 dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset,
2175 pthread_mutex_lock(&ff->bfl);
2176 err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
2178 ret = translate_error(fs, fh->ino, err);
2182 err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL);
2184 ret = translate_error(fs, fh->ino, err);
2188 err = ext2fs_file_read(efp, buf, len, &got);
2190 ret = translate_error(fs, fh->ino, err);
2195 err = ext2fs_file_close(efp);
2199 ret = translate_error(fs, fh->ino, err);
2203 if (fs_writeable(fs)) {
2204 ret = update_atime(fs, fh->ino);
2209 pthread_mutex_unlock(&ff->bfl);
2210 return got ? (int) got : ret;
2213 static int op_write(const char *path EXT2FS_ATTR((unused)),
2214 const char *buf, size_t len, off_t offset,
2215 struct fuse_file_info *fp)
2217 struct fuse_context *ctxt = fuse_get_context();
2218 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2219 struct fuse2fs_file_handle *fh =
2220 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2224 unsigned int got = 0;
2227 FUSE2FS_CHECK_CONTEXT(ff);
2229 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2230 dbg_printf("%s: ino=%d off=%jd len=%jd\n", __func__, fh->ino, offset,
2232 pthread_mutex_lock(&ff->bfl);
2233 if (!fs_writeable(fs)) {
2238 if (!fs_can_allocate(ff, len / fs->blocksize)) {
2243 err = ext2fs_file_open(fs, fh->ino, fh->open_flags, &efp);
2245 ret = translate_error(fs, fh->ino, err);
2249 err = ext2fs_file_llseek(efp, offset, SEEK_SET, NULL);
2251 ret = translate_error(fs, fh->ino, err);
2255 err = ext2fs_file_write(efp, buf, len, &got);
2257 ret = translate_error(fs, fh->ino, err);
2261 err = ext2fs_file_flush(efp);
2264 ret = translate_error(fs, fh->ino, err);
2269 err = ext2fs_file_close(efp);
2273 ret = translate_error(fs, fh->ino, err);
2277 ret = update_mtime(fs, fh->ino, NULL);
2282 pthread_mutex_unlock(&ff->bfl);
2283 return got ? (int) got : ret;
2286 static int op_release(const char *path EXT2FS_ATTR((unused)),
2287 struct fuse_file_info *fp)
2289 struct fuse_context *ctxt = fuse_get_context();
2290 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2291 struct fuse2fs_file_handle *fh =
2292 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2297 FUSE2FS_CHECK_CONTEXT(ff);
2299 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2300 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2301 pthread_mutex_lock(&ff->bfl);
2302 if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
2303 err = ext2fs_flush2(fs, EXT2_FLAG_FLUSH_NO_SYNC);
2305 ret = translate_error(fs, fh->ino, err);
2308 pthread_mutex_unlock(&ff->bfl);
2310 ext2fs_free_mem(&fh);
2315 static int op_fsync(const char *path EXT2FS_ATTR((unused)),
2316 int datasync EXT2FS_ATTR((unused)),
2317 struct fuse_file_info *fp)
2319 struct fuse_context *ctxt = fuse_get_context();
2320 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2321 struct fuse2fs_file_handle *fh =
2322 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2327 FUSE2FS_CHECK_CONTEXT(ff);
2329 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2330 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2331 /* For now, flush everything, even if it's slow */
2332 pthread_mutex_lock(&ff->bfl);
2333 if (fs_writeable(fs) && fh->open_flags & EXT2_FILE_WRITE) {
2334 err = ext2fs_flush2(fs, 0);
2336 ret = translate_error(fs, fh->ino, err);
2338 pthread_mutex_unlock(&ff->bfl);
2343 static int op_statfs(const char *path EXT2FS_ATTR((unused)),
2344 struct statvfs *buf)
2346 struct fuse_context *ctxt = fuse_get_context();
2347 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2350 blk64_t overhead, reserved, free;
2352 FUSE2FS_CHECK_CONTEXT(ff);
2354 dbg_printf("%s: path=%s\n", __func__, path);
2355 buf->f_bsize = fs->blocksize;
2361 overhead = fs->desc_blocks +
2362 (blk64_t)fs->group_desc_count *
2363 (fs->inode_blocks_per_group + 2);
2364 reserved = ext2fs_r_blocks_count(fs->super);
2366 reserved = ext2fs_blocks_count(fs->super) / 10;
2367 free = ext2fs_free_blocks_count(fs->super);
2369 buf->f_blocks = ext2fs_blocks_count(fs->super) - overhead;
2370 buf->f_bfree = free;
2371 if (free < reserved)
2374 buf->f_bavail = free - reserved;
2375 buf->f_files = fs->super->s_inodes_count;
2376 buf->f_ffree = fs->super->s_free_inodes_count;
2377 buf->f_favail = fs->super->s_free_inodes_count;
2378 f = (uint64_t *)fs->super->s_uuid;
2384 if (fs->flags & EXT2_FLAG_RW)
2385 buf->f_flag |= ST_RDONLY;
2386 buf->f_namemax = EXT2_NAME_LEN;
2391 typedef errcode_t (*xattr_xlate_get)(void **cooked_buf, size_t *cooked_sz,
2392 const void *raw_buf, size_t raw_sz);
2393 typedef errcode_t (*xattr_xlate_set)(const void *cooked_buf, size_t cooked_sz,
2394 const void **raw_buf, size_t *raw_sz);
2395 struct xattr_translate {
2397 xattr_xlate_get get;
2398 xattr_xlate_set set;
2401 #define XATTR_TRANSLATOR(p, g, s) \
2403 .get = (xattr_xlate_get)(g), \
2404 .set = (xattr_xlate_set)(s)}
2406 static struct xattr_translate xattr_translators[] = {
2407 #ifdef TRANSLATE_LINUX_ACLS
2408 XATTR_TRANSLATOR(ACL_EA_ACCESS, ext4_to_fuse_acl, fuse_to_ext4_acl),
2409 XATTR_TRANSLATOR(ACL_EA_DEFAULT, ext4_to_fuse_acl, fuse_to_ext4_acl),
2411 XATTR_TRANSLATOR(NULL, NULL, NULL),
2413 #undef XATTR_TRANSLATOR
2415 static int op_getxattr(const char *path, const char *key, char *value,
2418 struct fuse_context *ctxt = fuse_get_context();
2419 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2421 struct ext2_xattr_handle *h;
2422 struct xattr_translate *xt;
2429 FUSE2FS_CHECK_CONTEXT(ff);
2431 pthread_mutex_lock(&ff->bfl);
2432 if (!ext2fs_has_feature_xattr(fs->super)) {
2437 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2438 if (err || ino == 0) {
2439 ret = translate_error(fs, 0, err);
2442 dbg_printf("%s: ino=%d\n", __func__, ino);
2444 ret = check_inum_access(fs, ino, R_OK);
2448 err = ext2fs_xattrs_open(fs, ino, &h);
2450 ret = translate_error(fs, ino, err);
2454 err = ext2fs_xattrs_read(h);
2456 ret = translate_error(fs, ino, err);
2460 err = ext2fs_xattr_get(h, key, &ptr, &plen);
2462 ret = translate_error(fs, ino, err);
2466 for (xt = xattr_translators; xt->prefix != NULL; xt++) {
2467 if (strncmp(key, xt->prefix, strlen(xt->prefix)) == 0) {
2468 err = xt->get(&cptr, &clen, ptr, plen);
2471 ext2fs_free_mem(&ptr);
2479 } else if (len < plen) {
2482 memcpy(value, ptr, plen);
2487 ext2fs_free_mem(&ptr);
2489 err = ext2fs_xattrs_close(&h);
2491 ret = translate_error(fs, ino, err);
2493 pthread_mutex_unlock(&ff->bfl);
2498 static int count_buffer_space(char *name, char *value EXT2FS_ATTR((unused)),
2499 size_t value_len EXT2FS_ATTR((unused)),
2502 unsigned int *x = data;
2504 *x = *x + strlen(name) + 1;
2508 static int copy_names(char *name, char *value EXT2FS_ATTR((unused)),
2509 size_t value_len EXT2FS_ATTR((unused)), void *data)
2512 size_t name_len = strlen(name);
2514 memcpy(*b, name, name_len + 1);
2515 *b = *b + name_len + 1;
2520 static int op_listxattr(const char *path, char *names, size_t len)
2522 struct fuse_context *ctxt = fuse_get_context();
2523 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2525 struct ext2_xattr_handle *h;
2531 FUSE2FS_CHECK_CONTEXT(ff);
2533 pthread_mutex_lock(&ff->bfl);
2534 if (!ext2fs_has_feature_xattr(fs->super)) {
2539 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2540 if (err || ino == 0) {
2541 ret = translate_error(fs, ino, err);
2544 dbg_printf("%s: ino=%d\n", __func__, ino);
2546 ret = check_inum_access(fs, ino, R_OK);
2550 err = ext2fs_xattrs_open(fs, ino, &h);
2552 ret = translate_error(fs, ino, err);
2556 err = ext2fs_xattrs_read(h);
2558 ret = translate_error(fs, ino, err);
2562 /* Count buffer space needed for names */
2564 err = ext2fs_xattrs_iterate(h, count_buffer_space, &bufsz);
2566 ret = translate_error(fs, ino, err);
2573 } else if (len < bufsz) {
2578 /* Copy names out */
2579 memset(names, 0, len);
2580 err = ext2fs_xattrs_iterate(h, copy_names, &names);
2582 ret = translate_error(fs, ino, err);
2587 err = ext2fs_xattrs_close(&h);
2589 ret = translate_error(fs, ino, err);
2591 pthread_mutex_unlock(&ff->bfl);
2596 static int op_setxattr(const char *path EXT2FS_ATTR((unused)),
2597 const char *key, const char *value,
2598 size_t len, int flags EXT2FS_ATTR((unused)))
2600 struct fuse_context *ctxt = fuse_get_context();
2601 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2603 struct ext2_xattr_handle *h;
2604 struct xattr_translate *xt;
2611 FUSE2FS_CHECK_CONTEXT(ff);
2613 pthread_mutex_lock(&ff->bfl);
2614 if (!ext2fs_has_feature_xattr(fs->super)) {
2619 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2620 if (err || ino == 0) {
2621 ret = translate_error(fs, 0, err);
2624 dbg_printf("%s: ino=%d\n", __func__, ino);
2626 ret = check_inum_access(fs, ino, W_OK);
2627 if (ret == -EACCES) {
2633 err = ext2fs_xattrs_open(fs, ino, &h);
2635 ret = translate_error(fs, ino, err);
2639 err = ext2fs_xattrs_read(h);
2641 ret = translate_error(fs, ino, err);
2647 for (xt = xattr_translators; xt->prefix != NULL; xt++) {
2648 if (strncmp(key, xt->prefix, strlen(xt->prefix)) == 0) {
2649 err = xt->set(value, len, &cvalue, &clen);
2655 err = ext2fs_xattr_set(h, key, cvalue, clen);
2657 ret = translate_error(fs, ino, err);
2661 ret = update_ctime(fs, ino, NULL);
2663 if (cvalue != value)
2664 ext2fs_free_mem(&cvalue);
2666 err = ext2fs_xattrs_close(&h);
2668 ret = translate_error(fs, ino, err);
2670 pthread_mutex_unlock(&ff->bfl);
2675 static int op_removexattr(const char *path, const char *key)
2677 struct fuse_context *ctxt = fuse_get_context();
2678 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2680 struct ext2_xattr_handle *h;
2685 FUSE2FS_CHECK_CONTEXT(ff);
2687 pthread_mutex_lock(&ff->bfl);
2688 if (!ext2fs_has_feature_xattr(fs->super)) {
2693 if (!fs_can_allocate(ff, 1)) {
2698 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2699 if (err || ino == 0) {
2700 ret = translate_error(fs, 0, err);
2703 dbg_printf("%s: ino=%d\n", __func__, ino);
2705 ret = check_inum_access(fs, ino, W_OK);
2709 err = ext2fs_xattrs_open(fs, ino, &h);
2711 ret = translate_error(fs, ino, err);
2715 err = ext2fs_xattrs_read(h);
2717 ret = translate_error(fs, ino, err);
2721 err = ext2fs_xattr_remove(h, key);
2723 ret = translate_error(fs, ino, err);
2727 ret = update_ctime(fs, ino, NULL);
2729 err = ext2fs_xattrs_close(&h);
2731 ret = translate_error(fs, ino, err);
2733 pthread_mutex_unlock(&ff->bfl);
2738 struct readdir_iter {
2740 fuse_fill_dir_t func;
2743 static int op_readdir_iter(ext2_ino_t dir EXT2FS_ATTR((unused)),
2744 int entry EXT2FS_ATTR((unused)),
2745 struct ext2_dir_entry *dirent,
2746 int offset EXT2FS_ATTR((unused)),
2747 int blocksize EXT2FS_ATTR((unused)),
2748 char *buf EXT2FS_ATTR((unused)), void *data)
2750 struct readdir_iter *i = data;
2751 char namebuf[EXT2_NAME_LEN + 1];
2754 memcpy(namebuf, dirent->name, dirent->name_len & 0xFF);
2755 namebuf[dirent->name_len & 0xFF] = 0;
2756 ret = i->func(i->buf, namebuf, NULL, 0);
2758 return DIRENT_ABORT;
2763 static int op_readdir(const char *path EXT2FS_ATTR((unused)),
2764 void *buf, fuse_fill_dir_t fill_func,
2765 off_t offset EXT2FS_ATTR((unused)),
2766 struct fuse_file_info *fp)
2768 struct fuse_context *ctxt = fuse_get_context();
2769 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2770 struct fuse2fs_file_handle *fh =
2771 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
2774 struct readdir_iter i;
2777 FUSE2FS_CHECK_CONTEXT(ff);
2779 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
2780 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
2781 pthread_mutex_lock(&ff->bfl);
2784 err = ext2fs_dir_iterate2(fs, fh->ino, 0, NULL, op_readdir_iter, &i);
2786 ret = translate_error(fs, fh->ino, err);
2790 if (fs_writeable(fs)) {
2791 ret = update_atime(fs, fh->ino);
2796 pthread_mutex_unlock(&ff->bfl);
2800 static int op_access(const char *path, int mask)
2802 struct fuse_context *ctxt = fuse_get_context();
2803 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2809 FUSE2FS_CHECK_CONTEXT(ff);
2811 dbg_printf("%s: path=%s mask=0x%x\n", __func__, path, mask);
2812 pthread_mutex_lock(&ff->bfl);
2813 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
2814 if (err || ino == 0) {
2815 ret = translate_error(fs, 0, err);
2819 ret = check_inum_access(fs, ino, mask);
2824 pthread_mutex_unlock(&ff->bfl);
2828 static int op_create(const char *path, mode_t mode, struct fuse_file_info *fp)
2830 struct fuse_context *ctxt = fuse_get_context();
2831 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
2833 ext2_ino_t parent, child;
2838 struct ext2_inode_large inode;
2841 FUSE2FS_CHECK_CONTEXT(ff);
2843 dbg_printf("%s: path=%s mode=0%o\n", __func__, path, mode);
2844 temp_path = strdup(path);
2849 node_name = strrchr(temp_path, '/');
2858 pthread_mutex_lock(&ff->bfl);
2859 if (!fs_can_allocate(ff, 1)) {
2864 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
2867 ret = translate_error(fs, 0, err);
2871 ret = check_inum_access(fs, parent, W_OK);
2877 filetype = ext2_file_type(mode);
2879 err = ext2fs_new_inode(fs, parent, mode, 0, &child);
2881 ret = translate_error(fs, parent, err);
2885 dbg_printf("%s: creating ino=%d/name=%s in dir=%d\n", __func__, child,
2887 err = ext2fs_link(fs, parent, node_name, child, filetype);
2888 if (err == EXT2_ET_DIR_NO_SPACE) {
2889 err = ext2fs_expand_dir(fs, parent);
2891 ret = translate_error(fs, parent, err);
2895 err = ext2fs_link(fs, parent, node_name, child,
2899 ret = translate_error(fs, parent, err);
2903 ret = update_mtime(fs, parent, NULL);
2907 memset(&inode, 0, sizeof(inode));
2908 inode.i_mode = mode;
2909 inode.i_links_count = 1;
2910 inode.i_extra_isize = sizeof(struct ext2_inode_large) -
2911 EXT2_GOOD_OLD_INODE_SIZE;
2912 inode.i_uid = ctxt->uid;
2913 ext2fs_set_i_uid_high(inode, ctxt->uid >> 16);
2914 inode.i_gid = ctxt->gid;
2915 ext2fs_set_i_gid_high(inode, ctxt->gid >> 16);
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();
3123 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3125 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3126 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3127 memset(&inode, 0, sizeof(inode));
3128 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3131 return translate_error(fs, fh->ino, err);
3133 if (!ff->fakeroot && ctxt->uid != 0 && inode_uid(inode) != ctxt->uid)
3136 if ((inode.i_flags ^ flags) & ~FUSE2FS_MODIFIABLE_IFLAGS)
3139 inode.i_flags = (inode.i_flags & ~FUSE2FS_MODIFIABLE_IFLAGS) |
3140 (flags & FUSE2FS_MODIFIABLE_IFLAGS);
3142 ret = update_ctime(fs, fh->ino, &inode);
3146 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3149 return translate_error(fs, fh->ino, err);
3154 static int ioctl_getversion(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3158 struct ext2_inode_large inode;
3160 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3161 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3162 memset(&inode, 0, sizeof(inode));
3163 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3166 return translate_error(fs, fh->ino, err);
3168 *(__u32 *)data = inode.i_generation;
3172 static int ioctl_setversion(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3176 struct ext2_inode_large inode;
3178 __u32 generation = *(__u32 *)data;
3179 struct fuse_context *ctxt = fuse_get_context();
3180 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3182 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3183 dbg_printf("%s: ino=%d\n", __func__, fh->ino);
3184 memset(&inode, 0, sizeof(inode));
3185 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3188 return translate_error(fs, fh->ino, err);
3190 if (!ff->fakeroot && ctxt->uid != 0 && inode_uid(inode) != ctxt->uid)
3193 inode.i_generation = generation;
3195 ret = update_ctime(fs, fh->ino, &inode);
3199 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3202 return translate_error(fs, fh->ino, err);
3206 #endif /* SUPPORT_I_FLAGS */
3209 static int ioctl_fitrim(ext2_filsys fs, struct fuse2fs_file_handle *fh,
3212 struct fstrim_range *fr = data;
3213 blk64_t start, end, max_blocks, b, cleared;
3216 start = fr->start / fs->blocksize;
3217 end = (fr->start + fr->len - 1) / fs->blocksize;
3218 dbg_printf("%s: start=%llu end=%llu\n", __func__, start, end);
3220 if (start < fs->super->s_first_data_block)
3221 start = fs->super->s_first_data_block;
3222 if (start >= ext2fs_blocks_count(fs->super))
3223 start = ext2fs_blocks_count(fs->super) - 1;
3225 if (end < fs->super->s_first_data_block)
3226 end = fs->super->s_first_data_block;
3227 if (end >= ext2fs_blocks_count(fs->super))
3228 end = ext2fs_blocks_count(fs->super) - 1;
3231 max_blocks = 2048ULL * 1024 * 1024 / fs->blocksize;
3234 while (start <= end) {
3235 err = ext2fs_find_first_zero_block_bitmap2(fs->block_map,
3236 start, end, &start);
3240 return translate_error(fs, fh->ino, err);
3242 b = start + max_blocks < end ? start + max_blocks : end;
3243 err = ext2fs_find_first_set_block_bitmap2(fs->block_map,
3245 if (err && err != ENOENT)
3246 return translate_error(fs, fh->ino, err);
3247 if (b - start >= fr->minlen) {
3248 err = io_channel_discard(fs->io, start, b - start);
3250 return translate_error(fs, fh->ino, err);
3251 cleared += b - start;
3252 fr->len = cleared * fs->blocksize;
3261 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
3262 static int op_ioctl(const char *path EXT2FS_ATTR((unused)), int cmd,
3263 void *arg EXT2FS_ATTR((unused)),
3264 struct fuse_file_info *fp,
3265 unsigned int flags EXT2FS_ATTR((unused)), void *data)
3267 struct fuse_context *ctxt = fuse_get_context();
3268 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3269 struct fuse2fs_file_handle *fh =
3270 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3274 FUSE2FS_CHECK_CONTEXT(ff);
3276 pthread_mutex_lock(&ff->bfl);
3277 switch ((unsigned long) cmd) {
3278 #ifdef SUPPORT_I_FLAGS
3279 case EXT2_IOC_GETFLAGS:
3280 ret = ioctl_getflags(fs, fh, data);
3282 case EXT2_IOC_SETFLAGS:
3283 ret = ioctl_setflags(fs, fh, data);
3285 case EXT2_IOC_GETVERSION:
3286 ret = ioctl_getversion(fs, fh, data);
3288 case EXT2_IOC_SETVERSION:
3289 ret = ioctl_setversion(fs, fh, data);
3294 ret = ioctl_fitrim(fs, fh, data);
3298 dbg_printf("%s: Unknown ioctl %d\n", __func__, cmd);
3301 pthread_mutex_unlock(&ff->bfl);
3305 #endif /* FUSE 28 */
3307 static int op_bmap(const char *path, size_t blocksize EXT2FS_ATTR((unused)),
3310 struct fuse_context *ctxt = fuse_get_context();
3311 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3317 FUSE2FS_CHECK_CONTEXT(ff);
3319 pthread_mutex_lock(&ff->bfl);
3320 err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
3322 ret = translate_error(fs, 0, err);
3325 dbg_printf("%s: ino=%d blk=%"PRIu64"\n", __func__, ino, *idx);
3327 err = ext2fs_bmap2(fs, ino, NULL, NULL, 0, *idx, 0, (blk64_t *)idx);
3329 ret = translate_error(fs, ino, err);
3334 pthread_mutex_unlock(&ff->bfl);
3338 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3339 # ifdef SUPPORT_FALLOCATE
3340 static int fallocate_helper(struct fuse_file_info *fp, int mode, off_t offset,
3343 struct fuse_context *ctxt = fuse_get_context();
3344 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3345 struct fuse2fs_file_handle *fh =
3346 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3348 struct ext2_inode_large inode;
3354 FUSE2FS_CHECK_CONTEXT(ff);
3356 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3357 start = offset / fs->blocksize;
3358 end = (offset + len - 1) / fs->blocksize;
3359 dbg_printf("%s: ino=%d mode=0x%x start=%jd end=%llu\n", __func__,
3360 fh->ino, mode, offset / fs->blocksize, end);
3361 if (!fs_can_allocate(ff, len / fs->blocksize))
3364 memset(&inode, 0, sizeof(inode));
3365 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3369 fsize = EXT2_I_SIZE(&inode);
3371 /* Allocate a bunch of blocks */
3372 flags = (mode & FL_KEEP_SIZE_FLAG ? 0 :
3373 EXT2_FALLOCATE_INIT_BEYOND_EOF);
3374 err = ext2fs_fallocate(fs, flags, fh->ino,
3375 (struct ext2_inode *)&inode,
3376 ~0ULL, start, end - start + 1);
3377 if (err && err != EXT2_ET_BLOCK_ALLOC_FAIL)
3378 return translate_error(fs, fh->ino, err);
3381 if (!(mode & FL_KEEP_SIZE_FLAG)) {
3382 if ((__u64) offset + len > fsize) {
3383 err = ext2fs_inode_size_set(fs,
3384 (struct ext2_inode *)&inode,
3387 return translate_error(fs, fh->ino, err);
3391 err = update_mtime(fs, fh->ino, &inode);
3395 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3398 return translate_error(fs, fh->ino, err);
3403 static errcode_t clean_block_middle(ext2_filsys fs, ext2_ino_t ino,
3404 struct ext2_inode_large *inode, off_t offset,
3405 off_t len, char **buf)
3412 residue = offset % fs->blocksize;
3417 err = ext2fs_get_mem(fs->blocksize, buf);
3422 err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0,
3423 offset / fs->blocksize, &retflags, &blk);
3426 if (!blk || (retflags & BMAP_RET_UNINIT))
3429 err = io_channel_read_blk(fs->io, blk, 1, *buf);
3433 memset(*buf + residue, 0, len);
3435 return io_channel_write_blk(fs->io, blk, 1, *buf);
3438 static errcode_t clean_block_edge(ext2_filsys fs, ext2_ino_t ino,
3439 struct ext2_inode_large *inode, off_t offset,
3440 int clean_before, char **buf)
3447 residue = offset % fs->blocksize;
3452 err = ext2fs_get_mem(fs->blocksize, buf);
3457 err = ext2fs_bmap2(fs, ino, (struct ext2_inode *)inode, *buf, 0,
3458 offset / fs->blocksize, &retflags, &blk);
3462 err = io_channel_read_blk(fs->io, blk, 1, *buf);
3465 if (!blk || (retflags & BMAP_RET_UNINIT))
3469 memset(*buf, 0, residue);
3471 memset(*buf + residue, 0, fs->blocksize - residue);
3473 return io_channel_write_blk(fs->io, blk, 1, *buf);
3476 static int punch_helper(struct fuse_file_info *fp, int mode, off_t offset,
3479 struct fuse_context *ctxt = fuse_get_context();
3480 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3481 struct fuse2fs_file_handle *fh =
3482 (struct fuse2fs_file_handle *)(uintptr_t)fp->fh;
3484 struct ext2_inode_large inode;
3489 FUSE2FS_CHECK_CONTEXT(ff);
3491 FUSE2FS_CHECK_MAGIC(fs, fh, FUSE2FS_FILE_MAGIC);
3492 dbg_printf("%s: offset=%jd len=%jd\n", __func__, offset, len);
3494 /* kernel ext4 punch requires this flag to be set */
3495 if (!(mode & FL_KEEP_SIZE_FLAG))
3498 /* Punch out a bunch of blocks */
3499 start = (offset + fs->blocksize - 1) / fs->blocksize;
3500 end = (offset + len - fs->blocksize) / fs->blocksize;
3501 dbg_printf("%s: ino=%d mode=0x%x start=%llu end=%llu\n", __func__,
3502 fh->ino, mode, start, end);
3504 memset(&inode, 0, sizeof(inode));
3505 err = ext2fs_read_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3508 return translate_error(fs, fh->ino, err);
3510 /* Zero everything before the first block and after the last block */
3511 if ((offset / fs->blocksize) == ((offset + len) / fs->blocksize))
3512 err = clean_block_middle(fs, fh->ino, &inode, offset,
3515 err = clean_block_edge(fs, fh->ino, &inode, offset, 0, &buf);
3517 err = clean_block_edge(fs, fh->ino, &inode,
3518 offset + len, 1, &buf);
3521 ext2fs_free_mem(&buf);
3523 return translate_error(fs, fh->ino, err);
3525 /* Unmap full blocks in the middle */
3527 err = ext2fs_punch(fs, fh->ino, (struct ext2_inode *)&inode,
3530 return translate_error(fs, fh->ino, err);
3533 err = update_mtime(fs, fh->ino, &inode);
3537 err = ext2fs_write_inode_full(fs, fh->ino, (struct ext2_inode *)&inode,
3540 return translate_error(fs, fh->ino, err);
3545 static int op_fallocate(const char *path EXT2FS_ATTR((unused)), int mode,
3546 off_t offset, off_t len,
3547 struct fuse_file_info *fp)
3549 struct fuse_context *ctxt = fuse_get_context();
3550 struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
3551 ext2_filsys fs = ff->fs;
3554 /* Catch unknown flags */
3555 if (mode & ~(FL_PUNCH_HOLE_FLAG | FL_KEEP_SIZE_FLAG))
3558 pthread_mutex_lock(&ff->bfl);
3559 if (!fs_writeable(fs)) {
3563 if (mode & FL_PUNCH_HOLE_FLAG)
3564 ret = punch_helper(fp, mode, offset, len);
3566 ret = fallocate_helper(fp, mode, offset, len);
3568 pthread_mutex_unlock(&ff->bfl);
3572 # endif /* SUPPORT_FALLOCATE */
3573 #endif /* FUSE 29 */
3575 static struct fuse_operations fs_ops = {
3577 .destroy = op_destroy,
3578 .getattr = op_getattr,
3579 .readlink = op_readlink,
3582 .unlink = op_unlink,
3584 .symlink = op_symlink,
3585 .rename = op_rename,
3589 .truncate = op_truncate,
3593 .statfs = op_statfs,
3594 .release = op_release,
3596 .setxattr = op_setxattr,
3597 .getxattr = op_getxattr,
3598 .listxattr = op_listxattr,
3599 .removexattr = op_removexattr,
3601 .readdir = op_readdir,
3602 .releasedir = op_release,
3603 .fsyncdir = op_fsync,
3604 .access = op_access,
3605 .create = op_create,
3606 .ftruncate = op_ftruncate,
3607 .fgetattr = op_fgetattr,
3608 .utimens = op_utimens,
3609 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3610 # if defined(UTIME_NOW) || defined(UTIME_OMIT)
3611 .flag_utime_omit_ok = 1,
3619 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
3621 .flag_nullpath_ok = 1,
3623 #if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 9)
3625 # ifdef SUPPORT_FALLOCATE
3626 .fallocate = op_fallocate,
3631 static int get_random_bytes(void *p, size_t sz)
3636 fd = open("/dev/urandom", O_RDONLY);
3638 perror("/dev/urandom");
3642 r = read(fd, p, sz);
3645 return (size_t) r == sz;
3654 #define FUSE2FS_OPT(t, p, v) { t, offsetof(struct fuse2fs, p), v }
3656 static struct fuse_opt fuse2fs_opts[] = {
3657 FUSE2FS_OPT("ro", ro, 1),
3658 FUSE2FS_OPT("errors=panic", panic_on_error, 1),
3659 FUSE2FS_OPT("minixdf", minixdf, 1),
3660 FUSE2FS_OPT("fakeroot", fakeroot, 1),
3661 FUSE2FS_OPT("fuse2fs_debug", debug, 1),
3662 FUSE2FS_OPT("no_default_opts", no_default_opts, 1),
3663 FUSE2FS_OPT("norecovery", norecovery, 1),
3664 FUSE2FS_OPT("offset=%lu", offset, 0),
3666 FUSE_OPT_KEY("-V", FUSE2FS_VERSION),
3667 FUSE_OPT_KEY("--version", FUSE2FS_VERSION),
3668 FUSE_OPT_KEY("-h", FUSE2FS_HELP),
3669 FUSE_OPT_KEY("--help", FUSE2FS_HELP),
3670 FUSE_OPT_KEY("--helpfull", FUSE2FS_HELPFULL),
3675 static int fuse2fs_opt_proc(void *data, const char *arg,
3676 int key, struct fuse_args *outargs)
3678 struct fuse2fs *ff = data;
3681 case FUSE_OPT_KEY_NONOPT:
3683 ff->device = strdup(arg);
3688 case FUSE2FS_HELPFULL:
3690 "usage: %s device/image mountpoint [options]\n"
3692 "general options:\n"
3693 " -o opt,[opt...] mount options\n"
3694 " -h --help print help\n"
3695 " -V --version print version\n"
3697 "fuse2fs options:\n"
3698 " -o ro read-only mount\n"
3699 " -o errors=panic dump core on error\n"
3700 " -o minixdf minix-style df\n"
3701 " -o fakeroot pretend to be root for permission checks\n"
3702 " -o no_default_opts do not include default fuse options\n"
3703 " -o offset=<bytes> similar to mount -o offset=<bytes>, mount the partition starting at <bytes>\n"
3704 " -o norecovery don't replay the journal (implies ro)\n"
3705 " -o fuse2fs_debug enable fuse2fs debugging\n"
3708 if (key == FUSE2FS_HELPFULL) {
3709 fuse_opt_add_arg(outargs, "-ho");
3710 fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL);
3712 fprintf(stderr, "Try --helpfull to get a list of "
3713 "all flags, including the FUSE options.\n");
3717 case FUSE2FS_VERSION:
3718 fprintf(stderr, "fuse2fs %s (%s)\n", E2FSPROGS_VERSION,
3720 fuse_opt_add_arg(outargs, "--version");
3721 fuse_main(outargs->argc, outargs->argv, &fs_ops, NULL);
3727 int main(int argc, char *argv[])
3729 struct fuse_args args = FUSE_ARGS_INIT(argc, argv);
3730 struct fuse2fs fctx;
3733 char extra_args[BUFSIZ];
3735 int flags = EXT2_FLAG_64BITS | EXT2_FLAG_THREADS | EXT2_FLAG_EXCLUSIVE;
3737 memset(&fctx, 0, sizeof(fctx));
3738 fctx.magic = FUSE2FS_MAGIC;
3740 fuse_opt_parse(&args, &fctx, fuse2fs_opts, fuse2fs_opt_proc);
3741 if (fctx.device == NULL) {
3742 fprintf(stderr, "Missing ext4 device/image\n");
3743 fprintf(stderr, "See '%s -h' for usage\n", argv[0]);
3747 if (fctx.norecovery)
3750 printf("%s", _("Mounting read-only.\n"));
3753 setlocale(LC_MESSAGES, "");
3754 setlocale(LC_CTYPE, "");
3755 bindtextdomain(NLS_CAT_NAME, LOCALEDIR);
3756 textdomain(NLS_CAT_NAME);
3757 set_com_err_gettext(gettext);
3759 add_error_table(&et_ext2_error_table);
3761 /* Set up error logging */
3762 logfile = getenv("FUSE2FS_LOGFILE");
3764 fctx.err_fp = fopen(logfile, "a");
3770 fctx.err_fp = stderr;
3772 /* Will we allow users to allocate every last block? */
3773 if (getenv("FUSE2FS_ALLOC_ALL_BLOCKS")) {
3774 printf(_("%s: Allowing users to allocate all blocks. "
3775 "This is dangerous!\n"), fctx.device);
3776 fctx.alloc_all_blocks = 1;
3779 /* Start up the fs (while we still can use stdout) */
3782 flags |= EXT2_FLAG_RW;
3784 sprintf(options, "offset=%lu", fctx.offset);
3785 err = ext2fs_open2(fctx.device, options, flags, 0, 0, unix_io_manager,
3788 printf(_("%s: %s.\n"), fctx.device, error_message(err));
3789 printf(_("Please run e2fsck -fy %s.\n"), fctx.device);
3792 fctx.fs = global_fs;
3793 global_fs->priv_data = &fctx;
3797 if (ext2fs_has_feature_journal_needs_recovery(global_fs->super)) {
3798 if (fctx.norecovery) {
3799 printf(_("%s: mounting read-only without "
3800 "recovering journal\n"),
3802 } else if (!fctx.ro) {
3803 printf(_("%s: recovering journal\n"), fctx.device);
3804 err = ext2fs_run_ext3_journal(&global_fs);
3806 printf(_("%s: %s.\n"), fctx.device,
3807 error_message(err));
3808 printf(_("Please run e2fsck -fy %s.\n"),
3812 ext2fs_clear_feature_journal_needs_recovery(global_fs->super);
3813 ext2fs_mark_super_dirty(global_fs);
3815 printf("%s", _("Journal needs recovery; running "
3816 "`e2fsck -E journal_only' is required.\n"));
3822 if (ext2fs_has_feature_journal(global_fs->super))
3823 printf(_("%s: Writing to the journal is not supported.\n"),
3825 err = ext2fs_read_inode_bitmap(global_fs);
3827 translate_error(global_fs, 0, err);
3830 err = ext2fs_read_block_bitmap(global_fs);
3832 translate_error(global_fs, 0, err);
3837 if (!(global_fs->super->s_state & EXT2_VALID_FS))
3838 printf("%s", _("Warning: Mounting unchecked fs, running e2fsck "
3839 "is recommended.\n"));
3840 if (global_fs->super->s_max_mnt_count > 0 &&
3841 global_fs->super->s_mnt_count >= global_fs->super->s_max_mnt_count)
3842 printf("%s", _("Warning: Maximal mount count reached, running "
3843 "e2fsck is recommended.\n"));
3844 if (global_fs->super->s_checkinterval > 0 &&
3845 (time_t) (global_fs->super->s_lastcheck +
3846 global_fs->super->s_checkinterval) <= time(0))
3847 printf("%s", _("Warning: Check time reached; running e2fsck "
3848 "is recommended.\n"));
3849 if (global_fs->super->s_last_orphan)
3851 _("Orphans detected; running e2fsck is recommended.\n"));
3853 if (global_fs->super->s_state & EXT2_ERROR_FS) {
3855 _("Errors detected; running e2fsck is required.\n"));
3859 /* Initialize generation counter */
3860 get_random_bytes(&fctx.next_generation, sizeof(unsigned int));
3862 /* Set up default fuse parameters */
3863 snprintf(extra_args, BUFSIZ, "-okernel_cache,subtype=ext4,use_ino,"
3864 "fsname=%s,attr_timeout=0" FUSE_PLATFORM_OPTS,
3866 if (fctx.no_default_opts == 0)
3867 fuse_opt_add_arg(&args, extra_args);
3869 if (fctx.fakeroot) {
3870 #ifdef HAVE_MOUNT_NODEV
3871 fuse_opt_add_arg(&args,"-onodev");
3873 #ifdef HAVE_MOUNT_NOSUID
3874 fuse_opt_add_arg(&args,"-onosuid");
3881 printf("fuse arguments:");
3882 for (i = 0; i < args.argc; i++)
3883 printf(" '%s'", args.argv[i]);
3887 pthread_mutex_init(&fctx.bfl, NULL);
3888 fuse_main(args.argc, args.argv, &fs_ops, &fctx);
3889 pthread_mutex_destroy(&fctx.bfl);
3894 err = ext2fs_close(global_fs);
3896 com_err(argv[0], err, "while closing fs");
3902 static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
3903 const char *file, int line)
3905 struct timespec now;
3907 struct fuse2fs *ff = fs->priv_data;
3910 /* Translate ext2 error to unix error code */
3911 if (err < EXT2_ET_BASE)
3912 goto no_translation;
3914 case EXT2_ET_NO_MEMORY:
3915 case EXT2_ET_TDB_ERR_OOM:
3918 case EXT2_ET_INVALID_ARGUMENT:
3919 case EXT2_ET_LLSEEK_FAILED:
3922 case EXT2_ET_NO_DIRECTORY:
3925 case EXT2_ET_FILE_NOT_FOUND:
3928 case EXT2_ET_DIR_NO_SPACE:
3931 case EXT2_ET_TOOSMALL:
3932 case EXT2_ET_BLOCK_ALLOC_FAIL:
3933 case EXT2_ET_INODE_ALLOC_FAIL:
3934 case EXT2_ET_EA_NO_SPACE:
3937 case EXT2_ET_SYMLINK_LOOP:
3940 case EXT2_ET_FILE_TOO_BIG:
3943 case EXT2_ET_TDB_ERR_EXISTS:
3944 case EXT2_ET_FILE_EXISTS:
3947 case EXT2_ET_MMP_FAILED:
3948 case EXT2_ET_MMP_FSCK_ON:
3951 case EXT2_ET_EA_KEY_NOT_FOUND:
3958 /* Sometimes fuse returns a garbage file handle pointer to us... */
3959 case EXT2_ET_MAGIC_EXT2_FILE:
3962 case EXT2_ET_UNIMPLEMENTED:
3976 fprintf(ff->err_fp, "FUSE2FS (%s): %s (inode #%d) at %s:%d.\n",
3977 fs->device_name ? fs->device_name : "???",
3978 error_message(err), ino, file, line);
3980 fprintf(ff->err_fp, "FUSE2FS (%s): %s at %s:%d.\n",
3981 fs->device_name ? fs->device_name : "???",
3982 error_message(err), file, line);
3985 /* Make a note in the error log */
3987 fs->super->s_last_error_time = now.tv_sec;
3988 fs->super->s_last_error_ino = ino;
3989 fs->super->s_last_error_line = line;
3990 fs->super->s_last_error_block = err; /* Yeah... */
3991 strncpy((char *)fs->super->s_last_error_func, file,
3992 sizeof(fs->super->s_last_error_func));
3993 if (fs->super->s_first_error_time == 0) {
3994 fs->super->s_first_error_time = now.tv_sec;
3995 fs->super->s_first_error_ino = ino;
3996 fs->super->s_first_error_line = line;
3997 fs->super->s_first_error_block = err;
3998 strncpy((char *)fs->super->s_first_error_func, file,
3999 sizeof(fs->super->s_first_error_func));
4002 fs->super->s_error_count++;
4003 ext2fs_mark_super_dirty(fs);
4005 if (ff->panic_on_error)