Skip to content

Commit

Permalink
Add support for [p]readv[_async]
Browse files Browse the repository at this point in the history
Signed-off-by: Ronnie Sahlberg <[email protected]>
  • Loading branch information
sahlberg committed Dec 15, 2024
1 parent 9b3aced commit 2e7971e
Show file tree
Hide file tree
Showing 8 changed files with 464 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/Makefile.am
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
noinst_PROGRAMS = nfsclient-async nfsclient-raw nfsclient-sync \
nfsclient-bcast nfsclient-listservers nfs-fh nfs-io nfs-ln nfs4-cat \
portmap-client portmap-server nfs-writefile nfs-stats-cb \
nfs-nfsstat
nfs-nfsstat nfs-cat-preadv

if HAVE_TALLOC_TEVENT
noinst_PROGRAMS += nfs4-cat-talloc
Expand Down Expand Up @@ -39,6 +39,7 @@ nfs_io_LDADD = $(COMMON_LIBS)
nfs_ln_LDADD = $(COMMON_LIBS)
nfs4_cat_LDADD = $(COMMON_LIBS) -levent
nfs4_cat_talloc_LDADD = $(COMMON_LIBS) -ltevent -ltalloc
nfs_cat_preadv_LDADD = $(COMMON_LIBS)
portmap_client_LDADD = $(COMMON_LIBS)
portmap_server_LDADD = $(COMMON_LIBS) -levent
nfs_pthreads_example_LDADD = $(COMMON_LIBS)
Expand Down
223 changes: 223 additions & 0 deletions examples/nfs-cat-preadv.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,223 @@
/*
Copyright (C) by Ronnie Sahlberg <[email protected]> 2015
This program 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.
This program 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/>.
*/

#define _FILE_OFFSET_BITS 64
#define _GNU_SOURCE

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

#ifdef AROS
#include "aros_compat.h"
#endif


#ifdef WIN32
#include <win32/win32_compat.h>
#pragma comment(lib, "ws2_32.lib")
WSADATA wsaData;
#else
#include <sys/stat.h>
#include <string.h>
#endif

#ifdef HAVE_POLL_H
#include <poll.h>
#endif

#ifdef HAVE_UNISTD_H
#include <unistd.h>
#endif

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <fcntl.h>
#include "libnfs.h"
#include "libnfs-raw.h"
#include "libnfs-raw-mount.h"

struct file_context {
int fd;
struct nfs_context *nfs;
struct nfsfh *nfsfh;
struct nfs_url *url;
};

void usage(void)
{
fprintf(stderr, "Usage: nfs-cat-preadv <file>\n");
fprintf(stderr, "<file> cat an nfs file.\n");
exit(0);
}

static void
free_file_context(struct file_context *file_context)
{
if (file_context->fd != -1) {
close(file_context->fd);
}
if (file_context->nfsfh != NULL) {
nfs_close(file_context->nfs, file_context->nfsfh);
}
if (file_context->nfs != NULL) {
nfs_destroy_context(file_context->nfs);
}
nfs_destroy_url(file_context->url);
free(file_context);
}

static struct file_context *
open_file(const char *url, int flags)
{
struct file_context *file_context;

file_context = malloc(sizeof(struct file_context));
if (file_context == NULL) {
fprintf(stderr, "Failed to malloc file_context\n");
return NULL;
}
file_context->fd = -1;
file_context->nfs = NULL;
file_context->nfsfh = NULL;
file_context->url = NULL;

file_context->nfs = nfs_init_context();
if (file_context->nfs == NULL) {
fprintf(stderr, "failed to init context\n");
free_file_context(file_context);
return NULL;
}

file_context->url = nfs_parse_url_full(file_context->nfs, url);
if (file_context->url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}

if (nfs_mount(file_context->nfs, file_context->url->server,
file_context->url->path) != 0) {
fprintf(stderr, "Failed to mount nfs share : %s\n",
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}

if (flags == O_RDONLY) {
if (nfs_open(file_context->nfs, file_context->url->file, flags,
&file_context->nfsfh) != 0) {
fprintf(stderr, "Failed to open file %s: %s\n",
file_context->url->file,
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
} else {
if (nfs_creat(file_context->nfs, file_context->url->file, 0660,
&file_context->nfsfh) != 0) {
fprintf(stderr, "Failed to creat file %s: %s\n",
file_context->url->file,
nfs_get_error(file_context->nfs));
free_file_context(file_context);
return NULL;
}
}
return file_context;
}

#define BUFSIZE 10240
static char buf2[BUFSIZE];
static char buf1[BUFSIZE];
static char buf0[BUFSIZE];

int main(int argc, char *argv[])
{
struct file_context *nf;
struct nfs_stat_64 st;
uint64_t off;
int64_t count;
int i;

#ifdef WIN32
if (WSAStartup(MAKEWORD(2,2), &wsaData) != 0) {
printf("Failed to start Winsock2\n");
return 10;
}
#endif

#ifdef AROS
aros_init_socket();
#endif

if (argc < 2) {
usage();
}

nf = open_file(argv[1], O_RDONLY);
if (nf == NULL) {
fprintf(stderr, "Failed to open %s\n", argv[1]);
exit(10);
}
if (nfs_fstat64(nf->nfs, nf->nfsfh, &st) < 0) {
fprintf(stderr, "Failed to stat %s\n", argv[1]);
exit(10);
}

off = 0;
while (off < st.nfs_size) {
struct iovec iov[3];

iov[0].iov_base = buf0;
iov[0].iov_len = sizeof(buf0);
iov[1].iov_base = buf1;
iov[1].iov_len = sizeof(buf1);
iov[2].iov_base = buf2;
iov[2].iov_len = sizeof(buf2);
count = st.nfs_size - off;
if (count > 3 * BUFSIZE) {
count = BUFSIZE;
}

count = nfs_preadv(nf->nfs, nf->nfsfh, iov, 3, off);
if (count < 0) {
fprintf(stderr, "Failed to read from file\n");
free_file_context(nf);
return 10;
}
off += count;
for (i = 0; i < 3; i++) {
if (count >= iov[i].iov_len) {
fwrite(iov[i].iov_base, iov[i].iov_len, 1, stdout);
count -= iov[i].iov_len;
} else {
fwrite(iov[i].iov_base, count, 1, stdout);
count = 0;
}
if (count == 0) {
break;
}
}
}

free_file_context(nf);

return 0;
}
6 changes: 6 additions & 0 deletions include/libnfs-private.h
Original file line number Diff line number Diff line change
Expand Up @@ -903,6 +903,9 @@ int nfs3_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb,
int nfs3_pread_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh,
void *buf, size_t count, uint64_t offset,
nfs_cb cb, void *private_data, int update_pos);
int nfs3_preadv_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh,
const struct iovec *iov, int iovcnt, uint64_t offset,
nfs_cb cb, void *private_data, int update_pos);
int nfs3_pwrite_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh,
const char *buf, size_t count, uint64_t offset,
nfs_cb cb, void *private_data, int update_pos);
Expand Down Expand Up @@ -985,6 +988,9 @@ int nfs4_opendir_async(struct nfs_context *nfs, const char *path, nfs_cb cb,
int nfs4_pread_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh,
void *buf, size_t count, uint64_t offset,
nfs_cb cb, void *private_data, int update_pos);
int nfs4_preadv_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh,
const struct iovec *iov, int iovcnt, uint64_t offset,
nfs_cb cb, void *private_data, int update_pos);
int nfs4_pwrite_async_internal(struct nfs_context *nfs, struct nfsfh *nfsfh,
uint64_t offset, size_t count, const char *buf,
nfs_cb cb, void *private_data, int update_pos);
Expand Down
62 changes: 62 additions & 0 deletions include/nfsc/libnfs.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,6 +701,7 @@ EXTERN int nfs_close_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
EXTERN int nfs_close(struct nfs_context *nfs, struct nfsfh *nfsfh);


