-
Notifications
You must be signed in to change notification settings - Fork 2
/
heap_fs.cc
1712 lines (1388 loc) · 45.2 KB
/
heap_fs.cc
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
#include <cassert>
#include <cstring>
#include <iostream>
#include <stddef.h>
#if defined(__linux__)
#include <linux/limits.h>
#elif defined(__APPLE__)
#include <sys/syslimits.h>
#endif
#include <fuse.h>
#include <fuse_lowlevel.h>
#include <fuse_opt.h>
#include "filesystem.h"
#include "spdlog/sinks/stdout_color_sinks.h"
#include "spdlog/spdlog.h"
struct Extent {
Extent(size_t size)
: size(size)
, buf(new char[size]) {}
size_t size;
std::unique_ptr<char[]> buf;
};
struct FileSystem;
class Inode {
public:
Inode(
fuse_ino_t ino,
time_t time,
uid_t uid,
gid_t gid,
blksize_t blksize,
mode_t mode,
FileSystem* fs)
: ino(ino)
, fs_(fs) {
memset(&i_st, 0, sizeof(i_st));
i_st.st_ino = ino;
i_st.st_atime = time;
i_st.st_mtime = time;
i_st.st_ctime = time;
i_st.st_uid = uid;
i_st.st_gid = gid;
i_st.st_blksize = blksize;
}
virtual ~Inode() = 0;
const fuse_ino_t ino;
struct stat i_st;
bool is_regular() const;
bool is_directory() const;
bool is_symlink() const;
long int krefs = 0;
protected:
FileSystem* fs_;
};
class RegInode : public Inode {
public:
RegInode(
fuse_ino_t ino,
time_t time,
uid_t uid,
gid_t gid,
blksize_t blksize,
mode_t mode,
FileSystem* fs)
: Inode(ino, time, uid, gid, blksize, mode, fs) {
i_st.st_nlink = 1;
i_st.st_mode = S_IFREG | mode;
}
~RegInode();
std::map<off_t, Extent> extents_;
};
class DirInode : public Inode {
public:
typedef std::map<std::string, std::shared_ptr<Inode>> dir_t;
DirInode(
fuse_ino_t ino,
time_t time,
uid_t uid,
gid_t gid,
blksize_t blksize,
mode_t mode,
FileSystem* fs)
: Inode(ino, time, uid, gid, blksize, mode, fs) {
i_st.st_nlink = 2;
i_st.st_blocks = 1;
i_st.st_mode = S_IFDIR | mode;
}
dir_t dentries;
};
class SymlinkInode : public Inode {
public:
SymlinkInode(
fuse_ino_t ino,
time_t time,
uid_t uid,
gid_t gid,
blksize_t blksize,
const std::string& link,
FileSystem* fs)
: Inode(ino, time, uid, gid, blksize, 0, fs) {
i_st.st_mode = S_IFLNK;
i_st.st_size = link.length();
this->link = link;
}
std::string link;
};
class FileSystem : public filesystem_base {
public:
FileSystem(size_t size, const std::shared_ptr<spdlog::logger>& log);
FileSystem(const FileSystem& other) = delete;
FileSystem(FileSystem&& other) = delete;
~FileSystem() = default;
FileSystem& operator=(const FileSystem& other) = delete;
FileSystem& operator=(const FileSystem&& other) = delete;
public:
void destroy();
int lookup(fuse_ino_t parent_ino, const std::string& name, struct stat* st);
void forget(fuse_ino_t ino, long unsigned nlookup);
int statfs(fuse_ino_t ino, struct statvfs* stbuf);
// TODO: get rid of this method
void free_space(Extent* extent);
// inode operations
public:
int mknod(
fuse_ino_t parent_ino,
const std::string& name,
mode_t mode,
dev_t rdev,
struct stat* st,
uid_t uid,
gid_t gid);
int symlink(
const std::string& link,
fuse_ino_t parent_ino,
const std::string& name,
struct stat* st,
uid_t uid,
gid_t gid);
int link(
fuse_ino_t ino,
fuse_ino_t newparent_ino,
const std::string& newname,
struct stat* st,
uid_t uid,
gid_t gid);
int rename(
fuse_ino_t parent_ino,
const std::string& name,
fuse_ino_t newparent_ino,
const std::string& newname,
uid_t uid,
gid_t gid);
int unlink(
fuse_ino_t parent_ino, const std::string& name, uid_t uid, gid_t gid);
int access(fuse_ino_t ino, int mask, uid_t uid, gid_t gid);
int getattr(fuse_ino_t ino, struct stat* st, uid_t uid, gid_t gid);
int setattr(
fuse_ino_t ino,
FileHandle* fh,
struct stat* attr,
int to_set,
uid_t uid,
gid_t gid);
ssize_t
readlink(fuse_ino_t ino, char* path, size_t maxlen, uid_t uid, gid_t gid);
// directory operations
public:
int mkdir(
fuse_ino_t parent_ino,
const std::string& name,
mode_t mode,
struct stat* st,
uid_t uid,
gid_t gid);
int opendir(fuse_ino_t ino, int flags, uid_t uid, gid_t gid);
ssize_t readdir(
fuse_req_t req, fuse_ino_t ino, char* buf, size_t bufsize, off_t off);
int
rmdir(fuse_ino_t parent_ino, const std::string& name, uid_t uid, gid_t gid);
void releasedir(fuse_ino_t ino);
// file handle operation
public:
int create(
fuse_ino_t parent_ino,
const std::string& name,
mode_t mode,
int flags,
struct stat* st,
FileHandle** fhp,
uid_t uid,
gid_t gid);
int open(fuse_ino_t ino, int flags, FileHandle** fhp, uid_t uid, gid_t gid);
ssize_t write_buf(FileHandle* fh, struct fuse_bufvec* bufv, off_t off);
ssize_t read(FileHandle* fh, off_t offset, size_t size, char* buf);
void release(fuse_ino_t ino, FileHandle* fh);
private:
std::mutex mutex_;
std::shared_ptr<spdlog::logger> log_;
// inode management
private:
void add_inode(const std::shared_ptr<Inode>& inode);
void get_inode(const std::shared_ptr<Inode>& inode);
void put_inode(fuse_ino_t ino, long int dec);
std::shared_ptr<Inode> inode(fuse_ino_t ino) {
return inodes_.at(ino);
;
}
std::shared_ptr<DirInode> dir_inode(fuse_ino_t ino) {
auto in = inode(ino);
assert(in->is_directory());
return std::static_pointer_cast<DirInode>(in);
}
std::shared_ptr<SymlinkInode> symlink_inode(fuse_ino_t ino) {
auto in = inode(ino);
assert(in->is_symlink());
return std::static_pointer_cast<SymlinkInode>(in);
}
std::atomic<fuse_ino_t> next_ino_;
std::unordered_map<fuse_ino_t, std::shared_ptr<Inode>> inodes_;
// helpers
private:
// TODO: probably do not need to pass shared ptr here
ssize_t write(
const std::shared_ptr<RegInode>& in,
off_t offset,
size_t size,
const char* buf);
int
access(const std::shared_ptr<Inode>& in, int mask, uid_t uid, gid_t gid);
int truncate(
const std::shared_ptr<RegInode>& in, off_t newsize, uid_t uid, gid_t gid);
int allocate_space(
RegInode* in,
std::map<off_t, Extent>::iterator* it,
off_t offset,
size_t size,
bool upper_bound);
uint64_t nfiles() const;
struct statvfs stat;
size_t avail_bytes_;
};
struct FileHandle {
std::shared_ptr<RegInode> in;
int flags;
FileHandle(std::shared_ptr<RegInode> in, int flags)
: in(in)
, flags(flags) {}
};
FileSystem::FileSystem(size_t size, const std::shared_ptr<spdlog::logger>& log)
: log_(log)
, next_ino_(FUSE_ROOT_ID) {
auto now = std::time(nullptr);
auto root = std::make_shared<DirInode>(
next_ino_++, now, getuid(), getgid(), 4096, 0755, this);
add_inode(root);
avail_bytes_ = size;
memset(&stat, 0, sizeof(stat));
stat.f_fsid = 983983;
stat.f_namemax = PATH_MAX;
stat.f_bsize = 4096;
stat.f_frsize = 4096;
stat.f_blocks = size / 4096;
stat.f_files = 0;
stat.f_bfree = stat.f_blocks;
stat.f_bavail = stat.f_blocks;
if (size < 1ULL << 20) {
log_->info("creating {} byte file system", size);
} else if (size < 1ULL << 30) {
auto mbs = size / (1ULL << 20);
log_->info("creating {} mb file system", mbs);
} else if (size < 1ULL << 40) {
auto gbs = size / (1ULL << 30);
log_->info("creating {} gb file system", gbs);
} else if (size < 1ULL << 50) {
auto tbs = size / (1ULL << 40);
log_->info("creating {} tb file system", tbs);
} else {
assert(0 == "oh yeh?");
}
if (!next_ino_.is_lock_free()) {
log_->warn("inode number allocation may not be lock free");
}
}
void FileSystem::add_inode(const std::shared_ptr<Inode>& inode) {
assert(inode->krefs == 0);
inode->krefs++;
[[maybe_unused]] auto res = inodes_.emplace(inode->ino, inode);
assert(res.second); // check for duplicate ino
}
void FileSystem::get_inode(const std::shared_ptr<Inode>& inode) {
inode->krefs++;
auto res = inodes_.emplace(inode->ino, inode);
if (!res.second) {
assert(inode->krefs > 0);
} else {
assert(inode->krefs == 0);
}
}
void FileSystem::put_inode(fuse_ino_t ino, long int dec) {
auto it = inodes_.find(ino);
assert(it != inodes_.end());
assert(it->second->krefs > 0);
it->second->krefs -= dec;
assert(it->second->krefs >= 0);
if (it->second->krefs == 0) {
inodes_.erase(it);
}
}
uint64_t FileSystem::nfiles() const {
uint64_t ret = 0;
for (auto it = inodes_.begin(); it != inodes_.end(); it++)
if (it->second->i_st.st_mode & S_IFREG) ret++;
return ret;
}
void FileSystem::destroy() {
log_->info("shutting down file system");
// note that according to the fuse documentation when the file system is
// unmounted and shutdown all of the inode references implicitly drop to
// zero.
}
int FileSystem::create(
fuse_ino_t parent_ino,
const std::string& name,
mode_t mode,
int flags,
struct stat* st,
FileHandle** fhp,
uid_t uid,
gid_t gid) {
log_->debug(
"create parent {} name {} mode {} flags {} uid {} gid {}",
parent_ino,
name,
mode,
flags,
uid,
gid);
if (name.length() > NAME_MAX) {
log_->debug("create name {} too long", name);
return -ENAMETOOLONG;
}
auto now = std::time(nullptr);
auto in = std::make_shared<RegInode>(
next_ino_++, now, uid, gid, 4096, S_IFREG | mode, this);
auto fh = std::make_unique<FileHandle>(in, flags);
std::lock_guard<std::mutex> l(mutex_);
auto parent_in = dir_inode(parent_ino);
DirInode::dir_t& children = parent_in->dentries;
if (children.find(name) != children.end()) {
log_->debug("create name {} already exists", name);
return -EEXIST;
}
int ret = access(parent_in, W_OK, uid, gid);
if (ret) {
log_->debug("create name {} access denied ret {}", name, ret);
return ret;
}
children[name] = in;
add_inode(in);
parent_in->i_st.st_ctime = now;
parent_in->i_st.st_mtime = now;
*st = in->i_st;
*fhp = fh.release();
log_->debug("created name {} with ino {}", name, in->ino);
return 0;
}
int FileSystem::getattr(fuse_ino_t ino, struct stat* st, uid_t uid, gid_t gid) {
std::lock_guard<std::mutex> l(mutex_);
auto in = inode(ino);
*st = in->i_st;
return 0;
}
int FileSystem::unlink(
fuse_ino_t parent_ino, const std::string& name, uid_t uid, gid_t gid) {
std::lock_guard<std::mutex> l(mutex_);
auto parent_in = dir_inode(parent_ino);
DirInode::dir_t::const_iterator it = parent_in->dentries.find(name);
if (it == parent_in->dentries.end()) return -ENOENT;
int ret = access(parent_in, W_OK, uid, gid);
if (ret) return ret;
auto in = it->second;
// see unlink(2): EISDIR may be another case
if (in->is_directory()) return -EPERM;
if (parent_in->i_st.st_mode & S_ISVTX) {
if (uid && uid != in->i_st.st_uid && uid != parent_in->i_st.st_uid)
return -EPERM;
}
auto now = std::time(nullptr);
in->i_st.st_ctime = now;
in->i_st.st_nlink--;
parent_in->i_st.st_ctime = now;
parent_in->i_st.st_mtime = now;
parent_in->dentries.erase(it);
return 0;
}
int FileSystem::lookup(
fuse_ino_t parent_ino, const std::string& name, struct stat* st) {
std::lock_guard<std::mutex> l(mutex_);
// FIXME: should this be -ENOTDIR or -ENOENT in some cases?
auto parent_in = dir_inode(parent_ino);
DirInode::dir_t::const_iterator it = parent_in->dentries.find(name);
if (it == parent_in->dentries.end()) {
log_->debug("lookup parent {} name {} not found", parent_ino, name);
return -ENOENT;
}
auto in = it->second;
// bump kernel inode cache reference count
get_inode(in);
*st = in->i_st;
log_->debug("lookup parent {} name {} found {}", parent_ino, name, in->ino);
return 0;
}
int FileSystem::open(
fuse_ino_t ino, int flags, FileHandle** fhp, uid_t uid, gid_t gid) {
int mode = 0;
if ((flags & O_ACCMODE) == O_RDONLY)
mode = R_OK;
else if ((flags & O_ACCMODE) == O_WRONLY)
mode = W_OK;
else if ((flags & O_ACCMODE) == O_RDWR)
mode = R_OK | W_OK;
if (!(mode & W_OK) && (flags & O_TRUNC)) {
const int ret = -EACCES;
log_->debug(
"open ino {} flags {} uid {} gid {} ret {}",
ino,
flags,
uid,
gid,
ret);
return ret;
}
std::lock_guard<std::mutex> l(mutex_);
auto generic_in = inode(ino);
auto in = std::dynamic_pointer_cast<RegInode>(generic_in);
assert(in->is_regular());
auto fh = std::make_unique<FileHandle>(in, flags);
int ret = access(in, mode, uid, gid);
if (ret) {
log_->debug(
"open ino {} flags {} uid {} gid {} ret {}",
ino,
flags,
uid,
gid,
ret);
return ret;
}
if (flags & O_TRUNC) {
ret = truncate(in, 0, uid, gid);
if (ret) {
log_->debug(
"open ino {} flags {} uid {} gid {} ret {}",
ino,
flags,
uid,
gid,
ret);
return ret;
}
auto now = std::time(nullptr);
in->i_st.st_mtime = now;
in->i_st.st_ctime = now;
}
*fhp = fh.release();
log_->debug(
"open ino {} flags {} uid {} gid {} ret {}", ino, flags, uid, gid, 0);
return 0;
}
void FileSystem::release(fuse_ino_t ino, FileHandle* fh) {
log_->debug("release ino {} fh {}", ino, (void*)fh);
assert(fh);
delete fh;
}
void FileSystem::forget(fuse_ino_t ino, long unsigned nlookup) {
log_->debug("forget ino {} nlookup {}", ino, nlookup);
std::lock_guard<std::mutex> l(mutex_);
put_inode(ino, nlookup);
}
ssize_t
FileSystem::write_buf(FileHandle* fh, struct fuse_bufvec* bufv, off_t off) {
std::lock_guard<std::mutex> l(mutex_);
auto gen_in = fh->in;
assert(gen_in->is_regular());
auto in = std::dynamic_pointer_cast<RegInode>(gen_in);
size_t written = 0;
for (size_t i = bufv->idx; i < bufv->count; i++) {
struct fuse_buf* buf = bufv->buf + i;
assert(!(buf->flags & FUSE_BUF_IS_FD));
assert(!(buf->flags & FUSE_BUF_FD_RETRY));
assert(!(buf->flags & FUSE_BUF_FD_SEEK));
ssize_t ret;
if (i == bufv->idx) {
ret = write(
in, off, buf->size - bufv->off, (char*)buf->mem + bufv->off);
if (ret < 0) return ret;
assert(buf->size > bufv->off);
if (ret < (ssize_t)(buf->size - bufv->off)) return written;
} else {
ret = write(in, off, buf->size, (char*)buf->mem);
if (ret < 0) return ret;
if (ret < (ssize_t)buf->size) return written;
}
off += ret;
written += ret;
}
return written;
}
ssize_t FileSystem::read(FileHandle* fh, off_t offset, size_t size, char* buf) {
std::lock_guard<std::mutex> l(mutex_);
std::shared_ptr<RegInode> in = std::dynamic_pointer_cast<RegInode>(fh->in);
in->i_st.st_atime = std::time(nullptr);
// reads that start past eof return nothing
if (offset >= in->i_st.st_size || size == 0) return 0;
// clip the read so that it doesn't pass eof
size_t left;
if ((off_t)(offset + size) > in->i_st.st_size)
left = in->i_st.st_size - offset;
else
left = size;
const size_t new_size = left;
char* dst = buf;
/*
* find first segment that might intersect the read
*
* upper_bound(offset) will return a pointer to the first segment whose
* offset is greater (>) than the target offset. Thus, the immediately
* preceeding segment is the first that has an offset less than or equal to
* (<=) the offset which is what we are interested in.
*
* 1) it == begin(): can't move backward
* 2) it == end() / other: <= case described above
*/
auto it = in->extents_.upper_bound(offset);
if (it != in->extents_.begin()) { // not empty
assert(!in->extents_.empty());
--it;
} else if (it == in->extents_.end()) { // empty
assert(in->extents_.empty());
memset(dst, 0, new_size);
return new_size;
}
assert(it != in->extents_.end());
off_t seg_offset = it->first;
while (left) {
// size of movement this round
size_t done = 0;
// read starts before the current segment. return zeros up until the
// beginning of the segment or until we've completed the read.
if (offset < seg_offset) {
done = std::min(left, (size_t)(seg_offset - offset));
memset(dst, 0, done);
} else {
const auto& extent = it->second;
off_t seg_end_offset = seg_offset + extent.size;
// fixme: there may be a case here where the end of file lands
// inside an allocated extent, but logically it shoudl be returning
// zeros
// read starts within the current segment. return valid data up
// until the end of the segment or until we've completed the read.
if (offset < seg_end_offset) {
done = std::min(left, (size_t)(seg_end_offset - offset));
size_t blkoff = offset - seg_offset;
std::memcpy(dst, extent.buf.get() + blkoff, done);
} else if (++it == in->extents_.end()) {
seg_offset = offset + left;
assert(offset < seg_offset);
// assert that we'll be done
continue;
} else {
seg_offset = it->first;
continue;
}
}
dst += done;
offset += done;
left -= done;
}
return new_size;
}
int FileSystem::mkdir(
fuse_ino_t parent_ino,
const std::string& name,
mode_t mode,
struct stat* st,
uid_t uid,
gid_t gid) {
if (name.length() > NAME_MAX) {
const int ret = -ENAMETOOLONG;
log_->debug(
"mkdir parent {} name {} mode {} uid {} gid {} ret {}",
parent_ino,
name,
mode,
uid,
gid,
ret);
return ret;
}
auto now = std::time(nullptr);
auto in = std::make_shared<DirInode>(
next_ino_++, now, uid, gid, 4096, mode, this);
std::lock_guard<std::mutex> l(mutex_);
auto parent_in = dir_inode(parent_ino);
DirInode::dir_t& children = parent_in->dentries;
if (children.find(name) != children.end()) {
const int ret = -EEXIST;
log_->debug(
"mkdir parent {} name {} mode {} uid {} gid {} ret {}",
parent_ino,
name,
mode,
uid,
gid,
ret);
return ret;
}
int ret = access(parent_in, W_OK, uid, gid);
if (ret) {
log_->debug(
"mkdir parent {} name {} mode {} uid {} gid {} ret {}",
parent_ino,
name,
mode,
uid,
gid,
ret);
return ret;
}
children[name] = in;
add_inode(in);
parent_in->i_st.st_ctime = now;
parent_in->i_st.st_mtime = now;
parent_in->i_st.st_nlink++;
*st = in->i_st;
log_->debug(
"mkdir parent {} name {} mode {} uid {} gid {} ret {}",
parent_ino,
name,
mode,
uid,
gid,
0);
return 0;
}
int FileSystem::rmdir(
fuse_ino_t parent_ino, const std::string& name, uid_t uid, gid_t gid) {
std::lock_guard<std::mutex> l(mutex_);
auto parent_in = dir_inode(parent_ino);
DirInode::dir_t& children = parent_in->dentries;
DirInode::dir_t::const_iterator it = children.find(name);
if (it == children.end()) {
log_->debug(
"rmdir ENOENT parent {} name {} uid {} gid {}",
parent_ino,
name,
uid,
gid);
return -ENOENT;
}
if (!it->second->is_directory()) {
log_->debug(
"rmdir ENOTDIR parent {} name {} uid {} gid {}",
parent_ino,
name,
uid,
gid);
return -ENOTDIR;
}
auto in = std::static_pointer_cast<DirInode>(it->second);
if (in->dentries.size()) {
log_->debug(
"rmdir ENOTEMPTY parent {} name {} uid {} gid {}",
parent_ino,
name,
uid,
gid);
return -ENOTEMPTY;
}
if (parent_in->i_st.st_mode & S_ISVTX) {
if (uid && uid != in->i_st.st_uid && uid != parent_in->i_st.st_uid) {
log_->debug(
"rmdir EPERM parent {} name {} uid {} gid {}",
parent_ino,
name,
uid,
gid);
return -EPERM;
}
}
auto now = std::time(nullptr);
parent_in->i_st.st_mtime = now;
parent_in->i_st.st_ctime = now;
parent_in->dentries.erase(it);
parent_in->i_st.st_nlink--;
log_->debug(
"rmdir parent {} name {} uid {} gid {}", parent_ino, name, uid, gid);
return 0;
}
int FileSystem::rename(
fuse_ino_t parent_ino,
const std::string& name,
fuse_ino_t newparent_ino,
const std::string& newname,
uid_t uid,
gid_t gid) {
if (name.length() > NAME_MAX || newname.length() > NAME_MAX)
return -ENAMETOOLONG;
std::lock_guard<std::mutex> l(mutex_);
// old
auto parent_in = dir_inode(parent_ino);
DirInode::dir_t& parent_children = parent_in->dentries;
DirInode::dir_t::const_iterator old_it = parent_children.find(name);
if (old_it == parent_children.end()) return -ENOENT;
auto old_in = old_it->second;
assert(old_in);
// new
auto newparent_in = dir_inode(newparent_ino);
DirInode::dir_t& newparent_children = newparent_in->dentries;
DirInode::dir_t::const_iterator new_it = newparent_children.find(newname);
std::shared_ptr<Inode> new_in = NULL;
if (new_it != newparent_children.end()) {
new_in = new_it->second;
assert(new_in);
}
/*
* EACCES write permission is denied for the directory containing oldpath or
* newpath,
*
* (TODO) or search permission is denied for one of the directories in the
* path prefix of old‐ path or newpath,
*
* or oldpath is a directory and does not allow write permission (needed
* to update the .. entry). (See also path_resolution(7).) TODO: this is
* implemented but what is the affect on ".." update?
*/
int ret = access(parent_in, W_OK, uid, gid);
if (ret) return ret;
ret = access(newparent_in, W_OK, uid, gid);
if (ret) return ret;
if (old_in->i_st.st_mode & S_IFDIR) {
ret = access(old_in, W_OK, uid, gid);
if (ret) return ret;
}
/*
* EPERM or EACCES The directory containing oldpath has the sticky bit
* (S_ISVTX) set and the process's effective user ID is neither the user ID
* of the file to be deleted nor that of the directory containing it, and
* the process is not privileged (Linux: does not have the CAP_FOWNER
* capability);
*
* or newpath is an existing file and the directory containing it has the
* sticky bit set and the process's effective user ID is neither the user
* ID of the file to be replaced nor that of the directory containing
* it, and the process is not privileged (Linux: does not have the
* CAP_FOWNER capability);
*
* or the filesystem containing pathname does not support renaming of the
* type requested.
*/
if (parent_in->i_st.st_mode & S_ISVTX) {
if (uid && uid != old_in->i_st.st_uid && uid != parent_in->i_st.st_uid)
return -EPERM;
}
if (
new_in && newparent_in->i_st.st_mode & S_ISVTX && uid
&& uid != new_in->i_st.st_uid && uid != newparent_in->i_st.st_uid) {
return -EPERM;
}
if (new_in) {
if (old_in->i_st.st_mode & S_IFDIR) {
if (new_in->i_st.st_mode & S_IFDIR) {
DirInode::dir_t& new_children
= std::static_pointer_cast<DirInode>(new_in)->dentries;
if (new_children.size()) return -ENOTEMPTY;
} else
return -ENOTDIR;
} else {
if (new_in->i_st.st_mode & S_IFDIR) return -EISDIR;
}
newparent_children.erase(new_it);
}
old_in->i_st.st_ctime = std::time(nullptr);
newparent_children[newname] = old_it->second;
parent_children.erase(old_it);
return 0;
}
int FileSystem::setattr(
fuse_ino_t ino,
FileHandle* fh,
struct stat* attr,
int to_set,
uid_t uid,
gid_t gid) {
std::lock_guard<std::mutex> l(mutex_);
mode_t clear_mode = 0;
auto in = inode(ino);
auto now = std::time(nullptr);
if (to_set & FUSE_SET_ATTR_MODE) {
if (uid && in->i_st.st_uid != uid) return -EPERM;
if (uid && in->i_st.st_gid != gid) clear_mode |= S_ISGID;
in->i_st.st_mode = attr->st_mode;
}
if (to_set & (FUSE_SET_ATTR_UID | FUSE_SET_ATTR_GID)) {
/*
* Only a privileged process (Linux: one with the CAP_CHOWN
* capability) may change the owner of a file. The owner of a file may
* change the group of the file to any group of which that owner is a
* member. A privileged process (Linux: with CAP_CHOWN) may change the
* group arbitrarily.
*
* TODO: group membership for owner is not enforced.
*/
if (
uid && (to_set & FUSE_SET_ATTR_UID)
&& (in->i_st.st_uid != attr->st_uid))
return -EPERM;
if (uid && (to_set & FUSE_SET_ATTR_GID) && (uid != in->i_st.st_uid))
return -EPERM;
if (to_set & FUSE_SET_ATTR_UID) in->i_st.st_uid = attr->st_uid;
if (to_set & FUSE_SET_ATTR_GID) in->i_st.st_gid = attr->st_gid;