nfs_pwrite_async: handle short writes
[deb_libnfs.git] / lib / libnfs.c
index 4f0650f41abbe9ccca78bfc7a01fece238b9e706..2da52e4368abc2c95e27df905c9b03f62dd1f6ed 100644 (file)
@@ -133,6 +133,7 @@ struct nfs_cb_data {
        int num_calls;
        uint64_t start_offset, max_offset;
        char *buffer;
+       char *usrbuf;
 };
 
 struct nfs_mcb_data {
@@ -1560,6 +1561,14 @@ static void nfs_pread_cb(struct rpc_context *rpc, int status, void *command_data
        free_nfs_cb_data(data);
 }
 
+static void nfs_fill_READ3args(READ3args *args, struct nfsfh *fh, uint64_t offset, uint64_t count)
+{
+       memset(args, 0, sizeof(READ3args));
+       args->file = fh->fh;
+       args->offset = offset;
+       args->count = count;
+}
+
 static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
 {
        struct nfs_mcb_data *mdata = private_data;
@@ -1590,7 +1599,7 @@ static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_dat
                        if (res->READ3res_u.resok.count > 0) {
                                if (res->READ3res_u.resok.count <= mdata->count) {
                                        memcpy(&data->buffer[mdata->offset - data->start_offset], res->READ3res_u.resok.data.data_val, res->READ3res_u.resok.count);
-                                       if ((unsigned)data->max_offset < mdata->offset + res->READ3res_u.resok.count) {
+                                       if (data->max_offset < mdata->offset + res->READ3res_u.resok.count) {
                                                data->max_offset = mdata->offset + res->READ3res_u.resok.count;
                                        }
                                } else {
@@ -1646,11 +1655,7 @@ int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offse
 
        if (count <= nfs_get_readmax(nfs)) {
                READ3args args;
-
-               memset(&args, 0, sizeof(READ3args));
-               args.file = nfsfh->fh;
-               args.offset = offset;
-               args.count = count;
+               nfs_fill_READ3args(&args, nfsfh, offset, count);
 
                if (rpc_nfs3_read_async(nfs->rpc, nfs_pread_cb, &args, data) != 0) {
                        rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
@@ -1700,10 +1705,7 @@ int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offse
                mdata->offset = offset;
                mdata->count  = readcount;
 
-               memset(&args, 0, sizeof(READ3args));
-               args.file = nfsfh->fh;
-               args.offset = offset;
-               args.count = readcount;
+               nfs_fill_READ3args(&args, nfsfh, offset, readcount);
 
                if (rpc_nfs3_read_async(nfs->rpc, nfs_pread_mcb, &args, mdata) != 0) {
                        rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
@@ -1735,36 +1737,16 @@ int nfs_read_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t count,
 /*
  * Async pwrite()
  */
-static void nfs_pwrite_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
+static void nfs_fill_WRITE3args (WRITE3args *args, struct nfsfh *fh, uint64_t offset, uint64_t count,
+                                 void *buf)
 {
-       struct nfs_cb_data *data = private_data;
-       struct nfs_context *nfs = data->nfs;
-       WRITE3res *res;
-
-       assert(rpc->magic == RPC_CONTEXT_MAGIC);
-
-       if (status == RPC_STATUS_ERROR) {
-               data->cb(-EFAULT, nfs, command_data, data->private_data);
-               free_nfs_cb_data(data);
-               return;
-       }
-       if (status == RPC_STATUS_CANCEL) {
-               data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
-               free_nfs_cb_data(data);
-               return;
-       }
-
-       res = command_data;
-       if (res->status != NFS3_OK) {
-               rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
-               data->cb(nfsstat3_to_errno(res->status), nfs, rpc_get_error(nfs->rpc), data->private_data);
-               free_nfs_cb_data(data);
-               return;
-       }
-
-       data->nfsfh->offset += res->WRITE3res_u.resok.count;
-       data->cb(res->WRITE3res_u.resok.count, nfs, NULL, data->private_data);
-       free_nfs_cb_data(data);
+       memset(args, 0, sizeof(WRITE3args));
+       args->file = fh->fh;
+       args->offset = offset;
+       args->count  = count;
+       args->stable = fh->is_sync?FILE_SYNC:UNSTABLE;
+       args->data.data_len = count;
+       args->data.data_val = buf;
 }
 
 static void nfs_pwrite_mcb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
@@ -1793,30 +1775,49 @@ static void nfs_pwrite_mcb(struct rpc_context *rpc, int status, void *command_da
                        rpc_set_error(nfs->rpc, "NFS: Write failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
                        data->error = 1;
                } else  {
+                       if (res->WRITE3res_u.resok.count < mdata->count) {
+                               if (res->WRITE3res_u.resok.count == 0) {
+                                       rpc_set_error(nfs->rpc, "NFS: Write failed. No bytes written!");
+                                       data->error = 1;
+                               } else {
+                                       /* reissue reminder of this write request */
+                                       WRITE3args args;
+                                       mdata->offset += res->WRITE3res_u.resok.count;
+                                       mdata->count -= res->WRITE3res_u.resok.count;
+                                       nfs_fill_WRITE3args(&args, data->nfsfh, mdata->offset, mdata->count,
+                                                                               &data->usrbuf[mdata->offset - data->start_offset]);
+                                       if (rpc_nfs3_write_async(nfs->rpc, nfs_pwrite_mcb, &args, mdata) == 0) {
+                                               data->num_calls++;
+                                               return;
+                                       } else {
+                                               rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path);
+                                               data->error = 1;
+                                       }
+                               }
+                       }
                        if (res->WRITE3res_u.resok.count > 0) {
-                               if ((unsigned)data->max_offset < mdata->offset + res->WRITE3res_u.resok.count) {
+                               if (data->max_offset < mdata->offset + res->WRITE3res_u.resok.count) {
                                        data->max_offset = mdata->offset + res->WRITE3res_u.resok.count;
                                }
                        }
                }
        }
 
+       free(mdata);
+
        if (data->num_calls > 0) {
                /* still waiting for more replies */
-               free(mdata);
                return;
        }
 
        if (data->error != 0) {
                data->cb(-EFAULT, nfs, command_data, data->private_data);
                free_nfs_cb_data(data);
-               free(mdata);
                return;
        }
        if (data->cancel != 0) {
                data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
                free_nfs_cb_data(data);
-               free(mdata);
                return;
        }
 
@@ -1824,7 +1825,6 @@ static void nfs_pwrite_mcb(struct rpc_context *rpc, int status, void *command_da
        data->cb(data->max_offset - data->start_offset, nfs, NULL, data->private_data);
 
        free_nfs_cb_data(data);
-       free(mdata);
 }
 
 
@@ -1842,31 +1842,11 @@ int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offs
        data->cb           = cb;
        data->private_data = private_data;
        data->nfsfh        = nfsfh;
+       data->usrbuf       = buf;
 
        nfsfh->offset = offset;
 
-       if (count <= nfs_get_writemax(nfs)) {
-               WRITE3args args;
-
-               memset(&args, 0, sizeof(WRITE3args));
-               args.file = nfsfh->fh;
-               args.offset = offset;
-               args.count  = count;
-               args.stable = nfsfh->is_sync?FILE_SYNC:UNSTABLE;
-               args.data.data_len = count;
-               args.data.data_val = buf;
-
-               if (rpc_nfs3_write_async(nfs->rpc, nfs_pwrite_cb, &args, data) != 0) {
-                       rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path);
-                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
-                       free_nfs_cb_data(data);
-                       return -1;
-               }
-               return 0;
-       }
-
        /* hello, clang-analyzer */
-       assert(count > 0);
        assert(data->num_calls == 0);
 
        /* trying to write more than maximum server write size, we has to chop it up into smaller
@@ -1876,7 +1856,7 @@ int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offs
        data->max_offset = offset;
        data->start_offset = offset;
 
-       while (count > 0) {
+       do {
                uint64_t writecount = count;
                struct nfs_mcb_data *mdata;
                WRITE3args args;
@@ -1897,13 +1877,7 @@ int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offs
                mdata->offset = offset;
                mdata->count  = writecount;
 
-               memset(&args, 0, sizeof(WRITE3args));
-               args.file = nfsfh->fh;
-               args.offset = offset;
-               args.count  = writecount;
-               args.stable = nfsfh->is_sync?FILE_SYNC:UNSTABLE;
-               args.data.data_len = writecount;
-               args.data.data_val = &buf[offset - data->start_offset];
+               nfs_fill_WRITE3args(&args, nfsfh, offset, writecount, &buf[offset - data->start_offset]);
 
                if (rpc_nfs3_write_async(nfs->rpc, nfs_pwrite_mcb, &args, mdata) != 0) {
                        rpc_set_error(nfs->rpc, "RPC error: Failed to send WRITE call for %s", data->path);
@@ -1918,7 +1892,7 @@ int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offs
                count               -= writecount;
                offset              += writecount;
                data->num_calls++;
-       }
+       } while (count > 0);
 
        return 0;
 }