struct iovec;
/*
* PREAD()
*/
Expand Down Expand Up @@ -731,6 +732,36 @@ EXTERN int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
EXTERN int nfs_pread(struct nfs_context *nfs, struct nfsfh *nfsfh,
void *buf, size_t count, uint64_t offset);

/*
* PREADV()
*/
/*
* Async preadv()
*
* Function returns
* 0 : The command was queued successfully. The callback will be invoked once
* the command completes.
* <0 : An error occured when trying to queue the command.
* The callback will not be invoked.
*
* When the callback is invoked, status indicates the result:
* >=0 : Success.
* status is numer of bytes read.
* -errno : An error occured.
* data is the error string.
*/
EXTERN int nfs_preadv_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
const struct iovec *iov, int iovcnt, uint64_t offset,
nfs_cb cb, void *private_data);
/*
* Sync preadv()
* Function returns
* >=0 : numer of bytes read.
* -errno : An error occured.
*/
EXTERN int nfs_preadv(struct nfs_context *nfs, struct nfsfh *nfsfh,
const struct iovec *iov, int iovcnt, uint64_t offset);



/*
Expand Down Expand Up @@ -763,6 +794,37 @@ EXTERN int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
EXTERN int nfs_read(struct nfs_context *nfs, struct nfsfh *nfsfh,
void *buf, size_t count);

/*
* READV()
*/
/*
* Async readv()
*
* Function returns
* 0 : The command was queued successfully. The callback will be invoked once
* the command completes.
* <0 : An error occured when trying to queue the command.
* The callback will not be invoked.
*
* When the callback is invoked, status indicates the result:
* >=0 : Success.
* status is numer of bytes read.
* -errno : An error occured.
* data is the error string.
*/
EXTERN int nfs_readv_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
const struct iovec *iov, int iovcnt,
nfs_cb cb, void *private_data);
/*
* Sync readv()
* Function returns
* >=0 : numer of bytes read.
* -errno : An error occured.
*/
EXTERN int nfs_readv(struct nfs_context *nfs, struct nfsfh *nfsfh,
const struct iovec *iov, int iovcnt,
void *buf, size_t count);

/*
* PWRITE()
*/
Expand Down
28 changes: 28 additions & 0 deletions lib/libnfs-sync.c
Original file line number Diff line number Diff line change
Expand Up @@ -699,6 +699,34 @@ nfs_pread(struct nfs_context *nfs, struct nfsfh *nfsfh,
return cb_data.status;
}

/*
* preadv()
*/
int
nfs_preadv(struct nfs_context *nfs, struct nfsfh *nfsfh,
const struct iovec *iov, int iovcnt, uint64_t offset)
{
struct sync_cb_data cb_data;

cb_data.call = "pread";
if (nfs_init_cb_data(&nfs, &cb_data)) {
return -1;
}

if (nfs_preadv_async(nfs, nfsfh, iov, iovcnt, offset,
pread_cb, &cb_data) != 0) {
nfs_set_error(nfs, "nfs_pread_async failed. %s",
nfs_get_error(nfs));
nfs_destroy_cb_sem(&cb_data);
return -1;
}

wait_for_nfs_reply(nfs, &cb_data);
nfs_destroy_cb_sem(&cb_data);

return cb_data.status;
}

/*
* read()
*/
Expand Down
Loading

0 comments on commit 2e7971e

Please sign in to comment.