-
Notifications
You must be signed in to change notification settings - Fork 5
/
esfs.c
322 lines (269 loc) · 9.06 KB
/
esfs.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
/*
This file is part of ESFS, a FUSE-based filesystem that supports snapshots.
ESFS is Copyright (C) 2013 Elod Csirmaz
<http://www.epcsirmaz.com/> <https://github.com/csirmaz>.
ESFS is based on Big Brother File System (fuse-tutorial)
Copyright (C) 2012 Joseph J. Pfeiffer, Jr., Ph.D. <pfeiffer@cs.nmsu.edu>,
and was forked from it on 21 August 2013.
Big Brother File System can be distributed under the terms of
the GNU GPLv3. See the file COPYING.
See also <http://www.cs.nmsu.edu/~pfeiffer/fuse-tutorial/>.
Big Brother File System was derived from function prototypes found in
/usr/include/fuse/fuse.h
Copyright (C) 2001-2007 Miklos Szeredi <miklos@szeredi.hu>
fuse.h is licensed under the LGPLv2.
ESFS is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free Software
Foundation, either version 3 of the License, or (at your option) any later
version.
ESFS is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
details.
You should have received a copy of the GNU General Public License along
with this program. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* NOTE: A Perl script is used to replace $ with esfs_ and $$ with ESFS_
* in this file. To write $, use \$.
*/
#include "params_c.h"
#include <ctype.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h> // AT_FDCWD
#include <fuse.h>
#include <libgen.h>
#include <limits.h> // PATH_MAX
#include <stdlib.h>
#include <stdio.h>
#include <stdarg.h> // va_list, &c.
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/stat.h> // utimens
#include <sys/select.h> // pselect
#if $$DEBUG > 0
# include <sys/syscall.h> // for gettid only
#endif
#include <pthread.h> // mutexes
#include <ftw.h> // nftw
#include "types_c.h"
#include "util_c.c"
#include "mflock_c.c"
#include "util_locking_c.c"
#include "snapshot_c.c"
#include "mfd_c.c"
#include "block_c.c"
#include "fuse_fd_close_c.c"
#include "fuse_fd_read_c.c"
#include "fuse_fd_write_c.c"
#include "fuse_path_open_c.c"
#include "fuse_path_read_c.c"
#include "fuse_path_write_c.c"
///////////////////////////////////////////////////////////
//
// Prototypes for all these functions, and the C-style comments,
// come from /usr/include/fuse.h
/** Get file system statistics
*
* The 'f_frsize', 'f_favail', 'f_fsid' and 'f_flag' fields are ignored
*
* Replaced 'struct statfs' parameter with 'struct statvfs' in
* version 2.5
*/
// int (*statfs) (const char *, struct statvfs *);
int $statfs(const char *path, struct statvfs *statv)
{
$$IF_PATH_MAIN_ONLY
$dlogdbg("statfs(path=\"%s\")\n", path);
// get stats for underlying filesystem
if(statvfs(fpath, statv) == 0) { return 0; }
return -errno;
// log_statvfs(statv);
}
/**
* Initialize filesystem
*
* The return value will passed in the private_data field of
* fuse_context to all file operations and as a parameter to the
* destroy() method.
*
* Introduced in version 2.3
* Changed in version 2.6
*/
// void *(*init) (struct fuse_conn_info *conn);
// Undocumented but extraordinarily useful fact: the fuse_context is
// set up before this function is called, and
// fuse_get_context()->private_data returns the user_data passed to
// fuse_main(). Really seems like either it should be a third
// parameter coming in here, or else the fact should be documented
// (and this might as well return void, as it did in older versions of
// FUSE).
void *$init(struct fuse_conn_info *conn)
{
struct $fsdata_t *fsdata;
fsdata = ((struct $fsdata_t *) fuse_get_context()->private_data);
$dlogi("Initialised ESFS\n");
return fsdata;
}
/**
* Clean up filesystem
*
* Called on filesystem exit.
*
* Introduced in version 2.3
*/
// void (*destroy) (void *userdata==private_data);
void $destroy(void *privdata)
{
// TODO free all memory
struct $fsdata_t *fsdata;
fsdata = ((struct $fsdata_t *) privdata);
$mflock_destroy(fsdata);
$b_destroy_block_buffer(fsdata);
$dlogi("Bye!\n");
#if $$DEBUG > 0
log_close(fsdata->logfile);
#endif
free(fsdata->rootdir);
free(fsdata);
}
struct fuse_operations $oper = {
.getattr = $getattr,
.readlink = $readlink,
.mknod = $mknod,
.mkdir = $mkdir,
.unlink = $unlink,
.rmdir = $rmdir,
.symlink = $symlink,
.rename = $rename,
.link = $link,
.chmod = $chmod,
.chown = $chown,
.truncate = $truncate,
.open = $open,
.read = $read,
.write = $write,
.statfs = $statfs,
.flush = $flush,
.release = $release,
.fsync = $fsync,
.setxattr = $setxattr,
.getxattr = $getxattr,
.listxattr = $listxattr,
.removexattr = $removexattr,
.opendir = $opendir,
.readdir = $readdir,
.releasedir = $releasedir,
.fsyncdir = $fsyncdir,
.init = $init,
.destroy = $destroy,
.access = $access,
.create = $create,
.ftruncate = $ftruncate,
.fgetattr = $fgetattr,
.utimens = $utimens
};
void $usage(void)
{
fprintf(stderr, "USAGE: esfs [ FUSE and mount options ] [--local-log] (RootDir) (MountPoint)\n\n");
}
int main(int argc, char *argv[])
{
int ret;
int local_log = 0;
struct $fsdata_t *fsdata;
// The FS doesn't do any access checking on its own (the comment
// blocks in fuse.h mention some of the functions that need
// accesses checked -- but note there are other functions, like
// chown(), that also need checking!). Since running bbfs as root
// will therefore open holes in the system
// security, we'll check if root is trying to mount the filesystem
// and refuse if it is. The somewhat smaller hole of an ordinary
// user doing it with the allow_other flag is still there because
// we don't parse the options string.
if((getuid() == 0) || (geteuid() == 0)) {
fprintf(stderr, "Running ESFS as root opens security holes. Aborting.\n");
return 1;
}
if((ret = $check_params()) != 0) {
fprintf(stderr, "There's a problem with the parameters; ESFS needs to be recompiled. Code = %d. Aborting.\n", ret);
return 1;
}
// If the first argument is "-h", call fuse to print the FUSE and mount options
if(argc > 1 && strcmp(argv[1], "-h") == 0) {
for(ret = 2; ret < argc; ret++) { argv[ret] = NULL; }
argc = 2;
$usage();
fprintf(stderr, "FUSE AND MOUNT OPTIONS:\n\n");
fuse_main(argc, argv, NULL, NULL);
return 1;
}
// Perform some sanity checking on the command line: make sure
// there are enough arguments, and that neither of the last two
// start with a hyphen (this will break if you actually have a
// rootpoint or mountpoint whose name starts with a hyphen)
if((argc < 3) || (argv[argc - 2][0] == '-') || (argv[argc - 1][0] == '-')) {
$usage();
return 1;
}
fsdata = malloc(sizeof(struct $fsdata_t));
if(fsdata == NULL) {
fprintf(stderr, "Out of memory. Aborting.\n");
return 1;
}
// Pull the rootdir out of the argument list and save it in the internal data
fsdata->rootdir = realpath(argv[argc - 2], NULL); // this calls malloc
if(fsdata->rootdir == NULL) {
fprintf(stderr, "Error getting the root directory from '%s'. Aborting.\n", argv[argc - 2]);
return 1;
}
fsdata->rootdir_len = strlen(fsdata->rootdir);
fsdata->sn_number = 0;
argv[argc - 2] = argv[argc - 1];
argv[argc - 1] = NULL;
argc--;
// Pull the optional local-log argument out of the argument list
if(strcmp(argv[argc - 2], "--local-log") == 0) {
local_log = 1;
argv[argc - 2] = argv[argc - 1];
argv[argc - 1] = NULL;
argc--;
}
#if $$DEBUG > 0
if((fsdata->logfile = log_open(local_log == 0 ? "/var/log/esfs.log" : "esfs.log")) == NULL) {
fprintf(stderr, "Could not open the logfile at '%s'. Aborting.\n", (local_log == 0 ? "/var/log/esfs.log" : "esfs.log"));
return 1;
}
#endif
// Initial things to do
// Get the main snapshot dir path
if($map_path(fsdata->sn_dir, $$SNDIR, fsdata) != 0) {
fprintf(stderr, "Snapshot directory path is too long. Aborting.\n");
return 1;
}
if($sn_check_dir(fsdata) != 0) {
fprintf(stderr, "Snapshot directory check failed, please check the logs. Aborting.\n");
return 1;
}
if($sn_get_latest(fsdata) != 0) {
fprintf(stderr, "Getting the latest snapshot failed, please check the logs. Aborting.\n");
return 1;
}
if($b_init_block_buffer(fsdata) != 0){
fprintf(stderr, "Failed to initialise the global block buffer. Aborting.\n");
return 1;
}
if($mflock_init(fsdata) != 0) {
fprintf(stderr, "Failed to initialise the mutexes. Aborting.\n");
return 1;
}
// turn over control to fuse
// user_data user data supplied in the context during the init() method
// Returns: 0 on success, nonzero on failure
if((ret = fuse_main(argc, argv, &$oper, fsdata)) != 0) {
fprintf(stderr, "FUSE returned with error '%d'. Aborting.\n", ret);
}
return ret;
}