Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
NFSv4: Add support for fsync()
Signed-off-by: Ronnie Sahlberg <ronniesahlberg@gmail.com>
  • Loading branch information
sahlberg committed Aug 25, 2017
1 parent ebc2f0a commit 80d3f48
Show file tree
Hide file tree
Showing 4 changed files with 87 additions and 8 deletions.
2 changes: 2 additions & 0 deletions include/libnfs-private.h
Expand Up @@ -480,6 +480,8 @@ int nfs4_create_async(struct nfs_context *nfs, const char *path, int flags,
int mode, nfs_cb cb, void *private_data);
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_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 @@ -664,7 +664,8 @@ nfs_fsync(struct nfs_context *nfs, struct nfsfh *nfsfh)
cb_data.is_finished = 0;

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

Expand Down
6 changes: 4 additions & 2 deletions lib/libnfs.c
Expand Up @@ -1124,9 +1124,11 @@ nfs_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
switch (nfs->version) {
case NFS_V3:
return nfs3_fsync_async(nfs, nfsfh, cb, private_data);
case NFS_V4:
return nfs4_fsync_async(nfs, nfsfh, 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
84 changes: 79 additions & 5 deletions lib/nfs_v4.c
Expand Up @@ -567,17 +567,26 @@ nfs4_op_create(struct nfs_context *nfs, nfs_argop4 *op, const char *name,
}

static int
nfs4_op_close(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh)
nfs4_op_commit(struct nfs_context *nfs, nfs_argop4 *op)
{
COMMIT4args *coargs;

op[0].argop = OP_COMMIT;
coargs = &op[0].nfs_argop4_u.opcommit;
coargs->offset = 0;
coargs->count = 0;

return 1;
}

static int
nfs4_op_close(struct nfs_context *nfs, nfs_argop4 *op, struct nfsfh *fh)
{
CLOSE4args *clargs;
int i = 0;

if (fh->is_dirty) {
op[i].argop = OP_COMMIT;
coargs = &op[i++].nfs_argop4_u.opcommit;
coargs->offset = 0;
coargs->count = 0;
i += nfs4_op_commit(nfs, &op[i]);
}

op[i].argop = OP_CLOSE;
Expand Down Expand Up @@ -3475,3 +3484,68 @@ nfs4_truncate_async(struct nfs_context *nfs, const char *path, uint64_t length,

return 0;
}

static void
nfs4_fsync_cb(struct rpc_context *rpc, int status, void *command_data,
void *private_data)
{
struct nfs4_cb_data *data = private_data;
struct nfs_context *nfs = data->nfs;
COMPOUND4res *res = command_data;

assert(rpc->magic == RPC_CONTEXT_MAGIC);

if (res) {
nfs_increment_seqid(nfs, res->status);
}

if (check_nfs4_error(nfs, status, data, res, "FSYNC")) {
free_nfs4_cb_data(data);
return;
}

data->cb(0, nfs, NULL, data->private_data);
free_nfs4_cb_data(data);
}

int
nfs4_fsync_async(struct nfs_context *nfs, struct nfsfh *nfsfh, nfs_cb cb,
void *private_data)
{
COMPOUND4args args;
nfs_argop4 op[3];
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;

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

i = nfs4_op_putfh(nfs, &op[0], nfsfh);
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;
}

0 comments on commit 80d3f48

Please sign in to comment.