forked from vasi/squashfuse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ll.c
585 lines (514 loc) · 14.1 KB
/
ll.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
/*
* Copyright (c) 2012 Dave Vasilevsky <[email protected]>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR(S) ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "ll.h"
#include "fuseprivate.h"
#include "stat.h"
#include "nonstd.h"
#include <errno.h>
#include <float.h>
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <signal.h>
#include <unistd.h>
static const double SQFS_TIMEOUT = DBL_MAX;
/* See comment near alarm_tick for details of how idle timeouts are
managed. */
/* timeout, in seconds, after which we will automatically unmount */
static unsigned int idle_timeout_secs = 0;
/* last access timestamp */
static time_t last_access = 0;
/* count of files and directories currently open. drecement after
* last_access for correctness. */
static sig_atomic_t open_refcount = 0;
/* same as lib/fuse_signals.c */
static struct fuse_session *fuse_instance = NULL;
static void update_access_time(void) {
#ifdef SQFS_MULTITHREADED
/* We only need to track access time if we have an idle timeout,
* don't bother with expensive operations if idle_timeout is 0.
*/
if (idle_timeout_secs) {
time_t now = time(NULL);
__atomic_store_n(&last_access, now, __ATOMIC_RELEASE);
}
#else
last_access = time(NULL);
#endif
}
static void update_open_refcount(int delta) {
#ifdef SQFS_MULTITHREADED
__atomic_fetch_add(&open_refcount, delta, __ATOMIC_RELEASE);
#else
open_refcount += delta;
#endif
}
static inline time_t get_access_time(void) {
#ifdef SQFS_MULTITHREADED
return __atomic_load_n(&last_access, __ATOMIC_ACQUIRE);
#else
return last_access;
#endif
}
static inline sig_atomic_t get_open_refcount(void) {
#ifdef SQFS_MULTITHREADED
return __atomic_load_n(&open_refcount, __ATOMIC_ACQUIRE);
#else
return open_refcount;
#endif
}
void sqfs_ll_op_getattr(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi) {
sqfs_ll_i lli;
struct stat st;
update_access_time();
if (sqfs_ll_iget(req, &lli, ino))
return;
if (sqfs_stat(&lli.ll->fs, &lli.inode, &st)) {
fuse_reply_err(req, ENOENT);
} else {
st.st_ino = ino;
fuse_reply_attr(req, &st, SQFS_TIMEOUT);
}
}
void sqfs_ll_op_opendir(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi) {
sqfs_ll_i *lli;
update_access_time();
fi->fh = (intptr_t)NULL;
lli = malloc(sizeof(*lli));
if (!lli) {
fuse_reply_err(req, ENOMEM);
return;
}
if (sqfs_ll_iget(req, lli, ino) == SQFS_OK) {
if (!S_ISDIR(lli->inode.base.mode)) {
fuse_reply_err(req, ENOTDIR);
} else {
fi->fh = (intptr_t)lli;
update_open_refcount(1);
fuse_reply_open(req, fi);
return;
}
}
free(lli);
}
void sqfs_ll_op_create(fuse_req_t req, fuse_ino_t parent, const char *name,
mode_t mode, struct fuse_file_info *fi) {
update_access_time();
fuse_reply_err(req, EROFS);
}
void sqfs_ll_op_releasedir(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi) {
update_access_time();
update_open_refcount(-1);
free((sqfs_ll_i*)(intptr_t)fi->fh);
fuse_reply_err(req, 0); /* yes, this is necessary */
}
size_t sqfs_ll_add_direntry(fuse_req_t req, char *buf, size_t bufsize,
const char *name, const struct stat *st, off_t off) {
#if HAVE_DECL_FUSE_ADD_DIRENTRY
return fuse_add_direntry(req, buf, bufsize, name, st, off);
#else
size_t esize = fuse_dirent_size(strlen(name));
if (bufsize >= esize)
fuse_add_dirent(buf, name, st, off);
return esize;
#endif
}
void sqfs_ll_op_readdir(fuse_req_t req, fuse_ino_t ino, size_t size,
off_t off, struct fuse_file_info *fi) {
sqfs_err sqerr;
sqfs_dir dir;
sqfs_name namebuf;
sqfs_dir_entry entry;
size_t esize;
struct stat st;
char *buf = NULL, *bufpos = NULL;
sqfs_ll_i *lli = (sqfs_ll_i*)(intptr_t)fi->fh;
int err = 0;
update_access_time();
if (sqfs_dir_open(&lli->ll->fs, &lli->inode, &dir, off))
err = EINVAL;
if (!err && !(bufpos = buf = malloc(size)))
err = ENOMEM;
if (!err) {
memset(&st, 0, sizeof(st));
sqfs_dentry_init(&entry, namebuf);
while (sqfs_dir_next(&lli->ll->fs, &dir, &entry, &sqerr)) {
st.st_ino = lli->ll->ino_fuse_num(lli->ll, &entry);
st.st_mode = sqfs_dentry_mode(&entry);
esize = sqfs_ll_add_direntry(req, bufpos, size, sqfs_dentry_name(&entry),
&st, sqfs_dentry_next_offset(&entry));
if (esize > size)
break;
bufpos += esize;
size -= esize;
}
if (sqerr)
err = EIO;
}
if (err)
fuse_reply_err(req, err);
else
fuse_reply_buf(req, buf, bufpos - buf);
free(buf);
}
void sqfs_ll_op_lookup(fuse_req_t req, fuse_ino_t parent,
const char *name) {
sqfs_ll_i lli;
sqfs_err sqerr;
sqfs_name namebuf;
sqfs_dir_entry entry;
bool found;
sqfs_inode inode;
update_access_time();
if (sqfs_ll_iget(req, &lli, parent))
return;
if (!S_ISDIR(lli.inode.base.mode)) {
fuse_reply_err(req, ENOTDIR);
return;
}
sqfs_dentry_init(&entry, namebuf);
sqerr = sqfs_dir_lookup(&lli.ll->fs, &lli.inode, name, strlen(name), &entry,
&found);
if (sqerr) {
fuse_reply_err(req, EIO);
return;
}
if (!found) {
/* Returning with zero inode indicates not found with
* timeout, i.e. future lookups of this name will not generate
* fuse requests.
*/
struct fuse_entry_param fentry;
memset(&fentry, 0, sizeof(fentry));
fentry.attr_timeout = fentry.entry_timeout = SQFS_TIMEOUT;
fentry.ino = 0;
fuse_reply_entry(req, &fentry);
return;
}
if (sqfs_inode_get(&lli.ll->fs, &inode, sqfs_dentry_inode(&entry))) {
fuse_reply_err(req, ENOENT);
} else {
struct fuse_entry_param fentry;
memset(&fentry, 0, sizeof(fentry));
if (sqfs_stat(&lli.ll->fs, &inode, &fentry.attr)) {
fuse_reply_err(req, EIO);
} else {
fentry.attr_timeout = fentry.entry_timeout = SQFS_TIMEOUT;
fentry.ino = lli.ll->ino_register(lli.ll, &entry);
fentry.attr.st_ino = fentry.ino;
fuse_reply_entry(req, &fentry);
}
}
}
void sqfs_ll_op_open(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi) {
sqfs_inode *inode;
sqfs_ll *ll;
update_access_time();
if (fi->flags & (O_WRONLY | O_RDWR)) {
fuse_reply_err(req, EROFS);
return;
}
inode = malloc(sizeof(sqfs_inode));
if (!inode) {
fuse_reply_err(req, ENOMEM);
return;
}
ll = fuse_req_userdata(req);
if (sqfs_ll_inode(ll, inode, ino)) {
fuse_reply_err(req, ENOENT);
} else if (!S_ISREG(inode->base.mode)) {
fuse_reply_err(req, EISDIR);
} else {
fi->fh = (intptr_t)inode;
fi->keep_cache = 1;
update_open_refcount(1);
fuse_reply_open(req, fi);
return;
}
free(inode);
}
void sqfs_ll_op_release(fuse_req_t req, fuse_ino_t ino,
struct fuse_file_info *fi) {
free((sqfs_inode*)(intptr_t)fi->fh);
fi->fh = 0;
update_access_time();
update_open_refcount(-1);
fuse_reply_err(req, 0);
}
void sqfs_ll_op_read(fuse_req_t req, fuse_ino_t ino,
size_t size, off_t off, struct fuse_file_info *fi) {
sqfs_ll *ll = fuse_req_userdata(req);
sqfs_inode *inode = (sqfs_inode*)(intptr_t)fi->fh;
sqfs_err err = SQFS_OK;
off_t osize;
char *buf = malloc(size);
if (!buf) {
fuse_reply_err(req, ENOMEM);
return;
}
update_access_time();
osize = size;
err = sqfs_read_range(&ll->fs, inode, off, &osize, buf);
if (err) {
fuse_reply_err(req, EIO);
} else if (osize == 0) { /* EOF */
fuse_reply_buf(req, NULL, 0);
} else {
fuse_reply_buf(req, buf, osize);
}
free(buf);
}
void sqfs_ll_op_readlink(fuse_req_t req, fuse_ino_t ino) {
char *dst;
size_t size;
sqfs_ll_i lli;
update_access_time();
if (sqfs_ll_iget(req, &lli, ino))
return;
if (!S_ISLNK(lli.inode.base.mode)) {
fuse_reply_err(req, EINVAL);
} else if (sqfs_readlink(&lli.ll->fs, &lli.inode, NULL, &size)) {
fuse_reply_err(req, EIO);
} else if (!(dst = malloc(size + 1))) {
fuse_reply_err(req, ENOMEM);
} else if (sqfs_readlink(&lli.ll->fs, &lli.inode, dst, &size)) {
fuse_reply_err(req, EIO);
free(dst);
} else {
fuse_reply_readlink(req, dst);
free(dst);
}
}
void sqfs_ll_op_listxattr(fuse_req_t req, fuse_ino_t ino, size_t size) {
sqfs_ll_i lli;
char *buf;
int ferr;
update_access_time();
if (sqfs_ll_iget(req, &lli, ino))
return;
buf = NULL;
if (size && !(buf = malloc(size))) {
fuse_reply_err(req, ENOMEM);
return;
}
ferr = sqfs_listxattr(&lli.ll->fs, &lli.inode, buf, &size);
if (ferr) {
fuse_reply_err(req, ferr);
} else if (buf) {
fuse_reply_buf(req, buf, size);
} else {
fuse_reply_xattr(req, size);
}
free(buf);
}
void sqfs_ll_op_getxattr(fuse_req_t req, fuse_ino_t ino,
const char *name, size_t size
#ifdef FUSE_XATTR_POSITION
, uint32_t position
#endif
) {
sqfs_ll_i lli;
char *buf = NULL;
size_t real = size;
#ifdef FUSE_XATTR_POSITION
if (position != 0) { /* We don't support resource forks */
fuse_reply_err(req, EINVAL);
return;
}
#endif
update_access_time();
if (sqfs_ll_iget(req, &lli, ino))
return;
if (!(buf = malloc(size)))
fuse_reply_err(req, ENOMEM);
else if (sqfs_xattr_lookup(&lli.ll->fs, &lli.inode, name, buf, &real))
fuse_reply_err(req, EIO);
else if (real == 0)
fuse_reply_err(req, sqfs_enoattr());
else if (size == 0)
fuse_reply_xattr(req, real);
else if (size < real)
fuse_reply_err(req, ERANGE);
else
fuse_reply_buf(req, buf, real);
free(buf);
}
void sqfs_ll_op_forget(fuse_req_t req, fuse_ino_t ino,
unsigned long nlookup) {
sqfs_ll_i lli;
update_access_time();
sqfs_ll_iget(req, &lli, SQFS_FUSE_INODE_NONE);
lli.ll->ino_forget(lli.ll, ino, nlookup);
fuse_reply_none(req);
}
void stfs_ll_op_statfs(fuse_req_t req, fuse_ino_t ino) {
sqfs_ll *ll;
struct statvfs st;
int err;
ll = fuse_req_userdata(req);
err = sqfs_statfs(&ll->fs, &st);
if (err == 0) {
fuse_reply_statfs(req, &st);
} else {
fuse_reply_err(req, err);
}
}
/* Helpers to abstract out FUSE 2.5 vs 3.0+ differences */
#if FUSE_USE_VERSION >= 30
sqfs_err sqfs_ll_mount(
sqfs_ll_chan *ch,
const char *mountpoint,
struct fuse_args *args,
struct fuse_lowlevel_ops *ops,
size_t ops_size,
void *userdata) {
ch->session = fuse_session_new(args, ops, ops_size, userdata);
if (!ch->session) {
return SQFS_ERR;
}
if (fuse_session_mount(ch->session, mountpoint)) {
fuse_session_destroy(ch->session);
ch->session = NULL;
return SQFS_ERR;
}
return SQFS_OK;
}
void sqfs_ll_unmount(sqfs_ll_chan *ch, const char *mountpoint) {
fuse_session_unmount(ch->session);
fuse_session_destroy(ch->session);
ch->session = NULL;
}
#else /* FUSE_USE_VERSION >= 30 */
void sqfs_ll_unmount(sqfs_ll_chan *ch, const char *mountpoint) {
if (ch->session) {
#if HAVE_DECL_FUSE_SESSION_REMOVE_CHAN
fuse_session_remove_chan(ch->ch);
#endif
fuse_session_destroy(ch->session);
}
#ifdef HAVE_NEW_FUSE_UNMOUNT
fuse_unmount(mountpoint, ch->ch);
#else
close(ch->fd);
fuse_unmount(mountpoint);
#endif
}
sqfs_err sqfs_ll_mount(
sqfs_ll_chan *ch,
const char *mountpoint,
struct fuse_args *args,
struct fuse_lowlevel_ops *ops,
size_t ops_size,
void *userdata) {
#ifdef HAVE_NEW_FUSE_UNMOUNT
ch->ch = fuse_mount(mountpoint, args);
if (!ch->ch) {
return SQFS_ERR;
}
#else
ch->fd = fuse_mount(mountpoint, args);
if (ch->fd == -1)
return SQFS_ERR;
ch->ch = fuse_kern_chan_new(ch->fd);
if (!ch->ch) {
close(ch->fd);
return SQFS_ERR;
}
#endif
ch->session = fuse_lowlevel_new(args,
ops, sizeof(*ops), userdata);
if (!ch->session) {
sqfs_ll_unmount(ch, mountpoint);
return SQFS_ERR;
}
fuse_session_add_chan(ch->session, ch->ch);
return SQFS_OK;
}
#endif /* FUSE_USE_VERSION >= 30 */
/* Idle unmount timeout management is based on signal handling from
fuse (see set_one_signal_handler and exit_handler in libfuse's
lib/fuse_signals.c.
When an idle timeout is set, we use a one second alarm to check if
no activity has taken place within the idle window, as tracked by
last_access. We also maintain an open/opendir refcount so that
directories and files can be held open and unaccessed without
triggering the idle timeout.
*/
void alarm_tick(int sig) {
if (!fuse_instance || idle_timeout_secs == 0) {
return;
}
if (get_open_refcount() == 0 &&
time(NULL) - get_access_time() > idle_timeout_secs) {
/* Safely shutting down fuse in a cross-platform way is a dark art!
But just about any platform should stop on SIGINT, so do that */
kill(getpid(), SIGINT);
return;
}
alarm(1); /* always reset our alarm */
}
void setup_idle_timeout(struct fuse_session *se, unsigned int timeout_secs) {
idle_timeout_secs = timeout_secs;
update_access_time();
struct sigaction sa;
memset(&sa, 0, sizeof(struct sigaction));
sa.sa_handler = alarm_tick;
sigemptyset(&(sa.sa_mask));
sa.sa_flags = 0;
fuse_instance = se;
if (sigaction(SIGALRM, &sa, NULL) == -1) {
perror("fuse: cannot get old signal handler");
return;
}
alarm(1);
}
void teardown_idle_timeout() {
alarm(0);
fuse_instance = NULL;
}
sqfs_ll *sqfs_ll_open(const char *path, size_t offset, const char *subdir) {
sqfs_ll *ll;
ll = malloc(sizeof(*ll));
if (!ll) {
perror("Can't allocate memory");
} else {
memset(ll, 0, sizeof(*ll));
ll->fs.offset = offset;
if (sqfs_open_image(&ll->fs, path, offset, subdir) == SQFS_OK) {
if (sqfs_ll_init(ll))
fprintf(stderr, "Can't initialize this filesystem!\n");
else
return ll;
sqfs_destroy(&ll->fs);
}
free(ll);
}
return NULL;
}