File: fsel.pl

package info (click to toggle)
libfuse-perl 0.16.1%2B20180422git6becd92d7fce3fc411d7c-4
  • links: PTS, VCS
  • area: main
  • in suites: bullseye, buster
  • size: 612 kB
  • sloc: perl: 1,938; makefile: 8
file content (208 lines) | stat: -rwxr-xr-x 5,534 bytes parent folder | download | duplicates (4)
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
#!/usr/bin/env perl

use strict;
no strict qw(refs);

use threads;
use threads::shared;

use Carp;
local $SIG{'__WARN__'} = \&Carp::cluck;

use Fuse qw(:all);
use Fcntl qw(:mode);
use POSIX;
use IO::Poll qw(POLLIN);
use Time::HiRes qw(sleep);
use Getopt::Long;

# $fsel_open_mask is used to limit the number of opens to 1 per file. This
# uses the file index (0-F) as $fh, as poll support requires a unique handle
# per open file. Lifting this would require more complete open file
# management.
my $fsel_open_mask :shared = 0;

# Maximum "file" size.
use constant FSEL_CNT_MAX   => 10;
use constant FSEL_FILES     => 16;

# Used only as a lock for $fsel_poll_notify_mask and @fsel_cnt.
my $fsel_mutex :shared;
# Mask indicating what FDs have poll notifications waiting.
my $fsel_poll_notify_mask :shared = 0;
# Poll notification handles.
my @fsel_poll_handle :shared;
# Number of bytes for each "file".
my @fsel_cnt :shared;
# Initialize all byte counts.
map { $fsel_cnt[$_] = 0 } (0 .. (FSEL_FILES - 1));

sub fsel_path_index {
    my ($path) = @_;
    print 'called ', (caller(0))[3], "\n";

    return -1 if $path !~ m{^/([0-9A-F])$};
    return hex($1);
}

sub fsel_getattr {
    my ($path) = @_;
    print 'called ', (caller(0))[3], "\n";
    my @stbuf = (0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0);

    if ($path eq '/') {
        @stbuf[2, 3] = (S_IFDIR | 0555, 2);
        return @stbuf;
    }

    my $idx = fsel_path_index($path);
    return -&ENOENT if $idx < 0;

    @stbuf[2, 3, 7] = (S_IFREG | 0444, 1, $fsel_cnt[$idx]);
    return @stbuf;
}

sub fsel_readdir {
    my ($path, $offset) = @_;
    print 'called ', (caller(0))[3], "\n";

    return -&ENOENT if $path ne '/';

    return('.', '..', map { sprintf('%X', $_) } (0 .. (FSEL_FILES - 1)), 0);
}

sub fsel_open {
    my ($path, $flags, $info) = @_;
    print 'called ', (caller(0))[3], "\n";

    my $idx = fsel_path_index($path);
    return -&ENOENT if $idx < 0;
    return -&EACCES if $flags & O_ACCMODE != O_RDONLY;
    return -&EBUSY  if $fsel_open_mask & (1 << $idx);
    $fsel_open_mask |= (1 << $idx);

    # fsel files are nonseekable somewhat pipe-like files which get filled
    # up periodically by the producer thread, and consumed on read. Tell
    # FUSE to do this right.
    @{$info}{'direct_io', 'nonseekable'} = (1, 1);
    return (0, $idx);
}

sub fsel_release {
    my ($path, $flags, $fh) = @_;
    print 'called ', (caller(0))[3], "\n";

    $fsel_open_mask &= ~(1 << $fh);
    return 0;
}

sub fsel_read {
    my ($path, $size, $offset, $fh) = @_;
    print 'called ', (caller(0))[3], "\n";
    lock($fsel_mutex);

    if ($fsel_cnt[$fh] < $size) {
        $size = $fsel_cnt[$fh];
    }
    printf("READ   \%X transferred=\%u cnt=\%u\n", $fh, $size, $fsel_cnt[$fh]);
    $fsel_cnt[$fh] -= $size;

    return(chr($fh) x $size);
}

our $polled_zero :shared = 0;

sub fsel_poll {
    my ($path, $ph, $revents, $fh) = @_;
    print 'called ', (caller(0))[3], ", path = \"$path\", fh = $fh, revents = $revents\n";

    lock($fsel_mutex);

    if ($ph) {
        my $oldph = $fsel_poll_handle[$fh];
        pollhandle_destroy($oldph) if $oldph;
        $fsel_poll_notify_mask |= (1 << $fh);
        $fsel_poll_handle[$fh] = $ph;
    }

    if ($fsel_cnt[$fh]) {
        $revents |= POLLIN;
        printf("POLL   \%X cnt=\%u polled_zero=\%u\n", $fh, $fsel_cnt[$fh],
                $polled_zero);
        $polled_zero = 0;
    }
    else {
        $polled_zero++;
    }

    return(0, $revents);
}

sub fsel_producer {
    print 'called ', (caller(0))[3], "\n";
    local $SIG{'KILL'} = sub { threads->exit(); };
    my ($tv, $idx, $nr) = (0.25, 0, 1);

    while (1) {
        {
            my ($i, $t);
            lock($fsel_mutex);

            # This is the main producer loop which is executed every 250
            # msec. On each iteration, it adds one byte to 1, 2 or 4 files
            # and sends a poll notification if a poll handle is present.
            for (($i, $t) = (0, $idx); $i < $nr; $i++,
                    $t = (($t + int(FSEL_FILES / $nr)) % FSEL_FILES)) {
                next if $fsel_cnt[$t] == FSEL_CNT_MAX;

                $fsel_cnt[$t]++;
                if ($fsel_poll_notify_mask & (1 << $t)) {
                    printf("NOTIFY \%X\n", $t);
                    my $ph = $fsel_poll_handle[$t];
                    notify_poll($ph);
                    pollhandle_destroy($ph);
                    $fsel_poll_notify_mask &= ~(1 << $t);
                    $fsel_poll_handle[$t] = undef;
                }
            }

            $idx = ($idx + 1) % FSEL_FILES;
            if ($idx == 0) {
                # Cycle through 1, 2 and 4.
                $nr = ($nr * 2) % 7;
            }
        }

        sleep($tv);
    }
}

croak("Fuse doesn't have poll") unless Fuse::fuse_version() >= 2.8;

my %fuseargs = (
    'getattr'   => 'main::fsel_getattr',
    'readdir'   => 'main::fsel_readdir',
    'open'      => 'main::fsel_open',
    'release'   => 'main::fsel_release',
    'read'      => 'main::fsel_read',
    'poll'      => 'main::fsel_poll',
);

GetOptions(
    'use-threads'       => sub {
        print STDERR "Warning: Fuse currently has bugs related to threading which may cause misbehavior\n";
        $fuseargs{'threaded'} = 1;
    },
    'debug'             => sub {
        $fuseargs{'debug'} = 1;
    }
) || croak("Malformed options passed");

$fuseargs{'mountpoint'} = $ARGV[0];

my $thread = threads->create(\&fsel_producer);

Fuse::main(%fuseargs);

$thread->kill('KILL');
$thread->join();