Navigation Menu

Skip to content

Commit

Permalink
NFSv4: Add support for ftruncate()
Browse files Browse the repository at this point in the history
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
  • Loading branch information
sahlberg committed Aug 25, 2017
1 parent 80d3f48 commit 73b8129
Show file tree
Hide file tree
Showing 8 changed files with 203 additions and 15 deletions.
2 changes: 2 additions & 0 deletions include/libnfs-private.h
Expand Up @@ -482,6 +482,8 @@ int nfs4_fstat64_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
void *private_data);
int nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
void *private_data);
int nfs4_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
uint64_t length, nfs_cb cb, void *private_data);
int nfs4_link_async(struct nfs_context *nfs, const char *oldpath,
const char *newpath, nfs_cb cb, void *private_data);
int nfs4_mkdir2_async(struct nfs_context *nfs, const char *path, int mode,
Expand Down
3 changes: 2 additions & 1 deletion lib/libnfs-sync.c
Expand Up @@ -702,7 +702,8 @@ nfs_ftruncate(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t length)

if (nfs_ftruncate_async(nfs, nfsfh, length, ftruncate_cb,
&cb_data) != 0) {
nfs_set_error(nfs, "nfs_ftruncate_async failed");
nfs_set_error(nfs, "nfs_ftruncate_async failed. %s",
nfs_get_error(nfs));
return -1;
}

Expand Down
7 changes: 5 additions & 2 deletions lib/libnfs.c
Expand Up @@ -1141,9 +1141,12 @@ nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
case NFS_V3:
return nfs3_ftruncate_async(nfs, nfsfh, length,
cb, private_data);
case NFS_V4:
return nfs4_ftruncate_async(nfs, nfsfh, length,
cb, private_data);
default:
nfs_set_error(nfs, "%s does not support NFSv4",
__FUNCTION__);
nfs_set_error(nfs, "%s does not support NFSv%d",
__FUNCTION__, nfs->version);
return -1;
}
}
Expand Down
66 changes: 57 additions & 9 deletions lib/nfs_v4.c
Expand Up @@ -655,8 +655,8 @@ nfs4_op_open_confirm(struct nfs_context *nfs, nfs_argop4 *op, uint32_t seqid,
}

