READDIRPLUS emulation: Some embedded servers do not support READDIRPLUS,
[deb_libnfs.git] / lib / libnfs.c
index d924c296a2bb87057a450739b1d91e1529fe260b..c1b4962caffa1efb3e7fdf8b68d3502f7c87b493 100644 (file)
 /*
  * High level api to nfs filesystems
  */
-
-#define _GNU_SOURCE
-
-#if defined(WIN32)
-#include <winsock2.h>
+#ifdef WIN32
+#include "win32_compat.h"
 #define DllExport
-#define O_SYNC 0
-typedef int uid_t;
-typedef int gid_t;
 #else
 #include <strings.h>
 #include <sys/statvfs.h>
 #include <utime.h>
 #include <unistd.h>
-#endif
+#endif/*WIN32*/
+
+#define _GNU_SOURCE
 
 #include <stdio.h>
 #include <stdarg.h>
@@ -125,6 +121,11 @@ int nfs_get_fd(struct nfs_context *nfs)
        return rpc_get_fd(nfs->rpc);
 }
 
+int nfs_queue_length(struct nfs_context *nfs)
+{
+       return rpc_queue_length(nfs->rpc);
+}
+
 int nfs_which_events(struct nfs_context *nfs)
 {
        return rpc_which_events(nfs->rpc);
@@ -681,10 +682,10 @@ static void nfs_stat_1_cb(struct rpc_context *rpc _U_, int status, void *command
         st.st_gid     = res->GETATTR3res_u.resok.obj_attributes.gid;
         st.st_rdev    = 0;
         st.st_size    = res->GETATTR3res_u.resok.obj_attributes.size;
-#if !defined(WIN32)
+#ifndef WIN32
         st.st_blksize = 4096;
         st.st_blocks  = res->GETATTR3res_u.resok.obj_attributes.size / 4096;
-#endif
+#endif//WIN32        
         st.st_atime   = res->GETATTR3res_u.resok.obj_attributes.atime.seconds;
         st.st_mtime   = res->GETATTR3res_u.resok.obj_attributes.mtime.seconds;
         st.st_ctime   = res->GETATTR3res_u.resok.obj_attributes.ctime.seconds;
@@ -1041,6 +1042,65 @@ static void nfs_pwrite_cb(struct rpc_context *rpc _U_, int status, void *command
        free_nfs_cb_data(data);
 }
 
+static void nfs_pwrite_mcb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
+{
+       struct nfs_mcb_data *mdata = private_data;
+       struct nfs_cb_data *data = mdata->data;
+       struct nfs_context *nfs = data->nfs;
+       WRITE3res *res;
+
+       data->num_calls--;
+
+       if (status == RPC_STATUS_ERROR) {
+               /* flag the failure but do not invoke callback until we have received all responses */
+               data->error = 1;
+       }
+       if (status == RPC_STATUS_CANCEL) {
+               /* flag the cancellation but do not invoke callback until we have received all responses */
+               data->cancel = 1;
+       }
+
+       if (status == RPC_STATUS_SUCCESS) {
+               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->error = 1;
+               } else  {
+                       if (res->WRITE3res_u.resok.count > 0) {
+                               if ((unsigned)data->max_offset < mdata->offset + res->WRITE3res_u.resok.count) {
+                                       data->max_offset = mdata->offset + res->WRITE3res_u.resok.count;
+                               }
+                       }
+               }
+       }
+
+       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;
+       }
+
+       data->nfsfh->offset = data->max_offset;
+       data->cb(data->max_offset - data->start_offset, nfs, NULL, data->private_data);
+
+       free_nfs_cb_data(data);
+       free(mdata);
+}
+
+
 int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset, size_t count, char *buf, nfs_cb cb, void *private_data)
 {
        struct nfs_cb_data *data;
@@ -1057,12 +1117,54 @@ int nfs_pwrite_async(struct nfs_context *nfs, struct nfsfh *nfsfh, off_t offset,
        data->nfsfh        = nfsfh;
 
        nfsfh->offset = offset;
-       if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_cb, &nfsfh->fh, buf, offset, count, nfsfh->is_sync?FILE_SYNC:UNSTABLE, 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;
+
+       if (count <= nfs_get_writemax(nfs)) {
+               if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_cb, &nfsfh->fh, buf, offset, count, nfsfh->is_sync?FILE_SYNC:UNSTABLE, 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;
+       }
+
+       /* trying to write more than maximum server write size, we has to chop it up into smaller
+        * chunks.
+        * we send all writes in parallell so that performance is still good.
+        */
+       data->max_offset = offset;
+       data->start_offset = offset;
+
+       while (count > 0) {
+               size_t writecount = count;
+               struct nfs_mcb_data *mdata;
+
+               if (writecount > nfs_get_writemax(nfs)) {
+                       writecount = nfs_get_writemax(nfs);
+               }
+
+               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");
+                       return -1;
+               }
+               memset(mdata, 0, sizeof(struct nfs_mcb_data));
+               mdata->data   = data;
+               mdata->offset = offset;
+               mdata->count  = writecount;
+
+               if (rpc_nfs_write_async(nfs->rpc, nfs_pwrite_mcb, &nfsfh->fh, &buf[offset - data->start_offset], offset, writecount, nfsfh->is_sync?FILE_SYNC:UNSTABLE, 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);
+                       return -1;
+               }
+
+               count               -= writecount;
+               offset              += writecount;
+               data->num_calls++;
        }
+
        return 0;
 }
 
