-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfuse2fs.c
4067 lines (3552 loc) · 94 KB
/
fuse2fs.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* fuse2fs.c - FUSE server for e2fsprogs.
*
* Copyright (C) 2014 Oracle.
*
* converted to rentrant code for vufuse (Renzo Davoli 2024)
*
* %Begin-Header%
* This file may be redistributed under the terms of the GNU Public
* License.
* %End-Header%
*/
#ifndef _GNU_SOURCE
#define _GNU_SOURCE
#endif
#include "config.h"
#include <pthread.h>
#ifdef __linux__
# include <linux/fs.h>
# include <linux/falloc.h>
# include <linux/xattr.h>
# ifdef HAVE_SYS_ACL_H
# define TRANSLATE_LINUX_ACLS
# endif
#endif
#ifdef TRANSLATE_LINUX_ACLS
# include <sys/acl.h>
#endif
#include <sys/ioctl.h>
#include <unistd.h>
#include <fuse.h>
#include <inttypes.h>
#include "ext2fs/ext2fs.h"
#include "ext2fs/ext2_fs.h"
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
# define FUSE_PLATFORM_OPTS ""
#else
# ifdef __linux__
# define FUSE_PLATFORM_OPTS ",use_ino,big_writes"
# else
# define FUSE_PLATFORM_OPTS ",use_ino"
# endif
#endif
#include "../version.h"
#ifdef ENABLE_NLS
#include <libintl.h>
#include <locale.h>
#define _(a) (gettext(a))
#ifdef gettext_noop
#define N_(a) gettext_noop(a)
#else
#define N_(a) (a)
#endif
#define P_(singular, plural, n) (ngettext(singular, plural, n))
#ifndef NLS_CAT_NAME
#define NLS_CAT_NAME "e2fsprogs"
#endif
#ifndef LOCALEDIR
#define LOCALEDIR "/usr/share/locale"
#endif
#else
#define _(a) (a)
#define N_(a) a
#define P_(singular, plural, n) ((n) == 1 ? (singular) : (plural))
#endif
#undef DEBUG
#ifdef DEBUG
# define dbg_printf(f, a...) do {printf("FUSE2FS-" f, ## a); \
fflush(stdout); \
} while (0)
#else
# define dbg_printf(f, ...)
#endif
#if FUSE_VERSION >= FUSE_MAKE_VERSION(2, 8)
# ifdef _IOR
# ifdef _IOW
# define SUPPORT_I_FLAGS
# endif
# endif
#endif
#ifdef FALLOC_FL_KEEP_SIZE
# define FL_KEEP_SIZE_FLAG FALLOC_FL_KEEP_SIZE
# define SUPPORT_FALLOCATE
#else
# define FL_KEEP_SIZE_FLAG (0)
#endif
#ifdef FALLOC_FL_PUNCH_HOLE
# define FL_PUNCH_HOLE_FLAG FALLOC_FL_PUNCH_HOLE
#else
# define FL_PUNCH_HOLE_FLAG (0)
#endif
errcode_t ext2fs_run_ext3_journal(ext2_filsys *fs);
#ifdef CONFIG_JBD_DEBUG /* Enabled by configure --enable-jbd-debug */
int journal_enable_debug = -1;
#endif
/* ACL translation stuff */
#ifdef TRANSLATE_LINUX_ACLS
/*
* Copied from acl_ea.h in libacl source; ACLs have to be sent to and from fuse
* in this format... at least on Linux.
*/
#define ACL_EA_ACCESS "system.posix_acl_access"
#define ACL_EA_DEFAULT "system.posix_acl_default"
#define ACL_EA_VERSION 0x0002
typedef struct {
u_int16_t e_tag;
u_int16_t e_perm;
u_int32_t e_id;
} acl_ea_entry;
typedef struct {
u_int32_t a_version;
#if __GNUC_PREREQ (4, 8)
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wpedantic"
#endif
acl_ea_entry a_entries[0];
#if __GNUC_PREREQ (4, 8)
#pragma GCC diagnostic pop
#endif
} acl_ea_header;
static inline size_t acl_ea_size(int count)
{
return sizeof(acl_ea_header) + count * sizeof(acl_ea_entry);
}
static inline int acl_ea_count(size_t size)
{
if (size < sizeof(acl_ea_header))
return -1;
size -= sizeof(acl_ea_header);
if (size % sizeof(acl_ea_entry))
return -1;
return size / sizeof(acl_ea_entry);
}
/*
* ext4 ACL structures, copied from fs/ext4/acl.h.
*/
#define EXT4_ACL_VERSION 0x0001
typedef struct {
__u16 e_tag;
__u16 e_perm;
__u32 e_id;
} ext4_acl_entry;
typedef struct {
__u16 e_tag;
__u16 e_perm;
} ext4_acl_entry_short;
typedef struct {
__u32 a_version;
} ext4_acl_header;
static inline size_t ext4_acl_size(int count)
{
if (count <= 4) {
return sizeof(ext4_acl_header) +
count * sizeof(ext4_acl_entry_short);
} else {
return sizeof(ext4_acl_header) +
4 * sizeof(ext4_acl_entry_short) +
(count - 4) * sizeof(ext4_acl_entry);
}
}
static inline int ext4_acl_count(size_t size)
{
ssize_t s;
size -= sizeof(ext4_acl_header);
s = size - 4 * sizeof(ext4_acl_entry_short);
if (s < 0) {
if (size % sizeof(ext4_acl_entry_short))
return -1;
return size / sizeof(ext4_acl_entry_short);
}
if (s % sizeof(ext4_acl_entry))
return -1;
return s / sizeof(ext4_acl_entry) + 4;
}
static errcode_t fuse_to_ext4_acl(acl_ea_header *facl, size_t facl_sz,
ext4_acl_header **eacl, size_t *eacl_sz)
{
int i, facl_count;
ext4_acl_header *h;
size_t h_sz;
ext4_acl_entry *e;
acl_ea_entry *a;
unsigned char *hptr;
errcode_t err;
facl_count = acl_ea_count(facl_sz);
h_sz = ext4_acl_size(facl_count);
if (facl_count < 0 || facl->a_version != ACL_EA_VERSION)
return EXT2_ET_INVALID_ARGUMENT;
err = ext2fs_get_mem(h_sz, &h);
if (err)
return err;
h->a_version = ext2fs_cpu_to_le32(EXT4_ACL_VERSION);
hptr = (unsigned char *) (h + 1);
for (i = 0, a = facl->a_entries; i < facl_count; i++, a++) {
e = (ext4_acl_entry *) hptr;
e->e_tag = ext2fs_cpu_to_le16(a->e_tag);
e->e_perm = ext2fs_cpu_to_le16(a->e_perm);
switch (a->e_tag) {
case ACL_USER:
case ACL_GROUP:
e->e_id = ext2fs_cpu_to_le32(a->e_id);
hptr += sizeof(ext4_acl_entry);
break;
case ACL_USER_OBJ:
case ACL_GROUP_OBJ:
case ACL_MASK:
case ACL_OTHER:
hptr += sizeof(ext4_acl_entry_short);
break;
default:
err = EXT2_ET_INVALID_ARGUMENT;
goto out;
}
}
*eacl = h;
*eacl_sz = h_sz;
return err;
out:
ext2fs_free_mem(&h);
return err;
}
static errcode_t ext4_to_fuse_acl(acl_ea_header **facl, size_t *facl_sz,
ext4_acl_header *eacl, size_t eacl_sz)
{
int i, eacl_count;
acl_ea_header *f;
ext4_acl_entry *e;
acl_ea_entry *a;
size_t f_sz;
unsigned char *hptr;
errcode_t err;
eacl_count = ext4_acl_count(eacl_sz);
f_sz = acl_ea_size(eacl_count);
if (eacl_count < 0 ||
eacl->a_version != ext2fs_cpu_to_le32(EXT4_ACL_VERSION))
return EXT2_ET_INVALID_ARGUMENT;
err = ext2fs_get_mem(f_sz, &f);
if (err)
return err;
f->a_version = ACL_EA_VERSION;
hptr = (unsigned char *) (eacl + 1);
for (i = 0, a = f->a_entries; i < eacl_count; i++, a++) {
e = (ext4_acl_entry *) hptr;
a->e_tag = ext2fs_le16_to_cpu(e->e_tag);
a->e_perm = ext2fs_le16_to_cpu(e->e_perm);
switch (a->e_tag) {
case ACL_USER:
case ACL_GROUP:
a->e_id = ext2fs_le32_to_cpu(e->e_id);
hptr += sizeof(ext4_acl_entry);
break;
case ACL_USER_OBJ:
case ACL_GROUP_OBJ:
case ACL_MASK:
case ACL_OTHER:
hptr += sizeof(ext4_acl_entry_short);
break;
default:
err = EXT2_ET_INVALID_ARGUMENT;
goto out;
}
}
*facl = f;
*facl_sz = f_sz;
return err;
out:
ext2fs_free_mem(&f);
return err;
}
#endif /* TRANSLATE_LINUX_ACLS */
/*
* ext2_file_t contains a struct inode, so we can't leave files open.
* Use this as a proxy instead.
*/
#define FUSE2FS_FILE_MAGIC (0xEF53DEAFUL)
struct fuse2fs_file_handle {
unsigned long magic;
ext2_ino_t ino;
int open_flags;
};
/* Main program context */
#define FUSE2FS_MAGIC (0xEF53DEADUL)
struct fuse2fs {
unsigned long magic;
ext2_filsys fs;
pthread_mutex_t bfl;
char *device;
int ro;
int debug;
int no_default_opts;
int panic_on_error;
int minixdf;
int fakeroot;
int alloc_all_blocks;
int norecovery;
unsigned long offset;
FILE *err_fp;
unsigned int next_generation;
};
#define FUSE2FS_CHECK_MAGIC(fs, ptr, num) do {if ((ptr)->magic != (num)) \
return translate_error((fs), 0, EXT2_ET_MAGIC_EXT2_FILE); \
} while (0)
#define FUSE2FS_CHECK_CONTEXT(ptr) do {if ((ptr)->magic != FUSE2FS_MAGIC) \
return translate_error(NULL, 0, EXT2_ET_BAD_MAGIC); \
} while (0)
static int __translate_error(ext2_filsys fs, errcode_t err, ext2_ino_t ino,
const char *file, int line);
#define translate_error(fs, ino, err) __translate_error((fs), (err), (ino), \
__FILE__, __LINE__)
/* for macosx */
#ifndef W_OK
# define W_OK 2
#endif
#ifndef R_OK
# define R_OK 4
#endif
#define EXT4_EPOCH_BITS 2
#define EXT4_EPOCH_MASK ((1 << EXT4_EPOCH_BITS) - 1)
#define EXT4_NSEC_MASK (~0UL << EXT4_EPOCH_BITS)
/*
* Extended fields will fit into an inode if the filesystem was formatted
* with large inodes (-I 256 or larger) and there are not currently any EAs
* consuming all of the available space. For new inodes we always reserve
* enough space for the kernel's known extended fields, but for inodes
* created with an old kernel this might not have been the case. None of
* the extended inode fields is critical for correct filesystem operation.
* This macro checks if a certain field fits in the inode. Note that
* inode-size = GOOD_OLD_INODE_SIZE + i_extra_isize
*/
#define EXT4_FITS_IN_INODE(ext4_inode, field) \
((offsetof(typeof(*ext4_inode), field) + \
sizeof((ext4_inode)->field)) \
<= ((size_t) EXT2_GOOD_OLD_INODE_SIZE + \
(ext4_inode)->i_extra_isize)) \
static inline __u32 ext4_encode_extra_time(const struct timespec *time)
{
__u32 extra = sizeof(time->tv_sec) > 4 ?
((time->tv_sec - (__s32)time->tv_sec) >> 32) &
EXT4_EPOCH_MASK : 0;
return extra | (time->tv_nsec << EXT4_EPOCH_BITS);
}
static inline void ext4_decode_extra_time(struct timespec *time, __u32 extra)
{
if (sizeof(time->tv_sec) > 4 && (extra & EXT4_EPOCH_MASK)) {
__u64 extra_bits = extra & EXT4_EPOCH_MASK;
/*
* Prior to kernel 3.14?, we had a broken decode function,
* wherein we effectively did this:
* if (extra_bits == 3)
* extra_bits = 0;
*/
time->tv_sec += extra_bits << 32;
}
time->tv_nsec = ((extra) & EXT4_NSEC_MASK) >> EXT4_EPOCH_BITS;
}
#define EXT4_INODE_SET_XTIME(xtime, timespec, raw_inode) \
do { \
(raw_inode)->xtime = (timespec)->tv_sec; \
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
(raw_inode)->xtime ## _extra = \
ext4_encode_extra_time(timespec); \
} while (0)
#define EXT4_EINODE_SET_XTIME(xtime, timespec, raw_inode) \
do { \
if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
(raw_inode)->xtime = (timespec)->tv_sec; \
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
(raw_inode)->xtime ## _extra = \
ext4_encode_extra_time(timespec); \
} while (0)
#define EXT4_INODE_GET_XTIME(xtime, timespec, raw_inode) \
do { \
(timespec)->tv_sec = (signed)((raw_inode)->xtime); \
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
ext4_decode_extra_time((timespec), \
(raw_inode)->xtime ## _extra); \
else \
(timespec)->tv_nsec = 0; \
} while (0)
#define EXT4_EINODE_GET_XTIME(xtime, timespec, raw_inode) \
do { \
if (EXT4_FITS_IN_INODE(raw_inode, xtime)) \
(timespec)->tv_sec = \
(signed)((raw_inode)->xtime); \
if (EXT4_FITS_IN_INODE(raw_inode, xtime ## _extra)) \
ext4_decode_extra_time((timespec), \
raw_inode->xtime ## _extra); \
else \
(timespec)->tv_nsec = 0; \
} while (0)
static void get_now(struct timespec *now)
{
#ifdef CLOCK_REALTIME
if (!clock_gettime(CLOCK_REALTIME, now))
return;
#endif
now->tv_sec = time(NULL);
now->tv_nsec = 0;
}
static void increment_version(struct ext2_inode_large *inode)
{
__u64 ver;
ver = inode->osd1.linux1.l_i_version;
if (EXT4_FITS_IN_INODE(inode, i_version_hi))
ver |= (__u64)inode->i_version_hi << 32;
ver++;
inode->osd1.linux1.l_i_version = ver;
if (EXT4_FITS_IN_INODE(inode, i_version_hi))
inode->i_version_hi = ver >> 32;
}
static void init_times(struct ext2_inode_large *inode)
{
struct timespec now;
get_now(&now);
EXT4_INODE_SET_XTIME(i_atime, &now, inode);
EXT4_INODE_SET_XTIME(i_ctime, &now, inode);
EXT4_INODE_SET_XTIME(i_mtime, &now, inode);
EXT4_EINODE_SET_XTIME(i_crtime, &now, inode);
increment_version(inode);
}
static int update_ctime(ext2_filsys fs, ext2_ino_t ino,
struct ext2_inode_large *pinode)
{
errcode_t err;
struct timespec now;
struct ext2_inode_large inode;
get_now(&now);
/* If user already has a inode buffer, just update that */
if (pinode) {
increment_version(pinode);
EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
return 0;
}
/* Otherwise we have to read-modify-write the inode */
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
increment_version(&inode);
EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
return 0;
}
static int update_atime(ext2_filsys fs, ext2_ino_t ino)
{
errcode_t err;
struct ext2_inode_large inode, *pinode;
struct timespec atime, mtime, now;
if (!(fs->flags & EXT2_FLAG_RW))
return 0;
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
pinode = &inode;
EXT4_INODE_GET_XTIME(i_atime, &atime, pinode);
EXT4_INODE_GET_XTIME(i_mtime, &mtime, pinode);
get_now(&now);
/*
* If atime is newer than mtime and atime hasn't been updated in thirty
* seconds, skip the atime update. Same idea as Linux "relatime".
*/
if (atime.tv_sec >= mtime.tv_sec && atime.tv_sec >= now.tv_sec - 30)
return 0;
EXT4_INODE_SET_XTIME(i_atime, &now, &inode);
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
return 0;
}
static int update_mtime(ext2_filsys fs, ext2_ino_t ino,
struct ext2_inode_large *pinode)
{
errcode_t err;
struct ext2_inode_large inode;
struct timespec now;
if (pinode) {
get_now(&now);
EXT4_INODE_SET_XTIME(i_mtime, &now, pinode);
EXT4_INODE_SET_XTIME(i_ctime, &now, pinode);
increment_version(pinode);
return 0;
}
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
get_now(&now);
EXT4_INODE_SET_XTIME(i_mtime, &now, &inode);
EXT4_INODE_SET_XTIME(i_ctime, &now, &inode);
increment_version(&inode);
err = ext2fs_write_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
return 0;
}
static int ext2_file_type(unsigned int mode)
{
if (LINUX_S_ISREG(mode))
return EXT2_FT_REG_FILE;
if (LINUX_S_ISDIR(mode))
return EXT2_FT_DIR;
if (LINUX_S_ISCHR(mode))
return EXT2_FT_CHRDEV;
if (LINUX_S_ISBLK(mode))
return EXT2_FT_BLKDEV;
if (LINUX_S_ISLNK(mode))
return EXT2_FT_SYMLINK;
if (LINUX_S_ISFIFO(mode))
return EXT2_FT_FIFO;
if (LINUX_S_ISSOCK(mode))
return EXT2_FT_SOCK;
return 0;
}
static int fs_can_allocate(struct fuse2fs *ff, blk64_t num)
{
ext2_filsys fs = ff->fs;
blk64_t reserved;
dbg_printf("%s: Asking for %llu; alloc_all=%d total=%llu free=%llu "
"rsvd=%llu\n", __func__, num, ff->alloc_all_blocks,
ext2fs_blocks_count(fs->super),
ext2fs_free_blocks_count(fs->super),
ext2fs_r_blocks_count(fs->super));
if (num > ext2fs_blocks_count(fs->super))
return 0;
if (ff->alloc_all_blocks)
return 1;
/*
* Different meaning for r_blocks -- libext2fs has bugs where the FS
* can get corrupted if it totally runs out of blocks. Avoid this
* by refusing to allocate any of the reserve blocks to anybody.
*/
reserved = ext2fs_r_blocks_count(fs->super);
if (reserved == 0)
reserved = ext2fs_blocks_count(fs->super) / 10;
return ext2fs_free_blocks_count(fs->super) > reserved + num;
}
static int fs_writeable(ext2_filsys fs)
{
return (fs->flags & EXT2_FLAG_RW) && (fs->super->s_error_count == 0);
}
static int check_inum_access(ext2_filsys fs, ext2_ino_t ino, mode_t mask)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
struct ext2_inode inode;
mode_t perms;
errcode_t err;
/* no writing to read-only or broken fs */
if ((mask & W_OK) && !fs_writeable(fs))
return -EROFS;
err = ext2fs_read_inode(fs, ino, &inode);
if (err)
return translate_error(fs, ino, err);
perms = inode.i_mode & 0777;
dbg_printf("access ino=%d mask=e%s%s%s perms=0%o fuid=%d fgid=%d "
"uid=%d gid=%d\n", ino,
(mask & R_OK ? "r" : ""), (mask & W_OK ? "w" : ""),
(mask & X_OK ? "x" : ""), perms, inode_uid(inode),
inode_gid(inode), ctxt->uid, ctxt->gid);
/* existence check */
if (mask == 0)
return 0;
/* is immutable? */
if ((mask & W_OK) &&
(inode.i_flags & EXT2_IMMUTABLE_FL))
return -EACCES;
/* Figure out what root's allowed to do */
if (ff->fakeroot || ctxt->uid == 0) {
/* Non-file access always ok */
if (!LINUX_S_ISREG(inode.i_mode))
return 0;
/* R/W access to a file always ok */
if (!(mask & X_OK))
return 0;
/* X access to a file ok if a user/group/other can X */
if (perms & 0111)
return 0;
/* Trying to execute a file that's not executable. BZZT! */
return -EACCES;
}
/* allow owner, if perms match */
if (inode_uid(inode) == ctxt->uid) {
if ((mask & (perms >> 6)) == mask)
return 0;
return -EACCES;
}
/* allow group, if perms match */
if (inode_gid(inode) == ctxt->gid) {
if ((mask & (perms >> 3)) == mask)
return 0;
return -EACCES;
}
/* otherwise check other */
if ((mask & perms) == mask)
return 0;
return -EACCES;
}
static void op_destroy(void *p EXT2FS_ATTR((unused)))
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
if (ff->magic != FUSE2FS_MAGIC) {
translate_error(NULL, 0, EXT2_ET_BAD_MAGIC);
return;
}
fs = ff->fs;
dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
if (fs->flags & EXT2_FLAG_RW) {
fs->super->s_state |= EXT2_VALID_FS;
if (fs->super->s_error_count)
fs->super->s_state |= EXT2_ERROR_FS;
ext2fs_mark_super_dirty(fs);
err = ext2fs_set_gdt_csum(fs);
if (err)
translate_error(fs, 0, err);
err = ext2fs_flush2(fs, 0);
if (err)
translate_error(fs, 0, err);
}
}
static void *op_init(struct fuse_conn_info *conn
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, struct fuse_config *cfg EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
if (ff->magic != FUSE2FS_MAGIC) {
translate_error(NULL, 0, EXT2_ET_BAD_MAGIC);
return NULL;
}
fs = ff->fs;
dbg_printf("%s: dev=%s\n", __func__, fs->device_name);
#ifdef FUSE_CAP_IOCTL_DIR
conn->want |= FUSE_CAP_IOCTL_DIR;
#endif
if (fs->flags & EXT2_FLAG_RW) {
fs->super->s_mnt_count++;
ext2fs_set_tstamp(fs->super, s_mtime, time(NULL));
fs->super->s_state &= ~EXT2_VALID_FS;
ext2fs_mark_super_dirty(fs);
err = ext2fs_flush2(fs, 0);
if (err)
translate_error(fs, 0, err);
}
return ff;
}
static int stat_inode(ext2_filsys fs, ext2_ino_t ino, struct stat *statbuf)
{
struct ext2_inode_large inode;
dev_t fakedev = 0;
errcode_t err;
int ret = 0;
struct timespec tv;
memset(&inode, 0, sizeof(inode));
err = ext2fs_read_inode_full(fs, ino, (struct ext2_inode *)&inode,
sizeof(inode));
if (err)
return translate_error(fs, ino, err);
memcpy(&fakedev, fs->super->s_uuid, sizeof(fakedev));
statbuf->st_dev = fakedev;
statbuf->st_ino = ino;
statbuf->st_mode = inode.i_mode;
statbuf->st_nlink = inode.i_links_count;
statbuf->st_uid = inode_uid(inode);
statbuf->st_gid = inode_gid(inode);
statbuf->st_size = EXT2_I_SIZE(&inode);
statbuf->st_blksize = fs->blocksize;
statbuf->st_blocks = ext2fs_get_stat_i_blocks(fs,
(struct ext2_inode *)&inode);
EXT4_INODE_GET_XTIME(i_atime, &tv, &inode);
statbuf->st_atime = tv.tv_sec;
EXT4_INODE_GET_XTIME(i_mtime, &tv, &inode);
statbuf->st_mtime = tv.tv_sec;
EXT4_INODE_GET_XTIME(i_ctime, &tv, &inode);
statbuf->st_ctime = tv.tv_sec;
if (LINUX_S_ISCHR(inode.i_mode) ||
LINUX_S_ISBLK(inode.i_mode)) {
if (inode.i_block[0])
statbuf->st_rdev = inode.i_block[0];
else
statbuf->st_rdev = inode.i_block[1];
}
return ret;
}
static int op_getattr(const char *path, struct stat *statbuf
#if FUSE_VERSION >= FUSE_MAKE_VERSION(3, 0)
, struct fuse_file_info *fi EXT2FS_ATTR((unused))
#endif
)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
ext2_ino_t ino;
errcode_t err;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s\n", __func__, path);
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err) {
ret = translate_error(fs, 0, err);
goto out;
}
ret = stat_inode(fs, ino, statbuf);
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_readlink(const char *path, char *buf, size_t len)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
errcode_t err;
ext2_ino_t ino;
struct ext2_inode inode;
unsigned int got;
ext2_file_t file;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s\n", __func__, path);
pthread_mutex_lock(&ff->bfl);
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, path, &ino);
if (err || ino == 0) {
ret = translate_error(fs, 0, err);
goto out;
}
err = ext2fs_read_inode(fs, ino, &inode);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
if (!LINUX_S_ISLNK(inode.i_mode)) {
ret = -EINVAL;
goto out;
}
len--;
if (inode.i_size < len)
len = inode.i_size;
if (ext2fs_is_fast_symlink(&inode))
memcpy(buf, (char *)inode.i_block, len);
else {
/* big/inline symlink */
err = ext2fs_file_open(fs, ino, 0, &file);
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
err = ext2fs_file_read(file, buf, len, &got);
if (err || got != len) {
ext2fs_file_close(file);
ret = translate_error(fs, ino, err);
goto out2;
}
out2:
err = ext2fs_file_close(file);
if (ret)
goto out;
if (err) {
ret = translate_error(fs, ino, err);
goto out;
}
}
buf[len] = 0;
if (fs_writeable(fs)) {
ret = update_atime(fs, ino);
if (ret)
goto out;
}
out:
pthread_mutex_unlock(&ff->bfl);
return ret;
}
static int op_mknod(const char *path, mode_t mode, dev_t dev)
{
struct fuse_context *ctxt = fuse_get_context();
struct fuse2fs *ff = (struct fuse2fs *)ctxt->private_data;
ext2_filsys fs;
ext2_ino_t parent, child;
char *temp_path;
errcode_t err;
char *node_name, a;
int filetype;
struct ext2_inode_large inode;
int ret = 0;
FUSE2FS_CHECK_CONTEXT(ff);
fs = ff->fs;
dbg_printf("%s: path=%s mode=0%o dev=0x%x\n", __func__, path, mode,
(unsigned int)dev);
temp_path = strdup(path);
if (!temp_path) {
ret = -ENOMEM;
goto out;
}
node_name = strrchr(temp_path, '/');
if (!node_name) {
ret = -ENOMEM;
goto out;
}
node_name++;
a = *node_name;
*node_name = 0;
pthread_mutex_lock(&ff->bfl);
if (!fs_can_allocate(ff, 2)) {
ret = -ENOSPC;
goto out2;
}
err = ext2fs_namei(fs, EXT2_ROOT_INO, EXT2_ROOT_INO, temp_path,
&parent);
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
ret = check_inum_access(fs, parent, W_OK);
if (ret)
goto out2;
*node_name = a;
if (LINUX_S_ISCHR(mode))
filetype = EXT2_FT_CHRDEV;
else if (LINUX_S_ISBLK(mode))
filetype = EXT2_FT_BLKDEV;
else if (LINUX_S_ISFIFO(mode))
filetype = EXT2_FT_FIFO;
else if (LINUX_S_ISSOCK(mode))
filetype = EXT2_FT_SOCK;
else {
ret = -EINVAL;
goto out2;
}
err = ext2fs_new_inode(fs, parent, mode, 0, &child);
if (err) {
ret = translate_error(fs, 0, err);
goto out2;
}
dbg_printf("%s: create ino=%d/name=%s in dir=%d\n", __func__, child,
node_name, parent);
err = ext2fs_link(fs, parent, node_name, child, filetype);
if (err == EXT2_ET_DIR_NO_SPACE) {
err = ext2fs_expand_dir(fs, parent);
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}
err = ext2fs_link(fs, parent, node_name, child,
filetype);
}
if (err) {
ret = translate_error(fs, parent, err);
goto out2;
}