static int
nfs4_op_setattr(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh,
void *sabuf)
nfs4_op_truncate(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh,
void *sabuf)
{
SETATTR4args *saargs;
static uint32_t mask[2] = {1 << (FATTR4_SIZE),
Expand Down Expand Up @@ -1750,7 +1750,7 @@ nfs4_open_truncate_cb(struct rpc_context *rpc, int status, void *command_data,
}

i = nfs4_op_putfh(nfs, op, fh);
i += nfs4_op_setattr(nfs, &op[i], fh, data->filler.blob3.val);
i += nfs4_op_truncate(nfs, &op[i], fh, data->filler.blob3.val);

memset(&args, 0, sizeof(args));
args.argarray.argarray_len = i;
Expand Down Expand Up @@ -3432,7 +3432,7 @@ nfs4_truncate_open_cb(struct rpc_context *rpc, int status, void *command_data,
}

i = nfs4_op_putfh(nfs, &op[0], fh);
i += nfs4_op_setattr(nfs, &op[i], fh, data->filler.blob3.val);
i += nfs4_op_truncate(nfs, &op[i], fh, data->filler.blob3.val);
i += nfs4_op_close(nfs, &op[i], fh);

memset(&args, 0, sizeof(args));
Expand Down Expand Up @@ -3509,11 +3509,11 @@ nfs4_fsync_cb(struct rpc_context *rpc, int status, void *command_data,
}

int
nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *fh, nfs_cb cb,
void *private_data)
{
COMPOUND4args args;
nfs_argop4 op[3];
nfs_argop4 op[2];
struct nfs4_cb_data *data;
int i;

Expand All @@ -3530,11 +3530,59 @@ nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,

memset(op, 0, sizeof(op));

i = nfs4_op_putfh(nfs, &op[0], nfsfh);
i = nfs4_op_putfh(nfs, &op[0], fh);
i += nfs4_op_commit(nfs, &op[i]);

data->filler.blob0.val = nfsfh;
data->filler.blob0.free = (blob_free)nfs_free_nfsfh;
memset(&args, 0, sizeof(args));
args.argarray.argarray_len = i;
args.argarray.argarray_val = op;

if (rpc_nfs4_compound_async(nfs->rpc, nfs4_fsync_cb, &args,
data) != 0) {
data->filler.blob0.val = NULL;
free_nfs4_cb_data(data);
return -1;
}

return 0;
}

int
nfs4_ftruncate_async(struct nfs_context *nfs, struct nfsfh *fh,
uint64_t length, nfs_cb cb, void *private_data)
{
COMPOUND4args args;
nfs_argop4 op[2];
struct nfs4_cb_data *data;
int i;

data = malloc(sizeof(*data));
if (data == NULL) {
nfs_set_error(nfs, "Out of memory.");
return -1;
}
memset(data, 0, sizeof(*data));

data->nfs = nfs;
data->cb = cb;
data->private_data = private_data;

data->filler.blob3.val = malloc(12);
if (data->filler.blob3.val == NULL) {
nfs_set_error(nfs, "Out of memory");
free_nfs4_cb_data(data);
return -1;
}
data->filler.blob3.free = free;

memset(data->filler.blob3.val, 0, 12);
length = nfs_hton64(length);
memcpy(data->filler.blob3.val, &length, sizeof(uint64_t));

memset(op, 0, sizeof(op));

i = nfs4_op_putfh(nfs, &op[0], fh);
i += nfs4_op_truncate(nfs, &op[i], fh, data->filler.blob3.val);

memset(&args, 0, sizeof(args));
args.argarray.argarray_len = i;
Expand Down
6 changes: 3 additions & 3 deletions tests/Makefile.am
Expand Up @@ -4,9 +4,9 @@ AM_CPPFLAGS = -I${srcdir}/../include -I${srcdir}/../include/nfsc \
AM_CFLAGS = $(WARN_CFLAGS)
LDADD = ../lib/libnfs.la

noinst_PROGRAMS = prog_create prog_fstat prog_link prog_lstat prog_mkdir \
prog_mknod prog_mount prog_open_read prog_open_write prog_rename \
prog_rmdir prog_stat prog_symlink prog_timeout prog_unlink
noinst_PROGRAMS = prog_create prog_fstat prog_ftruncate prog_link prog_lstat \
prog_mkdir prog_mknod prog_mount prog_open_read prog_open_write \
prog_rename prog_rmdir prog_stat prog_symlink prog_timeout prog_unlink

EXTRA_PROGRAMS = ld_timeout
CLEANFILES = ld_timeout.o ld_timeout.so
Expand Down
92 changes: 92 additions & 0 deletions tests/prog_ftruncate.c
@@ -0,0 +1,92 @@
/* -*- mode:c; tab-width:8; c-basic-offset:8; indent-tabs-mode:nil; -*- */
/*
Copyright (C) by Ronnie Sahlberg <ronniesahlberg@gmail.com> 2017
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

#include <fcntl.h>
#include <inttypes.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <string.h>
#include <sys/types.h>

#include "libnfs.h"

void usage(void)
{
fprintf(stderr, "Usage: prog_ftruncate <url> <cwd> <path> <offset>\n");
exit(1);
}

int main(int argc, char *argv[])
{
struct nfs_context *nfs;
struct nfsfh *nfsfh;
struct nfs_url *url;
uint64_t length;

if (argc != 5) {
usage();
}

length = strtol(argv[4], NULL, 10);

nfs = nfs_init_context();
if (nfs == NULL) {
printf("failed to init context\n");
exit(1);
}

url = nfs_parse_url_full(nfs, argv[1]);
if (url == NULL) {
fprintf(stderr, "%s\n", nfs_get_error(nfs));
exit(1);
}

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

if (nfs_chdir(nfs, argv[2]) != 0) {
fprintf(stderr, "Failed to chdir to \"%s\" : %s\n",
argv[2], nfs_get_error(nfs));
exit(1);
}

if (nfs_open(nfs, argv[3], O_WRONLY, &nfsfh)) {
fprintf(stderr, "Failed to open file : %s\n",
nfs_get_error(nfs));
exit(1);
}

if (nfs_ftruncate(nfs, nfsfh, length)) {
fprintf(stderr, "Failed to ftruncate file : %s\n",
nfs_get_error(nfs));
exit(1);
}

nfs_destroy_url(url);
nfs_close(nfs, nfsfh);
nfs_destroy_context(nfs);

return 0;
}
25 changes: 25 additions & 0 deletions tests/test_0330_ftruncate.sh
@@ -0,0 +1,25 @@
#!/bin/sh

. ./functions.sh

echo "NFSv${VERS} Basic nfs_ftruncate() test."

start_share

dd if=/dev/zero of=testdata/testfile count=1 bs=32768 2>/dev/null

echo -n "test nfs_ftruncate() ... "
./prog_ftruncate "${TESTURL}/?version=${VERS}" "." testfile 12377 || failure
success

echo -n "test nfs_fstat64() ... "
./prog_fstat "${TESTURL}/?version=${VERS}" "." testfile > "${TESTDIR}/output" || failure
success

echo -n "verify nfs_size ... "
grep "nfs_size:12377" "${TESTDIR}/output" >/dev/null || failure
success

stop_share

exit 0
17 changes: 17 additions & 0 deletions tests/test_0332_ftruncate_valgrind_leak_check.sh
@@ -0,0 +1,17 @@
#!/bin/sh

. ./functions.sh

echo "NFSv${VERS} Basic valgrind leak check for nfs_ftruncate()."

start_share

dd if=/dev/zero of=testdata/testfile count=1 bs=32768 2>/dev/null

echo -n "test nfs_ftruncate() (1) ... "
libtool --mode=execute valgrind --leak-check=full --error-exitcode=99 ./prog_ftruncate "${TESTURL}/?version=${VERS}" "." testfile 12377 >/dev/null 2>&1 || failure
success

stop_share

exit 0

0 comments on commit 73b8129

Please sign in to comment.