@@ -1656,20 +1758,89 @@ int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void
 
 
 
-
-
 /*
  * Async opendir()
  */
-static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
+
+/* ReadDirPlus Emulation Callback data */
+struct rdpe_cb_data {
+       int getattrcount;
+       int status;
+       struct nfs_cb_data *data;
+};
+
+/* ReadDirPlus Emulation LOOKUP Callback data */
+struct rdpe_lookup_cb_data {
+       struct rdpe_cb_data *rdpe_cb_data;
+       struct nfsdirent *nfsdirent;
+};
+
+/* Workaround for servers lacking READDIRPLUS, use READDIR instead and a GETATTR-loop */
+static void nfs_opendir3_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
 {
-       READDIRPLUS3res *res;
+       LOOKUP3res *res = command_data;
+       struct rdpe_lookup_cb_data *rdpe_lookup_cb_data = private_data;
+       struct rdpe_cb_data *rdpe_cb_data = rdpe_lookup_cb_data->rdpe_cb_data;
+       struct nfs_cb_data *data = rdpe_cb_data->data;
+       struct nfsdir *nfsdir = data->continue_data;
+       struct nfs_context *nfs = data->nfs;
+       struct nfsdirent *nfsdirent = rdpe_lookup_cb_data->nfsdirent;
+
+       free(rdpe_lookup_cb_data);
+
+       rdpe_cb_data->getattrcount--;
+
+       if (status == RPC_STATUS_ERROR) {
+               rdpe_cb_data->status = RPC_STATUS_ERROR;
+       }
+       if (status == RPC_STATUS_CANCEL) {
+               rdpe_cb_data->status = RPC_STATUS_CANCEL;
+       }
+       if (status == RPC_STATUS_SUCCESS && res->status != NFS3_OK) {
+               rdpe_cb_data->status = RPC_STATUS_ERROR;
+       }
+       if (status == RPC_STATUS_SUCCESS && res->status == NFS3_OK) {
+               if (res->LOOKUP3res_u.resok.obj_attributes.attributes_follow) {
+                       fattr3 *attributes = &res->LOOKUP3res_u.resok.obj_attributes.post_op_attr_u.attributes;
+
+                       nfsdirent->type = attributes->type;
+                       nfsdirent->mode = attributes->mode;
+                       nfsdirent->size = attributes->size;
+
+                       nfsdirent->atime.tv_sec  = attributes->atime.seconds;
+                       nfsdirent->atime.tv_usec = attributes->atime.nseconds/1000;
+                       nfsdirent->mtime.tv_sec  = attributes->mtime.seconds;
+                       nfsdirent->mtime.tv_usec = attributes->mtime.nseconds/1000;
+                       nfsdirent->ctime.tv_sec  = attributes->ctime.seconds;
+                       nfsdirent->ctime.tv_usec = attributes->ctime.nseconds/1000;
+               }
+       }
+
+       if (rdpe_cb_data->getattrcount == 0) {
+               if (rdpe_cb_data->status != RPC_STATUS_SUCCESS) {
+                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+                       nfs_free_nfsdir(nfsdir);
+               } else {
+                       data->cb(0, nfs, nfsdir, data->private_data);
+               }
+               free(rdpe_cb_data);
+
+               data->continue_data = NULL;
+               free_nfs_cb_data(data);
+       }
+}
+
+static void nfs_opendir2_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
+{
+       READDIR3res *res = command_data;
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
        struct nfsdir *nfsdir = data->continue_data;
-       struct entryplus3 *entry;
+       struct nfsdirent *nfsdirent;
+       struct entry3 *entry;
        uint64_t cookie;
-
+       struct rdpe_cb_data *rdpe_cb_data;
+       
        if (status == RPC_STATUS_ERROR) {
                data->cb(-EFAULT, nfs, command_data, data->private_data);
                nfs_free_nfsdir(nfsdir);
@@ -1677,6 +1848,7 @@ static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *comman
                free_nfs_cb_data(data);
                return;
        }
+
        if (status == RPC_STATUS_CANCEL) {
                data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
                nfs_free_nfsdir(nfsdir);
@@ -1685,7 +1857,6 @@ static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *comman
                return;
        }
 
-       res = command_data;
        if (res->status != NFS3_OK) {
                rpc_set_error(nfs->rpc, "NFS: READDIR of %s failed with %s(%d)", data->saved_path, 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);
@@ -1695,6 +1866,126 @@ static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *comman
                return;
        }
 
+       entry =res->READDIR3res_u.resok.reply.entries;
+       while (entry != NULL) {
+               nfsdirent = malloc(sizeof(struct nfsdirent));
+               if (nfsdirent == NULL) {
+                       data->cb(-ENOMEM, nfs, "Failed to allocate dirent", data->private_data);
+                       nfs_free_nfsdir(nfsdir);
+                       data->continue_data = NULL;
+                       free_nfs_cb_data(data);
+                       return;
+               }
+               memset(nfsdirent, 0, sizeof(struct nfsdirent));
+               nfsdirent->name = strdup(entry->name);
+               if (nfsdirent->name == NULL) {
+                       data->cb(-ENOMEM, nfs, "Failed to allocate dirent->name", data->private_data);
+                       nfs_free_nfsdir(nfsdir);
+                       data->continue_data = NULL;
+                       free_nfs_cb_data(data);
+                       return;
+               }
+               nfsdirent->inode = entry->fileid;
+
+               nfsdirent->next  = nfsdir->entries;
+               nfsdir->entries  = nfsdirent;
+
+               cookie = entry->cookie;
+               entry  = entry->nextentry;
+       }
+
+       if (res->READDIR3res_u.resok.reply.eof == 0) {
+               if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, cookie, res->READDIR3res_u.resok.cookieverf, 8192, data) != 0) {
+                       rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
+                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+                       nfs_free_nfsdir(nfsdir);
+                       data->continue_data = NULL;
+                       free_nfs_cb_data(data);
+                       return;
+               }
+               return;
+       }
+
+       /* steal the dirhandle */
+       nfsdir->current = nfsdir->entries;
+
+       rdpe_cb_data = malloc(sizeof(struct rdpe_cb_data));
+       rdpe_cb_data->getattrcount = 0;
+       rdpe_cb_data->status = RPC_STATUS_SUCCESS;
+       rdpe_cb_data->data = data;
+       for (nfsdirent = nfsdir->entries; nfsdirent; nfsdirent = nfsdirent->next) {
+               struct rdpe_lookup_cb_data *rdpe_lookup_cb_data;
+
+               rdpe_lookup_cb_data = malloc(sizeof(struct rdpe_lookup_cb_data));
+               rdpe_lookup_cb_data->rdpe_cb_data = rdpe_cb_data;
+               rdpe_lookup_cb_data->nfsdirent = nfsdirent;
+
+               if (rpc_nfs_lookup_async(nfs->rpc, nfs_opendir3_cb, &data->fh, nfsdirent->name, rdpe_lookup_cb_data) != 0) {
+                       rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR LOOKUP call");
+
+                       /* if we have already commands in flight, we cant just stop, we have to wait for the
+                        * commands in flight to complete
+                        */
+                       if (rdpe_cb_data->getattrcount > 0) {
+                               rdpe_cb_data->status = RPC_STATUS_ERROR;
+                               free(rdpe_lookup_cb_data);
+                               return;
+                       }
+
+                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+                       nfs_free_nfsdir(nfsdir);
+                       data->continue_data = NULL;
+                       free_nfs_cb_data(data);
+                       free(rdpe_lookup_cb_data);
+                       free(rdpe_cb_data);
+                       return;
+               }
+               rdpe_cb_data->getattrcount++;
+       }
+}
+
+
+static void nfs_opendir_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
+{
+       READDIRPLUS3res *res = command_data;
+       struct nfs_cb_data *data = private_data;
+       struct nfs_context *nfs = data->nfs;
+       struct nfsdir *nfsdir = data->continue_data;
+       struct entryplus3 *entry;
+       uint64_t cookie;
+       
+
+       if (status == RPC_STATUS_ERROR || (status == RPC_STATUS_SUCCESS && res->status == NFS3ERR_NOTSUPP) ){
+               cookieverf3 cv;
+
+               if (rpc_nfs_readdir_async(nfs->rpc, nfs_opendir2_cb, &data->fh, 0, (char *)&cv, 8192, data) != 0) {
+                       rpc_set_error(nfs->rpc, "RPC error: Failed to send READDIR call for %s", data->path);
+                       data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+                       nfs_free_nfsdir(nfsdir);
+                       data->continue_data = NULL;
+                       free_nfs_cb_data(data);
+                       return;
+               }
+               return;
+       }
+
+       if (status == RPC_STATUS_CANCEL) {
+               data->cb(-EINTR, nfs, "Command was cancelled", data->private_data);
+               nfs_free_nfsdir(nfsdir);
+               data->continue_data = NULL;
+               free_nfs_cb_data(data);
+               return;
+       }
+
+       if (res->status != NFS3_OK) {
+               rpc_set_error(nfs->rpc, "NFS: READDIRPLUS of %s failed with %s(%d)", data->saved_path, 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);
+               nfs_free_nfsdir(nfsdir);
+               data->continue_data = NULL;
+               free_nfs_cb_data(data);
+               return;
+       }
+
        entry =res->READDIRPLUS3res_u.resok.reply.entries;
        while (entry != NULL) {
                struct nfsdirent *nfsdirent;
@@ -2903,7 +3194,11 @@ size_t nfs_get_readmax(struct nfs_context *nfs)
  */
 size_t nfs_get_writemax(struct nfs_context *nfs)
 {
-       return nfs->writemax;
+       /* Some XDR libraries can not marshall PDUs bigger than this */
+        if (nfs->writemax < 32768) {
+               return nfs->writemax;
+       }
+       return 32768;
 }
 
 void nfs_set_error(struct nfs_context *nfs, char *error_string, ...)
@@ -2918,7 +3213,7 @@ void nfs_set_error(struct nfs_context *nfs, char *error_string, ...)
                free(nfs->rpc->error_string);
        }
        nfs->rpc->error_string = str;
-        va_end(ap);
+    va_end(ap);
 }
 
 
@@ -3096,35 +3391,3 @@ const char *nfs_get_export(struct nfs_context *nfs) {
        return nfs->export;
 }
 
-
-#if defined(WIN32)
-int poll(struct pollfd *fds, int nfsd, int timeout)
-{
-       fd_set rfds, wfds, efds;
-       int ret;
-
-       FD_ZERO(&rfds);
-       FD_ZERO(&wfds);
-       FD_ZERO(&efds);
-       if (fds->events & POLLIN) {
-               FD_SET(fds->fd, &rfds);
-       }
-       if (fds->events & POLLOUT) {
-               FD_SET(fds->fd, &wfds);
-       }
-       FD_SET(fds->fd, &efds);
-       select(fds->fd + 1, &rfds, &wfds, &efds, NULL);
-       fds->revents = 0;
-       if (FD_ISSET(fds->fd, &rfds)) {
-               fds->revents |= POLLIN;
-       }
-       if (FD_ISSET(fds->fd, &wfds)) {
-               fds->revents |= POLLOUT;
-       }
-       if (FD_ISSET(fds->fd, &efds)) {
-               fds->revents |= POLLHUP;
-       }
-       return 1;
-}
-#endif
-