PORTMAP: Rename the functions in PMAP to PMAP2 to desribe the version of PMAP we...
[deb_libnfs.git] / lib / libnfs.c
index 937cb9d2ea59dde379cd57b3f63fca6f65f012fe..36a05a8e9ce562f4c376af48b8d24f4a7c78f9bc 100644 (file)
@@ -130,9 +130,12 @@ struct nfs_cb_data {
        /* for multi-read/write calls. */
        int error;
        int cancel;
+       int oom;
        int num_calls;
        uint64_t start_offset, max_offset;
        char *buffer;
+       size_t request_size;
+       char *usrbuf;
 };
 
 struct nfs_mcb_data {
@@ -466,7 +469,7 @@ static void rpc_connect_program_2_cb(struct rpc_context *rpc, int status, void *
                return;
        }
 
-       if (rpc_pmap_getport_async(rpc, data->program, data->version, IPPROTO_TCP, rpc_connect_program_3_cb, private_data) != 0) {
+       if (rpc_pmap2_getport_async(rpc, data->program, data->version, IPPROTO_TCP, rpc_connect_program_3_cb, private_data) != 0) {
                data->cb(rpc, status, command_data, data->private_data);
                free_rpc_cb_data(data);
                return;
@@ -493,7 +496,7 @@ static void rpc_connect_program_1_cb(struct rpc_context *rpc, int status, void *
                return;
        }
 
-       if (rpc_pmap_null_async(rpc, rpc_connect_program_2_cb, data) != 0) {
+       if (rpc_pmap2_null_async(rpc, rpc_connect_program_2_cb, data) != 0) {
                data->cb(rpc, status, command_data, data->private_data);
                free_rpc_cb_data(data);
                return;
@@ -809,7 +812,7 @@ static void nfs_mount_2_cb(struct rpc_context *rpc, int status, void *command_da
                return;
        }
 
-       if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, nfs_mount_3_cb, private_data) != 0) {
+       if (rpc_pmap2_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, nfs_mount_3_cb, private_data) != 0) {
                data->cb(-ENOMEM, nfs, command_data, data->private_data);
                free_nfs_cb_data(data);
                return;
@@ -837,7 +840,7 @@ static void nfs_mount_1_cb(struct rpc_context *rpc, int status, void *command_da
                return;
        }
 
-       if (rpc_pmap_null_async(rpc, nfs_mount_2_cb, data) != 0) {
+       if (rpc_pmap2_null_async(rpc, nfs_mount_2_cb, data) != 0) {
                data->cb(-ENOMEM, nfs, command_data, data->private_data);
                free_nfs_cb_data(data);
                return;
@@ -1528,38 +1531,6 @@ int nfs_chdir_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void *
 /*
  * Async pread()
  */
-static void nfs_pread_cb(struct rpc_context *rpc, int status, void *command_data, void *private_data)
-{
-       struct nfs_cb_data *data = private_data;
-       struct nfs_context *nfs = data->nfs;
-       READ3res *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: Read 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->READ3res_u.resok.count;
-       data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_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));
@@ -1588,17 +1559,29 @@ static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_dat
                data->cancel = 1;
        }
 
-       /* reassemble the data into the buffer */
        if (status == RPC_STATUS_SUCCESS) {
                res = command_data;
                if (res->status != NFS3_OK) {
                        rpc_set_error(nfs->rpc, "NFS: Read failed with %s(%d)", nfsstat3_to_str(res->status), nfsstat3_to_errno(res->status));
                        data->error = 1;
                } else  {
+                       /* if we have more than one call or we have received a short read we need a reassembly buffer */
+                       if (data->num_calls || (res->READ3res_u.resok.count < mdata->count && !res->READ3res_u.resok.eof)) {
+                               if (data->buffer == NULL) {
+                                       data->buffer =  malloc(data->request_size);
+                                       if (data->buffer == NULL) {
+                                               rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)data->request_size);
+                                               data->oom = 1;
+                                       }
+                               }
+                       }
                        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) {
+                                       /* copy data into reassembly buffer if we have one */
+                                       if (data->buffer != NULL) {
+                                               memcpy(&data->buffer[mdata->offset - data->start_offset], res->READ3res_u.resok.data.data_val, 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 {
@@ -1606,33 +1589,59 @@ static void nfs_pread_mcb(struct rpc_context *rpc, int status, void *command_dat
                                        data->error = 1;
                                }
                        }
+                       /* check if we have received a short read */
+                       if (res->READ3res_u.resok.count < mdata->count && !res->READ3res_u.resok.eof) {
+                               if (res->READ3res_u.resok.count == 0) {
+                                       rpc_set_error(nfs->rpc, "NFS: Read failed. No bytes read and not at EOF!");
+                                       data->error = 1;
+                               } else {
+                                       /* reissue reminder of this read request */
+                                       READ3args args;
+                                       mdata->offset += res->READ3res_u.resok.count;
+                                       mdata->count -= res->READ3res_u.resok.count;
+                                       nfs_fill_READ3args(&args, data->nfsfh, mdata->offset, mdata->count);
+                                       if (rpc_nfs3_read_async(nfs->rpc, nfs_pread_mcb, &args, mdata) == 0) {
+                                               data->num_calls++;
+                                               return;
+                                       } else {
+                                               rpc_set_error(nfs->rpc, "RPC error: Failed to send READ call for %s", data->path);
+                                               data->oom = 1;
+                                       }
+                               }
+                       }
                }
        }
 
+       free(mdata);
+
        if (data->num_calls > 0) {
                /* still waiting for more replies */
-               free(mdata);
                return;
        }
-
+       if (data->oom != 0) {
+               data->cb(-ENOMEM, nfs, command_data, data->private_data);
+               free_nfs_cb_data(data);
+               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;
        }
 
        data->nfsfh->offset = data->max_offset;
-       data->cb(data->max_offset - data->start_offset, nfs, data->buffer, data->private_data);
+       if (data->buffer) {
+               data->cb(data->max_offset - data->start_offset, nfs, data->buffer, data->private_data);
+       } else {
+               data->cb(res->READ3res_u.resok.count, nfs, res->READ3res_u.resok.data.data_val, data->private_data);
+       }
 
        free_nfs_cb_data(data);
-       free(mdata);
 }
 
 int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offset, uint64_t count, nfs_cb cb, void *private_data)
@@ -1649,41 +1658,19 @@ int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offse
        data->cb           = cb;
        data->private_data = private_data;
        data->nfsfh        = nfsfh;
+       data->request_size = count;
 
        nfsfh->offset = offset;
 
-       if (count <= nfs_get_readmax(nfs)) {
-               READ3args args;
-               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);
-                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
-                       free_nfs_cb_data(data);
-                       return -1;
-               }
-               return 0;
-       }
-
-       assert(count > 0);
        assert(data->num_calls == 0);
 
