From 73b81297e75425cd0385d3b7564b619492a13d39 Mon Sep 17 00:00:00 2001 From: Ronnie Sahlberg Date: Sat, 26 Aug 2017 01:31:25 +1000 Subject: [PATCH] NFSv4: Add support for ftruncate() Signed-off-by: Ronnie Sahlberg --- include/libnfs-private.h | 2 + lib/libnfs-sync.c | 3 +- lib/libnfs.c | 7 +- lib/nfs_v4.c | 66 +++++++++++-- tests/Makefile.am | 6 +- tests/prog_ftruncate.c | 92 +++++++++++++++++++ tests/test_0330_ftruncate.sh | 25 +++++ ...test_0332_ftruncate_valgrind_leak_check.sh | 17 ++++ 8 files changed, 203 insertions(+), 15 deletions(-) create mode 100644 tests/prog_ftruncate.c create mode 100755 tests/test_0330_ftruncate.sh create mode 100755 tests/test_0332_ftruncate_valgrind_leak_check.sh diff --git a/include/libnfs-private.h b/include/libnfs-private.h index 6414baa0..a7e2978b 100644 --- a/include/libnfs-private.h +++ b/include/libnfs-private.h @@ -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, diff --git a/lib/libnfs-sync.c b/lib/libnfs-sync.c index 935b4d02..c659824a 100644 --- a/lib/libnfs-sync.c +++ b/lib/libnfs-sync.c @@ -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; } diff --git a/lib/libnfs.c b/lib/libnfs.c index 99d4475d..24c49f7e 100755 --- a/lib/libnfs.c +++ b/lib/libnfs.c @@ -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; } } diff --git a/lib/nfs_v4.c b/lib/nfs_v4.c index 7c3dc4e5..0a93ebbb 100644 --- a/lib/nfs_v4.c +++ b/lib/nfs_v4.c @@ -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), @@ -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; @@ -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)); @@ -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; @@ -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; diff --git a/tests/Makefile.am b/tests/Makefile.am index a46e0aad..2f3b8bdc 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -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 diff --git a/tests/prog_ftruncate.c b/tests/prog_ftruncate.c new file mode 100644 index 00000000..05a2aaeb --- /dev/null +++ b/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 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 . +*/ + +#define _FILE_OFFSET_BITS 64 +#define _GNU_SOURCE + +#include +#include +#include +#include +#include +#include +#include + +#include "libnfs.h" + +void usage(void) +{ + fprintf(stderr, "Usage: prog_ftruncate \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; +} diff --git a/tests/test_0330_ftruncate.sh b/tests/test_0330_ftruncate.sh new file mode 100755 index 00000000..0c13c301 --- /dev/null +++ b/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 diff --git a/tests/test_0332_ftruncate_valgrind_leak_check.sh b/tests/test_0332_ftruncate_valgrind_leak_check.sh new file mode 100755 index 00000000..e11d9dbc --- /dev/null +++ b/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