NFSv4: Add support for ftruncate()
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>libnfs-4.0.0-vitalif
parent
80d3f48199
commit
73b81297e7
|
@ -482,6 +482,8 @@ int nfs4_fstat64_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
|
||||||
void *private_data);
|
void *private_data);
|
||||||
int nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
|
int nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
|
||||||
void *private_data);
|
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,
|
int nfs4_link_async(struct nfs_context *nfs, const char *oldpath,
|
||||||
const char *newpath, nfs_cb cb, void *private_data);
|
const char *newpath, nfs_cb cb, void *private_data);
|
||||||
int nfs4_mkdir2_async(struct nfs_context *nfs, const char *path, int mode,
|
int nfs4_mkdir2_async(struct nfs_context *nfs, const char *path, int mode,
|
||||||
|
|
|
@ -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,
|
if (nfs_ftruncate_async(nfs, nfsfh, length, ftruncate_cb,
|
||||||
&cb_data) != 0) {
|
&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;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1141,9 +1141,12 @@ nfs_ftruncate_async(struct nfs_context *nfs, struct nfsfh *nfsfh,
|
||||||
case NFS_V3:
|
case NFS_V3:
|
||||||
return nfs3_ftruncate_async(nfs, nfsfh, length,
|
return nfs3_ftruncate_async(nfs, nfsfh, length,
|
||||||
cb, private_data);
|
cb, private_data);
|
||||||
|
case NFS_V4:
|
||||||
|
return nfs4_ftruncate_async(nfs, nfsfh, length,
|
||||||
|
cb, private_data);
|
||||||
default:
|
default:
|
||||||
nfs_set_error(nfs, "%s does not support NFSv4",
|
nfs_set_error(nfs, "%s does not support NFSv%d",
|
||||||
__FUNCTION__);
|
__FUNCTION__, nfs->version);
|
||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
68
lib/nfs_v4.c
68
lib/nfs_v4.c
|
@ -655,8 +655,8 @@ nfs4_op_open_confirm(struct nfs_context *nfs, nfs_argop4 *op, uint32_t seqid,
|
||||||
}
|
}
|
||||||
|
|
||||||
static int
|
static int
|
||||||
nfs4_op_setattr(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh,
|
nfs4_op_truncate(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh,
|
||||||
void *sabuf)
|
void *sabuf)
|
||||||
{
|
{
|
||||||
SETATTR4args *saargs;
|
SETATTR4args *saargs;
|
||||||
static uint32_t mask[2] = {1 << (FATTR4_SIZE),
|
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_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));
|
memset(&args, 0, sizeof(args));
|
||||||
args.argarray.argarray_len = i;
|
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_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);
|
i += nfs4_op_close(nfs, &op[i], fh);
|
||||||
|
|
||||||
memset(&args, 0, sizeof(args));
|
memset(&args, 0, sizeof(args));
|
||||||
|
@ -3509,11 +3509,11 @@ nfs4_fsync_cb(struct rpc_context *rpc, int status, void *command_data,
|
||||||
}
|
}
|
||||||
|
|
||||||
int
|
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)
|
void *private_data)
|
||||||
{
|
{
|
||||||
COMPOUND4args args;
|
COMPOUND4args args;
|
||||||
nfs_argop4 op[3];
|
nfs_argop4 op[2];
|
||||||
struct nfs4_cb_data *data;
|
struct nfs4_cb_data *data;
|
||||||
int i;
|
int i;
|
||||||
|
|
||||||
|
@ -3530,12 +3530,60 @@ nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
|
||||||
|
|
||||||
memset(op, 0, sizeof(op));
|
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]);
|
i += nfs4_op_commit(nfs, &op[i]);
|
||||||
|
|
||||||
data->filler.blob0.val = nfsfh;
|
memset(&args, 0, sizeof(args));
|
||||||
data->filler.blob0.free = (blob_free)nfs_free_nfsfh;
|
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));
|
memset(&args, 0, sizeof(args));
|
||||||
args.argarray.argarray_len = i;
|
args.argarray.argarray_len = i;
|
||||||
args.argarray.argarray_val = op;
|
args.argarray.argarray_val = op;
|
||||||
|
|
|
@ -4,9 +4,9 @@ AM_CPPFLAGS = -I${srcdir}/../include -I${srcdir}/../include/nfsc \
|
||||||
AM_CFLAGS = $(WARN_CFLAGS)
|
AM_CFLAGS = $(WARN_CFLAGS)
|
||||||
LDADD = ../lib/libnfs.la
|
LDADD = ../lib/libnfs.la
|
||||||
|
|
||||||
noinst_PROGRAMS = prog_create prog_fstat prog_link prog_lstat prog_mkdir \
|
noinst_PROGRAMS = prog_create prog_fstat prog_ftruncate prog_link prog_lstat \
|
||||||
prog_mknod prog_mount prog_open_read prog_open_write prog_rename \
|
prog_mkdir prog_mknod prog_mount prog_open_read prog_open_write \
|
||||||
prog_rmdir prog_stat prog_symlink prog_timeout prog_unlink
|
prog_rename prog_rmdir prog_stat prog_symlink prog_timeout prog_unlink
|
||||||
|
|
||||||
EXTRA_PROGRAMS = ld_timeout
|
EXTRA_PROGRAMS = ld_timeout
|
||||||
CLEANFILES = ld_timeout.o ld_timeout.so
|
CLEANFILES = ld_timeout.o ld_timeout.so
|
||||||
|
|
|
@ -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;
|
||||||
|
}
|
|
@ -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
|
|
@ -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
|
Loading…
Reference in New Issue