2 Copyright (C) 2010 by Ronnie Sahlberg <ronniesahlberg@gmail.com>
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU Lesser General Public License as published by
6 the Free Software Foundation; either version 2.1 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU Lesser General Public License for more details.
14 You should have received a copy of the GNU Lesser General Public License
15 along with this program; if not, see <http://www.gnu.org/licenses/>.
18 * High level api to nfs filesystems
25 #include "aros_compat.h"
29 #include "win32_compat.h"
32 #ifdef HAVE_SYS_SOCKET_H
33 #include <sys/socket.h>
41 #define statvfs statfs
48 #ifdef HAVE_SYS_STATVFS_H
49 #include <sys/statvfs.h>
52 #ifdef HAVE_SYS_IOCTL_H
53 #include <sys/ioctl.h>
68 #ifdef HAVE_NETINET_IN_H
69 #include <netinet/in.h>
80 #include <sys/types.h>
85 #ifdef HAVE_SYS_SOCKIO_H
86 #include <sys/sockio.h>
89 #include "libnfs-zdr.h"
91 #include "libnfs-raw.h"
92 #include "libnfs-raw-mount.h"
93 #include "libnfs-raw-nfs.h"
94 #include "libnfs-private.h"
106 static void wait_for_reply(struct rpc_context
*rpc
, struct sync_cb_data
*cb_data
)
110 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
112 while (!cb_data
->is_finished
) {
114 pfd
.fd
= rpc_get_fd(rpc
);
115 pfd
.events
= rpc_which_events(rpc
);
116 if (poll(&pfd
, 1, -1) < 0) {
117 rpc_set_error(rpc
, "Poll failed");
118 cb_data
->status
= -EIO
;
121 if (rpc_service(rpc
, pfd
.revents
) < 0) {
122 rpc_set_error(rpc
, "rpc_service failed");
123 cb_data
->status
= -EIO
;
126 if (rpc_get_fd(rpc
) == -1) {
127 rpc_set_error(rpc
, "Socket closed\n");
133 static void wait_for_nfs_reply(struct nfs_context
*nfs
, struct sync_cb_data
*cb_data
)
137 while (!cb_data
->is_finished
) {
139 pfd
.fd
= nfs_get_fd(nfs
);
140 pfd
.events
= nfs_which_events(nfs
);
141 if (poll(&pfd
, 1, -1) < 0) {
142 nfs_set_error(nfs
, "Poll failed");
143 cb_data
->status
= -EIO
;
146 if (nfs_service(nfs
, pfd
.revents
) < 0) {
147 nfs_set_error(nfs
, "nfs_service failed");
148 cb_data
->status
= -EIO
;
160 * connect to the server and mount the export
162 static void mount_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
164 struct sync_cb_data
*cb_data
= private_data
;
166 cb_data
->is_finished
= 1;
167 cb_data
->status
= status
;
170 nfs_set_error(nfs
, "mount/mnt call failed with \"%s\"", (char *)data
);
175 int nfs_mount(struct nfs_context
*nfs
, const char *server
, const char *export
)
177 struct sync_cb_data cb_data
;
178 struct rpc_context
*rpc
= nfs_get_rpc_context(nfs
);
180 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
182 cb_data
.is_finished
= 0;
184 if (nfs_mount_async(nfs
, server
, export
, mount_cb
, &cb_data
) != 0) {
185 nfs_set_error(nfs
, "nfs_mount_async failed");
189 wait_for_nfs_reply(nfs
, &cb_data
);
191 /* Dont want any more callbacks even if the socket is closed */
192 rpc
->connect_cb
= NULL
;
194 return cb_data
.status
;
201 static void stat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
203 struct sync_cb_data
*cb_data
= private_data
;
205 cb_data
->is_finished
= 1;
206 cb_data
->status
= status
;
209 nfs_set_error(nfs
, "stat call failed with \"%s\"", (char *)data
);
213 memcpy(cb_data
->return_data
, data
, sizeof(struct __stat64
));
215 memcpy(cb_data
->return_data
, data
, sizeof(struct stat
));
220 int nfs_stat(struct nfs_context
*nfs
, const char *path
, struct __stat64
*st
)
222 int nfs_stat(struct nfs_context
*nfs
, const char *path
, struct stat
*st
)
225 struct sync_cb_data cb_data
;
227 cb_data
.is_finished
= 0;
228 cb_data
.return_data
= st
;
230 if (nfs_stat_async(nfs
, path
, stat_cb
, &cb_data
) != 0) {
231 nfs_set_error(nfs
, "nfs_stat_async failed");
235 wait_for_nfs_reply(nfs
, &cb_data
);
237 return cb_data
.status
;
240 static void stat64_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
242 struct sync_cb_data
*cb_data
= private_data
;
244 cb_data
->is_finished
= 1;
245 cb_data
->status
= status
;
248 nfs_set_error(nfs
, "stat call failed with \"%s\"", (char *)data
);
251 memcpy(cb_data
->return_data
, data
, sizeof(struct nfs_stat_64
));
254 int nfs_stat64(struct nfs_context
*nfs
, const char *path
, struct nfs_stat_64
*st
)
256 struct sync_cb_data cb_data
;
258 cb_data
.is_finished
= 0;
259 cb_data
.return_data
= st
;
261 if (nfs_stat64_async(nfs
, path
, stat64_cb
, &cb_data
) != 0) {
262 nfs_set_error(nfs
, "nfs_stat64_async failed");
266 wait_for_nfs_reply(nfs
, &cb_data
);
268 return cb_data
.status
;
274 static void open_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
276 struct sync_cb_data
*cb_data
= private_data
;
277 struct nfsfh
*fh
, **nfsfh
;
279 cb_data
->is_finished
= 1;
280 cb_data
->status
= status
;
283 nfs_set_error(nfs
, "open call failed with \"%s\"", (char *)data
);
288 nfsfh
= cb_data
->return_data
;
292 int nfs_open(struct nfs_context
*nfs
, const char *path
, int flags
, struct nfsfh
**nfsfh
)
294 struct sync_cb_data cb_data
;
296 cb_data
.is_finished
= 0;
297 cb_data
.return_data
= nfsfh
;
299 if (nfs_open_async(nfs
, path
, flags
, open_cb
, &cb_data
) != 0) {
300 nfs_set_error(nfs
, "nfs_open_async failed");
304 wait_for_nfs_reply(nfs
, &cb_data
);
306 return cb_data
.status
;
312 static void chdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
314 struct sync_cb_data
*cb_data
= private_data
;
316 cb_data
->is_finished
= 1;
317 cb_data
->status
= status
;
320 nfs_set_error(nfs
, "chdir call failed with \"%s\"", (char *)data
);
325 int nfs_chdir(struct nfs_context
*nfs
, const char *path
)
327 struct sync_cb_data cb_data
;
329 cb_data
.is_finished
= 0;
331 if (nfs_chdir_async(nfs
, path
, chdir_cb
, &cb_data
) != 0) {
332 nfs_set_error(nfs
, "nfs_chdir_async failed with %s",
337 wait_for_nfs_reply(nfs
, &cb_data
);
339 return cb_data
.status
;
347 static void pread_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
349 struct sync_cb_data
*cb_data
= private_data
;
351 cb_data
->is_finished
= 1;
352 cb_data
->status
= status
;
355 nfs_set_error(nfs
, "%s call failed with \"%s\"", cb_data
->call
, (char *)data
);
359 buffer
= cb_data
->return_data
;
360 memcpy(buffer
, (char *)data
, status
);
363 int nfs_pread(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buffer
)
365 struct sync_cb_data cb_data
;
367 cb_data
.is_finished
= 0;
368 cb_data
.return_data
= buffer
;
369 cb_data
.call
= "pread";
371 if (nfs_pread_async(nfs
, nfsfh
, offset
, count
, pread_cb
, &cb_data
) != 0) {
372 nfs_set_error(nfs
, "nfs_pread_async failed");
376 wait_for_nfs_reply(nfs
, &cb_data
);
378 return cb_data
.status
;
384 int nfs_read(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buffer
)
386 struct sync_cb_data cb_data
;
388 cb_data
.is_finished
= 0;
389 cb_data
.return_data
= buffer
;
390 cb_data
.call
= "read";
392 if (nfs_read_async(nfs
, nfsfh
, count
, pread_cb
, &cb_data
) != 0) {
393 nfs_set_error(nfs
, "nfs_read_async failed");
397 wait_for_nfs_reply(nfs
, &cb_data
);
399 return cb_data
.status
;
405 static void close_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
407 struct sync_cb_data
*cb_data
= private_data
;
408 cb_data
->is_finished
= 1;
409 cb_data
->status
= status
;
412 nfs_set_error(nfs
, "close call failed with \"%s\"", (char *)data
);
417 int nfs_close(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
419 struct sync_cb_data cb_data
;
421 cb_data
.is_finished
= 0;
423 if (nfs_close_async(nfs
, nfsfh
, close_cb
, &cb_data
) != 0) {
424 nfs_set_error(nfs
, "nfs_close_async failed");
428 wait_for_nfs_reply(nfs
, &cb_data
);
430 return cb_data
.status
;
439 int nfs_fstat(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, struct stat
*st
)
441 struct sync_cb_data cb_data
;
443 cb_data
.is_finished
= 0;
444 cb_data
.return_data
= st
;
446 if (nfs_fstat_async(nfs
, nfsfh
, stat_cb
, &cb_data
) != 0) {
447 nfs_set_error(nfs
, "nfs_fstat_async failed");
451 wait_for_nfs_reply(nfs
, &cb_data
);
453 return cb_data
.status
;
460 static void pwrite_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
462 struct sync_cb_data
*cb_data
= private_data
;
463 cb_data
->is_finished
= 1;
464 cb_data
->status
= status
;
467 nfs_set_error(nfs
, "%s call failed with \"%s\"", cb_data
->call
, (char *)data
);
470 int nfs_pwrite(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buf
)
472 struct sync_cb_data cb_data
;
474 cb_data
.is_finished
= 0;
475 cb_data
.call
= "pwrite";
477 if (nfs_pwrite_async(nfs
, nfsfh
, offset
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
478 nfs_set_error(nfs
, "nfs_pwrite_async failed");
482 wait_for_nfs_reply(nfs
, &cb_data
);
484 return cb_data
.status
;
490 int nfs_write(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
)
492 struct sync_cb_data cb_data
;
494 cb_data
.is_finished
= 0;
495 cb_data
.call
= "write";
497 if (nfs_write_async(nfs
, nfsfh
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
498 nfs_set_error(nfs
, "nfs_write_async failed");
502 wait_for_nfs_reply(nfs
, &cb_data
);
504 return cb_data
.status
;
511 static void fsync_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
513 struct sync_cb_data
*cb_data
= private_data
;
514 cb_data
->is_finished
= 1;
515 cb_data
->status
= status
;
518 nfs_set_error(nfs
, "fsync call failed with \"%s\"", (char *)data
);
523 int nfs_fsync(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
525 struct sync_cb_data cb_data
;
527 cb_data
.is_finished
= 0;
529 if (nfs_fsync_async(nfs
, nfsfh
, fsync_cb
, &cb_data
) != 0) {
530 nfs_set_error(nfs
, "nfs_fsync_async failed");
534 wait_for_nfs_reply(nfs
, &cb_data
);
536 return cb_data
.status
;
545 static void ftruncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
547 struct sync_cb_data
*cb_data
= private_data
;
548 cb_data
->is_finished
= 1;
549 cb_data
->status
= status
;
552 nfs_set_error(nfs
, "ftruncate call failed with \"%s\"", (char *)data
);
557 int nfs_ftruncate(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
)
559 struct sync_cb_data cb_data
;
561 cb_data
.is_finished
= 0;
563 if (nfs_ftruncate_async(nfs
, nfsfh
, length
, ftruncate_cb
, &cb_data
) != 0) {
564 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
568 wait_for_nfs_reply(nfs
, &cb_data
);
570 return cb_data
.status
;
578 static void truncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
580 struct sync_cb_data
*cb_data
= private_data
;
581 cb_data
->is_finished
= 1;
582 cb_data
->status
= status
;
585 nfs_set_error(nfs
, "truncate call failed with \"%s\"", (char *)data
);
590 int nfs_truncate(struct nfs_context
*nfs
, const char *path
, uint64_t length
)
592 struct sync_cb_data cb_data
;
594 cb_data
.is_finished
= 0;
596 if (nfs_truncate_async(nfs
, path
, length
, truncate_cb
, &cb_data
) != 0) {
597 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
601 wait_for_nfs_reply(nfs
, &cb_data
);
603 return cb_data
.status
;
613 static void mkdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
615 struct sync_cb_data
*cb_data
= private_data
;
616 cb_data
->is_finished
= 1;
617 cb_data
->status
= status
;
620 nfs_set_error(nfs
, "mkdir call failed with \"%s\"", (char *)data
);
625 int nfs_mkdir(struct nfs_context
*nfs
, const char *path
)
627 struct sync_cb_data cb_data
;
629 cb_data
.is_finished
= 0;
631 if (nfs_mkdir_async(nfs
, path
, mkdir_cb
, &cb_data
) != 0) {
632 nfs_set_error(nfs
, "nfs_mkdir_async failed");
636 wait_for_nfs_reply(nfs
, &cb_data
);
638 return cb_data
.status
;
648 static void rmdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
650 struct sync_cb_data
*cb_data
= private_data
;
651 cb_data
->is_finished
= 1;
652 cb_data
->status
= status
;
655 nfs_set_error(nfs
, "rmdir call failed with \"%s\"", (char *)data
);
660 int nfs_rmdir(struct nfs_context
*nfs
, const char *path
)
662 struct sync_cb_data cb_data
;
664 cb_data
.is_finished
= 0;
666 if (nfs_rmdir_async(nfs
, path
, rmdir_cb
, &cb_data
) != 0) {
667 nfs_set_error(nfs
, "nfs_rmdir_async failed");
671 wait_for_nfs_reply(nfs
, &cb_data
);
673 return cb_data
.status
;
681 static void creat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
683 struct sync_cb_data
*cb_data
= private_data
;
684 struct nfsfh
*fh
, **nfsfh
;
686 cb_data
->is_finished
= 1;
687 cb_data
->status
= status
;
690 nfs_set_error(nfs
, "creat call failed with \"%s\"", (char *)data
);
695 nfsfh
= cb_data
->return_data
;
699 int nfs_create(struct nfs_context
*nfs
, const char *path
, int flags
, int mode
, struct nfsfh
**nfsfh
)
701 struct sync_cb_data cb_data
;
703 cb_data
.is_finished
= 0;
704 cb_data
.return_data
= nfsfh
;
706 if (nfs_create_async(nfs
, path
, flags
, mode
, creat_cb
, &cb_data
) != 0) {
707 nfs_set_error(nfs
, "nfs_create_async failed");
711 wait_for_nfs_reply(nfs
, &cb_data
);
713 return cb_data
.status
;
716 int nfs_creat(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
718 return nfs_create(nfs
, path
, 0, mode
, nfsfh
);
724 static void mknod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
726 struct sync_cb_data
*cb_data
= private_data
;
728 cb_data
->is_finished
= 1;
729 cb_data
->status
= status
;
732 nfs_set_error(nfs
, "mknod call failed with \"%s\"", (char *)data
);
737 int nfs_mknod(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
)
739 struct sync_cb_data cb_data
;
741 cb_data
.is_finished
= 0;
743 if (nfs_mknod_async(nfs
, path
, mode
, dev
, mknod_cb
, &cb_data
) != 0) {
744 nfs_set_error(nfs
, "nfs_creat_async failed");
748 wait_for_nfs_reply(nfs
, &cb_data
);
750 return cb_data
.status
;
757 static void unlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
759 struct sync_cb_data
*cb_data
= private_data
;
761 cb_data
->is_finished
= 1;
762 cb_data
->status
= status
;
765 nfs_set_error(nfs
, "unlink call failed with \"%s\"", (char *)data
);
770 int nfs_unlink(struct nfs_context
*nfs
, const char *path
)
772 struct sync_cb_data cb_data
;
774 cb_data
.is_finished
= 0;
776 if (nfs_unlink_async(nfs
, path
, unlink_cb
, &cb_data
) != 0) {
777 nfs_set_error(nfs
, "nfs_unlink_async failed");
781 wait_for_nfs_reply(nfs
, &cb_data
);
783 return cb_data
.status
;
791 static void opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
793 struct sync_cb_data
*cb_data
= private_data
;
794 struct nfsdir
*dir
, **nfsdir
;
796 cb_data
->is_finished
= 1;
797 cb_data
->status
= status
;
800 nfs_set_error(nfs
, "opendir call failed with \"%s\"", (char *)data
);
805 nfsdir
= cb_data
->return_data
;
809 int nfs_opendir(struct nfs_context
*nfs
, const char *path
, struct nfsdir
**nfsdir
)
811 struct sync_cb_data cb_data
;
813 cb_data
.is_finished
= 0;
814 cb_data
.return_data
= nfsdir
;
816 if (nfs_opendir_async(nfs
, path
, opendir_cb
, &cb_data
) != 0) {
817 nfs_set_error(nfs
, "nfs_opendir_async failed");
821 wait_for_nfs_reply(nfs
, &cb_data
);
823 return cb_data
.status
;
830 static void lseek_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
832 struct sync_cb_data
*cb_data
= private_data
;
834 cb_data
->is_finished
= 1;
835 cb_data
->status
= status
;
838 nfs_set_error(nfs
, "lseek call failed with \"%s\"", (char *)data
);
842 if (cb_data
->return_data
!= NULL
) {
843 memcpy(cb_data
->return_data
, data
, sizeof(uint64_t));
847 int nfs_lseek(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int64_t offset
, int whence
, uint64_t *current_offset
)
849 struct sync_cb_data cb_data
;
851 cb_data
.is_finished
= 0;
852 cb_data
.return_data
= current_offset
;
854 if (nfs_lseek_async(nfs
, nfsfh
, offset
, whence
, lseek_cb
, &cb_data
) != 0) {
855 nfs_set_error(nfs
, "nfs_lseek_async failed");
859 wait_for_nfs_reply(nfs
, &cb_data
);
861 return cb_data
.status
;
869 static void statvfs_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
871 struct sync_cb_data
*cb_data
= private_data
;
873 cb_data
->is_finished
= 1;
874 cb_data
->status
= status
;
877 nfs_set_error(nfs
, "statvfs call failed with \"%s\"", (char *)data
);
881 memcpy(cb_data
->return_data
, data
, sizeof(struct statvfs
));
884 int nfs_statvfs(struct nfs_context
*nfs
, const char *path
, struct statvfs
*svfs
)
886 struct sync_cb_data cb_data
;
888 cb_data
.is_finished
= 0;
889 cb_data
.return_data
= svfs
;
891 if (nfs_statvfs_async(nfs
, path
, statvfs_cb
, &cb_data
) != 0) {
892 nfs_set_error(nfs
, "nfs_statvfs_async failed");
896 wait_for_nfs_reply(nfs
, &cb_data
);
898 return cb_data
.status
;
908 static void readlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
910 struct sync_cb_data
*cb_data
= private_data
;
912 cb_data
->is_finished
= 1;
913 cb_data
->status
= status
;
916 nfs_set_error(nfs
, "readlink call failed with \"%s\"", (char *)data
);
920 if (strlen(data
) > (size_t)cb_data
->return_int
) {
921 nfs_set_error(nfs
, "Too small buffer for readlink");
922 cb_data
->status
= -ENAMETOOLONG
;
926 memcpy(cb_data
->return_data
, data
, strlen(data
)+1);
929 int nfs_readlink(struct nfs_context
*nfs
, const char *path
, char *buf
, int bufsize
)
931 struct sync_cb_data cb_data
;
933 cb_data
.is_finished
= 0;
934 cb_data
.return_data
= buf
;
935 cb_data
.return_int
= bufsize
;
937 if (nfs_readlink_async(nfs
, path
, readlink_cb
, &cb_data
) != 0) {
938 nfs_set_error(nfs
, "nfs_readlink_async failed");
942 wait_for_nfs_reply(nfs
, &cb_data
);
944 return cb_data
.status
;
952 static void chmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
954 struct sync_cb_data
*cb_data
= private_data
;
956 cb_data
->is_finished
= 1;
957 cb_data
->status
= status
;
960 nfs_set_error(nfs
, "chmod call failed with \"%s\"", (char *)data
);
965 int nfs_chmod(struct nfs_context
*nfs
, const char *path
, int mode
)
967 struct sync_cb_data cb_data
;
969 cb_data
.is_finished
= 0;
971 if (nfs_chmod_async(nfs
, path
, mode
, chmod_cb
, &cb_data
) != 0) {
972 nfs_set_error(nfs
, "nfs_chmod_async failed");
976 wait_for_nfs_reply(nfs
, &cb_data
);
978 return cb_data
.status
;
987 static void fchmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
989 struct sync_cb_data
*cb_data
= private_data
;
991 cb_data
->is_finished
= 1;
992 cb_data
->status
= status
;
995 nfs_set_error(nfs
, "fchmod call failed with \"%s\"", (char *)data
);
1000 int nfs_fchmod(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
)
1002 struct sync_cb_data cb_data
;
1004 cb_data
.is_finished
= 0;
1006 if (nfs_fchmod_async(nfs
, nfsfh
, mode
, fchmod_cb
, &cb_data
) != 0) {
1007 nfs_set_error(nfs
, "nfs_fchmod_async failed");
1011 wait_for_nfs_reply(nfs
, &cb_data
);
1013 return cb_data
.status
;
1022 static void chown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1024 struct sync_cb_data
*cb_data
= private_data
;
1026 cb_data
->is_finished
= 1;
1027 cb_data
->status
= status
;
1030 nfs_set_error(nfs
, "chown call failed with \"%s\"", (char *)data
);
1035 int nfs_chown(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
)
1037 struct sync_cb_data cb_data
;
1039 cb_data
.is_finished
= 0;
1041 if (nfs_chown_async(nfs
, path
, uid
, gid
, chown_cb
, &cb_data
) != 0) {
1042 nfs_set_error(nfs
, "nfs_chown_async failed");
1046 wait_for_nfs_reply(nfs
, &cb_data
);
1048 return cb_data
.status
;
1054 static void fchown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1056 struct sync_cb_data
*cb_data
= private_data
;
1058 cb_data
->is_finished
= 1;
1059 cb_data
->status
= status
;
1062 nfs_set_error(nfs
, "fchown call failed with \"%s\"", (char *)data
);
1067 int nfs_fchown(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
)
1069 struct sync_cb_data cb_data
;
1071 cb_data
.is_finished
= 0;
1073 if (nfs_fchown_async(nfs
, nfsfh
, uid
, gid
, fchown_cb
, &cb_data
) != 0) {
1074 nfs_set_error(nfs
, "nfs_fchown_async failed");
1078 wait_for_nfs_reply(nfs
, &cb_data
);
1080 return cb_data
.status
;
1088 static void utimes_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1090 struct sync_cb_data
*cb_data
= private_data
;
1092 cb_data
->is_finished
= 1;
1093 cb_data
->status
= status
;
1096 nfs_set_error(nfs
, "utimes call failed with \"%s\"", (char *)data
);
1101 int nfs_utimes(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
)
1103 struct sync_cb_data cb_data
;
1105 cb_data
.is_finished
= 0;
1107 if (nfs_utimes_async(nfs
, path
, times
, utimes_cb
, &cb_data
) != 0) {
1108 nfs_set_error(nfs
, "nfs_utimes_async failed");
1112 wait_for_nfs_reply(nfs
, &cb_data
);
1114 return cb_data
.status
;
1122 static void utime_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1124 struct sync_cb_data
*cb_data
= private_data
;
1126 cb_data
->is_finished
= 1;
1127 cb_data
->status
= status
;
1130 nfs_set_error(nfs
, "utime call failed with \"%s\"", (char *)data
);
1135 int nfs_utime(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
)
1137 struct sync_cb_data cb_data
;
1139 cb_data
.is_finished
= 0;
1141 if (nfs_utime_async(nfs
, path
, times
, utime_cb
, &cb_data
) != 0) {
1142 nfs_set_error(nfs
, "nfs_utimes_async failed");
1146 wait_for_nfs_reply(nfs
, &cb_data
);
1148 return cb_data
.status
;
1157 static void access_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1159 struct sync_cb_data
*cb_data
= private_data
;
1161 cb_data
->is_finished
= 1;
1162 cb_data
->status
= status
;
1165 nfs_set_error(nfs
, "access call failed with \"%s\"", (char *)data
);
1170 int nfs_access(struct nfs_context
*nfs
, const char *path
, int mode
)
1172 struct sync_cb_data cb_data
;
1174 cb_data
.is_finished
= 0;
1176 if (nfs_access_async(nfs
, path
, mode
, access_cb
, &cb_data
) != 0) {
1177 nfs_set_error(nfs
, "nfs_access_async failed");
1181 wait_for_nfs_reply(nfs
, &cb_data
);
1183 return cb_data
.status
;
1191 static void symlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1193 struct sync_cb_data
*cb_data
= private_data
;
1195 cb_data
->is_finished
= 1;
1196 cb_data
->status
= status
;
1199 nfs_set_error(nfs
, "symlink call failed with \"%s\"", (char *)data
);
1204 int nfs_symlink(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1206 struct sync_cb_data cb_data
;
1208 cb_data
.is_finished
= 0;
1210 if (nfs_symlink_async(nfs
, oldpath
, newpath
, symlink_cb
, &cb_data
) != 0) {
1211 nfs_set_error(nfs
, "nfs_symlink_async failed");
1215 wait_for_nfs_reply(nfs
, &cb_data
);
1217 return cb_data
.status
;
1225 static void rename_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1227 struct sync_cb_data
*cb_data
= private_data
;
1229 cb_data
->is_finished
= 1;
1230 cb_data
->status
= status
;
1233 nfs_set_error(nfs
, "rename call failed with \"%s\"", (char *)data
);
1238 int nfs_rename(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1240 struct sync_cb_data cb_data
;
1242 cb_data
.is_finished
= 0;
1244 if (nfs_rename_async(nfs
, oldpath
, newpath
, rename_cb
, &cb_data
) != 0) {
1245 nfs_set_error(nfs
, "nfs_rename_async failed");
1249 wait_for_nfs_reply(nfs
, &cb_data
);
1251 return cb_data
.status
;
1259 static void link_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1261 struct sync_cb_data
*cb_data
= private_data
;
1263 cb_data
->is_finished
= 1;
1264 cb_data
->status
= status
;
1267 nfs_set_error(nfs
, "link call failed with \"%s\"", (char *)data
);
1272 int nfs_link(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1274 struct sync_cb_data cb_data
;
1276 cb_data
.is_finished
= 0;
1278 if (nfs_link_async(nfs
, oldpath
, newpath
, link_cb
, &cb_data
) != 0) {
1279 nfs_set_error(nfs
, "nfs_link_async failed");
1283 wait_for_nfs_reply(nfs
, &cb_data
);
1285 return cb_data
.status
;
1288 void mount_getexports_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
1290 struct sync_cb_data
*cb_data
= private_data
;
1293 assert(mount_context
->magic
== RPC_CONTEXT_MAGIC
);
1295 cb_data
->is_finished
= 1;
1296 cb_data
->status
= status
;
1297 cb_data
->return_data
= NULL
;
1300 rpc_set_error(mount_context
, "mount/export call failed with \"%s\"", (char *)data
);
1304 export
= *(exports
*)data
;
1305 while (export
!= NULL
) {
1308 new_export
= malloc(sizeof(*new_export
));
1309 memset(new_export
, 0, sizeof(*new_export
));
1310 new_export
->ex_dir
= strdup(export
->ex_dir
);
1311 new_export
->ex_next
= cb_data
->return_data
;
1313 cb_data
->return_data
= new_export
;
1315 export
= export
->ex_next
;
1319 struct exportnode
*mount_getexports(const char *server
)
1321 struct sync_cb_data cb_data
;
1322 struct rpc_context
*rpc
;
1325 cb_data
.is_finished
= 0;
1326 cb_data
.return_data
= NULL
;
1328 rpc
= rpc_init_context();
1329 if (mount_getexports_async(rpc
, server
, mount_getexports_cb
, &cb_data
) != 0) {
1330 rpc_destroy_context(rpc
);
1334 wait_for_reply(rpc
, &cb_data
);
1335 rpc_destroy_context(rpc
);
1337 return cb_data
.return_data
;
1340 void mount_free_export_list(struct exportnode
*exports
)
1342 struct exportnode
*tmp
;
1344 while ((tmp
= exports
)) {
1345 exports
= exports
->ex_next
;
1353 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
1355 while (srv
!= NULL
) {
1356 struct nfs_server_list
*next
= srv
->next
;
1364 struct nfs_list_data
{
1366 struct nfs_server_list
*srvrs
;
1369 void callit_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
1371 struct nfs_list_data
*srv_data
= private_data
;
1372 struct sockaddr
*sin
;
1374 struct nfs_server_list
*srvr
;
1376 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1378 if (status
== RPC_STATUS_CANCEL
) {
1382 srv_data
->status
= -1;
1386 sin
= rpc_get_recv_sockaddr(rpc
);
1388 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
1389 srv_data
->status
= -1;
1393 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1394 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
1395 srv_data
->status
= -1;
1399 /* check for dupes */
1400 for (srvr
= srv_data
->srvrs
; srvr
; srvr
= srvr
->next
) {
1401 if (!strcmp(hostdd
, srvr
->addr
)) {
1406 srvr
= malloc(sizeof(struct nfs_server_list
));
1408 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
1409 srv_data
->status
= -1;
1413 srvr
->addr
= strdup(hostdd
);
1414 if (srvr
->addr
== NULL
) {
1415 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
1417 srv_data
->status
= -1;
1421 srvr
->next
= srv_data
->srvrs
;
1422 srv_data
->srvrs
= srvr
;
1427 static int send_nfsd_probes(struct rpc_context
*rpc
, INTERFACE_INFO
*InterfaceList
, int numIfs
, struct nfs_list_data
*data
)
1431 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1433 for(i
= 0; i
< numIfs
; i
++)
1437 unsigned long nFlags
= 0;
1439 pAddress
= (SOCKADDR
*) & (InterfaceList
[i
].iiBroadcastAddress
);
1441 if(pAddress
->sa_family
!= AF_INET
)
1444 nFlags
= InterfaceList
[i
].iiFlags
;
1446 if (!(nFlags
& IFF_UP
))
1451 if (nFlags
& IFF_LOOPBACK
)
1456 if (!(nFlags
& IFF_BROADCAST
))
1461 if (getnameinfo(pAddress
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0)
1466 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0)
1471 if (rpc_pmap2_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0)
1479 struct nfs_server_list
*nfs_find_local_servers(void)
1481 struct rpc_context
*rpc
;
1482 struct nfs_list_data data
= {0, NULL
};
1483 struct timeval tv_start
, tv_current
;
1486 INTERFACE_INFO InterfaceList
[20];
1487 unsigned long nBytesReturned
;
1488 int nNumInterfaces
= 0;
1490 rpc
= rpc_init_udp_context();
1496 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0)
1498 rpc_destroy_context(rpc
);
1502 if (WSAIoctl(rpc_get_fd(rpc
), SIO_GET_INTERFACE_LIST
, 0, 0, &InterfaceList
, sizeof(InterfaceList
), &nBytesReturned
, 0, 0) == SOCKET_ERROR
)
1507 nNumInterfaces
= nBytesReturned
/ sizeof(INTERFACE_INFO
);
1509 for (loop
=0; loop
<3; loop
++)
1511 if (send_nfsd_probes(rpc
, InterfaceList
, nNumInterfaces
, &data
) != 0)
1513 rpc_destroy_context(rpc
);
1517 win32_gettimeofday(&tv_start
, NULL
);
1522 pfd
.fd
= rpc_get_fd(rpc
);
1523 pfd
.events
= rpc_which_events(rpc
);
1525 win32_gettimeofday(&tv_current
, NULL
);
1527 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1528 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1530 if (poll(&pfd
, 1, mpt
) < 0)
1532 free_nfs_srvr_list(data
.srvrs
);
1533 rpc_destroy_context(rpc
);
1536 if (pfd
.revents
== 0)
1541 if (rpc_service(rpc
, pfd
.revents
) < 0)
1548 rpc_destroy_context(rpc
);
1550 if (data
.status
!= 0)
1552 free_nfs_srvr_list(data
.srvrs
);
1559 static int send_nfsd_probes(struct rpc_context
*rpc
, struct ifconf
*ifc
, struct nfs_list_data
*data
)
1563 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1565 for (ptr
=(char *)(ifc
->ifc_buf
); ptr
< (char *)(ifc
->ifc_buf
) + ifc
->ifc_len
; ) {
1569 ifr
= (struct ifreq
*)ptr
;
1570 #ifdef HAVE_SOCKADDR_LEN
1571 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
1572 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
1574 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
1577 ptr
+= sizeof(struct ifreq
);
1580 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
1583 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
1586 if (!(ifr
->ifr_flags
& IFF_UP
)) {
1589 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
1592 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
1595 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
1598 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1601 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
1605 if (rpc_pmap2_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0) {
1613 struct nfs_server_list
*nfs_find_local_servers(void)
1615 struct rpc_context
*rpc
;
1616 struct nfs_list_data data
= {0, NULL
};
1617 struct timeval tv_start
, tv_current
;
1622 rpc
= rpc_init_udp_context();
1627 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
1628 rpc_destroy_context(rpc
);
1633 /* get list of all interfaces */
1634 size
= sizeof(struct ifreq
);
1638 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
1643 ifc
.ifc_buf
= malloc(size
);
1644 memset(ifc
.ifc_buf
, 0, size
);
1645 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
1646 rpc_destroy_context(rpc
);
1652 for (loop
=0; loop
<3; loop
++) {
1653 if (send_nfsd_probes(rpc
, &ifc
, &data
) != 0) {
1654 rpc_destroy_context(rpc
);
1659 gettimeofday(&tv_start
, NULL
);
1663 pfd
.fd
= rpc_get_fd(rpc
);
1664 pfd
.events
= rpc_which_events(rpc
);
1666 gettimeofday(&tv_current
, NULL
);
1668 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1669 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1671 if (poll(&pfd
, 1, mpt
) < 0) {
1672 free_nfs_srvr_list(data
.srvrs
);
1673 rpc_destroy_context(rpc
);
1676 if (pfd
.revents
== 0) {
1680 if (rpc_service(rpc
, pfd
.revents
) < 0) {
1687 rpc_destroy_context(rpc
);
1689 if (data
.status
!= 0) {
1690 free_nfs_srvr_list(data
.srvrs
);