Skip to content

Commit

Permalink
Implemented write_buf() wrapper. Still needs fuse_buf_copy().
Browse files Browse the repository at this point in the history
Made alterations to read_buf() wrapper to use an array, instead of a
single item, to accommodate different strategies.

Altered loopback.pl to support write_buf, and to use the modified
read_buf semantics.

Also changed open() use in daemonize() inside loopback.pl to 3-operand
calling syntax, because it's just the right thing to do.
  • Loading branch information
demonfoo committed Sep 4, 2013
1 parent 5e348a2 commit fd05cb5
Show file tree
Hide file tree
Showing 2 changed files with 122 additions and 45 deletions.
140 changes: 100 additions & 40 deletions Fuse.xs
Expand Up @@ -1579,14 +1579,70 @@ int _PLfuse_poll(const char *file, struct fuse_file_info *fi,
#if FUSE_VERSION >= 29
int _PLfuse_write_buf (const char *file, struct fuse_bufvec *buf, off_t off,
struct fuse_file_info *fi) {
croak("write_buf(): NOT IMPLEMENTED, DO NOT USE YET!");
int rv, i;
HV *bvhash;
AV *bvlist;
SV *sv;
#ifndef PERL_HAS_64BITINT
char *temp;
#endif
FUSE_CONTEXT_PRE;
DEBUGf("write_buf begin\n");
ENTER;
SAVETMPS;
PUSHMARK(SP);
XPUSHs(file ? sv_2mortal(newSVpv(file,0)) : &PL_sv_undef);
bvlist = newAV();
for (i = 0; i < buf->count; i++) {
bvhash = newHV();
sv = newSViv(buf->buf[i].size);
(void) hv_store(bvhash, "size", 4, sv, 0);
sv = newSViv(buf->buf[i].flags);
(void) hv_store(bvhash, "flags", 5, sv, 0);
sv = &PL_sv_undef;
if (buf->buf[i].mem) {
sv = newSV_type(SVt_PV);
SvPV_set(sv, (char *)buf->buf[i].mem);
SvLEN_set(sv, 0);
SvCUR_set(sv, buf->buf[i].size);
SvPOK_on(sv);
SvREADONLY_on(sv);
}
(void) hv_store(bvhash, "mem", 3, sv, 0);
sv = newSViv(buf->buf[i].fd);
(void) hv_store(bvhash, "fd", 2, sv, 0);
sv = newSViv(buf->buf[i].pos);
av_push(bvlist, newRV((SV *)bvhash));
}
XPUSHs(sv_2mortal(newRV_noinc((SV *)bvlist)));
#ifdef PERL_HAS_64BITINT
XPUSHs(sv_2mortal(newSViv(off)));
#else
if (asprintf(&temp, "%llu", off) == -1)
croak("Memory allocation failure!");
XPUSHs(sv_2mortal(newSVpv(temp, 0)));
free(temp);
#endif
XPUSHs(FH_GETHANDLE(fi));
PUTBACK;

rv = call_sv(MY_CXT.callback[41], G_SCALAR);
SPAGAIN;
rv = rv ? POPi : -ENOENT;

FREETMPS;
LEAVE;
PUTBACK;
DEBUGf("write_buf end: %i\n", rv);
FUSE_CONTEXT_POST;
return rv;
}

int _PLfuse_read_buf (const char *file, struct fuse_bufvec **bufp, size_t size,
off_t off, struct fuse_file_info *fi) {
int rv;
SV *readbuf;
HV *bvhash;
AV *bvlist;
struct fuse_bufvec *src;
#ifndef PERL_HAS_64BITINT
char *temp;
Expand All @@ -1606,17 +1662,15 @@ int _PLfuse_read_buf (const char *file, struct fuse_bufvec **bufp, size_t size,
XPUSHs(sv_2mortal(newSVpv(temp, 0)));
free(temp);
#endif
/* Create the 'readbuf' SV, and preallocate it to the requested read
* size; this should allow us to zero-copy safely. */
readbuf = newSVpv("", 0);
SvLEN_set(readbuf, size);
bvlist = newAV();
bvhash = newHV();
(void) hv_store(bvhash, "size", 4, newSViv(size), 0);
(void) hv_store(bvhash, "flags", 5, newSViv(0), 0);
(void) hv_store(bvhash, "mem", 3, readbuf, 0);
(void) hv_store(bvhash, "fd", 2, &PL_sv_undef, 0);
(void) hv_store(bvhash, "pos", 3, &PL_sv_undef, 0);
XPUSHs(sv_2mortal(newRV_noinc((SV*) bvhash)));
(void) hv_store(bvhash, "size", 4, newSViv(size), 0);
(void) hv_store(bvhash, "flags", 5, newSViv(0), 0);
(void) hv_store(bvhash, "mem", 3, newSVpv("", 0), 0);
(void) hv_store(bvhash, "fd", 2, newSViv(-1), 0);
(void) hv_store(bvhash, "pos", 3, newSViv(0), 0);
av_push(bvlist, newRV((SV *)bvhash));
XPUSHs(sv_2mortal(newRV_noinc((SV*) bvlist)));
XPUSHs(FH_GETHANDLE(fi));
PUTBACK;

Expand All @@ -1626,46 +1680,52 @@ int _PLfuse_read_buf (const char *file, struct fuse_bufvec **bufp, size_t size,
rv = -ENOENT;
else {
SV **svp;
int i;

rv = POPi;
if (rv)
goto READ_BUF_FAIL;

src = malloc(sizeof(struct fuse_bufvec));
src = malloc(sizeof(struct fuse_bufvec) +
(av_len(bvlist) * sizeof(struct fuse_buf)));
if (src == NULL)
croak("Memory allocation failure!");
*src = FUSE_BUFVEC_INIT(size);
if ((svp = hv_fetch(bvhash, "flags", 5, 0)) != NULL) {
src->buf[0].flags = SvIV(*svp);
}
if (src->buf[0].flags & FUSE_BUF_IS_FD) {
if ((svp = hv_fetch(bvhash, "fd", 2, 0)) != NULL) {
src->buf[0].fd = SvIV(*svp);
}
else
croak("FUSE_BUF_IS_FD passed but no fd!");
*src = FUSE_BUFVEC_INIT(0);
src->count = av_len(bvlist) + 1;
for (i = 0; i <= av_len(bvlist); i++) {
svp = av_fetch(bvlist, i, 1);
if (svp == NULL || *svp == NULL || !SvROK(*svp) ||
(bvhash = (HV *)SvRV(*svp)) == NULL ||
SvTYPE((SV *)bvhash) != SVt_PVHV)
croak("Entry provided as part of bufvec was wrong!");
if ((svp = hv_fetch(bvhash, "size", 4, 0)) != NULL)
src->buf[i].size = SvIV(*svp);
if ((svp = hv_fetch(bvhash, "flags", 5, 0)) != NULL)
src->buf[i].flags = SvIV(*svp);
if (src->buf[i].flags & FUSE_BUF_IS_FD) {
if ((svp = hv_fetch(bvhash, "fd", 2, 0)) != NULL)
src->buf[i].fd = SvIV(*svp);
else
croak("FUSE_BUF_IS_FD passed but no fd!");

if (src->buf[0].flags & FUSE_BUF_FD_SEEK) {
if ((svp = hv_fetch(bvhash, "pos", 3, 0)) != NULL) {
src->buf[0].fd = SvIV(*svp);
if (src->buf[i].flags & FUSE_BUF_FD_SEEK) {
if ((svp = hv_fetch(bvhash, "pos", 3, 0)) != NULL)
src->buf[i].fd = SvIV(*svp);
else
croak("FUSE_BUF_FD_SEEK passed but no pos!");
}
else
croak("FUSE_BUF_FD_SEEK passed but no pos!");
}
}
else {
if ((svp = hv_fetch(bvhash, "mem", 3, 0)) != NULL) {
src->buf[0].mem = SvPV_nolen(*svp);
/* Should keep Perl from free()ing the memory
* zone the SV points to, since it'll be
* free()'d elsewhere at (potentially) any
* time... */
SvLEN_set(*svp, 0);
else {
if ((svp = hv_fetch(bvhash, "mem", 3, 0)) != NULL) {
src->buf[i].mem = SvPV_nolen(*svp);
/* Should keep Perl from free()ing the memory
* zone the SV points to, since it'll be
* free()'d elsewhere at (potentially) any
* time... */
SvLEN_set(*svp, 0);
}
}
}
if ((svp = hv_fetch(bvhash, "size", 4, 0)) != NULL) {
src->buf[0].size = SvIV(*svp);
}
*bufp = src;
}

Expand Down
27 changes: 22 additions & 5 deletions examples/loopback.pl
Expand Up @@ -30,6 +30,7 @@
1;
} and do {
$use_lchown = 1;
Lchown->import();

This comment has been minimized.

Copy link
@demonfoo

demonfoo Sep 4, 2013

Author Collaborator

Oops, also added this (and the Unix::Mknod->import(); below. Turns out these didn't get exercised when run as non-root, and since they weren't imported, the subroutine calls ended up unresolved.

};

my $has_mknod = 0;
Expand All @@ -38,6 +39,7 @@
1;
} and do {
$has_mknod = 1;
Unix::Mknod->import();
};

use blib;
Expand Down Expand Up @@ -110,9 +112,8 @@ sub x_read_buf {
return -ENOENT() unless -e ($file = fixup($file));
my ($fsize) = -s $file;
return -ENOSYS() unless open($handle,$file);
print STDERR "x_read_buf(): test 1\n";
if(seek($handle,$off,SEEK_SET)) {
$bufvec->{'size'} = read($handle,$bufvec->{'mem'},$size);
$bufvec->[0]{'size'} = read($handle,$bufvec->[0]{'mem'},$size);
}
return $rv;
}
Expand All @@ -131,6 +132,21 @@ sub x_write {
return length($buf);
}

sub x_write_buf {
my ($file,$bufvec,$off) = @_;
my ($rv);
return -ENOENT() unless -e ($file = fixup($file));
my ($fsize) = -s $file;
return -ENOSYS() unless open(FILE,'+<',$file);
return -EBADF() unless $#$bufvec == 0 and !($bufvec->[0]{flags} & &Fuse::FUSE_BUF_IS_FD());
if($rv = seek(FILE,$off,SEEK_SET)) {
$rv = print(FILE $bufvec->[0]{mem});
}
$rv = -ENOSYS() unless $rv;
close(FILE);
return $rv;
}

sub err { return (-shift || -$!) }

sub x_readlink { return readlink(fixup(shift)); }
Expand Down Expand Up @@ -217,17 +233,17 @@ sub x_statfs {
# from http://perldoc.perl.org/perlipc.html#Complete-Dissociation-of-Child -from-Parent
sub daemonize {
chdir("/") || die "can't chdir to /: $!";
open(STDIN, "< /dev/null") || die "can't read /dev/null: $!";
open(STDIN, '<', '/dev/null') || die "can't read /dev/null: $!";
if ($logfile) {
open(STDOUT, '>', $logfile) || die "can't open logfile: $!";
}
else {
open(STDOUT, "> /dev/null") || die "can't write to /dev/null: $!";
open(STDOUT, '>', '/dev/null') || die "can't write to /dev/null: $!";
}
defined(my $pid = fork()) || die "can't fork: $!";
exit if $pid; # non-zero now means I am the parent
(setsid() != -1) || die "Can't start a new session: $!";
open(STDERR, ">&STDOUT") || die "can't dup stdout: $!";
open(STDERR, '>&', \*STDOUT) || die "can't dup stdout: $!";
if ($pidfile) {
open(PIDFILE, '>', $pidfile);
print PIDFILE $$, "\n";
Expand Down Expand Up @@ -275,6 +291,7 @@ sub daemonize {
'read' => 'main::x_read',
'read_buf' => 'main::x_read_buf',
'write' => 'main::x_write',
'write_buf' => 'main::x_write_buf',
'statfs' => 'main::x_statfs',
%extraopts,
);
Expand Down

0 comments on commit fd05cb5

Please sign in to comment.