-       /* trying to read more than maximum server read size, we has to chop it up into smaller
-        * reads and collect into a reassembly buffer.
+       /* chop requests into chunks of at most READMAX bytes if necessary.
         * we send all reads in parallel so that performance is still good.
         */
        data->max_offset = offset;
        data->start_offset = offset;
 
-       data->buffer =  malloc(count);
-       if (data->buffer == NULL) {
-               rpc_set_error(nfs->rpc, "Out-Of-Memory: Failed to allocate reassembly buffer for %d bytes", (int)count);
-               data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
-               free_nfs_cb_data(data);
-               return -1;
-       }
-
-       while (count > 0) {
+       do {
                uint64_t readcount = count;
                struct nfs_mcb_data *mdata;
                READ3args args;
@@ -1695,9 +1682,12 @@ int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offse
                mdata = malloc(sizeof(struct nfs_mcb_data));
                if (mdata == NULL) {
                        rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure");
-                       if (data->num_calls == 0)
+                       if (data->num_calls == 0) {
                                free_nfs_cb_data(data);
-                       return -1;
+                               return -1;
+                       }
+                       data->oom = 1;
+                       break;
                }
                memset(mdata, 0, sizeof(struct nfs_mcb_data));
                mdata->data   = data;
@@ -1708,17 +1698,19 @@ int nfs_pread_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offse
 
                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);
-                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
                        free(mdata);
-                       if (data->num_calls == 0)
+                       if (data->num_calls == 0) {
                                free_nfs_cb_data(data);
-                       return -1;
+                               return -1;
+                       }
+                       data->oom = 1;
+                       break;
                }
 
                count               -= readcount;
                offset              += readcount;
                data->num_calls++;
