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_creat(struct nfs_context
*nfs
, const char *path
, 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_creat_async(nfs
, path
, mode
, creat_cb
, &cb_data
) != 0) {
707 nfs_set_error(nfs
, "nfs_creat_async failed");
711 wait_for_nfs_reply(nfs
, &cb_data
);
713 return cb_data
.status
;
719 static void mknod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
721 struct sync_cb_data
*cb_data
= private_data
;
723 cb_data
->is_finished
= 1;
724 cb_data
->status
= status
;
727 nfs_set_error(nfs
, "mknod call failed with \"%s\"", (char *)data
);
732 int nfs_mknod(struct nfs_context
*nfs
, const char *path
, int mode
, int dev
)
734 struct sync_cb_data cb_data
;
736 cb_data
.is_finished
= 0;
738 if (nfs_mknod_async(nfs
, path
, mode
, dev
, mknod_cb
, &cb_data
) != 0) {
739 nfs_set_error(nfs
, "nfs_creat_async failed");
743 wait_for_nfs_reply(nfs
, &cb_data
);
745 return cb_data
.status
;
752 static void unlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
754 struct sync_cb_data
*cb_data
= private_data
;
756 cb_data
->is_finished
= 1;
757 cb_data
->status
= status
;
760 nfs_set_error(nfs
, "unlink call failed with \"%s\"", (char *)data
);
765 int nfs_unlink(struct nfs_context
*nfs
, const char *path
)
767 struct sync_cb_data cb_data
;
769 cb_data
.is_finished
= 0;
771 if (nfs_unlink_async(nfs
, path
, unlink_cb
, &cb_data
) != 0) {
772 nfs_set_error(nfs
, "nfs_unlink_async failed");
776 wait_for_nfs_reply(nfs
, &cb_data
);
778 return cb_data
.status
;
786 static void opendir_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
788 struct sync_cb_data
*cb_data
= private_data
;
789 struct nfsdir
*dir
, **nfsdir
;
791 cb_data
->is_finished
= 1;
792 cb_data
->status
= status
;
795 nfs_set_error(nfs
, "opendir call failed with \"%s\"", (char *)data
);
800 nfsdir
= cb_data
->return_data
;
804 int nfs_opendir(struct nfs_context
*nfs
, const char *path
, struct nfsdir
**nfsdir
)
806 struct sync_cb_data cb_data
;
808 cb_data
.is_finished
= 0;
809 cb_data
.return_data
= nfsdir
;
811 if (nfs_opendir_async(nfs
, path
, opendir_cb
, &cb_data
) != 0) {
812 nfs_set_error(nfs
, "nfs_opendir_async failed");
816 wait_for_nfs_reply(nfs
, &cb_data
);
818 return cb_data
.status
;
825 static void lseek_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
827 struct sync_cb_data
*cb_data
= private_data
;
829 cb_data
->is_finished
= 1;
830 cb_data
->status
= status
;
833 nfs_set_error(nfs
, "lseek call failed with \"%s\"", (char *)data
);
837 if (cb_data
->return_data
!= NULL
) {
838 memcpy(cb_data
->return_data
, data
, sizeof(uint64_t));
842 int nfs_lseek(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int64_t offset
, int whence
, uint64_t *current_offset
)
844 struct sync_cb_data cb_data
;
846 cb_data
.is_finished
= 0;
847 cb_data
.return_data
= current_offset
;
849 if (nfs_lseek_async(nfs
, nfsfh
, offset
, whence
, lseek_cb
, &cb_data
) != 0) {
850 nfs_set_error(nfs
, "nfs_lseek_async failed");
854 wait_for_nfs_reply(nfs
, &cb_data
);
856 return cb_data
.status
;
864 static void statvfs_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
866 struct sync_cb_data
*cb_data
= private_data
;
868 cb_data
->is_finished
= 1;
869 cb_data
->status
= status
;
872 nfs_set_error(nfs
, "statvfs call failed with \"%s\"", (char *)data
);
876 memcpy(cb_data
->return_data
, data
, sizeof(struct statvfs
));
879 int nfs_statvfs(struct nfs_context
*nfs
, const char *path
, struct statvfs
*svfs
)
881 struct sync_cb_data cb_data
;
883 cb_data
.is_finished
= 0;
884 cb_data
.return_data
= svfs
;
886 if (nfs_statvfs_async(nfs
, path
, statvfs_cb
, &cb_data
) != 0) {
887 nfs_set_error(nfs
, "nfs_statvfs_async failed");
891 wait_for_nfs_reply(nfs
, &cb_data
);
893 return cb_data
.status
;
903 static void readlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
905 struct sync_cb_data
*cb_data
= private_data
;
907 cb_data
->is_finished
= 1;
908 cb_data
->status
= status
;
911 nfs_set_error(nfs
, "readlink call failed with \"%s\"", (char *)data
);
915 if (strlen(data
) > (size_t)cb_data
->return_int
) {
916 nfs_set_error(nfs
, "Too small buffer for readlink");
917 cb_data
->status
= -ENAMETOOLONG
;
921 memcpy(cb_data
->return_data
, data
, strlen(data
)+1);
924 int nfs_readlink(struct nfs_context
*nfs
, const char *path
, char *buf
, int bufsize
)
926 struct sync_cb_data cb_data
;
928 cb_data
.is_finished
= 0;
929 cb_data
.return_data
= buf
;
930 cb_data
.return_int
= bufsize
;
932 if (nfs_readlink_async(nfs
, path
, readlink_cb
, &cb_data
) != 0) {
933 nfs_set_error(nfs
, "nfs_readlink_async failed");
937 wait_for_nfs_reply(nfs
, &cb_data
);
939 return cb_data
.status
;
947 static void chmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
949 struct sync_cb_data
*cb_data
= private_data
;
951 cb_data
->is_finished
= 1;
952 cb_data
->status
= status
;
955 nfs_set_error(nfs
, "chmod call failed with \"%s\"", (char *)data
);
960 int nfs_chmod(struct nfs_context
*nfs
, const char *path
, int mode
)
962 struct sync_cb_data cb_data
;
964 cb_data
.is_finished
= 0;
966 if (nfs_chmod_async(nfs
, path
, mode
, chmod_cb
, &cb_data
) != 0) {
967 nfs_set_error(nfs
, "nfs_chmod_async failed");
971 wait_for_nfs_reply(nfs
, &cb_data
);
973 return cb_data
.status
;
982 static void fchmod_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
984 struct sync_cb_data
*cb_data
= private_data
;
986 cb_data
->is_finished
= 1;
987 cb_data
->status
= status
;
990 nfs_set_error(nfs
, "fchmod call failed with \"%s\"", (char *)data
);
995 int nfs_fchmod(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int mode
)
997 struct sync_cb_data cb_data
;
999 cb_data
.is_finished
= 0;
1001 if (nfs_fchmod_async(nfs
, nfsfh
, mode
, fchmod_cb
, &cb_data
) != 0) {
1002 nfs_set_error(nfs
, "nfs_fchmod_async failed");
1006 wait_for_nfs_reply(nfs
, &cb_data
);
1008 return cb_data
.status
;
1017 static void chown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1019 struct sync_cb_data
*cb_data
= private_data
;
1021 cb_data
->is_finished
= 1;
1022 cb_data
->status
= status
;
1025 nfs_set_error(nfs
, "chown call failed with \"%s\"", (char *)data
);
1030 int nfs_chown(struct nfs_context
*nfs
, const char *path
, int uid
, int gid
)
1032 struct sync_cb_data cb_data
;
1034 cb_data
.is_finished
= 0;
1036 if (nfs_chown_async(nfs
, path
, uid
, gid
, chown_cb
, &cb_data
) != 0) {
1037 nfs_set_error(nfs
, "nfs_chown_async failed");
1041 wait_for_nfs_reply(nfs
, &cb_data
);
1043 return cb_data
.status
;
1049 static void fchown_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1051 struct sync_cb_data
*cb_data
= private_data
;
1053 cb_data
->is_finished
= 1;
1054 cb_data
->status
= status
;
1057 nfs_set_error(nfs
, "fchown call failed with \"%s\"", (char *)data
);
1062 int nfs_fchown(struct nfs_context
*nfs
, struct nfsfh
*nfsfh
, int uid
, int gid
)
1064 struct sync_cb_data cb_data
;
1066 cb_data
.is_finished
= 0;
1068 if (nfs_fchown_async(nfs
, nfsfh
, uid
, gid
, fchown_cb
, &cb_data
) != 0) {
1069 nfs_set_error(nfs
, "nfs_fchown_async failed");
1073 wait_for_nfs_reply(nfs
, &cb_data
);
1075 return cb_data
.status
;
1083 static void utimes_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1085 struct sync_cb_data
*cb_data
= private_data
;
1087 cb_data
->is_finished
= 1;
1088 cb_data
->status
= status
;
1091 nfs_set_error(nfs
, "utimes call failed with \"%s\"", (char *)data
);
1096 int nfs_utimes(struct nfs_context
*nfs
, const char *path
, struct timeval
*times
)
1098 struct sync_cb_data cb_data
;
1100 cb_data
.is_finished
= 0;
1102 if (nfs_utimes_async(nfs
, path
, times
, utimes_cb
, &cb_data
) != 0) {
1103 nfs_set_error(nfs
, "nfs_utimes_async failed");
1107 wait_for_nfs_reply(nfs
, &cb_data
);
1109 return cb_data
.status
;
1117 static void utime_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1119 struct sync_cb_data
*cb_data
= private_data
;
1121 cb_data
->is_finished
= 1;
1122 cb_data
->status
= status
;
1125 nfs_set_error(nfs
, "utime call failed with \"%s\"", (char *)data
);
1130 int nfs_utime(struct nfs_context
*nfs
, const char *path
, struct utimbuf
*times
)
1132 struct sync_cb_data cb_data
;
1134 cb_data
.is_finished
= 0;
1136 if (nfs_utime_async(nfs
, path
, times
, utime_cb
, &cb_data
) != 0) {
1137 nfs_set_error(nfs
, "nfs_utimes_async failed");
1141 wait_for_nfs_reply(nfs
, &cb_data
);
1143 return cb_data
.status
;
1152 static void access_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1154 struct sync_cb_data
*cb_data
= private_data
;
1156 cb_data
->is_finished
= 1;
1157 cb_data
->status
= status
;
1160 nfs_set_error(nfs
, "access call failed with \"%s\"", (char *)data
);
1165 int nfs_access(struct nfs_context
*nfs
, const char *path
, int mode
)
1167 struct sync_cb_data cb_data
;
1169 cb_data
.is_finished
= 0;
1171 if (nfs_access_async(nfs
, path
, mode
, access_cb
, &cb_data
) != 0) {
1172 nfs_set_error(nfs
, "nfs_access_async failed");
1176 wait_for_nfs_reply(nfs
, &cb_data
);
1178 return cb_data
.status
;
1186 static void symlink_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1188 struct sync_cb_data
*cb_data
= private_data
;
1190 cb_data
->is_finished
= 1;
1191 cb_data
->status
= status
;
1194 nfs_set_error(nfs
, "symlink call failed with \"%s\"", (char *)data
);
1199 int nfs_symlink(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1201 struct sync_cb_data cb_data
;
1203 cb_data
.is_finished
= 0;
1205 if (nfs_symlink_async(nfs
, oldpath
, newpath
, symlink_cb
, &cb_data
) != 0) {
1206 nfs_set_error(nfs
, "nfs_symlink_async failed");
1210 wait_for_nfs_reply(nfs
, &cb_data
);
1212 return cb_data
.status
;
1220 static void rename_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1222 struct sync_cb_data
*cb_data
= private_data
;
1224 cb_data
->is_finished
= 1;
1225 cb_data
->status
= status
;
1228 nfs_set_error(nfs
, "rename call failed with \"%s\"", (char *)data
);
1233 int nfs_rename(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1235 struct sync_cb_data cb_data
;
1237 cb_data
.is_finished
= 0;
1239 if (nfs_rename_async(nfs
, oldpath
, newpath
, rename_cb
, &cb_data
) != 0) {
1240 nfs_set_error(nfs
, "nfs_rename_async failed");
1244 wait_for_nfs_reply(nfs
, &cb_data
);
1246 return cb_data
.status
;
1254 static void link_cb(int status
, struct nfs_context
*nfs
, void *data
, void *private_data
)
1256 struct sync_cb_data
*cb_data
= private_data
;
1258 cb_data
->is_finished
= 1;
1259 cb_data
->status
= status
;
1262 nfs_set_error(nfs
, "link call failed with \"%s\"", (char *)data
);
1267 int nfs_link(struct nfs_context
*nfs
, const char *oldpath
, const char *newpath
)
1269 struct sync_cb_data cb_data
;
1271 cb_data
.is_finished
= 0;
1273 if (nfs_link_async(nfs
, oldpath
, newpath
, link_cb
, &cb_data
) != 0) {
1274 nfs_set_error(nfs
, "nfs_link_async failed");
1278 wait_for_nfs_reply(nfs
, &cb_data
);
1280 return cb_data
.status
;
1283 void mount_getexports_cb(struct rpc_context
*mount_context
, int status
, void *data
, void *private_data
)
1285 struct sync_cb_data
*cb_data
= private_data
;
1288 assert(mount_context
->magic
== RPC_CONTEXT_MAGIC
);
1290 cb_data
->is_finished
= 1;
1291 cb_data
->status
= status
;
1292 cb_data
->return_data
= NULL
;
1295 rpc_set_error(mount_context
, "mount/export call failed with \"%s\"", (char *)data
);
1299 export
= *(exports
*)data
;
1300 while (export
!= NULL
) {
1303 new_export
= malloc(sizeof(*new_export
));
1304 memset(new_export
, 0, sizeof(*new_export
));
1305 new_export
->ex_dir
= strdup(export
->ex_dir
);
1306 new_export
->ex_next
= cb_data
->return_data
;
1308 cb_data
->return_data
= new_export
;
1310 export
= export
->ex_next
;
1314 struct exportnode
*mount_getexports(const char *server
)
1316 struct sync_cb_data cb_data
;
1317 struct rpc_context
*rpc
;
1320 cb_data
.is_finished
= 0;
1321 cb_data
.return_data
= NULL
;
1323 rpc
= rpc_init_context();
1324 if (mount_getexports_async(rpc
, server
, mount_getexports_cb
, &cb_data
) != 0) {
1325 rpc_destroy_context(rpc
);
1329 wait_for_reply(rpc
, &cb_data
);
1330 rpc_destroy_context(rpc
);
1332 return cb_data
.return_data
;
1335 void mount_free_export_list(struct exportnode
*exports
)
1337 struct exportnode
*tmp
;
1339 while ((tmp
= exports
)) {
1340 exports
= exports
->ex_next
;
1348 void free_nfs_srvr_list(struct nfs_server_list
*srv
)
1350 while (srv
!= NULL
) {
1351 struct nfs_server_list
*next
= srv
->next
;
1359 struct nfs_list_data
{
1361 struct nfs_server_list
*srvrs
;
1364 void callit_cb(struct rpc_context
*rpc
, int status
, void *data _U_
, void *private_data
)
1366 struct nfs_list_data
*srv_data
= private_data
;
1367 struct sockaddr
*sin
;
1369 struct nfs_server_list
*srvr
;
1371 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1373 if (status
== RPC_STATUS_CANCEL
) {
1377 srv_data
->status
= -1;
1381 sin
= rpc_get_recv_sockaddr(rpc
);
1383 rpc_set_error(rpc
, "failed to get sockaddr in CALLIT callback");
1384 srv_data
->status
= -1;
1388 if (getnameinfo(sin
, sizeof(struct sockaddr_in
), &hostdd
[0], sizeof(hostdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1389 rpc_set_error(rpc
, "getnameinfo failed in CALLIT callback");
1390 srv_data
->status
= -1;
1394 /* check for dupes */
1395 for (srvr
= srv_data
->srvrs
; srvr
; srvr
= srvr
->next
) {
1396 if (!strcmp(hostdd
, srvr
->addr
)) {
1401 srvr
= malloc(sizeof(struct nfs_server_list
));
1403 rpc_set_error(rpc
, "Malloc failed when allocating server structure");
1404 srv_data
->status
= -1;
1408 srvr
->addr
= strdup(hostdd
);
1409 if (srvr
->addr
== NULL
) {
1410 rpc_set_error(rpc
, "Strdup failed when allocating server structure");
1412 srv_data
->status
= -1;
1416 srvr
->next
= srv_data
->srvrs
;
1417 srv_data
->srvrs
= srvr
;
1422 static int send_nfsd_probes(struct rpc_context
*rpc
, INTERFACE_INFO
*InterfaceList
, int numIfs
, struct nfs_list_data
*data
)
1426 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1428 for(i
= 0; i
< numIfs
; i
++)
1432 unsigned long nFlags
= 0;
1434 pAddress
= (SOCKADDR
*) & (InterfaceList
[i
].iiBroadcastAddress
);
1436 if(pAddress
->sa_family
!= AF_INET
)
1439 nFlags
= InterfaceList
[i
].iiFlags
;
1441 if (!(nFlags
& IFF_UP
))
1446 if (nFlags
& IFF_LOOPBACK
)
1451 if (!(nFlags
& IFF_BROADCAST
))
1456 if (getnameinfo(pAddress
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0)
1461 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0)
1466 if (rpc_pmap2_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0)
1474 struct nfs_server_list
*nfs_find_local_servers(void)
1476 struct rpc_context
*rpc
;
1477 struct nfs_list_data data
= {0, NULL
};
1478 struct timeval tv_start
, tv_current
;
1481 INTERFACE_INFO InterfaceList
[20];
1482 unsigned long nBytesReturned
;
1483 int nNumInterfaces
= 0;
1485 rpc
= rpc_init_udp_context();
1491 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0)
1493 rpc_destroy_context(rpc
);
1497 if (WSAIoctl(rpc_get_fd(rpc
), SIO_GET_INTERFACE_LIST
, 0, 0, &InterfaceList
, sizeof(InterfaceList
), &nBytesReturned
, 0, 0) == SOCKET_ERROR
)
1502 nNumInterfaces
= nBytesReturned
/ sizeof(INTERFACE_INFO
);
1504 for (loop
=0; loop
<3; loop
++)
1506 if (send_nfsd_probes(rpc
, InterfaceList
, nNumInterfaces
, &data
) != 0)
1508 rpc_destroy_context(rpc
);
1512 win32_gettimeofday(&tv_start
, NULL
);
1517 pfd
.fd
= rpc_get_fd(rpc
);
1518 pfd
.events
= rpc_which_events(rpc
);
1520 win32_gettimeofday(&tv_current
, NULL
);
1522 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1523 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1525 if (poll(&pfd
, 1, mpt
) < 0)
1527 free_nfs_srvr_list(data
.srvrs
);
1528 rpc_destroy_context(rpc
);
1531 if (pfd
.revents
== 0)
1536 if (rpc_service(rpc
, pfd
.revents
) < 0)
1543 rpc_destroy_context(rpc
);
1545 if (data
.status
!= 0)
1547 free_nfs_srvr_list(data
.srvrs
);
1554 static int send_nfsd_probes(struct rpc_context
*rpc
, struct ifconf
*ifc
, struct nfs_list_data
*data
)
1558 assert(rpc
->magic
== RPC_CONTEXT_MAGIC
);
1560 for (ptr
=(char *)(ifc
->ifc_buf
); ptr
< (char *)(ifc
->ifc_buf
) + ifc
->ifc_len
; ) {
1564 ifr
= (struct ifreq
*)ptr
;
1565 #ifdef HAVE_SOCKADDR_LEN
1566 if (ifr
->ifr_addr
.sa_len
> sizeof(struct sockaddr
)) {
1567 ptr
+= sizeof(ifr
->ifr_name
) + ifr
->ifr_addr
.sa_len
;
1569 ptr
+= sizeof(ifr
->ifr_name
) + sizeof(struct sockaddr
);
1572 ptr
+= sizeof(struct ifreq
);
1575 if (ifr
->ifr_addr
.sa_family
!= AF_INET
) {
1578 if (ioctl(rpc_get_fd(rpc
), SIOCGIFFLAGS
, ifr
) < 0) {
1581 if (!(ifr
->ifr_flags
& IFF_UP
)) {
1584 if (ifr
->ifr_flags
& IFF_LOOPBACK
) {
1587 if (!(ifr
->ifr_flags
& IFF_BROADCAST
)) {
1590 if (ioctl(rpc_get_fd(rpc
), SIOCGIFBRDADDR
, ifr
) < 0) {
1593 if (getnameinfo(&ifr
->ifr_broadaddr
, sizeof(struct sockaddr_in
), &bcdd
[0], sizeof(bcdd
), NULL
, 0, NI_NUMERICHOST
) < 0) {
1596 if (rpc_set_udp_destination(rpc
, bcdd
, 111, 1) < 0) {
1600 if (rpc_pmap2_callit_async(rpc
, MOUNT_PROGRAM
, 2, 0, NULL
, 0, callit_cb
, data
) < 0) {
1608 struct nfs_server_list
*nfs_find_local_servers(void)
1610 struct rpc_context
*rpc
;
1611 struct nfs_list_data data
= {0, NULL
};
1612 struct timeval tv_start
, tv_current
;
1617 rpc
= rpc_init_udp_context();
1622 if (rpc_bind_udp(rpc
, "0.0.0.0", 0) < 0) {
1623 rpc_destroy_context(rpc
);
1628 /* get list of all interfaces */
1629 size
= sizeof(struct ifreq
);
1633 while(ifc
.ifc_len
> (size
- sizeof(struct ifreq
))) {
1638 ifc
.ifc_buf
= malloc(size
);
1639 memset(ifc
.ifc_buf
, 0, size
);
1640 if (ioctl(rpc_get_fd(rpc
), SIOCGIFCONF
, (caddr_t
)&ifc
) < 0) {
1641 rpc_destroy_context(rpc
);
1647 for (loop
=0; loop
<3; loop
++) {
1648 if (send_nfsd_probes(rpc
, &ifc
, &data
) != 0) {
1649 rpc_destroy_context(rpc
);
1654 gettimeofday(&tv_start
, NULL
);
1658 pfd
.fd
= rpc_get_fd(rpc
);
1659 pfd
.events
= rpc_which_events(rpc
);
1661 gettimeofday(&tv_current
, NULL
);
1663 - (tv_current
.tv_sec
*1000 + tv_current
.tv_usec
/ 1000)
1664 + (tv_start
.tv_sec
*1000 + tv_start
.tv_usec
/ 1000);
1666 if (poll(&pfd
, 1, mpt
) < 0) {
1667 free_nfs_srvr_list(data
.srvrs
);
1668 rpc_destroy_context(rpc
);
1671 if (pfd
.revents
== 0) {
1675 if (rpc_service(rpc
, pfd
.revents
) < 0) {
1682 rpc_destroy_context(rpc
);
1684 if (data
.status
!= 0) {
1685 free_nfs_srvr_list(data
.srvrs
);