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"
37 #define statvfs statfs
44 #ifdef HAVE_SYS_STATVFS_H
45 #include <sys/statvfs.h>
48 #ifdef HAVE_SYS_IOCTL_H
49 #include <sys/ioctl.h>
52 #ifdef HAVE_SYS_SOCKET_H
53 #include <sys/socket.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"
105 static void wait_for_reply(struct rpc_context
*rpc
, struct sync_cb_data
*cb_data
)
109 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
111 while (!cb_data
->is_finished
) {
113 pfd
.fd
= rpc_get_fd(rpc
);
114 pfd
.events
= rpc_which_events(rpc
);
115 if (poll(&pfd
, 1, -1) < 0) {
116 rpc_set_error(rpc
, "Poll failed");
117 cb_data
->status
= -EIO
;
120 if (rpc_service(rpc
, pfd
.revents
) < 0) {
121 rpc_set_error(rpc
, "rpc_service failed");
122 cb_data
->status
= -EIO
;
125 if (rpc_get_fd(rpc
) == -1) {
126 rpc_set_error(rpc
, "Socket closed\n");
132 static void wait_for_nfs_reply(struct nfs_context
*nfs
, struct sync_cb_data
*cb_data
)
136 while (!cb_data
->is_finished
) {
138 pfd
.fd
= nfs_get_fd(nfs
);
139 pfd
.events
= nfs_which_events(nfs
);
140 if (poll(&pfd
, 1, -1) < 0) {
141 nfs_set_error(nfs
, "Poll failed");
142 cb_data
->status
= -EIO
;
145 if (nfs_service(nfs
, pfd
.revents
) < 0) {
146 nfs_set_error(nfs
, "nfs_service failed");
147 cb_data
->status
= -EIO
;
159 * connect to the server and mount the export
161 static void mount_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
163 struct sync_cb_data
*cb_data
= private_data
;
165 cb_data
->is_finished
= 1;
166 cb_data
->status
= status
;
169 nfs_set_error(nfs
, "mount/mnt call failed with \"%s\"", (char *)data
);
174 int nfs_mount(struct nfs_context
*nfs
, const char *server
, const char *export
)
176 struct sync_cb_data cb_data
;
177 struct rpc_context
*rpc
= nfs_get_rpc_context(nfs
);
179 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
181 cb_data
.is_finished
= 0;
183 if (nfs_mount_async(nfs
, server
, export
, mount_cb
, &cb_data
) != 0) {
184 nfs_set_error(nfs
, "nfs_mount_async failed");
188 wait_for_nfs_reply(nfs
, &cb_data
);
190 /* Dont want any more callbacks even if the socket is closed */
191 rpc
->connect_cb
= NULL
;
193 return cb_data
.status
;
200 static void stat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
202 struct sync_cb_data
*cb_data
= private_data
;
204 cb_data
->is_finished
= 1;
205 cb_data
->status
= status
;
208 nfs_set_error(nfs
, "stat call failed with \"%s\"", (char *)data
);
212 memcpy(cb_data
->return_data
, data
, sizeof(struct __stat64
));
214 memcpy(cb_data
->return_data
, data
, sizeof(struct stat
));
219 int nfs_stat(struct nfs_context
*nfs
, const char *path
, struct __stat64
*st
)
221 int nfs_stat(struct nfs_context
*nfs
, const char *path
, struct stat
*st
)
224 struct sync_cb_data cb_data
;
226 cb_data
.is_finished
= 0;
227 cb_data
.return_data
= st
;
229 if (nfs_stat_async(nfs
, path
, stat_cb
, &cb_data
) != 0) {
230 nfs_set_error(nfs
, "nfs_stat_async failed");
234 wait_for_nfs_reply(nfs
, &cb_data
);
236 return cb_data
.status
;
245 static void open_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
247 struct sync_cb_data
*cb_data
= private_data
;
248 struct nfsfh
*fh
, **nfsfh
;
250 cb_data
->is_finished
= 1;
251 cb_data
->status
= status
;
254 nfs_set_error(nfs
, "open call failed with \"%s\"", (char *)data
);
259 nfsfh
= cb_data
->return_data
;
263 int nfs_open(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
265 struct sync_cb_data cb_data
;
267 cb_data
.is_finished
= 0;
268 cb_data
.return_data
= nfsfh
;
270 if (nfs_open_async(nfs
, path
, mode
, open_cb
, &cb_data
) != 0) {
271 nfs_set_error(nfs
, "nfs_open_async failed");
275 wait_for_nfs_reply(nfs
, &cb_data
);
277 return cb_data
.status
;
286 static void pread_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
288 struct sync_cb_data
*cb_data
= private_data
;
290 cb_data
->is_finished
= 1;
291 cb_data
->status
= status
;
294 nfs_set_error(nfs
, "pread call failed with \"%s\"", (char *)data
);
298 buffer
= cb_data
->return_data
;
299 memcpy(buffer
, (char *)data
, status
);
302 int nfs_pread(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buffer
)
304 struct sync_cb_data cb_data
;
306 cb_data
.is_finished
= 0;
307 cb_data
.return_data
= buffer
;
309 if (nfs_pread_async(nfs
, nfsfh
, offset
, count
, pread_cb
, &cb_data
) != 0) {
310 nfs_set_error(nfs
, "nfs_pread_async failed");
314 wait_for_nfs_reply(nfs
, &cb_data
);
316 return cb_data
.status
;
322 int nfs_read(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buffer
)
324 return nfs_pread(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buffer
);
330 static void close_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
332 struct sync_cb_data
*cb_data
= private_data
;
333 cb_data
->is_finished
= 1;
334 cb_data
->status
= status
;
337 nfs_set_error(nfs
, "close call failed with \"%s\"", (char *)data
);
342 int nfs_close(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
344 struct sync_cb_data cb_data
;
346 cb_data
.is_finished
= 0;
348 if (nfs_close_async(nfs
, nfsfh
, close_cb
, &cb_data
) != 0) {
349 nfs_set_error(nfs
, "nfs_close_async failed");
353 wait_for_nfs_reply(nfs
, &cb_data
);
355 return cb_data
.status
;
364 int nfs_fstat(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, struct stat
*st
)
366 struct sync_cb_data cb_data
;
368 cb_data
.is_finished
= 0;
369 cb_data
.return_data
= st
;
371 if (nfs_fstat_async(nfs
, nfsfh
, stat_cb
, &cb_data
) != 0) {
372 nfs_set_error(nfs
, "nfs_fstat_async failed");
376 wait_for_nfs_reply(nfs
, &cb_data
);
378 return cb_data
.status
;
385 static void pwrite_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
387 struct sync_cb_data
*cb_data
= private_data
;
388 cb_data
->is_finished
= 1;
389 cb_data
->status
= status
;
392 nfs_set_error(nfs
, "pwrite call failed with \"%s\"", (char *)data
);
397 int nfs_pwrite(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, uint64_t count
, char *buf
)
399 struct sync_cb_data cb_data
;
401 cb_data
.is_finished
= 0;
403 if (nfs_pwrite_async(nfs
, nfsfh
, offset
, count
, buf
, pwrite_cb
, &cb_data
) != 0) {
404 nfs_set_error(nfs
, "nfs_pwrite_async failed");
408 wait_for_nfs_reply(nfs
, &cb_data
);
410 return cb_data
.status
;
416 int nfs_write(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t count
, char *buf
)
418 return nfs_pwrite(nfs
, nfsfh
, nfs_get_current_offset(nfsfh
), count
, buf
);
425 static void fsync_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
427 struct sync_cb_data
*cb_data
= private_data
;
428 cb_data
->is_finished
= 1;
429 cb_data
->status
= status
;
432 nfs_set_error(nfs
, "fsync call failed with \"%s\"", (char *)data
);
437 int nfs_fsync(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
)
439 struct sync_cb_data cb_data
;
441 cb_data
.is_finished
= 0;
443 if (nfs_fsync_async(nfs
, nfsfh
, fsync_cb
, &cb_data
) != 0) {
444 nfs_set_error(nfs
, "nfs_fsync_async failed");
448 wait_for_nfs_reply(nfs
, &cb_data
);
450 return cb_data
.status
;
459 static void ftruncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
461 struct sync_cb_data
*cb_data
= private_data
;
462 cb_data
->is_finished
= 1;
463 cb_data
->status
= status
;
466 nfs_set_error(nfs
, "ftruncate call failed with \"%s\"", (char *)data
);
471 int nfs_ftruncate(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t length
)
473 struct sync_cb_data cb_data
;
475 cb_data
.is_finished
= 0;
477 if (nfs_ftruncate_async(nfs
, nfsfh
, length
, ftruncate_cb
, &cb_data
) != 0) {
478 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
482 wait_for_nfs_reply(nfs
, &cb_data
);
484 return cb_data
.status
;
492 static void truncate_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
494 struct sync_cb_data
*cb_data
= private_data
;
495 cb_data
->is_finished
= 1;
496 cb_data
->status
= status
;
499 nfs_set_error(nfs
, "truncate call failed with \"%s\"", (char *)data
);
504 int nfs_truncate(struct nfs_context
*nfs
, const char *path
, uint64_t length
)
506 struct sync_cb_data cb_data
;
508 cb_data
.is_finished
= 0;
510 if (nfs_truncate_async(nfs
, path
, length
, truncate_cb
, &cb_data
) != 0) {
511 nfs_set_error(nfs
, "nfs_ftruncate_async failed");
515 wait_for_nfs_reply(nfs
, &cb_data
);
517 return cb_data
.status
;
527 static void mkdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
529 struct sync_cb_data
*cb_data
= private_data
;
530 cb_data
->is_finished
= 1;
531 cb_data
->status
= status
;
534 nfs_set_error(nfs
, "mkdir call failed with \"%s\"", (char *)data
);
539 int nfs_mkdir(struct nfs_context
*nfs
, const char *path
)
541 struct sync_cb_data cb_data
;
543 cb_data
.is_finished
= 0;
545 if (nfs_mkdir_async(nfs
, path
, mkdir_cb
, &cb_data
) != 0) {
546 nfs_set_error(nfs
, "nfs_mkdir_async failed");
550 wait_for_nfs_reply(nfs
, &cb_data
);
552 return cb_data
.status
;
562 static void rmdir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
564 struct sync_cb_data
*cb_data
= private_data
;
565 cb_data
->is_finished
= 1;
566 cb_data
->status
= status
;
569 nfs_set_error(nfs
, "rmdir call failed with \"%s\"", (char *)data
);
574 int nfs_rmdir(struct nfs_context
*nfs
, const char *path
)
576 struct sync_cb_data cb_data
;
578 cb_data
.is_finished
= 0;
580 if (nfs_rmdir_async(nfs
, path
, rmdir_cb
, &cb_data
) != 0) {
581 nfs_set_error(nfs
, "nfs_rmdir_async failed");
585 wait_for_nfs_reply(nfs
, &cb_data
);
587 return cb_data
.status
;
595 static void creat_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
597 struct sync_cb_data
*cb_data
= private_data
;
598 struct nfsfh
*fh
, **nfsfh
;
600 cb_data
->is_finished
= 1;
601 cb_data
->status
= status
;
604 nfs_set_error(nfs
, "creat call failed with \"%s\"", (char *)data
);
609 nfsfh
= cb_data
->return_data
;
613 int nfs_creat(struct nfs_context
*nfs
, const char *path
, int mode
, struct nfsfh
**nfsfh
)
615 struct sync_cb_data cb_data
;
617 cb_data
.is_finished
= 0;
618 cb_data
.return_data
= nfsfh
;
620 if (nfs_creat_async(nfs
, path
, mode
, creat_cb
, &cb_data
) != 0) {
621 nfs_set_error(nfs
, "nfs_creat_async failed");
625 wait_for_nfs_reply(nfs
, &cb_data
);
627 return cb_data
.status
;
633 static void mknod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
635 struct sync_cb_data
*cb_data
= private_data
;
637 cb_data
->is_finished
= 1;
638 cb_data
->status
= status
;
641 nfs_set_error(nfs
, "mknod call failed with \"%s\"", (char *)data
);
646 int nfs_mknod(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
)
648 struct sync_cb_data cb_data
;
650 cb_data
.is_finished
= 0;
652 if (nfs_mknod_async(nfs
, path
, mode
, dev
, mknod_cb
, &cb_data
) != 0) {
653 nfs_set_error(nfs
, "nfs_creat_async failed");
657 wait_for_nfs_reply(nfs
, &cb_data
);
659 return cb_data
.status
;
666 static void unlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
668 struct sync_cb_data
*cb_data
= private_data
;
670 cb_data
->is_finished
= 1;
671 cb_data
->status
= status
;
674 nfs_set_error(nfs
, "unlink call failed with \"%s\"", (char *)data
);
679 int nfs_unlink(struct nfs_context
*nfs
, const char *path
)
681 struct sync_cb_data cb_data
;
683 cb_data
.is_finished
= 0;
685 if (nfs_unlink_async(nfs
, path
, unlink_cb
, &cb_data
) != 0) {
686 nfs_set_error(nfs
, "nfs_unlink_async failed");
690 wait_for_nfs_reply(nfs
, &cb_data
);
692 return cb_data
.status
;
700 static void opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
702 struct sync_cb_data
*cb_data
= private_data
;
703 struct nfsdir
*dir
, **nfsdir
;
705 cb_data
->is_finished
= 1;
706 cb_data
->status
= status
;
709 nfs_set_error(nfs
, "opendir call failed with \"%s\"", (char *)data
);
714 nfsdir
= cb_data
->return_data
;
718 int nfs_opendir(struct nfs_context
*nfs
, const char *path
, struct nfsdir
**nfsdir
)
720 struct sync_cb_data cb_data
;
722 cb_data
.is_finished
= 0;
723 cb_data
.return_data
= nfsdir
;
725 if (nfs_opendir_async(nfs
, path
, opendir_cb
, &cb_data
) != 0) {
726 nfs_set_error(nfs
, "nfs_opendir_async failed");
730 wait_for_nfs_reply(nfs
, &cb_data
);
732 return cb_data
.status
;
739 static void lseek_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
741 struct sync_cb_data
*cb_data
= private_data
;
743 cb_data
->is_finished
= 1;
744 cb_data
->status
= status
;
747 nfs_set_error(nfs
, "lseek call failed with \"%s\"", (char *)data
);
751 if (cb_data
->return_data
!= NULL
) {
752 memcpy(cb_data
->return_data
, data
, sizeof(uint64_t));
756 int nfs_lseek(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, uint64_t offset
, int whence
, uint64_t *current_offset
)
758 struct sync_cb_data cb_data
;
760 cb_data
.is_finished
= 0;
761 cb_data
.return_data
= current_offset
;
763 if (nfs_lseek_async(nfs
, nfsfh
, offset
, whence
, lseek_cb
, &cb_data
) != 0) {
764 nfs_set_error(nfs
, "nfs_lseek_async failed");
768 wait_for_nfs_reply(nfs
, &cb_data
);
770 return cb_data
.status
;
778 static void statvfs_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
780 struct sync_cb_data
*cb_data
= private_data
;
782 cb_data
->is_finished
= 1;
783 cb_data
->status
= status
;
786 nfs_set_error(nfs
, "statvfs call failed with \"%s\"", (char *)data
);
790 memcpy(cb_data
->return_data
, data
, sizeof(struct statvfs
));
793 int nfs_statvfs(struct nfs_context
*nfs
, const char *path
, struct statvfs
*svfs
)
795 struct sync_cb_data cb_data
;
797 cb_data
.is_finished
= 0;
798 cb_data
.return_data
= svfs
;
800 if (nfs_statvfs_async(nfs
, path
, statvfs_cb
, &cb_data
) != 0) {
801 nfs_set_error(nfs
, "nfs_statvfs_async failed");
805 wait_for_nfs_reply(nfs
, &cb_data
);
807 return cb_data
.status
;
817 static void readlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
819 struct sync_cb_data
*cb_data
= private_data
;
821 cb_data
->is_finished
= 1;
822 cb_data
->status
= status
;
825 nfs_set_error(nfs
, "readlink call failed with \"%s\"", (char *)data
);
829 if (strlen(data
) > (size_t)cb_data
->return_int
) {
830 nfs_set_error(nfs
, "Too small buffer for readlink");
831 cb_data
->status
= -ENAMETOOLONG
;
835 memcpy(cb_data
->return_data
, data
, strlen(data
)+1);
838 int nfs_readlink(struct nfs_context
*nfs
, const char *path
, char *buf
, int bufsize
)
840 struct sync_cb_data cb_data
;
842 cb_data
.is_finished
= 0;
843 cb_data
.return_data
= buf
;
844 cb_data
.return_int
= bufsize
;
846 if (nfs_readlink_async(nfs
, path
, readlink_cb
, &cb_data
) != 0) {
847 nfs_set_error(nfs
, "nfs_readlink_async failed");
851 wait_for_nfs_reply(nfs
, &cb_data
);
853 return cb_data
.status
;
861 static void chmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
863 struct sync_cb_data
*cb_data
= private_data
;
865 cb_data
->is_finished
= 1;
866 cb_data
->status
= status
;
869 nfs_set_error(nfs
, "chmod call failed with \"%s\"", (char *)data
);
874 int nfs_chmod(struct nfs_context
*nfs
, const char *path
, int mode
)
876 struct sync_cb_data cb_data
;
878 cb_data
.is_finished
= 0;
880 if (nfs_chmod_async(nfs
, path
, mode
, chmod_cb
, &cb_data
) != 0) {
881 nfs_set_error(nfs
, "nfs_chmod_async failed");
885 wait_for_nfs_reply(nfs
, &cb_data
);
887 return cb_data
.status
;
896 static void fchmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
898 struct sync_cb_data
*cb_data
= private_data
;
900 cb_data
->is_finished
= 1;
901 cb_data
->status
= status
;
904 nfs_set_error(nfs
, "fchmod call failed with \"%s\"", (char *)data
);
909 int nfs_fchmod(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
)
911 struct sync_cb_data cb_data
;
913 cb_data
.is_finished
= 0;
915 if (nfs_fchmod_async(nfs
, nfsfh
, mode
, fchmod_cb
, &cb_data
) != 0) {
916 nfs_set_error(nfs
, "nfs_fchmod_async failed");
920 wait_for_nfs_reply(nfs
, &cb_data
);
922 return cb_data
.status
;
931 static void chown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
933 struct sync_cb_data
*cb_data
= private_data
;
935 cb_data
->is_finished
= 1;
936 cb_data
->status
= status
;
939 nfs_set_error(nfs
, "chown call failed with \"%s\"", (char *)data
);
944 int nfs_chown(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
)
946 struct sync_cb_data cb_data
;
948 cb_data
.is_finished
= 0;
950 if (nfs_chown_async(nfs
, path
, uid
, gid
, chown_cb
, &cb_data
) != 0) {
951 nfs_set_error(nfs
, "nfs_chown_async failed");
955 wait_for_nfs_reply(nfs
, &cb_data
);
957 return cb_data
.status
;
963 static void fchown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
965 struct sync_cb_data
*cb_data
= private_data
;
967 cb_data
->is_finished
= 1;
968 cb_data
->status
= status
;
971 nfs_set_error(nfs
, "fchown call failed with \"%s\"", (char *)data
);
976 int nfs_fchown(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
)
978 struct sync_cb_data cb_data
;
980 cb_data
.is_finished
= 0;
982 if (nfs_fchown_async(nfs
, nfsfh
, uid
, gid
, fchown_cb
, &cb_data
) != 0) {
983 nfs_set_error(nfs
, "nfs_fchown_async failed");
987 wait_for_nfs_reply(nfs
, &cb_data
);
989 return cb_data
.status
;
997 static void utimes_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
999 struct sync_cb_data
*cb_data
= private_data
;
1001 cb_data
->is_finished
= 1;
1002 cb_data
->status
= status
;
1005 nfs_set_error(nfs
, "utimes call failed with \"%s\"", (char *)data
);
1010 int nfs_utimes(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
)
1012 struct sync_cb_data cb_data
;
1014 cb_data
.is_finished
= 0;
1016 if (nfs_utimes_async(nfs
, path
, times
, utimes_cb
, &cb_data
) != 0) {
1017 nfs_set_error(nfs
, "nfs_utimes_async failed");
1021 wait_for_nfs_reply(nfs
, &cb_data
);
1023 return cb_data
.status
;
1031 static void utime_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1033 struct sync_cb_data
*cb_data
= private_data
;
1035 cb_data
->is_finished
= 1;
1036 cb_data
->status
= status
;
1039 nfs_set_error(nfs
, "utime call failed with \"%s\"", (char *)data
);
1044 int nfs_utime(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
)
1046 struct sync_cb_data cb_data
;
1048 cb_data
.is_finished
= 0;
1050 if (nfs_utime_async(nfs
, path
, times
, utime_cb
, &cb_data
) != 0) {
1051 nfs_set_error(nfs
, "nfs_utimes_async failed");
1055 wait_for_nfs_reply(nfs
, &cb_data
);
1057 return cb_data
.status
;
1066 static void access_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1068 struct sync_cb_data
*cb_data
= private_data
;
1070 cb_data
->is_finished
= 1;
1071 cb_data
->status
= status
;
1074 nfs_set_error(nfs
, "access call failed with \"%s\"", (char *)data
);
1079 int nfs_access(struct nfs_context
*nfs
, const char *path
, int mode
)
1081 struct sync_cb_data cb_data
;
1083 cb_data
.is_finished
= 0;
1085 if (nfs_access_async(nfs
, path
, mode
, access_cb
, &cb_data
) != 0) {
1086 nfs_set_error(nfs
, "nfs_access_async failed");
1090 wait_for_nfs_reply(nfs
, &cb_data
);
1092 return cb_data
.status
;
1100 static void symlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1102 struct sync_cb_data
*cb_data
= private_data
;
1104 cb_data
->is_finished
= 1;
1105 cb_data
->status
= status
;
1108 nfs_set_error(nfs
, "symlink call failed with \"%s\"", (char *)data
);
1113 int nfs_symlink(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1115 struct sync_cb_data cb_data
;
1117 cb_data
.is_finished
= 0;
1119 if (nfs_symlink_async(nfs
, oldpath
, newpath
, symlink_cb
, &cb_data
) != 0) {
1120 nfs_set_error(nfs
, "nfs_symlink_async failed");
1124 wait_for_nfs_reply(nfs
, &cb_data
);
1126 return cb_data
.status
;
1134 static void rename_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1136 struct sync_cb_data
*cb_data
= private_data
;
1138 cb_data
->is_finished
= 1;
1139 cb_data
->status
= status
;
1142 nfs_set_error(nfs
, "rename call failed with \"%s\"", (char *)data
);
1147 int nfs_rename(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1149 struct sync_cb_data cb_data
;
1151 cb_data
.is_finished
= 0;
1153 if (nfs_rename_async(nfs
, oldpath
, newpath
, rename_cb
, &cb_data
) != 0) {
1154 nfs_set_error(nfs
, "nfs_rename_async failed");
1158 wait_for_nfs_reply(nfs
, &cb_data
);
1160 return cb_data
.status
;
1168 static void link_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1170 struct sync_cb_data
*cb_data
= private_data
;
1172 cb_data
->is_finished
= 1;
1173 cb_data
->status
= status
;
1176 nfs_set_error(nfs
, "link call failed with \"%s\"", (char *)data
);
1181 int nfs_link(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1183 struct sync_cb_data cb_data
;
1185 cb_data
.is_finished
= 0;
1187 if (nfs_link_async(nfs
, oldpath
, newpath
, link_cb
, &cb_data
) != 0) {
1188 nfs_set_error(nfs
, "nfs_link_async failed");
1192 wait_for_nfs_reply(nfs
, &cb_data
);
1194 return cb_data
.status
;
1197 void mount_getexports_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
1199 struct sync_cb_data
*cb_data
= private_data
;
1202 assert(mount_context
->magic
== RPC_CONTEXT_MAGIC
);
1204 cb_data
->is_finished
= 1;
1205 cb_data
->status
= status
;
1206 cb_data
->return_data
= NULL
;
1209 rpc_set_error(mount_context
, "mount/export call failed with \"%s\"", (char *)data
);
1213 export
= *(exports
*)data
;
1214 while (export
!= NULL
) {
1217 new_export
= malloc(sizeof(*new_export
));
1218 memset(new_export
, 0, sizeof(*new_export
));
1219 new_export
->ex_dir
= strdup(export
->ex_dir
);
1220 new_export
->ex_next
= cb_data
->return_data
;
1222 cb_data
->return_data
= new_export
;
1224 export
= export
->ex_next
;
1228 struct exportnode
*mount_getexports(const char *server
)
1230 struct sync_cb_data cb_data
;
1231 struct rpc_context
*rpc
;
1234 cb_data
.is_finished
= 0;
1235 cb_data
.return_data
= NULL
;
1237 rpc
= rpc_init_context();
1238 if (mount_getexports_async(rpc
, server
, mount_getexports_cb
, &cb_data
) != 0) {
1239 rpc_destroy_context(rpc
);
1243 wait_for_reply(rpc
, &cb_data
);
1244 rpc_destroy_context(rpc
);
1246 return cb_data
.return_data
;
1249 void mount_free_export_list(struct exportnode
*exports
)
1251 struct exportnode
*tmp
;
1253 while ((tmp
= exports
)) {
1254 exports
= exports
->ex_next
;
1262 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
1264 while (srv
!= NULL
) {
1265 struct nfs_server_list
*next
= srv
->next
;
1273 struct nfs_list_data
{
1275 struct nfs_server_list
*srvrs
;
1278 void callit_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
1280 struct nfs_list_data
*srv_data
= private_data
;
1281 struct sockaddr
*sin
;
1283 struct nfs_server_list
*srvr
;
1285 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1287 if (status
== RPC_STATUS_CANCEL
) {
1291 srv_data
->status
= -1;
1295 sin
= rpc_get_recv_sockaddr(rpc
);
1297 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
1298 srv_data
->status
= -1;
1302 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1303 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
1304 srv_data
->status
= -1;
1308 /* check for dupes */
1309 for (srvr
= srv_data
->srvrs
; srvr
; srvr
= srvr
->next
) {
1310 if (!strcmp(hostdd
, srvr
->addr
)) {
1315 srvr
= malloc(sizeof(struct nfs_server_list
));
1317 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
1318 srv_data
->status
= -1;
1322 srvr
->addr
= strdup(hostdd
);
1323 if (srvr
->addr
== NULL
) {
1324 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
1326 srv_data
->status
= -1;
1330 srvr
->next
= srv_data
->srvrs
;
1331 srv_data
->srvrs
= srvr
;
1336 static int send_nfsd_probes(struct rpc_context
*rpc
, INTERFACE_INFO
*InterfaceList
, int numIfs
, struct nfs_list_data
*data
)
1340 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1342 for(i
= 0; i
< numIfs
; i
++)
1346 unsigned long nFlags
= 0;
1348 pAddress
= (SOCKADDR
*) & (InterfaceList
[i
].iiBroadcastAddress
);
1350 if(pAddress
->sa_family
!= AF_INET
)
1353 nFlags
= InterfaceList
[i
].iiFlags
;
1355 if (!(nFlags
& IFF_UP
))
1360 if (nFlags
& IFF_LOOPBACK
)
1365 if (!(nFlags
& IFF_BROADCAST
))
1370 if (getnameinfo(pAddress
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0)
1375 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0)
1380 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0)
1388 struct nfs_server_list
*nfs_find_local_servers(void)
1390 struct rpc_context
*rpc
;
1391 struct nfs_list_data data
= {0, NULL
};
1392 struct timeval tv_start
, tv_current
;
1395 INTERFACE_INFO InterfaceList
[20];
1396 unsigned long nBytesReturned
;
1397 int nNumInterfaces
= 0;
1399 rpc
= rpc_init_udp_context();
1405 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0)
1407 rpc_destroy_context(rpc
);
1411 if (WSAIoctl(rpc_get_fd(rpc
), SIO_GET_INTERFACE_LIST
, 0, 0, &InterfaceList
, sizeof(InterfaceList
), &nBytesReturned
, 0, 0) == SOCKET_ERROR
)
1416 nNumInterfaces
= nBytesReturned
/ sizeof(INTERFACE_INFO
);
1418 for (loop
=0; loop
<3; loop
++)
1420 if (send_nfsd_probes(rpc
, InterfaceList
, nNumInterfaces
, &data
) != 0)
1422 rpc_destroy_context(rpc
);
1426 win32_gettimeofday(&tv_start
, NULL
);
1431 pfd
.fd
= rpc_get_fd(rpc
);
1432 pfd
.events
= rpc_which_events(rpc
);
1434 win32_gettimeofday(&tv_current
, NULL
);
1436 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1437 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1439 if (poll(&pfd
, 1, mpt
) < 0)
1441 free_nfs_srvr_list(data
.srvrs
);
1442 rpc_destroy_context(rpc
);
1445 if (pfd
.revents
== 0)
1450 if (rpc_service(rpc
, pfd
.revents
) < 0)
1457 rpc_destroy_context(rpc
);
1459 if (data
.status
!= 0)
1461 free_nfs_srvr_list(data
.srvrs
);
1468 static int send_nfsd_probes(struct rpc_context
*rpc
, struct ifconf
*ifc
, struct nfs_list_data
*data
)
1472 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1474 for (ptr
=(char *)(ifc
->ifc_buf
); ptr
< (char *)(ifc
->ifc_buf
) + ifc
->ifc_len
; ) {
1478 ifr
= (struct ifreq
*)ptr
;
1479 #ifdef HAVE_SOCKADDR_LEN
1480 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
1481 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
1483 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
1486 ptr
+= sizeof(struct ifreq
);
1489 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
1492 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
1495 if (!(ifr
->ifr_flags
& IFF_UP
)) {
1498 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
1501 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
1504 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
1507 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1510 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
1514 if (rpc_pmap_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0) {
1522 struct nfs_server_list
*nfs_find_local_servers(void)
1524 struct rpc_context
*rpc
;
1525 struct nfs_list_data data
= {0, NULL
};
1526 struct timeval tv_start
, tv_current
;
1531 rpc
= rpc_init_udp_context();
1536 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
1537 rpc_destroy_context(rpc
);
1542 /* get list of all interfaces */
1543 size
= sizeof(struct ifreq
);
1547 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
1552 ifc
.ifc_buf
= malloc(size
);
1553 memset(ifc
.ifc_buf
, 0, size
);
1554 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
1555 rpc_destroy_context(rpc
);
1561 for (loop
=0; loop
<3; loop
++) {
1562 if (send_nfsd_probes(rpc
, &ifc
, &data
) != 0) {
1563 rpc_destroy_context(rpc
);
1568 gettimeofday(&tv_start
, NULL
);
1572 pfd
.fd
= rpc_get_fd(rpc
);
1573 pfd
.events
= rpc_which_events(rpc
);
1575 gettimeofday(&tv_current
, NULL
);
1577 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1578 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1580 if (poll(&pfd
, 1, mpt
) < 0) {
1581 free_nfs_srvr_list(data
.srvrs
);
1582 rpc_destroy_context(rpc
);
1585 if (pfd
.revents
== 0) {
1589 if (rpc_service(rpc
, pfd
.revents
) < 0) {
1596 rpc_destroy_context(rpc
);
1598 if (data
.status
!= 0) {
1599 free_nfs_srvr_list(data
.srvrs
);