-        }
+        } while (count > 0);
 
         return 0;
 }
@@ -1736,38 +1728,6 @@ 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)
-{
-       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);
-}
-
 static void nfs_fill_WRITE3args (WRITE3args *args, struct nfsfh *fh, uint64_t offset, uint64_t count,
                                  void *buf)
 {
@@ -1806,30 +1766,53 @@ 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->oom = 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->oom != 0) {
+               data->cb(-ENOMEM, nfs, command_data, data->private_data);
+               free_nfs_cb_data(data);
+               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;
        }
 
@@ -1837,7 +1820,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);
 }
 
 
@@ -1855,34 +1837,20 @@ 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;
-               nfs_fill_WRITE3args(&args, nfsfh, offset, count, 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
-        * chunks.
+       /* chop requests into chunks of at most WRITEMAX bytes if necessary.
         * we send all writes in parallel so that performance is still good.
         */
        data->max_offset = offset;
        data->start_offset = offset;
 
-       while (count > 0) {
+       do {
                uint64_t writecount = count;
                struct nfs_mcb_data *mdata;
                WRITE3args args;
@@ -1894,9 +1862,12 @@ int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offs
                mdata = malloc(sizeof(struct nfs_mcb_data));
                if (mdata == NULL) {
                        rpc_set_error(nfs->rpc, "out of memory: failed to allocate nfs_mcb_data structure");
-                       if (data->num_calls == 0)
+                       if (data->num_calls == 0) {
                                free_nfs_cb_data(data);
-                       return -1;
+                               return -1;
+                       }
+                       data->oom = 1;
+                       break;
                }
                memset(mdata, 0, sizeof(struct nfs_mcb_data));
                mdata->data   = data;
@@ -1907,18 +1878,19 @@ int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, uint64_t offs
 
                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);
-                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
                        free(mdata);
-                       if (data->num_calls == 0)
+                       if (data->num_calls == 0) {
                                free_nfs_cb_data(data);
-
-                       return -1;
+                               return -1;
+                       }
+                       data->oom = 1;
+                       break;
                }
 
                count               -= writecount;
                offset              += writecount;
                data->num_calls++;
-       }
+       } while (count > 0);
 
        return 0;
 }
@@ -4396,7 +4368,7 @@ static void mount_export_2_cb(struct rpc_context *rpc, int status, void *command
                return;
        }
 
-       if (rpc_pmap_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, mount_export_3_cb, private_data) != 0) {
+       if (rpc_pmap2_getport_async(rpc, MOUNT_PROGRAM, MOUNT_V3, IPPROTO_TCP, mount_export_3_cb, private_data) != 0) {
                data->cb(rpc, -ENOMEM, command_data, data->private_data);
                free_mount_cb_data(data);
                return;
@@ -4423,7 +4395,7 @@ static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command
                return;
        }
 
-       if (rpc_pmap_null_async(rpc, mount_export_2_cb, data) != 0) {
+       if (rpc_pmap2_null_async(rpc, mount_export_2_cb, data) != 0) {
                data->cb(rpc, -ENOMEM, command_data, data->private_data);
                free_mount_cb_data(data);
                return;