Add MKNOD command support
[deb_libnfs.git] / lib / libnfs.c
index ba7b64893f4ede24fc3b6be4f4eab5774dac09fc..71c6e9cb1b2ad78b3a3783ff0a962810fafd349d 100644 (file)
@@ -111,7 +111,7 @@ struct nfs_mcb_data {
 static int nfs_lookup_path_async_internal(struct nfs_context *nfs, struct nfs_cb_data *data, struct nfs_fh3 *fh);
 
 
-void nfs_set_auth(struct nfs_context *nfs, struct AUTH *auth)
+void nfs_set_auth(struct nfs_context *nfs, AUTH *auth)
 {
        rpc_set_auth(nfs->rpc, auth);
 }
@@ -292,6 +292,9 @@ static void nfs_mount_7_cb(struct rpc_context *rpc, int status, void *command_da
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
 
+       /* Dont want any more callbacks even if the socket is closed */
+       rpc->connect_cb = NULL;
+
        if (status == RPC_STATUS_ERROR) {
                data->cb(-EFAULT, nfs, command_data, data->private_data);
                free_nfs_cb_data(data);
@@ -352,6 +355,8 @@ static void nfs_mount_6_cb(struct rpc_context *rpc, int status, void *command_da
                free_nfs_cb_data(data);
                return;
        }
+       /* NFS TCP connections we want to autoreconnect after sessions are torn down (due to inactivity or error) */
+       rpc_set_autoreconnect(rpc);
 }
 
 
@@ -383,6 +388,9 @@ static void nfs_mount_4_cb(struct rpc_context *rpc, int status, void *command_da
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
 
+       /* Dont want any more callbacks even if the socket is closed */
+       rpc->connect_cb = NULL;
+
        if (status == RPC_STATUS_ERROR) {
                data->cb(-EFAULT, nfs, command_data, data->private_data);
                free_nfs_cb_data(data);
@@ -463,6 +471,9 @@ static void nfs_mount_1_cb(struct rpc_context *rpc, int status, void *command_da
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
 
+       /* Dont want any more callbacks even if the socket is closed */
+       rpc->connect_cb = NULL;
+
        if (status == RPC_STATUS_ERROR) {
                data->cb(-EFAULT, nfs, command_data, data->private_data);
                free_nfs_cb_data(data);
@@ -499,12 +510,10 @@ int nfs_mount_async(struct nfs_context *nfs, const char *server, const char *exp
        new_export = strdup(export);
        if (nfs->server != NULL) {
                free(nfs->server);
-               nfs->server = NULL;
        }
        nfs->server        = new_server;
        if (nfs->export != NULL) {
                free(nfs->export);
-               nfs->export = NULL;
        }
        nfs->export        = new_export;
        data->nfs          = nfs;
@@ -1757,21 +1766,193 @@ int nfs_unlink_async(struct nfs_context *nfs, const char *path, nfs_cb cb, void
 }
 
 
+/*
+ * Async mknod()
+ */
+struct mknod_cb_data {
+       char *path;
+       int mode;
+       int major;
+       int minor;
+};
+
+static void free_mknod_cb_data(void *ptr)
+{
+       struct mknod_cb_data *data = ptr;
+
+       free(data->path);
+       free(data);
+}
+
+static void nfs_mknod_cb(struct rpc_context *rpc _U_, int status, void *command_data, void *private_data)
+{
+       MKNOD3res *res;
+       struct nfs_cb_data *data = private_data;
+       struct nfs_context *nfs = data->nfs;
+       char *str = data->continue_data;
+       
+       str = &str[strlen(str) + 1];
+
+       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: MKNOD of %s/%s failed with %s(%d)", data->saved_path, str, 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->cb(0, nfs, NULL, data->private_data);
+       free_nfs_cb_data(data);
+}
+
+static int nfs_mknod_continue_internal(struct nfs_context *nfs, struct nfs_cb_data *data)
+{
+       struct mknod_cb_data *cb_data = data->continue_data;
+       char *str = cb_data->path;
+       
+       str = &str[strlen(str) + 1];
+
+       if (rpc_nfs_mknod_async(nfs->rpc, nfs_mknod_cb, &data->fh, str, cb_data->mode, cb_data->major, cb_data->minor, data) != 0) {
+               data->cb(-ENOMEM, nfs, rpc_get_error(nfs->rpc), data->private_data);
+               free_nfs_cb_data(data);
+               return -1;
+       }
+       return 0;
+}
+
+int nfs_mknod_async(struct nfs_context *nfs, const char *path, int mode, int dev, nfs_cb cb, void *private_data)
+{
+       char *ptr;
+       struct mknod_cb_data *cb_data;
+
+       cb_data = malloc(sizeof(struct mknod_cb_data));
+       if (cb_data == NULL) {
+               rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for cb data");
+               return -1;
+       }
+
+       cb_data->path = strdup(path);
+       if (cb_data->path == NULL) {
+               rpc_set_error(nfs->rpc, "Out of memory, failed to allocate mode buffer for path");
+               free(cb_data);          
+               return -1;
+       }
 
+       ptr = strrchr(cb_data->path, '/');
+       if (ptr == NULL) {
+               rpc_set_error(nfs->rpc, "Invalid path %s", path);
+               return -1;
+       }
+       *ptr = 0;
 
+       cb_data->mode = mode;
+       cb_data->major = major(dev);
+       cb_data->minor = minor(dev);
+
+       /* data->path now points to the parent directory,  and beyond the nul terminateor is the new directory to create */
+       if (nfs_lookuppath_async(nfs, cb_data->path, cb, private_data, nfs_mknod_continue_internal, cb_data, free_mknod_cb_data, 0) != 0) {
+               rpc_set_error(nfs->rpc, "Out of memory: failed to start parsing the path components");
+               free_mknod_cb_data(cb_data);
+               return -1;
+       }
+
+       return 0;
+}
 
 /*
  * 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);
@@ -1779,6 +1960,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);
@@ -1787,7 +1969,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);
@@ -1797,6 +1978,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;
@@ -2002,9 +2303,7 @@ static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *comm
        FSSTAT3res *res;
        struct nfs_cb_data *data = private_data;
        struct nfs_context *nfs = data->nfs;
-#ifndef WIN32
        struct statvfs svfs;
-#endif/*WIN32*/
 
        if (status == RPC_STATUS_ERROR) {
                data->cb(-EFAULT, nfs, command_data, data->private_data);
@@ -2025,7 +2324,6 @@ static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *comm
                return;
        }
 
-#ifndef WIN32
        svfs.f_bsize   = 4096;
        svfs.f_frsize  = 4096;
        svfs.f_blocks  = res->FSSTAT3res_u.resok.tbytes/4096;
@@ -2039,9 +2337,6 @@ static void nfs_statvfs_1_cb(struct rpc_context *rpc _U_, int status, void *comm
        svfs.f_namemax = 256;
 
        data->cb(0, nfs, &svfs, data->private_data);
-#else
-  data->cb(0, nfs,NULL, data->private_data);  
-#endif/*WIN32*/
        free_nfs_cb_data(data);
 }
 
@@ -3077,6 +3372,9 @@ static void mount_export_4_cb(struct rpc_context *rpc, int status, void *command
 {
        struct mount_cb_data *data = private_data;
 
+       /* Dont want any more callbacks even if the socket is closed */
+       rpc->connect_cb = NULL;
+
        if (status == RPC_STATUS_ERROR) {       
                data->cb(rpc, -EFAULT, command_data, data->private_data);
                free_mount_cb_data(data);
@@ -3153,6 +3451,9 @@ static void mount_export_1_cb(struct rpc_context *rpc, int status, void *command
 {
        struct mount_cb_data *data = private_data;
 
+       /* Dont want any more callbacks even if the socket is closed */
+       rpc->connect_cb = NULL;
+
        if (status == RPC_STATUS_ERROR) {
                data->cb(rpc, -EFAULT, command_data, data->private_data);
                free_mount_cb_data(data);
@@ -3208,35 +3509,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
-